isl_basic_map_apply_range: extract out isl_basic_map_check_applies_range
[isl.git] / isl_map.c
blob35125db28f6d3ea00ff01b6ff78a3eb41f4e4909
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
8 * Copyright 2018-2019 Cerebras Systems
10 * Use of this software is governed by the MIT license
12 * Written by Sven Verdoolaege, K.U.Leuven, Departement
13 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
14 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
18 * B.P. 105 - 78153 Le Chesnay, France
19 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
20 * CS 42112, 75589 Paris Cedex 12, France
21 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
24 #include <string.h>
25 #include <isl_ctx_private.h>
26 #include <isl_map_private.h>
27 #include <isl_blk.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
30 #include "isl_space_private.h"
31 #include "isl_equalities.h"
32 #include <isl_lp_private.h>
33 #include <isl_seq.h>
34 #include <isl/set.h>
35 #include <isl/map.h>
36 #include <isl_reordering.h>
37 #include "isl_sample.h"
38 #include <isl_sort.h>
39 #include "isl_tab.h"
40 #include <isl/vec.h>
41 #include <isl_mat_private.h>
42 #include <isl_vec_private.h>
43 #include <isl_dim_map.h>
44 #include <isl_local_space_private.h>
45 #include <isl_aff_private.h>
46 #include <isl_options_private.h>
47 #include <isl_morph.h>
48 #include <isl_val_private.h>
49 #include <isl_printer_private.h>
51 #include <bset_to_bmap.c>
52 #include <bset_from_bmap.c>
53 #include <set_to_map.c>
54 #include <set_from_map.c>
56 /* Treat "bset" as a basic map.
57 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
58 * this function performs a redundant cast.
60 static __isl_keep const isl_basic_map *const_bset_to_bmap(
61 __isl_keep const isl_basic_set *bset)
63 return (const isl_basic_map *) bset;
66 #undef TYPE
67 #define TYPE isl_basic_map
68 #include "has_single_reference_templ.c"
70 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
72 switch (type) {
73 case isl_dim_param: return 1;
74 case isl_dim_in: return 1 + space->nparam;
75 case isl_dim_out: return 1 + space->nparam + space->n_in;
76 default: return 0;
80 isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
81 enum isl_dim_type type)
83 if (!bmap)
84 return isl_size_error;
85 switch (type) {
86 case isl_dim_cst: return 1;
87 case isl_dim_param:
88 case isl_dim_in:
89 case isl_dim_out: return isl_space_dim(bmap->dim, type);
90 case isl_dim_div: return bmap->n_div;
91 case isl_dim_all: return isl_basic_map_total_dim(bmap);
92 default: return 0;
96 /* Return the space of "map".
98 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
100 return map ? map->dim : NULL;
103 /* Return the space of "set".
105 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
107 return isl_map_peek_space(set_to_map(set));
110 isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
112 return isl_space_dim(isl_map_peek_space(map), type);
115 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
117 return isl_map_dim(set_to_map(set), type);
120 /* Return the position of the variables of the given type
121 * within the sequence of variables of "bmap".
123 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
124 enum isl_dim_type type)
126 isl_space *space;
128 space = isl_basic_map_peek_space(bmap);
129 if (!space)
130 return isl_size_error;
132 switch (type) {
133 case isl_dim_param:
134 case isl_dim_in:
135 case isl_dim_out: return isl_space_offset(space, type);
136 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
137 case isl_dim_cst:
138 default:
139 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
140 "invalid dimension type", return isl_size_error);
144 /* Return the position of the variables of the given type
145 * within the sequence of variables of "bset".
147 isl_size isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
148 enum isl_dim_type type)
150 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
153 /* Return the position of the coefficients of the variables of the given type
154 * within the sequence of coefficients of "bmap".
156 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
157 enum isl_dim_type type)
159 switch (type) {
160 case isl_dim_cst: return 0;
161 case isl_dim_param:
162 case isl_dim_in:
163 case isl_dim_out:
164 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
165 default: return 0;
169 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
170 enum isl_dim_type type)
172 return isl_basic_map_offset(bset, type);
175 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
177 return pos(map->dim, type);
180 isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset,
181 enum isl_dim_type type)
183 return isl_basic_map_dim(bset, type);
186 isl_size isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
188 return isl_basic_set_dim(bset, isl_dim_set);
191 isl_size isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
193 return isl_basic_set_dim(bset, isl_dim_param);
196 isl_size isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
198 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
201 isl_size isl_set_n_dim(__isl_keep isl_set *set)
203 return isl_set_dim(set, isl_dim_set);
206 isl_size isl_set_n_param(__isl_keep isl_set *set)
208 return isl_set_dim(set, isl_dim_param);
211 isl_size isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
213 isl_size dim;
215 if (!bmap)
216 return isl_size_error;
217 dim = isl_space_dim(bmap->dim, isl_dim_all);
218 if (dim < 0)
219 return isl_size_error;
220 return dim + bmap->n_div;
223 /* Return the number of equality constraints in the description of "bmap".
224 * Return isl_size_error on error.
226 isl_size isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
228 if (!bmap)
229 return isl_size_error;
230 return bmap->n_eq;
233 /* Return the number of equality constraints in the description of "bset".
234 * Return isl_size_error on error.
236 isl_size isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
238 return isl_basic_map_n_equality(bset_to_bmap(bset));
241 /* Return the number of inequality constraints in the description of "bmap".
242 * Return isl_size_error on error.
244 isl_size isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
246 if (!bmap)
247 return isl_size_error;
248 return bmap->n_ineq;
251 /* Return the number of inequality constraints in the description of "bset".
252 * Return isl_size_error on error.
254 isl_size isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
256 return isl_basic_map_n_inequality(bset_to_bmap(bset));
259 /* Do "bmap1" and "bmap2" have the same parameters?
261 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
262 __isl_keep isl_basic_map *bmap2)
264 isl_space *space1, *space2;
266 space1 = isl_basic_map_peek_space(bmap1);
267 space2 = isl_basic_map_peek_space(bmap2);
268 return isl_space_has_equal_params(space1, space2);
271 /* Do "map1" and "map2" have the same parameters?
273 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
274 __isl_keep isl_map *map2)
276 isl_space *space1, *space2;
278 space1 = isl_map_peek_space(map1);
279 space2 = isl_map_peek_space(map2);
280 return isl_space_has_equal_params(space1, space2);
283 /* Do "map" and "set" have the same parameters?
285 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
286 __isl_keep isl_set *set)
288 return isl_map_has_equal_params(map, set_to_map(set));
291 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
293 static isl_bool isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map *bmap,
294 enum isl_dim_type type, __isl_keep isl_basic_set *bset)
296 isl_space *bmap_space, *bset_space;
298 bmap_space = isl_basic_map_peek_space(bmap);
299 bset_space = isl_basic_set_peek_space(bset);
300 return isl_space_tuple_is_equal(bmap_space, type,
301 bset_space, isl_dim_set);
304 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
306 static isl_bool isl_map_set_tuple_is_equal(__isl_keep isl_map *map,
307 enum isl_dim_type type, __isl_keep isl_set *set)
309 return isl_map_tuple_is_equal(map, type, set_to_map(set), isl_dim_set);
312 isl_bool isl_map_compatible_domain(__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_map_set_tuple_is_equal(map, isl_dim_in, set);
324 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
325 __isl_keep isl_basic_set *bset)
327 isl_bool m;
328 if (!bmap || !bset)
329 return isl_bool_error;
330 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
331 if (m < 0 || !m)
332 return m;
333 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_in, bset);
336 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
337 __isl_keep isl_set *set)
339 isl_bool m;
340 if (!map || !set)
341 return isl_bool_error;
342 m = isl_map_has_equal_params(map, set_to_map(set));
343 if (m < 0 || !m)
344 return m;
345 return isl_map_set_tuple_is_equal(map, isl_dim_out, set);
348 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
349 __isl_keep isl_basic_set *bset)
351 isl_bool m;
352 if (!bmap || !bset)
353 return isl_bool_error;
354 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
355 if (m < 0 || !m)
356 return m;
357 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_out, bset);
360 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
362 return bmap ? bmap->ctx : NULL;
365 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
367 return bset ? bset->ctx : NULL;
370 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
372 return map ? map->ctx : NULL;
375 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
377 return set ? set->ctx : NULL;
380 /* Return the space of "bmap".
382 __isl_keep isl_space *isl_basic_map_peek_space(
383 __isl_keep const isl_basic_map *bmap)
385 return bmap ? bmap->dim : NULL;
388 /* Return the space of "bset".
390 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
392 return isl_basic_map_peek_space(bset_to_bmap(bset));
395 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
397 return isl_space_copy(isl_basic_map_peek_space(bmap));
400 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
402 return isl_basic_map_get_space(bset_to_bmap(bset));
405 /* Return the space of "bmap".
406 * This may be either a copy or the space itself
407 * if there is only one reference to "bmap".
408 * This allows the space to be modified inplace
409 * if both the basic map and its space have only a single reference.
410 * The caller is not allowed to modify "bmap" between this call and
411 * a subsequent call to isl_basic_map_restore_space.
412 * The only exception is that isl_basic_map_free can be called instead.
414 static __isl_give isl_space *isl_basic_map_take_space(
415 __isl_keep isl_basic_map *bmap)
417 isl_space *space;
419 if (!bmap)
420 return NULL;
421 if (bmap->ref != 1)
422 return isl_basic_map_get_space(bmap);
423 space = bmap->dim;
424 bmap->dim = NULL;
425 return space;
428 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
429 * due to a preceding call to isl_basic_map_take_space.
430 * However, in this case, "bmap" only has a single reference and
431 * then the call to isl_basic_map_cow has no effect.
433 static __isl_give isl_basic_map *isl_basic_map_restore_space(
434 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
436 if (!bmap || !space)
437 goto error;
439 if (bmap->dim == space) {
440 isl_space_free(space);
441 return bmap;
444 bmap = isl_basic_map_cow(bmap);
445 if (!bmap)
446 goto error;
447 isl_space_free(bmap->dim);
448 bmap->dim = space;
450 return bmap;
451 error:
452 isl_basic_map_free(bmap);
453 isl_space_free(space);
454 return NULL;
457 /* Extract the divs in "bmap" as a matrix.
459 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
461 int i;
462 isl_ctx *ctx;
463 isl_mat *div;
464 isl_size v_div;
465 unsigned cols;
467 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
468 if (v_div < 0)
469 return NULL;
471 ctx = isl_basic_map_get_ctx(bmap);
472 cols = 1 + 1 + v_div + bmap->n_div;
473 div = isl_mat_alloc(ctx, bmap->n_div, cols);
474 if (!div)
475 return NULL;
477 for (i = 0; i < bmap->n_div; ++i)
478 isl_seq_cpy(div->row[i], bmap->div[i], cols);
480 return div;
483 /* Extract the divs in "bset" as a matrix.
485 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
487 return isl_basic_map_get_divs(bset);
490 __isl_give isl_local_space *isl_basic_map_get_local_space(
491 __isl_keep isl_basic_map *bmap)
493 isl_mat *div;
495 if (!bmap)
496 return NULL;
498 div = isl_basic_map_get_divs(bmap);
499 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
502 __isl_give isl_local_space *isl_basic_set_get_local_space(
503 __isl_keep isl_basic_set *bset)
505 return isl_basic_map_get_local_space(bset);
508 /* For each known div d = floor(f/m), add the constraints
510 * f - m d >= 0
511 * -(f-(m-1)) + m d >= 0
513 * Do not finalize the result.
515 static __isl_give isl_basic_map *add_known_div_constraints(
516 __isl_take isl_basic_map *bmap)
518 int i;
519 isl_size n_div;
521 n_div = isl_basic_map_dim(bmap, isl_dim_div);
522 if (n_div < 0)
523 return isl_basic_map_free(bmap);
524 if (n_div == 0)
525 return bmap;
526 bmap = isl_basic_map_cow(bmap);
527 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
528 if (!bmap)
529 return NULL;
530 for (i = 0; i < n_div; ++i) {
531 if (isl_int_is_zero(bmap->div[i][0]))
532 continue;
533 bmap = isl_basic_map_add_div_constraints(bmap, i);
536 return bmap;
539 __isl_give isl_basic_map *isl_basic_map_from_local_space(
540 __isl_take isl_local_space *ls)
542 int i;
543 isl_size n_div;
544 isl_basic_map *bmap;
546 n_div = isl_local_space_dim(ls, isl_dim_div);
547 if (n_div < 0)
548 ls = isl_local_space_free(ls);
549 if (!ls)
550 return NULL;
552 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
553 n_div, 0, 2 * n_div);
555 for (i = 0; i < n_div; ++i)
556 if (isl_basic_map_alloc_div(bmap) < 0)
557 goto error;
559 for (i = 0; i < n_div; ++i)
560 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
561 bmap = add_known_div_constraints(bmap);
563 isl_local_space_free(ls);
564 return bmap;
565 error:
566 isl_local_space_free(ls);
567 isl_basic_map_free(bmap);
568 return NULL;
571 __isl_give isl_basic_set *isl_basic_set_from_local_space(
572 __isl_take isl_local_space *ls)
574 return isl_basic_map_from_local_space(ls);
577 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
579 return isl_space_copy(isl_map_peek_space(map));
582 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
584 if (!set)
585 return NULL;
586 return isl_space_copy(set->dim);
589 /* Return the space of "map".
590 * This may be either a copy or the space itself
591 * if there is only one reference to "map".
592 * This allows the space to be modified inplace
593 * if both the map and its space have only a single reference.
594 * The caller is not allowed to modify "map" between this call and
595 * a subsequent call to isl_map_restore_space.
596 * The only exception is that isl_map_free can be called instead.
598 static __isl_give isl_space *isl_map_take_space(__isl_keep isl_map *map)
600 isl_space *space;
602 if (!map)
603 return NULL;
604 if (map->ref != 1)
605 return isl_map_get_space(map);
606 space = map->dim;
607 map->dim = NULL;
608 return space;
611 /* Set the space of "map" to "space", where the space of "map" may be missing
612 * due to a preceding call to isl_map_take_space.
613 * However, in this case, "map" only has a single reference and
614 * then the call to isl_map_cow has no effect.
616 static __isl_give isl_map *isl_map_restore_space(__isl_take isl_map *map,
617 __isl_take isl_space *space)
619 if (!map || !space)
620 goto error;
622 if (map->dim == space) {
623 isl_space_free(space);
624 return map;
627 map = isl_map_cow(map);
628 if (!map)
629 goto error;
630 isl_space_free(map->dim);
631 map->dim = space;
633 return map;
634 error:
635 isl_map_free(map);
636 isl_space_free(space);
637 return NULL;
640 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
641 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
643 isl_space *space;
645 space = isl_basic_map_take_space(bmap);
646 space = isl_space_set_tuple_name(space, type, s);
647 bmap = isl_basic_map_restore_space(bmap, space);
648 bmap = isl_basic_map_finalize(bmap);
649 return bmap;
652 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
653 __isl_take isl_basic_set *bset, const char *s)
655 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
658 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
659 enum isl_dim_type type)
661 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
664 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
665 enum isl_dim_type type, const char *s)
667 int i;
668 isl_space *space;
670 map = isl_map_cow(map);
671 if (!map)
672 return NULL;
674 for (i = 0; i < map->n; ++i) {
675 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
676 if (!map->p[i])
677 goto error;
680 space = isl_map_take_space(map);
681 space = isl_space_set_tuple_name(space, type, s);
682 map = isl_map_restore_space(map, space);
684 return map;
685 error:
686 isl_map_free(map);
687 return NULL;
690 /* Replace the identifier of the tuple of type "type" by "id".
692 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
693 __isl_take isl_basic_map *bmap,
694 enum isl_dim_type type, __isl_take isl_id *id)
696 isl_space *space;
698 space = isl_basic_map_take_space(bmap);
699 space = isl_space_set_tuple_id(space, type, id);
700 bmap = isl_basic_map_restore_space(bmap, space);
701 bmap = isl_basic_map_finalize(bmap);
702 return bmap;
705 /* Replace the identifier of the tuple by "id".
707 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
708 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
710 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
713 /* Does the input or output tuple have a name?
715 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
717 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
720 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
721 enum isl_dim_type type)
723 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
726 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
727 const char *s)
729 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
730 isl_dim_set, s));
733 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
734 enum isl_dim_type type, __isl_take isl_id *id)
736 isl_space *space;
738 space = isl_map_take_space(map);
739 space = isl_space_set_tuple_id(space, type, id);
740 map = isl_map_restore_space(map, space);
742 return isl_map_reset_space(map, isl_map_get_space(map));
745 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
746 __isl_take isl_id *id)
748 return isl_map_set_tuple_id(set, isl_dim_set, id);
751 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
752 enum isl_dim_type type)
754 isl_space *space;
756 space = isl_map_take_space(map);
757 space = isl_space_reset_tuple_id(space, type);
758 map = isl_map_restore_space(map, space);
760 return isl_map_reset_space(map, isl_map_get_space(map));
763 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
765 return isl_map_reset_tuple_id(set, isl_dim_set);
768 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
770 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
773 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
774 enum isl_dim_type type)
776 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
779 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
781 return isl_map_has_tuple_id(set, isl_dim_set);
784 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
786 return isl_map_get_tuple_id(set, isl_dim_set);
789 /* Does the set tuple have a name?
791 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
793 if (!set)
794 return isl_bool_error;
795 return isl_space_has_tuple_name(set->dim, isl_dim_set);
799 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
801 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
804 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
806 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
809 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
810 enum isl_dim_type type, unsigned pos)
812 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
815 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
816 enum isl_dim_type type, unsigned pos)
818 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
821 /* Does the given dimension have a name?
823 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
824 enum isl_dim_type type, unsigned pos)
826 if (!map)
827 return isl_bool_error;
828 return isl_space_has_dim_name(map->dim, type, pos);
831 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
832 enum isl_dim_type type, unsigned pos)
834 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
837 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
838 enum isl_dim_type type, unsigned pos)
840 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
843 /* Does the given dimension have a name?
845 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
846 enum isl_dim_type type, unsigned pos)
848 if (!set)
849 return isl_bool_error;
850 return isl_space_has_dim_name(set->dim, type, pos);
853 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
854 __isl_take isl_basic_map *bmap,
855 enum isl_dim_type type, unsigned pos, const char *s)
857 isl_space *space;
859 space = isl_basic_map_take_space(bmap);
860 space = isl_space_set_dim_name(space, type, pos, s);
861 bmap = isl_basic_map_restore_space(bmap, space);
862 return isl_basic_map_finalize(bmap);
865 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
866 enum isl_dim_type type, unsigned pos, const char *s)
868 int i;
869 isl_space *space;
871 map = isl_map_cow(map);
872 if (!map)
873 return NULL;
875 for (i = 0; i < map->n; ++i) {
876 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
877 if (!map->p[i])
878 goto error;
881 space = isl_map_take_space(map);
882 space = isl_space_set_dim_name(space, type, pos, s);
883 map = isl_map_restore_space(map, space);
885 return map;
886 error:
887 isl_map_free(map);
888 return NULL;
891 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
892 __isl_take isl_basic_set *bset,
893 enum isl_dim_type type, unsigned pos, const char *s)
895 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
896 type, pos, s));
899 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
900 enum isl_dim_type type, unsigned pos, const char *s)
902 return set_from_map(isl_map_set_dim_name(set_to_map(set),
903 type, pos, s));
906 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
907 enum isl_dim_type type, unsigned pos)
909 if (!bmap)
910 return isl_bool_error;
911 return isl_space_has_dim_id(bmap->dim, type, pos);
914 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
915 enum isl_dim_type type, unsigned pos)
917 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
920 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
921 enum isl_dim_type type, unsigned pos)
923 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
926 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
927 enum isl_dim_type type, unsigned pos)
929 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
932 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
933 enum isl_dim_type type, unsigned pos)
935 return isl_map_has_dim_id(set, type, pos);
938 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
939 enum isl_dim_type type, unsigned pos)
941 return isl_map_get_dim_id(set, type, pos);
944 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
945 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
947 isl_space *space;
949 space = isl_map_take_space(map);
950 space = isl_space_set_dim_id(space, type, pos, id);
951 map = isl_map_restore_space(map, space);
953 return isl_map_reset_space(map, isl_map_get_space(map));
956 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
957 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
959 return isl_map_set_dim_id(set, type, pos, id);
962 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
963 __isl_keep isl_id *id)
965 if (!map)
966 return -1;
967 return isl_space_find_dim_by_id(map->dim, type, id);
970 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
971 __isl_keep isl_id *id)
973 return isl_map_find_dim_by_id(set, type, id);
976 /* Return the position of the dimension of the given type and name
977 * in "bmap".
978 * Return -1 if no such dimension can be found.
980 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
981 enum isl_dim_type type, const char *name)
983 if (!bmap)
984 return -1;
985 return isl_space_find_dim_by_name(bmap->dim, type, name);
988 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
989 const char *name)
991 if (!map)
992 return -1;
993 return isl_space_find_dim_by_name(map->dim, type, name);
996 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
997 const char *name)
999 return isl_map_find_dim_by_name(set, type, name);
1002 /* Check whether equality i of bset is a pure stride constraint
1003 * on a single dimension, i.e., of the form
1005 * v = k e
1007 * with k a constant and e an existentially quantified variable.
1009 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
1011 isl_size nparam;
1012 isl_size d;
1013 isl_size n_div;
1014 int pos1;
1015 int pos2;
1017 nparam = isl_basic_set_dim(bset, isl_dim_param);
1018 d = isl_basic_set_dim(bset, isl_dim_set);
1019 n_div = isl_basic_set_dim(bset, isl_dim_div);
1020 if (nparam < 0 || d < 0 || n_div < 0)
1021 return isl_bool_error;
1023 if (!isl_int_is_zero(bset->eq[i][0]))
1024 return isl_bool_false;
1026 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
1027 return isl_bool_false;
1028 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
1029 if (pos1 == -1)
1030 return isl_bool_false;
1031 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
1032 d - pos1 - 1) != -1)
1033 return isl_bool_false;
1035 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
1036 if (pos2 == -1)
1037 return isl_bool_false;
1038 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
1039 n_div - pos2 - 1) != -1)
1040 return isl_bool_false;
1041 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
1042 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
1043 return isl_bool_false;
1045 return isl_bool_true;
1048 /* Reset the user pointer on all identifiers of parameters and tuples
1049 * of the space of "map".
1051 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
1053 isl_space *space;
1055 space = isl_map_get_space(map);
1056 space = isl_space_reset_user(space);
1057 map = isl_map_reset_space(map, space);
1059 return map;
1062 /* Reset the user pointer on all identifiers of parameters and tuples
1063 * of the space of "set".
1065 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
1067 return isl_map_reset_user(set);
1070 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
1072 if (!bmap)
1073 return isl_bool_error;
1074 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1077 /* Has "map" been marked as a rational map?
1078 * In particular, have all basic maps in "map" been marked this way?
1079 * An empty map is not considered to be rational.
1080 * Maps where only some of the basic maps are marked rational
1081 * are not allowed.
1083 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1085 int i;
1086 isl_bool rational;
1088 if (!map)
1089 return isl_bool_error;
1090 if (map->n == 0)
1091 return isl_bool_false;
1092 rational = isl_basic_map_is_rational(map->p[0]);
1093 if (rational < 0)
1094 return rational;
1095 for (i = 1; i < map->n; ++i) {
1096 isl_bool rational_i;
1098 rational_i = isl_basic_map_is_rational(map->p[i]);
1099 if (rational_i < 0)
1100 return rational_i;
1101 if (rational != rational_i)
1102 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1103 "mixed rational and integer basic maps "
1104 "not supported", return isl_bool_error);
1107 return rational;
1110 /* Has "set" been marked as a rational set?
1111 * In particular, have all basic set in "set" been marked this way?
1112 * An empty set is not considered to be rational.
1113 * Sets where only some of the basic sets are marked rational
1114 * are not allowed.
1116 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1118 return isl_map_is_rational(set);
1121 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1123 return isl_basic_map_is_rational(bset);
1126 /* Does "bmap" contain any rational points?
1128 * If "bmap" has an equality for each dimension, equating the dimension
1129 * to an integer constant, then it has no rational points, even if it
1130 * is marked as rational.
1132 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1134 isl_bool has_rational = isl_bool_true;
1135 isl_size total;
1137 if (!bmap)
1138 return isl_bool_error;
1139 if (isl_basic_map_plain_is_empty(bmap))
1140 return isl_bool_false;
1141 if (!isl_basic_map_is_rational(bmap))
1142 return isl_bool_false;
1143 bmap = isl_basic_map_copy(bmap);
1144 bmap = isl_basic_map_implicit_equalities(bmap);
1145 total = isl_basic_map_dim(bmap, isl_dim_all);
1146 if (total < 0)
1147 return isl_bool_error;
1148 if (bmap->n_eq == total) {
1149 int i, j;
1150 for (i = 0; i < bmap->n_eq; ++i) {
1151 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1152 if (j < 0)
1153 break;
1154 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1155 !isl_int_is_negone(bmap->eq[i][1 + j]))
1156 break;
1157 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1158 total - j - 1);
1159 if (j >= 0)
1160 break;
1162 if (i == bmap->n_eq)
1163 has_rational = isl_bool_false;
1165 isl_basic_map_free(bmap);
1167 return has_rational;
1170 /* Does "map" contain any rational points?
1172 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1174 int i;
1175 isl_bool has_rational;
1177 if (!map)
1178 return isl_bool_error;
1179 for (i = 0; i < map->n; ++i) {
1180 has_rational = isl_basic_map_has_rational(map->p[i]);
1181 if (has_rational < 0 || has_rational)
1182 return has_rational;
1184 return isl_bool_false;
1187 /* Does "set" contain any rational points?
1189 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1191 return isl_map_has_rational(set);
1194 /* Is this basic set a parameter domain?
1196 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1198 if (!bset)
1199 return isl_bool_error;
1200 return isl_space_is_params(bset->dim);
1203 /* Is this set a parameter domain?
1205 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1207 if (!set)
1208 return isl_bool_error;
1209 return isl_space_is_params(set->dim);
1212 /* Is this map actually a parameter domain?
1213 * Users should never call this function. Outside of isl,
1214 * a map can never be a parameter domain.
1216 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1218 if (!map)
1219 return isl_bool_error;
1220 return isl_space_is_params(map->dim);
1223 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1224 __isl_take isl_basic_map *bmap, unsigned extra,
1225 unsigned n_eq, unsigned n_ineq)
1227 int i;
1228 isl_space *space = isl_basic_map_peek_space(bmap);
1229 isl_size n_var = isl_space_dim(space, isl_dim_all);
1230 size_t row_size = 1 + n_var + extra;
1232 bmap->ctx = ctx;
1233 isl_ctx_ref(ctx);
1235 if (n_var < 0)
1236 return isl_basic_map_free(bmap);
1238 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1239 if (isl_blk_is_error(bmap->block))
1240 goto error;
1242 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1243 if ((n_ineq + n_eq) && !bmap->ineq)
1244 goto error;
1246 if (extra == 0) {
1247 bmap->block2 = isl_blk_empty();
1248 bmap->div = NULL;
1249 } else {
1250 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1251 if (isl_blk_is_error(bmap->block2))
1252 goto error;
1254 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1255 if (!bmap->div)
1256 goto error;
1259 for (i = 0; i < n_ineq + n_eq; ++i)
1260 bmap->ineq[i] = bmap->block.data + i * row_size;
1262 for (i = 0; i < extra; ++i)
1263 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1265 bmap->ref = 1;
1266 bmap->flags = 0;
1267 bmap->c_size = n_eq + n_ineq;
1268 bmap->eq = bmap->ineq + n_ineq;
1269 bmap->extra = extra;
1270 bmap->n_eq = 0;
1271 bmap->n_ineq = 0;
1272 bmap->n_div = 0;
1273 bmap->sample = NULL;
1275 return bmap;
1276 error:
1277 isl_basic_map_free(bmap);
1278 return NULL;
1281 __isl_give isl_basic_set *isl_basic_set_alloc(isl_ctx *ctx,
1282 unsigned nparam, unsigned dim, unsigned extra,
1283 unsigned n_eq, unsigned n_ineq)
1285 struct isl_basic_map *bmap;
1286 isl_space *space;
1288 space = isl_space_set_alloc(ctx, nparam, dim);
1289 if (!space)
1290 return NULL;
1292 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1293 return bset_from_bmap(bmap);
1296 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *space,
1297 unsigned extra, unsigned n_eq, unsigned n_ineq)
1299 struct isl_basic_map *bmap;
1300 if (!space)
1301 return NULL;
1302 isl_assert(space->ctx, space->n_in == 0, goto error);
1303 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1304 return bset_from_bmap(bmap);
1305 error:
1306 isl_space_free(space);
1307 return NULL;
1310 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1311 unsigned extra, unsigned n_eq, unsigned n_ineq)
1313 struct isl_basic_map *bmap;
1315 if (!space)
1316 return NULL;
1317 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1318 if (!bmap)
1319 goto error;
1320 bmap->dim = space;
1322 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1323 error:
1324 isl_space_free(space);
1325 return NULL;
1328 __isl_give isl_basic_map *isl_basic_map_alloc(isl_ctx *ctx,
1329 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1330 unsigned n_eq, unsigned n_ineq)
1332 struct isl_basic_map *bmap;
1333 isl_space *space;
1335 space = isl_space_alloc(ctx, nparam, in, out);
1336 if (!space)
1337 return NULL;
1339 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1340 return bmap;
1343 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1344 __isl_keep isl_basic_map *src)
1346 int i;
1347 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1349 if (!dst || total < 0)
1350 return isl_basic_map_free(dst);
1352 for (i = 0; i < src->n_eq; ++i) {
1353 int j = isl_basic_map_alloc_equality(dst);
1354 if (j < 0)
1355 return isl_basic_map_free(dst);
1356 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1359 for (i = 0; i < src->n_ineq; ++i) {
1360 int j = isl_basic_map_alloc_inequality(dst);
1361 if (j < 0)
1362 return isl_basic_map_free(dst);
1363 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1366 for (i = 0; i < src->n_div; ++i) {
1367 int j = isl_basic_map_alloc_div(dst);
1368 if (j < 0)
1369 return isl_basic_map_free(dst);
1370 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1372 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1373 return dst;
1376 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1378 struct isl_basic_map *dup;
1380 if (!bmap)
1381 return NULL;
1382 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1383 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1384 dup = dup_constraints(dup, bmap);
1385 if (!dup)
1386 return NULL;
1387 dup->flags = bmap->flags;
1388 dup->sample = isl_vec_copy(bmap->sample);
1389 return dup;
1392 __isl_give isl_basic_set *isl_basic_set_dup(__isl_keep isl_basic_set *bset)
1394 struct isl_basic_map *dup;
1396 dup = isl_basic_map_dup(bset_to_bmap(bset));
1397 return bset_from_bmap(dup);
1400 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1402 if (!bset)
1403 return NULL;
1405 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1406 bset->ref++;
1407 return bset;
1409 return isl_basic_set_dup(bset);
1412 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1414 if (!set)
1415 return NULL;
1417 set->ref++;
1418 return set;
1421 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1423 if (!bmap)
1424 return NULL;
1426 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1427 bmap->ref++;
1428 return bmap;
1430 bmap = isl_basic_map_dup(bmap);
1431 if (bmap)
1432 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1433 return bmap;
1436 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1438 if (!map)
1439 return NULL;
1441 map->ref++;
1442 return map;
1445 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1447 if (!bmap)
1448 return NULL;
1450 if (--bmap->ref > 0)
1451 return NULL;
1453 isl_ctx_deref(bmap->ctx);
1454 free(bmap->div);
1455 isl_blk_free(bmap->ctx, bmap->block2);
1456 free(bmap->ineq);
1457 isl_blk_free(bmap->ctx, bmap->block);
1458 isl_vec_free(bmap->sample);
1459 isl_space_free(bmap->dim);
1460 free(bmap);
1462 return NULL;
1465 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1467 return isl_basic_map_free(bset_to_bmap(bset));
1470 static int room_for_con(__isl_keep isl_basic_map *bmap, unsigned n)
1472 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1475 /* Check that "bset" does not involve any parameters.
1477 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1479 isl_size nparam;
1481 nparam = isl_basic_set_dim(bset, isl_dim_param);
1482 if (nparam < 0)
1483 return isl_stat_error;
1484 if (nparam != 0)
1485 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1486 "basic set should not have any parameters",
1487 return isl_stat_error);
1488 return isl_stat_ok;
1491 /* Check that "bset" does not involve any local variables.
1493 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1495 isl_size n_div;
1497 n_div = isl_basic_set_dim(bset, isl_dim_div);
1498 if (n_div < 0)
1499 return isl_stat_error;
1500 if (n_div != 0)
1501 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1502 "basic set should not have any local variables",
1503 return isl_stat_error);
1504 return isl_stat_ok;
1507 #undef TYPE
1508 #define TYPE isl_map
1510 #include "isl_check_named_params_templ.c"
1512 #undef TYPE
1513 #define TYPE isl_basic_map
1515 static
1516 #include "isl_check_named_params_templ.c"
1518 /* Check that "bmap1" and "bmap2" have the same parameters,
1519 * reporting an error if they do not.
1521 static isl_stat isl_basic_map_check_equal_params(
1522 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1524 isl_bool match;
1526 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1527 if (match < 0)
1528 return isl_stat_error;
1529 if (!match)
1530 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1531 "parameters don't match", return isl_stat_error);
1532 return isl_stat_ok;
1535 #undef TYPE
1536 #define TYPE isl_map
1538 #include "isl_align_params_bin_templ.c"
1540 #undef SUFFIX
1541 #define SUFFIX set
1542 #undef ARG1
1543 #define ARG1 isl_map
1544 #undef ARG2
1545 #define ARG2 isl_set
1547 #include "isl_align_params_templ.c"
1549 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1550 __isl_keep isl_map *map2,
1551 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1553 isl_bool r;
1555 if (!map1 || !map2)
1556 return isl_bool_error;
1557 if (isl_map_has_equal_params(map1, map2))
1558 return fn(map1, map2);
1559 if (isl_map_check_named_params(map1) < 0)
1560 return isl_bool_error;
1561 if (isl_map_check_named_params(map2) < 0)
1562 return isl_bool_error;
1563 map1 = isl_map_copy(map1);
1564 map2 = isl_map_copy(map2);
1565 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1566 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1567 r = fn(map1, map2);
1568 isl_map_free(map1);
1569 isl_map_free(map2);
1570 return r;
1573 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map *bmap)
1575 isl_size total;
1576 struct isl_ctx *ctx;
1578 total = isl_basic_map_dim(bmap, isl_dim_all);
1579 if (total < 0)
1580 return -1;
1581 ctx = bmap->ctx;
1582 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1583 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1584 return -1);
1585 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1586 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1587 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1588 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1589 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1590 isl_int *t;
1591 int j = isl_basic_map_alloc_inequality(bmap);
1592 if (j < 0)
1593 return -1;
1594 t = bmap->ineq[j];
1595 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1596 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1597 bmap->eq[-1] = t;
1598 bmap->n_eq++;
1599 bmap->n_ineq--;
1600 bmap->eq--;
1601 return 0;
1603 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1604 bmap->extra - bmap->n_div);
1605 return bmap->n_eq++;
1608 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set *bset)
1610 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1613 __isl_give isl_basic_map *isl_basic_map_free_equality(
1614 __isl_take isl_basic_map *bmap, unsigned n)
1616 if (!bmap)
1617 return NULL;
1618 if (n > bmap->n_eq)
1619 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1620 "invalid number of equalities",
1621 isl_basic_map_free(bmap));
1622 bmap->n_eq -= n;
1623 return bmap;
1626 __isl_give isl_basic_set *isl_basic_set_free_equality(
1627 __isl_take isl_basic_set *bset, unsigned n)
1629 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset),
1630 n));
1633 /* Drop the equality constraint at position "pos",
1634 * preserving the order of the other equality constraints.
1636 int isl_basic_map_drop_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
1638 isl_int *t;
1639 int r;
1641 if (!bmap)
1642 return -1;
1643 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1645 t = bmap->eq[pos];
1646 bmap->n_eq--;
1647 for (r = pos; r < bmap->n_eq; ++r)
1648 bmap->eq[r] = bmap->eq[r + 1];
1649 bmap->eq[bmap->n_eq] = t;
1651 return 0;
1654 /* Turn inequality "pos" of "bmap" into an equality.
1656 * In particular, we move the inequality in front of the equalities
1657 * and move the last inequality in the position of the moved inequality.
1658 * Note that isl_tab_make_equalities_explicit depends on this particular
1659 * change in the ordering of the constraints.
1661 void isl_basic_map_inequality_to_equality(
1662 __isl_keep isl_basic_map *bmap, unsigned pos)
1664 isl_int *t;
1666 t = bmap->ineq[pos];
1667 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1668 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1669 bmap->eq[-1] = t;
1670 bmap->n_eq++;
1671 bmap->n_ineq--;
1672 bmap->eq--;
1673 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1674 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1675 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1676 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1679 static int room_for_ineq(__isl_keep isl_basic_map *bmap, unsigned n)
1681 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1684 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1686 isl_size total;
1687 struct isl_ctx *ctx;
1689 total = isl_basic_map_dim(bmap, isl_dim_all);
1690 if (total < 0)
1691 return -1;
1692 ctx = bmap->ctx;
1693 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1694 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1695 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1696 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1697 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1698 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1699 bmap->extra - bmap->n_div);
1700 return bmap->n_ineq++;
1703 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1705 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1708 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1709 __isl_take isl_basic_map *bmap, unsigned n)
1711 if (!bmap)
1712 return NULL;
1713 if (n > bmap->n_ineq)
1714 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1715 "invalid number of inequalities",
1716 return isl_basic_map_free(bmap));
1717 bmap->n_ineq -= n;
1718 return bmap;
1721 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1722 __isl_take isl_basic_set *bset, unsigned n)
1724 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1725 n));
1728 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1730 isl_int *t;
1731 if (!bmap)
1732 return -1;
1733 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1735 if (pos != bmap->n_ineq - 1) {
1736 t = bmap->ineq[pos];
1737 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1738 bmap->ineq[bmap->n_ineq - 1] = t;
1739 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1741 bmap->n_ineq--;
1742 return 0;
1745 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1747 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1750 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1751 isl_int *eq)
1753 isl_bool empty;
1754 isl_size total;
1755 int k;
1757 empty = isl_basic_map_plain_is_empty(bmap);
1758 if (empty < 0)
1759 return isl_basic_map_free(bmap);
1760 if (empty)
1761 return bmap;
1763 bmap = isl_basic_map_cow(bmap);
1764 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1765 total = isl_basic_map_dim(bmap, isl_dim_all);
1766 if (total < 0)
1767 return isl_basic_map_free(bmap);
1768 k = isl_basic_map_alloc_equality(bmap);
1769 if (k < 0)
1770 goto error;
1771 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1772 return bmap;
1773 error:
1774 isl_basic_map_free(bmap);
1775 return NULL;
1778 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1779 isl_int *eq)
1781 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1784 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1785 isl_int *ineq)
1787 isl_size total;
1788 int k;
1790 bmap = isl_basic_map_cow(bmap);
1791 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1792 total = isl_basic_map_dim(bmap, isl_dim_all);
1793 if (total < 0)
1794 return isl_basic_map_free(bmap);
1795 k = isl_basic_map_alloc_inequality(bmap);
1796 if (k < 0)
1797 goto error;
1798 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1799 return bmap;
1800 error:
1801 isl_basic_map_free(bmap);
1802 return NULL;
1805 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1806 isl_int *ineq)
1808 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1811 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1813 isl_size total;
1815 total = isl_basic_map_dim(bmap, isl_dim_all);
1816 if (total < 0)
1817 return -1;
1818 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1819 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1820 bmap->extra - bmap->n_div);
1821 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1822 return bmap->n_div++;
1825 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1827 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1830 #undef TYPE
1831 #define TYPE isl_basic_map
1832 #include "check_type_range_templ.c"
1834 /* Check that there are "n" dimensions of type "type" starting at "first"
1835 * in "bset".
1837 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1838 enum isl_dim_type type, unsigned first, unsigned n)
1840 return isl_basic_map_check_range(bset_to_bmap(bset),
1841 type, first, n);
1844 /* Insert an extra integer division, prescribed by "div", to "bmap"
1845 * at (integer division) position "pos".
1847 * The integer division is first added at the end and then moved
1848 * into the right position.
1850 __isl_give isl_basic_map *isl_basic_map_insert_div(
1851 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1853 int i, k;
1854 isl_size total;
1856 bmap = isl_basic_map_cow(bmap);
1857 total = isl_basic_map_dim(bmap, isl_dim_all);
1858 if (total < 0 || !div)
1859 return isl_basic_map_free(bmap);
1861 if (div->size != 1 + 1 + total)
1862 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1863 "unexpected size", return isl_basic_map_free(bmap));
1864 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1865 return isl_basic_map_free(bmap);
1867 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1868 k = isl_basic_map_alloc_div(bmap);
1869 if (k < 0)
1870 return isl_basic_map_free(bmap);
1871 isl_seq_cpy(bmap->div[k], div->el, div->size);
1872 isl_int_set_si(bmap->div[k][div->size], 0);
1874 for (i = k; i > pos; --i)
1875 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1877 return bmap;
1880 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1882 if (!bmap)
1883 return isl_stat_error;
1884 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1885 bmap->n_div -= n;
1886 return isl_stat_ok;
1889 static __isl_give isl_basic_map *add_constraints(
1890 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1891 unsigned i_pos, unsigned o_pos)
1893 isl_size total, n_param, n_in, n_out, n_div;
1894 unsigned o_in, o_out;
1895 isl_ctx *ctx;
1896 isl_space *space;
1897 struct isl_dim_map *dim_map;
1899 space = isl_basic_map_peek_space(bmap2);
1900 if (!bmap1 || !space)
1901 goto error;
1903 total = isl_basic_map_dim(bmap1, isl_dim_all);
1904 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1905 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1906 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1907 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1908 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1909 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1910 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1911 goto error;
1912 ctx = isl_basic_map_get_ctx(bmap1);
1913 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1914 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1915 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1916 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1917 isl_dim_map_div(dim_map, bmap2, total);
1919 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1920 error:
1921 isl_basic_map_free(bmap1);
1922 isl_basic_map_free(bmap2);
1923 return NULL;
1926 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1927 unsigned extra, unsigned n_eq, unsigned n_ineq)
1929 isl_space *space;
1930 struct isl_basic_map *ext;
1931 unsigned flags;
1932 int dims_ok;
1934 if (!base)
1935 goto error;
1937 dims_ok = base->extra >= base->n_div + extra;
1939 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1940 room_for_ineq(base, n_ineq))
1941 return base;
1943 extra += base->extra;
1944 n_eq += base->n_eq;
1945 n_ineq += base->n_ineq;
1947 space = isl_basic_map_get_space(base);
1948 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1949 if (!ext)
1950 goto error;
1952 if (dims_ok)
1953 ext->sample = isl_vec_copy(base->sample);
1954 flags = base->flags;
1955 ext = add_constraints(ext, base, 0, 0);
1956 if (ext) {
1957 ext->flags = flags;
1958 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1961 return ext;
1963 error:
1964 isl_basic_map_free(base);
1965 return NULL;
1968 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
1969 unsigned extra, unsigned n_eq, unsigned n_ineq)
1971 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1972 extra, n_eq, n_ineq));
1975 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
1976 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1978 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
1981 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
1982 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1984 isl_basic_map *bmap = bset_to_bmap(base);
1985 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1986 return bset_from_bmap(bmap);
1989 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1991 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1994 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1996 if (!bmap)
1997 return NULL;
1999 if (bmap->ref > 1) {
2000 bmap->ref--;
2001 bmap = isl_basic_map_dup(bmap);
2003 if (bmap) {
2004 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
2005 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2007 return bmap;
2010 /* Clear all cached information in "map", either because it is about
2011 * to be modified or because it is being freed.
2012 * Always return the same pointer that is passed in.
2013 * This is needed for the use in isl_map_free.
2015 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2017 isl_basic_map_free(map->cached_simple_hull[0]);
2018 isl_basic_map_free(map->cached_simple_hull[1]);
2019 map->cached_simple_hull[0] = NULL;
2020 map->cached_simple_hull[1] = NULL;
2021 return map;
2024 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2026 return isl_map_cow(set);
2029 /* Return an isl_map that is equal to "map" and that has only
2030 * a single reference.
2032 * If the original input already has only one reference, then
2033 * simply return it, but clear all cached information, since
2034 * it may be rendered invalid by the operations that will be
2035 * performed on the result.
2037 * Otherwise, create a duplicate (without any cached information).
2039 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2041 if (!map)
2042 return NULL;
2044 if (map->ref == 1)
2045 return clear_caches(map);
2046 map->ref--;
2047 return isl_map_dup(map);
2050 static void swap_vars(struct isl_blk blk, isl_int *a,
2051 unsigned a_len, unsigned b_len)
2053 isl_seq_cpy(blk.data, a+a_len, b_len);
2054 isl_seq_cpy(blk.data+b_len, a, a_len);
2055 isl_seq_cpy(a, blk.data, b_len+a_len);
2058 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2059 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2061 int i;
2062 struct isl_blk blk;
2064 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2065 goto error;
2067 if (n1 == 0 || n2 == 0)
2068 return bmap;
2070 bmap = isl_basic_map_cow(bmap);
2071 if (!bmap)
2072 return NULL;
2074 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2075 if (isl_blk_is_error(blk))
2076 goto error;
2078 for (i = 0; i < bmap->n_eq; ++i)
2079 swap_vars(blk,
2080 bmap->eq[i] + pos, n1, n2);
2082 for (i = 0; i < bmap->n_ineq; ++i)
2083 swap_vars(blk,
2084 bmap->ineq[i] + pos, n1, n2);
2086 for (i = 0; i < bmap->n_div; ++i)
2087 swap_vars(blk,
2088 bmap->div[i]+1 + pos, n1, n2);
2090 isl_blk_free(bmap->ctx, blk);
2092 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2093 bmap = isl_basic_map_gauss(bmap, NULL);
2094 return isl_basic_map_finalize(bmap);
2095 error:
2096 isl_basic_map_free(bmap);
2097 return NULL;
2100 /* The given basic map has turned out to be empty.
2101 * Explicitly mark it as such and change the representation
2102 * to a canonical representation of the empty basic map.
2103 * Since the basic map has conflicting constraints,
2104 * it must have at least one constraint, except perhaps
2105 * if it was already explicitly marked as being empty.
2106 * Do nothing in the latter case.
2108 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2109 __isl_take isl_basic_map *bmap)
2111 int i = 0;
2112 isl_bool empty;
2113 isl_size total;
2115 empty = isl_basic_map_plain_is_empty(bmap);
2116 if (empty < 0)
2117 return isl_basic_map_free(bmap);
2118 if (empty)
2119 return bmap;
2120 total = isl_basic_map_dim(bmap, isl_dim_all);
2121 if (total < 0)
2122 return isl_basic_map_free(bmap);
2123 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2124 return isl_basic_map_free(bmap);
2125 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2126 if (!bmap)
2127 return NULL;
2128 if (bmap->n_eq > 0) {
2129 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2130 if (!bmap)
2131 return NULL;
2132 } else {
2133 i = isl_basic_map_alloc_equality(bmap);
2134 if (i < 0)
2135 goto error;
2137 isl_int_set_si(bmap->eq[i][0], 1);
2138 isl_seq_clr(bmap->eq[i]+1, total);
2139 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2140 isl_vec_free(bmap->sample);
2141 bmap->sample = NULL;
2142 return isl_basic_map_finalize(bmap);
2143 error:
2144 isl_basic_map_free(bmap);
2145 return NULL;
2148 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2149 __isl_take isl_basic_set *bset)
2151 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2154 __isl_give isl_basic_map *isl_basic_map_set_rational(
2155 __isl_take isl_basic_map *bmap)
2157 if (!bmap)
2158 return NULL;
2160 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2161 return bmap;
2163 bmap = isl_basic_map_cow(bmap);
2164 if (!bmap)
2165 return NULL;
2167 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2169 return isl_basic_map_finalize(bmap);
2172 __isl_give isl_basic_set *isl_basic_set_set_rational(
2173 __isl_take isl_basic_set *bset)
2175 return isl_basic_map_set_rational(bset);
2178 __isl_give isl_basic_set *isl_basic_set_set_integral(
2179 __isl_take isl_basic_set *bset)
2181 if (!bset)
2182 return NULL;
2184 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2185 return bset;
2187 bset = isl_basic_set_cow(bset);
2188 if (!bset)
2189 return NULL;
2191 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2193 return isl_basic_set_finalize(bset);
2196 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2198 int i;
2200 map = isl_map_cow(map);
2201 if (!map)
2202 return NULL;
2203 for (i = 0; i < map->n; ++i) {
2204 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2205 if (!map->p[i])
2206 goto error;
2208 return map;
2209 error:
2210 isl_map_free(map);
2211 return NULL;
2214 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2216 return isl_map_set_rational(set);
2219 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2220 * of "bmap").
2222 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2224 isl_int *t = bmap->div[a];
2225 bmap->div[a] = bmap->div[b];
2226 bmap->div[b] = t;
2229 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2230 * div definitions accordingly.
2232 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2233 int a, int b)
2235 int i;
2236 isl_size off;
2238 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2239 if (off < 0)
2240 return isl_basic_map_free(bmap);
2242 swap_div(bmap, a, b);
2244 for (i = 0; i < bmap->n_eq; ++i)
2245 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2247 for (i = 0; i < bmap->n_ineq; ++i)
2248 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2250 for (i = 0; i < bmap->n_div; ++i)
2251 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2252 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2254 return bmap;
2257 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2259 isl_seq_cpy(c, c + n, rem);
2260 isl_seq_clr(c + rem, n);
2263 /* Drop n dimensions starting at first.
2265 * In principle, this frees up some extra variables as the number
2266 * of columns remains constant, but we would have to extend
2267 * the div array too as the number of rows in this array is assumed
2268 * to be equal to extra.
2270 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2271 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2273 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2276 /* Move "n" divs starting at "first" to the end of the list of divs.
2278 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2279 unsigned first, unsigned n)
2281 isl_int **div;
2282 int i;
2284 if (first + n == bmap->n_div)
2285 return bmap;
2287 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2288 if (!div)
2289 goto error;
2290 for (i = 0; i < n; ++i)
2291 div[i] = bmap->div[first + i];
2292 for (i = 0; i < bmap->n_div - first - n; ++i)
2293 bmap->div[first + i] = bmap->div[first + n + i];
2294 for (i = 0; i < n; ++i)
2295 bmap->div[bmap->n_div - n + i] = div[i];
2296 free(div);
2297 return bmap;
2298 error:
2299 isl_basic_map_free(bmap);
2300 return NULL;
2303 #undef TYPE
2304 #define TYPE isl_map
2305 static
2306 #include "check_type_range_templ.c"
2308 /* Check that there are "n" dimensions of type "type" starting at "first"
2309 * in "set".
2311 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2312 enum isl_dim_type type, unsigned first, unsigned n)
2314 return isl_map_check_range(set_to_map(set), type, first, n);
2317 /* Drop "n" dimensions of type "type" starting at "first".
2318 * Perform the core computation, without cowing or
2319 * simplifying and finalizing the result.
2321 * In principle, this frees up some extra variables as the number
2322 * of columns remains constant, but we would have to extend
2323 * the div array too as the number of rows in this array is assumed
2324 * to be equal to extra.
2326 __isl_give isl_basic_map *isl_basic_map_drop_core(
2327 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2328 unsigned first, unsigned n)
2330 int i;
2331 unsigned offset;
2332 unsigned left;
2333 isl_size total;
2335 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2336 return isl_basic_map_free(bmap);
2338 total = isl_basic_map_dim(bmap, isl_dim_all);
2339 if (total < 0)
2340 return isl_basic_map_free(bmap);
2342 offset = isl_basic_map_offset(bmap, type) + first;
2343 left = total - (offset - 1) - n;
2344 for (i = 0; i < bmap->n_eq; ++i)
2345 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2347 for (i = 0; i < bmap->n_ineq; ++i)
2348 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2350 for (i = 0; i < bmap->n_div; ++i)
2351 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2353 if (type == isl_dim_div) {
2354 bmap = move_divs_last(bmap, first, n);
2355 if (!bmap)
2356 return NULL;
2357 if (isl_basic_map_free_div(bmap, n) < 0)
2358 return isl_basic_map_free(bmap);
2359 } else
2360 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2361 if (!bmap->dim)
2362 return isl_basic_map_free(bmap);
2364 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2365 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2366 return bmap;
2369 /* Drop "n" dimensions of type "type" starting at "first".
2371 * In principle, this frees up some extra variables as the number
2372 * of columns remains constant, but we would have to extend
2373 * the div array too as the number of rows in this array is assumed
2374 * to be equal to extra.
2376 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2377 enum isl_dim_type type, unsigned first, unsigned n)
2379 if (!bmap)
2380 return NULL;
2381 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2382 return bmap;
2384 bmap = isl_basic_map_cow(bmap);
2385 if (!bmap)
2386 return NULL;
2388 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2390 bmap = isl_basic_map_simplify(bmap);
2391 return isl_basic_map_finalize(bmap);
2394 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2395 enum isl_dim_type type, unsigned first, unsigned n)
2397 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2398 type, first, n));
2401 /* No longer consider "map" to be normalized.
2403 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2405 if (!map)
2406 return NULL;
2407 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2408 return map;
2411 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2412 enum isl_dim_type type, unsigned first, unsigned n)
2414 int i;
2415 isl_space *space;
2417 if (isl_map_check_range(map, type, first, n) < 0)
2418 return isl_map_free(map);
2420 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2421 return map;
2422 map = isl_map_cow(map);
2423 if (!map)
2424 goto error;
2426 for (i = 0; i < map->n; ++i) {
2427 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2428 if (!map->p[i])
2429 goto error;
2431 map = isl_map_unmark_normalized(map);
2433 space = isl_map_take_space(map);
2434 space = isl_space_drop_dims(space, type, first, n);
2435 map = isl_map_restore_space(map, space);
2437 return map;
2438 error:
2439 isl_map_free(map);
2440 return NULL;
2443 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2444 enum isl_dim_type type, unsigned first, unsigned n)
2446 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2449 /* Drop the integer division at position "div", which is assumed
2450 * not to appear in any of the constraints or
2451 * in any of the other integer divisions.
2453 * Since the integer division is redundant, there is no need to cow.
2455 __isl_give isl_basic_map *isl_basic_map_drop_div(
2456 __isl_take isl_basic_map *bmap, unsigned div)
2458 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2461 /* Eliminate the specified n dimensions starting at first from the
2462 * constraints, without removing the dimensions from the space.
2463 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2465 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2466 enum isl_dim_type type, unsigned first, unsigned n)
2468 int i;
2470 if (n == 0)
2471 return map;
2473 if (isl_map_check_range(map, type, first, n) < 0)
2474 return isl_map_free(map);
2476 map = isl_map_cow(map);
2477 if (!map)
2478 return NULL;
2480 for (i = 0; i < map->n; ++i) {
2481 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2482 if (!map->p[i])
2483 goto error;
2485 return map;
2486 error:
2487 isl_map_free(map);
2488 return NULL;
2491 /* Eliminate the specified n dimensions starting at first from the
2492 * constraints, without removing the dimensions from the space.
2493 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2495 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2496 enum isl_dim_type type, unsigned first, unsigned n)
2498 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2501 /* Eliminate the specified n dimensions starting at first from the
2502 * constraints, without removing the dimensions from the space.
2503 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2505 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2506 unsigned first, unsigned n)
2508 return isl_set_eliminate(set, isl_dim_set, first, n);
2511 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2512 __isl_take isl_basic_map *bmap)
2514 isl_size v_div;
2516 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2517 if (v_div < 0)
2518 return isl_basic_map_free(bmap);
2519 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2520 if (!bmap)
2521 return NULL;
2522 bmap->n_div = 0;
2523 return isl_basic_map_finalize(bmap);
2526 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2527 __isl_take isl_basic_set *bset)
2529 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2532 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2534 int i;
2536 if (!map)
2537 return NULL;
2538 if (map->n == 0)
2539 return map;
2541 map = isl_map_cow(map);
2542 if (!map)
2543 return NULL;
2545 for (i = 0; i < map->n; ++i) {
2546 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2547 if (!map->p[i])
2548 goto error;
2550 return map;
2551 error:
2552 isl_map_free(map);
2553 return NULL;
2556 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2558 return isl_map_remove_divs(set);
2561 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2562 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2563 unsigned first, unsigned n)
2565 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2566 return isl_basic_map_free(bmap);
2567 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2568 return bmap;
2569 bmap = isl_basic_map_eliminate_vars(bmap,
2570 isl_basic_map_offset(bmap, type) - 1 + first, n);
2571 if (!bmap)
2572 return bmap;
2573 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2574 return bmap;
2575 bmap = isl_basic_map_drop(bmap, type, first, n);
2576 return bmap;
2579 /* Return true if the definition of the given div (recursively) involves
2580 * any of the given variables.
2582 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2583 unsigned first, unsigned n)
2585 int i;
2586 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2588 if (isl_int_is_zero(bmap->div[div][0]))
2589 return isl_bool_false;
2590 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2591 return isl_bool_true;
2593 for (i = bmap->n_div - 1; i >= 0; --i) {
2594 isl_bool involves;
2596 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2597 continue;
2598 involves = div_involves_vars(bmap, i, first, n);
2599 if (involves < 0 || involves)
2600 return involves;
2603 return isl_bool_false;
2606 /* Try and add a lower and/or upper bound on "div" to "bmap"
2607 * based on inequality "i".
2608 * "total" is the total number of variables (excluding the divs).
2609 * "v" is a temporary object that can be used during the calculations.
2610 * If "lb" is set, then a lower bound should be constructed.
2611 * If "ub" is set, then an upper bound should be constructed.
2613 * The calling function has already checked that the inequality does not
2614 * reference "div", but we still need to check that the inequality is
2615 * of the right form. We'll consider the case where we want to construct
2616 * a lower bound. The construction of upper bounds is similar.
2618 * Let "div" be of the form
2620 * q = floor((a + f(x))/d)
2622 * We essentially check if constraint "i" is of the form
2624 * b + f(x) >= 0
2626 * so that we can use it to derive a lower bound on "div".
2627 * However, we allow a slightly more general form
2629 * b + g(x) >= 0
2631 * with the condition that the coefficients of g(x) - f(x) are all
2632 * divisible by d.
2633 * Rewriting this constraint as
2635 * 0 >= -b - g(x)
2637 * adding a + f(x) to both sides and dividing by d, we obtain
2639 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2641 * Taking the floor on both sides, we obtain
2643 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2645 * or
2647 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2649 * In the case of an upper bound, we construct the constraint
2651 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2654 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2655 __isl_take isl_basic_map *bmap, int div, int i,
2656 unsigned total, isl_int v, int lb, int ub)
2658 int j;
2660 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2661 if (lb) {
2662 isl_int_sub(v, bmap->ineq[i][1 + j],
2663 bmap->div[div][1 + 1 + j]);
2664 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2666 if (ub) {
2667 isl_int_add(v, bmap->ineq[i][1 + j],
2668 bmap->div[div][1 + 1 + j]);
2669 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2672 if (!lb && !ub)
2673 return bmap;
2675 bmap = isl_basic_map_cow(bmap);
2676 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2677 if (lb) {
2678 int k = isl_basic_map_alloc_inequality(bmap);
2679 if (k < 0)
2680 goto error;
2681 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2682 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2683 bmap->div[div][1 + j]);
2684 isl_int_cdiv_q(bmap->ineq[k][j],
2685 bmap->ineq[k][j], bmap->div[div][0]);
2687 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2689 if (ub) {
2690 int k = isl_basic_map_alloc_inequality(bmap);
2691 if (k < 0)
2692 goto error;
2693 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2694 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2695 bmap->div[div][1 + j]);
2696 isl_int_fdiv_q(bmap->ineq[k][j],
2697 bmap->ineq[k][j], bmap->div[div][0]);
2699 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2702 return bmap;
2703 error:
2704 isl_basic_map_free(bmap);
2705 return NULL;
2708 /* This function is called right before "div" is eliminated from "bmap"
2709 * using Fourier-Motzkin.
2710 * Look through the constraints of "bmap" for constraints on the argument
2711 * of the integer division and use them to construct constraints on the
2712 * integer division itself. These constraints can then be combined
2713 * during the Fourier-Motzkin elimination.
2714 * Note that it is only useful to introduce lower bounds on "div"
2715 * if "bmap" already contains upper bounds on "div" as the newly
2716 * introduce lower bounds can then be combined with the pre-existing
2717 * upper bounds. Similarly for upper bounds.
2718 * We therefore first check if "bmap" contains any lower and/or upper bounds
2719 * on "div".
2721 * It is interesting to note that the introduction of these constraints
2722 * can indeed lead to more accurate results, even when compared to
2723 * deriving constraints on the argument of "div" from constraints on "div".
2724 * Consider, for example, the set
2726 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2728 * The second constraint can be rewritten as
2730 * 2 * [(-i-2j+3)/4] + k >= 0
2732 * from which we can derive
2734 * -i - 2j + 3 >= -2k
2736 * or
2738 * i + 2j <= 3 + 2k
2740 * Combined with the first constraint, we obtain
2742 * -3 <= 3 + 2k or k >= -3
2744 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2745 * the first constraint, we obtain
2747 * [(i + 2j)/4] >= [-3/4] = -1
2749 * Combining this constraint with the second constraint, we obtain
2751 * k >= -2
2753 static __isl_give isl_basic_map *insert_bounds_on_div(
2754 __isl_take isl_basic_map *bmap, int div)
2756 int i;
2757 int check_lb, check_ub;
2758 isl_int v;
2759 isl_size v_div;
2761 if (!bmap)
2762 return NULL;
2764 if (isl_int_is_zero(bmap->div[div][0]))
2765 return bmap;
2767 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2768 if (v_div < 0)
2769 return isl_basic_map_free(bmap);
2771 check_lb = 0;
2772 check_ub = 0;
2773 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2774 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2775 if (s > 0)
2776 check_ub = 1;
2777 if (s < 0)
2778 check_lb = 1;
2781 if (!check_lb && !check_ub)
2782 return bmap;
2784 isl_int_init(v);
2786 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2787 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2788 continue;
2790 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2791 check_lb, check_ub);
2794 isl_int_clear(v);
2796 return bmap;
2799 /* Remove all divs (recursively) involving any of the given dimensions
2800 * in their definitions.
2802 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2803 __isl_take isl_basic_map *bmap,
2804 enum isl_dim_type type, unsigned first, unsigned n)
2806 int i;
2808 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2809 return isl_basic_map_free(bmap);
2810 first += isl_basic_map_offset(bmap, type);
2812 for (i = bmap->n_div - 1; i >= 0; --i) {
2813 isl_bool involves;
2815 involves = div_involves_vars(bmap, i, first, n);
2816 if (involves < 0)
2817 return isl_basic_map_free(bmap);
2818 if (!involves)
2819 continue;
2820 bmap = insert_bounds_on_div(bmap, i);
2821 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2822 if (!bmap)
2823 return NULL;
2824 i = bmap->n_div;
2827 return bmap;
2830 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2831 __isl_take isl_basic_set *bset,
2832 enum isl_dim_type type, unsigned first, unsigned n)
2834 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2837 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2838 enum isl_dim_type type, unsigned first, unsigned n)
2840 int i;
2842 if (!map)
2843 return NULL;
2844 if (map->n == 0)
2845 return map;
2847 map = isl_map_cow(map);
2848 if (!map)
2849 return NULL;
2851 for (i = 0; i < map->n; ++i) {
2852 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2853 type, first, n);
2854 if (!map->p[i])
2855 goto error;
2857 return map;
2858 error:
2859 isl_map_free(map);
2860 return NULL;
2863 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2864 enum isl_dim_type type, unsigned first, unsigned n)
2866 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2867 type, first, n));
2870 /* Does the description of "bmap" depend on the specified dimensions?
2871 * We also check whether the dimensions appear in any of the div definitions.
2872 * In principle there is no need for this check. If the dimensions appear
2873 * in a div definition, they also appear in the defining constraints of that
2874 * div.
2876 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2877 enum isl_dim_type type, unsigned first, unsigned n)
2879 int i;
2881 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2882 return isl_bool_error;
2884 first += isl_basic_map_offset(bmap, type);
2885 for (i = 0; i < bmap->n_eq; ++i)
2886 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2887 return isl_bool_true;
2888 for (i = 0; i < bmap->n_ineq; ++i)
2889 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2890 return isl_bool_true;
2891 for (i = 0; i < bmap->n_div; ++i) {
2892 if (isl_int_is_zero(bmap->div[i][0]))
2893 continue;
2894 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2895 return isl_bool_true;
2898 return isl_bool_false;
2901 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2902 enum isl_dim_type type, unsigned first, unsigned n)
2904 int i;
2906 if (isl_map_check_range(map, type, first, n) < 0)
2907 return isl_bool_error;
2909 for (i = 0; i < map->n; ++i) {
2910 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2911 type, first, n);
2912 if (involves < 0 || involves)
2913 return involves;
2916 return isl_bool_false;
2919 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2920 enum isl_dim_type type, unsigned first, unsigned n)
2922 return isl_basic_map_involves_dims(bset, type, first, n);
2925 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2926 enum isl_dim_type type, unsigned first, unsigned n)
2928 return isl_map_involves_dims(set, type, first, n);
2931 /* Drop all constraints in bmap that involve any of the dimensions
2932 * first to first+n-1.
2933 * This function only performs the actual removal of constraints.
2935 * This function should not call finalize since it is used by
2936 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2938 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2939 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2941 int i;
2943 if (n == 0)
2944 return bmap;
2946 bmap = isl_basic_map_cow(bmap);
2948 if (!bmap)
2949 return NULL;
2951 for (i = bmap->n_eq - 1; i >= 0; --i) {
2952 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2953 continue;
2954 if (isl_basic_map_drop_equality(bmap, i) < 0)
2955 return isl_basic_map_free(bmap);
2958 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2959 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2960 continue;
2961 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2962 return isl_basic_map_free(bmap);
2965 return bmap;
2968 /* Drop all constraints in bset that involve any of the dimensions
2969 * first to first+n-1.
2970 * This function only performs the actual removal of constraints.
2972 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2973 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2975 return isl_basic_map_drop_constraints_involving(bset, first, n);
2978 /* Drop all constraints in bmap that do not involve any of the dimensions
2979 * first to first + n - 1 of the given type.
2981 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2982 __isl_take isl_basic_map *bmap,
2983 enum isl_dim_type type, unsigned first, unsigned n)
2985 int i;
2987 if (n == 0) {
2988 isl_space *space = isl_basic_map_get_space(bmap);
2989 isl_basic_map_free(bmap);
2990 return isl_basic_map_universe(space);
2992 bmap = isl_basic_map_cow(bmap);
2993 if (!bmap)
2994 return NULL;
2996 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2997 return isl_basic_map_free(bmap);
2999 first += isl_basic_map_offset(bmap, type) - 1;
3001 for (i = bmap->n_eq - 1; i >= 0; --i) {
3002 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3003 continue;
3004 if (isl_basic_map_drop_equality(bmap, i) < 0)
3005 return isl_basic_map_free(bmap);
3008 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3009 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3010 continue;
3011 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3012 return isl_basic_map_free(bmap);
3015 bmap = isl_basic_map_add_known_div_constraints(bmap);
3016 return bmap;
3019 /* Drop all constraints in bset that do not involve any of the dimensions
3020 * first to first + n - 1 of the given type.
3022 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3023 __isl_take isl_basic_set *bset,
3024 enum isl_dim_type type, unsigned first, unsigned n)
3026 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3027 type, first, n);
3030 /* Drop all constraints in bmap that involve any of the dimensions
3031 * first to first + n - 1 of the given type.
3033 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3034 __isl_take isl_basic_map *bmap,
3035 enum isl_dim_type type, unsigned first, unsigned n)
3037 if (!bmap)
3038 return NULL;
3039 if (n == 0)
3040 return bmap;
3042 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3043 return isl_basic_map_free(bmap);
3045 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3046 first += isl_basic_map_offset(bmap, type) - 1;
3047 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3048 bmap = isl_basic_map_add_known_div_constraints(bmap);
3049 return bmap;
3052 /* Drop all constraints in bset that involve any of the dimensions
3053 * first to first + n - 1 of the given type.
3055 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3056 __isl_take isl_basic_set *bset,
3057 enum isl_dim_type type, unsigned first, unsigned n)
3059 return isl_basic_map_drop_constraints_involving_dims(bset,
3060 type, first, n);
3063 /* Drop constraints from "map" by applying "drop" to each basic map.
3065 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3066 enum isl_dim_type type, unsigned first, unsigned n,
3067 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3068 enum isl_dim_type type, unsigned first, unsigned n))
3070 int i;
3072 if (isl_map_check_range(map, type, first, n) < 0)
3073 return isl_map_free(map);
3075 map = isl_map_cow(map);
3076 if (!map)
3077 return NULL;
3079 for (i = 0; i < map->n; ++i) {
3080 map->p[i] = drop(map->p[i], type, first, n);
3081 if (!map->p[i])
3082 return isl_map_free(map);
3085 if (map->n > 1)
3086 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3088 return map;
3091 /* Drop all constraints in map that involve any of the dimensions
3092 * first to first + n - 1 of the given type.
3094 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3095 __isl_take isl_map *map,
3096 enum isl_dim_type type, unsigned first, unsigned n)
3098 if (n == 0)
3099 return map;
3100 return drop_constraints(map, type, first, n,
3101 &isl_basic_map_drop_constraints_involving_dims);
3104 /* Drop all constraints in "map" that do not involve any of the dimensions
3105 * first to first + n - 1 of the given type.
3107 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3108 __isl_take isl_map *map,
3109 enum isl_dim_type type, unsigned first, unsigned n)
3111 if (n == 0) {
3112 isl_space *space = isl_map_get_space(map);
3113 isl_map_free(map);
3114 return isl_map_universe(space);
3116 return drop_constraints(map, type, first, n,
3117 &isl_basic_map_drop_constraints_not_involving_dims);
3120 /* Drop all constraints in set that involve any of the dimensions
3121 * first to first + n - 1 of the given type.
3123 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3124 __isl_take isl_set *set,
3125 enum isl_dim_type type, unsigned first, unsigned n)
3127 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3130 /* Drop all constraints in "set" that do not involve any of the dimensions
3131 * first to first + n - 1 of the given type.
3133 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3134 __isl_take isl_set *set,
3135 enum isl_dim_type type, unsigned first, unsigned n)
3137 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3140 /* Does local variable "div" of "bmap" have a complete explicit representation?
3141 * Having a complete explicit representation requires not only
3142 * an explicit representation, but also that all local variables
3143 * that appear in this explicit representation in turn have
3144 * a complete explicit representation.
3146 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3148 int i;
3149 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3150 isl_bool marked;
3152 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3153 if (marked < 0 || marked)
3154 return isl_bool_not(marked);
3156 for (i = bmap->n_div - 1; i >= 0; --i) {
3157 isl_bool known;
3159 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3160 continue;
3161 known = isl_basic_map_div_is_known(bmap, i);
3162 if (known < 0 || !known)
3163 return known;
3166 return isl_bool_true;
3169 /* Remove all divs that are unknown or defined in terms of unknown divs.
3171 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3172 __isl_take isl_basic_map *bmap)
3174 int i;
3176 if (!bmap)
3177 return NULL;
3179 for (i = bmap->n_div - 1; i >= 0; --i) {
3180 if (isl_basic_map_div_is_known(bmap, i))
3181 continue;
3182 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3183 if (!bmap)
3184 return NULL;
3185 i = bmap->n_div;
3188 return bmap;
3191 /* Remove all divs that are unknown or defined in terms of unknown divs.
3193 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3194 __isl_take isl_basic_set *bset)
3196 return isl_basic_map_remove_unknown_divs(bset);
3199 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3201 int i;
3203 if (!map)
3204 return NULL;
3205 if (map->n == 0)
3206 return map;
3208 map = isl_map_cow(map);
3209 if (!map)
3210 return NULL;
3212 for (i = 0; i < map->n; ++i) {
3213 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3214 if (!map->p[i])
3215 goto error;
3217 return map;
3218 error:
3219 isl_map_free(map);
3220 return NULL;
3223 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3225 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3228 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3229 __isl_take isl_basic_set *bset,
3230 enum isl_dim_type type, unsigned first, unsigned n)
3232 isl_basic_map *bmap = bset_to_bmap(bset);
3233 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3234 return bset_from_bmap(bmap);
3237 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3238 enum isl_dim_type type, unsigned first, unsigned n)
3240 int i;
3242 if (n == 0)
3243 return map;
3245 map = isl_map_cow(map);
3246 if (isl_map_check_range(map, type, first, n) < 0)
3247 return isl_map_free(map);
3249 for (i = 0; i < map->n; ++i) {
3250 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3251 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3252 if (!map->p[i])
3253 goto error;
3255 map = isl_map_drop(map, type, first, n);
3256 return map;
3257 error:
3258 isl_map_free(map);
3259 return NULL;
3262 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3263 enum isl_dim_type type, unsigned first, unsigned n)
3265 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3266 type, first, n));
3269 /* Project out n inputs starting at first using Fourier-Motzkin */
3270 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3271 unsigned first, unsigned n)
3273 return isl_map_remove_dims(map, isl_dim_in, first, n);
3276 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3277 FILE *out, int indent)
3279 isl_printer *p;
3281 if (!bset) {
3282 fprintf(out, "null basic set\n");
3283 return;
3286 fprintf(out, "%*s", indent, "");
3287 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3288 bset->ref, bset->dim->nparam, bset->dim->n_out,
3289 bset->extra, bset->flags);
3291 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3292 p = isl_printer_set_dump(p, 1);
3293 p = isl_printer_set_indent(p, indent);
3294 p = isl_printer_start_line(p);
3295 p = isl_printer_print_basic_set(p, bset);
3296 p = isl_printer_end_line(p);
3297 isl_printer_free(p);
3300 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3301 FILE *out, int indent)
3303 isl_printer *p;
3305 if (!bmap) {
3306 fprintf(out, "null basic map\n");
3307 return;
3310 fprintf(out, "%*s", indent, "");
3311 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3312 "flags: %x, n_name: %d\n",
3313 bmap->ref,
3314 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3315 bmap->extra, bmap->flags, bmap->dim->n_id);
3317 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3318 p = isl_printer_set_dump(p, 1);
3319 p = isl_printer_set_indent(p, indent);
3320 p = isl_printer_start_line(p);
3321 p = isl_printer_print_basic_map(p, bmap);
3322 p = isl_printer_end_line(p);
3323 isl_printer_free(p);
3326 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3327 unsigned pos)
3329 isl_size total;
3331 total = isl_basic_map_dim(bmap, isl_dim_all);
3332 if (total < 0)
3333 return isl_basic_map_free(bmap);
3334 if (pos >= bmap->n_ineq)
3335 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3336 "invalid position", return isl_basic_map_free(bmap));
3337 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3338 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3339 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3340 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3341 return bmap;
3344 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3345 unsigned flags)
3347 if (isl_space_check_is_set(space) < 0)
3348 goto error;
3349 return isl_map_alloc_space(space, n, flags);
3350 error:
3351 isl_space_free(space);
3352 return NULL;
3355 /* Make sure "map" has room for at least "n" more basic maps.
3357 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3359 int i;
3360 struct isl_map *grown = NULL;
3362 if (!map)
3363 return NULL;
3364 isl_assert(map->ctx, n >= 0, goto error);
3365 if (map->n + n <= map->size)
3366 return map;
3367 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3368 if (!grown)
3369 goto error;
3370 for (i = 0; i < map->n; ++i) {
3371 grown->p[i] = isl_basic_map_copy(map->p[i]);
3372 if (!grown->p[i])
3373 goto error;
3374 grown->n++;
3376 isl_map_free(map);
3377 return grown;
3378 error:
3379 isl_map_free(grown);
3380 isl_map_free(map);
3381 return NULL;
3384 /* Make sure "set" has room for at least "n" more basic sets.
3386 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3388 return set_from_map(isl_map_grow(set_to_map(set), n));
3391 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3393 return isl_map_from_basic_map(bset);
3396 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3398 struct isl_map *map;
3400 if (!bmap)
3401 return NULL;
3403 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3404 return isl_map_add_basic_map(map, bmap);
3407 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3408 __isl_take isl_basic_set *bset)
3410 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3411 bset_to_bmap(bset)));
3414 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3416 return isl_map_free(set);
3419 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3421 int i;
3423 if (!set) {
3424 fprintf(out, "null set\n");
3425 return;
3428 fprintf(out, "%*s", indent, "");
3429 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3430 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3431 set->flags);
3432 for (i = 0; i < set->n; ++i) {
3433 fprintf(out, "%*s", indent, "");
3434 fprintf(out, "basic set %d:\n", i);
3435 isl_basic_set_print_internal(set->p[i], out, indent+4);
3439 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3441 int i;
3443 if (!map) {
3444 fprintf(out, "null map\n");
3445 return;
3448 fprintf(out, "%*s", indent, "");
3449 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3450 "flags: %x, n_name: %d\n",
3451 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3452 map->dim->n_out, map->flags, map->dim->n_id);
3453 for (i = 0; i < map->n; ++i) {
3454 fprintf(out, "%*s", indent, "");
3455 fprintf(out, "basic map %d:\n", i);
3456 isl_basic_map_print_internal(map->p[i], out, indent+4);
3460 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3462 static isl_stat isl_basic_map_check_compatible_domain(
3463 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3465 isl_bool ok;
3467 ok = isl_basic_map_compatible_domain(bmap, bset);
3468 if (ok < 0)
3469 return isl_stat_error;
3470 if (!ok)
3471 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3472 "incompatible spaces", return isl_stat_error);
3474 return isl_stat_ok;
3477 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3478 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3480 struct isl_basic_map *bmap_domain;
3481 isl_size dim;
3483 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3484 goto error;
3486 dim = isl_basic_set_dim(bset, isl_dim_set);
3487 if (dim < 0)
3488 goto error;
3489 if (dim != 0 &&
3490 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3491 goto error;
3493 bmap = isl_basic_map_cow(bmap);
3494 if (!bmap)
3495 goto error;
3496 bmap = isl_basic_map_extend(bmap,
3497 bset->n_div, bset->n_eq, bset->n_ineq);
3498 bmap_domain = isl_basic_map_from_domain(bset);
3499 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3501 bmap = isl_basic_map_simplify(bmap);
3502 return isl_basic_map_finalize(bmap);
3503 error:
3504 isl_basic_map_free(bmap);
3505 isl_basic_set_free(bset);
3506 return NULL;
3509 /* Check that the space of "bset" is the same as that of the range of "bmap".
3511 static isl_stat isl_basic_map_check_compatible_range(
3512 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3514 isl_bool ok;
3516 ok = isl_basic_map_compatible_range(bmap, bset);
3517 if (ok < 0)
3518 return isl_stat_error;
3519 if (!ok)
3520 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3521 "incompatible spaces", return isl_stat_error);
3523 return isl_stat_ok;
3526 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3527 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3529 struct isl_basic_map *bmap_range;
3530 isl_size dim;
3532 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3533 goto error;
3535 dim = isl_basic_set_dim(bset, isl_dim_set);
3536 if (dim < 0)
3537 goto error;
3538 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3539 goto error;
3541 if (isl_basic_set_plain_is_universe(bset)) {
3542 isl_basic_set_free(bset);
3543 return bmap;
3546 bmap = isl_basic_map_cow(bmap);
3547 if (!bmap)
3548 goto error;
3549 bmap = isl_basic_map_extend(bmap,
3550 bset->n_div, bset->n_eq, bset->n_ineq);
3551 bmap_range = bset_to_bmap(bset);
3552 bmap = add_constraints(bmap, bmap_range, 0, 0);
3554 bmap = isl_basic_map_simplify(bmap);
3555 return isl_basic_map_finalize(bmap);
3556 error:
3557 isl_basic_map_free(bmap);
3558 isl_basic_set_free(bset);
3559 return NULL;
3562 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3563 __isl_keep isl_vec *vec)
3565 int i;
3566 isl_size total;
3567 isl_int s;
3569 total = isl_basic_map_dim(bmap, isl_dim_all);
3570 if (total < 0 || !vec)
3571 return isl_bool_error;
3573 if (1 + total != vec->size)
3574 return isl_bool_false;
3576 isl_int_init(s);
3578 for (i = 0; i < bmap->n_eq; ++i) {
3579 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3580 if (!isl_int_is_zero(s)) {
3581 isl_int_clear(s);
3582 return isl_bool_false;
3586 for (i = 0; i < bmap->n_ineq; ++i) {
3587 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3588 if (isl_int_is_neg(s)) {
3589 isl_int_clear(s);
3590 return isl_bool_false;
3594 isl_int_clear(s);
3596 return isl_bool_true;
3599 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3600 __isl_keep isl_vec *vec)
3602 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3605 __isl_give isl_basic_map *isl_basic_map_intersect(
3606 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3608 struct isl_vec *sample = NULL;
3609 isl_space *space1, *space2;
3610 isl_size dim1, dim2, nparam1, nparam2;
3612 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3613 goto error;
3614 space1 = isl_basic_map_peek_space(bmap1);
3615 space2 = isl_basic_map_peek_space(bmap2);
3616 dim1 = isl_space_dim(space1, isl_dim_all);
3617 dim2 = isl_space_dim(space2, isl_dim_all);
3618 nparam1 = isl_space_dim(space1, isl_dim_param);
3619 nparam2 = isl_space_dim(space2, isl_dim_param);
3620 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3621 goto error;
3622 if (dim1 == nparam1 && dim2 != nparam2)
3623 return isl_basic_map_intersect(bmap2, bmap1);
3625 if (dim2 != nparam2 &&
3626 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3627 goto error;
3629 if (isl_basic_map_plain_is_empty(bmap1)) {
3630 isl_basic_map_free(bmap2);
3631 return bmap1;
3633 if (isl_basic_map_plain_is_empty(bmap2)) {
3634 isl_basic_map_free(bmap1);
3635 return bmap2;
3638 if (bmap1->sample &&
3639 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3640 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3641 sample = isl_vec_copy(bmap1->sample);
3642 else if (bmap2->sample &&
3643 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3644 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3645 sample = isl_vec_copy(bmap2->sample);
3647 bmap1 = isl_basic_map_cow(bmap1);
3648 if (!bmap1)
3649 goto error;
3650 bmap1 = isl_basic_map_extend(bmap1,
3651 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3652 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3654 if (!bmap1)
3655 isl_vec_free(sample);
3656 else if (sample) {
3657 isl_vec_free(bmap1->sample);
3658 bmap1->sample = sample;
3661 bmap1 = isl_basic_map_simplify(bmap1);
3662 return isl_basic_map_finalize(bmap1);
3663 error:
3664 if (sample)
3665 isl_vec_free(sample);
3666 isl_basic_map_free(bmap1);
3667 isl_basic_map_free(bmap2);
3668 return NULL;
3671 __isl_give isl_basic_set *isl_basic_set_intersect(
3672 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3674 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3675 bset_to_bmap(bset2)));
3678 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3679 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3681 return isl_basic_set_intersect(bset1, bset2);
3684 /* Special case of isl_map_intersect, where both map1 and map2
3685 * are convex, without any divs and such that either map1 or map2
3686 * contains a single constraint. This constraint is then simply
3687 * added to the other map.
3689 static __isl_give isl_map *map_intersect_add_constraint(
3690 __isl_take isl_map *map1, __isl_take isl_map *map2)
3692 isl_assert(map1->ctx, map1->n == 1, goto error);
3693 isl_assert(map2->ctx, map1->n == 1, goto error);
3694 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3695 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3697 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3698 return isl_map_intersect(map2, map1);
3700 map1 = isl_map_cow(map1);
3701 if (!map1)
3702 goto error;
3703 if (isl_map_plain_is_empty(map1)) {
3704 isl_map_free(map2);
3705 return map1;
3707 if (map2->p[0]->n_eq == 1)
3708 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3709 else
3710 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3711 map2->p[0]->ineq[0]);
3713 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3714 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3715 if (!map1->p[0])
3716 goto error;
3718 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3719 isl_basic_map_free(map1->p[0]);
3720 map1->n = 0;
3723 isl_map_free(map2);
3725 map1 = isl_map_unmark_normalized(map1);
3726 return map1;
3727 error:
3728 isl_map_free(map1);
3729 isl_map_free(map2);
3730 return NULL;
3733 /* map2 may be either a parameter domain or a map living in the same
3734 * space as map1.
3736 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3737 __isl_take isl_map *map2)
3739 unsigned flags = 0;
3740 isl_bool equal;
3741 isl_map *result;
3742 int i, j;
3743 isl_size dim2, nparam2;
3745 if (!map1 || !map2)
3746 goto error;
3748 if ((isl_map_plain_is_empty(map1) ||
3749 isl_map_plain_is_universe(map2)) &&
3750 isl_space_is_equal(map1->dim, map2->dim)) {
3751 isl_map_free(map2);
3752 return map1;
3754 if ((isl_map_plain_is_empty(map2) ||
3755 isl_map_plain_is_universe(map1)) &&
3756 isl_space_is_equal(map1->dim, map2->dim)) {
3757 isl_map_free(map1);
3758 return map2;
3761 if (map1->n == 1 && map2->n == 1 &&
3762 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3763 isl_space_is_equal(map1->dim, map2->dim) &&
3764 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3765 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3766 return map_intersect_add_constraint(map1, map2);
3768 equal = isl_map_plain_is_equal(map1, map2);
3769 if (equal < 0)
3770 goto error;
3771 if (equal) {
3772 isl_map_free(map2);
3773 return map1;
3776 dim2 = isl_map_dim(map2, isl_dim_all);
3777 nparam2 = isl_map_dim(map2, isl_dim_param);
3778 if (dim2 < 0 || nparam2 < 0)
3779 goto error;
3780 if (dim2 != nparam2)
3781 isl_assert(map1->ctx,
3782 isl_space_is_equal(map1->dim, map2->dim), goto error);
3784 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3785 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3786 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3788 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3789 map1->n * map2->n, flags);
3790 if (!result)
3791 goto error;
3792 for (i = 0; i < map1->n; ++i)
3793 for (j = 0; j < map2->n; ++j) {
3794 struct isl_basic_map *part;
3795 part = isl_basic_map_intersect(
3796 isl_basic_map_copy(map1->p[i]),
3797 isl_basic_map_copy(map2->p[j]));
3798 if (isl_basic_map_is_empty(part) < 0)
3799 part = isl_basic_map_free(part);
3800 result = isl_map_add_basic_map(result, part);
3801 if (!result)
3802 goto error;
3804 isl_map_free(map1);
3805 isl_map_free(map2);
3806 return result;
3807 error:
3808 isl_map_free(map1);
3809 isl_map_free(map2);
3810 return NULL;
3813 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3814 __isl_take isl_map *map2)
3816 if (isl_map_check_equal_space(map1, map2) < 0)
3817 goto error;
3818 return map_intersect_internal(map1, map2);
3819 error:
3820 isl_map_free(map1);
3821 isl_map_free(map2);
3822 return NULL;
3825 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3826 __isl_take isl_map *map2)
3828 isl_map_align_params_bin(&map1, &map2);
3829 return map_intersect(map1, map2);
3832 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
3833 __isl_take isl_set *set2)
3835 return set_from_map(isl_map_intersect(set_to_map(set1),
3836 set_to_map(set2)));
3839 /* map_intersect_internal accepts intersections
3840 * with parameter domains, so we can just call that function.
3842 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
3843 __isl_take isl_set *params)
3845 isl_map_align_params_set(&map, &params);
3846 return map_intersect_internal(map, params);
3849 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3850 __isl_take isl_set *params)
3852 return isl_map_intersect_params(set, params);
3855 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3857 isl_space *space;
3858 unsigned pos;
3859 isl_size n1, n2;
3861 if (!bmap)
3862 return NULL;
3863 bmap = isl_basic_map_cow(bmap);
3864 if (!bmap)
3865 return NULL;
3866 space = isl_space_reverse(isl_space_copy(bmap->dim));
3867 pos = isl_basic_map_offset(bmap, isl_dim_in);
3868 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3869 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3870 if (n1 < 0 || n2 < 0)
3871 bmap = isl_basic_map_free(bmap);
3872 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3873 return isl_basic_map_reset_space(bmap, space);
3876 /* Given a basic map A -> (B -> C), return the corresponding basic map
3877 * A -> (C -> B).
3879 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
3880 __isl_take isl_basic_map *bmap)
3882 isl_space *space;
3883 isl_size offset, n1, n2;
3885 space = isl_basic_map_peek_space(bmap);
3886 if (isl_space_check_range_is_wrapping(space) < 0)
3887 return isl_basic_map_free(bmap);
3888 offset = isl_basic_map_var_offset(bmap, isl_dim_out);
3889 n1 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_in);
3890 n2 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_out);
3891 if (offset < 0 || n1 < 0 || n2 < 0)
3892 return isl_basic_map_free(bmap);
3894 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
3896 space = isl_basic_map_take_space(bmap);
3897 space = isl_space_range_reverse(space);
3898 bmap = isl_basic_map_restore_space(bmap, space);
3900 return bmap;
3903 static __isl_give isl_basic_map *basic_map_space_reset(
3904 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3906 isl_space *space;
3908 if (!bmap)
3909 return NULL;
3910 if (!isl_space_is_named_or_nested(bmap->dim, type))
3911 return bmap;
3913 space = isl_basic_map_get_space(bmap);
3914 space = isl_space_reset(space, type);
3915 bmap = isl_basic_map_reset_space(bmap, space);
3916 return bmap;
3919 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3920 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3921 unsigned pos, unsigned n)
3923 isl_bool rational, is_empty;
3924 isl_space *res_space;
3925 struct isl_basic_map *res;
3926 struct isl_dim_map *dim_map;
3927 isl_size total;
3928 unsigned off;
3929 enum isl_dim_type t;
3931 if (n == 0)
3932 return basic_map_space_reset(bmap, type);
3934 is_empty = isl_basic_map_plain_is_empty(bmap);
3935 total = isl_basic_map_dim(bmap, isl_dim_all);
3936 if (is_empty < 0 || total < 0)
3937 return isl_basic_map_free(bmap);
3938 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3939 type, pos, n);
3940 if (!res_space)
3941 return isl_basic_map_free(bmap);
3942 if (is_empty) {
3943 isl_basic_map_free(bmap);
3944 return isl_basic_map_empty(res_space);
3947 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
3948 off = 0;
3949 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3950 isl_size dim;
3952 if (t != type) {
3953 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3954 } else {
3955 isl_size size = isl_basic_map_dim(bmap, t);
3956 if (size < 0)
3957 dim_map = isl_dim_map_free(dim_map);
3958 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3959 0, pos, off);
3960 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3961 pos, size - pos, off + pos + n);
3963 dim = isl_space_dim(res_space, t);
3964 if (dim < 0)
3965 dim_map = isl_dim_map_free(dim_map);
3966 off += dim;
3968 isl_dim_map_div(dim_map, bmap, off);
3970 res = isl_basic_map_alloc_space(res_space,
3971 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3972 rational = isl_basic_map_is_rational(bmap);
3973 if (rational < 0)
3974 res = isl_basic_map_free(res);
3975 if (rational)
3976 res = isl_basic_map_set_rational(res);
3977 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3978 return isl_basic_map_finalize(res);
3981 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3982 __isl_take isl_basic_set *bset,
3983 enum isl_dim_type type, unsigned pos, unsigned n)
3985 return isl_basic_map_insert_dims(bset, type, pos, n);
3988 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3989 enum isl_dim_type type, unsigned n)
3991 isl_size dim;
3993 dim = isl_basic_map_dim(bmap, type);
3994 if (dim < 0)
3995 return isl_basic_map_free(bmap);
3996 return isl_basic_map_insert_dims(bmap, type, dim, n);
3999 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4000 enum isl_dim_type type, unsigned n)
4002 if (!bset)
4003 return NULL;
4004 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4005 return isl_basic_map_add_dims(bset, type, n);
4006 error:
4007 isl_basic_set_free(bset);
4008 return NULL;
4011 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4012 enum isl_dim_type type)
4014 isl_space *space;
4016 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4017 return map;
4019 space = isl_map_get_space(map);
4020 space = isl_space_reset(space, type);
4021 map = isl_map_reset_space(map, space);
4022 return map;
4025 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4026 enum isl_dim_type type, unsigned pos, unsigned n)
4028 int i;
4029 isl_space *space;
4031 if (n == 0)
4032 return map_space_reset(map, type);
4034 map = isl_map_cow(map);
4035 if (!map)
4036 return NULL;
4038 for (i = 0; i < map->n; ++i) {
4039 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4040 if (!map->p[i])
4041 goto error;
4044 space = isl_map_take_space(map);
4045 space = isl_space_insert_dims(space, type, pos, n);
4046 map = isl_map_restore_space(map, space);
4048 return map;
4049 error:
4050 isl_map_free(map);
4051 return NULL;
4054 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4055 enum isl_dim_type type, unsigned pos, unsigned n)
4057 return isl_map_insert_dims(set, type, pos, n);
4060 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4061 enum isl_dim_type type, unsigned n)
4063 isl_size dim;
4065 dim = isl_map_dim(map, type);
4066 if (dim < 0)
4067 return isl_map_free(map);
4068 return isl_map_insert_dims(map, type, dim, n);
4071 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4072 enum isl_dim_type type, unsigned n)
4074 if (!set)
4075 return NULL;
4076 isl_assert(set->ctx, type != isl_dim_in, goto error);
4077 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4078 error:
4079 isl_set_free(set);
4080 return NULL;
4083 __isl_give isl_basic_map *isl_basic_map_move_dims(
4084 __isl_take isl_basic_map *bmap,
4085 enum isl_dim_type dst_type, unsigned dst_pos,
4086 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4088 isl_space *space;
4089 struct isl_dim_map *dim_map;
4090 struct isl_basic_map *res;
4091 enum isl_dim_type t;
4092 isl_size total;
4093 unsigned off;
4095 if (!bmap)
4096 return NULL;
4097 if (n == 0) {
4098 bmap = isl_basic_map_reset(bmap, src_type);
4099 bmap = isl_basic_map_reset(bmap, dst_type);
4100 return bmap;
4103 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4104 return isl_basic_map_free(bmap);
4106 if (dst_type == src_type && dst_pos == src_pos)
4107 return bmap;
4109 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4111 if (pos(bmap->dim, dst_type) + dst_pos ==
4112 pos(bmap->dim, src_type) + src_pos +
4113 ((src_type < dst_type) ? n : 0)) {
4114 space = isl_basic_map_take_space(bmap);
4115 space = isl_space_move_dims(space, dst_type, dst_pos,
4116 src_type, src_pos, n);
4117 bmap = isl_basic_map_restore_space(bmap, space);
4118 bmap = isl_basic_map_finalize(bmap);
4120 return bmap;
4123 total = isl_basic_map_dim(bmap, isl_dim_all);
4124 if (total < 0)
4125 return isl_basic_map_free(bmap);
4126 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4128 off = 0;
4129 space = isl_basic_map_peek_space(bmap);
4130 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4131 isl_size size = isl_space_dim(space, t);
4132 if (size < 0)
4133 dim_map = isl_dim_map_free(dim_map);
4134 if (t == dst_type) {
4135 isl_dim_map_dim_range(dim_map, space, t,
4136 0, dst_pos, off);
4137 off += dst_pos;
4138 isl_dim_map_dim_range(dim_map, space, src_type,
4139 src_pos, n, off);
4140 off += n;
4141 isl_dim_map_dim_range(dim_map, space, t,
4142 dst_pos, size - dst_pos, off);
4143 off += size - dst_pos;
4144 } else if (t == src_type) {
4145 isl_dim_map_dim_range(dim_map, space, t,
4146 0, src_pos, off);
4147 off += src_pos;
4148 isl_dim_map_dim_range(dim_map, space, t,
4149 src_pos + n, size - src_pos - n, off);
4150 off += size - src_pos - n;
4151 } else {
4152 isl_dim_map_dim(dim_map, space, t, off);
4153 off += size;
4156 isl_dim_map_div(dim_map, bmap, off);
4158 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4159 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4160 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4161 space = isl_basic_map_take_space(bmap);
4162 space = isl_space_move_dims(space, dst_type, dst_pos,
4163 src_type, src_pos, n);
4164 bmap = isl_basic_map_restore_space(bmap, space);
4165 if (!bmap)
4166 goto error;
4168 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4169 bmap = isl_basic_map_gauss(bmap, NULL);
4170 bmap = isl_basic_map_finalize(bmap);
4172 return bmap;
4173 error:
4174 isl_basic_map_free(bmap);
4175 return NULL;
4178 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4179 enum isl_dim_type dst_type, unsigned dst_pos,
4180 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4182 isl_basic_map *bmap = bset_to_bmap(bset);
4183 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4184 src_type, src_pos, n);
4185 return bset_from_bmap(bmap);
4188 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4189 enum isl_dim_type dst_type, unsigned dst_pos,
4190 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4192 if (!set)
4193 return NULL;
4194 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4195 return set_from_map(isl_map_move_dims(set_to_map(set),
4196 dst_type, dst_pos, src_type, src_pos, n));
4197 error:
4198 isl_set_free(set);
4199 return NULL;
4202 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4203 enum isl_dim_type dst_type, unsigned dst_pos,
4204 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4206 int i;
4207 isl_space *space;
4209 if (n == 0) {
4210 map = isl_map_reset(map, src_type);
4211 map = isl_map_reset(map, dst_type);
4212 return map;
4215 if (isl_map_check_range(map, src_type, src_pos, n))
4216 return isl_map_free(map);
4218 if (dst_type == src_type && dst_pos == src_pos)
4219 return map;
4221 isl_assert(map->ctx, dst_type != src_type, goto error);
4223 map = isl_map_cow(map);
4224 if (!map)
4225 return NULL;
4227 for (i = 0; i < map->n; ++i) {
4228 map->p[i] = isl_basic_map_move_dims(map->p[i],
4229 dst_type, dst_pos,
4230 src_type, src_pos, n);
4231 if (!map->p[i])
4232 goto error;
4235 space = isl_map_take_space(map);
4236 space = isl_space_move_dims(space, dst_type, dst_pos,
4237 src_type, src_pos, n);
4238 map = isl_map_restore_space(map, space);
4240 return map;
4241 error:
4242 isl_map_free(map);
4243 return NULL;
4246 /* Move the specified dimensions to the last columns right before
4247 * the divs. Don't change the dimension specification of bmap.
4248 * That's the responsibility of the caller.
4250 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4251 enum isl_dim_type type, unsigned first, unsigned n)
4253 isl_space *space;
4254 struct isl_dim_map *dim_map;
4255 struct isl_basic_map *res;
4256 enum isl_dim_type t;
4257 isl_size total;
4258 unsigned off;
4260 if (!bmap)
4261 return NULL;
4262 if (isl_basic_map_offset(bmap, type) + first + n ==
4263 isl_basic_map_offset(bmap, isl_dim_div))
4264 return bmap;
4266 total = isl_basic_map_dim(bmap, isl_dim_all);
4267 if (total < 0)
4268 return isl_basic_map_free(bmap);
4269 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4271 off = 0;
4272 space = isl_basic_map_peek_space(bmap);
4273 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4274 isl_size size = isl_space_dim(space, t);
4275 if (size < 0)
4276 dim_map = isl_dim_map_free(dim_map);
4277 if (t == type) {
4278 isl_dim_map_dim_range(dim_map, space, t,
4279 0, first, off);
4280 off += first;
4281 isl_dim_map_dim_range(dim_map, space, t,
4282 first, n, total - bmap->n_div - n);
4283 isl_dim_map_dim_range(dim_map, space, t,
4284 first + n, size - (first + n), off);
4285 off += size - (first + n);
4286 } else {
4287 isl_dim_map_dim(dim_map, space, t, off);
4288 off += size;
4291 isl_dim_map_div(dim_map, bmap, off + n);
4293 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4294 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4295 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4296 return res;
4299 /* Insert "n" rows in the divs of "bmap".
4301 * The number of columns is not changed, which means that the last
4302 * dimensions of "bmap" are being reintepreted as the new divs.
4303 * The space of "bmap" is not adjusted, however, which means
4304 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4305 * from the space of "bmap" is the responsibility of the caller.
4307 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4308 int n)
4310 int i;
4311 size_t row_size;
4312 isl_int **new_div;
4313 isl_int *old;
4315 bmap = isl_basic_map_cow(bmap);
4316 if (!bmap)
4317 return NULL;
4319 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4320 old = bmap->block2.data;
4321 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4322 (bmap->extra + n) * (1 + row_size));
4323 if (!bmap->block2.data)
4324 return isl_basic_map_free(bmap);
4325 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4326 if (!new_div)
4327 return isl_basic_map_free(bmap);
4328 for (i = 0; i < n; ++i) {
4329 new_div[i] = bmap->block2.data +
4330 (bmap->extra + i) * (1 + row_size);
4331 isl_seq_clr(new_div[i], 1 + row_size);
4333 for (i = 0; i < bmap->extra; ++i)
4334 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4335 free(bmap->div);
4336 bmap->div = new_div;
4337 bmap->n_div += n;
4338 bmap->extra += n;
4340 return bmap;
4343 /* Drop constraints from "bmap" that only involve the variables
4344 * of "type" in the range [first, first + n] that are not related
4345 * to any of the variables outside that interval.
4346 * These constraints cannot influence the values for the variables
4347 * outside the interval, except in case they cause "bmap" to be empty.
4348 * Only drop the constraints if "bmap" is known to be non-empty.
4350 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4351 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4352 unsigned first, unsigned n)
4354 int i;
4355 int *groups;
4356 isl_size dim, n_div;
4357 isl_bool non_empty;
4359 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4360 if (non_empty < 0)
4361 return isl_basic_map_free(bmap);
4362 if (!non_empty)
4363 return bmap;
4365 dim = isl_basic_map_dim(bmap, isl_dim_all);
4366 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4367 if (dim < 0 || n_div < 0)
4368 return isl_basic_map_free(bmap);
4369 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4370 if (!groups)
4371 return isl_basic_map_free(bmap);
4372 first += isl_basic_map_offset(bmap, type) - 1;
4373 for (i = 0; i < first; ++i)
4374 groups[i] = -1;
4375 for (i = first + n; i < dim - n_div; ++i)
4376 groups[i] = -1;
4378 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4380 return bmap;
4383 /* Turn the n dimensions of type type, starting at first
4384 * into existentially quantified variables.
4386 * If a subset of the projected out variables are unrelated
4387 * to any of the variables that remain, then the constraints
4388 * involving this subset are simply dropped first.
4390 __isl_give isl_basic_map *isl_basic_map_project_out(
4391 __isl_take isl_basic_map *bmap,
4392 enum isl_dim_type type, unsigned first, unsigned n)
4394 isl_bool empty;
4395 isl_space *space;
4397 if (n == 0)
4398 return basic_map_space_reset(bmap, type);
4399 if (type == isl_dim_div)
4400 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4401 "cannot project out existentially quantified variables",
4402 return isl_basic_map_free(bmap));
4404 empty = isl_basic_map_plain_is_empty(bmap);
4405 if (empty < 0)
4406 return isl_basic_map_free(bmap);
4407 if (empty)
4408 bmap = isl_basic_map_set_to_empty(bmap);
4410 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4411 if (!bmap)
4412 return NULL;
4414 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4415 return isl_basic_map_remove_dims(bmap, type, first, n);
4417 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4418 return isl_basic_map_free(bmap);
4420 bmap = move_last(bmap, type, first, n);
4421 bmap = isl_basic_map_cow(bmap);
4422 bmap = insert_div_rows(bmap, n);
4424 space = isl_basic_map_take_space(bmap);
4425 space = isl_space_drop_dims(space, type, first, n);
4426 bmap = isl_basic_map_restore_space(bmap, space);
4427 bmap = isl_basic_map_simplify(bmap);
4428 bmap = isl_basic_map_drop_redundant_divs(bmap);
4429 return isl_basic_map_finalize(bmap);
4432 /* Turn the n dimensions of type type, starting at first
4433 * into existentially quantified variables.
4435 __isl_give isl_basic_set *isl_basic_set_project_out(
4436 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4437 unsigned first, unsigned n)
4439 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4440 type, first, n));
4443 /* Turn the n dimensions of type type, starting at first
4444 * into existentially quantified variables.
4446 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4447 enum isl_dim_type type, unsigned first, unsigned n)
4449 int i;
4450 isl_space *space;
4452 if (n == 0)
4453 return map_space_reset(map, type);
4455 if (isl_map_check_range(map, type, first, n) < 0)
4456 return isl_map_free(map);
4458 map = isl_map_cow(map);
4459 if (!map)
4460 return NULL;
4462 for (i = 0; i < map->n; ++i) {
4463 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4464 if (!map->p[i])
4465 goto error;
4468 if (map->n > 1)
4469 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4470 map = isl_map_unmark_normalized(map);
4472 space = isl_map_take_space(map);
4473 space = isl_space_drop_dims(space, type, first, n);
4474 map = isl_map_restore_space(map, space);
4476 return map;
4477 error:
4478 isl_map_free(map);
4479 return NULL;
4482 #undef TYPE
4483 #define TYPE isl_map
4484 #include "isl_project_out_all_params_templ.c"
4486 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4487 * into existentially quantified variables.
4489 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4490 enum isl_dim_type type, unsigned first, unsigned n)
4492 isl_size dim;
4494 dim = isl_map_dim(map, type);
4495 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4496 return isl_map_free(map);
4497 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4498 map = isl_map_project_out(map, type, 0, first);
4499 return map;
4502 /* Turn the n dimensions of type type, starting at first
4503 * into existentially quantified variables.
4505 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4506 enum isl_dim_type type, unsigned first, unsigned n)
4508 return set_from_map(isl_map_project_out(set_to_map(set),
4509 type, first, n));
4512 /* If "set" involves a parameter with identifier "id",
4513 * then turn it into an existentially quantified variable.
4515 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4516 __isl_take isl_id *id)
4518 int pos;
4520 if (!set || !id)
4521 goto error;
4522 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
4523 isl_id_free(id);
4524 if (pos < 0)
4525 return set;
4526 return isl_set_project_out(set, isl_dim_param, pos, 1);
4527 error:
4528 isl_set_free(set);
4529 isl_id_free(id);
4530 return NULL;
4533 /* If "set" involves any of the parameters with identifiers in "list",
4534 * then turn them into existentially quantified variables.
4536 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4537 __isl_take isl_id_list *list)
4539 int i;
4540 isl_size n;
4542 n = isl_id_list_size(list);
4543 if (n < 0)
4544 goto error;
4545 for (i = 0; i < n; ++i) {
4546 isl_id *id;
4548 id = isl_id_list_get_at(list, i);
4549 set = isl_set_project_out_param_id(set, id);
4552 isl_id_list_free(list);
4553 return set;
4554 error:
4555 isl_id_list_free(list);
4556 isl_set_free(set);
4557 return NULL;
4560 /* Project out all parameters from "set" by existentially quantifying
4561 * over them.
4563 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4565 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4568 /* Return a map that projects the elements in "set" onto their
4569 * "n" set dimensions starting at "first".
4570 * "type" should be equal to isl_dim_set.
4572 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4573 enum isl_dim_type type, unsigned first, unsigned n)
4575 int i;
4576 isl_map *map;
4578 if (type != isl_dim_set)
4579 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4580 "only set dimensions can be projected out", goto error);
4581 if (isl_set_check_range(set, type, first, n) < 0)
4582 return isl_set_free(set);
4584 map = isl_map_from_domain(set);
4585 map = isl_map_add_dims(map, isl_dim_out, n);
4586 for (i = 0; i < n; ++i)
4587 map = isl_map_equate(map, isl_dim_in, first + i,
4588 isl_dim_out, i);
4589 return map;
4590 error:
4591 isl_set_free(set);
4592 return NULL;
4595 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4596 unsigned n)
4598 int i, j;
4599 isl_size total;
4601 total = isl_basic_map_dim(bmap, isl_dim_all);
4602 if (total < 0)
4603 return isl_basic_map_free(bmap);
4604 for (i = 0; i < n; ++i) {
4605 j = isl_basic_map_alloc_div(bmap);
4606 if (j < 0)
4607 goto error;
4608 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4610 return bmap;
4611 error:
4612 isl_basic_map_free(bmap);
4613 return NULL;
4616 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4618 static isl_stat isl_basic_map_check_applies_range(
4619 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
4621 isl_bool equal;
4622 isl_space *space1, *space2;
4624 space1 = isl_basic_map_peek_space(bmap1);
4625 space2 = isl_basic_map_peek_space(bmap2);
4626 equal = isl_space_tuple_is_equal(space1, isl_dim_out,
4627 space2, isl_dim_in);
4628 if (equal < 0)
4629 return isl_stat_error;
4630 if (!equal)
4631 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4632 "spaces don't match", return isl_stat_error);
4633 return isl_stat_ok;
4636 __isl_give isl_basic_map *isl_basic_map_apply_range(
4637 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4639 isl_space *space_result = NULL;
4640 struct isl_basic_map *bmap;
4641 isl_size n_in, n_out, n, nparam;
4642 unsigned total, pos;
4643 struct isl_dim_map *dim_map1, *dim_map2;
4645 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4646 goto error;
4647 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
4648 goto error;
4650 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4651 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4652 n = isl_basic_map_dim(bmap1, isl_dim_out);
4653 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4654 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4655 goto error;
4657 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4658 isl_basic_map_get_space(bmap2));
4660 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4661 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4662 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4663 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4664 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4665 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4666 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4667 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4668 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4669 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4670 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4672 bmap = isl_basic_map_alloc_space(space_result,
4673 bmap1->n_div + bmap2->n_div + n,
4674 bmap1->n_eq + bmap2->n_eq,
4675 bmap1->n_ineq + bmap2->n_ineq);
4676 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4677 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4678 bmap = add_divs(bmap, n);
4679 bmap = isl_basic_map_simplify(bmap);
4680 bmap = isl_basic_map_drop_redundant_divs(bmap);
4681 return isl_basic_map_finalize(bmap);
4682 error:
4683 isl_basic_map_free(bmap1);
4684 isl_basic_map_free(bmap2);
4685 return NULL;
4688 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4689 __isl_take isl_basic_map *bmap)
4691 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4692 goto error;
4694 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4695 bmap));
4696 error:
4697 isl_basic_set_free(bset);
4698 isl_basic_map_free(bmap);
4699 return NULL;
4702 __isl_give isl_basic_map *isl_basic_map_apply_domain(
4703 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4705 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4706 goto error;
4707 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4708 bmap2->dim, isl_dim_in))
4709 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4710 "spaces don't match", goto error);
4712 bmap1 = isl_basic_map_reverse(bmap1);
4713 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4714 return isl_basic_map_reverse(bmap1);
4715 error:
4716 isl_basic_map_free(bmap1);
4717 isl_basic_map_free(bmap2);
4718 return NULL;
4721 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4722 * A \cap B -> f(A) + f(B)
4724 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4725 __isl_take isl_basic_map *bmap2)
4727 isl_size n_in, n_out, nparam;
4728 unsigned total, pos;
4729 struct isl_basic_map *bmap = NULL;
4730 struct isl_dim_map *dim_map1, *dim_map2;
4731 int i;
4733 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4734 goto error;
4736 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4737 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4738 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4739 if (nparam < 0 || n_in < 0 || n_out < 0)
4740 goto error;
4742 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4743 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4744 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4745 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4746 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4747 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4748 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4749 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4750 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4751 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4752 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4754 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4755 bmap1->n_div + bmap2->n_div + 2 * n_out,
4756 bmap1->n_eq + bmap2->n_eq + n_out,
4757 bmap1->n_ineq + bmap2->n_ineq);
4758 for (i = 0; i < n_out; ++i) {
4759 int j = isl_basic_map_alloc_equality(bmap);
4760 if (j < 0)
4761 goto error;
4762 isl_seq_clr(bmap->eq[j], 1+total);
4763 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4764 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4765 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4767 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4768 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4769 bmap = add_divs(bmap, 2 * n_out);
4771 bmap = isl_basic_map_simplify(bmap);
4772 return isl_basic_map_finalize(bmap);
4773 error:
4774 isl_basic_map_free(bmap);
4775 isl_basic_map_free(bmap1);
4776 isl_basic_map_free(bmap2);
4777 return NULL;
4780 /* Given two maps A -> f(A) and B -> g(B), construct a map
4781 * A \cap B -> f(A) + f(B)
4783 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4784 __isl_take isl_map *map2)
4786 struct isl_map *result;
4787 int i, j;
4789 if (isl_map_check_equal_space(map1, map2) < 0)
4790 goto error;
4792 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4793 map1->n * map2->n, 0);
4794 if (!result)
4795 goto error;
4796 for (i = 0; i < map1->n; ++i)
4797 for (j = 0; j < map2->n; ++j) {
4798 struct isl_basic_map *part;
4799 part = isl_basic_map_sum(
4800 isl_basic_map_copy(map1->p[i]),
4801 isl_basic_map_copy(map2->p[j]));
4802 if (isl_basic_map_is_empty(part))
4803 isl_basic_map_free(part);
4804 else
4805 result = isl_map_add_basic_map(result, part);
4806 if (!result)
4807 goto error;
4809 isl_map_free(map1);
4810 isl_map_free(map2);
4811 return result;
4812 error:
4813 isl_map_free(map1);
4814 isl_map_free(map2);
4815 return NULL;
4818 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4819 __isl_take isl_set *set2)
4821 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4824 /* Given a basic map A -> f(A), construct A -> -f(A).
4826 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4828 int i, j;
4829 unsigned off;
4830 isl_size n;
4832 bmap = isl_basic_map_cow(bmap);
4833 n = isl_basic_map_dim(bmap, isl_dim_out);
4834 if (n < 0)
4835 return isl_basic_map_free(bmap);
4837 off = isl_basic_map_offset(bmap, isl_dim_out);
4838 for (i = 0; i < bmap->n_eq; ++i)
4839 for (j = 0; j < n; ++j)
4840 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4841 for (i = 0; i < bmap->n_ineq; ++i)
4842 for (j = 0; j < n; ++j)
4843 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4844 for (i = 0; i < bmap->n_div; ++i)
4845 for (j = 0; j < n; ++j)
4846 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4847 bmap = isl_basic_map_gauss(bmap, NULL);
4848 return isl_basic_map_finalize(bmap);
4851 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4853 return isl_basic_map_neg(bset);
4856 /* Given a map A -> f(A), construct A -> -f(A).
4858 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4860 int i;
4862 map = isl_map_cow(map);
4863 if (!map)
4864 return NULL;
4866 for (i = 0; i < map->n; ++i) {
4867 map->p[i] = isl_basic_map_neg(map->p[i]);
4868 if (!map->p[i])
4869 goto error;
4872 return map;
4873 error:
4874 isl_map_free(map);
4875 return NULL;
4878 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4880 return set_from_map(isl_map_neg(set_to_map(set)));
4883 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4884 * A -> floor(f(A)/d).
4886 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4887 isl_int d)
4889 isl_size n_in, n_out, nparam;
4890 unsigned total, pos;
4891 struct isl_basic_map *result = NULL;
4892 struct isl_dim_map *dim_map;
4893 int i;
4895 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4896 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4897 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4898 if (nparam < 0 || n_in < 0 || n_out < 0)
4899 return isl_basic_map_free(bmap);
4901 total = nparam + n_in + n_out + bmap->n_div + n_out;
4902 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4903 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4904 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4905 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4906 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4908 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4909 bmap->n_div + n_out,
4910 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4911 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4912 result = add_divs(result, n_out);
4913 for (i = 0; i < n_out; ++i) {
4914 int j;
4915 j = isl_basic_map_alloc_inequality(result);
4916 if (j < 0)
4917 goto error;
4918 isl_seq_clr(result->ineq[j], 1+total);
4919 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4920 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4921 j = isl_basic_map_alloc_inequality(result);
4922 if (j < 0)
4923 goto error;
4924 isl_seq_clr(result->ineq[j], 1+total);
4925 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4926 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4927 isl_int_sub_ui(result->ineq[j][0], d, 1);
4930 result = isl_basic_map_simplify(result);
4931 return isl_basic_map_finalize(result);
4932 error:
4933 isl_basic_map_free(result);
4934 return NULL;
4937 /* Given a map A -> f(A) and an integer d, construct a map
4938 * A -> floor(f(A)/d).
4940 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4942 int i;
4944 map = isl_map_cow(map);
4945 if (!map)
4946 return NULL;
4948 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4949 for (i = 0; i < map->n; ++i) {
4950 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4951 if (!map->p[i])
4952 goto error;
4954 map = isl_map_unmark_normalized(map);
4956 return map;
4957 error:
4958 isl_map_free(map);
4959 return NULL;
4962 /* Given a map A -> f(A) and an integer d, construct a map
4963 * A -> floor(f(A)/d).
4965 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4966 __isl_take isl_val *d)
4968 if (!map || !d)
4969 goto error;
4970 if (!isl_val_is_int(d))
4971 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4972 "expecting integer denominator", goto error);
4973 map = isl_map_floordiv(map, d->n);
4974 isl_val_free(d);
4975 return map;
4976 error:
4977 isl_map_free(map);
4978 isl_val_free(d);
4979 return NULL;
4982 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4983 unsigned pos)
4985 int i;
4986 isl_size nparam;
4987 isl_size n_in;
4988 isl_size total;
4990 total = isl_basic_map_dim(bmap, isl_dim_all);
4991 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4992 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4993 if (total < 0 || nparam < 0 || n_in < 0)
4994 return isl_basic_map_free(bmap);
4995 i = isl_basic_map_alloc_equality(bmap);
4996 if (i < 0)
4997 goto error;
4998 isl_seq_clr(bmap->eq[i], 1 + total);
4999 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5000 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5001 return isl_basic_map_finalize(bmap);
5002 error:
5003 isl_basic_map_free(bmap);
5004 return NULL;
5007 /* Add a constraint to "bmap" expressing i_pos < o_pos
5009 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5010 unsigned pos)
5012 int i;
5013 isl_size nparam;
5014 isl_size n_in;
5015 isl_size total;
5017 total = isl_basic_map_dim(bmap, isl_dim_all);
5018 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5019 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5020 if (total < 0 || nparam < 0 || n_in < 0)
5021 return isl_basic_map_free(bmap);
5022 i = isl_basic_map_alloc_inequality(bmap);
5023 if (i < 0)
5024 goto error;
5025 isl_seq_clr(bmap->ineq[i], 1 + total);
5026 isl_int_set_si(bmap->ineq[i][0], -1);
5027 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5028 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5029 return isl_basic_map_finalize(bmap);
5030 error:
5031 isl_basic_map_free(bmap);
5032 return NULL;
5035 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5037 static __isl_give isl_basic_map *var_less_or_equal(
5038 __isl_take isl_basic_map *bmap, unsigned pos)
5040 int i;
5041 isl_size nparam;
5042 isl_size n_in;
5043 isl_size total;
5045 total = isl_basic_map_dim(bmap, isl_dim_all);
5046 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5047 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5048 if (total < 0 || nparam < 0 || n_in < 0)
5049 return isl_basic_map_free(bmap);
5050 i = isl_basic_map_alloc_inequality(bmap);
5051 if (i < 0)
5052 goto error;
5053 isl_seq_clr(bmap->ineq[i], 1 + total);
5054 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5055 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5056 return isl_basic_map_finalize(bmap);
5057 error:
5058 isl_basic_map_free(bmap);
5059 return NULL;
5062 /* Add a constraint to "bmap" expressing i_pos > o_pos
5064 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5065 unsigned pos)
5067 int i;
5068 isl_size nparam;
5069 isl_size n_in;
5070 isl_size total;
5072 total = isl_basic_map_dim(bmap, isl_dim_all);
5073 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5074 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5075 if (total < 0 || nparam < 0 || n_in < 0)
5076 return isl_basic_map_free(bmap);
5077 i = isl_basic_map_alloc_inequality(bmap);
5078 if (i < 0)
5079 goto error;
5080 isl_seq_clr(bmap->ineq[i], 1 + total);
5081 isl_int_set_si(bmap->ineq[i][0], -1);
5082 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5083 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5084 return isl_basic_map_finalize(bmap);
5085 error:
5086 isl_basic_map_free(bmap);
5087 return NULL;
5090 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5092 static __isl_give isl_basic_map *var_more_or_equal(
5093 __isl_take isl_basic_map *bmap, unsigned pos)
5095 int i;
5096 isl_size nparam;
5097 isl_size n_in;
5098 isl_size total;
5100 total = isl_basic_map_dim(bmap, isl_dim_all);
5101 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5102 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5103 if (total < 0 || nparam < 0 || n_in < 0)
5104 return isl_basic_map_free(bmap);
5105 i = isl_basic_map_alloc_inequality(bmap);
5106 if (i < 0)
5107 goto error;
5108 isl_seq_clr(bmap->ineq[i], 1 + total);
5109 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5110 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5111 return isl_basic_map_finalize(bmap);
5112 error:
5113 isl_basic_map_free(bmap);
5114 return NULL;
5117 __isl_give isl_basic_map *isl_basic_map_equal(
5118 __isl_take isl_space *space, unsigned n_equal)
5120 int i;
5121 struct isl_basic_map *bmap;
5122 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5123 if (!bmap)
5124 return NULL;
5125 for (i = 0; i < n_equal && bmap; ++i)
5126 bmap = var_equal(bmap, i);
5127 return isl_basic_map_finalize(bmap);
5130 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5132 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5133 unsigned pos)
5135 int i;
5136 struct isl_basic_map *bmap;
5137 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5138 if (!bmap)
5139 return NULL;
5140 for (i = 0; i < pos && bmap; ++i)
5141 bmap = var_equal(bmap, i);
5142 if (bmap)
5143 bmap = var_less(bmap, pos);
5144 return isl_basic_map_finalize(bmap);
5147 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5149 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5150 __isl_take isl_space *space, unsigned pos)
5152 int i;
5153 isl_basic_map *bmap;
5155 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5156 for (i = 0; i < pos; ++i)
5157 bmap = var_equal(bmap, i);
5158 bmap = var_less_or_equal(bmap, pos);
5159 return isl_basic_map_finalize(bmap);
5162 /* Return a relation on "space" expressing i_pos > o_pos
5164 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5165 unsigned pos)
5167 int i;
5168 struct isl_basic_map *bmap;
5169 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5170 if (!bmap)
5171 return NULL;
5172 for (i = 0; i < pos && bmap; ++i)
5173 bmap = var_equal(bmap, i);
5174 if (bmap)
5175 bmap = var_more(bmap, pos);
5176 return isl_basic_map_finalize(bmap);
5179 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5181 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5182 __isl_take isl_space *space, unsigned pos)
5184 int i;
5185 isl_basic_map *bmap;
5187 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5188 for (i = 0; i < pos; ++i)
5189 bmap = var_equal(bmap, i);
5190 bmap = var_more_or_equal(bmap, pos);
5191 return isl_basic_map_finalize(bmap);
5194 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5195 unsigned n, int equal)
5197 struct isl_map *map;
5198 int i;
5200 if (n == 0 && equal)
5201 return isl_map_universe(space);
5203 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5205 for (i = 0; i + 1 < n; ++i)
5206 map = isl_map_add_basic_map(map,
5207 isl_basic_map_less_at(isl_space_copy(space), i));
5208 if (n > 0) {
5209 if (equal)
5210 map = isl_map_add_basic_map(map,
5211 isl_basic_map_less_or_equal_at(space, n - 1));
5212 else
5213 map = isl_map_add_basic_map(map,
5214 isl_basic_map_less_at(space, n - 1));
5215 } else
5216 isl_space_free(space);
5218 return map;
5221 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5223 if (!space)
5224 return NULL;
5225 return map_lex_lte_first(space, space->n_out, equal);
5228 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5229 unsigned n)
5231 return map_lex_lte_first(space, n, 0);
5234 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5235 unsigned n)
5237 return map_lex_lte_first(space, n, 1);
5240 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5242 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5245 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5247 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5250 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5251 unsigned n, int equal)
5253 struct isl_map *map;
5254 int i;
5256 if (n == 0 && equal)
5257 return isl_map_universe(space);
5259 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5261 for (i = 0; i + 1 < n; ++i)
5262 map = isl_map_add_basic_map(map,
5263 isl_basic_map_more_at(isl_space_copy(space), i));
5264 if (n > 0) {
5265 if (equal)
5266 map = isl_map_add_basic_map(map,
5267 isl_basic_map_more_or_equal_at(space, n - 1));
5268 else
5269 map = isl_map_add_basic_map(map,
5270 isl_basic_map_more_at(space, n - 1));
5271 } else
5272 isl_space_free(space);
5274 return map;
5277 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5279 if (!space)
5280 return NULL;
5281 return map_lex_gte_first(space, space->n_out, equal);
5284 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5285 unsigned n)
5287 return map_lex_gte_first(space, n, 0);
5290 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5291 unsigned n)
5293 return map_lex_gte_first(space, n, 1);
5296 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5298 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5301 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5303 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5306 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5307 __isl_take isl_set *set2)
5309 isl_map *map;
5310 map = isl_map_lex_le(isl_set_get_space(set1));
5311 map = isl_map_intersect_domain(map, set1);
5312 map = isl_map_intersect_range(map, set2);
5313 return map;
5316 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5317 __isl_take isl_set *set2)
5319 isl_map *map;
5320 map = isl_map_lex_lt(isl_set_get_space(set1));
5321 map = isl_map_intersect_domain(map, set1);
5322 map = isl_map_intersect_range(map, set2);
5323 return map;
5326 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5327 __isl_take isl_set *set2)
5329 isl_map *map;
5330 map = isl_map_lex_ge(isl_set_get_space(set1));
5331 map = isl_map_intersect_domain(map, set1);
5332 map = isl_map_intersect_range(map, set2);
5333 return map;
5336 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5337 __isl_take isl_set *set2)
5339 isl_map *map;
5340 map = isl_map_lex_gt(isl_set_get_space(set1));
5341 map = isl_map_intersect_domain(map, set1);
5342 map = isl_map_intersect_range(map, set2);
5343 return map;
5346 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5347 __isl_take isl_map *map2)
5349 isl_map *map;
5350 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5351 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5352 map = isl_map_apply_range(map, isl_map_reverse(map2));
5353 return map;
5356 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5357 __isl_take isl_map *map2)
5359 isl_map *map;
5360 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5361 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5362 map = isl_map_apply_range(map, isl_map_reverse(map2));
5363 return map;
5366 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5367 __isl_take isl_map *map2)
5369 isl_map *map;
5370 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5371 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5372 map = isl_map_apply_range(map, isl_map_reverse(map2));
5373 return map;
5376 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5377 __isl_take isl_map *map2)
5379 isl_map *map;
5380 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5381 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5382 map = isl_map_apply_range(map, isl_map_reverse(map2));
5383 return map;
5386 /* For the div d = floor(f/m) at position "div", add the constraint
5388 * f - m d >= 0
5390 static __isl_give isl_basic_map *add_upper_div_constraint(
5391 __isl_take isl_basic_map *bmap, unsigned div)
5393 int i;
5394 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5395 isl_size n_div;
5396 unsigned pos;
5398 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5399 if (v_div < 0 || n_div < 0)
5400 return isl_basic_map_free(bmap);
5401 pos = v_div + div;
5402 i = isl_basic_map_alloc_inequality(bmap);
5403 if (i < 0)
5404 return isl_basic_map_free(bmap);
5405 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5406 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5408 return bmap;
5411 /* For the div d = floor(f/m) at position "div", add the constraint
5413 * -(f-(m-1)) + m d >= 0
5415 static __isl_give isl_basic_map *add_lower_div_constraint(
5416 __isl_take isl_basic_map *bmap, unsigned div)
5418 int i;
5419 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5420 isl_size n_div;
5421 unsigned pos;
5423 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5424 if (v_div < 0 || n_div < 0)
5425 return isl_basic_map_free(bmap);
5426 pos = v_div + div;
5427 i = isl_basic_map_alloc_inequality(bmap);
5428 if (i < 0)
5429 return isl_basic_map_free(bmap);
5430 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5431 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5432 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5433 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5435 return bmap;
5438 /* For the div d = floor(f/m) at position "pos", add the constraints
5440 * f - m d >= 0
5441 * -(f-(m-1)) + m d >= 0
5443 * Note that the second constraint is the negation of
5445 * f - m d >= m
5447 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5448 __isl_take isl_basic_map *bmap, unsigned pos)
5450 bmap = add_upper_div_constraint(bmap, pos);
5451 bmap = add_lower_div_constraint(bmap, pos);
5452 return bmap;
5455 /* For each known div d = floor(f/m), add the constraints
5457 * f - m d >= 0
5458 * -(f-(m-1)) + m d >= 0
5460 * Remove duplicate constraints in case of some these div constraints
5461 * already appear in "bmap".
5463 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5464 __isl_take isl_basic_map *bmap)
5466 isl_size n_div;
5468 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5469 if (n_div < 0)
5470 return isl_basic_map_free(bmap);
5471 if (n_div == 0)
5472 return bmap;
5474 bmap = add_known_div_constraints(bmap);
5475 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5476 bmap = isl_basic_map_finalize(bmap);
5477 return bmap;
5480 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5482 * In particular, if this div is of the form d = floor(f/m),
5483 * then add the constraint
5485 * f - m d >= 0
5487 * if sign < 0 or the constraint
5489 * -(f-(m-1)) + m d >= 0
5491 * if sign > 0.
5493 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5494 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5496 if (sign < 0)
5497 return add_upper_div_constraint(bmap, div);
5498 else
5499 return add_lower_div_constraint(bmap, div);
5502 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5503 __isl_take isl_basic_map *bmap)
5505 isl_space *space;
5507 if (!bmap)
5508 goto error;
5509 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5510 bmap->n_div == 0 &&
5511 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5512 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5513 return bset_from_bmap(bmap);
5514 bmap = isl_basic_map_cow(bmap);
5515 if (!bmap)
5516 return NULL;
5517 space = isl_basic_map_take_space(bmap);
5518 space = isl_space_underlying(space, bmap->n_div);
5519 bmap = isl_basic_map_restore_space(bmap, space);
5520 if (!bmap)
5521 return NULL;
5522 bmap->extra -= bmap->n_div;
5523 bmap->n_div = 0;
5524 bmap = isl_basic_map_finalize(bmap);
5525 return bset_from_bmap(bmap);
5526 error:
5527 isl_basic_map_free(bmap);
5528 return NULL;
5531 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5532 __isl_take isl_basic_set *bset)
5534 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5537 /* Replace each element in "list" by the result of applying
5538 * isl_basic_map_underlying_set to the element.
5540 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5541 __isl_take isl_basic_map_list *list)
5543 int i;
5544 isl_size n;
5546 n = isl_basic_map_list_n_basic_map(list);
5547 if (n < 0)
5548 goto error;
5550 for (i = 0; i < n; ++i) {
5551 isl_basic_map *bmap;
5552 isl_basic_set *bset;
5554 bmap = isl_basic_map_list_get_basic_map(list, i);
5555 bset = isl_basic_set_underlying_set(bmap);
5556 list = isl_basic_set_list_set_basic_set(list, i, bset);
5559 return list;
5560 error:
5561 isl_basic_map_list_free(list);
5562 return NULL;
5565 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5566 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5568 struct isl_basic_map *bmap;
5569 struct isl_ctx *ctx;
5570 isl_size dim, bmap_total;
5571 unsigned total;
5572 int i;
5574 if (!bset || !like)
5575 goto error;
5576 ctx = bset->ctx;
5577 if (isl_basic_set_check_no_params(bset) < 0 ||
5578 isl_basic_set_check_no_locals(bset) < 0)
5579 goto error;
5580 dim = isl_basic_set_dim(bset, isl_dim_set);
5581 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5582 if (dim < 0 || bmap_total < 0)
5583 goto error;
5584 isl_assert(ctx, dim == bmap_total, goto error);
5585 if (like->n_div == 0) {
5586 isl_space *space = isl_basic_map_get_space(like);
5587 isl_basic_map_free(like);
5588 return isl_basic_map_reset_space(bset, space);
5590 bset = isl_basic_set_cow(bset);
5591 if (!bset)
5592 goto error;
5593 total = dim + bset->extra;
5594 bmap = bset_to_bmap(bset);
5595 isl_space_free(isl_basic_map_take_space(bmap));
5596 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5597 if (!bmap)
5598 goto error;
5599 bmap->n_div = like->n_div;
5600 bmap->extra += like->n_div;
5601 if (bmap->extra) {
5602 unsigned ltotal;
5603 isl_int **div;
5604 ltotal = total - bmap->extra + like->extra;
5605 if (ltotal > total)
5606 ltotal = total;
5607 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5608 bmap->extra * (1 + 1 + total));
5609 if (isl_blk_is_error(bmap->block2))
5610 goto error;
5611 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5612 if (!div)
5613 goto error;
5614 bmap->div = div;
5615 for (i = 0; i < bmap->extra; ++i)
5616 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5617 for (i = 0; i < like->n_div; ++i) {
5618 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5619 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5621 bmap = isl_basic_map_add_known_div_constraints(bmap);
5623 isl_basic_map_free(like);
5624 bmap = isl_basic_map_simplify(bmap);
5625 bmap = isl_basic_map_finalize(bmap);
5626 return bmap;
5627 error:
5628 isl_basic_map_free(like);
5629 isl_basic_set_free(bset);
5630 return NULL;
5633 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5634 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5636 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5637 bset_to_bmap(like)));
5640 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5642 int i;
5644 map = isl_map_cow(map);
5645 if (!map)
5646 return NULL;
5647 map->dim = isl_space_cow(map->dim);
5648 if (!map->dim)
5649 goto error;
5651 for (i = 1; i < map->n; ++i)
5652 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5653 goto error);
5654 for (i = 0; i < map->n; ++i) {
5655 map->p[i] = bset_to_bmap(
5656 isl_basic_map_underlying_set(map->p[i]));
5657 if (!map->p[i])
5658 goto error;
5660 if (map->n == 0)
5661 map->dim = isl_space_underlying(map->dim, 0);
5662 else {
5663 isl_space_free(map->dim);
5664 map->dim = isl_space_copy(map->p[0]->dim);
5666 if (!map->dim)
5667 goto error;
5668 return set_from_map(map);
5669 error:
5670 isl_map_free(map);
5671 return NULL;
5674 /* Replace the space of "bmap" by "space".
5676 * If the space of "bmap" is identical to "space" (including the identifiers
5677 * of the input and output dimensions), then simply return the original input.
5679 __isl_give isl_basic_map *isl_basic_map_reset_space(
5680 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5682 isl_bool equal;
5683 isl_space *bmap_space;
5685 bmap_space = isl_basic_map_peek_space(bmap);
5686 equal = isl_space_is_equal(bmap_space, space);
5687 if (equal >= 0 && equal)
5688 equal = isl_space_has_equal_ids(bmap_space, space);
5689 if (equal < 0)
5690 goto error;
5691 if (equal) {
5692 isl_space_free(space);
5693 return bmap;
5695 isl_space_free(isl_basic_map_take_space(bmap));
5696 bmap = isl_basic_map_restore_space(bmap, space);
5698 bmap = isl_basic_map_finalize(bmap);
5700 return bmap;
5701 error:
5702 isl_basic_map_free(bmap);
5703 isl_space_free(space);
5704 return NULL;
5707 __isl_give isl_basic_set *isl_basic_set_reset_space(
5708 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
5710 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5711 space));
5714 /* Check that the total dimensions of "map" and "space" are the same.
5716 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5717 __isl_keep isl_space *space)
5719 isl_size dim1, dim2;
5721 dim1 = isl_map_dim(map, isl_dim_all);
5722 dim2 = isl_space_dim(space, isl_dim_all);
5723 if (dim1 < 0 || dim2 < 0)
5724 return isl_stat_error;
5725 if (dim1 == dim2)
5726 return isl_stat_ok;
5727 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5728 "total dimensions do not match", return isl_stat_error);
5731 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5732 __isl_take isl_space *space)
5734 int i;
5736 map = isl_map_cow(map);
5737 if (!map || !space)
5738 goto error;
5740 for (i = 0; i < map->n; ++i) {
5741 map->p[i] = isl_basic_map_reset_space(map->p[i],
5742 isl_space_copy(space));
5743 if (!map->p[i])
5744 goto error;
5746 isl_space_free(isl_map_take_space(map));
5747 map = isl_map_restore_space(map, space);
5749 return map;
5750 error:
5751 isl_map_free(map);
5752 isl_space_free(space);
5753 return NULL;
5756 /* Replace the space of "map" by "space", without modifying
5757 * the dimension of "map".
5759 * If the space of "map" is identical to "space" (including the identifiers
5760 * of the input and output dimensions), then simply return the original input.
5762 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5763 __isl_take isl_space *space)
5765 isl_bool equal;
5766 isl_space *map_space;
5768 map_space = isl_map_peek_space(map);
5769 equal = isl_space_is_equal(map_space, space);
5770 if (equal >= 0 && equal)
5771 equal = isl_space_has_equal_ids(map_space, space);
5772 if (equal < 0)
5773 goto error;
5774 if (equal) {
5775 isl_space_free(space);
5776 return map;
5778 if (check_map_space_equal_total_dim(map, space) < 0)
5779 goto error;
5780 return isl_map_reset_space(map, space);
5781 error:
5782 isl_map_free(map);
5783 isl_space_free(space);
5784 return NULL;
5787 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5788 __isl_take isl_space *space)
5790 return set_from_map(isl_map_reset_space(set_to_map(set), space));
5793 /* Compute the parameter domain of the given basic set.
5795 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5797 isl_bool is_params;
5798 isl_space *space;
5799 isl_size n;
5801 is_params = isl_basic_set_is_params(bset);
5802 if (is_params < 0)
5803 return isl_basic_set_free(bset);
5804 if (is_params)
5805 return bset;
5807 n = isl_basic_set_dim(bset, isl_dim_set);
5808 if (n < 0)
5809 return isl_basic_set_free(bset);
5810 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5811 space = isl_basic_set_get_space(bset);
5812 space = isl_space_params(space);
5813 bset = isl_basic_set_reset_space(bset, space);
5814 return bset;
5817 /* Construct a zero-dimensional basic set with the given parameter domain.
5819 __isl_give isl_basic_set *isl_basic_set_from_params(
5820 __isl_take isl_basic_set *bset)
5822 isl_space *space;
5823 space = isl_basic_set_get_space(bset);
5824 space = isl_space_set_from_params(space);
5825 bset = isl_basic_set_reset_space(bset, space);
5826 return bset;
5829 /* Compute the parameter domain of the given set.
5831 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5833 return isl_map_params(set_to_map(set));
5836 /* Construct a zero-dimensional set with the given parameter domain.
5838 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5840 isl_space *space;
5841 space = isl_set_get_space(set);
5842 space = isl_space_set_from_params(space);
5843 set = isl_set_reset_space(set, space);
5844 return set;
5847 /* Compute the parameter domain of the given map.
5849 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5851 isl_space *space;
5852 isl_size n_in, n_out;
5854 n_in = isl_map_dim(map, isl_dim_in);
5855 n_out = isl_map_dim(map, isl_dim_out);
5856 if (n_in < 0 || n_out < 0)
5857 return isl_map_free(map);
5858 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
5859 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
5860 space = isl_map_get_space(map);
5861 space = isl_space_params(space);
5862 map = isl_map_reset_space(map, space);
5863 return map;
5866 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5868 isl_space *space;
5869 isl_size n_out;
5871 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5872 if (n_out < 0)
5873 return isl_basic_map_free(bmap);
5874 space = isl_space_domain(isl_basic_map_get_space(bmap));
5876 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5878 return isl_basic_map_reset_space(bmap, space);
5881 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5883 if (!bmap)
5884 return isl_bool_error;
5885 return isl_space_may_be_set(bmap->dim);
5888 /* Is this basic map actually a set?
5889 * Users should never call this function. Outside of isl,
5890 * the type should indicate whether something is a set or a map.
5892 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5894 if (!bmap)
5895 return isl_bool_error;
5896 return isl_space_is_set(bmap->dim);
5899 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
5901 isl_bool is_set;
5903 is_set = isl_basic_map_is_set(bmap);
5904 if (is_set < 0)
5905 goto error;
5906 if (is_set)
5907 return bmap;
5908 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5909 error:
5910 isl_basic_map_free(bmap);
5911 return NULL;
5914 __isl_give isl_basic_map *isl_basic_map_domain_map(
5915 __isl_take isl_basic_map *bmap)
5917 int i;
5918 isl_space *space;
5919 isl_basic_map *domain;
5920 isl_size nparam, n_in, n_out;
5922 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5923 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5924 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5925 if (nparam < 0 || n_in < 0 || n_out < 0)
5926 return isl_basic_map_free(bmap);
5928 space = isl_basic_map_get_space(bmap);
5929 space = isl_space_from_range(isl_space_domain(space));
5930 domain = isl_basic_map_universe(space);
5932 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5933 bmap = isl_basic_map_apply_range(bmap, domain);
5934 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5936 for (i = 0; i < n_in; ++i)
5937 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5938 isl_dim_out, i);
5940 bmap = isl_basic_map_gauss(bmap, NULL);
5941 return isl_basic_map_finalize(bmap);
5944 __isl_give isl_basic_map *isl_basic_map_range_map(
5945 __isl_take isl_basic_map *bmap)
5947 int i;
5948 isl_space *space;
5949 isl_basic_map *range;
5950 isl_size nparam, n_in, n_out;
5952 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5953 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5954 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5955 if (nparam < 0 || n_in < 0 || n_out < 0)
5956 return isl_basic_map_free(bmap);
5958 space = isl_basic_map_get_space(bmap);
5959 space = isl_space_from_range(isl_space_range(space));
5960 range = isl_basic_map_universe(space);
5962 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5963 bmap = isl_basic_map_apply_range(bmap, range);
5964 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5966 for (i = 0; i < n_out; ++i)
5967 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5968 isl_dim_out, i);
5970 bmap = isl_basic_map_gauss(bmap, NULL);
5971 return isl_basic_map_finalize(bmap);
5974 int isl_map_may_be_set(__isl_keep isl_map *map)
5976 if (!map)
5977 return -1;
5978 return isl_space_may_be_set(map->dim);
5981 /* Is this map actually a set?
5982 * Users should never call this function. Outside of isl,
5983 * the type should indicate whether something is a set or a map.
5985 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5987 if (!map)
5988 return isl_bool_error;
5989 return isl_space_is_set(map->dim);
5992 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5994 int i;
5995 isl_bool is_set;
5996 struct isl_set *set;
5998 is_set = isl_map_is_set(map);
5999 if (is_set < 0)
6000 goto error;
6001 if (is_set)
6002 return set_from_map(map);
6004 map = isl_map_cow(map);
6005 if (!map)
6006 goto error;
6008 set = set_from_map(map);
6009 set->dim = isl_space_range(set->dim);
6010 if (!set->dim)
6011 goto error;
6012 for (i = 0; i < map->n; ++i) {
6013 set->p[i] = isl_basic_map_range(map->p[i]);
6014 if (!set->p[i])
6015 goto error;
6017 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6018 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6019 return set;
6020 error:
6021 isl_map_free(map);
6022 return NULL;
6025 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6026 * to each of its basic maps.
6028 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6029 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6030 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6032 int i;
6033 isl_space *space;
6035 map = isl_map_cow(map);
6036 if (!map)
6037 return NULL;
6039 for (i = 0; i < map->n; ++i) {
6040 map->p[i] = fn_bmap(map->p[i]);
6041 if (!map->p[i])
6042 return isl_map_free(map);
6044 map = isl_map_unmark_normalized(map);
6046 space = isl_map_take_space(map);
6047 space = fn_space(space);
6048 map = isl_map_restore_space(map, space);
6050 return map;
6053 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6055 return isl_map_transform(map, &isl_space_domain_map,
6056 &isl_basic_map_domain_map);
6059 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6061 return isl_map_transform(map, &isl_space_range_map,
6062 &isl_basic_map_range_map);
6065 /* Given a wrapped map of the form A[B -> C],
6066 * return the map A[B -> C] -> B.
6068 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6070 isl_id *id;
6071 isl_map *map;
6073 if (!set)
6074 return NULL;
6075 if (!isl_set_has_tuple_id(set))
6076 return isl_map_domain_map(isl_set_unwrap(set));
6078 id = isl_set_get_tuple_id(set);
6079 map = isl_map_domain_map(isl_set_unwrap(set));
6080 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6082 return map;
6085 __isl_give isl_basic_map *isl_basic_map_from_domain(
6086 __isl_take isl_basic_set *bset)
6088 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6091 __isl_give isl_basic_map *isl_basic_map_from_range(
6092 __isl_take isl_basic_set *bset)
6094 isl_space *space;
6095 space = isl_basic_set_get_space(bset);
6096 space = isl_space_from_range(space);
6097 bset = isl_basic_set_reset_space(bset, space);
6098 return bset_to_bmap(bset);
6101 /* Create a relation with the given set as range.
6102 * The domain of the created relation is a zero-dimensional
6103 * flat anonymous space.
6105 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6107 isl_space *space;
6108 space = isl_set_get_space(set);
6109 space = isl_space_from_range(space);
6110 set = isl_set_reset_space(set, space);
6111 return set_to_map(set);
6114 /* Create a relation with the given set as domain.
6115 * The range of the created relation is a zero-dimensional
6116 * flat anonymous space.
6118 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6120 return isl_map_reverse(isl_map_from_range(set));
6123 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6124 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6126 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6129 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6130 __isl_take isl_set *range)
6132 return isl_map_apply_range(isl_map_reverse(domain), range);
6135 /* Return a newly allocated isl_map with given space and flags and
6136 * room for "n" basic maps.
6137 * Make sure that all cached information is cleared.
6139 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6140 unsigned flags)
6142 struct isl_map *map;
6144 if (!space)
6145 return NULL;
6146 if (n < 0)
6147 isl_die(space->ctx, isl_error_internal,
6148 "negative number of basic maps", goto error);
6149 map = isl_calloc(space->ctx, struct isl_map,
6150 sizeof(struct isl_map) +
6151 (n - 1) * sizeof(struct isl_basic_map *));
6152 if (!map)
6153 goto error;
6155 map->ctx = space->ctx;
6156 isl_ctx_ref(map->ctx);
6157 map->ref = 1;
6158 map->size = n;
6159 map->n = 0;
6160 map->dim = space;
6161 map->flags = flags;
6162 return map;
6163 error:
6164 isl_space_free(space);
6165 return NULL;
6168 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6170 struct isl_basic_map *bmap;
6171 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6172 bmap = isl_basic_map_set_to_empty(bmap);
6173 return bmap;
6176 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6178 struct isl_basic_set *bset;
6179 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6180 bset = isl_basic_set_set_to_empty(bset);
6181 return bset;
6184 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6186 struct isl_basic_map *bmap;
6187 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6188 bmap = isl_basic_map_finalize(bmap);
6189 return bmap;
6192 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6194 struct isl_basic_set *bset;
6195 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6196 bset = isl_basic_set_finalize(bset);
6197 return bset;
6200 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6201 __isl_take isl_space *space)
6203 int i;
6204 isl_size total = isl_space_dim(space, isl_dim_all);
6205 isl_basic_map *bmap;
6207 if (total < 0)
6208 space = isl_space_free(space);
6209 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6210 for (i = 0; i < total; ++i) {
6211 int k = isl_basic_map_alloc_inequality(bmap);
6212 if (k < 0)
6213 goto error;
6214 isl_seq_clr(bmap->ineq[k], 1 + total);
6215 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6217 return bmap;
6218 error:
6219 isl_basic_map_free(bmap);
6220 return NULL;
6223 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6224 __isl_take isl_space *space)
6226 return isl_basic_map_nat_universe(space);
6229 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6231 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6234 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6236 return isl_map_nat_universe(space);
6239 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6241 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6244 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6246 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6249 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6251 struct isl_map *map;
6252 if (!space)
6253 return NULL;
6254 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6255 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6256 return map;
6259 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6261 struct isl_set *set;
6262 if (!space)
6263 return NULL;
6264 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6265 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6266 return set;
6269 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6271 int i;
6272 struct isl_map *dup;
6274 if (!map)
6275 return NULL;
6276 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6277 for (i = 0; i < map->n; ++i)
6278 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6279 return dup;
6282 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6283 __isl_take isl_basic_map *bmap)
6285 if (!bmap || !map)
6286 goto error;
6287 if (isl_basic_map_plain_is_empty(bmap)) {
6288 isl_basic_map_free(bmap);
6289 return map;
6291 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6292 goto error;
6293 isl_assert(map->ctx, map->n < map->size, goto error);
6294 map->p[map->n] = bmap;
6295 map->n++;
6296 map = isl_map_unmark_normalized(map);
6297 return map;
6298 error:
6299 if (map)
6300 isl_map_free(map);
6301 if (bmap)
6302 isl_basic_map_free(bmap);
6303 return NULL;
6306 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6308 int i;
6310 if (!map)
6311 return NULL;
6313 if (--map->ref > 0)
6314 return NULL;
6316 clear_caches(map);
6317 isl_ctx_deref(map->ctx);
6318 for (i = 0; i < map->n; ++i)
6319 isl_basic_map_free(map->p[i]);
6320 isl_space_free(map->dim);
6321 free(map);
6323 return NULL;
6326 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6327 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6329 int j;
6330 isl_size total;
6332 total = isl_basic_map_dim(bmap, isl_dim_all);
6333 if (total < 0)
6334 return isl_basic_map_free(bmap);
6336 bmap = isl_basic_map_cow(bmap);
6337 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6338 j = isl_basic_map_alloc_equality(bmap);
6339 if (j < 0)
6340 goto error;
6341 isl_seq_clr(bmap->eq[j] + 1, total);
6342 isl_int_set_si(bmap->eq[j][pos], -1);
6343 isl_int_set_si(bmap->eq[j][0], value);
6344 bmap = isl_basic_map_simplify(bmap);
6345 return isl_basic_map_finalize(bmap);
6346 error:
6347 isl_basic_map_free(bmap);
6348 return NULL;
6351 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6352 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6354 int j;
6355 isl_size total;
6357 total = isl_basic_map_dim(bmap, isl_dim_all);
6358 if (total < 0)
6359 return isl_basic_map_free(bmap);
6361 bmap = isl_basic_map_cow(bmap);
6362 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6363 j = isl_basic_map_alloc_equality(bmap);
6364 if (j < 0)
6365 goto error;
6366 isl_seq_clr(bmap->eq[j] + 1, total);
6367 isl_int_set_si(bmap->eq[j][pos], -1);
6368 isl_int_set(bmap->eq[j][0], value);
6369 bmap = isl_basic_map_simplify(bmap);
6370 return isl_basic_map_finalize(bmap);
6371 error:
6372 isl_basic_map_free(bmap);
6373 return NULL;
6376 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6377 enum isl_dim_type type, unsigned pos, int value)
6379 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6380 return isl_basic_map_free(bmap);
6381 return isl_basic_map_fix_pos_si(bmap,
6382 isl_basic_map_offset(bmap, type) + pos, value);
6385 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6386 enum isl_dim_type type, unsigned pos, isl_int value)
6388 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6389 return isl_basic_map_free(bmap);
6390 return isl_basic_map_fix_pos(bmap,
6391 isl_basic_map_offset(bmap, type) + pos, value);
6394 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6395 * to be equal to "v".
6397 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6398 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6400 if (!bmap || !v)
6401 goto error;
6402 if (!isl_val_is_int(v))
6403 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6404 "expecting integer value", goto error);
6405 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6406 goto error;
6407 pos += isl_basic_map_offset(bmap, type);
6408 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6409 isl_val_free(v);
6410 return bmap;
6411 error:
6412 isl_basic_map_free(bmap);
6413 isl_val_free(v);
6414 return NULL;
6417 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6418 * to be equal to "v".
6420 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6421 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6423 return isl_basic_map_fix_val(bset, type, pos, v);
6426 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6427 enum isl_dim_type type, unsigned pos, int value)
6429 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6430 type, pos, value));
6433 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6434 enum isl_dim_type type, unsigned pos, isl_int value)
6436 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6437 type, pos, value));
6440 /* Remove the basic map at position "i" from "map" if this basic map
6441 * is (obviously) empty.
6443 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6445 isl_bool empty;
6447 if (!map)
6448 return NULL;
6450 empty = isl_basic_map_plain_is_empty(map->p[i]);
6451 if (empty < 0)
6452 return isl_map_free(map);
6453 if (!empty)
6454 return map;
6456 isl_basic_map_free(map->p[i]);
6457 map->n--;
6458 if (i != map->n) {
6459 map->p[i] = map->p[map->n];
6460 map = isl_map_unmark_normalized(map);
6464 return map;
6467 /* Perform "fn" on each basic map of "map", where we may not be holding
6468 * the only reference to "map".
6469 * In particular, "fn" should be a semantics preserving operation
6470 * that we want to apply to all copies of "map". We therefore need
6471 * to be careful not to modify "map" in a way that breaks "map"
6472 * in case anything goes wrong.
6474 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6475 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6477 struct isl_basic_map *bmap;
6478 int i;
6480 if (!map)
6481 return NULL;
6483 for (i = map->n - 1; i >= 0; --i) {
6484 bmap = isl_basic_map_copy(map->p[i]);
6485 bmap = fn(bmap);
6486 if (!bmap)
6487 goto error;
6488 isl_basic_map_free(map->p[i]);
6489 map->p[i] = bmap;
6490 map = remove_if_empty(map, i);
6491 if (!map)
6492 return NULL;
6495 return map;
6496 error:
6497 isl_map_free(map);
6498 return NULL;
6501 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6502 enum isl_dim_type type, unsigned pos, int value)
6504 int i;
6506 map = isl_map_cow(map);
6507 if (isl_map_check_range(map, type, pos, 1) < 0)
6508 return isl_map_free(map);
6509 for (i = map->n - 1; i >= 0; --i) {
6510 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6511 map = remove_if_empty(map, i);
6512 if (!map)
6513 return NULL;
6515 map = isl_map_unmark_normalized(map);
6516 return map;
6519 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6520 enum isl_dim_type type, unsigned pos, int value)
6522 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6525 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6526 enum isl_dim_type type, unsigned pos, isl_int value)
6528 int i;
6530 map = isl_map_cow(map);
6531 if (isl_map_check_range(map, type, pos, 1) < 0)
6532 return isl_map_free(map);
6533 for (i = 0; i < map->n; ++i) {
6534 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6535 if (!map->p[i])
6536 goto error;
6538 map = isl_map_unmark_normalized(map);
6539 return map;
6540 error:
6541 isl_map_free(map);
6542 return NULL;
6545 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6546 enum isl_dim_type type, unsigned pos, isl_int value)
6548 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6551 /* Fix the value of the variable at position "pos" of type "type" of "map"
6552 * to be equal to "v".
6554 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6555 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6557 int i;
6559 map = isl_map_cow(map);
6560 if (!map || !v)
6561 goto error;
6563 if (!isl_val_is_int(v))
6564 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6565 "expecting integer value", goto error);
6566 if (isl_map_check_range(map, type, pos, 1) < 0)
6567 goto error;
6568 for (i = map->n - 1; i >= 0; --i) {
6569 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6570 isl_val_copy(v));
6571 map = remove_if_empty(map, i);
6572 if (!map)
6573 goto error;
6575 map = isl_map_unmark_normalized(map);
6576 isl_val_free(v);
6577 return map;
6578 error:
6579 isl_map_free(map);
6580 isl_val_free(v);
6581 return NULL;
6584 /* Fix the value of the variable at position "pos" of type "type" of "set"
6585 * to be equal to "v".
6587 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6588 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6590 return isl_map_fix_val(set, type, pos, v);
6593 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6594 unsigned input, int value)
6596 return isl_map_fix_si(map, isl_dim_in, input, value);
6599 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6600 int value)
6602 return set_from_map(isl_map_fix_si(set_to_map(set),
6603 isl_dim_set, dim, value));
6606 static __isl_give isl_basic_map *basic_map_bound_si(
6607 __isl_take isl_basic_map *bmap,
6608 enum isl_dim_type type, unsigned pos, int value, int upper)
6610 int j;
6611 isl_size total;
6613 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6614 return isl_basic_map_free(bmap);
6615 total = isl_basic_map_dim(bmap, isl_dim_all);
6616 if (total < 0)
6617 return isl_basic_map_free(bmap);
6618 pos += isl_basic_map_offset(bmap, type);
6619 bmap = isl_basic_map_cow(bmap);
6620 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6621 j = isl_basic_map_alloc_inequality(bmap);
6622 if (j < 0)
6623 goto error;
6624 isl_seq_clr(bmap->ineq[j], 1 + total);
6625 if (upper) {
6626 isl_int_set_si(bmap->ineq[j][pos], -1);
6627 isl_int_set_si(bmap->ineq[j][0], value);
6628 } else {
6629 isl_int_set_si(bmap->ineq[j][pos], 1);
6630 isl_int_set_si(bmap->ineq[j][0], -value);
6632 bmap = isl_basic_map_simplify(bmap);
6633 return isl_basic_map_finalize(bmap);
6634 error:
6635 isl_basic_map_free(bmap);
6636 return NULL;
6639 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6640 __isl_take isl_basic_map *bmap,
6641 enum isl_dim_type type, unsigned pos, int value)
6643 return basic_map_bound_si(bmap, type, pos, value, 0);
6646 /* Constrain the values of the given dimension to be no greater than "value".
6648 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6649 __isl_take isl_basic_map *bmap,
6650 enum isl_dim_type type, unsigned pos, int value)
6652 return basic_map_bound_si(bmap, type, pos, value, 1);
6655 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6656 enum isl_dim_type type, unsigned pos, int value, int upper)
6658 int i;
6660 map = isl_map_cow(map);
6661 if (isl_map_check_range(map, type, pos, 1) < 0)
6662 return isl_map_free(map);
6663 for (i = 0; i < map->n; ++i) {
6664 map->p[i] = basic_map_bound_si(map->p[i],
6665 type, pos, value, upper);
6666 if (!map->p[i])
6667 goto error;
6669 map = isl_map_unmark_normalized(map);
6670 return map;
6671 error:
6672 isl_map_free(map);
6673 return NULL;
6676 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6677 enum isl_dim_type type, unsigned pos, int value)
6679 return map_bound_si(map, type, pos, value, 0);
6682 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6683 enum isl_dim_type type, unsigned pos, int value)
6685 return map_bound_si(map, type, pos, value, 1);
6688 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6689 enum isl_dim_type type, unsigned pos, int value)
6691 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6692 type, pos, value));
6695 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6696 enum isl_dim_type type, unsigned pos, int value)
6698 return isl_map_upper_bound_si(set, type, pos, value);
6701 /* Bound the given variable of "bmap" from below (or above is "upper"
6702 * is set) to "value".
6704 static __isl_give isl_basic_map *basic_map_bound(
6705 __isl_take isl_basic_map *bmap,
6706 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6708 int j;
6709 isl_size total;
6711 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6712 return isl_basic_map_free(bmap);
6713 total = isl_basic_map_dim(bmap, isl_dim_all);
6714 if (total < 0)
6715 return isl_basic_map_free(bmap);
6716 pos += isl_basic_map_offset(bmap, type);
6717 bmap = isl_basic_map_cow(bmap);
6718 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6719 j = isl_basic_map_alloc_inequality(bmap);
6720 if (j < 0)
6721 goto error;
6722 isl_seq_clr(bmap->ineq[j], 1 + total);
6723 if (upper) {
6724 isl_int_set_si(bmap->ineq[j][pos], -1);
6725 isl_int_set(bmap->ineq[j][0], value);
6726 } else {
6727 isl_int_set_si(bmap->ineq[j][pos], 1);
6728 isl_int_neg(bmap->ineq[j][0], value);
6730 bmap = isl_basic_map_simplify(bmap);
6731 return isl_basic_map_finalize(bmap);
6732 error:
6733 isl_basic_map_free(bmap);
6734 return NULL;
6737 /* Bound the given variable of "map" from below (or above is "upper"
6738 * is set) to "value".
6740 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6741 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6743 int i;
6745 map = isl_map_cow(map);
6746 if (isl_map_check_range(map, type, pos, 1) < 0)
6747 return isl_map_free(map);
6748 for (i = map->n - 1; i >= 0; --i) {
6749 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6750 map = remove_if_empty(map, i);
6751 if (!map)
6752 return NULL;
6754 map = isl_map_unmark_normalized(map);
6755 return map;
6758 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6759 enum isl_dim_type type, unsigned pos, isl_int value)
6761 return map_bound(map, type, pos, value, 0);
6764 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6765 enum isl_dim_type type, unsigned pos, isl_int value)
6767 return map_bound(map, type, pos, value, 1);
6770 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6771 enum isl_dim_type type, unsigned pos, isl_int value)
6773 return isl_map_lower_bound(set, type, pos, value);
6776 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6777 enum isl_dim_type type, unsigned pos, isl_int value)
6779 return isl_map_upper_bound(set, type, pos, value);
6782 /* Force the values of the variable at position "pos" of type "type" of "map"
6783 * to be no smaller than "value".
6785 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
6786 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6788 if (!value)
6789 goto error;
6790 if (!isl_val_is_int(value))
6791 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6792 "expecting integer value", goto error);
6793 map = isl_map_lower_bound(map, type, pos, value->n);
6794 isl_val_free(value);
6795 return map;
6796 error:
6797 isl_val_free(value);
6798 isl_map_free(map);
6799 return NULL;
6802 /* Force the values of the variable at position "pos" of type "type" of "set"
6803 * to be no smaller than "value".
6805 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6806 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6808 isl_map *map;
6810 map = set_to_map(set);
6811 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
6814 /* Force the values of the variable at position "pos" of type "type" of "map"
6815 * to be no greater than "value".
6817 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
6818 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6820 if (!value)
6821 goto error;
6822 if (!isl_val_is_int(value))
6823 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6824 "expecting integer value", goto error);
6825 map = isl_map_upper_bound(map, type, pos, value->n);
6826 isl_val_free(value);
6827 return map;
6828 error:
6829 isl_val_free(value);
6830 isl_map_free(map);
6831 return NULL;
6834 /* Force the values of the variable at position "pos" of type "type" of "set"
6835 * to be no greater than "value".
6837 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6838 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6840 isl_map *map;
6842 map = set_to_map(set);
6843 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
6846 #undef BASE
6847 #define BASE val
6848 #include "isl_map_bound_templ.c"
6850 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6851 * for each set dimension, by treating the set as a map.
6853 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
6854 __isl_take isl_multi_val *bound,
6855 __isl_give isl_map *map_bound(__isl_take isl_map *map,
6856 unsigned pos, __isl_take isl_val *value))
6858 isl_map *map;
6860 map = set_to_map(set);
6861 return set_from_map(map_bound_multi_val(map, bound, map_bound));
6864 #undef BASE
6865 #define BASE pw_aff
6866 #include "isl_map_bound_templ.c"
6868 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6869 * for each set dimension, by converting the set and the bound
6870 * to objects living in a map space.
6872 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
6873 __isl_take isl_multi_pw_aff *bound,
6874 __isl_give isl_map *set_bound(__isl_take isl_map *map,
6875 unsigned pos, __isl_take TYPE *value))
6877 isl_map *map;
6879 map = isl_map_from_range(set);
6880 bound = isl_multi_pw_aff_from_range(bound);
6881 map = map_bound_multi_pw_aff(map, bound, set_bound);
6882 return isl_map_range(map);
6885 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
6886 * setting a bound on the given output dimension.
6888 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
6889 unsigned pos, __isl_take isl_val *v)
6891 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
6894 /* Force the values of the set dimensions of "set"
6895 * to be no smaller than the corresponding values in "lower".
6897 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
6898 __isl_take isl_multi_val *lower)
6900 return set_bound_multi_val(set, lower, &map_lower_bound_val);
6903 /* Force the values of the output dimensions of "map"
6904 * to be no smaller than the corresponding values in "lower".
6906 __isl_give isl_map *isl_map_lower_bound_multi_val(__isl_take isl_map *map,
6907 __isl_take isl_multi_val *lower)
6909 return map_bound_multi_val(map, lower, &map_lower_bound_val);
6912 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
6913 * setting a bound on the given output dimension.
6915 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
6916 unsigned pos, __isl_take isl_val *v)
6918 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
6921 /* Force the values of the set dimensions of "set"
6922 * to be no greater than the corresponding values in "upper".
6924 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
6925 __isl_take isl_multi_val *upper)
6927 return set_bound_multi_val(set, upper, &map_upper_bound_val);
6930 /* Force the values of the set dimensions of "set"
6931 * to be no greater than the corresponding values in "upper".
6933 __isl_give isl_map *isl_map_upper_bound_multi_val(__isl_take isl_map *map,
6934 __isl_take isl_multi_val *upper)
6936 return map_bound_multi_val(map, upper, &map_upper_bound_val);
6939 /* Force the symbolic constant expression "bound"
6940 * to satisfy the relation "order" with respect to
6941 * the output variable at position "pos" of "map".
6943 * Create an affine expression representing the output variable
6944 * in terms of the range and
6945 * compare it using "order" to "bound" (defined on the domain).
6946 * The result is a relation between elements in domain and range that
6947 * can be intersected with "map".
6949 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
6950 unsigned pos, __isl_take isl_pw_aff *bound,
6951 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
6952 __isl_take isl_pw_aff *pa2))
6954 isl_space *space;
6955 isl_local_space *ls;
6956 isl_pw_aff *var;
6958 space = isl_space_range(isl_map_get_space(map));
6959 ls = isl_local_space_from_space(space);
6960 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
6961 map = isl_map_intersect(map, order(bound, var));
6962 return map;
6965 /* Force the values of the output variable at position "pos" of "map"
6966 * to be no smaller than the symbolic constant expression "lower".
6968 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
6969 unsigned pos, __isl_take isl_pw_aff *lower)
6971 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
6974 /* Force the values of the output variable at position "pos" of "map"
6975 * to be no greater than the symbolic constant expression "upper".
6977 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
6978 unsigned pos, __isl_take isl_pw_aff *upper)
6980 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
6983 /* Force the values of the set dimensions of "set"
6984 * to be no smaller than the corresponding constant symbolic expressions
6985 * in "lower".
6987 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
6988 __isl_take isl_multi_pw_aff *lower)
6990 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
6993 /* Force the values of the set dimensions of "set"
6994 * to be no greater than the corresponding constant symbolic expressions
6995 * in "upper".
6997 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
6998 __isl_take isl_multi_pw_aff *upper)
7000 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7003 /* Force the values of the output dimensions of "map"
7004 * to be no smaller than the corresponding constant symbolic expressions
7005 * in "lower".
7007 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7008 __isl_take isl_multi_pw_aff *lower)
7010 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7013 /* Force the values of the output dimensions of "map"
7014 * to be no greater than the corresponding constant symbolic expressions
7015 * in "upper".
7017 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7018 __isl_take isl_multi_pw_aff *upper)
7020 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7023 /* Bound the given variable of "bset" from below (or above is "upper"
7024 * is set) to "value".
7026 static __isl_give isl_basic_set *isl_basic_set_bound(
7027 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7028 isl_int value, int upper)
7030 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7031 type, pos, value, upper));
7034 /* Bound the given variable of "bset" from below (or above is "upper"
7035 * is set) to "value".
7037 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7038 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7039 __isl_take isl_val *value, int upper)
7041 if (!value)
7042 goto error;
7043 if (!isl_val_is_int(value))
7044 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7045 "expecting integer value", goto error);
7046 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7047 isl_val_free(value);
7048 return bset;
7049 error:
7050 isl_val_free(value);
7051 isl_basic_set_free(bset);
7052 return NULL;
7055 /* Bound the given variable of "bset" from below to "value".
7057 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7058 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7059 __isl_take isl_val *value)
7061 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7064 /* Bound the given variable of "bset" from above to "value".
7066 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7067 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7068 __isl_take isl_val *value)
7070 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7073 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7075 return isl_map_transform(map, &isl_space_reverse,
7076 &isl_basic_map_reverse);
7079 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7081 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7083 return isl_map_transform(map, &isl_space_range_reverse,
7084 &isl_basic_map_range_reverse);
7087 #undef TYPE
7088 #define TYPE isl_pw_multi_aff
7089 #undef SUFFIX
7090 #define SUFFIX _pw_multi_aff
7091 #undef EMPTY
7092 #define EMPTY isl_pw_multi_aff_empty
7093 #undef ADD
7094 #define ADD isl_pw_multi_aff_union_add
7095 #include "isl_map_lexopt_templ.c"
7097 /* Given a map "map", compute the lexicographically minimal
7098 * (or maximal) image element for each domain element in dom,
7099 * in the form of an isl_pw_multi_aff.
7100 * If "empty" is not NULL, then set *empty to those elements in dom that
7101 * do not have an image element.
7102 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7103 * should be computed over the domain of "map". "empty" is also NULL
7104 * in this case.
7106 * We first compute the lexicographically minimal or maximal element
7107 * in the first basic map. This results in a partial solution "res"
7108 * and a subset "todo" of dom that still need to be handled.
7109 * We then consider each of the remaining maps in "map" and successively
7110 * update both "res" and "todo".
7111 * If "empty" is NULL, then the todo sets are not needed and therefore
7112 * also not computed.
7114 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7115 __isl_take isl_map *map, __isl_take isl_set *dom,
7116 __isl_give isl_set **empty, unsigned flags)
7118 int i;
7119 int full;
7120 isl_pw_multi_aff *res;
7121 isl_set *todo;
7123 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7124 if (!map || (!full && !dom))
7125 goto error;
7127 if (isl_map_plain_is_empty(map)) {
7128 if (empty)
7129 *empty = dom;
7130 else
7131 isl_set_free(dom);
7132 return isl_pw_multi_aff_from_map(map);
7135 res = basic_map_partial_lexopt_pw_multi_aff(
7136 isl_basic_map_copy(map->p[0]),
7137 isl_set_copy(dom), empty, flags);
7139 if (empty)
7140 todo = *empty;
7141 for (i = 1; i < map->n; ++i) {
7142 isl_pw_multi_aff *res_i;
7144 res_i = basic_map_partial_lexopt_pw_multi_aff(
7145 isl_basic_map_copy(map->p[i]),
7146 isl_set_copy(dom), empty, flags);
7148 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7149 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7150 else
7151 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7153 if (empty)
7154 todo = isl_set_intersect(todo, *empty);
7157 isl_set_free(dom);
7158 isl_map_free(map);
7160 if (empty)
7161 *empty = todo;
7163 return res;
7164 error:
7165 if (empty)
7166 *empty = NULL;
7167 isl_set_free(dom);
7168 isl_map_free(map);
7169 return NULL;
7172 #undef TYPE
7173 #define TYPE isl_map
7174 #undef SUFFIX
7175 #define SUFFIX
7176 #undef EMPTY
7177 #define EMPTY isl_map_empty
7178 #undef ADD
7179 #define ADD isl_map_union_disjoint
7180 #include "isl_map_lexopt_templ.c"
7182 /* Given a map "map", compute the lexicographically minimal
7183 * (or maximal) image element for each domain element in "dom",
7184 * in the form of an isl_map.
7185 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7186 * do not have an image element.
7187 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7188 * should be computed over the domain of "map". "empty" is also NULL
7189 * in this case.
7191 * If the input consists of more than one disjunct, then first
7192 * compute the desired result in the form of an isl_pw_multi_aff and
7193 * then convert that into an isl_map.
7195 * This function used to have an explicit implementation in terms
7196 * of isl_maps, but it would continually intersect the domains of
7197 * partial results with the complement of the domain of the next
7198 * partial solution, potentially leading to an explosion in the number
7199 * of disjuncts if there are several disjuncts in the input.
7200 * An even earlier implementation of this function would look for
7201 * better results in the domain of the partial result and for extra
7202 * results in the complement of this domain, which would lead to
7203 * even more splintering.
7205 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7206 __isl_take isl_map *map, __isl_take isl_set *dom,
7207 __isl_give isl_set **empty, unsigned flags)
7209 int full;
7210 struct isl_map *res;
7211 isl_pw_multi_aff *pma;
7213 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7214 if (!map || (!full && !dom))
7215 goto error;
7217 if (isl_map_plain_is_empty(map)) {
7218 if (empty)
7219 *empty = dom;
7220 else
7221 isl_set_free(dom);
7222 return map;
7225 if (map->n == 1) {
7226 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7227 dom, empty, flags);
7228 isl_map_free(map);
7229 return res;
7232 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7233 flags);
7234 return isl_map_from_pw_multi_aff_internal(pma);
7235 error:
7236 if (empty)
7237 *empty = NULL;
7238 isl_set_free(dom);
7239 isl_map_free(map);
7240 return NULL;
7243 __isl_give isl_map *isl_map_partial_lexmax(
7244 __isl_take isl_map *map, __isl_take isl_set *dom,
7245 __isl_give isl_set **empty)
7247 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7250 __isl_give isl_map *isl_map_partial_lexmin(
7251 __isl_take isl_map *map, __isl_take isl_set *dom,
7252 __isl_give isl_set **empty)
7254 return isl_map_partial_lexopt(map, dom, empty, 0);
7257 __isl_give isl_set *isl_set_partial_lexmin(
7258 __isl_take isl_set *set, __isl_take isl_set *dom,
7259 __isl_give isl_set **empty)
7261 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7262 dom, empty));
7265 __isl_give isl_set *isl_set_partial_lexmax(
7266 __isl_take isl_set *set, __isl_take isl_set *dom,
7267 __isl_give isl_set **empty)
7269 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7270 dom, empty));
7273 /* Compute the lexicographic minimum (or maximum if "flags" includes
7274 * ISL_OPT_MAX) of "bset" over its parametric domain.
7276 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7277 unsigned flags)
7279 return isl_basic_map_lexopt(bset, flags);
7282 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7284 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7287 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7289 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7292 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7294 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7297 /* Compute the lexicographic minimum of "bset" over its parametric domain
7298 * for the purpose of quantifier elimination.
7299 * That is, find an explicit representation for all the existentially
7300 * quantified variables in "bset" by computing their lexicographic
7301 * minimum.
7303 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7304 __isl_take isl_basic_set *bset)
7306 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7309 /* Given a basic map with one output dimension, compute the minimum or
7310 * maximum of that dimension as an isl_pw_aff.
7312 * Compute the optimum as a lexicographic optimum over the single
7313 * output dimension and extract the single isl_pw_aff from the result.
7315 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7316 int max)
7318 isl_pw_multi_aff *pma;
7319 isl_pw_aff *pwaff;
7321 bmap = isl_basic_map_copy(bmap);
7322 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7323 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7324 isl_pw_multi_aff_free(pma);
7326 return pwaff;
7329 /* Compute the minimum or maximum of the given output dimension
7330 * as a function of the parameters and the input dimensions,
7331 * but independently of the other output dimensions.
7333 * We first project out the other output dimension and then compute
7334 * the "lexicographic" maximum in each basic map, combining the results
7335 * using isl_pw_aff_union_max.
7337 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7338 int max)
7340 int i;
7341 isl_pw_aff *pwaff;
7342 isl_size n_out;
7344 n_out = isl_map_dim(map, isl_dim_out);
7345 if (n_out < 0)
7346 map = isl_map_free(map);
7347 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7348 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7349 if (!map)
7350 return NULL;
7352 if (map->n == 0) {
7353 isl_space *space = isl_map_get_space(map);
7354 isl_map_free(map);
7355 return isl_pw_aff_empty(space);
7358 pwaff = basic_map_dim_opt(map->p[0], max);
7359 for (i = 1; i < map->n; ++i) {
7360 isl_pw_aff *pwaff_i;
7362 pwaff_i = basic_map_dim_opt(map->p[i], max);
7363 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7366 isl_map_free(map);
7368 return pwaff;
7371 /* Compute the minimum of the given output dimension as a function of the
7372 * parameters and input dimensions, but independently of
7373 * the other output dimensions.
7375 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7377 return map_dim_opt(map, pos, 0);
7380 /* Compute the maximum of the given output dimension as a function of the
7381 * parameters and input dimensions, but independently of
7382 * the other output dimensions.
7384 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7386 return map_dim_opt(map, pos, 1);
7389 /* Compute the minimum or maximum of the given set dimension
7390 * as a function of the parameters,
7391 * but independently of the other set dimensions.
7393 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7394 int max)
7396 return map_dim_opt(set, pos, max);
7399 /* Compute the maximum of the given set dimension as a function of the
7400 * parameters, but independently of the other set dimensions.
7402 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7404 return set_dim_opt(set, pos, 1);
7407 /* Compute the minimum of the given set dimension as a function of the
7408 * parameters, but independently of the other set dimensions.
7410 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7412 return set_dim_opt(set, pos, 0);
7415 /* Apply a preimage specified by "mat" on the parameters of "bset".
7416 * bset is assumed to have only parameters and divs.
7418 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7419 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7421 isl_size nparam;
7423 nparam = isl_basic_set_dim(bset, isl_dim_param);
7424 if (nparam < 0 || !mat)
7425 goto error;
7427 bset->dim = isl_space_cow(bset->dim);
7428 if (!bset->dim)
7429 goto error;
7431 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7433 bset->dim->nparam = 0;
7434 bset->dim->n_out = nparam;
7435 bset = isl_basic_set_preimage(bset, mat);
7436 if (bset) {
7437 bset->dim->nparam = bset->dim->n_out;
7438 bset->dim->n_out = 0;
7440 return bset;
7441 error:
7442 isl_mat_free(mat);
7443 isl_basic_set_free(bset);
7444 return NULL;
7447 /* Apply a preimage specified by "mat" on the parameters of "set".
7448 * set is assumed to have only parameters and divs.
7450 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7451 __isl_take isl_mat *mat)
7453 isl_space *space;
7454 isl_size nparam;
7456 nparam = isl_set_dim(set, isl_dim_param);
7457 if (nparam < 0 || !mat)
7458 goto error;
7460 if (mat->n_row != 1 + nparam)
7461 isl_die(isl_set_get_ctx(set), isl_error_internal,
7462 "unexpected number of rows", goto error);
7464 space = isl_set_get_space(set);
7465 space = isl_space_move_dims(space, isl_dim_set, 0,
7466 isl_dim_param, 0, nparam);
7467 set = isl_set_reset_space(set, space);
7468 set = isl_set_preimage(set, mat);
7469 nparam = isl_set_dim(set, isl_dim_out);
7470 if (nparam < 0)
7471 set = isl_set_free(set);
7472 space = isl_set_get_space(set);
7473 space = isl_space_move_dims(space, isl_dim_param, 0,
7474 isl_dim_out, 0, nparam);
7475 set = isl_set_reset_space(set, space);
7476 return set;
7477 error:
7478 isl_mat_free(mat);
7479 isl_set_free(set);
7480 return NULL;
7483 /* Intersect the basic set "bset" with the affine space specified by the
7484 * equalities in "eq".
7486 static __isl_give isl_basic_set *basic_set_append_equalities(
7487 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7489 int i, k;
7490 unsigned len;
7492 if (!bset || !eq)
7493 goto error;
7495 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7496 if (!bset)
7497 goto error;
7499 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7500 for (i = 0; i < eq->n_row; ++i) {
7501 k = isl_basic_set_alloc_equality(bset);
7502 if (k < 0)
7503 goto error;
7504 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7505 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7507 isl_mat_free(eq);
7509 bset = isl_basic_set_gauss(bset, NULL);
7510 bset = isl_basic_set_finalize(bset);
7512 return bset;
7513 error:
7514 isl_mat_free(eq);
7515 isl_basic_set_free(bset);
7516 return NULL;
7519 /* Intersect the set "set" with the affine space specified by the
7520 * equalities in "eq".
7522 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
7523 __isl_take isl_mat *eq)
7525 int i;
7527 if (!set || !eq)
7528 goto error;
7530 for (i = 0; i < set->n; ++i) {
7531 set->p[i] = basic_set_append_equalities(set->p[i],
7532 isl_mat_copy(eq));
7533 if (!set->p[i])
7534 goto error;
7536 isl_mat_free(eq);
7537 return set;
7538 error:
7539 isl_mat_free(eq);
7540 isl_set_free(set);
7541 return NULL;
7544 /* Given a basic set "bset" that only involves parameters and existentially
7545 * quantified variables, return the index of the first equality
7546 * that only involves parameters. If there is no such equality then
7547 * return bset->n_eq.
7549 * This function assumes that isl_basic_set_gauss has been called on "bset".
7551 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7553 int i, j;
7554 isl_size nparam, n_div;
7556 nparam = isl_basic_set_dim(bset, isl_dim_param);
7557 n_div = isl_basic_set_dim(bset, isl_dim_div);
7558 if (nparam < 0 || n_div < 0)
7559 return -1;
7561 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7562 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7563 ++i;
7566 return i;
7569 /* Compute an explicit representation for the existentially quantified
7570 * variables in "bset" by computing the "minimal value" of the set
7571 * variables. Since there are no set variables, the computation of
7572 * the minimal value essentially computes an explicit representation
7573 * of the non-empty part(s) of "bset".
7575 * The input only involves parameters and existentially quantified variables.
7576 * All equalities among parameters have been removed.
7578 * Since the existentially quantified variables in the result are in general
7579 * going to be different from those in the input, we first replace
7580 * them by the minimal number of variables based on their equalities.
7581 * This should simplify the parametric integer programming.
7583 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7585 isl_morph *morph1, *morph2;
7586 isl_set *set;
7587 isl_size n;
7589 if (!bset)
7590 return NULL;
7591 if (bset->n_eq == 0)
7592 return isl_basic_set_lexmin_compute_divs(bset);
7594 morph1 = isl_basic_set_parameter_compression(bset);
7595 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7596 bset = isl_basic_set_lift(bset);
7597 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7598 bset = isl_morph_basic_set(morph2, bset);
7599 n = isl_basic_set_dim(bset, isl_dim_set);
7600 if (n < 0)
7601 bset = isl_basic_set_free(bset);
7602 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7604 set = isl_basic_set_lexmin_compute_divs(bset);
7606 set = isl_morph_set(isl_morph_inverse(morph1), set);
7608 return set;
7611 /* Project the given basic set onto its parameter domain, possibly introducing
7612 * new, explicit, existential variables in the constraints.
7613 * The input has parameters and (possibly implicit) existential variables.
7614 * The output has the same parameters, but only
7615 * explicit existentially quantified variables.
7617 * The actual projection is performed by pip, but pip doesn't seem
7618 * to like equalities very much, so we first remove the equalities
7619 * among the parameters by performing a variable compression on
7620 * the parameters. Afterward, an inverse transformation is performed
7621 * and the equalities among the parameters are inserted back in.
7623 * The variable compression on the parameters may uncover additional
7624 * equalities that were only implicit before. We therefore check
7625 * if there are any new parameter equalities in the result and
7626 * if so recurse. The removal of parameter equalities is required
7627 * for the parameter compression performed by base_compute_divs.
7629 static __isl_give isl_set *parameter_compute_divs(
7630 __isl_take isl_basic_set *bset)
7632 int i;
7633 struct isl_mat *eq;
7634 struct isl_mat *T, *T2;
7635 struct isl_set *set;
7636 isl_size nparam;
7638 bset = isl_basic_set_cow(bset);
7639 if (!bset)
7640 return NULL;
7642 if (bset->n_eq == 0)
7643 return base_compute_divs(bset);
7645 bset = isl_basic_set_gauss(bset, NULL);
7646 if (!bset)
7647 return NULL;
7648 if (isl_basic_set_plain_is_empty(bset))
7649 return isl_set_from_basic_set(bset);
7651 i = first_parameter_equality(bset);
7652 if (i == bset->n_eq)
7653 return base_compute_divs(bset);
7655 nparam = isl_basic_set_dim(bset, isl_dim_param);
7656 if (nparam < 0)
7657 return isl_set_from_basic_set(isl_basic_set_free(bset));
7658 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7659 0, 1 + nparam);
7660 eq = isl_mat_cow(eq);
7661 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7662 if (T && T->n_col == 0) {
7663 isl_mat_free(T);
7664 isl_mat_free(T2);
7665 isl_mat_free(eq);
7666 bset = isl_basic_set_set_to_empty(bset);
7667 return isl_set_from_basic_set(bset);
7669 bset = basic_set_parameter_preimage(bset, T);
7671 i = first_parameter_equality(bset);
7672 if (!bset)
7673 set = NULL;
7674 else if (i == bset->n_eq)
7675 set = base_compute_divs(bset);
7676 else
7677 set = parameter_compute_divs(bset);
7678 set = set_parameter_preimage(set, T2);
7679 set = set_append_equalities(set, eq);
7680 return set;
7683 /* Insert the divs from "ls" before those of "bmap".
7685 * The number of columns is not changed, which means that the last
7686 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7687 * The caller is responsible for removing the same number of dimensions
7688 * from the space of "bmap".
7690 static __isl_give isl_basic_map *insert_divs_from_local_space(
7691 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7693 int i;
7694 isl_size n_div;
7695 int old_n_div;
7697 n_div = isl_local_space_dim(ls, isl_dim_div);
7698 if (n_div < 0)
7699 return isl_basic_map_free(bmap);
7700 if (n_div == 0)
7701 return bmap;
7703 old_n_div = bmap->n_div;
7704 bmap = insert_div_rows(bmap, n_div);
7705 if (!bmap)
7706 return NULL;
7708 for (i = 0; i < n_div; ++i) {
7709 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7710 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7713 return bmap;
7716 /* Replace the space of "bmap" by the space and divs of "ls".
7718 * If "ls" has any divs, then we simplify the result since we may
7719 * have discovered some additional equalities that could simplify
7720 * the div expressions.
7722 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7723 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7725 isl_size n_div;
7727 bmap = isl_basic_map_cow(bmap);
7728 n_div = isl_local_space_dim(ls, isl_dim_div);
7729 if (!bmap || n_div < 0)
7730 goto error;
7732 bmap = insert_divs_from_local_space(bmap, ls);
7733 if (!bmap)
7734 goto error;
7736 isl_space_free(bmap->dim);
7737 bmap->dim = isl_local_space_get_space(ls);
7738 if (!bmap->dim)
7739 goto error;
7741 isl_local_space_free(ls);
7742 if (n_div > 0)
7743 bmap = isl_basic_map_simplify(bmap);
7744 bmap = isl_basic_map_finalize(bmap);
7745 return bmap;
7746 error:
7747 isl_basic_map_free(bmap);
7748 isl_local_space_free(ls);
7749 return NULL;
7752 /* Replace the space of "map" by the space and divs of "ls".
7754 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7755 __isl_take isl_local_space *ls)
7757 int i;
7759 map = isl_map_cow(map);
7760 if (!map || !ls)
7761 goto error;
7763 for (i = 0; i < map->n; ++i) {
7764 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7765 isl_local_space_copy(ls));
7766 if (!map->p[i])
7767 goto error;
7769 isl_space_free(isl_map_take_space(map));
7770 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
7772 isl_local_space_free(ls);
7773 return map;
7774 error:
7775 isl_local_space_free(ls);
7776 isl_map_free(map);
7777 return NULL;
7780 /* Compute an explicit representation for the existentially
7781 * quantified variables for which do not know any explicit representation yet.
7783 * We first sort the existentially quantified variables so that the
7784 * existentially quantified variables for which we already have an explicit
7785 * representation are placed before those for which we do not.
7786 * The input dimensions, the output dimensions and the existentially
7787 * quantified variables for which we already have an explicit
7788 * representation are then turned into parameters.
7789 * compute_divs returns a map with the same parameters and
7790 * no input or output dimensions and the dimension specification
7791 * is reset to that of the input, including the existentially quantified
7792 * variables for which we already had an explicit representation.
7794 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7796 struct isl_basic_set *bset;
7797 struct isl_set *set;
7798 struct isl_map *map;
7799 isl_space *space;
7800 isl_local_space *ls;
7801 isl_size nparam;
7802 isl_size n_in;
7803 isl_size n_out;
7804 int n_known;
7805 int i;
7807 bmap = isl_basic_map_sort_divs(bmap);
7808 bmap = isl_basic_map_cow(bmap);
7809 if (!bmap)
7810 return NULL;
7812 n_known = isl_basic_map_first_unknown_div(bmap);
7813 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7814 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7815 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7816 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
7817 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7819 space = isl_space_set_alloc(bmap->ctx,
7820 nparam + n_in + n_out + n_known, 0);
7821 if (!space)
7822 goto error;
7824 ls = isl_basic_map_get_local_space(bmap);
7825 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7826 n_known, bmap->n_div - n_known);
7827 if (n_known > 0) {
7828 for (i = n_known; i < bmap->n_div; ++i)
7829 swap_div(bmap, i - n_known, i);
7830 bmap->n_div -= n_known;
7831 bmap->extra -= n_known;
7833 bmap = isl_basic_map_reset_space(bmap, space);
7834 bset = bset_from_bmap(bmap);
7836 set = parameter_compute_divs(bset);
7837 map = set_to_map(set);
7838 map = replace_space_by_local_space(map, ls);
7840 return map;
7841 error:
7842 isl_basic_map_free(bmap);
7843 return NULL;
7846 /* Remove the explicit representation of local variable "div",
7847 * if there is any.
7849 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7850 __isl_take isl_basic_map *bmap, int div)
7852 isl_bool unknown;
7854 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7855 if (unknown < 0)
7856 return isl_basic_map_free(bmap);
7857 if (unknown)
7858 return bmap;
7860 bmap = isl_basic_map_cow(bmap);
7861 if (!bmap)
7862 return NULL;
7863 isl_int_set_si(bmap->div[div][0], 0);
7864 return bmap;
7867 /* Is local variable "div" of "bmap" marked as not having an explicit
7868 * representation?
7869 * Note that even if "div" is not marked in this way and therefore
7870 * has an explicit representation, this representation may still
7871 * depend (indirectly) on other local variables that do not
7872 * have an explicit representation.
7874 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7875 int div)
7877 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7878 return isl_bool_error;
7879 return isl_int_is_zero(bmap->div[div][0]);
7882 /* Return the position of the first local variable that does not
7883 * have an explicit representation.
7884 * Return the total number of local variables if they all have
7885 * an explicit representation.
7886 * Return -1 on error.
7888 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7890 int i;
7892 if (!bmap)
7893 return -1;
7895 for (i = 0; i < bmap->n_div; ++i) {
7896 if (!isl_basic_map_div_is_known(bmap, i))
7897 return i;
7899 return bmap->n_div;
7902 /* Return the position of the first local variable that does not
7903 * have an explicit representation.
7904 * Return the total number of local variables if they all have
7905 * an explicit representation.
7906 * Return -1 on error.
7908 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7910 return isl_basic_map_first_unknown_div(bset);
7913 /* Does "bmap" have an explicit representation for all local variables?
7915 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7917 int first;
7918 isl_size n;
7920 n = isl_basic_map_dim(bmap, isl_dim_div);
7921 first = isl_basic_map_first_unknown_div(bmap);
7922 if (n < 0 || first < 0)
7923 return isl_bool_error;
7924 return first == n;
7927 /* Do all basic maps in "map" have an explicit representation
7928 * for all local variables?
7930 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7932 int i;
7934 if (!map)
7935 return isl_bool_error;
7937 for (i = 0; i < map->n; ++i) {
7938 int known = isl_basic_map_divs_known(map->p[i]);
7939 if (known <= 0)
7940 return known;
7943 return isl_bool_true;
7946 /* If bmap contains any unknown divs, then compute explicit
7947 * expressions for them. However, this computation may be
7948 * quite expensive, so first try to remove divs that aren't
7949 * strictly needed.
7951 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7953 int known;
7954 struct isl_map *map;
7956 known = isl_basic_map_divs_known(bmap);
7957 if (known < 0)
7958 goto error;
7959 if (known)
7960 return isl_map_from_basic_map(bmap);
7962 bmap = isl_basic_map_drop_redundant_divs(bmap);
7964 known = isl_basic_map_divs_known(bmap);
7965 if (known < 0)
7966 goto error;
7967 if (known)
7968 return isl_map_from_basic_map(bmap);
7970 map = compute_divs(bmap);
7971 return map;
7972 error:
7973 isl_basic_map_free(bmap);
7974 return NULL;
7977 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7979 int i;
7980 int known;
7981 struct isl_map *res;
7983 if (!map)
7984 return NULL;
7985 if (map->n == 0)
7986 return map;
7988 known = isl_map_divs_known(map);
7989 if (known < 0) {
7990 isl_map_free(map);
7991 return NULL;
7993 if (known)
7994 return map;
7996 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7997 for (i = 1 ; i < map->n; ++i) {
7998 struct isl_map *r2;
7999 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8000 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8001 res = isl_map_union_disjoint(res, r2);
8002 else
8003 res = isl_map_union(res, r2);
8005 isl_map_free(map);
8007 return res;
8010 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8012 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8015 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8017 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8020 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8022 int i;
8023 struct isl_set *set;
8025 if (!map)
8026 goto error;
8028 map = isl_map_cow(map);
8029 if (!map)
8030 return NULL;
8032 set = set_from_map(map);
8033 set->dim = isl_space_domain(set->dim);
8034 if (!set->dim)
8035 goto error;
8036 for (i = 0; i < map->n; ++i) {
8037 set->p[i] = isl_basic_map_domain(map->p[i]);
8038 if (!set->p[i])
8039 goto error;
8041 ISL_F_CLR(set, ISL_MAP_DISJOINT);
8042 ISL_F_CLR(set, ISL_SET_NORMALIZED);
8043 return set;
8044 error:
8045 isl_map_free(map);
8046 return NULL;
8049 /* Return the union of "map1" and "map2", where we assume for now that
8050 * "map1" and "map2" are disjoint. Note that the basic maps inside
8051 * "map1" or "map2" may not be disjoint from each other.
8052 * Also note that this function is also called from isl_map_union,
8053 * which takes care of handling the situation where "map1" and "map2"
8054 * may not be disjoint.
8056 * If one of the inputs is empty, we can simply return the other input.
8057 * Similarly, if one of the inputs is universal, then it is equal to the union.
8059 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8060 __isl_take isl_map *map2)
8062 int i;
8063 unsigned flags = 0;
8064 struct isl_map *map = NULL;
8065 int is_universe;
8067 if (isl_map_check_equal_space(map1, map2) < 0)
8068 goto error;
8070 if (map1->n == 0) {
8071 isl_map_free(map1);
8072 return map2;
8074 if (map2->n == 0) {
8075 isl_map_free(map2);
8076 return map1;
8079 is_universe = isl_map_plain_is_universe(map1);
8080 if (is_universe < 0)
8081 goto error;
8082 if (is_universe) {
8083 isl_map_free(map2);
8084 return map1;
8087 is_universe = isl_map_plain_is_universe(map2);
8088 if (is_universe < 0)
8089 goto error;
8090 if (is_universe) {
8091 isl_map_free(map1);
8092 return map2;
8095 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8096 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8097 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8099 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8100 map1->n + map2->n, flags);
8101 if (!map)
8102 goto error;
8103 for (i = 0; i < map1->n; ++i) {
8104 map = isl_map_add_basic_map(map,
8105 isl_basic_map_copy(map1->p[i]));
8106 if (!map)
8107 goto error;
8109 for (i = 0; i < map2->n; ++i) {
8110 map = isl_map_add_basic_map(map,
8111 isl_basic_map_copy(map2->p[i]));
8112 if (!map)
8113 goto error;
8115 isl_map_free(map1);
8116 isl_map_free(map2);
8117 return map;
8118 error:
8119 isl_map_free(map);
8120 isl_map_free(map1);
8121 isl_map_free(map2);
8122 return NULL;
8125 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8126 * guaranteed to be disjoint by the caller.
8128 * Note that this functions is called from within isl_map_make_disjoint,
8129 * so we have to be careful not to touch the constraints of the inputs
8130 * in any way.
8132 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8133 __isl_take isl_map *map2)
8135 isl_map_align_params_bin(&map1, &map2);
8136 return map_union_disjoint(map1, map2);
8139 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8140 * not be disjoint.
8142 * We currently simply call map_union_disjoint, the internal operation
8143 * of which does not really depend on the inputs being disjoint.
8144 * If the result contains more than one basic map, then we clear
8145 * the disjoint flag since the result may contain basic maps from
8146 * both inputs and these are not guaranteed to be disjoint.
8148 * As a special case, if "map1" and "map2" are obviously equal,
8149 * then we simply return "map1".
8151 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8152 __isl_take isl_map *map2)
8154 int equal;
8156 if (isl_map_align_params_bin(&map1, &map2) < 0)
8157 goto error;
8159 equal = isl_map_plain_is_equal(map1, map2);
8160 if (equal < 0)
8161 goto error;
8162 if (equal) {
8163 isl_map_free(map2);
8164 return map1;
8167 map1 = map_union_disjoint(map1, map2);
8168 if (!map1)
8169 return NULL;
8170 if (map1->n > 1)
8171 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8172 return map1;
8173 error:
8174 isl_map_free(map1);
8175 isl_map_free(map2);
8176 return NULL;
8179 __isl_give isl_set *isl_set_union_disjoint(
8180 __isl_take isl_set *set1, __isl_take isl_set *set2)
8182 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8183 set_to_map(set2)));
8186 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8187 __isl_take isl_set *set2)
8189 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8192 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8193 * the results in a map living in "space".
8195 * "map" and "set" are assumed to be compatible and non-NULL.
8197 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8198 __isl_take isl_space *space, __isl_take isl_set *set,
8199 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8200 __isl_take isl_basic_set *bset))
8202 unsigned flags = 0;
8203 struct isl_map *result;
8204 int i, j;
8206 if (isl_set_plain_is_universe(set)) {
8207 isl_set_free(set);
8208 return isl_map_reset_equal_dim_space(map, space);
8211 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8212 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8213 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8215 result = isl_map_alloc_space(space, map->n * set->n, flags);
8216 for (i = 0; result && i < map->n; ++i)
8217 for (j = 0; j < set->n; ++j) {
8218 result = isl_map_add_basic_map(result,
8219 fn(isl_basic_map_copy(map->p[i]),
8220 isl_basic_set_copy(set->p[j])));
8221 if (!result)
8222 break;
8225 isl_map_free(map);
8226 isl_set_free(set);
8227 return result;
8230 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8231 __isl_take isl_set *set)
8233 isl_bool ok;
8234 isl_space *space;
8236 isl_map_align_params_set(&map, &set);
8237 ok = isl_map_compatible_range(map, set);
8238 if (ok < 0)
8239 goto error;
8240 if (!ok)
8241 isl_die(set->ctx, isl_error_invalid,
8242 "incompatible spaces", goto error);
8244 space = isl_map_get_space(map);
8245 return map_intersect_set(map, space, set,
8246 &isl_basic_map_intersect_range);
8247 error:
8248 isl_map_free(map);
8249 isl_set_free(set);
8250 return NULL;
8253 /* Intersect the domain of "map" with "set".
8255 * If the domain dimensions of "map" do not have any identifiers,
8256 * then copy them over from "set".
8258 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8259 __isl_take isl_set *set)
8261 isl_bool ok;
8262 isl_space *space;
8264 isl_map_align_params_set(&map, &set);
8265 ok = isl_map_compatible_domain(map, set);
8266 if (ok < 0)
8267 goto error;
8268 if (!ok)
8269 isl_die(set->ctx, isl_error_invalid,
8270 "incompatible spaces", goto error);
8272 space = isl_map_get_space(map);
8273 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8274 isl_set_peek_space(set), isl_dim_set);
8275 return map_intersect_set(map, space, set,
8276 &isl_basic_map_intersect_domain);
8277 error:
8278 isl_map_free(map);
8279 isl_set_free(set);
8280 return NULL;
8283 /* Data structure that specifies how isl_map_intersect_factor
8284 * should operate.
8286 * "preserve_type" is the tuple where the factor differs from
8287 * the input map and of which the identifiers needs
8288 * to be preserved explicitly.
8289 * "other_factor" is used to extract the space of the other factor
8290 * from the space of the product ("map").
8291 * "product" is used to combine the given factor and a universe map
8292 * in the space returned by "other_factor" to produce a map
8293 * that lives in the same space as the input map.
8295 struct isl_intersect_factor_control {
8296 enum isl_dim_type preserve_type;
8297 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8298 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8299 __isl_take isl_map *other);
8302 /* Given a map "map" in some product space and a map "factor"
8303 * living in some factor space, return the intersection.
8305 * After aligning the parameters,
8306 * the map "factor" is first extended to a map living in the same space
8307 * as "map" and then a regular intersection is computed.
8309 * Note that the extension is computed as a product, which is anonymous
8310 * by default. If "map" has an identifier on the corresponding tuple,
8311 * then this identifier needs to be set on the product
8312 * before the intersection is computed.
8314 static __isl_give isl_map *isl_map_intersect_factor(
8315 __isl_take isl_map *map, __isl_take isl_map *factor,
8316 struct isl_intersect_factor_control *control)
8318 isl_bool equal, has_id;
8319 isl_id *id;
8320 isl_space *space;
8321 isl_map *other, *product;
8323 equal = isl_map_has_equal_params(map, factor);
8324 if (equal < 0)
8325 goto error;
8326 if (!equal) {
8327 map = isl_map_align_params(map, isl_map_get_space(factor));
8328 factor = isl_map_align_params(factor, isl_map_get_space(map));
8331 space = isl_map_get_space(map);
8332 has_id = isl_space_has_tuple_id(space, control->preserve_type);
8333 if (has_id < 0)
8334 space = isl_space_free(space);
8335 else if (has_id)
8336 id = isl_space_get_tuple_id(space, control->preserve_type);
8338 other = isl_map_universe(control->other_factor(space));
8339 product = control->product(factor, other);
8341 if (has_id >= 0 && has_id)
8342 product = isl_map_set_tuple_id(product,
8343 control->preserve_type, id);
8345 return map_intersect(map, product);
8346 error:
8347 isl_map_free(map);
8348 isl_map_free(factor);
8349 return NULL;
8352 /* Return the domain product of "map2" and "map1".
8354 static __isl_give isl_map *isl_map_reverse_domain_product(
8355 __isl_take isl_map *map1, __isl_take isl_map *map2)
8357 return isl_map_domain_product(map2, map1);
8360 /* Return the range product of "map2" and "map1".
8362 static __isl_give isl_map *isl_map_reverse_range_product(
8363 __isl_take isl_map *map1, __isl_take isl_map *map2)
8365 return isl_map_range_product(map2, map1);
8368 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8369 * in the space B -> C, return the intersection.
8371 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8372 __isl_take isl_map *map, __isl_take isl_map *factor)
8374 struct isl_intersect_factor_control control = {
8375 .preserve_type = isl_dim_in,
8376 .other_factor = isl_space_domain_factor_domain,
8377 .product = isl_map_reverse_domain_product,
8380 return isl_map_intersect_factor(map, factor, &control);
8383 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8384 * in the space A -> B, return the intersection.
8386 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8387 __isl_take isl_map *map, __isl_take isl_map *factor)
8389 struct isl_intersect_factor_control control = {
8390 .preserve_type = isl_dim_out,
8391 .other_factor = isl_space_range_factor_range,
8392 .product = isl_map_range_product,
8395 return isl_map_intersect_factor(map, factor, &control);
8398 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8399 * in the space A -> C, return the intersection.
8401 __isl_give isl_map *isl_map_intersect_range_factor_range(
8402 __isl_take isl_map *map, __isl_take isl_map *factor)
8404 struct isl_intersect_factor_control control = {
8405 .preserve_type = isl_dim_out,
8406 .other_factor = isl_space_range_factor_domain,
8407 .product = isl_map_reverse_range_product,
8410 return isl_map_intersect_factor(map, factor, &control);
8413 /* Given a set "set" in a space [A -> B] and a set "domain"
8414 * in the space A, return the intersection.
8416 * The set "domain" is first extended to a set living in the space
8417 * [A -> B] and then a regular intersection is computed.
8419 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8420 __isl_take isl_set *domain)
8422 struct isl_intersect_factor_control control = {
8423 .preserve_type = isl_dim_set,
8424 .other_factor = isl_space_factor_range,
8425 .product = isl_map_range_product,
8428 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8429 set_to_map(domain), &control));
8432 /* Given a set "set" in a space [A -> B] and a set "range"
8433 * in the space B, return the intersection.
8435 * The set "range" is first extended to a set living in the space
8436 * [A -> B] and then a regular intersection is computed.
8438 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8439 __isl_take isl_set *range)
8441 struct isl_intersect_factor_control control = {
8442 .preserve_type = isl_dim_set,
8443 .other_factor = isl_space_factor_domain,
8444 .product = isl_map_reverse_range_product,
8447 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8448 set_to_map(range), &control));
8451 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8452 __isl_take isl_map *map2)
8454 if (isl_map_align_params_bin(&map1, &map2) < 0)
8455 goto error;
8456 map1 = isl_map_reverse(map1);
8457 map1 = isl_map_apply_range(map1, map2);
8458 return isl_map_reverse(map1);
8459 error:
8460 isl_map_free(map1);
8461 isl_map_free(map2);
8462 return NULL;
8465 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8466 __isl_take isl_map *map2)
8468 isl_space *space;
8469 struct isl_map *result;
8470 int i, j;
8472 if (isl_map_align_params_bin(&map1, &map2) < 0)
8473 goto error;
8475 space = isl_space_join(isl_space_copy(map1->dim),
8476 isl_space_copy(map2->dim));
8478 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8479 if (!result)
8480 goto error;
8481 for (i = 0; i < map1->n; ++i)
8482 for (j = 0; j < map2->n; ++j) {
8483 result = isl_map_add_basic_map(result,
8484 isl_basic_map_apply_range(
8485 isl_basic_map_copy(map1->p[i]),
8486 isl_basic_map_copy(map2->p[j])));
8487 if (!result)
8488 goto error;
8490 isl_map_free(map1);
8491 isl_map_free(map2);
8492 if (result && result->n <= 1)
8493 ISL_F_SET(result, ISL_MAP_DISJOINT);
8494 return result;
8495 error:
8496 isl_map_free(map1);
8497 isl_map_free(map2);
8498 return NULL;
8501 /* Is "bmap" a transformation, i.e.,
8502 * does it relate elements from the same space.
8504 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
8506 isl_space *space;
8508 space = isl_basic_map_peek_space(bmap);
8509 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8512 /* Check that "bmap" is a transformation, i.e.,
8513 * that it relates elements from the same space.
8515 static isl_stat isl_basic_map_check_transformation(
8516 __isl_keep isl_basic_map *bmap)
8518 isl_bool equal;
8520 equal = isl_basic_map_is_transformation(bmap);
8521 if (equal < 0)
8522 return isl_stat_error;
8523 if (!equal)
8524 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
8525 "domain and range don't match", return isl_stat_error);
8526 return isl_stat_ok;
8530 * returns range - domain
8532 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8534 isl_space *target_space;
8535 struct isl_basic_set *bset;
8536 isl_size dim;
8537 isl_size nparam;
8538 isl_size total;
8539 int i;
8541 if (isl_basic_map_check_transformation(bmap) < 0)
8542 return isl_basic_map_free(bmap);
8543 dim = isl_basic_map_dim(bmap, isl_dim_in);
8544 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8545 if (dim < 0 || nparam < 0)
8546 goto error;
8547 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8548 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8549 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8550 total = isl_basic_map_dim(bmap, isl_dim_all);
8551 if (total < 0)
8552 bmap = isl_basic_map_free(bmap);
8553 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8554 for (i = 0; i < dim; ++i) {
8555 int j = isl_basic_map_alloc_equality(bmap);
8556 if (j < 0) {
8557 bmap = isl_basic_map_free(bmap);
8558 break;
8560 isl_seq_clr(bmap->eq[j], 1 + total);
8561 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8562 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8563 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8565 bset = isl_basic_map_domain(bmap);
8566 bset = isl_basic_set_reset_space(bset, target_space);
8567 return bset;
8568 error:
8569 isl_basic_map_free(bmap);
8570 return NULL;
8573 /* Is the tuple of type "type1" of "map" the same as
8574 * the tuple of type "type2" of "space"?
8576 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
8577 enum isl_dim_type type1, __isl_keep isl_space *space,
8578 enum isl_dim_type type2)
8580 isl_space *map_space;
8582 map_space = isl_map_peek_space(map);
8583 return isl_space_tuple_is_equal(map_space, type1, space, type2);
8586 /* Is the tuple of type "type1" of "map1" the same as
8587 * the tuple of type "type2" of "map2"?
8589 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
8590 enum isl_dim_type type1, __isl_keep isl_map *map2,
8591 enum isl_dim_type type2)
8593 isl_space *space1, *space2;
8595 space1 = isl_map_peek_space(map1);
8596 space2 = isl_map_peek_space(map2);
8597 return isl_space_tuple_is_equal(space1, type1, space2, type2);
8600 /* Check that "map" is a transformation, i.e.,
8601 * that it relates elements from the same space.
8603 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
8605 isl_bool equal;
8607 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
8608 if (equal < 0)
8609 return isl_stat_error;
8610 if (!equal)
8611 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8612 "domain and range don't match", return isl_stat_error);
8613 return isl_stat_ok;
8617 * returns range - domain
8619 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8621 int i;
8622 isl_space *space;
8623 struct isl_set *result;
8625 if (isl_map_check_transformation(map) < 0)
8626 goto error;
8627 space = isl_map_get_space(map);
8628 space = isl_space_domain(space);
8629 result = isl_set_alloc_space(space, map->n, 0);
8630 if (!result)
8631 goto error;
8632 for (i = 0; i < map->n; ++i)
8633 result = isl_set_add_basic_set(result,
8634 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8635 isl_map_free(map);
8636 return result;
8637 error:
8638 isl_map_free(map);
8639 return NULL;
8643 * returns [domain -> range] -> range - domain
8645 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8646 __isl_take isl_basic_map *bmap)
8648 int i, k;
8649 isl_space *space;
8650 isl_basic_map *domain;
8651 isl_size nparam, n;
8652 isl_size total;
8654 if (isl_basic_map_check_transformation(bmap) < 0)
8655 return isl_basic_map_free(bmap);
8657 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8658 n = isl_basic_map_dim(bmap, isl_dim_in);
8659 if (nparam < 0 || n < 0)
8660 return isl_basic_map_free(bmap);
8662 space = isl_basic_map_get_space(bmap);
8663 space = isl_space_from_range(isl_space_domain(space));
8664 domain = isl_basic_map_universe(space);
8666 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8667 bmap = isl_basic_map_apply_range(bmap, domain);
8668 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8670 total = isl_basic_map_dim(bmap, isl_dim_all);
8671 if (total < 0)
8672 return isl_basic_map_free(bmap);
8674 for (i = 0; i < n; ++i) {
8675 k = isl_basic_map_alloc_equality(bmap);
8676 if (k < 0)
8677 goto error;
8678 isl_seq_clr(bmap->eq[k], 1 + total);
8679 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8680 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8681 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8684 bmap = isl_basic_map_gauss(bmap, NULL);
8685 return isl_basic_map_finalize(bmap);
8686 error:
8687 isl_basic_map_free(bmap);
8688 return NULL;
8692 * returns [domain -> range] -> range - domain
8694 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8696 if (isl_map_check_transformation(map) < 0)
8697 return isl_map_free(map);
8699 return isl_map_transform(map, &isl_space_range_map,
8700 &isl_basic_map_deltas_map);
8703 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8705 isl_size n_in, n_out;
8707 n_in = isl_space_dim(space, isl_dim_in);
8708 n_out = isl_space_dim(space, isl_dim_out);
8709 if (n_in < 0 || n_out < 0)
8710 goto error;
8711 if (n_in != n_out)
8712 isl_die(space->ctx, isl_error_invalid,
8713 "number of input and output dimensions needs to be "
8714 "the same", goto error);
8715 return isl_basic_map_equal(space, n_in);
8716 error:
8717 isl_space_free(space);
8718 return NULL;
8721 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
8723 return isl_map_from_basic_map(isl_basic_map_identity(space));
8726 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8728 isl_space *space = isl_set_get_space(set);
8729 isl_map *id;
8730 id = isl_map_identity(isl_space_map_from_set(space));
8731 return isl_map_intersect_range(id, set);
8734 /* Construct a basic set with all set dimensions having only non-negative
8735 * values.
8737 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8738 __isl_take isl_space *space)
8740 int i;
8741 isl_size nparam;
8742 isl_size dim;
8743 isl_size total;
8744 struct isl_basic_set *bset;
8746 nparam = isl_space_dim(space, isl_dim_param);
8747 dim = isl_space_dim(space, isl_dim_set);
8748 total = isl_space_dim(space, isl_dim_all);
8749 if (nparam < 0 || dim < 0 || total < 0)
8750 space = isl_space_free(space);
8751 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8752 if (!bset)
8753 return NULL;
8754 for (i = 0; i < dim; ++i) {
8755 int k = isl_basic_set_alloc_inequality(bset);
8756 if (k < 0)
8757 goto error;
8758 isl_seq_clr(bset->ineq[k], 1 + total);
8759 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8761 return bset;
8762 error:
8763 isl_basic_set_free(bset);
8764 return NULL;
8767 /* Construct the half-space x_pos >= 0.
8769 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8770 int pos)
8772 int k;
8773 isl_size total;
8774 isl_basic_set *nonneg;
8776 total = isl_space_dim(space, isl_dim_all);
8777 if (total < 0)
8778 space = isl_space_free(space);
8779 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8780 k = isl_basic_set_alloc_inequality(nonneg);
8781 if (k < 0)
8782 goto error;
8783 isl_seq_clr(nonneg->ineq[k], 1 + total);
8784 isl_int_set_si(nonneg->ineq[k][pos], 1);
8786 return isl_basic_set_finalize(nonneg);
8787 error:
8788 isl_basic_set_free(nonneg);
8789 return NULL;
8792 /* Construct the half-space x_pos <= -1.
8794 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8795 int pos)
8797 int k;
8798 isl_size total;
8799 isl_basic_set *neg;
8801 total = isl_space_dim(space, isl_dim_all);
8802 if (total < 0)
8803 space = isl_space_free(space);
8804 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8805 k = isl_basic_set_alloc_inequality(neg);
8806 if (k < 0)
8807 goto error;
8808 isl_seq_clr(neg->ineq[k], 1 + total);
8809 isl_int_set_si(neg->ineq[k][0], -1);
8810 isl_int_set_si(neg->ineq[k][pos], -1);
8812 return isl_basic_set_finalize(neg);
8813 error:
8814 isl_basic_set_free(neg);
8815 return NULL;
8818 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8819 enum isl_dim_type type, unsigned first, unsigned n)
8821 int i;
8822 unsigned offset;
8823 isl_basic_set *nonneg;
8824 isl_basic_set *neg;
8826 if (n == 0)
8827 return set;
8829 if (isl_set_check_range(set, type, first, n) < 0)
8830 return isl_set_free(set);
8832 offset = pos(set->dim, type);
8833 for (i = 0; i < n; ++i) {
8834 nonneg = nonneg_halfspace(isl_set_get_space(set),
8835 offset + first + i);
8836 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8838 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8841 return set;
8844 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8845 int len,
8846 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8847 void *user)
8849 isl_set *half;
8851 if (!set)
8852 return isl_stat_error;
8853 if (isl_set_plain_is_empty(set)) {
8854 isl_set_free(set);
8855 return isl_stat_ok;
8857 if (first == len)
8858 return fn(set, signs, user);
8860 signs[first] = 1;
8861 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8862 1 + first));
8863 half = isl_set_intersect(half, isl_set_copy(set));
8864 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8865 goto error;
8867 signs[first] = -1;
8868 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8869 1 + first));
8870 half = isl_set_intersect(half, set);
8871 return foreach_orthant(half, signs, first + 1, len, fn, user);
8872 error:
8873 isl_set_free(set);
8874 return isl_stat_error;
8877 /* Call "fn" on the intersections of "set" with each of the orthants
8878 * (except for obviously empty intersections). The orthant is identified
8879 * by the signs array, with each entry having value 1 or -1 according
8880 * to the sign of the corresponding variable.
8882 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8883 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8884 void *user)
8886 isl_size nparam;
8887 isl_size nvar;
8888 int *signs;
8889 isl_stat r;
8891 if (!set)
8892 return isl_stat_error;
8893 if (isl_set_plain_is_empty(set))
8894 return isl_stat_ok;
8896 nparam = isl_set_dim(set, isl_dim_param);
8897 nvar = isl_set_dim(set, isl_dim_set);
8898 if (nparam < 0 || nvar < 0)
8899 return isl_stat_error;
8901 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8903 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8904 fn, user);
8906 free(signs);
8908 return r;
8911 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8913 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8916 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8917 __isl_keep isl_basic_map *bmap2)
8919 isl_bool is_subset;
8920 struct isl_map *map1;
8921 struct isl_map *map2;
8923 if (!bmap1 || !bmap2)
8924 return isl_bool_error;
8926 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8927 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8929 is_subset = isl_map_is_subset(map1, map2);
8931 isl_map_free(map1);
8932 isl_map_free(map2);
8934 return is_subset;
8937 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8938 __isl_keep isl_basic_set *bset2)
8940 return isl_basic_map_is_subset(bset1, bset2);
8943 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8944 __isl_keep isl_basic_map *bmap2)
8946 isl_bool is_subset;
8948 if (!bmap1 || !bmap2)
8949 return isl_bool_error;
8950 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8951 if (is_subset != isl_bool_true)
8952 return is_subset;
8953 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8954 return is_subset;
8957 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8958 __isl_keep isl_basic_set *bset2)
8960 return isl_basic_map_is_equal(
8961 bset_to_bmap(bset1), bset_to_bmap(bset2));
8964 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8966 int i;
8967 int is_empty;
8969 if (!map)
8970 return isl_bool_error;
8971 for (i = 0; i < map->n; ++i) {
8972 is_empty = isl_basic_map_is_empty(map->p[i]);
8973 if (is_empty < 0)
8974 return isl_bool_error;
8975 if (!is_empty)
8976 return isl_bool_false;
8978 return isl_bool_true;
8981 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8983 return map ? map->n == 0 : isl_bool_error;
8986 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8988 return set ? set->n == 0 : isl_bool_error;
8991 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8993 return isl_map_is_empty(set_to_map(set));
8996 #undef TYPE
8997 #define TYPE isl_basic_map
8999 static
9000 #include "isl_type_has_equal_space_bin_templ.c"
9001 #include "isl_type_check_equal_space_templ.c"
9003 /* Check that "bset1" and "bset2" live in the same space,
9004 * reporting an error if they do not.
9006 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9007 __isl_keep isl_basic_set *bset2)
9009 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9010 bset_to_bmap(bset1));
9013 #undef TYPE
9014 #define TYPE isl_map
9016 #include "isl_type_has_equal_space_bin_templ.c"
9017 #include "isl_type_check_equal_space_templ.c"
9019 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9020 __isl_keep isl_set *set2)
9022 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9025 #undef TYPE1
9026 #define TYPE1 isl_map
9027 #undef TYPE2
9028 #define TYPE2 isl_basic_map
9029 #undef TYPE_PAIR
9030 #define TYPE_PAIR isl_map_basic_map
9032 static
9033 #include "isl_type_has_equal_space_templ.c"
9034 #include "isl_type_check_equal_space_templ.c"
9036 /* Check that "set" and "bset" live in the same space,
9037 * reporting an error if they do not.
9039 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9040 __isl_keep isl_basic_set *bset)
9042 return isl_map_basic_map_check_equal_space(set_to_map(set),
9043 bset_to_bmap(bset));
9046 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9048 isl_bool is_subset;
9050 if (!map1 || !map2)
9051 return isl_bool_error;
9052 is_subset = isl_map_is_subset(map1, map2);
9053 if (is_subset != isl_bool_true)
9054 return is_subset;
9055 is_subset = isl_map_is_subset(map2, map1);
9056 return is_subset;
9059 /* Is "map1" equal to "map2"?
9061 * First check if they are obviously equal.
9062 * If not, then perform a more detailed analysis.
9064 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9066 isl_bool equal;
9068 equal = isl_map_plain_is_equal(map1, map2);
9069 if (equal < 0 || equal)
9070 return equal;
9071 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9074 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9075 __isl_keep isl_basic_map *bmap2)
9077 isl_bool is_subset;
9079 if (!bmap1 || !bmap2)
9080 return isl_bool_error;
9081 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9082 if (is_subset != isl_bool_true)
9083 return is_subset;
9084 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9085 return isl_bool_not(is_subset);
9088 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9089 __isl_keep isl_map *map2)
9091 isl_bool is_subset;
9093 if (!map1 || !map2)
9094 return isl_bool_error;
9095 is_subset = isl_map_is_subset(map1, map2);
9096 if (is_subset != isl_bool_true)
9097 return is_subset;
9098 is_subset = isl_map_is_subset(map2, map1);
9099 return isl_bool_not(is_subset);
9102 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9103 __isl_keep isl_set *set2)
9105 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9108 /* Is "bmap" obviously equal to the universe with the same space?
9110 * That is, does it not have any constraints?
9112 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9114 if (!bmap)
9115 return isl_bool_error;
9116 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9119 /* Is "bset" obviously equal to the universe with the same space?
9121 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9123 return isl_basic_map_plain_is_universe(bset);
9126 /* If "c" does not involve any existentially quantified variables,
9127 * then set *univ to false and abort
9129 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9131 isl_bool *univ = user;
9132 isl_size n;
9134 n = isl_constraint_dim(c, isl_dim_div);
9135 if (n < 0)
9136 c = isl_constraint_free(c);
9137 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9138 isl_constraint_free(c);
9139 if (*univ < 0 || !*univ)
9140 return isl_stat_error;
9141 return isl_stat_ok;
9144 /* Is "bmap" equal to the universe with the same space?
9146 * First check if it is obviously equal to the universe.
9147 * If not and if there are any constraints not involving
9148 * existentially quantified variables, then it is certainly
9149 * not equal to the universe.
9150 * Otherwise, check if the universe is a subset of "bmap".
9152 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9154 isl_size n_div;
9155 isl_bool univ;
9156 isl_basic_map *test;
9158 univ = isl_basic_map_plain_is_universe(bmap);
9159 if (univ < 0 || univ)
9160 return univ;
9161 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9162 if (n_div < 0)
9163 return isl_bool_error;
9164 if (n_div == 0)
9165 return isl_bool_false;
9166 univ = isl_bool_true;
9167 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9168 univ)
9169 return isl_bool_error;
9170 if (univ < 0 || !univ)
9171 return univ;
9172 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9173 univ = isl_basic_map_is_subset(test, bmap);
9174 isl_basic_map_free(test);
9175 return univ;
9178 /* Is "bset" equal to the universe with the same space?
9180 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9182 return isl_basic_map_is_universe(bset);
9185 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9187 int i;
9189 if (!map)
9190 return isl_bool_error;
9192 for (i = 0; i < map->n; ++i) {
9193 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9194 if (r < 0 || r)
9195 return r;
9198 return isl_bool_false;
9201 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9203 return isl_map_plain_is_universe(set_to_map(set));
9206 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9208 struct isl_basic_set *bset = NULL;
9209 struct isl_vec *sample = NULL;
9210 isl_bool empty, non_empty;
9212 if (!bmap)
9213 return isl_bool_error;
9215 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9216 return isl_bool_true;
9218 if (isl_basic_map_plain_is_universe(bmap))
9219 return isl_bool_false;
9221 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9222 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9223 copy = isl_basic_map_remove_redundancies(copy);
9224 empty = isl_basic_map_plain_is_empty(copy);
9225 isl_basic_map_free(copy);
9226 return empty;
9229 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9230 if (non_empty < 0)
9231 return isl_bool_error;
9232 if (non_empty)
9233 return isl_bool_false;
9234 isl_vec_free(bmap->sample);
9235 bmap->sample = NULL;
9236 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9237 if (!bset)
9238 return isl_bool_error;
9239 sample = isl_basic_set_sample_vec(bset);
9240 if (!sample)
9241 return isl_bool_error;
9242 empty = sample->size == 0;
9243 isl_vec_free(bmap->sample);
9244 bmap->sample = sample;
9245 if (empty)
9246 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9248 return empty;
9251 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9253 if (!bmap)
9254 return isl_bool_error;
9255 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9258 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9260 if (!bset)
9261 return isl_bool_error;
9262 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9265 /* Is "bmap" known to be non-empty?
9267 * That is, is the cached sample still valid?
9269 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9271 isl_size total;
9273 if (!bmap)
9274 return isl_bool_error;
9275 if (!bmap->sample)
9276 return isl_bool_false;
9277 total = isl_basic_map_dim(bmap, isl_dim_all);
9278 if (total < 0)
9279 return isl_bool_error;
9280 if (bmap->sample->size != 1 + total)
9281 return isl_bool_false;
9282 return isl_basic_map_contains(bmap, bmap->sample);
9285 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9287 return isl_basic_map_is_empty(bset_to_bmap(bset));
9290 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9291 __isl_take isl_basic_map *bmap2)
9293 struct isl_map *map;
9295 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9296 goto error;
9298 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9299 if (!map)
9300 goto error;
9301 map = isl_map_add_basic_map(map, bmap1);
9302 map = isl_map_add_basic_map(map, bmap2);
9303 return map;
9304 error:
9305 isl_basic_map_free(bmap1);
9306 isl_basic_map_free(bmap2);
9307 return NULL;
9310 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9311 __isl_take isl_basic_set *bset2)
9313 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9314 bset_to_bmap(bset2)));
9317 /* Order divs such that any div only depends on previous divs */
9318 __isl_give isl_basic_map *isl_basic_map_order_divs(
9319 __isl_take isl_basic_map *bmap)
9321 int i;
9322 isl_size off;
9324 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9325 if (off < 0)
9326 return isl_basic_map_free(bmap);
9328 for (i = 0; i < bmap->n_div; ++i) {
9329 int pos;
9330 if (isl_int_is_zero(bmap->div[i][0]))
9331 continue;
9332 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9333 bmap->n_div-i);
9334 if (pos == -1)
9335 continue;
9336 if (pos == 0)
9337 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9338 "integer division depends on itself",
9339 return isl_basic_map_free(bmap));
9340 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9341 if (!bmap)
9342 return NULL;
9343 --i;
9345 return bmap;
9348 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9350 int i;
9352 if (!map)
9353 return 0;
9355 for (i = 0; i < map->n; ++i) {
9356 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9357 if (!map->p[i])
9358 goto error;
9361 return map;
9362 error:
9363 isl_map_free(map);
9364 return NULL;
9367 /* Sort the local variables of "bset".
9369 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9370 __isl_take isl_basic_set *bset)
9372 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9375 /* Apply the expansion computed by isl_merge_divs.
9376 * The expansion itself is given by "exp" while the resulting
9377 * list of divs is given by "div".
9379 * Move the integer divisions of "bmap" into the right position
9380 * according to "exp" and then introduce the additional integer
9381 * divisions, adding div constraints.
9382 * The moving should be done first to avoid moving coefficients
9383 * in the definitions of the extra integer divisions.
9385 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9386 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9388 int i, j;
9389 int n_div;
9391 bmap = isl_basic_map_cow(bmap);
9392 if (!bmap || !div)
9393 goto error;
9395 if (div->n_row < bmap->n_div)
9396 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9397 "not an expansion", goto error);
9399 n_div = bmap->n_div;
9400 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9401 2 * (div->n_row - n_div));
9403 for (i = n_div; i < div->n_row; ++i)
9404 if (isl_basic_map_alloc_div(bmap) < 0)
9405 goto error;
9407 for (j = n_div - 1; j >= 0; --j) {
9408 if (exp[j] == j)
9409 break;
9410 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9411 if (!bmap)
9412 goto error;
9414 j = 0;
9415 for (i = 0; i < div->n_row; ++i) {
9416 if (j < n_div && exp[j] == i) {
9417 j++;
9418 } else {
9419 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9420 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9421 continue;
9422 bmap = isl_basic_map_add_div_constraints(bmap, i);
9423 if (!bmap)
9424 goto error;
9428 isl_mat_free(div);
9429 return bmap;
9430 error:
9431 isl_basic_map_free(bmap);
9432 isl_mat_free(div);
9433 return NULL;
9436 /* Apply the expansion computed by isl_merge_divs.
9437 * The expansion itself is given by "exp" while the resulting
9438 * list of divs is given by "div".
9440 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9441 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9443 return isl_basic_map_expand_divs(bset, div, exp);
9446 /* Look for a div in dst that corresponds to the div "div" in src.
9447 * The divs before "div" in src and dst are assumed to be the same.
9449 * Return the position of the corresponding div in dst
9450 * if there is one. Otherwise, return a position beyond the integer divisions.
9451 * Return -1 on error.
9453 static int find_div(__isl_keep isl_basic_map *dst,
9454 __isl_keep isl_basic_map *src, unsigned div)
9456 int i;
9457 isl_size n_div;
9458 isl_size v_div;
9460 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9461 n_div = isl_basic_map_dim(dst, isl_dim_div);
9462 if (n_div < 0 || v_div < 0)
9463 return -1;
9464 isl_assert(dst->ctx, div <= n_div, return -1);
9465 for (i = div; i < n_div; ++i)
9466 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9467 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9468 n_div - div) == -1)
9469 return i;
9470 return n_div;
9473 /* Align the divs of "dst" to those of "src", adding divs from "src"
9474 * if needed. That is, make sure that the first src->n_div divs
9475 * of the result are equal to those of src.
9477 * The result is not finalized as by design it will have redundant
9478 * divs if any divs from "src" were copied.
9480 __isl_give isl_basic_map *isl_basic_map_align_divs(
9481 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9483 int i;
9484 isl_bool known;
9485 int extended;
9486 isl_size v_div;
9487 isl_size dst_n_div;
9489 if (!dst || !src)
9490 return isl_basic_map_free(dst);
9492 if (src->n_div == 0)
9493 return dst;
9495 known = isl_basic_map_divs_known(src);
9496 if (known < 0)
9497 return isl_basic_map_free(dst);
9498 if (!known)
9499 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9500 "some src divs are unknown",
9501 return isl_basic_map_free(dst));
9503 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9504 if (v_div < 0)
9505 return isl_basic_map_free(dst);
9507 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
9508 if (!src)
9509 return isl_basic_map_free(dst);
9511 extended = 0;
9512 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9513 if (dst_n_div < 0)
9514 dst = isl_basic_map_free(dst);
9515 for (i = 0; i < src->n_div; ++i) {
9516 int j = find_div(dst, src, i);
9517 if (j < 0)
9518 dst = isl_basic_map_free(dst);
9519 if (j == dst_n_div) {
9520 if (!extended) {
9521 int extra = src->n_div - i;
9522 dst = isl_basic_map_cow(dst);
9523 if (!dst)
9524 goto error;
9525 dst = isl_basic_map_extend(dst,
9526 extra, 0, 2 * extra);
9527 extended = 1;
9529 j = isl_basic_map_alloc_div(dst);
9530 if (j < 0)
9531 goto error;
9532 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9533 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9534 dst_n_div++;
9535 dst = isl_basic_map_add_div_constraints(dst, j);
9536 if (!dst)
9537 goto error;
9539 if (j != i)
9540 dst = isl_basic_map_swap_div(dst, i, j);
9541 if (!dst)
9542 goto error;
9544 isl_basic_map_free(src);
9545 return dst;
9546 error:
9547 isl_basic_map_free(src);
9548 isl_basic_map_free(dst);
9549 return NULL;
9552 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9554 int i;
9556 if (!map)
9557 return NULL;
9558 if (map->n == 0)
9559 return map;
9560 map = isl_map_compute_divs(map);
9561 map = isl_map_cow(map);
9562 if (!map)
9563 return NULL;
9565 for (i = 1; i < map->n; ++i)
9566 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9567 for (i = 1; i < map->n; ++i) {
9568 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9569 if (!map->p[i])
9570 return isl_map_free(map);
9573 map = isl_map_unmark_normalized(map);
9574 return map;
9577 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9579 return isl_map_align_divs_internal(map);
9582 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9584 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9587 /* Align the divs of the basic maps in "map" to those
9588 * of the basic maps in "list", as well as to the other basic maps in "map".
9589 * The elements in "list" are assumed to have known divs.
9591 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9592 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9594 int i;
9595 isl_size n;
9597 n = isl_basic_map_list_n_basic_map(list);
9598 map = isl_map_compute_divs(map);
9599 map = isl_map_cow(map);
9600 if (!map || n < 0)
9601 return isl_map_free(map);
9602 if (map->n == 0)
9603 return map;
9605 for (i = 0; i < n; ++i) {
9606 isl_basic_map *bmap;
9608 bmap = isl_basic_map_list_get_basic_map(list, i);
9609 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9610 isl_basic_map_free(bmap);
9612 if (!map->p[0])
9613 return isl_map_free(map);
9615 return isl_map_align_divs_internal(map);
9618 /* Align the divs of each element of "list" to those of "bmap".
9619 * Both "bmap" and the elements of "list" are assumed to have known divs.
9621 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9622 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9624 int i;
9625 isl_size n;
9627 n = isl_basic_map_list_n_basic_map(list);
9628 if (n < 0 || !bmap)
9629 return isl_basic_map_list_free(list);
9631 for (i = 0; i < n; ++i) {
9632 isl_basic_map *bmap_i;
9634 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9635 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9636 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9639 return list;
9642 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9643 __isl_take isl_map *map)
9645 isl_bool ok;
9647 isl_map_align_params_set(&map, &set);
9648 ok = isl_map_compatible_domain(map, set);
9649 if (ok < 0)
9650 goto error;
9651 if (!ok)
9652 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9653 "incompatible spaces", goto error);
9654 map = isl_map_intersect_domain(map, set);
9655 set = isl_map_range(map);
9656 return set;
9657 error:
9658 isl_set_free(set);
9659 isl_map_free(map);
9660 return NULL;
9663 /* There is no need to cow as removing empty parts doesn't change
9664 * the meaning of the set.
9666 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9668 int i;
9670 if (!map)
9671 return NULL;
9673 for (i = map->n - 1; i >= 0; --i)
9674 map = remove_if_empty(map, i);
9676 return map;
9679 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
9681 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9684 /* Create a binary relation that maps the shared initial "pos" dimensions
9685 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9687 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9688 __isl_keep isl_basic_set *bset2, int pos)
9690 isl_basic_map *bmap1;
9691 isl_basic_map *bmap2;
9693 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9694 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9695 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9696 isl_dim_out, 0, pos);
9697 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9698 isl_dim_out, 0, pos);
9699 return isl_basic_map_range_product(bmap1, bmap2);
9702 /* Given two basic sets bset1 and bset2, compute the maximal difference
9703 * between the values of dimension pos in bset1 and those in bset2
9704 * for any common value of the parameters and dimensions preceding pos.
9706 static enum isl_lp_result basic_set_maximal_difference_at(
9707 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9708 int pos, isl_int *opt)
9710 isl_basic_map *bmap1;
9711 struct isl_ctx *ctx;
9712 struct isl_vec *obj;
9713 isl_size total;
9714 isl_size nparam;
9715 isl_size dim1;
9716 enum isl_lp_result res;
9718 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9719 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9720 if (nparam < 0 || dim1 < 0 || !bset2)
9721 return isl_lp_error;
9723 bmap1 = join_initial(bset1, bset2, pos);
9724 total = isl_basic_map_dim(bmap1, isl_dim_all);
9725 if (total < 0)
9726 return isl_lp_error;
9728 ctx = bmap1->ctx;
9729 obj = isl_vec_alloc(ctx, 1 + total);
9730 if (!obj)
9731 goto error;
9732 isl_seq_clr(obj->block.data, 1 + total);
9733 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9734 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9735 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9736 opt, NULL, NULL);
9737 isl_basic_map_free(bmap1);
9738 isl_vec_free(obj);
9739 return res;
9740 error:
9741 isl_basic_map_free(bmap1);
9742 return isl_lp_error;
9745 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9746 * for any common value of the parameters and dimensions preceding pos
9747 * in both basic sets, the values of dimension pos in bset1 are
9748 * smaller or larger than those in bset2.
9750 * Returns
9751 * 1 if bset1 follows bset2
9752 * -1 if bset1 precedes bset2
9753 * 0 if bset1 and bset2 are incomparable
9754 * -2 if some error occurred.
9756 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9757 __isl_keep isl_basic_set *bset2, int pos)
9759 isl_int opt;
9760 enum isl_lp_result res;
9761 int cmp;
9763 isl_int_init(opt);
9765 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9767 if (res == isl_lp_empty)
9768 cmp = 0;
9769 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9770 res == isl_lp_unbounded)
9771 cmp = 1;
9772 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9773 cmp = -1;
9774 else
9775 cmp = -2;
9777 isl_int_clear(opt);
9778 return cmp;
9781 /* Given two basic sets bset1 and bset2, check whether
9782 * for any common value of the parameters and dimensions preceding pos
9783 * there is a value of dimension pos in bset1 that is larger
9784 * than a value of the same dimension in bset2.
9786 * Return
9787 * 1 if there exists such a pair
9788 * 0 if there is no such pair, but there is a pair of equal values
9789 * -1 otherwise
9790 * -2 if some error occurred.
9792 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9793 __isl_keep isl_basic_set *bset2, int pos)
9795 isl_bool empty;
9796 isl_basic_map *bmap;
9797 isl_size dim1;
9799 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9800 if (dim1 < 0)
9801 return -2;
9802 bmap = join_initial(bset1, bset2, pos);
9803 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9804 isl_dim_out, dim1 - pos);
9805 empty = isl_basic_map_is_empty(bmap);
9806 if (empty < 0)
9807 goto error;
9808 if (empty) {
9809 isl_basic_map_free(bmap);
9810 return -1;
9812 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9813 isl_dim_out, dim1 - pos);
9814 empty = isl_basic_map_is_empty(bmap);
9815 if (empty < 0)
9816 goto error;
9817 isl_basic_map_free(bmap);
9818 if (empty)
9819 return 0;
9820 return 1;
9821 error:
9822 isl_basic_map_free(bmap);
9823 return -2;
9826 /* Given two sets set1 and set2, check whether
9827 * for any common value of the parameters and dimensions preceding pos
9828 * there is a value of dimension pos in set1 that is larger
9829 * than a value of the same dimension in set2.
9831 * Return
9832 * 1 if there exists such a pair
9833 * 0 if there is no such pair, but there is a pair of equal values
9834 * -1 otherwise
9835 * -2 if some error occurred.
9837 int isl_set_follows_at(__isl_keep isl_set *set1,
9838 __isl_keep isl_set *set2, int pos)
9840 int i, j;
9841 int follows = -1;
9843 if (!set1 || !set2)
9844 return -2;
9846 for (i = 0; i < set1->n; ++i)
9847 for (j = 0; j < set2->n; ++j) {
9848 int f;
9849 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9850 if (f == 1 || f == -2)
9851 return f;
9852 if (f > follows)
9853 follows = f;
9856 return follows;
9859 static isl_bool isl_basic_map_plain_has_fixed_var(
9860 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9862 int i;
9863 int d;
9864 isl_size total;
9866 total = isl_basic_map_dim(bmap, isl_dim_all);
9867 if (total < 0)
9868 return isl_bool_error;
9869 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9870 for (; d+1 > pos; --d)
9871 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9872 break;
9873 if (d != pos)
9874 continue;
9875 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9876 return isl_bool_false;
9877 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9878 return isl_bool_false;
9879 if (!isl_int_is_one(bmap->eq[i][1+d]))
9880 return isl_bool_false;
9881 if (val)
9882 isl_int_neg(*val, bmap->eq[i][0]);
9883 return isl_bool_true;
9885 return isl_bool_false;
9888 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9889 unsigned pos, isl_int *val)
9891 int i;
9892 isl_int v;
9893 isl_int tmp;
9894 isl_bool fixed;
9896 if (!map)
9897 return isl_bool_error;
9898 if (map->n == 0)
9899 return isl_bool_false;
9900 if (map->n == 1)
9901 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9902 isl_int_init(v);
9903 isl_int_init(tmp);
9904 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9905 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9906 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9907 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9908 fixed = isl_bool_false;
9910 if (val)
9911 isl_int_set(*val, v);
9912 isl_int_clear(tmp);
9913 isl_int_clear(v);
9914 return fixed;
9917 static isl_bool isl_basic_set_plain_has_fixed_var(
9918 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9920 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9921 pos, val);
9924 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9925 enum isl_dim_type type, unsigned pos, isl_int *val)
9927 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9928 return isl_bool_error;
9929 return isl_basic_map_plain_has_fixed_var(bmap,
9930 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9933 /* If "bmap" obviously lies on a hyperplane where the given dimension
9934 * has a fixed value, then return that value.
9935 * Otherwise return NaN.
9937 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9938 __isl_keep isl_basic_map *bmap,
9939 enum isl_dim_type type, unsigned pos)
9941 isl_ctx *ctx;
9942 isl_val *v;
9943 isl_bool fixed;
9945 if (!bmap)
9946 return NULL;
9947 ctx = isl_basic_map_get_ctx(bmap);
9948 v = isl_val_alloc(ctx);
9949 if (!v)
9950 return NULL;
9951 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9952 if (fixed < 0)
9953 return isl_val_free(v);
9954 if (fixed) {
9955 isl_int_set_si(v->d, 1);
9956 return v;
9958 isl_val_free(v);
9959 return isl_val_nan(ctx);
9962 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9963 enum isl_dim_type type, unsigned pos, isl_int *val)
9965 if (isl_map_check_range(map, type, pos, 1) < 0)
9966 return isl_bool_error;
9967 return isl_map_plain_has_fixed_var(map,
9968 map_offset(map, type) - 1 + pos, val);
9971 /* If "map" obviously lies on a hyperplane where the given dimension
9972 * has a fixed value, then return that value.
9973 * Otherwise return NaN.
9975 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9976 enum isl_dim_type type, unsigned pos)
9978 isl_ctx *ctx;
9979 isl_val *v;
9980 isl_bool fixed;
9982 if (!map)
9983 return NULL;
9984 ctx = isl_map_get_ctx(map);
9985 v = isl_val_alloc(ctx);
9986 if (!v)
9987 return NULL;
9988 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9989 if (fixed < 0)
9990 return isl_val_free(v);
9991 if (fixed) {
9992 isl_int_set_si(v->d, 1);
9993 return v;
9995 isl_val_free(v);
9996 return isl_val_nan(ctx);
9999 /* If "set" obviously lies on a hyperplane where the given dimension
10000 * has a fixed value, then return that value.
10001 * Otherwise return NaN.
10003 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10004 enum isl_dim_type type, unsigned pos)
10006 return isl_map_plain_get_val_if_fixed(set, type, pos);
10009 /* Return a sequence of values in the same space as "set"
10010 * that are equal to the corresponding set dimensions of "set"
10011 * for those set dimensions that obviously lie on a hyperplane
10012 * where the dimension has a fixed value.
10013 * The other elements are set to NaN.
10015 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10016 __isl_keep isl_set *set)
10018 int i;
10019 isl_size n;
10020 isl_space *space;
10021 isl_multi_val *mv;
10023 space = isl_space_drop_all_params(isl_set_get_space(set));
10024 mv = isl_multi_val_alloc(space);
10025 n = isl_multi_val_size(mv);
10026 if (n < 0)
10027 return isl_multi_val_free(mv);
10029 for (i = 0; i < n; ++i) {
10030 isl_val *v;
10032 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10033 mv = isl_multi_val_set_val(mv, i, v);
10036 return mv;
10039 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10040 * then return this fixed value in *val.
10042 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10043 unsigned dim, isl_int *val)
10045 isl_size nparam;
10047 nparam = isl_basic_set_dim(bset, isl_dim_param);
10048 if (nparam < 0)
10049 return isl_bool_error;
10050 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10053 /* Return -1 if the constraint "c1" should be sorted before "c2"
10054 * and 1 if it should be sorted after "c2".
10055 * Return 0 if the two constraints are the same (up to the constant term).
10057 * In particular, if a constraint involves later variables than another
10058 * then it is sorted after this other constraint.
10059 * uset_gist depends on constraints without existentially quantified
10060 * variables sorting first.
10062 * For constraints that have the same latest variable, those
10063 * with the same coefficient for this latest variable (first in absolute value
10064 * and then in actual value) are grouped together.
10065 * This is useful for detecting pairs of constraints that can
10066 * be chained in their printed representation.
10068 * Finally, within a group, constraints are sorted according to
10069 * their coefficients (excluding the constant term).
10071 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10073 isl_int **c1 = (isl_int **) p1;
10074 isl_int **c2 = (isl_int **) p2;
10075 int l1, l2;
10076 unsigned size = *(unsigned *) arg;
10077 int cmp;
10079 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10080 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10082 if (l1 != l2)
10083 return l1 - l2;
10085 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10086 if (cmp != 0)
10087 return cmp;
10088 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10089 if (cmp != 0)
10090 return -cmp;
10092 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10095 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10096 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10097 * and 0 if the two constraints are the same (up to the constant term).
10099 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10100 isl_int *c1, isl_int *c2)
10102 isl_size total;
10103 unsigned size;
10105 total = isl_basic_map_dim(bmap, isl_dim_all);
10106 if (total < 0)
10107 return -2;
10108 size = total;
10109 return sort_constraint_cmp(&c1, &c2, &size);
10112 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10113 __isl_take isl_basic_map *bmap)
10115 isl_size total;
10116 unsigned size;
10118 if (!bmap)
10119 return NULL;
10120 if (bmap->n_ineq == 0)
10121 return bmap;
10122 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10123 return bmap;
10124 total = isl_basic_map_dim(bmap, isl_dim_all);
10125 if (total < 0)
10126 return isl_basic_map_free(bmap);
10127 size = total;
10128 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10129 &sort_constraint_cmp, &size) < 0)
10130 return isl_basic_map_free(bmap);
10131 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10132 return bmap;
10135 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10136 __isl_take isl_basic_set *bset)
10138 isl_basic_map *bmap = bset_to_bmap(bset);
10139 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10142 __isl_give isl_basic_map *isl_basic_map_normalize(
10143 __isl_take isl_basic_map *bmap)
10145 bmap = isl_basic_map_remove_redundancies(bmap);
10146 bmap = isl_basic_map_sort_constraints(bmap);
10147 return bmap;
10149 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10150 __isl_keep isl_basic_map *bmap2)
10152 int i, cmp;
10153 isl_size total;
10154 isl_space *space1, *space2;
10156 if (!bmap1 || !bmap2)
10157 return -1;
10159 if (bmap1 == bmap2)
10160 return 0;
10161 space1 = isl_basic_map_peek_space(bmap1);
10162 space2 = isl_basic_map_peek_space(bmap2);
10163 cmp = isl_space_cmp(space1, space2);
10164 if (cmp)
10165 return cmp;
10166 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10167 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10168 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10169 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10170 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10171 return 0;
10172 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10173 return 1;
10174 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10175 return -1;
10176 if (bmap1->n_eq != bmap2->n_eq)
10177 return bmap1->n_eq - bmap2->n_eq;
10178 if (bmap1->n_ineq != bmap2->n_ineq)
10179 return bmap1->n_ineq - bmap2->n_ineq;
10180 if (bmap1->n_div != bmap2->n_div)
10181 return bmap1->n_div - bmap2->n_div;
10182 total = isl_basic_map_dim(bmap1, isl_dim_all);
10183 if (total < 0)
10184 return -1;
10185 for (i = 0; i < bmap1->n_eq; ++i) {
10186 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10187 if (cmp)
10188 return cmp;
10190 for (i = 0; i < bmap1->n_ineq; ++i) {
10191 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10192 if (cmp)
10193 return cmp;
10195 for (i = 0; i < bmap1->n_div; ++i) {
10196 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10197 if (cmp)
10198 return cmp;
10200 return 0;
10203 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10204 __isl_keep isl_basic_set *bset2)
10206 return isl_basic_map_plain_cmp(bset1, bset2);
10209 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10211 int i, cmp;
10213 if (set1 == set2)
10214 return 0;
10215 if (set1->n != set2->n)
10216 return set1->n - set2->n;
10218 for (i = 0; i < set1->n; ++i) {
10219 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10220 if (cmp)
10221 return cmp;
10224 return 0;
10227 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10228 __isl_keep isl_basic_map *bmap2)
10230 if (!bmap1 || !bmap2)
10231 return isl_bool_error;
10232 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10235 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10236 __isl_keep isl_basic_set *bset2)
10238 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10239 bset_to_bmap(bset2));
10242 static int qsort_bmap_cmp(const void *p1, const void *p2)
10244 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10245 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10247 return isl_basic_map_plain_cmp(bmap1, bmap2);
10250 /* Sort the basic maps of "map" and remove duplicate basic maps.
10252 * While removing basic maps, we make sure that the basic maps remain
10253 * sorted because isl_map_normalize expects the basic maps of the result
10254 * to be sorted.
10256 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10258 int i, j;
10260 map = isl_map_remove_empty_parts(map);
10261 if (!map)
10262 return NULL;
10263 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10264 for (i = map->n - 1; i >= 1; --i) {
10265 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10266 continue;
10267 isl_basic_map_free(map->p[i-1]);
10268 for (j = i; j < map->n; ++j)
10269 map->p[j - 1] = map->p[j];
10270 map->n--;
10273 return map;
10276 /* Remove obvious duplicates among the basic maps of "map".
10278 * Unlike isl_map_normalize, this function does not remove redundant
10279 * constraints and only removes duplicates that have exactly the same
10280 * constraints in the input. It does sort the constraints and
10281 * the basic maps to ease the detection of duplicates.
10283 * If "map" has already been normalized or if the basic maps are
10284 * disjoint, then there can be no duplicates.
10286 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10288 int i;
10289 isl_basic_map *bmap;
10291 if (!map)
10292 return NULL;
10293 if (map->n <= 1)
10294 return map;
10295 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10296 return map;
10297 for (i = 0; i < map->n; ++i) {
10298 bmap = isl_basic_map_copy(map->p[i]);
10299 bmap = isl_basic_map_sort_constraints(bmap);
10300 if (!bmap)
10301 return isl_map_free(map);
10302 isl_basic_map_free(map->p[i]);
10303 map->p[i] = bmap;
10306 map = sort_and_remove_duplicates(map);
10307 return map;
10310 /* We normalize in place, but if anything goes wrong we need
10311 * to return NULL, so we need to make sure we don't change the
10312 * meaning of any possible other copies of map.
10314 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10316 int i;
10317 struct isl_basic_map *bmap;
10319 if (!map)
10320 return NULL;
10321 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10322 return map;
10323 for (i = 0; i < map->n; ++i) {
10324 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10325 if (!bmap)
10326 goto error;
10327 isl_basic_map_free(map->p[i]);
10328 map->p[i] = bmap;
10331 map = sort_and_remove_duplicates(map);
10332 if (map)
10333 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10334 return map;
10335 error:
10336 isl_map_free(map);
10337 return NULL;
10340 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10342 return set_from_map(isl_map_normalize(set_to_map(set)));
10345 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10346 __isl_keep isl_map *map2)
10348 int i;
10349 isl_bool equal;
10351 if (!map1 || !map2)
10352 return isl_bool_error;
10354 if (map1 == map2)
10355 return isl_bool_true;
10356 equal = isl_map_has_equal_space(map1, map2);
10357 if (equal < 0 || !equal)
10358 return equal;
10360 map1 = isl_map_copy(map1);
10361 map2 = isl_map_copy(map2);
10362 map1 = isl_map_normalize(map1);
10363 map2 = isl_map_normalize(map2);
10364 if (!map1 || !map2)
10365 goto error;
10366 equal = map1->n == map2->n;
10367 for (i = 0; equal && i < map1->n; ++i) {
10368 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10369 if (equal < 0)
10370 goto error;
10372 isl_map_free(map1);
10373 isl_map_free(map2);
10374 return equal;
10375 error:
10376 isl_map_free(map1);
10377 isl_map_free(map2);
10378 return isl_bool_error;
10381 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10382 __isl_keep isl_set *set2)
10384 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10387 /* Return the basic maps in "map" as a list.
10389 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10390 __isl_keep isl_map *map)
10392 int i;
10393 isl_ctx *ctx;
10394 isl_basic_map_list *list;
10396 if (!map)
10397 return NULL;
10398 ctx = isl_map_get_ctx(map);
10399 list = isl_basic_map_list_alloc(ctx, map->n);
10401 for (i = 0; i < map->n; ++i) {
10402 isl_basic_map *bmap;
10404 bmap = isl_basic_map_copy(map->p[i]);
10405 list = isl_basic_map_list_add(list, bmap);
10408 return list;
10411 /* Return the intersection of the elements in the non-empty list "list".
10412 * All elements are assumed to live in the same space.
10414 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10415 __isl_take isl_basic_map_list *list)
10417 int i;
10418 isl_size n;
10419 isl_basic_map *bmap;
10421 n = isl_basic_map_list_n_basic_map(list);
10422 if (n < 0)
10423 goto error;
10424 if (n < 1)
10425 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10426 "expecting non-empty list", goto error);
10428 bmap = isl_basic_map_list_get_basic_map(list, 0);
10429 for (i = 1; i < n; ++i) {
10430 isl_basic_map *bmap_i;
10432 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10433 bmap = isl_basic_map_intersect(bmap, bmap_i);
10436 isl_basic_map_list_free(list);
10437 return bmap;
10438 error:
10439 isl_basic_map_list_free(list);
10440 return NULL;
10443 /* Return the intersection of the elements in the non-empty list "list".
10444 * All elements are assumed to live in the same space.
10446 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10447 __isl_take isl_basic_set_list *list)
10449 return isl_basic_map_list_intersect(list);
10452 /* Return the union of the elements of "list".
10453 * The list is required to have at least one element.
10455 __isl_give isl_set *isl_basic_set_list_union(
10456 __isl_take isl_basic_set_list *list)
10458 int i;
10459 isl_size n;
10460 isl_space *space;
10461 isl_basic_set *bset;
10462 isl_set *set;
10464 n = isl_basic_set_list_n_basic_set(list);
10465 if (n < 0)
10466 goto error;
10467 if (n < 1)
10468 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10469 "expecting non-empty list", goto error);
10471 bset = isl_basic_set_list_get_basic_set(list, 0);
10472 space = isl_basic_set_get_space(bset);
10473 isl_basic_set_free(bset);
10475 set = isl_set_alloc_space(space, n, 0);
10476 for (i = 0; i < n; ++i) {
10477 bset = isl_basic_set_list_get_basic_set(list, i);
10478 set = isl_set_add_basic_set(set, bset);
10481 isl_basic_set_list_free(list);
10482 return set;
10483 error:
10484 isl_basic_set_list_free(list);
10485 return NULL;
10488 /* Return the union of the elements in the non-empty list "list".
10489 * All elements are assumed to live in the same space.
10491 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10493 int i;
10494 isl_size n;
10495 isl_set *set;
10497 n = isl_set_list_n_set(list);
10498 if (n < 0)
10499 goto error;
10500 if (n < 1)
10501 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10502 "expecting non-empty list", goto error);
10504 set = isl_set_list_get_set(list, 0);
10505 for (i = 1; i < n; ++i) {
10506 isl_set *set_i;
10508 set_i = isl_set_list_get_set(list, i);
10509 set = isl_set_union(set, set_i);
10512 isl_set_list_free(list);
10513 return set;
10514 error:
10515 isl_set_list_free(list);
10516 return NULL;
10519 __isl_give isl_basic_map *isl_basic_map_product(
10520 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10522 isl_space *space_result = NULL;
10523 struct isl_basic_map *bmap;
10524 unsigned in1, in2, out1, out2, nparam, total, pos;
10525 struct isl_dim_map *dim_map1, *dim_map2;
10527 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10528 goto error;
10529 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10530 isl_space_copy(bmap2->dim));
10532 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10533 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10534 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10535 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10536 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10538 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10539 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10540 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10541 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10542 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10543 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10544 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10545 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10546 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10547 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10548 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10550 bmap = isl_basic_map_alloc_space(space_result,
10551 bmap1->n_div + bmap2->n_div,
10552 bmap1->n_eq + bmap2->n_eq,
10553 bmap1->n_ineq + bmap2->n_ineq);
10554 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10555 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10556 bmap = isl_basic_map_simplify(bmap);
10557 return isl_basic_map_finalize(bmap);
10558 error:
10559 isl_basic_map_free(bmap1);
10560 isl_basic_map_free(bmap2);
10561 return NULL;
10564 __isl_give isl_basic_map *isl_basic_map_flat_product(
10565 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10567 isl_basic_map *prod;
10569 prod = isl_basic_map_product(bmap1, bmap2);
10570 prod = isl_basic_map_flatten(prod);
10571 return prod;
10574 __isl_give isl_basic_set *isl_basic_set_flat_product(
10575 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10577 return isl_basic_map_flat_range_product(bset1, bset2);
10580 __isl_give isl_basic_map *isl_basic_map_domain_product(
10581 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10583 isl_space *space1, *space2;
10584 isl_space *space_result = NULL;
10585 isl_basic_map *bmap;
10586 isl_size in1, in2, out, nparam;
10587 unsigned total, pos;
10588 struct isl_dim_map *dim_map1, *dim_map2;
10590 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10591 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10592 out = isl_basic_map_dim(bmap1, isl_dim_out);
10593 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10594 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10595 goto error;
10597 space1 = isl_basic_map_get_space(bmap1);
10598 space2 = isl_basic_map_get_space(bmap2);
10599 space_result = isl_space_domain_product(space1, space2);
10601 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10602 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10603 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10604 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10605 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10606 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10607 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10608 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10609 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10610 isl_dim_map_div(dim_map1, bmap1, pos += out);
10611 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10613 bmap = isl_basic_map_alloc_space(space_result,
10614 bmap1->n_div + bmap2->n_div,
10615 bmap1->n_eq + bmap2->n_eq,
10616 bmap1->n_ineq + bmap2->n_ineq);
10617 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10618 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10619 bmap = isl_basic_map_simplify(bmap);
10620 return isl_basic_map_finalize(bmap);
10621 error:
10622 isl_basic_map_free(bmap1);
10623 isl_basic_map_free(bmap2);
10624 return NULL;
10627 __isl_give isl_basic_map *isl_basic_map_range_product(
10628 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10630 isl_bool rational;
10631 isl_space *space_result = NULL;
10632 isl_basic_map *bmap;
10633 isl_size in, out1, out2, nparam;
10634 unsigned total, pos;
10635 struct isl_dim_map *dim_map1, *dim_map2;
10637 rational = isl_basic_map_is_rational(bmap1);
10638 if (rational >= 0 && rational)
10639 rational = isl_basic_map_is_rational(bmap2);
10640 in = isl_basic_map_dim(bmap1, isl_dim_in);
10641 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10642 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10643 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10644 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10645 goto error;
10647 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10648 goto error;
10650 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10651 isl_space_copy(bmap2->dim));
10653 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10654 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10655 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10656 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10657 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10658 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10659 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10660 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10661 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10662 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10663 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10665 bmap = isl_basic_map_alloc_space(space_result,
10666 bmap1->n_div + bmap2->n_div,
10667 bmap1->n_eq + bmap2->n_eq,
10668 bmap1->n_ineq + bmap2->n_ineq);
10669 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10670 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10671 if (rational)
10672 bmap = isl_basic_map_set_rational(bmap);
10673 bmap = isl_basic_map_simplify(bmap);
10674 return isl_basic_map_finalize(bmap);
10675 error:
10676 isl_basic_map_free(bmap1);
10677 isl_basic_map_free(bmap2);
10678 return NULL;
10681 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10682 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10684 isl_basic_map *prod;
10686 prod = isl_basic_map_range_product(bmap1, bmap2);
10687 prod = isl_basic_map_flatten_range(prod);
10688 return prod;
10691 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10692 * and collect the results.
10693 * The result live in the space obtained by calling "space_product"
10694 * on the spaces of "map1" and "map2".
10695 * If "remove_duplicates" is set then the result may contain duplicates
10696 * (even if the inputs do not) and so we try and remove the obvious
10697 * duplicates.
10699 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10700 __isl_take isl_map *map2,
10701 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10702 __isl_take isl_space *right),
10703 __isl_give isl_basic_map *(*basic_map_product)(
10704 __isl_take isl_basic_map *left,
10705 __isl_take isl_basic_map *right),
10706 int remove_duplicates)
10708 unsigned flags = 0;
10709 struct isl_map *result;
10710 int i, j;
10711 isl_bool m;
10713 m = isl_map_has_equal_params(map1, map2);
10714 if (m < 0)
10715 goto error;
10716 if (!m)
10717 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10718 "parameters don't match", goto error);
10720 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10721 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10722 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10724 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10725 isl_space_copy(map2->dim)),
10726 map1->n * map2->n, flags);
10727 if (!result)
10728 goto error;
10729 for (i = 0; i < map1->n; ++i)
10730 for (j = 0; j < map2->n; ++j) {
10731 struct isl_basic_map *part;
10732 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10733 isl_basic_map_copy(map2->p[j]));
10734 if (isl_basic_map_is_empty(part))
10735 isl_basic_map_free(part);
10736 else
10737 result = isl_map_add_basic_map(result, part);
10738 if (!result)
10739 goto error;
10741 if (remove_duplicates)
10742 result = isl_map_remove_obvious_duplicates(result);
10743 isl_map_free(map1);
10744 isl_map_free(map2);
10745 return result;
10746 error:
10747 isl_map_free(map1);
10748 isl_map_free(map2);
10749 return NULL;
10752 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10754 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10755 __isl_take isl_map *map2)
10757 isl_map_align_params_bin(&map1, &map2);
10758 return map_product(map1, map2, &isl_space_product,
10759 &isl_basic_map_product, 0);
10762 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10764 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10765 __isl_take isl_map *map2)
10767 isl_map *prod;
10769 prod = isl_map_product(map1, map2);
10770 prod = isl_map_flatten(prod);
10771 return prod;
10774 /* Given two set A and B, construct its Cartesian product A x B.
10776 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
10777 __isl_take isl_set *set2)
10779 return isl_map_range_product(set1, set2);
10782 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10783 __isl_take isl_set *set2)
10785 return isl_map_flat_range_product(set1, set2);
10788 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10790 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10791 __isl_take isl_map *map2)
10793 isl_map_align_params_bin(&map1, &map2);
10794 return map_product(map1, map2, &isl_space_domain_product,
10795 &isl_basic_map_domain_product, 1);
10798 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10800 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10801 __isl_take isl_map *map2)
10803 isl_map_align_params_bin(&map1, &map2);
10804 return map_product(map1, map2, &isl_space_range_product,
10805 &isl_basic_map_range_product, 1);
10808 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10810 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10812 isl_space *space;
10813 isl_size total1, keep1, total2, keep2;
10815 total1 = isl_map_dim(map, isl_dim_in);
10816 total2 = isl_map_dim(map, isl_dim_out);
10817 if (total1 < 0 || total2 < 0)
10818 return isl_map_free(map);
10819 if (!isl_space_domain_is_wrapping(map->dim) ||
10820 !isl_space_range_is_wrapping(map->dim))
10821 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10822 "not a product", return isl_map_free(map));
10824 space = isl_map_get_space(map);
10825 space = isl_space_factor_domain(space);
10826 keep1 = isl_space_dim(space, isl_dim_in);
10827 keep2 = isl_space_dim(space, isl_dim_out);
10828 if (keep1 < 0 || keep2 < 0)
10829 map = isl_map_free(map);
10830 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10831 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10832 map = isl_map_reset_space(map, space);
10834 return map;
10837 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10839 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10841 isl_space *space;
10842 isl_size total1, keep1, total2, keep2;
10844 total1 = isl_map_dim(map, isl_dim_in);
10845 total2 = isl_map_dim(map, isl_dim_out);
10846 if (total1 < 0 || total2 < 0)
10847 return isl_map_free(map);
10848 if (!isl_space_domain_is_wrapping(map->dim) ||
10849 !isl_space_range_is_wrapping(map->dim))
10850 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10851 "not a product", return isl_map_free(map));
10853 space = isl_map_get_space(map);
10854 space = isl_space_factor_range(space);
10855 keep1 = isl_space_dim(space, isl_dim_in);
10856 keep2 = isl_space_dim(space, isl_dim_out);
10857 if (keep1 < 0 || keep2 < 0)
10858 map = isl_map_free(map);
10859 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10860 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10861 map = isl_map_reset_space(map, space);
10863 return map;
10866 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10868 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10870 isl_space *space;
10871 isl_size total, keep;
10873 total = isl_map_dim(map, isl_dim_in);
10874 if (total < 0)
10875 return isl_map_free(map);
10876 if (!isl_space_domain_is_wrapping(map->dim))
10877 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10878 "domain is not a product", return isl_map_free(map));
10880 space = isl_map_get_space(map);
10881 space = isl_space_domain_factor_domain(space);
10882 keep = isl_space_dim(space, isl_dim_in);
10883 if (keep < 0)
10884 map = isl_map_free(map);
10885 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10886 map = isl_map_reset_space(map, space);
10888 return map;
10891 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10893 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10895 isl_space *space;
10896 isl_size total, keep;
10898 total = isl_map_dim(map, isl_dim_in);
10899 if (total < 0)
10900 return isl_map_free(map);
10901 if (!isl_space_domain_is_wrapping(map->dim))
10902 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10903 "domain is not a product", return isl_map_free(map));
10905 space = isl_map_get_space(map);
10906 space = isl_space_domain_factor_range(space);
10907 keep = isl_space_dim(space, isl_dim_in);
10908 if (keep < 0)
10909 map = isl_map_free(map);
10910 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10911 map = isl_map_reset_space(map, space);
10913 return map;
10916 /* Given a map A -> [B -> C], extract the map A -> B.
10918 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10920 isl_space *space;
10921 isl_size total, keep;
10923 total = isl_map_dim(map, isl_dim_out);
10924 if (total < 0)
10925 return isl_map_free(map);
10926 if (!isl_space_range_is_wrapping(map->dim))
10927 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10928 "range is not a product", return isl_map_free(map));
10930 space = isl_map_get_space(map);
10931 space = isl_space_range_factor_domain(space);
10932 keep = isl_space_dim(space, isl_dim_out);
10933 if (keep < 0)
10934 map = isl_map_free(map);
10935 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10936 map = isl_map_reset_space(map, space);
10938 return map;
10941 /* Given a map A -> [B -> C], extract the map A -> C.
10943 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10945 isl_space *space;
10946 isl_size total, keep;
10948 total = isl_map_dim(map, isl_dim_out);
10949 if (total < 0)
10950 return isl_map_free(map);
10951 if (!isl_space_range_is_wrapping(map->dim))
10952 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10953 "range is not a product", return isl_map_free(map));
10955 space = isl_map_get_space(map);
10956 space = isl_space_range_factor_range(space);
10957 keep = isl_space_dim(space, isl_dim_out);
10958 if (keep < 0)
10959 map = isl_map_free(map);
10960 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10961 map = isl_map_reset_space(map, space);
10963 return map;
10966 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10968 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10969 __isl_take isl_map *map2)
10971 isl_map *prod;
10973 prod = isl_map_domain_product(map1, map2);
10974 prod = isl_map_flatten_domain(prod);
10975 return prod;
10978 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10980 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10981 __isl_take isl_map *map2)
10983 isl_map *prod;
10985 prod = isl_map_range_product(map1, map2);
10986 prod = isl_map_flatten_range(prod);
10987 return prod;
10990 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10992 int i;
10993 uint32_t hash = isl_hash_init();
10994 isl_size total;
10996 if (!bmap)
10997 return 0;
10998 bmap = isl_basic_map_copy(bmap);
10999 bmap = isl_basic_map_normalize(bmap);
11000 total = isl_basic_map_dim(bmap, isl_dim_all);
11001 if (total < 0)
11002 return 0;
11003 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11004 for (i = 0; i < bmap->n_eq; ++i) {
11005 uint32_t c_hash;
11006 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11007 isl_hash_hash(hash, c_hash);
11009 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11010 for (i = 0; i < bmap->n_ineq; ++i) {
11011 uint32_t c_hash;
11012 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11013 isl_hash_hash(hash, c_hash);
11015 isl_hash_byte(hash, bmap->n_div & 0xFF);
11016 for (i = 0; i < bmap->n_div; ++i) {
11017 uint32_t c_hash;
11018 if (isl_int_is_zero(bmap->div[i][0]))
11019 continue;
11020 isl_hash_byte(hash, i & 0xFF);
11021 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11022 isl_hash_hash(hash, c_hash);
11024 isl_basic_map_free(bmap);
11025 return hash;
11028 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11030 return isl_basic_map_get_hash(bset_to_bmap(bset));
11033 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11035 int i;
11036 uint32_t hash;
11038 if (!map)
11039 return 0;
11040 map = isl_map_copy(map);
11041 map = isl_map_normalize(map);
11042 if (!map)
11043 return 0;
11045 hash = isl_hash_init();
11046 for (i = 0; i < map->n; ++i) {
11047 uint32_t bmap_hash;
11048 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11049 isl_hash_hash(hash, bmap_hash);
11052 isl_map_free(map);
11054 return hash;
11057 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11059 return isl_map_get_hash(set_to_map(set));
11062 /* Return the number of basic maps in the (current) representation of "map".
11064 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11066 return map ? map->n : isl_size_error;
11069 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11071 return set ? set->n : isl_size_error;
11074 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11075 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11077 int i;
11079 if (!map)
11080 return isl_stat_error;
11082 for (i = 0; i < map->n; ++i)
11083 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11084 return isl_stat_error;
11086 return isl_stat_ok;
11089 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11090 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11092 int i;
11094 if (!set)
11095 return isl_stat_error;
11097 for (i = 0; i < set->n; ++i)
11098 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11099 return isl_stat_error;
11101 return isl_stat_ok;
11104 /* Return a list of basic sets, the union of which is equal to "set".
11106 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11107 __isl_keep isl_set *set)
11109 int i;
11110 isl_basic_set_list *list;
11112 if (!set)
11113 return NULL;
11115 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11116 for (i = 0; i < set->n; ++i) {
11117 isl_basic_set *bset;
11119 bset = isl_basic_set_copy(set->p[i]);
11120 list = isl_basic_set_list_add(list, bset);
11123 return list;
11126 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11128 isl_space *space;
11130 if (!bset)
11131 return NULL;
11133 bset = isl_basic_set_cow(bset);
11134 if (!bset)
11135 return NULL;
11137 space = isl_basic_set_get_space(bset);
11138 space = isl_space_lift(space, bset->n_div);
11139 if (!space)
11140 goto error;
11141 isl_space_free(bset->dim);
11142 bset->dim = space;
11143 bset->extra -= bset->n_div;
11144 bset->n_div = 0;
11146 bset = isl_basic_set_finalize(bset);
11148 return bset;
11149 error:
11150 isl_basic_set_free(bset);
11151 return NULL;
11154 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11156 int i;
11157 isl_space *space;
11158 unsigned n_div;
11160 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11162 if (!set)
11163 return NULL;
11165 set = isl_set_cow(set);
11166 if (!set)
11167 return NULL;
11169 n_div = set->p[0]->n_div;
11170 space = isl_set_get_space(set);
11171 space = isl_space_lift(space, n_div);
11172 if (!space)
11173 goto error;
11174 isl_space_free(set->dim);
11175 set->dim = space;
11177 for (i = 0; i < set->n; ++i) {
11178 set->p[i] = isl_basic_set_lift(set->p[i]);
11179 if (!set->p[i])
11180 goto error;
11183 return set;
11184 error:
11185 isl_set_free(set);
11186 return NULL;
11189 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11191 isl_size dim;
11192 int size = 0;
11194 dim = isl_basic_set_dim(bset, isl_dim_all);
11195 if (dim < 0)
11196 return -1;
11197 size += bset->n_eq * (1 + dim);
11198 size += bset->n_ineq * (1 + dim);
11199 size += bset->n_div * (2 + dim);
11201 return size;
11204 int isl_set_size(__isl_keep isl_set *set)
11206 int i;
11207 int size = 0;
11209 if (!set)
11210 return -1;
11212 for (i = 0; i < set->n; ++i)
11213 size += isl_basic_set_size(set->p[i]);
11215 return size;
11218 /* Check if there is any lower bound (if lower == 0) and/or upper
11219 * bound (if upper == 0) on the specified dim.
11221 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11222 enum isl_dim_type type, unsigned pos, int lower, int upper)
11224 int i;
11226 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11227 return isl_bool_error;
11229 pos += isl_basic_map_offset(bmap, type);
11231 for (i = 0; i < bmap->n_div; ++i) {
11232 if (isl_int_is_zero(bmap->div[i][0]))
11233 continue;
11234 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
11235 return isl_bool_true;
11238 for (i = 0; i < bmap->n_eq; ++i)
11239 if (!isl_int_is_zero(bmap->eq[i][pos]))
11240 return isl_bool_true;
11242 for (i = 0; i < bmap->n_ineq; ++i) {
11243 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
11244 if (sgn > 0)
11245 lower = 1;
11246 if (sgn < 0)
11247 upper = 1;
11250 return lower && upper;
11253 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11254 enum isl_dim_type type, unsigned pos)
11256 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11259 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11260 enum isl_dim_type type, unsigned pos)
11262 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11265 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11266 enum isl_dim_type type, unsigned pos)
11268 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11271 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11272 enum isl_dim_type type, unsigned pos)
11274 int i;
11276 if (!map)
11277 return isl_bool_error;
11279 for (i = 0; i < map->n; ++i) {
11280 isl_bool bounded;
11281 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11282 if (bounded < 0 || !bounded)
11283 return bounded;
11286 return isl_bool_true;
11289 /* Return true if the specified dim is involved in both an upper bound
11290 * and a lower bound.
11292 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11293 enum isl_dim_type type, unsigned pos)
11295 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11298 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11300 static isl_bool has_any_bound(__isl_keep isl_map *map,
11301 enum isl_dim_type type, unsigned pos,
11302 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11303 enum isl_dim_type type, unsigned pos))
11305 int i;
11307 if (!map)
11308 return isl_bool_error;
11310 for (i = 0; i < map->n; ++i) {
11311 isl_bool bounded;
11312 bounded = fn(map->p[i], type, pos);
11313 if (bounded < 0 || bounded)
11314 return bounded;
11317 return isl_bool_false;
11320 /* Return 1 if the specified dim is involved in any lower bound.
11322 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11323 enum isl_dim_type type, unsigned pos)
11325 return has_any_bound(set, type, pos,
11326 &isl_basic_map_dim_has_lower_bound);
11329 /* Return 1 if the specified dim is involved in any upper bound.
11331 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11332 enum isl_dim_type type, unsigned pos)
11334 return has_any_bound(set, type, pos,
11335 &isl_basic_map_dim_has_upper_bound);
11338 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11340 static isl_bool has_bound(__isl_keep isl_map *map,
11341 enum isl_dim_type type, unsigned pos,
11342 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11343 enum isl_dim_type type, unsigned pos))
11345 int i;
11347 if (!map)
11348 return isl_bool_error;
11350 for (i = 0; i < map->n; ++i) {
11351 isl_bool bounded;
11352 bounded = fn(map->p[i], type, pos);
11353 if (bounded < 0 || !bounded)
11354 return bounded;
11357 return isl_bool_true;
11360 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11362 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11363 enum isl_dim_type type, unsigned pos)
11365 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11368 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11370 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11371 enum isl_dim_type type, unsigned pos)
11373 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11376 /* For each of the "n" variables starting at "first", determine
11377 * the sign of the variable and put the results in the first "n"
11378 * elements of the array "signs".
11379 * Sign
11380 * 1 means that the variable is non-negative
11381 * -1 means that the variable is non-positive
11382 * 0 means the variable attains both positive and negative values.
11384 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11385 unsigned first, unsigned n, int *signs)
11387 isl_vec *bound = NULL;
11388 struct isl_tab *tab = NULL;
11389 struct isl_tab_undo *snap;
11390 int i;
11391 isl_size total;
11393 total = isl_basic_set_dim(bset, isl_dim_all);
11394 if (total < 0 || !signs)
11395 return isl_stat_error;
11397 bound = isl_vec_alloc(bset->ctx, 1 + total);
11398 tab = isl_tab_from_basic_set(bset, 0);
11399 if (!bound || !tab)
11400 goto error;
11402 isl_seq_clr(bound->el, bound->size);
11403 isl_int_set_si(bound->el[0], -1);
11405 snap = isl_tab_snap(tab);
11406 for (i = 0; i < n; ++i) {
11407 int empty;
11409 isl_int_set_si(bound->el[1 + first + i], -1);
11410 if (isl_tab_add_ineq(tab, bound->el) < 0)
11411 goto error;
11412 empty = tab->empty;
11413 isl_int_set_si(bound->el[1 + first + i], 0);
11414 if (isl_tab_rollback(tab, snap) < 0)
11415 goto error;
11417 if (empty) {
11418 signs[i] = 1;
11419 continue;
11422 isl_int_set_si(bound->el[1 + first + i], 1);
11423 if (isl_tab_add_ineq(tab, bound->el) < 0)
11424 goto error;
11425 empty = tab->empty;
11426 isl_int_set_si(bound->el[1 + first + i], 0);
11427 if (isl_tab_rollback(tab, snap) < 0)
11428 goto error;
11430 signs[i] = empty ? -1 : 0;
11433 isl_tab_free(tab);
11434 isl_vec_free(bound);
11435 return isl_stat_ok;
11436 error:
11437 isl_tab_free(tab);
11438 isl_vec_free(bound);
11439 return isl_stat_error;
11442 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11443 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11445 if (!bset || !signs)
11446 return isl_stat_error;
11447 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11448 return isl_stat_error;
11450 first += pos(bset->dim, type) - 1;
11451 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11454 /* Is it possible for the integer division "div" to depend (possibly
11455 * indirectly) on any output dimensions?
11457 * If the div is undefined, then we conservatively assume that it
11458 * may depend on them.
11459 * Otherwise, we check if it actually depends on them or on any integer
11460 * divisions that may depend on them.
11462 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11464 int i;
11465 isl_size n_out, n_div;
11466 unsigned o_out, o_div;
11468 if (isl_int_is_zero(bmap->div[div][0]))
11469 return isl_bool_true;
11471 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11472 if (n_out < 0)
11473 return isl_bool_error;
11474 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11476 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11477 return isl_bool_true;
11479 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11480 if (n_div < 0)
11481 return isl_bool_error;
11482 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11484 for (i = 0; i < n_div; ++i) {
11485 isl_bool may_involve;
11487 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11488 continue;
11489 may_involve = div_may_involve_output(bmap, i);
11490 if (may_involve < 0 || may_involve)
11491 return may_involve;
11494 return isl_bool_false;
11497 /* Return the first integer division of "bmap" in the range
11498 * [first, first + n[ that may depend on any output dimensions and
11499 * that has a non-zero coefficient in "c" (where the first coefficient
11500 * in "c" corresponds to integer division "first").
11502 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11503 isl_int *c, int first, int n)
11505 int k;
11507 if (!bmap)
11508 return -1;
11510 for (k = first; k < first + n; ++k) {
11511 isl_bool may_involve;
11513 if (isl_int_is_zero(c[k]))
11514 continue;
11515 may_involve = div_may_involve_output(bmap, k);
11516 if (may_involve < 0)
11517 return -1;
11518 if (may_involve)
11519 return k;
11522 return first + n;
11525 /* Look for a pair of inequality constraints in "bmap" of the form
11527 * -l + i >= 0 or i >= l
11528 * and
11529 * n + l - i >= 0 or i <= l + n
11531 * with n < "m" and i the output dimension at position "pos".
11532 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11533 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11534 * and earlier output dimensions, as well as integer divisions that do
11535 * not involve any of the output dimensions.
11537 * Return the index of the first inequality constraint or bmap->n_ineq
11538 * if no such pair can be found.
11540 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11541 int pos, isl_int m)
11543 int i, j;
11544 isl_ctx *ctx;
11545 isl_size total;
11546 isl_size n_div, n_out;
11547 unsigned o_div, o_out;
11548 int less;
11550 total = isl_basic_map_dim(bmap, isl_dim_all);
11551 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11552 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11553 if (total < 0 || n_out < 0 || n_div < 0)
11554 return -1;
11556 ctx = isl_basic_map_get_ctx(bmap);
11557 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11558 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11559 for (i = 0; i < bmap->n_ineq; ++i) {
11560 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11561 continue;
11562 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11563 n_out - (pos + 1)) != -1)
11564 continue;
11565 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11566 0, n_div) < n_div)
11567 continue;
11568 for (j = i + 1; j < bmap->n_ineq; ++j) {
11569 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11570 ctx->one))
11571 continue;
11572 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11573 bmap->ineq[j] + 1, total))
11574 continue;
11575 break;
11577 if (j >= bmap->n_ineq)
11578 continue;
11579 isl_int_add(bmap->ineq[i][0],
11580 bmap->ineq[i][0], bmap->ineq[j][0]);
11581 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11582 isl_int_sub(bmap->ineq[i][0],
11583 bmap->ineq[i][0], bmap->ineq[j][0]);
11584 if (!less)
11585 continue;
11586 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11587 return i;
11588 else
11589 return j;
11592 return bmap->n_ineq;
11595 /* Return the index of the equality of "bmap" that defines
11596 * the output dimension "pos" in terms of earlier dimensions.
11597 * The equality may also involve integer divisions, as long
11598 * as those integer divisions are defined in terms of
11599 * parameters or input dimensions.
11600 * In this case, *div is set to the number of integer divisions and
11601 * *ineq is set to the number of inequality constraints (provided
11602 * div and ineq are not NULL).
11604 * The equality may also involve a single integer division involving
11605 * the output dimensions (typically only output dimension "pos") as
11606 * long as the coefficient of output dimension "pos" is 1 or -1 and
11607 * there is a pair of constraints i >= l and i <= l + n, with i referring
11608 * to output dimension "pos", l an expression involving only earlier
11609 * dimensions and n smaller than the coefficient of the integer division
11610 * in the equality. In this case, the output dimension can be defined
11611 * in terms of a modulo expression that does not involve the integer division.
11612 * *div is then set to this single integer division and
11613 * *ineq is set to the index of constraint i >= l.
11615 * Return bmap->n_eq if there is no such equality.
11616 * Return -1 on error.
11618 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11619 int pos, int *div, int *ineq)
11621 int j, k, l;
11622 isl_size n_div, n_out;
11623 unsigned o_div, o_out;
11625 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11626 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11627 if (n_out < 0 || n_div < 0)
11628 return -1;
11630 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11631 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11633 if (ineq)
11634 *ineq = bmap->n_ineq;
11635 if (div)
11636 *div = n_div;
11637 for (j = 0; j < bmap->n_eq; ++j) {
11638 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11639 continue;
11640 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11641 n_out - (pos + 1)) != -1)
11642 continue;
11643 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11644 0, n_div);
11645 if (k >= n_div)
11646 return j;
11647 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11648 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11649 continue;
11650 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11651 k + 1, n_div - (k+1)) < n_div)
11652 continue;
11653 l = find_modulo_constraint_pair(bmap, pos,
11654 bmap->eq[j][o_div + k]);
11655 if (l < 0)
11656 return -1;
11657 if (l >= bmap->n_ineq)
11658 continue;
11659 if (div)
11660 *div = k;
11661 if (ineq)
11662 *ineq = l;
11663 return j;
11666 return bmap->n_eq;
11669 /* Check if the given basic map is obviously single-valued.
11670 * In particular, for each output dimension, check that there is
11671 * an equality that defines the output dimension in terms of
11672 * earlier dimensions.
11674 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11676 int i;
11677 isl_size n_out;
11679 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11680 if (n_out < 0)
11681 return isl_bool_error;
11683 for (i = 0; i < n_out; ++i) {
11684 int eq;
11686 eq = isl_basic_map_output_defining_equality(bmap, i,
11687 NULL, NULL);
11688 if (eq < 0)
11689 return isl_bool_error;
11690 if (eq >= bmap->n_eq)
11691 return isl_bool_false;
11694 return isl_bool_true;
11697 /* Check if the given basic map is single-valued.
11698 * We simply compute
11700 * M \circ M^-1
11702 * and check if the result is a subset of the identity mapping.
11704 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11706 isl_space *space;
11707 isl_basic_map *test;
11708 isl_basic_map *id;
11709 isl_bool sv;
11711 sv = isl_basic_map_plain_is_single_valued(bmap);
11712 if (sv < 0 || sv)
11713 return sv;
11715 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11716 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11718 space = isl_basic_map_get_space(bmap);
11719 space = isl_space_map_from_set(isl_space_range(space));
11720 id = isl_basic_map_identity(space);
11722 sv = isl_basic_map_is_subset(test, id);
11724 isl_basic_map_free(test);
11725 isl_basic_map_free(id);
11727 return sv;
11730 /* Check if the given map is obviously single-valued.
11732 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11734 if (!map)
11735 return isl_bool_error;
11736 if (map->n == 0)
11737 return isl_bool_true;
11738 if (map->n >= 2)
11739 return isl_bool_false;
11741 return isl_basic_map_plain_is_single_valued(map->p[0]);
11744 /* Check if the given map is single-valued.
11745 * We simply compute
11747 * M \circ M^-1
11749 * and check if the result is a subset of the identity mapping.
11751 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11753 isl_space *space;
11754 isl_map *test;
11755 isl_map *id;
11756 isl_bool sv;
11758 sv = isl_map_plain_is_single_valued(map);
11759 if (sv < 0 || sv)
11760 return sv;
11762 test = isl_map_reverse(isl_map_copy(map));
11763 test = isl_map_apply_range(test, isl_map_copy(map));
11765 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11766 id = isl_map_identity(space);
11768 sv = isl_map_is_subset(test, id);
11770 isl_map_free(test);
11771 isl_map_free(id);
11773 return sv;
11776 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11778 isl_bool in;
11780 map = isl_map_copy(map);
11781 map = isl_map_reverse(map);
11782 in = isl_map_is_single_valued(map);
11783 isl_map_free(map);
11785 return in;
11788 /* Check if the given map is obviously injective.
11790 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11792 isl_bool in;
11794 map = isl_map_copy(map);
11795 map = isl_map_reverse(map);
11796 in = isl_map_plain_is_single_valued(map);
11797 isl_map_free(map);
11799 return in;
11802 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11804 isl_bool sv;
11806 sv = isl_map_is_single_valued(map);
11807 if (sv < 0 || !sv)
11808 return sv;
11810 return isl_map_is_injective(map);
11813 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11815 return isl_map_is_single_valued(set_to_map(set));
11818 /* Does "map" only map elements to themselves?
11820 * If the domain and range spaces are different, then "map"
11821 * is considered not to be an identity relation, even if it is empty.
11822 * Otherwise, construct the maximal identity relation and
11823 * check whether "map" is a subset of this relation.
11825 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11827 isl_map *id;
11828 isl_bool equal, is_identity;
11830 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
11831 if (equal < 0 || !equal)
11832 return equal;
11834 id = isl_map_identity(isl_map_get_space(map));
11835 is_identity = isl_map_is_subset(map, id);
11836 isl_map_free(id);
11838 return is_identity;
11841 int isl_map_is_translation(__isl_keep isl_map *map)
11843 int ok;
11844 isl_set *delta;
11846 delta = isl_map_deltas(isl_map_copy(map));
11847 ok = isl_set_is_singleton(delta);
11848 isl_set_free(delta);
11850 return ok;
11853 static int unique(isl_int *p, unsigned pos, unsigned len)
11855 if (isl_seq_first_non_zero(p, pos) != -1)
11856 return 0;
11857 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11858 return 0;
11859 return 1;
11862 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11864 int i, j;
11865 isl_size nvar, n_div;
11866 unsigned ovar;
11868 n_div = isl_basic_set_dim(bset, isl_dim_div);
11869 if (n_div < 0)
11870 return isl_bool_error;
11871 if (n_div != 0)
11872 return isl_bool_false;
11874 nvar = isl_basic_set_dim(bset, isl_dim_set);
11875 if (nvar < 0)
11876 return isl_bool_error;
11877 ovar = isl_space_offset(bset->dim, isl_dim_set);
11878 for (j = 0; j < nvar; ++j) {
11879 int lower = 0, upper = 0;
11880 for (i = 0; i < bset->n_eq; ++i) {
11881 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11882 continue;
11883 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11884 return isl_bool_false;
11885 break;
11887 if (i < bset->n_eq)
11888 continue;
11889 for (i = 0; i < bset->n_ineq; ++i) {
11890 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11891 continue;
11892 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11893 return isl_bool_false;
11894 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11895 lower = 1;
11896 else
11897 upper = 1;
11899 if (!lower || !upper)
11900 return isl_bool_false;
11903 return isl_bool_true;
11906 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11908 if (!set)
11909 return isl_bool_error;
11910 if (set->n != 1)
11911 return isl_bool_false;
11913 return isl_basic_set_is_box(set->p[0]);
11916 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11918 if (!bset)
11919 return isl_bool_error;
11921 return isl_space_is_wrapping(bset->dim);
11924 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11926 if (!set)
11927 return isl_bool_error;
11929 return isl_space_is_wrapping(set->dim);
11932 /* Modify the space of "map" through a call to "change".
11933 * If "can_change" is set (not NULL), then first call it to check
11934 * if the modification is allowed, printing the error message "cannot_change"
11935 * if it is not.
11937 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11938 isl_bool (*can_change)(__isl_keep isl_map *map),
11939 const char *cannot_change,
11940 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11942 isl_bool ok;
11943 isl_space *space;
11945 if (!map)
11946 return NULL;
11948 ok = can_change ? can_change(map) : isl_bool_true;
11949 if (ok < 0)
11950 return isl_map_free(map);
11951 if (!ok)
11952 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11953 return isl_map_free(map));
11955 space = change(isl_map_get_space(map));
11956 map = isl_map_reset_space(map, space);
11958 return map;
11961 /* Is the domain of "map" a wrapped relation?
11963 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11965 if (!map)
11966 return isl_bool_error;
11968 return isl_space_domain_is_wrapping(map->dim);
11971 /* Does "map" have a wrapped relation in both domain and range?
11973 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11975 return isl_space_is_product(isl_map_peek_space(map));
11978 /* Is the range of "map" a wrapped relation?
11980 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11982 if (!map)
11983 return isl_bool_error;
11985 return isl_space_range_is_wrapping(map->dim);
11988 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11990 isl_space *space;
11992 space = isl_basic_map_take_space(bmap);
11993 space = isl_space_wrap(space);
11994 bmap = isl_basic_map_restore_space(bmap, space);
11996 bmap = isl_basic_map_finalize(bmap);
11998 return bset_from_bmap(bmap);
12001 /* Given a map A -> B, return the set (A -> B).
12003 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12005 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12008 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12010 bset = isl_basic_set_cow(bset);
12011 if (!bset)
12012 return NULL;
12014 bset->dim = isl_space_unwrap(bset->dim);
12015 if (!bset->dim)
12016 goto error;
12018 bset = isl_basic_set_finalize(bset);
12020 return bset_to_bmap(bset);
12021 error:
12022 isl_basic_set_free(bset);
12023 return NULL;
12026 /* Given a set (A -> B), return the map A -> B.
12027 * Error out if "set" is not of the form (A -> B).
12029 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12031 return isl_map_change_space(set, &isl_set_is_wrapping,
12032 "not a wrapping set", &isl_space_unwrap);
12035 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12036 enum isl_dim_type type)
12038 isl_space *space;
12040 space = isl_basic_map_take_space(bmap);
12041 space = isl_space_reset(space, type);
12042 bmap = isl_basic_map_restore_space(bmap, space);
12044 bmap = isl_basic_map_mark_final(bmap);
12046 return bmap;
12049 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12050 enum isl_dim_type type)
12052 int i;
12053 isl_space *space;
12055 if (!map)
12056 return NULL;
12058 if (!isl_space_is_named_or_nested(map->dim, type))
12059 return map;
12061 map = isl_map_cow(map);
12062 if (!map)
12063 return NULL;
12065 for (i = 0; i < map->n; ++i) {
12066 map->p[i] = isl_basic_map_reset(map->p[i], type);
12067 if (!map->p[i])
12068 goto error;
12071 space = isl_map_take_space(map);
12072 space = isl_space_reset(space, type);
12073 map = isl_map_restore_space(map, space);
12075 return map;
12076 error:
12077 isl_map_free(map);
12078 return NULL;
12081 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12083 isl_space *space;
12085 space = isl_basic_map_take_space(bmap);
12086 space = isl_space_flatten(space);
12087 bmap = isl_basic_map_restore_space(bmap, space);
12089 bmap = isl_basic_map_mark_final(bmap);
12091 return bmap;
12094 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12096 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12099 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12100 __isl_take isl_basic_map *bmap)
12102 isl_space *space;
12104 space = isl_basic_map_take_space(bmap);
12105 space = isl_space_flatten_domain(space);
12106 bmap = isl_basic_map_restore_space(bmap, space);
12108 bmap = isl_basic_map_mark_final(bmap);
12110 return bmap;
12113 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12114 __isl_take isl_basic_map *bmap)
12116 isl_space *space;
12118 space = isl_basic_map_take_space(bmap);
12119 space = isl_space_flatten_range(space);
12120 bmap = isl_basic_map_restore_space(bmap, space);
12122 bmap = isl_basic_map_mark_final(bmap);
12124 return bmap;
12127 /* Remove any internal structure from the spaces of domain and range of "map".
12129 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12131 if (!map)
12132 return NULL;
12134 if (!map->dim->nested[0] && !map->dim->nested[1])
12135 return map;
12137 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12140 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12142 return set_from_map(isl_map_flatten(set_to_map(set)));
12145 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12147 isl_space *space, *flat_space;
12148 isl_map *map;
12150 space = isl_set_get_space(set);
12151 flat_space = isl_space_flatten(isl_space_copy(space));
12152 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12153 flat_space));
12154 map = isl_map_intersect_domain(map, set);
12156 return map;
12159 /* Remove any internal structure from the space of the domain of "map".
12161 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12163 if (!map)
12164 return NULL;
12166 if (!map->dim->nested[0])
12167 return map;
12169 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12172 /* Remove any internal structure from the space of the range of "map".
12174 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12176 if (!map)
12177 return NULL;
12179 if (!map->dim->nested[1])
12180 return map;
12182 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12185 /* Reorder the dimensions of "bmap" according to the given dim_map
12186 * and set the dimension specification to "space" and
12187 * perform Gaussian elimination on the result.
12189 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12190 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12192 isl_basic_map *res;
12193 unsigned flags;
12194 isl_size n_div;
12196 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12197 if (n_div < 0 || !space || !dim_map)
12198 goto error;
12200 flags = bmap->flags;
12201 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12202 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12203 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12204 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12205 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12206 if (res)
12207 res->flags = flags;
12208 res = isl_basic_map_gauss(res, NULL);
12209 res = isl_basic_map_finalize(res);
12210 return res;
12211 error:
12212 isl_dim_map_free(dim_map);
12213 isl_basic_map_free(bmap);
12214 isl_space_free(space);
12215 return NULL;
12218 /* Reorder the dimensions of "map" according to given reordering.
12220 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12221 __isl_take isl_reordering *r)
12223 int i;
12224 struct isl_dim_map *dim_map;
12226 map = isl_map_cow(map);
12227 dim_map = isl_dim_map_from_reordering(r);
12228 if (!map || !r || !dim_map)
12229 goto error;
12231 for (i = 0; i < map->n; ++i) {
12232 struct isl_dim_map *dim_map_i;
12233 isl_space *space;
12235 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12237 space = isl_reordering_get_space(r);
12238 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12240 if (!map->p[i])
12241 goto error;
12244 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12245 map = isl_map_unmark_normalized(map);
12247 isl_reordering_free(r);
12248 isl_dim_map_free(dim_map);
12249 return map;
12250 error:
12251 isl_dim_map_free(dim_map);
12252 isl_map_free(map);
12253 isl_reordering_free(r);
12254 return NULL;
12257 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12258 __isl_take isl_reordering *r)
12260 return set_from_map(isl_map_realign(set_to_map(set), r));
12263 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12264 __isl_take isl_space *model)
12266 isl_ctx *ctx;
12267 isl_bool aligned;
12269 if (!map || !model)
12270 goto error;
12272 ctx = isl_space_get_ctx(model);
12273 if (!isl_space_has_named_params(model))
12274 isl_die(ctx, isl_error_invalid,
12275 "model has unnamed parameters", goto error);
12276 if (isl_map_check_named_params(map) < 0)
12277 goto error;
12278 aligned = isl_map_space_has_equal_params(map, model);
12279 if (aligned < 0)
12280 goto error;
12281 if (!aligned) {
12282 isl_reordering *exp;
12284 exp = isl_parameter_alignment_reordering(map->dim, model);
12285 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
12286 map = isl_map_realign(map, exp);
12289 isl_space_free(model);
12290 return map;
12291 error:
12292 isl_space_free(model);
12293 isl_map_free(map);
12294 return NULL;
12297 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12298 __isl_take isl_space *model)
12300 return isl_map_align_params(set, model);
12303 /* Align the parameters of "bmap" to those of "model", introducing
12304 * additional parameters if needed.
12306 __isl_give isl_basic_map *isl_basic_map_align_params(
12307 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12309 isl_ctx *ctx;
12310 isl_bool equal_params;
12312 if (!bmap || !model)
12313 goto error;
12315 ctx = isl_space_get_ctx(model);
12316 if (!isl_space_has_named_params(model))
12317 isl_die(ctx, isl_error_invalid,
12318 "model has unnamed parameters", goto error);
12319 if (isl_basic_map_check_named_params(bmap) < 0)
12320 goto error;
12321 equal_params = isl_space_has_equal_params(bmap->dim, model);
12322 if (equal_params < 0)
12323 goto error;
12324 if (!equal_params) {
12325 isl_reordering *exp;
12326 struct isl_dim_map *dim_map;
12328 exp = isl_parameter_alignment_reordering(bmap->dim, model);
12329 exp = isl_reordering_extend_space(exp,
12330 isl_basic_map_get_space(bmap));
12331 dim_map = isl_dim_map_from_reordering(exp);
12332 bmap = isl_basic_map_realign(bmap,
12333 isl_reordering_get_space(exp),
12334 isl_dim_map_extend(dim_map, bmap));
12335 isl_reordering_free(exp);
12336 isl_dim_map_free(dim_map);
12339 isl_space_free(model);
12340 return bmap;
12341 error:
12342 isl_space_free(model);
12343 isl_basic_map_free(bmap);
12344 return NULL;
12347 /* Do "bset" and "space" have the same parameters?
12349 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12350 __isl_keep isl_space *space)
12352 isl_space *bset_space;
12354 bset_space = isl_basic_set_peek_space(bset);
12355 return isl_space_has_equal_params(bset_space, space);
12358 /* Do "map" and "space" have the same parameters?
12360 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12361 __isl_keep isl_space *space)
12363 isl_space *map_space;
12365 map_space = isl_map_peek_space(map);
12366 return isl_space_has_equal_params(map_space, space);
12369 /* Do "set" and "space" have the same parameters?
12371 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12372 __isl_keep isl_space *space)
12374 return isl_map_space_has_equal_params(set_to_map(set), space);
12377 /* Align the parameters of "bset" to those of "model", introducing
12378 * additional parameters if needed.
12380 __isl_give isl_basic_set *isl_basic_set_align_params(
12381 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12383 return isl_basic_map_align_params(bset, model);
12386 /* Drop all parameters not referenced by "map".
12388 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
12390 int i;
12391 isl_size n;
12393 n = isl_map_dim(map, isl_dim_param);
12394 if (isl_map_check_named_params(map) < 0 || n < 0)
12395 return isl_map_free(map);
12397 for (i = n - 1; i >= 0; i--) {
12398 isl_bool involves;
12400 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
12401 if (involves < 0)
12402 return isl_map_free(map);
12403 if (!involves)
12404 map = isl_map_project_out(map, isl_dim_param, i, 1);
12407 return map;
12410 /* Drop all parameters not referenced by "set".
12412 __isl_give isl_set *isl_set_drop_unused_params(
12413 __isl_take isl_set *set)
12415 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12418 /* Drop all parameters not referenced by "bmap".
12420 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
12421 __isl_take isl_basic_map *bmap)
12423 isl_size nparam;
12424 int i;
12426 nparam = isl_basic_map_dim(bmap, isl_dim_param);
12427 if (nparam < 0 || isl_basic_map_check_named_params(bmap) < 0)
12428 return isl_basic_map_free(bmap);
12430 for (i = nparam - 1; i >= 0; i--) {
12431 isl_bool involves;
12433 involves = isl_basic_map_involves_dims(bmap,
12434 isl_dim_param, i, 1);
12435 if (involves < 0)
12436 return isl_basic_map_free(bmap);
12437 if (!involves)
12438 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
12441 return bmap;
12444 /* Drop all parameters not referenced by "bset".
12446 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12447 __isl_take isl_basic_set *bset)
12449 return bset_from_bmap(isl_basic_map_drop_unused_params(
12450 bset_to_bmap(bset)));
12453 /* Given a tuple of identifiers "tuple" in a space that corresponds
12454 * to that of "set", if any of those identifiers appear as parameters
12455 * in "set", then equate those parameters with the corresponding
12456 * set dimensions and project out the parameters.
12457 * The result therefore has no such parameters.
12459 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12460 __isl_keep isl_multi_id *tuple)
12462 int i;
12463 isl_size n;
12464 isl_space *set_space, *tuple_space;
12466 set_space = isl_set_peek_space(set);
12467 tuple_space = isl_multi_id_peek_space(tuple);
12468 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12469 return isl_set_free(set);
12470 n = isl_multi_id_size(tuple);
12471 if (n < 0)
12472 return isl_set_free(set);
12473 for (i = 0; i < n; ++i) {
12474 isl_id *id;
12475 int pos;
12477 id = isl_multi_id_get_at(tuple, i);
12478 if (!id)
12479 return isl_set_free(set);
12480 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12481 isl_id_free(id);
12482 if (pos < 0)
12483 continue;
12484 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12485 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12487 return set;
12490 /* Bind the set dimensions of "set" to parameters with identifiers
12491 * specified by "tuple", living in the same space as "set".
12493 * If no parameters with these identifiers appear in "set" already,
12494 * then the set dimensions are simply reinterpreted as parameters.
12495 * Otherwise, the parameters are first equated to the corresponding
12496 * set dimensions.
12498 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12499 __isl_take isl_multi_id *tuple)
12501 isl_space *space;
12503 set = equate_params(set, tuple);
12504 space = isl_set_get_space(set);
12505 space = isl_space_bind_set(space, tuple);
12506 isl_multi_id_free(tuple);
12507 set = isl_set_reset_space(set, space);
12509 return set;
12512 /* Given a tuple of identifiers "tuple" in a space that corresponds
12513 * to the domain of "map", if any of those identifiers appear as parameters
12514 * in "map", then equate those parameters with the corresponding
12515 * input dimensions and project out the parameters.
12516 * The result therefore has no such parameters.
12518 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12519 __isl_keep isl_multi_id *tuple)
12521 int i;
12522 isl_size n;
12523 isl_space *map_space, *tuple_space;
12525 map_space = isl_map_peek_space(map);
12526 tuple_space = isl_multi_id_peek_space(tuple);
12527 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12528 return isl_map_free(map);
12529 n = isl_multi_id_size(tuple);
12530 if (n < 0)
12531 return isl_map_free(map);
12532 for (i = 0; i < n; ++i) {
12533 isl_id *id;
12534 int pos;
12536 id = isl_multi_id_get_at(tuple, i);
12537 if (!id)
12538 return isl_map_free(map);
12539 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12540 isl_id_free(id);
12541 if (pos < 0)
12542 continue;
12543 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12544 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12546 return map;
12549 /* Bind the input dimensions of "map" to parameters with identifiers
12550 * specified by "tuple", living in the domain space of "map".
12552 * If no parameters with these identifiers appear in "map" already,
12553 * then the input dimensions are simply reinterpreted as parameters.
12554 * Otherwise, the parameters are first equated to the corresponding
12555 * input dimensions.
12557 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12558 __isl_take isl_multi_id *tuple)
12560 isl_space *space;
12561 isl_set *set;
12563 map = map_equate_params(map, tuple);
12564 space = isl_map_get_space(map);
12565 space = isl_space_bind_map_domain(space, tuple);
12566 isl_multi_id_free(tuple);
12567 set = set_from_map(isl_map_reset_space(map, space));
12569 return set;
12572 /* Bind the output dimensions of "map" to parameters with identifiers
12573 * specified by "tuple", living in the range space of "map".
12575 * Since binding is more easily implemented on the domain,
12576 * bind the input dimensions of the inverse of "map".
12578 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12579 __isl_take isl_multi_id *tuple)
12581 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12584 /* Insert a domain corresponding to "tuple"
12585 * into the nullary or unary relation "set".
12586 * The result has an extra initial tuple and is therefore
12587 * either a unary or binary relation.
12588 * Any parameters with identifiers in "tuple" are reinterpreted
12589 * as the corresponding domain dimensions.
12591 static __isl_give isl_map *unbind_params_insert_domain(
12592 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12594 isl_space *space;
12595 isl_reordering *r;
12597 space = isl_set_peek_space(set);
12598 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12599 isl_multi_id_free(tuple);
12601 return isl_map_realign(set_to_map(set), r);
12604 /* Construct a set with "tuple" as domain from the parameter domain "set".
12605 * Any parameters with identifiers in "tuple" are reinterpreted
12606 * as the corresponding set dimensions.
12608 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
12609 __isl_take isl_multi_id *tuple)
12611 isl_bool is_params;
12613 is_params = isl_set_is_params(set);
12614 if (is_params < 0)
12615 set = isl_set_free(set);
12616 else if (!is_params)
12617 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12618 "expecting parameter domain", set = isl_set_free(set));
12619 return set_from_map(unbind_params_insert_domain(set, tuple));
12622 /* Construct a map with "domain" as domain and "set" as range.
12623 * Any parameters with identifiers in "domain" are reinterpreted
12624 * as the corresponding domain dimensions.
12626 __isl_give isl_map *isl_set_unbind_params_insert_domain(
12627 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
12629 isl_bool is_params;
12631 is_params = isl_set_is_params(set);
12632 if (is_params < 0)
12633 set = isl_set_free(set);
12634 else if (is_params)
12635 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12636 "expecting proper set", set = isl_set_free(set));
12637 return unbind_params_insert_domain(set, domain);
12640 __isl_give isl_mat *isl_basic_map_equalities_matrix(
12641 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12642 enum isl_dim_type c2, enum isl_dim_type c3,
12643 enum isl_dim_type c4, enum isl_dim_type c5)
12645 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12646 struct isl_mat *mat;
12647 int i, j, k;
12648 int pos;
12649 isl_size total;
12651 total = isl_basic_map_dim(bmap, isl_dim_all);
12652 if (total < 0)
12653 return NULL;
12654 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
12655 if (!mat)
12656 return NULL;
12657 for (i = 0; i < bmap->n_eq; ++i)
12658 for (j = 0, pos = 0; j < 5; ++j) {
12659 int off = isl_basic_map_offset(bmap, c[j]);
12660 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12661 if (dim < 0)
12662 return isl_mat_free(mat);
12663 for (k = 0; k < dim; ++k) {
12664 isl_int_set(mat->row[i][pos],
12665 bmap->eq[i][off + k]);
12666 ++pos;
12670 return mat;
12673 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12674 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12675 enum isl_dim_type c2, enum isl_dim_type c3,
12676 enum isl_dim_type c4, enum isl_dim_type c5)
12678 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12679 struct isl_mat *mat;
12680 int i, j, k;
12681 int pos;
12682 isl_size total;
12684 total = isl_basic_map_dim(bmap, isl_dim_all);
12685 if (total < 0)
12686 return NULL;
12687 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12688 if (!mat)
12689 return NULL;
12690 for (i = 0; i < bmap->n_ineq; ++i)
12691 for (j = 0, pos = 0; j < 5; ++j) {
12692 int off = isl_basic_map_offset(bmap, c[j]);
12693 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12694 if (dim < 0)
12695 return isl_mat_free(mat);
12696 for (k = 0; k < dim; ++k) {
12697 isl_int_set(mat->row[i][pos],
12698 bmap->ineq[i][off + k]);
12699 ++pos;
12703 return mat;
12706 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
12707 __isl_take isl_space *space,
12708 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12709 enum isl_dim_type c2, enum isl_dim_type c3,
12710 enum isl_dim_type c4, enum isl_dim_type c5)
12712 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12713 isl_basic_map *bmap = NULL;
12714 isl_size dim;
12715 unsigned total;
12716 unsigned extra;
12717 int i, j, k, l;
12718 int pos;
12720 dim = isl_space_dim(space, isl_dim_all);
12721 if (dim < 0 || !eq || !ineq)
12722 goto error;
12724 if (eq->n_col != ineq->n_col)
12725 isl_die(space->ctx, isl_error_invalid,
12726 "equalities and inequalities matrices should have "
12727 "same number of columns", goto error);
12729 total = 1 + dim;
12731 if (eq->n_col < total)
12732 isl_die(space->ctx, isl_error_invalid,
12733 "number of columns too small", goto error);
12735 extra = eq->n_col - total;
12737 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
12738 eq->n_row, ineq->n_row);
12739 if (!bmap)
12740 goto error;
12741 for (i = 0; i < extra; ++i) {
12742 k = isl_basic_map_alloc_div(bmap);
12743 if (k < 0)
12744 goto error;
12745 isl_int_set_si(bmap->div[k][0], 0);
12747 for (i = 0; i < eq->n_row; ++i) {
12748 l = isl_basic_map_alloc_equality(bmap);
12749 if (l < 0)
12750 goto error;
12751 for (j = 0, pos = 0; j < 5; ++j) {
12752 int off = isl_basic_map_offset(bmap, c[j]);
12753 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12754 if (dim < 0)
12755 goto error;
12756 for (k = 0; k < dim; ++k) {
12757 isl_int_set(bmap->eq[l][off + k],
12758 eq->row[i][pos]);
12759 ++pos;
12763 for (i = 0; i < ineq->n_row; ++i) {
12764 l = isl_basic_map_alloc_inequality(bmap);
12765 if (l < 0)
12766 goto error;
12767 for (j = 0, pos = 0; j < 5; ++j) {
12768 int off = isl_basic_map_offset(bmap, c[j]);
12769 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12770 if (dim < 0)
12771 goto error;
12772 for (k = 0; k < dim; ++k) {
12773 isl_int_set(bmap->ineq[l][off + k],
12774 ineq->row[i][pos]);
12775 ++pos;
12780 isl_space_free(space);
12781 isl_mat_free(eq);
12782 isl_mat_free(ineq);
12784 bmap = isl_basic_map_simplify(bmap);
12785 return isl_basic_map_finalize(bmap);
12786 error:
12787 isl_space_free(space);
12788 isl_mat_free(eq);
12789 isl_mat_free(ineq);
12790 isl_basic_map_free(bmap);
12791 return NULL;
12794 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12795 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12796 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12798 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12799 c1, c2, c3, c4, isl_dim_in);
12802 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12803 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12804 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12806 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12807 c1, c2, c3, c4, isl_dim_in);
12810 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12811 __isl_take isl_space *space,
12812 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12813 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12815 isl_basic_map *bmap;
12816 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
12817 c1, c2, c3, c4, isl_dim_in);
12818 return bset_from_bmap(bmap);
12821 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12823 if (!bmap)
12824 return isl_bool_error;
12826 return isl_space_can_zip(bmap->dim);
12829 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12831 if (!map)
12832 return isl_bool_error;
12834 return isl_space_can_zip(map->dim);
12837 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12838 * (A -> C) -> (B -> D).
12840 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12842 unsigned pos;
12843 isl_size n_in;
12844 isl_size n1;
12845 isl_size n2;
12847 if (!bmap)
12848 return NULL;
12850 if (!isl_basic_map_can_zip(bmap))
12851 isl_die(bmap->ctx, isl_error_invalid,
12852 "basic map cannot be zipped", goto error);
12853 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12854 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12855 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12856 if (n_in < 0 || n1 < 0 || n2 < 0)
12857 return isl_basic_map_free(bmap);
12858 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
12859 bmap = isl_basic_map_cow(bmap);
12860 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12861 if (!bmap)
12862 return NULL;
12863 bmap->dim = isl_space_zip(bmap->dim);
12864 if (!bmap->dim)
12865 goto error;
12866 bmap = isl_basic_map_mark_final(bmap);
12867 return bmap;
12868 error:
12869 isl_basic_map_free(bmap);
12870 return NULL;
12873 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12874 * (A -> C) -> (B -> D).
12876 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12878 if (!map)
12879 return NULL;
12881 if (!isl_map_can_zip(map))
12882 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12883 goto error);
12885 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
12886 error:
12887 isl_map_free(map);
12888 return NULL;
12891 /* Can we apply isl_basic_map_curry to "bmap"?
12892 * That is, does it have a nested relation in its domain?
12894 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12896 if (!bmap)
12897 return isl_bool_error;
12899 return isl_space_can_curry(bmap->dim);
12902 /* Can we apply isl_map_curry to "map"?
12903 * That is, does it have a nested relation in its domain?
12905 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12907 if (!map)
12908 return isl_bool_error;
12910 return isl_space_can_curry(map->dim);
12913 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12914 * A -> (B -> C).
12916 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12919 if (!bmap)
12920 return NULL;
12922 if (!isl_basic_map_can_curry(bmap))
12923 isl_die(bmap->ctx, isl_error_invalid,
12924 "basic map cannot be curried", goto error);
12925 bmap = isl_basic_map_cow(bmap);
12926 if (!bmap)
12927 return NULL;
12928 bmap->dim = isl_space_curry(bmap->dim);
12929 if (!bmap->dim)
12930 goto error;
12931 bmap = isl_basic_map_mark_final(bmap);
12932 return bmap;
12933 error:
12934 isl_basic_map_free(bmap);
12935 return NULL;
12938 /* Given a map (A -> B) -> C, return the corresponding map
12939 * A -> (B -> C).
12941 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12943 return isl_map_change_space(map, &isl_map_can_curry,
12944 "map cannot be curried", &isl_space_curry);
12947 /* Can isl_map_range_curry be applied to "map"?
12948 * That is, does it have a nested relation in its range,
12949 * the domain of which is itself a nested relation?
12951 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12953 if (!map)
12954 return isl_bool_error;
12956 return isl_space_can_range_curry(map->dim);
12959 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12960 * A -> (B -> (C -> D)).
12962 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12964 return isl_map_change_space(map, &isl_map_can_range_curry,
12965 "map range cannot be curried",
12966 &isl_space_range_curry);
12969 /* Can we apply isl_basic_map_uncurry to "bmap"?
12970 * That is, does it have a nested relation in its domain?
12972 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12974 if (!bmap)
12975 return isl_bool_error;
12977 return isl_space_can_uncurry(bmap->dim);
12980 /* Can we apply isl_map_uncurry to "map"?
12981 * That is, does it have a nested relation in its domain?
12983 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12985 if (!map)
12986 return isl_bool_error;
12988 return isl_space_can_uncurry(map->dim);
12991 /* Given a basic map A -> (B -> C), return the corresponding basic map
12992 * (A -> B) -> C.
12994 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12997 if (!bmap)
12998 return NULL;
13000 if (!isl_basic_map_can_uncurry(bmap))
13001 isl_die(bmap->ctx, isl_error_invalid,
13002 "basic map cannot be uncurried",
13003 return isl_basic_map_free(bmap));
13004 bmap = isl_basic_map_cow(bmap);
13005 if (!bmap)
13006 return NULL;
13007 bmap->dim = isl_space_uncurry(bmap->dim);
13008 if (!bmap->dim)
13009 return isl_basic_map_free(bmap);
13010 bmap = isl_basic_map_mark_final(bmap);
13011 return bmap;
13014 /* Given a map A -> (B -> C), return the corresponding map
13015 * (A -> B) -> C.
13017 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13019 return isl_map_change_space(map, &isl_map_can_uncurry,
13020 "map cannot be uncurried", &isl_space_uncurry);
13023 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13024 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13026 return isl_map_equate(set, type1, pos1, type2, pos2);
13029 /* Construct a basic map where the given dimensions are equal to each other.
13031 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13032 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13034 isl_basic_map *bmap = NULL;
13035 int i;
13036 isl_size total;
13038 total = isl_space_dim(space, isl_dim_all);
13039 if (total < 0 ||
13040 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13041 isl_space_check_range(space, type2, pos2, 1) < 0)
13042 goto error;
13044 if (type1 == type2 && pos1 == pos2)
13045 return isl_basic_map_universe(space);
13047 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13048 i = isl_basic_map_alloc_equality(bmap);
13049 if (i < 0)
13050 goto error;
13051 isl_seq_clr(bmap->eq[i], 1 + total);
13052 pos1 += isl_basic_map_offset(bmap, type1);
13053 pos2 += isl_basic_map_offset(bmap, type2);
13054 isl_int_set_si(bmap->eq[i][pos1], -1);
13055 isl_int_set_si(bmap->eq[i][pos2], 1);
13056 bmap = isl_basic_map_finalize(bmap);
13057 isl_space_free(space);
13058 return bmap;
13059 error:
13060 isl_space_free(space);
13061 isl_basic_map_free(bmap);
13062 return NULL;
13065 /* Add a constraint imposing that the given two dimensions are equal.
13067 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13068 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13070 isl_basic_map *eq;
13072 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13074 bmap = isl_basic_map_intersect(bmap, eq);
13076 return bmap;
13079 /* Add a constraint imposing that the given two dimensions are equal.
13081 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13082 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13084 isl_basic_map *bmap;
13086 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13088 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13090 return map;
13093 /* Add a constraint imposing that the given two dimensions have opposite values.
13095 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13096 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13098 isl_basic_map *bmap = NULL;
13099 int i;
13100 isl_size total;
13102 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13103 return isl_map_free(map);
13104 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13105 return isl_map_free(map);
13107 total = isl_map_dim(map, isl_dim_all);
13108 if (total < 0)
13109 return isl_map_free(map);
13110 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13111 i = isl_basic_map_alloc_equality(bmap);
13112 if (i < 0)
13113 goto error;
13114 isl_seq_clr(bmap->eq[i], 1 + total);
13115 pos1 += isl_basic_map_offset(bmap, type1);
13116 pos2 += isl_basic_map_offset(bmap, type2);
13117 isl_int_set_si(bmap->eq[i][pos1], 1);
13118 isl_int_set_si(bmap->eq[i][pos2], 1);
13119 bmap = isl_basic_map_finalize(bmap);
13121 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13123 return map;
13124 error:
13125 isl_basic_map_free(bmap);
13126 isl_map_free(map);
13127 return NULL;
13130 /* Construct a constraint imposing that the value of the first dimension is
13131 * greater than or equal to that of the second.
13133 static __isl_give isl_constraint *constraint_order_ge(
13134 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13135 enum isl_dim_type type2, int pos2)
13137 isl_constraint *c;
13139 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13140 isl_space_check_range(space, type2, pos2, 1) < 0)
13141 space = isl_space_free(space);
13142 if (!space)
13143 return NULL;
13145 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13147 if (type1 == type2 && pos1 == pos2)
13148 return c;
13150 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13151 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13153 return c;
13156 /* Add a constraint imposing that the value of the first dimension is
13157 * greater than or equal to that of the second.
13159 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13160 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13162 isl_constraint *c;
13163 isl_space *space;
13165 if (type1 == type2 && pos1 == pos2)
13166 return bmap;
13167 space = isl_basic_map_get_space(bmap);
13168 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13169 bmap = isl_basic_map_add_constraint(bmap, c);
13171 return bmap;
13174 /* Add a constraint imposing that the value of the first dimension is
13175 * greater than or equal to that of the second.
13177 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13178 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13180 isl_constraint *c;
13181 isl_space *space;
13183 if (type1 == type2 && pos1 == pos2)
13184 return map;
13185 space = isl_map_get_space(map);
13186 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13187 map = isl_map_add_constraint(map, c);
13189 return map;
13192 /* Add a constraint imposing that the value of the first dimension is
13193 * less than or equal to that of the second.
13195 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
13196 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13198 return isl_map_order_ge(map, type2, pos2, type1, pos1);
13201 /* Construct a basic map where the value of the first dimension is
13202 * greater than that of the second.
13204 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13205 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13207 isl_basic_map *bmap = NULL;
13208 int i;
13209 isl_size total;
13211 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13212 isl_space_check_range(space, type2, pos2, 1) < 0)
13213 goto error;
13215 if (type1 == type2 && pos1 == pos2)
13216 return isl_basic_map_empty(space);
13218 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13219 total = isl_basic_map_dim(bmap, isl_dim_all);
13220 i = isl_basic_map_alloc_inequality(bmap);
13221 if (total < 0 || i < 0)
13222 return isl_basic_map_free(bmap);
13223 isl_seq_clr(bmap->ineq[i], 1 + total);
13224 pos1 += isl_basic_map_offset(bmap, type1);
13225 pos2 += isl_basic_map_offset(bmap, type2);
13226 isl_int_set_si(bmap->ineq[i][pos1], 1);
13227 isl_int_set_si(bmap->ineq[i][pos2], -1);
13228 isl_int_set_si(bmap->ineq[i][0], -1);
13229 bmap = isl_basic_map_finalize(bmap);
13231 return bmap;
13232 error:
13233 isl_space_free(space);
13234 isl_basic_map_free(bmap);
13235 return NULL;
13238 /* Add a constraint imposing that the value of the first dimension is
13239 * greater than that of the second.
13241 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13242 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13244 isl_basic_map *gt;
13246 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13248 bmap = isl_basic_map_intersect(bmap, gt);
13250 return bmap;
13253 /* Add a constraint imposing that the value of the first dimension is
13254 * greater than that of the second.
13256 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13257 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13259 isl_basic_map *bmap;
13261 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13263 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13265 return map;
13268 /* Add a constraint imposing that the value of the first dimension is
13269 * smaller than that of the second.
13271 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13272 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13274 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13277 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13278 int pos)
13280 isl_aff *div;
13281 isl_local_space *ls;
13283 if (!bmap)
13284 return NULL;
13286 if (!isl_basic_map_divs_known(bmap))
13287 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13288 "some divs are unknown", return NULL);
13290 ls = isl_basic_map_get_local_space(bmap);
13291 div = isl_local_space_get_div(ls, pos);
13292 isl_local_space_free(ls);
13294 return div;
13297 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13298 int pos)
13300 return isl_basic_map_get_div(bset, pos);
13303 /* Plug in "subs" for dimension "type", "pos" of "bset".
13305 * Let i be the dimension to replace and let "subs" be of the form
13307 * f/d
13309 * Any integer division with a non-zero coefficient for i,
13311 * floor((a i + g)/m)
13313 * is replaced by
13315 * floor((a f + d g)/(m d))
13317 * Constraints of the form
13319 * a i + g
13321 * are replaced by
13323 * a f + d g
13325 * We currently require that "subs" is an integral expression.
13326 * Handling rational expressions may require us to add stride constraints
13327 * as we do in isl_basic_set_preimage_multi_aff.
13329 __isl_give isl_basic_set *isl_basic_set_substitute(
13330 __isl_take isl_basic_set *bset,
13331 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
13333 int i;
13334 isl_int v;
13335 isl_ctx *ctx;
13336 isl_size n_div;
13338 if (bset && isl_basic_set_plain_is_empty(bset))
13339 return bset;
13341 bset = isl_basic_set_cow(bset);
13342 if (!bset || !subs)
13343 goto error;
13345 ctx = isl_basic_set_get_ctx(bset);
13346 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
13347 isl_die(ctx, isl_error_invalid,
13348 "spaces don't match", goto error);
13349 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
13350 if (n_div < 0)
13351 goto error;
13352 if (n_div != 0)
13353 isl_die(ctx, isl_error_unsupported,
13354 "cannot handle divs yet", goto error);
13355 if (!isl_int_is_one(subs->v->el[0]))
13356 isl_die(ctx, isl_error_invalid,
13357 "can only substitute integer expressions", goto error);
13359 pos += isl_basic_set_offset(bset, type);
13361 isl_int_init(v);
13363 for (i = 0; i < bset->n_eq; ++i) {
13364 if (isl_int_is_zero(bset->eq[i][pos]))
13365 continue;
13366 isl_int_set(v, bset->eq[i][pos]);
13367 isl_int_set_si(bset->eq[i][pos], 0);
13368 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
13369 v, subs->v->el + 1, subs->v->size - 1);
13372 for (i = 0; i < bset->n_ineq; ++i) {
13373 if (isl_int_is_zero(bset->ineq[i][pos]))
13374 continue;
13375 isl_int_set(v, bset->ineq[i][pos]);
13376 isl_int_set_si(bset->ineq[i][pos], 0);
13377 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
13378 v, subs->v->el + 1, subs->v->size - 1);
13381 for (i = 0; i < bset->n_div; ++i) {
13382 if (isl_int_is_zero(bset->div[i][1 + pos]))
13383 continue;
13384 isl_int_set(v, bset->div[i][1 + pos]);
13385 isl_int_set_si(bset->div[i][1 + pos], 0);
13386 isl_seq_combine(bset->div[i] + 1,
13387 subs->v->el[0], bset->div[i] + 1,
13388 v, subs->v->el + 1, subs->v->size - 1);
13389 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
13392 isl_int_clear(v);
13394 bset = isl_basic_set_simplify(bset);
13395 return isl_basic_set_finalize(bset);
13396 error:
13397 isl_basic_set_free(bset);
13398 return NULL;
13401 /* Plug in "subs" for dimension "type", "pos" of "set".
13403 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13404 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
13406 int i;
13408 if (set && isl_set_plain_is_empty(set))
13409 return set;
13411 set = isl_set_cow(set);
13412 if (!set || !subs)
13413 goto error;
13415 for (i = set->n - 1; i >= 0; --i) {
13416 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
13417 set = set_from_map(remove_if_empty(set_to_map(set), i));
13418 if (!set)
13419 return NULL;
13422 return set;
13423 error:
13424 isl_set_free(set);
13425 return NULL;
13428 /* Check if the range of "ma" is compatible with the domain or range
13429 * (depending on "type") of "bmap".
13431 static isl_stat check_basic_map_compatible_range_multi_aff(
13432 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13433 __isl_keep isl_multi_aff *ma)
13435 isl_bool m;
13436 isl_space *ma_space;
13438 ma_space = isl_multi_aff_get_space(ma);
13440 m = isl_space_has_equal_params(bmap->dim, ma_space);
13441 if (m < 0)
13442 goto error;
13443 if (!m)
13444 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13445 "parameters don't match", goto error);
13446 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13447 if (m < 0)
13448 goto error;
13449 if (!m)
13450 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13451 "spaces don't match", goto error);
13453 isl_space_free(ma_space);
13454 return isl_stat_ok;
13455 error:
13456 isl_space_free(ma_space);
13457 return isl_stat_error;
13460 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13461 * coefficients before the transformed range of dimensions,
13462 * the "n_after" coefficients after the transformed range of dimensions
13463 * and the coefficients of the other divs in "bmap".
13465 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13466 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13468 int i;
13469 isl_size n_param;
13470 isl_size n_set;
13471 isl_local_space *ls;
13473 if (n_div == 0)
13474 return bmap;
13476 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13477 n_param = isl_local_space_dim(ls, isl_dim_param);
13478 n_set = isl_local_space_dim(ls, isl_dim_set);
13479 if (n_param < 0 || n_set < 0)
13480 return isl_basic_map_free(bmap);
13482 for (i = 0; i < n_div; ++i) {
13483 int o_bmap = 0, o_ls = 0;
13485 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13486 o_bmap += 1 + 1 + n_param;
13487 o_ls += 1 + 1 + n_param;
13488 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13489 o_bmap += n_before;
13490 isl_seq_cpy(bmap->div[i] + o_bmap,
13491 ls->div->row[i] + o_ls, n_set);
13492 o_bmap += n_set;
13493 o_ls += n_set;
13494 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13495 o_bmap += n_after;
13496 isl_seq_cpy(bmap->div[i] + o_bmap,
13497 ls->div->row[i] + o_ls, n_div);
13498 o_bmap += n_div;
13499 o_ls += n_div;
13500 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13501 bmap = isl_basic_map_add_div_constraints(bmap, i);
13502 if (!bmap)
13503 goto error;
13506 isl_local_space_free(ls);
13507 return bmap;
13508 error:
13509 isl_local_space_free(ls);
13510 return isl_basic_map_free(bmap);
13513 /* How many stride constraints does "ma" enforce?
13514 * That is, how many of the affine expressions have a denominator
13515 * different from one?
13517 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13519 int i;
13520 int strides = 0;
13522 for (i = 0; i < ma->n; ++i)
13523 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13524 strides++;
13526 return strides;
13529 /* For each affine expression in ma of the form
13531 * x_i = (f_i y + h_i)/m_i
13533 * with m_i different from one, add a constraint to "bmap"
13534 * of the form
13536 * f_i y + h_i = m_i alpha_i
13538 * with alpha_i an additional existentially quantified variable.
13540 * The input variables of "ma" correspond to a subset of the variables
13541 * of "bmap". There are "n_before" variables in "bmap" before this
13542 * subset and "n_after" variables after this subset.
13543 * The integer divisions of the affine expressions in "ma" are assumed
13544 * to have been aligned. There are "n_div_ma" of them and
13545 * they appear first in "bmap", straight after the "n_after" variables.
13547 static __isl_give isl_basic_map *add_ma_strides(
13548 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13549 int n_before, int n_after, int n_div_ma)
13551 int i, k;
13552 int div;
13553 isl_size total;
13554 isl_size n_param;
13555 isl_size n_in;
13557 total = isl_basic_map_dim(bmap, isl_dim_all);
13558 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13559 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13560 if (total < 0 || n_param < 0 || n_in < 0)
13561 return isl_basic_map_free(bmap);
13562 for (i = 0; i < ma->n; ++i) {
13563 int o_bmap = 0, o_ma = 1;
13565 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13566 continue;
13567 div = isl_basic_map_alloc_div(bmap);
13568 k = isl_basic_map_alloc_equality(bmap);
13569 if (div < 0 || k < 0)
13570 goto error;
13571 isl_int_set_si(bmap->div[div][0], 0);
13572 isl_seq_cpy(bmap->eq[k] + o_bmap,
13573 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13574 o_bmap += 1 + n_param;
13575 o_ma += 1 + n_param;
13576 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13577 o_bmap += n_before;
13578 isl_seq_cpy(bmap->eq[k] + o_bmap,
13579 ma->u.p[i]->v->el + o_ma, n_in);
13580 o_bmap += n_in;
13581 o_ma += n_in;
13582 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13583 o_bmap += n_after;
13584 isl_seq_cpy(bmap->eq[k] + o_bmap,
13585 ma->u.p[i]->v->el + o_ma, n_div_ma);
13586 o_bmap += n_div_ma;
13587 o_ma += n_div_ma;
13588 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13589 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13590 total++;
13593 return bmap;
13594 error:
13595 isl_basic_map_free(bmap);
13596 return NULL;
13599 /* Replace the domain or range space (depending on "type) of "space" by "set".
13601 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13602 enum isl_dim_type type, __isl_take isl_space *set)
13604 if (type == isl_dim_in) {
13605 space = isl_space_range(space);
13606 space = isl_space_map_from_domain_and_range(set, space);
13607 } else {
13608 space = isl_space_domain(space);
13609 space = isl_space_map_from_domain_and_range(space, set);
13612 return space;
13615 /* Compute the preimage of the domain or range (depending on "type")
13616 * of "bmap" under the function represented by "ma".
13617 * In other words, plug in "ma" in the domain or range of "bmap".
13618 * The result is a basic map that lives in the same space as "bmap"
13619 * except that the domain or range has been replaced by
13620 * the domain space of "ma".
13622 * If bmap is represented by
13624 * A(p) + S u + B x + T v + C(divs) >= 0,
13626 * where u and x are input and output dimensions if type == isl_dim_out
13627 * while x and v are input and output dimensions if type == isl_dim_in,
13628 * and ma is represented by
13630 * x = D(p) + F(y) + G(divs')
13632 * then the result is
13634 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13636 * The divs in the input set are similarly adjusted.
13637 * In particular
13639 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13641 * becomes
13643 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13644 * B_i G(divs') + c_i(divs))/n_i)
13646 * If bmap is not a rational map and if F(y) involves any denominators
13648 * x_i = (f_i y + h_i)/m_i
13650 * then additional constraints are added to ensure that we only
13651 * map back integer points. That is we enforce
13653 * f_i y + h_i = m_i alpha_i
13655 * with alpha_i an additional existentially quantified variable.
13657 * We first copy over the divs from "ma".
13658 * Then we add the modified constraints and divs from "bmap".
13659 * Finally, we add the stride constraints, if needed.
13661 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13662 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13663 __isl_take isl_multi_aff *ma)
13665 int i, k;
13666 isl_space *space;
13667 isl_basic_map *res = NULL;
13668 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13669 isl_int f, c1, c2, g;
13670 isl_bool rational;
13671 int strides;
13673 isl_int_init(f);
13674 isl_int_init(c1);
13675 isl_int_init(c2);
13676 isl_int_init(g);
13678 ma = isl_multi_aff_align_divs(ma);
13679 if (!bmap || !ma)
13680 goto error;
13681 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13682 goto error;
13684 if (type == isl_dim_in) {
13685 n_before = 0;
13686 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13687 } else {
13688 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13689 n_after = 0;
13691 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13692 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13693 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13694 goto error;
13696 space = isl_multi_aff_get_domain_space(ma);
13697 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13698 rational = isl_basic_map_is_rational(bmap);
13699 strides = rational ? 0 : multi_aff_strides(ma);
13700 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13701 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13702 if (rational)
13703 res = isl_basic_map_set_rational(res);
13705 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13706 if (isl_basic_map_alloc_div(res) < 0)
13707 goto error;
13709 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13710 if (!res)
13711 goto error;
13713 for (i = 0; i < bmap->n_eq; ++i) {
13714 k = isl_basic_map_alloc_equality(res);
13715 if (k < 0)
13716 goto error;
13717 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13718 n_after, n_div_ma, n_div_bmap,
13719 f, c1, c2, g, 0) < 0)
13720 goto error;
13723 for (i = 0; i < bmap->n_ineq; ++i) {
13724 k = isl_basic_map_alloc_inequality(res);
13725 if (k < 0)
13726 goto error;
13727 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13728 n_after, n_div_ma, n_div_bmap,
13729 f, c1, c2, g, 0) < 0)
13730 goto error;
13733 for (i = 0; i < bmap->n_div; ++i) {
13734 if (isl_int_is_zero(bmap->div[i][0])) {
13735 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13736 continue;
13738 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13739 n_before, n_after, n_div_ma, n_div_bmap,
13740 f, c1, c2, g, 1) < 0)
13741 goto error;
13744 if (strides)
13745 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13747 isl_int_clear(f);
13748 isl_int_clear(c1);
13749 isl_int_clear(c2);
13750 isl_int_clear(g);
13751 isl_basic_map_free(bmap);
13752 isl_multi_aff_free(ma);
13753 res = isl_basic_map_simplify(res);
13754 return isl_basic_map_finalize(res);
13755 error:
13756 isl_int_clear(f);
13757 isl_int_clear(c1);
13758 isl_int_clear(c2);
13759 isl_int_clear(g);
13760 isl_basic_map_free(bmap);
13761 isl_multi_aff_free(ma);
13762 isl_basic_map_free(res);
13763 return NULL;
13766 /* Compute the preimage of "bset" under the function represented by "ma".
13767 * In other words, plug in "ma" in "bset". The result is a basic set
13768 * that lives in the domain space of "ma".
13770 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13771 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13773 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13776 /* Compute the preimage of the domain of "bmap" under the function
13777 * represented by "ma".
13778 * In other words, plug in "ma" in the domain of "bmap".
13779 * The result is a basic map that lives in the same space as "bmap"
13780 * except that the domain has been replaced by the domain space of "ma".
13782 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13783 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13785 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13788 /* Compute the preimage of the range of "bmap" under the function
13789 * represented by "ma".
13790 * In other words, plug in "ma" in the range of "bmap".
13791 * The result is a basic map that lives in the same space as "bmap"
13792 * except that the range has been replaced by the domain space of "ma".
13794 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13795 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13797 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13800 /* Check if the range of "ma" is compatible with the domain or range
13801 * (depending on "type") of "map".
13802 * Return isl_stat_error if anything is wrong.
13804 static isl_stat check_map_compatible_range_multi_aff(
13805 __isl_keep isl_map *map, enum isl_dim_type type,
13806 __isl_keep isl_multi_aff *ma)
13808 isl_bool m;
13809 isl_space *ma_space;
13811 ma_space = isl_multi_aff_get_space(ma);
13812 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
13813 isl_space_free(ma_space);
13814 if (m < 0)
13815 return isl_stat_error;
13816 if (!m)
13817 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13818 "spaces don't match", return isl_stat_error);
13819 return isl_stat_ok;
13822 /* Compute the preimage of the domain or range (depending on "type")
13823 * of "map" under the function represented by "ma".
13824 * In other words, plug in "ma" in the domain or range of "map".
13825 * The result is a map that lives in the same space as "map"
13826 * except that the domain or range has been replaced by
13827 * the domain space of "ma".
13829 * The parameters are assumed to have been aligned.
13831 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13832 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13834 int i;
13835 isl_space *space;
13837 map = isl_map_cow(map);
13838 ma = isl_multi_aff_align_divs(ma);
13839 if (!map || !ma)
13840 goto error;
13841 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13842 goto error;
13844 for (i = 0; i < map->n; ++i) {
13845 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13846 isl_multi_aff_copy(ma));
13847 if (!map->p[i])
13848 goto error;
13851 space = isl_multi_aff_get_domain_space(ma);
13852 space = isl_space_set(isl_map_get_space(map), type, space);
13854 isl_space_free(isl_map_take_space(map));
13855 map = isl_map_restore_space(map, space);
13856 if (!map)
13857 goto error;
13859 isl_multi_aff_free(ma);
13860 if (map->n > 1)
13861 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13862 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13863 return map;
13864 error:
13865 isl_multi_aff_free(ma);
13866 isl_map_free(map);
13867 return NULL;
13870 /* Compute the preimage of the domain or range (depending on "type")
13871 * of "map" under the function represented by "ma".
13872 * In other words, plug in "ma" in the domain or range of "map".
13873 * The result is a map that lives in the same space as "map"
13874 * except that the domain or range has been replaced by
13875 * the domain space of "ma".
13877 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13878 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13880 isl_bool aligned;
13882 if (!map || !ma)
13883 goto error;
13885 aligned = isl_map_space_has_equal_params(map, ma->space);
13886 if (aligned < 0)
13887 goto error;
13888 if (aligned)
13889 return map_preimage_multi_aff(map, type, ma);
13891 if (isl_map_check_named_params(map) < 0)
13892 goto error;
13893 if (!isl_space_has_named_params(ma->space))
13894 isl_die(map->ctx, isl_error_invalid,
13895 "unaligned unnamed parameters", goto error);
13896 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13897 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13899 return map_preimage_multi_aff(map, type, ma);
13900 error:
13901 isl_multi_aff_free(ma);
13902 return isl_map_free(map);
13905 /* Compute the preimage of "set" under the function represented by "ma".
13906 * In other words, plug in "ma" in "set". The result is a set
13907 * that lives in the domain space of "ma".
13909 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13910 __isl_take isl_multi_aff *ma)
13912 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13915 /* Compute the preimage of the domain of "map" under the function
13916 * represented by "ma".
13917 * In other words, plug in "ma" in the domain of "map".
13918 * The result is a map that lives in the same space as "map"
13919 * except that the domain has been replaced by the domain space of "ma".
13921 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13922 __isl_take isl_multi_aff *ma)
13924 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13927 /* Compute the preimage of the range of "map" under the function
13928 * represented by "ma".
13929 * In other words, plug in "ma" in the range of "map".
13930 * The result is a map that lives in the same space as "map"
13931 * except that the range has been replaced by the domain space of "ma".
13933 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13934 __isl_take isl_multi_aff *ma)
13936 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13939 /* Compute the preimage of "map" under the function represented by "pma".
13940 * In other words, plug in "pma" in the domain or range of "map".
13941 * The result is a map that lives in the same space as "map",
13942 * except that the space of type "type" has been replaced by
13943 * the domain space of "pma".
13945 * The parameters of "map" and "pma" are assumed to have been aligned.
13947 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13948 __isl_take isl_map *map, enum isl_dim_type type,
13949 __isl_take isl_pw_multi_aff *pma)
13951 int i;
13952 isl_map *res;
13954 if (!pma)
13955 goto error;
13957 if (pma->n == 0) {
13958 isl_pw_multi_aff_free(pma);
13959 res = isl_map_empty(isl_map_get_space(map));
13960 isl_map_free(map);
13961 return res;
13964 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13965 isl_multi_aff_copy(pma->p[0].maff));
13966 if (type == isl_dim_in)
13967 res = isl_map_intersect_domain(res,
13968 isl_map_copy(pma->p[0].set));
13969 else
13970 res = isl_map_intersect_range(res,
13971 isl_map_copy(pma->p[0].set));
13973 for (i = 1; i < pma->n; ++i) {
13974 isl_map *res_i;
13976 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13977 isl_multi_aff_copy(pma->p[i].maff));
13978 if (type == isl_dim_in)
13979 res_i = isl_map_intersect_domain(res_i,
13980 isl_map_copy(pma->p[i].set));
13981 else
13982 res_i = isl_map_intersect_range(res_i,
13983 isl_map_copy(pma->p[i].set));
13984 res = isl_map_union(res, res_i);
13987 isl_pw_multi_aff_free(pma);
13988 isl_map_free(map);
13989 return res;
13990 error:
13991 isl_pw_multi_aff_free(pma);
13992 isl_map_free(map);
13993 return NULL;
13996 /* Compute the preimage of "map" under the function represented by "pma".
13997 * In other words, plug in "pma" in the domain or range of "map".
13998 * The result is a map that lives in the same space as "map",
13999 * except that the space of type "type" has been replaced by
14000 * the domain space of "pma".
14002 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14003 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14005 isl_bool aligned;
14007 if (!map || !pma)
14008 goto error;
14010 aligned = isl_map_space_has_equal_params(map, pma->dim);
14011 if (aligned < 0)
14012 goto error;
14013 if (aligned)
14014 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14016 if (isl_map_check_named_params(map) < 0)
14017 goto error;
14018 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14019 goto error;
14020 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14021 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14023 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14024 error:
14025 isl_pw_multi_aff_free(pma);
14026 return isl_map_free(map);
14029 /* Compute the preimage of "set" under the function represented by "pma".
14030 * In other words, plug in "pma" in "set". The result is a set
14031 * that lives in the domain space of "pma".
14033 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14034 __isl_take isl_pw_multi_aff *pma)
14036 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14039 /* Compute the preimage of the domain of "map" under the function
14040 * represented by "pma".
14041 * In other words, plug in "pma" in the domain of "map".
14042 * The result is a map that lives in the same space as "map",
14043 * except that domain space has been replaced by the domain space of "pma".
14045 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14046 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14048 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14051 /* Compute the preimage of the range of "map" under the function
14052 * represented by "pma".
14053 * In other words, plug in "pma" in the range of "map".
14054 * The result is a map that lives in the same space as "map",
14055 * except that range space has been replaced by the domain space of "pma".
14057 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14058 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14060 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14063 /* Compute the preimage of "map" under the function represented by "mpa".
14064 * In other words, plug in "mpa" in the domain or range of "map".
14065 * The result is a map that lives in the same space as "map",
14066 * except that the space of type "type" has been replaced by
14067 * the domain space of "mpa".
14069 * If the map does not involve any constraints that refer to the
14070 * dimensions of the substituted space, then the only possible
14071 * effect of "mpa" on the map is to map the space to a different space.
14072 * We create a separate isl_multi_aff to effectuate this change
14073 * in order to avoid spurious splitting of the map along the pieces
14074 * of "mpa".
14075 * If "mpa" has a non-trivial explicit domain, however,
14076 * then the full substitution should be performed.
14078 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14079 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14081 isl_size n;
14082 isl_bool full;
14083 isl_pw_multi_aff *pma;
14085 n = isl_map_dim(map, type);
14086 if (n < 0 || !mpa)
14087 goto error;
14089 full = isl_map_involves_dims(map, type, 0, n);
14090 if (full >= 0 && !full)
14091 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14092 if (full < 0)
14093 goto error;
14094 if (!full) {
14095 isl_space *space;
14096 isl_multi_aff *ma;
14098 space = isl_multi_pw_aff_get_space(mpa);
14099 isl_multi_pw_aff_free(mpa);
14100 ma = isl_multi_aff_zero(space);
14101 return isl_map_preimage_multi_aff(map, type, ma);
14104 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14105 return isl_map_preimage_pw_multi_aff(map, type, pma);
14106 error:
14107 isl_map_free(map);
14108 isl_multi_pw_aff_free(mpa);
14109 return NULL;
14112 /* Compute the preimage of "map" under the function represented by "mpa".
14113 * In other words, plug in "mpa" in the domain "map".
14114 * The result is a map that lives in the same space as "map",
14115 * except that domain space has been replaced by the domain space of "mpa".
14117 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14118 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14120 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14123 /* Compute the preimage of "set" by the function represented by "mpa".
14124 * In other words, plug in "mpa" in "set".
14126 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14127 __isl_take isl_multi_pw_aff *mpa)
14129 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14132 /* Return a copy of the equality constraints of "bset" as a matrix.
14134 __isl_give isl_mat *isl_basic_set_extract_equalities(
14135 __isl_keep isl_basic_set *bset)
14137 isl_ctx *ctx;
14138 isl_size total;
14140 total = isl_basic_set_dim(bset, isl_dim_all);
14141 if (total < 0)
14142 return NULL;
14144 ctx = isl_basic_set_get_ctx(bset);
14145 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14148 /* Are the "n" "coefficients" starting at "first" of the integer division
14149 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14150 * to each other?
14151 * The "coefficient" at position 0 is the denominator.
14152 * The "coefficient" at position 1 is the constant term.
14154 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14155 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14156 unsigned first, unsigned n)
14158 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14159 return isl_bool_error;
14160 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14161 return isl_bool_error;
14162 return isl_seq_eq(bmap1->div[pos1] + first,
14163 bmap2->div[pos2] + first, n);
14166 /* Are the integer division expressions at position "pos1" in "bmap1" and
14167 * "pos2" in "bmap2" equal to each other, except that the constant terms
14168 * are different?
14170 isl_bool isl_basic_map_equal_div_expr_except_constant(
14171 __isl_keep isl_basic_map *bmap1, int pos1,
14172 __isl_keep isl_basic_map *bmap2, int pos2)
14174 isl_bool equal;
14175 isl_size total, total2;
14177 total = isl_basic_map_dim(bmap1, isl_dim_all);
14178 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14179 if (total < 0 || total2 < 0)
14180 return isl_bool_error;
14181 if (total != total2)
14182 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14183 "incomparable div expressions", return isl_bool_error);
14184 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14185 0, 1);
14186 if (equal < 0 || !equal)
14187 return equal;
14188 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14189 1, 1);
14190 if (equal < 0 || equal)
14191 return isl_bool_not(equal);
14192 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14193 2, total);
14196 /* Replace the numerator of the constant term of the integer division
14197 * expression at position "div" in "bmap" by "value".
14198 * The caller guarantees that this does not change the meaning
14199 * of the input.
14201 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14202 __isl_take isl_basic_map *bmap, int div, int value)
14204 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14205 return isl_basic_map_free(bmap);
14207 isl_int_set_si(bmap->div[div][1], value);
14209 return bmap;
14212 /* Is the point "inner" internal to inequality constraint "ineq"
14213 * of "bset"?
14214 * The point is considered to be internal to the inequality constraint,
14215 * if it strictly lies on the positive side of the inequality constraint,
14216 * or if it lies on the constraint and the constraint is lexico-positive.
14218 static isl_bool is_internal(__isl_keep isl_vec *inner,
14219 __isl_keep isl_basic_set *bset, int ineq)
14221 isl_ctx *ctx;
14222 int pos;
14223 isl_size total;
14225 if (!inner || !bset)
14226 return isl_bool_error;
14228 ctx = isl_basic_set_get_ctx(bset);
14229 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14230 &ctx->normalize_gcd);
14231 if (!isl_int_is_zero(ctx->normalize_gcd))
14232 return isl_int_is_nonneg(ctx->normalize_gcd);
14234 total = isl_basic_set_dim(bset, isl_dim_all);
14235 if (total < 0)
14236 return isl_bool_error;
14237 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14238 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14241 /* Tighten the inequality constraints of "bset" that are outward with respect
14242 * to the point "vec".
14243 * That is, tighten the constraints that are not satisfied by "vec".
14245 * "vec" is a point internal to some superset S of "bset" that is used
14246 * to make the subsets of S disjoint, by tightening one half of the constraints
14247 * that separate two subsets. In particular, the constraints of S
14248 * are all satisfied by "vec" and should not be tightened.
14249 * Of the internal constraints, those that have "vec" on the outside
14250 * are tightened. The shared facet is included in the adjacent subset
14251 * with the opposite constraint.
14252 * For constraints that saturate "vec", this criterion cannot be used
14253 * to determine which of the two sides should be tightened.
14254 * Instead, the sign of the first non-zero coefficient is used
14255 * to make this choice. Note that this second criterion is never used
14256 * on the constraints of S since "vec" is interior to "S".
14258 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14259 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14261 int j;
14263 bset = isl_basic_set_cow(bset);
14264 if (!bset)
14265 return NULL;
14266 for (j = 0; j < bset->n_ineq; ++j) {
14267 isl_bool internal;
14269 internal = is_internal(vec, bset, j);
14270 if (internal < 0)
14271 return isl_basic_set_free(bset);
14272 if (internal)
14273 continue;
14274 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14277 return bset;
14280 /* Replace the variables x of type "type" starting at "first" in "bmap"
14281 * by x' with x = M x' with M the matrix trans.
14282 * That is, replace the corresponding coefficients c by c M.
14284 * The transformation matrix should be a square matrix.
14286 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14287 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14288 __isl_take isl_mat *trans)
14290 unsigned pos;
14292 bmap = isl_basic_map_cow(bmap);
14293 if (!bmap || !trans)
14294 goto error;
14296 if (trans->n_row != trans->n_col)
14297 isl_die(trans->ctx, isl_error_invalid,
14298 "expecting square transformation matrix", goto error);
14299 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14300 goto error;
14302 pos = isl_basic_map_offset(bmap, type) + first;
14304 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14305 isl_mat_copy(trans)) < 0)
14306 goto error;
14307 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14308 isl_mat_copy(trans)) < 0)
14309 goto error;
14310 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14311 isl_mat_copy(trans)) < 0)
14312 goto error;
14314 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14315 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14317 isl_mat_free(trans);
14318 return bmap;
14319 error:
14320 isl_mat_free(trans);
14321 isl_basic_map_free(bmap);
14322 return NULL;
14325 /* Replace the variables x of type "type" starting at "first" in "bset"
14326 * by x' with x = M x' with M the matrix trans.
14327 * That is, replace the corresponding coefficients c by c M.
14329 * The transformation matrix should be a square matrix.
14331 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14332 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14333 __isl_take isl_mat *trans)
14335 return isl_basic_map_transform_dims(bset, type, first, trans);