drop deprecated isl_map_n_in
[isl.git] / isl_map.c
blob813828985f79408826e0bd6a7bb039ee8f24074e
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_n_in(__isl_keep const isl_basic_map *bmap)
207 return bmap ? bmap->dim->n_in : 0;
210 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
212 return bmap ? bmap->dim->n_out : 0;
215 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
217 return bmap ? bmap->dim->nparam : 0;
220 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
222 return bmap ? bmap->n_div : 0;
225 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
227 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
230 unsigned isl_map_n_out(__isl_keep const isl_map *map)
232 return map ? map->dim->n_out : 0;
235 unsigned isl_map_n_param(__isl_keep const isl_map *map)
237 return map ? map->dim->nparam : 0;
240 /* Return the number of equality constraints in the description of "bmap".
241 * Return -1 on error.
243 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
245 if (!bmap)
246 return -1;
247 return bmap->n_eq;
250 /* Return the number of equality constraints in the description of "bset".
251 * Return -1 on error.
253 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
255 return isl_basic_map_n_equality(bset_to_bmap(bset));
258 /* Return the number of inequality constraints in the description of "bmap".
259 * Return -1 on error.
261 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
263 if (!bmap)
264 return -1;
265 return bmap->n_ineq;
268 /* Return the number of inequality constraints in the description of "bset".
269 * Return -1 on error.
271 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
273 return isl_basic_map_n_inequality(bset_to_bmap(bset));
276 /* Do "bmap1" and "bmap2" have the same parameters?
278 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
279 __isl_keep isl_basic_map *bmap2)
281 isl_space *space1, *space2;
283 space1 = isl_basic_map_peek_space(bmap1);
284 space2 = isl_basic_map_peek_space(bmap2);
285 return isl_space_has_equal_params(space1, space2);
288 /* Do "map1" and "map2" have the same parameters?
290 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
291 __isl_keep isl_map *map2)
293 isl_space *space1, *space2;
295 space1 = isl_map_peek_space(map1);
296 space2 = isl_map_peek_space(map2);
297 return isl_space_has_equal_params(space1, space2);
300 /* Do "map" and "set" have the same parameters?
302 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
303 __isl_keep isl_set *set)
305 return isl_map_has_equal_params(map, set_to_map(set));
308 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
309 __isl_keep isl_set *set)
311 isl_bool m;
312 if (!map || !set)
313 return isl_bool_error;
314 m = isl_map_has_equal_params(map, set_to_map(set));
315 if (m < 0 || !m)
316 return m;
317 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
318 set->dim, isl_dim_set);
321 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
322 __isl_keep isl_basic_set *bset)
324 isl_bool m;
325 if (!bmap || !bset)
326 return isl_bool_error;
327 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
328 if (m < 0 || !m)
329 return m;
330 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
331 bset->dim, isl_dim_set);
334 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
335 __isl_keep isl_set *set)
337 isl_bool m;
338 if (!map || !set)
339 return isl_bool_error;
340 m = isl_map_has_equal_params(map, set_to_map(set));
341 if (m < 0 || !m)
342 return m;
343 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
344 set->dim, isl_dim_set);
347 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
348 __isl_keep isl_basic_set *bset)
350 isl_bool m;
351 if (!bmap || !bset)
352 return isl_bool_error;
353 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
354 if (m < 0 || !m)
355 return m;
356 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
357 bset->dim, isl_dim_set);
360 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
362 return bmap ? bmap->ctx : NULL;
365 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
367 return bset ? bset->ctx : NULL;
370 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
372 return map ? map->ctx : NULL;
375 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
377 return set ? set->ctx : NULL;
380 /* Return the space of "bmap".
382 __isl_keep isl_space *isl_basic_map_peek_space(
383 __isl_keep const isl_basic_map *bmap)
385 return bmap ? bmap->dim : NULL;
388 /* Return the space of "bset".
390 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
392 return isl_basic_map_peek_space(bset_to_bmap(bset));
395 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
397 return isl_space_copy(isl_basic_map_peek_space(bmap));
400 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
402 return isl_basic_map_get_space(bset_to_bmap(bset));
405 /* Extract the divs in "bmap" as a matrix.
407 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
409 int i;
410 isl_ctx *ctx;
411 isl_mat *div;
412 int v_div;
413 unsigned cols;
415 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
416 if (v_div < 0)
417 return NULL;
419 ctx = isl_basic_map_get_ctx(bmap);
420 cols = 1 + 1 + v_div + bmap->n_div;
421 div = isl_mat_alloc(ctx, bmap->n_div, cols);
422 if (!div)
423 return NULL;
425 for (i = 0; i < bmap->n_div; ++i)
426 isl_seq_cpy(div->row[i], bmap->div[i], cols);
428 return div;
431 /* Extract the divs in "bset" as a matrix.
433 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
435 return isl_basic_map_get_divs(bset);
438 __isl_give isl_local_space *isl_basic_map_get_local_space(
439 __isl_keep isl_basic_map *bmap)
441 isl_mat *div;
443 if (!bmap)
444 return NULL;
446 div = isl_basic_map_get_divs(bmap);
447 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
450 __isl_give isl_local_space *isl_basic_set_get_local_space(
451 __isl_keep isl_basic_set *bset)
453 return isl_basic_map_get_local_space(bset);
456 /* For each known div d = floor(f/m), add the constraints
458 * f - m d >= 0
459 * -(f-(m-1)) + m d >= 0
461 * Do not finalize the result.
463 static __isl_give isl_basic_map *add_known_div_constraints(
464 __isl_take isl_basic_map *bmap)
466 int i;
467 unsigned n_div;
469 if (!bmap)
470 return NULL;
471 n_div = isl_basic_map_dim(bmap, isl_dim_div);
472 if (n_div == 0)
473 return bmap;
474 bmap = isl_basic_map_cow(bmap);
475 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
476 if (!bmap)
477 return NULL;
478 for (i = 0; i < n_div; ++i) {
479 if (isl_int_is_zero(bmap->div[i][0]))
480 continue;
481 bmap = isl_basic_map_add_div_constraints(bmap, i);
484 return bmap;
487 __isl_give isl_basic_map *isl_basic_map_from_local_space(
488 __isl_take isl_local_space *ls)
490 int i;
491 int n_div;
492 isl_basic_map *bmap;
494 if (!ls)
495 return NULL;
497 n_div = isl_local_space_dim(ls, isl_dim_div);
498 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
499 n_div, 0, 2 * n_div);
501 for (i = 0; i < n_div; ++i)
502 if (isl_basic_map_alloc_div(bmap) < 0)
503 goto error;
505 for (i = 0; i < n_div; ++i)
506 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
507 bmap = add_known_div_constraints(bmap);
509 isl_local_space_free(ls);
510 return bmap;
511 error:
512 isl_local_space_free(ls);
513 isl_basic_map_free(bmap);
514 return NULL;
517 __isl_give isl_basic_set *isl_basic_set_from_local_space(
518 __isl_take isl_local_space *ls)
520 return isl_basic_map_from_local_space(ls);
523 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
525 return isl_space_copy(isl_map_peek_space(map));
528 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
530 if (!set)
531 return NULL;
532 return isl_space_copy(set->dim);
535 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
536 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
538 bmap = isl_basic_map_cow(bmap);
539 if (!bmap)
540 return NULL;
541 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
542 if (!bmap->dim)
543 goto error;
544 bmap = isl_basic_map_finalize(bmap);
545 return bmap;
546 error:
547 isl_basic_map_free(bmap);
548 return NULL;
551 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
552 __isl_take isl_basic_set *bset, const char *s)
554 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
557 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
558 enum isl_dim_type type)
560 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
563 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
564 enum isl_dim_type type, const char *s)
566 int i;
568 map = isl_map_cow(map);
569 if (!map)
570 return NULL;
572 map->dim = isl_space_set_tuple_name(map->dim, type, s);
573 if (!map->dim)
574 goto error;
576 for (i = 0; i < map->n; ++i) {
577 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
578 if (!map->p[i])
579 goto error;
582 return map;
583 error:
584 isl_map_free(map);
585 return NULL;
588 /* Replace the identifier of the tuple of type "type" by "id".
590 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
591 __isl_take isl_basic_map *bmap,
592 enum isl_dim_type type, __isl_take isl_id *id)
594 bmap = isl_basic_map_cow(bmap);
595 if (!bmap)
596 goto error;
597 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
598 if (!bmap->dim)
599 return isl_basic_map_free(bmap);
600 bmap = isl_basic_map_finalize(bmap);
601 return bmap;
602 error:
603 isl_id_free(id);
604 return NULL;
607 /* Replace the identifier of the tuple by "id".
609 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
610 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
612 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
615 /* Does the input or output tuple have a name?
617 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
619 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
622 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
623 enum isl_dim_type type)
625 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
628 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
629 const char *s)
631 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
632 isl_dim_set, s));
635 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
636 enum isl_dim_type type, __isl_take isl_id *id)
638 map = isl_map_cow(map);
639 if (!map)
640 goto error;
642 map->dim = isl_space_set_tuple_id(map->dim, type, id);
644 return isl_map_reset_space(map, isl_space_copy(map->dim));
645 error:
646 isl_id_free(id);
647 return NULL;
650 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
651 __isl_take isl_id *id)
653 return isl_map_set_tuple_id(set, isl_dim_set, id);
656 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
657 enum isl_dim_type type)
659 map = isl_map_cow(map);
660 if (!map)
661 return NULL;
663 map->dim = isl_space_reset_tuple_id(map->dim, type);
665 return isl_map_reset_space(map, isl_space_copy(map->dim));
668 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
670 return isl_map_reset_tuple_id(set, isl_dim_set);
673 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
675 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
678 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
679 enum isl_dim_type type)
681 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
684 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
686 return isl_map_has_tuple_id(set, isl_dim_set);
689 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
691 return isl_map_get_tuple_id(set, isl_dim_set);
694 /* Does the set tuple have a name?
696 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
698 if (!set)
699 return isl_bool_error;
700 return isl_space_has_tuple_name(set->dim, isl_dim_set);
704 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
706 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
709 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
711 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
714 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
715 enum isl_dim_type type, unsigned pos)
717 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
720 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
721 enum isl_dim_type type, unsigned pos)
723 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
726 /* Does the given dimension have a name?
728 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
729 enum isl_dim_type type, unsigned pos)
731 if (!map)
732 return isl_bool_error;
733 return isl_space_has_dim_name(map->dim, type, pos);
736 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
737 enum isl_dim_type type, unsigned pos)
739 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
742 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
743 enum isl_dim_type type, unsigned pos)
745 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
748 /* Does the given dimension have a name?
750 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
751 enum isl_dim_type type, unsigned pos)
753 if (!set)
754 return isl_bool_error;
755 return isl_space_has_dim_name(set->dim, type, pos);
758 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
759 __isl_take isl_basic_map *bmap,
760 enum isl_dim_type type, unsigned pos, const char *s)
762 bmap = isl_basic_map_cow(bmap);
763 if (!bmap)
764 return NULL;
765 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
766 if (!bmap->dim)
767 goto error;
768 return isl_basic_map_finalize(bmap);
769 error:
770 isl_basic_map_free(bmap);
771 return NULL;
774 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
775 enum isl_dim_type type, unsigned pos, const char *s)
777 int i;
779 map = isl_map_cow(map);
780 if (!map)
781 return NULL;
783 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
784 if (!map->dim)
785 goto error;
787 for (i = 0; i < map->n; ++i) {
788 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
789 if (!map->p[i])
790 goto error;
793 return map;
794 error:
795 isl_map_free(map);
796 return NULL;
799 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
800 __isl_take isl_basic_set *bset,
801 enum isl_dim_type type, unsigned pos, const char *s)
803 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
804 type, pos, s));
807 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
808 enum isl_dim_type type, unsigned pos, const char *s)
810 return set_from_map(isl_map_set_dim_name(set_to_map(set),
811 type, pos, s));
814 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
815 enum isl_dim_type type, unsigned pos)
817 if (!bmap)
818 return isl_bool_error;
819 return isl_space_has_dim_id(bmap->dim, type, pos);
822 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
823 enum isl_dim_type type, unsigned pos)
825 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
828 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
829 enum isl_dim_type type, unsigned pos)
831 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
834 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
835 enum isl_dim_type type, unsigned pos)
837 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
840 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
841 enum isl_dim_type type, unsigned pos)
843 return isl_map_has_dim_id(set, type, pos);
846 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
847 enum isl_dim_type type, unsigned pos)
849 return isl_map_get_dim_id(set, type, pos);
852 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
853 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
855 map = isl_map_cow(map);
856 if (!map)
857 goto error;
859 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
861 return isl_map_reset_space(map, isl_space_copy(map->dim));
862 error:
863 isl_id_free(id);
864 return NULL;
867 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
868 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
870 return isl_map_set_dim_id(set, type, pos, id);
873 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
874 __isl_keep isl_id *id)
876 if (!map)
877 return -1;
878 return isl_space_find_dim_by_id(map->dim, type, id);
881 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
882 __isl_keep isl_id *id)
884 return isl_map_find_dim_by_id(set, type, id);
887 /* Return the position of the dimension of the given type and name
888 * in "bmap".
889 * Return -1 if no such dimension can be found.
891 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
892 enum isl_dim_type type, const char *name)
894 if (!bmap)
895 return -1;
896 return isl_space_find_dim_by_name(bmap->dim, type, name);
899 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
900 const char *name)
902 if (!map)
903 return -1;
904 return isl_space_find_dim_by_name(map->dim, type, name);
907 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
908 const char *name)
910 return isl_map_find_dim_by_name(set, type, name);
913 /* Check whether equality i of bset is a pure stride constraint
914 * on a single dimension, i.e., of the form
916 * v = k e
918 * with k a constant and e an existentially quantified variable.
920 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
922 unsigned nparam;
923 unsigned d;
924 unsigned n_div;
925 int pos1;
926 int pos2;
928 if (!bset)
929 return isl_bool_error;
931 if (!isl_int_is_zero(bset->eq[i][0]))
932 return isl_bool_false;
934 nparam = isl_basic_set_dim(bset, isl_dim_param);
935 d = isl_basic_set_dim(bset, isl_dim_set);
936 n_div = isl_basic_set_dim(bset, isl_dim_div);
938 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
939 return isl_bool_false;
940 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
941 if (pos1 == -1)
942 return isl_bool_false;
943 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
944 d - pos1 - 1) != -1)
945 return isl_bool_false;
947 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
948 if (pos2 == -1)
949 return isl_bool_false;
950 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
951 n_div - pos2 - 1) != -1)
952 return isl_bool_false;
953 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
954 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
955 return isl_bool_false;
957 return isl_bool_true;
960 /* Reset the user pointer on all identifiers of parameters and tuples
961 * of the space of "map".
963 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
965 isl_space *space;
967 space = isl_map_get_space(map);
968 space = isl_space_reset_user(space);
969 map = isl_map_reset_space(map, space);
971 return map;
974 /* Reset the user pointer on all identifiers of parameters and tuples
975 * of the space of "set".
977 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
979 return isl_map_reset_user(set);
982 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
984 if (!bmap)
985 return isl_bool_error;
986 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
989 /* Has "map" been marked as a rational map?
990 * In particular, have all basic maps in "map" been marked this way?
991 * An empty map is not considered to be rational.
992 * Maps where only some of the basic maps are marked rational
993 * are not allowed.
995 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
997 int i;
998 isl_bool rational;
1000 if (!map)
1001 return isl_bool_error;
1002 if (map->n == 0)
1003 return isl_bool_false;
1004 rational = isl_basic_map_is_rational(map->p[0]);
1005 if (rational < 0)
1006 return rational;
1007 for (i = 1; i < map->n; ++i) {
1008 isl_bool rational_i;
1010 rational_i = isl_basic_map_is_rational(map->p[i]);
1011 if (rational_i < 0)
1012 return rational_i;
1013 if (rational != rational_i)
1014 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1015 "mixed rational and integer basic maps "
1016 "not supported", return isl_bool_error);
1019 return rational;
1022 /* Has "set" been marked as a rational set?
1023 * In particular, have all basic set in "set" been marked this way?
1024 * An empty set is not considered to be rational.
1025 * Sets where only some of the basic sets are marked rational
1026 * are not allowed.
1028 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1030 return isl_map_is_rational(set);
1033 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1035 return isl_basic_map_is_rational(bset);
1038 /* Does "bmap" contain any rational points?
1040 * If "bmap" has an equality for each dimension, equating the dimension
1041 * to an integer constant, then it has no rational points, even if it
1042 * is marked as rational.
1044 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1046 isl_bool has_rational = isl_bool_true;
1047 unsigned total;
1049 if (!bmap)
1050 return isl_bool_error;
1051 if (isl_basic_map_plain_is_empty(bmap))
1052 return isl_bool_false;
1053 if (!isl_basic_map_is_rational(bmap))
1054 return isl_bool_false;
1055 bmap = isl_basic_map_copy(bmap);
1056 bmap = isl_basic_map_implicit_equalities(bmap);
1057 if (!bmap)
1058 return isl_bool_error;
1059 total = isl_basic_map_total_dim(bmap);
1060 if (bmap->n_eq == total) {
1061 int i, j;
1062 for (i = 0; i < bmap->n_eq; ++i) {
1063 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1064 if (j < 0)
1065 break;
1066 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1067 !isl_int_is_negone(bmap->eq[i][1 + j]))
1068 break;
1069 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1070 total - j - 1);
1071 if (j >= 0)
1072 break;
1074 if (i == bmap->n_eq)
1075 has_rational = isl_bool_false;
1077 isl_basic_map_free(bmap);
1079 return has_rational;
1082 /* Does "map" contain any rational points?
1084 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1086 int i;
1087 isl_bool has_rational;
1089 if (!map)
1090 return isl_bool_error;
1091 for (i = 0; i < map->n; ++i) {
1092 has_rational = isl_basic_map_has_rational(map->p[i]);
1093 if (has_rational < 0 || has_rational)
1094 return has_rational;
1096 return isl_bool_false;
1099 /* Does "set" contain any rational points?
1101 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1103 return isl_map_has_rational(set);
1106 /* Is this basic set a parameter domain?
1108 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1110 if (!bset)
1111 return isl_bool_error;
1112 return isl_space_is_params(bset->dim);
1115 /* Is this set a parameter domain?
1117 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1119 if (!set)
1120 return isl_bool_error;
1121 return isl_space_is_params(set->dim);
1124 /* Is this map actually a parameter domain?
1125 * Users should never call this function. Outside of isl,
1126 * a map can never be a parameter domain.
1128 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1130 if (!map)
1131 return isl_bool_error;
1132 return isl_space_is_params(map->dim);
1135 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1136 __isl_take isl_basic_map *bmap, unsigned extra,
1137 unsigned n_eq, unsigned n_ineq)
1139 int i;
1140 isl_space *space = isl_basic_map_peek_space(bmap);
1141 unsigned n_var = isl_space_dim(space, isl_dim_all);
1142 size_t row_size = 1 + n_var + extra;
1144 bmap->ctx = ctx;
1145 isl_ctx_ref(ctx);
1147 if (!space)
1148 return isl_basic_map_free(bmap);
1150 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1151 if (isl_blk_is_error(bmap->block))
1152 goto error;
1154 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1155 if ((n_ineq + n_eq) && !bmap->ineq)
1156 goto error;
1158 if (extra == 0) {
1159 bmap->block2 = isl_blk_empty();
1160 bmap->div = NULL;
1161 } else {
1162 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1163 if (isl_blk_is_error(bmap->block2))
1164 goto error;
1166 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1167 if (!bmap->div)
1168 goto error;
1171 for (i = 0; i < n_ineq + n_eq; ++i)
1172 bmap->ineq[i] = bmap->block.data + i * row_size;
1174 for (i = 0; i < extra; ++i)
1175 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1177 bmap->ref = 1;
1178 bmap->flags = 0;
1179 bmap->c_size = n_eq + n_ineq;
1180 bmap->eq = bmap->ineq + n_ineq;
1181 bmap->extra = extra;
1182 bmap->n_eq = 0;
1183 bmap->n_ineq = 0;
1184 bmap->n_div = 0;
1185 bmap->sample = NULL;
1187 return bmap;
1188 error:
1189 isl_basic_map_free(bmap);
1190 return NULL;
1193 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1194 unsigned nparam, unsigned dim, unsigned extra,
1195 unsigned n_eq, unsigned n_ineq)
1197 struct isl_basic_map *bmap;
1198 isl_space *space;
1200 space = isl_space_set_alloc(ctx, nparam, dim);
1201 if (!space)
1202 return NULL;
1204 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1205 return bset_from_bmap(bmap);
1208 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1209 unsigned extra, unsigned n_eq, unsigned n_ineq)
1211 struct isl_basic_map *bmap;
1212 if (!dim)
1213 return NULL;
1214 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1215 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1216 return bset_from_bmap(bmap);
1217 error:
1218 isl_space_free(dim);
1219 return NULL;
1222 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1223 unsigned extra, unsigned n_eq, unsigned n_ineq)
1225 struct isl_basic_map *bmap;
1227 if (!space)
1228 return NULL;
1229 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1230 if (!bmap)
1231 goto error;
1232 bmap->dim = space;
1234 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1235 error:
1236 isl_space_free(space);
1237 return NULL;
1240 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1241 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1242 unsigned n_eq, unsigned n_ineq)
1244 struct isl_basic_map *bmap;
1245 isl_space *dim;
1247 dim = isl_space_alloc(ctx, nparam, in, out);
1248 if (!dim)
1249 return NULL;
1251 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1252 return bmap;
1255 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1256 __isl_keep isl_basic_map *src)
1258 int i;
1259 unsigned total = isl_basic_map_total_dim(src);
1261 if (!dst)
1262 return NULL;
1264 for (i = 0; i < src->n_eq; ++i) {
1265 int j = isl_basic_map_alloc_equality(dst);
1266 if (j < 0)
1267 return isl_basic_map_free(dst);
1268 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1271 for (i = 0; i < src->n_ineq; ++i) {
1272 int j = isl_basic_map_alloc_inequality(dst);
1273 if (j < 0)
1274 return isl_basic_map_free(dst);
1275 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1278 for (i = 0; i < src->n_div; ++i) {
1279 int j = isl_basic_map_alloc_div(dst);
1280 if (j < 0)
1281 return isl_basic_map_free(dst);
1282 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1284 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1285 return dst;
1288 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1290 struct isl_basic_map *dup;
1292 if (!bmap)
1293 return NULL;
1294 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1295 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1296 dup = dup_constraints(dup, bmap);
1297 if (!dup)
1298 return NULL;
1299 dup->flags = bmap->flags;
1300 dup->sample = isl_vec_copy(bmap->sample);
1301 return dup;
1304 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1306 struct isl_basic_map *dup;
1308 dup = isl_basic_map_dup(bset_to_bmap(bset));
1309 return bset_from_bmap(dup);
1312 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1314 if (!bset)
1315 return NULL;
1317 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1318 bset->ref++;
1319 return bset;
1321 return isl_basic_set_dup(bset);
1324 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1326 if (!set)
1327 return NULL;
1329 set->ref++;
1330 return set;
1333 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1335 if (!bmap)
1336 return NULL;
1338 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1339 bmap->ref++;
1340 return bmap;
1342 bmap = isl_basic_map_dup(bmap);
1343 if (bmap)
1344 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1345 return bmap;
1348 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1350 if (!map)
1351 return NULL;
1353 map->ref++;
1354 return map;
1357 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1359 if (!bmap)
1360 return NULL;
1362 if (--bmap->ref > 0)
1363 return NULL;
1365 isl_ctx_deref(bmap->ctx);
1366 free(bmap->div);
1367 isl_blk_free(bmap->ctx, bmap->block2);
1368 free(bmap->ineq);
1369 isl_blk_free(bmap->ctx, bmap->block);
1370 isl_vec_free(bmap->sample);
1371 isl_space_free(bmap->dim);
1372 free(bmap);
1374 return NULL;
1377 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1379 return isl_basic_map_free(bset_to_bmap(bset));
1382 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1384 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1387 /* Check that "bset" does not involve any parameters.
1389 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1391 if (!bset)
1392 return isl_stat_error;
1393 if (isl_basic_set_dim(bset, isl_dim_param) != 0)
1394 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1395 "basic set should not have any parameters",
1396 return isl_stat_error);
1397 return isl_stat_ok;
1400 /* Check that "bset" does not involve any local variables.
1402 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1404 if (!bset)
1405 return isl_stat_error;
1406 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
1407 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1408 "basic set should not have any local variables",
1409 return isl_stat_error);
1410 return isl_stat_ok;
1413 /* Check that "map" has only named parameters, reporting an error
1414 * if it does not.
1416 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1418 return isl_space_check_named_params(isl_map_peek_space(map));
1421 /* Check that "bmap" has only named parameters, reporting an error
1422 * if it does not.
1424 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1426 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1429 /* Check that "bmap1" and "bmap2" have the same parameters,
1430 * reporting an error if they do not.
1432 static isl_stat isl_basic_map_check_equal_params(
1433 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1435 isl_bool match;
1437 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1438 if (match < 0)
1439 return isl_stat_error;
1440 if (!match)
1441 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1442 "parameters don't match", return isl_stat_error);
1443 return isl_stat_ok;
1446 __isl_give isl_map *isl_map_align_params_map_map_and(
1447 __isl_take isl_map *map1, __isl_take isl_map *map2,
1448 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1449 __isl_take isl_map *map2))
1451 if (!map1 || !map2)
1452 goto error;
1453 if (isl_map_has_equal_params(map1, map2))
1454 return fn(map1, map2);
1455 if (isl_map_check_named_params(map1) < 0)
1456 goto error;
1457 if (isl_map_check_named_params(map2) < 0)
1458 goto error;
1459 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1460 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1461 return fn(map1, map2);
1462 error:
1463 isl_map_free(map1);
1464 isl_map_free(map2);
1465 return NULL;
1468 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1469 __isl_keep isl_map *map2,
1470 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1472 isl_bool r;
1474 if (!map1 || !map2)
1475 return isl_bool_error;
1476 if (isl_map_has_equal_params(map1, map2))
1477 return fn(map1, map2);
1478 if (isl_map_check_named_params(map1) < 0)
1479 return isl_bool_error;
1480 if (isl_map_check_named_params(map2) < 0)
1481 return isl_bool_error;
1482 map1 = isl_map_copy(map1);
1483 map2 = isl_map_copy(map2);
1484 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1485 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1486 r = fn(map1, map2);
1487 isl_map_free(map1);
1488 isl_map_free(map2);
1489 return r;
1492 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1494 struct isl_ctx *ctx;
1495 if (!bmap)
1496 return -1;
1497 ctx = bmap->ctx;
1498 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1499 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1500 return -1);
1501 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1502 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1503 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1504 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1505 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1506 isl_int *t;
1507 int j = isl_basic_map_alloc_inequality(bmap);
1508 if (j < 0)
1509 return -1;
1510 t = bmap->ineq[j];
1511 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1512 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1513 bmap->eq[-1] = t;
1514 bmap->n_eq++;
1515 bmap->n_ineq--;
1516 bmap->eq--;
1517 return 0;
1519 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1520 bmap->extra - bmap->n_div);
1521 return bmap->n_eq++;
1524 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1526 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1529 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1531 if (!bmap)
1532 return -1;
1533 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1534 bmap->n_eq -= n;
1535 return 0;
1538 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1540 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1543 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1545 isl_int *t;
1546 if (!bmap)
1547 return -1;
1548 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1550 if (pos != bmap->n_eq - 1) {
1551 t = bmap->eq[pos];
1552 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1553 bmap->eq[bmap->n_eq - 1] = t;
1555 bmap->n_eq--;
1556 return 0;
1559 /* Turn inequality "pos" of "bmap" into an equality.
1561 * In particular, we move the inequality in front of the equalities
1562 * and move the last inequality in the position of the moved inequality.
1563 * Note that isl_tab_make_equalities_explicit depends on this particular
1564 * change in the ordering of the constraints.
1566 void isl_basic_map_inequality_to_equality(
1567 struct isl_basic_map *bmap, unsigned pos)
1569 isl_int *t;
1571 t = bmap->ineq[pos];
1572 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1573 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1574 bmap->eq[-1] = t;
1575 bmap->n_eq++;
1576 bmap->n_ineq--;
1577 bmap->eq--;
1578 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1579 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1580 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1581 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1584 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1586 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1589 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1591 struct isl_ctx *ctx;
1592 if (!bmap)
1593 return -1;
1594 ctx = bmap->ctx;
1595 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1596 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1597 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1598 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1599 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1600 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1601 1 + isl_basic_map_total_dim(bmap),
1602 bmap->extra - bmap->n_div);
1603 return bmap->n_ineq++;
1606 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1608 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1611 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1613 if (!bmap)
1614 return -1;
1615 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1616 bmap->n_ineq -= n;
1617 return 0;
1620 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1622 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1625 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1627 isl_int *t;
1628 if (!bmap)
1629 return -1;
1630 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1632 if (pos != bmap->n_ineq - 1) {
1633 t = bmap->ineq[pos];
1634 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1635 bmap->ineq[bmap->n_ineq - 1] = t;
1636 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1638 bmap->n_ineq--;
1639 return 0;
1642 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1644 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1647 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1648 isl_int *eq)
1650 isl_bool empty;
1651 int k;
1653 empty = isl_basic_map_plain_is_empty(bmap);
1654 if (empty < 0)
1655 return isl_basic_map_free(bmap);
1656 if (empty)
1657 return bmap;
1659 bmap = isl_basic_map_cow(bmap);
1660 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1661 if (!bmap)
1662 return NULL;
1663 k = isl_basic_map_alloc_equality(bmap);
1664 if (k < 0)
1665 goto error;
1666 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1667 return bmap;
1668 error:
1669 isl_basic_map_free(bmap);
1670 return NULL;
1673 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1674 isl_int *eq)
1676 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1679 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1680 isl_int *ineq)
1682 int k;
1684 bmap = isl_basic_map_cow(bmap);
1685 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1686 if (!bmap)
1687 return NULL;
1688 k = isl_basic_map_alloc_inequality(bmap);
1689 if (k < 0)
1690 goto error;
1691 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1692 return bmap;
1693 error:
1694 isl_basic_map_free(bmap);
1695 return NULL;
1698 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1699 isl_int *ineq)
1701 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1704 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1706 if (!bmap)
1707 return -1;
1708 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1709 isl_seq_clr(bmap->div[bmap->n_div] +
1710 1 + 1 + isl_basic_map_total_dim(bmap),
1711 bmap->extra - bmap->n_div);
1712 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1713 return bmap->n_div++;
1716 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1718 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1721 #undef TYPE
1722 #define TYPE isl_basic_map
1723 #include "check_type_range_templ.c"
1725 /* Check that there are "n" dimensions of type "type" starting at "first"
1726 * in "bset".
1728 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1729 enum isl_dim_type type, unsigned first, unsigned n)
1731 return isl_basic_map_check_range(bset_to_bmap(bset),
1732 type, first, n);
1735 /* Insert an extra integer division, prescribed by "div", to "bmap"
1736 * at (integer division) position "pos".
1738 * The integer division is first added at the end and then moved
1739 * into the right position.
1741 __isl_give isl_basic_map *isl_basic_map_insert_div(
1742 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1744 int i, k;
1746 bmap = isl_basic_map_cow(bmap);
1747 if (!bmap || !div)
1748 return isl_basic_map_free(bmap);
1750 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1751 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1752 "unexpected size", return isl_basic_map_free(bmap));
1753 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1754 return isl_basic_map_free(bmap);
1756 bmap = isl_basic_map_extend_space(bmap,
1757 isl_basic_map_get_space(bmap), 1, 0, 2);
1758 k = isl_basic_map_alloc_div(bmap);
1759 if (k < 0)
1760 return isl_basic_map_free(bmap);
1761 isl_seq_cpy(bmap->div[k], div->el, div->size);
1762 isl_int_set_si(bmap->div[k][div->size], 0);
1764 for (i = k; i > pos; --i)
1765 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1767 return bmap;
1770 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1772 if (!bmap)
1773 return isl_stat_error;
1774 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1775 bmap->n_div -= n;
1776 return isl_stat_ok;
1779 static __isl_give isl_basic_map *add_constraints(
1780 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1781 unsigned i_pos, unsigned o_pos)
1783 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1784 isl_ctx *ctx;
1785 isl_space *space;
1786 struct isl_dim_map *dim_map;
1788 space = isl_basic_map_peek_space(bmap2);
1789 if (!bmap1 || !space)
1790 goto error;
1792 total = isl_basic_map_dim(bmap1, isl_dim_all);
1793 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1794 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1795 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1796 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1797 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1798 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1799 ctx = isl_basic_map_get_ctx(bmap1);
1800 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1801 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1802 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1803 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1804 isl_dim_map_div(dim_map, bmap2, total);
1806 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1807 error:
1808 isl_basic_map_free(bmap1);
1809 isl_basic_map_free(bmap2);
1810 return NULL;
1813 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1814 struct isl_basic_set *bset2, unsigned pos)
1816 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1817 bset_to_bmap(bset2), 0, pos));
1820 __isl_give isl_basic_map *isl_basic_map_extend_space(
1821 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1822 unsigned extra, unsigned n_eq, unsigned n_ineq)
1824 struct isl_basic_map *ext;
1825 unsigned flags;
1826 int dims_ok;
1828 if (!space)
1829 goto error;
1831 if (!base)
1832 goto error;
1834 dims_ok = isl_space_is_equal(base->dim, space) &&
1835 base->extra >= base->n_div + extra;
1837 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1838 room_for_ineq(base, n_ineq)) {
1839 isl_space_free(space);
1840 return base;
1843 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1844 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1845 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1846 extra += base->extra;
1847 n_eq += base->n_eq;
1848 n_ineq += base->n_ineq;
1850 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1851 space = NULL;
1852 if (!ext)
1853 goto error;
1855 if (dims_ok)
1856 ext->sample = isl_vec_copy(base->sample);
1857 flags = base->flags;
1858 ext = add_constraints(ext, base, 0, 0);
1859 if (ext) {
1860 ext->flags = flags;
1861 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1864 return ext;
1866 error:
1867 isl_space_free(space);
1868 isl_basic_map_free(base);
1869 return NULL;
1872 __isl_give isl_basic_set *isl_basic_set_extend_space(
1873 __isl_take isl_basic_set *base,
1874 __isl_take isl_space *dim, unsigned extra,
1875 unsigned n_eq, unsigned n_ineq)
1877 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1878 dim, extra, n_eq, n_ineq));
1881 struct isl_basic_map *isl_basic_map_extend_constraints(
1882 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1884 if (!base)
1885 return NULL;
1886 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1887 0, n_eq, n_ineq);
1890 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1891 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1892 unsigned n_eq, unsigned n_ineq)
1894 struct isl_basic_map *bmap;
1895 isl_space *dim;
1897 if (!base)
1898 return NULL;
1899 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1900 if (!dim)
1901 goto error;
1903 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1904 return bmap;
1905 error:
1906 isl_basic_map_free(base);
1907 return NULL;
1910 struct isl_basic_set *isl_basic_set_extend_constraints(
1911 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1913 isl_basic_map *bmap = bset_to_bmap(base);
1914 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1915 return bset_from_bmap(bmap);
1918 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1920 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1923 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1925 if (!bmap)
1926 return NULL;
1928 if (bmap->ref > 1) {
1929 bmap->ref--;
1930 bmap = isl_basic_map_dup(bmap);
1932 if (bmap) {
1933 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1934 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1936 return bmap;
1939 /* Clear all cached information in "map", either because it is about
1940 * to be modified or because it is being freed.
1941 * Always return the same pointer that is passed in.
1942 * This is needed for the use in isl_map_free.
1944 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1946 isl_basic_map_free(map->cached_simple_hull[0]);
1947 isl_basic_map_free(map->cached_simple_hull[1]);
1948 map->cached_simple_hull[0] = NULL;
1949 map->cached_simple_hull[1] = NULL;
1950 return map;
1953 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1955 return isl_map_cow(set);
1958 /* Return an isl_map that is equal to "map" and that has only
1959 * a single reference.
1961 * If the original input already has only one reference, then
1962 * simply return it, but clear all cached information, since
1963 * it may be rendered invalid by the operations that will be
1964 * performed on the result.
1966 * Otherwise, create a duplicate (without any cached information).
1968 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1970 if (!map)
1971 return NULL;
1973 if (map->ref == 1)
1974 return clear_caches(map);
1975 map->ref--;
1976 return isl_map_dup(map);
1979 static void swap_vars(struct isl_blk blk, isl_int *a,
1980 unsigned a_len, unsigned b_len)
1982 isl_seq_cpy(blk.data, a+a_len, b_len);
1983 isl_seq_cpy(blk.data+b_len, a, a_len);
1984 isl_seq_cpy(a, blk.data, b_len+a_len);
1987 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1988 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1990 int i;
1991 struct isl_blk blk;
1993 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
1994 goto error;
1996 if (n1 == 0 || n2 == 0)
1997 return bmap;
1999 bmap = isl_basic_map_cow(bmap);
2000 if (!bmap)
2001 return NULL;
2003 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2004 if (isl_blk_is_error(blk))
2005 goto error;
2007 for (i = 0; i < bmap->n_eq; ++i)
2008 swap_vars(blk,
2009 bmap->eq[i] + pos, n1, n2);
2011 for (i = 0; i < bmap->n_ineq; ++i)
2012 swap_vars(blk,
2013 bmap->ineq[i] + pos, n1, n2);
2015 for (i = 0; i < bmap->n_div; ++i)
2016 swap_vars(blk,
2017 bmap->div[i]+1 + pos, n1, n2);
2019 isl_blk_free(bmap->ctx, blk);
2021 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2022 bmap = isl_basic_map_gauss(bmap, NULL);
2023 return isl_basic_map_finalize(bmap);
2024 error:
2025 isl_basic_map_free(bmap);
2026 return NULL;
2029 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2030 __isl_take isl_basic_map *bmap)
2032 int i = 0;
2033 unsigned total;
2034 if (!bmap)
2035 goto error;
2036 total = isl_basic_map_total_dim(bmap);
2037 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2038 return isl_basic_map_free(bmap);
2039 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2040 if (bmap->n_eq > 0)
2041 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2042 else {
2043 i = isl_basic_map_alloc_equality(bmap);
2044 if (i < 0)
2045 goto error;
2047 isl_int_set_si(bmap->eq[i][0], 1);
2048 isl_seq_clr(bmap->eq[i]+1, total);
2049 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2050 isl_vec_free(bmap->sample);
2051 bmap->sample = NULL;
2052 return isl_basic_map_finalize(bmap);
2053 error:
2054 isl_basic_map_free(bmap);
2055 return NULL;
2058 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2059 __isl_take isl_basic_set *bset)
2061 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2064 __isl_give isl_basic_map *isl_basic_map_set_rational(
2065 __isl_take isl_basic_map *bmap)
2067 if (!bmap)
2068 return NULL;
2070 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2071 return bmap;
2073 bmap = isl_basic_map_cow(bmap);
2074 if (!bmap)
2075 return NULL;
2077 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2079 return isl_basic_map_finalize(bmap);
2082 __isl_give isl_basic_set *isl_basic_set_set_rational(
2083 __isl_take isl_basic_set *bset)
2085 return isl_basic_map_set_rational(bset);
2088 __isl_give isl_basic_set *isl_basic_set_set_integral(
2089 __isl_take isl_basic_set *bset)
2091 if (!bset)
2092 return NULL;
2094 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2095 return bset;
2097 bset = isl_basic_set_cow(bset);
2098 if (!bset)
2099 return NULL;
2101 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2103 return isl_basic_set_finalize(bset);
2106 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2108 int i;
2110 map = isl_map_cow(map);
2111 if (!map)
2112 return NULL;
2113 for (i = 0; i < map->n; ++i) {
2114 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2115 if (!map->p[i])
2116 goto error;
2118 return map;
2119 error:
2120 isl_map_free(map);
2121 return NULL;
2124 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2126 return isl_map_set_rational(set);
2129 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2130 * of "bmap").
2132 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2134 isl_int *t = bmap->div[a];
2135 bmap->div[a] = bmap->div[b];
2136 bmap->div[b] = t;
2139 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2140 * div definitions accordingly.
2142 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2143 int a, int b)
2145 int i;
2146 int off;
2148 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2149 if (off < 0)
2150 return isl_basic_map_free(bmap);
2152 swap_div(bmap, a, b);
2154 for (i = 0; i < bmap->n_eq; ++i)
2155 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2157 for (i = 0; i < bmap->n_ineq; ++i)
2158 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2160 for (i = 0; i < bmap->n_div; ++i)
2161 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2162 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2164 return bmap;
2167 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2169 isl_seq_cpy(c, c + n, rem);
2170 isl_seq_clr(c + rem, n);
2173 /* Drop n dimensions starting at first.
2175 * In principle, this frees up some extra variables as the number
2176 * of columns remains constant, but we would have to extend
2177 * the div array too as the number of rows in this array is assumed
2178 * to be equal to extra.
2180 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2181 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2183 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2186 /* Move "n" divs starting at "first" to the end of the list of divs.
2188 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2189 unsigned first, unsigned n)
2191 isl_int **div;
2192 int i;
2194 if (first + n == bmap->n_div)
2195 return bmap;
2197 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2198 if (!div)
2199 goto error;
2200 for (i = 0; i < n; ++i)
2201 div[i] = bmap->div[first + i];
2202 for (i = 0; i < bmap->n_div - first - n; ++i)
2203 bmap->div[first + i] = bmap->div[first + n + i];
2204 for (i = 0; i < n; ++i)
2205 bmap->div[bmap->n_div - n + i] = div[i];
2206 free(div);
2207 return bmap;
2208 error:
2209 isl_basic_map_free(bmap);
2210 return NULL;
2213 #undef TYPE
2214 #define TYPE isl_map
2215 static
2216 #include "check_type_range_templ.c"
2218 /* Check that there are "n" dimensions of type "type" starting at "first"
2219 * in "set".
2221 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2222 enum isl_dim_type type, unsigned first, unsigned n)
2224 return isl_map_check_range(set_to_map(set), type, first, n);
2227 /* Drop "n" dimensions of type "type" starting at "first".
2228 * Perform the core computation, without cowing or
2229 * simplifying and finalizing the result.
2231 * In principle, this frees up some extra variables as the number
2232 * of columns remains constant, but we would have to extend
2233 * the div array too as the number of rows in this array is assumed
2234 * to be equal to extra.
2236 __isl_give isl_basic_map *isl_basic_map_drop_core(
2237 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2238 unsigned first, unsigned n)
2240 int i;
2241 unsigned offset;
2242 unsigned left;
2244 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2245 return isl_basic_map_free(bmap);
2247 offset = isl_basic_map_offset(bmap, type) + first;
2248 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2249 for (i = 0; i < bmap->n_eq; ++i)
2250 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2252 for (i = 0; i < bmap->n_ineq; ++i)
2253 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2255 for (i = 0; i < bmap->n_div; ++i)
2256 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2258 if (type == isl_dim_div) {
2259 bmap = move_divs_last(bmap, first, n);
2260 if (!bmap)
2261 return NULL;
2262 if (isl_basic_map_free_div(bmap, n) < 0)
2263 return isl_basic_map_free(bmap);
2264 } else
2265 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2266 if (!bmap->dim)
2267 return isl_basic_map_free(bmap);
2269 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2270 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2271 return bmap;
2274 /* Drop "n" dimensions of type "type" starting at "first".
2276 * In principle, this frees up some extra variables as the number
2277 * of columns remains constant, but we would have to extend
2278 * the div array too as the number of rows in this array is assumed
2279 * to be equal to extra.
2281 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2282 enum isl_dim_type type, unsigned first, unsigned n)
2284 if (!bmap)
2285 return NULL;
2286 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2287 return bmap;
2289 bmap = isl_basic_map_cow(bmap);
2290 if (!bmap)
2291 return NULL;
2293 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2295 bmap = isl_basic_map_simplify(bmap);
2296 return isl_basic_map_finalize(bmap);
2299 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2300 enum isl_dim_type type, unsigned first, unsigned n)
2302 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2303 type, first, n));
2306 /* No longer consider "map" to be normalized.
2308 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2310 if (!map)
2311 return NULL;
2312 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2313 return map;
2316 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2317 enum isl_dim_type type, unsigned first, unsigned n)
2319 int i;
2321 if (isl_map_check_range(map, type, first, n) < 0)
2322 return isl_map_free(map);
2324 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2325 return map;
2326 map = isl_map_cow(map);
2327 if (!map)
2328 goto error;
2329 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2330 if (!map->dim)
2331 goto error;
2333 for (i = 0; i < map->n; ++i) {
2334 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2335 if (!map->p[i])
2336 goto error;
2338 map = isl_map_unmark_normalized(map);
2340 return map;
2341 error:
2342 isl_map_free(map);
2343 return NULL;
2346 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2347 enum isl_dim_type type, unsigned first, unsigned n)
2349 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2352 /* Drop the integer division at position "div", which is assumed
2353 * not to appear in any of the constraints or
2354 * in any of the other integer divisions.
2356 * Since the integer division is redundant, there is no need to cow.
2358 __isl_give isl_basic_map *isl_basic_map_drop_div(
2359 __isl_take isl_basic_map *bmap, unsigned div)
2361 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
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_map *isl_map_eliminate(__isl_take isl_map *map,
2369 enum isl_dim_type type, unsigned first, unsigned n)
2371 int i;
2373 if (n == 0)
2374 return map;
2376 if (isl_map_check_range(map, type, first, n) < 0)
2377 return isl_map_free(map);
2379 map = isl_map_cow(map);
2380 if (!map)
2381 return NULL;
2383 for (i = 0; i < map->n; ++i) {
2384 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2385 if (!map->p[i])
2386 goto error;
2388 return map;
2389 error:
2390 isl_map_free(map);
2391 return NULL;
2394 /* Eliminate the specified n dimensions starting at first from the
2395 * constraints, without removing the dimensions from the space.
2396 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2398 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2399 enum isl_dim_type type, unsigned first, unsigned n)
2401 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2404 /* Eliminate the specified n dimensions starting at first from the
2405 * constraints, without removing the dimensions from the space.
2406 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2408 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2409 unsigned first, unsigned n)
2411 return isl_set_eliminate(set, isl_dim_set, first, n);
2414 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2415 __isl_take isl_basic_map *bmap)
2417 int v_div;
2419 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2420 if (v_div < 0)
2421 return isl_basic_map_free(bmap);
2422 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2423 if (!bmap)
2424 return NULL;
2425 bmap->n_div = 0;
2426 return isl_basic_map_finalize(bmap);
2429 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2430 __isl_take isl_basic_set *bset)
2432 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2435 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2437 int i;
2439 if (!map)
2440 return NULL;
2441 if (map->n == 0)
2442 return map;
2444 map = isl_map_cow(map);
2445 if (!map)
2446 return NULL;
2448 for (i = 0; i < map->n; ++i) {
2449 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2450 if (!map->p[i])
2451 goto error;
2453 return map;
2454 error:
2455 isl_map_free(map);
2456 return NULL;
2459 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2461 return isl_map_remove_divs(set);
2464 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2465 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2466 unsigned first, unsigned n)
2468 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2469 return isl_basic_map_free(bmap);
2470 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2471 return bmap;
2472 bmap = isl_basic_map_eliminate_vars(bmap,
2473 isl_basic_map_offset(bmap, type) - 1 + first, n);
2474 if (!bmap)
2475 return bmap;
2476 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2477 return bmap;
2478 bmap = isl_basic_map_drop(bmap, type, first, n);
2479 return bmap;
2482 /* Return true if the definition of the given div (recursively) involves
2483 * any of the given variables.
2485 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2486 unsigned first, unsigned n)
2488 int i;
2489 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2491 if (isl_int_is_zero(bmap->div[div][0]))
2492 return isl_bool_false;
2493 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2494 return isl_bool_true;
2496 for (i = bmap->n_div - 1; i >= 0; --i) {
2497 isl_bool involves;
2499 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2500 continue;
2501 involves = div_involves_vars(bmap, i, first, n);
2502 if (involves < 0 || involves)
2503 return involves;
2506 return isl_bool_false;
2509 /* Try and add a lower and/or upper bound on "div" to "bmap"
2510 * based on inequality "i".
2511 * "total" is the total number of variables (excluding the divs).
2512 * "v" is a temporary object that can be used during the calculations.
2513 * If "lb" is set, then a lower bound should be constructed.
2514 * If "ub" is set, then an upper bound should be constructed.
2516 * The calling function has already checked that the inequality does not
2517 * reference "div", but we still need to check that the inequality is
2518 * of the right form. We'll consider the case where we want to construct
2519 * a lower bound. The construction of upper bounds is similar.
2521 * Let "div" be of the form
2523 * q = floor((a + f(x))/d)
2525 * We essentially check if constraint "i" is of the form
2527 * b + f(x) >= 0
2529 * so that we can use it to derive a lower bound on "div".
2530 * However, we allow a slightly more general form
2532 * b + g(x) >= 0
2534 * with the condition that the coefficients of g(x) - f(x) are all
2535 * divisible by d.
2536 * Rewriting this constraint as
2538 * 0 >= -b - g(x)
2540 * adding a + f(x) to both sides and dividing by d, we obtain
2542 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2544 * Taking the floor on both sides, we obtain
2546 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2548 * or
2550 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2552 * In the case of an upper bound, we construct the constraint
2554 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2557 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2558 __isl_take isl_basic_map *bmap, int div, int i,
2559 unsigned total, isl_int v, int lb, int ub)
2561 int j;
2563 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2564 if (lb) {
2565 isl_int_sub(v, bmap->ineq[i][1 + j],
2566 bmap->div[div][1 + 1 + j]);
2567 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2569 if (ub) {
2570 isl_int_add(v, bmap->ineq[i][1 + j],
2571 bmap->div[div][1 + 1 + j]);
2572 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2575 if (!lb && !ub)
2576 return bmap;
2578 bmap = isl_basic_map_cow(bmap);
2579 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2580 if (lb) {
2581 int k = isl_basic_map_alloc_inequality(bmap);
2582 if (k < 0)
2583 goto error;
2584 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2585 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2586 bmap->div[div][1 + j]);
2587 isl_int_cdiv_q(bmap->ineq[k][j],
2588 bmap->ineq[k][j], bmap->div[div][0]);
2590 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2592 if (ub) {
2593 int k = isl_basic_map_alloc_inequality(bmap);
2594 if (k < 0)
2595 goto error;
2596 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2597 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2598 bmap->div[div][1 + j]);
2599 isl_int_fdiv_q(bmap->ineq[k][j],
2600 bmap->ineq[k][j], bmap->div[div][0]);
2602 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2605 return bmap;
2606 error:
2607 isl_basic_map_free(bmap);
2608 return NULL;
2611 /* This function is called right before "div" is eliminated from "bmap"
2612 * using Fourier-Motzkin.
2613 * Look through the constraints of "bmap" for constraints on the argument
2614 * of the integer division and use them to construct constraints on the
2615 * integer division itself. These constraints can then be combined
2616 * during the Fourier-Motzkin elimination.
2617 * Note that it is only useful to introduce lower bounds on "div"
2618 * if "bmap" already contains upper bounds on "div" as the newly
2619 * introduce lower bounds can then be combined with the pre-existing
2620 * upper bounds. Similarly for upper bounds.
2621 * We therefore first check if "bmap" contains any lower and/or upper bounds
2622 * on "div".
2624 * It is interesting to note that the introduction of these constraints
2625 * can indeed lead to more accurate results, even when compared to
2626 * deriving constraints on the argument of "div" from constraints on "div".
2627 * Consider, for example, the set
2629 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2631 * The second constraint can be rewritten as
2633 * 2 * [(-i-2j+3)/4] + k >= 0
2635 * from which we can derive
2637 * -i - 2j + 3 >= -2k
2639 * or
2641 * i + 2j <= 3 + 2k
2643 * Combined with the first constraint, we obtain
2645 * -3 <= 3 + 2k or k >= -3
2647 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2648 * the first constraint, we obtain
2650 * [(i + 2j)/4] >= [-3/4] = -1
2652 * Combining this constraint with the second constraint, we obtain
2654 * k >= -2
2656 static __isl_give isl_basic_map *insert_bounds_on_div(
2657 __isl_take isl_basic_map *bmap, int div)
2659 int i;
2660 int check_lb, check_ub;
2661 isl_int v;
2662 int v_div;
2664 if (!bmap)
2665 return NULL;
2667 if (isl_int_is_zero(bmap->div[div][0]))
2668 return bmap;
2670 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2671 if (v_div < 0)
2672 return isl_basic_map_free(bmap);
2674 check_lb = 0;
2675 check_ub = 0;
2676 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2677 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2678 if (s > 0)
2679 check_ub = 1;
2680 if (s < 0)
2681 check_lb = 1;
2684 if (!check_lb && !check_ub)
2685 return bmap;
2687 isl_int_init(v);
2689 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2690 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2691 continue;
2693 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2694 check_lb, check_ub);
2697 isl_int_clear(v);
2699 return bmap;
2702 /* Remove all divs (recursively) involving any of the given dimensions
2703 * in their definitions.
2705 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2706 __isl_take isl_basic_map *bmap,
2707 enum isl_dim_type type, unsigned first, unsigned n)
2709 int i;
2711 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2712 return isl_basic_map_free(bmap);
2713 first += isl_basic_map_offset(bmap, type);
2715 for (i = bmap->n_div - 1; i >= 0; --i) {
2716 isl_bool involves;
2718 involves = div_involves_vars(bmap, i, first, n);
2719 if (involves < 0)
2720 return isl_basic_map_free(bmap);
2721 if (!involves)
2722 continue;
2723 bmap = insert_bounds_on_div(bmap, i);
2724 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2725 if (!bmap)
2726 return NULL;
2727 i = bmap->n_div;
2730 return bmap;
2733 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2734 __isl_take isl_basic_set *bset,
2735 enum isl_dim_type type, unsigned first, unsigned n)
2737 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2740 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2741 enum isl_dim_type type, unsigned first, unsigned n)
2743 int i;
2745 if (!map)
2746 return NULL;
2747 if (map->n == 0)
2748 return map;
2750 map = isl_map_cow(map);
2751 if (!map)
2752 return NULL;
2754 for (i = 0; i < map->n; ++i) {
2755 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2756 type, first, n);
2757 if (!map->p[i])
2758 goto error;
2760 return map;
2761 error:
2762 isl_map_free(map);
2763 return NULL;
2766 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2767 enum isl_dim_type type, unsigned first, unsigned n)
2769 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2770 type, first, n));
2773 /* Does the description of "bmap" depend on the specified dimensions?
2774 * We also check whether the dimensions appear in any of the div definitions.
2775 * In principle there is no need for this check. If the dimensions appear
2776 * in a div definition, they also appear in the defining constraints of that
2777 * div.
2779 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2780 enum isl_dim_type type, unsigned first, unsigned n)
2782 int i;
2784 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2785 return isl_bool_error;
2787 first += isl_basic_map_offset(bmap, type);
2788 for (i = 0; i < bmap->n_eq; ++i)
2789 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2790 return isl_bool_true;
2791 for (i = 0; i < bmap->n_ineq; ++i)
2792 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2793 return isl_bool_true;
2794 for (i = 0; i < bmap->n_div; ++i) {
2795 if (isl_int_is_zero(bmap->div[i][0]))
2796 continue;
2797 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2798 return isl_bool_true;
2801 return isl_bool_false;
2804 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2805 enum isl_dim_type type, unsigned first, unsigned n)
2807 int i;
2809 if (isl_map_check_range(map, type, first, n) < 0)
2810 return isl_bool_error;
2812 for (i = 0; i < map->n; ++i) {
2813 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2814 type, first, n);
2815 if (involves < 0 || involves)
2816 return involves;
2819 return isl_bool_false;
2822 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2823 enum isl_dim_type type, unsigned first, unsigned n)
2825 return isl_basic_map_involves_dims(bset, type, first, n);
2828 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2829 enum isl_dim_type type, unsigned first, unsigned n)
2831 return isl_map_involves_dims(set, type, first, n);
2834 /* Drop all constraints in bmap that involve any of the dimensions
2835 * first to first+n-1.
2836 * This function only performs the actual removal of constraints.
2838 * This function should not call finalize since it is used by
2839 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2841 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2842 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2844 int i;
2846 if (n == 0)
2847 return bmap;
2849 bmap = isl_basic_map_cow(bmap);
2851 if (!bmap)
2852 return NULL;
2854 for (i = bmap->n_eq - 1; i >= 0; --i) {
2855 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2856 continue;
2857 isl_basic_map_drop_equality(bmap, i);
2860 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2861 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2862 continue;
2863 isl_basic_map_drop_inequality(bmap, i);
2866 return bmap;
2869 /* Drop all constraints in bset that involve any of the dimensions
2870 * first to first+n-1.
2871 * This function only performs the actual removal of constraints.
2873 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2874 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2876 return isl_basic_map_drop_constraints_involving(bset, first, n);
2879 /* Drop all constraints in bmap that do not involve any of the dimensions
2880 * first to first + n - 1 of the given type.
2882 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2883 __isl_take isl_basic_map *bmap,
2884 enum isl_dim_type type, unsigned first, unsigned n)
2886 int i;
2888 if (n == 0) {
2889 isl_space *space = isl_basic_map_get_space(bmap);
2890 isl_basic_map_free(bmap);
2891 return isl_basic_map_universe(space);
2893 bmap = isl_basic_map_cow(bmap);
2894 if (!bmap)
2895 return NULL;
2897 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2898 return isl_basic_map_free(bmap);
2900 first += isl_basic_map_offset(bmap, type) - 1;
2902 for (i = bmap->n_eq - 1; i >= 0; --i) {
2903 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2904 continue;
2905 isl_basic_map_drop_equality(bmap, i);
2908 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2909 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2910 continue;
2911 isl_basic_map_drop_inequality(bmap, i);
2914 bmap = isl_basic_map_add_known_div_constraints(bmap);
2915 return bmap;
2918 /* Drop all constraints in bset that do not involve any of the dimensions
2919 * first to first + n - 1 of the given type.
2921 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2922 __isl_take isl_basic_set *bset,
2923 enum isl_dim_type type, unsigned first, unsigned n)
2925 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2926 type, first, n);
2929 /* Drop all constraints in bmap that involve any of the dimensions
2930 * first to first + n - 1 of the given type.
2932 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2933 __isl_take isl_basic_map *bmap,
2934 enum isl_dim_type type, unsigned first, unsigned n)
2936 if (!bmap)
2937 return NULL;
2938 if (n == 0)
2939 return bmap;
2941 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2942 return isl_basic_map_free(bmap);
2944 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2945 first += isl_basic_map_offset(bmap, type) - 1;
2946 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2947 bmap = isl_basic_map_add_known_div_constraints(bmap);
2948 return bmap;
2951 /* Drop all constraints in bset that involve any of the dimensions
2952 * first to first + n - 1 of the given type.
2954 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2955 __isl_take isl_basic_set *bset,
2956 enum isl_dim_type type, unsigned first, unsigned n)
2958 return isl_basic_map_drop_constraints_involving_dims(bset,
2959 type, first, n);
2962 /* Drop constraints from "map" by applying "drop" to each basic map.
2964 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2965 enum isl_dim_type type, unsigned first, unsigned n,
2966 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2967 enum isl_dim_type type, unsigned first, unsigned n))
2969 int i;
2971 if (isl_map_check_range(map, type, first, n) < 0)
2972 return isl_map_free(map);
2974 map = isl_map_cow(map);
2975 if (!map)
2976 return NULL;
2978 for (i = 0; i < map->n; ++i) {
2979 map->p[i] = drop(map->p[i], type, first, n);
2980 if (!map->p[i])
2981 return isl_map_free(map);
2984 if (map->n > 1)
2985 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2987 return map;
2990 /* Drop all constraints in map that involve any of the dimensions
2991 * first to first + n - 1 of the given type.
2993 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2994 __isl_take isl_map *map,
2995 enum isl_dim_type type, unsigned first, unsigned n)
2997 if (n == 0)
2998 return map;
2999 return drop_constraints(map, type, first, n,
3000 &isl_basic_map_drop_constraints_involving_dims);
3003 /* Drop all constraints in "map" that do not involve any of the dimensions
3004 * first to first + n - 1 of the given type.
3006 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3007 __isl_take isl_map *map,
3008 enum isl_dim_type type, unsigned first, unsigned n)
3010 if (n == 0) {
3011 isl_space *space = isl_map_get_space(map);
3012 isl_map_free(map);
3013 return isl_map_universe(space);
3015 return drop_constraints(map, type, first, n,
3016 &isl_basic_map_drop_constraints_not_involving_dims);
3019 /* Drop all constraints in set that involve any of the dimensions
3020 * first to first + n - 1 of the given type.
3022 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3023 __isl_take isl_set *set,
3024 enum isl_dim_type type, unsigned first, unsigned n)
3026 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3029 /* Drop all constraints in "set" that do not involve any of the dimensions
3030 * first to first + n - 1 of the given type.
3032 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3033 __isl_take isl_set *set,
3034 enum isl_dim_type type, unsigned first, unsigned n)
3036 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3039 /* Does local variable "div" of "bmap" have a complete explicit representation?
3040 * Having a complete explicit representation requires not only
3041 * an explicit representation, but also that all local variables
3042 * that appear in this explicit representation in turn have
3043 * a complete explicit representation.
3045 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3047 int i;
3048 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3049 isl_bool marked;
3051 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3052 if (marked < 0 || marked)
3053 return isl_bool_not(marked);
3055 for (i = bmap->n_div - 1; i >= 0; --i) {
3056 isl_bool known;
3058 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3059 continue;
3060 known = isl_basic_map_div_is_known(bmap, i);
3061 if (known < 0 || !known)
3062 return known;
3065 return isl_bool_true;
3068 /* Remove all divs that are unknown or defined in terms of unknown divs.
3070 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3071 __isl_take isl_basic_map *bmap)
3073 int i;
3075 if (!bmap)
3076 return NULL;
3078 for (i = bmap->n_div - 1; i >= 0; --i) {
3079 if (isl_basic_map_div_is_known(bmap, i))
3080 continue;
3081 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3082 if (!bmap)
3083 return NULL;
3084 i = bmap->n_div;
3087 return bmap;
3090 /* Remove all divs that are unknown or defined in terms of unknown divs.
3092 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3093 __isl_take isl_basic_set *bset)
3095 return isl_basic_map_remove_unknown_divs(bset);
3098 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3100 int i;
3102 if (!map)
3103 return NULL;
3104 if (map->n == 0)
3105 return map;
3107 map = isl_map_cow(map);
3108 if (!map)
3109 return NULL;
3111 for (i = 0; i < map->n; ++i) {
3112 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3113 if (!map->p[i])
3114 goto error;
3116 return map;
3117 error:
3118 isl_map_free(map);
3119 return NULL;
3122 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3124 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3127 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3128 __isl_take isl_basic_set *bset,
3129 enum isl_dim_type type, unsigned first, unsigned n)
3131 isl_basic_map *bmap = bset_to_bmap(bset);
3132 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3133 return bset_from_bmap(bmap);
3136 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3137 enum isl_dim_type type, unsigned first, unsigned n)
3139 int i;
3141 if (n == 0)
3142 return map;
3144 map = isl_map_cow(map);
3145 if (isl_map_check_range(map, type, first, n) < 0)
3146 return isl_map_free(map);
3148 for (i = 0; i < map->n; ++i) {
3149 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3150 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3151 if (!map->p[i])
3152 goto error;
3154 map = isl_map_drop(map, type, first, n);
3155 return map;
3156 error:
3157 isl_map_free(map);
3158 return NULL;
3161 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3162 enum isl_dim_type type, unsigned first, unsigned n)
3164 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3165 type, first, n));
3168 /* Project out n inputs starting at first using Fourier-Motzkin */
3169 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3170 unsigned first, unsigned n)
3172 return isl_map_remove_dims(map, isl_dim_in, first, n);
3175 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3176 FILE *out, int indent)
3178 isl_printer *p;
3180 if (!bset) {
3181 fprintf(out, "null basic set\n");
3182 return;
3185 fprintf(out, "%*s", indent, "");
3186 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3187 bset->ref, bset->dim->nparam, bset->dim->n_out,
3188 bset->extra, bset->flags);
3190 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3191 p = isl_printer_set_dump(p, 1);
3192 p = isl_printer_set_indent(p, indent);
3193 p = isl_printer_start_line(p);
3194 p = isl_printer_print_basic_set(p, bset);
3195 p = isl_printer_end_line(p);
3196 isl_printer_free(p);
3199 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3200 FILE *out, int indent)
3202 isl_printer *p;
3204 if (!bmap) {
3205 fprintf(out, "null basic map\n");
3206 return;
3209 fprintf(out, "%*s", indent, "");
3210 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3211 "flags: %x, n_name: %d\n",
3212 bmap->ref,
3213 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3214 bmap->extra, bmap->flags, bmap->dim->n_id);
3216 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3217 p = isl_printer_set_dump(p, 1);
3218 p = isl_printer_set_indent(p, indent);
3219 p = isl_printer_start_line(p);
3220 p = isl_printer_print_basic_map(p, bmap);
3221 p = isl_printer_end_line(p);
3222 isl_printer_free(p);
3225 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3226 unsigned pos)
3228 unsigned total;
3230 if (!bmap)
3231 return NULL;
3232 total = isl_basic_map_total_dim(bmap);
3233 if (pos >= bmap->n_ineq)
3234 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3235 "invalid position", return isl_basic_map_free(bmap));
3236 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3237 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3238 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3239 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3240 return bmap;
3243 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3244 unsigned flags)
3246 if (isl_space_check_is_set(space) < 0)
3247 goto error;
3248 return isl_map_alloc_space(space, n, flags);
3249 error:
3250 isl_space_free(space);
3251 return NULL;
3254 /* Make sure "map" has room for at least "n" more basic maps.
3256 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3258 int i;
3259 struct isl_map *grown = NULL;
3261 if (!map)
3262 return NULL;
3263 isl_assert(map->ctx, n >= 0, goto error);
3264 if (map->n + n <= map->size)
3265 return map;
3266 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3267 if (!grown)
3268 goto error;
3269 for (i = 0; i < map->n; ++i) {
3270 grown->p[i] = isl_basic_map_copy(map->p[i]);
3271 if (!grown->p[i])
3272 goto error;
3273 grown->n++;
3275 isl_map_free(map);
3276 return grown;
3277 error:
3278 isl_map_free(grown);
3279 isl_map_free(map);
3280 return NULL;
3283 /* Make sure "set" has room for at least "n" more basic sets.
3285 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3287 return set_from_map(isl_map_grow(set_to_map(set), n));
3290 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3292 return isl_map_from_basic_map(bset);
3295 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3297 struct isl_map *map;
3299 if (!bmap)
3300 return NULL;
3302 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3303 return isl_map_add_basic_map(map, bmap);
3306 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3307 __isl_take isl_basic_set *bset)
3309 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3310 bset_to_bmap(bset)));
3313 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3315 return isl_map_free(set);
3318 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3320 int i;
3322 if (!set) {
3323 fprintf(out, "null set\n");
3324 return;
3327 fprintf(out, "%*s", indent, "");
3328 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3329 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3330 set->flags);
3331 for (i = 0; i < set->n; ++i) {
3332 fprintf(out, "%*s", indent, "");
3333 fprintf(out, "basic set %d:\n", i);
3334 isl_basic_set_print_internal(set->p[i], out, indent+4);
3338 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3340 int i;
3342 if (!map) {
3343 fprintf(out, "null map\n");
3344 return;
3347 fprintf(out, "%*s", indent, "");
3348 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3349 "flags: %x, n_name: %d\n",
3350 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3351 map->dim->n_out, map->flags, map->dim->n_id);
3352 for (i = 0; i < map->n; ++i) {
3353 fprintf(out, "%*s", indent, "");
3354 fprintf(out, "basic map %d:\n", i);
3355 isl_basic_map_print_internal(map->p[i], out, indent+4);
3359 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3360 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3362 struct isl_basic_map *bmap_domain;
3364 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3365 goto error;
3367 if (isl_basic_set_dim(bset, isl_dim_set) != 0)
3368 isl_assert(bset->ctx,
3369 isl_basic_map_compatible_domain(bmap, bset), goto error);
3371 bmap = isl_basic_map_cow(bmap);
3372 if (!bmap)
3373 goto error;
3374 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3375 bset->n_div, bset->n_eq, bset->n_ineq);
3376 bmap_domain = isl_basic_map_from_domain(bset);
3377 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3379 bmap = isl_basic_map_simplify(bmap);
3380 return isl_basic_map_finalize(bmap);
3381 error:
3382 isl_basic_map_free(bmap);
3383 isl_basic_set_free(bset);
3384 return NULL;
3387 /* Check that the space of "bset" is the same as that of the range of "bmap".
3389 static isl_stat isl_basic_map_check_compatible_range(
3390 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3392 isl_bool ok;
3394 ok = isl_basic_map_compatible_range(bmap, bset);
3395 if (ok < 0)
3396 return isl_stat_error;
3397 if (!ok)
3398 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3399 "incompatible spaces", return isl_stat_error);
3401 return isl_stat_ok;
3404 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3405 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3407 struct isl_basic_map *bmap_range;
3409 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3410 goto error;
3412 if (isl_basic_set_dim(bset, isl_dim_set) != 0 &&
3413 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3414 goto error;
3416 if (isl_basic_set_plain_is_universe(bset)) {
3417 isl_basic_set_free(bset);
3418 return bmap;
3421 bmap = isl_basic_map_cow(bmap);
3422 if (!bmap)
3423 goto error;
3424 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3425 bset->n_div, bset->n_eq, bset->n_ineq);
3426 bmap_range = bset_to_bmap(bset);
3427 bmap = add_constraints(bmap, bmap_range, 0, 0);
3429 bmap = isl_basic_map_simplify(bmap);
3430 return isl_basic_map_finalize(bmap);
3431 error:
3432 isl_basic_map_free(bmap);
3433 isl_basic_set_free(bset);
3434 return NULL;
3437 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3438 __isl_keep isl_vec *vec)
3440 int i;
3441 unsigned total;
3442 isl_int s;
3444 if (!bmap || !vec)
3445 return isl_bool_error;
3447 total = 1 + isl_basic_map_total_dim(bmap);
3448 if (total != vec->size)
3449 return isl_bool_false;
3451 isl_int_init(s);
3453 for (i = 0; i < bmap->n_eq; ++i) {
3454 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3455 if (!isl_int_is_zero(s)) {
3456 isl_int_clear(s);
3457 return isl_bool_false;
3461 for (i = 0; i < bmap->n_ineq; ++i) {
3462 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3463 if (isl_int_is_neg(s)) {
3464 isl_int_clear(s);
3465 return isl_bool_false;
3469 isl_int_clear(s);
3471 return isl_bool_true;
3474 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3475 __isl_keep isl_vec *vec)
3477 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3480 __isl_give isl_basic_map *isl_basic_map_intersect(
3481 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3483 struct isl_vec *sample = NULL;
3484 isl_space *space1, *space2;
3486 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3487 goto error;
3488 space1 = isl_basic_map_peek_space(bmap1);
3489 space2 = isl_basic_map_peek_space(bmap2);
3490 if (isl_space_dim(space1, isl_dim_all) ==
3491 isl_space_dim(space1, isl_dim_param) &&
3492 isl_space_dim(space2, isl_dim_all) !=
3493 isl_space_dim(space2, isl_dim_param))
3494 return isl_basic_map_intersect(bmap2, bmap1);
3496 if (isl_space_dim(space2, isl_dim_all) !=
3497 isl_space_dim(space2, isl_dim_param))
3498 isl_assert(bmap1->ctx,
3499 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3501 if (isl_basic_map_plain_is_empty(bmap1)) {
3502 isl_basic_map_free(bmap2);
3503 return bmap1;
3505 if (isl_basic_map_plain_is_empty(bmap2)) {
3506 isl_basic_map_free(bmap1);
3507 return bmap2;
3510 if (bmap1->sample &&
3511 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3512 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3513 sample = isl_vec_copy(bmap1->sample);
3514 else if (bmap2->sample &&
3515 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3516 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3517 sample = isl_vec_copy(bmap2->sample);
3519 bmap1 = isl_basic_map_cow(bmap1);
3520 if (!bmap1)
3521 goto error;
3522 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3523 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3524 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3526 if (!bmap1)
3527 isl_vec_free(sample);
3528 else if (sample) {
3529 isl_vec_free(bmap1->sample);
3530 bmap1->sample = sample;
3533 bmap1 = isl_basic_map_simplify(bmap1);
3534 return isl_basic_map_finalize(bmap1);
3535 error:
3536 if (sample)
3537 isl_vec_free(sample);
3538 isl_basic_map_free(bmap1);
3539 isl_basic_map_free(bmap2);
3540 return NULL;
3543 struct isl_basic_set *isl_basic_set_intersect(
3544 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3546 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3547 bset_to_bmap(bset2)));
3550 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3551 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3553 return isl_basic_set_intersect(bset1, bset2);
3556 /* Special case of isl_map_intersect, where both map1 and map2
3557 * are convex, without any divs and such that either map1 or map2
3558 * contains a single constraint. This constraint is then simply
3559 * added to the other map.
3561 static __isl_give isl_map *map_intersect_add_constraint(
3562 __isl_take isl_map *map1, __isl_take isl_map *map2)
3564 isl_assert(map1->ctx, map1->n == 1, goto error);
3565 isl_assert(map2->ctx, map1->n == 1, goto error);
3566 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3567 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3569 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3570 return isl_map_intersect(map2, map1);
3572 map1 = isl_map_cow(map1);
3573 if (!map1)
3574 goto error;
3575 if (isl_map_plain_is_empty(map1)) {
3576 isl_map_free(map2);
3577 return map1;
3579 if (map2->p[0]->n_eq == 1)
3580 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3581 else
3582 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3583 map2->p[0]->ineq[0]);
3585 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3586 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3587 if (!map1->p[0])
3588 goto error;
3590 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3591 isl_basic_map_free(map1->p[0]);
3592 map1->n = 0;
3595 isl_map_free(map2);
3597 map1 = isl_map_unmark_normalized(map1);
3598 return map1;
3599 error:
3600 isl_map_free(map1);
3601 isl_map_free(map2);
3602 return NULL;
3605 /* map2 may be either a parameter domain or a map living in the same
3606 * space as map1.
3608 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3609 __isl_take isl_map *map2)
3611 unsigned flags = 0;
3612 isl_bool equal;
3613 isl_map *result;
3614 int i, j;
3616 if (!map1 || !map2)
3617 goto error;
3619 if ((isl_map_plain_is_empty(map1) ||
3620 isl_map_plain_is_universe(map2)) &&
3621 isl_space_is_equal(map1->dim, map2->dim)) {
3622 isl_map_free(map2);
3623 return map1;
3625 if ((isl_map_plain_is_empty(map2) ||
3626 isl_map_plain_is_universe(map1)) &&
3627 isl_space_is_equal(map1->dim, map2->dim)) {
3628 isl_map_free(map1);
3629 return map2;
3632 if (map1->n == 1 && map2->n == 1 &&
3633 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3634 isl_space_is_equal(map1->dim, map2->dim) &&
3635 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3636 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3637 return map_intersect_add_constraint(map1, map2);
3639 equal = isl_map_plain_is_equal(map1, map2);
3640 if (equal < 0)
3641 goto error;
3642 if (equal) {
3643 isl_map_free(map2);
3644 return map1;
3647 if (isl_map_dim(map2, isl_dim_all) != isl_map_dim(map2, isl_dim_param))
3648 isl_assert(map1->ctx,
3649 isl_space_is_equal(map1->dim, map2->dim), goto error);
3651 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3652 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3653 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3655 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3656 map1->n * map2->n, flags);
3657 if (!result)
3658 goto error;
3659 for (i = 0; i < map1->n; ++i)
3660 for (j = 0; j < map2->n; ++j) {
3661 struct isl_basic_map *part;
3662 part = isl_basic_map_intersect(
3663 isl_basic_map_copy(map1->p[i]),
3664 isl_basic_map_copy(map2->p[j]));
3665 if (isl_basic_map_is_empty(part) < 0)
3666 part = isl_basic_map_free(part);
3667 result = isl_map_add_basic_map(result, part);
3668 if (!result)
3669 goto error;
3671 isl_map_free(map1);
3672 isl_map_free(map2);
3673 return result;
3674 error:
3675 isl_map_free(map1);
3676 isl_map_free(map2);
3677 return NULL;
3680 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3681 __isl_take isl_map *map2)
3683 if (!map1 || !map2)
3684 goto error;
3685 if (!isl_space_is_equal(map1->dim, map2->dim))
3686 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3687 "spaces don't match", goto error);
3688 return map_intersect_internal(map1, map2);
3689 error:
3690 isl_map_free(map1);
3691 isl_map_free(map2);
3692 return NULL;
3695 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3696 __isl_take isl_map *map2)
3698 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3701 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3703 return set_from_map(isl_map_intersect(set_to_map(set1),
3704 set_to_map(set2)));
3707 /* map_intersect_internal accepts intersections
3708 * with parameter domains, so we can just call that function.
3710 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3711 __isl_take isl_set *params)
3713 return map_intersect_internal(map, params);
3716 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3717 __isl_take isl_map *map2)
3719 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3722 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3723 __isl_take isl_set *params)
3725 return isl_map_intersect_params(set, params);
3728 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3730 isl_space *space;
3731 unsigned pos, n1, n2;
3733 if (!bmap)
3734 return NULL;
3735 bmap = isl_basic_map_cow(bmap);
3736 if (!bmap)
3737 return NULL;
3738 space = isl_space_reverse(isl_space_copy(bmap->dim));
3739 pos = isl_basic_map_offset(bmap, isl_dim_in);
3740 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3741 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3742 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3743 return isl_basic_map_reset_space(bmap, space);
3746 static __isl_give isl_basic_map *basic_map_space_reset(
3747 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3749 isl_space *space;
3751 if (!bmap)
3752 return NULL;
3753 if (!isl_space_is_named_or_nested(bmap->dim, type))
3754 return bmap;
3756 space = isl_basic_map_get_space(bmap);
3757 space = isl_space_reset(space, type);
3758 bmap = isl_basic_map_reset_space(bmap, space);
3759 return bmap;
3762 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3763 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3764 unsigned pos, unsigned n)
3766 isl_bool rational, is_empty;
3767 isl_space *res_space;
3768 struct isl_basic_map *res;
3769 struct isl_dim_map *dim_map;
3770 unsigned total, off;
3771 enum isl_dim_type t;
3773 if (n == 0)
3774 return basic_map_space_reset(bmap, type);
3776 is_empty = isl_basic_map_plain_is_empty(bmap);
3777 if (is_empty < 0)
3778 return isl_basic_map_free(bmap);
3779 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3780 type, pos, n);
3781 if (!res_space)
3782 return isl_basic_map_free(bmap);
3783 if (is_empty) {
3784 isl_basic_map_free(bmap);
3785 return isl_basic_map_empty(res_space);
3788 total = isl_basic_map_total_dim(bmap) + n;
3789 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3790 off = 0;
3791 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3792 if (t != type) {
3793 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3794 } else {
3795 unsigned size = isl_basic_map_dim(bmap, t);
3796 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3797 0, pos, off);
3798 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3799 pos, size - pos, off + pos + n);
3801 off += isl_space_dim(res_space, t);
3803 isl_dim_map_div(dim_map, bmap, off);
3805 res = isl_basic_map_alloc_space(res_space,
3806 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3807 rational = isl_basic_map_is_rational(bmap);
3808 if (rational < 0)
3809 res = isl_basic_map_free(res);
3810 if (rational)
3811 res = isl_basic_map_set_rational(res);
3812 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3813 return isl_basic_map_finalize(res);
3816 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3817 __isl_take isl_basic_set *bset,
3818 enum isl_dim_type type, unsigned pos, unsigned n)
3820 return isl_basic_map_insert_dims(bset, type, pos, n);
3823 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3824 enum isl_dim_type type, unsigned n)
3826 if (!bmap)
3827 return NULL;
3828 return isl_basic_map_insert_dims(bmap, type,
3829 isl_basic_map_dim(bmap, type), n);
3832 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3833 enum isl_dim_type type, unsigned n)
3835 if (!bset)
3836 return NULL;
3837 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3838 return isl_basic_map_add_dims(bset, type, n);
3839 error:
3840 isl_basic_set_free(bset);
3841 return NULL;
3844 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3845 enum isl_dim_type type)
3847 isl_space *space;
3849 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3850 return map;
3852 space = isl_map_get_space(map);
3853 space = isl_space_reset(space, type);
3854 map = isl_map_reset_space(map, space);
3855 return map;
3858 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3859 enum isl_dim_type type, unsigned pos, unsigned n)
3861 int i;
3863 if (n == 0)
3864 return map_space_reset(map, type);
3866 map = isl_map_cow(map);
3867 if (!map)
3868 return NULL;
3870 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3871 if (!map->dim)
3872 goto error;
3874 for (i = 0; i < map->n; ++i) {
3875 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3876 if (!map->p[i])
3877 goto error;
3880 return map;
3881 error:
3882 isl_map_free(map);
3883 return NULL;
3886 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3887 enum isl_dim_type type, unsigned pos, unsigned n)
3889 return isl_map_insert_dims(set, type, pos, n);
3892 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3893 enum isl_dim_type type, unsigned n)
3895 if (!map)
3896 return NULL;
3897 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3900 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3901 enum isl_dim_type type, unsigned n)
3903 if (!set)
3904 return NULL;
3905 isl_assert(set->ctx, type != isl_dim_in, goto error);
3906 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3907 error:
3908 isl_set_free(set);
3909 return NULL;
3912 __isl_give isl_basic_map *isl_basic_map_move_dims(
3913 __isl_take isl_basic_map *bmap,
3914 enum isl_dim_type dst_type, unsigned dst_pos,
3915 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3917 isl_space *space;
3918 struct isl_dim_map *dim_map;
3919 struct isl_basic_map *res;
3920 enum isl_dim_type t;
3921 unsigned total, off;
3923 if (!bmap)
3924 return NULL;
3925 if (n == 0) {
3926 bmap = isl_basic_map_reset(bmap, src_type);
3927 bmap = isl_basic_map_reset(bmap, dst_type);
3928 return bmap;
3931 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3932 return isl_basic_map_free(bmap);
3934 if (dst_type == src_type && dst_pos == src_pos)
3935 return bmap;
3937 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3939 if (pos(bmap->dim, dst_type) + dst_pos ==
3940 pos(bmap->dim, src_type) + src_pos +
3941 ((src_type < dst_type) ? n : 0)) {
3942 bmap = isl_basic_map_cow(bmap);
3943 if (!bmap)
3944 return NULL;
3946 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3947 src_type, src_pos, n);
3948 if (!bmap->dim)
3949 goto error;
3951 bmap = isl_basic_map_finalize(bmap);
3953 return bmap;
3956 total = isl_basic_map_total_dim(bmap);
3957 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3959 off = 0;
3960 space = isl_basic_map_peek_space(bmap);
3961 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3962 unsigned size = isl_space_dim(space, t);
3963 if (t == dst_type) {
3964 isl_dim_map_dim_range(dim_map, space, t,
3965 0, dst_pos, off);
3966 off += dst_pos;
3967 isl_dim_map_dim_range(dim_map, space, src_type,
3968 src_pos, n, off);
3969 off += n;
3970 isl_dim_map_dim_range(dim_map, space, t,
3971 dst_pos, size - dst_pos, off);
3972 off += size - dst_pos;
3973 } else if (t == src_type) {
3974 isl_dim_map_dim_range(dim_map, space, t,
3975 0, src_pos, off);
3976 off += src_pos;
3977 isl_dim_map_dim_range(dim_map, space, t,
3978 src_pos + n, size - src_pos - n, off);
3979 off += size - src_pos - n;
3980 } else {
3981 isl_dim_map_dim(dim_map, space, t, off);
3982 off += size;
3985 isl_dim_map_div(dim_map, bmap, off);
3987 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3988 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3989 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3990 if (!bmap)
3991 goto error;
3993 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3994 src_type, src_pos, n);
3995 if (!bmap->dim)
3996 goto error;
3998 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3999 bmap = isl_basic_map_gauss(bmap, NULL);
4000 bmap = isl_basic_map_finalize(bmap);
4002 return bmap;
4003 error:
4004 isl_basic_map_free(bmap);
4005 return NULL;
4008 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4009 enum isl_dim_type dst_type, unsigned dst_pos,
4010 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4012 isl_basic_map *bmap = bset_to_bmap(bset);
4013 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4014 src_type, src_pos, n);
4015 return bset_from_bmap(bmap);
4018 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4019 enum isl_dim_type dst_type, unsigned dst_pos,
4020 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4022 if (!set)
4023 return NULL;
4024 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4025 return set_from_map(isl_map_move_dims(set_to_map(set),
4026 dst_type, dst_pos, src_type, src_pos, n));
4027 error:
4028 isl_set_free(set);
4029 return NULL;
4032 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4033 enum isl_dim_type dst_type, unsigned dst_pos,
4034 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4036 int i;
4038 if (n == 0) {
4039 map = isl_map_reset(map, src_type);
4040 map = isl_map_reset(map, dst_type);
4041 return map;
4044 if (isl_map_check_range(map, src_type, src_pos, n))
4045 return isl_map_free(map);
4047 if (dst_type == src_type && dst_pos == src_pos)
4048 return map;
4050 isl_assert(map->ctx, dst_type != src_type, goto error);
4052 map = isl_map_cow(map);
4053 if (!map)
4054 return NULL;
4056 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4057 if (!map->dim)
4058 goto error;
4060 for (i = 0; i < map->n; ++i) {
4061 map->p[i] = isl_basic_map_move_dims(map->p[i],
4062 dst_type, dst_pos,
4063 src_type, src_pos, n);
4064 if (!map->p[i])
4065 goto error;
4068 return map;
4069 error:
4070 isl_map_free(map);
4071 return NULL;
4074 /* Move the specified dimensions to the last columns right before
4075 * the divs. Don't change the dimension specification of bmap.
4076 * That's the responsibility of the caller.
4078 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4079 enum isl_dim_type type, unsigned first, unsigned n)
4081 isl_space *space;
4082 struct isl_dim_map *dim_map;
4083 struct isl_basic_map *res;
4084 enum isl_dim_type t;
4085 unsigned total, off;
4087 if (!bmap)
4088 return NULL;
4089 if (isl_basic_map_offset(bmap, type) + first + n ==
4090 isl_basic_map_offset(bmap, isl_dim_div))
4091 return bmap;
4093 total = isl_basic_map_total_dim(bmap);
4094 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4096 off = 0;
4097 space = isl_basic_map_peek_space(bmap);
4098 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4099 unsigned size = isl_space_dim(space, t);
4100 if (t == type) {
4101 isl_dim_map_dim_range(dim_map, space, t,
4102 0, first, off);
4103 off += first;
4104 isl_dim_map_dim_range(dim_map, space, t,
4105 first, n, total - bmap->n_div - n);
4106 isl_dim_map_dim_range(dim_map, space, t,
4107 first + n, size - (first + n), off);
4108 off += size - (first + n);
4109 } else {
4110 isl_dim_map_dim(dim_map, space, t, off);
4111 off += size;
4114 isl_dim_map_div(dim_map, bmap, off + n);
4116 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4117 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4118 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4119 return res;
4122 /* Insert "n" rows in the divs of "bmap".
4124 * The number of columns is not changed, which means that the last
4125 * dimensions of "bmap" are being reintepreted as the new divs.
4126 * The space of "bmap" is not adjusted, however, which means
4127 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4128 * from the space of "bmap" is the responsibility of the caller.
4130 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4131 int n)
4133 int i;
4134 size_t row_size;
4135 isl_int **new_div;
4136 isl_int *old;
4138 bmap = isl_basic_map_cow(bmap);
4139 if (!bmap)
4140 return NULL;
4142 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4143 old = bmap->block2.data;
4144 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4145 (bmap->extra + n) * (1 + row_size));
4146 if (!bmap->block2.data)
4147 return isl_basic_map_free(bmap);
4148 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4149 if (!new_div)
4150 return isl_basic_map_free(bmap);
4151 for (i = 0; i < n; ++i) {
4152 new_div[i] = bmap->block2.data +
4153 (bmap->extra + i) * (1 + row_size);
4154 isl_seq_clr(new_div[i], 1 + row_size);
4156 for (i = 0; i < bmap->extra; ++i)
4157 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4158 free(bmap->div);
4159 bmap->div = new_div;
4160 bmap->n_div += n;
4161 bmap->extra += n;
4163 return bmap;
4166 /* Drop constraints from "bmap" that only involve the variables
4167 * of "type" in the range [first, first + n] that are not related
4168 * to any of the variables outside that interval.
4169 * These constraints cannot influence the values for the variables
4170 * outside the interval, except in case they cause "bmap" to be empty.
4171 * Only drop the constraints if "bmap" is known to be non-empty.
4173 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4174 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4175 unsigned first, unsigned n)
4177 int i;
4178 int *groups;
4179 unsigned dim, n_div;
4180 isl_bool non_empty;
4182 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4183 if (non_empty < 0)
4184 return isl_basic_map_free(bmap);
4185 if (!non_empty)
4186 return bmap;
4188 dim = isl_basic_map_dim(bmap, isl_dim_all);
4189 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4190 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4191 if (!groups)
4192 return isl_basic_map_free(bmap);
4193 first += isl_basic_map_offset(bmap, type) - 1;
4194 for (i = 0; i < first; ++i)
4195 groups[i] = -1;
4196 for (i = first + n; i < dim - n_div; ++i)
4197 groups[i] = -1;
4199 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4201 return bmap;
4204 /* Turn the n dimensions of type type, starting at first
4205 * into existentially quantified variables.
4207 * If a subset of the projected out variables are unrelated
4208 * to any of the variables that remain, then the constraints
4209 * involving this subset are simply dropped first.
4211 __isl_give isl_basic_map *isl_basic_map_project_out(
4212 __isl_take isl_basic_map *bmap,
4213 enum isl_dim_type type, unsigned first, unsigned n)
4215 isl_bool empty;
4217 if (n == 0)
4218 return basic_map_space_reset(bmap, type);
4219 if (type == isl_dim_div)
4220 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4221 "cannot project out existentially quantified variables",
4222 return isl_basic_map_free(bmap));
4224 empty = isl_basic_map_plain_is_empty(bmap);
4225 if (empty < 0)
4226 return isl_basic_map_free(bmap);
4227 if (empty)
4228 bmap = isl_basic_map_set_to_empty(bmap);
4230 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4231 if (!bmap)
4232 return NULL;
4234 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4235 return isl_basic_map_remove_dims(bmap, type, first, n);
4237 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4238 return isl_basic_map_free(bmap);
4240 bmap = move_last(bmap, type, first, n);
4241 bmap = isl_basic_map_cow(bmap);
4242 bmap = insert_div_rows(bmap, n);
4243 if (!bmap)
4244 return NULL;
4246 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4247 if (!bmap->dim)
4248 goto error;
4249 bmap = isl_basic_map_simplify(bmap);
4250 bmap = isl_basic_map_drop_redundant_divs(bmap);
4251 return isl_basic_map_finalize(bmap);
4252 error:
4253 isl_basic_map_free(bmap);
4254 return NULL;
4257 /* Turn the n dimensions of type type, starting at first
4258 * into existentially quantified variables.
4260 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4261 enum isl_dim_type type, unsigned first, unsigned n)
4263 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4264 type, first, n));
4267 /* Turn the n dimensions of type type, starting at first
4268 * into existentially quantified variables.
4270 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4271 enum isl_dim_type type, unsigned first, unsigned n)
4273 int i;
4275 if (n == 0)
4276 return map_space_reset(map, type);
4278 if (isl_map_check_range(map, type, first, n) < 0)
4279 return isl_map_free(map);
4281 map = isl_map_cow(map);
4282 if (!map)
4283 return NULL;
4285 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4286 if (!map->dim)
4287 goto error;
4289 for (i = 0; i < map->n; ++i) {
4290 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4291 if (!map->p[i])
4292 goto error;
4295 return map;
4296 error:
4297 isl_map_free(map);
4298 return NULL;
4301 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4302 * into existentially quantified variables.
4304 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4305 enum isl_dim_type type, unsigned first, unsigned n)
4307 unsigned dim;
4309 if (isl_map_check_range(map, type, first, n) < 0)
4310 return isl_map_free(map);
4311 dim = isl_map_dim(map, type);
4312 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4313 map = isl_map_project_out(map, type, 0, first);
4314 return map;
4317 /* Turn the n dimensions of type type, starting at first
4318 * into existentially quantified variables.
4320 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4321 enum isl_dim_type type, unsigned first, unsigned n)
4323 return set_from_map(isl_map_project_out(set_to_map(set),
4324 type, first, n));
4327 /* Return a map that projects the elements in "set" onto their
4328 * "n" set dimensions starting at "first".
4329 * "type" should be equal to isl_dim_set.
4331 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4332 enum isl_dim_type type, unsigned first, unsigned n)
4334 int i;
4335 isl_map *map;
4337 if (type != isl_dim_set)
4338 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4339 "only set dimensions can be projected out", goto error);
4340 if (isl_set_check_range(set, type, first, n) < 0)
4341 return isl_set_free(set);
4343 map = isl_map_from_domain(set);
4344 map = isl_map_add_dims(map, isl_dim_out, n);
4345 for (i = 0; i < n; ++i)
4346 map = isl_map_equate(map, isl_dim_in, first + i,
4347 isl_dim_out, i);
4348 return map;
4349 error:
4350 isl_set_free(set);
4351 return NULL;
4354 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4355 unsigned n)
4357 int i, j;
4359 for (i = 0; i < n; ++i) {
4360 j = isl_basic_map_alloc_div(bmap);
4361 if (j < 0)
4362 goto error;
4363 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4365 return bmap;
4366 error:
4367 isl_basic_map_free(bmap);
4368 return NULL;
4371 struct isl_basic_map *isl_basic_map_apply_range(
4372 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4374 isl_space *space_result = NULL;
4375 struct isl_basic_map *bmap;
4376 unsigned n_in, n_out, n, nparam, total, pos;
4377 struct isl_dim_map *dim_map1, *dim_map2;
4379 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4380 goto error;
4381 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4382 bmap2->dim, isl_dim_in))
4383 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4384 "spaces don't match", goto error);
4386 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4387 isl_basic_map_get_space(bmap2));
4389 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4390 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4391 n = isl_basic_map_dim(bmap1, isl_dim_out);
4392 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4394 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4395 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4396 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4397 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4398 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4399 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4400 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4401 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4402 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4403 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4404 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4406 bmap = isl_basic_map_alloc_space(space_result,
4407 bmap1->n_div + bmap2->n_div + n,
4408 bmap1->n_eq + bmap2->n_eq,
4409 bmap1->n_ineq + bmap2->n_ineq);
4410 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4411 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4412 bmap = add_divs(bmap, n);
4413 bmap = isl_basic_map_simplify(bmap);
4414 bmap = isl_basic_map_drop_redundant_divs(bmap);
4415 return isl_basic_map_finalize(bmap);
4416 error:
4417 isl_basic_map_free(bmap1);
4418 isl_basic_map_free(bmap2);
4419 return NULL;
4422 struct isl_basic_set *isl_basic_set_apply(
4423 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4425 if (!bset || !bmap)
4426 goto error;
4428 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4429 goto error);
4431 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4432 bmap));
4433 error:
4434 isl_basic_set_free(bset);
4435 isl_basic_map_free(bmap);
4436 return NULL;
4439 struct isl_basic_map *isl_basic_map_apply_domain(
4440 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4442 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4443 goto error;
4444 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4445 bmap2->dim, isl_dim_in))
4446 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4447 "spaces don't match", goto error);
4449 bmap1 = isl_basic_map_reverse(bmap1);
4450 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4451 return isl_basic_map_reverse(bmap1);
4452 error:
4453 isl_basic_map_free(bmap1);
4454 isl_basic_map_free(bmap2);
4455 return NULL;
4458 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4459 * A \cap B -> f(A) + f(B)
4461 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4462 __isl_take isl_basic_map *bmap2)
4464 unsigned n_in, n_out, nparam, total, pos;
4465 struct isl_basic_map *bmap = NULL;
4466 struct isl_dim_map *dim_map1, *dim_map2;
4467 int i;
4469 if (!bmap1 || !bmap2)
4470 goto error;
4472 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4473 goto error);
4475 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4476 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4477 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4479 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4480 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4481 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4482 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4483 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4484 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4485 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4486 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4487 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4488 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4489 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4491 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4492 bmap1->n_div + bmap2->n_div + 2 * n_out,
4493 bmap1->n_eq + bmap2->n_eq + n_out,
4494 bmap1->n_ineq + bmap2->n_ineq);
4495 for (i = 0; i < n_out; ++i) {
4496 int j = isl_basic_map_alloc_equality(bmap);
4497 if (j < 0)
4498 goto error;
4499 isl_seq_clr(bmap->eq[j], 1+total);
4500 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4501 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4502 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4504 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4505 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4506 bmap = add_divs(bmap, 2 * n_out);
4508 bmap = isl_basic_map_simplify(bmap);
4509 return isl_basic_map_finalize(bmap);
4510 error:
4511 isl_basic_map_free(bmap);
4512 isl_basic_map_free(bmap1);
4513 isl_basic_map_free(bmap2);
4514 return NULL;
4517 /* Given two maps A -> f(A) and B -> g(B), construct a map
4518 * A \cap B -> f(A) + f(B)
4520 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4521 __isl_take isl_map *map2)
4523 struct isl_map *result;
4524 int i, j;
4526 if (!map1 || !map2)
4527 goto error;
4529 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4531 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4532 map1->n * map2->n, 0);
4533 if (!result)
4534 goto error;
4535 for (i = 0; i < map1->n; ++i)
4536 for (j = 0; j < map2->n; ++j) {
4537 struct isl_basic_map *part;
4538 part = isl_basic_map_sum(
4539 isl_basic_map_copy(map1->p[i]),
4540 isl_basic_map_copy(map2->p[j]));
4541 if (isl_basic_map_is_empty(part))
4542 isl_basic_map_free(part);
4543 else
4544 result = isl_map_add_basic_map(result, part);
4545 if (!result)
4546 goto error;
4548 isl_map_free(map1);
4549 isl_map_free(map2);
4550 return result;
4551 error:
4552 isl_map_free(map1);
4553 isl_map_free(map2);
4554 return NULL;
4557 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4558 __isl_take isl_set *set2)
4560 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4563 /* Given a basic map A -> f(A), construct A -> -f(A).
4565 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4567 int i, j;
4568 unsigned off, n;
4570 bmap = isl_basic_map_cow(bmap);
4571 if (!bmap)
4572 return NULL;
4574 n = isl_basic_map_dim(bmap, isl_dim_out);
4575 off = isl_basic_map_offset(bmap, isl_dim_out);
4576 for (i = 0; i < bmap->n_eq; ++i)
4577 for (j = 0; j < n; ++j)
4578 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4579 for (i = 0; i < bmap->n_ineq; ++i)
4580 for (j = 0; j < n; ++j)
4581 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4582 for (i = 0; i < bmap->n_div; ++i)
4583 for (j = 0; j < n; ++j)
4584 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4585 bmap = isl_basic_map_gauss(bmap, NULL);
4586 return isl_basic_map_finalize(bmap);
4589 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4591 return isl_basic_map_neg(bset);
4594 /* Given a map A -> f(A), construct A -> -f(A).
4596 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4598 int i;
4600 map = isl_map_cow(map);
4601 if (!map)
4602 return NULL;
4604 for (i = 0; i < map->n; ++i) {
4605 map->p[i] = isl_basic_map_neg(map->p[i]);
4606 if (!map->p[i])
4607 goto error;
4610 return map;
4611 error:
4612 isl_map_free(map);
4613 return NULL;
4616 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4618 return set_from_map(isl_map_neg(set_to_map(set)));
4621 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4622 * A -> floor(f(A)/d).
4624 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4625 isl_int d)
4627 unsigned n_in, n_out, nparam, total, pos;
4628 struct isl_basic_map *result = NULL;
4629 struct isl_dim_map *dim_map;
4630 int i;
4632 if (!bmap)
4633 return NULL;
4635 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4636 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4637 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4639 total = nparam + n_in + n_out + bmap->n_div + n_out;
4640 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4641 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4642 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4643 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4644 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4646 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4647 bmap->n_div + n_out,
4648 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4649 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4650 result = add_divs(result, n_out);
4651 for (i = 0; i < n_out; ++i) {
4652 int j;
4653 j = isl_basic_map_alloc_inequality(result);
4654 if (j < 0)
4655 goto error;
4656 isl_seq_clr(result->ineq[j], 1+total);
4657 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4658 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4659 j = isl_basic_map_alloc_inequality(result);
4660 if (j < 0)
4661 goto error;
4662 isl_seq_clr(result->ineq[j], 1+total);
4663 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4664 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4665 isl_int_sub_ui(result->ineq[j][0], d, 1);
4668 result = isl_basic_map_simplify(result);
4669 return isl_basic_map_finalize(result);
4670 error:
4671 isl_basic_map_free(result);
4672 return NULL;
4675 /* Given a map A -> f(A) and an integer d, construct a map
4676 * A -> floor(f(A)/d).
4678 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4680 int i;
4682 map = isl_map_cow(map);
4683 if (!map)
4684 return NULL;
4686 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4687 for (i = 0; i < map->n; ++i) {
4688 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4689 if (!map->p[i])
4690 goto error;
4692 map = isl_map_unmark_normalized(map);
4694 return map;
4695 error:
4696 isl_map_free(map);
4697 return NULL;
4700 /* Given a map A -> f(A) and an integer d, construct a map
4701 * A -> floor(f(A)/d).
4703 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4704 __isl_take isl_val *d)
4706 if (!map || !d)
4707 goto error;
4708 if (!isl_val_is_int(d))
4709 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4710 "expecting integer denominator", goto error);
4711 map = isl_map_floordiv(map, d->n);
4712 isl_val_free(d);
4713 return map;
4714 error:
4715 isl_map_free(map);
4716 isl_val_free(d);
4717 return NULL;
4720 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4721 unsigned pos)
4723 int i;
4724 unsigned nparam;
4725 unsigned n_in;
4727 i = isl_basic_map_alloc_equality(bmap);
4728 if (i < 0)
4729 goto error;
4730 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4731 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4732 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4733 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4734 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4735 return isl_basic_map_finalize(bmap);
4736 error:
4737 isl_basic_map_free(bmap);
4738 return NULL;
4741 /* Add a constraint to "bmap" expressing i_pos < o_pos
4743 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4744 unsigned pos)
4746 int i;
4747 unsigned nparam;
4748 unsigned n_in;
4750 i = isl_basic_map_alloc_inequality(bmap);
4751 if (i < 0)
4752 goto error;
4753 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4754 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4755 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4756 isl_int_set_si(bmap->ineq[i][0], -1);
4757 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4758 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4759 return isl_basic_map_finalize(bmap);
4760 error:
4761 isl_basic_map_free(bmap);
4762 return NULL;
4765 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4767 static __isl_give isl_basic_map *var_less_or_equal(
4768 __isl_take isl_basic_map *bmap, unsigned pos)
4770 int i;
4771 unsigned nparam;
4772 unsigned n_in;
4774 i = isl_basic_map_alloc_inequality(bmap);
4775 if (i < 0)
4776 goto error;
4777 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4778 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4779 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4780 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4781 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4782 return isl_basic_map_finalize(bmap);
4783 error:
4784 isl_basic_map_free(bmap);
4785 return NULL;
4788 /* Add a constraint to "bmap" expressing i_pos > o_pos
4790 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4791 unsigned pos)
4793 int i;
4794 unsigned nparam;
4795 unsigned n_in;
4797 i = isl_basic_map_alloc_inequality(bmap);
4798 if (i < 0)
4799 goto error;
4800 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4801 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4802 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4803 isl_int_set_si(bmap->ineq[i][0], -1);
4804 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4805 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4806 return isl_basic_map_finalize(bmap);
4807 error:
4808 isl_basic_map_free(bmap);
4809 return NULL;
4812 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4814 static __isl_give isl_basic_map *var_more_or_equal(
4815 __isl_take isl_basic_map *bmap, unsigned pos)
4817 int i;
4818 unsigned nparam;
4819 unsigned n_in;
4821 i = isl_basic_map_alloc_inequality(bmap);
4822 if (i < 0)
4823 goto error;
4824 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4825 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4826 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4827 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4828 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4829 return isl_basic_map_finalize(bmap);
4830 error:
4831 isl_basic_map_free(bmap);
4832 return NULL;
4835 __isl_give isl_basic_map *isl_basic_map_equal(
4836 __isl_take isl_space *space, unsigned n_equal)
4838 int i;
4839 struct isl_basic_map *bmap;
4840 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4841 if (!bmap)
4842 return NULL;
4843 for (i = 0; i < n_equal && bmap; ++i)
4844 bmap = var_equal(bmap, i);
4845 return isl_basic_map_finalize(bmap);
4848 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4850 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4851 unsigned pos)
4853 int i;
4854 struct isl_basic_map *bmap;
4855 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4856 if (!bmap)
4857 return NULL;
4858 for (i = 0; i < pos && bmap; ++i)
4859 bmap = var_equal(bmap, i);
4860 if (bmap)
4861 bmap = var_less(bmap, pos);
4862 return isl_basic_map_finalize(bmap);
4865 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4867 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4868 __isl_take isl_space *space, unsigned pos)
4870 int i;
4871 isl_basic_map *bmap;
4873 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4874 for (i = 0; i < pos; ++i)
4875 bmap = var_equal(bmap, i);
4876 bmap = var_less_or_equal(bmap, pos);
4877 return isl_basic_map_finalize(bmap);
4880 /* Return a relation on "space" expressing i_pos > o_pos
4882 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4883 unsigned pos)
4885 int i;
4886 struct isl_basic_map *bmap;
4887 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4888 if (!bmap)
4889 return NULL;
4890 for (i = 0; i < pos && bmap; ++i)
4891 bmap = var_equal(bmap, i);
4892 if (bmap)
4893 bmap = var_more(bmap, pos);
4894 return isl_basic_map_finalize(bmap);
4897 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4899 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4900 __isl_take isl_space *space, unsigned pos)
4902 int i;
4903 isl_basic_map *bmap;
4905 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4906 for (i = 0; i < pos; ++i)
4907 bmap = var_equal(bmap, i);
4908 bmap = var_more_or_equal(bmap, pos);
4909 return isl_basic_map_finalize(bmap);
4912 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4913 unsigned n, int equal)
4915 struct isl_map *map;
4916 int i;
4918 if (n == 0 && equal)
4919 return isl_map_universe(space);
4921 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4923 for (i = 0; i + 1 < n; ++i)
4924 map = isl_map_add_basic_map(map,
4925 isl_basic_map_less_at(isl_space_copy(space), i));
4926 if (n > 0) {
4927 if (equal)
4928 map = isl_map_add_basic_map(map,
4929 isl_basic_map_less_or_equal_at(space, n - 1));
4930 else
4931 map = isl_map_add_basic_map(map,
4932 isl_basic_map_less_at(space, n - 1));
4933 } else
4934 isl_space_free(space);
4936 return map;
4939 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
4941 if (!space)
4942 return NULL;
4943 return map_lex_lte_first(space, space->n_out, equal);
4946 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4948 return map_lex_lte_first(dim, n, 0);
4951 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4953 return map_lex_lte_first(dim, n, 1);
4956 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4958 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4961 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4963 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4966 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
4967 unsigned n, int equal)
4969 struct isl_map *map;
4970 int i;
4972 if (n == 0 && equal)
4973 return isl_map_universe(space);
4975 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4977 for (i = 0; i + 1 < n; ++i)
4978 map = isl_map_add_basic_map(map,
4979 isl_basic_map_more_at(isl_space_copy(space), i));
4980 if (n > 0) {
4981 if (equal)
4982 map = isl_map_add_basic_map(map,
4983 isl_basic_map_more_or_equal_at(space, n - 1));
4984 else
4985 map = isl_map_add_basic_map(map,
4986 isl_basic_map_more_at(space, n - 1));
4987 } else
4988 isl_space_free(space);
4990 return map;
4993 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
4995 if (!space)
4996 return NULL;
4997 return map_lex_gte_first(space, space->n_out, equal);
5000 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5002 return map_lex_gte_first(dim, n, 0);
5005 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5007 return map_lex_gte_first(dim, n, 1);
5010 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5012 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5015 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5017 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5020 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5021 __isl_take isl_set *set2)
5023 isl_map *map;
5024 map = isl_map_lex_le(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_set_lex_lt_set(__isl_take isl_set *set1,
5031 __isl_take isl_set *set2)
5033 isl_map *map;
5034 map = isl_map_lex_lt(isl_set_get_space(set1));
5035 map = isl_map_intersect_domain(map, set1);
5036 map = isl_map_intersect_range(map, set2);
5037 return map;
5040 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5041 __isl_take isl_set *set2)
5043 isl_map *map;
5044 map = isl_map_lex_ge(isl_set_get_space(set1));
5045 map = isl_map_intersect_domain(map, set1);
5046 map = isl_map_intersect_range(map, set2);
5047 return map;
5050 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5051 __isl_take isl_set *set2)
5053 isl_map *map;
5054 map = isl_map_lex_gt(isl_set_get_space(set1));
5055 map = isl_map_intersect_domain(map, set1);
5056 map = isl_map_intersect_range(map, set2);
5057 return map;
5060 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5061 __isl_take isl_map *map2)
5063 isl_map *map;
5064 map = isl_map_lex_le(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 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5071 __isl_take isl_map *map2)
5073 isl_map *map;
5074 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5075 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5076 map = isl_map_apply_range(map, isl_map_reverse(map2));
5077 return map;
5080 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5081 __isl_take isl_map *map2)
5083 isl_map *map;
5084 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5085 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5086 map = isl_map_apply_range(map, isl_map_reverse(map2));
5087 return map;
5090 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5091 __isl_take isl_map *map2)
5093 isl_map *map;
5094 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5095 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5096 map = isl_map_apply_range(map, isl_map_reverse(map2));
5097 return map;
5100 /* For the div d = floor(f/m) at position "div", add the constraint
5102 * f - m d >= 0
5104 static __isl_give isl_basic_map *add_upper_div_constraint(
5105 __isl_take isl_basic_map *bmap, unsigned div)
5107 int i;
5108 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5109 unsigned n_div, pos;
5111 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5112 if (v_div < 0)
5113 return isl_basic_map_free(bmap);
5114 pos = v_div + div;
5115 i = isl_basic_map_alloc_inequality(bmap);
5116 if (i < 0)
5117 return isl_basic_map_free(bmap);
5118 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5119 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5121 return bmap;
5124 /* For the div d = floor(f/m) at position "div", add the constraint
5126 * -(f-(m-1)) + m d >= 0
5128 static __isl_give isl_basic_map *add_lower_div_constraint(
5129 __isl_take isl_basic_map *bmap, unsigned div)
5131 int i;
5132 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5133 unsigned n_div, pos;
5135 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5136 if (v_div < 0)
5137 return isl_basic_map_free(bmap);
5138 pos = v_div + div;
5139 i = isl_basic_map_alloc_inequality(bmap);
5140 if (i < 0)
5141 return isl_basic_map_free(bmap);
5142 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5143 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5144 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5145 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5147 return bmap;
5150 /* For the div d = floor(f/m) at position "pos", add the constraints
5152 * f - m d >= 0
5153 * -(f-(m-1)) + m d >= 0
5155 * Note that the second constraint is the negation of
5157 * f - m d >= m
5159 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5160 __isl_take isl_basic_map *bmap, unsigned pos)
5162 bmap = add_upper_div_constraint(bmap, pos);
5163 bmap = add_lower_div_constraint(bmap, pos);
5164 return bmap;
5167 /* For each known div d = floor(f/m), add the constraints
5169 * f - m d >= 0
5170 * -(f-(m-1)) + m d >= 0
5172 * Remove duplicate constraints in case of some these div constraints
5173 * already appear in "bmap".
5175 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5176 __isl_take isl_basic_map *bmap)
5178 unsigned n_div;
5180 if (!bmap)
5181 return NULL;
5182 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5183 if (n_div == 0)
5184 return bmap;
5186 bmap = add_known_div_constraints(bmap);
5187 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5188 bmap = isl_basic_map_finalize(bmap);
5189 return bmap;
5192 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5194 * In particular, if this div is of the form d = floor(f/m),
5195 * then add the constraint
5197 * f - m d >= 0
5199 * if sign < 0 or the constraint
5201 * -(f-(m-1)) + m d >= 0
5203 * if sign > 0.
5205 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5206 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5208 if (sign < 0)
5209 return add_upper_div_constraint(bmap, div);
5210 else
5211 return add_lower_div_constraint(bmap, div);
5214 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5215 __isl_take isl_basic_map *bmap)
5217 if (!bmap)
5218 goto error;
5219 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5220 bmap->n_div == 0 &&
5221 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5222 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5223 return bset_from_bmap(bmap);
5224 bmap = isl_basic_map_cow(bmap);
5225 if (!bmap)
5226 goto error;
5227 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5228 if (!bmap->dim)
5229 goto error;
5230 bmap->extra -= bmap->n_div;
5231 bmap->n_div = 0;
5232 bmap = isl_basic_map_finalize(bmap);
5233 return bset_from_bmap(bmap);
5234 error:
5235 isl_basic_map_free(bmap);
5236 return NULL;
5239 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5240 __isl_take isl_basic_set *bset)
5242 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5245 /* Replace each element in "list" by the result of applying
5246 * isl_basic_map_underlying_set to the element.
5248 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5249 __isl_take isl_basic_map_list *list)
5251 int i, n;
5253 if (!list)
5254 return NULL;
5256 n = isl_basic_map_list_n_basic_map(list);
5257 for (i = 0; i < n; ++i) {
5258 isl_basic_map *bmap;
5259 isl_basic_set *bset;
5261 bmap = isl_basic_map_list_get_basic_map(list, i);
5262 bset = isl_basic_set_underlying_set(bmap);
5263 list = isl_basic_set_list_set_basic_set(list, i, bset);
5266 return list;
5269 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5270 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5272 struct isl_basic_map *bmap;
5273 struct isl_ctx *ctx;
5274 unsigned dim, total;
5275 int i;
5277 if (!bset || !like)
5278 goto error;
5279 ctx = bset->ctx;
5280 if (isl_basic_set_check_no_params(bset) < 0 ||
5281 isl_basic_set_check_no_locals(bset) < 0)
5282 goto error;
5283 dim = isl_basic_set_dim(bset, isl_dim_set);
5284 isl_assert(ctx, dim == isl_basic_map_total_dim(like), goto error);
5285 if (like->n_div == 0) {
5286 isl_space *space = isl_basic_map_get_space(like);
5287 isl_basic_map_free(like);
5288 return isl_basic_map_reset_space(bset, space);
5290 bset = isl_basic_set_cow(bset);
5291 if (!bset)
5292 goto error;
5293 total = dim + bset->extra;
5294 bmap = bset_to_bmap(bset);
5295 isl_space_free(bmap->dim);
5296 bmap->dim = isl_space_copy(like->dim);
5297 if (!bmap->dim)
5298 goto error;
5299 bmap->n_div = like->n_div;
5300 bmap->extra += like->n_div;
5301 if (bmap->extra) {
5302 unsigned ltotal;
5303 isl_int **div;
5304 ltotal = total - bmap->extra + like->extra;
5305 if (ltotal > total)
5306 ltotal = total;
5307 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5308 bmap->extra * (1 + 1 + total));
5309 if (isl_blk_is_error(bmap->block2))
5310 goto error;
5311 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5312 if (!div)
5313 goto error;
5314 bmap->div = div;
5315 for (i = 0; i < bmap->extra; ++i)
5316 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5317 for (i = 0; i < like->n_div; ++i) {
5318 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5319 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5321 bmap = isl_basic_map_add_known_div_constraints(bmap);
5323 isl_basic_map_free(like);
5324 bmap = isl_basic_map_simplify(bmap);
5325 bmap = isl_basic_map_finalize(bmap);
5326 return bmap;
5327 error:
5328 isl_basic_map_free(like);
5329 isl_basic_set_free(bset);
5330 return NULL;
5333 struct isl_basic_set *isl_basic_set_from_underlying_set(
5334 struct isl_basic_set *bset, struct isl_basic_set *like)
5336 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5337 bset_to_bmap(like)));
5340 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5342 int i;
5344 map = isl_map_cow(map);
5345 if (!map)
5346 return NULL;
5347 map->dim = isl_space_cow(map->dim);
5348 if (!map->dim)
5349 goto error;
5351 for (i = 1; i < map->n; ++i)
5352 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5353 goto error);
5354 for (i = 0; i < map->n; ++i) {
5355 map->p[i] = bset_to_bmap(
5356 isl_basic_map_underlying_set(map->p[i]));
5357 if (!map->p[i])
5358 goto error;
5360 if (map->n == 0)
5361 map->dim = isl_space_underlying(map->dim, 0);
5362 else {
5363 isl_space_free(map->dim);
5364 map->dim = isl_space_copy(map->p[0]->dim);
5366 if (!map->dim)
5367 goto error;
5368 return set_from_map(map);
5369 error:
5370 isl_map_free(map);
5371 return NULL;
5374 /* Replace the space of "bmap" by "space".
5376 * If the space of "bmap" is identical to "space" (including the identifiers
5377 * of the input and output dimensions), then simply return the original input.
5379 __isl_give isl_basic_map *isl_basic_map_reset_space(
5380 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5382 isl_bool equal;
5383 isl_space *bmap_space;
5385 bmap_space = isl_basic_map_peek_space(bmap);
5386 equal = isl_space_is_equal(bmap_space, space);
5387 if (equal >= 0 && equal)
5388 equal = isl_space_has_equal_ids(bmap_space, space);
5389 if (equal < 0)
5390 goto error;
5391 if (equal) {
5392 isl_space_free(space);
5393 return bmap;
5395 bmap = isl_basic_map_cow(bmap);
5396 if (!bmap || !space)
5397 goto error;
5399 isl_space_free(bmap->dim);
5400 bmap->dim = space;
5402 bmap = isl_basic_map_finalize(bmap);
5404 return bmap;
5405 error:
5406 isl_basic_map_free(bmap);
5407 isl_space_free(space);
5408 return NULL;
5411 __isl_give isl_basic_set *isl_basic_set_reset_space(
5412 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5414 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5415 dim));
5418 /* Check that the total dimensions of "map" and "space" are the same.
5420 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5421 __isl_keep isl_space *space)
5423 unsigned dim1, dim2;
5425 if (!map || !space)
5426 return isl_stat_error;
5427 dim1 = isl_map_dim(map, isl_dim_all);
5428 dim2 = isl_space_dim(space, isl_dim_all);
5429 if (dim1 == dim2)
5430 return isl_stat_ok;
5431 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5432 "total dimensions do not match", return isl_stat_error);
5435 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5436 __isl_take isl_space *space)
5438 int i;
5440 map = isl_map_cow(map);
5441 if (!map || !space)
5442 goto error;
5444 for (i = 0; i < map->n; ++i) {
5445 map->p[i] = isl_basic_map_reset_space(map->p[i],
5446 isl_space_copy(space));
5447 if (!map->p[i])
5448 goto error;
5450 isl_space_free(map->dim);
5451 map->dim = space;
5453 return map;
5454 error:
5455 isl_map_free(map);
5456 isl_space_free(space);
5457 return NULL;
5460 /* Replace the space of "map" by "space", without modifying
5461 * the dimension of "map".
5463 * If the space of "map" is identical to "space" (including the identifiers
5464 * of the input and output dimensions), then simply return the original input.
5466 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5467 __isl_take isl_space *space)
5469 isl_bool equal;
5470 isl_space *map_space;
5472 map_space = isl_map_peek_space(map);
5473 equal = isl_space_is_equal(map_space, space);
5474 if (equal >= 0 && equal)
5475 equal = isl_space_has_equal_ids(map_space, space);
5476 if (equal < 0)
5477 goto error;
5478 if (equal) {
5479 isl_space_free(space);
5480 return map;
5482 if (check_map_space_equal_total_dim(map, space) < 0)
5483 goto error;
5484 return isl_map_reset_space(map, space);
5485 error:
5486 isl_map_free(map);
5487 isl_space_free(space);
5488 return NULL;
5491 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5492 __isl_take isl_space *dim)
5494 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5497 /* Compute the parameter domain of the given basic set.
5499 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5501 isl_bool is_params;
5502 isl_space *space;
5503 unsigned n;
5505 is_params = isl_basic_set_is_params(bset);
5506 if (is_params < 0)
5507 return isl_basic_set_free(bset);
5508 if (is_params)
5509 return bset;
5511 n = isl_basic_set_dim(bset, isl_dim_set);
5512 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5513 space = isl_basic_set_get_space(bset);
5514 space = isl_space_params(space);
5515 bset = isl_basic_set_reset_space(bset, space);
5516 return bset;
5519 /* Construct a zero-dimensional basic set with the given parameter domain.
5521 __isl_give isl_basic_set *isl_basic_set_from_params(
5522 __isl_take isl_basic_set *bset)
5524 isl_space *space;
5525 space = isl_basic_set_get_space(bset);
5526 space = isl_space_set_from_params(space);
5527 bset = isl_basic_set_reset_space(bset, space);
5528 return bset;
5531 /* Compute the parameter domain of the given set.
5533 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5535 return isl_map_params(set_to_map(set));
5538 /* Construct a zero-dimensional set with the given parameter domain.
5540 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5542 isl_space *space;
5543 space = isl_set_get_space(set);
5544 space = isl_space_set_from_params(space);
5545 set = isl_set_reset_space(set, space);
5546 return set;
5549 /* Compute the parameter domain of the given map.
5551 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5553 isl_space *space;
5554 unsigned n;
5556 n = isl_map_dim(map, isl_dim_in);
5557 map = isl_map_project_out(map, isl_dim_in, 0, n);
5558 n = isl_map_dim(map, isl_dim_out);
5559 map = isl_map_project_out(map, isl_dim_out, 0, n);
5560 space = isl_map_get_space(map);
5561 space = isl_space_params(space);
5562 map = isl_map_reset_space(map, space);
5563 return map;
5566 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5568 isl_space *space;
5569 unsigned n_out;
5571 if (!bmap)
5572 return NULL;
5573 space = isl_space_domain(isl_basic_map_get_space(bmap));
5575 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5576 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5578 return isl_basic_map_reset_space(bmap, space);
5581 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5583 if (!bmap)
5584 return isl_bool_error;
5585 return isl_space_may_be_set(bmap->dim);
5588 /* Is this basic map actually a set?
5589 * Users should never call this function. Outside of isl,
5590 * the type should indicate whether something is a set or a map.
5592 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5594 if (!bmap)
5595 return isl_bool_error;
5596 return isl_space_is_set(bmap->dim);
5599 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5601 isl_bool is_set;
5603 is_set = isl_basic_map_is_set(bmap);
5604 if (is_set < 0)
5605 goto error;
5606 if (is_set)
5607 return bmap;
5608 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5609 error:
5610 isl_basic_map_free(bmap);
5611 return NULL;
5614 __isl_give isl_basic_map *isl_basic_map_domain_map(
5615 __isl_take isl_basic_map *bmap)
5617 int i;
5618 isl_space *space;
5619 isl_basic_map *domain;
5620 int nparam, n_in, n_out;
5622 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5623 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5624 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5626 space = isl_basic_map_get_space(bmap);
5627 space = isl_space_from_range(isl_space_domain(space));
5628 domain = isl_basic_map_universe(space);
5630 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5631 bmap = isl_basic_map_apply_range(bmap, domain);
5632 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5634 for (i = 0; i < n_in; ++i)
5635 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5636 isl_dim_out, i);
5638 bmap = isl_basic_map_gauss(bmap, NULL);
5639 return isl_basic_map_finalize(bmap);
5642 __isl_give isl_basic_map *isl_basic_map_range_map(
5643 __isl_take isl_basic_map *bmap)
5645 int i;
5646 isl_space *space;
5647 isl_basic_map *range;
5648 int nparam, n_in, n_out;
5650 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5651 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5652 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5654 space = isl_basic_map_get_space(bmap);
5655 space = isl_space_from_range(isl_space_range(space));
5656 range = isl_basic_map_universe(space);
5658 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5659 bmap = isl_basic_map_apply_range(bmap, range);
5660 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5662 for (i = 0; i < n_out; ++i)
5663 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5664 isl_dim_out, i);
5666 bmap = isl_basic_map_gauss(bmap, NULL);
5667 return isl_basic_map_finalize(bmap);
5670 int isl_map_may_be_set(__isl_keep isl_map *map)
5672 if (!map)
5673 return -1;
5674 return isl_space_may_be_set(map->dim);
5677 /* Is this map actually a set?
5678 * Users should never call this function. Outside of isl,
5679 * the type should indicate whether something is a set or a map.
5681 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5683 if (!map)
5684 return isl_bool_error;
5685 return isl_space_is_set(map->dim);
5688 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5690 int i;
5691 isl_bool is_set;
5692 struct isl_set *set;
5694 is_set = isl_map_is_set(map);
5695 if (is_set < 0)
5696 goto error;
5697 if (is_set)
5698 return set_from_map(map);
5700 map = isl_map_cow(map);
5701 if (!map)
5702 goto error;
5704 set = set_from_map(map);
5705 set->dim = isl_space_range(set->dim);
5706 if (!set->dim)
5707 goto error;
5708 for (i = 0; i < map->n; ++i) {
5709 set->p[i] = isl_basic_map_range(map->p[i]);
5710 if (!set->p[i])
5711 goto error;
5713 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5714 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5715 return set;
5716 error:
5717 isl_map_free(map);
5718 return NULL;
5721 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5723 int i;
5725 map = isl_map_cow(map);
5726 if (!map)
5727 return NULL;
5729 map->dim = isl_space_domain_map(map->dim);
5730 if (!map->dim)
5731 goto error;
5732 for (i = 0; i < map->n; ++i) {
5733 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5734 if (!map->p[i])
5735 goto error;
5737 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5738 map = isl_map_unmark_normalized(map);
5739 return map;
5740 error:
5741 isl_map_free(map);
5742 return NULL;
5745 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5747 int i;
5748 isl_space *range_dim;
5750 map = isl_map_cow(map);
5751 if (!map)
5752 return NULL;
5754 range_dim = isl_space_range(isl_map_get_space(map));
5755 range_dim = isl_space_from_range(range_dim);
5756 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5757 map->dim = isl_space_join(map->dim, range_dim);
5758 if (!map->dim)
5759 goto error;
5760 for (i = 0; i < map->n; ++i) {
5761 map->p[i] = isl_basic_map_range_map(map->p[i]);
5762 if (!map->p[i])
5763 goto error;
5765 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5766 map = isl_map_unmark_normalized(map);
5767 return map;
5768 error:
5769 isl_map_free(map);
5770 return NULL;
5773 /* Given a wrapped map of the form A[B -> C],
5774 * return the map A[B -> C] -> B.
5776 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5778 isl_id *id;
5779 isl_map *map;
5781 if (!set)
5782 return NULL;
5783 if (!isl_set_has_tuple_id(set))
5784 return isl_map_domain_map(isl_set_unwrap(set));
5786 id = isl_set_get_tuple_id(set);
5787 map = isl_map_domain_map(isl_set_unwrap(set));
5788 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5790 return map;
5793 __isl_give isl_basic_map *isl_basic_map_from_domain(
5794 __isl_take isl_basic_set *bset)
5796 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5799 __isl_give isl_basic_map *isl_basic_map_from_range(
5800 __isl_take isl_basic_set *bset)
5802 isl_space *space;
5803 space = isl_basic_set_get_space(bset);
5804 space = isl_space_from_range(space);
5805 bset = isl_basic_set_reset_space(bset, space);
5806 return bset_to_bmap(bset);
5809 /* Create a relation with the given set as range.
5810 * The domain of the created relation is a zero-dimensional
5811 * flat anonymous space.
5813 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5815 isl_space *space;
5816 space = isl_set_get_space(set);
5817 space = isl_space_from_range(space);
5818 set = isl_set_reset_space(set, space);
5819 return set_to_map(set);
5822 /* Create a relation with the given set as domain.
5823 * The range of the created relation is a zero-dimensional
5824 * flat anonymous space.
5826 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5828 return isl_map_reverse(isl_map_from_range(set));
5831 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5832 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5834 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5837 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5838 __isl_take isl_set *range)
5840 return isl_map_apply_range(isl_map_reverse(domain), range);
5843 /* Return a newly allocated isl_map with given space and flags and
5844 * room for "n" basic maps.
5845 * Make sure that all cached information is cleared.
5847 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5848 unsigned flags)
5850 struct isl_map *map;
5852 if (!space)
5853 return NULL;
5854 if (n < 0)
5855 isl_die(space->ctx, isl_error_internal,
5856 "negative number of basic maps", goto error);
5857 map = isl_calloc(space->ctx, struct isl_map,
5858 sizeof(struct isl_map) +
5859 (n - 1) * sizeof(struct isl_basic_map *));
5860 if (!map)
5861 goto error;
5863 map->ctx = space->ctx;
5864 isl_ctx_ref(map->ctx);
5865 map->ref = 1;
5866 map->size = n;
5867 map->n = 0;
5868 map->dim = space;
5869 map->flags = flags;
5870 return map;
5871 error:
5872 isl_space_free(space);
5873 return NULL;
5876 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5878 struct isl_basic_map *bmap;
5879 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5880 bmap = isl_basic_map_set_to_empty(bmap);
5881 return bmap;
5884 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5886 struct isl_basic_set *bset;
5887 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5888 bset = isl_basic_set_set_to_empty(bset);
5889 return bset;
5892 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5894 struct isl_basic_map *bmap;
5895 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5896 bmap = isl_basic_map_finalize(bmap);
5897 return bmap;
5900 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
5902 struct isl_basic_set *bset;
5903 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5904 bset = isl_basic_set_finalize(bset);
5905 return bset;
5908 __isl_give isl_basic_map *isl_basic_map_nat_universe(
5909 __isl_take isl_space *space)
5911 int i;
5912 unsigned total = isl_space_dim(space, isl_dim_all);
5913 isl_basic_map *bmap;
5915 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
5916 for (i = 0; i < total; ++i) {
5917 int k = isl_basic_map_alloc_inequality(bmap);
5918 if (k < 0)
5919 goto error;
5920 isl_seq_clr(bmap->ineq[k], 1 + total);
5921 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5923 return bmap;
5924 error:
5925 isl_basic_map_free(bmap);
5926 return NULL;
5929 __isl_give isl_basic_set *isl_basic_set_nat_universe(
5930 __isl_take isl_space *space)
5932 return isl_basic_map_nat_universe(space);
5935 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5937 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5940 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5942 return isl_map_nat_universe(dim);
5945 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
5947 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
5950 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
5952 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
5955 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
5957 struct isl_map *map;
5958 if (!space)
5959 return NULL;
5960 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
5961 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
5962 return map;
5965 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
5967 struct isl_set *set;
5968 if (!space)
5969 return NULL;
5970 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
5971 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
5972 return set;
5975 struct isl_map *isl_map_dup(struct isl_map *map)
5977 int i;
5978 struct isl_map *dup;
5980 if (!map)
5981 return NULL;
5982 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5983 for (i = 0; i < map->n; ++i)
5984 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5985 return dup;
5988 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5989 __isl_take isl_basic_map *bmap)
5991 if (!bmap || !map)
5992 goto error;
5993 if (isl_basic_map_plain_is_empty(bmap)) {
5994 isl_basic_map_free(bmap);
5995 return map;
5997 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5998 isl_assert(map->ctx, map->n < map->size, goto error);
5999 map->p[map->n] = bmap;
6000 map->n++;
6001 map = isl_map_unmark_normalized(map);
6002 return map;
6003 error:
6004 if (map)
6005 isl_map_free(map);
6006 if (bmap)
6007 isl_basic_map_free(bmap);
6008 return NULL;
6011 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6013 int i;
6015 if (!map)
6016 return NULL;
6018 if (--map->ref > 0)
6019 return NULL;
6021 clear_caches(map);
6022 isl_ctx_deref(map->ctx);
6023 for (i = 0; i < map->n; ++i)
6024 isl_basic_map_free(map->p[i]);
6025 isl_space_free(map->dim);
6026 free(map);
6028 return NULL;
6031 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6032 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6034 int j;
6036 bmap = isl_basic_map_cow(bmap);
6037 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6038 j = isl_basic_map_alloc_equality(bmap);
6039 if (j < 0)
6040 goto error;
6041 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6042 isl_int_set_si(bmap->eq[j][pos], -1);
6043 isl_int_set_si(bmap->eq[j][0], value);
6044 bmap = isl_basic_map_simplify(bmap);
6045 return isl_basic_map_finalize(bmap);
6046 error:
6047 isl_basic_map_free(bmap);
6048 return NULL;
6051 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6052 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6054 int j;
6056 bmap = isl_basic_map_cow(bmap);
6057 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6058 j = isl_basic_map_alloc_equality(bmap);
6059 if (j < 0)
6060 goto error;
6061 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6062 isl_int_set_si(bmap->eq[j][pos], -1);
6063 isl_int_set(bmap->eq[j][0], value);
6064 bmap = isl_basic_map_simplify(bmap);
6065 return isl_basic_map_finalize(bmap);
6066 error:
6067 isl_basic_map_free(bmap);
6068 return NULL;
6071 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6072 enum isl_dim_type type, unsigned pos, int value)
6074 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6075 return isl_basic_map_free(bmap);
6076 return isl_basic_map_fix_pos_si(bmap,
6077 isl_basic_map_offset(bmap, type) + pos, value);
6080 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6081 enum isl_dim_type type, unsigned pos, isl_int value)
6083 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6084 return isl_basic_map_free(bmap);
6085 return isl_basic_map_fix_pos(bmap,
6086 isl_basic_map_offset(bmap, type) + pos, value);
6089 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6090 * to be equal to "v".
6092 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6093 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6095 if (!bmap || !v)
6096 goto error;
6097 if (!isl_val_is_int(v))
6098 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6099 "expecting integer value", goto error);
6100 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6101 goto error;
6102 pos += isl_basic_map_offset(bmap, type);
6103 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6104 isl_val_free(v);
6105 return bmap;
6106 error:
6107 isl_basic_map_free(bmap);
6108 isl_val_free(v);
6109 return NULL;
6112 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6113 * to be equal to "v".
6115 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6116 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6118 return isl_basic_map_fix_val(bset, type, pos, v);
6121 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6122 enum isl_dim_type type, unsigned pos, int value)
6124 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6125 type, pos, value));
6128 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6129 enum isl_dim_type type, unsigned pos, isl_int value)
6131 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6132 type, pos, value));
6135 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6136 unsigned input, int value)
6138 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6141 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6142 unsigned dim, int value)
6144 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6145 isl_dim_set, dim, value));
6148 /* Remove the basic map at position "i" from "map" if this basic map
6149 * is (obviously) empty.
6151 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6153 isl_bool empty;
6155 if (!map)
6156 return NULL;
6158 empty = isl_basic_map_plain_is_empty(map->p[i]);
6159 if (empty < 0)
6160 return isl_map_free(map);
6161 if (!empty)
6162 return map;
6164 isl_basic_map_free(map->p[i]);
6165 map->n--;
6166 if (i != map->n) {
6167 map->p[i] = map->p[map->n];
6168 map = isl_map_unmark_normalized(map);
6172 return map;
6175 /* Perform "fn" on each basic map of "map", where we may not be holding
6176 * the only reference to "map".
6177 * In particular, "fn" should be a semantics preserving operation
6178 * that we want to apply to all copies of "map". We therefore need
6179 * to be careful not to modify "map" in a way that breaks "map"
6180 * in case anything goes wrong.
6182 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6183 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6185 struct isl_basic_map *bmap;
6186 int i;
6188 if (!map)
6189 return NULL;
6191 for (i = map->n - 1; i >= 0; --i) {
6192 bmap = isl_basic_map_copy(map->p[i]);
6193 bmap = fn(bmap);
6194 if (!bmap)
6195 goto error;
6196 isl_basic_map_free(map->p[i]);
6197 map->p[i] = bmap;
6198 map = remove_if_empty(map, i);
6199 if (!map)
6200 return NULL;
6203 return map;
6204 error:
6205 isl_map_free(map);
6206 return NULL;
6209 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6210 enum isl_dim_type type, unsigned pos, int value)
6212 int i;
6214 map = isl_map_cow(map);
6215 if (isl_map_check_range(map, type, pos, 1) < 0)
6216 return isl_map_free(map);
6217 for (i = map->n - 1; i >= 0; --i) {
6218 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6219 map = remove_if_empty(map, i);
6220 if (!map)
6221 return NULL;
6223 map = isl_map_unmark_normalized(map);
6224 return map;
6227 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6228 enum isl_dim_type type, unsigned pos, int value)
6230 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6233 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6234 enum isl_dim_type type, unsigned pos, isl_int value)
6236 int i;
6238 map = isl_map_cow(map);
6239 if (isl_map_check_range(map, type, pos, 1) < 0)
6240 return isl_map_free(map);
6241 for (i = 0; i < map->n; ++i) {
6242 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6243 if (!map->p[i])
6244 goto error;
6246 map = isl_map_unmark_normalized(map);
6247 return map;
6248 error:
6249 isl_map_free(map);
6250 return NULL;
6253 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6254 enum isl_dim_type type, unsigned pos, isl_int value)
6256 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6259 /* Fix the value of the variable at position "pos" of type "type" of "map"
6260 * to be equal to "v".
6262 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6263 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6265 int i;
6267 map = isl_map_cow(map);
6268 if (!map || !v)
6269 goto error;
6271 if (!isl_val_is_int(v))
6272 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6273 "expecting integer value", goto error);
6274 if (isl_map_check_range(map, type, pos, 1) < 0)
6275 goto error;
6276 for (i = map->n - 1; i >= 0; --i) {
6277 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6278 isl_val_copy(v));
6279 map = remove_if_empty(map, i);
6280 if (!map)
6281 goto error;
6283 map = isl_map_unmark_normalized(map);
6284 isl_val_free(v);
6285 return map;
6286 error:
6287 isl_map_free(map);
6288 isl_val_free(v);
6289 return NULL;
6292 /* Fix the value of the variable at position "pos" of type "type" of "set"
6293 * to be equal to "v".
6295 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6296 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6298 return isl_map_fix_val(set, type, pos, v);
6301 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6302 unsigned input, int value)
6304 return isl_map_fix_si(map, isl_dim_in, input, value);
6307 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6309 return set_from_map(isl_map_fix_si(set_to_map(set),
6310 isl_dim_set, dim, value));
6313 static __isl_give isl_basic_map *basic_map_bound_si(
6314 __isl_take isl_basic_map *bmap,
6315 enum isl_dim_type type, unsigned pos, int value, int upper)
6317 int j;
6319 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6320 return isl_basic_map_free(bmap);
6321 pos += isl_basic_map_offset(bmap, type);
6322 bmap = isl_basic_map_cow(bmap);
6323 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6324 j = isl_basic_map_alloc_inequality(bmap);
6325 if (j < 0)
6326 goto error;
6327 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6328 if (upper) {
6329 isl_int_set_si(bmap->ineq[j][pos], -1);
6330 isl_int_set_si(bmap->ineq[j][0], value);
6331 } else {
6332 isl_int_set_si(bmap->ineq[j][pos], 1);
6333 isl_int_set_si(bmap->ineq[j][0], -value);
6335 bmap = isl_basic_map_simplify(bmap);
6336 return isl_basic_map_finalize(bmap);
6337 error:
6338 isl_basic_map_free(bmap);
6339 return NULL;
6342 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6343 __isl_take isl_basic_map *bmap,
6344 enum isl_dim_type type, unsigned pos, int value)
6346 return basic_map_bound_si(bmap, type, pos, value, 0);
6349 /* Constrain the values of the given dimension to be no greater than "value".
6351 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6352 __isl_take isl_basic_map *bmap,
6353 enum isl_dim_type type, unsigned pos, int value)
6355 return basic_map_bound_si(bmap, type, pos, value, 1);
6358 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6359 enum isl_dim_type type, unsigned pos, int value, int upper)
6361 int i;
6363 map = isl_map_cow(map);
6364 if (isl_map_check_range(map, type, pos, 1) < 0)
6365 return isl_map_free(map);
6366 for (i = 0; i < map->n; ++i) {
6367 map->p[i] = basic_map_bound_si(map->p[i],
6368 type, pos, value, upper);
6369 if (!map->p[i])
6370 goto error;
6372 map = isl_map_unmark_normalized(map);
6373 return map;
6374 error:
6375 isl_map_free(map);
6376 return NULL;
6379 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6380 enum isl_dim_type type, unsigned pos, int value)
6382 return map_bound_si(map, type, pos, value, 0);
6385 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6386 enum isl_dim_type type, unsigned pos, int value)
6388 return map_bound_si(map, type, pos, value, 1);
6391 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6392 enum isl_dim_type type, unsigned pos, int value)
6394 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6395 type, pos, value));
6398 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6399 enum isl_dim_type type, unsigned pos, int value)
6401 return isl_map_upper_bound_si(set, type, pos, value);
6404 /* Bound the given variable of "bmap" from below (or above is "upper"
6405 * is set) to "value".
6407 static __isl_give isl_basic_map *basic_map_bound(
6408 __isl_take isl_basic_map *bmap,
6409 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6411 int j;
6413 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6414 return isl_basic_map_free(bmap);
6415 pos += isl_basic_map_offset(bmap, type);
6416 bmap = isl_basic_map_cow(bmap);
6417 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6418 j = isl_basic_map_alloc_inequality(bmap);
6419 if (j < 0)
6420 goto error;
6421 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6422 if (upper) {
6423 isl_int_set_si(bmap->ineq[j][pos], -1);
6424 isl_int_set(bmap->ineq[j][0], value);
6425 } else {
6426 isl_int_set_si(bmap->ineq[j][pos], 1);
6427 isl_int_neg(bmap->ineq[j][0], value);
6429 bmap = isl_basic_map_simplify(bmap);
6430 return isl_basic_map_finalize(bmap);
6431 error:
6432 isl_basic_map_free(bmap);
6433 return NULL;
6436 /* Bound the given variable of "map" from below (or above is "upper"
6437 * is set) to "value".
6439 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6440 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6442 int i;
6444 map = isl_map_cow(map);
6445 if (isl_map_check_range(map, type, pos, 1) < 0)
6446 return isl_map_free(map);
6447 for (i = map->n - 1; i >= 0; --i) {
6448 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6449 map = remove_if_empty(map, i);
6450 if (!map)
6451 return NULL;
6453 map = isl_map_unmark_normalized(map);
6454 return map;
6457 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6458 enum isl_dim_type type, unsigned pos, isl_int value)
6460 return map_bound(map, type, pos, value, 0);
6463 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6464 enum isl_dim_type type, unsigned pos, isl_int value)
6466 return map_bound(map, type, pos, value, 1);
6469 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6470 enum isl_dim_type type, unsigned pos, isl_int value)
6472 return isl_map_lower_bound(set, type, pos, value);
6475 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6476 enum isl_dim_type type, unsigned pos, isl_int value)
6478 return isl_map_upper_bound(set, type, pos, value);
6481 /* Force the values of the variable at position "pos" of type "type" of "set"
6482 * to be no smaller than "value".
6484 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6485 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6487 if (!value)
6488 goto error;
6489 if (!isl_val_is_int(value))
6490 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6491 "expecting integer value", goto error);
6492 set = isl_set_lower_bound(set, type, pos, value->n);
6493 isl_val_free(value);
6494 return set;
6495 error:
6496 isl_val_free(value);
6497 isl_set_free(set);
6498 return NULL;
6501 /* Force the values of the variable at position "pos" of type "type" of "set"
6502 * to be no greater than "value".
6504 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6505 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6507 if (!value)
6508 goto error;
6509 if (!isl_val_is_int(value))
6510 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6511 "expecting integer value", goto error);
6512 set = isl_set_upper_bound(set, type, pos, value->n);
6513 isl_val_free(value);
6514 return set;
6515 error:
6516 isl_val_free(value);
6517 isl_set_free(set);
6518 return NULL;
6521 /* Bound the given variable of "bset" from below (or above is "upper"
6522 * is set) to "value".
6524 static __isl_give isl_basic_set *isl_basic_set_bound(
6525 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6526 isl_int value, int upper)
6528 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6529 type, pos, value, upper));
6532 /* Bound the given variable of "bset" from below (or above is "upper"
6533 * is set) to "value".
6535 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6536 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6537 __isl_take isl_val *value, int upper)
6539 if (!value)
6540 goto error;
6541 if (!isl_val_is_int(value))
6542 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6543 "expecting integer value", goto error);
6544 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6545 isl_val_free(value);
6546 return bset;
6547 error:
6548 isl_val_free(value);
6549 isl_basic_set_free(bset);
6550 return NULL;
6553 /* Bound the given variable of "bset" from below to "value".
6555 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6556 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6557 __isl_take isl_val *value)
6559 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6562 /* Bound the given variable of "bset" from above to "value".
6564 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6565 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6566 __isl_take isl_val *value)
6568 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6571 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6573 int i;
6575 map = isl_map_cow(map);
6576 if (!map)
6577 return NULL;
6579 map->dim = isl_space_reverse(map->dim);
6580 if (!map->dim)
6581 goto error;
6582 for (i = 0; i < map->n; ++i) {
6583 map->p[i] = isl_basic_map_reverse(map->p[i]);
6584 if (!map->p[i])
6585 goto error;
6587 map = isl_map_unmark_normalized(map);
6588 return map;
6589 error:
6590 isl_map_free(map);
6591 return NULL;
6594 #undef TYPE
6595 #define TYPE isl_pw_multi_aff
6596 #undef SUFFIX
6597 #define SUFFIX _pw_multi_aff
6598 #undef EMPTY
6599 #define EMPTY isl_pw_multi_aff_empty
6600 #undef ADD
6601 #define ADD isl_pw_multi_aff_union_add
6602 #include "isl_map_lexopt_templ.c"
6604 /* Given a map "map", compute the lexicographically minimal
6605 * (or maximal) image element for each domain element in dom,
6606 * in the form of an isl_pw_multi_aff.
6607 * If "empty" is not NULL, then set *empty to those elements in dom that
6608 * do not have an image element.
6609 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6610 * should be computed over the domain of "map". "empty" is also NULL
6611 * in this case.
6613 * We first compute the lexicographically minimal or maximal element
6614 * in the first basic map. This results in a partial solution "res"
6615 * and a subset "todo" of dom that still need to be handled.
6616 * We then consider each of the remaining maps in "map" and successively
6617 * update both "res" and "todo".
6618 * If "empty" is NULL, then the todo sets are not needed and therefore
6619 * also not computed.
6621 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6622 __isl_take isl_map *map, __isl_take isl_set *dom,
6623 __isl_give isl_set **empty, unsigned flags)
6625 int i;
6626 int full;
6627 isl_pw_multi_aff *res;
6628 isl_set *todo;
6630 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6631 if (!map || (!full && !dom))
6632 goto error;
6634 if (isl_map_plain_is_empty(map)) {
6635 if (empty)
6636 *empty = dom;
6637 else
6638 isl_set_free(dom);
6639 return isl_pw_multi_aff_from_map(map);
6642 res = basic_map_partial_lexopt_pw_multi_aff(
6643 isl_basic_map_copy(map->p[0]),
6644 isl_set_copy(dom), empty, flags);
6646 if (empty)
6647 todo = *empty;
6648 for (i = 1; i < map->n; ++i) {
6649 isl_pw_multi_aff *res_i;
6651 res_i = basic_map_partial_lexopt_pw_multi_aff(
6652 isl_basic_map_copy(map->p[i]),
6653 isl_set_copy(dom), empty, flags);
6655 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6656 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6657 else
6658 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6660 if (empty)
6661 todo = isl_set_intersect(todo, *empty);
6664 isl_set_free(dom);
6665 isl_map_free(map);
6667 if (empty)
6668 *empty = todo;
6670 return res;
6671 error:
6672 if (empty)
6673 *empty = NULL;
6674 isl_set_free(dom);
6675 isl_map_free(map);
6676 return NULL;
6679 #undef TYPE
6680 #define TYPE isl_map
6681 #undef SUFFIX
6682 #define SUFFIX
6683 #undef EMPTY
6684 #define EMPTY isl_map_empty
6685 #undef ADD
6686 #define ADD isl_map_union_disjoint
6687 #include "isl_map_lexopt_templ.c"
6689 /* Given a map "map", compute the lexicographically minimal
6690 * (or maximal) image element for each domain element in "dom",
6691 * in the form of an isl_map.
6692 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6693 * do not have an image element.
6694 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6695 * should be computed over the domain of "map". "empty" is also NULL
6696 * in this case.
6698 * If the input consists of more than one disjunct, then first
6699 * compute the desired result in the form of an isl_pw_multi_aff and
6700 * then convert that into an isl_map.
6702 * This function used to have an explicit implementation in terms
6703 * of isl_maps, but it would continually intersect the domains of
6704 * partial results with the complement of the domain of the next
6705 * partial solution, potentially leading to an explosion in the number
6706 * of disjuncts if there are several disjuncts in the input.
6707 * An even earlier implementation of this function would look for
6708 * better results in the domain of the partial result and for extra
6709 * results in the complement of this domain, which would lead to
6710 * even more splintering.
6712 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6713 __isl_take isl_map *map, __isl_take isl_set *dom,
6714 __isl_give isl_set **empty, unsigned flags)
6716 int full;
6717 struct isl_map *res;
6718 isl_pw_multi_aff *pma;
6720 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6721 if (!map || (!full && !dom))
6722 goto error;
6724 if (isl_map_plain_is_empty(map)) {
6725 if (empty)
6726 *empty = dom;
6727 else
6728 isl_set_free(dom);
6729 return map;
6732 if (map->n == 1) {
6733 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6734 dom, empty, flags);
6735 isl_map_free(map);
6736 return res;
6739 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6740 flags);
6741 return isl_map_from_pw_multi_aff(pma);
6742 error:
6743 if (empty)
6744 *empty = NULL;
6745 isl_set_free(dom);
6746 isl_map_free(map);
6747 return NULL;
6750 __isl_give isl_map *isl_map_partial_lexmax(
6751 __isl_take isl_map *map, __isl_take isl_set *dom,
6752 __isl_give isl_set **empty)
6754 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6757 __isl_give isl_map *isl_map_partial_lexmin(
6758 __isl_take isl_map *map, __isl_take isl_set *dom,
6759 __isl_give isl_set **empty)
6761 return isl_map_partial_lexopt(map, dom, empty, 0);
6764 __isl_give isl_set *isl_set_partial_lexmin(
6765 __isl_take isl_set *set, __isl_take isl_set *dom,
6766 __isl_give isl_set **empty)
6768 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6769 dom, empty));
6772 __isl_give isl_set *isl_set_partial_lexmax(
6773 __isl_take isl_set *set, __isl_take isl_set *dom,
6774 __isl_give isl_set **empty)
6776 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6777 dom, empty));
6780 /* Compute the lexicographic minimum (or maximum if "flags" includes
6781 * ISL_OPT_MAX) of "bset" over its parametric domain.
6783 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6784 unsigned flags)
6786 return isl_basic_map_lexopt(bset, flags);
6789 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6791 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6794 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6796 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6799 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6801 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6804 /* Compute the lexicographic minimum of "bset" over its parametric domain
6805 * for the purpose of quantifier elimination.
6806 * That is, find an explicit representation for all the existentially
6807 * quantified variables in "bset" by computing their lexicographic
6808 * minimum.
6810 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6811 __isl_take isl_basic_set *bset)
6813 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6816 /* Given a basic map with one output dimension, compute the minimum or
6817 * maximum of that dimension as an isl_pw_aff.
6819 * Compute the optimum as a lexicographic optimum over the single
6820 * output dimension and extract the single isl_pw_aff from the result.
6822 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6823 int max)
6825 isl_pw_multi_aff *pma;
6826 isl_pw_aff *pwaff;
6828 bmap = isl_basic_map_copy(bmap);
6829 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6830 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6831 isl_pw_multi_aff_free(pma);
6833 return pwaff;
6836 /* Compute the minimum or maximum of the given output dimension
6837 * as a function of the parameters and the input dimensions,
6838 * but independently of the other output dimensions.
6840 * We first project out the other output dimension and then compute
6841 * the "lexicographic" maximum in each basic map, combining the results
6842 * using isl_pw_aff_union_max.
6844 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6845 int max)
6847 int i;
6848 isl_pw_aff *pwaff;
6849 unsigned n_out;
6851 n_out = isl_map_dim(map, isl_dim_out);
6852 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6853 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6854 if (!map)
6855 return NULL;
6857 if (map->n == 0) {
6858 isl_space *space = isl_map_get_space(map);
6859 isl_map_free(map);
6860 return isl_pw_aff_empty(space);
6863 pwaff = basic_map_dim_opt(map->p[0], max);
6864 for (i = 1; i < map->n; ++i) {
6865 isl_pw_aff *pwaff_i;
6867 pwaff_i = basic_map_dim_opt(map->p[i], max);
6868 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6871 isl_map_free(map);
6873 return pwaff;
6876 /* Compute the minimum of the given output dimension as a function of the
6877 * parameters and input dimensions, but independently of
6878 * the other output dimensions.
6880 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6882 return map_dim_opt(map, pos, 0);
6885 /* Compute the maximum of the given output dimension as a function of the
6886 * parameters and input dimensions, but independently of
6887 * the other output dimensions.
6889 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6891 return map_dim_opt(map, pos, 1);
6894 /* Compute the minimum or maximum of the given set dimension
6895 * as a function of the parameters,
6896 * but independently of the other set dimensions.
6898 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6899 int max)
6901 return map_dim_opt(set, pos, max);
6904 /* Compute the maximum of the given set dimension as a function of the
6905 * parameters, but independently of the other set dimensions.
6907 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6909 return set_dim_opt(set, pos, 1);
6912 /* Compute the minimum of the given set dimension as a function of the
6913 * parameters, but independently of the other set dimensions.
6915 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6917 return set_dim_opt(set, pos, 0);
6920 /* Apply a preimage specified by "mat" on the parameters of "bset".
6921 * bset is assumed to have only parameters and divs.
6923 static __isl_give isl_basic_set *basic_set_parameter_preimage(
6924 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
6926 unsigned nparam;
6928 if (!bset || !mat)
6929 goto error;
6931 bset->dim = isl_space_cow(bset->dim);
6932 if (!bset->dim)
6933 goto error;
6935 nparam = isl_basic_set_dim(bset, isl_dim_param);
6937 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6939 bset->dim->nparam = 0;
6940 bset->dim->n_out = nparam;
6941 bset = isl_basic_set_preimage(bset, mat);
6942 if (bset) {
6943 bset->dim->nparam = bset->dim->n_out;
6944 bset->dim->n_out = 0;
6946 return bset;
6947 error:
6948 isl_mat_free(mat);
6949 isl_basic_set_free(bset);
6950 return NULL;
6953 /* Apply a preimage specified by "mat" on the parameters of "set".
6954 * set is assumed to have only parameters and divs.
6956 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6957 __isl_take isl_mat *mat)
6959 isl_space *space;
6960 unsigned nparam;
6962 if (!set || !mat)
6963 goto error;
6965 nparam = isl_set_dim(set, isl_dim_param);
6967 if (mat->n_row != 1 + nparam)
6968 isl_die(isl_set_get_ctx(set), isl_error_internal,
6969 "unexpected number of rows", goto error);
6971 space = isl_set_get_space(set);
6972 space = isl_space_move_dims(space, isl_dim_set, 0,
6973 isl_dim_param, 0, nparam);
6974 set = isl_set_reset_space(set, space);
6975 set = isl_set_preimage(set, mat);
6976 nparam = isl_set_dim(set, isl_dim_out);
6977 space = isl_set_get_space(set);
6978 space = isl_space_move_dims(space, isl_dim_param, 0,
6979 isl_dim_out, 0, nparam);
6980 set = isl_set_reset_space(set, space);
6981 return set;
6982 error:
6983 isl_mat_free(mat);
6984 isl_set_free(set);
6985 return NULL;
6988 /* Intersect the basic set "bset" with the affine space specified by the
6989 * equalities in "eq".
6991 static __isl_give isl_basic_set *basic_set_append_equalities(
6992 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
6994 int i, k;
6995 unsigned len;
6997 if (!bset || !eq)
6998 goto error;
7000 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7001 eq->n_row, 0);
7002 if (!bset)
7003 goto error;
7005 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7006 for (i = 0; i < eq->n_row; ++i) {
7007 k = isl_basic_set_alloc_equality(bset);
7008 if (k < 0)
7009 goto error;
7010 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7011 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7013 isl_mat_free(eq);
7015 bset = isl_basic_set_gauss(bset, NULL);
7016 bset = isl_basic_set_finalize(bset);
7018 return bset;
7019 error:
7020 isl_mat_free(eq);
7021 isl_basic_set_free(bset);
7022 return NULL;
7025 /* Intersect the set "set" with the affine space specified by the
7026 * equalities in "eq".
7028 static struct isl_set *set_append_equalities(struct isl_set *set,
7029 struct isl_mat *eq)
7031 int i;
7033 if (!set || !eq)
7034 goto error;
7036 for (i = 0; i < set->n; ++i) {
7037 set->p[i] = basic_set_append_equalities(set->p[i],
7038 isl_mat_copy(eq));
7039 if (!set->p[i])
7040 goto error;
7042 isl_mat_free(eq);
7043 return set;
7044 error:
7045 isl_mat_free(eq);
7046 isl_set_free(set);
7047 return NULL;
7050 /* Given a basic set "bset" that only involves parameters and existentially
7051 * quantified variables, return the index of the first equality
7052 * that only involves parameters. If there is no such equality then
7053 * return bset->n_eq.
7055 * This function assumes that isl_basic_set_gauss has been called on "bset".
7057 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7059 int i, j;
7060 unsigned nparam, n_div;
7062 if (!bset)
7063 return -1;
7065 nparam = isl_basic_set_dim(bset, isl_dim_param);
7066 n_div = isl_basic_set_dim(bset, isl_dim_div);
7068 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7069 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7070 ++i;
7073 return i;
7076 /* Compute an explicit representation for the existentially quantified
7077 * variables in "bset" by computing the "minimal value" of the set
7078 * variables. Since there are no set variables, the computation of
7079 * the minimal value essentially computes an explicit representation
7080 * of the non-empty part(s) of "bset".
7082 * The input only involves parameters and existentially quantified variables.
7083 * All equalities among parameters have been removed.
7085 * Since the existentially quantified variables in the result are in general
7086 * going to be different from those in the input, we first replace
7087 * them by the minimal number of variables based on their equalities.
7088 * This should simplify the parametric integer programming.
7090 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7092 isl_morph *morph1, *morph2;
7093 isl_set *set;
7094 unsigned n;
7096 if (!bset)
7097 return NULL;
7098 if (bset->n_eq == 0)
7099 return isl_basic_set_lexmin_compute_divs(bset);
7101 morph1 = isl_basic_set_parameter_compression(bset);
7102 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7103 bset = isl_basic_set_lift(bset);
7104 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7105 bset = isl_morph_basic_set(morph2, bset);
7106 n = isl_basic_set_dim(bset, isl_dim_set);
7107 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7109 set = isl_basic_set_lexmin_compute_divs(bset);
7111 set = isl_morph_set(isl_morph_inverse(morph1), set);
7113 return set;
7116 /* Project the given basic set onto its parameter domain, possibly introducing
7117 * new, explicit, existential variables in the constraints.
7118 * The input has parameters and (possibly implicit) existential variables.
7119 * The output has the same parameters, but only
7120 * explicit existentially quantified variables.
7122 * The actual projection is performed by pip, but pip doesn't seem
7123 * to like equalities very much, so we first remove the equalities
7124 * among the parameters by performing a variable compression on
7125 * the parameters. Afterward, an inverse transformation is performed
7126 * and the equalities among the parameters are inserted back in.
7128 * The variable compression on the parameters may uncover additional
7129 * equalities that were only implicit before. We therefore check
7130 * if there are any new parameter equalities in the result and
7131 * if so recurse. The removal of parameter equalities is required
7132 * for the parameter compression performed by base_compute_divs.
7134 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7136 int i;
7137 struct isl_mat *eq;
7138 struct isl_mat *T, *T2;
7139 struct isl_set *set;
7140 unsigned nparam;
7142 bset = isl_basic_set_cow(bset);
7143 if (!bset)
7144 return NULL;
7146 if (bset->n_eq == 0)
7147 return base_compute_divs(bset);
7149 bset = isl_basic_set_gauss(bset, NULL);
7150 if (!bset)
7151 return NULL;
7152 if (isl_basic_set_plain_is_empty(bset))
7153 return isl_set_from_basic_set(bset);
7155 i = first_parameter_equality(bset);
7156 if (i == bset->n_eq)
7157 return base_compute_divs(bset);
7159 nparam = isl_basic_set_dim(bset, isl_dim_param);
7160 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7161 0, 1 + nparam);
7162 eq = isl_mat_cow(eq);
7163 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7164 if (T && T->n_col == 0) {
7165 isl_mat_free(T);
7166 isl_mat_free(T2);
7167 isl_mat_free(eq);
7168 bset = isl_basic_set_set_to_empty(bset);
7169 return isl_set_from_basic_set(bset);
7171 bset = basic_set_parameter_preimage(bset, T);
7173 i = first_parameter_equality(bset);
7174 if (!bset)
7175 set = NULL;
7176 else if (i == bset->n_eq)
7177 set = base_compute_divs(bset);
7178 else
7179 set = parameter_compute_divs(bset);
7180 set = set_parameter_preimage(set, T2);
7181 set = set_append_equalities(set, eq);
7182 return set;
7185 /* Insert the divs from "ls" before those of "bmap".
7187 * The number of columns is not changed, which means that the last
7188 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7189 * The caller is responsible for removing the same number of dimensions
7190 * from the space of "bmap".
7192 static __isl_give isl_basic_map *insert_divs_from_local_space(
7193 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7195 int i;
7196 int n_div;
7197 int old_n_div;
7199 n_div = isl_local_space_dim(ls, isl_dim_div);
7200 if (n_div == 0)
7201 return bmap;
7203 old_n_div = bmap->n_div;
7204 bmap = insert_div_rows(bmap, n_div);
7205 if (!bmap)
7206 return NULL;
7208 for (i = 0; i < n_div; ++i) {
7209 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7210 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7213 return bmap;
7216 /* Replace the space of "bmap" by the space and divs of "ls".
7218 * If "ls" has any divs, then we simplify the result since we may
7219 * have discovered some additional equalities that could simplify
7220 * the div expressions.
7222 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7223 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7225 int n_div;
7227 bmap = isl_basic_map_cow(bmap);
7228 if (!bmap || !ls)
7229 goto error;
7231 n_div = isl_local_space_dim(ls, isl_dim_div);
7232 bmap = insert_divs_from_local_space(bmap, ls);
7233 if (!bmap)
7234 goto error;
7236 isl_space_free(bmap->dim);
7237 bmap->dim = isl_local_space_get_space(ls);
7238 if (!bmap->dim)
7239 goto error;
7241 isl_local_space_free(ls);
7242 if (n_div > 0)
7243 bmap = isl_basic_map_simplify(bmap);
7244 bmap = isl_basic_map_finalize(bmap);
7245 return bmap;
7246 error:
7247 isl_basic_map_free(bmap);
7248 isl_local_space_free(ls);
7249 return NULL;
7252 /* Replace the space of "map" by the space and divs of "ls".
7254 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7255 __isl_take isl_local_space *ls)
7257 int i;
7259 map = isl_map_cow(map);
7260 if (!map || !ls)
7261 goto error;
7263 for (i = 0; i < map->n; ++i) {
7264 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7265 isl_local_space_copy(ls));
7266 if (!map->p[i])
7267 goto error;
7269 isl_space_free(map->dim);
7270 map->dim = isl_local_space_get_space(ls);
7271 if (!map->dim)
7272 goto error;
7274 isl_local_space_free(ls);
7275 return map;
7276 error:
7277 isl_local_space_free(ls);
7278 isl_map_free(map);
7279 return NULL;
7282 /* Compute an explicit representation for the existentially
7283 * quantified variables for which do not know any explicit representation yet.
7285 * We first sort the existentially quantified variables so that the
7286 * existentially quantified variables for which we already have an explicit
7287 * representation are placed before those for which we do not.
7288 * The input dimensions, the output dimensions and the existentially
7289 * quantified variables for which we already have an explicit
7290 * representation are then turned into parameters.
7291 * compute_divs returns a map with the same parameters and
7292 * no input or output dimensions and the dimension specification
7293 * is reset to that of the input, including the existentially quantified
7294 * variables for which we already had an explicit representation.
7296 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7298 struct isl_basic_set *bset;
7299 struct isl_set *set;
7300 struct isl_map *map;
7301 isl_space *space;
7302 isl_local_space *ls;
7303 unsigned nparam;
7304 unsigned n_in;
7305 unsigned n_out;
7306 int n_known;
7307 int i;
7309 bmap = isl_basic_map_sort_divs(bmap);
7310 bmap = isl_basic_map_cow(bmap);
7311 if (!bmap)
7312 return NULL;
7314 n_known = isl_basic_map_first_unknown_div(bmap);
7315 if (n_known < 0)
7316 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7318 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7319 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7320 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7321 space = isl_space_set_alloc(bmap->ctx,
7322 nparam + n_in + n_out + n_known, 0);
7323 if (!space)
7324 goto error;
7326 ls = isl_basic_map_get_local_space(bmap);
7327 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7328 n_known, bmap->n_div - n_known);
7329 if (n_known > 0) {
7330 for (i = n_known; i < bmap->n_div; ++i)
7331 swap_div(bmap, i - n_known, i);
7332 bmap->n_div -= n_known;
7333 bmap->extra -= n_known;
7335 bmap = isl_basic_map_reset_space(bmap, space);
7336 bset = bset_from_bmap(bmap);
7338 set = parameter_compute_divs(bset);
7339 map = set_to_map(set);
7340 map = replace_space_by_local_space(map, ls);
7342 return map;
7343 error:
7344 isl_basic_map_free(bmap);
7345 return NULL;
7348 /* Remove the explicit representation of local variable "div",
7349 * if there is any.
7351 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7352 __isl_take isl_basic_map *bmap, int div)
7354 isl_bool unknown;
7356 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7357 if (unknown < 0)
7358 return isl_basic_map_free(bmap);
7359 if (unknown)
7360 return bmap;
7362 bmap = isl_basic_map_cow(bmap);
7363 if (!bmap)
7364 return NULL;
7365 isl_int_set_si(bmap->div[div][0], 0);
7366 return bmap;
7369 /* Is local variable "div" of "bmap" marked as not having an explicit
7370 * representation?
7371 * Note that even if "div" is not marked in this way and therefore
7372 * has an explicit representation, this representation may still
7373 * depend (indirectly) on other local variables that do not
7374 * have an explicit representation.
7376 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7377 int div)
7379 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7380 return isl_bool_error;
7381 return isl_int_is_zero(bmap->div[div][0]);
7384 /* Return the position of the first local variable that does not
7385 * have an explicit representation.
7386 * Return the total number of local variables if they all have
7387 * an explicit representation.
7388 * Return -1 on error.
7390 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7392 int i;
7394 if (!bmap)
7395 return -1;
7397 for (i = 0; i < bmap->n_div; ++i) {
7398 if (!isl_basic_map_div_is_known(bmap, i))
7399 return i;
7401 return bmap->n_div;
7404 /* Return the position of the first local variable that does not
7405 * have an explicit representation.
7406 * Return the total number of local variables if they all have
7407 * an explicit representation.
7408 * Return -1 on error.
7410 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7412 return isl_basic_map_first_unknown_div(bset);
7415 /* Does "bmap" have an explicit representation for all local variables?
7417 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7419 int first, n;
7421 n = isl_basic_map_dim(bmap, isl_dim_div);
7422 first = isl_basic_map_first_unknown_div(bmap);
7423 if (first < 0)
7424 return isl_bool_error;
7425 return first == n;
7428 /* Do all basic maps in "map" have an explicit representation
7429 * for all local variables?
7431 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7433 int i;
7435 if (!map)
7436 return isl_bool_error;
7438 for (i = 0; i < map->n; ++i) {
7439 int known = isl_basic_map_divs_known(map->p[i]);
7440 if (known <= 0)
7441 return known;
7444 return isl_bool_true;
7447 /* If bmap contains any unknown divs, then compute explicit
7448 * expressions for them. However, this computation may be
7449 * quite expensive, so first try to remove divs that aren't
7450 * strictly needed.
7452 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7454 int known;
7455 struct isl_map *map;
7457 known = isl_basic_map_divs_known(bmap);
7458 if (known < 0)
7459 goto error;
7460 if (known)
7461 return isl_map_from_basic_map(bmap);
7463 bmap = isl_basic_map_drop_redundant_divs(bmap);
7465 known = isl_basic_map_divs_known(bmap);
7466 if (known < 0)
7467 goto error;
7468 if (known)
7469 return isl_map_from_basic_map(bmap);
7471 map = compute_divs(bmap);
7472 return map;
7473 error:
7474 isl_basic_map_free(bmap);
7475 return NULL;
7478 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7480 int i;
7481 int known;
7482 struct isl_map *res;
7484 if (!map)
7485 return NULL;
7486 if (map->n == 0)
7487 return map;
7489 known = isl_map_divs_known(map);
7490 if (known < 0) {
7491 isl_map_free(map);
7492 return NULL;
7494 if (known)
7495 return map;
7497 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7498 for (i = 1 ; i < map->n; ++i) {
7499 struct isl_map *r2;
7500 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7501 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7502 res = isl_map_union_disjoint(res, r2);
7503 else
7504 res = isl_map_union(res, r2);
7506 isl_map_free(map);
7508 return res;
7511 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7513 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7516 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7518 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7521 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7523 int i;
7524 struct isl_set *set;
7526 if (!map)
7527 goto error;
7529 map = isl_map_cow(map);
7530 if (!map)
7531 return NULL;
7533 set = set_from_map(map);
7534 set->dim = isl_space_domain(set->dim);
7535 if (!set->dim)
7536 goto error;
7537 for (i = 0; i < map->n; ++i) {
7538 set->p[i] = isl_basic_map_domain(map->p[i]);
7539 if (!set->p[i])
7540 goto error;
7542 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7543 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7544 return set;
7545 error:
7546 isl_map_free(map);
7547 return NULL;
7550 /* Return the union of "map1" and "map2", where we assume for now that
7551 * "map1" and "map2" are disjoint. Note that the basic maps inside
7552 * "map1" or "map2" may not be disjoint from each other.
7553 * Also note that this function is also called from isl_map_union,
7554 * which takes care of handling the situation where "map1" and "map2"
7555 * may not be disjoint.
7557 * If one of the inputs is empty, we can simply return the other input.
7558 * Similarly, if one of the inputs is universal, then it is equal to the union.
7560 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7561 __isl_take isl_map *map2)
7563 int i;
7564 unsigned flags = 0;
7565 struct isl_map *map = NULL;
7566 int is_universe;
7568 if (!map1 || !map2)
7569 goto error;
7571 if (!isl_space_is_equal(map1->dim, map2->dim))
7572 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7573 "spaces don't match", goto error);
7575 if (map1->n == 0) {
7576 isl_map_free(map1);
7577 return map2;
7579 if (map2->n == 0) {
7580 isl_map_free(map2);
7581 return map1;
7584 is_universe = isl_map_plain_is_universe(map1);
7585 if (is_universe < 0)
7586 goto error;
7587 if (is_universe) {
7588 isl_map_free(map2);
7589 return map1;
7592 is_universe = isl_map_plain_is_universe(map2);
7593 if (is_universe < 0)
7594 goto error;
7595 if (is_universe) {
7596 isl_map_free(map1);
7597 return map2;
7600 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7601 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7602 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7604 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7605 map1->n + map2->n, flags);
7606 if (!map)
7607 goto error;
7608 for (i = 0; i < map1->n; ++i) {
7609 map = isl_map_add_basic_map(map,
7610 isl_basic_map_copy(map1->p[i]));
7611 if (!map)
7612 goto error;
7614 for (i = 0; i < map2->n; ++i) {
7615 map = isl_map_add_basic_map(map,
7616 isl_basic_map_copy(map2->p[i]));
7617 if (!map)
7618 goto error;
7620 isl_map_free(map1);
7621 isl_map_free(map2);
7622 return map;
7623 error:
7624 isl_map_free(map);
7625 isl_map_free(map1);
7626 isl_map_free(map2);
7627 return NULL;
7630 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7631 * guaranteed to be disjoint by the caller.
7633 * Note that this functions is called from within isl_map_make_disjoint,
7634 * so we have to be careful not to touch the constraints of the inputs
7635 * in any way.
7637 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7638 __isl_take isl_map *map2)
7640 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7643 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7644 * not be disjoint. The parameters are assumed to have been aligned.
7646 * We currently simply call map_union_disjoint, the internal operation
7647 * of which does not really depend on the inputs being disjoint.
7648 * If the result contains more than one basic map, then we clear
7649 * the disjoint flag since the result may contain basic maps from
7650 * both inputs and these are not guaranteed to be disjoint.
7652 * As a special case, if "map1" and "map2" are obviously equal,
7653 * then we simply return "map1".
7655 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7656 __isl_take isl_map *map2)
7658 int equal;
7660 if (!map1 || !map2)
7661 goto error;
7663 equal = isl_map_plain_is_equal(map1, map2);
7664 if (equal < 0)
7665 goto error;
7666 if (equal) {
7667 isl_map_free(map2);
7668 return map1;
7671 map1 = map_union_disjoint(map1, map2);
7672 if (!map1)
7673 return NULL;
7674 if (map1->n > 1)
7675 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7676 return map1;
7677 error:
7678 isl_map_free(map1);
7679 isl_map_free(map2);
7680 return NULL;
7683 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7684 * not be disjoint.
7686 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7687 __isl_take isl_map *map2)
7689 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7692 __isl_give isl_set *isl_set_union_disjoint(
7693 __isl_take isl_set *set1, __isl_take isl_set *set2)
7695 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7696 set_to_map(set2)));
7699 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7701 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7704 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7705 * the results.
7707 * "map" and "set" are assumed to be compatible and non-NULL.
7709 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7710 __isl_take isl_set *set,
7711 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7712 __isl_take isl_basic_set *bset))
7714 unsigned flags = 0;
7715 struct isl_map *result;
7716 int i, j;
7718 if (isl_set_plain_is_universe(set)) {
7719 isl_set_free(set);
7720 return map;
7723 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7724 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7725 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7727 result = isl_map_alloc_space(isl_space_copy(map->dim),
7728 map->n * set->n, flags);
7729 for (i = 0; result && i < map->n; ++i)
7730 for (j = 0; j < set->n; ++j) {
7731 result = isl_map_add_basic_map(result,
7732 fn(isl_basic_map_copy(map->p[i]),
7733 isl_basic_set_copy(set->p[j])));
7734 if (!result)
7735 break;
7738 isl_map_free(map);
7739 isl_set_free(set);
7740 return result;
7743 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7744 __isl_take isl_set *set)
7746 isl_bool ok;
7748 ok = isl_map_compatible_range(map, set);
7749 if (ok < 0)
7750 goto error;
7751 if (!ok)
7752 isl_die(set->ctx, isl_error_invalid,
7753 "incompatible spaces", goto error);
7755 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7756 error:
7757 isl_map_free(map);
7758 isl_set_free(set);
7759 return NULL;
7762 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7763 __isl_take isl_set *set)
7765 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7768 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7769 __isl_take isl_set *set)
7771 isl_bool ok;
7773 ok = isl_map_compatible_domain(map, set);
7774 if (ok < 0)
7775 goto error;
7776 if (!ok)
7777 isl_die(set->ctx, isl_error_invalid,
7778 "incompatible spaces", goto error);
7780 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7781 error:
7782 isl_map_free(map);
7783 isl_set_free(set);
7784 return NULL;
7787 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7788 __isl_take isl_set *set)
7790 return isl_map_align_params_map_map_and(map, set,
7791 &map_intersect_domain);
7794 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7795 * in the space B -> C, return the intersection.
7796 * The parameters are assumed to have been aligned.
7798 * The map "factor" is first extended to a map living in the space
7799 * [A -> B] -> C and then a regular intersection is computed.
7801 static __isl_give isl_map *map_intersect_domain_factor_range(
7802 __isl_take isl_map *map, __isl_take isl_map *factor)
7804 isl_space *space;
7805 isl_map *ext_factor;
7807 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7808 ext_factor = isl_map_universe(space);
7809 ext_factor = isl_map_domain_product(ext_factor, factor);
7810 return map_intersect(map, ext_factor);
7813 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7814 * in the space B -> C, return the intersection.
7816 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7817 __isl_take isl_map *map, __isl_take isl_map *factor)
7819 return isl_map_align_params_map_map_and(map, factor,
7820 &map_intersect_domain_factor_range);
7823 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7824 * in the space A -> C, return the intersection.
7826 * The map "factor" is first extended to a map living in the space
7827 * A -> [B -> C] and then a regular intersection is computed.
7829 static __isl_give isl_map *map_intersect_range_factor_range(
7830 __isl_take isl_map *map, __isl_take isl_map *factor)
7832 isl_space *space;
7833 isl_map *ext_factor;
7835 space = isl_space_range_factor_domain(isl_map_get_space(map));
7836 ext_factor = isl_map_universe(space);
7837 ext_factor = isl_map_range_product(ext_factor, factor);
7838 return isl_map_intersect(map, ext_factor);
7841 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7842 * in the space A -> C, return the intersection.
7844 __isl_give isl_map *isl_map_intersect_range_factor_range(
7845 __isl_take isl_map *map, __isl_take isl_map *factor)
7847 return isl_map_align_params_map_map_and(map, factor,
7848 &map_intersect_range_factor_range);
7851 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7852 __isl_take isl_map *map2)
7854 if (!map1 || !map2)
7855 goto error;
7856 map1 = isl_map_reverse(map1);
7857 map1 = isl_map_apply_range(map1, map2);
7858 return isl_map_reverse(map1);
7859 error:
7860 isl_map_free(map1);
7861 isl_map_free(map2);
7862 return NULL;
7865 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7866 __isl_take isl_map *map2)
7868 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7871 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7872 __isl_take isl_map *map2)
7874 isl_space *space;
7875 struct isl_map *result;
7876 int i, j;
7878 if (!map1 || !map2)
7879 goto error;
7881 space = isl_space_join(isl_space_copy(map1->dim),
7882 isl_space_copy(map2->dim));
7884 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
7885 if (!result)
7886 goto error;
7887 for (i = 0; i < map1->n; ++i)
7888 for (j = 0; j < map2->n; ++j) {
7889 result = isl_map_add_basic_map(result,
7890 isl_basic_map_apply_range(
7891 isl_basic_map_copy(map1->p[i]),
7892 isl_basic_map_copy(map2->p[j])));
7893 if (!result)
7894 goto error;
7896 isl_map_free(map1);
7897 isl_map_free(map2);
7898 if (result && result->n <= 1)
7899 ISL_F_SET(result, ISL_MAP_DISJOINT);
7900 return result;
7901 error:
7902 isl_map_free(map1);
7903 isl_map_free(map2);
7904 return NULL;
7907 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7908 __isl_take isl_map *map2)
7910 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7914 * returns range - domain
7916 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
7918 isl_space *target_space;
7919 struct isl_basic_set *bset;
7920 unsigned dim;
7921 unsigned nparam;
7922 int i;
7924 if (!bmap)
7925 goto error;
7926 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7927 bmap->dim, isl_dim_out),
7928 goto error);
7929 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7930 dim = isl_basic_map_dim(bmap, isl_dim_in);
7931 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7932 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7933 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7934 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7935 for (i = 0; i < dim; ++i) {
7936 int j = isl_basic_map_alloc_equality(bmap);
7937 if (j < 0) {
7938 bmap = isl_basic_map_free(bmap);
7939 break;
7941 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7942 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7943 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7944 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7946 bset = isl_basic_map_domain(bmap);
7947 bset = isl_basic_set_reset_space(bset, target_space);
7948 return bset;
7949 error:
7950 isl_basic_map_free(bmap);
7951 return NULL;
7954 /* Check that domain and range of "map" are the same.
7956 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
7958 isl_space *space;
7959 isl_bool equal;
7961 space = isl_map_peek_space(map);
7962 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
7963 if (equal < 0)
7964 return isl_stat_error;
7965 if (!equal)
7966 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7967 "domain and range don't match", return isl_stat_error);
7968 return isl_stat_ok;
7972 * returns range - domain
7974 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7976 int i;
7977 isl_space *dim;
7978 struct isl_set *result;
7980 if (!map)
7981 return NULL;
7983 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7984 map->dim, isl_dim_out),
7985 goto error);
7986 dim = isl_map_get_space(map);
7987 dim = isl_space_domain(dim);
7988 result = isl_set_alloc_space(dim, map->n, 0);
7989 if (!result)
7990 goto error;
7991 for (i = 0; i < map->n; ++i)
7992 result = isl_set_add_basic_set(result,
7993 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7994 isl_map_free(map);
7995 return result;
7996 error:
7997 isl_map_free(map);
7998 return NULL;
8002 * returns [domain -> range] -> range - domain
8004 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8005 __isl_take isl_basic_map *bmap)
8007 int i, k;
8008 isl_space *space;
8009 isl_basic_map *domain;
8010 int nparam, n;
8011 unsigned total;
8013 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8014 bmap->dim, isl_dim_out))
8015 isl_die(bmap->ctx, isl_error_invalid,
8016 "domain and range don't match", goto error);
8018 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8019 n = isl_basic_map_dim(bmap, isl_dim_in);
8021 space = isl_basic_map_get_space(bmap);
8022 space = isl_space_from_range(isl_space_domain(space));
8023 domain = isl_basic_map_universe(space);
8025 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8026 bmap = isl_basic_map_apply_range(bmap, domain);
8027 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8029 total = isl_basic_map_total_dim(bmap);
8031 for (i = 0; i < n; ++i) {
8032 k = isl_basic_map_alloc_equality(bmap);
8033 if (k < 0)
8034 goto error;
8035 isl_seq_clr(bmap->eq[k], 1 + total);
8036 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8037 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8038 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8041 bmap = isl_basic_map_gauss(bmap, NULL);
8042 return isl_basic_map_finalize(bmap);
8043 error:
8044 isl_basic_map_free(bmap);
8045 return NULL;
8049 * returns [domain -> range] -> range - domain
8051 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8053 int i;
8054 isl_space *domain_space;
8056 if (isl_map_check_equal_tuples(map) < 0)
8057 return isl_map_free(map);
8059 map = isl_map_cow(map);
8060 if (!map)
8061 return NULL;
8063 domain_space = isl_space_domain(isl_map_get_space(map));
8064 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8065 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8066 if (!map->dim)
8067 goto error;
8068 for (i = 0; i < map->n; ++i) {
8069 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8070 if (!map->p[i])
8071 goto error;
8073 map = isl_map_unmark_normalized(map);
8074 return map;
8075 error:
8076 isl_map_free(map);
8077 return NULL;
8080 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8082 unsigned n_in, n_out;
8084 if (!space)
8085 return NULL;
8086 n_in = isl_space_dim(space, isl_dim_in);
8087 n_out = isl_space_dim(space, isl_dim_out);
8088 if (n_in != n_out)
8089 isl_die(space->ctx, isl_error_invalid,
8090 "number of input and output dimensions needs to be "
8091 "the same", goto error);
8092 return isl_basic_map_equal(space, n_in);
8093 error:
8094 isl_space_free(space);
8095 return NULL;
8098 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8100 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8103 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8105 isl_space *dim = isl_set_get_space(set);
8106 isl_map *id;
8107 id = isl_map_identity(isl_space_map_from_set(dim));
8108 return isl_map_intersect_range(id, set);
8111 /* Construct a basic set with all set dimensions having only non-negative
8112 * values.
8114 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8115 __isl_take isl_space *space)
8117 int i;
8118 unsigned nparam;
8119 unsigned dim;
8120 struct isl_basic_set *bset;
8122 if (!space)
8123 return NULL;
8124 nparam = isl_space_dim(space, isl_dim_param);
8125 dim = isl_space_dim(space, isl_dim_set);
8126 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8127 if (!bset)
8128 return NULL;
8129 for (i = 0; i < dim; ++i) {
8130 int k = isl_basic_set_alloc_inequality(bset);
8131 if (k < 0)
8132 goto error;
8133 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8134 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8136 return bset;
8137 error:
8138 isl_basic_set_free(bset);
8139 return NULL;
8142 /* Construct the half-space x_pos >= 0.
8144 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8145 int pos)
8147 int k;
8148 isl_basic_set *nonneg;
8150 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8151 k = isl_basic_set_alloc_inequality(nonneg);
8152 if (k < 0)
8153 goto error;
8154 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8155 isl_int_set_si(nonneg->ineq[k][pos], 1);
8157 return isl_basic_set_finalize(nonneg);
8158 error:
8159 isl_basic_set_free(nonneg);
8160 return NULL;
8163 /* Construct the half-space x_pos <= -1.
8165 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8166 int pos)
8168 int k;
8169 isl_basic_set *neg;
8171 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8172 k = isl_basic_set_alloc_inequality(neg);
8173 if (k < 0)
8174 goto error;
8175 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8176 isl_int_set_si(neg->ineq[k][0], -1);
8177 isl_int_set_si(neg->ineq[k][pos], -1);
8179 return isl_basic_set_finalize(neg);
8180 error:
8181 isl_basic_set_free(neg);
8182 return NULL;
8185 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8186 enum isl_dim_type type, unsigned first, unsigned n)
8188 int i;
8189 unsigned offset;
8190 isl_basic_set *nonneg;
8191 isl_basic_set *neg;
8193 if (n == 0)
8194 return set;
8196 if (isl_set_check_range(set, type, first, n) < 0)
8197 return isl_set_free(set);
8199 offset = pos(set->dim, type);
8200 for (i = 0; i < n; ++i) {
8201 nonneg = nonneg_halfspace(isl_set_get_space(set),
8202 offset + first + i);
8203 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8205 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8208 return set;
8211 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8212 int len,
8213 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8214 void *user)
8216 isl_set *half;
8218 if (!set)
8219 return isl_stat_error;
8220 if (isl_set_plain_is_empty(set)) {
8221 isl_set_free(set);
8222 return isl_stat_ok;
8224 if (first == len)
8225 return fn(set, signs, user);
8227 signs[first] = 1;
8228 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8229 1 + first));
8230 half = isl_set_intersect(half, isl_set_copy(set));
8231 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8232 goto error;
8234 signs[first] = -1;
8235 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8236 1 + first));
8237 half = isl_set_intersect(half, set);
8238 return foreach_orthant(half, signs, first + 1, len, fn, user);
8239 error:
8240 isl_set_free(set);
8241 return isl_stat_error;
8244 /* Call "fn" on the intersections of "set" with each of the orthants
8245 * (except for obviously empty intersections). The orthant is identified
8246 * by the signs array, with each entry having value 1 or -1 according
8247 * to the sign of the corresponding variable.
8249 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8250 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8251 void *user)
8253 unsigned nparam;
8254 unsigned nvar;
8255 int *signs;
8256 isl_stat r;
8258 if (!set)
8259 return isl_stat_error;
8260 if (isl_set_plain_is_empty(set))
8261 return isl_stat_ok;
8263 nparam = isl_set_dim(set, isl_dim_param);
8264 nvar = isl_set_dim(set, isl_dim_set);
8266 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8268 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8269 fn, user);
8271 free(signs);
8273 return r;
8276 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8278 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8281 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8282 __isl_keep isl_basic_map *bmap2)
8284 isl_bool is_subset;
8285 struct isl_map *map1;
8286 struct isl_map *map2;
8288 if (!bmap1 || !bmap2)
8289 return isl_bool_error;
8291 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8292 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8294 is_subset = isl_map_is_subset(map1, map2);
8296 isl_map_free(map1);
8297 isl_map_free(map2);
8299 return is_subset;
8302 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8303 __isl_keep isl_basic_set *bset2)
8305 return isl_basic_map_is_subset(bset1, bset2);
8308 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8309 __isl_keep isl_basic_map *bmap2)
8311 isl_bool is_subset;
8313 if (!bmap1 || !bmap2)
8314 return isl_bool_error;
8315 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8316 if (is_subset != isl_bool_true)
8317 return is_subset;
8318 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8319 return is_subset;
8322 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8323 __isl_keep isl_basic_set *bset2)
8325 return isl_basic_map_is_equal(
8326 bset_to_bmap(bset1), bset_to_bmap(bset2));
8329 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8331 int i;
8332 int is_empty;
8334 if (!map)
8335 return isl_bool_error;
8336 for (i = 0; i < map->n; ++i) {
8337 is_empty = isl_basic_map_is_empty(map->p[i]);
8338 if (is_empty < 0)
8339 return isl_bool_error;
8340 if (!is_empty)
8341 return isl_bool_false;
8343 return isl_bool_true;
8346 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8348 return map ? map->n == 0 : isl_bool_error;
8351 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8353 return set ? set->n == 0 : isl_bool_error;
8356 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8358 return isl_map_is_empty(set_to_map(set));
8361 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8362 __isl_keep isl_map *map2)
8364 if (!map1 || !map2)
8365 return isl_bool_error;
8367 return isl_space_is_equal(map1->dim, map2->dim);
8370 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8371 __isl_keep isl_set *set2)
8373 if (!set1 || !set2)
8374 return isl_bool_error;
8376 return isl_space_is_equal(set1->dim, set2->dim);
8379 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8381 isl_bool is_subset;
8383 if (!map1 || !map2)
8384 return isl_bool_error;
8385 is_subset = isl_map_is_subset(map1, map2);
8386 if (is_subset != isl_bool_true)
8387 return is_subset;
8388 is_subset = isl_map_is_subset(map2, map1);
8389 return is_subset;
8392 /* Is "map1" equal to "map2"?
8394 * First check if they are obviously equal.
8395 * If not, then perform a more detailed analysis.
8397 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8399 isl_bool equal;
8401 equal = isl_map_plain_is_equal(map1, map2);
8402 if (equal < 0 || equal)
8403 return equal;
8404 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8407 isl_bool isl_basic_map_is_strict_subset(
8408 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8410 isl_bool is_subset;
8412 if (!bmap1 || !bmap2)
8413 return isl_bool_error;
8414 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8415 if (is_subset != isl_bool_true)
8416 return is_subset;
8417 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8418 return isl_bool_not(is_subset);
8421 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8422 __isl_keep isl_map *map2)
8424 isl_bool is_subset;
8426 if (!map1 || !map2)
8427 return isl_bool_error;
8428 is_subset = isl_map_is_subset(map1, map2);
8429 if (is_subset != isl_bool_true)
8430 return is_subset;
8431 is_subset = isl_map_is_subset(map2, map1);
8432 return isl_bool_not(is_subset);
8435 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8436 __isl_keep isl_set *set2)
8438 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8441 /* Is "bmap" obviously equal to the universe with the same space?
8443 * That is, does it not have any constraints?
8445 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8447 if (!bmap)
8448 return isl_bool_error;
8449 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8452 /* Is "bset" obviously equal to the universe with the same space?
8454 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8456 return isl_basic_map_plain_is_universe(bset);
8459 /* If "c" does not involve any existentially quantified variables,
8460 * then set *univ to false and abort
8462 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8464 isl_bool *univ = user;
8465 unsigned n;
8467 n = isl_constraint_dim(c, isl_dim_div);
8468 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8469 isl_constraint_free(c);
8470 if (*univ < 0 || !*univ)
8471 return isl_stat_error;
8472 return isl_stat_ok;
8475 /* Is "bmap" equal to the universe with the same space?
8477 * First check if it is obviously equal to the universe.
8478 * If not and if there are any constraints not involving
8479 * existentially quantified variables, then it is certainly
8480 * not equal to the universe.
8481 * Otherwise, check if the universe is a subset of "bmap".
8483 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8485 isl_bool univ;
8486 isl_basic_map *test;
8488 univ = isl_basic_map_plain_is_universe(bmap);
8489 if (univ < 0 || univ)
8490 return univ;
8491 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8492 return isl_bool_false;
8493 univ = isl_bool_true;
8494 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8495 univ)
8496 return isl_bool_error;
8497 if (univ < 0 || !univ)
8498 return univ;
8499 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8500 univ = isl_basic_map_is_subset(test, bmap);
8501 isl_basic_map_free(test);
8502 return univ;
8505 /* Is "bset" equal to the universe with the same space?
8507 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8509 return isl_basic_map_is_universe(bset);
8512 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8514 int i;
8516 if (!map)
8517 return isl_bool_error;
8519 for (i = 0; i < map->n; ++i) {
8520 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8521 if (r < 0 || r)
8522 return r;
8525 return isl_bool_false;
8528 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8530 return isl_map_plain_is_universe(set_to_map(set));
8533 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8535 struct isl_basic_set *bset = NULL;
8536 struct isl_vec *sample = NULL;
8537 isl_bool empty, non_empty;
8539 if (!bmap)
8540 return isl_bool_error;
8542 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8543 return isl_bool_true;
8545 if (isl_basic_map_plain_is_universe(bmap))
8546 return isl_bool_false;
8548 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8549 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8550 copy = isl_basic_map_remove_redundancies(copy);
8551 empty = isl_basic_map_plain_is_empty(copy);
8552 isl_basic_map_free(copy);
8553 return empty;
8556 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8557 if (non_empty < 0)
8558 return isl_bool_error;
8559 if (non_empty)
8560 return isl_bool_false;
8561 isl_vec_free(bmap->sample);
8562 bmap->sample = NULL;
8563 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8564 if (!bset)
8565 return isl_bool_error;
8566 sample = isl_basic_set_sample_vec(bset);
8567 if (!sample)
8568 return isl_bool_error;
8569 empty = sample->size == 0;
8570 isl_vec_free(bmap->sample);
8571 bmap->sample = sample;
8572 if (empty)
8573 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8575 return empty;
8578 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8580 if (!bmap)
8581 return isl_bool_error;
8582 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8585 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8587 if (!bset)
8588 return isl_bool_error;
8589 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8592 /* Is "bmap" known to be non-empty?
8594 * That is, is the cached sample still valid?
8596 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8598 unsigned total;
8600 if (!bmap)
8601 return isl_bool_error;
8602 if (!bmap->sample)
8603 return isl_bool_false;
8604 total = 1 + isl_basic_map_total_dim(bmap);
8605 if (bmap->sample->size != total)
8606 return isl_bool_false;
8607 return isl_basic_map_contains(bmap, bmap->sample);
8610 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8612 return isl_basic_map_is_empty(bset_to_bmap(bset));
8615 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8616 __isl_take isl_basic_map *bmap2)
8618 struct isl_map *map;
8619 if (!bmap1 || !bmap2)
8620 goto error;
8622 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8624 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8625 if (!map)
8626 goto error;
8627 map = isl_map_add_basic_map(map, bmap1);
8628 map = isl_map_add_basic_map(map, bmap2);
8629 return map;
8630 error:
8631 isl_basic_map_free(bmap1);
8632 isl_basic_map_free(bmap2);
8633 return NULL;
8636 struct isl_set *isl_basic_set_union(
8637 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8639 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8640 bset_to_bmap(bset2)));
8643 /* Order divs such that any div only depends on previous divs */
8644 __isl_give isl_basic_map *isl_basic_map_order_divs(
8645 __isl_take isl_basic_map *bmap)
8647 int i;
8648 int off;
8650 off = isl_basic_map_var_offset(bmap, isl_dim_div);
8651 if (off < 0)
8652 return isl_basic_map_free(bmap);
8654 for (i = 0; i < bmap->n_div; ++i) {
8655 int pos;
8656 if (isl_int_is_zero(bmap->div[i][0]))
8657 continue;
8658 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8659 bmap->n_div-i);
8660 if (pos == -1)
8661 continue;
8662 if (pos == 0)
8663 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8664 "integer division depends on itself",
8665 return isl_basic_map_free(bmap));
8666 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8667 if (!bmap)
8668 return NULL;
8669 --i;
8671 return bmap;
8674 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8676 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8679 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8681 int i;
8683 if (!map)
8684 return 0;
8686 for (i = 0; i < map->n; ++i) {
8687 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8688 if (!map->p[i])
8689 goto error;
8692 return map;
8693 error:
8694 isl_map_free(map);
8695 return NULL;
8698 /* Sort the local variables of "bset".
8700 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8701 __isl_take isl_basic_set *bset)
8703 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8706 /* Apply the expansion computed by isl_merge_divs.
8707 * The expansion itself is given by "exp" while the resulting
8708 * list of divs is given by "div".
8710 * Move the integer divisions of "bmap" into the right position
8711 * according to "exp" and then introduce the additional integer
8712 * divisions, adding div constraints.
8713 * The moving should be done first to avoid moving coefficients
8714 * in the definitions of the extra integer divisions.
8716 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8717 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8719 int i, j;
8720 int n_div;
8722 bmap = isl_basic_map_cow(bmap);
8723 if (!bmap || !div)
8724 goto error;
8726 if (div->n_row < bmap->n_div)
8727 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8728 "not an expansion", goto error);
8730 n_div = bmap->n_div;
8731 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8732 div->n_row - n_div, 0,
8733 2 * (div->n_row - n_div));
8735 for (i = n_div; i < div->n_row; ++i)
8736 if (isl_basic_map_alloc_div(bmap) < 0)
8737 goto error;
8739 for (j = n_div - 1; j >= 0; --j) {
8740 if (exp[j] == j)
8741 break;
8742 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8743 if (!bmap)
8744 goto error;
8746 j = 0;
8747 for (i = 0; i < div->n_row; ++i) {
8748 if (j < n_div && exp[j] == i) {
8749 j++;
8750 } else {
8751 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8752 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8753 continue;
8754 bmap = isl_basic_map_add_div_constraints(bmap, i);
8755 if (!bmap)
8756 goto error;
8760 isl_mat_free(div);
8761 return bmap;
8762 error:
8763 isl_basic_map_free(bmap);
8764 isl_mat_free(div);
8765 return NULL;
8768 /* Apply the expansion computed by isl_merge_divs.
8769 * The expansion itself is given by "exp" while the resulting
8770 * list of divs is given by "div".
8772 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8773 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8775 return isl_basic_map_expand_divs(bset, div, exp);
8778 /* Look for a div in dst that corresponds to the div "div" in src.
8779 * The divs before "div" in src and dst are assumed to be the same.
8781 * Return the position of the corresponding div in dst
8782 * if there is one. Otherwise, return a position beyond the integer divisions.
8783 * Return -1 on error.
8785 static int find_div(__isl_keep isl_basic_map *dst,
8786 __isl_keep isl_basic_map *src, unsigned div)
8788 int i;
8789 unsigned n_div;
8790 int v_div;
8792 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8793 if (!dst || v_div < 0)
8794 return -1;
8796 n_div = isl_basic_map_dim(dst, isl_dim_div);
8797 isl_assert(dst->ctx, div <= n_div, return -1);
8798 for (i = div; i < n_div; ++i)
8799 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
8800 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
8801 n_div - div) == -1)
8802 return i;
8803 return n_div;
8806 /* Align the divs of "dst" to those of "src", adding divs from "src"
8807 * if needed. That is, make sure that the first src->n_div divs
8808 * of the result are equal to those of src.
8810 * The result is not finalized as by design it will have redundant
8811 * divs if any divs from "src" were copied.
8813 __isl_give isl_basic_map *isl_basic_map_align_divs(
8814 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8816 int i;
8817 isl_bool known;
8818 int extended;
8819 int v_div;
8820 unsigned dst_n_div;
8822 if (!dst || !src)
8823 return isl_basic_map_free(dst);
8825 if (src->n_div == 0)
8826 return dst;
8828 known = isl_basic_map_divs_known(src);
8829 if (known < 0)
8830 return isl_basic_map_free(dst);
8831 if (!known)
8832 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8833 "some src divs are unknown",
8834 return isl_basic_map_free(dst));
8836 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8837 if (v_div < 0)
8838 return isl_basic_map_free(dst);
8840 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8841 if (!src)
8842 return isl_basic_map_free(dst);
8844 extended = 0;
8845 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
8846 for (i = 0; i < src->n_div; ++i) {
8847 int j = find_div(dst, src, i);
8848 if (j < 0)
8849 dst = isl_basic_map_free(dst);
8850 if (j == dst_n_div) {
8851 if (!extended) {
8852 int extra = src->n_div - i;
8853 dst = isl_basic_map_cow(dst);
8854 if (!dst)
8855 goto error;
8856 dst = isl_basic_map_extend_space(dst,
8857 isl_space_copy(dst->dim),
8858 extra, 0, 2 * extra);
8859 extended = 1;
8861 j = isl_basic_map_alloc_div(dst);
8862 if (j < 0)
8863 goto error;
8864 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
8865 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
8866 dst_n_div++;
8867 dst = isl_basic_map_add_div_constraints(dst, j);
8868 if (!dst)
8869 goto error;
8871 if (j != i)
8872 dst = isl_basic_map_swap_div(dst, i, j);
8873 if (!dst)
8874 goto error;
8876 isl_basic_map_free(src);
8877 return dst;
8878 error:
8879 isl_basic_map_free(src);
8880 isl_basic_map_free(dst);
8881 return NULL;
8884 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8886 int i;
8888 if (!map)
8889 return NULL;
8890 if (map->n == 0)
8891 return map;
8892 map = isl_map_compute_divs(map);
8893 map = isl_map_cow(map);
8894 if (!map)
8895 return NULL;
8897 for (i = 1; i < map->n; ++i)
8898 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8899 for (i = 1; i < map->n; ++i) {
8900 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8901 if (!map->p[i])
8902 return isl_map_free(map);
8905 map = isl_map_unmark_normalized(map);
8906 return map;
8909 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8911 return isl_map_align_divs_internal(map);
8914 struct isl_set *isl_set_align_divs(struct isl_set *set)
8916 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8919 /* Align the divs of the basic maps in "map" to those
8920 * of the basic maps in "list", as well as to the other basic maps in "map".
8921 * The elements in "list" are assumed to have known divs.
8923 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8924 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8926 int i, n;
8928 map = isl_map_compute_divs(map);
8929 map = isl_map_cow(map);
8930 if (!map || !list)
8931 return isl_map_free(map);
8932 if (map->n == 0)
8933 return map;
8935 n = isl_basic_map_list_n_basic_map(list);
8936 for (i = 0; i < n; ++i) {
8937 isl_basic_map *bmap;
8939 bmap = isl_basic_map_list_get_basic_map(list, i);
8940 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8941 isl_basic_map_free(bmap);
8943 if (!map->p[0])
8944 return isl_map_free(map);
8946 return isl_map_align_divs_internal(map);
8949 /* Align the divs of each element of "list" to those of "bmap".
8950 * Both "bmap" and the elements of "list" are assumed to have known divs.
8952 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8953 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8955 int i, n;
8957 if (!list || !bmap)
8958 return isl_basic_map_list_free(list);
8960 n = isl_basic_map_list_n_basic_map(list);
8961 for (i = 0; i < n; ++i) {
8962 isl_basic_map *bmap_i;
8964 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8965 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8966 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8969 return list;
8972 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8973 __isl_take isl_map *map)
8975 isl_bool ok;
8977 ok = isl_map_compatible_domain(map, set);
8978 if (ok < 0)
8979 goto error;
8980 if (!ok)
8981 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8982 "incompatible spaces", goto error);
8983 map = isl_map_intersect_domain(map, set);
8984 set = isl_map_range(map);
8985 return set;
8986 error:
8987 isl_set_free(set);
8988 isl_map_free(map);
8989 return NULL;
8992 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8993 __isl_take isl_map *map)
8995 return isl_map_align_params_map_map_and(set, map, &set_apply);
8998 /* There is no need to cow as removing empty parts doesn't change
8999 * the meaning of the set.
9001 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9003 int i;
9005 if (!map)
9006 return NULL;
9008 for (i = map->n - 1; i >= 0; --i)
9009 map = remove_if_empty(map, i);
9011 return map;
9014 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9016 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9019 /* Create a binary relation that maps the shared initial "pos" dimensions
9020 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9022 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9023 __isl_keep isl_basic_set *bset2, int pos)
9025 isl_basic_map *bmap1;
9026 isl_basic_map *bmap2;
9028 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9029 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9030 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9031 isl_dim_out, 0, pos);
9032 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9033 isl_dim_out, 0, pos);
9034 return isl_basic_map_range_product(bmap1, bmap2);
9037 /* Given two basic sets bset1 and bset2, compute the maximal difference
9038 * between the values of dimension pos in bset1 and those in bset2
9039 * for any common value of the parameters and dimensions preceding pos.
9041 static enum isl_lp_result basic_set_maximal_difference_at(
9042 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9043 int pos, isl_int *opt)
9045 isl_basic_map *bmap1;
9046 struct isl_ctx *ctx;
9047 struct isl_vec *obj;
9048 unsigned total;
9049 unsigned nparam;
9050 unsigned dim1;
9051 enum isl_lp_result res;
9053 if (!bset1 || !bset2)
9054 return isl_lp_error;
9056 nparam = isl_basic_set_n_param(bset1);
9057 dim1 = isl_basic_set_n_dim(bset1);
9059 bmap1 = join_initial(bset1, bset2, pos);
9060 if (!bmap1)
9061 return isl_lp_error;
9063 total = isl_basic_map_total_dim(bmap1);
9064 ctx = bmap1->ctx;
9065 obj = isl_vec_alloc(ctx, 1 + total);
9066 if (!obj)
9067 goto error;
9068 isl_seq_clr(obj->block.data, 1 + total);
9069 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9070 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9071 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9072 opt, NULL, NULL);
9073 isl_basic_map_free(bmap1);
9074 isl_vec_free(obj);
9075 return res;
9076 error:
9077 isl_basic_map_free(bmap1);
9078 return isl_lp_error;
9081 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9082 * for any common value of the parameters and dimensions preceding pos
9083 * in both basic sets, the values of dimension pos in bset1 are
9084 * smaller or larger than those in bset2.
9086 * Returns
9087 * 1 if bset1 follows bset2
9088 * -1 if bset1 precedes bset2
9089 * 0 if bset1 and bset2 are incomparable
9090 * -2 if some error occurred.
9092 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9093 __isl_keep isl_basic_set *bset2, int pos)
9095 isl_int opt;
9096 enum isl_lp_result res;
9097 int cmp;
9099 isl_int_init(opt);
9101 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9103 if (res == isl_lp_empty)
9104 cmp = 0;
9105 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9106 res == isl_lp_unbounded)
9107 cmp = 1;
9108 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9109 cmp = -1;
9110 else
9111 cmp = -2;
9113 isl_int_clear(opt);
9114 return cmp;
9117 /* Given two basic sets bset1 and bset2, check whether
9118 * for any common value of the parameters and dimensions preceding pos
9119 * there is a value of dimension pos in bset1 that is larger
9120 * than a value of the same dimension in bset2.
9122 * Return
9123 * 1 if there exists such a pair
9124 * 0 if there is no such pair, but there is a pair of equal values
9125 * -1 otherwise
9126 * -2 if some error occurred.
9128 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9129 __isl_keep isl_basic_set *bset2, int pos)
9131 isl_bool empty;
9132 isl_basic_map *bmap;
9133 unsigned dim1;
9135 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9136 bmap = join_initial(bset1, bset2, pos);
9137 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9138 isl_dim_out, dim1 - pos);
9139 empty = isl_basic_map_is_empty(bmap);
9140 if (empty < 0)
9141 goto error;
9142 if (empty) {
9143 isl_basic_map_free(bmap);
9144 return -1;
9146 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9147 isl_dim_out, dim1 - pos);
9148 empty = isl_basic_map_is_empty(bmap);
9149 if (empty < 0)
9150 goto error;
9151 isl_basic_map_free(bmap);
9152 if (empty)
9153 return 0;
9154 return 1;
9155 error:
9156 isl_basic_map_free(bmap);
9157 return -2;
9160 /* Given two sets set1 and set2, check whether
9161 * for any common value of the parameters and dimensions preceding pos
9162 * there is a value of dimension pos in set1 that is larger
9163 * than a value of the same dimension in set2.
9165 * Return
9166 * 1 if there exists such a pair
9167 * 0 if there is no such pair, but there is a pair of equal values
9168 * -1 otherwise
9169 * -2 if some error occurred.
9171 int isl_set_follows_at(__isl_keep isl_set *set1,
9172 __isl_keep isl_set *set2, int pos)
9174 int i, j;
9175 int follows = -1;
9177 if (!set1 || !set2)
9178 return -2;
9180 for (i = 0; i < set1->n; ++i)
9181 for (j = 0; j < set2->n; ++j) {
9182 int f;
9183 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9184 if (f == 1 || f == -2)
9185 return f;
9186 if (f > follows)
9187 follows = f;
9190 return follows;
9193 static isl_bool isl_basic_map_plain_has_fixed_var(
9194 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9196 int i;
9197 int d;
9198 unsigned total;
9200 if (!bmap)
9201 return isl_bool_error;
9202 total = isl_basic_map_total_dim(bmap);
9203 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9204 for (; d+1 > pos; --d)
9205 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9206 break;
9207 if (d != pos)
9208 continue;
9209 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9210 return isl_bool_false;
9211 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9212 return isl_bool_false;
9213 if (!isl_int_is_one(bmap->eq[i][1+d]))
9214 return isl_bool_false;
9215 if (val)
9216 isl_int_neg(*val, bmap->eq[i][0]);
9217 return isl_bool_true;
9219 return isl_bool_false;
9222 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9223 unsigned pos, isl_int *val)
9225 int i;
9226 isl_int v;
9227 isl_int tmp;
9228 isl_bool fixed;
9230 if (!map)
9231 return isl_bool_error;
9232 if (map->n == 0)
9233 return isl_bool_false;
9234 if (map->n == 1)
9235 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9236 isl_int_init(v);
9237 isl_int_init(tmp);
9238 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9239 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9240 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9241 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9242 fixed = isl_bool_false;
9244 if (val)
9245 isl_int_set(*val, v);
9246 isl_int_clear(tmp);
9247 isl_int_clear(v);
9248 return fixed;
9251 static isl_bool isl_basic_set_plain_has_fixed_var(
9252 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9254 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9255 pos, val);
9258 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9259 enum isl_dim_type type, unsigned pos, isl_int *val)
9261 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9262 return isl_bool_error;
9263 return isl_basic_map_plain_has_fixed_var(bmap,
9264 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9267 /* If "bmap" obviously lies on a hyperplane where the given dimension
9268 * has a fixed value, then return that value.
9269 * Otherwise return NaN.
9271 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9272 __isl_keep isl_basic_map *bmap,
9273 enum isl_dim_type type, unsigned pos)
9275 isl_ctx *ctx;
9276 isl_val *v;
9277 isl_bool fixed;
9279 if (!bmap)
9280 return NULL;
9281 ctx = isl_basic_map_get_ctx(bmap);
9282 v = isl_val_alloc(ctx);
9283 if (!v)
9284 return NULL;
9285 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9286 if (fixed < 0)
9287 return isl_val_free(v);
9288 if (fixed) {
9289 isl_int_set_si(v->d, 1);
9290 return v;
9292 isl_val_free(v);
9293 return isl_val_nan(ctx);
9296 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9297 enum isl_dim_type type, unsigned pos, isl_int *val)
9299 if (isl_map_check_range(map, type, pos, 1) < 0)
9300 return isl_bool_error;
9301 return isl_map_plain_has_fixed_var(map,
9302 map_offset(map, type) - 1 + pos, val);
9305 /* If "map" obviously lies on a hyperplane where the given dimension
9306 * has a fixed value, then return that value.
9307 * Otherwise return NaN.
9309 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9310 enum isl_dim_type type, unsigned pos)
9312 isl_ctx *ctx;
9313 isl_val *v;
9314 isl_bool fixed;
9316 if (!map)
9317 return NULL;
9318 ctx = isl_map_get_ctx(map);
9319 v = isl_val_alloc(ctx);
9320 if (!v)
9321 return NULL;
9322 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9323 if (fixed < 0)
9324 return isl_val_free(v);
9325 if (fixed) {
9326 isl_int_set_si(v->d, 1);
9327 return v;
9329 isl_val_free(v);
9330 return isl_val_nan(ctx);
9333 /* If "set" obviously lies on a hyperplane where the given dimension
9334 * has a fixed value, then return that value.
9335 * Otherwise return NaN.
9337 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9338 enum isl_dim_type type, unsigned pos)
9340 return isl_map_plain_get_val_if_fixed(set, type, pos);
9343 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9344 * then return this fixed value in *val.
9346 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9347 unsigned dim, isl_int *val)
9349 return isl_basic_set_plain_has_fixed_var(bset,
9350 isl_basic_set_n_param(bset) + dim, val);
9353 /* Return -1 if the constraint "c1" should be sorted before "c2"
9354 * and 1 if it should be sorted after "c2".
9355 * Return 0 if the two constraints are the same (up to the constant term).
9357 * In particular, if a constraint involves later variables than another
9358 * then it is sorted after this other constraint.
9359 * uset_gist depends on constraints without existentially quantified
9360 * variables sorting first.
9362 * For constraints that have the same latest variable, those
9363 * with the same coefficient for this latest variable (first in absolute value
9364 * and then in actual value) are grouped together.
9365 * This is useful for detecting pairs of constraints that can
9366 * be chained in their printed representation.
9368 * Finally, within a group, constraints are sorted according to
9369 * their coefficients (excluding the constant term).
9371 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9373 isl_int **c1 = (isl_int **) p1;
9374 isl_int **c2 = (isl_int **) p2;
9375 int l1, l2;
9376 unsigned size = *(unsigned *) arg;
9377 int cmp;
9379 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9380 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9382 if (l1 != l2)
9383 return l1 - l2;
9385 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9386 if (cmp != 0)
9387 return cmp;
9388 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9389 if (cmp != 0)
9390 return -cmp;
9392 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9395 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9396 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9397 * and 0 if the two constraints are the same (up to the constant term).
9399 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9400 isl_int *c1, isl_int *c2)
9402 unsigned total;
9404 if (!bmap)
9405 return -2;
9406 total = isl_basic_map_total_dim(bmap);
9407 return sort_constraint_cmp(&c1, &c2, &total);
9410 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9411 __isl_take isl_basic_map *bmap)
9413 unsigned total;
9415 if (!bmap)
9416 return NULL;
9417 if (bmap->n_ineq == 0)
9418 return bmap;
9419 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9420 return bmap;
9421 total = isl_basic_map_total_dim(bmap);
9422 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9423 &sort_constraint_cmp, &total) < 0)
9424 return isl_basic_map_free(bmap);
9425 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9426 return bmap;
9429 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9430 __isl_take isl_basic_set *bset)
9432 isl_basic_map *bmap = bset_to_bmap(bset);
9433 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9436 __isl_give isl_basic_map *isl_basic_map_normalize(
9437 __isl_take isl_basic_map *bmap)
9439 bmap = isl_basic_map_remove_redundancies(bmap);
9440 bmap = isl_basic_map_sort_constraints(bmap);
9441 return bmap;
9443 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9444 __isl_keep isl_basic_map *bmap2)
9446 int i, cmp;
9447 unsigned total;
9448 isl_space *space1, *space2;
9450 if (!bmap1 || !bmap2)
9451 return -1;
9453 if (bmap1 == bmap2)
9454 return 0;
9455 space1 = isl_basic_map_peek_space(bmap1);
9456 space2 = isl_basic_map_peek_space(bmap2);
9457 cmp = isl_space_cmp(space1, space2);
9458 if (cmp)
9459 return cmp;
9460 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9461 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9462 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9463 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9464 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9465 return 0;
9466 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9467 return 1;
9468 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9469 return -1;
9470 if (bmap1->n_eq != bmap2->n_eq)
9471 return bmap1->n_eq - bmap2->n_eq;
9472 if (bmap1->n_ineq != bmap2->n_ineq)
9473 return bmap1->n_ineq - bmap2->n_ineq;
9474 if (bmap1->n_div != bmap2->n_div)
9475 return bmap1->n_div - bmap2->n_div;
9476 total = isl_basic_map_total_dim(bmap1);
9477 for (i = 0; i < bmap1->n_eq; ++i) {
9478 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9479 if (cmp)
9480 return cmp;
9482 for (i = 0; i < bmap1->n_ineq; ++i) {
9483 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9484 if (cmp)
9485 return cmp;
9487 for (i = 0; i < bmap1->n_div; ++i) {
9488 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9489 if (cmp)
9490 return cmp;
9492 return 0;
9495 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9496 __isl_keep isl_basic_set *bset2)
9498 return isl_basic_map_plain_cmp(bset1, bset2);
9501 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9503 int i, cmp;
9505 if (set1 == set2)
9506 return 0;
9507 if (set1->n != set2->n)
9508 return set1->n - set2->n;
9510 for (i = 0; i < set1->n; ++i) {
9511 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9512 if (cmp)
9513 return cmp;
9516 return 0;
9519 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9520 __isl_keep isl_basic_map *bmap2)
9522 if (!bmap1 || !bmap2)
9523 return isl_bool_error;
9524 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9527 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9528 __isl_keep isl_basic_set *bset2)
9530 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9531 bset_to_bmap(bset2));
9534 static int qsort_bmap_cmp(const void *p1, const void *p2)
9536 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9537 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9539 return isl_basic_map_plain_cmp(bmap1, bmap2);
9542 /* Sort the basic maps of "map" and remove duplicate basic maps.
9544 * While removing basic maps, we make sure that the basic maps remain
9545 * sorted because isl_map_normalize expects the basic maps of the result
9546 * to be sorted.
9548 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9550 int i, j;
9552 map = isl_map_remove_empty_parts(map);
9553 if (!map)
9554 return NULL;
9555 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9556 for (i = map->n - 1; i >= 1; --i) {
9557 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9558 continue;
9559 isl_basic_map_free(map->p[i-1]);
9560 for (j = i; j < map->n; ++j)
9561 map->p[j - 1] = map->p[j];
9562 map->n--;
9565 return map;
9568 /* Remove obvious duplicates among the basic maps of "map".
9570 * Unlike isl_map_normalize, this function does not remove redundant
9571 * constraints and only removes duplicates that have exactly the same
9572 * constraints in the input. It does sort the constraints and
9573 * the basic maps to ease the detection of duplicates.
9575 * If "map" has already been normalized or if the basic maps are
9576 * disjoint, then there can be no duplicates.
9578 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9580 int i;
9581 isl_basic_map *bmap;
9583 if (!map)
9584 return NULL;
9585 if (map->n <= 1)
9586 return map;
9587 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9588 return map;
9589 for (i = 0; i < map->n; ++i) {
9590 bmap = isl_basic_map_copy(map->p[i]);
9591 bmap = isl_basic_map_sort_constraints(bmap);
9592 if (!bmap)
9593 return isl_map_free(map);
9594 isl_basic_map_free(map->p[i]);
9595 map->p[i] = bmap;
9598 map = sort_and_remove_duplicates(map);
9599 return map;
9602 /* We normalize in place, but if anything goes wrong we need
9603 * to return NULL, so we need to make sure we don't change the
9604 * meaning of any possible other copies of map.
9606 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9608 int i;
9609 struct isl_basic_map *bmap;
9611 if (!map)
9612 return NULL;
9613 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9614 return map;
9615 for (i = 0; i < map->n; ++i) {
9616 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9617 if (!bmap)
9618 goto error;
9619 isl_basic_map_free(map->p[i]);
9620 map->p[i] = bmap;
9623 map = sort_and_remove_duplicates(map);
9624 if (map)
9625 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9626 return map;
9627 error:
9628 isl_map_free(map);
9629 return NULL;
9632 struct isl_set *isl_set_normalize(struct isl_set *set)
9634 return set_from_map(isl_map_normalize(set_to_map(set)));
9637 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9638 __isl_keep isl_map *map2)
9640 int i;
9641 isl_bool equal;
9643 if (!map1 || !map2)
9644 return isl_bool_error;
9646 if (map1 == map2)
9647 return isl_bool_true;
9648 if (!isl_space_is_equal(map1->dim, map2->dim))
9649 return isl_bool_false;
9651 map1 = isl_map_copy(map1);
9652 map2 = isl_map_copy(map2);
9653 map1 = isl_map_normalize(map1);
9654 map2 = isl_map_normalize(map2);
9655 if (!map1 || !map2)
9656 goto error;
9657 equal = map1->n == map2->n;
9658 for (i = 0; equal && i < map1->n; ++i) {
9659 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9660 if (equal < 0)
9661 goto error;
9663 isl_map_free(map1);
9664 isl_map_free(map2);
9665 return equal;
9666 error:
9667 isl_map_free(map1);
9668 isl_map_free(map2);
9669 return isl_bool_error;
9672 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9673 __isl_keep isl_set *set2)
9675 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9678 /* Return the basic maps in "map" as a list.
9680 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9681 __isl_keep isl_map *map)
9683 int i;
9684 isl_ctx *ctx;
9685 isl_basic_map_list *list;
9687 if (!map)
9688 return NULL;
9689 ctx = isl_map_get_ctx(map);
9690 list = isl_basic_map_list_alloc(ctx, map->n);
9692 for (i = 0; i < map->n; ++i) {
9693 isl_basic_map *bmap;
9695 bmap = isl_basic_map_copy(map->p[i]);
9696 list = isl_basic_map_list_add(list, bmap);
9699 return list;
9702 /* Return the intersection of the elements in the non-empty list "list".
9703 * All elements are assumed to live in the same space.
9705 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9706 __isl_take isl_basic_map_list *list)
9708 int i, n;
9709 isl_basic_map *bmap;
9711 if (!list)
9712 return NULL;
9713 n = isl_basic_map_list_n_basic_map(list);
9714 if (n < 1)
9715 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9716 "expecting non-empty list", goto error);
9718 bmap = isl_basic_map_list_get_basic_map(list, 0);
9719 for (i = 1; i < n; ++i) {
9720 isl_basic_map *bmap_i;
9722 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9723 bmap = isl_basic_map_intersect(bmap, bmap_i);
9726 isl_basic_map_list_free(list);
9727 return bmap;
9728 error:
9729 isl_basic_map_list_free(list);
9730 return NULL;
9733 /* Return the intersection of the elements in the non-empty list "list".
9734 * All elements are assumed to live in the same space.
9736 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9737 __isl_take isl_basic_set_list *list)
9739 return isl_basic_map_list_intersect(list);
9742 /* Return the union of the elements of "list".
9743 * The list is required to have at least one element.
9745 __isl_give isl_set *isl_basic_set_list_union(
9746 __isl_take isl_basic_set_list *list)
9748 int i, n;
9749 isl_space *space;
9750 isl_basic_set *bset;
9751 isl_set *set;
9753 if (!list)
9754 return NULL;
9755 n = isl_basic_set_list_n_basic_set(list);
9756 if (n < 1)
9757 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9758 "expecting non-empty list", goto error);
9760 bset = isl_basic_set_list_get_basic_set(list, 0);
9761 space = isl_basic_set_get_space(bset);
9762 isl_basic_set_free(bset);
9764 set = isl_set_alloc_space(space, n, 0);
9765 for (i = 0; i < n; ++i) {
9766 bset = isl_basic_set_list_get_basic_set(list, i);
9767 set = isl_set_add_basic_set(set, bset);
9770 isl_basic_set_list_free(list);
9771 return set;
9772 error:
9773 isl_basic_set_list_free(list);
9774 return NULL;
9777 /* Return the union of the elements in the non-empty list "list".
9778 * All elements are assumed to live in the same space.
9780 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9782 int i, n;
9783 isl_set *set;
9785 if (!list)
9786 return NULL;
9787 n = isl_set_list_n_set(list);
9788 if (n < 1)
9789 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9790 "expecting non-empty list", goto error);
9792 set = isl_set_list_get_set(list, 0);
9793 for (i = 1; i < n; ++i) {
9794 isl_set *set_i;
9796 set_i = isl_set_list_get_set(list, i);
9797 set = isl_set_union(set, set_i);
9800 isl_set_list_free(list);
9801 return set;
9802 error:
9803 isl_set_list_free(list);
9804 return NULL;
9807 __isl_give isl_basic_map *isl_basic_map_product(
9808 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9810 isl_space *space_result = NULL;
9811 struct isl_basic_map *bmap;
9812 unsigned in1, in2, out1, out2, nparam, total, pos;
9813 struct isl_dim_map *dim_map1, *dim_map2;
9815 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9816 goto error;
9817 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9818 isl_space_copy(bmap2->dim));
9820 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9821 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9822 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9823 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9824 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9826 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9827 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9828 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9829 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9830 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9831 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9832 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9833 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9834 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9835 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9836 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9838 bmap = isl_basic_map_alloc_space(space_result,
9839 bmap1->n_div + bmap2->n_div,
9840 bmap1->n_eq + bmap2->n_eq,
9841 bmap1->n_ineq + bmap2->n_ineq);
9842 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9843 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9844 bmap = isl_basic_map_simplify(bmap);
9845 return isl_basic_map_finalize(bmap);
9846 error:
9847 isl_basic_map_free(bmap1);
9848 isl_basic_map_free(bmap2);
9849 return NULL;
9852 __isl_give isl_basic_map *isl_basic_map_flat_product(
9853 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9855 isl_basic_map *prod;
9857 prod = isl_basic_map_product(bmap1, bmap2);
9858 prod = isl_basic_map_flatten(prod);
9859 return prod;
9862 __isl_give isl_basic_set *isl_basic_set_flat_product(
9863 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9865 return isl_basic_map_flat_range_product(bset1, bset2);
9868 __isl_give isl_basic_map *isl_basic_map_domain_product(
9869 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9871 isl_space *space1, *space2;
9872 isl_space *space_result = NULL;
9873 isl_basic_map *bmap;
9874 unsigned in1, in2, out, nparam, total, pos;
9875 struct isl_dim_map *dim_map1, *dim_map2;
9877 if (!bmap1 || !bmap2)
9878 goto error;
9880 space1 = isl_basic_map_get_space(bmap1);
9881 space2 = isl_basic_map_get_space(bmap2);
9882 space_result = isl_space_domain_product(space1, space2);
9884 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9885 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9886 out = isl_basic_map_dim(bmap1, isl_dim_out);
9887 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9889 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9890 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9891 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9892 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9893 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9894 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9895 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9896 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9897 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9898 isl_dim_map_div(dim_map1, bmap1, pos += out);
9899 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9901 bmap = isl_basic_map_alloc_space(space_result,
9902 bmap1->n_div + bmap2->n_div,
9903 bmap1->n_eq + bmap2->n_eq,
9904 bmap1->n_ineq + bmap2->n_ineq);
9905 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9906 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9907 bmap = isl_basic_map_simplify(bmap);
9908 return isl_basic_map_finalize(bmap);
9909 error:
9910 isl_basic_map_free(bmap1);
9911 isl_basic_map_free(bmap2);
9912 return NULL;
9915 __isl_give isl_basic_map *isl_basic_map_range_product(
9916 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9918 isl_bool rational;
9919 isl_space *space_result = NULL;
9920 isl_basic_map *bmap;
9921 unsigned in, out1, out2, nparam, total, pos;
9922 struct isl_dim_map *dim_map1, *dim_map2;
9924 rational = isl_basic_map_is_rational(bmap1);
9925 if (rational >= 0 && rational)
9926 rational = isl_basic_map_is_rational(bmap2);
9927 if (!bmap1 || !bmap2 || rational < 0)
9928 goto error;
9930 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9931 goto error;
9933 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9934 isl_space_copy(bmap2->dim));
9936 in = isl_basic_map_dim(bmap1, isl_dim_in);
9937 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9938 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9939 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9941 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9942 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9943 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9944 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9945 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9946 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9947 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9948 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9949 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9950 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9951 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9953 bmap = isl_basic_map_alloc_space(space_result,
9954 bmap1->n_div + bmap2->n_div,
9955 bmap1->n_eq + bmap2->n_eq,
9956 bmap1->n_ineq + bmap2->n_ineq);
9957 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9958 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9959 if (rational)
9960 bmap = isl_basic_map_set_rational(bmap);
9961 bmap = isl_basic_map_simplify(bmap);
9962 return isl_basic_map_finalize(bmap);
9963 error:
9964 isl_basic_map_free(bmap1);
9965 isl_basic_map_free(bmap2);
9966 return NULL;
9969 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9970 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9972 isl_basic_map *prod;
9974 prod = isl_basic_map_range_product(bmap1, bmap2);
9975 prod = isl_basic_map_flatten_range(prod);
9976 return prod;
9979 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9980 * and collect the results.
9981 * The result live in the space obtained by calling "space_product"
9982 * on the spaces of "map1" and "map2".
9983 * If "remove_duplicates" is set then the result may contain duplicates
9984 * (even if the inputs do not) and so we try and remove the obvious
9985 * duplicates.
9987 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9988 __isl_take isl_map *map2,
9989 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9990 __isl_take isl_space *right),
9991 __isl_give isl_basic_map *(*basic_map_product)(
9992 __isl_take isl_basic_map *left,
9993 __isl_take isl_basic_map *right),
9994 int remove_duplicates)
9996 unsigned flags = 0;
9997 struct isl_map *result;
9998 int i, j;
9999 isl_bool m;
10001 m = isl_map_has_equal_params(map1, map2);
10002 if (m < 0)
10003 goto error;
10004 if (!m)
10005 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10006 "parameters don't match", goto error);
10008 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10009 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10010 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10012 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10013 isl_space_copy(map2->dim)),
10014 map1->n * map2->n, flags);
10015 if (!result)
10016 goto error;
10017 for (i = 0; i < map1->n; ++i)
10018 for (j = 0; j < map2->n; ++j) {
10019 struct isl_basic_map *part;
10020 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10021 isl_basic_map_copy(map2->p[j]));
10022 if (isl_basic_map_is_empty(part))
10023 isl_basic_map_free(part);
10024 else
10025 result = isl_map_add_basic_map(result, part);
10026 if (!result)
10027 goto error;
10029 if (remove_duplicates)
10030 result = isl_map_remove_obvious_duplicates(result);
10031 isl_map_free(map1);
10032 isl_map_free(map2);
10033 return result;
10034 error:
10035 isl_map_free(map1);
10036 isl_map_free(map2);
10037 return NULL;
10040 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10042 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10043 __isl_take isl_map *map2)
10045 return map_product(map1, map2, &isl_space_product,
10046 &isl_basic_map_product, 0);
10049 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10050 __isl_take isl_map *map2)
10052 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10055 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10057 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10058 __isl_take isl_map *map2)
10060 isl_map *prod;
10062 prod = isl_map_product(map1, map2);
10063 prod = isl_map_flatten(prod);
10064 return prod;
10067 /* Given two set A and B, construct its Cartesian product A x B.
10069 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10071 return isl_map_range_product(set1, set2);
10074 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10075 __isl_take isl_set *set2)
10077 return isl_map_flat_range_product(set1, set2);
10080 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10082 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10083 __isl_take isl_map *map2)
10085 return map_product(map1, map2, &isl_space_domain_product,
10086 &isl_basic_map_domain_product, 1);
10089 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10091 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10092 __isl_take isl_map *map2)
10094 return map_product(map1, map2, &isl_space_range_product,
10095 &isl_basic_map_range_product, 1);
10098 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10099 __isl_take isl_map *map2)
10101 return isl_map_align_params_map_map_and(map1, map2,
10102 &map_domain_product_aligned);
10105 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10106 __isl_take isl_map *map2)
10108 return isl_map_align_params_map_map_and(map1, map2,
10109 &map_range_product_aligned);
10112 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10114 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10116 isl_space *space;
10117 int total1, keep1, total2, keep2;
10119 if (!map)
10120 return NULL;
10121 if (!isl_space_domain_is_wrapping(map->dim) ||
10122 !isl_space_range_is_wrapping(map->dim))
10123 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10124 "not a product", return isl_map_free(map));
10126 space = isl_map_get_space(map);
10127 total1 = isl_space_dim(space, isl_dim_in);
10128 total2 = isl_space_dim(space, isl_dim_out);
10129 space = isl_space_factor_domain(space);
10130 keep1 = isl_space_dim(space, isl_dim_in);
10131 keep2 = isl_space_dim(space, isl_dim_out);
10132 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10133 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10134 map = isl_map_reset_space(map, space);
10136 return map;
10139 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10141 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10143 isl_space *space;
10144 int total1, keep1, total2, keep2;
10146 if (!map)
10147 return NULL;
10148 if (!isl_space_domain_is_wrapping(map->dim) ||
10149 !isl_space_range_is_wrapping(map->dim))
10150 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10151 "not a product", return isl_map_free(map));
10153 space = isl_map_get_space(map);
10154 total1 = isl_space_dim(space, isl_dim_in);
10155 total2 = isl_space_dim(space, isl_dim_out);
10156 space = isl_space_factor_range(space);
10157 keep1 = isl_space_dim(space, isl_dim_in);
10158 keep2 = isl_space_dim(space, isl_dim_out);
10159 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10160 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10161 map = isl_map_reset_space(map, space);
10163 return map;
10166 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10168 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10170 isl_space *space;
10171 int total, keep;
10173 if (!map)
10174 return NULL;
10175 if (!isl_space_domain_is_wrapping(map->dim))
10176 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10177 "domain is not a product", return isl_map_free(map));
10179 space = isl_map_get_space(map);
10180 total = isl_space_dim(space, isl_dim_in);
10181 space = isl_space_domain_factor_domain(space);
10182 keep = isl_space_dim(space, isl_dim_in);
10183 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10184 map = isl_map_reset_space(map, space);
10186 return map;
10189 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10191 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10193 isl_space *space;
10194 int total, keep;
10196 if (!map)
10197 return NULL;
10198 if (!isl_space_domain_is_wrapping(map->dim))
10199 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10200 "domain is not a product", return isl_map_free(map));
10202 space = isl_map_get_space(map);
10203 total = isl_space_dim(space, isl_dim_in);
10204 space = isl_space_domain_factor_range(space);
10205 keep = isl_space_dim(space, isl_dim_in);
10206 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10207 map = isl_map_reset_space(map, space);
10209 return map;
10212 /* Given a map A -> [B -> C], extract the map A -> B.
10214 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10216 isl_space *space;
10217 int total, keep;
10219 if (!map)
10220 return NULL;
10221 if (!isl_space_range_is_wrapping(map->dim))
10222 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10223 "range is not a product", return isl_map_free(map));
10225 space = isl_map_get_space(map);
10226 total = isl_space_dim(space, isl_dim_out);
10227 space = isl_space_range_factor_domain(space);
10228 keep = isl_space_dim(space, isl_dim_out);
10229 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10230 map = isl_map_reset_space(map, space);
10232 return map;
10235 /* Given a map A -> [B -> C], extract the map A -> C.
10237 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10239 isl_space *space;
10240 int total, keep;
10242 if (!map)
10243 return NULL;
10244 if (!isl_space_range_is_wrapping(map->dim))
10245 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10246 "range is not a product", return isl_map_free(map));
10248 space = isl_map_get_space(map);
10249 total = isl_space_dim(space, isl_dim_out);
10250 space = isl_space_range_factor_range(space);
10251 keep = isl_space_dim(space, isl_dim_out);
10252 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10253 map = isl_map_reset_space(map, space);
10255 return map;
10258 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10260 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10261 __isl_take isl_map *map2)
10263 isl_map *prod;
10265 prod = isl_map_domain_product(map1, map2);
10266 prod = isl_map_flatten_domain(prod);
10267 return prod;
10270 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10272 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10273 __isl_take isl_map *map2)
10275 isl_map *prod;
10277 prod = isl_map_range_product(map1, map2);
10278 prod = isl_map_flatten_range(prod);
10279 return prod;
10282 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10284 int i;
10285 uint32_t hash = isl_hash_init();
10286 unsigned total;
10288 if (!bmap)
10289 return 0;
10290 bmap = isl_basic_map_copy(bmap);
10291 bmap = isl_basic_map_normalize(bmap);
10292 if (!bmap)
10293 return 0;
10294 total = isl_basic_map_total_dim(bmap);
10295 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10296 for (i = 0; i < bmap->n_eq; ++i) {
10297 uint32_t c_hash;
10298 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10299 isl_hash_hash(hash, c_hash);
10301 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10302 for (i = 0; i < bmap->n_ineq; ++i) {
10303 uint32_t c_hash;
10304 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10305 isl_hash_hash(hash, c_hash);
10307 isl_hash_byte(hash, bmap->n_div & 0xFF);
10308 for (i = 0; i < bmap->n_div; ++i) {
10309 uint32_t c_hash;
10310 if (isl_int_is_zero(bmap->div[i][0]))
10311 continue;
10312 isl_hash_byte(hash, i & 0xFF);
10313 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10314 isl_hash_hash(hash, c_hash);
10316 isl_basic_map_free(bmap);
10317 return hash;
10320 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10322 return isl_basic_map_get_hash(bset_to_bmap(bset));
10325 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10327 int i;
10328 uint32_t hash;
10330 if (!map)
10331 return 0;
10332 map = isl_map_copy(map);
10333 map = isl_map_normalize(map);
10334 if (!map)
10335 return 0;
10337 hash = isl_hash_init();
10338 for (i = 0; i < map->n; ++i) {
10339 uint32_t bmap_hash;
10340 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10341 isl_hash_hash(hash, bmap_hash);
10344 isl_map_free(map);
10346 return hash;
10349 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10351 return isl_map_get_hash(set_to_map(set));
10354 /* Return the number of basic maps in the (current) representation of "map".
10356 int isl_map_n_basic_map(__isl_keep isl_map *map)
10358 return map ? map->n : 0;
10361 int isl_set_n_basic_set(__isl_keep isl_set *set)
10363 return set ? set->n : 0;
10366 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10367 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10369 int i;
10371 if (!map)
10372 return isl_stat_error;
10374 for (i = 0; i < map->n; ++i)
10375 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10376 return isl_stat_error;
10378 return isl_stat_ok;
10381 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10382 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10384 int i;
10386 if (!set)
10387 return isl_stat_error;
10389 for (i = 0; i < set->n; ++i)
10390 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10391 return isl_stat_error;
10393 return isl_stat_ok;
10396 /* Return a list of basic sets, the union of which is equal to "set".
10398 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10399 __isl_keep isl_set *set)
10401 int i;
10402 isl_basic_set_list *list;
10404 if (!set)
10405 return NULL;
10407 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10408 for (i = 0; i < set->n; ++i) {
10409 isl_basic_set *bset;
10411 bset = isl_basic_set_copy(set->p[i]);
10412 list = isl_basic_set_list_add(list, bset);
10415 return list;
10418 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10420 isl_space *space;
10422 if (!bset)
10423 return NULL;
10425 bset = isl_basic_set_cow(bset);
10426 if (!bset)
10427 return NULL;
10429 space = isl_basic_set_get_space(bset);
10430 space = isl_space_lift(space, bset->n_div);
10431 if (!space)
10432 goto error;
10433 isl_space_free(bset->dim);
10434 bset->dim = space;
10435 bset->extra -= bset->n_div;
10436 bset->n_div = 0;
10438 bset = isl_basic_set_finalize(bset);
10440 return bset;
10441 error:
10442 isl_basic_set_free(bset);
10443 return NULL;
10446 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10448 int i;
10449 isl_space *space;
10450 unsigned n_div;
10452 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10454 if (!set)
10455 return NULL;
10457 set = isl_set_cow(set);
10458 if (!set)
10459 return NULL;
10461 n_div = set->p[0]->n_div;
10462 space = isl_set_get_space(set);
10463 space = isl_space_lift(space, n_div);
10464 if (!space)
10465 goto error;
10466 isl_space_free(set->dim);
10467 set->dim = space;
10469 for (i = 0; i < set->n; ++i) {
10470 set->p[i] = isl_basic_set_lift(set->p[i]);
10471 if (!set->p[i])
10472 goto error;
10475 return set;
10476 error:
10477 isl_set_free(set);
10478 return NULL;
10481 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10483 unsigned dim;
10484 int size = 0;
10486 if (!bset)
10487 return -1;
10489 dim = isl_basic_set_total_dim(bset);
10490 size += bset->n_eq * (1 + dim);
10491 size += bset->n_ineq * (1 + dim);
10492 size += bset->n_div * (2 + dim);
10494 return size;
10497 int isl_set_size(__isl_keep isl_set *set)
10499 int i;
10500 int size = 0;
10502 if (!set)
10503 return -1;
10505 for (i = 0; i < set->n; ++i)
10506 size += isl_basic_set_size(set->p[i]);
10508 return size;
10511 /* Check if there is any lower bound (if lower == 0) and/or upper
10512 * bound (if upper == 0) on the specified dim.
10514 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10515 enum isl_dim_type type, unsigned pos, int lower, int upper)
10517 int i;
10519 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10520 return isl_bool_error;
10522 pos += isl_basic_map_offset(bmap, type);
10524 for (i = 0; i < bmap->n_div; ++i) {
10525 if (isl_int_is_zero(bmap->div[i][0]))
10526 continue;
10527 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10528 return isl_bool_true;
10531 for (i = 0; i < bmap->n_eq; ++i)
10532 if (!isl_int_is_zero(bmap->eq[i][pos]))
10533 return isl_bool_true;
10535 for (i = 0; i < bmap->n_ineq; ++i) {
10536 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10537 if (sgn > 0)
10538 lower = 1;
10539 if (sgn < 0)
10540 upper = 1;
10543 return lower && upper;
10546 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10547 enum isl_dim_type type, unsigned pos)
10549 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10552 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10553 enum isl_dim_type type, unsigned pos)
10555 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10558 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10559 enum isl_dim_type type, unsigned pos)
10561 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10564 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10565 enum isl_dim_type type, unsigned pos)
10567 int i;
10569 if (!map)
10570 return isl_bool_error;
10572 for (i = 0; i < map->n; ++i) {
10573 isl_bool bounded;
10574 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10575 if (bounded < 0 || !bounded)
10576 return bounded;
10579 return isl_bool_true;
10582 /* Return true if the specified dim is involved in both an upper bound
10583 * and a lower bound.
10585 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10586 enum isl_dim_type type, unsigned pos)
10588 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10591 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10593 static isl_bool has_any_bound(__isl_keep isl_map *map,
10594 enum isl_dim_type type, unsigned pos,
10595 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10596 enum isl_dim_type type, unsigned pos))
10598 int i;
10600 if (!map)
10601 return isl_bool_error;
10603 for (i = 0; i < map->n; ++i) {
10604 isl_bool bounded;
10605 bounded = fn(map->p[i], type, pos);
10606 if (bounded < 0 || bounded)
10607 return bounded;
10610 return isl_bool_false;
10613 /* Return 1 if the specified dim is involved in any lower bound.
10615 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10616 enum isl_dim_type type, unsigned pos)
10618 return has_any_bound(set, type, pos,
10619 &isl_basic_map_dim_has_lower_bound);
10622 /* Return 1 if the specified dim is involved in any upper bound.
10624 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10625 enum isl_dim_type type, unsigned pos)
10627 return has_any_bound(set, type, pos,
10628 &isl_basic_map_dim_has_upper_bound);
10631 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10633 static isl_bool has_bound(__isl_keep isl_map *map,
10634 enum isl_dim_type type, unsigned pos,
10635 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10636 enum isl_dim_type type, unsigned pos))
10638 int i;
10640 if (!map)
10641 return isl_bool_error;
10643 for (i = 0; i < map->n; ++i) {
10644 isl_bool bounded;
10645 bounded = fn(map->p[i], type, pos);
10646 if (bounded < 0 || !bounded)
10647 return bounded;
10650 return isl_bool_true;
10653 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10655 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10656 enum isl_dim_type type, unsigned pos)
10658 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10661 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10663 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10664 enum isl_dim_type type, unsigned pos)
10666 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10669 /* For each of the "n" variables starting at "first", determine
10670 * the sign of the variable and put the results in the first "n"
10671 * elements of the array "signs".
10672 * Sign
10673 * 1 means that the variable is non-negative
10674 * -1 means that the variable is non-positive
10675 * 0 means the variable attains both positive and negative values.
10677 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10678 unsigned first, unsigned n, int *signs)
10680 isl_vec *bound = NULL;
10681 struct isl_tab *tab = NULL;
10682 struct isl_tab_undo *snap;
10683 int i;
10685 if (!bset || !signs)
10686 return isl_stat_error;
10688 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10689 tab = isl_tab_from_basic_set(bset, 0);
10690 if (!bound || !tab)
10691 goto error;
10693 isl_seq_clr(bound->el, bound->size);
10694 isl_int_set_si(bound->el[0], -1);
10696 snap = isl_tab_snap(tab);
10697 for (i = 0; i < n; ++i) {
10698 int empty;
10700 isl_int_set_si(bound->el[1 + first + i], -1);
10701 if (isl_tab_add_ineq(tab, bound->el) < 0)
10702 goto error;
10703 empty = tab->empty;
10704 isl_int_set_si(bound->el[1 + first + i], 0);
10705 if (isl_tab_rollback(tab, snap) < 0)
10706 goto error;
10708 if (empty) {
10709 signs[i] = 1;
10710 continue;
10713 isl_int_set_si(bound->el[1 + first + i], 1);
10714 if (isl_tab_add_ineq(tab, bound->el) < 0)
10715 goto error;
10716 empty = tab->empty;
10717 isl_int_set_si(bound->el[1 + first + i], 0);
10718 if (isl_tab_rollback(tab, snap) < 0)
10719 goto error;
10721 signs[i] = empty ? -1 : 0;
10724 isl_tab_free(tab);
10725 isl_vec_free(bound);
10726 return isl_stat_ok;
10727 error:
10728 isl_tab_free(tab);
10729 isl_vec_free(bound);
10730 return isl_stat_error;
10733 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10734 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10736 if (!bset || !signs)
10737 return isl_stat_error;
10738 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10739 return isl_stat_error;
10741 first += pos(bset->dim, type) - 1;
10742 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10745 /* Is it possible for the integer division "div" to depend (possibly
10746 * indirectly) on any output dimensions?
10748 * If the div is undefined, then we conservatively assume that it
10749 * may depend on them.
10750 * Otherwise, we check if it actually depends on them or on any integer
10751 * divisions that may depend on them.
10753 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10755 int i;
10756 unsigned n_out, o_out;
10757 unsigned n_div, o_div;
10759 if (isl_int_is_zero(bmap->div[div][0]))
10760 return isl_bool_true;
10762 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10763 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10765 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10766 return isl_bool_true;
10768 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10769 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10771 for (i = 0; i < n_div; ++i) {
10772 isl_bool may_involve;
10774 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10775 continue;
10776 may_involve = div_may_involve_output(bmap, i);
10777 if (may_involve < 0 || may_involve)
10778 return may_involve;
10781 return isl_bool_false;
10784 /* Return the first integer division of "bmap" in the range
10785 * [first, first + n[ that may depend on any output dimensions and
10786 * that has a non-zero coefficient in "c" (where the first coefficient
10787 * in "c" corresponds to integer division "first").
10789 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10790 isl_int *c, int first, int n)
10792 int k;
10794 if (!bmap)
10795 return -1;
10797 for (k = first; k < first + n; ++k) {
10798 isl_bool may_involve;
10800 if (isl_int_is_zero(c[k]))
10801 continue;
10802 may_involve = div_may_involve_output(bmap, k);
10803 if (may_involve < 0)
10804 return -1;
10805 if (may_involve)
10806 return k;
10809 return first + n;
10812 /* Look for a pair of inequality constraints in "bmap" of the form
10814 * -l + i >= 0 or i >= l
10815 * and
10816 * n + l - i >= 0 or i <= l + n
10818 * with n < "m" and i the output dimension at position "pos".
10819 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10820 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10821 * and earlier output dimensions, as well as integer divisions that do
10822 * not involve any of the output dimensions.
10824 * Return the index of the first inequality constraint or bmap->n_ineq
10825 * if no such pair can be found.
10827 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10828 int pos, isl_int m)
10830 int i, j;
10831 isl_ctx *ctx;
10832 unsigned total;
10833 unsigned n_div, o_div;
10834 unsigned n_out, o_out;
10835 int less;
10837 if (!bmap)
10838 return -1;
10840 ctx = isl_basic_map_get_ctx(bmap);
10841 total = isl_basic_map_total_dim(bmap);
10842 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10843 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10844 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10845 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10846 for (i = 0; i < bmap->n_ineq; ++i) {
10847 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10848 continue;
10849 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10850 n_out - (pos + 1)) != -1)
10851 continue;
10852 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10853 0, n_div) < n_div)
10854 continue;
10855 for (j = i + 1; j < bmap->n_ineq; ++j) {
10856 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10857 ctx->one))
10858 continue;
10859 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10860 bmap->ineq[j] + 1, total))
10861 continue;
10862 break;
10864 if (j >= bmap->n_ineq)
10865 continue;
10866 isl_int_add(bmap->ineq[i][0],
10867 bmap->ineq[i][0], bmap->ineq[j][0]);
10868 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10869 isl_int_sub(bmap->ineq[i][0],
10870 bmap->ineq[i][0], bmap->ineq[j][0]);
10871 if (!less)
10872 continue;
10873 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10874 return i;
10875 else
10876 return j;
10879 return bmap->n_ineq;
10882 /* Return the index of the equality of "bmap" that defines
10883 * the output dimension "pos" in terms of earlier dimensions.
10884 * The equality may also involve integer divisions, as long
10885 * as those integer divisions are defined in terms of
10886 * parameters or input dimensions.
10887 * In this case, *div is set to the number of integer divisions and
10888 * *ineq is set to the number of inequality constraints (provided
10889 * div and ineq are not NULL).
10891 * The equality may also involve a single integer division involving
10892 * the output dimensions (typically only output dimension "pos") as
10893 * long as the coefficient of output dimension "pos" is 1 or -1 and
10894 * there is a pair of constraints i >= l and i <= l + n, with i referring
10895 * to output dimension "pos", l an expression involving only earlier
10896 * dimensions and n smaller than the coefficient of the integer division
10897 * in the equality. In this case, the output dimension can be defined
10898 * in terms of a modulo expression that does not involve the integer division.
10899 * *div is then set to this single integer division and
10900 * *ineq is set to the index of constraint i >= l.
10902 * Return bmap->n_eq if there is no such equality.
10903 * Return -1 on error.
10905 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10906 int pos, int *div, int *ineq)
10908 int j, k, l;
10909 unsigned n_out, o_out;
10910 unsigned n_div, o_div;
10912 if (!bmap)
10913 return -1;
10915 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10916 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10917 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10918 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10920 if (ineq)
10921 *ineq = bmap->n_ineq;
10922 if (div)
10923 *div = n_div;
10924 for (j = 0; j < bmap->n_eq; ++j) {
10925 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10926 continue;
10927 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10928 n_out - (pos + 1)) != -1)
10929 continue;
10930 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10931 0, n_div);
10932 if (k >= n_div)
10933 return j;
10934 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10935 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10936 continue;
10937 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10938 k + 1, n_div - (k+1)) < n_div)
10939 continue;
10940 l = find_modulo_constraint_pair(bmap, pos,
10941 bmap->eq[j][o_div + k]);
10942 if (l < 0)
10943 return -1;
10944 if (l >= bmap->n_ineq)
10945 continue;
10946 if (div)
10947 *div = k;
10948 if (ineq)
10949 *ineq = l;
10950 return j;
10953 return bmap->n_eq;
10956 /* Check if the given basic map is obviously single-valued.
10957 * In particular, for each output dimension, check that there is
10958 * an equality that defines the output dimension in terms of
10959 * earlier dimensions.
10961 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10963 int i;
10964 unsigned n_out;
10966 if (!bmap)
10967 return isl_bool_error;
10969 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10971 for (i = 0; i < n_out; ++i) {
10972 int eq;
10974 eq = isl_basic_map_output_defining_equality(bmap, i,
10975 NULL, NULL);
10976 if (eq < 0)
10977 return isl_bool_error;
10978 if (eq >= bmap->n_eq)
10979 return isl_bool_false;
10982 return isl_bool_true;
10985 /* Check if the given basic map is single-valued.
10986 * We simply compute
10988 * M \circ M^-1
10990 * and check if the result is a subset of the identity mapping.
10992 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10994 isl_space *space;
10995 isl_basic_map *test;
10996 isl_basic_map *id;
10997 isl_bool sv;
10999 sv = isl_basic_map_plain_is_single_valued(bmap);
11000 if (sv < 0 || sv)
11001 return sv;
11003 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11004 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11006 space = isl_basic_map_get_space(bmap);
11007 space = isl_space_map_from_set(isl_space_range(space));
11008 id = isl_basic_map_identity(space);
11010 sv = isl_basic_map_is_subset(test, id);
11012 isl_basic_map_free(test);
11013 isl_basic_map_free(id);
11015 return sv;
11018 /* Check if the given map is obviously single-valued.
11020 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11022 if (!map)
11023 return isl_bool_error;
11024 if (map->n == 0)
11025 return isl_bool_true;
11026 if (map->n >= 2)
11027 return isl_bool_false;
11029 return isl_basic_map_plain_is_single_valued(map->p[0]);
11032 /* Check if the given map is single-valued.
11033 * We simply compute
11035 * M \circ M^-1
11037 * and check if the result is a subset of the identity mapping.
11039 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11041 isl_space *dim;
11042 isl_map *test;
11043 isl_map *id;
11044 isl_bool sv;
11046 sv = isl_map_plain_is_single_valued(map);
11047 if (sv < 0 || sv)
11048 return sv;
11050 test = isl_map_reverse(isl_map_copy(map));
11051 test = isl_map_apply_range(test, isl_map_copy(map));
11053 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11054 id = isl_map_identity(dim);
11056 sv = isl_map_is_subset(test, id);
11058 isl_map_free(test);
11059 isl_map_free(id);
11061 return sv;
11064 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11066 isl_bool in;
11068 map = isl_map_copy(map);
11069 map = isl_map_reverse(map);
11070 in = isl_map_is_single_valued(map);
11071 isl_map_free(map);
11073 return in;
11076 /* Check if the given map is obviously injective.
11078 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11080 isl_bool in;
11082 map = isl_map_copy(map);
11083 map = isl_map_reverse(map);
11084 in = isl_map_plain_is_single_valued(map);
11085 isl_map_free(map);
11087 return in;
11090 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11092 isl_bool sv;
11094 sv = isl_map_is_single_valued(map);
11095 if (sv < 0 || !sv)
11096 return sv;
11098 return isl_map_is_injective(map);
11101 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11103 return isl_map_is_single_valued(set_to_map(set));
11106 /* Does "map" only map elements to themselves?
11108 * If the domain and range spaces are different, then "map"
11109 * is considered not to be an identity relation, even if it is empty.
11110 * Otherwise, construct the maximal identity relation and
11111 * check whether "map" is a subset of this relation.
11113 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11115 isl_space *space;
11116 isl_map *id;
11117 isl_bool equal, is_identity;
11119 space = isl_map_get_space(map);
11120 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11121 isl_space_free(space);
11122 if (equal < 0 || !equal)
11123 return equal;
11125 id = isl_map_identity(isl_map_get_space(map));
11126 is_identity = isl_map_is_subset(map, id);
11127 isl_map_free(id);
11129 return is_identity;
11132 int isl_map_is_translation(__isl_keep isl_map *map)
11134 int ok;
11135 isl_set *delta;
11137 delta = isl_map_deltas(isl_map_copy(map));
11138 ok = isl_set_is_singleton(delta);
11139 isl_set_free(delta);
11141 return ok;
11144 static int unique(isl_int *p, unsigned pos, unsigned len)
11146 if (isl_seq_first_non_zero(p, pos) != -1)
11147 return 0;
11148 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11149 return 0;
11150 return 1;
11153 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11155 int i, j;
11156 unsigned nvar;
11157 unsigned ovar;
11159 if (!bset)
11160 return isl_bool_error;
11162 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11163 return isl_bool_false;
11165 nvar = isl_basic_set_dim(bset, isl_dim_set);
11166 ovar = isl_space_offset(bset->dim, isl_dim_set);
11167 for (j = 0; j < nvar; ++j) {
11168 int lower = 0, upper = 0;
11169 for (i = 0; i < bset->n_eq; ++i) {
11170 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11171 continue;
11172 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11173 return isl_bool_false;
11174 break;
11176 if (i < bset->n_eq)
11177 continue;
11178 for (i = 0; i < bset->n_ineq; ++i) {
11179 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11180 continue;
11181 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11182 return isl_bool_false;
11183 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11184 lower = 1;
11185 else
11186 upper = 1;
11188 if (!lower || !upper)
11189 return isl_bool_false;
11192 return isl_bool_true;
11195 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11197 if (!set)
11198 return isl_bool_error;
11199 if (set->n != 1)
11200 return isl_bool_false;
11202 return isl_basic_set_is_box(set->p[0]);
11205 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11207 if (!bset)
11208 return isl_bool_error;
11210 return isl_space_is_wrapping(bset->dim);
11213 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11215 if (!set)
11216 return isl_bool_error;
11218 return isl_space_is_wrapping(set->dim);
11221 /* Modify the space of "map" through a call to "change".
11222 * If "can_change" is set (not NULL), then first call it to check
11223 * if the modification is allowed, printing the error message "cannot_change"
11224 * if it is not.
11226 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11227 isl_bool (*can_change)(__isl_keep isl_map *map),
11228 const char *cannot_change,
11229 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11231 isl_bool ok;
11232 isl_space *space;
11234 if (!map)
11235 return NULL;
11237 ok = can_change ? can_change(map) : isl_bool_true;
11238 if (ok < 0)
11239 return isl_map_free(map);
11240 if (!ok)
11241 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11242 return isl_map_free(map));
11244 space = change(isl_map_get_space(map));
11245 map = isl_map_reset_space(map, space);
11247 return map;
11250 /* Is the domain of "map" a wrapped relation?
11252 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11254 if (!map)
11255 return isl_bool_error;
11257 return isl_space_domain_is_wrapping(map->dim);
11260 /* Does "map" have a wrapped relation in both domain and range?
11262 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11264 return isl_space_is_product(isl_map_peek_space(map));
11267 /* Is the range of "map" a wrapped relation?
11269 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11271 if (!map)
11272 return isl_bool_error;
11274 return isl_space_range_is_wrapping(map->dim);
11277 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11279 bmap = isl_basic_map_cow(bmap);
11280 if (!bmap)
11281 return NULL;
11283 bmap->dim = isl_space_wrap(bmap->dim);
11284 if (!bmap->dim)
11285 goto error;
11287 bmap = isl_basic_map_finalize(bmap);
11289 return bset_from_bmap(bmap);
11290 error:
11291 isl_basic_map_free(bmap);
11292 return NULL;
11295 /* Given a map A -> B, return the set (A -> B).
11297 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11299 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11302 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11304 bset = isl_basic_set_cow(bset);
11305 if (!bset)
11306 return NULL;
11308 bset->dim = isl_space_unwrap(bset->dim);
11309 if (!bset->dim)
11310 goto error;
11312 bset = isl_basic_set_finalize(bset);
11314 return bset_to_bmap(bset);
11315 error:
11316 isl_basic_set_free(bset);
11317 return NULL;
11320 /* Given a set (A -> B), return the map A -> B.
11321 * Error out if "set" is not of the form (A -> B).
11323 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11325 return isl_map_change_space(set, &isl_set_is_wrapping,
11326 "not a wrapping set", &isl_space_unwrap);
11329 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11330 enum isl_dim_type type)
11332 if (!bmap)
11333 return NULL;
11335 if (!isl_space_is_named_or_nested(bmap->dim, type))
11336 return bmap;
11338 bmap = isl_basic_map_cow(bmap);
11339 if (!bmap)
11340 return NULL;
11342 bmap->dim = isl_space_reset(bmap->dim, type);
11343 if (!bmap->dim)
11344 goto error;
11346 bmap = isl_basic_map_finalize(bmap);
11348 return bmap;
11349 error:
11350 isl_basic_map_free(bmap);
11351 return NULL;
11354 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11355 enum isl_dim_type type)
11357 int i;
11359 if (!map)
11360 return NULL;
11362 if (!isl_space_is_named_or_nested(map->dim, type))
11363 return map;
11365 map = isl_map_cow(map);
11366 if (!map)
11367 return NULL;
11369 for (i = 0; i < map->n; ++i) {
11370 map->p[i] = isl_basic_map_reset(map->p[i], type);
11371 if (!map->p[i])
11372 goto error;
11374 map->dim = isl_space_reset(map->dim, type);
11375 if (!map->dim)
11376 goto error;
11378 return map;
11379 error:
11380 isl_map_free(map);
11381 return NULL;
11384 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11386 if (!bmap)
11387 return NULL;
11389 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11390 return bmap;
11392 bmap = isl_basic_map_cow(bmap);
11393 if (!bmap)
11394 return NULL;
11396 bmap->dim = isl_space_flatten(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_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11410 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11413 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11414 __isl_take isl_basic_map *bmap)
11416 if (!bmap)
11417 return NULL;
11419 if (!bmap->dim->nested[0])
11420 return bmap;
11422 bmap = isl_basic_map_cow(bmap);
11423 if (!bmap)
11424 return NULL;
11426 bmap->dim = isl_space_flatten_domain(bmap->dim);
11427 if (!bmap->dim)
11428 goto error;
11430 bmap = isl_basic_map_finalize(bmap);
11432 return bmap;
11433 error:
11434 isl_basic_map_free(bmap);
11435 return NULL;
11438 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11439 __isl_take isl_basic_map *bmap)
11441 if (!bmap)
11442 return NULL;
11444 if (!bmap->dim->nested[1])
11445 return bmap;
11447 bmap = isl_basic_map_cow(bmap);
11448 if (!bmap)
11449 return NULL;
11451 bmap->dim = isl_space_flatten_range(bmap->dim);
11452 if (!bmap->dim)
11453 goto error;
11455 bmap = isl_basic_map_finalize(bmap);
11457 return bmap;
11458 error:
11459 isl_basic_map_free(bmap);
11460 return NULL;
11463 /* Remove any internal structure from the spaces of domain and range of "map".
11465 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11467 if (!map)
11468 return NULL;
11470 if (!map->dim->nested[0] && !map->dim->nested[1])
11471 return map;
11473 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11476 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11478 return set_from_map(isl_map_flatten(set_to_map(set)));
11481 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11483 isl_space *space, *flat_space;
11484 isl_map *map;
11486 space = isl_set_get_space(set);
11487 flat_space = isl_space_flatten(isl_space_copy(space));
11488 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11489 flat_space));
11490 map = isl_map_intersect_domain(map, set);
11492 return map;
11495 /* Remove any internal structure from the space of the domain of "map".
11497 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11499 if (!map)
11500 return NULL;
11502 if (!map->dim->nested[0])
11503 return map;
11505 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11508 /* Remove any internal structure from the space of the range of "map".
11510 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11512 if (!map)
11513 return NULL;
11515 if (!map->dim->nested[1])
11516 return map;
11518 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11521 /* Reorder the dimensions of "bmap" according to the given dim_map
11522 * and set the dimension specification to "space" and
11523 * perform Gaussian elimination on the result.
11525 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11526 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11528 isl_basic_map *res;
11529 unsigned flags;
11530 unsigned n_div;
11532 if (!bmap || !space || !dim_map)
11533 goto error;
11535 flags = bmap->flags;
11536 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11537 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11538 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11539 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11540 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11541 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11542 if (res)
11543 res->flags = flags;
11544 res = isl_basic_map_gauss(res, NULL);
11545 res = isl_basic_map_finalize(res);
11546 return res;
11547 error:
11548 isl_dim_map_free(dim_map);
11549 isl_basic_map_free(bmap);
11550 isl_space_free(space);
11551 return NULL;
11554 /* Reorder the dimensions of "map" according to given reordering.
11556 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11557 __isl_take isl_reordering *r)
11559 int i;
11560 struct isl_dim_map *dim_map;
11562 map = isl_map_cow(map);
11563 dim_map = isl_dim_map_from_reordering(r);
11564 if (!map || !r || !dim_map)
11565 goto error;
11567 for (i = 0; i < map->n; ++i) {
11568 struct isl_dim_map *dim_map_i;
11569 isl_space *space;
11571 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11573 space = isl_reordering_get_space(r);
11574 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11576 if (!map->p[i])
11577 goto error;
11580 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11581 map = isl_map_unmark_normalized(map);
11583 isl_reordering_free(r);
11584 isl_dim_map_free(dim_map);
11585 return map;
11586 error:
11587 isl_dim_map_free(dim_map);
11588 isl_map_free(map);
11589 isl_reordering_free(r);
11590 return NULL;
11593 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11594 __isl_take isl_reordering *r)
11596 return set_from_map(isl_map_realign(set_to_map(set), r));
11599 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11600 __isl_take isl_space *model)
11602 isl_ctx *ctx;
11603 isl_bool aligned;
11605 if (!map || !model)
11606 goto error;
11608 ctx = isl_space_get_ctx(model);
11609 if (!isl_space_has_named_params(model))
11610 isl_die(ctx, isl_error_invalid,
11611 "model has unnamed parameters", goto error);
11612 if (isl_map_check_named_params(map) < 0)
11613 goto error;
11614 aligned = isl_map_space_has_equal_params(map, model);
11615 if (aligned < 0)
11616 goto error;
11617 if (!aligned) {
11618 isl_reordering *exp;
11620 exp = isl_parameter_alignment_reordering(map->dim, model);
11621 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11622 map = isl_map_realign(map, exp);
11625 isl_space_free(model);
11626 return map;
11627 error:
11628 isl_space_free(model);
11629 isl_map_free(map);
11630 return NULL;
11633 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11634 __isl_take isl_space *model)
11636 return isl_map_align_params(set, model);
11639 /* Align the parameters of "bmap" to those of "model", introducing
11640 * additional parameters if needed.
11642 __isl_give isl_basic_map *isl_basic_map_align_params(
11643 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11645 isl_ctx *ctx;
11646 isl_bool equal_params;
11648 if (!bmap || !model)
11649 goto error;
11651 ctx = isl_space_get_ctx(model);
11652 if (!isl_space_has_named_params(model))
11653 isl_die(ctx, isl_error_invalid,
11654 "model has unnamed parameters", goto error);
11655 if (isl_basic_map_check_named_params(bmap) < 0)
11656 goto error;
11657 equal_params = isl_space_has_equal_params(bmap->dim, model);
11658 if (equal_params < 0)
11659 goto error;
11660 if (!equal_params) {
11661 isl_reordering *exp;
11662 struct isl_dim_map *dim_map;
11664 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11665 exp = isl_reordering_extend_space(exp,
11666 isl_basic_map_get_space(bmap));
11667 dim_map = isl_dim_map_from_reordering(exp);
11668 bmap = isl_basic_map_realign(bmap,
11669 isl_reordering_get_space(exp),
11670 isl_dim_map_extend(dim_map, bmap));
11671 isl_reordering_free(exp);
11672 isl_dim_map_free(dim_map);
11675 isl_space_free(model);
11676 return bmap;
11677 error:
11678 isl_space_free(model);
11679 isl_basic_map_free(bmap);
11680 return NULL;
11683 /* Do "bset" and "space" have the same parameters?
11685 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11686 __isl_keep isl_space *space)
11688 isl_space *bset_space;
11690 bset_space = isl_basic_set_peek_space(bset);
11691 return isl_space_has_equal_params(bset_space, space);
11694 /* Do "map" and "space" have the same parameters?
11696 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11697 __isl_keep isl_space *space)
11699 isl_space *map_space;
11701 map_space = isl_map_peek_space(map);
11702 return isl_space_has_equal_params(map_space, space);
11705 /* Do "set" and "space" have the same parameters?
11707 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11708 __isl_keep isl_space *space)
11710 return isl_map_space_has_equal_params(set_to_map(set), space);
11713 /* Align the parameters of "bset" to those of "model", introducing
11714 * additional parameters if needed.
11716 __isl_give isl_basic_set *isl_basic_set_align_params(
11717 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11719 return isl_basic_map_align_params(bset, model);
11722 /* Drop all parameters not referenced by "map".
11724 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11726 int i;
11728 if (isl_map_check_named_params(map) < 0)
11729 return isl_map_free(map);
11731 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11732 isl_bool involves;
11734 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11735 if (involves < 0)
11736 return isl_map_free(map);
11737 if (!involves)
11738 map = isl_map_project_out(map, isl_dim_param, i, 1);
11741 return map;
11744 /* Drop all parameters not referenced by "set".
11746 __isl_give isl_set *isl_set_drop_unused_params(
11747 __isl_take isl_set *set)
11749 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11752 /* Drop all parameters not referenced by "bmap".
11754 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11755 __isl_take isl_basic_map *bmap)
11757 int i;
11759 if (isl_basic_map_check_named_params(bmap) < 0)
11760 return isl_basic_map_free(bmap);
11762 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11763 isl_bool involves;
11765 involves = isl_basic_map_involves_dims(bmap,
11766 isl_dim_param, i, 1);
11767 if (involves < 0)
11768 return isl_basic_map_free(bmap);
11769 if (!involves)
11770 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11773 return bmap;
11776 /* Drop all parameters not referenced by "bset".
11778 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11779 __isl_take isl_basic_set *bset)
11781 return bset_from_bmap(isl_basic_map_drop_unused_params(
11782 bset_to_bmap(bset)));
11785 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11786 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11787 enum isl_dim_type c2, enum isl_dim_type c3,
11788 enum isl_dim_type c4, enum isl_dim_type c5)
11790 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11791 struct isl_mat *mat;
11792 int i, j, k;
11793 int pos;
11795 if (!bmap)
11796 return NULL;
11797 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11798 isl_basic_map_total_dim(bmap) + 1);
11799 if (!mat)
11800 return NULL;
11801 for (i = 0; i < bmap->n_eq; ++i)
11802 for (j = 0, pos = 0; j < 5; ++j) {
11803 int off = isl_basic_map_offset(bmap, c[j]);
11804 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11805 isl_int_set(mat->row[i][pos],
11806 bmap->eq[i][off + k]);
11807 ++pos;
11811 return mat;
11814 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11815 __isl_keep isl_basic_map *bmap, 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 struct isl_mat *mat;
11821 int i, j, k;
11822 int pos;
11824 if (!bmap)
11825 return NULL;
11826 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11827 isl_basic_map_total_dim(bmap) + 1);
11828 if (!mat)
11829 return NULL;
11830 for (i = 0; i < bmap->n_ineq; ++i)
11831 for (j = 0, pos = 0; j < 5; ++j) {
11832 int off = isl_basic_map_offset(bmap, c[j]);
11833 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11834 isl_int_set(mat->row[i][pos],
11835 bmap->ineq[i][off + k]);
11836 ++pos;
11840 return mat;
11843 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11844 __isl_take isl_space *space,
11845 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11846 enum isl_dim_type c2, enum isl_dim_type c3,
11847 enum isl_dim_type c4, enum isl_dim_type c5)
11849 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11850 isl_basic_map *bmap = NULL;
11851 unsigned total;
11852 unsigned extra;
11853 int i, j, k, l;
11854 int pos;
11856 if (!space || !eq || !ineq)
11857 goto error;
11859 if (eq->n_col != ineq->n_col)
11860 isl_die(space->ctx, isl_error_invalid,
11861 "equalities and inequalities matrices should have "
11862 "same number of columns", goto error);
11864 total = 1 + isl_space_dim(space, isl_dim_all);
11866 if (eq->n_col < total)
11867 isl_die(space->ctx, isl_error_invalid,
11868 "number of columns too small", goto error);
11870 extra = eq->n_col - total;
11872 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11873 eq->n_row, ineq->n_row);
11874 if (!bmap)
11875 goto error;
11876 for (i = 0; i < extra; ++i) {
11877 k = isl_basic_map_alloc_div(bmap);
11878 if (k < 0)
11879 goto error;
11880 isl_int_set_si(bmap->div[k][0], 0);
11882 for (i = 0; i < eq->n_row; ++i) {
11883 l = isl_basic_map_alloc_equality(bmap);
11884 if (l < 0)
11885 goto error;
11886 for (j = 0, pos = 0; j < 5; ++j) {
11887 int off = isl_basic_map_offset(bmap, c[j]);
11888 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11889 isl_int_set(bmap->eq[l][off + k],
11890 eq->row[i][pos]);
11891 ++pos;
11895 for (i = 0; i < ineq->n_row; ++i) {
11896 l = isl_basic_map_alloc_inequality(bmap);
11897 if (l < 0)
11898 goto error;
11899 for (j = 0, pos = 0; j < 5; ++j) {
11900 int off = isl_basic_map_offset(bmap, c[j]);
11901 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11902 isl_int_set(bmap->ineq[l][off + k],
11903 ineq->row[i][pos]);
11904 ++pos;
11909 isl_space_free(space);
11910 isl_mat_free(eq);
11911 isl_mat_free(ineq);
11913 bmap = isl_basic_map_simplify(bmap);
11914 return isl_basic_map_finalize(bmap);
11915 error:
11916 isl_space_free(space);
11917 isl_mat_free(eq);
11918 isl_mat_free(ineq);
11919 isl_basic_map_free(bmap);
11920 return NULL;
11923 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11924 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11925 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11927 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11928 c1, c2, c3, c4, isl_dim_in);
11931 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11932 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11933 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11935 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11936 c1, c2, c3, c4, isl_dim_in);
11939 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11940 __isl_take isl_space *dim,
11941 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11942 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11944 isl_basic_map *bmap;
11945 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11946 c1, c2, c3, c4, isl_dim_in);
11947 return bset_from_bmap(bmap);
11950 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11952 if (!bmap)
11953 return isl_bool_error;
11955 return isl_space_can_zip(bmap->dim);
11958 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11960 if (!map)
11961 return isl_bool_error;
11963 return isl_space_can_zip(map->dim);
11966 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11967 * (A -> C) -> (B -> D).
11969 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11971 unsigned pos;
11972 unsigned n1;
11973 unsigned n2;
11975 if (!bmap)
11976 return NULL;
11978 if (!isl_basic_map_can_zip(bmap))
11979 isl_die(bmap->ctx, isl_error_invalid,
11980 "basic map cannot be zipped", goto error);
11981 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11982 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11983 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11984 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11985 bmap = isl_basic_map_cow(bmap);
11986 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11987 if (!bmap)
11988 return NULL;
11989 bmap->dim = isl_space_zip(bmap->dim);
11990 if (!bmap->dim)
11991 goto error;
11992 bmap = isl_basic_map_mark_final(bmap);
11993 return bmap;
11994 error:
11995 isl_basic_map_free(bmap);
11996 return NULL;
11999 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12000 * (A -> C) -> (B -> D).
12002 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12004 int i;
12006 if (!map)
12007 return NULL;
12009 if (!isl_map_can_zip(map))
12010 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12011 goto error);
12013 map = isl_map_cow(map);
12014 if (!map)
12015 return NULL;
12017 for (i = 0; i < map->n; ++i) {
12018 map->p[i] = isl_basic_map_zip(map->p[i]);
12019 if (!map->p[i])
12020 goto error;
12023 map->dim = isl_space_zip(map->dim);
12024 if (!map->dim)
12025 goto error;
12027 return map;
12028 error:
12029 isl_map_free(map);
12030 return NULL;
12033 /* Can we apply isl_basic_map_curry to "bmap"?
12034 * That is, does it have a nested relation in its domain?
12036 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12038 if (!bmap)
12039 return isl_bool_error;
12041 return isl_space_can_curry(bmap->dim);
12044 /* Can we apply isl_map_curry to "map"?
12045 * That is, does it have a nested relation in its domain?
12047 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12049 if (!map)
12050 return isl_bool_error;
12052 return isl_space_can_curry(map->dim);
12055 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12056 * A -> (B -> C).
12058 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12061 if (!bmap)
12062 return NULL;
12064 if (!isl_basic_map_can_curry(bmap))
12065 isl_die(bmap->ctx, isl_error_invalid,
12066 "basic map cannot be curried", goto error);
12067 bmap = isl_basic_map_cow(bmap);
12068 if (!bmap)
12069 return NULL;
12070 bmap->dim = isl_space_curry(bmap->dim);
12071 if (!bmap->dim)
12072 goto error;
12073 bmap = isl_basic_map_mark_final(bmap);
12074 return bmap;
12075 error:
12076 isl_basic_map_free(bmap);
12077 return NULL;
12080 /* Given a map (A -> B) -> C, return the corresponding map
12081 * A -> (B -> C).
12083 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12085 return isl_map_change_space(map, &isl_map_can_curry,
12086 "map cannot be curried", &isl_space_curry);
12089 /* Can isl_map_range_curry be applied to "map"?
12090 * That is, does it have a nested relation in its range,
12091 * the domain of which is itself a nested relation?
12093 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12095 if (!map)
12096 return isl_bool_error;
12098 return isl_space_can_range_curry(map->dim);
12101 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12102 * A -> (B -> (C -> D)).
12104 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12106 return isl_map_change_space(map, &isl_map_can_range_curry,
12107 "map range cannot be curried",
12108 &isl_space_range_curry);
12111 /* Can we apply isl_basic_map_uncurry to "bmap"?
12112 * That is, does it have a nested relation in its domain?
12114 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12116 if (!bmap)
12117 return isl_bool_error;
12119 return isl_space_can_uncurry(bmap->dim);
12122 /* Can we apply isl_map_uncurry to "map"?
12123 * That is, does it have a nested relation in its domain?
12125 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12127 if (!map)
12128 return isl_bool_error;
12130 return isl_space_can_uncurry(map->dim);
12133 /* Given a basic map A -> (B -> C), return the corresponding basic map
12134 * (A -> B) -> C.
12136 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12139 if (!bmap)
12140 return NULL;
12142 if (!isl_basic_map_can_uncurry(bmap))
12143 isl_die(bmap->ctx, isl_error_invalid,
12144 "basic map cannot be uncurried",
12145 return isl_basic_map_free(bmap));
12146 bmap = isl_basic_map_cow(bmap);
12147 if (!bmap)
12148 return NULL;
12149 bmap->dim = isl_space_uncurry(bmap->dim);
12150 if (!bmap->dim)
12151 return isl_basic_map_free(bmap);
12152 bmap = isl_basic_map_mark_final(bmap);
12153 return bmap;
12156 /* Given a map A -> (B -> C), return the corresponding map
12157 * (A -> B) -> C.
12159 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12161 return isl_map_change_space(map, &isl_map_can_uncurry,
12162 "map cannot be uncurried", &isl_space_uncurry);
12165 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12166 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12168 return isl_map_equate(set, type1, pos1, type2, pos2);
12171 /* Construct a basic map where the given dimensions are equal to each other.
12173 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12174 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12176 isl_basic_map *bmap = NULL;
12177 int i;
12179 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12180 isl_space_check_range(space, type2, pos2, 1) < 0)
12181 goto error;
12183 if (type1 == type2 && pos1 == pos2)
12184 return isl_basic_map_universe(space);
12186 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12187 i = isl_basic_map_alloc_equality(bmap);
12188 if (i < 0)
12189 goto error;
12190 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12191 pos1 += isl_basic_map_offset(bmap, type1);
12192 pos2 += isl_basic_map_offset(bmap, type2);
12193 isl_int_set_si(bmap->eq[i][pos1], -1);
12194 isl_int_set_si(bmap->eq[i][pos2], 1);
12195 bmap = isl_basic_map_finalize(bmap);
12196 isl_space_free(space);
12197 return bmap;
12198 error:
12199 isl_space_free(space);
12200 isl_basic_map_free(bmap);
12201 return NULL;
12204 /* Add a constraint imposing that the given two dimensions are equal.
12206 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12207 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12209 isl_basic_map *eq;
12211 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12213 bmap = isl_basic_map_intersect(bmap, eq);
12215 return bmap;
12218 /* Add a constraint imposing that the given two dimensions are equal.
12220 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12221 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12223 isl_basic_map *bmap;
12225 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12227 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12229 return map;
12232 /* Add a constraint imposing that the given two dimensions have opposite values.
12234 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12235 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12237 isl_basic_map *bmap = NULL;
12238 int i;
12240 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12241 return isl_map_free(map);
12242 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12243 return isl_map_free(map);
12245 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12246 i = isl_basic_map_alloc_equality(bmap);
12247 if (i < 0)
12248 goto error;
12249 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12250 pos1 += isl_basic_map_offset(bmap, type1);
12251 pos2 += isl_basic_map_offset(bmap, type2);
12252 isl_int_set_si(bmap->eq[i][pos1], 1);
12253 isl_int_set_si(bmap->eq[i][pos2], 1);
12254 bmap = isl_basic_map_finalize(bmap);
12256 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12258 return map;
12259 error:
12260 isl_basic_map_free(bmap);
12261 isl_map_free(map);
12262 return NULL;
12265 /* Construct a constraint imposing that the value of the first dimension is
12266 * greater than or equal to that of the second.
12268 static __isl_give isl_constraint *constraint_order_ge(
12269 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12270 enum isl_dim_type type2, int pos2)
12272 isl_constraint *c;
12274 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12275 isl_space_check_range(space, type2, pos2, 1) < 0)
12276 space = isl_space_free(space);
12277 if (!space)
12278 return NULL;
12280 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12282 if (type1 == type2 && pos1 == pos2)
12283 return c;
12285 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12286 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12288 return c;
12291 /* Add a constraint imposing that the value of the first dimension is
12292 * greater than or equal to that of the second.
12294 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12295 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12297 isl_constraint *c;
12298 isl_space *space;
12300 if (type1 == type2 && pos1 == pos2)
12301 return bmap;
12302 space = isl_basic_map_get_space(bmap);
12303 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12304 bmap = isl_basic_map_add_constraint(bmap, c);
12306 return bmap;
12309 /* Add a constraint imposing that the value of the first dimension is
12310 * greater than or equal to that of the second.
12312 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12313 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12315 isl_constraint *c;
12316 isl_space *space;
12318 if (type1 == type2 && pos1 == pos2)
12319 return map;
12320 space = isl_map_get_space(map);
12321 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12322 map = isl_map_add_constraint(map, c);
12324 return map;
12327 /* Add a constraint imposing that the value of the first dimension is
12328 * less than or equal to that of the second.
12330 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12331 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12333 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12336 /* Construct a basic map where the value of the first dimension is
12337 * greater than that of the second.
12339 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12340 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12342 isl_basic_map *bmap = NULL;
12343 int i;
12345 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12346 isl_space_check_range(space, type2, pos2, 1) < 0)
12347 goto error;
12349 if (type1 == type2 && pos1 == pos2)
12350 return isl_basic_map_empty(space);
12352 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12353 i = isl_basic_map_alloc_inequality(bmap);
12354 if (i < 0)
12355 return isl_basic_map_free(bmap);
12356 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12357 pos1 += isl_basic_map_offset(bmap, type1);
12358 pos2 += isl_basic_map_offset(bmap, type2);
12359 isl_int_set_si(bmap->ineq[i][pos1], 1);
12360 isl_int_set_si(bmap->ineq[i][pos2], -1);
12361 isl_int_set_si(bmap->ineq[i][0], -1);
12362 bmap = isl_basic_map_finalize(bmap);
12364 return bmap;
12365 error:
12366 isl_space_free(space);
12367 isl_basic_map_free(bmap);
12368 return NULL;
12371 /* Add a constraint imposing that the value of the first dimension is
12372 * greater than that of the second.
12374 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12375 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12377 isl_basic_map *gt;
12379 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12381 bmap = isl_basic_map_intersect(bmap, gt);
12383 return bmap;
12386 /* Add a constraint imposing that the value of the first dimension is
12387 * greater than that of the second.
12389 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12390 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12392 isl_basic_map *bmap;
12394 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12396 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12398 return map;
12401 /* Add a constraint imposing that the value of the first dimension is
12402 * smaller than that of the second.
12404 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12405 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12407 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12410 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12411 int pos)
12413 isl_aff *div;
12414 isl_local_space *ls;
12416 if (!bmap)
12417 return NULL;
12419 if (!isl_basic_map_divs_known(bmap))
12420 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12421 "some divs are unknown", return NULL);
12423 ls = isl_basic_map_get_local_space(bmap);
12424 div = isl_local_space_get_div(ls, pos);
12425 isl_local_space_free(ls);
12427 return div;
12430 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12431 int pos)
12433 return isl_basic_map_get_div(bset, pos);
12436 /* Plug in "subs" for dimension "type", "pos" of "bset".
12438 * Let i be the dimension to replace and let "subs" be of the form
12440 * f/d
12442 * Any integer division with a non-zero coefficient for i,
12444 * floor((a i + g)/m)
12446 * is replaced by
12448 * floor((a f + d g)/(m d))
12450 * Constraints of the form
12452 * a i + g
12454 * are replaced by
12456 * a f + d g
12458 * We currently require that "subs" is an integral expression.
12459 * Handling rational expressions may require us to add stride constraints
12460 * as we do in isl_basic_set_preimage_multi_aff.
12462 __isl_give isl_basic_set *isl_basic_set_substitute(
12463 __isl_take isl_basic_set *bset,
12464 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12466 int i;
12467 isl_int v;
12468 isl_ctx *ctx;
12470 if (bset && isl_basic_set_plain_is_empty(bset))
12471 return bset;
12473 bset = isl_basic_set_cow(bset);
12474 if (!bset || !subs)
12475 goto error;
12477 ctx = isl_basic_set_get_ctx(bset);
12478 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12479 isl_die(ctx, isl_error_invalid,
12480 "spaces don't match", goto error);
12481 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12482 isl_die(ctx, isl_error_unsupported,
12483 "cannot handle divs yet", goto error);
12484 if (!isl_int_is_one(subs->v->el[0]))
12485 isl_die(ctx, isl_error_invalid,
12486 "can only substitute integer expressions", goto error);
12488 pos += isl_basic_set_offset(bset, type);
12490 isl_int_init(v);
12492 for (i = 0; i < bset->n_eq; ++i) {
12493 if (isl_int_is_zero(bset->eq[i][pos]))
12494 continue;
12495 isl_int_set(v, bset->eq[i][pos]);
12496 isl_int_set_si(bset->eq[i][pos], 0);
12497 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12498 v, subs->v->el + 1, subs->v->size - 1);
12501 for (i = 0; i < bset->n_ineq; ++i) {
12502 if (isl_int_is_zero(bset->ineq[i][pos]))
12503 continue;
12504 isl_int_set(v, bset->ineq[i][pos]);
12505 isl_int_set_si(bset->ineq[i][pos], 0);
12506 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12507 v, subs->v->el + 1, subs->v->size - 1);
12510 for (i = 0; i < bset->n_div; ++i) {
12511 if (isl_int_is_zero(bset->div[i][1 + pos]))
12512 continue;
12513 isl_int_set(v, bset->div[i][1 + pos]);
12514 isl_int_set_si(bset->div[i][1 + pos], 0);
12515 isl_seq_combine(bset->div[i] + 1,
12516 subs->v->el[0], bset->div[i] + 1,
12517 v, subs->v->el + 1, subs->v->size - 1);
12518 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12521 isl_int_clear(v);
12523 bset = isl_basic_set_simplify(bset);
12524 return isl_basic_set_finalize(bset);
12525 error:
12526 isl_basic_set_free(bset);
12527 return NULL;
12530 /* Plug in "subs" for dimension "type", "pos" of "set".
12532 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12533 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12535 int i;
12537 if (set && isl_set_plain_is_empty(set))
12538 return set;
12540 set = isl_set_cow(set);
12541 if (!set || !subs)
12542 goto error;
12544 for (i = set->n - 1; i >= 0; --i) {
12545 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12546 set = set_from_map(remove_if_empty(set_to_map(set), i));
12547 if (!set)
12548 return NULL;
12551 return set;
12552 error:
12553 isl_set_free(set);
12554 return NULL;
12557 /* Check if the range of "ma" is compatible with the domain or range
12558 * (depending on "type") of "bmap".
12560 static isl_stat check_basic_map_compatible_range_multi_aff(
12561 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12562 __isl_keep isl_multi_aff *ma)
12564 isl_bool m;
12565 isl_space *ma_space;
12567 ma_space = isl_multi_aff_get_space(ma);
12569 m = isl_space_has_equal_params(bmap->dim, ma_space);
12570 if (m < 0)
12571 goto error;
12572 if (!m)
12573 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12574 "parameters don't match", goto error);
12575 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12576 if (m < 0)
12577 goto error;
12578 if (!m)
12579 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12580 "spaces don't match", goto error);
12582 isl_space_free(ma_space);
12583 return isl_stat_ok;
12584 error:
12585 isl_space_free(ma_space);
12586 return isl_stat_error;
12589 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12590 * coefficients before the transformed range of dimensions,
12591 * the "n_after" coefficients after the transformed range of dimensions
12592 * and the coefficients of the other divs in "bmap".
12594 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
12595 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12597 int i;
12598 int n_param;
12599 int n_set;
12600 isl_local_space *ls;
12602 if (n_div == 0)
12603 return bmap;
12605 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12606 if (!ls)
12607 return isl_basic_map_free(bmap);
12609 n_param = isl_local_space_dim(ls, isl_dim_param);
12610 n_set = isl_local_space_dim(ls, isl_dim_set);
12611 for (i = 0; i < n_div; ++i) {
12612 int o_bmap = 0, o_ls = 0;
12614 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12615 o_bmap += 1 + 1 + n_param;
12616 o_ls += 1 + 1 + n_param;
12617 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12618 o_bmap += n_before;
12619 isl_seq_cpy(bmap->div[i] + o_bmap,
12620 ls->div->row[i] + o_ls, n_set);
12621 o_bmap += n_set;
12622 o_ls += n_set;
12623 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12624 o_bmap += n_after;
12625 isl_seq_cpy(bmap->div[i] + o_bmap,
12626 ls->div->row[i] + o_ls, n_div);
12627 o_bmap += n_div;
12628 o_ls += n_div;
12629 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12630 bmap = isl_basic_map_add_div_constraints(bmap, i);
12631 if (!bmap)
12632 goto error;
12635 isl_local_space_free(ls);
12636 return bmap;
12637 error:
12638 isl_local_space_free(ls);
12639 return isl_basic_map_free(bmap);
12642 /* How many stride constraints does "ma" enforce?
12643 * That is, how many of the affine expressions have a denominator
12644 * different from one?
12646 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12648 int i;
12649 int strides = 0;
12651 for (i = 0; i < ma->n; ++i)
12652 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12653 strides++;
12655 return strides;
12658 /* For each affine expression in ma of the form
12660 * x_i = (f_i y + h_i)/m_i
12662 * with m_i different from one, add a constraint to "bmap"
12663 * of the form
12665 * f_i y + h_i = m_i alpha_i
12667 * with alpha_i an additional existentially quantified variable.
12669 * The input variables of "ma" correspond to a subset of the variables
12670 * of "bmap". There are "n_before" variables in "bmap" before this
12671 * subset and "n_after" variables after this subset.
12672 * The integer divisions of the affine expressions in "ma" are assumed
12673 * to have been aligned. There are "n_div_ma" of them and
12674 * they appear first in "bmap", straight after the "n_after" variables.
12676 static __isl_give isl_basic_map *add_ma_strides(
12677 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12678 int n_before, int n_after, int n_div_ma)
12680 int i, k;
12681 int div;
12682 int total;
12683 int n_param;
12684 int n_in;
12686 total = isl_basic_map_total_dim(bmap);
12687 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12688 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12689 for (i = 0; i < ma->n; ++i) {
12690 int o_bmap = 0, o_ma = 1;
12692 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12693 continue;
12694 div = isl_basic_map_alloc_div(bmap);
12695 k = isl_basic_map_alloc_equality(bmap);
12696 if (div < 0 || k < 0)
12697 goto error;
12698 isl_int_set_si(bmap->div[div][0], 0);
12699 isl_seq_cpy(bmap->eq[k] + o_bmap,
12700 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12701 o_bmap += 1 + n_param;
12702 o_ma += 1 + n_param;
12703 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12704 o_bmap += n_before;
12705 isl_seq_cpy(bmap->eq[k] + o_bmap,
12706 ma->u.p[i]->v->el + o_ma, n_in);
12707 o_bmap += n_in;
12708 o_ma += n_in;
12709 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12710 o_bmap += n_after;
12711 isl_seq_cpy(bmap->eq[k] + o_bmap,
12712 ma->u.p[i]->v->el + o_ma, n_div_ma);
12713 o_bmap += n_div_ma;
12714 o_ma += n_div_ma;
12715 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12716 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12717 total++;
12720 return bmap;
12721 error:
12722 isl_basic_map_free(bmap);
12723 return NULL;
12726 /* Replace the domain or range space (depending on "type) of "space" by "set".
12728 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12729 enum isl_dim_type type, __isl_take isl_space *set)
12731 if (type == isl_dim_in) {
12732 space = isl_space_range(space);
12733 space = isl_space_map_from_domain_and_range(set, space);
12734 } else {
12735 space = isl_space_domain(space);
12736 space = isl_space_map_from_domain_and_range(space, set);
12739 return space;
12742 /* Compute the preimage of the domain or range (depending on "type")
12743 * of "bmap" under the function represented by "ma".
12744 * In other words, plug in "ma" in the domain or range of "bmap".
12745 * The result is a basic map that lives in the same space as "bmap"
12746 * except that the domain or range has been replaced by
12747 * the domain space of "ma".
12749 * If bmap is represented by
12751 * A(p) + S u + B x + T v + C(divs) >= 0,
12753 * where u and x are input and output dimensions if type == isl_dim_out
12754 * while x and v are input and output dimensions if type == isl_dim_in,
12755 * and ma is represented by
12757 * x = D(p) + F(y) + G(divs')
12759 * then the result is
12761 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12763 * The divs in the input set are similarly adjusted.
12764 * In particular
12766 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12768 * becomes
12770 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12771 * B_i G(divs') + c_i(divs))/n_i)
12773 * If bmap is not a rational map and if F(y) involves any denominators
12775 * x_i = (f_i y + h_i)/m_i
12777 * then additional constraints are added to ensure that we only
12778 * map back integer points. That is we enforce
12780 * f_i y + h_i = m_i alpha_i
12782 * with alpha_i an additional existentially quantified variable.
12784 * We first copy over the divs from "ma".
12785 * Then we add the modified constraints and divs from "bmap".
12786 * Finally, we add the stride constraints, if needed.
12788 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12789 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12790 __isl_take isl_multi_aff *ma)
12792 int i, k;
12793 isl_space *space;
12794 isl_basic_map *res = NULL;
12795 int n_before, n_after, n_div_bmap, n_div_ma;
12796 isl_int f, c1, c2, g;
12797 isl_bool rational;
12798 int strides;
12800 isl_int_init(f);
12801 isl_int_init(c1);
12802 isl_int_init(c2);
12803 isl_int_init(g);
12805 ma = isl_multi_aff_align_divs(ma);
12806 if (!bmap || !ma)
12807 goto error;
12808 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12809 goto error;
12811 if (type == isl_dim_in) {
12812 n_before = 0;
12813 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12814 } else {
12815 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12816 n_after = 0;
12818 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12819 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12821 space = isl_multi_aff_get_domain_space(ma);
12822 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12823 rational = isl_basic_map_is_rational(bmap);
12824 strides = rational ? 0 : multi_aff_strides(ma);
12825 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12826 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12827 if (rational)
12828 res = isl_basic_map_set_rational(res);
12830 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12831 if (isl_basic_map_alloc_div(res) < 0)
12832 goto error;
12834 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
12835 if (!res)
12836 goto error;
12838 for (i = 0; i < bmap->n_eq; ++i) {
12839 k = isl_basic_map_alloc_equality(res);
12840 if (k < 0)
12841 goto error;
12842 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12843 n_after, n_div_ma, n_div_bmap,
12844 f, c1, c2, g, 0) < 0)
12845 goto error;
12848 for (i = 0; i < bmap->n_ineq; ++i) {
12849 k = isl_basic_map_alloc_inequality(res);
12850 if (k < 0)
12851 goto error;
12852 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12853 n_after, n_div_ma, n_div_bmap,
12854 f, c1, c2, g, 0) < 0)
12855 goto error;
12858 for (i = 0; i < bmap->n_div; ++i) {
12859 if (isl_int_is_zero(bmap->div[i][0])) {
12860 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12861 continue;
12863 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12864 n_before, n_after, n_div_ma, n_div_bmap,
12865 f, c1, c2, g, 1) < 0)
12866 goto error;
12869 if (strides)
12870 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12872 isl_int_clear(f);
12873 isl_int_clear(c1);
12874 isl_int_clear(c2);
12875 isl_int_clear(g);
12876 isl_basic_map_free(bmap);
12877 isl_multi_aff_free(ma);
12878 res = isl_basic_map_simplify(res);
12879 return isl_basic_map_finalize(res);
12880 error:
12881 isl_int_clear(f);
12882 isl_int_clear(c1);
12883 isl_int_clear(c2);
12884 isl_int_clear(g);
12885 isl_basic_map_free(bmap);
12886 isl_multi_aff_free(ma);
12887 isl_basic_map_free(res);
12888 return NULL;
12891 /* Compute the preimage of "bset" under the function represented by "ma".
12892 * In other words, plug in "ma" in "bset". The result is a basic set
12893 * that lives in the domain space of "ma".
12895 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12896 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12898 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12901 /* Compute the preimage of the domain of "bmap" under the function
12902 * represented by "ma".
12903 * In other words, plug in "ma" in the domain of "bmap".
12904 * The result is a basic map that lives in the same space as "bmap"
12905 * except that the domain has been replaced by the domain space of "ma".
12907 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12908 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12910 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12913 /* Compute the preimage of the range of "bmap" under the function
12914 * represented by "ma".
12915 * In other words, plug in "ma" in the range of "bmap".
12916 * The result is a basic map that lives in the same space as "bmap"
12917 * except that the range has been replaced by the domain space of "ma".
12919 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12920 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12922 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12925 /* Check if the range of "ma" is compatible with the domain or range
12926 * (depending on "type") of "map".
12927 * Return isl_stat_error if anything is wrong.
12929 static isl_stat check_map_compatible_range_multi_aff(
12930 __isl_keep isl_map *map, enum isl_dim_type type,
12931 __isl_keep isl_multi_aff *ma)
12933 isl_bool m;
12934 isl_space *ma_space;
12936 ma_space = isl_multi_aff_get_space(ma);
12937 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12938 isl_space_free(ma_space);
12939 if (m < 0)
12940 return isl_stat_error;
12941 if (!m)
12942 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12943 "spaces don't match", return isl_stat_error);
12944 return isl_stat_ok;
12947 /* Compute the preimage of the domain or range (depending on "type")
12948 * of "map" under the function represented by "ma".
12949 * In other words, plug in "ma" in the domain or range of "map".
12950 * The result is a map that lives in the same space as "map"
12951 * except that the domain or range has been replaced by
12952 * the domain space of "ma".
12954 * The parameters are assumed to have been aligned.
12956 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12957 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12959 int i;
12960 isl_space *space;
12962 map = isl_map_cow(map);
12963 ma = isl_multi_aff_align_divs(ma);
12964 if (!map || !ma)
12965 goto error;
12966 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12967 goto error;
12969 for (i = 0; i < map->n; ++i) {
12970 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12971 isl_multi_aff_copy(ma));
12972 if (!map->p[i])
12973 goto error;
12976 space = isl_multi_aff_get_domain_space(ma);
12977 space = isl_space_set(isl_map_get_space(map), type, space);
12979 isl_space_free(map->dim);
12980 map->dim = space;
12981 if (!map->dim)
12982 goto error;
12984 isl_multi_aff_free(ma);
12985 if (map->n > 1)
12986 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12987 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12988 return map;
12989 error:
12990 isl_multi_aff_free(ma);
12991 isl_map_free(map);
12992 return NULL;
12995 /* Compute the preimage of the domain or range (depending on "type")
12996 * of "map" under the function represented by "ma".
12997 * In other words, plug in "ma" in the domain or range of "map".
12998 * The result is a map that lives in the same space as "map"
12999 * except that the domain or range has been replaced by
13000 * the domain space of "ma".
13002 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13003 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13005 isl_bool aligned;
13007 if (!map || !ma)
13008 goto error;
13010 aligned = isl_map_space_has_equal_params(map, ma->space);
13011 if (aligned < 0)
13012 goto error;
13013 if (aligned)
13014 return map_preimage_multi_aff(map, type, ma);
13016 if (isl_map_check_named_params(map) < 0)
13017 goto error;
13018 if (!isl_space_has_named_params(ma->space))
13019 isl_die(map->ctx, isl_error_invalid,
13020 "unaligned unnamed parameters", goto error);
13021 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13022 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13024 return map_preimage_multi_aff(map, type, ma);
13025 error:
13026 isl_multi_aff_free(ma);
13027 return isl_map_free(map);
13030 /* Compute the preimage of "set" under the function represented by "ma".
13031 * In other words, plug in "ma" in "set". The result is a set
13032 * that lives in the domain space of "ma".
13034 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13035 __isl_take isl_multi_aff *ma)
13037 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13040 /* Compute the preimage of the domain of "map" under the function
13041 * represented by "ma".
13042 * In other words, plug in "ma" in the domain of "map".
13043 * The result is a map that lives in the same space as "map"
13044 * except that the domain has been replaced by the domain space of "ma".
13046 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13047 __isl_take isl_multi_aff *ma)
13049 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13052 /* Compute the preimage of the range of "map" under the function
13053 * represented by "ma".
13054 * In other words, plug in "ma" in the range of "map".
13055 * The result is a map that lives in the same space as "map"
13056 * except that the range has been replaced by the domain space of "ma".
13058 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13059 __isl_take isl_multi_aff *ma)
13061 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13064 /* Compute the preimage of "map" under the function represented by "pma".
13065 * In other words, plug in "pma" in the domain or range of "map".
13066 * The result is a map that lives in the same space as "map",
13067 * except that the space of type "type" has been replaced by
13068 * the domain space of "pma".
13070 * The parameters of "map" and "pma" are assumed to have been aligned.
13072 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13073 __isl_take isl_map *map, enum isl_dim_type type,
13074 __isl_take isl_pw_multi_aff *pma)
13076 int i;
13077 isl_map *res;
13079 if (!pma)
13080 goto error;
13082 if (pma->n == 0) {
13083 isl_pw_multi_aff_free(pma);
13084 res = isl_map_empty(isl_map_get_space(map));
13085 isl_map_free(map);
13086 return res;
13089 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13090 isl_multi_aff_copy(pma->p[0].maff));
13091 if (type == isl_dim_in)
13092 res = isl_map_intersect_domain(res,
13093 isl_map_copy(pma->p[0].set));
13094 else
13095 res = isl_map_intersect_range(res,
13096 isl_map_copy(pma->p[0].set));
13098 for (i = 1; i < pma->n; ++i) {
13099 isl_map *res_i;
13101 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13102 isl_multi_aff_copy(pma->p[i].maff));
13103 if (type == isl_dim_in)
13104 res_i = isl_map_intersect_domain(res_i,
13105 isl_map_copy(pma->p[i].set));
13106 else
13107 res_i = isl_map_intersect_range(res_i,
13108 isl_map_copy(pma->p[i].set));
13109 res = isl_map_union(res, res_i);
13112 isl_pw_multi_aff_free(pma);
13113 isl_map_free(map);
13114 return res;
13115 error:
13116 isl_pw_multi_aff_free(pma);
13117 isl_map_free(map);
13118 return NULL;
13121 /* Compute the preimage of "map" under the function represented by "pma".
13122 * In other words, plug in "pma" in the domain or range of "map".
13123 * The result is a map that lives in the same space as "map",
13124 * except that the space of type "type" has been replaced by
13125 * the domain space of "pma".
13127 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13128 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13130 isl_bool aligned;
13132 if (!map || !pma)
13133 goto error;
13135 aligned = isl_map_space_has_equal_params(map, pma->dim);
13136 if (aligned < 0)
13137 goto error;
13138 if (aligned)
13139 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13141 if (isl_map_check_named_params(map) < 0)
13142 goto error;
13143 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13144 goto error;
13145 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13146 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13148 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13149 error:
13150 isl_pw_multi_aff_free(pma);
13151 return isl_map_free(map);
13154 /* Compute the preimage of "set" under the function represented by "pma".
13155 * In other words, plug in "pma" in "set". The result is a set
13156 * that lives in the domain space of "pma".
13158 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13159 __isl_take isl_pw_multi_aff *pma)
13161 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13164 /* Compute the preimage of the domain of "map" under the function
13165 * represented by "pma".
13166 * In other words, plug in "pma" in the domain of "map".
13167 * The result is a map that lives in the same space as "map",
13168 * except that domain space has been replaced by the domain space of "pma".
13170 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13171 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13173 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13176 /* Compute the preimage of the range of "map" under the function
13177 * represented by "pma".
13178 * In other words, plug in "pma" in the range of "map".
13179 * The result is a map that lives in the same space as "map",
13180 * except that range space has been replaced by the domain space of "pma".
13182 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13183 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13185 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13188 /* Compute the preimage of "map" under the function represented by "mpa".
13189 * In other words, plug in "mpa" in the domain or range of "map".
13190 * The result is a map that lives in the same space as "map",
13191 * except that the space of type "type" has been replaced by
13192 * the domain space of "mpa".
13194 * If the map does not involve any constraints that refer to the
13195 * dimensions of the substituted space, then the only possible
13196 * effect of "mpa" on the map is to map the space to a different space.
13197 * We create a separate isl_multi_aff to effectuate this change
13198 * in order to avoid spurious splitting of the map along the pieces
13199 * of "mpa".
13200 * If "mpa" has a non-trivial explicit domain, however,
13201 * then the full substitution should be performed.
13203 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13204 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13206 int n;
13207 isl_bool full;
13208 isl_pw_multi_aff *pma;
13210 if (!map || !mpa)
13211 goto error;
13213 n = isl_map_dim(map, type);
13214 full = isl_map_involves_dims(map, type, 0, n);
13215 if (full >= 0 && !full)
13216 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13217 if (full < 0)
13218 goto error;
13219 if (!full) {
13220 isl_space *space;
13221 isl_multi_aff *ma;
13223 space = isl_multi_pw_aff_get_space(mpa);
13224 isl_multi_pw_aff_free(mpa);
13225 ma = isl_multi_aff_zero(space);
13226 return isl_map_preimage_multi_aff(map, type, ma);
13229 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13230 return isl_map_preimage_pw_multi_aff(map, type, pma);
13231 error:
13232 isl_map_free(map);
13233 isl_multi_pw_aff_free(mpa);
13234 return NULL;
13237 /* Compute the preimage of "map" under the function represented by "mpa".
13238 * In other words, plug in "mpa" in the domain "map".
13239 * The result is a map that lives in the same space as "map",
13240 * except that domain space has been replaced by the domain space of "mpa".
13242 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13243 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13245 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13248 /* Compute the preimage of "set" by the function represented by "mpa".
13249 * In other words, plug in "mpa" in "set".
13251 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13252 __isl_take isl_multi_pw_aff *mpa)
13254 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13257 /* Return a copy of the equality constraints of "bset" as a matrix.
13259 __isl_give isl_mat *isl_basic_set_extract_equalities(
13260 __isl_keep isl_basic_set *bset)
13262 isl_ctx *ctx;
13263 unsigned total;
13265 if (!bset)
13266 return NULL;
13268 ctx = isl_basic_set_get_ctx(bset);
13269 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13270 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13273 /* Are the "n" "coefficients" starting at "first" of the integer division
13274 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13275 * to each other?
13276 * The "coefficient" at position 0 is the denominator.
13277 * The "coefficient" at position 1 is the constant term.
13279 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13280 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13281 unsigned first, unsigned n)
13283 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13284 return isl_bool_error;
13285 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13286 return isl_bool_error;
13287 return isl_seq_eq(bmap1->div[pos1] + first,
13288 bmap2->div[pos2] + first, n);
13291 /* Are the integer division expressions at position "pos1" in "bmap1" and
13292 * "pos2" in "bmap2" equal to each other, except that the constant terms
13293 * are different?
13295 isl_bool isl_basic_map_equal_div_expr_except_constant(
13296 __isl_keep isl_basic_map *bmap1, int pos1,
13297 __isl_keep isl_basic_map *bmap2, int pos2)
13299 isl_bool equal;
13300 unsigned total;
13302 if (!bmap1 || !bmap2)
13303 return isl_bool_error;
13304 total = isl_basic_map_total_dim(bmap1);
13305 if (total != isl_basic_map_total_dim(bmap2))
13306 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13307 "incomparable div expressions", return isl_bool_error);
13308 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13309 0, 1);
13310 if (equal < 0 || !equal)
13311 return equal;
13312 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13313 1, 1);
13314 if (equal < 0 || equal)
13315 return isl_bool_not(equal);
13316 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13317 2, total);
13320 /* Replace the numerator of the constant term of the integer division
13321 * expression at position "div" in "bmap" by "value".
13322 * The caller guarantees that this does not change the meaning
13323 * of the input.
13325 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13326 __isl_take isl_basic_map *bmap, int div, int value)
13328 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13329 return isl_basic_map_free(bmap);
13331 isl_int_set_si(bmap->div[div][1], value);
13333 return bmap;
13336 /* Is the point "inner" internal to inequality constraint "ineq"
13337 * of "bset"?
13338 * The point is considered to be internal to the inequality constraint,
13339 * if it strictly lies on the positive side of the inequality constraint,
13340 * or if it lies on the constraint and the constraint is lexico-positive.
13342 static isl_bool is_internal(__isl_keep isl_vec *inner,
13343 __isl_keep isl_basic_set *bset, int ineq)
13345 isl_ctx *ctx;
13346 int pos;
13347 unsigned total;
13349 if (!inner || !bset)
13350 return isl_bool_error;
13352 ctx = isl_basic_set_get_ctx(bset);
13353 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13354 &ctx->normalize_gcd);
13355 if (!isl_int_is_zero(ctx->normalize_gcd))
13356 return isl_int_is_nonneg(ctx->normalize_gcd);
13358 total = isl_basic_set_dim(bset, isl_dim_all);
13359 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13360 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13363 /* Tighten the inequality constraints of "bset" that are outward with respect
13364 * to the point "vec".
13365 * That is, tighten the constraints that are not satisfied by "vec".
13367 * "vec" is a point internal to some superset S of "bset" that is used
13368 * to make the subsets of S disjoint, by tightening one half of the constraints
13369 * that separate two subsets. In particular, the constraints of S
13370 * are all satisfied by "vec" and should not be tightened.
13371 * Of the internal constraints, those that have "vec" on the outside
13372 * are tightened. The shared facet is included in the adjacent subset
13373 * with the opposite constraint.
13374 * For constraints that saturate "vec", this criterion cannot be used
13375 * to determine which of the two sides should be tightened.
13376 * Instead, the sign of the first non-zero coefficient is used
13377 * to make this choice. Note that this second criterion is never used
13378 * on the constraints of S since "vec" is interior to "S".
13380 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13381 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13383 int j;
13385 bset = isl_basic_set_cow(bset);
13386 if (!bset)
13387 return NULL;
13388 for (j = 0; j < bset->n_ineq; ++j) {
13389 isl_bool internal;
13391 internal = is_internal(vec, bset, j);
13392 if (internal < 0)
13393 return isl_basic_set_free(bset);
13394 if (internal)
13395 continue;
13396 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13399 return bset;
13402 /* Replace the variables x of type "type" starting at "first" in "bmap"
13403 * by x' with x = M x' with M the matrix trans.
13404 * That is, replace the corresponding coefficients c by c M.
13406 * The transformation matrix should be a square matrix.
13408 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13409 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13410 __isl_take isl_mat *trans)
13412 unsigned pos;
13414 bmap = isl_basic_map_cow(bmap);
13415 if (!bmap || !trans)
13416 goto error;
13418 if (trans->n_row != trans->n_col)
13419 isl_die(trans->ctx, isl_error_invalid,
13420 "expecting square transformation matrix", goto error);
13421 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13422 goto error;
13424 pos = isl_basic_map_offset(bmap, type) + first;
13426 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13427 isl_mat_copy(trans)) < 0)
13428 goto error;
13429 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13430 isl_mat_copy(trans)) < 0)
13431 goto error;
13432 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13433 isl_mat_copy(trans)) < 0)
13434 goto error;
13436 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13437 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13439 isl_mat_free(trans);
13440 return bmap;
13441 error:
13442 isl_mat_free(trans);
13443 isl_basic_map_free(bmap);
13444 return NULL;
13447 /* Replace the variables x of type "type" starting at "first" in "bset"
13448 * by x' with x = M x' with M the matrix trans.
13449 * That is, replace the corresponding coefficients c by c M.
13451 * The transformation matrix should be a square matrix.
13453 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13454 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13455 __isl_take isl_mat *trans)
13457 return isl_basic_map_transform_dims(bset, type, first, trans);