introduce isl_size return type
[isl.git] / isl_map.c
blob6ece14037c862d3da635fe0b809ddc7f65fea880
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/id.h>
27 #include <isl/constraint.h>
28 #include "isl_space_private.h"
29 #include "isl_equalities.h"
30 #include <isl_lp_private.h>
31 #include <isl_seq.h>
32 #include <isl/set.h>
33 #include <isl/map.h>
34 #include <isl_reordering.h>
35 #include "isl_sample.h"
36 #include <isl_sort.h>
37 #include "isl_tab.h"
38 #include <isl/vec.h>
39 #include <isl_mat_private.h>
40 #include <isl_vec_private.h>
41 #include <isl_dim_map.h>
42 #include <isl_local_space_private.h>
43 #include <isl_aff_private.h>
44 #include <isl_options_private.h>
45 #include <isl_morph.h>
46 #include <isl_val_private.h>
47 #include <isl_printer_private.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 /* Treat "bset" as a basic map.
55 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
56 * this function performs a redundant cast.
58 static __isl_keep const isl_basic_map *const_bset_to_bmap(
59 __isl_keep const isl_basic_set *bset)
61 return (const isl_basic_map *) bset;
64 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
66 switch (type) {
67 case isl_dim_param: return 1;
68 case isl_dim_in: return 1 + dim->nparam;
69 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
70 default: return 0;
74 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
75 enum isl_dim_type type)
77 if (!bmap)
78 return 0;
79 switch (type) {
80 case isl_dim_cst: return 1;
81 case isl_dim_param:
82 case isl_dim_in:
83 case isl_dim_out: return isl_space_dim(bmap->dim, type);
84 case isl_dim_div: return bmap->n_div;
85 case isl_dim_all: return isl_basic_map_total_dim(bmap);
86 default: return 0;
90 /* Return the space of "map".
92 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
94 return map ? map->dim : NULL;
97 /* Return the space of "set".
99 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
101 return isl_map_peek_space(set_to_map(set));
104 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
106 return isl_space_dim(isl_map_peek_space(map), type);
109 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
111 return isl_map_dim(set_to_map(set), type);
114 /* Return the position of the variables of the given type
115 * within the sequence of variables of "bmap".
117 int isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
118 enum isl_dim_type type)
120 isl_space *space;
122 space = isl_basic_map_peek_space(bmap);
123 if (!space)
124 return -1;
126 switch (type) {
127 case isl_dim_param:
128 case isl_dim_in:
129 case isl_dim_out: return isl_space_offset(space, type);
130 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
131 case isl_dim_cst:
132 default:
133 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
134 "invalid dimension type", return -1);
138 /* Return the position of the variables of the given type
139 * within the sequence of variables of "bset".
141 int isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
142 enum isl_dim_type type)
144 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
147 /* Return the position of the coefficients of the variables of the given type
148 * within the sequence of coefficients of "bmap".
150 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
151 enum isl_dim_type type)
153 switch (type) {
154 case isl_dim_cst: return 0;
155 case isl_dim_param:
156 case isl_dim_in:
157 case isl_dim_out:
158 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
159 default: return 0;
163 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
164 enum isl_dim_type type)
166 return isl_basic_map_offset(bset, type);
169 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
171 return pos(map->dim, type);
174 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
175 enum isl_dim_type type)
177 return isl_basic_map_dim(bset, type);
180 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
182 return isl_basic_set_dim(bset, isl_dim_set);
185 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
187 return isl_basic_set_dim(bset, isl_dim_param);
190 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
192 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
195 unsigned isl_set_n_dim(__isl_keep isl_set *set)
197 return isl_set_dim(set, isl_dim_set);
200 unsigned isl_set_n_param(__isl_keep isl_set *set)
202 return isl_set_dim(set, isl_dim_param);
205 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
207 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
210 /* Return the number of equality constraints in the description of "bmap".
211 * Return -1 on error.
213 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
215 if (!bmap)
216 return -1;
217 return bmap->n_eq;
220 /* Return the number of equality constraints in the description of "bset".
221 * Return -1 on error.
223 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
225 return isl_basic_map_n_equality(bset_to_bmap(bset));
228 /* Return the number of inequality constraints in the description of "bmap".
229 * Return -1 on error.
231 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
233 if (!bmap)
234 return -1;
235 return bmap->n_ineq;
238 /* Return the number of inequality constraints in the description of "bset".
239 * Return -1 on error.
241 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
243 return isl_basic_map_n_inequality(bset_to_bmap(bset));
246 /* Do "bmap1" and "bmap2" have the same parameters?
248 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
249 __isl_keep isl_basic_map *bmap2)
251 isl_space *space1, *space2;
253 space1 = isl_basic_map_peek_space(bmap1);
254 space2 = isl_basic_map_peek_space(bmap2);
255 return isl_space_has_equal_params(space1, space2);
258 /* Do "map1" and "map2" have the same parameters?
260 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
261 __isl_keep isl_map *map2)
263 isl_space *space1, *space2;
265 space1 = isl_map_peek_space(map1);
266 space2 = isl_map_peek_space(map2);
267 return isl_space_has_equal_params(space1, space2);
270 /* Do "map" and "set" have the same parameters?
272 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
273 __isl_keep isl_set *set)
275 return isl_map_has_equal_params(map, set_to_map(set));
278 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
279 __isl_keep isl_set *set)
281 isl_bool m;
282 if (!map || !set)
283 return isl_bool_error;
284 m = isl_map_has_equal_params(map, set_to_map(set));
285 if (m < 0 || !m)
286 return m;
287 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
288 set->dim, isl_dim_set);
291 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
292 __isl_keep isl_basic_set *bset)
294 isl_bool m;
295 if (!bmap || !bset)
296 return isl_bool_error;
297 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
298 if (m < 0 || !m)
299 return m;
300 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
301 bset->dim, isl_dim_set);
304 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
305 __isl_keep isl_set *set)
307 isl_bool m;
308 if (!map || !set)
309 return isl_bool_error;
310 m = isl_map_has_equal_params(map, set_to_map(set));
311 if (m < 0 || !m)
312 return m;
313 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
314 set->dim, isl_dim_set);
317 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
318 __isl_keep isl_basic_set *bset)
320 isl_bool m;
321 if (!bmap || !bset)
322 return isl_bool_error;
323 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
324 if (m < 0 || !m)
325 return m;
326 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
327 bset->dim, isl_dim_set);
330 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
332 return bmap ? bmap->ctx : NULL;
335 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
337 return bset ? bset->ctx : NULL;
340 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
342 return map ? map->ctx : NULL;
345 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
347 return set ? set->ctx : NULL;
350 /* Return the space of "bmap".
352 __isl_keep isl_space *isl_basic_map_peek_space(
353 __isl_keep const isl_basic_map *bmap)
355 return bmap ? bmap->dim : NULL;
358 /* Return the space of "bset".
360 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
362 return isl_basic_map_peek_space(bset_to_bmap(bset));
365 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
367 return isl_space_copy(isl_basic_map_peek_space(bmap));
370 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
372 return isl_basic_map_get_space(bset_to_bmap(bset));
375 /* Extract the divs in "bmap" as a matrix.
377 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
379 int i;
380 isl_ctx *ctx;
381 isl_mat *div;
382 int v_div;
383 unsigned cols;
385 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
386 if (v_div < 0)
387 return NULL;
389 ctx = isl_basic_map_get_ctx(bmap);
390 cols = 1 + 1 + v_div + bmap->n_div;
391 div = isl_mat_alloc(ctx, bmap->n_div, cols);
392 if (!div)
393 return NULL;
395 for (i = 0; i < bmap->n_div; ++i)
396 isl_seq_cpy(div->row[i], bmap->div[i], cols);
398 return div;
401 /* Extract the divs in "bset" as a matrix.
403 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
405 return isl_basic_map_get_divs(bset);
408 __isl_give isl_local_space *isl_basic_map_get_local_space(
409 __isl_keep isl_basic_map *bmap)
411 isl_mat *div;
413 if (!bmap)
414 return NULL;
416 div = isl_basic_map_get_divs(bmap);
417 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
420 __isl_give isl_local_space *isl_basic_set_get_local_space(
421 __isl_keep isl_basic_set *bset)
423 return isl_basic_map_get_local_space(bset);
426 /* For each known div d = floor(f/m), add the constraints
428 * f - m d >= 0
429 * -(f-(m-1)) + m d >= 0
431 * Do not finalize the result.
433 static __isl_give isl_basic_map *add_known_div_constraints(
434 __isl_take isl_basic_map *bmap)
436 int i;
437 unsigned n_div;
439 if (!bmap)
440 return NULL;
441 n_div = isl_basic_map_dim(bmap, isl_dim_div);
442 if (n_div == 0)
443 return bmap;
444 bmap = isl_basic_map_cow(bmap);
445 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
446 if (!bmap)
447 return NULL;
448 for (i = 0; i < n_div; ++i) {
449 if (isl_int_is_zero(bmap->div[i][0]))
450 continue;
451 bmap = isl_basic_map_add_div_constraints(bmap, i);
454 return bmap;
457 __isl_give isl_basic_map *isl_basic_map_from_local_space(
458 __isl_take isl_local_space *ls)
460 int i;
461 int n_div;
462 isl_basic_map *bmap;
464 if (!ls)
465 return NULL;
467 n_div = isl_local_space_dim(ls, isl_dim_div);
468 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
469 n_div, 0, 2 * n_div);
471 for (i = 0; i < n_div; ++i)
472 if (isl_basic_map_alloc_div(bmap) < 0)
473 goto error;
475 for (i = 0; i < n_div; ++i)
476 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
477 bmap = add_known_div_constraints(bmap);
479 isl_local_space_free(ls);
480 return bmap;
481 error:
482 isl_local_space_free(ls);
483 isl_basic_map_free(bmap);
484 return NULL;
487 __isl_give isl_basic_set *isl_basic_set_from_local_space(
488 __isl_take isl_local_space *ls)
490 return isl_basic_map_from_local_space(ls);
493 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
495 return isl_space_copy(isl_map_peek_space(map));
498 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
500 if (!set)
501 return NULL;
502 return isl_space_copy(set->dim);
505 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
506 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
508 bmap = isl_basic_map_cow(bmap);
509 if (!bmap)
510 return NULL;
511 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
512 if (!bmap->dim)
513 goto error;
514 bmap = isl_basic_map_finalize(bmap);
515 return bmap;
516 error:
517 isl_basic_map_free(bmap);
518 return NULL;
521 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
522 __isl_take isl_basic_set *bset, const char *s)
524 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
527 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
528 enum isl_dim_type type)
530 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
533 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
534 enum isl_dim_type type, const char *s)
536 int i;
538 map = isl_map_cow(map);
539 if (!map)
540 return NULL;
542 map->dim = isl_space_set_tuple_name(map->dim, type, s);
543 if (!map->dim)
544 goto error;
546 for (i = 0; i < map->n; ++i) {
547 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
548 if (!map->p[i])
549 goto error;
552 return map;
553 error:
554 isl_map_free(map);
555 return NULL;
558 /* Replace the identifier of the tuple of type "type" by "id".
560 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
561 __isl_take isl_basic_map *bmap,
562 enum isl_dim_type type, __isl_take isl_id *id)
564 bmap = isl_basic_map_cow(bmap);
565 if (!bmap)
566 goto error;
567 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
568 if (!bmap->dim)
569 return isl_basic_map_free(bmap);
570 bmap = isl_basic_map_finalize(bmap);
571 return bmap;
572 error:
573 isl_id_free(id);
574 return NULL;
577 /* Replace the identifier of the tuple by "id".
579 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
580 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
582 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
585 /* Does the input or output tuple have a name?
587 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
589 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
592 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
593 enum isl_dim_type type)
595 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
598 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
599 const char *s)
601 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
602 isl_dim_set, s));
605 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
606 enum isl_dim_type type, __isl_take isl_id *id)
608 map = isl_map_cow(map);
609 if (!map)
610 goto error;
612 map->dim = isl_space_set_tuple_id(map->dim, type, id);
614 return isl_map_reset_space(map, isl_space_copy(map->dim));
615 error:
616 isl_id_free(id);
617 return NULL;
620 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
621 __isl_take isl_id *id)
623 return isl_map_set_tuple_id(set, isl_dim_set, id);
626 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
627 enum isl_dim_type type)
629 map = isl_map_cow(map);
630 if (!map)
631 return NULL;
633 map->dim = isl_space_reset_tuple_id(map->dim, type);
635 return isl_map_reset_space(map, isl_space_copy(map->dim));
638 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
640 return isl_map_reset_tuple_id(set, isl_dim_set);
643 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
645 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
648 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
649 enum isl_dim_type type)
651 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
654 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
656 return isl_map_has_tuple_id(set, isl_dim_set);
659 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
661 return isl_map_get_tuple_id(set, isl_dim_set);
664 /* Does the set tuple have a name?
666 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
668 if (!set)
669 return isl_bool_error;
670 return isl_space_has_tuple_name(set->dim, isl_dim_set);
674 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
676 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
679 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
681 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
684 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
685 enum isl_dim_type type, unsigned pos)
687 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
690 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
691 enum isl_dim_type type, unsigned pos)
693 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
696 /* Does the given dimension have a name?
698 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
699 enum isl_dim_type type, unsigned pos)
701 if (!map)
702 return isl_bool_error;
703 return isl_space_has_dim_name(map->dim, type, pos);
706 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
707 enum isl_dim_type type, unsigned pos)
709 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
712 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
713 enum isl_dim_type type, unsigned pos)
715 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
718 /* Does the given dimension have a name?
720 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
721 enum isl_dim_type type, unsigned pos)
723 if (!set)
724 return isl_bool_error;
725 return isl_space_has_dim_name(set->dim, type, pos);
728 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
729 __isl_take isl_basic_map *bmap,
730 enum isl_dim_type type, unsigned pos, const char *s)
732 bmap = isl_basic_map_cow(bmap);
733 if (!bmap)
734 return NULL;
735 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
736 if (!bmap->dim)
737 goto error;
738 return isl_basic_map_finalize(bmap);
739 error:
740 isl_basic_map_free(bmap);
741 return NULL;
744 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
745 enum isl_dim_type type, unsigned pos, const char *s)
747 int i;
749 map = isl_map_cow(map);
750 if (!map)
751 return NULL;
753 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
754 if (!map->dim)
755 goto error;
757 for (i = 0; i < map->n; ++i) {
758 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
759 if (!map->p[i])
760 goto error;
763 return map;
764 error:
765 isl_map_free(map);
766 return NULL;
769 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
770 __isl_take isl_basic_set *bset,
771 enum isl_dim_type type, unsigned pos, const char *s)
773 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
774 type, pos, s));
777 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
778 enum isl_dim_type type, unsigned pos, const char *s)
780 return set_from_map(isl_map_set_dim_name(set_to_map(set),
781 type, pos, s));
784 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
785 enum isl_dim_type type, unsigned pos)
787 if (!bmap)
788 return isl_bool_error;
789 return isl_space_has_dim_id(bmap->dim, type, pos);
792 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
793 enum isl_dim_type type, unsigned pos)
795 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
798 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
799 enum isl_dim_type type, unsigned pos)
801 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
804 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
805 enum isl_dim_type type, unsigned pos)
807 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
810 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
811 enum isl_dim_type type, unsigned pos)
813 return isl_map_has_dim_id(set, type, pos);
816 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
817 enum isl_dim_type type, unsigned pos)
819 return isl_map_get_dim_id(set, type, pos);
822 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
823 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
825 map = isl_map_cow(map);
826 if (!map)
827 goto error;
829 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
831 return isl_map_reset_space(map, isl_space_copy(map->dim));
832 error:
833 isl_id_free(id);
834 return NULL;
837 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
838 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
840 return isl_map_set_dim_id(set, type, pos, id);
843 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
844 __isl_keep isl_id *id)
846 if (!map)
847 return -1;
848 return isl_space_find_dim_by_id(map->dim, type, id);
851 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
852 __isl_keep isl_id *id)
854 return isl_map_find_dim_by_id(set, type, id);
857 /* Return the position of the dimension of the given type and name
858 * in "bmap".
859 * Return -1 if no such dimension can be found.
861 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
862 enum isl_dim_type type, const char *name)
864 if (!bmap)
865 return -1;
866 return isl_space_find_dim_by_name(bmap->dim, type, name);
869 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
870 const char *name)
872 if (!map)
873 return -1;
874 return isl_space_find_dim_by_name(map->dim, type, name);
877 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
878 const char *name)
880 return isl_map_find_dim_by_name(set, type, name);
883 /* Check whether equality i of bset is a pure stride constraint
884 * on a single dimension, i.e., of the form
886 * v = k e
888 * with k a constant and e an existentially quantified variable.
890 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
892 unsigned nparam;
893 unsigned d;
894 unsigned n_div;
895 int pos1;
896 int pos2;
898 if (!bset)
899 return isl_bool_error;
901 if (!isl_int_is_zero(bset->eq[i][0]))
902 return isl_bool_false;
904 nparam = isl_basic_set_dim(bset, isl_dim_param);
905 d = isl_basic_set_dim(bset, isl_dim_set);
906 n_div = isl_basic_set_dim(bset, isl_dim_div);
908 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
909 return isl_bool_false;
910 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
911 if (pos1 == -1)
912 return isl_bool_false;
913 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
914 d - pos1 - 1) != -1)
915 return isl_bool_false;
917 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
918 if (pos2 == -1)
919 return isl_bool_false;
920 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
921 n_div - pos2 - 1) != -1)
922 return isl_bool_false;
923 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
924 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
925 return isl_bool_false;
927 return isl_bool_true;
930 /* Reset the user pointer on all identifiers of parameters and tuples
931 * of the space of "map".
933 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
935 isl_space *space;
937 space = isl_map_get_space(map);
938 space = isl_space_reset_user(space);
939 map = isl_map_reset_space(map, space);
941 return map;
944 /* Reset the user pointer on all identifiers of parameters and tuples
945 * of the space of "set".
947 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
949 return isl_map_reset_user(set);
952 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
954 if (!bmap)
955 return isl_bool_error;
956 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
959 /* Has "map" been marked as a rational map?
960 * In particular, have all basic maps in "map" been marked this way?
961 * An empty map is not considered to be rational.
962 * Maps where only some of the basic maps are marked rational
963 * are not allowed.
965 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
967 int i;
968 isl_bool rational;
970 if (!map)
971 return isl_bool_error;
972 if (map->n == 0)
973 return isl_bool_false;
974 rational = isl_basic_map_is_rational(map->p[0]);
975 if (rational < 0)
976 return rational;
977 for (i = 1; i < map->n; ++i) {
978 isl_bool rational_i;
980 rational_i = isl_basic_map_is_rational(map->p[i]);
981 if (rational_i < 0)
982 return rational_i;
983 if (rational != rational_i)
984 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
985 "mixed rational and integer basic maps "
986 "not supported", return isl_bool_error);
989 return rational;
992 /* Has "set" been marked as a rational set?
993 * In particular, have all basic set in "set" been marked this way?
994 * An empty set is not considered to be rational.
995 * Sets where only some of the basic sets are marked rational
996 * are not allowed.
998 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1000 return isl_map_is_rational(set);
1003 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1005 return isl_basic_map_is_rational(bset);
1008 /* Does "bmap" contain any rational points?
1010 * If "bmap" has an equality for each dimension, equating the dimension
1011 * to an integer constant, then it has no rational points, even if it
1012 * is marked as rational.
1014 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1016 isl_bool has_rational = isl_bool_true;
1017 unsigned total;
1019 if (!bmap)
1020 return isl_bool_error;
1021 if (isl_basic_map_plain_is_empty(bmap))
1022 return isl_bool_false;
1023 if (!isl_basic_map_is_rational(bmap))
1024 return isl_bool_false;
1025 bmap = isl_basic_map_copy(bmap);
1026 bmap = isl_basic_map_implicit_equalities(bmap);
1027 if (!bmap)
1028 return isl_bool_error;
1029 total = isl_basic_map_total_dim(bmap);
1030 if (bmap->n_eq == total) {
1031 int i, j;
1032 for (i = 0; i < bmap->n_eq; ++i) {
1033 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1034 if (j < 0)
1035 break;
1036 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1037 !isl_int_is_negone(bmap->eq[i][1 + j]))
1038 break;
1039 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1040 total - j - 1);
1041 if (j >= 0)
1042 break;
1044 if (i == bmap->n_eq)
1045 has_rational = isl_bool_false;
1047 isl_basic_map_free(bmap);
1049 return has_rational;
1052 /* Does "map" contain any rational points?
1054 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1056 int i;
1057 isl_bool has_rational;
1059 if (!map)
1060 return isl_bool_error;
1061 for (i = 0; i < map->n; ++i) {
1062 has_rational = isl_basic_map_has_rational(map->p[i]);
1063 if (has_rational < 0 || has_rational)
1064 return has_rational;
1066 return isl_bool_false;
1069 /* Does "set" contain any rational points?
1071 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1073 return isl_map_has_rational(set);
1076 /* Is this basic set a parameter domain?
1078 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1080 if (!bset)
1081 return isl_bool_error;
1082 return isl_space_is_params(bset->dim);
1085 /* Is this set a parameter domain?
1087 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1089 if (!set)
1090 return isl_bool_error;
1091 return isl_space_is_params(set->dim);
1094 /* Is this map actually a parameter domain?
1095 * Users should never call this function. Outside of isl,
1096 * a map can never be a parameter domain.
1098 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1100 if (!map)
1101 return isl_bool_error;
1102 return isl_space_is_params(map->dim);
1105 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1106 __isl_take isl_basic_map *bmap, unsigned extra,
1107 unsigned n_eq, unsigned n_ineq)
1109 int i;
1110 isl_space *space = isl_basic_map_peek_space(bmap);
1111 unsigned n_var = isl_space_dim(space, isl_dim_all);
1112 size_t row_size = 1 + n_var + extra;
1114 bmap->ctx = ctx;
1115 isl_ctx_ref(ctx);
1117 if (!space)
1118 return isl_basic_map_free(bmap);
1120 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1121 if (isl_blk_is_error(bmap->block))
1122 goto error;
1124 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1125 if ((n_ineq + n_eq) && !bmap->ineq)
1126 goto error;
1128 if (extra == 0) {
1129 bmap->block2 = isl_blk_empty();
1130 bmap->div = NULL;
1131 } else {
1132 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1133 if (isl_blk_is_error(bmap->block2))
1134 goto error;
1136 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1137 if (!bmap->div)
1138 goto error;
1141 for (i = 0; i < n_ineq + n_eq; ++i)
1142 bmap->ineq[i] = bmap->block.data + i * row_size;
1144 for (i = 0; i < extra; ++i)
1145 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1147 bmap->ref = 1;
1148 bmap->flags = 0;
1149 bmap->c_size = n_eq + n_ineq;
1150 bmap->eq = bmap->ineq + n_ineq;
1151 bmap->extra = extra;
1152 bmap->n_eq = 0;
1153 bmap->n_ineq = 0;
1154 bmap->n_div = 0;
1155 bmap->sample = NULL;
1157 return bmap;
1158 error:
1159 isl_basic_map_free(bmap);
1160 return NULL;
1163 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1164 unsigned nparam, unsigned dim, unsigned extra,
1165 unsigned n_eq, unsigned n_ineq)
1167 struct isl_basic_map *bmap;
1168 isl_space *space;
1170 space = isl_space_set_alloc(ctx, nparam, dim);
1171 if (!space)
1172 return NULL;
1174 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1175 return bset_from_bmap(bmap);
1178 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1179 unsigned extra, unsigned n_eq, unsigned n_ineq)
1181 struct isl_basic_map *bmap;
1182 if (!dim)
1183 return NULL;
1184 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1185 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1186 return bset_from_bmap(bmap);
1187 error:
1188 isl_space_free(dim);
1189 return NULL;
1192 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1193 unsigned extra, unsigned n_eq, unsigned n_ineq)
1195 struct isl_basic_map *bmap;
1197 if (!space)
1198 return NULL;
1199 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1200 if (!bmap)
1201 goto error;
1202 bmap->dim = space;
1204 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1205 error:
1206 isl_space_free(space);
1207 return NULL;
1210 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1211 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1212 unsigned n_eq, unsigned n_ineq)
1214 struct isl_basic_map *bmap;
1215 isl_space *dim;
1217 dim = isl_space_alloc(ctx, nparam, in, out);
1218 if (!dim)
1219 return NULL;
1221 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1222 return bmap;
1225 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1226 __isl_keep isl_basic_map *src)
1228 int i;
1229 unsigned total = isl_basic_map_total_dim(src);
1231 if (!dst)
1232 return NULL;
1234 for (i = 0; i < src->n_eq; ++i) {
1235 int j = isl_basic_map_alloc_equality(dst);
1236 if (j < 0)
1237 return isl_basic_map_free(dst);
1238 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1241 for (i = 0; i < src->n_ineq; ++i) {
1242 int j = isl_basic_map_alloc_inequality(dst);
1243 if (j < 0)
1244 return isl_basic_map_free(dst);
1245 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1248 for (i = 0; i < src->n_div; ++i) {
1249 int j = isl_basic_map_alloc_div(dst);
1250 if (j < 0)
1251 return isl_basic_map_free(dst);
1252 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1254 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1255 return dst;
1258 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1260 struct isl_basic_map *dup;
1262 if (!bmap)
1263 return NULL;
1264 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1265 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1266 dup = dup_constraints(dup, bmap);
1267 if (!dup)
1268 return NULL;
1269 dup->flags = bmap->flags;
1270 dup->sample = isl_vec_copy(bmap->sample);
1271 return dup;
1274 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1276 struct isl_basic_map *dup;
1278 dup = isl_basic_map_dup(bset_to_bmap(bset));
1279 return bset_from_bmap(dup);
1282 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1284 if (!bset)
1285 return NULL;
1287 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1288 bset->ref++;
1289 return bset;
1291 return isl_basic_set_dup(bset);
1294 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1296 if (!set)
1297 return NULL;
1299 set->ref++;
1300 return set;
1303 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1305 if (!bmap)
1306 return NULL;
1308 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1309 bmap->ref++;
1310 return bmap;
1312 bmap = isl_basic_map_dup(bmap);
1313 if (bmap)
1314 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1315 return bmap;
1318 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1320 if (!map)
1321 return NULL;
1323 map->ref++;
1324 return map;
1327 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1329 if (!bmap)
1330 return NULL;
1332 if (--bmap->ref > 0)
1333 return NULL;
1335 isl_ctx_deref(bmap->ctx);
1336 free(bmap->div);
1337 isl_blk_free(bmap->ctx, bmap->block2);
1338 free(bmap->ineq);
1339 isl_blk_free(bmap->ctx, bmap->block);
1340 isl_vec_free(bmap->sample);
1341 isl_space_free(bmap->dim);
1342 free(bmap);
1344 return NULL;
1347 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1349 return isl_basic_map_free(bset_to_bmap(bset));
1352 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1354 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1357 /* Check that "bset" does not involve any parameters.
1359 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1361 if (!bset)
1362 return isl_stat_error;
1363 if (isl_basic_set_dim(bset, isl_dim_param) != 0)
1364 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1365 "basic set should not have any parameters",
1366 return isl_stat_error);
1367 return isl_stat_ok;
1370 /* Check that "bset" does not involve any local variables.
1372 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1374 if (!bset)
1375 return isl_stat_error;
1376 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
1377 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1378 "basic set should not have any local variables",
1379 return isl_stat_error);
1380 return isl_stat_ok;
1383 /* Check that "map" has only named parameters, reporting an error
1384 * if it does not.
1386 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1388 return isl_space_check_named_params(isl_map_peek_space(map));
1391 /* Check that "bmap" has only named parameters, reporting an error
1392 * if it does not.
1394 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1396 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1399 /* Check that "bmap1" and "bmap2" have the same parameters,
1400 * reporting an error if they do not.
1402 static isl_stat isl_basic_map_check_equal_params(
1403 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1405 isl_bool match;
1407 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1408 if (match < 0)
1409 return isl_stat_error;
1410 if (!match)
1411 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1412 "parameters don't match", return isl_stat_error);
1413 return isl_stat_ok;
1416 __isl_give isl_map *isl_map_align_params_map_map_and(
1417 __isl_take isl_map *map1, __isl_take isl_map *map2,
1418 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1419 __isl_take isl_map *map2))
1421 if (!map1 || !map2)
1422 goto error;
1423 if (isl_map_has_equal_params(map1, map2))
1424 return fn(map1, map2);
1425 if (isl_map_check_named_params(map1) < 0)
1426 goto error;
1427 if (isl_map_check_named_params(map2) < 0)
1428 goto error;
1429 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1430 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1431 return fn(map1, map2);
1432 error:
1433 isl_map_free(map1);
1434 isl_map_free(map2);
1435 return NULL;
1438 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1439 __isl_keep isl_map *map2,
1440 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1442 isl_bool r;
1444 if (!map1 || !map2)
1445 return isl_bool_error;
1446 if (isl_map_has_equal_params(map1, map2))
1447 return fn(map1, map2);
1448 if (isl_map_check_named_params(map1) < 0)
1449 return isl_bool_error;
1450 if (isl_map_check_named_params(map2) < 0)
1451 return isl_bool_error;
1452 map1 = isl_map_copy(map1);
1453 map2 = isl_map_copy(map2);
1454 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1455 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1456 r = fn(map1, map2);
1457 isl_map_free(map1);
1458 isl_map_free(map2);
1459 return r;
1462 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1464 struct isl_ctx *ctx;
1465 if (!bmap)
1466 return -1;
1467 ctx = bmap->ctx;
1468 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1469 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1470 return -1);
1471 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1472 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1473 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1474 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1475 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1476 isl_int *t;
1477 int j = isl_basic_map_alloc_inequality(bmap);
1478 if (j < 0)
1479 return -1;
1480 t = bmap->ineq[j];
1481 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1482 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1483 bmap->eq[-1] = t;
1484 bmap->n_eq++;
1485 bmap->n_ineq--;
1486 bmap->eq--;
1487 return 0;
1489 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1490 bmap->extra - bmap->n_div);
1491 return bmap->n_eq++;
1494 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1496 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1499 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1501 if (!bmap)
1502 return -1;
1503 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1504 bmap->n_eq -= n;
1505 return 0;
1508 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1510 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1513 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1515 isl_int *t;
1516 if (!bmap)
1517 return -1;
1518 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1520 if (pos != bmap->n_eq - 1) {
1521 t = bmap->eq[pos];
1522 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1523 bmap->eq[bmap->n_eq - 1] = t;
1525 bmap->n_eq--;
1526 return 0;
1529 /* Turn inequality "pos" of "bmap" into an equality.
1531 * In particular, we move the inequality in front of the equalities
1532 * and move the last inequality in the position of the moved inequality.
1533 * Note that isl_tab_make_equalities_explicit depends on this particular
1534 * change in the ordering of the constraints.
1536 void isl_basic_map_inequality_to_equality(
1537 struct isl_basic_map *bmap, unsigned pos)
1539 isl_int *t;
1541 t = bmap->ineq[pos];
1542 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1543 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1544 bmap->eq[-1] = t;
1545 bmap->n_eq++;
1546 bmap->n_ineq--;
1547 bmap->eq--;
1548 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1549 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1550 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1551 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1554 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1556 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1559 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1561 struct isl_ctx *ctx;
1562 if (!bmap)
1563 return -1;
1564 ctx = bmap->ctx;
1565 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1566 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1567 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1568 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1569 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1570 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1571 1 + isl_basic_map_total_dim(bmap),
1572 bmap->extra - bmap->n_div);
1573 return bmap->n_ineq++;
1576 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1578 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1581 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1583 if (!bmap)
1584 return -1;
1585 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1586 bmap->n_ineq -= n;
1587 return 0;
1590 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1592 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1595 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1597 isl_int *t;
1598 if (!bmap)
1599 return -1;
1600 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1602 if (pos != bmap->n_ineq - 1) {
1603 t = bmap->ineq[pos];
1604 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1605 bmap->ineq[bmap->n_ineq - 1] = t;
1606 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1608 bmap->n_ineq--;
1609 return 0;
1612 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1614 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1617 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1618 isl_int *eq)
1620 isl_bool empty;
1621 int k;
1623 empty = isl_basic_map_plain_is_empty(bmap);
1624 if (empty < 0)
1625 return isl_basic_map_free(bmap);
1626 if (empty)
1627 return bmap;
1629 bmap = isl_basic_map_cow(bmap);
1630 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1631 if (!bmap)
1632 return NULL;
1633 k = isl_basic_map_alloc_equality(bmap);
1634 if (k < 0)
1635 goto error;
1636 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1637 return bmap;
1638 error:
1639 isl_basic_map_free(bmap);
1640 return NULL;
1643 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1644 isl_int *eq)
1646 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1649 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1650 isl_int *ineq)
1652 int k;
1654 bmap = isl_basic_map_cow(bmap);
1655 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1656 if (!bmap)
1657 return NULL;
1658 k = isl_basic_map_alloc_inequality(bmap);
1659 if (k < 0)
1660 goto error;
1661 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1662 return bmap;
1663 error:
1664 isl_basic_map_free(bmap);
1665 return NULL;
1668 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1669 isl_int *ineq)
1671 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1674 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1676 if (!bmap)
1677 return -1;
1678 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1679 isl_seq_clr(bmap->div[bmap->n_div] +
1680 1 + 1 + isl_basic_map_total_dim(bmap),
1681 bmap->extra - bmap->n_div);
1682 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1683 return bmap->n_div++;
1686 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1688 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1691 #undef TYPE
1692 #define TYPE isl_basic_map
1693 #include "check_type_range_templ.c"
1695 /* Check that there are "n" dimensions of type "type" starting at "first"
1696 * in "bset".
1698 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1699 enum isl_dim_type type, unsigned first, unsigned n)
1701 return isl_basic_map_check_range(bset_to_bmap(bset),
1702 type, first, n);
1705 /* Insert an extra integer division, prescribed by "div", to "bmap"
1706 * at (integer division) position "pos".
1708 * The integer division is first added at the end and then moved
1709 * into the right position.
1711 __isl_give isl_basic_map *isl_basic_map_insert_div(
1712 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1714 int i, k;
1716 bmap = isl_basic_map_cow(bmap);
1717 if (!bmap || !div)
1718 return isl_basic_map_free(bmap);
1720 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1721 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1722 "unexpected size", return isl_basic_map_free(bmap));
1723 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1724 return isl_basic_map_free(bmap);
1726 bmap = isl_basic_map_extend_space(bmap,
1727 isl_basic_map_get_space(bmap), 1, 0, 2);
1728 k = isl_basic_map_alloc_div(bmap);
1729 if (k < 0)
1730 return isl_basic_map_free(bmap);
1731 isl_seq_cpy(bmap->div[k], div->el, div->size);
1732 isl_int_set_si(bmap->div[k][div->size], 0);
1734 for (i = k; i > pos; --i)
1735 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1737 return bmap;
1740 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1742 if (!bmap)
1743 return isl_stat_error;
1744 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1745 bmap->n_div -= n;
1746 return isl_stat_ok;
1749 static __isl_give isl_basic_map *add_constraints(
1750 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1751 unsigned i_pos, unsigned o_pos)
1753 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1754 isl_ctx *ctx;
1755 isl_space *space;
1756 struct isl_dim_map *dim_map;
1758 space = isl_basic_map_peek_space(bmap2);
1759 if (!bmap1 || !space)
1760 goto error;
1762 total = isl_basic_map_dim(bmap1, isl_dim_all);
1763 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1764 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1765 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1766 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1767 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1768 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1769 ctx = isl_basic_map_get_ctx(bmap1);
1770 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1771 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1772 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1773 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1774 isl_dim_map_div(dim_map, bmap2, total);
1776 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1777 error:
1778 isl_basic_map_free(bmap1);
1779 isl_basic_map_free(bmap2);
1780 return NULL;
1783 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1784 struct isl_basic_set *bset2, unsigned pos)
1786 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1787 bset_to_bmap(bset2), 0, pos));
1790 __isl_give isl_basic_map *isl_basic_map_extend_space(
1791 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1792 unsigned extra, unsigned n_eq, unsigned n_ineq)
1794 struct isl_basic_map *ext;
1795 unsigned flags;
1796 int dims_ok;
1798 if (!space)
1799 goto error;
1801 if (!base)
1802 goto error;
1804 dims_ok = isl_space_is_equal(base->dim, space) &&
1805 base->extra >= base->n_div + extra;
1807 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1808 room_for_ineq(base, n_ineq)) {
1809 isl_space_free(space);
1810 return base;
1813 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1814 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1815 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1816 extra += base->extra;
1817 n_eq += base->n_eq;
1818 n_ineq += base->n_ineq;
1820 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1821 space = NULL;
1822 if (!ext)
1823 goto error;
1825 if (dims_ok)
1826 ext->sample = isl_vec_copy(base->sample);
1827 flags = base->flags;
1828 ext = add_constraints(ext, base, 0, 0);
1829 if (ext) {
1830 ext->flags = flags;
1831 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1834 return ext;
1836 error:
1837 isl_space_free(space);
1838 isl_basic_map_free(base);
1839 return NULL;
1842 __isl_give isl_basic_set *isl_basic_set_extend_space(
1843 __isl_take isl_basic_set *base,
1844 __isl_take isl_space *dim, unsigned extra,
1845 unsigned n_eq, unsigned n_ineq)
1847 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1848 dim, extra, n_eq, n_ineq));
1851 struct isl_basic_map *isl_basic_map_extend_constraints(
1852 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1854 if (!base)
1855 return NULL;
1856 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1857 0, n_eq, n_ineq);
1860 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1861 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1862 unsigned n_eq, unsigned n_ineq)
1864 struct isl_basic_map *bmap;
1865 isl_space *dim;
1867 if (!base)
1868 return NULL;
1869 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1870 if (!dim)
1871 goto error;
1873 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1874 return bmap;
1875 error:
1876 isl_basic_map_free(base);
1877 return NULL;
1880 struct isl_basic_set *isl_basic_set_extend_constraints(
1881 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1883 isl_basic_map *bmap = bset_to_bmap(base);
1884 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1885 return bset_from_bmap(bmap);
1888 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1890 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1893 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1895 if (!bmap)
1896 return NULL;
1898 if (bmap->ref > 1) {
1899 bmap->ref--;
1900 bmap = isl_basic_map_dup(bmap);
1902 if (bmap) {
1903 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1904 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1906 return bmap;
1909 /* Clear all cached information in "map", either because it is about
1910 * to be modified or because it is being freed.
1911 * Always return the same pointer that is passed in.
1912 * This is needed for the use in isl_map_free.
1914 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1916 isl_basic_map_free(map->cached_simple_hull[0]);
1917 isl_basic_map_free(map->cached_simple_hull[1]);
1918 map->cached_simple_hull[0] = NULL;
1919 map->cached_simple_hull[1] = NULL;
1920 return map;
1923 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1925 return isl_map_cow(set);
1928 /* Return an isl_map that is equal to "map" and that has only
1929 * a single reference.
1931 * If the original input already has only one reference, then
1932 * simply return it, but clear all cached information, since
1933 * it may be rendered invalid by the operations that will be
1934 * performed on the result.
1936 * Otherwise, create a duplicate (without any cached information).
1938 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1940 if (!map)
1941 return NULL;
1943 if (map->ref == 1)
1944 return clear_caches(map);
1945 map->ref--;
1946 return isl_map_dup(map);
1949 static void swap_vars(struct isl_blk blk, isl_int *a,
1950 unsigned a_len, unsigned b_len)
1952 isl_seq_cpy(blk.data, a+a_len, b_len);
1953 isl_seq_cpy(blk.data+b_len, a, a_len);
1954 isl_seq_cpy(a, blk.data, b_len+a_len);
1957 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1958 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1960 int i;
1961 struct isl_blk blk;
1963 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
1964 goto error;
1966 if (n1 == 0 || n2 == 0)
1967 return bmap;
1969 bmap = isl_basic_map_cow(bmap);
1970 if (!bmap)
1971 return NULL;
1973 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1974 if (isl_blk_is_error(blk))
1975 goto error;
1977 for (i = 0; i < bmap->n_eq; ++i)
1978 swap_vars(blk,
1979 bmap->eq[i] + pos, n1, n2);
1981 for (i = 0; i < bmap->n_ineq; ++i)
1982 swap_vars(blk,
1983 bmap->ineq[i] + pos, n1, n2);
1985 for (i = 0; i < bmap->n_div; ++i)
1986 swap_vars(blk,
1987 bmap->div[i]+1 + pos, n1, n2);
1989 isl_blk_free(bmap->ctx, blk);
1991 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
1992 bmap = isl_basic_map_gauss(bmap, NULL);
1993 return isl_basic_map_finalize(bmap);
1994 error:
1995 isl_basic_map_free(bmap);
1996 return NULL;
1999 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2000 __isl_take isl_basic_map *bmap)
2002 int i = 0;
2003 unsigned total;
2004 if (!bmap)
2005 goto error;
2006 total = isl_basic_map_total_dim(bmap);
2007 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2008 return isl_basic_map_free(bmap);
2009 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2010 if (bmap->n_eq > 0)
2011 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2012 else {
2013 i = isl_basic_map_alloc_equality(bmap);
2014 if (i < 0)
2015 goto error;
2017 isl_int_set_si(bmap->eq[i][0], 1);
2018 isl_seq_clr(bmap->eq[i]+1, total);
2019 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2020 isl_vec_free(bmap->sample);
2021 bmap->sample = NULL;
2022 return isl_basic_map_finalize(bmap);
2023 error:
2024 isl_basic_map_free(bmap);
2025 return NULL;
2028 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2029 __isl_take isl_basic_set *bset)
2031 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2034 __isl_give isl_basic_map *isl_basic_map_set_rational(
2035 __isl_take isl_basic_map *bmap)
2037 if (!bmap)
2038 return NULL;
2040 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2041 return bmap;
2043 bmap = isl_basic_map_cow(bmap);
2044 if (!bmap)
2045 return NULL;
2047 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2049 return isl_basic_map_finalize(bmap);
2052 __isl_give isl_basic_set *isl_basic_set_set_rational(
2053 __isl_take isl_basic_set *bset)
2055 return isl_basic_map_set_rational(bset);
2058 __isl_give isl_basic_set *isl_basic_set_set_integral(
2059 __isl_take isl_basic_set *bset)
2061 if (!bset)
2062 return NULL;
2064 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2065 return bset;
2067 bset = isl_basic_set_cow(bset);
2068 if (!bset)
2069 return NULL;
2071 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2073 return isl_basic_set_finalize(bset);
2076 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2078 int i;
2080 map = isl_map_cow(map);
2081 if (!map)
2082 return NULL;
2083 for (i = 0; i < map->n; ++i) {
2084 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2085 if (!map->p[i])
2086 goto error;
2088 return map;
2089 error:
2090 isl_map_free(map);
2091 return NULL;
2094 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2096 return isl_map_set_rational(set);
2099 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2100 * of "bmap").
2102 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2104 isl_int *t = bmap->div[a];
2105 bmap->div[a] = bmap->div[b];
2106 bmap->div[b] = t;
2109 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2110 * div definitions accordingly.
2112 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2113 int a, int b)
2115 int i;
2116 int off;
2118 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2119 if (off < 0)
2120 return isl_basic_map_free(bmap);
2122 swap_div(bmap, a, b);
2124 for (i = 0; i < bmap->n_eq; ++i)
2125 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2127 for (i = 0; i < bmap->n_ineq; ++i)
2128 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2130 for (i = 0; i < bmap->n_div; ++i)
2131 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2132 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2134 return bmap;
2137 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2139 isl_seq_cpy(c, c + n, rem);
2140 isl_seq_clr(c + rem, n);
2143 /* Drop n dimensions starting at first.
2145 * In principle, this frees up some extra variables as the number
2146 * of columns remains constant, but we would have to extend
2147 * the div array too as the number of rows in this array is assumed
2148 * to be equal to extra.
2150 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2151 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2153 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2156 /* Move "n" divs starting at "first" to the end of the list of divs.
2158 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2159 unsigned first, unsigned n)
2161 isl_int **div;
2162 int i;
2164 if (first + n == bmap->n_div)
2165 return bmap;
2167 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2168 if (!div)
2169 goto error;
2170 for (i = 0; i < n; ++i)
2171 div[i] = bmap->div[first + i];
2172 for (i = 0; i < bmap->n_div - first - n; ++i)
2173 bmap->div[first + i] = bmap->div[first + n + i];
2174 for (i = 0; i < n; ++i)
2175 bmap->div[bmap->n_div - n + i] = div[i];
2176 free(div);
2177 return bmap;
2178 error:
2179 isl_basic_map_free(bmap);
2180 return NULL;
2183 #undef TYPE
2184 #define TYPE isl_map
2185 static
2186 #include "check_type_range_templ.c"
2188 /* Check that there are "n" dimensions of type "type" starting at "first"
2189 * in "set".
2191 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2192 enum isl_dim_type type, unsigned first, unsigned n)
2194 return isl_map_check_range(set_to_map(set), type, first, n);
2197 /* Drop "n" dimensions of type "type" starting at "first".
2198 * Perform the core computation, without cowing or
2199 * simplifying and finalizing the result.
2201 * In principle, this frees up some extra variables as the number
2202 * of columns remains constant, but we would have to extend
2203 * the div array too as the number of rows in this array is assumed
2204 * to be equal to extra.
2206 __isl_give isl_basic_map *isl_basic_map_drop_core(
2207 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2208 unsigned first, unsigned n)
2210 int i;
2211 unsigned offset;
2212 unsigned left;
2214 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2215 return isl_basic_map_free(bmap);
2217 offset = isl_basic_map_offset(bmap, type) + first;
2218 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2219 for (i = 0; i < bmap->n_eq; ++i)
2220 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2222 for (i = 0; i < bmap->n_ineq; ++i)
2223 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2225 for (i = 0; i < bmap->n_div; ++i)
2226 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2228 if (type == isl_dim_div) {
2229 bmap = move_divs_last(bmap, first, n);
2230 if (!bmap)
2231 return NULL;
2232 if (isl_basic_map_free_div(bmap, n) < 0)
2233 return isl_basic_map_free(bmap);
2234 } else
2235 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2236 if (!bmap->dim)
2237 return isl_basic_map_free(bmap);
2239 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2240 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2241 return bmap;
2244 /* Drop "n" dimensions of type "type" starting at "first".
2246 * In principle, this frees up some extra variables as the number
2247 * of columns remains constant, but we would have to extend
2248 * the div array too as the number of rows in this array is assumed
2249 * to be equal to extra.
2251 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2252 enum isl_dim_type type, unsigned first, unsigned n)
2254 if (!bmap)
2255 return NULL;
2256 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2257 return bmap;
2259 bmap = isl_basic_map_cow(bmap);
2260 if (!bmap)
2261 return NULL;
2263 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2265 bmap = isl_basic_map_simplify(bmap);
2266 return isl_basic_map_finalize(bmap);
2269 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2270 enum isl_dim_type type, unsigned first, unsigned n)
2272 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2273 type, first, n));
2276 /* No longer consider "map" to be normalized.
2278 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2280 if (!map)
2281 return NULL;
2282 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2283 return map;
2286 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2287 enum isl_dim_type type, unsigned first, unsigned n)
2289 int i;
2291 if (isl_map_check_range(map, type, first, n) < 0)
2292 return isl_map_free(map);
2294 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2295 return map;
2296 map = isl_map_cow(map);
2297 if (!map)
2298 goto error;
2299 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2300 if (!map->dim)
2301 goto error;
2303 for (i = 0; i < map->n; ++i) {
2304 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2305 if (!map->p[i])
2306 goto error;
2308 map = isl_map_unmark_normalized(map);
2310 return map;
2311 error:
2312 isl_map_free(map);
2313 return NULL;
2316 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2317 enum isl_dim_type type, unsigned first, unsigned n)
2319 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2322 /* Drop the integer division at position "div", which is assumed
2323 * not to appear in any of the constraints or
2324 * in any of the other integer divisions.
2326 * Since the integer division is redundant, there is no need to cow.
2328 __isl_give isl_basic_map *isl_basic_map_drop_div(
2329 __isl_take isl_basic_map *bmap, unsigned div)
2331 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2334 /* Eliminate the specified n dimensions starting at first from the
2335 * constraints, without removing the dimensions from the space.
2336 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2338 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2339 enum isl_dim_type type, unsigned first, unsigned n)
2341 int i;
2343 if (n == 0)
2344 return map;
2346 if (isl_map_check_range(map, type, first, n) < 0)
2347 return isl_map_free(map);
2349 map = isl_map_cow(map);
2350 if (!map)
2351 return NULL;
2353 for (i = 0; i < map->n; ++i) {
2354 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2355 if (!map->p[i])
2356 goto error;
2358 return map;
2359 error:
2360 isl_map_free(map);
2361 return NULL;
2364 /* Eliminate the specified n dimensions starting at first from the
2365 * constraints, without removing the dimensions from the space.
2366 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2368 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2369 enum isl_dim_type type, unsigned first, unsigned n)
2371 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2374 /* Eliminate the specified n dimensions starting at first from the
2375 * constraints, without removing the dimensions from the space.
2376 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2378 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2379 unsigned first, unsigned n)
2381 return isl_set_eliminate(set, isl_dim_set, first, n);
2384 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2385 __isl_take isl_basic_map *bmap)
2387 int v_div;
2389 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2390 if (v_div < 0)
2391 return isl_basic_map_free(bmap);
2392 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2393 if (!bmap)
2394 return NULL;
2395 bmap->n_div = 0;
2396 return isl_basic_map_finalize(bmap);
2399 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2400 __isl_take isl_basic_set *bset)
2402 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2405 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2407 int i;
2409 if (!map)
2410 return NULL;
2411 if (map->n == 0)
2412 return map;
2414 map = isl_map_cow(map);
2415 if (!map)
2416 return NULL;
2418 for (i = 0; i < map->n; ++i) {
2419 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2420 if (!map->p[i])
2421 goto error;
2423 return map;
2424 error:
2425 isl_map_free(map);
2426 return NULL;
2429 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2431 return isl_map_remove_divs(set);
2434 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2435 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2436 unsigned first, unsigned n)
2438 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2439 return isl_basic_map_free(bmap);
2440 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2441 return bmap;
2442 bmap = isl_basic_map_eliminate_vars(bmap,
2443 isl_basic_map_offset(bmap, type) - 1 + first, n);
2444 if (!bmap)
2445 return bmap;
2446 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2447 return bmap;
2448 bmap = isl_basic_map_drop(bmap, type, first, n);
2449 return bmap;
2452 /* Return true if the definition of the given div (recursively) involves
2453 * any of the given variables.
2455 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2456 unsigned first, unsigned n)
2458 int i;
2459 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2461 if (isl_int_is_zero(bmap->div[div][0]))
2462 return isl_bool_false;
2463 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2464 return isl_bool_true;
2466 for (i = bmap->n_div - 1; i >= 0; --i) {
2467 isl_bool involves;
2469 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2470 continue;
2471 involves = div_involves_vars(bmap, i, first, n);
2472 if (involves < 0 || involves)
2473 return involves;
2476 return isl_bool_false;
2479 /* Try and add a lower and/or upper bound on "div" to "bmap"
2480 * based on inequality "i".
2481 * "total" is the total number of variables (excluding the divs).
2482 * "v" is a temporary object that can be used during the calculations.
2483 * If "lb" is set, then a lower bound should be constructed.
2484 * If "ub" is set, then an upper bound should be constructed.
2486 * The calling function has already checked that the inequality does not
2487 * reference "div", but we still need to check that the inequality is
2488 * of the right form. We'll consider the case where we want to construct
2489 * a lower bound. The construction of upper bounds is similar.
2491 * Let "div" be of the form
2493 * q = floor((a + f(x))/d)
2495 * We essentially check if constraint "i" is of the form
2497 * b + f(x) >= 0
2499 * so that we can use it to derive a lower bound on "div".
2500 * However, we allow a slightly more general form
2502 * b + g(x) >= 0
2504 * with the condition that the coefficients of g(x) - f(x) are all
2505 * divisible by d.
2506 * Rewriting this constraint as
2508 * 0 >= -b - g(x)
2510 * adding a + f(x) to both sides and dividing by d, we obtain
2512 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2514 * Taking the floor on both sides, we obtain
2516 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2518 * or
2520 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2522 * In the case of an upper bound, we construct the constraint
2524 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2527 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2528 __isl_take isl_basic_map *bmap, int div, int i,
2529 unsigned total, isl_int v, int lb, int ub)
2531 int j;
2533 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2534 if (lb) {
2535 isl_int_sub(v, bmap->ineq[i][1 + j],
2536 bmap->div[div][1 + 1 + j]);
2537 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2539 if (ub) {
2540 isl_int_add(v, bmap->ineq[i][1 + j],
2541 bmap->div[div][1 + 1 + j]);
2542 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2545 if (!lb && !ub)
2546 return bmap;
2548 bmap = isl_basic_map_cow(bmap);
2549 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2550 if (lb) {
2551 int k = isl_basic_map_alloc_inequality(bmap);
2552 if (k < 0)
2553 goto error;
2554 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2555 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2556 bmap->div[div][1 + j]);
2557 isl_int_cdiv_q(bmap->ineq[k][j],
2558 bmap->ineq[k][j], bmap->div[div][0]);
2560 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2562 if (ub) {
2563 int k = isl_basic_map_alloc_inequality(bmap);
2564 if (k < 0)
2565 goto error;
2566 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2567 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2568 bmap->div[div][1 + j]);
2569 isl_int_fdiv_q(bmap->ineq[k][j],
2570 bmap->ineq[k][j], bmap->div[div][0]);
2572 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2575 return bmap;
2576 error:
2577 isl_basic_map_free(bmap);
2578 return NULL;
2581 /* This function is called right before "div" is eliminated from "bmap"
2582 * using Fourier-Motzkin.
2583 * Look through the constraints of "bmap" for constraints on the argument
2584 * of the integer division and use them to construct constraints on the
2585 * integer division itself. These constraints can then be combined
2586 * during the Fourier-Motzkin elimination.
2587 * Note that it is only useful to introduce lower bounds on "div"
2588 * if "bmap" already contains upper bounds on "div" as the newly
2589 * introduce lower bounds can then be combined with the pre-existing
2590 * upper bounds. Similarly for upper bounds.
2591 * We therefore first check if "bmap" contains any lower and/or upper bounds
2592 * on "div".
2594 * It is interesting to note that the introduction of these constraints
2595 * can indeed lead to more accurate results, even when compared to
2596 * deriving constraints on the argument of "div" from constraints on "div".
2597 * Consider, for example, the set
2599 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2601 * The second constraint can be rewritten as
2603 * 2 * [(-i-2j+3)/4] + k >= 0
2605 * from which we can derive
2607 * -i - 2j + 3 >= -2k
2609 * or
2611 * i + 2j <= 3 + 2k
2613 * Combined with the first constraint, we obtain
2615 * -3 <= 3 + 2k or k >= -3
2617 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2618 * the first constraint, we obtain
2620 * [(i + 2j)/4] >= [-3/4] = -1
2622 * Combining this constraint with the second constraint, we obtain
2624 * k >= -2
2626 static __isl_give isl_basic_map *insert_bounds_on_div(
2627 __isl_take isl_basic_map *bmap, int div)
2629 int i;
2630 int check_lb, check_ub;
2631 isl_int v;
2632 int v_div;
2634 if (!bmap)
2635 return NULL;
2637 if (isl_int_is_zero(bmap->div[div][0]))
2638 return bmap;
2640 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2641 if (v_div < 0)
2642 return isl_basic_map_free(bmap);
2644 check_lb = 0;
2645 check_ub = 0;
2646 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2647 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2648 if (s > 0)
2649 check_ub = 1;
2650 if (s < 0)
2651 check_lb = 1;
2654 if (!check_lb && !check_ub)
2655 return bmap;
2657 isl_int_init(v);
2659 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2660 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2661 continue;
2663 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2664 check_lb, check_ub);
2667 isl_int_clear(v);
2669 return bmap;
2672 /* Remove all divs (recursively) involving any of the given dimensions
2673 * in their definitions.
2675 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2676 __isl_take isl_basic_map *bmap,
2677 enum isl_dim_type type, unsigned first, unsigned n)
2679 int i;
2681 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2682 return isl_basic_map_free(bmap);
2683 first += isl_basic_map_offset(bmap, type);
2685 for (i = bmap->n_div - 1; i >= 0; --i) {
2686 isl_bool involves;
2688 involves = div_involves_vars(bmap, i, first, n);
2689 if (involves < 0)
2690 return isl_basic_map_free(bmap);
2691 if (!involves)
2692 continue;
2693 bmap = insert_bounds_on_div(bmap, i);
2694 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2695 if (!bmap)
2696 return NULL;
2697 i = bmap->n_div;
2700 return bmap;
2703 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2704 __isl_take isl_basic_set *bset,
2705 enum isl_dim_type type, unsigned first, unsigned n)
2707 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2710 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2711 enum isl_dim_type type, unsigned first, unsigned n)
2713 int i;
2715 if (!map)
2716 return NULL;
2717 if (map->n == 0)
2718 return map;
2720 map = isl_map_cow(map);
2721 if (!map)
2722 return NULL;
2724 for (i = 0; i < map->n; ++i) {
2725 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2726 type, first, n);
2727 if (!map->p[i])
2728 goto error;
2730 return map;
2731 error:
2732 isl_map_free(map);
2733 return NULL;
2736 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2737 enum isl_dim_type type, unsigned first, unsigned n)
2739 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2740 type, first, n));
2743 /* Does the description of "bmap" depend on the specified dimensions?
2744 * We also check whether the dimensions appear in any of the div definitions.
2745 * In principle there is no need for this check. If the dimensions appear
2746 * in a div definition, they also appear in the defining constraints of that
2747 * div.
2749 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2750 enum isl_dim_type type, unsigned first, unsigned n)
2752 int i;
2754 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2755 return isl_bool_error;
2757 first += isl_basic_map_offset(bmap, type);
2758 for (i = 0; i < bmap->n_eq; ++i)
2759 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2760 return isl_bool_true;
2761 for (i = 0; i < bmap->n_ineq; ++i)
2762 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2763 return isl_bool_true;
2764 for (i = 0; i < bmap->n_div; ++i) {
2765 if (isl_int_is_zero(bmap->div[i][0]))
2766 continue;
2767 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2768 return isl_bool_true;
2771 return isl_bool_false;
2774 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2775 enum isl_dim_type type, unsigned first, unsigned n)
2777 int i;
2779 if (isl_map_check_range(map, type, first, n) < 0)
2780 return isl_bool_error;
2782 for (i = 0; i < map->n; ++i) {
2783 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2784 type, first, n);
2785 if (involves < 0 || involves)
2786 return involves;
2789 return isl_bool_false;
2792 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2793 enum isl_dim_type type, unsigned first, unsigned n)
2795 return isl_basic_map_involves_dims(bset, type, first, n);
2798 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2799 enum isl_dim_type type, unsigned first, unsigned n)
2801 return isl_map_involves_dims(set, type, first, n);
2804 /* Drop all constraints in bmap that involve any of the dimensions
2805 * first to first+n-1.
2806 * This function only performs the actual removal of constraints.
2808 * This function should not call finalize since it is used by
2809 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2811 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2812 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2814 int i;
2816 if (n == 0)
2817 return bmap;
2819 bmap = isl_basic_map_cow(bmap);
2821 if (!bmap)
2822 return NULL;
2824 for (i = bmap->n_eq - 1; i >= 0; --i) {
2825 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2826 continue;
2827 isl_basic_map_drop_equality(bmap, i);
2830 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2831 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2832 continue;
2833 isl_basic_map_drop_inequality(bmap, i);
2836 return bmap;
2839 /* Drop all constraints in bset that involve any of the dimensions
2840 * first to first+n-1.
2841 * This function only performs the actual removal of constraints.
2843 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2844 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2846 return isl_basic_map_drop_constraints_involving(bset, first, n);
2849 /* Drop all constraints in bmap that do not involve any of the dimensions
2850 * first to first + n - 1 of the given type.
2852 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2853 __isl_take isl_basic_map *bmap,
2854 enum isl_dim_type type, unsigned first, unsigned n)
2856 int i;
2858 if (n == 0) {
2859 isl_space *space = isl_basic_map_get_space(bmap);
2860 isl_basic_map_free(bmap);
2861 return isl_basic_map_universe(space);
2863 bmap = isl_basic_map_cow(bmap);
2864 if (!bmap)
2865 return NULL;
2867 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2868 return isl_basic_map_free(bmap);
2870 first += isl_basic_map_offset(bmap, type) - 1;
2872 for (i = bmap->n_eq - 1; i >= 0; --i) {
2873 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2874 continue;
2875 isl_basic_map_drop_equality(bmap, i);
2878 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2879 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2880 continue;
2881 isl_basic_map_drop_inequality(bmap, i);
2884 bmap = isl_basic_map_add_known_div_constraints(bmap);
2885 return bmap;
2888 /* Drop all constraints in bset that do not involve any of the dimensions
2889 * first to first + n - 1 of the given type.
2891 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2892 __isl_take isl_basic_set *bset,
2893 enum isl_dim_type type, unsigned first, unsigned n)
2895 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2896 type, first, n);
2899 /* Drop all constraints in bmap that involve any of the dimensions
2900 * first to first + n - 1 of the given type.
2902 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2903 __isl_take isl_basic_map *bmap,
2904 enum isl_dim_type type, unsigned first, unsigned n)
2906 if (!bmap)
2907 return NULL;
2908 if (n == 0)
2909 return bmap;
2911 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2912 return isl_basic_map_free(bmap);
2914 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2915 first += isl_basic_map_offset(bmap, type) - 1;
2916 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2917 bmap = isl_basic_map_add_known_div_constraints(bmap);
2918 return bmap;
2921 /* Drop all constraints in bset that involve any of the dimensions
2922 * first to first + n - 1 of the given type.
2924 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2925 __isl_take isl_basic_set *bset,
2926 enum isl_dim_type type, unsigned first, unsigned n)
2928 return isl_basic_map_drop_constraints_involving_dims(bset,
2929 type, first, n);
2932 /* Drop constraints from "map" by applying "drop" to each basic map.
2934 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2935 enum isl_dim_type type, unsigned first, unsigned n,
2936 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2937 enum isl_dim_type type, unsigned first, unsigned n))
2939 int i;
2941 if (isl_map_check_range(map, type, first, n) < 0)
2942 return isl_map_free(map);
2944 map = isl_map_cow(map);
2945 if (!map)
2946 return NULL;
2948 for (i = 0; i < map->n; ++i) {
2949 map->p[i] = drop(map->p[i], type, first, n);
2950 if (!map->p[i])
2951 return isl_map_free(map);
2954 if (map->n > 1)
2955 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2957 return map;
2960 /* Drop all constraints in map that involve any of the dimensions
2961 * first to first + n - 1 of the given type.
2963 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2964 __isl_take isl_map *map,
2965 enum isl_dim_type type, unsigned first, unsigned n)
2967 if (n == 0)
2968 return map;
2969 return drop_constraints(map, type, first, n,
2970 &isl_basic_map_drop_constraints_involving_dims);
2973 /* Drop all constraints in "map" that do not involve any of the dimensions
2974 * first to first + n - 1 of the given type.
2976 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2977 __isl_take isl_map *map,
2978 enum isl_dim_type type, unsigned first, unsigned n)
2980 if (n == 0) {
2981 isl_space *space = isl_map_get_space(map);
2982 isl_map_free(map);
2983 return isl_map_universe(space);
2985 return drop_constraints(map, type, first, n,
2986 &isl_basic_map_drop_constraints_not_involving_dims);
2989 /* Drop all constraints in set that involve any of the dimensions
2990 * first to first + n - 1 of the given type.
2992 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2993 __isl_take isl_set *set,
2994 enum isl_dim_type type, unsigned first, unsigned n)
2996 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2999 /* Drop all constraints in "set" that do not involve any of the dimensions
3000 * first to first + n - 1 of the given type.
3002 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3003 __isl_take isl_set *set,
3004 enum isl_dim_type type, unsigned first, unsigned n)
3006 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3009 /* Does local variable "div" of "bmap" have a complete explicit representation?
3010 * Having a complete explicit representation requires not only
3011 * an explicit representation, but also that all local variables
3012 * that appear in this explicit representation in turn have
3013 * a complete explicit representation.
3015 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3017 int i;
3018 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3019 isl_bool marked;
3021 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3022 if (marked < 0 || marked)
3023 return isl_bool_not(marked);
3025 for (i = bmap->n_div - 1; i >= 0; --i) {
3026 isl_bool known;
3028 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3029 continue;
3030 known = isl_basic_map_div_is_known(bmap, i);
3031 if (known < 0 || !known)
3032 return known;
3035 return isl_bool_true;
3038 /* Remove all divs that are unknown or defined in terms of unknown divs.
3040 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3041 __isl_take isl_basic_map *bmap)
3043 int i;
3045 if (!bmap)
3046 return NULL;
3048 for (i = bmap->n_div - 1; i >= 0; --i) {
3049 if (isl_basic_map_div_is_known(bmap, i))
3050 continue;
3051 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3052 if (!bmap)
3053 return NULL;
3054 i = bmap->n_div;
3057 return bmap;
3060 /* Remove all divs that are unknown or defined in terms of unknown divs.
3062 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3063 __isl_take isl_basic_set *bset)
3065 return isl_basic_map_remove_unknown_divs(bset);
3068 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3070 int i;
3072 if (!map)
3073 return NULL;
3074 if (map->n == 0)
3075 return map;
3077 map = isl_map_cow(map);
3078 if (!map)
3079 return NULL;
3081 for (i = 0; i < map->n; ++i) {
3082 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3083 if (!map->p[i])
3084 goto error;
3086 return map;
3087 error:
3088 isl_map_free(map);
3089 return NULL;
3092 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3094 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3097 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3098 __isl_take isl_basic_set *bset,
3099 enum isl_dim_type type, unsigned first, unsigned n)
3101 isl_basic_map *bmap = bset_to_bmap(bset);
3102 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3103 return bset_from_bmap(bmap);
3106 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3107 enum isl_dim_type type, unsigned first, unsigned n)
3109 int i;
3111 if (n == 0)
3112 return map;
3114 map = isl_map_cow(map);
3115 if (isl_map_check_range(map, type, first, n) < 0)
3116 return isl_map_free(map);
3118 for (i = 0; i < map->n; ++i) {
3119 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3120 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3121 if (!map->p[i])
3122 goto error;
3124 map = isl_map_drop(map, type, first, n);
3125 return map;
3126 error:
3127 isl_map_free(map);
3128 return NULL;
3131 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3132 enum isl_dim_type type, unsigned first, unsigned n)
3134 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3135 type, first, n));
3138 /* Project out n inputs starting at first using Fourier-Motzkin */
3139 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3140 unsigned first, unsigned n)
3142 return isl_map_remove_dims(map, isl_dim_in, first, n);
3145 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3146 FILE *out, int indent)
3148 isl_printer *p;
3150 if (!bset) {
3151 fprintf(out, "null basic set\n");
3152 return;
3155 fprintf(out, "%*s", indent, "");
3156 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3157 bset->ref, bset->dim->nparam, bset->dim->n_out,
3158 bset->extra, bset->flags);
3160 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3161 p = isl_printer_set_dump(p, 1);
3162 p = isl_printer_set_indent(p, indent);
3163 p = isl_printer_start_line(p);
3164 p = isl_printer_print_basic_set(p, bset);
3165 p = isl_printer_end_line(p);
3166 isl_printer_free(p);
3169 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3170 FILE *out, int indent)
3172 isl_printer *p;
3174 if (!bmap) {
3175 fprintf(out, "null basic map\n");
3176 return;
3179 fprintf(out, "%*s", indent, "");
3180 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3181 "flags: %x, n_name: %d\n",
3182 bmap->ref,
3183 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3184 bmap->extra, bmap->flags, bmap->dim->n_id);
3186 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3187 p = isl_printer_set_dump(p, 1);
3188 p = isl_printer_set_indent(p, indent);
3189 p = isl_printer_start_line(p);
3190 p = isl_printer_print_basic_map(p, bmap);
3191 p = isl_printer_end_line(p);
3192 isl_printer_free(p);
3195 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3196 unsigned pos)
3198 unsigned total;
3200 if (!bmap)
3201 return NULL;
3202 total = isl_basic_map_total_dim(bmap);
3203 if (pos >= bmap->n_ineq)
3204 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3205 "invalid position", return isl_basic_map_free(bmap));
3206 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3207 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3208 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3209 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3210 return bmap;
3213 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3214 unsigned flags)
3216 if (isl_space_check_is_set(space) < 0)
3217 goto error;
3218 return isl_map_alloc_space(space, n, flags);
3219 error:
3220 isl_space_free(space);
3221 return NULL;
3224 /* Make sure "map" has room for at least "n" more basic maps.
3226 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3228 int i;
3229 struct isl_map *grown = NULL;
3231 if (!map)
3232 return NULL;
3233 isl_assert(map->ctx, n >= 0, goto error);
3234 if (map->n + n <= map->size)
3235 return map;
3236 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3237 if (!grown)
3238 goto error;
3239 for (i = 0; i < map->n; ++i) {
3240 grown->p[i] = isl_basic_map_copy(map->p[i]);
3241 if (!grown->p[i])
3242 goto error;
3243 grown->n++;
3245 isl_map_free(map);
3246 return grown;
3247 error:
3248 isl_map_free(grown);
3249 isl_map_free(map);
3250 return NULL;
3253 /* Make sure "set" has room for at least "n" more basic sets.
3255 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3257 return set_from_map(isl_map_grow(set_to_map(set), n));
3260 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3262 return isl_map_from_basic_map(bset);
3265 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3267 struct isl_map *map;
3269 if (!bmap)
3270 return NULL;
3272 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3273 return isl_map_add_basic_map(map, bmap);
3276 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3277 __isl_take isl_basic_set *bset)
3279 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3280 bset_to_bmap(bset)));
3283 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3285 return isl_map_free(set);
3288 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3290 int i;
3292 if (!set) {
3293 fprintf(out, "null set\n");
3294 return;
3297 fprintf(out, "%*s", indent, "");
3298 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3299 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3300 set->flags);
3301 for (i = 0; i < set->n; ++i) {
3302 fprintf(out, "%*s", indent, "");
3303 fprintf(out, "basic set %d:\n", i);
3304 isl_basic_set_print_internal(set->p[i], out, indent+4);
3308 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3310 int i;
3312 if (!map) {
3313 fprintf(out, "null map\n");
3314 return;
3317 fprintf(out, "%*s", indent, "");
3318 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3319 "flags: %x, n_name: %d\n",
3320 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3321 map->dim->n_out, map->flags, map->dim->n_id);
3322 for (i = 0; i < map->n; ++i) {
3323 fprintf(out, "%*s", indent, "");
3324 fprintf(out, "basic map %d:\n", i);
3325 isl_basic_map_print_internal(map->p[i], out, indent+4);
3329 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3330 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3332 struct isl_basic_map *bmap_domain;
3334 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3335 goto error;
3337 if (isl_basic_set_dim(bset, isl_dim_set) != 0)
3338 isl_assert(bset->ctx,
3339 isl_basic_map_compatible_domain(bmap, bset), goto error);
3341 bmap = isl_basic_map_cow(bmap);
3342 if (!bmap)
3343 goto error;
3344 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3345 bset->n_div, bset->n_eq, bset->n_ineq);
3346 bmap_domain = isl_basic_map_from_domain(bset);
3347 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3349 bmap = isl_basic_map_simplify(bmap);
3350 return isl_basic_map_finalize(bmap);
3351 error:
3352 isl_basic_map_free(bmap);
3353 isl_basic_set_free(bset);
3354 return NULL;
3357 /* Check that the space of "bset" is the same as that of the range of "bmap".
3359 static isl_stat isl_basic_map_check_compatible_range(
3360 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3362 isl_bool ok;
3364 ok = isl_basic_map_compatible_range(bmap, bset);
3365 if (ok < 0)
3366 return isl_stat_error;
3367 if (!ok)
3368 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3369 "incompatible spaces", return isl_stat_error);
3371 return isl_stat_ok;
3374 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3375 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3377 struct isl_basic_map *bmap_range;
3379 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3380 goto error;
3382 if (isl_basic_set_dim(bset, isl_dim_set) != 0 &&
3383 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3384 goto error;
3386 if (isl_basic_set_plain_is_universe(bset)) {
3387 isl_basic_set_free(bset);
3388 return bmap;
3391 bmap = isl_basic_map_cow(bmap);
3392 if (!bmap)
3393 goto error;
3394 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3395 bset->n_div, bset->n_eq, bset->n_ineq);
3396 bmap_range = bset_to_bmap(bset);
3397 bmap = add_constraints(bmap, bmap_range, 0, 0);
3399 bmap = isl_basic_map_simplify(bmap);
3400 return isl_basic_map_finalize(bmap);
3401 error:
3402 isl_basic_map_free(bmap);
3403 isl_basic_set_free(bset);
3404 return NULL;
3407 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3408 __isl_keep isl_vec *vec)
3410 int i;
3411 unsigned total;
3412 isl_int s;
3414 if (!bmap || !vec)
3415 return isl_bool_error;
3417 total = 1 + isl_basic_map_total_dim(bmap);
3418 if (total != vec->size)
3419 return isl_bool_false;
3421 isl_int_init(s);
3423 for (i = 0; i < bmap->n_eq; ++i) {
3424 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3425 if (!isl_int_is_zero(s)) {
3426 isl_int_clear(s);
3427 return isl_bool_false;
3431 for (i = 0; i < bmap->n_ineq; ++i) {
3432 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3433 if (isl_int_is_neg(s)) {
3434 isl_int_clear(s);
3435 return isl_bool_false;
3439 isl_int_clear(s);
3441 return isl_bool_true;
3444 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3445 __isl_keep isl_vec *vec)
3447 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3450 __isl_give isl_basic_map *isl_basic_map_intersect(
3451 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3453 struct isl_vec *sample = NULL;
3454 isl_space *space1, *space2;
3456 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3457 goto error;
3458 space1 = isl_basic_map_peek_space(bmap1);
3459 space2 = isl_basic_map_peek_space(bmap2);
3460 if (isl_space_dim(space1, isl_dim_all) ==
3461 isl_space_dim(space1, isl_dim_param) &&
3462 isl_space_dim(space2, isl_dim_all) !=
3463 isl_space_dim(space2, isl_dim_param))
3464 return isl_basic_map_intersect(bmap2, bmap1);
3466 if (isl_space_dim(space2, isl_dim_all) !=
3467 isl_space_dim(space2, isl_dim_param))
3468 isl_assert(bmap1->ctx,
3469 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3471 if (isl_basic_map_plain_is_empty(bmap1)) {
3472 isl_basic_map_free(bmap2);
3473 return bmap1;
3475 if (isl_basic_map_plain_is_empty(bmap2)) {
3476 isl_basic_map_free(bmap1);
3477 return bmap2;
3480 if (bmap1->sample &&
3481 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3482 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3483 sample = isl_vec_copy(bmap1->sample);
3484 else if (bmap2->sample &&
3485 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3486 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3487 sample = isl_vec_copy(bmap2->sample);
3489 bmap1 = isl_basic_map_cow(bmap1);
3490 if (!bmap1)
3491 goto error;
3492 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3493 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3494 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3496 if (!bmap1)
3497 isl_vec_free(sample);
3498 else if (sample) {
3499 isl_vec_free(bmap1->sample);
3500 bmap1->sample = sample;
3503 bmap1 = isl_basic_map_simplify(bmap1);
3504 return isl_basic_map_finalize(bmap1);
3505 error:
3506 if (sample)
3507 isl_vec_free(sample);
3508 isl_basic_map_free(bmap1);
3509 isl_basic_map_free(bmap2);
3510 return NULL;
3513 struct isl_basic_set *isl_basic_set_intersect(
3514 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3516 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3517 bset_to_bmap(bset2)));
3520 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3521 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3523 return isl_basic_set_intersect(bset1, bset2);
3526 /* Special case of isl_map_intersect, where both map1 and map2
3527 * are convex, without any divs and such that either map1 or map2
3528 * contains a single constraint. This constraint is then simply
3529 * added to the other map.
3531 static __isl_give isl_map *map_intersect_add_constraint(
3532 __isl_take isl_map *map1, __isl_take isl_map *map2)
3534 isl_assert(map1->ctx, map1->n == 1, goto error);
3535 isl_assert(map2->ctx, map1->n == 1, goto error);
3536 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3537 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3539 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3540 return isl_map_intersect(map2, map1);
3542 map1 = isl_map_cow(map1);
3543 if (!map1)
3544 goto error;
3545 if (isl_map_plain_is_empty(map1)) {
3546 isl_map_free(map2);
3547 return map1;
3549 if (map2->p[0]->n_eq == 1)
3550 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3551 else
3552 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3553 map2->p[0]->ineq[0]);
3555 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3556 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3557 if (!map1->p[0])
3558 goto error;
3560 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3561 isl_basic_map_free(map1->p[0]);
3562 map1->n = 0;
3565 isl_map_free(map2);
3567 map1 = isl_map_unmark_normalized(map1);
3568 return map1;
3569 error:
3570 isl_map_free(map1);
3571 isl_map_free(map2);
3572 return NULL;
3575 /* map2 may be either a parameter domain or a map living in the same
3576 * space as map1.
3578 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3579 __isl_take isl_map *map2)
3581 unsigned flags = 0;
3582 isl_bool equal;
3583 isl_map *result;
3584 int i, j;
3586 if (!map1 || !map2)
3587 goto error;
3589 if ((isl_map_plain_is_empty(map1) ||
3590 isl_map_plain_is_universe(map2)) &&
3591 isl_space_is_equal(map1->dim, map2->dim)) {
3592 isl_map_free(map2);
3593 return map1;
3595 if ((isl_map_plain_is_empty(map2) ||
3596 isl_map_plain_is_universe(map1)) &&
3597 isl_space_is_equal(map1->dim, map2->dim)) {
3598 isl_map_free(map1);
3599 return map2;
3602 if (map1->n == 1 && map2->n == 1 &&
3603 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3604 isl_space_is_equal(map1->dim, map2->dim) &&
3605 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3606 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3607 return map_intersect_add_constraint(map1, map2);
3609 equal = isl_map_plain_is_equal(map1, map2);
3610 if (equal < 0)
3611 goto error;
3612 if (equal) {
3613 isl_map_free(map2);
3614 return map1;
3617 if (isl_map_dim(map2, isl_dim_all) != isl_map_dim(map2, isl_dim_param))
3618 isl_assert(map1->ctx,
3619 isl_space_is_equal(map1->dim, map2->dim), goto error);
3621 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3622 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3623 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3625 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3626 map1->n * map2->n, flags);
3627 if (!result)
3628 goto error;
3629 for (i = 0; i < map1->n; ++i)
3630 for (j = 0; j < map2->n; ++j) {
3631 struct isl_basic_map *part;
3632 part = isl_basic_map_intersect(
3633 isl_basic_map_copy(map1->p[i]),
3634 isl_basic_map_copy(map2->p[j]));
3635 if (isl_basic_map_is_empty(part) < 0)
3636 part = isl_basic_map_free(part);
3637 result = isl_map_add_basic_map(result, part);
3638 if (!result)
3639 goto error;
3641 isl_map_free(map1);
3642 isl_map_free(map2);
3643 return result;
3644 error:
3645 isl_map_free(map1);
3646 isl_map_free(map2);
3647 return NULL;
3650 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3651 __isl_take isl_map *map2)
3653 if (!map1 || !map2)
3654 goto error;
3655 if (!isl_space_is_equal(map1->dim, map2->dim))
3656 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3657 "spaces don't match", goto error);
3658 return map_intersect_internal(map1, map2);
3659 error:
3660 isl_map_free(map1);
3661 isl_map_free(map2);
3662 return NULL;
3665 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3666 __isl_take isl_map *map2)
3668 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3671 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3673 return set_from_map(isl_map_intersect(set_to_map(set1),
3674 set_to_map(set2)));
3677 /* map_intersect_internal accepts intersections
3678 * with parameter domains, so we can just call that function.
3680 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3681 __isl_take isl_set *params)
3683 return map_intersect_internal(map, params);
3686 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3687 __isl_take isl_map *map2)
3689 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3692 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3693 __isl_take isl_set *params)
3695 return isl_map_intersect_params(set, params);
3698 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3700 isl_space *space;
3701 unsigned pos, n1, n2;
3703 if (!bmap)
3704 return NULL;
3705 bmap = isl_basic_map_cow(bmap);
3706 if (!bmap)
3707 return NULL;
3708 space = isl_space_reverse(isl_space_copy(bmap->dim));
3709 pos = isl_basic_map_offset(bmap, isl_dim_in);
3710 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3711 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3712 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3713 return isl_basic_map_reset_space(bmap, space);
3716 static __isl_give isl_basic_map *basic_map_space_reset(
3717 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3719 isl_space *space;
3721 if (!bmap)
3722 return NULL;
3723 if (!isl_space_is_named_or_nested(bmap->dim, type))
3724 return bmap;
3726 space = isl_basic_map_get_space(bmap);
3727 space = isl_space_reset(space, type);
3728 bmap = isl_basic_map_reset_space(bmap, space);
3729 return bmap;
3732 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3733 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3734 unsigned pos, unsigned n)
3736 isl_bool rational, is_empty;
3737 isl_space *res_space;
3738 struct isl_basic_map *res;
3739 struct isl_dim_map *dim_map;
3740 unsigned total, off;
3741 enum isl_dim_type t;
3743 if (n == 0)
3744 return basic_map_space_reset(bmap, type);
3746 is_empty = isl_basic_map_plain_is_empty(bmap);
3747 if (is_empty < 0)
3748 return isl_basic_map_free(bmap);
3749 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3750 type, pos, n);
3751 if (!res_space)
3752 return isl_basic_map_free(bmap);
3753 if (is_empty) {
3754 isl_basic_map_free(bmap);
3755 return isl_basic_map_empty(res_space);
3758 total = isl_basic_map_total_dim(bmap) + n;
3759 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3760 off = 0;
3761 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3762 if (t != type) {
3763 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3764 } else {
3765 unsigned size = isl_basic_map_dim(bmap, t);
3766 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3767 0, pos, off);
3768 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3769 pos, size - pos, off + pos + n);
3771 off += isl_space_dim(res_space, t);
3773 isl_dim_map_div(dim_map, bmap, off);
3775 res = isl_basic_map_alloc_space(res_space,
3776 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3777 rational = isl_basic_map_is_rational(bmap);
3778 if (rational < 0)
3779 res = isl_basic_map_free(res);
3780 if (rational)
3781 res = isl_basic_map_set_rational(res);
3782 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3783 return isl_basic_map_finalize(res);
3786 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3787 __isl_take isl_basic_set *bset,
3788 enum isl_dim_type type, unsigned pos, unsigned n)
3790 return isl_basic_map_insert_dims(bset, type, pos, n);
3793 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3794 enum isl_dim_type type, unsigned n)
3796 if (!bmap)
3797 return NULL;
3798 return isl_basic_map_insert_dims(bmap, type,
3799 isl_basic_map_dim(bmap, type), n);
3802 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3803 enum isl_dim_type type, unsigned n)
3805 if (!bset)
3806 return NULL;
3807 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3808 return isl_basic_map_add_dims(bset, type, n);
3809 error:
3810 isl_basic_set_free(bset);
3811 return NULL;
3814 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3815 enum isl_dim_type type)
3817 isl_space *space;
3819 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3820 return map;
3822 space = isl_map_get_space(map);
3823 space = isl_space_reset(space, type);
3824 map = isl_map_reset_space(map, space);
3825 return map;
3828 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3829 enum isl_dim_type type, unsigned pos, unsigned n)
3831 int i;
3833 if (n == 0)
3834 return map_space_reset(map, type);
3836 map = isl_map_cow(map);
3837 if (!map)
3838 return NULL;
3840 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3841 if (!map->dim)
3842 goto error;
3844 for (i = 0; i < map->n; ++i) {
3845 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3846 if (!map->p[i])
3847 goto error;
3850 return map;
3851 error:
3852 isl_map_free(map);
3853 return NULL;
3856 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3857 enum isl_dim_type type, unsigned pos, unsigned n)
3859 return isl_map_insert_dims(set, type, pos, n);
3862 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3863 enum isl_dim_type type, unsigned n)
3865 if (!map)
3866 return NULL;
3867 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3870 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3871 enum isl_dim_type type, unsigned n)
3873 if (!set)
3874 return NULL;
3875 isl_assert(set->ctx, type != isl_dim_in, goto error);
3876 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3877 error:
3878 isl_set_free(set);
3879 return NULL;
3882 __isl_give isl_basic_map *isl_basic_map_move_dims(
3883 __isl_take isl_basic_map *bmap,
3884 enum isl_dim_type dst_type, unsigned dst_pos,
3885 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3887 isl_space *space;
3888 struct isl_dim_map *dim_map;
3889 struct isl_basic_map *res;
3890 enum isl_dim_type t;
3891 unsigned total, off;
3893 if (!bmap)
3894 return NULL;
3895 if (n == 0) {
3896 bmap = isl_basic_map_reset(bmap, src_type);
3897 bmap = isl_basic_map_reset(bmap, dst_type);
3898 return bmap;
3901 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3902 return isl_basic_map_free(bmap);
3904 if (dst_type == src_type && dst_pos == src_pos)
3905 return bmap;
3907 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3909 if (pos(bmap->dim, dst_type) + dst_pos ==
3910 pos(bmap->dim, src_type) + src_pos +
3911 ((src_type < dst_type) ? n : 0)) {
3912 bmap = isl_basic_map_cow(bmap);
3913 if (!bmap)
3914 return NULL;
3916 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3917 src_type, src_pos, n);
3918 if (!bmap->dim)
3919 goto error;
3921 bmap = isl_basic_map_finalize(bmap);
3923 return bmap;
3926 total = isl_basic_map_total_dim(bmap);
3927 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3929 off = 0;
3930 space = isl_basic_map_peek_space(bmap);
3931 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3932 unsigned size = isl_space_dim(space, t);
3933 if (t == dst_type) {
3934 isl_dim_map_dim_range(dim_map, space, t,
3935 0, dst_pos, off);
3936 off += dst_pos;
3937 isl_dim_map_dim_range(dim_map, space, src_type,
3938 src_pos, n, off);
3939 off += n;
3940 isl_dim_map_dim_range(dim_map, space, t,
3941 dst_pos, size - dst_pos, off);
3942 off += size - dst_pos;
3943 } else if (t == src_type) {
3944 isl_dim_map_dim_range(dim_map, space, t,
3945 0, src_pos, off);
3946 off += src_pos;
3947 isl_dim_map_dim_range(dim_map, space, t,
3948 src_pos + n, size - src_pos - n, off);
3949 off += size - src_pos - n;
3950 } else {
3951 isl_dim_map_dim(dim_map, space, t, off);
3952 off += size;
3955 isl_dim_map_div(dim_map, bmap, off);
3957 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3958 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3959 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3960 if (!bmap)
3961 goto error;
3963 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3964 src_type, src_pos, n);
3965 if (!bmap->dim)
3966 goto error;
3968 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3969 bmap = isl_basic_map_gauss(bmap, NULL);
3970 bmap = isl_basic_map_finalize(bmap);
3972 return bmap;
3973 error:
3974 isl_basic_map_free(bmap);
3975 return NULL;
3978 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3979 enum isl_dim_type dst_type, unsigned dst_pos,
3980 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3982 isl_basic_map *bmap = bset_to_bmap(bset);
3983 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3984 src_type, src_pos, n);
3985 return bset_from_bmap(bmap);
3988 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3989 enum isl_dim_type dst_type, unsigned dst_pos,
3990 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3992 if (!set)
3993 return NULL;
3994 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3995 return set_from_map(isl_map_move_dims(set_to_map(set),
3996 dst_type, dst_pos, src_type, src_pos, n));
3997 error:
3998 isl_set_free(set);
3999 return NULL;
4002 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4003 enum isl_dim_type dst_type, unsigned dst_pos,
4004 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4006 int i;
4008 if (n == 0) {
4009 map = isl_map_reset(map, src_type);
4010 map = isl_map_reset(map, dst_type);
4011 return map;
4014 if (isl_map_check_range(map, src_type, src_pos, n))
4015 return isl_map_free(map);
4017 if (dst_type == src_type && dst_pos == src_pos)
4018 return map;
4020 isl_assert(map->ctx, dst_type != src_type, goto error);
4022 map = isl_map_cow(map);
4023 if (!map)
4024 return NULL;
4026 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4027 if (!map->dim)
4028 goto error;
4030 for (i = 0; i < map->n; ++i) {
4031 map->p[i] = isl_basic_map_move_dims(map->p[i],
4032 dst_type, dst_pos,
4033 src_type, src_pos, n);
4034 if (!map->p[i])
4035 goto error;
4038 return map;
4039 error:
4040 isl_map_free(map);
4041 return NULL;
4044 /* Move the specified dimensions to the last columns right before
4045 * the divs. Don't change the dimension specification of bmap.
4046 * That's the responsibility of the caller.
4048 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4049 enum isl_dim_type type, unsigned first, unsigned n)
4051 isl_space *space;
4052 struct isl_dim_map *dim_map;
4053 struct isl_basic_map *res;
4054 enum isl_dim_type t;
4055 unsigned total, off;
4057 if (!bmap)
4058 return NULL;
4059 if (isl_basic_map_offset(bmap, type) + first + n ==
4060 isl_basic_map_offset(bmap, isl_dim_div))
4061 return bmap;
4063 total = isl_basic_map_total_dim(bmap);
4064 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4066 off = 0;
4067 space = isl_basic_map_peek_space(bmap);
4068 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4069 unsigned size = isl_space_dim(space, t);
4070 if (t == type) {
4071 isl_dim_map_dim_range(dim_map, space, t,
4072 0, first, off);
4073 off += first;
4074 isl_dim_map_dim_range(dim_map, space, t,
4075 first, n, total - bmap->n_div - n);
4076 isl_dim_map_dim_range(dim_map, space, t,
4077 first + n, size - (first + n), off);
4078 off += size - (first + n);
4079 } else {
4080 isl_dim_map_dim(dim_map, space, t, off);
4081 off += size;
4084 isl_dim_map_div(dim_map, bmap, off + n);
4086 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4087 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4088 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4089 return res;
4092 /* Insert "n" rows in the divs of "bmap".
4094 * The number of columns is not changed, which means that the last
4095 * dimensions of "bmap" are being reintepreted as the new divs.
4096 * The space of "bmap" is not adjusted, however, which means
4097 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4098 * from the space of "bmap" is the responsibility of the caller.
4100 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4101 int n)
4103 int i;
4104 size_t row_size;
4105 isl_int **new_div;
4106 isl_int *old;
4108 bmap = isl_basic_map_cow(bmap);
4109 if (!bmap)
4110 return NULL;
4112 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4113 old = bmap->block2.data;
4114 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4115 (bmap->extra + n) * (1 + row_size));
4116 if (!bmap->block2.data)
4117 return isl_basic_map_free(bmap);
4118 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4119 if (!new_div)
4120 return isl_basic_map_free(bmap);
4121 for (i = 0; i < n; ++i) {
4122 new_div[i] = bmap->block2.data +
4123 (bmap->extra + i) * (1 + row_size);
4124 isl_seq_clr(new_div[i], 1 + row_size);
4126 for (i = 0; i < bmap->extra; ++i)
4127 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4128 free(bmap->div);
4129 bmap->div = new_div;
4130 bmap->n_div += n;
4131 bmap->extra += n;
4133 return bmap;
4136 /* Drop constraints from "bmap" that only involve the variables
4137 * of "type" in the range [first, first + n] that are not related
4138 * to any of the variables outside that interval.
4139 * These constraints cannot influence the values for the variables
4140 * outside the interval, except in case they cause "bmap" to be empty.
4141 * Only drop the constraints if "bmap" is known to be non-empty.
4143 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4144 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4145 unsigned first, unsigned n)
4147 int i;
4148 int *groups;
4149 unsigned dim, n_div;
4150 isl_bool non_empty;
4152 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4153 if (non_empty < 0)
4154 return isl_basic_map_free(bmap);
4155 if (!non_empty)
4156 return bmap;
4158 dim = isl_basic_map_dim(bmap, isl_dim_all);
4159 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4160 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4161 if (!groups)
4162 return isl_basic_map_free(bmap);
4163 first += isl_basic_map_offset(bmap, type) - 1;
4164 for (i = 0; i < first; ++i)
4165 groups[i] = -1;
4166 for (i = first + n; i < dim - n_div; ++i)
4167 groups[i] = -1;
4169 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4171 return bmap;
4174 /* Turn the n dimensions of type type, starting at first
4175 * into existentially quantified variables.
4177 * If a subset of the projected out variables are unrelated
4178 * to any of the variables that remain, then the constraints
4179 * involving this subset are simply dropped first.
4181 __isl_give isl_basic_map *isl_basic_map_project_out(
4182 __isl_take isl_basic_map *bmap,
4183 enum isl_dim_type type, unsigned first, unsigned n)
4185 isl_bool empty;
4187 if (n == 0)
4188 return basic_map_space_reset(bmap, type);
4189 if (type == isl_dim_div)
4190 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4191 "cannot project out existentially quantified variables",
4192 return isl_basic_map_free(bmap));
4194 empty = isl_basic_map_plain_is_empty(bmap);
4195 if (empty < 0)
4196 return isl_basic_map_free(bmap);
4197 if (empty)
4198 bmap = isl_basic_map_set_to_empty(bmap);
4200 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4201 if (!bmap)
4202 return NULL;
4204 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4205 return isl_basic_map_remove_dims(bmap, type, first, n);
4207 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4208 return isl_basic_map_free(bmap);
4210 bmap = move_last(bmap, type, first, n);
4211 bmap = isl_basic_map_cow(bmap);
4212 bmap = insert_div_rows(bmap, n);
4213 if (!bmap)
4214 return NULL;
4216 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4217 if (!bmap->dim)
4218 goto error;
4219 bmap = isl_basic_map_simplify(bmap);
4220 bmap = isl_basic_map_drop_redundant_divs(bmap);
4221 return isl_basic_map_finalize(bmap);
4222 error:
4223 isl_basic_map_free(bmap);
4224 return NULL;
4227 /* Turn the n dimensions of type type, starting at first
4228 * into existentially quantified variables.
4230 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4231 enum isl_dim_type type, unsigned first, unsigned n)
4233 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4234 type, first, n));
4237 /* Turn the n dimensions of type type, starting at first
4238 * into existentially quantified variables.
4240 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4241 enum isl_dim_type type, unsigned first, unsigned n)
4243 int i;
4245 if (n == 0)
4246 return map_space_reset(map, type);
4248 if (isl_map_check_range(map, type, first, n) < 0)
4249 return isl_map_free(map);
4251 map = isl_map_cow(map);
4252 if (!map)
4253 return NULL;
4255 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4256 if (!map->dim)
4257 goto error;
4259 for (i = 0; i < map->n; ++i) {
4260 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4261 if (!map->p[i])
4262 goto error;
4265 return map;
4266 error:
4267 isl_map_free(map);
4268 return NULL;
4271 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4272 * into existentially quantified variables.
4274 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4275 enum isl_dim_type type, unsigned first, unsigned n)
4277 unsigned dim;
4279 if (isl_map_check_range(map, type, first, n) < 0)
4280 return isl_map_free(map);
4281 dim = isl_map_dim(map, type);
4282 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4283 map = isl_map_project_out(map, type, 0, first);
4284 return map;
4287 /* Turn the n dimensions of type type, starting at first
4288 * into existentially quantified variables.
4290 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4291 enum isl_dim_type type, unsigned first, unsigned n)
4293 return set_from_map(isl_map_project_out(set_to_map(set),
4294 type, first, n));
4297 /* Return a map that projects the elements in "set" onto their
4298 * "n" set dimensions starting at "first".
4299 * "type" should be equal to isl_dim_set.
4301 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4302 enum isl_dim_type type, unsigned first, unsigned n)
4304 int i;
4305 isl_map *map;
4307 if (type != isl_dim_set)
4308 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4309 "only set dimensions can be projected out", goto error);
4310 if (isl_set_check_range(set, type, first, n) < 0)
4311 return isl_set_free(set);
4313 map = isl_map_from_domain(set);
4314 map = isl_map_add_dims(map, isl_dim_out, n);
4315 for (i = 0; i < n; ++i)
4316 map = isl_map_equate(map, isl_dim_in, first + i,
4317 isl_dim_out, i);
4318 return map;
4319 error:
4320 isl_set_free(set);
4321 return NULL;
4324 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4325 unsigned n)
4327 int i, j;
4329 for (i = 0; i < n; ++i) {
4330 j = isl_basic_map_alloc_div(bmap);
4331 if (j < 0)
4332 goto error;
4333 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4335 return bmap;
4336 error:
4337 isl_basic_map_free(bmap);
4338 return NULL;
4341 struct isl_basic_map *isl_basic_map_apply_range(
4342 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4344 isl_space *space_result = NULL;
4345 struct isl_basic_map *bmap;
4346 unsigned n_in, n_out, n, nparam, total, pos;
4347 struct isl_dim_map *dim_map1, *dim_map2;
4349 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4350 goto error;
4351 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4352 bmap2->dim, isl_dim_in))
4353 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4354 "spaces don't match", goto error);
4356 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4357 isl_basic_map_get_space(bmap2));
4359 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4360 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4361 n = isl_basic_map_dim(bmap1, isl_dim_out);
4362 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4364 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4365 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4366 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4367 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4368 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4369 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4370 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4371 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4372 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4373 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4374 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4376 bmap = isl_basic_map_alloc_space(space_result,
4377 bmap1->n_div + bmap2->n_div + n,
4378 bmap1->n_eq + bmap2->n_eq,
4379 bmap1->n_ineq + bmap2->n_ineq);
4380 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4381 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4382 bmap = add_divs(bmap, n);
4383 bmap = isl_basic_map_simplify(bmap);
4384 bmap = isl_basic_map_drop_redundant_divs(bmap);
4385 return isl_basic_map_finalize(bmap);
4386 error:
4387 isl_basic_map_free(bmap1);
4388 isl_basic_map_free(bmap2);
4389 return NULL;
4392 struct isl_basic_set *isl_basic_set_apply(
4393 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4395 if (!bset || !bmap)
4396 goto error;
4398 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4399 goto error);
4401 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4402 bmap));
4403 error:
4404 isl_basic_set_free(bset);
4405 isl_basic_map_free(bmap);
4406 return NULL;
4409 struct isl_basic_map *isl_basic_map_apply_domain(
4410 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4412 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4413 goto error;
4414 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4415 bmap2->dim, isl_dim_in))
4416 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4417 "spaces don't match", goto error);
4419 bmap1 = isl_basic_map_reverse(bmap1);
4420 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4421 return isl_basic_map_reverse(bmap1);
4422 error:
4423 isl_basic_map_free(bmap1);
4424 isl_basic_map_free(bmap2);
4425 return NULL;
4428 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4429 * A \cap B -> f(A) + f(B)
4431 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4432 __isl_take isl_basic_map *bmap2)
4434 unsigned n_in, n_out, nparam, total, pos;
4435 struct isl_basic_map *bmap = NULL;
4436 struct isl_dim_map *dim_map1, *dim_map2;
4437 int i;
4439 if (!bmap1 || !bmap2)
4440 goto error;
4442 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4443 goto error);
4445 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4446 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4447 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4449 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4450 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4451 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4452 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4453 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4454 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4455 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4456 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4457 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4458 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4459 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4461 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4462 bmap1->n_div + bmap2->n_div + 2 * n_out,
4463 bmap1->n_eq + bmap2->n_eq + n_out,
4464 bmap1->n_ineq + bmap2->n_ineq);
4465 for (i = 0; i < n_out; ++i) {
4466 int j = isl_basic_map_alloc_equality(bmap);
4467 if (j < 0)
4468 goto error;
4469 isl_seq_clr(bmap->eq[j], 1+total);
4470 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4471 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4472 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4474 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4475 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4476 bmap = add_divs(bmap, 2 * n_out);
4478 bmap = isl_basic_map_simplify(bmap);
4479 return isl_basic_map_finalize(bmap);
4480 error:
4481 isl_basic_map_free(bmap);
4482 isl_basic_map_free(bmap1);
4483 isl_basic_map_free(bmap2);
4484 return NULL;
4487 /* Given two maps A -> f(A) and B -> g(B), construct a map
4488 * A \cap B -> f(A) + f(B)
4490 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4491 __isl_take isl_map *map2)
4493 struct isl_map *result;
4494 int i, j;
4496 if (!map1 || !map2)
4497 goto error;
4499 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4501 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4502 map1->n * map2->n, 0);
4503 if (!result)
4504 goto error;
4505 for (i = 0; i < map1->n; ++i)
4506 for (j = 0; j < map2->n; ++j) {
4507 struct isl_basic_map *part;
4508 part = isl_basic_map_sum(
4509 isl_basic_map_copy(map1->p[i]),
4510 isl_basic_map_copy(map2->p[j]));
4511 if (isl_basic_map_is_empty(part))
4512 isl_basic_map_free(part);
4513 else
4514 result = isl_map_add_basic_map(result, part);
4515 if (!result)
4516 goto error;
4518 isl_map_free(map1);
4519 isl_map_free(map2);
4520 return result;
4521 error:
4522 isl_map_free(map1);
4523 isl_map_free(map2);
4524 return NULL;
4527 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4528 __isl_take isl_set *set2)
4530 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4533 /* Given a basic map A -> f(A), construct A -> -f(A).
4535 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4537 int i, j;
4538 unsigned off, n;
4540 bmap = isl_basic_map_cow(bmap);
4541 if (!bmap)
4542 return NULL;
4544 n = isl_basic_map_dim(bmap, isl_dim_out);
4545 off = isl_basic_map_offset(bmap, isl_dim_out);
4546 for (i = 0; i < bmap->n_eq; ++i)
4547 for (j = 0; j < n; ++j)
4548 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4549 for (i = 0; i < bmap->n_ineq; ++i)
4550 for (j = 0; j < n; ++j)
4551 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4552 for (i = 0; i < bmap->n_div; ++i)
4553 for (j = 0; j < n; ++j)
4554 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4555 bmap = isl_basic_map_gauss(bmap, NULL);
4556 return isl_basic_map_finalize(bmap);
4559 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4561 return isl_basic_map_neg(bset);
4564 /* Given a map A -> f(A), construct A -> -f(A).
4566 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4568 int i;
4570 map = isl_map_cow(map);
4571 if (!map)
4572 return NULL;
4574 for (i = 0; i < map->n; ++i) {
4575 map->p[i] = isl_basic_map_neg(map->p[i]);
4576 if (!map->p[i])
4577 goto error;
4580 return map;
4581 error:
4582 isl_map_free(map);
4583 return NULL;
4586 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4588 return set_from_map(isl_map_neg(set_to_map(set)));
4591 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4592 * A -> floor(f(A)/d).
4594 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4595 isl_int d)
4597 unsigned n_in, n_out, nparam, total, pos;
4598 struct isl_basic_map *result = NULL;
4599 struct isl_dim_map *dim_map;
4600 int i;
4602 if (!bmap)
4603 return NULL;
4605 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4606 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4607 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4609 total = nparam + n_in + n_out + bmap->n_div + n_out;
4610 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4611 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4612 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4613 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4614 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4616 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4617 bmap->n_div + n_out,
4618 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4619 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4620 result = add_divs(result, n_out);
4621 for (i = 0; i < n_out; ++i) {
4622 int j;
4623 j = isl_basic_map_alloc_inequality(result);
4624 if (j < 0)
4625 goto error;
4626 isl_seq_clr(result->ineq[j], 1+total);
4627 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4628 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4629 j = isl_basic_map_alloc_inequality(result);
4630 if (j < 0)
4631 goto error;
4632 isl_seq_clr(result->ineq[j], 1+total);
4633 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4634 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4635 isl_int_sub_ui(result->ineq[j][0], d, 1);
4638 result = isl_basic_map_simplify(result);
4639 return isl_basic_map_finalize(result);
4640 error:
4641 isl_basic_map_free(result);
4642 return NULL;
4645 /* Given a map A -> f(A) and an integer d, construct a map
4646 * A -> floor(f(A)/d).
4648 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4650 int i;
4652 map = isl_map_cow(map);
4653 if (!map)
4654 return NULL;
4656 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4657 for (i = 0; i < map->n; ++i) {
4658 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4659 if (!map->p[i])
4660 goto error;
4662 map = isl_map_unmark_normalized(map);
4664 return map;
4665 error:
4666 isl_map_free(map);
4667 return NULL;
4670 /* Given a map A -> f(A) and an integer d, construct a map
4671 * A -> floor(f(A)/d).
4673 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4674 __isl_take isl_val *d)
4676 if (!map || !d)
4677 goto error;
4678 if (!isl_val_is_int(d))
4679 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4680 "expecting integer denominator", goto error);
4681 map = isl_map_floordiv(map, d->n);
4682 isl_val_free(d);
4683 return map;
4684 error:
4685 isl_map_free(map);
4686 isl_val_free(d);
4687 return NULL;
4690 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4691 unsigned pos)
4693 int i;
4694 unsigned nparam;
4695 unsigned n_in;
4697 i = isl_basic_map_alloc_equality(bmap);
4698 if (i < 0)
4699 goto error;
4700 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4701 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4702 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4703 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4704 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4705 return isl_basic_map_finalize(bmap);
4706 error:
4707 isl_basic_map_free(bmap);
4708 return NULL;
4711 /* Add a constraint to "bmap" expressing i_pos < o_pos
4713 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4714 unsigned pos)
4716 int i;
4717 unsigned nparam;
4718 unsigned n_in;
4720 i = isl_basic_map_alloc_inequality(bmap);
4721 if (i < 0)
4722 goto error;
4723 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4724 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4725 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4726 isl_int_set_si(bmap->ineq[i][0], -1);
4727 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4728 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4729 return isl_basic_map_finalize(bmap);
4730 error:
4731 isl_basic_map_free(bmap);
4732 return NULL;
4735 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4737 static __isl_give isl_basic_map *var_less_or_equal(
4738 __isl_take isl_basic_map *bmap, unsigned pos)
4740 int i;
4741 unsigned nparam;
4742 unsigned n_in;
4744 i = isl_basic_map_alloc_inequality(bmap);
4745 if (i < 0)
4746 goto error;
4747 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4748 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4749 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4750 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4751 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4752 return isl_basic_map_finalize(bmap);
4753 error:
4754 isl_basic_map_free(bmap);
4755 return NULL;
4758 /* Add a constraint to "bmap" expressing i_pos > o_pos
4760 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4761 unsigned pos)
4763 int i;
4764 unsigned nparam;
4765 unsigned n_in;
4767 i = isl_basic_map_alloc_inequality(bmap);
4768 if (i < 0)
4769 goto error;
4770 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4771 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4772 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4773 isl_int_set_si(bmap->ineq[i][0], -1);
4774 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4775 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4776 return isl_basic_map_finalize(bmap);
4777 error:
4778 isl_basic_map_free(bmap);
4779 return NULL;
4782 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4784 static __isl_give isl_basic_map *var_more_or_equal(
4785 __isl_take isl_basic_map *bmap, unsigned pos)
4787 int i;
4788 unsigned nparam;
4789 unsigned n_in;
4791 i = isl_basic_map_alloc_inequality(bmap);
4792 if (i < 0)
4793 goto error;
4794 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4795 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4796 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4797 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4798 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4799 return isl_basic_map_finalize(bmap);
4800 error:
4801 isl_basic_map_free(bmap);
4802 return NULL;
4805 __isl_give isl_basic_map *isl_basic_map_equal(
4806 __isl_take isl_space *space, unsigned n_equal)
4808 int i;
4809 struct isl_basic_map *bmap;
4810 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4811 if (!bmap)
4812 return NULL;
4813 for (i = 0; i < n_equal && bmap; ++i)
4814 bmap = var_equal(bmap, i);
4815 return isl_basic_map_finalize(bmap);
4818 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4820 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4821 unsigned pos)
4823 int i;
4824 struct isl_basic_map *bmap;
4825 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4826 if (!bmap)
4827 return NULL;
4828 for (i = 0; i < pos && bmap; ++i)
4829 bmap = var_equal(bmap, i);
4830 if (bmap)
4831 bmap = var_less(bmap, pos);
4832 return isl_basic_map_finalize(bmap);
4835 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4837 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4838 __isl_take isl_space *space, unsigned pos)
4840 int i;
4841 isl_basic_map *bmap;
4843 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4844 for (i = 0; i < pos; ++i)
4845 bmap = var_equal(bmap, i);
4846 bmap = var_less_or_equal(bmap, pos);
4847 return isl_basic_map_finalize(bmap);
4850 /* Return a relation on "space" expressing i_pos > o_pos
4852 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4853 unsigned pos)
4855 int i;
4856 struct isl_basic_map *bmap;
4857 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4858 if (!bmap)
4859 return NULL;
4860 for (i = 0; i < pos && bmap; ++i)
4861 bmap = var_equal(bmap, i);
4862 if (bmap)
4863 bmap = var_more(bmap, pos);
4864 return isl_basic_map_finalize(bmap);
4867 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4869 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4870 __isl_take isl_space *space, unsigned pos)
4872 int i;
4873 isl_basic_map *bmap;
4875 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4876 for (i = 0; i < pos; ++i)
4877 bmap = var_equal(bmap, i);
4878 bmap = var_more_or_equal(bmap, pos);
4879 return isl_basic_map_finalize(bmap);
4882 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4883 unsigned n, int equal)
4885 struct isl_map *map;
4886 int i;
4888 if (n == 0 && equal)
4889 return isl_map_universe(space);
4891 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4893 for (i = 0; i + 1 < n; ++i)
4894 map = isl_map_add_basic_map(map,
4895 isl_basic_map_less_at(isl_space_copy(space), i));
4896 if (n > 0) {
4897 if (equal)
4898 map = isl_map_add_basic_map(map,
4899 isl_basic_map_less_or_equal_at(space, n - 1));
4900 else
4901 map = isl_map_add_basic_map(map,
4902 isl_basic_map_less_at(space, n - 1));
4903 } else
4904 isl_space_free(space);
4906 return map;
4909 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
4911 if (!space)
4912 return NULL;
4913 return map_lex_lte_first(space, space->n_out, equal);
4916 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4918 return map_lex_lte_first(dim, n, 0);
4921 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4923 return map_lex_lte_first(dim, n, 1);
4926 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4928 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4931 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4933 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4936 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
4937 unsigned n, int equal)
4939 struct isl_map *map;
4940 int i;
4942 if (n == 0 && equal)
4943 return isl_map_universe(space);
4945 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4947 for (i = 0; i + 1 < n; ++i)
4948 map = isl_map_add_basic_map(map,
4949 isl_basic_map_more_at(isl_space_copy(space), i));
4950 if (n > 0) {
4951 if (equal)
4952 map = isl_map_add_basic_map(map,
4953 isl_basic_map_more_or_equal_at(space, n - 1));
4954 else
4955 map = isl_map_add_basic_map(map,
4956 isl_basic_map_more_at(space, n - 1));
4957 } else
4958 isl_space_free(space);
4960 return map;
4963 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
4965 if (!space)
4966 return NULL;
4967 return map_lex_gte_first(space, space->n_out, equal);
4970 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4972 return map_lex_gte_first(dim, n, 0);
4975 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4977 return map_lex_gte_first(dim, n, 1);
4980 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4982 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4985 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4987 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4990 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4991 __isl_take isl_set *set2)
4993 isl_map *map;
4994 map = isl_map_lex_le(isl_set_get_space(set1));
4995 map = isl_map_intersect_domain(map, set1);
4996 map = isl_map_intersect_range(map, set2);
4997 return map;
5000 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5001 __isl_take isl_set *set2)
5003 isl_map *map;
5004 map = isl_map_lex_lt(isl_set_get_space(set1));
5005 map = isl_map_intersect_domain(map, set1);
5006 map = isl_map_intersect_range(map, set2);
5007 return map;
5010 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5011 __isl_take isl_set *set2)
5013 isl_map *map;
5014 map = isl_map_lex_ge(isl_set_get_space(set1));
5015 map = isl_map_intersect_domain(map, set1);
5016 map = isl_map_intersect_range(map, set2);
5017 return map;
5020 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5021 __isl_take isl_set *set2)
5023 isl_map *map;
5024 map = isl_map_lex_gt(isl_set_get_space(set1));
5025 map = isl_map_intersect_domain(map, set1);
5026 map = isl_map_intersect_range(map, set2);
5027 return map;
5030 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5031 __isl_take isl_map *map2)
5033 isl_map *map;
5034 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5035 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5036 map = isl_map_apply_range(map, isl_map_reverse(map2));
5037 return map;
5040 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5041 __isl_take isl_map *map2)
5043 isl_map *map;
5044 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5045 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5046 map = isl_map_apply_range(map, isl_map_reverse(map2));
5047 return map;
5050 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5051 __isl_take isl_map *map2)
5053 isl_map *map;
5054 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5055 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5056 map = isl_map_apply_range(map, isl_map_reverse(map2));
5057 return map;
5060 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5061 __isl_take isl_map *map2)
5063 isl_map *map;
5064 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5065 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5066 map = isl_map_apply_range(map, isl_map_reverse(map2));
5067 return map;
5070 /* For the div d = floor(f/m) at position "div", add the constraint
5072 * f - m d >= 0
5074 static __isl_give isl_basic_map *add_upper_div_constraint(
5075 __isl_take isl_basic_map *bmap, unsigned div)
5077 int i;
5078 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5079 unsigned n_div, pos;
5081 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5082 if (v_div < 0)
5083 return isl_basic_map_free(bmap);
5084 pos = v_div + div;
5085 i = isl_basic_map_alloc_inequality(bmap);
5086 if (i < 0)
5087 return isl_basic_map_free(bmap);
5088 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5089 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5091 return bmap;
5094 /* For the div d = floor(f/m) at position "div", add the constraint
5096 * -(f-(m-1)) + m d >= 0
5098 static __isl_give isl_basic_map *add_lower_div_constraint(
5099 __isl_take isl_basic_map *bmap, unsigned div)
5101 int i;
5102 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5103 unsigned n_div, pos;
5105 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5106 if (v_div < 0)
5107 return isl_basic_map_free(bmap);
5108 pos = v_div + div;
5109 i = isl_basic_map_alloc_inequality(bmap);
5110 if (i < 0)
5111 return isl_basic_map_free(bmap);
5112 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5113 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5114 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5115 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5117 return bmap;
5120 /* For the div d = floor(f/m) at position "pos", add the constraints
5122 * f - m d >= 0
5123 * -(f-(m-1)) + m d >= 0
5125 * Note that the second constraint is the negation of
5127 * f - m d >= m
5129 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5130 __isl_take isl_basic_map *bmap, unsigned pos)
5132 bmap = add_upper_div_constraint(bmap, pos);
5133 bmap = add_lower_div_constraint(bmap, pos);
5134 return bmap;
5137 /* For each known div d = floor(f/m), add the constraints
5139 * f - m d >= 0
5140 * -(f-(m-1)) + m d >= 0
5142 * Remove duplicate constraints in case of some these div constraints
5143 * already appear in "bmap".
5145 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5146 __isl_take isl_basic_map *bmap)
5148 unsigned n_div;
5150 if (!bmap)
5151 return NULL;
5152 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5153 if (n_div == 0)
5154 return bmap;
5156 bmap = add_known_div_constraints(bmap);
5157 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5158 bmap = isl_basic_map_finalize(bmap);
5159 return bmap;
5162 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5164 * In particular, if this div is of the form d = floor(f/m),
5165 * then add the constraint
5167 * f - m d >= 0
5169 * if sign < 0 or the constraint
5171 * -(f-(m-1)) + m d >= 0
5173 * if sign > 0.
5175 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5176 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5178 if (sign < 0)
5179 return add_upper_div_constraint(bmap, div);
5180 else
5181 return add_lower_div_constraint(bmap, div);
5184 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5185 __isl_take isl_basic_map *bmap)
5187 if (!bmap)
5188 goto error;
5189 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5190 bmap->n_div == 0 &&
5191 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5192 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5193 return bset_from_bmap(bmap);
5194 bmap = isl_basic_map_cow(bmap);
5195 if (!bmap)
5196 goto error;
5197 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5198 if (!bmap->dim)
5199 goto error;
5200 bmap->extra -= bmap->n_div;
5201 bmap->n_div = 0;
5202 bmap = isl_basic_map_finalize(bmap);
5203 return bset_from_bmap(bmap);
5204 error:
5205 isl_basic_map_free(bmap);
5206 return NULL;
5209 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5210 __isl_take isl_basic_set *bset)
5212 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5215 /* Replace each element in "list" by the result of applying
5216 * isl_basic_map_underlying_set to the element.
5218 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5219 __isl_take isl_basic_map_list *list)
5221 int i, n;
5223 if (!list)
5224 return NULL;
5226 n = isl_basic_map_list_n_basic_map(list);
5227 for (i = 0; i < n; ++i) {
5228 isl_basic_map *bmap;
5229 isl_basic_set *bset;
5231 bmap = isl_basic_map_list_get_basic_map(list, i);
5232 bset = isl_basic_set_underlying_set(bmap);
5233 list = isl_basic_set_list_set_basic_set(list, i, bset);
5236 return list;
5239 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5240 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5242 struct isl_basic_map *bmap;
5243 struct isl_ctx *ctx;
5244 unsigned dim, total;
5245 int i;
5247 if (!bset || !like)
5248 goto error;
5249 ctx = bset->ctx;
5250 if (isl_basic_set_check_no_params(bset) < 0 ||
5251 isl_basic_set_check_no_locals(bset) < 0)
5252 goto error;
5253 dim = isl_basic_set_dim(bset, isl_dim_set);
5254 isl_assert(ctx, dim == isl_basic_map_total_dim(like), goto error);
5255 if (like->n_div == 0) {
5256 isl_space *space = isl_basic_map_get_space(like);
5257 isl_basic_map_free(like);
5258 return isl_basic_map_reset_space(bset, space);
5260 bset = isl_basic_set_cow(bset);
5261 if (!bset)
5262 goto error;
5263 total = dim + bset->extra;
5264 bmap = bset_to_bmap(bset);
5265 isl_space_free(bmap->dim);
5266 bmap->dim = isl_space_copy(like->dim);
5267 if (!bmap->dim)
5268 goto error;
5269 bmap->n_div = like->n_div;
5270 bmap->extra += like->n_div;
5271 if (bmap->extra) {
5272 unsigned ltotal;
5273 isl_int **div;
5274 ltotal = total - bmap->extra + like->extra;
5275 if (ltotal > total)
5276 ltotal = total;
5277 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5278 bmap->extra * (1 + 1 + total));
5279 if (isl_blk_is_error(bmap->block2))
5280 goto error;
5281 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5282 if (!div)
5283 goto error;
5284 bmap->div = div;
5285 for (i = 0; i < bmap->extra; ++i)
5286 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5287 for (i = 0; i < like->n_div; ++i) {
5288 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5289 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5291 bmap = isl_basic_map_add_known_div_constraints(bmap);
5293 isl_basic_map_free(like);
5294 bmap = isl_basic_map_simplify(bmap);
5295 bmap = isl_basic_map_finalize(bmap);
5296 return bmap;
5297 error:
5298 isl_basic_map_free(like);
5299 isl_basic_set_free(bset);
5300 return NULL;
5303 struct isl_basic_set *isl_basic_set_from_underlying_set(
5304 struct isl_basic_set *bset, struct isl_basic_set *like)
5306 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5307 bset_to_bmap(like)));
5310 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5312 int i;
5314 map = isl_map_cow(map);
5315 if (!map)
5316 return NULL;
5317 map->dim = isl_space_cow(map->dim);
5318 if (!map->dim)
5319 goto error;
5321 for (i = 1; i < map->n; ++i)
5322 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5323 goto error);
5324 for (i = 0; i < map->n; ++i) {
5325 map->p[i] = bset_to_bmap(
5326 isl_basic_map_underlying_set(map->p[i]));
5327 if (!map->p[i])
5328 goto error;
5330 if (map->n == 0)
5331 map->dim = isl_space_underlying(map->dim, 0);
5332 else {
5333 isl_space_free(map->dim);
5334 map->dim = isl_space_copy(map->p[0]->dim);
5336 if (!map->dim)
5337 goto error;
5338 return set_from_map(map);
5339 error:
5340 isl_map_free(map);
5341 return NULL;
5344 /* Replace the space of "bmap" by "space".
5346 * If the space of "bmap" is identical to "space" (including the identifiers
5347 * of the input and output dimensions), then simply return the original input.
5349 __isl_give isl_basic_map *isl_basic_map_reset_space(
5350 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5352 isl_bool equal;
5353 isl_space *bmap_space;
5355 bmap_space = isl_basic_map_peek_space(bmap);
5356 equal = isl_space_is_equal(bmap_space, space);
5357 if (equal >= 0 && equal)
5358 equal = isl_space_has_equal_ids(bmap_space, space);
5359 if (equal < 0)
5360 goto error;
5361 if (equal) {
5362 isl_space_free(space);
5363 return bmap;
5365 bmap = isl_basic_map_cow(bmap);
5366 if (!bmap || !space)
5367 goto error;
5369 isl_space_free(bmap->dim);
5370 bmap->dim = space;
5372 bmap = isl_basic_map_finalize(bmap);
5374 return bmap;
5375 error:
5376 isl_basic_map_free(bmap);
5377 isl_space_free(space);
5378 return NULL;
5381 __isl_give isl_basic_set *isl_basic_set_reset_space(
5382 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5384 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5385 dim));
5388 /* Check that the total dimensions of "map" and "space" are the same.
5390 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5391 __isl_keep isl_space *space)
5393 unsigned dim1, dim2;
5395 if (!map || !space)
5396 return isl_stat_error;
5397 dim1 = isl_map_dim(map, isl_dim_all);
5398 dim2 = isl_space_dim(space, isl_dim_all);
5399 if (dim1 == dim2)
5400 return isl_stat_ok;
5401 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5402 "total dimensions do not match", return isl_stat_error);
5405 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5406 __isl_take isl_space *space)
5408 int i;
5410 map = isl_map_cow(map);
5411 if (!map || !space)
5412 goto error;
5414 for (i = 0; i < map->n; ++i) {
5415 map->p[i] = isl_basic_map_reset_space(map->p[i],
5416 isl_space_copy(space));
5417 if (!map->p[i])
5418 goto error;
5420 isl_space_free(map->dim);
5421 map->dim = space;
5423 return map;
5424 error:
5425 isl_map_free(map);
5426 isl_space_free(space);
5427 return NULL;
5430 /* Replace the space of "map" by "space", without modifying
5431 * the dimension of "map".
5433 * If the space of "map" is identical to "space" (including the identifiers
5434 * of the input and output dimensions), then simply return the original input.
5436 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5437 __isl_take isl_space *space)
5439 isl_bool equal;
5440 isl_space *map_space;
5442 map_space = isl_map_peek_space(map);
5443 equal = isl_space_is_equal(map_space, space);
5444 if (equal >= 0 && equal)
5445 equal = isl_space_has_equal_ids(map_space, space);
5446 if (equal < 0)
5447 goto error;
5448 if (equal) {
5449 isl_space_free(space);
5450 return map;
5452 if (check_map_space_equal_total_dim(map, space) < 0)
5453 goto error;
5454 return isl_map_reset_space(map, space);
5455 error:
5456 isl_map_free(map);
5457 isl_space_free(space);
5458 return NULL;
5461 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5462 __isl_take isl_space *dim)
5464 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5467 /* Compute the parameter domain of the given basic set.
5469 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5471 isl_bool is_params;
5472 isl_space *space;
5473 unsigned n;
5475 is_params = isl_basic_set_is_params(bset);
5476 if (is_params < 0)
5477 return isl_basic_set_free(bset);
5478 if (is_params)
5479 return bset;
5481 n = isl_basic_set_dim(bset, isl_dim_set);
5482 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5483 space = isl_basic_set_get_space(bset);
5484 space = isl_space_params(space);
5485 bset = isl_basic_set_reset_space(bset, space);
5486 return bset;
5489 /* Construct a zero-dimensional basic set with the given parameter domain.
5491 __isl_give isl_basic_set *isl_basic_set_from_params(
5492 __isl_take isl_basic_set *bset)
5494 isl_space *space;
5495 space = isl_basic_set_get_space(bset);
5496 space = isl_space_set_from_params(space);
5497 bset = isl_basic_set_reset_space(bset, space);
5498 return bset;
5501 /* Compute the parameter domain of the given set.
5503 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5505 return isl_map_params(set_to_map(set));
5508 /* Construct a zero-dimensional set with the given parameter domain.
5510 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5512 isl_space *space;
5513 space = isl_set_get_space(set);
5514 space = isl_space_set_from_params(space);
5515 set = isl_set_reset_space(set, space);
5516 return set;
5519 /* Compute the parameter domain of the given map.
5521 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5523 isl_space *space;
5524 unsigned n;
5526 n = isl_map_dim(map, isl_dim_in);
5527 map = isl_map_project_out(map, isl_dim_in, 0, n);
5528 n = isl_map_dim(map, isl_dim_out);
5529 map = isl_map_project_out(map, isl_dim_out, 0, n);
5530 space = isl_map_get_space(map);
5531 space = isl_space_params(space);
5532 map = isl_map_reset_space(map, space);
5533 return map;
5536 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5538 isl_space *space;
5539 unsigned n_out;
5541 if (!bmap)
5542 return NULL;
5543 space = isl_space_domain(isl_basic_map_get_space(bmap));
5545 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5546 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5548 return isl_basic_map_reset_space(bmap, space);
5551 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5553 if (!bmap)
5554 return isl_bool_error;
5555 return isl_space_may_be_set(bmap->dim);
5558 /* Is this basic map actually a set?
5559 * Users should never call this function. Outside of isl,
5560 * the type should indicate whether something is a set or a map.
5562 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5564 if (!bmap)
5565 return isl_bool_error;
5566 return isl_space_is_set(bmap->dim);
5569 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5571 isl_bool is_set;
5573 is_set = isl_basic_map_is_set(bmap);
5574 if (is_set < 0)
5575 goto error;
5576 if (is_set)
5577 return bmap;
5578 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5579 error:
5580 isl_basic_map_free(bmap);
5581 return NULL;
5584 __isl_give isl_basic_map *isl_basic_map_domain_map(
5585 __isl_take isl_basic_map *bmap)
5587 int i;
5588 isl_space *space;
5589 isl_basic_map *domain;
5590 int nparam, n_in, n_out;
5592 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5593 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5594 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5596 space = isl_basic_map_get_space(bmap);
5597 space = isl_space_from_range(isl_space_domain(space));
5598 domain = isl_basic_map_universe(space);
5600 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5601 bmap = isl_basic_map_apply_range(bmap, domain);
5602 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5604 for (i = 0; i < n_in; ++i)
5605 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5606 isl_dim_out, i);
5608 bmap = isl_basic_map_gauss(bmap, NULL);
5609 return isl_basic_map_finalize(bmap);
5612 __isl_give isl_basic_map *isl_basic_map_range_map(
5613 __isl_take isl_basic_map *bmap)
5615 int i;
5616 isl_space *space;
5617 isl_basic_map *range;
5618 int nparam, n_in, n_out;
5620 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5621 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5622 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5624 space = isl_basic_map_get_space(bmap);
5625 space = isl_space_from_range(isl_space_range(space));
5626 range = isl_basic_map_universe(space);
5628 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5629 bmap = isl_basic_map_apply_range(bmap, range);
5630 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5632 for (i = 0; i < n_out; ++i)
5633 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5634 isl_dim_out, i);
5636 bmap = isl_basic_map_gauss(bmap, NULL);
5637 return isl_basic_map_finalize(bmap);
5640 int isl_map_may_be_set(__isl_keep isl_map *map)
5642 if (!map)
5643 return -1;
5644 return isl_space_may_be_set(map->dim);
5647 /* Is this map actually a set?
5648 * Users should never call this function. Outside of isl,
5649 * the type should indicate whether something is a set or a map.
5651 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5653 if (!map)
5654 return isl_bool_error;
5655 return isl_space_is_set(map->dim);
5658 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5660 int i;
5661 isl_bool is_set;
5662 struct isl_set *set;
5664 is_set = isl_map_is_set(map);
5665 if (is_set < 0)
5666 goto error;
5667 if (is_set)
5668 return set_from_map(map);
5670 map = isl_map_cow(map);
5671 if (!map)
5672 goto error;
5674 set = set_from_map(map);
5675 set->dim = isl_space_range(set->dim);
5676 if (!set->dim)
5677 goto error;
5678 for (i = 0; i < map->n; ++i) {
5679 set->p[i] = isl_basic_map_range(map->p[i]);
5680 if (!set->p[i])
5681 goto error;
5683 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5684 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5685 return set;
5686 error:
5687 isl_map_free(map);
5688 return NULL;
5691 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5693 int i;
5695 map = isl_map_cow(map);
5696 if (!map)
5697 return NULL;
5699 map->dim = isl_space_domain_map(map->dim);
5700 if (!map->dim)
5701 goto error;
5702 for (i = 0; i < map->n; ++i) {
5703 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5704 if (!map->p[i])
5705 goto error;
5707 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5708 map = isl_map_unmark_normalized(map);
5709 return map;
5710 error:
5711 isl_map_free(map);
5712 return NULL;
5715 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5717 int i;
5718 isl_space *range_dim;
5720 map = isl_map_cow(map);
5721 if (!map)
5722 return NULL;
5724 range_dim = isl_space_range(isl_map_get_space(map));
5725 range_dim = isl_space_from_range(range_dim);
5726 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5727 map->dim = isl_space_join(map->dim, range_dim);
5728 if (!map->dim)
5729 goto error;
5730 for (i = 0; i < map->n; ++i) {
5731 map->p[i] = isl_basic_map_range_map(map->p[i]);
5732 if (!map->p[i])
5733 goto error;
5735 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5736 map = isl_map_unmark_normalized(map);
5737 return map;
5738 error:
5739 isl_map_free(map);
5740 return NULL;
5743 /* Given a wrapped map of the form A[B -> C],
5744 * return the map A[B -> C] -> B.
5746 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5748 isl_id *id;
5749 isl_map *map;
5751 if (!set)
5752 return NULL;
5753 if (!isl_set_has_tuple_id(set))
5754 return isl_map_domain_map(isl_set_unwrap(set));
5756 id = isl_set_get_tuple_id(set);
5757 map = isl_map_domain_map(isl_set_unwrap(set));
5758 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5760 return map;
5763 __isl_give isl_basic_map *isl_basic_map_from_domain(
5764 __isl_take isl_basic_set *bset)
5766 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5769 __isl_give isl_basic_map *isl_basic_map_from_range(
5770 __isl_take isl_basic_set *bset)
5772 isl_space *space;
5773 space = isl_basic_set_get_space(bset);
5774 space = isl_space_from_range(space);
5775 bset = isl_basic_set_reset_space(bset, space);
5776 return bset_to_bmap(bset);
5779 /* Create a relation with the given set as range.
5780 * The domain of the created relation is a zero-dimensional
5781 * flat anonymous space.
5783 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5785 isl_space *space;
5786 space = isl_set_get_space(set);
5787 space = isl_space_from_range(space);
5788 set = isl_set_reset_space(set, space);
5789 return set_to_map(set);
5792 /* Create a relation with the given set as domain.
5793 * The range of the created relation is a zero-dimensional
5794 * flat anonymous space.
5796 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5798 return isl_map_reverse(isl_map_from_range(set));
5801 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5802 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5804 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5807 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5808 __isl_take isl_set *range)
5810 return isl_map_apply_range(isl_map_reverse(domain), range);
5813 /* Return a newly allocated isl_map with given space and flags and
5814 * room for "n" basic maps.
5815 * Make sure that all cached information is cleared.
5817 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5818 unsigned flags)
5820 struct isl_map *map;
5822 if (!space)
5823 return NULL;
5824 if (n < 0)
5825 isl_die(space->ctx, isl_error_internal,
5826 "negative number of basic maps", goto error);
5827 map = isl_calloc(space->ctx, struct isl_map,
5828 sizeof(struct isl_map) +
5829 (n - 1) * sizeof(struct isl_basic_map *));
5830 if (!map)
5831 goto error;
5833 map->ctx = space->ctx;
5834 isl_ctx_ref(map->ctx);
5835 map->ref = 1;
5836 map->size = n;
5837 map->n = 0;
5838 map->dim = space;
5839 map->flags = flags;
5840 return map;
5841 error:
5842 isl_space_free(space);
5843 return NULL;
5846 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5848 struct isl_basic_map *bmap;
5849 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5850 bmap = isl_basic_map_set_to_empty(bmap);
5851 return bmap;
5854 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5856 struct isl_basic_set *bset;
5857 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5858 bset = isl_basic_set_set_to_empty(bset);
5859 return bset;
5862 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5864 struct isl_basic_map *bmap;
5865 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5866 bmap = isl_basic_map_finalize(bmap);
5867 return bmap;
5870 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
5872 struct isl_basic_set *bset;
5873 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5874 bset = isl_basic_set_finalize(bset);
5875 return bset;
5878 __isl_give isl_basic_map *isl_basic_map_nat_universe(
5879 __isl_take isl_space *space)
5881 int i;
5882 unsigned total = isl_space_dim(space, isl_dim_all);
5883 isl_basic_map *bmap;
5885 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
5886 for (i = 0; i < total; ++i) {
5887 int k = isl_basic_map_alloc_inequality(bmap);
5888 if (k < 0)
5889 goto error;
5890 isl_seq_clr(bmap->ineq[k], 1 + total);
5891 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5893 return bmap;
5894 error:
5895 isl_basic_map_free(bmap);
5896 return NULL;
5899 __isl_give isl_basic_set *isl_basic_set_nat_universe(
5900 __isl_take isl_space *space)
5902 return isl_basic_map_nat_universe(space);
5905 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5907 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5910 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5912 return isl_map_nat_universe(dim);
5915 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
5917 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
5920 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
5922 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
5925 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
5927 struct isl_map *map;
5928 if (!space)
5929 return NULL;
5930 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
5931 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
5932 return map;
5935 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
5937 struct isl_set *set;
5938 if (!space)
5939 return NULL;
5940 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
5941 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
5942 return set;
5945 struct isl_map *isl_map_dup(struct isl_map *map)
5947 int i;
5948 struct isl_map *dup;
5950 if (!map)
5951 return NULL;
5952 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5953 for (i = 0; i < map->n; ++i)
5954 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5955 return dup;
5958 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5959 __isl_take isl_basic_map *bmap)
5961 if (!bmap || !map)
5962 goto error;
5963 if (isl_basic_map_plain_is_empty(bmap)) {
5964 isl_basic_map_free(bmap);
5965 return map;
5967 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5968 isl_assert(map->ctx, map->n < map->size, goto error);
5969 map->p[map->n] = bmap;
5970 map->n++;
5971 map = isl_map_unmark_normalized(map);
5972 return map;
5973 error:
5974 if (map)
5975 isl_map_free(map);
5976 if (bmap)
5977 isl_basic_map_free(bmap);
5978 return NULL;
5981 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5983 int i;
5985 if (!map)
5986 return NULL;
5988 if (--map->ref > 0)
5989 return NULL;
5991 clear_caches(map);
5992 isl_ctx_deref(map->ctx);
5993 for (i = 0; i < map->n; ++i)
5994 isl_basic_map_free(map->p[i]);
5995 isl_space_free(map->dim);
5996 free(map);
5998 return NULL;
6001 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6002 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6004 int j;
6006 bmap = isl_basic_map_cow(bmap);
6007 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6008 j = isl_basic_map_alloc_equality(bmap);
6009 if (j < 0)
6010 goto error;
6011 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6012 isl_int_set_si(bmap->eq[j][pos], -1);
6013 isl_int_set_si(bmap->eq[j][0], value);
6014 bmap = isl_basic_map_simplify(bmap);
6015 return isl_basic_map_finalize(bmap);
6016 error:
6017 isl_basic_map_free(bmap);
6018 return NULL;
6021 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6022 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6024 int j;
6026 bmap = isl_basic_map_cow(bmap);
6027 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6028 j = isl_basic_map_alloc_equality(bmap);
6029 if (j < 0)
6030 goto error;
6031 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6032 isl_int_set_si(bmap->eq[j][pos], -1);
6033 isl_int_set(bmap->eq[j][0], value);
6034 bmap = isl_basic_map_simplify(bmap);
6035 return isl_basic_map_finalize(bmap);
6036 error:
6037 isl_basic_map_free(bmap);
6038 return NULL;
6041 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6042 enum isl_dim_type type, unsigned pos, int value)
6044 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6045 return isl_basic_map_free(bmap);
6046 return isl_basic_map_fix_pos_si(bmap,
6047 isl_basic_map_offset(bmap, type) + pos, value);
6050 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6051 enum isl_dim_type type, unsigned pos, isl_int value)
6053 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6054 return isl_basic_map_free(bmap);
6055 return isl_basic_map_fix_pos(bmap,
6056 isl_basic_map_offset(bmap, type) + pos, value);
6059 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6060 * to be equal to "v".
6062 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6063 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6065 if (!bmap || !v)
6066 goto error;
6067 if (!isl_val_is_int(v))
6068 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6069 "expecting integer value", goto error);
6070 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6071 goto error;
6072 pos += isl_basic_map_offset(bmap, type);
6073 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6074 isl_val_free(v);
6075 return bmap;
6076 error:
6077 isl_basic_map_free(bmap);
6078 isl_val_free(v);
6079 return NULL;
6082 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6083 * to be equal to "v".
6085 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6086 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6088 return isl_basic_map_fix_val(bset, type, pos, v);
6091 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6092 enum isl_dim_type type, unsigned pos, int value)
6094 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6095 type, pos, value));
6098 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6099 enum isl_dim_type type, unsigned pos, isl_int value)
6101 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6102 type, pos, value));
6105 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6106 unsigned input, int value)
6108 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6111 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6112 unsigned dim, int value)
6114 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6115 isl_dim_set, dim, value));
6118 /* Remove the basic map at position "i" from "map" if this basic map
6119 * is (obviously) empty.
6121 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6123 isl_bool empty;
6125 if (!map)
6126 return NULL;
6128 empty = isl_basic_map_plain_is_empty(map->p[i]);
6129 if (empty < 0)
6130 return isl_map_free(map);
6131 if (!empty)
6132 return map;
6134 isl_basic_map_free(map->p[i]);
6135 map->n--;
6136 if (i != map->n) {
6137 map->p[i] = map->p[map->n];
6138 map = isl_map_unmark_normalized(map);
6142 return map;
6145 /* Perform "fn" on each basic map of "map", where we may not be holding
6146 * the only reference to "map".
6147 * In particular, "fn" should be a semantics preserving operation
6148 * that we want to apply to all copies of "map". We therefore need
6149 * to be careful not to modify "map" in a way that breaks "map"
6150 * in case anything goes wrong.
6152 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6153 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6155 struct isl_basic_map *bmap;
6156 int i;
6158 if (!map)
6159 return NULL;
6161 for (i = map->n - 1; i >= 0; --i) {
6162 bmap = isl_basic_map_copy(map->p[i]);
6163 bmap = fn(bmap);
6164 if (!bmap)
6165 goto error;
6166 isl_basic_map_free(map->p[i]);
6167 map->p[i] = bmap;
6168 map = remove_if_empty(map, i);
6169 if (!map)
6170 return NULL;
6173 return map;
6174 error:
6175 isl_map_free(map);
6176 return NULL;
6179 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6180 enum isl_dim_type type, unsigned pos, int value)
6182 int i;
6184 map = isl_map_cow(map);
6185 if (isl_map_check_range(map, type, pos, 1) < 0)
6186 return isl_map_free(map);
6187 for (i = map->n - 1; i >= 0; --i) {
6188 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6189 map = remove_if_empty(map, i);
6190 if (!map)
6191 return NULL;
6193 map = isl_map_unmark_normalized(map);
6194 return map;
6197 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6198 enum isl_dim_type type, unsigned pos, int value)
6200 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6203 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6204 enum isl_dim_type type, unsigned pos, isl_int value)
6206 int i;
6208 map = isl_map_cow(map);
6209 if (isl_map_check_range(map, type, pos, 1) < 0)
6210 return isl_map_free(map);
6211 for (i = 0; i < map->n; ++i) {
6212 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6213 if (!map->p[i])
6214 goto error;
6216 map = isl_map_unmark_normalized(map);
6217 return map;
6218 error:
6219 isl_map_free(map);
6220 return NULL;
6223 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6224 enum isl_dim_type type, unsigned pos, isl_int value)
6226 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6229 /* Fix the value of the variable at position "pos" of type "type" of "map"
6230 * to be equal to "v".
6232 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6233 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6235 int i;
6237 map = isl_map_cow(map);
6238 if (!map || !v)
6239 goto error;
6241 if (!isl_val_is_int(v))
6242 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6243 "expecting integer value", goto error);
6244 if (isl_map_check_range(map, type, pos, 1) < 0)
6245 goto error;
6246 for (i = map->n - 1; i >= 0; --i) {
6247 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6248 isl_val_copy(v));
6249 map = remove_if_empty(map, i);
6250 if (!map)
6251 goto error;
6253 map = isl_map_unmark_normalized(map);
6254 isl_val_free(v);
6255 return map;
6256 error:
6257 isl_map_free(map);
6258 isl_val_free(v);
6259 return NULL;
6262 /* Fix the value of the variable at position "pos" of type "type" of "set"
6263 * to be equal to "v".
6265 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6266 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6268 return isl_map_fix_val(set, type, pos, v);
6271 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6272 unsigned input, int value)
6274 return isl_map_fix_si(map, isl_dim_in, input, value);
6277 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6279 return set_from_map(isl_map_fix_si(set_to_map(set),
6280 isl_dim_set, dim, value));
6283 static __isl_give isl_basic_map *basic_map_bound_si(
6284 __isl_take isl_basic_map *bmap,
6285 enum isl_dim_type type, unsigned pos, int value, int upper)
6287 int j;
6289 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6290 return isl_basic_map_free(bmap);
6291 pos += isl_basic_map_offset(bmap, type);
6292 bmap = isl_basic_map_cow(bmap);
6293 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6294 j = isl_basic_map_alloc_inequality(bmap);
6295 if (j < 0)
6296 goto error;
6297 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6298 if (upper) {
6299 isl_int_set_si(bmap->ineq[j][pos], -1);
6300 isl_int_set_si(bmap->ineq[j][0], value);
6301 } else {
6302 isl_int_set_si(bmap->ineq[j][pos], 1);
6303 isl_int_set_si(bmap->ineq[j][0], -value);
6305 bmap = isl_basic_map_simplify(bmap);
6306 return isl_basic_map_finalize(bmap);
6307 error:
6308 isl_basic_map_free(bmap);
6309 return NULL;
6312 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6313 __isl_take isl_basic_map *bmap,
6314 enum isl_dim_type type, unsigned pos, int value)
6316 return basic_map_bound_si(bmap, type, pos, value, 0);
6319 /* Constrain the values of the given dimension to be no greater than "value".
6321 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6322 __isl_take isl_basic_map *bmap,
6323 enum isl_dim_type type, unsigned pos, int value)
6325 return basic_map_bound_si(bmap, type, pos, value, 1);
6328 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6329 enum isl_dim_type type, unsigned pos, int value, int upper)
6331 int i;
6333 map = isl_map_cow(map);
6334 if (isl_map_check_range(map, type, pos, 1) < 0)
6335 return isl_map_free(map);
6336 for (i = 0; i < map->n; ++i) {
6337 map->p[i] = basic_map_bound_si(map->p[i],
6338 type, pos, value, upper);
6339 if (!map->p[i])
6340 goto error;
6342 map = isl_map_unmark_normalized(map);
6343 return map;
6344 error:
6345 isl_map_free(map);
6346 return NULL;
6349 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6350 enum isl_dim_type type, unsigned pos, int value)
6352 return map_bound_si(map, type, pos, value, 0);
6355 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6356 enum isl_dim_type type, unsigned pos, int value)
6358 return map_bound_si(map, type, pos, value, 1);
6361 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6362 enum isl_dim_type type, unsigned pos, int value)
6364 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6365 type, pos, value));
6368 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6369 enum isl_dim_type type, unsigned pos, int value)
6371 return isl_map_upper_bound_si(set, type, pos, value);
6374 /* Bound the given variable of "bmap" from below (or above is "upper"
6375 * is set) to "value".
6377 static __isl_give isl_basic_map *basic_map_bound(
6378 __isl_take isl_basic_map *bmap,
6379 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6381 int j;
6383 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6384 return isl_basic_map_free(bmap);
6385 pos += isl_basic_map_offset(bmap, type);
6386 bmap = isl_basic_map_cow(bmap);
6387 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6388 j = isl_basic_map_alloc_inequality(bmap);
6389 if (j < 0)
6390 goto error;
6391 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6392 if (upper) {
6393 isl_int_set_si(bmap->ineq[j][pos], -1);
6394 isl_int_set(bmap->ineq[j][0], value);
6395 } else {
6396 isl_int_set_si(bmap->ineq[j][pos], 1);
6397 isl_int_neg(bmap->ineq[j][0], value);
6399 bmap = isl_basic_map_simplify(bmap);
6400 return isl_basic_map_finalize(bmap);
6401 error:
6402 isl_basic_map_free(bmap);
6403 return NULL;
6406 /* Bound the given variable of "map" from below (or above is "upper"
6407 * is set) to "value".
6409 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6410 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6412 int i;
6414 map = isl_map_cow(map);
6415 if (isl_map_check_range(map, type, pos, 1) < 0)
6416 return isl_map_free(map);
6417 for (i = map->n - 1; i >= 0; --i) {
6418 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6419 map = remove_if_empty(map, i);
6420 if (!map)
6421 return NULL;
6423 map = isl_map_unmark_normalized(map);
6424 return map;
6427 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6428 enum isl_dim_type type, unsigned pos, isl_int value)
6430 return map_bound(map, type, pos, value, 0);
6433 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6434 enum isl_dim_type type, unsigned pos, isl_int value)
6436 return map_bound(map, type, pos, value, 1);
6439 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6440 enum isl_dim_type type, unsigned pos, isl_int value)
6442 return isl_map_lower_bound(set, type, pos, value);
6445 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6446 enum isl_dim_type type, unsigned pos, isl_int value)
6448 return isl_map_upper_bound(set, type, pos, value);
6451 /* Force the values of the variable at position "pos" of type "type" of "set"
6452 * to be no smaller than "value".
6454 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6455 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6457 if (!value)
6458 goto error;
6459 if (!isl_val_is_int(value))
6460 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6461 "expecting integer value", goto error);
6462 set = isl_set_lower_bound(set, type, pos, value->n);
6463 isl_val_free(value);
6464 return set;
6465 error:
6466 isl_val_free(value);
6467 isl_set_free(set);
6468 return NULL;
6471 /* Force the values of the variable at position "pos" of type "type" of "set"
6472 * to be no greater than "value".
6474 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6475 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6477 if (!value)
6478 goto error;
6479 if (!isl_val_is_int(value))
6480 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6481 "expecting integer value", goto error);
6482 set = isl_set_upper_bound(set, type, pos, value->n);
6483 isl_val_free(value);
6484 return set;
6485 error:
6486 isl_val_free(value);
6487 isl_set_free(set);
6488 return NULL;
6491 /* Bound the given variable of "bset" from below (or above is "upper"
6492 * is set) to "value".
6494 static __isl_give isl_basic_set *isl_basic_set_bound(
6495 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6496 isl_int value, int upper)
6498 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6499 type, pos, value, upper));
6502 /* Bound the given variable of "bset" from below (or above is "upper"
6503 * is set) to "value".
6505 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6506 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6507 __isl_take isl_val *value, int upper)
6509 if (!value)
6510 goto error;
6511 if (!isl_val_is_int(value))
6512 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6513 "expecting integer value", goto error);
6514 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6515 isl_val_free(value);
6516 return bset;
6517 error:
6518 isl_val_free(value);
6519 isl_basic_set_free(bset);
6520 return NULL;
6523 /* Bound the given variable of "bset" from below to "value".
6525 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6526 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6527 __isl_take isl_val *value)
6529 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6532 /* Bound the given variable of "bset" from above to "value".
6534 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6535 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6536 __isl_take isl_val *value)
6538 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6541 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6543 int i;
6545 map = isl_map_cow(map);
6546 if (!map)
6547 return NULL;
6549 map->dim = isl_space_reverse(map->dim);
6550 if (!map->dim)
6551 goto error;
6552 for (i = 0; i < map->n; ++i) {
6553 map->p[i] = isl_basic_map_reverse(map->p[i]);
6554 if (!map->p[i])
6555 goto error;
6557 map = isl_map_unmark_normalized(map);
6558 return map;
6559 error:
6560 isl_map_free(map);
6561 return NULL;
6564 #undef TYPE
6565 #define TYPE isl_pw_multi_aff
6566 #undef SUFFIX
6567 #define SUFFIX _pw_multi_aff
6568 #undef EMPTY
6569 #define EMPTY isl_pw_multi_aff_empty
6570 #undef ADD
6571 #define ADD isl_pw_multi_aff_union_add
6572 #include "isl_map_lexopt_templ.c"
6574 /* Given a map "map", compute the lexicographically minimal
6575 * (or maximal) image element for each domain element in dom,
6576 * in the form of an isl_pw_multi_aff.
6577 * If "empty" is not NULL, then set *empty to those elements in dom that
6578 * do not have an image element.
6579 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6580 * should be computed over the domain of "map". "empty" is also NULL
6581 * in this case.
6583 * We first compute the lexicographically minimal or maximal element
6584 * in the first basic map. This results in a partial solution "res"
6585 * and a subset "todo" of dom that still need to be handled.
6586 * We then consider each of the remaining maps in "map" and successively
6587 * update both "res" and "todo".
6588 * If "empty" is NULL, then the todo sets are not needed and therefore
6589 * also not computed.
6591 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6592 __isl_take isl_map *map, __isl_take isl_set *dom,
6593 __isl_give isl_set **empty, unsigned flags)
6595 int i;
6596 int full;
6597 isl_pw_multi_aff *res;
6598 isl_set *todo;
6600 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6601 if (!map || (!full && !dom))
6602 goto error;
6604 if (isl_map_plain_is_empty(map)) {
6605 if (empty)
6606 *empty = dom;
6607 else
6608 isl_set_free(dom);
6609 return isl_pw_multi_aff_from_map(map);
6612 res = basic_map_partial_lexopt_pw_multi_aff(
6613 isl_basic_map_copy(map->p[0]),
6614 isl_set_copy(dom), empty, flags);
6616 if (empty)
6617 todo = *empty;
6618 for (i = 1; i < map->n; ++i) {
6619 isl_pw_multi_aff *res_i;
6621 res_i = basic_map_partial_lexopt_pw_multi_aff(
6622 isl_basic_map_copy(map->p[i]),
6623 isl_set_copy(dom), empty, flags);
6625 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6626 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6627 else
6628 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6630 if (empty)
6631 todo = isl_set_intersect(todo, *empty);
6634 isl_set_free(dom);
6635 isl_map_free(map);
6637 if (empty)
6638 *empty = todo;
6640 return res;
6641 error:
6642 if (empty)
6643 *empty = NULL;
6644 isl_set_free(dom);
6645 isl_map_free(map);
6646 return NULL;
6649 #undef TYPE
6650 #define TYPE isl_map
6651 #undef SUFFIX
6652 #define SUFFIX
6653 #undef EMPTY
6654 #define EMPTY isl_map_empty
6655 #undef ADD
6656 #define ADD isl_map_union_disjoint
6657 #include "isl_map_lexopt_templ.c"
6659 /* Given a map "map", compute the lexicographically minimal
6660 * (or maximal) image element for each domain element in "dom",
6661 * in the form of an isl_map.
6662 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6663 * do not have an image element.
6664 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6665 * should be computed over the domain of "map". "empty" is also NULL
6666 * in this case.
6668 * If the input consists of more than one disjunct, then first
6669 * compute the desired result in the form of an isl_pw_multi_aff and
6670 * then convert that into an isl_map.
6672 * This function used to have an explicit implementation in terms
6673 * of isl_maps, but it would continually intersect the domains of
6674 * partial results with the complement of the domain of the next
6675 * partial solution, potentially leading to an explosion in the number
6676 * of disjuncts if there are several disjuncts in the input.
6677 * An even earlier implementation of this function would look for
6678 * better results in the domain of the partial result and for extra
6679 * results in the complement of this domain, which would lead to
6680 * even more splintering.
6682 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6683 __isl_take isl_map *map, __isl_take isl_set *dom,
6684 __isl_give isl_set **empty, unsigned flags)
6686 int full;
6687 struct isl_map *res;
6688 isl_pw_multi_aff *pma;
6690 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6691 if (!map || (!full && !dom))
6692 goto error;
6694 if (isl_map_plain_is_empty(map)) {
6695 if (empty)
6696 *empty = dom;
6697 else
6698 isl_set_free(dom);
6699 return map;
6702 if (map->n == 1) {
6703 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6704 dom, empty, flags);
6705 isl_map_free(map);
6706 return res;
6709 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6710 flags);
6711 return isl_map_from_pw_multi_aff(pma);
6712 error:
6713 if (empty)
6714 *empty = NULL;
6715 isl_set_free(dom);
6716 isl_map_free(map);
6717 return NULL;
6720 __isl_give isl_map *isl_map_partial_lexmax(
6721 __isl_take isl_map *map, __isl_take isl_set *dom,
6722 __isl_give isl_set **empty)
6724 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6727 __isl_give isl_map *isl_map_partial_lexmin(
6728 __isl_take isl_map *map, __isl_take isl_set *dom,
6729 __isl_give isl_set **empty)
6731 return isl_map_partial_lexopt(map, dom, empty, 0);
6734 __isl_give isl_set *isl_set_partial_lexmin(
6735 __isl_take isl_set *set, __isl_take isl_set *dom,
6736 __isl_give isl_set **empty)
6738 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6739 dom, empty));
6742 __isl_give isl_set *isl_set_partial_lexmax(
6743 __isl_take isl_set *set, __isl_take isl_set *dom,
6744 __isl_give isl_set **empty)
6746 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6747 dom, empty));
6750 /* Compute the lexicographic minimum (or maximum if "flags" includes
6751 * ISL_OPT_MAX) of "bset" over its parametric domain.
6753 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6754 unsigned flags)
6756 return isl_basic_map_lexopt(bset, flags);
6759 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6761 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6764 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6766 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6769 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6771 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6774 /* Compute the lexicographic minimum of "bset" over its parametric domain
6775 * for the purpose of quantifier elimination.
6776 * That is, find an explicit representation for all the existentially
6777 * quantified variables in "bset" by computing their lexicographic
6778 * minimum.
6780 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6781 __isl_take isl_basic_set *bset)
6783 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6786 /* Given a basic map with one output dimension, compute the minimum or
6787 * maximum of that dimension as an isl_pw_aff.
6789 * Compute the optimum as a lexicographic optimum over the single
6790 * output dimension and extract the single isl_pw_aff from the result.
6792 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6793 int max)
6795 isl_pw_multi_aff *pma;
6796 isl_pw_aff *pwaff;
6798 bmap = isl_basic_map_copy(bmap);
6799 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6800 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6801 isl_pw_multi_aff_free(pma);
6803 return pwaff;
6806 /* Compute the minimum or maximum of the given output dimension
6807 * as a function of the parameters and the input dimensions,
6808 * but independently of the other output dimensions.
6810 * We first project out the other output dimension and then compute
6811 * the "lexicographic" maximum in each basic map, combining the results
6812 * using isl_pw_aff_union_max.
6814 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6815 int max)
6817 int i;
6818 isl_pw_aff *pwaff;
6819 unsigned n_out;
6821 n_out = isl_map_dim(map, isl_dim_out);
6822 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6823 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6824 if (!map)
6825 return NULL;
6827 if (map->n == 0) {
6828 isl_space *space = isl_map_get_space(map);
6829 isl_map_free(map);
6830 return isl_pw_aff_empty(space);
6833 pwaff = basic_map_dim_opt(map->p[0], max);
6834 for (i = 1; i < map->n; ++i) {
6835 isl_pw_aff *pwaff_i;
6837 pwaff_i = basic_map_dim_opt(map->p[i], max);
6838 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6841 isl_map_free(map);
6843 return pwaff;
6846 /* Compute the minimum of the given output dimension as a function of the
6847 * parameters and input dimensions, but independently of
6848 * the other output dimensions.
6850 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6852 return map_dim_opt(map, pos, 0);
6855 /* Compute the maximum of the given output dimension as a function of the
6856 * parameters and input dimensions, but independently of
6857 * the other output dimensions.
6859 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6861 return map_dim_opt(map, pos, 1);
6864 /* Compute the minimum or maximum of the given set dimension
6865 * as a function of the parameters,
6866 * but independently of the other set dimensions.
6868 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6869 int max)
6871 return map_dim_opt(set, pos, max);
6874 /* Compute the maximum of the given set dimension as a function of the
6875 * parameters, but independently of the other set dimensions.
6877 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6879 return set_dim_opt(set, pos, 1);
6882 /* Compute the minimum of the given set dimension as a function of the
6883 * parameters, but independently of the other set dimensions.
6885 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6887 return set_dim_opt(set, pos, 0);
6890 /* Apply a preimage specified by "mat" on the parameters of "bset".
6891 * bset is assumed to have only parameters and divs.
6893 static __isl_give isl_basic_set *basic_set_parameter_preimage(
6894 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
6896 unsigned nparam;
6898 if (!bset || !mat)
6899 goto error;
6901 bset->dim = isl_space_cow(bset->dim);
6902 if (!bset->dim)
6903 goto error;
6905 nparam = isl_basic_set_dim(bset, isl_dim_param);
6907 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6909 bset->dim->nparam = 0;
6910 bset->dim->n_out = nparam;
6911 bset = isl_basic_set_preimage(bset, mat);
6912 if (bset) {
6913 bset->dim->nparam = bset->dim->n_out;
6914 bset->dim->n_out = 0;
6916 return bset;
6917 error:
6918 isl_mat_free(mat);
6919 isl_basic_set_free(bset);
6920 return NULL;
6923 /* Apply a preimage specified by "mat" on the parameters of "set".
6924 * set is assumed to have only parameters and divs.
6926 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6927 __isl_take isl_mat *mat)
6929 isl_space *space;
6930 unsigned nparam;
6932 if (!set || !mat)
6933 goto error;
6935 nparam = isl_set_dim(set, isl_dim_param);
6937 if (mat->n_row != 1 + nparam)
6938 isl_die(isl_set_get_ctx(set), isl_error_internal,
6939 "unexpected number of rows", goto error);
6941 space = isl_set_get_space(set);
6942 space = isl_space_move_dims(space, isl_dim_set, 0,
6943 isl_dim_param, 0, nparam);
6944 set = isl_set_reset_space(set, space);
6945 set = isl_set_preimage(set, mat);
6946 nparam = isl_set_dim(set, isl_dim_out);
6947 space = isl_set_get_space(set);
6948 space = isl_space_move_dims(space, isl_dim_param, 0,
6949 isl_dim_out, 0, nparam);
6950 set = isl_set_reset_space(set, space);
6951 return set;
6952 error:
6953 isl_mat_free(mat);
6954 isl_set_free(set);
6955 return NULL;
6958 /* Intersect the basic set "bset" with the affine space specified by the
6959 * equalities in "eq".
6961 static __isl_give isl_basic_set *basic_set_append_equalities(
6962 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
6964 int i, k;
6965 unsigned len;
6967 if (!bset || !eq)
6968 goto error;
6970 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6971 eq->n_row, 0);
6972 if (!bset)
6973 goto error;
6975 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
6976 for (i = 0; i < eq->n_row; ++i) {
6977 k = isl_basic_set_alloc_equality(bset);
6978 if (k < 0)
6979 goto error;
6980 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6981 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6983 isl_mat_free(eq);
6985 bset = isl_basic_set_gauss(bset, NULL);
6986 bset = isl_basic_set_finalize(bset);
6988 return bset;
6989 error:
6990 isl_mat_free(eq);
6991 isl_basic_set_free(bset);
6992 return NULL;
6995 /* Intersect the set "set" with the affine space specified by the
6996 * equalities in "eq".
6998 static struct isl_set *set_append_equalities(struct isl_set *set,
6999 struct isl_mat *eq)
7001 int i;
7003 if (!set || !eq)
7004 goto error;
7006 for (i = 0; i < set->n; ++i) {
7007 set->p[i] = basic_set_append_equalities(set->p[i],
7008 isl_mat_copy(eq));
7009 if (!set->p[i])
7010 goto error;
7012 isl_mat_free(eq);
7013 return set;
7014 error:
7015 isl_mat_free(eq);
7016 isl_set_free(set);
7017 return NULL;
7020 /* Given a basic set "bset" that only involves parameters and existentially
7021 * quantified variables, return the index of the first equality
7022 * that only involves parameters. If there is no such equality then
7023 * return bset->n_eq.
7025 * This function assumes that isl_basic_set_gauss has been called on "bset".
7027 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7029 int i, j;
7030 unsigned nparam, n_div;
7032 if (!bset)
7033 return -1;
7035 nparam = isl_basic_set_dim(bset, isl_dim_param);
7036 n_div = isl_basic_set_dim(bset, isl_dim_div);
7038 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7039 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7040 ++i;
7043 return i;
7046 /* Compute an explicit representation for the existentially quantified
7047 * variables in "bset" by computing the "minimal value" of the set
7048 * variables. Since there are no set variables, the computation of
7049 * the minimal value essentially computes an explicit representation
7050 * of the non-empty part(s) of "bset".
7052 * The input only involves parameters and existentially quantified variables.
7053 * All equalities among parameters have been removed.
7055 * Since the existentially quantified variables in the result are in general
7056 * going to be different from those in the input, we first replace
7057 * them by the minimal number of variables based on their equalities.
7058 * This should simplify the parametric integer programming.
7060 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7062 isl_morph *morph1, *morph2;
7063 isl_set *set;
7064 unsigned n;
7066 if (!bset)
7067 return NULL;
7068 if (bset->n_eq == 0)
7069 return isl_basic_set_lexmin_compute_divs(bset);
7071 morph1 = isl_basic_set_parameter_compression(bset);
7072 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7073 bset = isl_basic_set_lift(bset);
7074 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7075 bset = isl_morph_basic_set(morph2, bset);
7076 n = isl_basic_set_dim(bset, isl_dim_set);
7077 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7079 set = isl_basic_set_lexmin_compute_divs(bset);
7081 set = isl_morph_set(isl_morph_inverse(morph1), set);
7083 return set;
7086 /* Project the given basic set onto its parameter domain, possibly introducing
7087 * new, explicit, existential variables in the constraints.
7088 * The input has parameters and (possibly implicit) existential variables.
7089 * The output has the same parameters, but only
7090 * explicit existentially quantified variables.
7092 * The actual projection is performed by pip, but pip doesn't seem
7093 * to like equalities very much, so we first remove the equalities
7094 * among the parameters by performing a variable compression on
7095 * the parameters. Afterward, an inverse transformation is performed
7096 * and the equalities among the parameters are inserted back in.
7098 * The variable compression on the parameters may uncover additional
7099 * equalities that were only implicit before. We therefore check
7100 * if there are any new parameter equalities in the result and
7101 * if so recurse. The removal of parameter equalities is required
7102 * for the parameter compression performed by base_compute_divs.
7104 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7106 int i;
7107 struct isl_mat *eq;
7108 struct isl_mat *T, *T2;
7109 struct isl_set *set;
7110 unsigned nparam;
7112 bset = isl_basic_set_cow(bset);
7113 if (!bset)
7114 return NULL;
7116 if (bset->n_eq == 0)
7117 return base_compute_divs(bset);
7119 bset = isl_basic_set_gauss(bset, NULL);
7120 if (!bset)
7121 return NULL;
7122 if (isl_basic_set_plain_is_empty(bset))
7123 return isl_set_from_basic_set(bset);
7125 i = first_parameter_equality(bset);
7126 if (i == bset->n_eq)
7127 return base_compute_divs(bset);
7129 nparam = isl_basic_set_dim(bset, isl_dim_param);
7130 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7131 0, 1 + nparam);
7132 eq = isl_mat_cow(eq);
7133 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7134 if (T && T->n_col == 0) {
7135 isl_mat_free(T);
7136 isl_mat_free(T2);
7137 isl_mat_free(eq);
7138 bset = isl_basic_set_set_to_empty(bset);
7139 return isl_set_from_basic_set(bset);
7141 bset = basic_set_parameter_preimage(bset, T);
7143 i = first_parameter_equality(bset);
7144 if (!bset)
7145 set = NULL;
7146 else if (i == bset->n_eq)
7147 set = base_compute_divs(bset);
7148 else
7149 set = parameter_compute_divs(bset);
7150 set = set_parameter_preimage(set, T2);
7151 set = set_append_equalities(set, eq);
7152 return set;
7155 /* Insert the divs from "ls" before those of "bmap".
7157 * The number of columns is not changed, which means that the last
7158 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7159 * The caller is responsible for removing the same number of dimensions
7160 * from the space of "bmap".
7162 static __isl_give isl_basic_map *insert_divs_from_local_space(
7163 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7165 int i;
7166 int n_div;
7167 int old_n_div;
7169 n_div = isl_local_space_dim(ls, isl_dim_div);
7170 if (n_div == 0)
7171 return bmap;
7173 old_n_div = bmap->n_div;
7174 bmap = insert_div_rows(bmap, n_div);
7175 if (!bmap)
7176 return NULL;
7178 for (i = 0; i < n_div; ++i) {
7179 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7180 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7183 return bmap;
7186 /* Replace the space of "bmap" by the space and divs of "ls".
7188 * If "ls" has any divs, then we simplify the result since we may
7189 * have discovered some additional equalities that could simplify
7190 * the div expressions.
7192 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7193 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7195 int n_div;
7197 bmap = isl_basic_map_cow(bmap);
7198 if (!bmap || !ls)
7199 goto error;
7201 n_div = isl_local_space_dim(ls, isl_dim_div);
7202 bmap = insert_divs_from_local_space(bmap, ls);
7203 if (!bmap)
7204 goto error;
7206 isl_space_free(bmap->dim);
7207 bmap->dim = isl_local_space_get_space(ls);
7208 if (!bmap->dim)
7209 goto error;
7211 isl_local_space_free(ls);
7212 if (n_div > 0)
7213 bmap = isl_basic_map_simplify(bmap);
7214 bmap = isl_basic_map_finalize(bmap);
7215 return bmap;
7216 error:
7217 isl_basic_map_free(bmap);
7218 isl_local_space_free(ls);
7219 return NULL;
7222 /* Replace the space of "map" by the space and divs of "ls".
7224 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7225 __isl_take isl_local_space *ls)
7227 int i;
7229 map = isl_map_cow(map);
7230 if (!map || !ls)
7231 goto error;
7233 for (i = 0; i < map->n; ++i) {
7234 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7235 isl_local_space_copy(ls));
7236 if (!map->p[i])
7237 goto error;
7239 isl_space_free(map->dim);
7240 map->dim = isl_local_space_get_space(ls);
7241 if (!map->dim)
7242 goto error;
7244 isl_local_space_free(ls);
7245 return map;
7246 error:
7247 isl_local_space_free(ls);
7248 isl_map_free(map);
7249 return NULL;
7252 /* Compute an explicit representation for the existentially
7253 * quantified variables for which do not know any explicit representation yet.
7255 * We first sort the existentially quantified variables so that the
7256 * existentially quantified variables for which we already have an explicit
7257 * representation are placed before those for which we do not.
7258 * The input dimensions, the output dimensions and the existentially
7259 * quantified variables for which we already have an explicit
7260 * representation are then turned into parameters.
7261 * compute_divs returns a map with the same parameters and
7262 * no input or output dimensions and the dimension specification
7263 * is reset to that of the input, including the existentially quantified
7264 * variables for which we already had an explicit representation.
7266 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7268 struct isl_basic_set *bset;
7269 struct isl_set *set;
7270 struct isl_map *map;
7271 isl_space *space;
7272 isl_local_space *ls;
7273 unsigned nparam;
7274 unsigned n_in;
7275 unsigned n_out;
7276 int n_known;
7277 int i;
7279 bmap = isl_basic_map_sort_divs(bmap);
7280 bmap = isl_basic_map_cow(bmap);
7281 if (!bmap)
7282 return NULL;
7284 n_known = isl_basic_map_first_unknown_div(bmap);
7285 if (n_known < 0)
7286 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7288 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7289 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7290 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7291 space = isl_space_set_alloc(bmap->ctx,
7292 nparam + n_in + n_out + n_known, 0);
7293 if (!space)
7294 goto error;
7296 ls = isl_basic_map_get_local_space(bmap);
7297 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7298 n_known, bmap->n_div - n_known);
7299 if (n_known > 0) {
7300 for (i = n_known; i < bmap->n_div; ++i)
7301 swap_div(bmap, i - n_known, i);
7302 bmap->n_div -= n_known;
7303 bmap->extra -= n_known;
7305 bmap = isl_basic_map_reset_space(bmap, space);
7306 bset = bset_from_bmap(bmap);
7308 set = parameter_compute_divs(bset);
7309 map = set_to_map(set);
7310 map = replace_space_by_local_space(map, ls);
7312 return map;
7313 error:
7314 isl_basic_map_free(bmap);
7315 return NULL;
7318 /* Remove the explicit representation of local variable "div",
7319 * if there is any.
7321 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7322 __isl_take isl_basic_map *bmap, int div)
7324 isl_bool unknown;
7326 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7327 if (unknown < 0)
7328 return isl_basic_map_free(bmap);
7329 if (unknown)
7330 return bmap;
7332 bmap = isl_basic_map_cow(bmap);
7333 if (!bmap)
7334 return NULL;
7335 isl_int_set_si(bmap->div[div][0], 0);
7336 return bmap;
7339 /* Is local variable "div" of "bmap" marked as not having an explicit
7340 * representation?
7341 * Note that even if "div" is not marked in this way and therefore
7342 * has an explicit representation, this representation may still
7343 * depend (indirectly) on other local variables that do not
7344 * have an explicit representation.
7346 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7347 int div)
7349 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7350 return isl_bool_error;
7351 return isl_int_is_zero(bmap->div[div][0]);
7354 /* Return the position of the first local variable that does not
7355 * have an explicit representation.
7356 * Return the total number of local variables if they all have
7357 * an explicit representation.
7358 * Return -1 on error.
7360 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7362 int i;
7364 if (!bmap)
7365 return -1;
7367 for (i = 0; i < bmap->n_div; ++i) {
7368 if (!isl_basic_map_div_is_known(bmap, i))
7369 return i;
7371 return bmap->n_div;
7374 /* Return the position of the first local variable that does not
7375 * have an explicit representation.
7376 * Return the total number of local variables if they all have
7377 * an explicit representation.
7378 * Return -1 on error.
7380 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7382 return isl_basic_map_first_unknown_div(bset);
7385 /* Does "bmap" have an explicit representation for all local variables?
7387 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7389 int first, n;
7391 n = isl_basic_map_dim(bmap, isl_dim_div);
7392 first = isl_basic_map_first_unknown_div(bmap);
7393 if (first < 0)
7394 return isl_bool_error;
7395 return first == n;
7398 /* Do all basic maps in "map" have an explicit representation
7399 * for all local variables?
7401 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7403 int i;
7405 if (!map)
7406 return isl_bool_error;
7408 for (i = 0; i < map->n; ++i) {
7409 int known = isl_basic_map_divs_known(map->p[i]);
7410 if (known <= 0)
7411 return known;
7414 return isl_bool_true;
7417 /* If bmap contains any unknown divs, then compute explicit
7418 * expressions for them. However, this computation may be
7419 * quite expensive, so first try to remove divs that aren't
7420 * strictly needed.
7422 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7424 int known;
7425 struct isl_map *map;
7427 known = isl_basic_map_divs_known(bmap);
7428 if (known < 0)
7429 goto error;
7430 if (known)
7431 return isl_map_from_basic_map(bmap);
7433 bmap = isl_basic_map_drop_redundant_divs(bmap);
7435 known = isl_basic_map_divs_known(bmap);
7436 if (known < 0)
7437 goto error;
7438 if (known)
7439 return isl_map_from_basic_map(bmap);
7441 map = compute_divs(bmap);
7442 return map;
7443 error:
7444 isl_basic_map_free(bmap);
7445 return NULL;
7448 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7450 int i;
7451 int known;
7452 struct isl_map *res;
7454 if (!map)
7455 return NULL;
7456 if (map->n == 0)
7457 return map;
7459 known = isl_map_divs_known(map);
7460 if (known < 0) {
7461 isl_map_free(map);
7462 return NULL;
7464 if (known)
7465 return map;
7467 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7468 for (i = 1 ; i < map->n; ++i) {
7469 struct isl_map *r2;
7470 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7471 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7472 res = isl_map_union_disjoint(res, r2);
7473 else
7474 res = isl_map_union(res, r2);
7476 isl_map_free(map);
7478 return res;
7481 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7483 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7486 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7488 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7491 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7493 int i;
7494 struct isl_set *set;
7496 if (!map)
7497 goto error;
7499 map = isl_map_cow(map);
7500 if (!map)
7501 return NULL;
7503 set = set_from_map(map);
7504 set->dim = isl_space_domain(set->dim);
7505 if (!set->dim)
7506 goto error;
7507 for (i = 0; i < map->n; ++i) {
7508 set->p[i] = isl_basic_map_domain(map->p[i]);
7509 if (!set->p[i])
7510 goto error;
7512 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7513 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7514 return set;
7515 error:
7516 isl_map_free(map);
7517 return NULL;
7520 /* Return the union of "map1" and "map2", where we assume for now that
7521 * "map1" and "map2" are disjoint. Note that the basic maps inside
7522 * "map1" or "map2" may not be disjoint from each other.
7523 * Also note that this function is also called from isl_map_union,
7524 * which takes care of handling the situation where "map1" and "map2"
7525 * may not be disjoint.
7527 * If one of the inputs is empty, we can simply return the other input.
7528 * Similarly, if one of the inputs is universal, then it is equal to the union.
7530 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7531 __isl_take isl_map *map2)
7533 int i;
7534 unsigned flags = 0;
7535 struct isl_map *map = NULL;
7536 int is_universe;
7538 if (!map1 || !map2)
7539 goto error;
7541 if (!isl_space_is_equal(map1->dim, map2->dim))
7542 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7543 "spaces don't match", goto error);
7545 if (map1->n == 0) {
7546 isl_map_free(map1);
7547 return map2;
7549 if (map2->n == 0) {
7550 isl_map_free(map2);
7551 return map1;
7554 is_universe = isl_map_plain_is_universe(map1);
7555 if (is_universe < 0)
7556 goto error;
7557 if (is_universe) {
7558 isl_map_free(map2);
7559 return map1;
7562 is_universe = isl_map_plain_is_universe(map2);
7563 if (is_universe < 0)
7564 goto error;
7565 if (is_universe) {
7566 isl_map_free(map1);
7567 return map2;
7570 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7571 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7572 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7574 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7575 map1->n + map2->n, flags);
7576 if (!map)
7577 goto error;
7578 for (i = 0; i < map1->n; ++i) {
7579 map = isl_map_add_basic_map(map,
7580 isl_basic_map_copy(map1->p[i]));
7581 if (!map)
7582 goto error;
7584 for (i = 0; i < map2->n; ++i) {
7585 map = isl_map_add_basic_map(map,
7586 isl_basic_map_copy(map2->p[i]));
7587 if (!map)
7588 goto error;
7590 isl_map_free(map1);
7591 isl_map_free(map2);
7592 return map;
7593 error:
7594 isl_map_free(map);
7595 isl_map_free(map1);
7596 isl_map_free(map2);
7597 return NULL;
7600 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7601 * guaranteed to be disjoint by the caller.
7603 * Note that this functions is called from within isl_map_make_disjoint,
7604 * so we have to be careful not to touch the constraints of the inputs
7605 * in any way.
7607 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7608 __isl_take isl_map *map2)
7610 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7613 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7614 * not be disjoint. The parameters are assumed to have been aligned.
7616 * We currently simply call map_union_disjoint, the internal operation
7617 * of which does not really depend on the inputs being disjoint.
7618 * If the result contains more than one basic map, then we clear
7619 * the disjoint flag since the result may contain basic maps from
7620 * both inputs and these are not guaranteed to be disjoint.
7622 * As a special case, if "map1" and "map2" are obviously equal,
7623 * then we simply return "map1".
7625 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7626 __isl_take isl_map *map2)
7628 int equal;
7630 if (!map1 || !map2)
7631 goto error;
7633 equal = isl_map_plain_is_equal(map1, map2);
7634 if (equal < 0)
7635 goto error;
7636 if (equal) {
7637 isl_map_free(map2);
7638 return map1;
7641 map1 = map_union_disjoint(map1, map2);
7642 if (!map1)
7643 return NULL;
7644 if (map1->n > 1)
7645 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7646 return map1;
7647 error:
7648 isl_map_free(map1);
7649 isl_map_free(map2);
7650 return NULL;
7653 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7654 * not be disjoint.
7656 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7657 __isl_take isl_map *map2)
7659 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7662 __isl_give isl_set *isl_set_union_disjoint(
7663 __isl_take isl_set *set1, __isl_take isl_set *set2)
7665 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7666 set_to_map(set2)));
7669 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7671 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7674 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7675 * the results.
7677 * "map" and "set" are assumed to be compatible and non-NULL.
7679 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7680 __isl_take isl_set *set,
7681 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7682 __isl_take isl_basic_set *bset))
7684 unsigned flags = 0;
7685 struct isl_map *result;
7686 int i, j;
7688 if (isl_set_plain_is_universe(set)) {
7689 isl_set_free(set);
7690 return map;
7693 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7694 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7695 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7697 result = isl_map_alloc_space(isl_space_copy(map->dim),
7698 map->n * set->n, flags);
7699 for (i = 0; result && i < map->n; ++i)
7700 for (j = 0; j < set->n; ++j) {
7701 result = isl_map_add_basic_map(result,
7702 fn(isl_basic_map_copy(map->p[i]),
7703 isl_basic_set_copy(set->p[j])));
7704 if (!result)
7705 break;
7708 isl_map_free(map);
7709 isl_set_free(set);
7710 return result;
7713 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7714 __isl_take isl_set *set)
7716 isl_bool ok;
7718 ok = isl_map_compatible_range(map, set);
7719 if (ok < 0)
7720 goto error;
7721 if (!ok)
7722 isl_die(set->ctx, isl_error_invalid,
7723 "incompatible spaces", goto error);
7725 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7726 error:
7727 isl_map_free(map);
7728 isl_set_free(set);
7729 return NULL;
7732 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7733 __isl_take isl_set *set)
7735 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7738 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7739 __isl_take isl_set *set)
7741 isl_bool ok;
7743 ok = isl_map_compatible_domain(map, set);
7744 if (ok < 0)
7745 goto error;
7746 if (!ok)
7747 isl_die(set->ctx, isl_error_invalid,
7748 "incompatible spaces", goto error);
7750 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7751 error:
7752 isl_map_free(map);
7753 isl_set_free(set);
7754 return NULL;
7757 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7758 __isl_take isl_set *set)
7760 return isl_map_align_params_map_map_and(map, set,
7761 &map_intersect_domain);
7764 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7765 * in the space B -> C, return the intersection.
7766 * The parameters are assumed to have been aligned.
7768 * The map "factor" is first extended to a map living in the space
7769 * [A -> B] -> C and then a regular intersection is computed.
7771 static __isl_give isl_map *map_intersect_domain_factor_range(
7772 __isl_take isl_map *map, __isl_take isl_map *factor)
7774 isl_space *space;
7775 isl_map *ext_factor;
7777 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7778 ext_factor = isl_map_universe(space);
7779 ext_factor = isl_map_domain_product(ext_factor, factor);
7780 return map_intersect(map, ext_factor);
7783 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7784 * in the space B -> C, return the intersection.
7786 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7787 __isl_take isl_map *map, __isl_take isl_map *factor)
7789 return isl_map_align_params_map_map_and(map, factor,
7790 &map_intersect_domain_factor_range);
7793 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7794 * in the space A -> C, return the intersection.
7796 * The map "factor" is first extended to a map living in the space
7797 * A -> [B -> C] and then a regular intersection is computed.
7799 static __isl_give isl_map *map_intersect_range_factor_range(
7800 __isl_take isl_map *map, __isl_take isl_map *factor)
7802 isl_space *space;
7803 isl_map *ext_factor;
7805 space = isl_space_range_factor_domain(isl_map_get_space(map));
7806 ext_factor = isl_map_universe(space);
7807 ext_factor = isl_map_range_product(ext_factor, factor);
7808 return isl_map_intersect(map, ext_factor);
7811 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7812 * in the space A -> C, return the intersection.
7814 __isl_give isl_map *isl_map_intersect_range_factor_range(
7815 __isl_take isl_map *map, __isl_take isl_map *factor)
7817 return isl_map_align_params_map_map_and(map, factor,
7818 &map_intersect_range_factor_range);
7821 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7822 __isl_take isl_map *map2)
7824 if (!map1 || !map2)
7825 goto error;
7826 map1 = isl_map_reverse(map1);
7827 map1 = isl_map_apply_range(map1, map2);
7828 return isl_map_reverse(map1);
7829 error:
7830 isl_map_free(map1);
7831 isl_map_free(map2);
7832 return NULL;
7835 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7836 __isl_take isl_map *map2)
7838 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7841 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7842 __isl_take isl_map *map2)
7844 isl_space *space;
7845 struct isl_map *result;
7846 int i, j;
7848 if (!map1 || !map2)
7849 goto error;
7851 space = isl_space_join(isl_space_copy(map1->dim),
7852 isl_space_copy(map2->dim));
7854 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
7855 if (!result)
7856 goto error;
7857 for (i = 0; i < map1->n; ++i)
7858 for (j = 0; j < map2->n; ++j) {
7859 result = isl_map_add_basic_map(result,
7860 isl_basic_map_apply_range(
7861 isl_basic_map_copy(map1->p[i]),
7862 isl_basic_map_copy(map2->p[j])));
7863 if (!result)
7864 goto error;
7866 isl_map_free(map1);
7867 isl_map_free(map2);
7868 if (result && result->n <= 1)
7869 ISL_F_SET(result, ISL_MAP_DISJOINT);
7870 return result;
7871 error:
7872 isl_map_free(map1);
7873 isl_map_free(map2);
7874 return NULL;
7877 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7878 __isl_take isl_map *map2)
7880 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7884 * returns range - domain
7886 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
7888 isl_space *target_space;
7889 struct isl_basic_set *bset;
7890 unsigned dim;
7891 unsigned nparam;
7892 int i;
7894 if (!bmap)
7895 goto error;
7896 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7897 bmap->dim, isl_dim_out),
7898 goto error);
7899 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7900 dim = isl_basic_map_dim(bmap, isl_dim_in);
7901 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7902 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7903 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7904 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7905 for (i = 0; i < dim; ++i) {
7906 int j = isl_basic_map_alloc_equality(bmap);
7907 if (j < 0) {
7908 bmap = isl_basic_map_free(bmap);
7909 break;
7911 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7912 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7913 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7914 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7916 bset = isl_basic_map_domain(bmap);
7917 bset = isl_basic_set_reset_space(bset, target_space);
7918 return bset;
7919 error:
7920 isl_basic_map_free(bmap);
7921 return NULL;
7924 /* Check that domain and range of "map" are the same.
7926 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
7928 isl_space *space;
7929 isl_bool equal;
7931 space = isl_map_peek_space(map);
7932 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
7933 if (equal < 0)
7934 return isl_stat_error;
7935 if (!equal)
7936 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7937 "domain and range don't match", return isl_stat_error);
7938 return isl_stat_ok;
7942 * returns range - domain
7944 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7946 int i;
7947 isl_space *dim;
7948 struct isl_set *result;
7950 if (!map)
7951 return NULL;
7953 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7954 map->dim, isl_dim_out),
7955 goto error);
7956 dim = isl_map_get_space(map);
7957 dim = isl_space_domain(dim);
7958 result = isl_set_alloc_space(dim, map->n, 0);
7959 if (!result)
7960 goto error;
7961 for (i = 0; i < map->n; ++i)
7962 result = isl_set_add_basic_set(result,
7963 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7964 isl_map_free(map);
7965 return result;
7966 error:
7967 isl_map_free(map);
7968 return NULL;
7972 * returns [domain -> range] -> range - domain
7974 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7975 __isl_take isl_basic_map *bmap)
7977 int i, k;
7978 isl_space *space;
7979 isl_basic_map *domain;
7980 int nparam, n;
7981 unsigned total;
7983 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7984 bmap->dim, isl_dim_out))
7985 isl_die(bmap->ctx, isl_error_invalid,
7986 "domain and range don't match", goto error);
7988 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7989 n = isl_basic_map_dim(bmap, isl_dim_in);
7991 space = isl_basic_map_get_space(bmap);
7992 space = isl_space_from_range(isl_space_domain(space));
7993 domain = isl_basic_map_universe(space);
7995 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7996 bmap = isl_basic_map_apply_range(bmap, domain);
7997 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7999 total = isl_basic_map_total_dim(bmap);
8001 for (i = 0; i < n; ++i) {
8002 k = isl_basic_map_alloc_equality(bmap);
8003 if (k < 0)
8004 goto error;
8005 isl_seq_clr(bmap->eq[k], 1 + total);
8006 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8007 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8008 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8011 bmap = isl_basic_map_gauss(bmap, NULL);
8012 return isl_basic_map_finalize(bmap);
8013 error:
8014 isl_basic_map_free(bmap);
8015 return NULL;
8019 * returns [domain -> range] -> range - domain
8021 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8023 int i;
8024 isl_space *domain_space;
8026 if (isl_map_check_equal_tuples(map) < 0)
8027 return isl_map_free(map);
8029 map = isl_map_cow(map);
8030 if (!map)
8031 return NULL;
8033 domain_space = isl_space_domain(isl_map_get_space(map));
8034 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8035 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8036 if (!map->dim)
8037 goto error;
8038 for (i = 0; i < map->n; ++i) {
8039 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8040 if (!map->p[i])
8041 goto error;
8043 map = isl_map_unmark_normalized(map);
8044 return map;
8045 error:
8046 isl_map_free(map);
8047 return NULL;
8050 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8052 unsigned n_in, n_out;
8054 if (!space)
8055 return NULL;
8056 n_in = isl_space_dim(space, isl_dim_in);
8057 n_out = isl_space_dim(space, isl_dim_out);
8058 if (n_in != n_out)
8059 isl_die(space->ctx, isl_error_invalid,
8060 "number of input and output dimensions needs to be "
8061 "the same", goto error);
8062 return isl_basic_map_equal(space, n_in);
8063 error:
8064 isl_space_free(space);
8065 return NULL;
8068 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8070 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8073 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8075 isl_space *dim = isl_set_get_space(set);
8076 isl_map *id;
8077 id = isl_map_identity(isl_space_map_from_set(dim));
8078 return isl_map_intersect_range(id, set);
8081 /* Construct a basic set with all set dimensions having only non-negative
8082 * values.
8084 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8085 __isl_take isl_space *space)
8087 int i;
8088 unsigned nparam;
8089 unsigned dim;
8090 struct isl_basic_set *bset;
8092 if (!space)
8093 return NULL;
8094 nparam = isl_space_dim(space, isl_dim_param);
8095 dim = isl_space_dim(space, isl_dim_set);
8096 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8097 if (!bset)
8098 return NULL;
8099 for (i = 0; i < dim; ++i) {
8100 int k = isl_basic_set_alloc_inequality(bset);
8101 if (k < 0)
8102 goto error;
8103 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8104 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8106 return bset;
8107 error:
8108 isl_basic_set_free(bset);
8109 return NULL;
8112 /* Construct the half-space x_pos >= 0.
8114 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8115 int pos)
8117 int k;
8118 isl_basic_set *nonneg;
8120 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8121 k = isl_basic_set_alloc_inequality(nonneg);
8122 if (k < 0)
8123 goto error;
8124 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8125 isl_int_set_si(nonneg->ineq[k][pos], 1);
8127 return isl_basic_set_finalize(nonneg);
8128 error:
8129 isl_basic_set_free(nonneg);
8130 return NULL;
8133 /* Construct the half-space x_pos <= -1.
8135 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8136 int pos)
8138 int k;
8139 isl_basic_set *neg;
8141 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8142 k = isl_basic_set_alloc_inequality(neg);
8143 if (k < 0)
8144 goto error;
8145 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8146 isl_int_set_si(neg->ineq[k][0], -1);
8147 isl_int_set_si(neg->ineq[k][pos], -1);
8149 return isl_basic_set_finalize(neg);
8150 error:
8151 isl_basic_set_free(neg);
8152 return NULL;
8155 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8156 enum isl_dim_type type, unsigned first, unsigned n)
8158 int i;
8159 unsigned offset;
8160 isl_basic_set *nonneg;
8161 isl_basic_set *neg;
8163 if (n == 0)
8164 return set;
8166 if (isl_set_check_range(set, type, first, n) < 0)
8167 return isl_set_free(set);
8169 offset = pos(set->dim, type);
8170 for (i = 0; i < n; ++i) {
8171 nonneg = nonneg_halfspace(isl_set_get_space(set),
8172 offset + first + i);
8173 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8175 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8178 return set;
8181 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8182 int len,
8183 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8184 void *user)
8186 isl_set *half;
8188 if (!set)
8189 return isl_stat_error;
8190 if (isl_set_plain_is_empty(set)) {
8191 isl_set_free(set);
8192 return isl_stat_ok;
8194 if (first == len)
8195 return fn(set, signs, user);
8197 signs[first] = 1;
8198 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8199 1 + first));
8200 half = isl_set_intersect(half, isl_set_copy(set));
8201 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8202 goto error;
8204 signs[first] = -1;
8205 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8206 1 + first));
8207 half = isl_set_intersect(half, set);
8208 return foreach_orthant(half, signs, first + 1, len, fn, user);
8209 error:
8210 isl_set_free(set);
8211 return isl_stat_error;
8214 /* Call "fn" on the intersections of "set" with each of the orthants
8215 * (except for obviously empty intersections). The orthant is identified
8216 * by the signs array, with each entry having value 1 or -1 according
8217 * to the sign of the corresponding variable.
8219 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8220 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8221 void *user)
8223 unsigned nparam;
8224 unsigned nvar;
8225 int *signs;
8226 isl_stat r;
8228 if (!set)
8229 return isl_stat_error;
8230 if (isl_set_plain_is_empty(set))
8231 return isl_stat_ok;
8233 nparam = isl_set_dim(set, isl_dim_param);
8234 nvar = isl_set_dim(set, isl_dim_set);
8236 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8238 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8239 fn, user);
8241 free(signs);
8243 return r;
8246 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8248 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8251 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8252 __isl_keep isl_basic_map *bmap2)
8254 isl_bool is_subset;
8255 struct isl_map *map1;
8256 struct isl_map *map2;
8258 if (!bmap1 || !bmap2)
8259 return isl_bool_error;
8261 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8262 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8264 is_subset = isl_map_is_subset(map1, map2);
8266 isl_map_free(map1);
8267 isl_map_free(map2);
8269 return is_subset;
8272 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8273 __isl_keep isl_basic_set *bset2)
8275 return isl_basic_map_is_subset(bset1, bset2);
8278 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8279 __isl_keep isl_basic_map *bmap2)
8281 isl_bool is_subset;
8283 if (!bmap1 || !bmap2)
8284 return isl_bool_error;
8285 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8286 if (is_subset != isl_bool_true)
8287 return is_subset;
8288 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8289 return is_subset;
8292 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8293 __isl_keep isl_basic_set *bset2)
8295 return isl_basic_map_is_equal(
8296 bset_to_bmap(bset1), bset_to_bmap(bset2));
8299 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8301 int i;
8302 int is_empty;
8304 if (!map)
8305 return isl_bool_error;
8306 for (i = 0; i < map->n; ++i) {
8307 is_empty = isl_basic_map_is_empty(map->p[i]);
8308 if (is_empty < 0)
8309 return isl_bool_error;
8310 if (!is_empty)
8311 return isl_bool_false;
8313 return isl_bool_true;
8316 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8318 return map ? map->n == 0 : isl_bool_error;
8321 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8323 return set ? set->n == 0 : isl_bool_error;
8326 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8328 return isl_map_is_empty(set_to_map(set));
8331 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8332 __isl_keep isl_map *map2)
8334 if (!map1 || !map2)
8335 return isl_bool_error;
8337 return isl_space_is_equal(map1->dim, map2->dim);
8340 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8341 __isl_keep isl_set *set2)
8343 if (!set1 || !set2)
8344 return isl_bool_error;
8346 return isl_space_is_equal(set1->dim, set2->dim);
8349 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8351 isl_bool is_subset;
8353 if (!map1 || !map2)
8354 return isl_bool_error;
8355 is_subset = isl_map_is_subset(map1, map2);
8356 if (is_subset != isl_bool_true)
8357 return is_subset;
8358 is_subset = isl_map_is_subset(map2, map1);
8359 return is_subset;
8362 /* Is "map1" equal to "map2"?
8364 * First check if they are obviously equal.
8365 * If not, then perform a more detailed analysis.
8367 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8369 isl_bool equal;
8371 equal = isl_map_plain_is_equal(map1, map2);
8372 if (equal < 0 || equal)
8373 return equal;
8374 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8377 isl_bool isl_basic_map_is_strict_subset(
8378 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8380 isl_bool is_subset;
8382 if (!bmap1 || !bmap2)
8383 return isl_bool_error;
8384 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8385 if (is_subset != isl_bool_true)
8386 return is_subset;
8387 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8388 return isl_bool_not(is_subset);
8391 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8392 __isl_keep isl_map *map2)
8394 isl_bool is_subset;
8396 if (!map1 || !map2)
8397 return isl_bool_error;
8398 is_subset = isl_map_is_subset(map1, map2);
8399 if (is_subset != isl_bool_true)
8400 return is_subset;
8401 is_subset = isl_map_is_subset(map2, map1);
8402 return isl_bool_not(is_subset);
8405 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8406 __isl_keep isl_set *set2)
8408 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8411 /* Is "bmap" obviously equal to the universe with the same space?
8413 * That is, does it not have any constraints?
8415 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8417 if (!bmap)
8418 return isl_bool_error;
8419 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8422 /* Is "bset" obviously equal to the universe with the same space?
8424 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8426 return isl_basic_map_plain_is_universe(bset);
8429 /* If "c" does not involve any existentially quantified variables,
8430 * then set *univ to false and abort
8432 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8434 isl_bool *univ = user;
8435 unsigned n;
8437 n = isl_constraint_dim(c, isl_dim_div);
8438 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8439 isl_constraint_free(c);
8440 if (*univ < 0 || !*univ)
8441 return isl_stat_error;
8442 return isl_stat_ok;
8445 /* Is "bmap" equal to the universe with the same space?
8447 * First check if it is obviously equal to the universe.
8448 * If not and if there are any constraints not involving
8449 * existentially quantified variables, then it is certainly
8450 * not equal to the universe.
8451 * Otherwise, check if the universe is a subset of "bmap".
8453 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8455 isl_bool univ;
8456 isl_basic_map *test;
8458 univ = isl_basic_map_plain_is_universe(bmap);
8459 if (univ < 0 || univ)
8460 return univ;
8461 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8462 return isl_bool_false;
8463 univ = isl_bool_true;
8464 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8465 univ)
8466 return isl_bool_error;
8467 if (univ < 0 || !univ)
8468 return univ;
8469 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8470 univ = isl_basic_map_is_subset(test, bmap);
8471 isl_basic_map_free(test);
8472 return univ;
8475 /* Is "bset" equal to the universe with the same space?
8477 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8479 return isl_basic_map_is_universe(bset);
8482 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8484 int i;
8486 if (!map)
8487 return isl_bool_error;
8489 for (i = 0; i < map->n; ++i) {
8490 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8491 if (r < 0 || r)
8492 return r;
8495 return isl_bool_false;
8498 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8500 return isl_map_plain_is_universe(set_to_map(set));
8503 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8505 struct isl_basic_set *bset = NULL;
8506 struct isl_vec *sample = NULL;
8507 isl_bool empty, non_empty;
8509 if (!bmap)
8510 return isl_bool_error;
8512 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8513 return isl_bool_true;
8515 if (isl_basic_map_plain_is_universe(bmap))
8516 return isl_bool_false;
8518 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8519 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8520 copy = isl_basic_map_remove_redundancies(copy);
8521 empty = isl_basic_map_plain_is_empty(copy);
8522 isl_basic_map_free(copy);
8523 return empty;
8526 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8527 if (non_empty < 0)
8528 return isl_bool_error;
8529 if (non_empty)
8530 return isl_bool_false;
8531 isl_vec_free(bmap->sample);
8532 bmap->sample = NULL;
8533 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8534 if (!bset)
8535 return isl_bool_error;
8536 sample = isl_basic_set_sample_vec(bset);
8537 if (!sample)
8538 return isl_bool_error;
8539 empty = sample->size == 0;
8540 isl_vec_free(bmap->sample);
8541 bmap->sample = sample;
8542 if (empty)
8543 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8545 return empty;
8548 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8550 if (!bmap)
8551 return isl_bool_error;
8552 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8555 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8557 if (!bset)
8558 return isl_bool_error;
8559 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8562 /* Is "bmap" known to be non-empty?
8564 * That is, is the cached sample still valid?
8566 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8568 unsigned total;
8570 if (!bmap)
8571 return isl_bool_error;
8572 if (!bmap->sample)
8573 return isl_bool_false;
8574 total = 1 + isl_basic_map_total_dim(bmap);
8575 if (bmap->sample->size != total)
8576 return isl_bool_false;
8577 return isl_basic_map_contains(bmap, bmap->sample);
8580 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8582 return isl_basic_map_is_empty(bset_to_bmap(bset));
8585 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8586 __isl_take isl_basic_map *bmap2)
8588 struct isl_map *map;
8589 if (!bmap1 || !bmap2)
8590 goto error;
8592 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8594 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8595 if (!map)
8596 goto error;
8597 map = isl_map_add_basic_map(map, bmap1);
8598 map = isl_map_add_basic_map(map, bmap2);
8599 return map;
8600 error:
8601 isl_basic_map_free(bmap1);
8602 isl_basic_map_free(bmap2);
8603 return NULL;
8606 struct isl_set *isl_basic_set_union(
8607 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8609 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8610 bset_to_bmap(bset2)));
8613 /* Order divs such that any div only depends on previous divs */
8614 __isl_give isl_basic_map *isl_basic_map_order_divs(
8615 __isl_take isl_basic_map *bmap)
8617 int i;
8618 int off;
8620 off = isl_basic_map_var_offset(bmap, isl_dim_div);
8621 if (off < 0)
8622 return isl_basic_map_free(bmap);
8624 for (i = 0; i < bmap->n_div; ++i) {
8625 int pos;
8626 if (isl_int_is_zero(bmap->div[i][0]))
8627 continue;
8628 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8629 bmap->n_div-i);
8630 if (pos == -1)
8631 continue;
8632 if (pos == 0)
8633 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8634 "integer division depends on itself",
8635 return isl_basic_map_free(bmap));
8636 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8637 if (!bmap)
8638 return NULL;
8639 --i;
8641 return bmap;
8644 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8646 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8649 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8651 int i;
8653 if (!map)
8654 return 0;
8656 for (i = 0; i < map->n; ++i) {
8657 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8658 if (!map->p[i])
8659 goto error;
8662 return map;
8663 error:
8664 isl_map_free(map);
8665 return NULL;
8668 /* Sort the local variables of "bset".
8670 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8671 __isl_take isl_basic_set *bset)
8673 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8676 /* Apply the expansion computed by isl_merge_divs.
8677 * The expansion itself is given by "exp" while the resulting
8678 * list of divs is given by "div".
8680 * Move the integer divisions of "bmap" into the right position
8681 * according to "exp" and then introduce the additional integer
8682 * divisions, adding div constraints.
8683 * The moving should be done first to avoid moving coefficients
8684 * in the definitions of the extra integer divisions.
8686 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8687 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8689 int i, j;
8690 int n_div;
8692 bmap = isl_basic_map_cow(bmap);
8693 if (!bmap || !div)
8694 goto error;
8696 if (div->n_row < bmap->n_div)
8697 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8698 "not an expansion", goto error);
8700 n_div = bmap->n_div;
8701 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8702 div->n_row - n_div, 0,
8703 2 * (div->n_row - n_div));
8705 for (i = n_div; i < div->n_row; ++i)
8706 if (isl_basic_map_alloc_div(bmap) < 0)
8707 goto error;
8709 for (j = n_div - 1; j >= 0; --j) {
8710 if (exp[j] == j)
8711 break;
8712 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8713 if (!bmap)
8714 goto error;
8716 j = 0;
8717 for (i = 0; i < div->n_row; ++i) {
8718 if (j < n_div && exp[j] == i) {
8719 j++;
8720 } else {
8721 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8722 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8723 continue;
8724 bmap = isl_basic_map_add_div_constraints(bmap, i);
8725 if (!bmap)
8726 goto error;
8730 isl_mat_free(div);
8731 return bmap;
8732 error:
8733 isl_basic_map_free(bmap);
8734 isl_mat_free(div);
8735 return NULL;
8738 /* Apply the expansion computed by isl_merge_divs.
8739 * The expansion itself is given by "exp" while the resulting
8740 * list of divs is given by "div".
8742 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8743 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8745 return isl_basic_map_expand_divs(bset, div, exp);
8748 /* Look for a div in dst that corresponds to the div "div" in src.
8749 * The divs before "div" in src and dst are assumed to be the same.
8751 * Return the position of the corresponding div in dst
8752 * if there is one. Otherwise, return a position beyond the integer divisions.
8753 * Return -1 on error.
8755 static int find_div(__isl_keep isl_basic_map *dst,
8756 __isl_keep isl_basic_map *src, unsigned div)
8758 int i;
8759 unsigned n_div;
8760 int v_div;
8762 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8763 if (!dst || v_div < 0)
8764 return -1;
8766 n_div = isl_basic_map_dim(dst, isl_dim_div);
8767 isl_assert(dst->ctx, div <= n_div, return -1);
8768 for (i = div; i < n_div; ++i)
8769 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
8770 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
8771 n_div - div) == -1)
8772 return i;
8773 return n_div;
8776 /* Align the divs of "dst" to those of "src", adding divs from "src"
8777 * if needed. That is, make sure that the first src->n_div divs
8778 * of the result are equal to those of src.
8780 * The result is not finalized as by design it will have redundant
8781 * divs if any divs from "src" were copied.
8783 __isl_give isl_basic_map *isl_basic_map_align_divs(
8784 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8786 int i;
8787 isl_bool known;
8788 int extended;
8789 int v_div;
8790 unsigned dst_n_div;
8792 if (!dst || !src)
8793 return isl_basic_map_free(dst);
8795 if (src->n_div == 0)
8796 return dst;
8798 known = isl_basic_map_divs_known(src);
8799 if (known < 0)
8800 return isl_basic_map_free(dst);
8801 if (!known)
8802 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8803 "some src divs are unknown",
8804 return isl_basic_map_free(dst));
8806 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8807 if (v_div < 0)
8808 return isl_basic_map_free(dst);
8810 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8811 if (!src)
8812 return isl_basic_map_free(dst);
8814 extended = 0;
8815 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
8816 for (i = 0; i < src->n_div; ++i) {
8817 int j = find_div(dst, src, i);
8818 if (j < 0)
8819 dst = isl_basic_map_free(dst);
8820 if (j == dst_n_div) {
8821 if (!extended) {
8822 int extra = src->n_div - i;
8823 dst = isl_basic_map_cow(dst);
8824 if (!dst)
8825 goto error;
8826 dst = isl_basic_map_extend_space(dst,
8827 isl_space_copy(dst->dim),
8828 extra, 0, 2 * extra);
8829 extended = 1;
8831 j = isl_basic_map_alloc_div(dst);
8832 if (j < 0)
8833 goto error;
8834 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
8835 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
8836 dst_n_div++;
8837 dst = isl_basic_map_add_div_constraints(dst, j);
8838 if (!dst)
8839 goto error;
8841 if (j != i)
8842 dst = isl_basic_map_swap_div(dst, i, j);
8843 if (!dst)
8844 goto error;
8846 isl_basic_map_free(src);
8847 return dst;
8848 error:
8849 isl_basic_map_free(src);
8850 isl_basic_map_free(dst);
8851 return NULL;
8854 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8856 int i;
8858 if (!map)
8859 return NULL;
8860 if (map->n == 0)
8861 return map;
8862 map = isl_map_compute_divs(map);
8863 map = isl_map_cow(map);
8864 if (!map)
8865 return NULL;
8867 for (i = 1; i < map->n; ++i)
8868 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8869 for (i = 1; i < map->n; ++i) {
8870 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8871 if (!map->p[i])
8872 return isl_map_free(map);
8875 map = isl_map_unmark_normalized(map);
8876 return map;
8879 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8881 return isl_map_align_divs_internal(map);
8884 struct isl_set *isl_set_align_divs(struct isl_set *set)
8886 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8889 /* Align the divs of the basic maps in "map" to those
8890 * of the basic maps in "list", as well as to the other basic maps in "map".
8891 * The elements in "list" are assumed to have known divs.
8893 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8894 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8896 int i, n;
8898 map = isl_map_compute_divs(map);
8899 map = isl_map_cow(map);
8900 if (!map || !list)
8901 return isl_map_free(map);
8902 if (map->n == 0)
8903 return map;
8905 n = isl_basic_map_list_n_basic_map(list);
8906 for (i = 0; i < n; ++i) {
8907 isl_basic_map *bmap;
8909 bmap = isl_basic_map_list_get_basic_map(list, i);
8910 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8911 isl_basic_map_free(bmap);
8913 if (!map->p[0])
8914 return isl_map_free(map);
8916 return isl_map_align_divs_internal(map);
8919 /* Align the divs of each element of "list" to those of "bmap".
8920 * Both "bmap" and the elements of "list" are assumed to have known divs.
8922 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8923 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8925 int i, n;
8927 if (!list || !bmap)
8928 return isl_basic_map_list_free(list);
8930 n = isl_basic_map_list_n_basic_map(list);
8931 for (i = 0; i < n; ++i) {
8932 isl_basic_map *bmap_i;
8934 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8935 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8936 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8939 return list;
8942 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8943 __isl_take isl_map *map)
8945 isl_bool ok;
8947 ok = isl_map_compatible_domain(map, set);
8948 if (ok < 0)
8949 goto error;
8950 if (!ok)
8951 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8952 "incompatible spaces", goto error);
8953 map = isl_map_intersect_domain(map, set);
8954 set = isl_map_range(map);
8955 return set;
8956 error:
8957 isl_set_free(set);
8958 isl_map_free(map);
8959 return NULL;
8962 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8963 __isl_take isl_map *map)
8965 return isl_map_align_params_map_map_and(set, map, &set_apply);
8968 /* There is no need to cow as removing empty parts doesn't change
8969 * the meaning of the set.
8971 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
8973 int i;
8975 if (!map)
8976 return NULL;
8978 for (i = map->n - 1; i >= 0; --i)
8979 map = remove_if_empty(map, i);
8981 return map;
8984 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8986 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8989 /* Create a binary relation that maps the shared initial "pos" dimensions
8990 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
8992 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
8993 __isl_keep isl_basic_set *bset2, int pos)
8995 isl_basic_map *bmap1;
8996 isl_basic_map *bmap2;
8998 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8999 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9000 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9001 isl_dim_out, 0, pos);
9002 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9003 isl_dim_out, 0, pos);
9004 return isl_basic_map_range_product(bmap1, bmap2);
9007 /* Given two basic sets bset1 and bset2, compute the maximal difference
9008 * between the values of dimension pos in bset1 and those in bset2
9009 * for any common value of the parameters and dimensions preceding pos.
9011 static enum isl_lp_result basic_set_maximal_difference_at(
9012 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9013 int pos, isl_int *opt)
9015 isl_basic_map *bmap1;
9016 struct isl_ctx *ctx;
9017 struct isl_vec *obj;
9018 unsigned total;
9019 unsigned nparam;
9020 unsigned dim1;
9021 enum isl_lp_result res;
9023 if (!bset1 || !bset2)
9024 return isl_lp_error;
9026 nparam = isl_basic_set_n_param(bset1);
9027 dim1 = isl_basic_set_n_dim(bset1);
9029 bmap1 = join_initial(bset1, bset2, pos);
9030 if (!bmap1)
9031 return isl_lp_error;
9033 total = isl_basic_map_total_dim(bmap1);
9034 ctx = bmap1->ctx;
9035 obj = isl_vec_alloc(ctx, 1 + total);
9036 if (!obj)
9037 goto error;
9038 isl_seq_clr(obj->block.data, 1 + total);
9039 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9040 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9041 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9042 opt, NULL, NULL);
9043 isl_basic_map_free(bmap1);
9044 isl_vec_free(obj);
9045 return res;
9046 error:
9047 isl_basic_map_free(bmap1);
9048 return isl_lp_error;
9051 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9052 * for any common value of the parameters and dimensions preceding pos
9053 * in both basic sets, the values of dimension pos in bset1 are
9054 * smaller or larger than those in bset2.
9056 * Returns
9057 * 1 if bset1 follows bset2
9058 * -1 if bset1 precedes bset2
9059 * 0 if bset1 and bset2 are incomparable
9060 * -2 if some error occurred.
9062 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9063 __isl_keep isl_basic_set *bset2, int pos)
9065 isl_int opt;
9066 enum isl_lp_result res;
9067 int cmp;
9069 isl_int_init(opt);
9071 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9073 if (res == isl_lp_empty)
9074 cmp = 0;
9075 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9076 res == isl_lp_unbounded)
9077 cmp = 1;
9078 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9079 cmp = -1;
9080 else
9081 cmp = -2;
9083 isl_int_clear(opt);
9084 return cmp;
9087 /* Given two basic sets bset1 and bset2, check whether
9088 * for any common value of the parameters and dimensions preceding pos
9089 * there is a value of dimension pos in bset1 that is larger
9090 * than a value of the same dimension in bset2.
9092 * Return
9093 * 1 if there exists such a pair
9094 * 0 if there is no such pair, but there is a pair of equal values
9095 * -1 otherwise
9096 * -2 if some error occurred.
9098 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9099 __isl_keep isl_basic_set *bset2, int pos)
9101 isl_bool empty;
9102 isl_basic_map *bmap;
9103 unsigned dim1;
9105 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9106 bmap = join_initial(bset1, bset2, pos);
9107 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9108 isl_dim_out, dim1 - pos);
9109 empty = isl_basic_map_is_empty(bmap);
9110 if (empty < 0)
9111 goto error;
9112 if (empty) {
9113 isl_basic_map_free(bmap);
9114 return -1;
9116 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9117 isl_dim_out, dim1 - pos);
9118 empty = isl_basic_map_is_empty(bmap);
9119 if (empty < 0)
9120 goto error;
9121 isl_basic_map_free(bmap);
9122 if (empty)
9123 return 0;
9124 return 1;
9125 error:
9126 isl_basic_map_free(bmap);
9127 return -2;
9130 /* Given two sets set1 and set2, check whether
9131 * for any common value of the parameters and dimensions preceding pos
9132 * there is a value of dimension pos in set1 that is larger
9133 * than a value of the same dimension in set2.
9135 * Return
9136 * 1 if there exists such a pair
9137 * 0 if there is no such pair, but there is a pair of equal values
9138 * -1 otherwise
9139 * -2 if some error occurred.
9141 int isl_set_follows_at(__isl_keep isl_set *set1,
9142 __isl_keep isl_set *set2, int pos)
9144 int i, j;
9145 int follows = -1;
9147 if (!set1 || !set2)
9148 return -2;
9150 for (i = 0; i < set1->n; ++i)
9151 for (j = 0; j < set2->n; ++j) {
9152 int f;
9153 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9154 if (f == 1 || f == -2)
9155 return f;
9156 if (f > follows)
9157 follows = f;
9160 return follows;
9163 static isl_bool isl_basic_map_plain_has_fixed_var(
9164 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9166 int i;
9167 int d;
9168 unsigned total;
9170 if (!bmap)
9171 return isl_bool_error;
9172 total = isl_basic_map_total_dim(bmap);
9173 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9174 for (; d+1 > pos; --d)
9175 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9176 break;
9177 if (d != pos)
9178 continue;
9179 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9180 return isl_bool_false;
9181 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9182 return isl_bool_false;
9183 if (!isl_int_is_one(bmap->eq[i][1+d]))
9184 return isl_bool_false;
9185 if (val)
9186 isl_int_neg(*val, bmap->eq[i][0]);
9187 return isl_bool_true;
9189 return isl_bool_false;
9192 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9193 unsigned pos, isl_int *val)
9195 int i;
9196 isl_int v;
9197 isl_int tmp;
9198 isl_bool fixed;
9200 if (!map)
9201 return isl_bool_error;
9202 if (map->n == 0)
9203 return isl_bool_false;
9204 if (map->n == 1)
9205 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9206 isl_int_init(v);
9207 isl_int_init(tmp);
9208 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9209 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9210 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9211 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9212 fixed = isl_bool_false;
9214 if (val)
9215 isl_int_set(*val, v);
9216 isl_int_clear(tmp);
9217 isl_int_clear(v);
9218 return fixed;
9221 static isl_bool isl_basic_set_plain_has_fixed_var(
9222 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9224 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9225 pos, val);
9228 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9229 enum isl_dim_type type, unsigned pos, isl_int *val)
9231 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9232 return isl_bool_error;
9233 return isl_basic_map_plain_has_fixed_var(bmap,
9234 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9237 /* If "bmap" obviously lies on a hyperplane where the given dimension
9238 * has a fixed value, then return that value.
9239 * Otherwise return NaN.
9241 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9242 __isl_keep isl_basic_map *bmap,
9243 enum isl_dim_type type, unsigned pos)
9245 isl_ctx *ctx;
9246 isl_val *v;
9247 isl_bool fixed;
9249 if (!bmap)
9250 return NULL;
9251 ctx = isl_basic_map_get_ctx(bmap);
9252 v = isl_val_alloc(ctx);
9253 if (!v)
9254 return NULL;
9255 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9256 if (fixed < 0)
9257 return isl_val_free(v);
9258 if (fixed) {
9259 isl_int_set_si(v->d, 1);
9260 return v;
9262 isl_val_free(v);
9263 return isl_val_nan(ctx);
9266 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9267 enum isl_dim_type type, unsigned pos, isl_int *val)
9269 if (isl_map_check_range(map, type, pos, 1) < 0)
9270 return isl_bool_error;
9271 return isl_map_plain_has_fixed_var(map,
9272 map_offset(map, type) - 1 + pos, val);
9275 /* If "map" obviously lies on a hyperplane where the given dimension
9276 * has a fixed value, then return that value.
9277 * Otherwise return NaN.
9279 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9280 enum isl_dim_type type, unsigned pos)
9282 isl_ctx *ctx;
9283 isl_val *v;
9284 isl_bool fixed;
9286 if (!map)
9287 return NULL;
9288 ctx = isl_map_get_ctx(map);
9289 v = isl_val_alloc(ctx);
9290 if (!v)
9291 return NULL;
9292 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9293 if (fixed < 0)
9294 return isl_val_free(v);
9295 if (fixed) {
9296 isl_int_set_si(v->d, 1);
9297 return v;
9299 isl_val_free(v);
9300 return isl_val_nan(ctx);
9303 /* If "set" obviously lies on a hyperplane where the given dimension
9304 * has a fixed value, then return that value.
9305 * Otherwise return NaN.
9307 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9308 enum isl_dim_type type, unsigned pos)
9310 return isl_map_plain_get_val_if_fixed(set, type, pos);
9313 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9314 * then return this fixed value in *val.
9316 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9317 unsigned dim, isl_int *val)
9319 return isl_basic_set_plain_has_fixed_var(bset,
9320 isl_basic_set_n_param(bset) + dim, val);
9323 /* Return -1 if the constraint "c1" should be sorted before "c2"
9324 * and 1 if it should be sorted after "c2".
9325 * Return 0 if the two constraints are the same (up to the constant term).
9327 * In particular, if a constraint involves later variables than another
9328 * then it is sorted after this other constraint.
9329 * uset_gist depends on constraints without existentially quantified
9330 * variables sorting first.
9332 * For constraints that have the same latest variable, those
9333 * with the same coefficient for this latest variable (first in absolute value
9334 * and then in actual value) are grouped together.
9335 * This is useful for detecting pairs of constraints that can
9336 * be chained in their printed representation.
9338 * Finally, within a group, constraints are sorted according to
9339 * their coefficients (excluding the constant term).
9341 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9343 isl_int **c1 = (isl_int **) p1;
9344 isl_int **c2 = (isl_int **) p2;
9345 int l1, l2;
9346 unsigned size = *(unsigned *) arg;
9347 int cmp;
9349 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9350 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9352 if (l1 != l2)
9353 return l1 - l2;
9355 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9356 if (cmp != 0)
9357 return cmp;
9358 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9359 if (cmp != 0)
9360 return -cmp;
9362 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9365 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9366 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9367 * and 0 if the two constraints are the same (up to the constant term).
9369 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9370 isl_int *c1, isl_int *c2)
9372 unsigned total;
9374 if (!bmap)
9375 return -2;
9376 total = isl_basic_map_total_dim(bmap);
9377 return sort_constraint_cmp(&c1, &c2, &total);
9380 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9381 __isl_take isl_basic_map *bmap)
9383 unsigned total;
9385 if (!bmap)
9386 return NULL;
9387 if (bmap->n_ineq == 0)
9388 return bmap;
9389 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9390 return bmap;
9391 total = isl_basic_map_total_dim(bmap);
9392 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9393 &sort_constraint_cmp, &total) < 0)
9394 return isl_basic_map_free(bmap);
9395 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9396 return bmap;
9399 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9400 __isl_take isl_basic_set *bset)
9402 isl_basic_map *bmap = bset_to_bmap(bset);
9403 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9406 __isl_give isl_basic_map *isl_basic_map_normalize(
9407 __isl_take isl_basic_map *bmap)
9409 bmap = isl_basic_map_remove_redundancies(bmap);
9410 bmap = isl_basic_map_sort_constraints(bmap);
9411 return bmap;
9413 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9414 __isl_keep isl_basic_map *bmap2)
9416 int i, cmp;
9417 unsigned total;
9418 isl_space *space1, *space2;
9420 if (!bmap1 || !bmap2)
9421 return -1;
9423 if (bmap1 == bmap2)
9424 return 0;
9425 space1 = isl_basic_map_peek_space(bmap1);
9426 space2 = isl_basic_map_peek_space(bmap2);
9427 cmp = isl_space_cmp(space1, space2);
9428 if (cmp)
9429 return cmp;
9430 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9431 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9432 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9433 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9434 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9435 return 0;
9436 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9437 return 1;
9438 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9439 return -1;
9440 if (bmap1->n_eq != bmap2->n_eq)
9441 return bmap1->n_eq - bmap2->n_eq;
9442 if (bmap1->n_ineq != bmap2->n_ineq)
9443 return bmap1->n_ineq - bmap2->n_ineq;
9444 if (bmap1->n_div != bmap2->n_div)
9445 return bmap1->n_div - bmap2->n_div;
9446 total = isl_basic_map_total_dim(bmap1);
9447 for (i = 0; i < bmap1->n_eq; ++i) {
9448 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9449 if (cmp)
9450 return cmp;
9452 for (i = 0; i < bmap1->n_ineq; ++i) {
9453 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9454 if (cmp)
9455 return cmp;
9457 for (i = 0; i < bmap1->n_div; ++i) {
9458 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9459 if (cmp)
9460 return cmp;
9462 return 0;
9465 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9466 __isl_keep isl_basic_set *bset2)
9468 return isl_basic_map_plain_cmp(bset1, bset2);
9471 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9473 int i, cmp;
9475 if (set1 == set2)
9476 return 0;
9477 if (set1->n != set2->n)
9478 return set1->n - set2->n;
9480 for (i = 0; i < set1->n; ++i) {
9481 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9482 if (cmp)
9483 return cmp;
9486 return 0;
9489 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9490 __isl_keep isl_basic_map *bmap2)
9492 if (!bmap1 || !bmap2)
9493 return isl_bool_error;
9494 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9497 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9498 __isl_keep isl_basic_set *bset2)
9500 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9501 bset_to_bmap(bset2));
9504 static int qsort_bmap_cmp(const void *p1, const void *p2)
9506 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9507 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9509 return isl_basic_map_plain_cmp(bmap1, bmap2);
9512 /* Sort the basic maps of "map" and remove duplicate basic maps.
9514 * While removing basic maps, we make sure that the basic maps remain
9515 * sorted because isl_map_normalize expects the basic maps of the result
9516 * to be sorted.
9518 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9520 int i, j;
9522 map = isl_map_remove_empty_parts(map);
9523 if (!map)
9524 return NULL;
9525 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9526 for (i = map->n - 1; i >= 1; --i) {
9527 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9528 continue;
9529 isl_basic_map_free(map->p[i-1]);
9530 for (j = i; j < map->n; ++j)
9531 map->p[j - 1] = map->p[j];
9532 map->n--;
9535 return map;
9538 /* Remove obvious duplicates among the basic maps of "map".
9540 * Unlike isl_map_normalize, this function does not remove redundant
9541 * constraints and only removes duplicates that have exactly the same
9542 * constraints in the input. It does sort the constraints and
9543 * the basic maps to ease the detection of duplicates.
9545 * If "map" has already been normalized or if the basic maps are
9546 * disjoint, then there can be no duplicates.
9548 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9550 int i;
9551 isl_basic_map *bmap;
9553 if (!map)
9554 return NULL;
9555 if (map->n <= 1)
9556 return map;
9557 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9558 return map;
9559 for (i = 0; i < map->n; ++i) {
9560 bmap = isl_basic_map_copy(map->p[i]);
9561 bmap = isl_basic_map_sort_constraints(bmap);
9562 if (!bmap)
9563 return isl_map_free(map);
9564 isl_basic_map_free(map->p[i]);
9565 map->p[i] = bmap;
9568 map = sort_and_remove_duplicates(map);
9569 return map;
9572 /* We normalize in place, but if anything goes wrong we need
9573 * to return NULL, so we need to make sure we don't change the
9574 * meaning of any possible other copies of map.
9576 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9578 int i;
9579 struct isl_basic_map *bmap;
9581 if (!map)
9582 return NULL;
9583 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9584 return map;
9585 for (i = 0; i < map->n; ++i) {
9586 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9587 if (!bmap)
9588 goto error;
9589 isl_basic_map_free(map->p[i]);
9590 map->p[i] = bmap;
9593 map = sort_and_remove_duplicates(map);
9594 if (map)
9595 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9596 return map;
9597 error:
9598 isl_map_free(map);
9599 return NULL;
9602 struct isl_set *isl_set_normalize(struct isl_set *set)
9604 return set_from_map(isl_map_normalize(set_to_map(set)));
9607 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9608 __isl_keep isl_map *map2)
9610 int i;
9611 isl_bool equal;
9613 if (!map1 || !map2)
9614 return isl_bool_error;
9616 if (map1 == map2)
9617 return isl_bool_true;
9618 if (!isl_space_is_equal(map1->dim, map2->dim))
9619 return isl_bool_false;
9621 map1 = isl_map_copy(map1);
9622 map2 = isl_map_copy(map2);
9623 map1 = isl_map_normalize(map1);
9624 map2 = isl_map_normalize(map2);
9625 if (!map1 || !map2)
9626 goto error;
9627 equal = map1->n == map2->n;
9628 for (i = 0; equal && i < map1->n; ++i) {
9629 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9630 if (equal < 0)
9631 goto error;
9633 isl_map_free(map1);
9634 isl_map_free(map2);
9635 return equal;
9636 error:
9637 isl_map_free(map1);
9638 isl_map_free(map2);
9639 return isl_bool_error;
9642 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9643 __isl_keep isl_set *set2)
9645 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9648 /* Return the basic maps in "map" as a list.
9650 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9651 __isl_keep isl_map *map)
9653 int i;
9654 isl_ctx *ctx;
9655 isl_basic_map_list *list;
9657 if (!map)
9658 return NULL;
9659 ctx = isl_map_get_ctx(map);
9660 list = isl_basic_map_list_alloc(ctx, map->n);
9662 for (i = 0; i < map->n; ++i) {
9663 isl_basic_map *bmap;
9665 bmap = isl_basic_map_copy(map->p[i]);
9666 list = isl_basic_map_list_add(list, bmap);
9669 return list;
9672 /* Return the intersection of the elements in the non-empty list "list".
9673 * All elements are assumed to live in the same space.
9675 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9676 __isl_take isl_basic_map_list *list)
9678 int i, n;
9679 isl_basic_map *bmap;
9681 if (!list)
9682 return NULL;
9683 n = isl_basic_map_list_n_basic_map(list);
9684 if (n < 1)
9685 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9686 "expecting non-empty list", goto error);
9688 bmap = isl_basic_map_list_get_basic_map(list, 0);
9689 for (i = 1; i < n; ++i) {
9690 isl_basic_map *bmap_i;
9692 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9693 bmap = isl_basic_map_intersect(bmap, bmap_i);
9696 isl_basic_map_list_free(list);
9697 return bmap;
9698 error:
9699 isl_basic_map_list_free(list);
9700 return NULL;
9703 /* Return the intersection of the elements in the non-empty list "list".
9704 * All elements are assumed to live in the same space.
9706 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9707 __isl_take isl_basic_set_list *list)
9709 return isl_basic_map_list_intersect(list);
9712 /* Return the union of the elements of "list".
9713 * The list is required to have at least one element.
9715 __isl_give isl_set *isl_basic_set_list_union(
9716 __isl_take isl_basic_set_list *list)
9718 int i, n;
9719 isl_space *space;
9720 isl_basic_set *bset;
9721 isl_set *set;
9723 if (!list)
9724 return NULL;
9725 n = isl_basic_set_list_n_basic_set(list);
9726 if (n < 1)
9727 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9728 "expecting non-empty list", goto error);
9730 bset = isl_basic_set_list_get_basic_set(list, 0);
9731 space = isl_basic_set_get_space(bset);
9732 isl_basic_set_free(bset);
9734 set = isl_set_alloc_space(space, n, 0);
9735 for (i = 0; i < n; ++i) {
9736 bset = isl_basic_set_list_get_basic_set(list, i);
9737 set = isl_set_add_basic_set(set, bset);
9740 isl_basic_set_list_free(list);
9741 return set;
9742 error:
9743 isl_basic_set_list_free(list);
9744 return NULL;
9747 /* Return the union of the elements in the non-empty list "list".
9748 * All elements are assumed to live in the same space.
9750 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9752 int i, n;
9753 isl_set *set;
9755 if (!list)
9756 return NULL;
9757 n = isl_set_list_n_set(list);
9758 if (n < 1)
9759 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9760 "expecting non-empty list", goto error);
9762 set = isl_set_list_get_set(list, 0);
9763 for (i = 1; i < n; ++i) {
9764 isl_set *set_i;
9766 set_i = isl_set_list_get_set(list, i);
9767 set = isl_set_union(set, set_i);
9770 isl_set_list_free(list);
9771 return set;
9772 error:
9773 isl_set_list_free(list);
9774 return NULL;
9777 __isl_give isl_basic_map *isl_basic_map_product(
9778 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9780 isl_space *space_result = NULL;
9781 struct isl_basic_map *bmap;
9782 unsigned in1, in2, out1, out2, nparam, total, pos;
9783 struct isl_dim_map *dim_map1, *dim_map2;
9785 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9786 goto error;
9787 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9788 isl_space_copy(bmap2->dim));
9790 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9791 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9792 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9793 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9794 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9796 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9797 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9798 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9799 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9800 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9801 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9802 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9803 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9804 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9805 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9806 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9808 bmap = isl_basic_map_alloc_space(space_result,
9809 bmap1->n_div + bmap2->n_div,
9810 bmap1->n_eq + bmap2->n_eq,
9811 bmap1->n_ineq + bmap2->n_ineq);
9812 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9813 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9814 bmap = isl_basic_map_simplify(bmap);
9815 return isl_basic_map_finalize(bmap);
9816 error:
9817 isl_basic_map_free(bmap1);
9818 isl_basic_map_free(bmap2);
9819 return NULL;
9822 __isl_give isl_basic_map *isl_basic_map_flat_product(
9823 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9825 isl_basic_map *prod;
9827 prod = isl_basic_map_product(bmap1, bmap2);
9828 prod = isl_basic_map_flatten(prod);
9829 return prod;
9832 __isl_give isl_basic_set *isl_basic_set_flat_product(
9833 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9835 return isl_basic_map_flat_range_product(bset1, bset2);
9838 __isl_give isl_basic_map *isl_basic_map_domain_product(
9839 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9841 isl_space *space1, *space2;
9842 isl_space *space_result = NULL;
9843 isl_basic_map *bmap;
9844 unsigned in1, in2, out, nparam, total, pos;
9845 struct isl_dim_map *dim_map1, *dim_map2;
9847 if (!bmap1 || !bmap2)
9848 goto error;
9850 space1 = isl_basic_map_get_space(bmap1);
9851 space2 = isl_basic_map_get_space(bmap2);
9852 space_result = isl_space_domain_product(space1, space2);
9854 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9855 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9856 out = isl_basic_map_dim(bmap1, isl_dim_out);
9857 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9859 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9860 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9861 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9862 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9863 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9864 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9865 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9866 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9867 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9868 isl_dim_map_div(dim_map1, bmap1, pos += out);
9869 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9871 bmap = isl_basic_map_alloc_space(space_result,
9872 bmap1->n_div + bmap2->n_div,
9873 bmap1->n_eq + bmap2->n_eq,
9874 bmap1->n_ineq + bmap2->n_ineq);
9875 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9876 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9877 bmap = isl_basic_map_simplify(bmap);
9878 return isl_basic_map_finalize(bmap);
9879 error:
9880 isl_basic_map_free(bmap1);
9881 isl_basic_map_free(bmap2);
9882 return NULL;
9885 __isl_give isl_basic_map *isl_basic_map_range_product(
9886 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9888 isl_bool rational;
9889 isl_space *space_result = NULL;
9890 isl_basic_map *bmap;
9891 unsigned in, out1, out2, nparam, total, pos;
9892 struct isl_dim_map *dim_map1, *dim_map2;
9894 rational = isl_basic_map_is_rational(bmap1);
9895 if (rational >= 0 && rational)
9896 rational = isl_basic_map_is_rational(bmap2);
9897 if (!bmap1 || !bmap2 || rational < 0)
9898 goto error;
9900 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9901 goto error;
9903 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9904 isl_space_copy(bmap2->dim));
9906 in = isl_basic_map_dim(bmap1, isl_dim_in);
9907 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9908 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9909 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9911 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9912 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9913 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9914 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9915 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9916 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9917 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9918 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9919 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9920 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9921 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9923 bmap = isl_basic_map_alloc_space(space_result,
9924 bmap1->n_div + bmap2->n_div,
9925 bmap1->n_eq + bmap2->n_eq,
9926 bmap1->n_ineq + bmap2->n_ineq);
9927 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9928 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9929 if (rational)
9930 bmap = isl_basic_map_set_rational(bmap);
9931 bmap = isl_basic_map_simplify(bmap);
9932 return isl_basic_map_finalize(bmap);
9933 error:
9934 isl_basic_map_free(bmap1);
9935 isl_basic_map_free(bmap2);
9936 return NULL;
9939 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9940 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9942 isl_basic_map *prod;
9944 prod = isl_basic_map_range_product(bmap1, bmap2);
9945 prod = isl_basic_map_flatten_range(prod);
9946 return prod;
9949 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9950 * and collect the results.
9951 * The result live in the space obtained by calling "space_product"
9952 * on the spaces of "map1" and "map2".
9953 * If "remove_duplicates" is set then the result may contain duplicates
9954 * (even if the inputs do not) and so we try and remove the obvious
9955 * duplicates.
9957 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9958 __isl_take isl_map *map2,
9959 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9960 __isl_take isl_space *right),
9961 __isl_give isl_basic_map *(*basic_map_product)(
9962 __isl_take isl_basic_map *left,
9963 __isl_take isl_basic_map *right),
9964 int remove_duplicates)
9966 unsigned flags = 0;
9967 struct isl_map *result;
9968 int i, j;
9969 isl_bool m;
9971 m = isl_map_has_equal_params(map1, map2);
9972 if (m < 0)
9973 goto error;
9974 if (!m)
9975 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
9976 "parameters don't match", goto error);
9978 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9979 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9980 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9982 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9983 isl_space_copy(map2->dim)),
9984 map1->n * map2->n, flags);
9985 if (!result)
9986 goto error;
9987 for (i = 0; i < map1->n; ++i)
9988 for (j = 0; j < map2->n; ++j) {
9989 struct isl_basic_map *part;
9990 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9991 isl_basic_map_copy(map2->p[j]));
9992 if (isl_basic_map_is_empty(part))
9993 isl_basic_map_free(part);
9994 else
9995 result = isl_map_add_basic_map(result, part);
9996 if (!result)
9997 goto error;
9999 if (remove_duplicates)
10000 result = isl_map_remove_obvious_duplicates(result);
10001 isl_map_free(map1);
10002 isl_map_free(map2);
10003 return result;
10004 error:
10005 isl_map_free(map1);
10006 isl_map_free(map2);
10007 return NULL;
10010 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10012 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10013 __isl_take isl_map *map2)
10015 return map_product(map1, map2, &isl_space_product,
10016 &isl_basic_map_product, 0);
10019 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10020 __isl_take isl_map *map2)
10022 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10025 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10027 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10028 __isl_take isl_map *map2)
10030 isl_map *prod;
10032 prod = isl_map_product(map1, map2);
10033 prod = isl_map_flatten(prod);
10034 return prod;
10037 /* Given two set A and B, construct its Cartesian product A x B.
10039 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10041 return isl_map_range_product(set1, set2);
10044 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10045 __isl_take isl_set *set2)
10047 return isl_map_flat_range_product(set1, set2);
10050 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10052 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10053 __isl_take isl_map *map2)
10055 return map_product(map1, map2, &isl_space_domain_product,
10056 &isl_basic_map_domain_product, 1);
10059 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10061 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10062 __isl_take isl_map *map2)
10064 return map_product(map1, map2, &isl_space_range_product,
10065 &isl_basic_map_range_product, 1);
10068 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10069 __isl_take isl_map *map2)
10071 return isl_map_align_params_map_map_and(map1, map2,
10072 &map_domain_product_aligned);
10075 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10076 __isl_take isl_map *map2)
10078 return isl_map_align_params_map_map_and(map1, map2,
10079 &map_range_product_aligned);
10082 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10084 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10086 isl_space *space;
10087 int total1, keep1, total2, keep2;
10089 if (!map)
10090 return NULL;
10091 if (!isl_space_domain_is_wrapping(map->dim) ||
10092 !isl_space_range_is_wrapping(map->dim))
10093 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10094 "not a product", return isl_map_free(map));
10096 space = isl_map_get_space(map);
10097 total1 = isl_space_dim(space, isl_dim_in);
10098 total2 = isl_space_dim(space, isl_dim_out);
10099 space = isl_space_factor_domain(space);
10100 keep1 = isl_space_dim(space, isl_dim_in);
10101 keep2 = isl_space_dim(space, isl_dim_out);
10102 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10103 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10104 map = isl_map_reset_space(map, space);
10106 return map;
10109 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10111 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10113 isl_space *space;
10114 int total1, keep1, total2, keep2;
10116 if (!map)
10117 return NULL;
10118 if (!isl_space_domain_is_wrapping(map->dim) ||
10119 !isl_space_range_is_wrapping(map->dim))
10120 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10121 "not a product", return isl_map_free(map));
10123 space = isl_map_get_space(map);
10124 total1 = isl_space_dim(space, isl_dim_in);
10125 total2 = isl_space_dim(space, isl_dim_out);
10126 space = isl_space_factor_range(space);
10127 keep1 = isl_space_dim(space, isl_dim_in);
10128 keep2 = isl_space_dim(space, isl_dim_out);
10129 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10130 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10131 map = isl_map_reset_space(map, space);
10133 return map;
10136 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10138 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10140 isl_space *space;
10141 int total, keep;
10143 if (!map)
10144 return NULL;
10145 if (!isl_space_domain_is_wrapping(map->dim))
10146 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10147 "domain is not a product", return isl_map_free(map));
10149 space = isl_map_get_space(map);
10150 total = isl_space_dim(space, isl_dim_in);
10151 space = isl_space_domain_factor_domain(space);
10152 keep = isl_space_dim(space, isl_dim_in);
10153 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10154 map = isl_map_reset_space(map, space);
10156 return map;
10159 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10161 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10163 isl_space *space;
10164 int total, keep;
10166 if (!map)
10167 return NULL;
10168 if (!isl_space_domain_is_wrapping(map->dim))
10169 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10170 "domain is not a product", return isl_map_free(map));
10172 space = isl_map_get_space(map);
10173 total = isl_space_dim(space, isl_dim_in);
10174 space = isl_space_domain_factor_range(space);
10175 keep = isl_space_dim(space, isl_dim_in);
10176 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10177 map = isl_map_reset_space(map, space);
10179 return map;
10182 /* Given a map A -> [B -> C], extract the map A -> B.
10184 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10186 isl_space *space;
10187 int total, keep;
10189 if (!map)
10190 return NULL;
10191 if (!isl_space_range_is_wrapping(map->dim))
10192 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10193 "range is not a product", return isl_map_free(map));
10195 space = isl_map_get_space(map);
10196 total = isl_space_dim(space, isl_dim_out);
10197 space = isl_space_range_factor_domain(space);
10198 keep = isl_space_dim(space, isl_dim_out);
10199 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10200 map = isl_map_reset_space(map, space);
10202 return map;
10205 /* Given a map A -> [B -> C], extract the map A -> C.
10207 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10209 isl_space *space;
10210 int total, keep;
10212 if (!map)
10213 return NULL;
10214 if (!isl_space_range_is_wrapping(map->dim))
10215 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10216 "range is not a product", return isl_map_free(map));
10218 space = isl_map_get_space(map);
10219 total = isl_space_dim(space, isl_dim_out);
10220 space = isl_space_range_factor_range(space);
10221 keep = isl_space_dim(space, isl_dim_out);
10222 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10223 map = isl_map_reset_space(map, space);
10225 return map;
10228 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10230 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10231 __isl_take isl_map *map2)
10233 isl_map *prod;
10235 prod = isl_map_domain_product(map1, map2);
10236 prod = isl_map_flatten_domain(prod);
10237 return prod;
10240 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10242 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10243 __isl_take isl_map *map2)
10245 isl_map *prod;
10247 prod = isl_map_range_product(map1, map2);
10248 prod = isl_map_flatten_range(prod);
10249 return prod;
10252 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10254 int i;
10255 uint32_t hash = isl_hash_init();
10256 unsigned total;
10258 if (!bmap)
10259 return 0;
10260 bmap = isl_basic_map_copy(bmap);
10261 bmap = isl_basic_map_normalize(bmap);
10262 if (!bmap)
10263 return 0;
10264 total = isl_basic_map_total_dim(bmap);
10265 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10266 for (i = 0; i < bmap->n_eq; ++i) {
10267 uint32_t c_hash;
10268 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10269 isl_hash_hash(hash, c_hash);
10271 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10272 for (i = 0; i < bmap->n_ineq; ++i) {
10273 uint32_t c_hash;
10274 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10275 isl_hash_hash(hash, c_hash);
10277 isl_hash_byte(hash, bmap->n_div & 0xFF);
10278 for (i = 0; i < bmap->n_div; ++i) {
10279 uint32_t c_hash;
10280 if (isl_int_is_zero(bmap->div[i][0]))
10281 continue;
10282 isl_hash_byte(hash, i & 0xFF);
10283 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10284 isl_hash_hash(hash, c_hash);
10286 isl_basic_map_free(bmap);
10287 return hash;
10290 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10292 return isl_basic_map_get_hash(bset_to_bmap(bset));
10295 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10297 int i;
10298 uint32_t hash;
10300 if (!map)
10301 return 0;
10302 map = isl_map_copy(map);
10303 map = isl_map_normalize(map);
10304 if (!map)
10305 return 0;
10307 hash = isl_hash_init();
10308 for (i = 0; i < map->n; ++i) {
10309 uint32_t bmap_hash;
10310 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10311 isl_hash_hash(hash, bmap_hash);
10314 isl_map_free(map);
10316 return hash;
10319 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10321 return isl_map_get_hash(set_to_map(set));
10324 /* Return the number of basic maps in the (current) representation of "map".
10326 int isl_map_n_basic_map(__isl_keep isl_map *map)
10328 return map ? map->n : 0;
10331 int isl_set_n_basic_set(__isl_keep isl_set *set)
10333 return set ? set->n : 0;
10336 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10337 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10339 int i;
10341 if (!map)
10342 return isl_stat_error;
10344 for (i = 0; i < map->n; ++i)
10345 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10346 return isl_stat_error;
10348 return isl_stat_ok;
10351 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10352 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10354 int i;
10356 if (!set)
10357 return isl_stat_error;
10359 for (i = 0; i < set->n; ++i)
10360 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10361 return isl_stat_error;
10363 return isl_stat_ok;
10366 /* Return a list of basic sets, the union of which is equal to "set".
10368 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10369 __isl_keep isl_set *set)
10371 int i;
10372 isl_basic_set_list *list;
10374 if (!set)
10375 return NULL;
10377 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10378 for (i = 0; i < set->n; ++i) {
10379 isl_basic_set *bset;
10381 bset = isl_basic_set_copy(set->p[i]);
10382 list = isl_basic_set_list_add(list, bset);
10385 return list;
10388 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10390 isl_space *space;
10392 if (!bset)
10393 return NULL;
10395 bset = isl_basic_set_cow(bset);
10396 if (!bset)
10397 return NULL;
10399 space = isl_basic_set_get_space(bset);
10400 space = isl_space_lift(space, bset->n_div);
10401 if (!space)
10402 goto error;
10403 isl_space_free(bset->dim);
10404 bset->dim = space;
10405 bset->extra -= bset->n_div;
10406 bset->n_div = 0;
10408 bset = isl_basic_set_finalize(bset);
10410 return bset;
10411 error:
10412 isl_basic_set_free(bset);
10413 return NULL;
10416 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10418 int i;
10419 isl_space *space;
10420 unsigned n_div;
10422 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10424 if (!set)
10425 return NULL;
10427 set = isl_set_cow(set);
10428 if (!set)
10429 return NULL;
10431 n_div = set->p[0]->n_div;
10432 space = isl_set_get_space(set);
10433 space = isl_space_lift(space, n_div);
10434 if (!space)
10435 goto error;
10436 isl_space_free(set->dim);
10437 set->dim = space;
10439 for (i = 0; i < set->n; ++i) {
10440 set->p[i] = isl_basic_set_lift(set->p[i]);
10441 if (!set->p[i])
10442 goto error;
10445 return set;
10446 error:
10447 isl_set_free(set);
10448 return NULL;
10451 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10453 unsigned dim;
10454 int size = 0;
10456 if (!bset)
10457 return -1;
10459 dim = isl_basic_set_total_dim(bset);
10460 size += bset->n_eq * (1 + dim);
10461 size += bset->n_ineq * (1 + dim);
10462 size += bset->n_div * (2 + dim);
10464 return size;
10467 int isl_set_size(__isl_keep isl_set *set)
10469 int i;
10470 int size = 0;
10472 if (!set)
10473 return -1;
10475 for (i = 0; i < set->n; ++i)
10476 size += isl_basic_set_size(set->p[i]);
10478 return size;
10481 /* Check if there is any lower bound (if lower == 0) and/or upper
10482 * bound (if upper == 0) on the specified dim.
10484 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10485 enum isl_dim_type type, unsigned pos, int lower, int upper)
10487 int i;
10489 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10490 return isl_bool_error;
10492 pos += isl_basic_map_offset(bmap, type);
10494 for (i = 0; i < bmap->n_div; ++i) {
10495 if (isl_int_is_zero(bmap->div[i][0]))
10496 continue;
10497 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10498 return isl_bool_true;
10501 for (i = 0; i < bmap->n_eq; ++i)
10502 if (!isl_int_is_zero(bmap->eq[i][pos]))
10503 return isl_bool_true;
10505 for (i = 0; i < bmap->n_ineq; ++i) {
10506 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10507 if (sgn > 0)
10508 lower = 1;
10509 if (sgn < 0)
10510 upper = 1;
10513 return lower && upper;
10516 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10517 enum isl_dim_type type, unsigned pos)
10519 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10522 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10523 enum isl_dim_type type, unsigned pos)
10525 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10528 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10529 enum isl_dim_type type, unsigned pos)
10531 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10534 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10535 enum isl_dim_type type, unsigned pos)
10537 int i;
10539 if (!map)
10540 return isl_bool_error;
10542 for (i = 0; i < map->n; ++i) {
10543 isl_bool bounded;
10544 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10545 if (bounded < 0 || !bounded)
10546 return bounded;
10549 return isl_bool_true;
10552 /* Return true if the specified dim is involved in both an upper bound
10553 * and a lower bound.
10555 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10556 enum isl_dim_type type, unsigned pos)
10558 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10561 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10563 static isl_bool has_any_bound(__isl_keep isl_map *map,
10564 enum isl_dim_type type, unsigned pos,
10565 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10566 enum isl_dim_type type, unsigned pos))
10568 int i;
10570 if (!map)
10571 return isl_bool_error;
10573 for (i = 0; i < map->n; ++i) {
10574 isl_bool bounded;
10575 bounded = fn(map->p[i], type, pos);
10576 if (bounded < 0 || bounded)
10577 return bounded;
10580 return isl_bool_false;
10583 /* Return 1 if the specified dim is involved in any lower bound.
10585 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10586 enum isl_dim_type type, unsigned pos)
10588 return has_any_bound(set, type, pos,
10589 &isl_basic_map_dim_has_lower_bound);
10592 /* Return 1 if the specified dim is involved in any upper bound.
10594 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10595 enum isl_dim_type type, unsigned pos)
10597 return has_any_bound(set, type, pos,
10598 &isl_basic_map_dim_has_upper_bound);
10601 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10603 static isl_bool has_bound(__isl_keep isl_map *map,
10604 enum isl_dim_type type, unsigned pos,
10605 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10606 enum isl_dim_type type, unsigned pos))
10608 int i;
10610 if (!map)
10611 return isl_bool_error;
10613 for (i = 0; i < map->n; ++i) {
10614 isl_bool bounded;
10615 bounded = fn(map->p[i], type, pos);
10616 if (bounded < 0 || !bounded)
10617 return bounded;
10620 return isl_bool_true;
10623 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10625 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10626 enum isl_dim_type type, unsigned pos)
10628 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10631 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10633 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10634 enum isl_dim_type type, unsigned pos)
10636 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10639 /* For each of the "n" variables starting at "first", determine
10640 * the sign of the variable and put the results in the first "n"
10641 * elements of the array "signs".
10642 * Sign
10643 * 1 means that the variable is non-negative
10644 * -1 means that the variable is non-positive
10645 * 0 means the variable attains both positive and negative values.
10647 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10648 unsigned first, unsigned n, int *signs)
10650 isl_vec *bound = NULL;
10651 struct isl_tab *tab = NULL;
10652 struct isl_tab_undo *snap;
10653 int i;
10655 if (!bset || !signs)
10656 return isl_stat_error;
10658 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10659 tab = isl_tab_from_basic_set(bset, 0);
10660 if (!bound || !tab)
10661 goto error;
10663 isl_seq_clr(bound->el, bound->size);
10664 isl_int_set_si(bound->el[0], -1);
10666 snap = isl_tab_snap(tab);
10667 for (i = 0; i < n; ++i) {
10668 int empty;
10670 isl_int_set_si(bound->el[1 + first + i], -1);
10671 if (isl_tab_add_ineq(tab, bound->el) < 0)
10672 goto error;
10673 empty = tab->empty;
10674 isl_int_set_si(bound->el[1 + first + i], 0);
10675 if (isl_tab_rollback(tab, snap) < 0)
10676 goto error;
10678 if (empty) {
10679 signs[i] = 1;
10680 continue;
10683 isl_int_set_si(bound->el[1 + first + i], 1);
10684 if (isl_tab_add_ineq(tab, bound->el) < 0)
10685 goto error;
10686 empty = tab->empty;
10687 isl_int_set_si(bound->el[1 + first + i], 0);
10688 if (isl_tab_rollback(tab, snap) < 0)
10689 goto error;
10691 signs[i] = empty ? -1 : 0;
10694 isl_tab_free(tab);
10695 isl_vec_free(bound);
10696 return isl_stat_ok;
10697 error:
10698 isl_tab_free(tab);
10699 isl_vec_free(bound);
10700 return isl_stat_error;
10703 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10704 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10706 if (!bset || !signs)
10707 return isl_stat_error;
10708 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10709 return isl_stat_error;
10711 first += pos(bset->dim, type) - 1;
10712 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10715 /* Is it possible for the integer division "div" to depend (possibly
10716 * indirectly) on any output dimensions?
10718 * If the div is undefined, then we conservatively assume that it
10719 * may depend on them.
10720 * Otherwise, we check if it actually depends on them or on any integer
10721 * divisions that may depend on them.
10723 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10725 int i;
10726 unsigned n_out, o_out;
10727 unsigned n_div, o_div;
10729 if (isl_int_is_zero(bmap->div[div][0]))
10730 return isl_bool_true;
10732 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10733 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10735 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10736 return isl_bool_true;
10738 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10739 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10741 for (i = 0; i < n_div; ++i) {
10742 isl_bool may_involve;
10744 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10745 continue;
10746 may_involve = div_may_involve_output(bmap, i);
10747 if (may_involve < 0 || may_involve)
10748 return may_involve;
10751 return isl_bool_false;
10754 /* Return the first integer division of "bmap" in the range
10755 * [first, first + n[ that may depend on any output dimensions and
10756 * that has a non-zero coefficient in "c" (where the first coefficient
10757 * in "c" corresponds to integer division "first").
10759 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10760 isl_int *c, int first, int n)
10762 int k;
10764 if (!bmap)
10765 return -1;
10767 for (k = first; k < first + n; ++k) {
10768 isl_bool may_involve;
10770 if (isl_int_is_zero(c[k]))
10771 continue;
10772 may_involve = div_may_involve_output(bmap, k);
10773 if (may_involve < 0)
10774 return -1;
10775 if (may_involve)
10776 return k;
10779 return first + n;
10782 /* Look for a pair of inequality constraints in "bmap" of the form
10784 * -l + i >= 0 or i >= l
10785 * and
10786 * n + l - i >= 0 or i <= l + n
10788 * with n < "m" and i the output dimension at position "pos".
10789 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10790 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10791 * and earlier output dimensions, as well as integer divisions that do
10792 * not involve any of the output dimensions.
10794 * Return the index of the first inequality constraint or bmap->n_ineq
10795 * if no such pair can be found.
10797 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10798 int pos, isl_int m)
10800 int i, j;
10801 isl_ctx *ctx;
10802 unsigned total;
10803 unsigned n_div, o_div;
10804 unsigned n_out, o_out;
10805 int less;
10807 if (!bmap)
10808 return -1;
10810 ctx = isl_basic_map_get_ctx(bmap);
10811 total = isl_basic_map_total_dim(bmap);
10812 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10813 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10814 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10815 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10816 for (i = 0; i < bmap->n_ineq; ++i) {
10817 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10818 continue;
10819 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10820 n_out - (pos + 1)) != -1)
10821 continue;
10822 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10823 0, n_div) < n_div)
10824 continue;
10825 for (j = i + 1; j < bmap->n_ineq; ++j) {
10826 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10827 ctx->one))
10828 continue;
10829 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10830 bmap->ineq[j] + 1, total))
10831 continue;
10832 break;
10834 if (j >= bmap->n_ineq)
10835 continue;
10836 isl_int_add(bmap->ineq[i][0],
10837 bmap->ineq[i][0], bmap->ineq[j][0]);
10838 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10839 isl_int_sub(bmap->ineq[i][0],
10840 bmap->ineq[i][0], bmap->ineq[j][0]);
10841 if (!less)
10842 continue;
10843 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10844 return i;
10845 else
10846 return j;
10849 return bmap->n_ineq;
10852 /* Return the index of the equality of "bmap" that defines
10853 * the output dimension "pos" in terms of earlier dimensions.
10854 * The equality may also involve integer divisions, as long
10855 * as those integer divisions are defined in terms of
10856 * parameters or input dimensions.
10857 * In this case, *div is set to the number of integer divisions and
10858 * *ineq is set to the number of inequality constraints (provided
10859 * div and ineq are not NULL).
10861 * The equality may also involve a single integer division involving
10862 * the output dimensions (typically only output dimension "pos") as
10863 * long as the coefficient of output dimension "pos" is 1 or -1 and
10864 * there is a pair of constraints i >= l and i <= l + n, with i referring
10865 * to output dimension "pos", l an expression involving only earlier
10866 * dimensions and n smaller than the coefficient of the integer division
10867 * in the equality. In this case, the output dimension can be defined
10868 * in terms of a modulo expression that does not involve the integer division.
10869 * *div is then set to this single integer division and
10870 * *ineq is set to the index of constraint i >= l.
10872 * Return bmap->n_eq if there is no such equality.
10873 * Return -1 on error.
10875 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10876 int pos, int *div, int *ineq)
10878 int j, k, l;
10879 unsigned n_out, o_out;
10880 unsigned n_div, o_div;
10882 if (!bmap)
10883 return -1;
10885 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10886 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10887 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10888 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10890 if (ineq)
10891 *ineq = bmap->n_ineq;
10892 if (div)
10893 *div = n_div;
10894 for (j = 0; j < bmap->n_eq; ++j) {
10895 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10896 continue;
10897 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10898 n_out - (pos + 1)) != -1)
10899 continue;
10900 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10901 0, n_div);
10902 if (k >= n_div)
10903 return j;
10904 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10905 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10906 continue;
10907 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10908 k + 1, n_div - (k+1)) < n_div)
10909 continue;
10910 l = find_modulo_constraint_pair(bmap, pos,
10911 bmap->eq[j][o_div + k]);
10912 if (l < 0)
10913 return -1;
10914 if (l >= bmap->n_ineq)
10915 continue;
10916 if (div)
10917 *div = k;
10918 if (ineq)
10919 *ineq = l;
10920 return j;
10923 return bmap->n_eq;
10926 /* Check if the given basic map is obviously single-valued.
10927 * In particular, for each output dimension, check that there is
10928 * an equality that defines the output dimension in terms of
10929 * earlier dimensions.
10931 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10933 int i;
10934 unsigned n_out;
10936 if (!bmap)
10937 return isl_bool_error;
10939 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10941 for (i = 0; i < n_out; ++i) {
10942 int eq;
10944 eq = isl_basic_map_output_defining_equality(bmap, i,
10945 NULL, NULL);
10946 if (eq < 0)
10947 return isl_bool_error;
10948 if (eq >= bmap->n_eq)
10949 return isl_bool_false;
10952 return isl_bool_true;
10955 /* Check if the given basic map is single-valued.
10956 * We simply compute
10958 * M \circ M^-1
10960 * and check if the result is a subset of the identity mapping.
10962 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10964 isl_space *space;
10965 isl_basic_map *test;
10966 isl_basic_map *id;
10967 isl_bool sv;
10969 sv = isl_basic_map_plain_is_single_valued(bmap);
10970 if (sv < 0 || sv)
10971 return sv;
10973 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10974 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10976 space = isl_basic_map_get_space(bmap);
10977 space = isl_space_map_from_set(isl_space_range(space));
10978 id = isl_basic_map_identity(space);
10980 sv = isl_basic_map_is_subset(test, id);
10982 isl_basic_map_free(test);
10983 isl_basic_map_free(id);
10985 return sv;
10988 /* Check if the given map is obviously single-valued.
10990 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10992 if (!map)
10993 return isl_bool_error;
10994 if (map->n == 0)
10995 return isl_bool_true;
10996 if (map->n >= 2)
10997 return isl_bool_false;
10999 return isl_basic_map_plain_is_single_valued(map->p[0]);
11002 /* Check if the given map is single-valued.
11003 * We simply compute
11005 * M \circ M^-1
11007 * and check if the result is a subset of the identity mapping.
11009 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11011 isl_space *dim;
11012 isl_map *test;
11013 isl_map *id;
11014 isl_bool sv;
11016 sv = isl_map_plain_is_single_valued(map);
11017 if (sv < 0 || sv)
11018 return sv;
11020 test = isl_map_reverse(isl_map_copy(map));
11021 test = isl_map_apply_range(test, isl_map_copy(map));
11023 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11024 id = isl_map_identity(dim);
11026 sv = isl_map_is_subset(test, id);
11028 isl_map_free(test);
11029 isl_map_free(id);
11031 return sv;
11034 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11036 isl_bool in;
11038 map = isl_map_copy(map);
11039 map = isl_map_reverse(map);
11040 in = isl_map_is_single_valued(map);
11041 isl_map_free(map);
11043 return in;
11046 /* Check if the given map is obviously injective.
11048 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11050 isl_bool in;
11052 map = isl_map_copy(map);
11053 map = isl_map_reverse(map);
11054 in = isl_map_plain_is_single_valued(map);
11055 isl_map_free(map);
11057 return in;
11060 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11062 isl_bool sv;
11064 sv = isl_map_is_single_valued(map);
11065 if (sv < 0 || !sv)
11066 return sv;
11068 return isl_map_is_injective(map);
11071 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11073 return isl_map_is_single_valued(set_to_map(set));
11076 /* Does "map" only map elements to themselves?
11078 * If the domain and range spaces are different, then "map"
11079 * is considered not to be an identity relation, even if it is empty.
11080 * Otherwise, construct the maximal identity relation and
11081 * check whether "map" is a subset of this relation.
11083 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11085 isl_space *space;
11086 isl_map *id;
11087 isl_bool equal, is_identity;
11089 space = isl_map_get_space(map);
11090 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11091 isl_space_free(space);
11092 if (equal < 0 || !equal)
11093 return equal;
11095 id = isl_map_identity(isl_map_get_space(map));
11096 is_identity = isl_map_is_subset(map, id);
11097 isl_map_free(id);
11099 return is_identity;
11102 int isl_map_is_translation(__isl_keep isl_map *map)
11104 int ok;
11105 isl_set *delta;
11107 delta = isl_map_deltas(isl_map_copy(map));
11108 ok = isl_set_is_singleton(delta);
11109 isl_set_free(delta);
11111 return ok;
11114 static int unique(isl_int *p, unsigned pos, unsigned len)
11116 if (isl_seq_first_non_zero(p, pos) != -1)
11117 return 0;
11118 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11119 return 0;
11120 return 1;
11123 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11125 int i, j;
11126 unsigned nvar;
11127 unsigned ovar;
11129 if (!bset)
11130 return isl_bool_error;
11132 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11133 return isl_bool_false;
11135 nvar = isl_basic_set_dim(bset, isl_dim_set);
11136 ovar = isl_space_offset(bset->dim, isl_dim_set);
11137 for (j = 0; j < nvar; ++j) {
11138 int lower = 0, upper = 0;
11139 for (i = 0; i < bset->n_eq; ++i) {
11140 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11141 continue;
11142 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11143 return isl_bool_false;
11144 break;
11146 if (i < bset->n_eq)
11147 continue;
11148 for (i = 0; i < bset->n_ineq; ++i) {
11149 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11150 continue;
11151 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11152 return isl_bool_false;
11153 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11154 lower = 1;
11155 else
11156 upper = 1;
11158 if (!lower || !upper)
11159 return isl_bool_false;
11162 return isl_bool_true;
11165 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11167 if (!set)
11168 return isl_bool_error;
11169 if (set->n != 1)
11170 return isl_bool_false;
11172 return isl_basic_set_is_box(set->p[0]);
11175 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11177 if (!bset)
11178 return isl_bool_error;
11180 return isl_space_is_wrapping(bset->dim);
11183 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11185 if (!set)
11186 return isl_bool_error;
11188 return isl_space_is_wrapping(set->dim);
11191 /* Modify the space of "map" through a call to "change".
11192 * If "can_change" is set (not NULL), then first call it to check
11193 * if the modification is allowed, printing the error message "cannot_change"
11194 * if it is not.
11196 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11197 isl_bool (*can_change)(__isl_keep isl_map *map),
11198 const char *cannot_change,
11199 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11201 isl_bool ok;
11202 isl_space *space;
11204 if (!map)
11205 return NULL;
11207 ok = can_change ? can_change(map) : isl_bool_true;
11208 if (ok < 0)
11209 return isl_map_free(map);
11210 if (!ok)
11211 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11212 return isl_map_free(map));
11214 space = change(isl_map_get_space(map));
11215 map = isl_map_reset_space(map, space);
11217 return map;
11220 /* Is the domain of "map" a wrapped relation?
11222 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11224 if (!map)
11225 return isl_bool_error;
11227 return isl_space_domain_is_wrapping(map->dim);
11230 /* Does "map" have a wrapped relation in both domain and range?
11232 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11234 return isl_space_is_product(isl_map_peek_space(map));
11237 /* Is the range of "map" a wrapped relation?
11239 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11241 if (!map)
11242 return isl_bool_error;
11244 return isl_space_range_is_wrapping(map->dim);
11247 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11249 bmap = isl_basic_map_cow(bmap);
11250 if (!bmap)
11251 return NULL;
11253 bmap->dim = isl_space_wrap(bmap->dim);
11254 if (!bmap->dim)
11255 goto error;
11257 bmap = isl_basic_map_finalize(bmap);
11259 return bset_from_bmap(bmap);
11260 error:
11261 isl_basic_map_free(bmap);
11262 return NULL;
11265 /* Given a map A -> B, return the set (A -> B).
11267 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11269 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11272 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11274 bset = isl_basic_set_cow(bset);
11275 if (!bset)
11276 return NULL;
11278 bset->dim = isl_space_unwrap(bset->dim);
11279 if (!bset->dim)
11280 goto error;
11282 bset = isl_basic_set_finalize(bset);
11284 return bset_to_bmap(bset);
11285 error:
11286 isl_basic_set_free(bset);
11287 return NULL;
11290 /* Given a set (A -> B), return the map A -> B.
11291 * Error out if "set" is not of the form (A -> B).
11293 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11295 return isl_map_change_space(set, &isl_set_is_wrapping,
11296 "not a wrapping set", &isl_space_unwrap);
11299 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11300 enum isl_dim_type type)
11302 if (!bmap)
11303 return NULL;
11305 if (!isl_space_is_named_or_nested(bmap->dim, type))
11306 return bmap;
11308 bmap = isl_basic_map_cow(bmap);
11309 if (!bmap)
11310 return NULL;
11312 bmap->dim = isl_space_reset(bmap->dim, type);
11313 if (!bmap->dim)
11314 goto error;
11316 bmap = isl_basic_map_finalize(bmap);
11318 return bmap;
11319 error:
11320 isl_basic_map_free(bmap);
11321 return NULL;
11324 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11325 enum isl_dim_type type)
11327 int i;
11329 if (!map)
11330 return NULL;
11332 if (!isl_space_is_named_or_nested(map->dim, type))
11333 return map;
11335 map = isl_map_cow(map);
11336 if (!map)
11337 return NULL;
11339 for (i = 0; i < map->n; ++i) {
11340 map->p[i] = isl_basic_map_reset(map->p[i], type);
11341 if (!map->p[i])
11342 goto error;
11344 map->dim = isl_space_reset(map->dim, type);
11345 if (!map->dim)
11346 goto error;
11348 return map;
11349 error:
11350 isl_map_free(map);
11351 return NULL;
11354 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11356 if (!bmap)
11357 return NULL;
11359 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11360 return bmap;
11362 bmap = isl_basic_map_cow(bmap);
11363 if (!bmap)
11364 return NULL;
11366 bmap->dim = isl_space_flatten(bmap->dim);
11367 if (!bmap->dim)
11368 goto error;
11370 bmap = isl_basic_map_finalize(bmap);
11372 return bmap;
11373 error:
11374 isl_basic_map_free(bmap);
11375 return NULL;
11378 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11380 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11383 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11384 __isl_take isl_basic_map *bmap)
11386 if (!bmap)
11387 return NULL;
11389 if (!bmap->dim->nested[0])
11390 return bmap;
11392 bmap = isl_basic_map_cow(bmap);
11393 if (!bmap)
11394 return NULL;
11396 bmap->dim = isl_space_flatten_domain(bmap->dim);
11397 if (!bmap->dim)
11398 goto error;
11400 bmap = isl_basic_map_finalize(bmap);
11402 return bmap;
11403 error:
11404 isl_basic_map_free(bmap);
11405 return NULL;
11408 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11409 __isl_take isl_basic_map *bmap)
11411 if (!bmap)
11412 return NULL;
11414 if (!bmap->dim->nested[1])
11415 return bmap;
11417 bmap = isl_basic_map_cow(bmap);
11418 if (!bmap)
11419 return NULL;
11421 bmap->dim = isl_space_flatten_range(bmap->dim);
11422 if (!bmap->dim)
11423 goto error;
11425 bmap = isl_basic_map_finalize(bmap);
11427 return bmap;
11428 error:
11429 isl_basic_map_free(bmap);
11430 return NULL;
11433 /* Remove any internal structure from the spaces of domain and range of "map".
11435 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11437 if (!map)
11438 return NULL;
11440 if (!map->dim->nested[0] && !map->dim->nested[1])
11441 return map;
11443 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11446 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11448 return set_from_map(isl_map_flatten(set_to_map(set)));
11451 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11453 isl_space *space, *flat_space;
11454 isl_map *map;
11456 space = isl_set_get_space(set);
11457 flat_space = isl_space_flatten(isl_space_copy(space));
11458 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11459 flat_space));
11460 map = isl_map_intersect_domain(map, set);
11462 return map;
11465 /* Remove any internal structure from the space of the domain of "map".
11467 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11469 if (!map)
11470 return NULL;
11472 if (!map->dim->nested[0])
11473 return map;
11475 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11478 /* Remove any internal structure from the space of the range of "map".
11480 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11482 if (!map)
11483 return NULL;
11485 if (!map->dim->nested[1])
11486 return map;
11488 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11491 /* Reorder the dimensions of "bmap" according to the given dim_map
11492 * and set the dimension specification to "space" and
11493 * perform Gaussian elimination on the result.
11495 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11496 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11498 isl_basic_map *res;
11499 unsigned flags;
11500 unsigned n_div;
11502 if (!bmap || !space || !dim_map)
11503 goto error;
11505 flags = bmap->flags;
11506 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11507 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11508 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11509 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11510 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11511 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11512 if (res)
11513 res->flags = flags;
11514 res = isl_basic_map_gauss(res, NULL);
11515 res = isl_basic_map_finalize(res);
11516 return res;
11517 error:
11518 isl_dim_map_free(dim_map);
11519 isl_basic_map_free(bmap);
11520 isl_space_free(space);
11521 return NULL;
11524 /* Reorder the dimensions of "map" according to given reordering.
11526 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11527 __isl_take isl_reordering *r)
11529 int i;
11530 struct isl_dim_map *dim_map;
11532 map = isl_map_cow(map);
11533 dim_map = isl_dim_map_from_reordering(r);
11534 if (!map || !r || !dim_map)
11535 goto error;
11537 for (i = 0; i < map->n; ++i) {
11538 struct isl_dim_map *dim_map_i;
11539 isl_space *space;
11541 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11543 space = isl_reordering_get_space(r);
11544 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11546 if (!map->p[i])
11547 goto error;
11550 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11551 map = isl_map_unmark_normalized(map);
11553 isl_reordering_free(r);
11554 isl_dim_map_free(dim_map);
11555 return map;
11556 error:
11557 isl_dim_map_free(dim_map);
11558 isl_map_free(map);
11559 isl_reordering_free(r);
11560 return NULL;
11563 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11564 __isl_take isl_reordering *r)
11566 return set_from_map(isl_map_realign(set_to_map(set), r));
11569 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11570 __isl_take isl_space *model)
11572 isl_ctx *ctx;
11573 isl_bool aligned;
11575 if (!map || !model)
11576 goto error;
11578 ctx = isl_space_get_ctx(model);
11579 if (!isl_space_has_named_params(model))
11580 isl_die(ctx, isl_error_invalid,
11581 "model has unnamed parameters", goto error);
11582 if (isl_map_check_named_params(map) < 0)
11583 goto error;
11584 aligned = isl_map_space_has_equal_params(map, model);
11585 if (aligned < 0)
11586 goto error;
11587 if (!aligned) {
11588 isl_reordering *exp;
11590 exp = isl_parameter_alignment_reordering(map->dim, model);
11591 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11592 map = isl_map_realign(map, exp);
11595 isl_space_free(model);
11596 return map;
11597 error:
11598 isl_space_free(model);
11599 isl_map_free(map);
11600 return NULL;
11603 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11604 __isl_take isl_space *model)
11606 return isl_map_align_params(set, model);
11609 /* Align the parameters of "bmap" to those of "model", introducing
11610 * additional parameters if needed.
11612 __isl_give isl_basic_map *isl_basic_map_align_params(
11613 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11615 isl_ctx *ctx;
11616 isl_bool equal_params;
11618 if (!bmap || !model)
11619 goto error;
11621 ctx = isl_space_get_ctx(model);
11622 if (!isl_space_has_named_params(model))
11623 isl_die(ctx, isl_error_invalid,
11624 "model has unnamed parameters", goto error);
11625 if (isl_basic_map_check_named_params(bmap) < 0)
11626 goto error;
11627 equal_params = isl_space_has_equal_params(bmap->dim, model);
11628 if (equal_params < 0)
11629 goto error;
11630 if (!equal_params) {
11631 isl_reordering *exp;
11632 struct isl_dim_map *dim_map;
11634 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11635 exp = isl_reordering_extend_space(exp,
11636 isl_basic_map_get_space(bmap));
11637 dim_map = isl_dim_map_from_reordering(exp);
11638 bmap = isl_basic_map_realign(bmap,
11639 isl_reordering_get_space(exp),
11640 isl_dim_map_extend(dim_map, bmap));
11641 isl_reordering_free(exp);
11642 isl_dim_map_free(dim_map);
11645 isl_space_free(model);
11646 return bmap;
11647 error:
11648 isl_space_free(model);
11649 isl_basic_map_free(bmap);
11650 return NULL;
11653 /* Do "bset" and "space" have the same parameters?
11655 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11656 __isl_keep isl_space *space)
11658 isl_space *bset_space;
11660 bset_space = isl_basic_set_peek_space(bset);
11661 return isl_space_has_equal_params(bset_space, space);
11664 /* Do "map" and "space" have the same parameters?
11666 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11667 __isl_keep isl_space *space)
11669 isl_space *map_space;
11671 map_space = isl_map_peek_space(map);
11672 return isl_space_has_equal_params(map_space, space);
11675 /* Do "set" and "space" have the same parameters?
11677 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11678 __isl_keep isl_space *space)
11680 return isl_map_space_has_equal_params(set_to_map(set), space);
11683 /* Align the parameters of "bset" to those of "model", introducing
11684 * additional parameters if needed.
11686 __isl_give isl_basic_set *isl_basic_set_align_params(
11687 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11689 return isl_basic_map_align_params(bset, model);
11692 /* Drop all parameters not referenced by "map".
11694 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11696 int i;
11698 if (isl_map_check_named_params(map) < 0)
11699 return isl_map_free(map);
11701 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11702 isl_bool involves;
11704 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11705 if (involves < 0)
11706 return isl_map_free(map);
11707 if (!involves)
11708 map = isl_map_project_out(map, isl_dim_param, i, 1);
11711 return map;
11714 /* Drop all parameters not referenced by "set".
11716 __isl_give isl_set *isl_set_drop_unused_params(
11717 __isl_take isl_set *set)
11719 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11722 /* Drop all parameters not referenced by "bmap".
11724 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11725 __isl_take isl_basic_map *bmap)
11727 int i;
11729 if (isl_basic_map_check_named_params(bmap) < 0)
11730 return isl_basic_map_free(bmap);
11732 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11733 isl_bool involves;
11735 involves = isl_basic_map_involves_dims(bmap,
11736 isl_dim_param, i, 1);
11737 if (involves < 0)
11738 return isl_basic_map_free(bmap);
11739 if (!involves)
11740 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11743 return bmap;
11746 /* Drop all parameters not referenced by "bset".
11748 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11749 __isl_take isl_basic_set *bset)
11751 return bset_from_bmap(isl_basic_map_drop_unused_params(
11752 bset_to_bmap(bset)));
11755 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11756 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11757 enum isl_dim_type c2, enum isl_dim_type c3,
11758 enum isl_dim_type c4, enum isl_dim_type c5)
11760 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11761 struct isl_mat *mat;
11762 int i, j, k;
11763 int pos;
11765 if (!bmap)
11766 return NULL;
11767 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11768 isl_basic_map_total_dim(bmap) + 1);
11769 if (!mat)
11770 return NULL;
11771 for (i = 0; i < bmap->n_eq; ++i)
11772 for (j = 0, pos = 0; j < 5; ++j) {
11773 int off = isl_basic_map_offset(bmap, c[j]);
11774 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11775 isl_int_set(mat->row[i][pos],
11776 bmap->eq[i][off + k]);
11777 ++pos;
11781 return mat;
11784 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11785 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11786 enum isl_dim_type c2, enum isl_dim_type c3,
11787 enum isl_dim_type c4, enum isl_dim_type c5)
11789 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11790 struct isl_mat *mat;
11791 int i, j, k;
11792 int pos;
11794 if (!bmap)
11795 return NULL;
11796 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11797 isl_basic_map_total_dim(bmap) + 1);
11798 if (!mat)
11799 return NULL;
11800 for (i = 0; i < bmap->n_ineq; ++i)
11801 for (j = 0, pos = 0; j < 5; ++j) {
11802 int off = isl_basic_map_offset(bmap, c[j]);
11803 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11804 isl_int_set(mat->row[i][pos],
11805 bmap->ineq[i][off + k]);
11806 ++pos;
11810 return mat;
11813 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11814 __isl_take isl_space *space,
11815 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11816 enum isl_dim_type c2, enum isl_dim_type c3,
11817 enum isl_dim_type c4, enum isl_dim_type c5)
11819 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11820 isl_basic_map *bmap = NULL;
11821 unsigned total;
11822 unsigned extra;
11823 int i, j, k, l;
11824 int pos;
11826 if (!space || !eq || !ineq)
11827 goto error;
11829 if (eq->n_col != ineq->n_col)
11830 isl_die(space->ctx, isl_error_invalid,
11831 "equalities and inequalities matrices should have "
11832 "same number of columns", goto error);
11834 total = 1 + isl_space_dim(space, isl_dim_all);
11836 if (eq->n_col < total)
11837 isl_die(space->ctx, isl_error_invalid,
11838 "number of columns too small", goto error);
11840 extra = eq->n_col - total;
11842 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11843 eq->n_row, ineq->n_row);
11844 if (!bmap)
11845 goto error;
11846 for (i = 0; i < extra; ++i) {
11847 k = isl_basic_map_alloc_div(bmap);
11848 if (k < 0)
11849 goto error;
11850 isl_int_set_si(bmap->div[k][0], 0);
11852 for (i = 0; i < eq->n_row; ++i) {
11853 l = isl_basic_map_alloc_equality(bmap);
11854 if (l < 0)
11855 goto error;
11856 for (j = 0, pos = 0; j < 5; ++j) {
11857 int off = isl_basic_map_offset(bmap, c[j]);
11858 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11859 isl_int_set(bmap->eq[l][off + k],
11860 eq->row[i][pos]);
11861 ++pos;
11865 for (i = 0; i < ineq->n_row; ++i) {
11866 l = isl_basic_map_alloc_inequality(bmap);
11867 if (l < 0)
11868 goto error;
11869 for (j = 0, pos = 0; j < 5; ++j) {
11870 int off = isl_basic_map_offset(bmap, c[j]);
11871 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11872 isl_int_set(bmap->ineq[l][off + k],
11873 ineq->row[i][pos]);
11874 ++pos;
11879 isl_space_free(space);
11880 isl_mat_free(eq);
11881 isl_mat_free(ineq);
11883 bmap = isl_basic_map_simplify(bmap);
11884 return isl_basic_map_finalize(bmap);
11885 error:
11886 isl_space_free(space);
11887 isl_mat_free(eq);
11888 isl_mat_free(ineq);
11889 isl_basic_map_free(bmap);
11890 return NULL;
11893 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11894 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11895 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11897 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11898 c1, c2, c3, c4, isl_dim_in);
11901 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11902 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11903 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11905 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11906 c1, c2, c3, c4, isl_dim_in);
11909 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11910 __isl_take isl_space *dim,
11911 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11912 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11914 isl_basic_map *bmap;
11915 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11916 c1, c2, c3, c4, isl_dim_in);
11917 return bset_from_bmap(bmap);
11920 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11922 if (!bmap)
11923 return isl_bool_error;
11925 return isl_space_can_zip(bmap->dim);
11928 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11930 if (!map)
11931 return isl_bool_error;
11933 return isl_space_can_zip(map->dim);
11936 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11937 * (A -> C) -> (B -> D).
11939 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11941 unsigned pos;
11942 unsigned n1;
11943 unsigned n2;
11945 if (!bmap)
11946 return NULL;
11948 if (!isl_basic_map_can_zip(bmap))
11949 isl_die(bmap->ctx, isl_error_invalid,
11950 "basic map cannot be zipped", goto error);
11951 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11952 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11953 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11954 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11955 bmap = isl_basic_map_cow(bmap);
11956 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11957 if (!bmap)
11958 return NULL;
11959 bmap->dim = isl_space_zip(bmap->dim);
11960 if (!bmap->dim)
11961 goto error;
11962 bmap = isl_basic_map_mark_final(bmap);
11963 return bmap;
11964 error:
11965 isl_basic_map_free(bmap);
11966 return NULL;
11969 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11970 * (A -> C) -> (B -> D).
11972 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11974 int i;
11976 if (!map)
11977 return NULL;
11979 if (!isl_map_can_zip(map))
11980 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11981 goto error);
11983 map = isl_map_cow(map);
11984 if (!map)
11985 return NULL;
11987 for (i = 0; i < map->n; ++i) {
11988 map->p[i] = isl_basic_map_zip(map->p[i]);
11989 if (!map->p[i])
11990 goto error;
11993 map->dim = isl_space_zip(map->dim);
11994 if (!map->dim)
11995 goto error;
11997 return map;
11998 error:
11999 isl_map_free(map);
12000 return NULL;
12003 /* Can we apply isl_basic_map_curry to "bmap"?
12004 * That is, does it have a nested relation in its domain?
12006 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12008 if (!bmap)
12009 return isl_bool_error;
12011 return isl_space_can_curry(bmap->dim);
12014 /* Can we apply isl_map_curry to "map"?
12015 * That is, does it have a nested relation in its domain?
12017 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12019 if (!map)
12020 return isl_bool_error;
12022 return isl_space_can_curry(map->dim);
12025 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12026 * A -> (B -> C).
12028 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12031 if (!bmap)
12032 return NULL;
12034 if (!isl_basic_map_can_curry(bmap))
12035 isl_die(bmap->ctx, isl_error_invalid,
12036 "basic map cannot be curried", goto error);
12037 bmap = isl_basic_map_cow(bmap);
12038 if (!bmap)
12039 return NULL;
12040 bmap->dim = isl_space_curry(bmap->dim);
12041 if (!bmap->dim)
12042 goto error;
12043 bmap = isl_basic_map_mark_final(bmap);
12044 return bmap;
12045 error:
12046 isl_basic_map_free(bmap);
12047 return NULL;
12050 /* Given a map (A -> B) -> C, return the corresponding map
12051 * A -> (B -> C).
12053 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12055 return isl_map_change_space(map, &isl_map_can_curry,
12056 "map cannot be curried", &isl_space_curry);
12059 /* Can isl_map_range_curry be applied to "map"?
12060 * That is, does it have a nested relation in its range,
12061 * the domain of which is itself a nested relation?
12063 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12065 if (!map)
12066 return isl_bool_error;
12068 return isl_space_can_range_curry(map->dim);
12071 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12072 * A -> (B -> (C -> D)).
12074 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12076 return isl_map_change_space(map, &isl_map_can_range_curry,
12077 "map range cannot be curried",
12078 &isl_space_range_curry);
12081 /* Can we apply isl_basic_map_uncurry to "bmap"?
12082 * That is, does it have a nested relation in its domain?
12084 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12086 if (!bmap)
12087 return isl_bool_error;
12089 return isl_space_can_uncurry(bmap->dim);
12092 /* Can we apply isl_map_uncurry to "map"?
12093 * That is, does it have a nested relation in its domain?
12095 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12097 if (!map)
12098 return isl_bool_error;
12100 return isl_space_can_uncurry(map->dim);
12103 /* Given a basic map A -> (B -> C), return the corresponding basic map
12104 * (A -> B) -> C.
12106 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12109 if (!bmap)
12110 return NULL;
12112 if (!isl_basic_map_can_uncurry(bmap))
12113 isl_die(bmap->ctx, isl_error_invalid,
12114 "basic map cannot be uncurried",
12115 return isl_basic_map_free(bmap));
12116 bmap = isl_basic_map_cow(bmap);
12117 if (!bmap)
12118 return NULL;
12119 bmap->dim = isl_space_uncurry(bmap->dim);
12120 if (!bmap->dim)
12121 return isl_basic_map_free(bmap);
12122 bmap = isl_basic_map_mark_final(bmap);
12123 return bmap;
12126 /* Given a map A -> (B -> C), return the corresponding map
12127 * (A -> B) -> C.
12129 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12131 return isl_map_change_space(map, &isl_map_can_uncurry,
12132 "map cannot be uncurried", &isl_space_uncurry);
12135 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12136 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12138 return isl_map_equate(set, type1, pos1, type2, pos2);
12141 /* Construct a basic map where the given dimensions are equal to each other.
12143 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12144 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12146 isl_basic_map *bmap = NULL;
12147 int i;
12149 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12150 isl_space_check_range(space, type2, pos2, 1) < 0)
12151 goto error;
12153 if (type1 == type2 && pos1 == pos2)
12154 return isl_basic_map_universe(space);
12156 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12157 i = isl_basic_map_alloc_equality(bmap);
12158 if (i < 0)
12159 goto error;
12160 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12161 pos1 += isl_basic_map_offset(bmap, type1);
12162 pos2 += isl_basic_map_offset(bmap, type2);
12163 isl_int_set_si(bmap->eq[i][pos1], -1);
12164 isl_int_set_si(bmap->eq[i][pos2], 1);
12165 bmap = isl_basic_map_finalize(bmap);
12166 isl_space_free(space);
12167 return bmap;
12168 error:
12169 isl_space_free(space);
12170 isl_basic_map_free(bmap);
12171 return NULL;
12174 /* Add a constraint imposing that the given two dimensions are equal.
12176 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12177 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12179 isl_basic_map *eq;
12181 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12183 bmap = isl_basic_map_intersect(bmap, eq);
12185 return bmap;
12188 /* Add a constraint imposing that the given two dimensions are equal.
12190 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12191 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12193 isl_basic_map *bmap;
12195 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12197 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12199 return map;
12202 /* Add a constraint imposing that the given two dimensions have opposite values.
12204 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12205 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12207 isl_basic_map *bmap = NULL;
12208 int i;
12210 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12211 return isl_map_free(map);
12212 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12213 return isl_map_free(map);
12215 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12216 i = isl_basic_map_alloc_equality(bmap);
12217 if (i < 0)
12218 goto error;
12219 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12220 pos1 += isl_basic_map_offset(bmap, type1);
12221 pos2 += isl_basic_map_offset(bmap, type2);
12222 isl_int_set_si(bmap->eq[i][pos1], 1);
12223 isl_int_set_si(bmap->eq[i][pos2], 1);
12224 bmap = isl_basic_map_finalize(bmap);
12226 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12228 return map;
12229 error:
12230 isl_basic_map_free(bmap);
12231 isl_map_free(map);
12232 return NULL;
12235 /* Construct a constraint imposing that the value of the first dimension is
12236 * greater than or equal to that of the second.
12238 static __isl_give isl_constraint *constraint_order_ge(
12239 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12240 enum isl_dim_type type2, int pos2)
12242 isl_constraint *c;
12244 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12245 isl_space_check_range(space, type2, pos2, 1) < 0)
12246 space = isl_space_free(space);
12247 if (!space)
12248 return NULL;
12250 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12252 if (type1 == type2 && pos1 == pos2)
12253 return c;
12255 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12256 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12258 return c;
12261 /* Add a constraint imposing that the value of the first dimension is
12262 * greater than or equal to that of the second.
12264 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12265 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12267 isl_constraint *c;
12268 isl_space *space;
12270 if (type1 == type2 && pos1 == pos2)
12271 return bmap;
12272 space = isl_basic_map_get_space(bmap);
12273 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12274 bmap = isl_basic_map_add_constraint(bmap, c);
12276 return bmap;
12279 /* Add a constraint imposing that the value of the first dimension is
12280 * greater than or equal to that of the second.
12282 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12283 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12285 isl_constraint *c;
12286 isl_space *space;
12288 if (type1 == type2 && pos1 == pos2)
12289 return map;
12290 space = isl_map_get_space(map);
12291 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12292 map = isl_map_add_constraint(map, c);
12294 return map;
12297 /* Add a constraint imposing that the value of the first dimension is
12298 * less than or equal to that of the second.
12300 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12301 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12303 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12306 /* Construct a basic map where the value of the first dimension is
12307 * greater than that of the second.
12309 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12310 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12312 isl_basic_map *bmap = NULL;
12313 int i;
12315 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12316 isl_space_check_range(space, type2, pos2, 1) < 0)
12317 goto error;
12319 if (type1 == type2 && pos1 == pos2)
12320 return isl_basic_map_empty(space);
12322 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12323 i = isl_basic_map_alloc_inequality(bmap);
12324 if (i < 0)
12325 return isl_basic_map_free(bmap);
12326 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12327 pos1 += isl_basic_map_offset(bmap, type1);
12328 pos2 += isl_basic_map_offset(bmap, type2);
12329 isl_int_set_si(bmap->ineq[i][pos1], 1);
12330 isl_int_set_si(bmap->ineq[i][pos2], -1);
12331 isl_int_set_si(bmap->ineq[i][0], -1);
12332 bmap = isl_basic_map_finalize(bmap);
12334 return bmap;
12335 error:
12336 isl_space_free(space);
12337 isl_basic_map_free(bmap);
12338 return NULL;
12341 /* Add a constraint imposing that the value of the first dimension is
12342 * greater than that of the second.
12344 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12345 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12347 isl_basic_map *gt;
12349 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12351 bmap = isl_basic_map_intersect(bmap, gt);
12353 return bmap;
12356 /* Add a constraint imposing that the value of the first dimension is
12357 * greater than that of the second.
12359 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12360 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12362 isl_basic_map *bmap;
12364 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12366 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12368 return map;
12371 /* Add a constraint imposing that the value of the first dimension is
12372 * smaller than that of the second.
12374 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12375 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12377 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12380 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12381 int pos)
12383 isl_aff *div;
12384 isl_local_space *ls;
12386 if (!bmap)
12387 return NULL;
12389 if (!isl_basic_map_divs_known(bmap))
12390 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12391 "some divs are unknown", return NULL);
12393 ls = isl_basic_map_get_local_space(bmap);
12394 div = isl_local_space_get_div(ls, pos);
12395 isl_local_space_free(ls);
12397 return div;
12400 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12401 int pos)
12403 return isl_basic_map_get_div(bset, pos);
12406 /* Plug in "subs" for dimension "type", "pos" of "bset".
12408 * Let i be the dimension to replace and let "subs" be of the form
12410 * f/d
12412 * Any integer division with a non-zero coefficient for i,
12414 * floor((a i + g)/m)
12416 * is replaced by
12418 * floor((a f + d g)/(m d))
12420 * Constraints of the form
12422 * a i + g
12424 * are replaced by
12426 * a f + d g
12428 * We currently require that "subs" is an integral expression.
12429 * Handling rational expressions may require us to add stride constraints
12430 * as we do in isl_basic_set_preimage_multi_aff.
12432 __isl_give isl_basic_set *isl_basic_set_substitute(
12433 __isl_take isl_basic_set *bset,
12434 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12436 int i;
12437 isl_int v;
12438 isl_ctx *ctx;
12440 if (bset && isl_basic_set_plain_is_empty(bset))
12441 return bset;
12443 bset = isl_basic_set_cow(bset);
12444 if (!bset || !subs)
12445 goto error;
12447 ctx = isl_basic_set_get_ctx(bset);
12448 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12449 isl_die(ctx, isl_error_invalid,
12450 "spaces don't match", goto error);
12451 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12452 isl_die(ctx, isl_error_unsupported,
12453 "cannot handle divs yet", goto error);
12454 if (!isl_int_is_one(subs->v->el[0]))
12455 isl_die(ctx, isl_error_invalid,
12456 "can only substitute integer expressions", goto error);
12458 pos += isl_basic_set_offset(bset, type);
12460 isl_int_init(v);
12462 for (i = 0; i < bset->n_eq; ++i) {
12463 if (isl_int_is_zero(bset->eq[i][pos]))
12464 continue;
12465 isl_int_set(v, bset->eq[i][pos]);
12466 isl_int_set_si(bset->eq[i][pos], 0);
12467 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12468 v, subs->v->el + 1, subs->v->size - 1);
12471 for (i = 0; i < bset->n_ineq; ++i) {
12472 if (isl_int_is_zero(bset->ineq[i][pos]))
12473 continue;
12474 isl_int_set(v, bset->ineq[i][pos]);
12475 isl_int_set_si(bset->ineq[i][pos], 0);
12476 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12477 v, subs->v->el + 1, subs->v->size - 1);
12480 for (i = 0; i < bset->n_div; ++i) {
12481 if (isl_int_is_zero(bset->div[i][1 + pos]))
12482 continue;
12483 isl_int_set(v, bset->div[i][1 + pos]);
12484 isl_int_set_si(bset->div[i][1 + pos], 0);
12485 isl_seq_combine(bset->div[i] + 1,
12486 subs->v->el[0], bset->div[i] + 1,
12487 v, subs->v->el + 1, subs->v->size - 1);
12488 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12491 isl_int_clear(v);
12493 bset = isl_basic_set_simplify(bset);
12494 return isl_basic_set_finalize(bset);
12495 error:
12496 isl_basic_set_free(bset);
12497 return NULL;
12500 /* Plug in "subs" for dimension "type", "pos" of "set".
12502 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12503 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12505 int i;
12507 if (set && isl_set_plain_is_empty(set))
12508 return set;
12510 set = isl_set_cow(set);
12511 if (!set || !subs)
12512 goto error;
12514 for (i = set->n - 1; i >= 0; --i) {
12515 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12516 set = set_from_map(remove_if_empty(set_to_map(set), i));
12517 if (!set)
12518 return NULL;
12521 return set;
12522 error:
12523 isl_set_free(set);
12524 return NULL;
12527 /* Check if the range of "ma" is compatible with the domain or range
12528 * (depending on "type") of "bmap".
12530 static isl_stat check_basic_map_compatible_range_multi_aff(
12531 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12532 __isl_keep isl_multi_aff *ma)
12534 isl_bool m;
12535 isl_space *ma_space;
12537 ma_space = isl_multi_aff_get_space(ma);
12539 m = isl_space_has_equal_params(bmap->dim, ma_space);
12540 if (m < 0)
12541 goto error;
12542 if (!m)
12543 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12544 "parameters don't match", goto error);
12545 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12546 if (m < 0)
12547 goto error;
12548 if (!m)
12549 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12550 "spaces don't match", goto error);
12552 isl_space_free(ma_space);
12553 return isl_stat_ok;
12554 error:
12555 isl_space_free(ma_space);
12556 return isl_stat_error;
12559 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12560 * coefficients before the transformed range of dimensions,
12561 * the "n_after" coefficients after the transformed range of dimensions
12562 * and the coefficients of the other divs in "bmap".
12564 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
12565 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12567 int i;
12568 int n_param;
12569 int n_set;
12570 isl_local_space *ls;
12572 if (n_div == 0)
12573 return bmap;
12575 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12576 if (!ls)
12577 return isl_basic_map_free(bmap);
12579 n_param = isl_local_space_dim(ls, isl_dim_param);
12580 n_set = isl_local_space_dim(ls, isl_dim_set);
12581 for (i = 0; i < n_div; ++i) {
12582 int o_bmap = 0, o_ls = 0;
12584 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12585 o_bmap += 1 + 1 + n_param;
12586 o_ls += 1 + 1 + n_param;
12587 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12588 o_bmap += n_before;
12589 isl_seq_cpy(bmap->div[i] + o_bmap,
12590 ls->div->row[i] + o_ls, n_set);
12591 o_bmap += n_set;
12592 o_ls += n_set;
12593 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12594 o_bmap += n_after;
12595 isl_seq_cpy(bmap->div[i] + o_bmap,
12596 ls->div->row[i] + o_ls, n_div);
12597 o_bmap += n_div;
12598 o_ls += n_div;
12599 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12600 bmap = isl_basic_map_add_div_constraints(bmap, i);
12601 if (!bmap)
12602 goto error;
12605 isl_local_space_free(ls);
12606 return bmap;
12607 error:
12608 isl_local_space_free(ls);
12609 return isl_basic_map_free(bmap);
12612 /* How many stride constraints does "ma" enforce?
12613 * That is, how many of the affine expressions have a denominator
12614 * different from one?
12616 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12618 int i;
12619 int strides = 0;
12621 for (i = 0; i < ma->n; ++i)
12622 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12623 strides++;
12625 return strides;
12628 /* For each affine expression in ma of the form
12630 * x_i = (f_i y + h_i)/m_i
12632 * with m_i different from one, add a constraint to "bmap"
12633 * of the form
12635 * f_i y + h_i = m_i alpha_i
12637 * with alpha_i an additional existentially quantified variable.
12639 * The input variables of "ma" correspond to a subset of the variables
12640 * of "bmap". There are "n_before" variables in "bmap" before this
12641 * subset and "n_after" variables after this subset.
12642 * The integer divisions of the affine expressions in "ma" are assumed
12643 * to have been aligned. There are "n_div_ma" of them and
12644 * they appear first in "bmap", straight after the "n_after" variables.
12646 static __isl_give isl_basic_map *add_ma_strides(
12647 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12648 int n_before, int n_after, int n_div_ma)
12650 int i, k;
12651 int div;
12652 int total;
12653 int n_param;
12654 int n_in;
12656 total = isl_basic_map_total_dim(bmap);
12657 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12658 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12659 for (i = 0; i < ma->n; ++i) {
12660 int o_bmap = 0, o_ma = 1;
12662 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12663 continue;
12664 div = isl_basic_map_alloc_div(bmap);
12665 k = isl_basic_map_alloc_equality(bmap);
12666 if (div < 0 || k < 0)
12667 goto error;
12668 isl_int_set_si(bmap->div[div][0], 0);
12669 isl_seq_cpy(bmap->eq[k] + o_bmap,
12670 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12671 o_bmap += 1 + n_param;
12672 o_ma += 1 + n_param;
12673 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12674 o_bmap += n_before;
12675 isl_seq_cpy(bmap->eq[k] + o_bmap,
12676 ma->u.p[i]->v->el + o_ma, n_in);
12677 o_bmap += n_in;
12678 o_ma += n_in;
12679 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12680 o_bmap += n_after;
12681 isl_seq_cpy(bmap->eq[k] + o_bmap,
12682 ma->u.p[i]->v->el + o_ma, n_div_ma);
12683 o_bmap += n_div_ma;
12684 o_ma += n_div_ma;
12685 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12686 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12687 total++;
12690 return bmap;
12691 error:
12692 isl_basic_map_free(bmap);
12693 return NULL;
12696 /* Replace the domain or range space (depending on "type) of "space" by "set".
12698 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12699 enum isl_dim_type type, __isl_take isl_space *set)
12701 if (type == isl_dim_in) {
12702 space = isl_space_range(space);
12703 space = isl_space_map_from_domain_and_range(set, space);
12704 } else {
12705 space = isl_space_domain(space);
12706 space = isl_space_map_from_domain_and_range(space, set);
12709 return space;
12712 /* Compute the preimage of the domain or range (depending on "type")
12713 * of "bmap" under the function represented by "ma".
12714 * In other words, plug in "ma" in the domain or range of "bmap".
12715 * The result is a basic map that lives in the same space as "bmap"
12716 * except that the domain or range has been replaced by
12717 * the domain space of "ma".
12719 * If bmap is represented by
12721 * A(p) + S u + B x + T v + C(divs) >= 0,
12723 * where u and x are input and output dimensions if type == isl_dim_out
12724 * while x and v are input and output dimensions if type == isl_dim_in,
12725 * and ma is represented by
12727 * x = D(p) + F(y) + G(divs')
12729 * then the result is
12731 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12733 * The divs in the input set are similarly adjusted.
12734 * In particular
12736 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12738 * becomes
12740 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12741 * B_i G(divs') + c_i(divs))/n_i)
12743 * If bmap is not a rational map and if F(y) involves any denominators
12745 * x_i = (f_i y + h_i)/m_i
12747 * then additional constraints are added to ensure that we only
12748 * map back integer points. That is we enforce
12750 * f_i y + h_i = m_i alpha_i
12752 * with alpha_i an additional existentially quantified variable.
12754 * We first copy over the divs from "ma".
12755 * Then we add the modified constraints and divs from "bmap".
12756 * Finally, we add the stride constraints, if needed.
12758 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12759 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12760 __isl_take isl_multi_aff *ma)
12762 int i, k;
12763 isl_space *space;
12764 isl_basic_map *res = NULL;
12765 int n_before, n_after, n_div_bmap, n_div_ma;
12766 isl_int f, c1, c2, g;
12767 isl_bool rational;
12768 int strides;
12770 isl_int_init(f);
12771 isl_int_init(c1);
12772 isl_int_init(c2);
12773 isl_int_init(g);
12775 ma = isl_multi_aff_align_divs(ma);
12776 if (!bmap || !ma)
12777 goto error;
12778 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12779 goto error;
12781 if (type == isl_dim_in) {
12782 n_before = 0;
12783 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12784 } else {
12785 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12786 n_after = 0;
12788 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12789 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12791 space = isl_multi_aff_get_domain_space(ma);
12792 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12793 rational = isl_basic_map_is_rational(bmap);
12794 strides = rational ? 0 : multi_aff_strides(ma);
12795 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12796 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12797 if (rational)
12798 res = isl_basic_map_set_rational(res);
12800 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12801 if (isl_basic_map_alloc_div(res) < 0)
12802 goto error;
12804 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
12805 if (!res)
12806 goto error;
12808 for (i = 0; i < bmap->n_eq; ++i) {
12809 k = isl_basic_map_alloc_equality(res);
12810 if (k < 0)
12811 goto error;
12812 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12813 n_after, n_div_ma, n_div_bmap,
12814 f, c1, c2, g, 0) < 0)
12815 goto error;
12818 for (i = 0; i < bmap->n_ineq; ++i) {
12819 k = isl_basic_map_alloc_inequality(res);
12820 if (k < 0)
12821 goto error;
12822 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12823 n_after, n_div_ma, n_div_bmap,
12824 f, c1, c2, g, 0) < 0)
12825 goto error;
12828 for (i = 0; i < bmap->n_div; ++i) {
12829 if (isl_int_is_zero(bmap->div[i][0])) {
12830 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12831 continue;
12833 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12834 n_before, n_after, n_div_ma, n_div_bmap,
12835 f, c1, c2, g, 1) < 0)
12836 goto error;
12839 if (strides)
12840 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12842 isl_int_clear(f);
12843 isl_int_clear(c1);
12844 isl_int_clear(c2);
12845 isl_int_clear(g);
12846 isl_basic_map_free(bmap);
12847 isl_multi_aff_free(ma);
12848 res = isl_basic_map_simplify(res);
12849 return isl_basic_map_finalize(res);
12850 error:
12851 isl_int_clear(f);
12852 isl_int_clear(c1);
12853 isl_int_clear(c2);
12854 isl_int_clear(g);
12855 isl_basic_map_free(bmap);
12856 isl_multi_aff_free(ma);
12857 isl_basic_map_free(res);
12858 return NULL;
12861 /* Compute the preimage of "bset" under the function represented by "ma".
12862 * In other words, plug in "ma" in "bset". The result is a basic set
12863 * that lives in the domain space of "ma".
12865 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12866 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12868 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12871 /* Compute the preimage of the domain of "bmap" under the function
12872 * represented by "ma".
12873 * In other words, plug in "ma" in the domain of "bmap".
12874 * The result is a basic map that lives in the same space as "bmap"
12875 * except that the domain has been replaced by the domain space of "ma".
12877 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12878 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12880 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12883 /* Compute the preimage of the range of "bmap" under the function
12884 * represented by "ma".
12885 * In other words, plug in "ma" in the range of "bmap".
12886 * The result is a basic map that lives in the same space as "bmap"
12887 * except that the range has been replaced by the domain space of "ma".
12889 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12890 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12892 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12895 /* Check if the range of "ma" is compatible with the domain or range
12896 * (depending on "type") of "map".
12897 * Return isl_stat_error if anything is wrong.
12899 static isl_stat check_map_compatible_range_multi_aff(
12900 __isl_keep isl_map *map, enum isl_dim_type type,
12901 __isl_keep isl_multi_aff *ma)
12903 isl_bool m;
12904 isl_space *ma_space;
12906 ma_space = isl_multi_aff_get_space(ma);
12907 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12908 isl_space_free(ma_space);
12909 if (m < 0)
12910 return isl_stat_error;
12911 if (!m)
12912 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12913 "spaces don't match", return isl_stat_error);
12914 return isl_stat_ok;
12917 /* Compute the preimage of the domain or range (depending on "type")
12918 * of "map" under the function represented by "ma".
12919 * In other words, plug in "ma" in the domain or range of "map".
12920 * The result is a map that lives in the same space as "map"
12921 * except that the domain or range has been replaced by
12922 * the domain space of "ma".
12924 * The parameters are assumed to have been aligned.
12926 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12927 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12929 int i;
12930 isl_space *space;
12932 map = isl_map_cow(map);
12933 ma = isl_multi_aff_align_divs(ma);
12934 if (!map || !ma)
12935 goto error;
12936 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12937 goto error;
12939 for (i = 0; i < map->n; ++i) {
12940 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12941 isl_multi_aff_copy(ma));
12942 if (!map->p[i])
12943 goto error;
12946 space = isl_multi_aff_get_domain_space(ma);
12947 space = isl_space_set(isl_map_get_space(map), type, space);
12949 isl_space_free(map->dim);
12950 map->dim = space;
12951 if (!map->dim)
12952 goto error;
12954 isl_multi_aff_free(ma);
12955 if (map->n > 1)
12956 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12957 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12958 return map;
12959 error:
12960 isl_multi_aff_free(ma);
12961 isl_map_free(map);
12962 return NULL;
12965 /* Compute the preimage of the domain or range (depending on "type")
12966 * of "map" under the function represented by "ma".
12967 * In other words, plug in "ma" in the domain or range of "map".
12968 * The result is a map that lives in the same space as "map"
12969 * except that the domain or range has been replaced by
12970 * the domain space of "ma".
12972 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12973 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12975 isl_bool aligned;
12977 if (!map || !ma)
12978 goto error;
12980 aligned = isl_map_space_has_equal_params(map, ma->space);
12981 if (aligned < 0)
12982 goto error;
12983 if (aligned)
12984 return map_preimage_multi_aff(map, type, ma);
12986 if (isl_map_check_named_params(map) < 0)
12987 goto error;
12988 if (!isl_space_has_named_params(ma->space))
12989 isl_die(map->ctx, isl_error_invalid,
12990 "unaligned unnamed parameters", goto error);
12991 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12992 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12994 return map_preimage_multi_aff(map, type, ma);
12995 error:
12996 isl_multi_aff_free(ma);
12997 return isl_map_free(map);
13000 /* Compute the preimage of "set" under the function represented by "ma".
13001 * In other words, plug in "ma" in "set". The result is a set
13002 * that lives in the domain space of "ma".
13004 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13005 __isl_take isl_multi_aff *ma)
13007 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13010 /* Compute the preimage of the domain of "map" under the function
13011 * represented by "ma".
13012 * In other words, plug in "ma" in the domain of "map".
13013 * The result is a map that lives in the same space as "map"
13014 * except that the domain has been replaced by the domain space of "ma".
13016 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13017 __isl_take isl_multi_aff *ma)
13019 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13022 /* Compute the preimage of the range of "map" under the function
13023 * represented by "ma".
13024 * In other words, plug in "ma" in the range of "map".
13025 * The result is a map that lives in the same space as "map"
13026 * except that the range has been replaced by the domain space of "ma".
13028 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13029 __isl_take isl_multi_aff *ma)
13031 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13034 /* Compute the preimage of "map" under the function represented by "pma".
13035 * In other words, plug in "pma" in the domain or range of "map".
13036 * The result is a map that lives in the same space as "map",
13037 * except that the space of type "type" has been replaced by
13038 * the domain space of "pma".
13040 * The parameters of "map" and "pma" are assumed to have been aligned.
13042 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13043 __isl_take isl_map *map, enum isl_dim_type type,
13044 __isl_take isl_pw_multi_aff *pma)
13046 int i;
13047 isl_map *res;
13049 if (!pma)
13050 goto error;
13052 if (pma->n == 0) {
13053 isl_pw_multi_aff_free(pma);
13054 res = isl_map_empty(isl_map_get_space(map));
13055 isl_map_free(map);
13056 return res;
13059 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13060 isl_multi_aff_copy(pma->p[0].maff));
13061 if (type == isl_dim_in)
13062 res = isl_map_intersect_domain(res,
13063 isl_map_copy(pma->p[0].set));
13064 else
13065 res = isl_map_intersect_range(res,
13066 isl_map_copy(pma->p[0].set));
13068 for (i = 1; i < pma->n; ++i) {
13069 isl_map *res_i;
13071 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13072 isl_multi_aff_copy(pma->p[i].maff));
13073 if (type == isl_dim_in)
13074 res_i = isl_map_intersect_domain(res_i,
13075 isl_map_copy(pma->p[i].set));
13076 else
13077 res_i = isl_map_intersect_range(res_i,
13078 isl_map_copy(pma->p[i].set));
13079 res = isl_map_union(res, res_i);
13082 isl_pw_multi_aff_free(pma);
13083 isl_map_free(map);
13084 return res;
13085 error:
13086 isl_pw_multi_aff_free(pma);
13087 isl_map_free(map);
13088 return NULL;
13091 /* Compute the preimage of "map" under the function represented by "pma".
13092 * In other words, plug in "pma" in the domain or range of "map".
13093 * The result is a map that lives in the same space as "map",
13094 * except that the space of type "type" has been replaced by
13095 * the domain space of "pma".
13097 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13098 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13100 isl_bool aligned;
13102 if (!map || !pma)
13103 goto error;
13105 aligned = isl_map_space_has_equal_params(map, pma->dim);
13106 if (aligned < 0)
13107 goto error;
13108 if (aligned)
13109 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13111 if (isl_map_check_named_params(map) < 0)
13112 goto error;
13113 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13114 goto error;
13115 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13116 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13118 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13119 error:
13120 isl_pw_multi_aff_free(pma);
13121 return isl_map_free(map);
13124 /* Compute the preimage of "set" under the function represented by "pma".
13125 * In other words, plug in "pma" in "set". The result is a set
13126 * that lives in the domain space of "pma".
13128 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13129 __isl_take isl_pw_multi_aff *pma)
13131 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13134 /* Compute the preimage of the domain of "map" under the function
13135 * represented by "pma".
13136 * In other words, plug in "pma" in the domain of "map".
13137 * The result is a map that lives in the same space as "map",
13138 * except that domain space has been replaced by the domain space of "pma".
13140 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13141 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13143 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13146 /* Compute the preimage of the range of "map" under the function
13147 * represented by "pma".
13148 * In other words, plug in "pma" in the range of "map".
13149 * The result is a map that lives in the same space as "map",
13150 * except that range space has been replaced by the domain space of "pma".
13152 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13153 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13155 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13158 /* Compute the preimage of "map" under the function represented by "mpa".
13159 * In other words, plug in "mpa" in the domain or range of "map".
13160 * The result is a map that lives in the same space as "map",
13161 * except that the space of type "type" has been replaced by
13162 * the domain space of "mpa".
13164 * If the map does not involve any constraints that refer to the
13165 * dimensions of the substituted space, then the only possible
13166 * effect of "mpa" on the map is to map the space to a different space.
13167 * We create a separate isl_multi_aff to effectuate this change
13168 * in order to avoid spurious splitting of the map along the pieces
13169 * of "mpa".
13170 * If "mpa" has a non-trivial explicit domain, however,
13171 * then the full substitution should be performed.
13173 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13174 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13176 int n;
13177 isl_bool full;
13178 isl_pw_multi_aff *pma;
13180 if (!map || !mpa)
13181 goto error;
13183 n = isl_map_dim(map, type);
13184 full = isl_map_involves_dims(map, type, 0, n);
13185 if (full >= 0 && !full)
13186 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13187 if (full < 0)
13188 goto error;
13189 if (!full) {
13190 isl_space *space;
13191 isl_multi_aff *ma;
13193 space = isl_multi_pw_aff_get_space(mpa);
13194 isl_multi_pw_aff_free(mpa);
13195 ma = isl_multi_aff_zero(space);
13196 return isl_map_preimage_multi_aff(map, type, ma);
13199 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13200 return isl_map_preimage_pw_multi_aff(map, type, pma);
13201 error:
13202 isl_map_free(map);
13203 isl_multi_pw_aff_free(mpa);
13204 return NULL;
13207 /* Compute the preimage of "map" under the function represented by "mpa".
13208 * In other words, plug in "mpa" in the domain "map".
13209 * The result is a map that lives in the same space as "map",
13210 * except that domain space has been replaced by the domain space of "mpa".
13212 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13213 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13215 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13218 /* Compute the preimage of "set" by the function represented by "mpa".
13219 * In other words, plug in "mpa" in "set".
13221 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13222 __isl_take isl_multi_pw_aff *mpa)
13224 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13227 /* Return a copy of the equality constraints of "bset" as a matrix.
13229 __isl_give isl_mat *isl_basic_set_extract_equalities(
13230 __isl_keep isl_basic_set *bset)
13232 isl_ctx *ctx;
13233 unsigned total;
13235 if (!bset)
13236 return NULL;
13238 ctx = isl_basic_set_get_ctx(bset);
13239 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13240 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13243 /* Are the "n" "coefficients" starting at "first" of the integer division
13244 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13245 * to each other?
13246 * The "coefficient" at position 0 is the denominator.
13247 * The "coefficient" at position 1 is the constant term.
13249 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13250 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13251 unsigned first, unsigned n)
13253 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13254 return isl_bool_error;
13255 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13256 return isl_bool_error;
13257 return isl_seq_eq(bmap1->div[pos1] + first,
13258 bmap2->div[pos2] + first, n);
13261 /* Are the integer division expressions at position "pos1" in "bmap1" and
13262 * "pos2" in "bmap2" equal to each other, except that the constant terms
13263 * are different?
13265 isl_bool isl_basic_map_equal_div_expr_except_constant(
13266 __isl_keep isl_basic_map *bmap1, int pos1,
13267 __isl_keep isl_basic_map *bmap2, int pos2)
13269 isl_bool equal;
13270 unsigned total;
13272 if (!bmap1 || !bmap2)
13273 return isl_bool_error;
13274 total = isl_basic_map_total_dim(bmap1);
13275 if (total != isl_basic_map_total_dim(bmap2))
13276 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13277 "incomparable div expressions", return isl_bool_error);
13278 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13279 0, 1);
13280 if (equal < 0 || !equal)
13281 return equal;
13282 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13283 1, 1);
13284 if (equal < 0 || equal)
13285 return isl_bool_not(equal);
13286 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13287 2, total);
13290 /* Replace the numerator of the constant term of the integer division
13291 * expression at position "div" in "bmap" by "value".
13292 * The caller guarantees that this does not change the meaning
13293 * of the input.
13295 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13296 __isl_take isl_basic_map *bmap, int div, int value)
13298 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13299 return isl_basic_map_free(bmap);
13301 isl_int_set_si(bmap->div[div][1], value);
13303 return bmap;
13306 /* Is the point "inner" internal to inequality constraint "ineq"
13307 * of "bset"?
13308 * The point is considered to be internal to the inequality constraint,
13309 * if it strictly lies on the positive side of the inequality constraint,
13310 * or if it lies on the constraint and the constraint is lexico-positive.
13312 static isl_bool is_internal(__isl_keep isl_vec *inner,
13313 __isl_keep isl_basic_set *bset, int ineq)
13315 isl_ctx *ctx;
13316 int pos;
13317 unsigned total;
13319 if (!inner || !bset)
13320 return isl_bool_error;
13322 ctx = isl_basic_set_get_ctx(bset);
13323 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13324 &ctx->normalize_gcd);
13325 if (!isl_int_is_zero(ctx->normalize_gcd))
13326 return isl_int_is_nonneg(ctx->normalize_gcd);
13328 total = isl_basic_set_dim(bset, isl_dim_all);
13329 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13330 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13333 /* Tighten the inequality constraints of "bset" that are outward with respect
13334 * to the point "vec".
13335 * That is, tighten the constraints that are not satisfied by "vec".
13337 * "vec" is a point internal to some superset S of "bset" that is used
13338 * to make the subsets of S disjoint, by tightening one half of the constraints
13339 * that separate two subsets. In particular, the constraints of S
13340 * are all satisfied by "vec" and should not be tightened.
13341 * Of the internal constraints, those that have "vec" on the outside
13342 * are tightened. The shared facet is included in the adjacent subset
13343 * with the opposite constraint.
13344 * For constraints that saturate "vec", this criterion cannot be used
13345 * to determine which of the two sides should be tightened.
13346 * Instead, the sign of the first non-zero coefficient is used
13347 * to make this choice. Note that this second criterion is never used
13348 * on the constraints of S since "vec" is interior to "S".
13350 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13351 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13353 int j;
13355 bset = isl_basic_set_cow(bset);
13356 if (!bset)
13357 return NULL;
13358 for (j = 0; j < bset->n_ineq; ++j) {
13359 isl_bool internal;
13361 internal = is_internal(vec, bset, j);
13362 if (internal < 0)
13363 return isl_basic_set_free(bset);
13364 if (internal)
13365 continue;
13366 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13369 return bset;
13372 /* Replace the variables x of type "type" starting at "first" in "bmap"
13373 * by x' with x = M x' with M the matrix trans.
13374 * That is, replace the corresponding coefficients c by c M.
13376 * The transformation matrix should be a square matrix.
13378 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13379 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13380 __isl_take isl_mat *trans)
13382 unsigned pos;
13384 bmap = isl_basic_map_cow(bmap);
13385 if (!bmap || !trans)
13386 goto error;
13388 if (trans->n_row != trans->n_col)
13389 isl_die(trans->ctx, isl_error_invalid,
13390 "expecting square transformation matrix", goto error);
13391 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13392 goto error;
13394 pos = isl_basic_map_offset(bmap, type) + first;
13396 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13397 isl_mat_copy(trans)) < 0)
13398 goto error;
13399 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13400 isl_mat_copy(trans)) < 0)
13401 goto error;
13402 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13403 isl_mat_copy(trans)) < 0)
13404 goto error;
13406 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13407 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13409 isl_mat_free(trans);
13410 return bmap;
13411 error:
13412 isl_mat_free(trans);
13413 isl_basic_map_free(bmap);
13414 return NULL;
13417 /* Replace the variables x of type "type" starting at "first" in "bset"
13418 * by x' with x = M x' with M the matrix trans.
13419 * That is, replace the corresponding coefficients c by c M.
13421 * The transformation matrix should be a square matrix.
13423 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13424 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13425 __isl_take isl_mat *trans)
13427 return isl_basic_map_transform_dims(bset, type, first, trans);