isl_map_simplify.c: ..._drop_more_redundant_divs: use isl_basic_map_var_offset
[isl.git] / isl_map.c
bloba13704277f29c51286a4298e0403d17a4d946627
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 n(__isl_keep isl_space *dim, enum isl_dim_type type)
66 switch (type) {
67 case isl_dim_param: return dim->nparam;
68 case isl_dim_in: return dim->n_in;
69 case isl_dim_out: return dim->n_out;
70 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
71 default: return 0;
75 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
77 switch (type) {
78 case isl_dim_param: return 1;
79 case isl_dim_in: return 1 + dim->nparam;
80 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
81 default: return 0;
85 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
86 enum isl_dim_type type)
88 if (!bmap)
89 return 0;
90 switch (type) {
91 case isl_dim_cst: return 1;
92 case isl_dim_param:
93 case isl_dim_in:
94 case isl_dim_out: return isl_space_dim(bmap->dim, type);
95 case isl_dim_div: return bmap->n_div;
96 case isl_dim_all: return isl_basic_map_total_dim(bmap);
97 default: return 0;
101 /* Return the space of "map".
103 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
105 return map ? map->dim : NULL;
108 /* Return the space of "set".
110 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
112 return isl_map_peek_space(set_to_map(set));
115 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
117 return map ? n(map->dim, type) : 0;
120 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
122 return set ? n(set->dim, type) : 0;
125 /* Return the position of the variables of the given type
126 * within the sequence of variables of "bmap".
128 int isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
129 enum isl_dim_type type)
131 isl_space *space;
133 space = isl_basic_map_peek_space(bmap);
134 if (!space)
135 return -1;
137 switch (type) {
138 case isl_dim_param:
139 case isl_dim_in:
140 case isl_dim_out: return isl_space_offset(space, type);
141 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
142 case isl_dim_cst:
143 default:
144 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
145 "invalid dimension type", return -1);
149 /* Return the position of the coefficients of the variables of the given type
150 * within the sequence of coefficients of "bmap".
152 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
153 enum isl_dim_type type)
155 switch (type) {
156 case isl_dim_cst: return 0;
157 case isl_dim_param:
158 case isl_dim_in:
159 case isl_dim_out:
160 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
161 default: return 0;
165 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
166 enum isl_dim_type type)
168 return isl_basic_map_offset(bset, type);
171 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
173 return pos(map->dim, type);
176 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
177 enum isl_dim_type type)
179 return isl_basic_map_dim(bset, type);
182 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
184 return isl_basic_set_dim(bset, isl_dim_set);
187 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
189 return isl_basic_set_dim(bset, isl_dim_param);
192 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
194 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
197 unsigned isl_set_n_dim(__isl_keep isl_set *set)
199 return isl_set_dim(set, isl_dim_set);
202 unsigned isl_set_n_param(__isl_keep isl_set *set)
204 return isl_set_dim(set, isl_dim_param);
207 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
209 return bmap ? bmap->dim->n_in : 0;
212 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
214 return bmap ? bmap->dim->n_out : 0;
217 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
219 return bmap ? bmap->dim->nparam : 0;
222 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
224 return bmap ? bmap->n_div : 0;
227 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
229 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
232 unsigned isl_map_n_in(__isl_keep const isl_map *map)
234 return map ? map->dim->n_in : 0;
237 unsigned isl_map_n_out(__isl_keep const isl_map *map)
239 return map ? map->dim->n_out : 0;
242 unsigned isl_map_n_param(__isl_keep const isl_map *map)
244 return map ? map->dim->nparam : 0;
247 /* Return the number of equality constraints in the description of "bmap".
248 * Return -1 on error.
250 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
252 if (!bmap)
253 return -1;
254 return bmap->n_eq;
257 /* Return the number of equality constraints in the description of "bset".
258 * Return -1 on error.
260 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
262 return isl_basic_map_n_equality(bset_to_bmap(bset));
265 /* Return the number of inequality constraints in the description of "bmap".
266 * Return -1 on error.
268 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
270 if (!bmap)
271 return -1;
272 return bmap->n_ineq;
275 /* Return the number of inequality constraints in the description of "bset".
276 * Return -1 on error.
278 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
280 return isl_basic_map_n_inequality(bset_to_bmap(bset));
283 /* Do "bmap1" and "bmap2" have the same parameters?
285 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
286 __isl_keep isl_basic_map *bmap2)
288 isl_space *space1, *space2;
290 space1 = isl_basic_map_peek_space(bmap1);
291 space2 = isl_basic_map_peek_space(bmap2);
292 return isl_space_has_equal_params(space1, space2);
295 /* Do "map1" and "map2" have the same parameters?
297 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
298 __isl_keep isl_map *map2)
300 isl_space *space1, *space2;
302 space1 = isl_map_peek_space(map1);
303 space2 = isl_map_peek_space(map2);
304 return isl_space_has_equal_params(space1, space2);
307 /* Do "map" and "set" have the same parameters?
309 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
310 __isl_keep isl_set *set)
312 return isl_map_has_equal_params(map, set_to_map(set));
315 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
316 __isl_keep isl_set *set)
318 isl_bool m;
319 if (!map || !set)
320 return isl_bool_error;
321 m = isl_map_has_equal_params(map, set_to_map(set));
322 if (m < 0 || !m)
323 return m;
324 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
325 set->dim, isl_dim_set);
328 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
329 __isl_keep isl_basic_set *bset)
331 isl_bool m;
332 if (!bmap || !bset)
333 return isl_bool_error;
334 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
335 if (m < 0 || !m)
336 return m;
337 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
338 bset->dim, isl_dim_set);
341 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
342 __isl_keep isl_set *set)
344 isl_bool m;
345 if (!map || !set)
346 return isl_bool_error;
347 m = isl_map_has_equal_params(map, set_to_map(set));
348 if (m < 0 || !m)
349 return m;
350 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
351 set->dim, isl_dim_set);
354 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
355 __isl_keep isl_basic_set *bset)
357 isl_bool m;
358 if (!bmap || !bset)
359 return isl_bool_error;
360 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
361 if (m < 0 || !m)
362 return m;
363 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
364 bset->dim, isl_dim_set);
367 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
369 return bmap ? bmap->ctx : NULL;
372 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
374 return bset ? bset->ctx : NULL;
377 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
379 return map ? map->ctx : NULL;
382 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
384 return set ? set->ctx : NULL;
387 /* Return the space of "bmap".
389 __isl_keep isl_space *isl_basic_map_peek_space(
390 __isl_keep const isl_basic_map *bmap)
392 return bmap ? bmap->dim : NULL;
395 /* Return the space of "bset".
397 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
399 return isl_basic_map_peek_space(bset_to_bmap(bset));
402 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
404 return isl_space_copy(isl_basic_map_peek_space(bmap));
407 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
409 return isl_basic_map_get_space(bset_to_bmap(bset));
412 /* Extract the divs in "bmap" as a matrix.
414 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
416 int i;
417 isl_ctx *ctx;
418 isl_mat *div;
419 int v_div;
420 unsigned cols;
422 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
423 if (v_div < 0)
424 return NULL;
426 ctx = isl_basic_map_get_ctx(bmap);
427 cols = 1 + 1 + v_div + bmap->n_div;
428 div = isl_mat_alloc(ctx, bmap->n_div, cols);
429 if (!div)
430 return NULL;
432 for (i = 0; i < bmap->n_div; ++i)
433 isl_seq_cpy(div->row[i], bmap->div[i], cols);
435 return div;
438 /* Extract the divs in "bset" as a matrix.
440 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
442 return isl_basic_map_get_divs(bset);
445 __isl_give isl_local_space *isl_basic_map_get_local_space(
446 __isl_keep isl_basic_map *bmap)
448 isl_mat *div;
450 if (!bmap)
451 return NULL;
453 div = isl_basic_map_get_divs(bmap);
454 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
457 __isl_give isl_local_space *isl_basic_set_get_local_space(
458 __isl_keep isl_basic_set *bset)
460 return isl_basic_map_get_local_space(bset);
463 /* For each known div d = floor(f/m), add the constraints
465 * f - m d >= 0
466 * -(f-(m-1)) + m d >= 0
468 * Do not finalize the result.
470 static __isl_give isl_basic_map *add_known_div_constraints(
471 __isl_take isl_basic_map *bmap)
473 int i;
474 unsigned n_div;
476 if (!bmap)
477 return NULL;
478 n_div = isl_basic_map_dim(bmap, isl_dim_div);
479 if (n_div == 0)
480 return bmap;
481 bmap = isl_basic_map_cow(bmap);
482 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
483 if (!bmap)
484 return NULL;
485 for (i = 0; i < n_div; ++i) {
486 if (isl_int_is_zero(bmap->div[i][0]))
487 continue;
488 bmap = isl_basic_map_add_div_constraints(bmap, i);
491 return bmap;
494 __isl_give isl_basic_map *isl_basic_map_from_local_space(
495 __isl_take isl_local_space *ls)
497 int i;
498 int n_div;
499 isl_basic_map *bmap;
501 if (!ls)
502 return NULL;
504 n_div = isl_local_space_dim(ls, isl_dim_div);
505 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
506 n_div, 0, 2 * n_div);
508 for (i = 0; i < n_div; ++i)
509 if (isl_basic_map_alloc_div(bmap) < 0)
510 goto error;
512 for (i = 0; i < n_div; ++i)
513 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
514 bmap = add_known_div_constraints(bmap);
516 isl_local_space_free(ls);
517 return bmap;
518 error:
519 isl_local_space_free(ls);
520 isl_basic_map_free(bmap);
521 return NULL;
524 __isl_give isl_basic_set *isl_basic_set_from_local_space(
525 __isl_take isl_local_space *ls)
527 return isl_basic_map_from_local_space(ls);
530 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
532 return isl_space_copy(isl_map_peek_space(map));
535 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
537 if (!set)
538 return NULL;
539 return isl_space_copy(set->dim);
542 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
543 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
545 bmap = isl_basic_map_cow(bmap);
546 if (!bmap)
547 return NULL;
548 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
549 if (!bmap->dim)
550 goto error;
551 bmap = isl_basic_map_finalize(bmap);
552 return bmap;
553 error:
554 isl_basic_map_free(bmap);
555 return NULL;
558 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
559 __isl_take isl_basic_set *bset, const char *s)
561 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
564 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
565 enum isl_dim_type type)
567 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
570 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
571 enum isl_dim_type type, const char *s)
573 int i;
575 map = isl_map_cow(map);
576 if (!map)
577 return NULL;
579 map->dim = isl_space_set_tuple_name(map->dim, type, s);
580 if (!map->dim)
581 goto error;
583 for (i = 0; i < map->n; ++i) {
584 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
585 if (!map->p[i])
586 goto error;
589 return map;
590 error:
591 isl_map_free(map);
592 return NULL;
595 /* Replace the identifier of the tuple of type "type" by "id".
597 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
598 __isl_take isl_basic_map *bmap,
599 enum isl_dim_type type, __isl_take isl_id *id)
601 bmap = isl_basic_map_cow(bmap);
602 if (!bmap)
603 goto error;
604 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
605 if (!bmap->dim)
606 return isl_basic_map_free(bmap);
607 bmap = isl_basic_map_finalize(bmap);
608 return bmap;
609 error:
610 isl_id_free(id);
611 return NULL;
614 /* Replace the identifier of the tuple by "id".
616 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
617 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
619 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
622 /* Does the input or output tuple have a name?
624 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
626 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
629 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
630 enum isl_dim_type type)
632 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
635 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
636 const char *s)
638 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
639 isl_dim_set, s));
642 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
643 enum isl_dim_type type, __isl_take isl_id *id)
645 map = isl_map_cow(map);
646 if (!map)
647 goto error;
649 map->dim = isl_space_set_tuple_id(map->dim, type, id);
651 return isl_map_reset_space(map, isl_space_copy(map->dim));
652 error:
653 isl_id_free(id);
654 return NULL;
657 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
658 __isl_take isl_id *id)
660 return isl_map_set_tuple_id(set, isl_dim_set, id);
663 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
664 enum isl_dim_type type)
666 map = isl_map_cow(map);
667 if (!map)
668 return NULL;
670 map->dim = isl_space_reset_tuple_id(map->dim, type);
672 return isl_map_reset_space(map, isl_space_copy(map->dim));
675 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
677 return isl_map_reset_tuple_id(set, isl_dim_set);
680 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
682 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
685 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
686 enum isl_dim_type type)
688 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
691 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
693 return isl_map_has_tuple_id(set, isl_dim_set);
696 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
698 return isl_map_get_tuple_id(set, isl_dim_set);
701 /* Does the set tuple have a name?
703 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
705 if (!set)
706 return isl_bool_error;
707 return isl_space_has_tuple_name(set->dim, isl_dim_set);
711 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
713 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
716 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
718 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
721 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
722 enum isl_dim_type type, unsigned pos)
724 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
727 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
728 enum isl_dim_type type, unsigned pos)
730 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
733 /* Does the given dimension have a name?
735 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
736 enum isl_dim_type type, unsigned pos)
738 if (!map)
739 return isl_bool_error;
740 return isl_space_has_dim_name(map->dim, type, pos);
743 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
744 enum isl_dim_type type, unsigned pos)
746 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
749 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
750 enum isl_dim_type type, unsigned pos)
752 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
755 /* Does the given dimension have a name?
757 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
758 enum isl_dim_type type, unsigned pos)
760 if (!set)
761 return isl_bool_error;
762 return isl_space_has_dim_name(set->dim, type, pos);
765 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
766 __isl_take isl_basic_map *bmap,
767 enum isl_dim_type type, unsigned pos, const char *s)
769 bmap = isl_basic_map_cow(bmap);
770 if (!bmap)
771 return NULL;
772 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
773 if (!bmap->dim)
774 goto error;
775 return isl_basic_map_finalize(bmap);
776 error:
777 isl_basic_map_free(bmap);
778 return NULL;
781 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
782 enum isl_dim_type type, unsigned pos, const char *s)
784 int i;
786 map = isl_map_cow(map);
787 if (!map)
788 return NULL;
790 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
791 if (!map->dim)
792 goto error;
794 for (i = 0; i < map->n; ++i) {
795 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
796 if (!map->p[i])
797 goto error;
800 return map;
801 error:
802 isl_map_free(map);
803 return NULL;
806 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
807 __isl_take isl_basic_set *bset,
808 enum isl_dim_type type, unsigned pos, const char *s)
810 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
811 type, pos, s));
814 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
815 enum isl_dim_type type, unsigned pos, const char *s)
817 return set_from_map(isl_map_set_dim_name(set_to_map(set),
818 type, pos, s));
821 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
822 enum isl_dim_type type, unsigned pos)
824 if (!bmap)
825 return isl_bool_error;
826 return isl_space_has_dim_id(bmap->dim, type, pos);
829 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
830 enum isl_dim_type type, unsigned pos)
832 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
835 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
836 enum isl_dim_type type, unsigned pos)
838 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
841 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
842 enum isl_dim_type type, unsigned pos)
844 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
847 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
848 enum isl_dim_type type, unsigned pos)
850 return isl_map_has_dim_id(set, type, pos);
853 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
854 enum isl_dim_type type, unsigned pos)
856 return isl_map_get_dim_id(set, type, pos);
859 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
860 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
862 map = isl_map_cow(map);
863 if (!map)
864 goto error;
866 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
868 return isl_map_reset_space(map, isl_space_copy(map->dim));
869 error:
870 isl_id_free(id);
871 return NULL;
874 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
875 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
877 return isl_map_set_dim_id(set, type, pos, id);
880 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
881 __isl_keep isl_id *id)
883 if (!map)
884 return -1;
885 return isl_space_find_dim_by_id(map->dim, type, id);
888 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
889 __isl_keep isl_id *id)
891 return isl_map_find_dim_by_id(set, type, id);
894 /* Return the position of the dimension of the given type and name
895 * in "bmap".
896 * Return -1 if no such dimension can be found.
898 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
899 enum isl_dim_type type, const char *name)
901 if (!bmap)
902 return -1;
903 return isl_space_find_dim_by_name(bmap->dim, type, name);
906 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
907 const char *name)
909 if (!map)
910 return -1;
911 return isl_space_find_dim_by_name(map->dim, type, name);
914 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
915 const char *name)
917 return isl_map_find_dim_by_name(set, type, name);
920 /* Check whether equality i of bset is a pure stride constraint
921 * on a single dimension, i.e., of the form
923 * v = k e
925 * with k a constant and e an existentially quantified variable.
927 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
929 unsigned nparam;
930 unsigned d;
931 unsigned n_div;
932 int pos1;
933 int pos2;
935 if (!bset)
936 return isl_bool_error;
938 if (!isl_int_is_zero(bset->eq[i][0]))
939 return isl_bool_false;
941 nparam = isl_basic_set_dim(bset, isl_dim_param);
942 d = isl_basic_set_dim(bset, isl_dim_set);
943 n_div = isl_basic_set_dim(bset, isl_dim_div);
945 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
946 return isl_bool_false;
947 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
948 if (pos1 == -1)
949 return isl_bool_false;
950 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
951 d - pos1 - 1) != -1)
952 return isl_bool_false;
954 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
955 if (pos2 == -1)
956 return isl_bool_false;
957 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
958 n_div - pos2 - 1) != -1)
959 return isl_bool_false;
960 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
961 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
962 return isl_bool_false;
964 return isl_bool_true;
967 /* Reset the user pointer on all identifiers of parameters and tuples
968 * of the space of "map".
970 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
972 isl_space *space;
974 space = isl_map_get_space(map);
975 space = isl_space_reset_user(space);
976 map = isl_map_reset_space(map, space);
978 return map;
981 /* Reset the user pointer on all identifiers of parameters and tuples
982 * of the space of "set".
984 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
986 return isl_map_reset_user(set);
989 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
991 if (!bmap)
992 return isl_bool_error;
993 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
996 /* Has "map" been marked as a rational map?
997 * In particular, have all basic maps in "map" been marked this way?
998 * An empty map is not considered to be rational.
999 * Maps where only some of the basic maps are marked rational
1000 * are not allowed.
1002 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1004 int i;
1005 isl_bool rational;
1007 if (!map)
1008 return isl_bool_error;
1009 if (map->n == 0)
1010 return isl_bool_false;
1011 rational = isl_basic_map_is_rational(map->p[0]);
1012 if (rational < 0)
1013 return rational;
1014 for (i = 1; i < map->n; ++i) {
1015 isl_bool rational_i;
1017 rational_i = isl_basic_map_is_rational(map->p[i]);
1018 if (rational_i < 0)
1019 return rational_i;
1020 if (rational != rational_i)
1021 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1022 "mixed rational and integer basic maps "
1023 "not supported", return isl_bool_error);
1026 return rational;
1029 /* Has "set" been marked as a rational set?
1030 * In particular, have all basic set in "set" been marked this way?
1031 * An empty set is not considered to be rational.
1032 * Sets where only some of the basic sets are marked rational
1033 * are not allowed.
1035 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1037 return isl_map_is_rational(set);
1040 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1042 return isl_basic_map_is_rational(bset);
1045 /* Does "bmap" contain any rational points?
1047 * If "bmap" has an equality for each dimension, equating the dimension
1048 * to an integer constant, then it has no rational points, even if it
1049 * is marked as rational.
1051 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1053 isl_bool has_rational = isl_bool_true;
1054 unsigned total;
1056 if (!bmap)
1057 return isl_bool_error;
1058 if (isl_basic_map_plain_is_empty(bmap))
1059 return isl_bool_false;
1060 if (!isl_basic_map_is_rational(bmap))
1061 return isl_bool_false;
1062 bmap = isl_basic_map_copy(bmap);
1063 bmap = isl_basic_map_implicit_equalities(bmap);
1064 if (!bmap)
1065 return isl_bool_error;
1066 total = isl_basic_map_total_dim(bmap);
1067 if (bmap->n_eq == total) {
1068 int i, j;
1069 for (i = 0; i < bmap->n_eq; ++i) {
1070 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1071 if (j < 0)
1072 break;
1073 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1074 !isl_int_is_negone(bmap->eq[i][1 + j]))
1075 break;
1076 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1077 total - j - 1);
1078 if (j >= 0)
1079 break;
1081 if (i == bmap->n_eq)
1082 has_rational = isl_bool_false;
1084 isl_basic_map_free(bmap);
1086 return has_rational;
1089 /* Does "map" contain any rational points?
1091 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1093 int i;
1094 isl_bool has_rational;
1096 if (!map)
1097 return isl_bool_error;
1098 for (i = 0; i < map->n; ++i) {
1099 has_rational = isl_basic_map_has_rational(map->p[i]);
1100 if (has_rational < 0 || has_rational)
1101 return has_rational;
1103 return isl_bool_false;
1106 /* Does "set" contain any rational points?
1108 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1110 return isl_map_has_rational(set);
1113 /* Is this basic set a parameter domain?
1115 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1117 if (!bset)
1118 return isl_bool_error;
1119 return isl_space_is_params(bset->dim);
1122 /* Is this set a parameter domain?
1124 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1126 if (!set)
1127 return isl_bool_error;
1128 return isl_space_is_params(set->dim);
1131 /* Is this map actually a parameter domain?
1132 * Users should never call this function. Outside of isl,
1133 * a map can never be a parameter domain.
1135 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1137 if (!map)
1138 return isl_bool_error;
1139 return isl_space_is_params(map->dim);
1142 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1143 __isl_take isl_basic_map *bmap, unsigned extra,
1144 unsigned n_eq, unsigned n_ineq)
1146 int i;
1147 isl_space *space = isl_basic_map_peek_space(bmap);
1148 unsigned n_var = isl_space_dim(space, isl_dim_all);
1149 size_t row_size = 1 + n_var + extra;
1151 bmap->ctx = ctx;
1152 isl_ctx_ref(ctx);
1154 if (!space)
1155 return isl_basic_map_free(bmap);
1157 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1158 if (isl_blk_is_error(bmap->block))
1159 goto error;
1161 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1162 if ((n_ineq + n_eq) && !bmap->ineq)
1163 goto error;
1165 if (extra == 0) {
1166 bmap->block2 = isl_blk_empty();
1167 bmap->div = NULL;
1168 } else {
1169 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1170 if (isl_blk_is_error(bmap->block2))
1171 goto error;
1173 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1174 if (!bmap->div)
1175 goto error;
1178 for (i = 0; i < n_ineq + n_eq; ++i)
1179 bmap->ineq[i] = bmap->block.data + i * row_size;
1181 for (i = 0; i < extra; ++i)
1182 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1184 bmap->ref = 1;
1185 bmap->flags = 0;
1186 bmap->c_size = n_eq + n_ineq;
1187 bmap->eq = bmap->ineq + n_ineq;
1188 bmap->extra = extra;
1189 bmap->n_eq = 0;
1190 bmap->n_ineq = 0;
1191 bmap->n_div = 0;
1192 bmap->sample = NULL;
1194 return bmap;
1195 error:
1196 isl_basic_map_free(bmap);
1197 return NULL;
1200 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1201 unsigned nparam, unsigned dim, unsigned extra,
1202 unsigned n_eq, unsigned n_ineq)
1204 struct isl_basic_map *bmap;
1205 isl_space *space;
1207 space = isl_space_set_alloc(ctx, nparam, dim);
1208 if (!space)
1209 return NULL;
1211 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1212 return bset_from_bmap(bmap);
1215 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1216 unsigned extra, unsigned n_eq, unsigned n_ineq)
1218 struct isl_basic_map *bmap;
1219 if (!dim)
1220 return NULL;
1221 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1222 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1223 return bset_from_bmap(bmap);
1224 error:
1225 isl_space_free(dim);
1226 return NULL;
1229 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1230 unsigned extra, unsigned n_eq, unsigned n_ineq)
1232 struct isl_basic_map *bmap;
1234 if (!space)
1235 return NULL;
1236 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1237 if (!bmap)
1238 goto error;
1239 bmap->dim = space;
1241 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1242 error:
1243 isl_space_free(space);
1244 return NULL;
1247 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1248 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1249 unsigned n_eq, unsigned n_ineq)
1251 struct isl_basic_map *bmap;
1252 isl_space *dim;
1254 dim = isl_space_alloc(ctx, nparam, in, out);
1255 if (!dim)
1256 return NULL;
1258 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1259 return bmap;
1262 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1263 __isl_keep isl_basic_map *src)
1265 int i;
1266 unsigned total = isl_basic_map_total_dim(src);
1268 if (!dst)
1269 return NULL;
1271 for (i = 0; i < src->n_eq; ++i) {
1272 int j = isl_basic_map_alloc_equality(dst);
1273 if (j < 0)
1274 return isl_basic_map_free(dst);
1275 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1278 for (i = 0; i < src->n_ineq; ++i) {
1279 int j = isl_basic_map_alloc_inequality(dst);
1280 if (j < 0)
1281 return isl_basic_map_free(dst);
1282 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1285 for (i = 0; i < src->n_div; ++i) {
1286 int j = isl_basic_map_alloc_div(dst);
1287 if (j < 0)
1288 return isl_basic_map_free(dst);
1289 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1291 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1292 return dst;
1295 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1297 struct isl_basic_map *dup;
1299 if (!bmap)
1300 return NULL;
1301 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1302 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1303 dup = dup_constraints(dup, bmap);
1304 if (!dup)
1305 return NULL;
1306 dup->flags = bmap->flags;
1307 dup->sample = isl_vec_copy(bmap->sample);
1308 return dup;
1311 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1313 struct isl_basic_map *dup;
1315 dup = isl_basic_map_dup(bset_to_bmap(bset));
1316 return bset_from_bmap(dup);
1319 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1321 if (!bset)
1322 return NULL;
1324 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1325 bset->ref++;
1326 return bset;
1328 return isl_basic_set_dup(bset);
1331 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1333 if (!set)
1334 return NULL;
1336 set->ref++;
1337 return set;
1340 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1342 if (!bmap)
1343 return NULL;
1345 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1346 bmap->ref++;
1347 return bmap;
1349 bmap = isl_basic_map_dup(bmap);
1350 if (bmap)
1351 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1352 return bmap;
1355 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1357 if (!map)
1358 return NULL;
1360 map->ref++;
1361 return map;
1364 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1366 if (!bmap)
1367 return NULL;
1369 if (--bmap->ref > 0)
1370 return NULL;
1372 isl_ctx_deref(bmap->ctx);
1373 free(bmap->div);
1374 isl_blk_free(bmap->ctx, bmap->block2);
1375 free(bmap->ineq);
1376 isl_blk_free(bmap->ctx, bmap->block);
1377 isl_vec_free(bmap->sample);
1378 isl_space_free(bmap->dim);
1379 free(bmap);
1381 return NULL;
1384 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1386 return isl_basic_map_free(bset_to_bmap(bset));
1389 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1391 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1394 /* Check that "bset" does not involve any parameters.
1396 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1398 if (!bset)
1399 return isl_stat_error;
1400 if (isl_basic_set_dim(bset, isl_dim_param) != 0)
1401 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1402 "basic set should not have any parameters",
1403 return isl_stat_error);
1404 return isl_stat_ok;
1407 /* Check that "bset" does not involve any local variables.
1409 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1411 if (!bset)
1412 return isl_stat_error;
1413 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
1414 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1415 "basic set should not have any local variables",
1416 return isl_stat_error);
1417 return isl_stat_ok;
1420 /* Check that "map" has only named parameters, reporting an error
1421 * if it does not.
1423 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1425 return isl_space_check_named_params(isl_map_peek_space(map));
1428 /* Check that "bmap" has only named parameters, reporting an error
1429 * if it does not.
1431 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1433 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1436 /* Check that "bmap1" and "bmap2" have the same parameters,
1437 * reporting an error if they do not.
1439 static isl_stat isl_basic_map_check_equal_params(
1440 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1442 isl_bool match;
1444 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1445 if (match < 0)
1446 return isl_stat_error;
1447 if (!match)
1448 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1449 "parameters don't match", return isl_stat_error);
1450 return isl_stat_ok;
1453 __isl_give isl_map *isl_map_align_params_map_map_and(
1454 __isl_take isl_map *map1, __isl_take isl_map *map2,
1455 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1456 __isl_take isl_map *map2))
1458 if (!map1 || !map2)
1459 goto error;
1460 if (isl_map_has_equal_params(map1, map2))
1461 return fn(map1, map2);
1462 if (isl_map_check_named_params(map1) < 0)
1463 goto error;
1464 if (isl_map_check_named_params(map2) < 0)
1465 goto error;
1466 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1467 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1468 return fn(map1, map2);
1469 error:
1470 isl_map_free(map1);
1471 isl_map_free(map2);
1472 return NULL;
1475 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1476 __isl_keep isl_map *map2,
1477 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1479 isl_bool r;
1481 if (!map1 || !map2)
1482 return isl_bool_error;
1483 if (isl_map_has_equal_params(map1, map2))
1484 return fn(map1, map2);
1485 if (isl_map_check_named_params(map1) < 0)
1486 return isl_bool_error;
1487 if (isl_map_check_named_params(map2) < 0)
1488 return isl_bool_error;
1489 map1 = isl_map_copy(map1);
1490 map2 = isl_map_copy(map2);
1491 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1492 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1493 r = fn(map1, map2);
1494 isl_map_free(map1);
1495 isl_map_free(map2);
1496 return r;
1499 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1501 struct isl_ctx *ctx;
1502 if (!bmap)
1503 return -1;
1504 ctx = bmap->ctx;
1505 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1506 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1507 return -1);
1508 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1509 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1510 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1511 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1512 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1513 isl_int *t;
1514 int j = isl_basic_map_alloc_inequality(bmap);
1515 if (j < 0)
1516 return -1;
1517 t = bmap->ineq[j];
1518 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1519 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1520 bmap->eq[-1] = t;
1521 bmap->n_eq++;
1522 bmap->n_ineq--;
1523 bmap->eq--;
1524 return 0;
1526 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1527 bmap->extra - bmap->n_div);
1528 return bmap->n_eq++;
1531 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1533 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1536 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1538 if (!bmap)
1539 return -1;
1540 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1541 bmap->n_eq -= n;
1542 return 0;
1545 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1547 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1550 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1552 isl_int *t;
1553 if (!bmap)
1554 return -1;
1555 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1557 if (pos != bmap->n_eq - 1) {
1558 t = bmap->eq[pos];
1559 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1560 bmap->eq[bmap->n_eq - 1] = t;
1562 bmap->n_eq--;
1563 return 0;
1566 /* Turn inequality "pos" of "bmap" into an equality.
1568 * In particular, we move the inequality in front of the equalities
1569 * and move the last inequality in the position of the moved inequality.
1570 * Note that isl_tab_make_equalities_explicit depends on this particular
1571 * change in the ordering of the constraints.
1573 void isl_basic_map_inequality_to_equality(
1574 struct isl_basic_map *bmap, unsigned pos)
1576 isl_int *t;
1578 t = bmap->ineq[pos];
1579 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1580 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1581 bmap->eq[-1] = t;
1582 bmap->n_eq++;
1583 bmap->n_ineq--;
1584 bmap->eq--;
1585 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1586 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1587 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1588 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1591 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1593 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1596 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1598 struct isl_ctx *ctx;
1599 if (!bmap)
1600 return -1;
1601 ctx = bmap->ctx;
1602 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1603 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1604 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1605 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1606 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1607 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1608 1 + isl_basic_map_total_dim(bmap),
1609 bmap->extra - bmap->n_div);
1610 return bmap->n_ineq++;
1613 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1615 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1618 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1620 if (!bmap)
1621 return -1;
1622 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1623 bmap->n_ineq -= n;
1624 return 0;
1627 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1629 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1632 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1634 isl_int *t;
1635 if (!bmap)
1636 return -1;
1637 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1639 if (pos != bmap->n_ineq - 1) {
1640 t = bmap->ineq[pos];
1641 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1642 bmap->ineq[bmap->n_ineq - 1] = t;
1643 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1645 bmap->n_ineq--;
1646 return 0;
1649 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1651 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1654 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1655 isl_int *eq)
1657 isl_bool empty;
1658 int k;
1660 empty = isl_basic_map_plain_is_empty(bmap);
1661 if (empty < 0)
1662 return isl_basic_map_free(bmap);
1663 if (empty)
1664 return bmap;
1666 bmap = isl_basic_map_cow(bmap);
1667 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1668 if (!bmap)
1669 return NULL;
1670 k = isl_basic_map_alloc_equality(bmap);
1671 if (k < 0)
1672 goto error;
1673 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1674 return bmap;
1675 error:
1676 isl_basic_map_free(bmap);
1677 return NULL;
1680 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1681 isl_int *eq)
1683 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1686 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1687 isl_int *ineq)
1689 int k;
1691 bmap = isl_basic_map_cow(bmap);
1692 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1693 if (!bmap)
1694 return NULL;
1695 k = isl_basic_map_alloc_inequality(bmap);
1696 if (k < 0)
1697 goto error;
1698 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1699 return bmap;
1700 error:
1701 isl_basic_map_free(bmap);
1702 return NULL;
1705 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1706 isl_int *ineq)
1708 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1711 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1713 if (!bmap)
1714 return -1;
1715 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1716 isl_seq_clr(bmap->div[bmap->n_div] +
1717 1 + 1 + isl_basic_map_total_dim(bmap),
1718 bmap->extra - bmap->n_div);
1719 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1720 return bmap->n_div++;
1723 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1725 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1728 #undef TYPE
1729 #define TYPE isl_basic_map
1730 #include "check_type_range_templ.c"
1732 /* Check that there are "n" dimensions of type "type" starting at "first"
1733 * in "bset".
1735 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1736 enum isl_dim_type type, unsigned first, unsigned n)
1738 return isl_basic_map_check_range(bset_to_bmap(bset),
1739 type, first, n);
1742 /* Insert an extra integer division, prescribed by "div", to "bmap"
1743 * at (integer division) position "pos".
1745 * The integer division is first added at the end and then moved
1746 * into the right position.
1748 __isl_give isl_basic_map *isl_basic_map_insert_div(
1749 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1751 int i, k;
1753 bmap = isl_basic_map_cow(bmap);
1754 if (!bmap || !div)
1755 return isl_basic_map_free(bmap);
1757 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1758 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1759 "unexpected size", return isl_basic_map_free(bmap));
1760 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1761 return isl_basic_map_free(bmap);
1763 bmap = isl_basic_map_extend_space(bmap,
1764 isl_basic_map_get_space(bmap), 1, 0, 2);
1765 k = isl_basic_map_alloc_div(bmap);
1766 if (k < 0)
1767 return isl_basic_map_free(bmap);
1768 isl_seq_cpy(bmap->div[k], div->el, div->size);
1769 isl_int_set_si(bmap->div[k][div->size], 0);
1771 for (i = k; i > pos; --i)
1772 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1774 return bmap;
1777 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1779 if (!bmap)
1780 return isl_stat_error;
1781 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1782 bmap->n_div -= n;
1783 return isl_stat_ok;
1786 static __isl_give isl_basic_map *add_constraints(
1787 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1788 unsigned i_pos, unsigned o_pos)
1790 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1791 isl_ctx *ctx;
1792 isl_space *space;
1793 struct isl_dim_map *dim_map;
1795 space = isl_basic_map_peek_space(bmap2);
1796 if (!bmap1 || !space)
1797 goto error;
1799 total = isl_basic_map_dim(bmap1, isl_dim_all);
1800 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1801 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1802 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1803 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1804 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1805 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1806 ctx = isl_basic_map_get_ctx(bmap1);
1807 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1808 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1809 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1810 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1811 isl_dim_map_div(dim_map, bmap2, total);
1813 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1814 error:
1815 isl_basic_map_free(bmap1);
1816 isl_basic_map_free(bmap2);
1817 return NULL;
1820 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1821 struct isl_basic_set *bset2, unsigned pos)
1823 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1824 bset_to_bmap(bset2), 0, pos));
1827 __isl_give isl_basic_map *isl_basic_map_extend_space(
1828 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1829 unsigned extra, unsigned n_eq, unsigned n_ineq)
1831 struct isl_basic_map *ext;
1832 unsigned flags;
1833 int dims_ok;
1835 if (!space)
1836 goto error;
1838 if (!base)
1839 goto error;
1841 dims_ok = isl_space_is_equal(base->dim, space) &&
1842 base->extra >= base->n_div + extra;
1844 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1845 room_for_ineq(base, n_ineq)) {
1846 isl_space_free(space);
1847 return base;
1850 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1851 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1852 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1853 extra += base->extra;
1854 n_eq += base->n_eq;
1855 n_ineq += base->n_ineq;
1857 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1858 space = NULL;
1859 if (!ext)
1860 goto error;
1862 if (dims_ok)
1863 ext->sample = isl_vec_copy(base->sample);
1864 flags = base->flags;
1865 ext = add_constraints(ext, base, 0, 0);
1866 if (ext) {
1867 ext->flags = flags;
1868 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1871 return ext;
1873 error:
1874 isl_space_free(space);
1875 isl_basic_map_free(base);
1876 return NULL;
1879 __isl_give isl_basic_set *isl_basic_set_extend_space(
1880 __isl_take isl_basic_set *base,
1881 __isl_take isl_space *dim, unsigned extra,
1882 unsigned n_eq, unsigned n_ineq)
1884 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1885 dim, extra, n_eq, n_ineq));
1888 struct isl_basic_map *isl_basic_map_extend_constraints(
1889 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1891 if (!base)
1892 return NULL;
1893 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1894 0, n_eq, n_ineq);
1897 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1898 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1899 unsigned n_eq, unsigned n_ineq)
1901 struct isl_basic_map *bmap;
1902 isl_space *dim;
1904 if (!base)
1905 return NULL;
1906 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1907 if (!dim)
1908 goto error;
1910 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1911 return bmap;
1912 error:
1913 isl_basic_map_free(base);
1914 return NULL;
1917 struct isl_basic_set *isl_basic_set_extend_constraints(
1918 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1920 isl_basic_map *bmap = bset_to_bmap(base);
1921 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1922 return bset_from_bmap(bmap);
1925 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1927 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1930 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1932 if (!bmap)
1933 return NULL;
1935 if (bmap->ref > 1) {
1936 bmap->ref--;
1937 bmap = isl_basic_map_dup(bmap);
1939 if (bmap) {
1940 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1941 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1943 return bmap;
1946 /* Clear all cached information in "map", either because it is about
1947 * to be modified or because it is being freed.
1948 * Always return the same pointer that is passed in.
1949 * This is needed for the use in isl_map_free.
1951 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1953 isl_basic_map_free(map->cached_simple_hull[0]);
1954 isl_basic_map_free(map->cached_simple_hull[1]);
1955 map->cached_simple_hull[0] = NULL;
1956 map->cached_simple_hull[1] = NULL;
1957 return map;
1960 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1962 return isl_map_cow(set);
1965 /* Return an isl_map that is equal to "map" and that has only
1966 * a single reference.
1968 * If the original input already has only one reference, then
1969 * simply return it, but clear all cached information, since
1970 * it may be rendered invalid by the operations that will be
1971 * performed on the result.
1973 * Otherwise, create a duplicate (without any cached information).
1975 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1977 if (!map)
1978 return NULL;
1980 if (map->ref == 1)
1981 return clear_caches(map);
1982 map->ref--;
1983 return isl_map_dup(map);
1986 static void swap_vars(struct isl_blk blk, isl_int *a,
1987 unsigned a_len, unsigned b_len)
1989 isl_seq_cpy(blk.data, a+a_len, b_len);
1990 isl_seq_cpy(blk.data+b_len, a, a_len);
1991 isl_seq_cpy(a, blk.data, b_len+a_len);
1994 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1995 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1997 int i;
1998 struct isl_blk blk;
2000 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2001 goto error;
2003 if (n1 == 0 || n2 == 0)
2004 return bmap;
2006 bmap = isl_basic_map_cow(bmap);
2007 if (!bmap)
2008 return NULL;
2010 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2011 if (isl_blk_is_error(blk))
2012 goto error;
2014 for (i = 0; i < bmap->n_eq; ++i)
2015 swap_vars(blk,
2016 bmap->eq[i] + pos, n1, n2);
2018 for (i = 0; i < bmap->n_ineq; ++i)
2019 swap_vars(blk,
2020 bmap->ineq[i] + pos, n1, n2);
2022 for (i = 0; i < bmap->n_div; ++i)
2023 swap_vars(blk,
2024 bmap->div[i]+1 + pos, n1, n2);
2026 isl_blk_free(bmap->ctx, blk);
2028 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2029 bmap = isl_basic_map_gauss(bmap, NULL);
2030 return isl_basic_map_finalize(bmap);
2031 error:
2032 isl_basic_map_free(bmap);
2033 return NULL;
2036 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2037 __isl_take isl_basic_map *bmap)
2039 int i = 0;
2040 unsigned total;
2041 if (!bmap)
2042 goto error;
2043 total = isl_basic_map_total_dim(bmap);
2044 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2045 return isl_basic_map_free(bmap);
2046 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2047 if (bmap->n_eq > 0)
2048 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2049 else {
2050 i = isl_basic_map_alloc_equality(bmap);
2051 if (i < 0)
2052 goto error;
2054 isl_int_set_si(bmap->eq[i][0], 1);
2055 isl_seq_clr(bmap->eq[i]+1, total);
2056 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2057 isl_vec_free(bmap->sample);
2058 bmap->sample = NULL;
2059 return isl_basic_map_finalize(bmap);
2060 error:
2061 isl_basic_map_free(bmap);
2062 return NULL;
2065 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2066 __isl_take isl_basic_set *bset)
2068 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2071 __isl_give isl_basic_map *isl_basic_map_set_rational(
2072 __isl_take isl_basic_map *bmap)
2074 if (!bmap)
2075 return NULL;
2077 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2078 return bmap;
2080 bmap = isl_basic_map_cow(bmap);
2081 if (!bmap)
2082 return NULL;
2084 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2086 return isl_basic_map_finalize(bmap);
2089 __isl_give isl_basic_set *isl_basic_set_set_rational(
2090 __isl_take isl_basic_set *bset)
2092 return isl_basic_map_set_rational(bset);
2095 __isl_give isl_basic_set *isl_basic_set_set_integral(
2096 __isl_take isl_basic_set *bset)
2098 if (!bset)
2099 return NULL;
2101 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2102 return bset;
2104 bset = isl_basic_set_cow(bset);
2105 if (!bset)
2106 return NULL;
2108 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2110 return isl_basic_set_finalize(bset);
2113 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2115 int i;
2117 map = isl_map_cow(map);
2118 if (!map)
2119 return NULL;
2120 for (i = 0; i < map->n; ++i) {
2121 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2122 if (!map->p[i])
2123 goto error;
2125 return map;
2126 error:
2127 isl_map_free(map);
2128 return NULL;
2131 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2133 return isl_map_set_rational(set);
2136 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2137 * of "bmap").
2139 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2141 isl_int *t = bmap->div[a];
2142 bmap->div[a] = bmap->div[b];
2143 bmap->div[b] = t;
2146 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2147 * div definitions accordingly.
2149 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2150 int a, int b)
2152 int i;
2153 int off;
2155 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2156 if (off < 0)
2157 return isl_basic_map_free(bmap);
2159 swap_div(bmap, a, b);
2161 for (i = 0; i < bmap->n_eq; ++i)
2162 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2164 for (i = 0; i < bmap->n_ineq; ++i)
2165 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2167 for (i = 0; i < bmap->n_div; ++i)
2168 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2169 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2171 return bmap;
2174 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2176 isl_seq_cpy(c, c + n, rem);
2177 isl_seq_clr(c + rem, n);
2180 /* Drop n dimensions starting at first.
2182 * In principle, this frees up some extra variables as the number
2183 * of columns remains constant, but we would have to extend
2184 * the div array too as the number of rows in this array is assumed
2185 * to be equal to extra.
2187 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2188 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2190 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2193 /* Move "n" divs starting at "first" to the end of the list of divs.
2195 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2196 unsigned first, unsigned n)
2198 isl_int **div;
2199 int i;
2201 if (first + n == bmap->n_div)
2202 return bmap;
2204 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2205 if (!div)
2206 goto error;
2207 for (i = 0; i < n; ++i)
2208 div[i] = bmap->div[first + i];
2209 for (i = 0; i < bmap->n_div - first - n; ++i)
2210 bmap->div[first + i] = bmap->div[first + n + i];
2211 for (i = 0; i < n; ++i)
2212 bmap->div[bmap->n_div - n + i] = div[i];
2213 free(div);
2214 return bmap;
2215 error:
2216 isl_basic_map_free(bmap);
2217 return NULL;
2220 #undef TYPE
2221 #define TYPE isl_map
2222 static
2223 #include "check_type_range_templ.c"
2225 /* Check that there are "n" dimensions of type "type" starting at "first"
2226 * in "set".
2228 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2229 enum isl_dim_type type, unsigned first, unsigned n)
2231 return isl_map_check_range(set_to_map(set), type, first, n);
2234 /* Drop "n" dimensions of type "type" starting at "first".
2235 * Perform the core computation, without cowing or
2236 * simplifying and finalizing the result.
2238 * In principle, this frees up some extra variables as the number
2239 * of columns remains constant, but we would have to extend
2240 * the div array too as the number of rows in this array is assumed
2241 * to be equal to extra.
2243 __isl_give isl_basic_map *isl_basic_map_drop_core(
2244 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2245 unsigned first, unsigned n)
2247 int i;
2248 unsigned offset;
2249 unsigned left;
2251 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2252 return isl_basic_map_free(bmap);
2254 offset = isl_basic_map_offset(bmap, type) + first;
2255 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2256 for (i = 0; i < bmap->n_eq; ++i)
2257 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2259 for (i = 0; i < bmap->n_ineq; ++i)
2260 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2262 for (i = 0; i < bmap->n_div; ++i)
2263 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2265 if (type == isl_dim_div) {
2266 bmap = move_divs_last(bmap, first, n);
2267 if (!bmap)
2268 return NULL;
2269 if (isl_basic_map_free_div(bmap, n) < 0)
2270 return isl_basic_map_free(bmap);
2271 } else
2272 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2273 if (!bmap->dim)
2274 return isl_basic_map_free(bmap);
2276 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2277 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2278 return bmap;
2281 /* Drop "n" dimensions of type "type" starting at "first".
2283 * In principle, this frees up some extra variables as the number
2284 * of columns remains constant, but we would have to extend
2285 * the div array too as the number of rows in this array is assumed
2286 * to be equal to extra.
2288 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2289 enum isl_dim_type type, unsigned first, unsigned n)
2291 if (!bmap)
2292 return NULL;
2293 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2294 return bmap;
2296 bmap = isl_basic_map_cow(bmap);
2297 if (!bmap)
2298 return NULL;
2300 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2302 bmap = isl_basic_map_simplify(bmap);
2303 return isl_basic_map_finalize(bmap);
2306 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2307 enum isl_dim_type type, unsigned first, unsigned n)
2309 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2310 type, first, n));
2313 /* No longer consider "map" to be normalized.
2315 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2317 if (!map)
2318 return NULL;
2319 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2320 return map;
2323 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2324 enum isl_dim_type type, unsigned first, unsigned n)
2326 int i;
2328 if (isl_map_check_range(map, type, first, n) < 0)
2329 return isl_map_free(map);
2331 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2332 return map;
2333 map = isl_map_cow(map);
2334 if (!map)
2335 goto error;
2336 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2337 if (!map->dim)
2338 goto error;
2340 for (i = 0; i < map->n; ++i) {
2341 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2342 if (!map->p[i])
2343 goto error;
2345 map = isl_map_unmark_normalized(map);
2347 return map;
2348 error:
2349 isl_map_free(map);
2350 return NULL;
2353 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2354 enum isl_dim_type type, unsigned first, unsigned n)
2356 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2359 /* Drop the integer division at position "div", which is assumed
2360 * not to appear in any of the constraints or
2361 * in any of the other integer divisions.
2363 * Since the integer division is redundant, there is no need to cow.
2365 __isl_give isl_basic_map *isl_basic_map_drop_div(
2366 __isl_take isl_basic_map *bmap, unsigned div)
2368 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2371 /* Eliminate the specified n dimensions starting at first from the
2372 * constraints, without removing the dimensions from the space.
2373 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2375 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2376 enum isl_dim_type type, unsigned first, unsigned n)
2378 int i;
2380 if (n == 0)
2381 return map;
2383 if (isl_map_check_range(map, type, first, n) < 0)
2384 return isl_map_free(map);
2386 map = isl_map_cow(map);
2387 if (!map)
2388 return NULL;
2390 for (i = 0; i < map->n; ++i) {
2391 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2392 if (!map->p[i])
2393 goto error;
2395 return map;
2396 error:
2397 isl_map_free(map);
2398 return NULL;
2401 /* Eliminate the specified n dimensions starting at first from the
2402 * constraints, without removing the dimensions from the space.
2403 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2405 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2406 enum isl_dim_type type, unsigned first, unsigned n)
2408 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2411 /* Eliminate the specified n dimensions starting at first from the
2412 * constraints, without removing the dimensions from the space.
2413 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2415 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2416 unsigned first, unsigned n)
2418 return isl_set_eliminate(set, isl_dim_set, first, n);
2421 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2422 __isl_take isl_basic_map *bmap)
2424 int v_div;
2426 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2427 if (v_div < 0)
2428 return isl_basic_map_free(bmap);
2429 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2430 if (!bmap)
2431 return NULL;
2432 bmap->n_div = 0;
2433 return isl_basic_map_finalize(bmap);
2436 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2437 __isl_take isl_basic_set *bset)
2439 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2442 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2444 int i;
2446 if (!map)
2447 return NULL;
2448 if (map->n == 0)
2449 return map;
2451 map = isl_map_cow(map);
2452 if (!map)
2453 return NULL;
2455 for (i = 0; i < map->n; ++i) {
2456 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2457 if (!map->p[i])
2458 goto error;
2460 return map;
2461 error:
2462 isl_map_free(map);
2463 return NULL;
2466 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2468 return isl_map_remove_divs(set);
2471 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2472 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2473 unsigned first, unsigned n)
2475 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2476 return isl_basic_map_free(bmap);
2477 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2478 return bmap;
2479 bmap = isl_basic_map_eliminate_vars(bmap,
2480 isl_basic_map_offset(bmap, type) - 1 + first, n);
2481 if (!bmap)
2482 return bmap;
2483 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2484 return bmap;
2485 bmap = isl_basic_map_drop(bmap, type, first, n);
2486 return bmap;
2489 /* Return true if the definition of the given div (recursively) involves
2490 * any of the given variables.
2492 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2493 unsigned first, unsigned n)
2495 int i;
2496 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2498 if (isl_int_is_zero(bmap->div[div][0]))
2499 return isl_bool_false;
2500 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2501 return isl_bool_true;
2503 for (i = bmap->n_div - 1; i >= 0; --i) {
2504 isl_bool involves;
2506 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2507 continue;
2508 involves = div_involves_vars(bmap, i, first, n);
2509 if (involves < 0 || involves)
2510 return involves;
2513 return isl_bool_false;
2516 /* Try and add a lower and/or upper bound on "div" to "bmap"
2517 * based on inequality "i".
2518 * "total" is the total number of variables (excluding the divs).
2519 * "v" is a temporary object that can be used during the calculations.
2520 * If "lb" is set, then a lower bound should be constructed.
2521 * If "ub" is set, then an upper bound should be constructed.
2523 * The calling function has already checked that the inequality does not
2524 * reference "div", but we still need to check that the inequality is
2525 * of the right form. We'll consider the case where we want to construct
2526 * a lower bound. The construction of upper bounds is similar.
2528 * Let "div" be of the form
2530 * q = floor((a + f(x))/d)
2532 * We essentially check if constraint "i" is of the form
2534 * b + f(x) >= 0
2536 * so that we can use it to derive a lower bound on "div".
2537 * However, we allow a slightly more general form
2539 * b + g(x) >= 0
2541 * with the condition that the coefficients of g(x) - f(x) are all
2542 * divisible by d.
2543 * Rewriting this constraint as
2545 * 0 >= -b - g(x)
2547 * adding a + f(x) to both sides and dividing by d, we obtain
2549 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2551 * Taking the floor on both sides, we obtain
2553 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2555 * or
2557 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2559 * In the case of an upper bound, we construct the constraint
2561 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2564 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2565 __isl_take isl_basic_map *bmap, int div, int i,
2566 unsigned total, isl_int v, int lb, int ub)
2568 int j;
2570 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2571 if (lb) {
2572 isl_int_sub(v, bmap->ineq[i][1 + j],
2573 bmap->div[div][1 + 1 + j]);
2574 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2576 if (ub) {
2577 isl_int_add(v, bmap->ineq[i][1 + j],
2578 bmap->div[div][1 + 1 + j]);
2579 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2582 if (!lb && !ub)
2583 return bmap;
2585 bmap = isl_basic_map_cow(bmap);
2586 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2587 if (lb) {
2588 int k = isl_basic_map_alloc_inequality(bmap);
2589 if (k < 0)
2590 goto error;
2591 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2592 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2593 bmap->div[div][1 + j]);
2594 isl_int_cdiv_q(bmap->ineq[k][j],
2595 bmap->ineq[k][j], bmap->div[div][0]);
2597 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2599 if (ub) {
2600 int k = isl_basic_map_alloc_inequality(bmap);
2601 if (k < 0)
2602 goto error;
2603 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2604 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2605 bmap->div[div][1 + j]);
2606 isl_int_fdiv_q(bmap->ineq[k][j],
2607 bmap->ineq[k][j], bmap->div[div][0]);
2609 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2612 return bmap;
2613 error:
2614 isl_basic_map_free(bmap);
2615 return NULL;
2618 /* This function is called right before "div" is eliminated from "bmap"
2619 * using Fourier-Motzkin.
2620 * Look through the constraints of "bmap" for constraints on the argument
2621 * of the integer division and use them to construct constraints on the
2622 * integer division itself. These constraints can then be combined
2623 * during the Fourier-Motzkin elimination.
2624 * Note that it is only useful to introduce lower bounds on "div"
2625 * if "bmap" already contains upper bounds on "div" as the newly
2626 * introduce lower bounds can then be combined with the pre-existing
2627 * upper bounds. Similarly for upper bounds.
2628 * We therefore first check if "bmap" contains any lower and/or upper bounds
2629 * on "div".
2631 * It is interesting to note that the introduction of these constraints
2632 * can indeed lead to more accurate results, even when compared to
2633 * deriving constraints on the argument of "div" from constraints on "div".
2634 * Consider, for example, the set
2636 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2638 * The second constraint can be rewritten as
2640 * 2 * [(-i-2j+3)/4] + k >= 0
2642 * from which we can derive
2644 * -i - 2j + 3 >= -2k
2646 * or
2648 * i + 2j <= 3 + 2k
2650 * Combined with the first constraint, we obtain
2652 * -3 <= 3 + 2k or k >= -3
2654 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2655 * the first constraint, we obtain
2657 * [(i + 2j)/4] >= [-3/4] = -1
2659 * Combining this constraint with the second constraint, we obtain
2661 * k >= -2
2663 static __isl_give isl_basic_map *insert_bounds_on_div(
2664 __isl_take isl_basic_map *bmap, int div)
2666 int i;
2667 int check_lb, check_ub;
2668 isl_int v;
2669 int v_div;
2671 if (!bmap)
2672 return NULL;
2674 if (isl_int_is_zero(bmap->div[div][0]))
2675 return bmap;
2677 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2678 if (v_div < 0)
2679 return isl_basic_map_free(bmap);
2681 check_lb = 0;
2682 check_ub = 0;
2683 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2684 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2685 if (s > 0)
2686 check_ub = 1;
2687 if (s < 0)
2688 check_lb = 1;
2691 if (!check_lb && !check_ub)
2692 return bmap;
2694 isl_int_init(v);
2696 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2697 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2698 continue;
2700 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2701 check_lb, check_ub);
2704 isl_int_clear(v);
2706 return bmap;
2709 /* Remove all divs (recursively) involving any of the given dimensions
2710 * in their definitions.
2712 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2713 __isl_take isl_basic_map *bmap,
2714 enum isl_dim_type type, unsigned first, unsigned n)
2716 int i;
2718 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2719 return isl_basic_map_free(bmap);
2720 first += isl_basic_map_offset(bmap, type);
2722 for (i = bmap->n_div - 1; i >= 0; --i) {
2723 isl_bool involves;
2725 involves = div_involves_vars(bmap, i, first, n);
2726 if (involves < 0)
2727 return isl_basic_map_free(bmap);
2728 if (!involves)
2729 continue;
2730 bmap = insert_bounds_on_div(bmap, i);
2731 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2732 if (!bmap)
2733 return NULL;
2734 i = bmap->n_div;
2737 return bmap;
2740 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2741 __isl_take isl_basic_set *bset,
2742 enum isl_dim_type type, unsigned first, unsigned n)
2744 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2747 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2748 enum isl_dim_type type, unsigned first, unsigned n)
2750 int i;
2752 if (!map)
2753 return NULL;
2754 if (map->n == 0)
2755 return map;
2757 map = isl_map_cow(map);
2758 if (!map)
2759 return NULL;
2761 for (i = 0; i < map->n; ++i) {
2762 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2763 type, first, n);
2764 if (!map->p[i])
2765 goto error;
2767 return map;
2768 error:
2769 isl_map_free(map);
2770 return NULL;
2773 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2774 enum isl_dim_type type, unsigned first, unsigned n)
2776 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2777 type, first, n));
2780 /* Does the description of "bmap" depend on the specified dimensions?
2781 * We also check whether the dimensions appear in any of the div definitions.
2782 * In principle there is no need for this check. If the dimensions appear
2783 * in a div definition, they also appear in the defining constraints of that
2784 * div.
2786 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2787 enum isl_dim_type type, unsigned first, unsigned n)
2789 int i;
2791 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2792 return isl_bool_error;
2794 first += isl_basic_map_offset(bmap, type);
2795 for (i = 0; i < bmap->n_eq; ++i)
2796 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2797 return isl_bool_true;
2798 for (i = 0; i < bmap->n_ineq; ++i)
2799 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2800 return isl_bool_true;
2801 for (i = 0; i < bmap->n_div; ++i) {
2802 if (isl_int_is_zero(bmap->div[i][0]))
2803 continue;
2804 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2805 return isl_bool_true;
2808 return isl_bool_false;
2811 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2812 enum isl_dim_type type, unsigned first, unsigned n)
2814 int i;
2816 if (isl_map_check_range(map, type, first, n) < 0)
2817 return isl_bool_error;
2819 for (i = 0; i < map->n; ++i) {
2820 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2821 type, first, n);
2822 if (involves < 0 || involves)
2823 return involves;
2826 return isl_bool_false;
2829 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2830 enum isl_dim_type type, unsigned first, unsigned n)
2832 return isl_basic_map_involves_dims(bset, type, first, n);
2835 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2836 enum isl_dim_type type, unsigned first, unsigned n)
2838 return isl_map_involves_dims(set, type, first, n);
2841 /* Drop all constraints in bmap that involve any of the dimensions
2842 * first to first+n-1.
2843 * This function only performs the actual removal of constraints.
2845 * This function should not call finalize since it is used by
2846 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2848 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2849 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2851 int i;
2853 if (n == 0)
2854 return bmap;
2856 bmap = isl_basic_map_cow(bmap);
2858 if (!bmap)
2859 return NULL;
2861 for (i = bmap->n_eq - 1; i >= 0; --i) {
2862 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2863 continue;
2864 isl_basic_map_drop_equality(bmap, i);
2867 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2868 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2869 continue;
2870 isl_basic_map_drop_inequality(bmap, i);
2873 return bmap;
2876 /* Drop all constraints in bset that involve any of the dimensions
2877 * first to first+n-1.
2878 * This function only performs the actual removal of constraints.
2880 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2881 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2883 return isl_basic_map_drop_constraints_involving(bset, first, n);
2886 /* Drop all constraints in bmap that do not involve any of the dimensions
2887 * first to first + n - 1 of the given type.
2889 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2890 __isl_take isl_basic_map *bmap,
2891 enum isl_dim_type type, unsigned first, unsigned n)
2893 int i;
2895 if (n == 0) {
2896 isl_space *space = isl_basic_map_get_space(bmap);
2897 isl_basic_map_free(bmap);
2898 return isl_basic_map_universe(space);
2900 bmap = isl_basic_map_cow(bmap);
2901 if (!bmap)
2902 return NULL;
2904 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2905 return isl_basic_map_free(bmap);
2907 first += isl_basic_map_offset(bmap, type) - 1;
2909 for (i = bmap->n_eq - 1; i >= 0; --i) {
2910 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2911 continue;
2912 isl_basic_map_drop_equality(bmap, i);
2915 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2916 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2917 continue;
2918 isl_basic_map_drop_inequality(bmap, i);
2921 bmap = isl_basic_map_add_known_div_constraints(bmap);
2922 return bmap;
2925 /* Drop all constraints in bset that do not involve any of the dimensions
2926 * first to first + n - 1 of the given type.
2928 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2929 __isl_take isl_basic_set *bset,
2930 enum isl_dim_type type, unsigned first, unsigned n)
2932 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2933 type, first, n);
2936 /* Drop all constraints in bmap that involve any of the dimensions
2937 * first to first + n - 1 of the given type.
2939 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2940 __isl_take isl_basic_map *bmap,
2941 enum isl_dim_type type, unsigned first, unsigned n)
2943 if (!bmap)
2944 return NULL;
2945 if (n == 0)
2946 return bmap;
2948 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2949 return isl_basic_map_free(bmap);
2951 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2952 first += isl_basic_map_offset(bmap, type) - 1;
2953 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2954 bmap = isl_basic_map_add_known_div_constraints(bmap);
2955 return bmap;
2958 /* Drop all constraints in bset that involve any of the dimensions
2959 * first to first + n - 1 of the given type.
2961 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2962 __isl_take isl_basic_set *bset,
2963 enum isl_dim_type type, unsigned first, unsigned n)
2965 return isl_basic_map_drop_constraints_involving_dims(bset,
2966 type, first, n);
2969 /* Drop constraints from "map" by applying "drop" to each basic map.
2971 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2972 enum isl_dim_type type, unsigned first, unsigned n,
2973 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2974 enum isl_dim_type type, unsigned first, unsigned n))
2976 int i;
2978 if (isl_map_check_range(map, type, first, n) < 0)
2979 return isl_map_free(map);
2981 map = isl_map_cow(map);
2982 if (!map)
2983 return NULL;
2985 for (i = 0; i < map->n; ++i) {
2986 map->p[i] = drop(map->p[i], type, first, n);
2987 if (!map->p[i])
2988 return isl_map_free(map);
2991 if (map->n > 1)
2992 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2994 return map;
2997 /* Drop all constraints in map that involve any of the dimensions
2998 * first to first + n - 1 of the given type.
3000 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3001 __isl_take isl_map *map,
3002 enum isl_dim_type type, unsigned first, unsigned n)
3004 if (n == 0)
3005 return map;
3006 return drop_constraints(map, type, first, n,
3007 &isl_basic_map_drop_constraints_involving_dims);
3010 /* Drop all constraints in "map" that do not involve any of the dimensions
3011 * first to first + n - 1 of the given type.
3013 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3014 __isl_take isl_map *map,
3015 enum isl_dim_type type, unsigned first, unsigned n)
3017 if (n == 0) {
3018 isl_space *space = isl_map_get_space(map);
3019 isl_map_free(map);
3020 return isl_map_universe(space);
3022 return drop_constraints(map, type, first, n,
3023 &isl_basic_map_drop_constraints_not_involving_dims);
3026 /* Drop all constraints in set that involve any of the dimensions
3027 * first to first + n - 1 of the given type.
3029 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3030 __isl_take isl_set *set,
3031 enum isl_dim_type type, unsigned first, unsigned n)
3033 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3036 /* Drop all constraints in "set" that do not involve any of the dimensions
3037 * first to first + n - 1 of the given type.
3039 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3040 __isl_take isl_set *set,
3041 enum isl_dim_type type, unsigned first, unsigned n)
3043 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3046 /* Does local variable "div" of "bmap" have a complete explicit representation?
3047 * Having a complete explicit representation requires not only
3048 * an explicit representation, but also that all local variables
3049 * that appear in this explicit representation in turn have
3050 * a complete explicit representation.
3052 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3054 int i;
3055 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3056 isl_bool marked;
3058 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3059 if (marked < 0 || marked)
3060 return isl_bool_not(marked);
3062 for (i = bmap->n_div - 1; i >= 0; --i) {
3063 isl_bool known;
3065 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3066 continue;
3067 known = isl_basic_map_div_is_known(bmap, i);
3068 if (known < 0 || !known)
3069 return known;
3072 return isl_bool_true;
3075 /* Remove all divs that are unknown or defined in terms of unknown divs.
3077 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3078 __isl_take isl_basic_map *bmap)
3080 int i;
3082 if (!bmap)
3083 return NULL;
3085 for (i = bmap->n_div - 1; i >= 0; --i) {
3086 if (isl_basic_map_div_is_known(bmap, i))
3087 continue;
3088 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3089 if (!bmap)
3090 return NULL;
3091 i = bmap->n_div;
3094 return bmap;
3097 /* Remove all divs that are unknown or defined in terms of unknown divs.
3099 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3100 __isl_take isl_basic_set *bset)
3102 return isl_basic_map_remove_unknown_divs(bset);
3105 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3107 int i;
3109 if (!map)
3110 return NULL;
3111 if (map->n == 0)
3112 return map;
3114 map = isl_map_cow(map);
3115 if (!map)
3116 return NULL;
3118 for (i = 0; i < map->n; ++i) {
3119 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3120 if (!map->p[i])
3121 goto error;
3123 return map;
3124 error:
3125 isl_map_free(map);
3126 return NULL;
3129 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3131 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3134 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3135 __isl_take isl_basic_set *bset,
3136 enum isl_dim_type type, unsigned first, unsigned n)
3138 isl_basic_map *bmap = bset_to_bmap(bset);
3139 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3140 return bset_from_bmap(bmap);
3143 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3144 enum isl_dim_type type, unsigned first, unsigned n)
3146 int i;
3148 if (n == 0)
3149 return map;
3151 map = isl_map_cow(map);
3152 if (isl_map_check_range(map, type, first, n) < 0)
3153 return isl_map_free(map);
3155 for (i = 0; i < map->n; ++i) {
3156 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3157 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3158 if (!map->p[i])
3159 goto error;
3161 map = isl_map_drop(map, type, first, n);
3162 return map;
3163 error:
3164 isl_map_free(map);
3165 return NULL;
3168 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3169 enum isl_dim_type type, unsigned first, unsigned n)
3171 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3172 type, first, n));
3175 /* Project out n inputs starting at first using Fourier-Motzkin */
3176 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3177 unsigned first, unsigned n)
3179 return isl_map_remove_dims(map, isl_dim_in, first, n);
3182 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3183 FILE *out, int indent)
3185 isl_printer *p;
3187 if (!bset) {
3188 fprintf(out, "null basic set\n");
3189 return;
3192 fprintf(out, "%*s", indent, "");
3193 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3194 bset->ref, bset->dim->nparam, bset->dim->n_out,
3195 bset->extra, bset->flags);
3197 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3198 p = isl_printer_set_dump(p, 1);
3199 p = isl_printer_set_indent(p, indent);
3200 p = isl_printer_start_line(p);
3201 p = isl_printer_print_basic_set(p, bset);
3202 p = isl_printer_end_line(p);
3203 isl_printer_free(p);
3206 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3207 FILE *out, int indent)
3209 isl_printer *p;
3211 if (!bmap) {
3212 fprintf(out, "null basic map\n");
3213 return;
3216 fprintf(out, "%*s", indent, "");
3217 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3218 "flags: %x, n_name: %d\n",
3219 bmap->ref,
3220 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3221 bmap->extra, bmap->flags, bmap->dim->n_id);
3223 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3224 p = isl_printer_set_dump(p, 1);
3225 p = isl_printer_set_indent(p, indent);
3226 p = isl_printer_start_line(p);
3227 p = isl_printer_print_basic_map(p, bmap);
3228 p = isl_printer_end_line(p);
3229 isl_printer_free(p);
3232 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3233 unsigned pos)
3235 unsigned total;
3237 if (!bmap)
3238 return NULL;
3239 total = isl_basic_map_total_dim(bmap);
3240 if (pos >= bmap->n_ineq)
3241 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3242 "invalid position", return isl_basic_map_free(bmap));
3243 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3244 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3245 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3246 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3247 return bmap;
3250 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3251 unsigned flags)
3253 if (isl_space_check_is_set(space) < 0)
3254 goto error;
3255 return isl_map_alloc_space(space, n, flags);
3256 error:
3257 isl_space_free(space);
3258 return NULL;
3261 /* Make sure "map" has room for at least "n" more basic maps.
3263 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3265 int i;
3266 struct isl_map *grown = NULL;
3268 if (!map)
3269 return NULL;
3270 isl_assert(map->ctx, n >= 0, goto error);
3271 if (map->n + n <= map->size)
3272 return map;
3273 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3274 if (!grown)
3275 goto error;
3276 for (i = 0; i < map->n; ++i) {
3277 grown->p[i] = isl_basic_map_copy(map->p[i]);
3278 if (!grown->p[i])
3279 goto error;
3280 grown->n++;
3282 isl_map_free(map);
3283 return grown;
3284 error:
3285 isl_map_free(grown);
3286 isl_map_free(map);
3287 return NULL;
3290 /* Make sure "set" has room for at least "n" more basic sets.
3292 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3294 return set_from_map(isl_map_grow(set_to_map(set), n));
3297 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3299 return isl_map_from_basic_map(bset);
3302 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3304 struct isl_map *map;
3306 if (!bmap)
3307 return NULL;
3309 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3310 return isl_map_add_basic_map(map, bmap);
3313 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3314 __isl_take isl_basic_set *bset)
3316 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3317 bset_to_bmap(bset)));
3320 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3322 return isl_map_free(set);
3325 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3327 int i;
3329 if (!set) {
3330 fprintf(out, "null set\n");
3331 return;
3334 fprintf(out, "%*s", indent, "");
3335 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3336 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3337 set->flags);
3338 for (i = 0; i < set->n; ++i) {
3339 fprintf(out, "%*s", indent, "");
3340 fprintf(out, "basic set %d:\n", i);
3341 isl_basic_set_print_internal(set->p[i], out, indent+4);
3345 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3347 int i;
3349 if (!map) {
3350 fprintf(out, "null map\n");
3351 return;
3354 fprintf(out, "%*s", indent, "");
3355 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3356 "flags: %x, n_name: %d\n",
3357 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3358 map->dim->n_out, map->flags, map->dim->n_id);
3359 for (i = 0; i < map->n; ++i) {
3360 fprintf(out, "%*s", indent, "");
3361 fprintf(out, "basic map %d:\n", i);
3362 isl_basic_map_print_internal(map->p[i], out, indent+4);
3366 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3367 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3369 struct isl_basic_map *bmap_domain;
3371 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3372 goto error;
3374 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3375 isl_assert(bset->ctx,
3376 isl_basic_map_compatible_domain(bmap, bset), goto error);
3378 bmap = isl_basic_map_cow(bmap);
3379 if (!bmap)
3380 goto error;
3381 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3382 bset->n_div, bset->n_eq, bset->n_ineq);
3383 bmap_domain = isl_basic_map_from_domain(bset);
3384 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3386 bmap = isl_basic_map_simplify(bmap);
3387 return isl_basic_map_finalize(bmap);
3388 error:
3389 isl_basic_map_free(bmap);
3390 isl_basic_set_free(bset);
3391 return NULL;
3394 /* Check that the space of "bset" is the same as that of the range of "bmap".
3396 static isl_stat isl_basic_map_check_compatible_range(
3397 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3399 isl_bool ok;
3401 ok = isl_basic_map_compatible_range(bmap, bset);
3402 if (ok < 0)
3403 return isl_stat_error;
3404 if (!ok)
3405 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3406 "incompatible spaces", return isl_stat_error);
3408 return isl_stat_ok;
3411 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3412 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3414 struct isl_basic_map *bmap_range;
3416 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3417 goto error;
3419 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3420 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3421 goto error;
3423 if (isl_basic_set_plain_is_universe(bset)) {
3424 isl_basic_set_free(bset);
3425 return bmap;
3428 bmap = isl_basic_map_cow(bmap);
3429 if (!bmap)
3430 goto error;
3431 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3432 bset->n_div, bset->n_eq, bset->n_ineq);
3433 bmap_range = bset_to_bmap(bset);
3434 bmap = add_constraints(bmap, bmap_range, 0, 0);
3436 bmap = isl_basic_map_simplify(bmap);
3437 return isl_basic_map_finalize(bmap);
3438 error:
3439 isl_basic_map_free(bmap);
3440 isl_basic_set_free(bset);
3441 return NULL;
3444 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3445 __isl_keep isl_vec *vec)
3447 int i;
3448 unsigned total;
3449 isl_int s;
3451 if (!bmap || !vec)
3452 return isl_bool_error;
3454 total = 1 + isl_basic_map_total_dim(bmap);
3455 if (total != vec->size)
3456 return isl_bool_false;
3458 isl_int_init(s);
3460 for (i = 0; i < bmap->n_eq; ++i) {
3461 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3462 if (!isl_int_is_zero(s)) {
3463 isl_int_clear(s);
3464 return isl_bool_false;
3468 for (i = 0; i < bmap->n_ineq; ++i) {
3469 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3470 if (isl_int_is_neg(s)) {
3471 isl_int_clear(s);
3472 return isl_bool_false;
3476 isl_int_clear(s);
3478 return isl_bool_true;
3481 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3482 __isl_keep isl_vec *vec)
3484 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3487 __isl_give isl_basic_map *isl_basic_map_intersect(
3488 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3490 struct isl_vec *sample = NULL;
3492 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3493 goto error;
3494 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3495 isl_space_dim(bmap1->dim, isl_dim_param) &&
3496 isl_space_dim(bmap2->dim, isl_dim_all) !=
3497 isl_space_dim(bmap2->dim, isl_dim_param))
3498 return isl_basic_map_intersect(bmap2, bmap1);
3500 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3501 isl_space_dim(bmap2->dim, isl_dim_param))
3502 isl_assert(bmap1->ctx,
3503 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3505 if (isl_basic_map_plain_is_empty(bmap1)) {
3506 isl_basic_map_free(bmap2);
3507 return bmap1;
3509 if (isl_basic_map_plain_is_empty(bmap2)) {
3510 isl_basic_map_free(bmap1);
3511 return bmap2;
3514 if (bmap1->sample &&
3515 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3516 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3517 sample = isl_vec_copy(bmap1->sample);
3518 else if (bmap2->sample &&
3519 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3520 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3521 sample = isl_vec_copy(bmap2->sample);
3523 bmap1 = isl_basic_map_cow(bmap1);
3524 if (!bmap1)
3525 goto error;
3526 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3527 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3528 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3530 if (!bmap1)
3531 isl_vec_free(sample);
3532 else if (sample) {
3533 isl_vec_free(bmap1->sample);
3534 bmap1->sample = sample;
3537 bmap1 = isl_basic_map_simplify(bmap1);
3538 return isl_basic_map_finalize(bmap1);
3539 error:
3540 if (sample)
3541 isl_vec_free(sample);
3542 isl_basic_map_free(bmap1);
3543 isl_basic_map_free(bmap2);
3544 return NULL;
3547 struct isl_basic_set *isl_basic_set_intersect(
3548 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3550 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3551 bset_to_bmap(bset2)));
3554 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3555 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3557 return isl_basic_set_intersect(bset1, bset2);
3560 /* Special case of isl_map_intersect, where both map1 and map2
3561 * are convex, without any divs and such that either map1 or map2
3562 * contains a single constraint. This constraint is then simply
3563 * added to the other map.
3565 static __isl_give isl_map *map_intersect_add_constraint(
3566 __isl_take isl_map *map1, __isl_take isl_map *map2)
3568 isl_assert(map1->ctx, map1->n == 1, goto error);
3569 isl_assert(map2->ctx, map1->n == 1, goto error);
3570 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3571 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3573 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3574 return isl_map_intersect(map2, map1);
3576 map1 = isl_map_cow(map1);
3577 if (!map1)
3578 goto error;
3579 if (isl_map_plain_is_empty(map1)) {
3580 isl_map_free(map2);
3581 return map1;
3583 if (map2->p[0]->n_eq == 1)
3584 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3585 else
3586 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3587 map2->p[0]->ineq[0]);
3589 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3590 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3591 if (!map1->p[0])
3592 goto error;
3594 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3595 isl_basic_map_free(map1->p[0]);
3596 map1->n = 0;
3599 isl_map_free(map2);
3601 map1 = isl_map_unmark_normalized(map1);
3602 return map1;
3603 error:
3604 isl_map_free(map1);
3605 isl_map_free(map2);
3606 return NULL;
3609 /* map2 may be either a parameter domain or a map living in the same
3610 * space as map1.
3612 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3613 __isl_take isl_map *map2)
3615 unsigned flags = 0;
3616 isl_bool equal;
3617 isl_map *result;
3618 int i, j;
3620 if (!map1 || !map2)
3621 goto error;
3623 if ((isl_map_plain_is_empty(map1) ||
3624 isl_map_plain_is_universe(map2)) &&
3625 isl_space_is_equal(map1->dim, map2->dim)) {
3626 isl_map_free(map2);
3627 return map1;
3629 if ((isl_map_plain_is_empty(map2) ||
3630 isl_map_plain_is_universe(map1)) &&
3631 isl_space_is_equal(map1->dim, map2->dim)) {
3632 isl_map_free(map1);
3633 return map2;
3636 if (map1->n == 1 && map2->n == 1 &&
3637 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3638 isl_space_is_equal(map1->dim, map2->dim) &&
3639 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3640 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3641 return map_intersect_add_constraint(map1, map2);
3643 equal = isl_map_plain_is_equal(map1, map2);
3644 if (equal < 0)
3645 goto error;
3646 if (equal) {
3647 isl_map_free(map2);
3648 return map1;
3651 if (isl_space_dim(map2->dim, isl_dim_all) !=
3652 isl_space_dim(map2->dim, isl_dim_param))
3653 isl_assert(map1->ctx,
3654 isl_space_is_equal(map1->dim, map2->dim), goto error);
3656 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3657 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3658 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3660 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3661 map1->n * map2->n, flags);
3662 if (!result)
3663 goto error;
3664 for (i = 0; i < map1->n; ++i)
3665 for (j = 0; j < map2->n; ++j) {
3666 struct isl_basic_map *part;
3667 part = isl_basic_map_intersect(
3668 isl_basic_map_copy(map1->p[i]),
3669 isl_basic_map_copy(map2->p[j]));
3670 if (isl_basic_map_is_empty(part) < 0)
3671 part = isl_basic_map_free(part);
3672 result = isl_map_add_basic_map(result, part);
3673 if (!result)
3674 goto error;
3676 isl_map_free(map1);
3677 isl_map_free(map2);
3678 return result;
3679 error:
3680 isl_map_free(map1);
3681 isl_map_free(map2);
3682 return NULL;
3685 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3686 __isl_take isl_map *map2)
3688 if (!map1 || !map2)
3689 goto error;
3690 if (!isl_space_is_equal(map1->dim, map2->dim))
3691 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3692 "spaces don't match", goto error);
3693 return map_intersect_internal(map1, map2);
3694 error:
3695 isl_map_free(map1);
3696 isl_map_free(map2);
3697 return NULL;
3700 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3701 __isl_take isl_map *map2)
3703 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3706 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3708 return set_from_map(isl_map_intersect(set_to_map(set1),
3709 set_to_map(set2)));
3712 /* map_intersect_internal accepts intersections
3713 * with parameter domains, so we can just call that function.
3715 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3716 __isl_take isl_set *params)
3718 return map_intersect_internal(map, params);
3721 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3722 __isl_take isl_map *map2)
3724 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3727 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3728 __isl_take isl_set *params)
3730 return isl_map_intersect_params(set, params);
3733 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3735 isl_space *space;
3736 unsigned pos, n1, n2;
3738 if (!bmap)
3739 return NULL;
3740 bmap = isl_basic_map_cow(bmap);
3741 if (!bmap)
3742 return NULL;
3743 space = isl_space_reverse(isl_space_copy(bmap->dim));
3744 pos = isl_basic_map_offset(bmap, isl_dim_in);
3745 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3746 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3747 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3748 return isl_basic_map_reset_space(bmap, space);
3751 static __isl_give isl_basic_map *basic_map_space_reset(
3752 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3754 isl_space *space;
3756 if (!bmap)
3757 return NULL;
3758 if (!isl_space_is_named_or_nested(bmap->dim, type))
3759 return bmap;
3761 space = isl_basic_map_get_space(bmap);
3762 space = isl_space_reset(space, type);
3763 bmap = isl_basic_map_reset_space(bmap, space);
3764 return bmap;
3767 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3768 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3769 unsigned pos, unsigned n)
3771 isl_bool rational, is_empty;
3772 isl_space *res_space;
3773 struct isl_basic_map *res;
3774 struct isl_dim_map *dim_map;
3775 unsigned total, off;
3776 enum isl_dim_type t;
3778 if (n == 0)
3779 return basic_map_space_reset(bmap, type);
3781 is_empty = isl_basic_map_plain_is_empty(bmap);
3782 if (is_empty < 0)
3783 return isl_basic_map_free(bmap);
3784 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3785 type, pos, n);
3786 if (!res_space)
3787 return isl_basic_map_free(bmap);
3788 if (is_empty) {
3789 isl_basic_map_free(bmap);
3790 return isl_basic_map_empty(res_space);
3793 total = isl_basic_map_total_dim(bmap) + n;
3794 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3795 off = 0;
3796 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3797 if (t != type) {
3798 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3799 } else {
3800 unsigned size = isl_basic_map_dim(bmap, t);
3801 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3802 0, pos, off);
3803 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3804 pos, size - pos, off + pos + n);
3806 off += isl_space_dim(res_space, t);
3808 isl_dim_map_div(dim_map, bmap, off);
3810 res = isl_basic_map_alloc_space(res_space,
3811 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3812 rational = isl_basic_map_is_rational(bmap);
3813 if (rational < 0)
3814 res = isl_basic_map_free(res);
3815 if (rational)
3816 res = isl_basic_map_set_rational(res);
3817 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3818 return isl_basic_map_finalize(res);
3821 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3822 __isl_take isl_basic_set *bset,
3823 enum isl_dim_type type, unsigned pos, unsigned n)
3825 return isl_basic_map_insert_dims(bset, type, pos, n);
3828 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3829 enum isl_dim_type type, unsigned n)
3831 if (!bmap)
3832 return NULL;
3833 return isl_basic_map_insert_dims(bmap, type,
3834 isl_basic_map_dim(bmap, type), n);
3837 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3838 enum isl_dim_type type, unsigned n)
3840 if (!bset)
3841 return NULL;
3842 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3843 return isl_basic_map_add_dims(bset, type, n);
3844 error:
3845 isl_basic_set_free(bset);
3846 return NULL;
3849 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3850 enum isl_dim_type type)
3852 isl_space *space;
3854 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3855 return map;
3857 space = isl_map_get_space(map);
3858 space = isl_space_reset(space, type);
3859 map = isl_map_reset_space(map, space);
3860 return map;
3863 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3864 enum isl_dim_type type, unsigned pos, unsigned n)
3866 int i;
3868 if (n == 0)
3869 return map_space_reset(map, type);
3871 map = isl_map_cow(map);
3872 if (!map)
3873 return NULL;
3875 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3876 if (!map->dim)
3877 goto error;
3879 for (i = 0; i < map->n; ++i) {
3880 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3881 if (!map->p[i])
3882 goto error;
3885 return map;
3886 error:
3887 isl_map_free(map);
3888 return NULL;
3891 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3892 enum isl_dim_type type, unsigned pos, unsigned n)
3894 return isl_map_insert_dims(set, type, pos, n);
3897 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3898 enum isl_dim_type type, unsigned n)
3900 if (!map)
3901 return NULL;
3902 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3905 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3906 enum isl_dim_type type, unsigned n)
3908 if (!set)
3909 return NULL;
3910 isl_assert(set->ctx, type != isl_dim_in, goto error);
3911 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3912 error:
3913 isl_set_free(set);
3914 return NULL;
3917 __isl_give isl_basic_map *isl_basic_map_move_dims(
3918 __isl_take isl_basic_map *bmap,
3919 enum isl_dim_type dst_type, unsigned dst_pos,
3920 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3922 struct isl_dim_map *dim_map;
3923 struct isl_basic_map *res;
3924 enum isl_dim_type t;
3925 unsigned total, off;
3927 if (!bmap)
3928 return NULL;
3929 if (n == 0) {
3930 bmap = isl_basic_map_reset(bmap, src_type);
3931 bmap = isl_basic_map_reset(bmap, dst_type);
3932 return bmap;
3935 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3936 return isl_basic_map_free(bmap);
3938 if (dst_type == src_type && dst_pos == src_pos)
3939 return bmap;
3941 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3943 if (pos(bmap->dim, dst_type) + dst_pos ==
3944 pos(bmap->dim, src_type) + src_pos +
3945 ((src_type < dst_type) ? n : 0)) {
3946 bmap = isl_basic_map_cow(bmap);
3947 if (!bmap)
3948 return NULL;
3950 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3951 src_type, src_pos, n);
3952 if (!bmap->dim)
3953 goto error;
3955 bmap = isl_basic_map_finalize(bmap);
3957 return bmap;
3960 total = isl_basic_map_total_dim(bmap);
3961 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3963 off = 0;
3964 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3965 unsigned size = isl_space_dim(bmap->dim, t);
3966 if (t == dst_type) {
3967 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3968 0, dst_pos, off);
3969 off += dst_pos;
3970 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3971 src_pos, n, off);
3972 off += n;
3973 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3974 dst_pos, size - dst_pos, off);
3975 off += size - dst_pos;
3976 } else if (t == src_type) {
3977 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3978 0, src_pos, off);
3979 off += src_pos;
3980 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3981 src_pos + n, size - src_pos - n, off);
3982 off += size - src_pos - n;
3983 } else {
3984 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3985 off += size;
3988 isl_dim_map_div(dim_map, bmap, off);
3990 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3991 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3992 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3993 if (!bmap)
3994 goto error;
3996 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3997 src_type, src_pos, n);
3998 if (!bmap->dim)
3999 goto error;
4001 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4002 bmap = isl_basic_map_gauss(bmap, NULL);
4003 bmap = isl_basic_map_finalize(bmap);
4005 return bmap;
4006 error:
4007 isl_basic_map_free(bmap);
4008 return NULL;
4011 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4012 enum isl_dim_type dst_type, unsigned dst_pos,
4013 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4015 isl_basic_map *bmap = bset_to_bmap(bset);
4016 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4017 src_type, src_pos, n);
4018 return bset_from_bmap(bmap);
4021 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4022 enum isl_dim_type dst_type, unsigned dst_pos,
4023 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4025 if (!set)
4026 return NULL;
4027 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4028 return set_from_map(isl_map_move_dims(set_to_map(set),
4029 dst_type, dst_pos, src_type, src_pos, n));
4030 error:
4031 isl_set_free(set);
4032 return NULL;
4035 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4036 enum isl_dim_type dst_type, unsigned dst_pos,
4037 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4039 int i;
4041 if (n == 0) {
4042 map = isl_map_reset(map, src_type);
4043 map = isl_map_reset(map, dst_type);
4044 return map;
4047 if (isl_map_check_range(map, src_type, src_pos, n))
4048 return isl_map_free(map);
4050 if (dst_type == src_type && dst_pos == src_pos)
4051 return map;
4053 isl_assert(map->ctx, dst_type != src_type, goto error);
4055 map = isl_map_cow(map);
4056 if (!map)
4057 return NULL;
4059 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4060 if (!map->dim)
4061 goto error;
4063 for (i = 0; i < map->n; ++i) {
4064 map->p[i] = isl_basic_map_move_dims(map->p[i],
4065 dst_type, dst_pos,
4066 src_type, src_pos, n);
4067 if (!map->p[i])
4068 goto error;
4071 return map;
4072 error:
4073 isl_map_free(map);
4074 return NULL;
4077 /* Move the specified dimensions to the last columns right before
4078 * the divs. Don't change the dimension specification of bmap.
4079 * That's the responsibility of the caller.
4081 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4082 enum isl_dim_type type, unsigned first, unsigned n)
4084 struct isl_dim_map *dim_map;
4085 struct isl_basic_map *res;
4086 enum isl_dim_type t;
4087 unsigned total, off;
4089 if (!bmap)
4090 return NULL;
4091 if (pos(bmap->dim, type) + first + n ==
4092 1 + isl_space_dim(bmap->dim, isl_dim_all))
4093 return bmap;
4095 total = isl_basic_map_total_dim(bmap);
4096 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4098 off = 0;
4099 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4100 unsigned size = isl_space_dim(bmap->dim, t);
4101 if (t == type) {
4102 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4103 0, first, off);
4104 off += first;
4105 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4106 first, n, total - bmap->n_div - n);
4107 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4108 first + n, size - (first + n), off);
4109 off += size - (first + n);
4110 } else {
4111 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4112 off += size;
4115 isl_dim_map_div(dim_map, bmap, off + n);
4117 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4118 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4119 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4120 return res;
4123 /* Insert "n" rows in the divs of "bmap".
4125 * The number of columns is not changed, which means that the last
4126 * dimensions of "bmap" are being reintepreted as the new divs.
4127 * The space of "bmap" is not adjusted, however, which means
4128 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4129 * from the space of "bmap" is the responsibility of the caller.
4131 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4132 int n)
4134 int i;
4135 size_t row_size;
4136 isl_int **new_div;
4137 isl_int *old;
4139 bmap = isl_basic_map_cow(bmap);
4140 if (!bmap)
4141 return NULL;
4143 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4144 old = bmap->block2.data;
4145 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4146 (bmap->extra + n) * (1 + row_size));
4147 if (!bmap->block2.data)
4148 return isl_basic_map_free(bmap);
4149 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4150 if (!new_div)
4151 return isl_basic_map_free(bmap);
4152 for (i = 0; i < n; ++i) {
4153 new_div[i] = bmap->block2.data +
4154 (bmap->extra + i) * (1 + row_size);
4155 isl_seq_clr(new_div[i], 1 + row_size);
4157 for (i = 0; i < bmap->extra; ++i)
4158 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4159 free(bmap->div);
4160 bmap->div = new_div;
4161 bmap->n_div += n;
4162 bmap->extra += n;
4164 return bmap;
4167 /* Drop constraints from "bmap" that only involve the variables
4168 * of "type" in the range [first, first + n] that are not related
4169 * to any of the variables outside that interval.
4170 * These constraints cannot influence the values for the variables
4171 * outside the interval, except in case they cause "bmap" to be empty.
4172 * Only drop the constraints if "bmap" is known to be non-empty.
4174 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4175 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4176 unsigned first, unsigned n)
4178 int i;
4179 int *groups;
4180 unsigned dim, n_div;
4181 isl_bool non_empty;
4183 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4184 if (non_empty < 0)
4185 return isl_basic_map_free(bmap);
4186 if (!non_empty)
4187 return bmap;
4189 dim = isl_basic_map_dim(bmap, isl_dim_all);
4190 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4191 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4192 if (!groups)
4193 return isl_basic_map_free(bmap);
4194 first += isl_basic_map_offset(bmap, type) - 1;
4195 for (i = 0; i < first; ++i)
4196 groups[i] = -1;
4197 for (i = first + n; i < dim - n_div; ++i)
4198 groups[i] = -1;
4200 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4202 return bmap;
4205 /* Turn the n dimensions of type type, starting at first
4206 * into existentially quantified variables.
4208 * If a subset of the projected out variables are unrelated
4209 * to any of the variables that remain, then the constraints
4210 * involving this subset are simply dropped first.
4212 __isl_give isl_basic_map *isl_basic_map_project_out(
4213 __isl_take isl_basic_map *bmap,
4214 enum isl_dim_type type, unsigned first, unsigned n)
4216 isl_bool empty;
4218 if (n == 0)
4219 return basic_map_space_reset(bmap, type);
4220 if (type == isl_dim_div)
4221 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4222 "cannot project out existentially quantified variables",
4223 return isl_basic_map_free(bmap));
4225 empty = isl_basic_map_plain_is_empty(bmap);
4226 if (empty < 0)
4227 return isl_basic_map_free(bmap);
4228 if (empty)
4229 bmap = isl_basic_map_set_to_empty(bmap);
4231 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4232 if (!bmap)
4233 return NULL;
4235 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4236 return isl_basic_map_remove_dims(bmap, type, first, n);
4238 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4239 return isl_basic_map_free(bmap);
4241 bmap = move_last(bmap, type, first, n);
4242 bmap = isl_basic_map_cow(bmap);
4243 bmap = insert_div_rows(bmap, n);
4244 if (!bmap)
4245 return NULL;
4247 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4248 if (!bmap->dim)
4249 goto error;
4250 bmap = isl_basic_map_simplify(bmap);
4251 bmap = isl_basic_map_drop_redundant_divs(bmap);
4252 return isl_basic_map_finalize(bmap);
4253 error:
4254 isl_basic_map_free(bmap);
4255 return NULL;
4258 /* Turn the n dimensions of type type, starting at first
4259 * into existentially quantified variables.
4261 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4262 enum isl_dim_type type, unsigned first, unsigned n)
4264 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4265 type, first, n));
4268 /* Turn the n dimensions of type type, starting at first
4269 * into existentially quantified variables.
4271 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4272 enum isl_dim_type type, unsigned first, unsigned n)
4274 int i;
4276 if (n == 0)
4277 return map_space_reset(map, type);
4279 if (isl_map_check_range(map, type, first, n) < 0)
4280 return isl_map_free(map);
4282 map = isl_map_cow(map);
4283 if (!map)
4284 return NULL;
4286 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4287 if (!map->dim)
4288 goto error;
4290 for (i = 0; i < map->n; ++i) {
4291 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4292 if (!map->p[i])
4293 goto error;
4296 return map;
4297 error:
4298 isl_map_free(map);
4299 return NULL;
4302 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4303 * into existentially quantified variables.
4305 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4306 enum isl_dim_type type, unsigned first, unsigned n)
4308 unsigned dim;
4310 if (isl_map_check_range(map, type, first, n) < 0)
4311 return isl_map_free(map);
4312 dim = isl_map_dim(map, type);
4313 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4314 map = isl_map_project_out(map, type, 0, first);
4315 return map;
4318 /* Turn the n dimensions of type type, starting at first
4319 * into existentially quantified variables.
4321 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4322 enum isl_dim_type type, unsigned first, unsigned n)
4324 return set_from_map(isl_map_project_out(set_to_map(set),
4325 type, first, n));
4328 /* Return a map that projects the elements in "set" onto their
4329 * "n" set dimensions starting at "first".
4330 * "type" should be equal to isl_dim_set.
4332 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4333 enum isl_dim_type type, unsigned first, unsigned n)
4335 int i;
4336 isl_map *map;
4338 if (type != isl_dim_set)
4339 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4340 "only set dimensions can be projected out", goto error);
4341 if (isl_set_check_range(set, type, first, n) < 0)
4342 return isl_set_free(set);
4344 map = isl_map_from_domain(set);
4345 map = isl_map_add_dims(map, isl_dim_out, n);
4346 for (i = 0; i < n; ++i)
4347 map = isl_map_equate(map, isl_dim_in, first + i,
4348 isl_dim_out, i);
4349 return map;
4350 error:
4351 isl_set_free(set);
4352 return NULL;
4355 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4356 unsigned n)
4358 int i, j;
4360 for (i = 0; i < n; ++i) {
4361 j = isl_basic_map_alloc_div(bmap);
4362 if (j < 0)
4363 goto error;
4364 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4366 return bmap;
4367 error:
4368 isl_basic_map_free(bmap);
4369 return NULL;
4372 struct isl_basic_map *isl_basic_map_apply_range(
4373 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4375 isl_space *space_result = NULL;
4376 struct isl_basic_map *bmap;
4377 unsigned n_in, n_out, n, nparam, total, pos;
4378 struct isl_dim_map *dim_map1, *dim_map2;
4380 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4381 goto error;
4382 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4383 bmap2->dim, isl_dim_in))
4384 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4385 "spaces don't match", goto error);
4387 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4388 isl_space_copy(bmap2->dim));
4390 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4391 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4392 n = isl_basic_map_dim(bmap1, isl_dim_out);
4393 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4395 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4396 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4397 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4398 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4399 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4400 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4401 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4402 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4403 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4404 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4405 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4407 bmap = isl_basic_map_alloc_space(space_result,
4408 bmap1->n_div + bmap2->n_div + n,
4409 bmap1->n_eq + bmap2->n_eq,
4410 bmap1->n_ineq + bmap2->n_ineq);
4411 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4412 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4413 bmap = add_divs(bmap, n);
4414 bmap = isl_basic_map_simplify(bmap);
4415 bmap = isl_basic_map_drop_redundant_divs(bmap);
4416 return isl_basic_map_finalize(bmap);
4417 error:
4418 isl_basic_map_free(bmap1);
4419 isl_basic_map_free(bmap2);
4420 return NULL;
4423 struct isl_basic_set *isl_basic_set_apply(
4424 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4426 if (!bset || !bmap)
4427 goto error;
4429 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4430 goto error);
4432 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4433 bmap));
4434 error:
4435 isl_basic_set_free(bset);
4436 isl_basic_map_free(bmap);
4437 return NULL;
4440 struct isl_basic_map *isl_basic_map_apply_domain(
4441 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4443 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4444 goto error;
4445 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4446 bmap2->dim, isl_dim_in))
4447 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4448 "spaces don't match", goto error);
4450 bmap1 = isl_basic_map_reverse(bmap1);
4451 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4452 return isl_basic_map_reverse(bmap1);
4453 error:
4454 isl_basic_map_free(bmap1);
4455 isl_basic_map_free(bmap2);
4456 return NULL;
4459 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4460 * A \cap B -> f(A) + f(B)
4462 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4463 __isl_take isl_basic_map *bmap2)
4465 unsigned n_in, n_out, nparam, total, pos;
4466 struct isl_basic_map *bmap = NULL;
4467 struct isl_dim_map *dim_map1, *dim_map2;
4468 int i;
4470 if (!bmap1 || !bmap2)
4471 goto error;
4473 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4474 goto error);
4476 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4477 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4478 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4480 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4481 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4482 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4483 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4484 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4485 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4486 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4487 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4488 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4489 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4490 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4492 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4493 bmap1->n_div + bmap2->n_div + 2 * n_out,
4494 bmap1->n_eq + bmap2->n_eq + n_out,
4495 bmap1->n_ineq + bmap2->n_ineq);
4496 for (i = 0; i < n_out; ++i) {
4497 int j = isl_basic_map_alloc_equality(bmap);
4498 if (j < 0)
4499 goto error;
4500 isl_seq_clr(bmap->eq[j], 1+total);
4501 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4502 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4503 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4505 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4506 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4507 bmap = add_divs(bmap, 2 * n_out);
4509 bmap = isl_basic_map_simplify(bmap);
4510 return isl_basic_map_finalize(bmap);
4511 error:
4512 isl_basic_map_free(bmap);
4513 isl_basic_map_free(bmap1);
4514 isl_basic_map_free(bmap2);
4515 return NULL;
4518 /* Given two maps A -> f(A) and B -> g(B), construct a map
4519 * A \cap B -> f(A) + f(B)
4521 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4522 __isl_take isl_map *map2)
4524 struct isl_map *result;
4525 int i, j;
4527 if (!map1 || !map2)
4528 goto error;
4530 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4532 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4533 map1->n * map2->n, 0);
4534 if (!result)
4535 goto error;
4536 for (i = 0; i < map1->n; ++i)
4537 for (j = 0; j < map2->n; ++j) {
4538 struct isl_basic_map *part;
4539 part = isl_basic_map_sum(
4540 isl_basic_map_copy(map1->p[i]),
4541 isl_basic_map_copy(map2->p[j]));
4542 if (isl_basic_map_is_empty(part))
4543 isl_basic_map_free(part);
4544 else
4545 result = isl_map_add_basic_map(result, part);
4546 if (!result)
4547 goto error;
4549 isl_map_free(map1);
4550 isl_map_free(map2);
4551 return result;
4552 error:
4553 isl_map_free(map1);
4554 isl_map_free(map2);
4555 return NULL;
4558 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4559 __isl_take isl_set *set2)
4561 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4564 /* Given a basic map A -> f(A), construct A -> -f(A).
4566 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4568 int i, j;
4569 unsigned off, n;
4571 bmap = isl_basic_map_cow(bmap);
4572 if (!bmap)
4573 return NULL;
4575 n = isl_basic_map_dim(bmap, isl_dim_out);
4576 off = isl_basic_map_offset(bmap, isl_dim_out);
4577 for (i = 0; i < bmap->n_eq; ++i)
4578 for (j = 0; j < n; ++j)
4579 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4580 for (i = 0; i < bmap->n_ineq; ++i)
4581 for (j = 0; j < n; ++j)
4582 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4583 for (i = 0; i < bmap->n_div; ++i)
4584 for (j = 0; j < n; ++j)
4585 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4586 bmap = isl_basic_map_gauss(bmap, NULL);
4587 return isl_basic_map_finalize(bmap);
4590 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4592 return isl_basic_map_neg(bset);
4595 /* Given a map A -> f(A), construct A -> -f(A).
4597 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4599 int i;
4601 map = isl_map_cow(map);
4602 if (!map)
4603 return NULL;
4605 for (i = 0; i < map->n; ++i) {
4606 map->p[i] = isl_basic_map_neg(map->p[i]);
4607 if (!map->p[i])
4608 goto error;
4611 return map;
4612 error:
4613 isl_map_free(map);
4614 return NULL;
4617 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4619 return set_from_map(isl_map_neg(set_to_map(set)));
4622 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4623 * A -> floor(f(A)/d).
4625 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4626 isl_int d)
4628 unsigned n_in, n_out, nparam, total, pos;
4629 struct isl_basic_map *result = NULL;
4630 struct isl_dim_map *dim_map;
4631 int i;
4633 if (!bmap)
4634 return NULL;
4636 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4637 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4638 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4640 total = nparam + n_in + n_out + bmap->n_div + n_out;
4641 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4642 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4643 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4644 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4645 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4647 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4648 bmap->n_div + n_out,
4649 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4650 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4651 result = add_divs(result, n_out);
4652 for (i = 0; i < n_out; ++i) {
4653 int j;
4654 j = isl_basic_map_alloc_inequality(result);
4655 if (j < 0)
4656 goto error;
4657 isl_seq_clr(result->ineq[j], 1+total);
4658 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4659 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4660 j = isl_basic_map_alloc_inequality(result);
4661 if (j < 0)
4662 goto error;
4663 isl_seq_clr(result->ineq[j], 1+total);
4664 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4665 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4666 isl_int_sub_ui(result->ineq[j][0], d, 1);
4669 result = isl_basic_map_simplify(result);
4670 return isl_basic_map_finalize(result);
4671 error:
4672 isl_basic_map_free(result);
4673 return NULL;
4676 /* Given a map A -> f(A) and an integer d, construct a map
4677 * A -> floor(f(A)/d).
4679 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4681 int i;
4683 map = isl_map_cow(map);
4684 if (!map)
4685 return NULL;
4687 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4688 for (i = 0; i < map->n; ++i) {
4689 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4690 if (!map->p[i])
4691 goto error;
4693 map = isl_map_unmark_normalized(map);
4695 return map;
4696 error:
4697 isl_map_free(map);
4698 return NULL;
4701 /* Given a map A -> f(A) and an integer d, construct a map
4702 * A -> floor(f(A)/d).
4704 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4705 __isl_take isl_val *d)
4707 if (!map || !d)
4708 goto error;
4709 if (!isl_val_is_int(d))
4710 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4711 "expecting integer denominator", goto error);
4712 map = isl_map_floordiv(map, d->n);
4713 isl_val_free(d);
4714 return map;
4715 error:
4716 isl_map_free(map);
4717 isl_val_free(d);
4718 return NULL;
4721 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4722 unsigned pos)
4724 int i;
4725 unsigned nparam;
4726 unsigned n_in;
4728 i = isl_basic_map_alloc_equality(bmap);
4729 if (i < 0)
4730 goto error;
4731 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4732 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4733 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4734 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4735 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4736 return isl_basic_map_finalize(bmap);
4737 error:
4738 isl_basic_map_free(bmap);
4739 return NULL;
4742 /* Add a constraint to "bmap" expressing i_pos < o_pos
4744 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4745 unsigned pos)
4747 int i;
4748 unsigned nparam;
4749 unsigned n_in;
4751 i = isl_basic_map_alloc_inequality(bmap);
4752 if (i < 0)
4753 goto error;
4754 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4755 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4756 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4757 isl_int_set_si(bmap->ineq[i][0], -1);
4758 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4759 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4760 return isl_basic_map_finalize(bmap);
4761 error:
4762 isl_basic_map_free(bmap);
4763 return NULL;
4766 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4768 static __isl_give isl_basic_map *var_less_or_equal(
4769 __isl_take isl_basic_map *bmap, unsigned pos)
4771 int i;
4772 unsigned nparam;
4773 unsigned n_in;
4775 i = isl_basic_map_alloc_inequality(bmap);
4776 if (i < 0)
4777 goto error;
4778 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4779 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4780 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4781 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4782 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4783 return isl_basic_map_finalize(bmap);
4784 error:
4785 isl_basic_map_free(bmap);
4786 return NULL;
4789 /* Add a constraint to "bmap" expressing i_pos > o_pos
4791 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4792 unsigned pos)
4794 int i;
4795 unsigned nparam;
4796 unsigned n_in;
4798 i = isl_basic_map_alloc_inequality(bmap);
4799 if (i < 0)
4800 goto error;
4801 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4802 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4803 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4804 isl_int_set_si(bmap->ineq[i][0], -1);
4805 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4806 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4807 return isl_basic_map_finalize(bmap);
4808 error:
4809 isl_basic_map_free(bmap);
4810 return NULL;
4813 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4815 static __isl_give isl_basic_map *var_more_or_equal(
4816 __isl_take isl_basic_map *bmap, unsigned pos)
4818 int i;
4819 unsigned nparam;
4820 unsigned n_in;
4822 i = isl_basic_map_alloc_inequality(bmap);
4823 if (i < 0)
4824 goto error;
4825 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4826 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4827 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4828 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4829 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4830 return isl_basic_map_finalize(bmap);
4831 error:
4832 isl_basic_map_free(bmap);
4833 return NULL;
4836 __isl_give isl_basic_map *isl_basic_map_equal(
4837 __isl_take isl_space *space, unsigned n_equal)
4839 int i;
4840 struct isl_basic_map *bmap;
4841 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4842 if (!bmap)
4843 return NULL;
4844 for (i = 0; i < n_equal && bmap; ++i)
4845 bmap = var_equal(bmap, i);
4846 return isl_basic_map_finalize(bmap);
4849 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4851 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4852 unsigned pos)
4854 int i;
4855 struct isl_basic_map *bmap;
4856 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4857 if (!bmap)
4858 return NULL;
4859 for (i = 0; i < pos && bmap; ++i)
4860 bmap = var_equal(bmap, i);
4861 if (bmap)
4862 bmap = var_less(bmap, pos);
4863 return isl_basic_map_finalize(bmap);
4866 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4868 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4869 __isl_take isl_space *space, unsigned pos)
4871 int i;
4872 isl_basic_map *bmap;
4874 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4875 for (i = 0; i < pos; ++i)
4876 bmap = var_equal(bmap, i);
4877 bmap = var_less_or_equal(bmap, pos);
4878 return isl_basic_map_finalize(bmap);
4881 /* Return a relation on "space" expressing i_pos > o_pos
4883 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4884 unsigned pos)
4886 int i;
4887 struct isl_basic_map *bmap;
4888 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4889 if (!bmap)
4890 return NULL;
4891 for (i = 0; i < pos && bmap; ++i)
4892 bmap = var_equal(bmap, i);
4893 if (bmap)
4894 bmap = var_more(bmap, pos);
4895 return isl_basic_map_finalize(bmap);
4898 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4900 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4901 __isl_take isl_space *space, unsigned pos)
4903 int i;
4904 isl_basic_map *bmap;
4906 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4907 for (i = 0; i < pos; ++i)
4908 bmap = var_equal(bmap, i);
4909 bmap = var_more_or_equal(bmap, pos);
4910 return isl_basic_map_finalize(bmap);
4913 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4914 unsigned n, int equal)
4916 struct isl_map *map;
4917 int i;
4919 if (n == 0 && equal)
4920 return isl_map_universe(space);
4922 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4924 for (i = 0; i + 1 < n; ++i)
4925 map = isl_map_add_basic_map(map,
4926 isl_basic_map_less_at(isl_space_copy(space), i));
4927 if (n > 0) {
4928 if (equal)
4929 map = isl_map_add_basic_map(map,
4930 isl_basic_map_less_or_equal_at(space, n - 1));
4931 else
4932 map = isl_map_add_basic_map(map,
4933 isl_basic_map_less_at(space, n - 1));
4934 } else
4935 isl_space_free(space);
4937 return map;
4940 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
4942 if (!space)
4943 return NULL;
4944 return map_lex_lte_first(space, space->n_out, equal);
4947 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4949 return map_lex_lte_first(dim, n, 0);
4952 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4954 return map_lex_lte_first(dim, n, 1);
4957 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4959 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4962 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4964 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4967 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
4968 unsigned n, int equal)
4970 struct isl_map *map;
4971 int i;
4973 if (n == 0 && equal)
4974 return isl_map_universe(space);
4976 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4978 for (i = 0; i + 1 < n; ++i)
4979 map = isl_map_add_basic_map(map,
4980 isl_basic_map_more_at(isl_space_copy(space), i));
4981 if (n > 0) {
4982 if (equal)
4983 map = isl_map_add_basic_map(map,
4984 isl_basic_map_more_or_equal_at(space, n - 1));
4985 else
4986 map = isl_map_add_basic_map(map,
4987 isl_basic_map_more_at(space, n - 1));
4988 } else
4989 isl_space_free(space);
4991 return map;
4994 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
4996 if (!space)
4997 return NULL;
4998 return map_lex_gte_first(space, space->n_out, equal);
5001 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5003 return map_lex_gte_first(dim, n, 0);
5006 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5008 return map_lex_gte_first(dim, n, 1);
5011 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5013 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5016 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5018 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5021 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5022 __isl_take isl_set *set2)
5024 isl_map *map;
5025 map = isl_map_lex_le(isl_set_get_space(set1));
5026 map = isl_map_intersect_domain(map, set1);
5027 map = isl_map_intersect_range(map, set2);
5028 return map;
5031 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5032 __isl_take isl_set *set2)
5034 isl_map *map;
5035 map = isl_map_lex_lt(isl_set_get_space(set1));
5036 map = isl_map_intersect_domain(map, set1);
5037 map = isl_map_intersect_range(map, set2);
5038 return map;
5041 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5042 __isl_take isl_set *set2)
5044 isl_map *map;
5045 map = isl_map_lex_ge(isl_set_get_space(set1));
5046 map = isl_map_intersect_domain(map, set1);
5047 map = isl_map_intersect_range(map, set2);
5048 return map;
5051 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5052 __isl_take isl_set *set2)
5054 isl_map *map;
5055 map = isl_map_lex_gt(isl_set_get_space(set1));
5056 map = isl_map_intersect_domain(map, set1);
5057 map = isl_map_intersect_range(map, set2);
5058 return map;
5061 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5062 __isl_take isl_map *map2)
5064 isl_map *map;
5065 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5066 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5067 map = isl_map_apply_range(map, isl_map_reverse(map2));
5068 return map;
5071 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5072 __isl_take isl_map *map2)
5074 isl_map *map;
5075 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5076 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5077 map = isl_map_apply_range(map, isl_map_reverse(map2));
5078 return map;
5081 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5082 __isl_take isl_map *map2)
5084 isl_map *map;
5085 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5086 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5087 map = isl_map_apply_range(map, isl_map_reverse(map2));
5088 return map;
5091 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5092 __isl_take isl_map *map2)
5094 isl_map *map;
5095 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5096 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5097 map = isl_map_apply_range(map, isl_map_reverse(map2));
5098 return map;
5101 /* For the div d = floor(f/m) at position "div", add the constraint
5103 * f - m d >= 0
5105 static __isl_give isl_basic_map *add_upper_div_constraint(
5106 __isl_take isl_basic_map *bmap, unsigned div)
5108 int i;
5109 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5110 unsigned n_div, pos;
5112 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5113 if (v_div < 0)
5114 return isl_basic_map_free(bmap);
5115 pos = v_div + div;
5116 i = isl_basic_map_alloc_inequality(bmap);
5117 if (i < 0)
5118 return isl_basic_map_free(bmap);
5119 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5120 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5122 return bmap;
5125 /* For the div d = floor(f/m) at position "div", add the constraint
5127 * -(f-(m-1)) + m d >= 0
5129 static __isl_give isl_basic_map *add_lower_div_constraint(
5130 __isl_take isl_basic_map *bmap, unsigned div)
5132 int i;
5133 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5134 unsigned n_div, pos;
5136 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5137 if (v_div < 0)
5138 return isl_basic_map_free(bmap);
5139 pos = v_div + div;
5140 i = isl_basic_map_alloc_inequality(bmap);
5141 if (i < 0)
5142 return isl_basic_map_free(bmap);
5143 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5144 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5145 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5146 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5148 return bmap;
5151 /* For the div d = floor(f/m) at position "pos", add the constraints
5153 * f - m d >= 0
5154 * -(f-(m-1)) + m d >= 0
5156 * Note that the second constraint is the negation of
5158 * f - m d >= m
5160 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5161 __isl_take isl_basic_map *bmap, unsigned pos)
5163 bmap = add_upper_div_constraint(bmap, pos);
5164 bmap = add_lower_div_constraint(bmap, pos);
5165 return bmap;
5168 /* For each known div d = floor(f/m), add the constraints
5170 * f - m d >= 0
5171 * -(f-(m-1)) + m d >= 0
5173 * Remove duplicate constraints in case of some these div constraints
5174 * already appear in "bmap".
5176 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5177 __isl_take isl_basic_map *bmap)
5179 unsigned n_div;
5181 if (!bmap)
5182 return NULL;
5183 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5184 if (n_div == 0)
5185 return bmap;
5187 bmap = add_known_div_constraints(bmap);
5188 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5189 bmap = isl_basic_map_finalize(bmap);
5190 return bmap;
5193 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5195 * In particular, if this div is of the form d = floor(f/m),
5196 * then add the constraint
5198 * f - m d >= 0
5200 * if sign < 0 or the constraint
5202 * -(f-(m-1)) + m d >= 0
5204 * if sign > 0.
5206 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5207 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5209 if (sign < 0)
5210 return add_upper_div_constraint(bmap, div);
5211 else
5212 return add_lower_div_constraint(bmap, div);
5215 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5216 __isl_take isl_basic_map *bmap)
5218 if (!bmap)
5219 goto error;
5220 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5221 bmap->n_div == 0 &&
5222 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5223 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5224 return bset_from_bmap(bmap);
5225 bmap = isl_basic_map_cow(bmap);
5226 if (!bmap)
5227 goto error;
5228 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5229 if (!bmap->dim)
5230 goto error;
5231 bmap->extra -= bmap->n_div;
5232 bmap->n_div = 0;
5233 bmap = isl_basic_map_finalize(bmap);
5234 return bset_from_bmap(bmap);
5235 error:
5236 isl_basic_map_free(bmap);
5237 return NULL;
5240 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5241 __isl_take isl_basic_set *bset)
5243 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5246 /* Replace each element in "list" by the result of applying
5247 * isl_basic_map_underlying_set to the element.
5249 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5250 __isl_take isl_basic_map_list *list)
5252 int i, n;
5254 if (!list)
5255 return NULL;
5257 n = isl_basic_map_list_n_basic_map(list);
5258 for (i = 0; i < n; ++i) {
5259 isl_basic_map *bmap;
5260 isl_basic_set *bset;
5262 bmap = isl_basic_map_list_get_basic_map(list, i);
5263 bset = isl_basic_set_underlying_set(bmap);
5264 list = isl_basic_set_list_set_basic_set(list, i, bset);
5267 return list;
5270 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5271 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5273 struct isl_basic_map *bmap;
5274 struct isl_ctx *ctx;
5275 unsigned total;
5276 int i;
5278 if (!bset || !like)
5279 goto error;
5280 ctx = bset->ctx;
5281 if (isl_basic_set_check_no_params(bset) < 0 ||
5282 isl_basic_set_check_no_locals(bset) < 0)
5283 goto error;
5284 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5285 goto error);
5286 if (like->n_div == 0) {
5287 isl_space *space = isl_basic_map_get_space(like);
5288 isl_basic_map_free(like);
5289 return isl_basic_map_reset_space(bset, space);
5291 bset = isl_basic_set_cow(bset);
5292 if (!bset)
5293 goto error;
5294 total = bset->dim->n_out + bset->extra;
5295 bmap = bset_to_bmap(bset);
5296 isl_space_free(bmap->dim);
5297 bmap->dim = isl_space_copy(like->dim);
5298 if (!bmap->dim)
5299 goto error;
5300 bmap->n_div = like->n_div;
5301 bmap->extra += like->n_div;
5302 if (bmap->extra) {
5303 unsigned ltotal;
5304 isl_int **div;
5305 ltotal = total - bmap->extra + like->extra;
5306 if (ltotal > total)
5307 ltotal = total;
5308 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5309 bmap->extra * (1 + 1 + total));
5310 if (isl_blk_is_error(bmap->block2))
5311 goto error;
5312 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5313 if (!div)
5314 goto error;
5315 bmap->div = div;
5316 for (i = 0; i < bmap->extra; ++i)
5317 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5318 for (i = 0; i < like->n_div; ++i) {
5319 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5320 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5322 bmap = isl_basic_map_add_known_div_constraints(bmap);
5324 isl_basic_map_free(like);
5325 bmap = isl_basic_map_simplify(bmap);
5326 bmap = isl_basic_map_finalize(bmap);
5327 return bmap;
5328 error:
5329 isl_basic_map_free(like);
5330 isl_basic_set_free(bset);
5331 return NULL;
5334 struct isl_basic_set *isl_basic_set_from_underlying_set(
5335 struct isl_basic_set *bset, struct isl_basic_set *like)
5337 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5338 bset_to_bmap(like)));
5341 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5343 int i;
5345 map = isl_map_cow(map);
5346 if (!map)
5347 return NULL;
5348 map->dim = isl_space_cow(map->dim);
5349 if (!map->dim)
5350 goto error;
5352 for (i = 1; i < map->n; ++i)
5353 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5354 goto error);
5355 for (i = 0; i < map->n; ++i) {
5356 map->p[i] = bset_to_bmap(
5357 isl_basic_map_underlying_set(map->p[i]));
5358 if (!map->p[i])
5359 goto error;
5361 if (map->n == 0)
5362 map->dim = isl_space_underlying(map->dim, 0);
5363 else {
5364 isl_space_free(map->dim);
5365 map->dim = isl_space_copy(map->p[0]->dim);
5367 if (!map->dim)
5368 goto error;
5369 return set_from_map(map);
5370 error:
5371 isl_map_free(map);
5372 return NULL;
5375 /* Replace the space of "bmap" by "space".
5377 * If the space of "bmap" is identical to "space" (including the identifiers
5378 * of the input and output dimensions), then simply return the original input.
5380 __isl_give isl_basic_map *isl_basic_map_reset_space(
5381 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5383 isl_bool equal;
5384 isl_space *bmap_space;
5386 bmap_space = isl_basic_map_peek_space(bmap);
5387 equal = isl_space_is_equal(bmap_space, space);
5388 if (equal >= 0 && equal)
5389 equal = isl_space_has_equal_ids(bmap_space, space);
5390 if (equal < 0)
5391 goto error;
5392 if (equal) {
5393 isl_space_free(space);
5394 return bmap;
5396 bmap = isl_basic_map_cow(bmap);
5397 if (!bmap || !space)
5398 goto error;
5400 isl_space_free(bmap->dim);
5401 bmap->dim = space;
5403 bmap = isl_basic_map_finalize(bmap);
5405 return bmap;
5406 error:
5407 isl_basic_map_free(bmap);
5408 isl_space_free(space);
5409 return NULL;
5412 __isl_give isl_basic_set *isl_basic_set_reset_space(
5413 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5415 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5416 dim));
5419 /* Check that the total dimensions of "map" and "space" are the same.
5421 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5422 __isl_keep isl_space *space)
5424 unsigned dim1, dim2;
5426 if (!map || !space)
5427 return isl_stat_error;
5428 dim1 = isl_map_dim(map, isl_dim_all);
5429 dim2 = isl_space_dim(space, isl_dim_all);
5430 if (dim1 == dim2)
5431 return isl_stat_ok;
5432 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5433 "total dimensions do not match", return isl_stat_error);
5436 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5437 __isl_take isl_space *space)
5439 int i;
5441 map = isl_map_cow(map);
5442 if (!map || !space)
5443 goto error;
5445 for (i = 0; i < map->n; ++i) {
5446 map->p[i] = isl_basic_map_reset_space(map->p[i],
5447 isl_space_copy(space));
5448 if (!map->p[i])
5449 goto error;
5451 isl_space_free(map->dim);
5452 map->dim = space;
5454 return map;
5455 error:
5456 isl_map_free(map);
5457 isl_space_free(space);
5458 return NULL;
5461 /* Replace the space of "map" by "space", without modifying
5462 * the dimension of "map".
5464 * If the space of "map" is identical to "space" (including the identifiers
5465 * of the input and output dimensions), then simply return the original input.
5467 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5468 __isl_take isl_space *space)
5470 isl_bool equal;
5471 isl_space *map_space;
5473 map_space = isl_map_peek_space(map);
5474 equal = isl_space_is_equal(map_space, space);
5475 if (equal >= 0 && equal)
5476 equal = isl_space_has_equal_ids(map_space, space);
5477 if (equal < 0)
5478 goto error;
5479 if (equal) {
5480 isl_space_free(space);
5481 return map;
5483 if (check_map_space_equal_total_dim(map, space) < 0)
5484 goto error;
5485 return isl_map_reset_space(map, space);
5486 error:
5487 isl_map_free(map);
5488 isl_space_free(space);
5489 return NULL;
5492 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5493 __isl_take isl_space *dim)
5495 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5498 /* Compute the parameter domain of the given basic set.
5500 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5502 isl_bool is_params;
5503 isl_space *space;
5504 unsigned n;
5506 is_params = isl_basic_set_is_params(bset);
5507 if (is_params < 0)
5508 return isl_basic_set_free(bset);
5509 if (is_params)
5510 return bset;
5512 n = isl_basic_set_dim(bset, isl_dim_set);
5513 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5514 space = isl_basic_set_get_space(bset);
5515 space = isl_space_params(space);
5516 bset = isl_basic_set_reset_space(bset, space);
5517 return bset;
5520 /* Construct a zero-dimensional basic set with the given parameter domain.
5522 __isl_give isl_basic_set *isl_basic_set_from_params(
5523 __isl_take isl_basic_set *bset)
5525 isl_space *space;
5526 space = isl_basic_set_get_space(bset);
5527 space = isl_space_set_from_params(space);
5528 bset = isl_basic_set_reset_space(bset, space);
5529 return bset;
5532 /* Compute the parameter domain of the given set.
5534 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5536 isl_space *space;
5537 unsigned n;
5539 if (isl_set_is_params(set))
5540 return set;
5542 n = isl_set_dim(set, isl_dim_set);
5543 set = isl_set_project_out(set, isl_dim_set, 0, n);
5544 space = isl_set_get_space(set);
5545 space = isl_space_params(space);
5546 set = isl_set_reset_space(set, space);
5547 return set;
5550 /* Construct a zero-dimensional set with the given parameter domain.
5552 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5554 isl_space *space;
5555 space = isl_set_get_space(set);
5556 space = isl_space_set_from_params(space);
5557 set = isl_set_reset_space(set, space);
5558 return set;
5561 /* Compute the parameter domain of the given map.
5563 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5565 isl_space *space;
5566 unsigned n;
5568 n = isl_map_dim(map, isl_dim_in);
5569 map = isl_map_project_out(map, isl_dim_in, 0, n);
5570 n = isl_map_dim(map, isl_dim_out);
5571 map = isl_map_project_out(map, isl_dim_out, 0, n);
5572 space = isl_map_get_space(map);
5573 space = isl_space_params(space);
5574 map = isl_map_reset_space(map, space);
5575 return map;
5578 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5580 isl_space *space;
5581 unsigned n_out;
5583 if (!bmap)
5584 return NULL;
5585 space = isl_space_domain(isl_basic_map_get_space(bmap));
5587 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5588 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5590 return isl_basic_map_reset_space(bmap, space);
5593 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5595 if (!bmap)
5596 return isl_bool_error;
5597 return isl_space_may_be_set(bmap->dim);
5600 /* Is this basic map actually a set?
5601 * Users should never call this function. Outside of isl,
5602 * the type should indicate whether something is a set or a map.
5604 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5606 if (!bmap)
5607 return isl_bool_error;
5608 return isl_space_is_set(bmap->dim);
5611 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5613 isl_bool is_set;
5615 is_set = isl_basic_map_is_set(bmap);
5616 if (is_set < 0)
5617 goto error;
5618 if (is_set)
5619 return bmap;
5620 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5621 error:
5622 isl_basic_map_free(bmap);
5623 return NULL;
5626 __isl_give isl_basic_map *isl_basic_map_domain_map(
5627 __isl_take isl_basic_map *bmap)
5629 int i;
5630 isl_space *space;
5631 isl_basic_map *domain;
5632 int nparam, n_in, n_out;
5634 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5635 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5636 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5638 space = isl_basic_map_get_space(bmap);
5639 space = isl_space_from_range(isl_space_domain(space));
5640 domain = isl_basic_map_universe(space);
5642 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5643 bmap = isl_basic_map_apply_range(bmap, domain);
5644 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5646 for (i = 0; i < n_in; ++i)
5647 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5648 isl_dim_out, i);
5650 bmap = isl_basic_map_gauss(bmap, NULL);
5651 return isl_basic_map_finalize(bmap);
5654 __isl_give isl_basic_map *isl_basic_map_range_map(
5655 __isl_take isl_basic_map *bmap)
5657 int i;
5658 isl_space *space;
5659 isl_basic_map *range;
5660 int nparam, n_in, n_out;
5662 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5663 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5664 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5666 space = isl_basic_map_get_space(bmap);
5667 space = isl_space_from_range(isl_space_range(space));
5668 range = isl_basic_map_universe(space);
5670 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5671 bmap = isl_basic_map_apply_range(bmap, range);
5672 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5674 for (i = 0; i < n_out; ++i)
5675 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5676 isl_dim_out, i);
5678 bmap = isl_basic_map_gauss(bmap, NULL);
5679 return isl_basic_map_finalize(bmap);
5682 int isl_map_may_be_set(__isl_keep isl_map *map)
5684 if (!map)
5685 return -1;
5686 return isl_space_may_be_set(map->dim);
5689 /* Is this map actually a set?
5690 * Users should never call this function. Outside of isl,
5691 * the type should indicate whether something is a set or a map.
5693 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5695 if (!map)
5696 return isl_bool_error;
5697 return isl_space_is_set(map->dim);
5700 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5702 int i;
5703 isl_bool is_set;
5704 struct isl_set *set;
5706 is_set = isl_map_is_set(map);
5707 if (is_set < 0)
5708 goto error;
5709 if (is_set)
5710 return set_from_map(map);
5712 map = isl_map_cow(map);
5713 if (!map)
5714 goto error;
5716 set = set_from_map(map);
5717 set->dim = isl_space_range(set->dim);
5718 if (!set->dim)
5719 goto error;
5720 for (i = 0; i < map->n; ++i) {
5721 set->p[i] = isl_basic_map_range(map->p[i]);
5722 if (!set->p[i])
5723 goto error;
5725 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5726 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5727 return set;
5728 error:
5729 isl_map_free(map);
5730 return NULL;
5733 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5735 int i;
5737 map = isl_map_cow(map);
5738 if (!map)
5739 return NULL;
5741 map->dim = isl_space_domain_map(map->dim);
5742 if (!map->dim)
5743 goto error;
5744 for (i = 0; i < map->n; ++i) {
5745 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5746 if (!map->p[i])
5747 goto error;
5749 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5750 map = isl_map_unmark_normalized(map);
5751 return map;
5752 error:
5753 isl_map_free(map);
5754 return NULL;
5757 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5759 int i;
5760 isl_space *range_dim;
5762 map = isl_map_cow(map);
5763 if (!map)
5764 return NULL;
5766 range_dim = isl_space_range(isl_map_get_space(map));
5767 range_dim = isl_space_from_range(range_dim);
5768 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5769 map->dim = isl_space_join(map->dim, range_dim);
5770 if (!map->dim)
5771 goto error;
5772 for (i = 0; i < map->n; ++i) {
5773 map->p[i] = isl_basic_map_range_map(map->p[i]);
5774 if (!map->p[i])
5775 goto error;
5777 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5778 map = isl_map_unmark_normalized(map);
5779 return map;
5780 error:
5781 isl_map_free(map);
5782 return NULL;
5785 /* Given a wrapped map of the form A[B -> C],
5786 * return the map A[B -> C] -> B.
5788 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5790 isl_id *id;
5791 isl_map *map;
5793 if (!set)
5794 return NULL;
5795 if (!isl_set_has_tuple_id(set))
5796 return isl_map_domain_map(isl_set_unwrap(set));
5798 id = isl_set_get_tuple_id(set);
5799 map = isl_map_domain_map(isl_set_unwrap(set));
5800 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5802 return map;
5805 __isl_give isl_basic_map *isl_basic_map_from_domain(
5806 __isl_take isl_basic_set *bset)
5808 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5811 __isl_give isl_basic_map *isl_basic_map_from_range(
5812 __isl_take isl_basic_set *bset)
5814 isl_space *space;
5815 space = isl_basic_set_get_space(bset);
5816 space = isl_space_from_range(space);
5817 bset = isl_basic_set_reset_space(bset, space);
5818 return bset_to_bmap(bset);
5821 /* Create a relation with the given set as range.
5822 * The domain of the created relation is a zero-dimensional
5823 * flat anonymous space.
5825 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5827 isl_space *space;
5828 space = isl_set_get_space(set);
5829 space = isl_space_from_range(space);
5830 set = isl_set_reset_space(set, space);
5831 return set_to_map(set);
5834 /* Create a relation with the given set as domain.
5835 * The range of the created relation is a zero-dimensional
5836 * flat anonymous space.
5838 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5840 return isl_map_reverse(isl_map_from_range(set));
5843 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5844 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5846 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5849 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5850 __isl_take isl_set *range)
5852 return isl_map_apply_range(isl_map_reverse(domain), range);
5855 /* Return a newly allocated isl_map with given space and flags and
5856 * room for "n" basic maps.
5857 * Make sure that all cached information is cleared.
5859 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5860 unsigned flags)
5862 struct isl_map *map;
5864 if (!space)
5865 return NULL;
5866 if (n < 0)
5867 isl_die(space->ctx, isl_error_internal,
5868 "negative number of basic maps", goto error);
5869 map = isl_calloc(space->ctx, struct isl_map,
5870 sizeof(struct isl_map) +
5871 (n - 1) * sizeof(struct isl_basic_map *));
5872 if (!map)
5873 goto error;
5875 map->ctx = space->ctx;
5876 isl_ctx_ref(map->ctx);
5877 map->ref = 1;
5878 map->size = n;
5879 map->n = 0;
5880 map->dim = space;
5881 map->flags = flags;
5882 return map;
5883 error:
5884 isl_space_free(space);
5885 return NULL;
5888 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5890 struct isl_basic_map *bmap;
5891 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5892 bmap = isl_basic_map_set_to_empty(bmap);
5893 return bmap;
5896 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5898 struct isl_basic_set *bset;
5899 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5900 bset = isl_basic_set_set_to_empty(bset);
5901 return bset;
5904 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5906 struct isl_basic_map *bmap;
5907 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5908 bmap = isl_basic_map_finalize(bmap);
5909 return bmap;
5912 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
5914 struct isl_basic_set *bset;
5915 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5916 bset = isl_basic_set_finalize(bset);
5917 return bset;
5920 __isl_give isl_basic_map *isl_basic_map_nat_universe(
5921 __isl_take isl_space *space)
5923 int i;
5924 unsigned total = isl_space_dim(space, isl_dim_all);
5925 isl_basic_map *bmap;
5927 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
5928 for (i = 0; i < total; ++i) {
5929 int k = isl_basic_map_alloc_inequality(bmap);
5930 if (k < 0)
5931 goto error;
5932 isl_seq_clr(bmap->ineq[k], 1 + total);
5933 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5935 return bmap;
5936 error:
5937 isl_basic_map_free(bmap);
5938 return NULL;
5941 __isl_give isl_basic_set *isl_basic_set_nat_universe(
5942 __isl_take isl_space *space)
5944 return isl_basic_map_nat_universe(space);
5947 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5949 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5952 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5954 return isl_map_nat_universe(dim);
5957 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
5959 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
5962 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
5964 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
5967 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
5969 struct isl_map *map;
5970 if (!space)
5971 return NULL;
5972 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
5973 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
5974 return map;
5977 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
5979 struct isl_set *set;
5980 if (!space)
5981 return NULL;
5982 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
5983 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
5984 return set;
5987 struct isl_map *isl_map_dup(struct isl_map *map)
5989 int i;
5990 struct isl_map *dup;
5992 if (!map)
5993 return NULL;
5994 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5995 for (i = 0; i < map->n; ++i)
5996 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5997 return dup;
6000 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6001 __isl_take isl_basic_map *bmap)
6003 if (!bmap || !map)
6004 goto error;
6005 if (isl_basic_map_plain_is_empty(bmap)) {
6006 isl_basic_map_free(bmap);
6007 return map;
6009 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6010 isl_assert(map->ctx, map->n < map->size, goto error);
6011 map->p[map->n] = bmap;
6012 map->n++;
6013 map = isl_map_unmark_normalized(map);
6014 return map;
6015 error:
6016 if (map)
6017 isl_map_free(map);
6018 if (bmap)
6019 isl_basic_map_free(bmap);
6020 return NULL;
6023 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6025 int i;
6027 if (!map)
6028 return NULL;
6030 if (--map->ref > 0)
6031 return NULL;
6033 clear_caches(map);
6034 isl_ctx_deref(map->ctx);
6035 for (i = 0; i < map->n; ++i)
6036 isl_basic_map_free(map->p[i]);
6037 isl_space_free(map->dim);
6038 free(map);
6040 return NULL;
6043 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6044 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6046 int j;
6048 bmap = isl_basic_map_cow(bmap);
6049 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6050 j = isl_basic_map_alloc_equality(bmap);
6051 if (j < 0)
6052 goto error;
6053 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6054 isl_int_set_si(bmap->eq[j][pos], -1);
6055 isl_int_set_si(bmap->eq[j][0], value);
6056 bmap = isl_basic_map_simplify(bmap);
6057 return isl_basic_map_finalize(bmap);
6058 error:
6059 isl_basic_map_free(bmap);
6060 return NULL;
6063 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6064 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6066 int j;
6068 bmap = isl_basic_map_cow(bmap);
6069 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6070 j = isl_basic_map_alloc_equality(bmap);
6071 if (j < 0)
6072 goto error;
6073 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6074 isl_int_set_si(bmap->eq[j][pos], -1);
6075 isl_int_set(bmap->eq[j][0], value);
6076 bmap = isl_basic_map_simplify(bmap);
6077 return isl_basic_map_finalize(bmap);
6078 error:
6079 isl_basic_map_free(bmap);
6080 return NULL;
6083 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6084 enum isl_dim_type type, unsigned pos, int value)
6086 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6087 return isl_basic_map_free(bmap);
6088 return isl_basic_map_fix_pos_si(bmap,
6089 isl_basic_map_offset(bmap, type) + pos, value);
6092 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6093 enum isl_dim_type type, unsigned pos, isl_int value)
6095 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6096 return isl_basic_map_free(bmap);
6097 return isl_basic_map_fix_pos(bmap,
6098 isl_basic_map_offset(bmap, type) + pos, value);
6101 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6102 * to be equal to "v".
6104 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6105 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6107 if (!bmap || !v)
6108 goto error;
6109 if (!isl_val_is_int(v))
6110 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6111 "expecting integer value", goto error);
6112 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6113 goto error;
6114 pos += isl_basic_map_offset(bmap, type);
6115 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6116 isl_val_free(v);
6117 return bmap;
6118 error:
6119 isl_basic_map_free(bmap);
6120 isl_val_free(v);
6121 return NULL;
6124 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6125 * to be equal to "v".
6127 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6128 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6130 return isl_basic_map_fix_val(bset, type, pos, v);
6133 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6134 enum isl_dim_type type, unsigned pos, int value)
6136 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6137 type, pos, value));
6140 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6141 enum isl_dim_type type, unsigned pos, isl_int value)
6143 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6144 type, pos, value));
6147 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6148 unsigned input, int value)
6150 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6153 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6154 unsigned dim, int value)
6156 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6157 isl_dim_set, dim, value));
6160 /* Remove the basic map at position "i" from "map" if this basic map
6161 * is (obviously) empty.
6163 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6165 isl_bool empty;
6167 if (!map)
6168 return NULL;
6170 empty = isl_basic_map_plain_is_empty(map->p[i]);
6171 if (empty < 0)
6172 return isl_map_free(map);
6173 if (!empty)
6174 return map;
6176 isl_basic_map_free(map->p[i]);
6177 map->n--;
6178 if (i != map->n) {
6179 map->p[i] = map->p[map->n];
6180 map = isl_map_unmark_normalized(map);
6184 return map;
6187 /* Perform "fn" on each basic map of "map", where we may not be holding
6188 * the only reference to "map".
6189 * In particular, "fn" should be a semantics preserving operation
6190 * that we want to apply to all copies of "map". We therefore need
6191 * to be careful not to modify "map" in a way that breaks "map"
6192 * in case anything goes wrong.
6194 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6195 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6197 struct isl_basic_map *bmap;
6198 int i;
6200 if (!map)
6201 return NULL;
6203 for (i = map->n - 1; i >= 0; --i) {
6204 bmap = isl_basic_map_copy(map->p[i]);
6205 bmap = fn(bmap);
6206 if (!bmap)
6207 goto error;
6208 isl_basic_map_free(map->p[i]);
6209 map->p[i] = bmap;
6210 map = remove_if_empty(map, i);
6211 if (!map)
6212 return NULL;
6215 return map;
6216 error:
6217 isl_map_free(map);
6218 return NULL;
6221 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6222 enum isl_dim_type type, unsigned pos, int value)
6224 int i;
6226 map = isl_map_cow(map);
6227 if (isl_map_check_range(map, type, pos, 1) < 0)
6228 return isl_map_free(map);
6229 for (i = map->n - 1; i >= 0; --i) {
6230 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6231 map = remove_if_empty(map, i);
6232 if (!map)
6233 return NULL;
6235 map = isl_map_unmark_normalized(map);
6236 return map;
6239 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6240 enum isl_dim_type type, unsigned pos, int value)
6242 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6245 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6246 enum isl_dim_type type, unsigned pos, isl_int value)
6248 int i;
6250 map = isl_map_cow(map);
6251 if (isl_map_check_range(map, type, pos, 1) < 0)
6252 return isl_map_free(map);
6253 for (i = 0; i < map->n; ++i) {
6254 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6255 if (!map->p[i])
6256 goto error;
6258 map = isl_map_unmark_normalized(map);
6259 return map;
6260 error:
6261 isl_map_free(map);
6262 return NULL;
6265 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6266 enum isl_dim_type type, unsigned pos, isl_int value)
6268 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6271 /* Fix the value of the variable at position "pos" of type "type" of "map"
6272 * to be equal to "v".
6274 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6275 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6277 int i;
6279 map = isl_map_cow(map);
6280 if (!map || !v)
6281 goto error;
6283 if (!isl_val_is_int(v))
6284 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6285 "expecting integer value", goto error);
6286 if (isl_map_check_range(map, type, pos, 1) < 0)
6287 goto error;
6288 for (i = map->n - 1; i >= 0; --i) {
6289 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6290 isl_val_copy(v));
6291 map = remove_if_empty(map, i);
6292 if (!map)
6293 goto error;
6295 map = isl_map_unmark_normalized(map);
6296 isl_val_free(v);
6297 return map;
6298 error:
6299 isl_map_free(map);
6300 isl_val_free(v);
6301 return NULL;
6304 /* Fix the value of the variable at position "pos" of type "type" of "set"
6305 * to be equal to "v".
6307 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6308 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6310 return isl_map_fix_val(set, type, pos, v);
6313 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6314 unsigned input, int value)
6316 return isl_map_fix_si(map, isl_dim_in, input, value);
6319 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6321 return set_from_map(isl_map_fix_si(set_to_map(set),
6322 isl_dim_set, dim, value));
6325 static __isl_give isl_basic_map *basic_map_bound_si(
6326 __isl_take isl_basic_map *bmap,
6327 enum isl_dim_type type, unsigned pos, int value, int upper)
6329 int j;
6331 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6332 return isl_basic_map_free(bmap);
6333 pos += isl_basic_map_offset(bmap, type);
6334 bmap = isl_basic_map_cow(bmap);
6335 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6336 j = isl_basic_map_alloc_inequality(bmap);
6337 if (j < 0)
6338 goto error;
6339 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6340 if (upper) {
6341 isl_int_set_si(bmap->ineq[j][pos], -1);
6342 isl_int_set_si(bmap->ineq[j][0], value);
6343 } else {
6344 isl_int_set_si(bmap->ineq[j][pos], 1);
6345 isl_int_set_si(bmap->ineq[j][0], -value);
6347 bmap = isl_basic_map_simplify(bmap);
6348 return isl_basic_map_finalize(bmap);
6349 error:
6350 isl_basic_map_free(bmap);
6351 return NULL;
6354 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6355 __isl_take isl_basic_map *bmap,
6356 enum isl_dim_type type, unsigned pos, int value)
6358 return basic_map_bound_si(bmap, type, pos, value, 0);
6361 /* Constrain the values of the given dimension to be no greater than "value".
6363 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6364 __isl_take isl_basic_map *bmap,
6365 enum isl_dim_type type, unsigned pos, int value)
6367 return basic_map_bound_si(bmap, type, pos, value, 1);
6370 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6371 enum isl_dim_type type, unsigned pos, int value, int upper)
6373 int i;
6375 map = isl_map_cow(map);
6376 if (isl_map_check_range(map, type, pos, 1) < 0)
6377 return isl_map_free(map);
6378 for (i = 0; i < map->n; ++i) {
6379 map->p[i] = basic_map_bound_si(map->p[i],
6380 type, pos, value, upper);
6381 if (!map->p[i])
6382 goto error;
6384 map = isl_map_unmark_normalized(map);
6385 return map;
6386 error:
6387 isl_map_free(map);
6388 return NULL;
6391 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6392 enum isl_dim_type type, unsigned pos, int value)
6394 return map_bound_si(map, type, pos, value, 0);
6397 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6398 enum isl_dim_type type, unsigned pos, int value)
6400 return map_bound_si(map, type, pos, value, 1);
6403 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6404 enum isl_dim_type type, unsigned pos, int value)
6406 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6407 type, pos, value));
6410 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6411 enum isl_dim_type type, unsigned pos, int value)
6413 return isl_map_upper_bound_si(set, type, pos, value);
6416 /* Bound the given variable of "bmap" from below (or above is "upper"
6417 * is set) to "value".
6419 static __isl_give isl_basic_map *basic_map_bound(
6420 __isl_take isl_basic_map *bmap,
6421 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6423 int j;
6425 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6426 return isl_basic_map_free(bmap);
6427 pos += isl_basic_map_offset(bmap, type);
6428 bmap = isl_basic_map_cow(bmap);
6429 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6430 j = isl_basic_map_alloc_inequality(bmap);
6431 if (j < 0)
6432 goto error;
6433 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6434 if (upper) {
6435 isl_int_set_si(bmap->ineq[j][pos], -1);
6436 isl_int_set(bmap->ineq[j][0], value);
6437 } else {
6438 isl_int_set_si(bmap->ineq[j][pos], 1);
6439 isl_int_neg(bmap->ineq[j][0], value);
6441 bmap = isl_basic_map_simplify(bmap);
6442 return isl_basic_map_finalize(bmap);
6443 error:
6444 isl_basic_map_free(bmap);
6445 return NULL;
6448 /* Bound the given variable of "map" from below (or above is "upper"
6449 * is set) to "value".
6451 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6452 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6454 int i;
6456 map = isl_map_cow(map);
6457 if (isl_map_check_range(map, type, pos, 1) < 0)
6458 return isl_map_free(map);
6459 for (i = map->n - 1; i >= 0; --i) {
6460 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6461 map = remove_if_empty(map, i);
6462 if (!map)
6463 return NULL;
6465 map = isl_map_unmark_normalized(map);
6466 return map;
6469 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6470 enum isl_dim_type type, unsigned pos, isl_int value)
6472 return map_bound(map, type, pos, value, 0);
6475 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6476 enum isl_dim_type type, unsigned pos, isl_int value)
6478 return map_bound(map, type, pos, value, 1);
6481 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6482 enum isl_dim_type type, unsigned pos, isl_int value)
6484 return isl_map_lower_bound(set, type, pos, value);
6487 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6488 enum isl_dim_type type, unsigned pos, isl_int value)
6490 return isl_map_upper_bound(set, type, pos, value);
6493 /* Force the values of the variable at position "pos" of type "type" of "set"
6494 * to be no smaller than "value".
6496 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6497 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6499 if (!value)
6500 goto error;
6501 if (!isl_val_is_int(value))
6502 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6503 "expecting integer value", goto error);
6504 set = isl_set_lower_bound(set, type, pos, value->n);
6505 isl_val_free(value);
6506 return set;
6507 error:
6508 isl_val_free(value);
6509 isl_set_free(set);
6510 return NULL;
6513 /* Force the values of the variable at position "pos" of type "type" of "set"
6514 * to be no greater than "value".
6516 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6517 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6519 if (!value)
6520 goto error;
6521 if (!isl_val_is_int(value))
6522 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6523 "expecting integer value", goto error);
6524 set = isl_set_upper_bound(set, type, pos, value->n);
6525 isl_val_free(value);
6526 return set;
6527 error:
6528 isl_val_free(value);
6529 isl_set_free(set);
6530 return NULL;
6533 /* Bound the given variable of "bset" from below (or above is "upper"
6534 * is set) to "value".
6536 static __isl_give isl_basic_set *isl_basic_set_bound(
6537 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6538 isl_int value, int upper)
6540 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6541 type, pos, value, upper));
6544 /* Bound the given variable of "bset" from below (or above is "upper"
6545 * is set) to "value".
6547 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6548 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6549 __isl_take isl_val *value, int upper)
6551 if (!value)
6552 goto error;
6553 if (!isl_val_is_int(value))
6554 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6555 "expecting integer value", goto error);
6556 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6557 isl_val_free(value);
6558 return bset;
6559 error:
6560 isl_val_free(value);
6561 isl_basic_set_free(bset);
6562 return NULL;
6565 /* Bound the given variable of "bset" from below to "value".
6567 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6568 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6569 __isl_take isl_val *value)
6571 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6574 /* Bound the given variable of "bset" from above to "value".
6576 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6577 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6578 __isl_take isl_val *value)
6580 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6583 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6585 int i;
6587 map = isl_map_cow(map);
6588 if (!map)
6589 return NULL;
6591 map->dim = isl_space_reverse(map->dim);
6592 if (!map->dim)
6593 goto error;
6594 for (i = 0; i < map->n; ++i) {
6595 map->p[i] = isl_basic_map_reverse(map->p[i]);
6596 if (!map->p[i])
6597 goto error;
6599 map = isl_map_unmark_normalized(map);
6600 return map;
6601 error:
6602 isl_map_free(map);
6603 return NULL;
6606 #undef TYPE
6607 #define TYPE isl_pw_multi_aff
6608 #undef SUFFIX
6609 #define SUFFIX _pw_multi_aff
6610 #undef EMPTY
6611 #define EMPTY isl_pw_multi_aff_empty
6612 #undef ADD
6613 #define ADD isl_pw_multi_aff_union_add
6614 #include "isl_map_lexopt_templ.c"
6616 /* Given a map "map", compute the lexicographically minimal
6617 * (or maximal) image element for each domain element in dom,
6618 * in the form of an isl_pw_multi_aff.
6619 * If "empty" is not NULL, then set *empty to those elements in dom that
6620 * do not have an image element.
6621 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6622 * should be computed over the domain of "map". "empty" is also NULL
6623 * in this case.
6625 * We first compute the lexicographically minimal or maximal element
6626 * in the first basic map. This results in a partial solution "res"
6627 * and a subset "todo" of dom that still need to be handled.
6628 * We then consider each of the remaining maps in "map" and successively
6629 * update both "res" and "todo".
6630 * If "empty" is NULL, then the todo sets are not needed and therefore
6631 * also not computed.
6633 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6634 __isl_take isl_map *map, __isl_take isl_set *dom,
6635 __isl_give isl_set **empty, unsigned flags)
6637 int i;
6638 int full;
6639 isl_pw_multi_aff *res;
6640 isl_set *todo;
6642 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6643 if (!map || (!full && !dom))
6644 goto error;
6646 if (isl_map_plain_is_empty(map)) {
6647 if (empty)
6648 *empty = dom;
6649 else
6650 isl_set_free(dom);
6651 return isl_pw_multi_aff_from_map(map);
6654 res = basic_map_partial_lexopt_pw_multi_aff(
6655 isl_basic_map_copy(map->p[0]),
6656 isl_set_copy(dom), empty, flags);
6658 if (empty)
6659 todo = *empty;
6660 for (i = 1; i < map->n; ++i) {
6661 isl_pw_multi_aff *res_i;
6663 res_i = basic_map_partial_lexopt_pw_multi_aff(
6664 isl_basic_map_copy(map->p[i]),
6665 isl_set_copy(dom), empty, flags);
6667 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6668 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6669 else
6670 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6672 if (empty)
6673 todo = isl_set_intersect(todo, *empty);
6676 isl_set_free(dom);
6677 isl_map_free(map);
6679 if (empty)
6680 *empty = todo;
6682 return res;
6683 error:
6684 if (empty)
6685 *empty = NULL;
6686 isl_set_free(dom);
6687 isl_map_free(map);
6688 return NULL;
6691 #undef TYPE
6692 #define TYPE isl_map
6693 #undef SUFFIX
6694 #define SUFFIX
6695 #undef EMPTY
6696 #define EMPTY isl_map_empty
6697 #undef ADD
6698 #define ADD isl_map_union_disjoint
6699 #include "isl_map_lexopt_templ.c"
6701 /* Given a map "map", compute the lexicographically minimal
6702 * (or maximal) image element for each domain element in "dom",
6703 * in the form of an isl_map.
6704 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6705 * do not have an image element.
6706 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6707 * should be computed over the domain of "map". "empty" is also NULL
6708 * in this case.
6710 * If the input consists of more than one disjunct, then first
6711 * compute the desired result in the form of an isl_pw_multi_aff and
6712 * then convert that into an isl_map.
6714 * This function used to have an explicit implementation in terms
6715 * of isl_maps, but it would continually intersect the domains of
6716 * partial results with the complement of the domain of the next
6717 * partial solution, potentially leading to an explosion in the number
6718 * of disjuncts if there are several disjuncts in the input.
6719 * An even earlier implementation of this function would look for
6720 * better results in the domain of the partial result and for extra
6721 * results in the complement of this domain, which would lead to
6722 * even more splintering.
6724 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6725 __isl_take isl_map *map, __isl_take isl_set *dom,
6726 __isl_give isl_set **empty, unsigned flags)
6728 int full;
6729 struct isl_map *res;
6730 isl_pw_multi_aff *pma;
6732 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6733 if (!map || (!full && !dom))
6734 goto error;
6736 if (isl_map_plain_is_empty(map)) {
6737 if (empty)
6738 *empty = dom;
6739 else
6740 isl_set_free(dom);
6741 return map;
6744 if (map->n == 1) {
6745 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6746 dom, empty, flags);
6747 isl_map_free(map);
6748 return res;
6751 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6752 flags);
6753 return isl_map_from_pw_multi_aff(pma);
6754 error:
6755 if (empty)
6756 *empty = NULL;
6757 isl_set_free(dom);
6758 isl_map_free(map);
6759 return NULL;
6762 __isl_give isl_map *isl_map_partial_lexmax(
6763 __isl_take isl_map *map, __isl_take isl_set *dom,
6764 __isl_give isl_set **empty)
6766 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6769 __isl_give isl_map *isl_map_partial_lexmin(
6770 __isl_take isl_map *map, __isl_take isl_set *dom,
6771 __isl_give isl_set **empty)
6773 return isl_map_partial_lexopt(map, dom, empty, 0);
6776 __isl_give isl_set *isl_set_partial_lexmin(
6777 __isl_take isl_set *set, __isl_take isl_set *dom,
6778 __isl_give isl_set **empty)
6780 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6781 dom, empty));
6784 __isl_give isl_set *isl_set_partial_lexmax(
6785 __isl_take isl_set *set, __isl_take isl_set *dom,
6786 __isl_give isl_set **empty)
6788 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6789 dom, empty));
6792 /* Compute the lexicographic minimum (or maximum if "flags" includes
6793 * ISL_OPT_MAX) of "bset" over its parametric domain.
6795 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6796 unsigned flags)
6798 return isl_basic_map_lexopt(bset, flags);
6801 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6803 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6806 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6808 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6811 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6813 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6816 /* Compute the lexicographic minimum of "bset" over its parametric domain
6817 * for the purpose of quantifier elimination.
6818 * That is, find an explicit representation for all the existentially
6819 * quantified variables in "bset" by computing their lexicographic
6820 * minimum.
6822 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6823 __isl_take isl_basic_set *bset)
6825 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6828 /* Given a basic map with one output dimension, compute the minimum or
6829 * maximum of that dimension as an isl_pw_aff.
6831 * Compute the optimum as a lexicographic optimum over the single
6832 * output dimension and extract the single isl_pw_aff from the result.
6834 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6835 int max)
6837 isl_pw_multi_aff *pma;
6838 isl_pw_aff *pwaff;
6840 bmap = isl_basic_map_copy(bmap);
6841 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6842 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6843 isl_pw_multi_aff_free(pma);
6845 return pwaff;
6848 /* Compute the minimum or maximum of the given output dimension
6849 * as a function of the parameters and the input dimensions,
6850 * but independently of the other output dimensions.
6852 * We first project out the other output dimension and then compute
6853 * the "lexicographic" maximum in each basic map, combining the results
6854 * using isl_pw_aff_union_max.
6856 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6857 int max)
6859 int i;
6860 isl_pw_aff *pwaff;
6861 unsigned n_out;
6863 n_out = isl_map_dim(map, isl_dim_out);
6864 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6865 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6866 if (!map)
6867 return NULL;
6869 if (map->n == 0) {
6870 isl_space *space = isl_map_get_space(map);
6871 isl_map_free(map);
6872 return isl_pw_aff_empty(space);
6875 pwaff = basic_map_dim_opt(map->p[0], max);
6876 for (i = 1; i < map->n; ++i) {
6877 isl_pw_aff *pwaff_i;
6879 pwaff_i = basic_map_dim_opt(map->p[i], max);
6880 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6883 isl_map_free(map);
6885 return pwaff;
6888 /* Compute the minimum of the given output dimension as a function of the
6889 * parameters and input dimensions, but independently of
6890 * the other output dimensions.
6892 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6894 return map_dim_opt(map, pos, 0);
6897 /* Compute the maximum of the given output dimension as a function of the
6898 * parameters and input dimensions, but independently of
6899 * the other output dimensions.
6901 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6903 return map_dim_opt(map, pos, 1);
6906 /* Compute the minimum or maximum of the given set dimension
6907 * as a function of the parameters,
6908 * but independently of the other set dimensions.
6910 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6911 int max)
6913 return map_dim_opt(set, pos, max);
6916 /* Compute the maximum of the given set dimension as a function of the
6917 * parameters, but independently of the other set dimensions.
6919 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6921 return set_dim_opt(set, pos, 1);
6924 /* Compute the minimum of the given set dimension as a function of the
6925 * parameters, but independently of the other set dimensions.
6927 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6929 return set_dim_opt(set, pos, 0);
6932 /* Apply a preimage specified by "mat" on the parameters of "bset".
6933 * bset is assumed to have only parameters and divs.
6935 static __isl_give isl_basic_set *basic_set_parameter_preimage(
6936 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
6938 unsigned nparam;
6940 if (!bset || !mat)
6941 goto error;
6943 bset->dim = isl_space_cow(bset->dim);
6944 if (!bset->dim)
6945 goto error;
6947 nparam = isl_basic_set_dim(bset, isl_dim_param);
6949 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6951 bset->dim->nparam = 0;
6952 bset->dim->n_out = nparam;
6953 bset = isl_basic_set_preimage(bset, mat);
6954 if (bset) {
6955 bset->dim->nparam = bset->dim->n_out;
6956 bset->dim->n_out = 0;
6958 return bset;
6959 error:
6960 isl_mat_free(mat);
6961 isl_basic_set_free(bset);
6962 return NULL;
6965 /* Apply a preimage specified by "mat" on the parameters of "set".
6966 * set is assumed to have only parameters and divs.
6968 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6969 __isl_take isl_mat *mat)
6971 isl_space *space;
6972 unsigned nparam;
6974 if (!set || !mat)
6975 goto error;
6977 nparam = isl_set_dim(set, isl_dim_param);
6979 if (mat->n_row != 1 + nparam)
6980 isl_die(isl_set_get_ctx(set), isl_error_internal,
6981 "unexpected number of rows", goto error);
6983 space = isl_set_get_space(set);
6984 space = isl_space_move_dims(space, isl_dim_set, 0,
6985 isl_dim_param, 0, nparam);
6986 set = isl_set_reset_space(set, space);
6987 set = isl_set_preimage(set, mat);
6988 nparam = isl_set_dim(set, isl_dim_out);
6989 space = isl_set_get_space(set);
6990 space = isl_space_move_dims(space, isl_dim_param, 0,
6991 isl_dim_out, 0, nparam);
6992 set = isl_set_reset_space(set, space);
6993 return set;
6994 error:
6995 isl_mat_free(mat);
6996 isl_set_free(set);
6997 return NULL;
7000 /* Intersect the basic set "bset" with the affine space specified by the
7001 * equalities in "eq".
7003 static __isl_give isl_basic_set *basic_set_append_equalities(
7004 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7006 int i, k;
7007 unsigned len;
7009 if (!bset || !eq)
7010 goto error;
7012 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7013 eq->n_row, 0);
7014 if (!bset)
7015 goto error;
7017 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7018 for (i = 0; i < eq->n_row; ++i) {
7019 k = isl_basic_set_alloc_equality(bset);
7020 if (k < 0)
7021 goto error;
7022 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7023 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7025 isl_mat_free(eq);
7027 bset = isl_basic_set_gauss(bset, NULL);
7028 bset = isl_basic_set_finalize(bset);
7030 return bset;
7031 error:
7032 isl_mat_free(eq);
7033 isl_basic_set_free(bset);
7034 return NULL;
7037 /* Intersect the set "set" with the affine space specified by the
7038 * equalities in "eq".
7040 static struct isl_set *set_append_equalities(struct isl_set *set,
7041 struct isl_mat *eq)
7043 int i;
7045 if (!set || !eq)
7046 goto error;
7048 for (i = 0; i < set->n; ++i) {
7049 set->p[i] = basic_set_append_equalities(set->p[i],
7050 isl_mat_copy(eq));
7051 if (!set->p[i])
7052 goto error;
7054 isl_mat_free(eq);
7055 return set;
7056 error:
7057 isl_mat_free(eq);
7058 isl_set_free(set);
7059 return NULL;
7062 /* Given a basic set "bset" that only involves parameters and existentially
7063 * quantified variables, return the index of the first equality
7064 * that only involves parameters. If there is no such equality then
7065 * return bset->n_eq.
7067 * This function assumes that isl_basic_set_gauss has been called on "bset".
7069 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7071 int i, j;
7072 unsigned nparam, n_div;
7074 if (!bset)
7075 return -1;
7077 nparam = isl_basic_set_dim(bset, isl_dim_param);
7078 n_div = isl_basic_set_dim(bset, isl_dim_div);
7080 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7081 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7082 ++i;
7085 return i;
7088 /* Compute an explicit representation for the existentially quantified
7089 * variables in "bset" by computing the "minimal value" of the set
7090 * variables. Since there are no set variables, the computation of
7091 * the minimal value essentially computes an explicit representation
7092 * of the non-empty part(s) of "bset".
7094 * The input only involves parameters and existentially quantified variables.
7095 * All equalities among parameters have been removed.
7097 * Since the existentially quantified variables in the result are in general
7098 * going to be different from those in the input, we first replace
7099 * them by the minimal number of variables based on their equalities.
7100 * This should simplify the parametric integer programming.
7102 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7104 isl_morph *morph1, *morph2;
7105 isl_set *set;
7106 unsigned n;
7108 if (!bset)
7109 return NULL;
7110 if (bset->n_eq == 0)
7111 return isl_basic_set_lexmin_compute_divs(bset);
7113 morph1 = isl_basic_set_parameter_compression(bset);
7114 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7115 bset = isl_basic_set_lift(bset);
7116 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7117 bset = isl_morph_basic_set(morph2, bset);
7118 n = isl_basic_set_dim(bset, isl_dim_set);
7119 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7121 set = isl_basic_set_lexmin_compute_divs(bset);
7123 set = isl_morph_set(isl_morph_inverse(morph1), set);
7125 return set;
7128 /* Project the given basic set onto its parameter domain, possibly introducing
7129 * new, explicit, existential variables in the constraints.
7130 * The input has parameters and (possibly implicit) existential variables.
7131 * The output has the same parameters, but only
7132 * explicit existentially quantified variables.
7134 * The actual projection is performed by pip, but pip doesn't seem
7135 * to like equalities very much, so we first remove the equalities
7136 * among the parameters by performing a variable compression on
7137 * the parameters. Afterward, an inverse transformation is performed
7138 * and the equalities among the parameters are inserted back in.
7140 * The variable compression on the parameters may uncover additional
7141 * equalities that were only implicit before. We therefore check
7142 * if there are any new parameter equalities in the result and
7143 * if so recurse. The removal of parameter equalities is required
7144 * for the parameter compression performed by base_compute_divs.
7146 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7148 int i;
7149 struct isl_mat *eq;
7150 struct isl_mat *T, *T2;
7151 struct isl_set *set;
7152 unsigned nparam;
7154 bset = isl_basic_set_cow(bset);
7155 if (!bset)
7156 return NULL;
7158 if (bset->n_eq == 0)
7159 return base_compute_divs(bset);
7161 bset = isl_basic_set_gauss(bset, NULL);
7162 if (!bset)
7163 return NULL;
7164 if (isl_basic_set_plain_is_empty(bset))
7165 return isl_set_from_basic_set(bset);
7167 i = first_parameter_equality(bset);
7168 if (i == bset->n_eq)
7169 return base_compute_divs(bset);
7171 nparam = isl_basic_set_dim(bset, isl_dim_param);
7172 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7173 0, 1 + nparam);
7174 eq = isl_mat_cow(eq);
7175 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7176 if (T && T->n_col == 0) {
7177 isl_mat_free(T);
7178 isl_mat_free(T2);
7179 isl_mat_free(eq);
7180 bset = isl_basic_set_set_to_empty(bset);
7181 return isl_set_from_basic_set(bset);
7183 bset = basic_set_parameter_preimage(bset, T);
7185 i = first_parameter_equality(bset);
7186 if (!bset)
7187 set = NULL;
7188 else if (i == bset->n_eq)
7189 set = base_compute_divs(bset);
7190 else
7191 set = parameter_compute_divs(bset);
7192 set = set_parameter_preimage(set, T2);
7193 set = set_append_equalities(set, eq);
7194 return set;
7197 /* Insert the divs from "ls" before those of "bmap".
7199 * The number of columns is not changed, which means that the last
7200 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7201 * The caller is responsible for removing the same number of dimensions
7202 * from the space of "bmap".
7204 static __isl_give isl_basic_map *insert_divs_from_local_space(
7205 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7207 int i;
7208 int n_div;
7209 int old_n_div;
7211 n_div = isl_local_space_dim(ls, isl_dim_div);
7212 if (n_div == 0)
7213 return bmap;
7215 old_n_div = bmap->n_div;
7216 bmap = insert_div_rows(bmap, n_div);
7217 if (!bmap)
7218 return NULL;
7220 for (i = 0; i < n_div; ++i) {
7221 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7222 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7225 return bmap;
7228 /* Replace the space of "bmap" by the space and divs of "ls".
7230 * If "ls" has any divs, then we simplify the result since we may
7231 * have discovered some additional equalities that could simplify
7232 * the div expressions.
7234 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7235 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7237 int n_div;
7239 bmap = isl_basic_map_cow(bmap);
7240 if (!bmap || !ls)
7241 goto error;
7243 n_div = isl_local_space_dim(ls, isl_dim_div);
7244 bmap = insert_divs_from_local_space(bmap, ls);
7245 if (!bmap)
7246 goto error;
7248 isl_space_free(bmap->dim);
7249 bmap->dim = isl_local_space_get_space(ls);
7250 if (!bmap->dim)
7251 goto error;
7253 isl_local_space_free(ls);
7254 if (n_div > 0)
7255 bmap = isl_basic_map_simplify(bmap);
7256 bmap = isl_basic_map_finalize(bmap);
7257 return bmap;
7258 error:
7259 isl_basic_map_free(bmap);
7260 isl_local_space_free(ls);
7261 return NULL;
7264 /* Replace the space of "map" by the space and divs of "ls".
7266 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7267 __isl_take isl_local_space *ls)
7269 int i;
7271 map = isl_map_cow(map);
7272 if (!map || !ls)
7273 goto error;
7275 for (i = 0; i < map->n; ++i) {
7276 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7277 isl_local_space_copy(ls));
7278 if (!map->p[i])
7279 goto error;
7281 isl_space_free(map->dim);
7282 map->dim = isl_local_space_get_space(ls);
7283 if (!map->dim)
7284 goto error;
7286 isl_local_space_free(ls);
7287 return map;
7288 error:
7289 isl_local_space_free(ls);
7290 isl_map_free(map);
7291 return NULL;
7294 /* Compute an explicit representation for the existentially
7295 * quantified variables for which do not know any explicit representation yet.
7297 * We first sort the existentially quantified variables so that the
7298 * existentially quantified variables for which we already have an explicit
7299 * representation are placed before those for which we do not.
7300 * The input dimensions, the output dimensions and the existentially
7301 * quantified variables for which we already have an explicit
7302 * representation are then turned into parameters.
7303 * compute_divs returns a map with the same parameters and
7304 * no input or output dimensions and the dimension specification
7305 * is reset to that of the input, including the existentially quantified
7306 * variables for which we already had an explicit representation.
7308 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7310 struct isl_basic_set *bset;
7311 struct isl_set *set;
7312 struct isl_map *map;
7313 isl_space *space;
7314 isl_local_space *ls;
7315 unsigned nparam;
7316 unsigned n_in;
7317 unsigned n_out;
7318 int n_known;
7319 int i;
7321 bmap = isl_basic_map_sort_divs(bmap);
7322 bmap = isl_basic_map_cow(bmap);
7323 if (!bmap)
7324 return NULL;
7326 n_known = isl_basic_map_first_unknown_div(bmap);
7327 if (n_known < 0)
7328 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7330 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7331 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7332 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7333 space = isl_space_set_alloc(bmap->ctx,
7334 nparam + n_in + n_out + n_known, 0);
7335 if (!space)
7336 goto error;
7338 ls = isl_basic_map_get_local_space(bmap);
7339 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7340 n_known, bmap->n_div - n_known);
7341 if (n_known > 0) {
7342 for (i = n_known; i < bmap->n_div; ++i)
7343 swap_div(bmap, i - n_known, i);
7344 bmap->n_div -= n_known;
7345 bmap->extra -= n_known;
7347 bmap = isl_basic_map_reset_space(bmap, space);
7348 bset = bset_from_bmap(bmap);
7350 set = parameter_compute_divs(bset);
7351 map = set_to_map(set);
7352 map = replace_space_by_local_space(map, ls);
7354 return map;
7355 error:
7356 isl_basic_map_free(bmap);
7357 return NULL;
7360 /* Remove the explicit representation of local variable "div",
7361 * if there is any.
7363 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7364 __isl_take isl_basic_map *bmap, int div)
7366 isl_bool unknown;
7368 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7369 if (unknown < 0)
7370 return isl_basic_map_free(bmap);
7371 if (unknown)
7372 return bmap;
7374 bmap = isl_basic_map_cow(bmap);
7375 if (!bmap)
7376 return NULL;
7377 isl_int_set_si(bmap->div[div][0], 0);
7378 return bmap;
7381 /* Is local variable "div" of "bmap" marked as not having an explicit
7382 * representation?
7383 * Note that even if "div" is not marked in this way and therefore
7384 * has an explicit representation, this representation may still
7385 * depend (indirectly) on other local variables that do not
7386 * have an explicit representation.
7388 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7389 int div)
7391 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7392 return isl_bool_error;
7393 return isl_int_is_zero(bmap->div[div][0]);
7396 /* Return the position of the first local variable that does not
7397 * have an explicit representation.
7398 * Return the total number of local variables if they all have
7399 * an explicit representation.
7400 * Return -1 on error.
7402 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7404 int i;
7406 if (!bmap)
7407 return -1;
7409 for (i = 0; i < bmap->n_div; ++i) {
7410 if (!isl_basic_map_div_is_known(bmap, i))
7411 return i;
7413 return bmap->n_div;
7416 /* Return the position of the first local variable that does not
7417 * have an explicit representation.
7418 * Return the total number of local variables if they all have
7419 * an explicit representation.
7420 * Return -1 on error.
7422 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7424 return isl_basic_map_first_unknown_div(bset);
7427 /* Does "bmap" have an explicit representation for all local variables?
7429 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7431 int first, n;
7433 n = isl_basic_map_dim(bmap, isl_dim_div);
7434 first = isl_basic_map_first_unknown_div(bmap);
7435 if (first < 0)
7436 return isl_bool_error;
7437 return first == n;
7440 /* Do all basic maps in "map" have an explicit representation
7441 * for all local variables?
7443 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7445 int i;
7447 if (!map)
7448 return isl_bool_error;
7450 for (i = 0; i < map->n; ++i) {
7451 int known = isl_basic_map_divs_known(map->p[i]);
7452 if (known <= 0)
7453 return known;
7456 return isl_bool_true;
7459 /* If bmap contains any unknown divs, then compute explicit
7460 * expressions for them. However, this computation may be
7461 * quite expensive, so first try to remove divs that aren't
7462 * strictly needed.
7464 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7466 int known;
7467 struct isl_map *map;
7469 known = isl_basic_map_divs_known(bmap);
7470 if (known < 0)
7471 goto error;
7472 if (known)
7473 return isl_map_from_basic_map(bmap);
7475 bmap = isl_basic_map_drop_redundant_divs(bmap);
7477 known = isl_basic_map_divs_known(bmap);
7478 if (known < 0)
7479 goto error;
7480 if (known)
7481 return isl_map_from_basic_map(bmap);
7483 map = compute_divs(bmap);
7484 return map;
7485 error:
7486 isl_basic_map_free(bmap);
7487 return NULL;
7490 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7492 int i;
7493 int known;
7494 struct isl_map *res;
7496 if (!map)
7497 return NULL;
7498 if (map->n == 0)
7499 return map;
7501 known = isl_map_divs_known(map);
7502 if (known < 0) {
7503 isl_map_free(map);
7504 return NULL;
7506 if (known)
7507 return map;
7509 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7510 for (i = 1 ; i < map->n; ++i) {
7511 struct isl_map *r2;
7512 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7513 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7514 res = isl_map_union_disjoint(res, r2);
7515 else
7516 res = isl_map_union(res, r2);
7518 isl_map_free(map);
7520 return res;
7523 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7525 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7528 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7530 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7533 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7535 int i;
7536 struct isl_set *set;
7538 if (!map)
7539 goto error;
7541 map = isl_map_cow(map);
7542 if (!map)
7543 return NULL;
7545 set = set_from_map(map);
7546 set->dim = isl_space_domain(set->dim);
7547 if (!set->dim)
7548 goto error;
7549 for (i = 0; i < map->n; ++i) {
7550 set->p[i] = isl_basic_map_domain(map->p[i]);
7551 if (!set->p[i])
7552 goto error;
7554 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7555 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7556 return set;
7557 error:
7558 isl_map_free(map);
7559 return NULL;
7562 /* Return the union of "map1" and "map2", where we assume for now that
7563 * "map1" and "map2" are disjoint. Note that the basic maps inside
7564 * "map1" or "map2" may not be disjoint from each other.
7565 * Also note that this function is also called from isl_map_union,
7566 * which takes care of handling the situation where "map1" and "map2"
7567 * may not be disjoint.
7569 * If one of the inputs is empty, we can simply return the other input.
7570 * Similarly, if one of the inputs is universal, then it is equal to the union.
7572 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7573 __isl_take isl_map *map2)
7575 int i;
7576 unsigned flags = 0;
7577 struct isl_map *map = NULL;
7578 int is_universe;
7580 if (!map1 || !map2)
7581 goto error;
7583 if (!isl_space_is_equal(map1->dim, map2->dim))
7584 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7585 "spaces don't match", goto error);
7587 if (map1->n == 0) {
7588 isl_map_free(map1);
7589 return map2;
7591 if (map2->n == 0) {
7592 isl_map_free(map2);
7593 return map1;
7596 is_universe = isl_map_plain_is_universe(map1);
7597 if (is_universe < 0)
7598 goto error;
7599 if (is_universe) {
7600 isl_map_free(map2);
7601 return map1;
7604 is_universe = isl_map_plain_is_universe(map2);
7605 if (is_universe < 0)
7606 goto error;
7607 if (is_universe) {
7608 isl_map_free(map1);
7609 return map2;
7612 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7613 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7614 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7616 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7617 map1->n + map2->n, flags);
7618 if (!map)
7619 goto error;
7620 for (i = 0; i < map1->n; ++i) {
7621 map = isl_map_add_basic_map(map,
7622 isl_basic_map_copy(map1->p[i]));
7623 if (!map)
7624 goto error;
7626 for (i = 0; i < map2->n; ++i) {
7627 map = isl_map_add_basic_map(map,
7628 isl_basic_map_copy(map2->p[i]));
7629 if (!map)
7630 goto error;
7632 isl_map_free(map1);
7633 isl_map_free(map2);
7634 return map;
7635 error:
7636 isl_map_free(map);
7637 isl_map_free(map1);
7638 isl_map_free(map2);
7639 return NULL;
7642 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7643 * guaranteed to be disjoint by the caller.
7645 * Note that this functions is called from within isl_map_make_disjoint,
7646 * so we have to be careful not to touch the constraints of the inputs
7647 * in any way.
7649 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7650 __isl_take isl_map *map2)
7652 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7655 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7656 * not be disjoint. The parameters are assumed to have been aligned.
7658 * We currently simply call map_union_disjoint, the internal operation
7659 * of which does not really depend on the inputs being disjoint.
7660 * If the result contains more than one basic map, then we clear
7661 * the disjoint flag since the result may contain basic maps from
7662 * both inputs and these are not guaranteed to be disjoint.
7664 * As a special case, if "map1" and "map2" are obviously equal,
7665 * then we simply return "map1".
7667 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7668 __isl_take isl_map *map2)
7670 int equal;
7672 if (!map1 || !map2)
7673 goto error;
7675 equal = isl_map_plain_is_equal(map1, map2);
7676 if (equal < 0)
7677 goto error;
7678 if (equal) {
7679 isl_map_free(map2);
7680 return map1;
7683 map1 = map_union_disjoint(map1, map2);
7684 if (!map1)
7685 return NULL;
7686 if (map1->n > 1)
7687 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7688 return map1;
7689 error:
7690 isl_map_free(map1);
7691 isl_map_free(map2);
7692 return NULL;
7695 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7696 * not be disjoint.
7698 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7699 __isl_take isl_map *map2)
7701 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7704 __isl_give isl_set *isl_set_union_disjoint(
7705 __isl_take isl_set *set1, __isl_take isl_set *set2)
7707 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7708 set_to_map(set2)));
7711 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7713 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7716 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7717 * the results.
7719 * "map" and "set" are assumed to be compatible and non-NULL.
7721 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7722 __isl_take isl_set *set,
7723 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7724 __isl_take isl_basic_set *bset))
7726 unsigned flags = 0;
7727 struct isl_map *result;
7728 int i, j;
7730 if (isl_set_plain_is_universe(set)) {
7731 isl_set_free(set);
7732 return map;
7735 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7736 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7737 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7739 result = isl_map_alloc_space(isl_space_copy(map->dim),
7740 map->n * set->n, flags);
7741 for (i = 0; result && i < map->n; ++i)
7742 for (j = 0; j < set->n; ++j) {
7743 result = isl_map_add_basic_map(result,
7744 fn(isl_basic_map_copy(map->p[i]),
7745 isl_basic_set_copy(set->p[j])));
7746 if (!result)
7747 break;
7750 isl_map_free(map);
7751 isl_set_free(set);
7752 return result;
7755 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7756 __isl_take isl_set *set)
7758 isl_bool ok;
7760 ok = isl_map_compatible_range(map, set);
7761 if (ok < 0)
7762 goto error;
7763 if (!ok)
7764 isl_die(set->ctx, isl_error_invalid,
7765 "incompatible spaces", goto error);
7767 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7768 error:
7769 isl_map_free(map);
7770 isl_set_free(set);
7771 return NULL;
7774 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7775 __isl_take isl_set *set)
7777 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7780 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7781 __isl_take isl_set *set)
7783 isl_bool ok;
7785 ok = isl_map_compatible_domain(map, set);
7786 if (ok < 0)
7787 goto error;
7788 if (!ok)
7789 isl_die(set->ctx, isl_error_invalid,
7790 "incompatible spaces", goto error);
7792 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7793 error:
7794 isl_map_free(map);
7795 isl_set_free(set);
7796 return NULL;
7799 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7800 __isl_take isl_set *set)
7802 return isl_map_align_params_map_map_and(map, set,
7803 &map_intersect_domain);
7806 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7807 * in the space B -> C, return the intersection.
7808 * The parameters are assumed to have been aligned.
7810 * The map "factor" is first extended to a map living in the space
7811 * [A -> B] -> C and then a regular intersection is computed.
7813 static __isl_give isl_map *map_intersect_domain_factor_range(
7814 __isl_take isl_map *map, __isl_take isl_map *factor)
7816 isl_space *space;
7817 isl_map *ext_factor;
7819 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7820 ext_factor = isl_map_universe(space);
7821 ext_factor = isl_map_domain_product(ext_factor, factor);
7822 return map_intersect(map, ext_factor);
7825 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7826 * in the space B -> C, return the intersection.
7828 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7829 __isl_take isl_map *map, __isl_take isl_map *factor)
7831 return isl_map_align_params_map_map_and(map, factor,
7832 &map_intersect_domain_factor_range);
7835 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7836 * in the space A -> C, return the intersection.
7838 * The map "factor" is first extended to a map living in the space
7839 * A -> [B -> C] and then a regular intersection is computed.
7841 static __isl_give isl_map *map_intersect_range_factor_range(
7842 __isl_take isl_map *map, __isl_take isl_map *factor)
7844 isl_space *space;
7845 isl_map *ext_factor;
7847 space = isl_space_range_factor_domain(isl_map_get_space(map));
7848 ext_factor = isl_map_universe(space);
7849 ext_factor = isl_map_range_product(ext_factor, factor);
7850 return isl_map_intersect(map, ext_factor);
7853 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7854 * in the space A -> C, return the intersection.
7856 __isl_give isl_map *isl_map_intersect_range_factor_range(
7857 __isl_take isl_map *map, __isl_take isl_map *factor)
7859 return isl_map_align_params_map_map_and(map, factor,
7860 &map_intersect_range_factor_range);
7863 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7864 __isl_take isl_map *map2)
7866 if (!map1 || !map2)
7867 goto error;
7868 map1 = isl_map_reverse(map1);
7869 map1 = isl_map_apply_range(map1, map2);
7870 return isl_map_reverse(map1);
7871 error:
7872 isl_map_free(map1);
7873 isl_map_free(map2);
7874 return NULL;
7877 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7878 __isl_take isl_map *map2)
7880 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7883 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7884 __isl_take isl_map *map2)
7886 isl_space *space;
7887 struct isl_map *result;
7888 int i, j;
7890 if (!map1 || !map2)
7891 goto error;
7893 space = isl_space_join(isl_space_copy(map1->dim),
7894 isl_space_copy(map2->dim));
7896 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
7897 if (!result)
7898 goto error;
7899 for (i = 0; i < map1->n; ++i)
7900 for (j = 0; j < map2->n; ++j) {
7901 result = isl_map_add_basic_map(result,
7902 isl_basic_map_apply_range(
7903 isl_basic_map_copy(map1->p[i]),
7904 isl_basic_map_copy(map2->p[j])));
7905 if (!result)
7906 goto error;
7908 isl_map_free(map1);
7909 isl_map_free(map2);
7910 if (result && result->n <= 1)
7911 ISL_F_SET(result, ISL_MAP_DISJOINT);
7912 return result;
7913 error:
7914 isl_map_free(map1);
7915 isl_map_free(map2);
7916 return NULL;
7919 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7920 __isl_take isl_map *map2)
7922 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7926 * returns range - domain
7928 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
7930 isl_space *target_space;
7931 struct isl_basic_set *bset;
7932 unsigned dim;
7933 unsigned nparam;
7934 int i;
7936 if (!bmap)
7937 goto error;
7938 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7939 bmap->dim, isl_dim_out),
7940 goto error);
7941 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7942 dim = isl_basic_map_dim(bmap, isl_dim_in);
7943 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7944 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7945 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7946 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7947 for (i = 0; i < dim; ++i) {
7948 int j = isl_basic_map_alloc_equality(bmap);
7949 if (j < 0) {
7950 bmap = isl_basic_map_free(bmap);
7951 break;
7953 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7954 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7955 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7956 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7958 bset = isl_basic_map_domain(bmap);
7959 bset = isl_basic_set_reset_space(bset, target_space);
7960 return bset;
7961 error:
7962 isl_basic_map_free(bmap);
7963 return NULL;
7966 /* Check that domain and range of "map" are the same.
7968 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
7970 isl_space *space;
7971 isl_bool equal;
7973 space = isl_map_peek_space(map);
7974 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
7975 if (equal < 0)
7976 return isl_stat_error;
7977 if (!equal)
7978 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7979 "domain and range don't match", return isl_stat_error);
7980 return isl_stat_ok;
7984 * returns range - domain
7986 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7988 int i;
7989 isl_space *dim;
7990 struct isl_set *result;
7992 if (!map)
7993 return NULL;
7995 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7996 map->dim, isl_dim_out),
7997 goto error);
7998 dim = isl_map_get_space(map);
7999 dim = isl_space_domain(dim);
8000 result = isl_set_alloc_space(dim, map->n, 0);
8001 if (!result)
8002 goto error;
8003 for (i = 0; i < map->n; ++i)
8004 result = isl_set_add_basic_set(result,
8005 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8006 isl_map_free(map);
8007 return result;
8008 error:
8009 isl_map_free(map);
8010 return NULL;
8014 * returns [domain -> range] -> range - domain
8016 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8017 __isl_take isl_basic_map *bmap)
8019 int i, k;
8020 isl_space *space;
8021 isl_basic_map *domain;
8022 int nparam, n;
8023 unsigned total;
8025 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8026 bmap->dim, isl_dim_out))
8027 isl_die(bmap->ctx, isl_error_invalid,
8028 "domain and range don't match", goto error);
8030 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8031 n = isl_basic_map_dim(bmap, isl_dim_in);
8033 space = isl_basic_map_get_space(bmap);
8034 space = isl_space_from_range(isl_space_domain(space));
8035 domain = isl_basic_map_universe(space);
8037 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8038 bmap = isl_basic_map_apply_range(bmap, domain);
8039 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8041 total = isl_basic_map_total_dim(bmap);
8043 for (i = 0; i < n; ++i) {
8044 k = isl_basic_map_alloc_equality(bmap);
8045 if (k < 0)
8046 goto error;
8047 isl_seq_clr(bmap->eq[k], 1 + total);
8048 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8049 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8050 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8053 bmap = isl_basic_map_gauss(bmap, NULL);
8054 return isl_basic_map_finalize(bmap);
8055 error:
8056 isl_basic_map_free(bmap);
8057 return NULL;
8061 * returns [domain -> range] -> range - domain
8063 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8065 int i;
8066 isl_space *domain_space;
8068 if (isl_map_check_equal_tuples(map) < 0)
8069 return isl_map_free(map);
8071 map = isl_map_cow(map);
8072 if (!map)
8073 return NULL;
8075 domain_space = isl_space_domain(isl_map_get_space(map));
8076 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8077 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8078 if (!map->dim)
8079 goto error;
8080 for (i = 0; i < map->n; ++i) {
8081 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8082 if (!map->p[i])
8083 goto error;
8085 map = isl_map_unmark_normalized(map);
8086 return map;
8087 error:
8088 isl_map_free(map);
8089 return NULL;
8092 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8094 unsigned n_in, n_out;
8096 if (!space)
8097 return NULL;
8098 n_in = isl_space_dim(space, isl_dim_in);
8099 n_out = isl_space_dim(space, isl_dim_out);
8100 if (n_in != n_out)
8101 isl_die(space->ctx, isl_error_invalid,
8102 "number of input and output dimensions needs to be "
8103 "the same", goto error);
8104 return isl_basic_map_equal(space, n_in);
8105 error:
8106 isl_space_free(space);
8107 return NULL;
8110 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8112 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8115 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8117 isl_space *dim = isl_set_get_space(set);
8118 isl_map *id;
8119 id = isl_map_identity(isl_space_map_from_set(dim));
8120 return isl_map_intersect_range(id, set);
8123 /* Construct a basic set with all set dimensions having only non-negative
8124 * values.
8126 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8127 __isl_take isl_space *space)
8129 int i;
8130 unsigned nparam;
8131 unsigned dim;
8132 struct isl_basic_set *bset;
8134 if (!space)
8135 return NULL;
8136 nparam = isl_space_dim(space, isl_dim_param);
8137 dim = isl_space_dim(space, isl_dim_set);
8138 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8139 if (!bset)
8140 return NULL;
8141 for (i = 0; i < dim; ++i) {
8142 int k = isl_basic_set_alloc_inequality(bset);
8143 if (k < 0)
8144 goto error;
8145 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8146 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8148 return bset;
8149 error:
8150 isl_basic_set_free(bset);
8151 return NULL;
8154 /* Construct the half-space x_pos >= 0.
8156 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8157 int pos)
8159 int k;
8160 isl_basic_set *nonneg;
8162 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8163 k = isl_basic_set_alloc_inequality(nonneg);
8164 if (k < 0)
8165 goto error;
8166 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8167 isl_int_set_si(nonneg->ineq[k][pos], 1);
8169 return isl_basic_set_finalize(nonneg);
8170 error:
8171 isl_basic_set_free(nonneg);
8172 return NULL;
8175 /* Construct the half-space x_pos <= -1.
8177 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8178 int pos)
8180 int k;
8181 isl_basic_set *neg;
8183 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8184 k = isl_basic_set_alloc_inequality(neg);
8185 if (k < 0)
8186 goto error;
8187 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8188 isl_int_set_si(neg->ineq[k][0], -1);
8189 isl_int_set_si(neg->ineq[k][pos], -1);
8191 return isl_basic_set_finalize(neg);
8192 error:
8193 isl_basic_set_free(neg);
8194 return NULL;
8197 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8198 enum isl_dim_type type, unsigned first, unsigned n)
8200 int i;
8201 unsigned offset;
8202 isl_basic_set *nonneg;
8203 isl_basic_set *neg;
8205 if (n == 0)
8206 return set;
8208 if (isl_set_check_range(set, type, first, n) < 0)
8209 return isl_set_free(set);
8211 offset = pos(set->dim, type);
8212 for (i = 0; i < n; ++i) {
8213 nonneg = nonneg_halfspace(isl_set_get_space(set),
8214 offset + first + i);
8215 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8217 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8220 return set;
8223 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8224 int len,
8225 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8226 void *user)
8228 isl_set *half;
8230 if (!set)
8231 return isl_stat_error;
8232 if (isl_set_plain_is_empty(set)) {
8233 isl_set_free(set);
8234 return isl_stat_ok;
8236 if (first == len)
8237 return fn(set, signs, user);
8239 signs[first] = 1;
8240 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8241 1 + first));
8242 half = isl_set_intersect(half, isl_set_copy(set));
8243 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8244 goto error;
8246 signs[first] = -1;
8247 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8248 1 + first));
8249 half = isl_set_intersect(half, set);
8250 return foreach_orthant(half, signs, first + 1, len, fn, user);
8251 error:
8252 isl_set_free(set);
8253 return isl_stat_error;
8256 /* Call "fn" on the intersections of "set" with each of the orthants
8257 * (except for obviously empty intersections). The orthant is identified
8258 * by the signs array, with each entry having value 1 or -1 according
8259 * to the sign of the corresponding variable.
8261 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8262 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8263 void *user)
8265 unsigned nparam;
8266 unsigned nvar;
8267 int *signs;
8268 isl_stat r;
8270 if (!set)
8271 return isl_stat_error;
8272 if (isl_set_plain_is_empty(set))
8273 return isl_stat_ok;
8275 nparam = isl_set_dim(set, isl_dim_param);
8276 nvar = isl_set_dim(set, isl_dim_set);
8278 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8280 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8281 fn, user);
8283 free(signs);
8285 return r;
8288 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8290 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8293 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8294 __isl_keep isl_basic_map *bmap2)
8296 isl_bool is_subset;
8297 struct isl_map *map1;
8298 struct isl_map *map2;
8300 if (!bmap1 || !bmap2)
8301 return isl_bool_error;
8303 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8304 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8306 is_subset = isl_map_is_subset(map1, map2);
8308 isl_map_free(map1);
8309 isl_map_free(map2);
8311 return is_subset;
8314 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8315 __isl_keep isl_basic_set *bset2)
8317 return isl_basic_map_is_subset(bset1, bset2);
8320 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8321 __isl_keep isl_basic_map *bmap2)
8323 isl_bool is_subset;
8325 if (!bmap1 || !bmap2)
8326 return isl_bool_error;
8327 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8328 if (is_subset != isl_bool_true)
8329 return is_subset;
8330 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8331 return is_subset;
8334 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8335 __isl_keep isl_basic_set *bset2)
8337 return isl_basic_map_is_equal(
8338 bset_to_bmap(bset1), bset_to_bmap(bset2));
8341 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8343 int i;
8344 int is_empty;
8346 if (!map)
8347 return isl_bool_error;
8348 for (i = 0; i < map->n; ++i) {
8349 is_empty = isl_basic_map_is_empty(map->p[i]);
8350 if (is_empty < 0)
8351 return isl_bool_error;
8352 if (!is_empty)
8353 return isl_bool_false;
8355 return isl_bool_true;
8358 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8360 return map ? map->n == 0 : isl_bool_error;
8363 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8365 return set ? set->n == 0 : isl_bool_error;
8368 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8370 return isl_map_is_empty(set_to_map(set));
8373 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8374 __isl_keep isl_map *map2)
8376 if (!map1 || !map2)
8377 return isl_bool_error;
8379 return isl_space_is_equal(map1->dim, map2->dim);
8382 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8383 __isl_keep isl_set *set2)
8385 if (!set1 || !set2)
8386 return isl_bool_error;
8388 return isl_space_is_equal(set1->dim, set2->dim);
8391 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8393 isl_bool is_subset;
8395 if (!map1 || !map2)
8396 return isl_bool_error;
8397 is_subset = isl_map_is_subset(map1, map2);
8398 if (is_subset != isl_bool_true)
8399 return is_subset;
8400 is_subset = isl_map_is_subset(map2, map1);
8401 return is_subset;
8404 /* Is "map1" equal to "map2"?
8406 * First check if they are obviously equal.
8407 * If not, then perform a more detailed analysis.
8409 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8411 isl_bool equal;
8413 equal = isl_map_plain_is_equal(map1, map2);
8414 if (equal < 0 || equal)
8415 return equal;
8416 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8419 isl_bool isl_basic_map_is_strict_subset(
8420 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8422 isl_bool is_subset;
8424 if (!bmap1 || !bmap2)
8425 return isl_bool_error;
8426 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8427 if (is_subset != isl_bool_true)
8428 return is_subset;
8429 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8430 return isl_bool_not(is_subset);
8433 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8434 __isl_keep isl_map *map2)
8436 isl_bool is_subset;
8438 if (!map1 || !map2)
8439 return isl_bool_error;
8440 is_subset = isl_map_is_subset(map1, map2);
8441 if (is_subset != isl_bool_true)
8442 return is_subset;
8443 is_subset = isl_map_is_subset(map2, map1);
8444 return isl_bool_not(is_subset);
8447 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8448 __isl_keep isl_set *set2)
8450 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8453 /* Is "bmap" obviously equal to the universe with the same space?
8455 * That is, does it not have any constraints?
8457 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8459 if (!bmap)
8460 return isl_bool_error;
8461 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8464 /* Is "bset" obviously equal to the universe with the same space?
8466 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8468 return isl_basic_map_plain_is_universe(bset);
8471 /* If "c" does not involve any existentially quantified variables,
8472 * then set *univ to false and abort
8474 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8476 isl_bool *univ = user;
8477 unsigned n;
8479 n = isl_constraint_dim(c, isl_dim_div);
8480 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8481 isl_constraint_free(c);
8482 if (*univ < 0 || !*univ)
8483 return isl_stat_error;
8484 return isl_stat_ok;
8487 /* Is "bmap" equal to the universe with the same space?
8489 * First check if it is obviously equal to the universe.
8490 * If not and if there are any constraints not involving
8491 * existentially quantified variables, then it is certainly
8492 * not equal to the universe.
8493 * Otherwise, check if the universe is a subset of "bmap".
8495 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8497 isl_bool univ;
8498 isl_basic_map *test;
8500 univ = isl_basic_map_plain_is_universe(bmap);
8501 if (univ < 0 || univ)
8502 return univ;
8503 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8504 return isl_bool_false;
8505 univ = isl_bool_true;
8506 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8507 univ)
8508 return isl_bool_error;
8509 if (univ < 0 || !univ)
8510 return univ;
8511 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8512 univ = isl_basic_map_is_subset(test, bmap);
8513 isl_basic_map_free(test);
8514 return univ;
8517 /* Is "bset" equal to the universe with the same space?
8519 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8521 return isl_basic_map_is_universe(bset);
8524 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8526 int i;
8528 if (!map)
8529 return isl_bool_error;
8531 for (i = 0; i < map->n; ++i) {
8532 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8533 if (r < 0 || r)
8534 return r;
8537 return isl_bool_false;
8540 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8542 return isl_map_plain_is_universe(set_to_map(set));
8545 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8547 struct isl_basic_set *bset = NULL;
8548 struct isl_vec *sample = NULL;
8549 isl_bool empty, non_empty;
8551 if (!bmap)
8552 return isl_bool_error;
8554 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8555 return isl_bool_true;
8557 if (isl_basic_map_plain_is_universe(bmap))
8558 return isl_bool_false;
8560 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8561 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8562 copy = isl_basic_map_remove_redundancies(copy);
8563 empty = isl_basic_map_plain_is_empty(copy);
8564 isl_basic_map_free(copy);
8565 return empty;
8568 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8569 if (non_empty < 0)
8570 return isl_bool_error;
8571 if (non_empty)
8572 return isl_bool_false;
8573 isl_vec_free(bmap->sample);
8574 bmap->sample = NULL;
8575 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8576 if (!bset)
8577 return isl_bool_error;
8578 sample = isl_basic_set_sample_vec(bset);
8579 if (!sample)
8580 return isl_bool_error;
8581 empty = sample->size == 0;
8582 isl_vec_free(bmap->sample);
8583 bmap->sample = sample;
8584 if (empty)
8585 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8587 return empty;
8590 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8592 if (!bmap)
8593 return isl_bool_error;
8594 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8597 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8599 if (!bset)
8600 return isl_bool_error;
8601 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8604 /* Is "bmap" known to be non-empty?
8606 * That is, is the cached sample still valid?
8608 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8610 unsigned total;
8612 if (!bmap)
8613 return isl_bool_error;
8614 if (!bmap->sample)
8615 return isl_bool_false;
8616 total = 1 + isl_basic_map_total_dim(bmap);
8617 if (bmap->sample->size != total)
8618 return isl_bool_false;
8619 return isl_basic_map_contains(bmap, bmap->sample);
8622 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8624 return isl_basic_map_is_empty(bset_to_bmap(bset));
8627 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8628 __isl_take isl_basic_map *bmap2)
8630 struct isl_map *map;
8631 if (!bmap1 || !bmap2)
8632 goto error;
8634 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8636 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8637 if (!map)
8638 goto error;
8639 map = isl_map_add_basic_map(map, bmap1);
8640 map = isl_map_add_basic_map(map, bmap2);
8641 return map;
8642 error:
8643 isl_basic_map_free(bmap1);
8644 isl_basic_map_free(bmap2);
8645 return NULL;
8648 struct isl_set *isl_basic_set_union(
8649 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8651 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8652 bset_to_bmap(bset2)));
8655 /* Order divs such that any div only depends on previous divs */
8656 __isl_give isl_basic_map *isl_basic_map_order_divs(
8657 __isl_take isl_basic_map *bmap)
8659 int i;
8660 int off;
8662 off = isl_basic_map_var_offset(bmap, isl_dim_div);
8663 if (off < 0)
8664 return isl_basic_map_free(bmap);
8666 for (i = 0; i < bmap->n_div; ++i) {
8667 int pos;
8668 if (isl_int_is_zero(bmap->div[i][0]))
8669 continue;
8670 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8671 bmap->n_div-i);
8672 if (pos == -1)
8673 continue;
8674 if (pos == 0)
8675 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8676 "integer division depends on itself",
8677 return isl_basic_map_free(bmap));
8678 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8679 if (!bmap)
8680 return NULL;
8681 --i;
8683 return bmap;
8686 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8688 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8691 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8693 int i;
8695 if (!map)
8696 return 0;
8698 for (i = 0; i < map->n; ++i) {
8699 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8700 if (!map->p[i])
8701 goto error;
8704 return map;
8705 error:
8706 isl_map_free(map);
8707 return NULL;
8710 /* Sort the local variables of "bset".
8712 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8713 __isl_take isl_basic_set *bset)
8715 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8718 /* Apply the expansion computed by isl_merge_divs.
8719 * The expansion itself is given by "exp" while the resulting
8720 * list of divs is given by "div".
8722 * Move the integer divisions of "bmap" into the right position
8723 * according to "exp" and then introduce the additional integer
8724 * divisions, adding div constraints.
8725 * The moving should be done first to avoid moving coefficients
8726 * in the definitions of the extra integer divisions.
8728 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8729 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8731 int i, j;
8732 int n_div;
8734 bmap = isl_basic_map_cow(bmap);
8735 if (!bmap || !div)
8736 goto error;
8738 if (div->n_row < bmap->n_div)
8739 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8740 "not an expansion", goto error);
8742 n_div = bmap->n_div;
8743 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8744 div->n_row - n_div, 0,
8745 2 * (div->n_row - n_div));
8747 for (i = n_div; i < div->n_row; ++i)
8748 if (isl_basic_map_alloc_div(bmap) < 0)
8749 goto error;
8751 for (j = n_div - 1; j >= 0; --j) {
8752 if (exp[j] == j)
8753 break;
8754 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8755 if (!bmap)
8756 goto error;
8758 j = 0;
8759 for (i = 0; i < div->n_row; ++i) {
8760 if (j < n_div && exp[j] == i) {
8761 j++;
8762 } else {
8763 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8764 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8765 continue;
8766 bmap = isl_basic_map_add_div_constraints(bmap, i);
8767 if (!bmap)
8768 goto error;
8772 isl_mat_free(div);
8773 return bmap;
8774 error:
8775 isl_basic_map_free(bmap);
8776 isl_mat_free(div);
8777 return NULL;
8780 /* Apply the expansion computed by isl_merge_divs.
8781 * The expansion itself is given by "exp" while the resulting
8782 * list of divs is given by "div".
8784 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8785 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8787 return isl_basic_map_expand_divs(bset, div, exp);
8790 /* Look for a div in dst that corresponds to the div "div" in src.
8791 * The divs before "div" in src and dst are assumed to be the same.
8793 * Return the position of the corresponding div in dst
8794 * if there is one. Otherwise, return a position beyond the integer divisions.
8795 * Return -1 on error.
8797 static int find_div(__isl_keep isl_basic_map *dst,
8798 __isl_keep isl_basic_map *src, unsigned div)
8800 int i;
8801 unsigned n_div;
8802 int v_div;
8804 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8805 if (!dst || v_div < 0)
8806 return -1;
8808 n_div = isl_basic_map_dim(dst, isl_dim_div);
8809 isl_assert(dst->ctx, div <= n_div, return -1);
8810 for (i = div; i < n_div; ++i)
8811 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
8812 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
8813 n_div - div) == -1)
8814 return i;
8815 return n_div;
8818 /* Align the divs of "dst" to those of "src", adding divs from "src"
8819 * if needed. That is, make sure that the first src->n_div divs
8820 * of the result are equal to those of src.
8822 * The result is not finalized as by design it will have redundant
8823 * divs if any divs from "src" were copied.
8825 __isl_give isl_basic_map *isl_basic_map_align_divs(
8826 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8828 int i;
8829 isl_bool known;
8830 int extended;
8831 int v_div;
8832 unsigned dst_n_div;
8834 if (!dst || !src)
8835 return isl_basic_map_free(dst);
8837 if (src->n_div == 0)
8838 return dst;
8840 known = isl_basic_map_divs_known(src);
8841 if (known < 0)
8842 return isl_basic_map_free(dst);
8843 if (!known)
8844 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8845 "some src divs are unknown",
8846 return isl_basic_map_free(dst));
8848 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8849 if (v_div < 0)
8850 return isl_basic_map_free(dst);
8852 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8853 if (!src)
8854 return isl_basic_map_free(dst);
8856 extended = 0;
8857 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
8858 for (i = 0; i < src->n_div; ++i) {
8859 int j = find_div(dst, src, i);
8860 if (j < 0)
8861 dst = isl_basic_map_free(dst);
8862 if (j == dst_n_div) {
8863 if (!extended) {
8864 int extra = src->n_div - i;
8865 dst = isl_basic_map_cow(dst);
8866 if (!dst)
8867 goto error;
8868 dst = isl_basic_map_extend_space(dst,
8869 isl_space_copy(dst->dim),
8870 extra, 0, 2 * extra);
8871 extended = 1;
8873 j = isl_basic_map_alloc_div(dst);
8874 if (j < 0)
8875 goto error;
8876 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
8877 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
8878 dst_n_div++;
8879 dst = isl_basic_map_add_div_constraints(dst, j);
8880 if (!dst)
8881 goto error;
8883 if (j != i)
8884 dst = isl_basic_map_swap_div(dst, i, j);
8885 if (!dst)
8886 goto error;
8888 isl_basic_map_free(src);
8889 return dst;
8890 error:
8891 isl_basic_map_free(src);
8892 isl_basic_map_free(dst);
8893 return NULL;
8896 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8898 int i;
8900 if (!map)
8901 return NULL;
8902 if (map->n == 0)
8903 return map;
8904 map = isl_map_compute_divs(map);
8905 map = isl_map_cow(map);
8906 if (!map)
8907 return NULL;
8909 for (i = 1; i < map->n; ++i)
8910 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8911 for (i = 1; i < map->n; ++i) {
8912 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8913 if (!map->p[i])
8914 return isl_map_free(map);
8917 map = isl_map_unmark_normalized(map);
8918 return map;
8921 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8923 return isl_map_align_divs_internal(map);
8926 struct isl_set *isl_set_align_divs(struct isl_set *set)
8928 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8931 /* Align the divs of the basic maps in "map" to those
8932 * of the basic maps in "list", as well as to the other basic maps in "map".
8933 * The elements in "list" are assumed to have known divs.
8935 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8936 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8938 int i, n;
8940 map = isl_map_compute_divs(map);
8941 map = isl_map_cow(map);
8942 if (!map || !list)
8943 return isl_map_free(map);
8944 if (map->n == 0)
8945 return map;
8947 n = isl_basic_map_list_n_basic_map(list);
8948 for (i = 0; i < n; ++i) {
8949 isl_basic_map *bmap;
8951 bmap = isl_basic_map_list_get_basic_map(list, i);
8952 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8953 isl_basic_map_free(bmap);
8955 if (!map->p[0])
8956 return isl_map_free(map);
8958 return isl_map_align_divs_internal(map);
8961 /* Align the divs of each element of "list" to those of "bmap".
8962 * Both "bmap" and the elements of "list" are assumed to have known divs.
8964 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8965 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8967 int i, n;
8969 if (!list || !bmap)
8970 return isl_basic_map_list_free(list);
8972 n = isl_basic_map_list_n_basic_map(list);
8973 for (i = 0; i < n; ++i) {
8974 isl_basic_map *bmap_i;
8976 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8977 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8978 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8981 return list;
8984 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8985 __isl_take isl_map *map)
8987 isl_bool ok;
8989 ok = isl_map_compatible_domain(map, set);
8990 if (ok < 0)
8991 goto error;
8992 if (!ok)
8993 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8994 "incompatible spaces", goto error);
8995 map = isl_map_intersect_domain(map, set);
8996 set = isl_map_range(map);
8997 return set;
8998 error:
8999 isl_set_free(set);
9000 isl_map_free(map);
9001 return NULL;
9004 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9005 __isl_take isl_map *map)
9007 return isl_map_align_params_map_map_and(set, map, &set_apply);
9010 /* There is no need to cow as removing empty parts doesn't change
9011 * the meaning of the set.
9013 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9015 int i;
9017 if (!map)
9018 return NULL;
9020 for (i = map->n - 1; i >= 0; --i)
9021 map = remove_if_empty(map, i);
9023 return map;
9026 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9028 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9031 /* Create a binary relation that maps the shared initial "pos" dimensions
9032 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9034 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9035 __isl_keep isl_basic_set *bset2, int pos)
9037 isl_basic_map *bmap1;
9038 isl_basic_map *bmap2;
9040 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9041 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9042 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9043 isl_dim_out, 0, pos);
9044 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9045 isl_dim_out, 0, pos);
9046 return isl_basic_map_range_product(bmap1, bmap2);
9049 /* Given two basic sets bset1 and bset2, compute the maximal difference
9050 * between the values of dimension pos in bset1 and those in bset2
9051 * for any common value of the parameters and dimensions preceding pos.
9053 static enum isl_lp_result basic_set_maximal_difference_at(
9054 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9055 int pos, isl_int *opt)
9057 isl_basic_map *bmap1;
9058 struct isl_ctx *ctx;
9059 struct isl_vec *obj;
9060 unsigned total;
9061 unsigned nparam;
9062 unsigned dim1;
9063 enum isl_lp_result res;
9065 if (!bset1 || !bset2)
9066 return isl_lp_error;
9068 nparam = isl_basic_set_n_param(bset1);
9069 dim1 = isl_basic_set_n_dim(bset1);
9071 bmap1 = join_initial(bset1, bset2, pos);
9072 if (!bmap1)
9073 return isl_lp_error;
9075 total = isl_basic_map_total_dim(bmap1);
9076 ctx = bmap1->ctx;
9077 obj = isl_vec_alloc(ctx, 1 + total);
9078 if (!obj)
9079 goto error;
9080 isl_seq_clr(obj->block.data, 1 + total);
9081 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9082 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9083 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9084 opt, NULL, NULL);
9085 isl_basic_map_free(bmap1);
9086 isl_vec_free(obj);
9087 return res;
9088 error:
9089 isl_basic_map_free(bmap1);
9090 return isl_lp_error;
9093 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9094 * for any common value of the parameters and dimensions preceding pos
9095 * in both basic sets, the values of dimension pos in bset1 are
9096 * smaller or larger than those in bset2.
9098 * Returns
9099 * 1 if bset1 follows bset2
9100 * -1 if bset1 precedes bset2
9101 * 0 if bset1 and bset2 are incomparable
9102 * -2 if some error occurred.
9104 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9105 __isl_keep isl_basic_set *bset2, int pos)
9107 isl_int opt;
9108 enum isl_lp_result res;
9109 int cmp;
9111 isl_int_init(opt);
9113 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9115 if (res == isl_lp_empty)
9116 cmp = 0;
9117 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9118 res == isl_lp_unbounded)
9119 cmp = 1;
9120 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9121 cmp = -1;
9122 else
9123 cmp = -2;
9125 isl_int_clear(opt);
9126 return cmp;
9129 /* Given two basic sets bset1 and bset2, check whether
9130 * for any common value of the parameters and dimensions preceding pos
9131 * there is a value of dimension pos in bset1 that is larger
9132 * than a value of the same dimension in bset2.
9134 * Return
9135 * 1 if there exists such a pair
9136 * 0 if there is no such pair, but there is a pair of equal values
9137 * -1 otherwise
9138 * -2 if some error occurred.
9140 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9141 __isl_keep isl_basic_set *bset2, int pos)
9143 isl_bool empty;
9144 isl_basic_map *bmap;
9145 unsigned dim1;
9147 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9148 bmap = join_initial(bset1, bset2, pos);
9149 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9150 isl_dim_out, dim1 - pos);
9151 empty = isl_basic_map_is_empty(bmap);
9152 if (empty < 0)
9153 goto error;
9154 if (empty) {
9155 isl_basic_map_free(bmap);
9156 return -1;
9158 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9159 isl_dim_out, dim1 - pos);
9160 empty = isl_basic_map_is_empty(bmap);
9161 if (empty < 0)
9162 goto error;
9163 isl_basic_map_free(bmap);
9164 if (empty)
9165 return 0;
9166 return 1;
9167 error:
9168 isl_basic_map_free(bmap);
9169 return -2;
9172 /* Given two sets set1 and set2, check whether
9173 * for any common value of the parameters and dimensions preceding pos
9174 * there is a value of dimension pos in set1 that is larger
9175 * than a value of the same dimension in set2.
9177 * Return
9178 * 1 if there exists such a pair
9179 * 0 if there is no such pair, but there is a pair of equal values
9180 * -1 otherwise
9181 * -2 if some error occurred.
9183 int isl_set_follows_at(__isl_keep isl_set *set1,
9184 __isl_keep isl_set *set2, int pos)
9186 int i, j;
9187 int follows = -1;
9189 if (!set1 || !set2)
9190 return -2;
9192 for (i = 0; i < set1->n; ++i)
9193 for (j = 0; j < set2->n; ++j) {
9194 int f;
9195 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9196 if (f == 1 || f == -2)
9197 return f;
9198 if (f > follows)
9199 follows = f;
9202 return follows;
9205 static isl_bool isl_basic_map_plain_has_fixed_var(
9206 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9208 int i;
9209 int d;
9210 unsigned total;
9212 if (!bmap)
9213 return isl_bool_error;
9214 total = isl_basic_map_total_dim(bmap);
9215 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9216 for (; d+1 > pos; --d)
9217 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9218 break;
9219 if (d != pos)
9220 continue;
9221 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9222 return isl_bool_false;
9223 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9224 return isl_bool_false;
9225 if (!isl_int_is_one(bmap->eq[i][1+d]))
9226 return isl_bool_false;
9227 if (val)
9228 isl_int_neg(*val, bmap->eq[i][0]);
9229 return isl_bool_true;
9231 return isl_bool_false;
9234 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9235 unsigned pos, isl_int *val)
9237 int i;
9238 isl_int v;
9239 isl_int tmp;
9240 isl_bool fixed;
9242 if (!map)
9243 return isl_bool_error;
9244 if (map->n == 0)
9245 return isl_bool_false;
9246 if (map->n == 1)
9247 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9248 isl_int_init(v);
9249 isl_int_init(tmp);
9250 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9251 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9252 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9253 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9254 fixed = isl_bool_false;
9256 if (val)
9257 isl_int_set(*val, v);
9258 isl_int_clear(tmp);
9259 isl_int_clear(v);
9260 return fixed;
9263 static isl_bool isl_basic_set_plain_has_fixed_var(
9264 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9266 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9267 pos, val);
9270 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9271 enum isl_dim_type type, unsigned pos, isl_int *val)
9273 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9274 return isl_bool_error;
9275 return isl_basic_map_plain_has_fixed_var(bmap,
9276 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9279 /* If "bmap" obviously lies on a hyperplane where the given dimension
9280 * has a fixed value, then return that value.
9281 * Otherwise return NaN.
9283 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9284 __isl_keep isl_basic_map *bmap,
9285 enum isl_dim_type type, unsigned pos)
9287 isl_ctx *ctx;
9288 isl_val *v;
9289 isl_bool fixed;
9291 if (!bmap)
9292 return NULL;
9293 ctx = isl_basic_map_get_ctx(bmap);
9294 v = isl_val_alloc(ctx);
9295 if (!v)
9296 return NULL;
9297 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9298 if (fixed < 0)
9299 return isl_val_free(v);
9300 if (fixed) {
9301 isl_int_set_si(v->d, 1);
9302 return v;
9304 isl_val_free(v);
9305 return isl_val_nan(ctx);
9308 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9309 enum isl_dim_type type, unsigned pos, isl_int *val)
9311 if (isl_map_check_range(map, type, pos, 1) < 0)
9312 return isl_bool_error;
9313 return isl_map_plain_has_fixed_var(map,
9314 map_offset(map, type) - 1 + pos, val);
9317 /* If "map" obviously lies on a hyperplane where the given dimension
9318 * has a fixed value, then return that value.
9319 * Otherwise return NaN.
9321 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9322 enum isl_dim_type type, unsigned pos)
9324 isl_ctx *ctx;
9325 isl_val *v;
9326 isl_bool fixed;
9328 if (!map)
9329 return NULL;
9330 ctx = isl_map_get_ctx(map);
9331 v = isl_val_alloc(ctx);
9332 if (!v)
9333 return NULL;
9334 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9335 if (fixed < 0)
9336 return isl_val_free(v);
9337 if (fixed) {
9338 isl_int_set_si(v->d, 1);
9339 return v;
9341 isl_val_free(v);
9342 return isl_val_nan(ctx);
9345 /* If "set" obviously lies on a hyperplane where the given dimension
9346 * has a fixed value, then return that value.
9347 * Otherwise return NaN.
9349 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9350 enum isl_dim_type type, unsigned pos)
9352 return isl_map_plain_get_val_if_fixed(set, type, pos);
9355 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9356 * then return this fixed value in *val.
9358 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9359 unsigned dim, isl_int *val)
9361 return isl_basic_set_plain_has_fixed_var(bset,
9362 isl_basic_set_n_param(bset) + dim, val);
9365 /* Return -1 if the constraint "c1" should be sorted before "c2"
9366 * and 1 if it should be sorted after "c2".
9367 * Return 0 if the two constraints are the same (up to the constant term).
9369 * In particular, if a constraint involves later variables than another
9370 * then it is sorted after this other constraint.
9371 * uset_gist depends on constraints without existentially quantified
9372 * variables sorting first.
9374 * For constraints that have the same latest variable, those
9375 * with the same coefficient for this latest variable (first in absolute value
9376 * and then in actual value) are grouped together.
9377 * This is useful for detecting pairs of constraints that can
9378 * be chained in their printed representation.
9380 * Finally, within a group, constraints are sorted according to
9381 * their coefficients (excluding the constant term).
9383 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9385 isl_int **c1 = (isl_int **) p1;
9386 isl_int **c2 = (isl_int **) p2;
9387 int l1, l2;
9388 unsigned size = *(unsigned *) arg;
9389 int cmp;
9391 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9392 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9394 if (l1 != l2)
9395 return l1 - l2;
9397 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9398 if (cmp != 0)
9399 return cmp;
9400 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9401 if (cmp != 0)
9402 return -cmp;
9404 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9407 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9408 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9409 * and 0 if the two constraints are the same (up to the constant term).
9411 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9412 isl_int *c1, isl_int *c2)
9414 unsigned total;
9416 if (!bmap)
9417 return -2;
9418 total = isl_basic_map_total_dim(bmap);
9419 return sort_constraint_cmp(&c1, &c2, &total);
9422 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9423 __isl_take isl_basic_map *bmap)
9425 unsigned total;
9427 if (!bmap)
9428 return NULL;
9429 if (bmap->n_ineq == 0)
9430 return bmap;
9431 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9432 return bmap;
9433 total = isl_basic_map_total_dim(bmap);
9434 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9435 &sort_constraint_cmp, &total) < 0)
9436 return isl_basic_map_free(bmap);
9437 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9438 return bmap;
9441 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9442 __isl_take isl_basic_set *bset)
9444 isl_basic_map *bmap = bset_to_bmap(bset);
9445 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9448 __isl_give isl_basic_map *isl_basic_map_normalize(
9449 __isl_take isl_basic_map *bmap)
9451 bmap = isl_basic_map_remove_redundancies(bmap);
9452 bmap = isl_basic_map_sort_constraints(bmap);
9453 return bmap;
9455 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9456 __isl_keep isl_basic_map *bmap2)
9458 int i, cmp;
9459 unsigned total;
9460 isl_space *space1, *space2;
9462 if (!bmap1 || !bmap2)
9463 return -1;
9465 if (bmap1 == bmap2)
9466 return 0;
9467 space1 = isl_basic_map_peek_space(bmap1);
9468 space2 = isl_basic_map_peek_space(bmap2);
9469 cmp = isl_space_cmp(space1, space2);
9470 if (cmp)
9471 return cmp;
9472 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9473 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9474 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9475 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9476 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9477 return 0;
9478 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9479 return 1;
9480 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9481 return -1;
9482 if (bmap1->n_eq != bmap2->n_eq)
9483 return bmap1->n_eq - bmap2->n_eq;
9484 if (bmap1->n_ineq != bmap2->n_ineq)
9485 return bmap1->n_ineq - bmap2->n_ineq;
9486 if (bmap1->n_div != bmap2->n_div)
9487 return bmap1->n_div - bmap2->n_div;
9488 total = isl_basic_map_total_dim(bmap1);
9489 for (i = 0; i < bmap1->n_eq; ++i) {
9490 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9491 if (cmp)
9492 return cmp;
9494 for (i = 0; i < bmap1->n_ineq; ++i) {
9495 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9496 if (cmp)
9497 return cmp;
9499 for (i = 0; i < bmap1->n_div; ++i) {
9500 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9501 if (cmp)
9502 return cmp;
9504 return 0;
9507 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9508 __isl_keep isl_basic_set *bset2)
9510 return isl_basic_map_plain_cmp(bset1, bset2);
9513 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9515 int i, cmp;
9517 if (set1 == set2)
9518 return 0;
9519 if (set1->n != set2->n)
9520 return set1->n - set2->n;
9522 for (i = 0; i < set1->n; ++i) {
9523 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9524 if (cmp)
9525 return cmp;
9528 return 0;
9531 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9532 __isl_keep isl_basic_map *bmap2)
9534 if (!bmap1 || !bmap2)
9535 return isl_bool_error;
9536 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9539 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9540 __isl_keep isl_basic_set *bset2)
9542 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9543 bset_to_bmap(bset2));
9546 static int qsort_bmap_cmp(const void *p1, const void *p2)
9548 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9549 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9551 return isl_basic_map_plain_cmp(bmap1, bmap2);
9554 /* Sort the basic maps of "map" and remove duplicate basic maps.
9556 * While removing basic maps, we make sure that the basic maps remain
9557 * sorted because isl_map_normalize expects the basic maps of the result
9558 * to be sorted.
9560 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9562 int i, j;
9564 map = isl_map_remove_empty_parts(map);
9565 if (!map)
9566 return NULL;
9567 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9568 for (i = map->n - 1; i >= 1; --i) {
9569 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9570 continue;
9571 isl_basic_map_free(map->p[i-1]);
9572 for (j = i; j < map->n; ++j)
9573 map->p[j - 1] = map->p[j];
9574 map->n--;
9577 return map;
9580 /* Remove obvious duplicates among the basic maps of "map".
9582 * Unlike isl_map_normalize, this function does not remove redundant
9583 * constraints and only removes duplicates that have exactly the same
9584 * constraints in the input. It does sort the constraints and
9585 * the basic maps to ease the detection of duplicates.
9587 * If "map" has already been normalized or if the basic maps are
9588 * disjoint, then there can be no duplicates.
9590 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9592 int i;
9593 isl_basic_map *bmap;
9595 if (!map)
9596 return NULL;
9597 if (map->n <= 1)
9598 return map;
9599 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9600 return map;
9601 for (i = 0; i < map->n; ++i) {
9602 bmap = isl_basic_map_copy(map->p[i]);
9603 bmap = isl_basic_map_sort_constraints(bmap);
9604 if (!bmap)
9605 return isl_map_free(map);
9606 isl_basic_map_free(map->p[i]);
9607 map->p[i] = bmap;
9610 map = sort_and_remove_duplicates(map);
9611 return map;
9614 /* We normalize in place, but if anything goes wrong we need
9615 * to return NULL, so we need to make sure we don't change the
9616 * meaning of any possible other copies of map.
9618 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9620 int i;
9621 struct isl_basic_map *bmap;
9623 if (!map)
9624 return NULL;
9625 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9626 return map;
9627 for (i = 0; i < map->n; ++i) {
9628 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9629 if (!bmap)
9630 goto error;
9631 isl_basic_map_free(map->p[i]);
9632 map->p[i] = bmap;
9635 map = sort_and_remove_duplicates(map);
9636 if (map)
9637 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9638 return map;
9639 error:
9640 isl_map_free(map);
9641 return NULL;
9644 struct isl_set *isl_set_normalize(struct isl_set *set)
9646 return set_from_map(isl_map_normalize(set_to_map(set)));
9649 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9650 __isl_keep isl_map *map2)
9652 int i;
9653 isl_bool equal;
9655 if (!map1 || !map2)
9656 return isl_bool_error;
9658 if (map1 == map2)
9659 return isl_bool_true;
9660 if (!isl_space_is_equal(map1->dim, map2->dim))
9661 return isl_bool_false;
9663 map1 = isl_map_copy(map1);
9664 map2 = isl_map_copy(map2);
9665 map1 = isl_map_normalize(map1);
9666 map2 = isl_map_normalize(map2);
9667 if (!map1 || !map2)
9668 goto error;
9669 equal = map1->n == map2->n;
9670 for (i = 0; equal && i < map1->n; ++i) {
9671 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9672 if (equal < 0)
9673 goto error;
9675 isl_map_free(map1);
9676 isl_map_free(map2);
9677 return equal;
9678 error:
9679 isl_map_free(map1);
9680 isl_map_free(map2);
9681 return isl_bool_error;
9684 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9685 __isl_keep isl_set *set2)
9687 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9690 /* Return the basic maps in "map" as a list.
9692 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9693 __isl_keep isl_map *map)
9695 int i;
9696 isl_ctx *ctx;
9697 isl_basic_map_list *list;
9699 if (!map)
9700 return NULL;
9701 ctx = isl_map_get_ctx(map);
9702 list = isl_basic_map_list_alloc(ctx, map->n);
9704 for (i = 0; i < map->n; ++i) {
9705 isl_basic_map *bmap;
9707 bmap = isl_basic_map_copy(map->p[i]);
9708 list = isl_basic_map_list_add(list, bmap);
9711 return list;
9714 /* Return the intersection of the elements in the non-empty list "list".
9715 * All elements are assumed to live in the same space.
9717 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9718 __isl_take isl_basic_map_list *list)
9720 int i, n;
9721 isl_basic_map *bmap;
9723 if (!list)
9724 return NULL;
9725 n = isl_basic_map_list_n_basic_map(list);
9726 if (n < 1)
9727 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9728 "expecting non-empty list", goto error);
9730 bmap = isl_basic_map_list_get_basic_map(list, 0);
9731 for (i = 1; i < n; ++i) {
9732 isl_basic_map *bmap_i;
9734 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9735 bmap = isl_basic_map_intersect(bmap, bmap_i);
9738 isl_basic_map_list_free(list);
9739 return bmap;
9740 error:
9741 isl_basic_map_list_free(list);
9742 return NULL;
9745 /* Return the intersection of the elements in the non-empty list "list".
9746 * All elements are assumed to live in the same space.
9748 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9749 __isl_take isl_basic_set_list *list)
9751 return isl_basic_map_list_intersect(list);
9754 /* Return the union of the elements of "list".
9755 * The list is required to have at least one element.
9757 __isl_give isl_set *isl_basic_set_list_union(
9758 __isl_take isl_basic_set_list *list)
9760 int i, n;
9761 isl_space *space;
9762 isl_basic_set *bset;
9763 isl_set *set;
9765 if (!list)
9766 return NULL;
9767 n = isl_basic_set_list_n_basic_set(list);
9768 if (n < 1)
9769 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9770 "expecting non-empty list", goto error);
9772 bset = isl_basic_set_list_get_basic_set(list, 0);
9773 space = isl_basic_set_get_space(bset);
9774 isl_basic_set_free(bset);
9776 set = isl_set_alloc_space(space, n, 0);
9777 for (i = 0; i < n; ++i) {
9778 bset = isl_basic_set_list_get_basic_set(list, i);
9779 set = isl_set_add_basic_set(set, bset);
9782 isl_basic_set_list_free(list);
9783 return set;
9784 error:
9785 isl_basic_set_list_free(list);
9786 return NULL;
9789 /* Return the union of the elements in the non-empty list "list".
9790 * All elements are assumed to live in the same space.
9792 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9794 int i, n;
9795 isl_set *set;
9797 if (!list)
9798 return NULL;
9799 n = isl_set_list_n_set(list);
9800 if (n < 1)
9801 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9802 "expecting non-empty list", goto error);
9804 set = isl_set_list_get_set(list, 0);
9805 for (i = 1; i < n; ++i) {
9806 isl_set *set_i;
9808 set_i = isl_set_list_get_set(list, i);
9809 set = isl_set_union(set, set_i);
9812 isl_set_list_free(list);
9813 return set;
9814 error:
9815 isl_set_list_free(list);
9816 return NULL;
9819 __isl_give isl_basic_map *isl_basic_map_product(
9820 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9822 isl_space *space_result = NULL;
9823 struct isl_basic_map *bmap;
9824 unsigned in1, in2, out1, out2, nparam, total, pos;
9825 struct isl_dim_map *dim_map1, *dim_map2;
9827 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9828 goto error;
9829 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9830 isl_space_copy(bmap2->dim));
9832 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9833 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9834 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9835 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9836 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9838 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9839 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9840 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9841 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9842 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9843 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9844 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9845 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9846 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9847 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9848 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9850 bmap = isl_basic_map_alloc_space(space_result,
9851 bmap1->n_div + bmap2->n_div,
9852 bmap1->n_eq + bmap2->n_eq,
9853 bmap1->n_ineq + bmap2->n_ineq);
9854 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9855 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9856 bmap = isl_basic_map_simplify(bmap);
9857 return isl_basic_map_finalize(bmap);
9858 error:
9859 isl_basic_map_free(bmap1);
9860 isl_basic_map_free(bmap2);
9861 return NULL;
9864 __isl_give isl_basic_map *isl_basic_map_flat_product(
9865 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9867 isl_basic_map *prod;
9869 prod = isl_basic_map_product(bmap1, bmap2);
9870 prod = isl_basic_map_flatten(prod);
9871 return prod;
9874 __isl_give isl_basic_set *isl_basic_set_flat_product(
9875 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9877 return isl_basic_map_flat_range_product(bset1, bset2);
9880 __isl_give isl_basic_map *isl_basic_map_domain_product(
9881 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9883 isl_space *space_result = NULL;
9884 isl_basic_map *bmap;
9885 unsigned in1, in2, out, nparam, total, pos;
9886 struct isl_dim_map *dim_map1, *dim_map2;
9888 if (!bmap1 || !bmap2)
9889 goto error;
9891 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9892 isl_space_copy(bmap2->dim));
9894 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9895 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9896 out = isl_basic_map_dim(bmap1, isl_dim_out);
9897 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9899 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9900 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9901 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9902 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9903 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9904 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9905 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9906 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9907 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9908 isl_dim_map_div(dim_map1, bmap1, pos += out);
9909 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9911 bmap = isl_basic_map_alloc_space(space_result,
9912 bmap1->n_div + bmap2->n_div,
9913 bmap1->n_eq + bmap2->n_eq,
9914 bmap1->n_ineq + bmap2->n_ineq);
9915 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9916 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9917 bmap = isl_basic_map_simplify(bmap);
9918 return isl_basic_map_finalize(bmap);
9919 error:
9920 isl_basic_map_free(bmap1);
9921 isl_basic_map_free(bmap2);
9922 return NULL;
9925 __isl_give isl_basic_map *isl_basic_map_range_product(
9926 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9928 isl_bool rational;
9929 isl_space *space_result = NULL;
9930 isl_basic_map *bmap;
9931 unsigned in, out1, out2, nparam, total, pos;
9932 struct isl_dim_map *dim_map1, *dim_map2;
9934 rational = isl_basic_map_is_rational(bmap1);
9935 if (rational >= 0 && rational)
9936 rational = isl_basic_map_is_rational(bmap2);
9937 if (!bmap1 || !bmap2 || rational < 0)
9938 goto error;
9940 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9941 goto error;
9943 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9944 isl_space_copy(bmap2->dim));
9946 in = isl_basic_map_dim(bmap1, isl_dim_in);
9947 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9948 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9949 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9951 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9952 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9953 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9954 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9955 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9956 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9957 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9958 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9959 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9960 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9961 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9963 bmap = isl_basic_map_alloc_space(space_result,
9964 bmap1->n_div + bmap2->n_div,
9965 bmap1->n_eq + bmap2->n_eq,
9966 bmap1->n_ineq + bmap2->n_ineq);
9967 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9968 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9969 if (rational)
9970 bmap = isl_basic_map_set_rational(bmap);
9971 bmap = isl_basic_map_simplify(bmap);
9972 return isl_basic_map_finalize(bmap);
9973 error:
9974 isl_basic_map_free(bmap1);
9975 isl_basic_map_free(bmap2);
9976 return NULL;
9979 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9980 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9982 isl_basic_map *prod;
9984 prod = isl_basic_map_range_product(bmap1, bmap2);
9985 prod = isl_basic_map_flatten_range(prod);
9986 return prod;
9989 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9990 * and collect the results.
9991 * The result live in the space obtained by calling "space_product"
9992 * on the spaces of "map1" and "map2".
9993 * If "remove_duplicates" is set then the result may contain duplicates
9994 * (even if the inputs do not) and so we try and remove the obvious
9995 * duplicates.
9997 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9998 __isl_take isl_map *map2,
9999 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10000 __isl_take isl_space *right),
10001 __isl_give isl_basic_map *(*basic_map_product)(
10002 __isl_take isl_basic_map *left,
10003 __isl_take isl_basic_map *right),
10004 int remove_duplicates)
10006 unsigned flags = 0;
10007 struct isl_map *result;
10008 int i, j;
10009 isl_bool m;
10011 m = isl_map_has_equal_params(map1, map2);
10012 if (m < 0)
10013 goto error;
10014 if (!m)
10015 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10016 "parameters don't match", goto error);
10018 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10019 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10020 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10022 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10023 isl_space_copy(map2->dim)),
10024 map1->n * map2->n, flags);
10025 if (!result)
10026 goto error;
10027 for (i = 0; i < map1->n; ++i)
10028 for (j = 0; j < map2->n; ++j) {
10029 struct isl_basic_map *part;
10030 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10031 isl_basic_map_copy(map2->p[j]));
10032 if (isl_basic_map_is_empty(part))
10033 isl_basic_map_free(part);
10034 else
10035 result = isl_map_add_basic_map(result, part);
10036 if (!result)
10037 goto error;
10039 if (remove_duplicates)
10040 result = isl_map_remove_obvious_duplicates(result);
10041 isl_map_free(map1);
10042 isl_map_free(map2);
10043 return result;
10044 error:
10045 isl_map_free(map1);
10046 isl_map_free(map2);
10047 return NULL;
10050 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10052 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10053 __isl_take isl_map *map2)
10055 return map_product(map1, map2, &isl_space_product,
10056 &isl_basic_map_product, 0);
10059 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10060 __isl_take isl_map *map2)
10062 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10065 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10067 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10068 __isl_take isl_map *map2)
10070 isl_map *prod;
10072 prod = isl_map_product(map1, map2);
10073 prod = isl_map_flatten(prod);
10074 return prod;
10077 /* Given two set A and B, construct its Cartesian product A x B.
10079 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10081 return isl_map_range_product(set1, set2);
10084 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10085 __isl_take isl_set *set2)
10087 return isl_map_flat_range_product(set1, set2);
10090 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10092 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10093 __isl_take isl_map *map2)
10095 return map_product(map1, map2, &isl_space_domain_product,
10096 &isl_basic_map_domain_product, 1);
10099 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10101 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10102 __isl_take isl_map *map2)
10104 return map_product(map1, map2, &isl_space_range_product,
10105 &isl_basic_map_range_product, 1);
10108 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10109 __isl_take isl_map *map2)
10111 return isl_map_align_params_map_map_and(map1, map2,
10112 &map_domain_product_aligned);
10115 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10116 __isl_take isl_map *map2)
10118 return isl_map_align_params_map_map_and(map1, map2,
10119 &map_range_product_aligned);
10122 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10124 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10126 isl_space *space;
10127 int total1, keep1, total2, keep2;
10129 if (!map)
10130 return NULL;
10131 if (!isl_space_domain_is_wrapping(map->dim) ||
10132 !isl_space_range_is_wrapping(map->dim))
10133 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10134 "not a product", return isl_map_free(map));
10136 space = isl_map_get_space(map);
10137 total1 = isl_space_dim(space, isl_dim_in);
10138 total2 = isl_space_dim(space, isl_dim_out);
10139 space = isl_space_factor_domain(space);
10140 keep1 = isl_space_dim(space, isl_dim_in);
10141 keep2 = isl_space_dim(space, isl_dim_out);
10142 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10143 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10144 map = isl_map_reset_space(map, space);
10146 return map;
10149 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10151 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10153 isl_space *space;
10154 int total1, keep1, total2, keep2;
10156 if (!map)
10157 return NULL;
10158 if (!isl_space_domain_is_wrapping(map->dim) ||
10159 !isl_space_range_is_wrapping(map->dim))
10160 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10161 "not a product", return isl_map_free(map));
10163 space = isl_map_get_space(map);
10164 total1 = isl_space_dim(space, isl_dim_in);
10165 total2 = isl_space_dim(space, isl_dim_out);
10166 space = isl_space_factor_range(space);
10167 keep1 = isl_space_dim(space, isl_dim_in);
10168 keep2 = isl_space_dim(space, isl_dim_out);
10169 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10170 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10171 map = isl_map_reset_space(map, space);
10173 return map;
10176 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10178 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10180 isl_space *space;
10181 int total, keep;
10183 if (!map)
10184 return NULL;
10185 if (!isl_space_domain_is_wrapping(map->dim))
10186 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10187 "domain is not a product", return isl_map_free(map));
10189 space = isl_map_get_space(map);
10190 total = isl_space_dim(space, isl_dim_in);
10191 space = isl_space_domain_factor_domain(space);
10192 keep = isl_space_dim(space, isl_dim_in);
10193 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10194 map = isl_map_reset_space(map, space);
10196 return map;
10199 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10201 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10203 isl_space *space;
10204 int total, keep;
10206 if (!map)
10207 return NULL;
10208 if (!isl_space_domain_is_wrapping(map->dim))
10209 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10210 "domain is not a product", return isl_map_free(map));
10212 space = isl_map_get_space(map);
10213 total = isl_space_dim(space, isl_dim_in);
10214 space = isl_space_domain_factor_range(space);
10215 keep = isl_space_dim(space, isl_dim_in);
10216 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10217 map = isl_map_reset_space(map, space);
10219 return map;
10222 /* Given a map A -> [B -> C], extract the map A -> B.
10224 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10226 isl_space *space;
10227 int total, keep;
10229 if (!map)
10230 return NULL;
10231 if (!isl_space_range_is_wrapping(map->dim))
10232 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10233 "range is not a product", return isl_map_free(map));
10235 space = isl_map_get_space(map);
10236 total = isl_space_dim(space, isl_dim_out);
10237 space = isl_space_range_factor_domain(space);
10238 keep = isl_space_dim(space, isl_dim_out);
10239 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10240 map = isl_map_reset_space(map, space);
10242 return map;
10245 /* Given a map A -> [B -> C], extract the map A -> C.
10247 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10249 isl_space *space;
10250 int total, keep;
10252 if (!map)
10253 return NULL;
10254 if (!isl_space_range_is_wrapping(map->dim))
10255 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10256 "range is not a product", return isl_map_free(map));
10258 space = isl_map_get_space(map);
10259 total = isl_space_dim(space, isl_dim_out);
10260 space = isl_space_range_factor_range(space);
10261 keep = isl_space_dim(space, isl_dim_out);
10262 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10263 map = isl_map_reset_space(map, space);
10265 return map;
10268 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10270 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10271 __isl_take isl_map *map2)
10273 isl_map *prod;
10275 prod = isl_map_domain_product(map1, map2);
10276 prod = isl_map_flatten_domain(prod);
10277 return prod;
10280 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10282 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10283 __isl_take isl_map *map2)
10285 isl_map *prod;
10287 prod = isl_map_range_product(map1, map2);
10288 prod = isl_map_flatten_range(prod);
10289 return prod;
10292 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10294 int i;
10295 uint32_t hash = isl_hash_init();
10296 unsigned total;
10298 if (!bmap)
10299 return 0;
10300 bmap = isl_basic_map_copy(bmap);
10301 bmap = isl_basic_map_normalize(bmap);
10302 if (!bmap)
10303 return 0;
10304 total = isl_basic_map_total_dim(bmap);
10305 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10306 for (i = 0; i < bmap->n_eq; ++i) {
10307 uint32_t c_hash;
10308 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10309 isl_hash_hash(hash, c_hash);
10311 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10312 for (i = 0; i < bmap->n_ineq; ++i) {
10313 uint32_t c_hash;
10314 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10315 isl_hash_hash(hash, c_hash);
10317 isl_hash_byte(hash, bmap->n_div & 0xFF);
10318 for (i = 0; i < bmap->n_div; ++i) {
10319 uint32_t c_hash;
10320 if (isl_int_is_zero(bmap->div[i][0]))
10321 continue;
10322 isl_hash_byte(hash, i & 0xFF);
10323 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10324 isl_hash_hash(hash, c_hash);
10326 isl_basic_map_free(bmap);
10327 return hash;
10330 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10332 return isl_basic_map_get_hash(bset_to_bmap(bset));
10335 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10337 int i;
10338 uint32_t hash;
10340 if (!map)
10341 return 0;
10342 map = isl_map_copy(map);
10343 map = isl_map_normalize(map);
10344 if (!map)
10345 return 0;
10347 hash = isl_hash_init();
10348 for (i = 0; i < map->n; ++i) {
10349 uint32_t bmap_hash;
10350 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10351 isl_hash_hash(hash, bmap_hash);
10354 isl_map_free(map);
10356 return hash;
10359 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10361 return isl_map_get_hash(set_to_map(set));
10364 /* Return the number of basic maps in the (current) representation of "map".
10366 int isl_map_n_basic_map(__isl_keep isl_map *map)
10368 return map ? map->n : 0;
10371 int isl_set_n_basic_set(__isl_keep isl_set *set)
10373 return set ? set->n : 0;
10376 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10377 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10379 int i;
10381 if (!map)
10382 return isl_stat_error;
10384 for (i = 0; i < map->n; ++i)
10385 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10386 return isl_stat_error;
10388 return isl_stat_ok;
10391 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10392 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10394 int i;
10396 if (!set)
10397 return isl_stat_error;
10399 for (i = 0; i < set->n; ++i)
10400 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10401 return isl_stat_error;
10403 return isl_stat_ok;
10406 /* Return a list of basic sets, the union of which is equal to "set".
10408 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10409 __isl_keep isl_set *set)
10411 int i;
10412 isl_basic_set_list *list;
10414 if (!set)
10415 return NULL;
10417 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10418 for (i = 0; i < set->n; ++i) {
10419 isl_basic_set *bset;
10421 bset = isl_basic_set_copy(set->p[i]);
10422 list = isl_basic_set_list_add(list, bset);
10425 return list;
10428 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10430 isl_space *space;
10432 if (!bset)
10433 return NULL;
10435 bset = isl_basic_set_cow(bset);
10436 if (!bset)
10437 return NULL;
10439 space = isl_basic_set_get_space(bset);
10440 space = isl_space_lift(space, bset->n_div);
10441 if (!space)
10442 goto error;
10443 isl_space_free(bset->dim);
10444 bset->dim = space;
10445 bset->extra -= bset->n_div;
10446 bset->n_div = 0;
10448 bset = isl_basic_set_finalize(bset);
10450 return bset;
10451 error:
10452 isl_basic_set_free(bset);
10453 return NULL;
10456 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10458 int i;
10459 isl_space *space;
10460 unsigned n_div;
10462 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10464 if (!set)
10465 return NULL;
10467 set = isl_set_cow(set);
10468 if (!set)
10469 return NULL;
10471 n_div = set->p[0]->n_div;
10472 space = isl_set_get_space(set);
10473 space = isl_space_lift(space, n_div);
10474 if (!space)
10475 goto error;
10476 isl_space_free(set->dim);
10477 set->dim = space;
10479 for (i = 0; i < set->n; ++i) {
10480 set->p[i] = isl_basic_set_lift(set->p[i]);
10481 if (!set->p[i])
10482 goto error;
10485 return set;
10486 error:
10487 isl_set_free(set);
10488 return NULL;
10491 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10493 unsigned dim;
10494 int size = 0;
10496 if (!bset)
10497 return -1;
10499 dim = isl_basic_set_total_dim(bset);
10500 size += bset->n_eq * (1 + dim);
10501 size += bset->n_ineq * (1 + dim);
10502 size += bset->n_div * (2 + dim);
10504 return size;
10507 int isl_set_size(__isl_keep isl_set *set)
10509 int i;
10510 int size = 0;
10512 if (!set)
10513 return -1;
10515 for (i = 0; i < set->n; ++i)
10516 size += isl_basic_set_size(set->p[i]);
10518 return size;
10521 /* Check if there is any lower bound (if lower == 0) and/or upper
10522 * bound (if upper == 0) on the specified dim.
10524 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10525 enum isl_dim_type type, unsigned pos, int lower, int upper)
10527 int i;
10529 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10530 return isl_bool_error;
10532 pos += isl_basic_map_offset(bmap, type);
10534 for (i = 0; i < bmap->n_div; ++i) {
10535 if (isl_int_is_zero(bmap->div[i][0]))
10536 continue;
10537 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10538 return isl_bool_true;
10541 for (i = 0; i < bmap->n_eq; ++i)
10542 if (!isl_int_is_zero(bmap->eq[i][pos]))
10543 return isl_bool_true;
10545 for (i = 0; i < bmap->n_ineq; ++i) {
10546 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10547 if (sgn > 0)
10548 lower = 1;
10549 if (sgn < 0)
10550 upper = 1;
10553 return lower && upper;
10556 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10557 enum isl_dim_type type, unsigned pos)
10559 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10562 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10563 enum isl_dim_type type, unsigned pos)
10565 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10568 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10569 enum isl_dim_type type, unsigned pos)
10571 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10574 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10575 enum isl_dim_type type, unsigned pos)
10577 int i;
10579 if (!map)
10580 return isl_bool_error;
10582 for (i = 0; i < map->n; ++i) {
10583 isl_bool bounded;
10584 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10585 if (bounded < 0 || !bounded)
10586 return bounded;
10589 return isl_bool_true;
10592 /* Return true if the specified dim is involved in both an upper bound
10593 * and a lower bound.
10595 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10596 enum isl_dim_type type, unsigned pos)
10598 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10601 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10603 static isl_bool has_any_bound(__isl_keep isl_map *map,
10604 enum isl_dim_type type, unsigned pos,
10605 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10606 enum isl_dim_type type, unsigned pos))
10608 int i;
10610 if (!map)
10611 return isl_bool_error;
10613 for (i = 0; i < map->n; ++i) {
10614 isl_bool bounded;
10615 bounded = fn(map->p[i], type, pos);
10616 if (bounded < 0 || bounded)
10617 return bounded;
10620 return isl_bool_false;
10623 /* Return 1 if the specified dim is involved in any lower bound.
10625 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10626 enum isl_dim_type type, unsigned pos)
10628 return has_any_bound(set, type, pos,
10629 &isl_basic_map_dim_has_lower_bound);
10632 /* Return 1 if the specified dim is involved in any upper bound.
10634 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10635 enum isl_dim_type type, unsigned pos)
10637 return has_any_bound(set, type, pos,
10638 &isl_basic_map_dim_has_upper_bound);
10641 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10643 static isl_bool has_bound(__isl_keep isl_map *map,
10644 enum isl_dim_type type, unsigned pos,
10645 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10646 enum isl_dim_type type, unsigned pos))
10648 int i;
10650 if (!map)
10651 return isl_bool_error;
10653 for (i = 0; i < map->n; ++i) {
10654 isl_bool bounded;
10655 bounded = fn(map->p[i], type, pos);
10656 if (bounded < 0 || !bounded)
10657 return bounded;
10660 return isl_bool_true;
10663 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10665 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10666 enum isl_dim_type type, unsigned pos)
10668 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10671 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10673 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10674 enum isl_dim_type type, unsigned pos)
10676 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10679 /* For each of the "n" variables starting at "first", determine
10680 * the sign of the variable and put the results in the first "n"
10681 * elements of the array "signs".
10682 * Sign
10683 * 1 means that the variable is non-negative
10684 * -1 means that the variable is non-positive
10685 * 0 means the variable attains both positive and negative values.
10687 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10688 unsigned first, unsigned n, int *signs)
10690 isl_vec *bound = NULL;
10691 struct isl_tab *tab = NULL;
10692 struct isl_tab_undo *snap;
10693 int i;
10695 if (!bset || !signs)
10696 return isl_stat_error;
10698 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10699 tab = isl_tab_from_basic_set(bset, 0);
10700 if (!bound || !tab)
10701 goto error;
10703 isl_seq_clr(bound->el, bound->size);
10704 isl_int_set_si(bound->el[0], -1);
10706 snap = isl_tab_snap(tab);
10707 for (i = 0; i < n; ++i) {
10708 int empty;
10710 isl_int_set_si(bound->el[1 + first + i], -1);
10711 if (isl_tab_add_ineq(tab, bound->el) < 0)
10712 goto error;
10713 empty = tab->empty;
10714 isl_int_set_si(bound->el[1 + first + i], 0);
10715 if (isl_tab_rollback(tab, snap) < 0)
10716 goto error;
10718 if (empty) {
10719 signs[i] = 1;
10720 continue;
10723 isl_int_set_si(bound->el[1 + first + i], 1);
10724 if (isl_tab_add_ineq(tab, bound->el) < 0)
10725 goto error;
10726 empty = tab->empty;
10727 isl_int_set_si(bound->el[1 + first + i], 0);
10728 if (isl_tab_rollback(tab, snap) < 0)
10729 goto error;
10731 signs[i] = empty ? -1 : 0;
10734 isl_tab_free(tab);
10735 isl_vec_free(bound);
10736 return isl_stat_ok;
10737 error:
10738 isl_tab_free(tab);
10739 isl_vec_free(bound);
10740 return isl_stat_error;
10743 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10744 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10746 if (!bset || !signs)
10747 return isl_stat_error;
10748 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10749 return isl_stat_error;
10751 first += pos(bset->dim, type) - 1;
10752 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10755 /* Is it possible for the integer division "div" to depend (possibly
10756 * indirectly) on any output dimensions?
10758 * If the div is undefined, then we conservatively assume that it
10759 * may depend on them.
10760 * Otherwise, we check if it actually depends on them or on any integer
10761 * divisions that may depend on them.
10763 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10765 int i;
10766 unsigned n_out, o_out;
10767 unsigned n_div, o_div;
10769 if (isl_int_is_zero(bmap->div[div][0]))
10770 return isl_bool_true;
10772 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10773 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10775 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10776 return isl_bool_true;
10778 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10779 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10781 for (i = 0; i < n_div; ++i) {
10782 isl_bool may_involve;
10784 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10785 continue;
10786 may_involve = div_may_involve_output(bmap, i);
10787 if (may_involve < 0 || may_involve)
10788 return may_involve;
10791 return isl_bool_false;
10794 /* Return the first integer division of "bmap" in the range
10795 * [first, first + n[ that may depend on any output dimensions and
10796 * that has a non-zero coefficient in "c" (where the first coefficient
10797 * in "c" corresponds to integer division "first").
10799 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10800 isl_int *c, int first, int n)
10802 int k;
10804 if (!bmap)
10805 return -1;
10807 for (k = first; k < first + n; ++k) {
10808 isl_bool may_involve;
10810 if (isl_int_is_zero(c[k]))
10811 continue;
10812 may_involve = div_may_involve_output(bmap, k);
10813 if (may_involve < 0)
10814 return -1;
10815 if (may_involve)
10816 return k;
10819 return first + n;
10822 /* Look for a pair of inequality constraints in "bmap" of the form
10824 * -l + i >= 0 or i >= l
10825 * and
10826 * n + l - i >= 0 or i <= l + n
10828 * with n < "m" and i the output dimension at position "pos".
10829 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10830 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10831 * and earlier output dimensions, as well as integer divisions that do
10832 * not involve any of the output dimensions.
10834 * Return the index of the first inequality constraint or bmap->n_ineq
10835 * if no such pair can be found.
10837 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10838 int pos, isl_int m)
10840 int i, j;
10841 isl_ctx *ctx;
10842 unsigned total;
10843 unsigned n_div, o_div;
10844 unsigned n_out, o_out;
10845 int less;
10847 if (!bmap)
10848 return -1;
10850 ctx = isl_basic_map_get_ctx(bmap);
10851 total = isl_basic_map_total_dim(bmap);
10852 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10853 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10854 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10855 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10856 for (i = 0; i < bmap->n_ineq; ++i) {
10857 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10858 continue;
10859 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10860 n_out - (pos + 1)) != -1)
10861 continue;
10862 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10863 0, n_div) < n_div)
10864 continue;
10865 for (j = i + 1; j < bmap->n_ineq; ++j) {
10866 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10867 ctx->one))
10868 continue;
10869 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10870 bmap->ineq[j] + 1, total))
10871 continue;
10872 break;
10874 if (j >= bmap->n_ineq)
10875 continue;
10876 isl_int_add(bmap->ineq[i][0],
10877 bmap->ineq[i][0], bmap->ineq[j][0]);
10878 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10879 isl_int_sub(bmap->ineq[i][0],
10880 bmap->ineq[i][0], bmap->ineq[j][0]);
10881 if (!less)
10882 continue;
10883 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10884 return i;
10885 else
10886 return j;
10889 return bmap->n_ineq;
10892 /* Return the index of the equality of "bmap" that defines
10893 * the output dimension "pos" in terms of earlier dimensions.
10894 * The equality may also involve integer divisions, as long
10895 * as those integer divisions are defined in terms of
10896 * parameters or input dimensions.
10897 * In this case, *div is set to the number of integer divisions and
10898 * *ineq is set to the number of inequality constraints (provided
10899 * div and ineq are not NULL).
10901 * The equality may also involve a single integer division involving
10902 * the output dimensions (typically only output dimension "pos") as
10903 * long as the coefficient of output dimension "pos" is 1 or -1 and
10904 * there is a pair of constraints i >= l and i <= l + n, with i referring
10905 * to output dimension "pos", l an expression involving only earlier
10906 * dimensions and n smaller than the coefficient of the integer division
10907 * in the equality. In this case, the output dimension can be defined
10908 * in terms of a modulo expression that does not involve the integer division.
10909 * *div is then set to this single integer division and
10910 * *ineq is set to the index of constraint i >= l.
10912 * Return bmap->n_eq if there is no such equality.
10913 * Return -1 on error.
10915 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10916 int pos, int *div, int *ineq)
10918 int j, k, l;
10919 unsigned n_out, o_out;
10920 unsigned n_div, o_div;
10922 if (!bmap)
10923 return -1;
10925 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10926 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10927 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10928 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10930 if (ineq)
10931 *ineq = bmap->n_ineq;
10932 if (div)
10933 *div = n_div;
10934 for (j = 0; j < bmap->n_eq; ++j) {
10935 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10936 continue;
10937 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10938 n_out - (pos + 1)) != -1)
10939 continue;
10940 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10941 0, n_div);
10942 if (k >= n_div)
10943 return j;
10944 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10945 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10946 continue;
10947 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10948 k + 1, n_div - (k+1)) < n_div)
10949 continue;
10950 l = find_modulo_constraint_pair(bmap, pos,
10951 bmap->eq[j][o_div + k]);
10952 if (l < 0)
10953 return -1;
10954 if (l >= bmap->n_ineq)
10955 continue;
10956 if (div)
10957 *div = k;
10958 if (ineq)
10959 *ineq = l;
10960 return j;
10963 return bmap->n_eq;
10966 /* Check if the given basic map is obviously single-valued.
10967 * In particular, for each output dimension, check that there is
10968 * an equality that defines the output dimension in terms of
10969 * earlier dimensions.
10971 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10973 int i;
10974 unsigned n_out;
10976 if (!bmap)
10977 return isl_bool_error;
10979 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10981 for (i = 0; i < n_out; ++i) {
10982 int eq;
10984 eq = isl_basic_map_output_defining_equality(bmap, i,
10985 NULL, NULL);
10986 if (eq < 0)
10987 return isl_bool_error;
10988 if (eq >= bmap->n_eq)
10989 return isl_bool_false;
10992 return isl_bool_true;
10995 /* Check if the given basic map is single-valued.
10996 * We simply compute
10998 * M \circ M^-1
11000 * and check if the result is a subset of the identity mapping.
11002 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11004 isl_space *space;
11005 isl_basic_map *test;
11006 isl_basic_map *id;
11007 isl_bool sv;
11009 sv = isl_basic_map_plain_is_single_valued(bmap);
11010 if (sv < 0 || sv)
11011 return sv;
11013 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11014 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11016 space = isl_basic_map_get_space(bmap);
11017 space = isl_space_map_from_set(isl_space_range(space));
11018 id = isl_basic_map_identity(space);
11020 sv = isl_basic_map_is_subset(test, id);
11022 isl_basic_map_free(test);
11023 isl_basic_map_free(id);
11025 return sv;
11028 /* Check if the given map is obviously single-valued.
11030 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11032 if (!map)
11033 return isl_bool_error;
11034 if (map->n == 0)
11035 return isl_bool_true;
11036 if (map->n >= 2)
11037 return isl_bool_false;
11039 return isl_basic_map_plain_is_single_valued(map->p[0]);
11042 /* Check if the given map is single-valued.
11043 * We simply compute
11045 * M \circ M^-1
11047 * and check if the result is a subset of the identity mapping.
11049 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11051 isl_space *dim;
11052 isl_map *test;
11053 isl_map *id;
11054 isl_bool sv;
11056 sv = isl_map_plain_is_single_valued(map);
11057 if (sv < 0 || sv)
11058 return sv;
11060 test = isl_map_reverse(isl_map_copy(map));
11061 test = isl_map_apply_range(test, isl_map_copy(map));
11063 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11064 id = isl_map_identity(dim);
11066 sv = isl_map_is_subset(test, id);
11068 isl_map_free(test);
11069 isl_map_free(id);
11071 return sv;
11074 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11076 isl_bool in;
11078 map = isl_map_copy(map);
11079 map = isl_map_reverse(map);
11080 in = isl_map_is_single_valued(map);
11081 isl_map_free(map);
11083 return in;
11086 /* Check if the given map is obviously injective.
11088 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11090 isl_bool in;
11092 map = isl_map_copy(map);
11093 map = isl_map_reverse(map);
11094 in = isl_map_plain_is_single_valued(map);
11095 isl_map_free(map);
11097 return in;
11100 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11102 isl_bool sv;
11104 sv = isl_map_is_single_valued(map);
11105 if (sv < 0 || !sv)
11106 return sv;
11108 return isl_map_is_injective(map);
11111 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11113 return isl_map_is_single_valued(set_to_map(set));
11116 /* Does "map" only map elements to themselves?
11118 * If the domain and range spaces are different, then "map"
11119 * is considered not to be an identity relation, even if it is empty.
11120 * Otherwise, construct the maximal identity relation and
11121 * check whether "map" is a subset of this relation.
11123 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11125 isl_space *space;
11126 isl_map *id;
11127 isl_bool equal, is_identity;
11129 space = isl_map_get_space(map);
11130 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11131 isl_space_free(space);
11132 if (equal < 0 || !equal)
11133 return equal;
11135 id = isl_map_identity(isl_map_get_space(map));
11136 is_identity = isl_map_is_subset(map, id);
11137 isl_map_free(id);
11139 return is_identity;
11142 int isl_map_is_translation(__isl_keep isl_map *map)
11144 int ok;
11145 isl_set *delta;
11147 delta = isl_map_deltas(isl_map_copy(map));
11148 ok = isl_set_is_singleton(delta);
11149 isl_set_free(delta);
11151 return ok;
11154 static int unique(isl_int *p, unsigned pos, unsigned len)
11156 if (isl_seq_first_non_zero(p, pos) != -1)
11157 return 0;
11158 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11159 return 0;
11160 return 1;
11163 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11165 int i, j;
11166 unsigned nvar;
11167 unsigned ovar;
11169 if (!bset)
11170 return isl_bool_error;
11172 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11173 return isl_bool_false;
11175 nvar = isl_basic_set_dim(bset, isl_dim_set);
11176 ovar = isl_space_offset(bset->dim, isl_dim_set);
11177 for (j = 0; j < nvar; ++j) {
11178 int lower = 0, upper = 0;
11179 for (i = 0; i < bset->n_eq; ++i) {
11180 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11181 continue;
11182 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11183 return isl_bool_false;
11184 break;
11186 if (i < bset->n_eq)
11187 continue;
11188 for (i = 0; i < bset->n_ineq; ++i) {
11189 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11190 continue;
11191 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11192 return isl_bool_false;
11193 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11194 lower = 1;
11195 else
11196 upper = 1;
11198 if (!lower || !upper)
11199 return isl_bool_false;
11202 return isl_bool_true;
11205 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11207 if (!set)
11208 return isl_bool_error;
11209 if (set->n != 1)
11210 return isl_bool_false;
11212 return isl_basic_set_is_box(set->p[0]);
11215 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11217 if (!bset)
11218 return isl_bool_error;
11220 return isl_space_is_wrapping(bset->dim);
11223 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11225 if (!set)
11226 return isl_bool_error;
11228 return isl_space_is_wrapping(set->dim);
11231 /* Modify the space of "map" through a call to "change".
11232 * If "can_change" is set (not NULL), then first call it to check
11233 * if the modification is allowed, printing the error message "cannot_change"
11234 * if it is not.
11236 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11237 isl_bool (*can_change)(__isl_keep isl_map *map),
11238 const char *cannot_change,
11239 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11241 isl_bool ok;
11242 isl_space *space;
11244 if (!map)
11245 return NULL;
11247 ok = can_change ? can_change(map) : isl_bool_true;
11248 if (ok < 0)
11249 return isl_map_free(map);
11250 if (!ok)
11251 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11252 return isl_map_free(map));
11254 space = change(isl_map_get_space(map));
11255 map = isl_map_reset_space(map, space);
11257 return map;
11260 /* Is the domain of "map" a wrapped relation?
11262 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11264 if (!map)
11265 return isl_bool_error;
11267 return isl_space_domain_is_wrapping(map->dim);
11270 /* Does "map" have a wrapped relation in both domain and range?
11272 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11274 return isl_space_is_product(isl_map_peek_space(map));
11277 /* Is the range of "map" a wrapped relation?
11279 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11281 if (!map)
11282 return isl_bool_error;
11284 return isl_space_range_is_wrapping(map->dim);
11287 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11289 bmap = isl_basic_map_cow(bmap);
11290 if (!bmap)
11291 return NULL;
11293 bmap->dim = isl_space_wrap(bmap->dim);
11294 if (!bmap->dim)
11295 goto error;
11297 bmap = isl_basic_map_finalize(bmap);
11299 return bset_from_bmap(bmap);
11300 error:
11301 isl_basic_map_free(bmap);
11302 return NULL;
11305 /* Given a map A -> B, return the set (A -> B).
11307 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11309 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11312 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11314 bset = isl_basic_set_cow(bset);
11315 if (!bset)
11316 return NULL;
11318 bset->dim = isl_space_unwrap(bset->dim);
11319 if (!bset->dim)
11320 goto error;
11322 bset = isl_basic_set_finalize(bset);
11324 return bset_to_bmap(bset);
11325 error:
11326 isl_basic_set_free(bset);
11327 return NULL;
11330 /* Given a set (A -> B), return the map A -> B.
11331 * Error out if "set" is not of the form (A -> B).
11333 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11335 return isl_map_change_space(set, &isl_set_is_wrapping,
11336 "not a wrapping set", &isl_space_unwrap);
11339 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11340 enum isl_dim_type type)
11342 if (!bmap)
11343 return NULL;
11345 if (!isl_space_is_named_or_nested(bmap->dim, type))
11346 return bmap;
11348 bmap = isl_basic_map_cow(bmap);
11349 if (!bmap)
11350 return NULL;
11352 bmap->dim = isl_space_reset(bmap->dim, type);
11353 if (!bmap->dim)
11354 goto error;
11356 bmap = isl_basic_map_finalize(bmap);
11358 return bmap;
11359 error:
11360 isl_basic_map_free(bmap);
11361 return NULL;
11364 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11365 enum isl_dim_type type)
11367 int i;
11369 if (!map)
11370 return NULL;
11372 if (!isl_space_is_named_or_nested(map->dim, type))
11373 return map;
11375 map = isl_map_cow(map);
11376 if (!map)
11377 return NULL;
11379 for (i = 0; i < map->n; ++i) {
11380 map->p[i] = isl_basic_map_reset(map->p[i], type);
11381 if (!map->p[i])
11382 goto error;
11384 map->dim = isl_space_reset(map->dim, type);
11385 if (!map->dim)
11386 goto error;
11388 return map;
11389 error:
11390 isl_map_free(map);
11391 return NULL;
11394 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11396 if (!bmap)
11397 return NULL;
11399 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11400 return bmap;
11402 bmap = isl_basic_map_cow(bmap);
11403 if (!bmap)
11404 return NULL;
11406 bmap->dim = isl_space_flatten(bmap->dim);
11407 if (!bmap->dim)
11408 goto error;
11410 bmap = isl_basic_map_finalize(bmap);
11412 return bmap;
11413 error:
11414 isl_basic_map_free(bmap);
11415 return NULL;
11418 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11420 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11423 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11424 __isl_take isl_basic_map *bmap)
11426 if (!bmap)
11427 return NULL;
11429 if (!bmap->dim->nested[0])
11430 return bmap;
11432 bmap = isl_basic_map_cow(bmap);
11433 if (!bmap)
11434 return NULL;
11436 bmap->dim = isl_space_flatten_domain(bmap->dim);
11437 if (!bmap->dim)
11438 goto error;
11440 bmap = isl_basic_map_finalize(bmap);
11442 return bmap;
11443 error:
11444 isl_basic_map_free(bmap);
11445 return NULL;
11448 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11449 __isl_take isl_basic_map *bmap)
11451 if (!bmap)
11452 return NULL;
11454 if (!bmap->dim->nested[1])
11455 return bmap;
11457 bmap = isl_basic_map_cow(bmap);
11458 if (!bmap)
11459 return NULL;
11461 bmap->dim = isl_space_flatten_range(bmap->dim);
11462 if (!bmap->dim)
11463 goto error;
11465 bmap = isl_basic_map_finalize(bmap);
11467 return bmap;
11468 error:
11469 isl_basic_map_free(bmap);
11470 return NULL;
11473 /* Remove any internal structure from the spaces of domain and range of "map".
11475 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11477 if (!map)
11478 return NULL;
11480 if (!map->dim->nested[0] && !map->dim->nested[1])
11481 return map;
11483 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11486 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11488 return set_from_map(isl_map_flatten(set_to_map(set)));
11491 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11493 isl_space *space, *flat_space;
11494 isl_map *map;
11496 space = isl_set_get_space(set);
11497 flat_space = isl_space_flatten(isl_space_copy(space));
11498 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11499 flat_space));
11500 map = isl_map_intersect_domain(map, set);
11502 return map;
11505 /* Remove any internal structure from the space of the domain of "map".
11507 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11509 if (!map)
11510 return NULL;
11512 if (!map->dim->nested[0])
11513 return map;
11515 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11518 /* Remove any internal structure from the space of the range of "map".
11520 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11522 if (!map)
11523 return NULL;
11525 if (!map->dim->nested[1])
11526 return map;
11528 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11531 /* Reorder the dimensions of "bmap" according to the given dim_map
11532 * and set the dimension specification to "space" and
11533 * perform Gaussian elimination on the result.
11535 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11536 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11538 isl_basic_map *res;
11539 unsigned flags;
11540 unsigned n_div;
11542 if (!bmap || !space || !dim_map)
11543 goto error;
11545 flags = bmap->flags;
11546 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11547 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11548 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11549 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11550 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11551 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11552 if (res)
11553 res->flags = flags;
11554 res = isl_basic_map_gauss(res, NULL);
11555 res = isl_basic_map_finalize(res);
11556 return res;
11557 error:
11558 isl_dim_map_free(dim_map);
11559 isl_basic_map_free(bmap);
11560 isl_space_free(space);
11561 return NULL;
11564 /* Reorder the dimensions of "map" according to given reordering.
11566 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11567 __isl_take isl_reordering *r)
11569 int i;
11570 struct isl_dim_map *dim_map;
11572 map = isl_map_cow(map);
11573 dim_map = isl_dim_map_from_reordering(r);
11574 if (!map || !r || !dim_map)
11575 goto error;
11577 for (i = 0; i < map->n; ++i) {
11578 struct isl_dim_map *dim_map_i;
11579 isl_space *space;
11581 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11583 space = isl_reordering_get_space(r);
11584 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11586 if (!map->p[i])
11587 goto error;
11590 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11591 map = isl_map_unmark_normalized(map);
11593 isl_reordering_free(r);
11594 isl_dim_map_free(dim_map);
11595 return map;
11596 error:
11597 isl_dim_map_free(dim_map);
11598 isl_map_free(map);
11599 isl_reordering_free(r);
11600 return NULL;
11603 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11604 __isl_take isl_reordering *r)
11606 return set_from_map(isl_map_realign(set_to_map(set), r));
11609 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11610 __isl_take isl_space *model)
11612 isl_ctx *ctx;
11613 isl_bool aligned;
11615 if (!map || !model)
11616 goto error;
11618 ctx = isl_space_get_ctx(model);
11619 if (!isl_space_has_named_params(model))
11620 isl_die(ctx, isl_error_invalid,
11621 "model has unnamed parameters", goto error);
11622 if (isl_map_check_named_params(map) < 0)
11623 goto error;
11624 aligned = isl_map_space_has_equal_params(map, model);
11625 if (aligned < 0)
11626 goto error;
11627 if (!aligned) {
11628 isl_reordering *exp;
11630 exp = isl_parameter_alignment_reordering(map->dim, model);
11631 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11632 map = isl_map_realign(map, exp);
11635 isl_space_free(model);
11636 return map;
11637 error:
11638 isl_space_free(model);
11639 isl_map_free(map);
11640 return NULL;
11643 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11644 __isl_take isl_space *model)
11646 return isl_map_align_params(set, model);
11649 /* Align the parameters of "bmap" to those of "model", introducing
11650 * additional parameters if needed.
11652 __isl_give isl_basic_map *isl_basic_map_align_params(
11653 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11655 isl_ctx *ctx;
11656 isl_bool equal_params;
11658 if (!bmap || !model)
11659 goto error;
11661 ctx = isl_space_get_ctx(model);
11662 if (!isl_space_has_named_params(model))
11663 isl_die(ctx, isl_error_invalid,
11664 "model has unnamed parameters", goto error);
11665 if (isl_basic_map_check_named_params(bmap) < 0)
11666 goto error;
11667 equal_params = isl_space_has_equal_params(bmap->dim, model);
11668 if (equal_params < 0)
11669 goto error;
11670 if (!equal_params) {
11671 isl_reordering *exp;
11672 struct isl_dim_map *dim_map;
11674 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11675 exp = isl_reordering_extend_space(exp,
11676 isl_basic_map_get_space(bmap));
11677 dim_map = isl_dim_map_from_reordering(exp);
11678 bmap = isl_basic_map_realign(bmap,
11679 isl_reordering_get_space(exp),
11680 isl_dim_map_extend(dim_map, bmap));
11681 isl_reordering_free(exp);
11682 isl_dim_map_free(dim_map);
11685 isl_space_free(model);
11686 return bmap;
11687 error:
11688 isl_space_free(model);
11689 isl_basic_map_free(bmap);
11690 return NULL;
11693 /* Do "bset" and "space" have the same parameters?
11695 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11696 __isl_keep isl_space *space)
11698 isl_space *bset_space;
11700 bset_space = isl_basic_set_peek_space(bset);
11701 return isl_space_has_equal_params(bset_space, space);
11704 /* Do "map" and "space" have the same parameters?
11706 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11707 __isl_keep isl_space *space)
11709 isl_space *map_space;
11711 map_space = isl_map_peek_space(map);
11712 return isl_space_has_equal_params(map_space, space);
11715 /* Do "set" and "space" have the same parameters?
11717 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11718 __isl_keep isl_space *space)
11720 return isl_map_space_has_equal_params(set_to_map(set), space);
11723 /* Align the parameters of "bset" to those of "model", introducing
11724 * additional parameters if needed.
11726 __isl_give isl_basic_set *isl_basic_set_align_params(
11727 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11729 return isl_basic_map_align_params(bset, model);
11732 /* Drop all parameters not referenced by "map".
11734 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11736 int i;
11738 if (isl_map_check_named_params(map) < 0)
11739 return isl_map_free(map);
11741 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11742 isl_bool involves;
11744 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11745 if (involves < 0)
11746 return isl_map_free(map);
11747 if (!involves)
11748 map = isl_map_project_out(map, isl_dim_param, i, 1);
11751 return map;
11754 /* Drop all parameters not referenced by "set".
11756 __isl_give isl_set *isl_set_drop_unused_params(
11757 __isl_take isl_set *set)
11759 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11762 /* Drop all parameters not referenced by "bmap".
11764 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11765 __isl_take isl_basic_map *bmap)
11767 int i;
11769 if (isl_basic_map_check_named_params(bmap) < 0)
11770 return isl_basic_map_free(bmap);
11772 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11773 isl_bool involves;
11775 involves = isl_basic_map_involves_dims(bmap,
11776 isl_dim_param, i, 1);
11777 if (involves < 0)
11778 return isl_basic_map_free(bmap);
11779 if (!involves)
11780 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11783 return bmap;
11786 /* Drop all parameters not referenced by "bset".
11788 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11789 __isl_take isl_basic_set *bset)
11791 return bset_from_bmap(isl_basic_map_drop_unused_params(
11792 bset_to_bmap(bset)));
11795 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11796 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11797 enum isl_dim_type c2, enum isl_dim_type c3,
11798 enum isl_dim_type c4, enum isl_dim_type c5)
11800 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11801 struct isl_mat *mat;
11802 int i, j, k;
11803 int pos;
11805 if (!bmap)
11806 return NULL;
11807 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11808 isl_basic_map_total_dim(bmap) + 1);
11809 if (!mat)
11810 return NULL;
11811 for (i = 0; i < bmap->n_eq; ++i)
11812 for (j = 0, pos = 0; j < 5; ++j) {
11813 int off = isl_basic_map_offset(bmap, c[j]);
11814 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11815 isl_int_set(mat->row[i][pos],
11816 bmap->eq[i][off + k]);
11817 ++pos;
11821 return mat;
11824 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11825 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11826 enum isl_dim_type c2, enum isl_dim_type c3,
11827 enum isl_dim_type c4, enum isl_dim_type c5)
11829 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11830 struct isl_mat *mat;
11831 int i, j, k;
11832 int pos;
11834 if (!bmap)
11835 return NULL;
11836 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11837 isl_basic_map_total_dim(bmap) + 1);
11838 if (!mat)
11839 return NULL;
11840 for (i = 0; i < bmap->n_ineq; ++i)
11841 for (j = 0, pos = 0; j < 5; ++j) {
11842 int off = isl_basic_map_offset(bmap, c[j]);
11843 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11844 isl_int_set(mat->row[i][pos],
11845 bmap->ineq[i][off + k]);
11846 ++pos;
11850 return mat;
11853 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11854 __isl_take isl_space *space,
11855 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11856 enum isl_dim_type c2, enum isl_dim_type c3,
11857 enum isl_dim_type c4, enum isl_dim_type c5)
11859 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11860 isl_basic_map *bmap = NULL;
11861 unsigned total;
11862 unsigned extra;
11863 int i, j, k, l;
11864 int pos;
11866 if (!space || !eq || !ineq)
11867 goto error;
11869 if (eq->n_col != ineq->n_col)
11870 isl_die(space->ctx, isl_error_invalid,
11871 "equalities and inequalities matrices should have "
11872 "same number of columns", goto error);
11874 total = 1 + isl_space_dim(space, isl_dim_all);
11876 if (eq->n_col < total)
11877 isl_die(space->ctx, isl_error_invalid,
11878 "number of columns too small", goto error);
11880 extra = eq->n_col - total;
11882 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11883 eq->n_row, ineq->n_row);
11884 if (!bmap)
11885 goto error;
11886 for (i = 0; i < extra; ++i) {
11887 k = isl_basic_map_alloc_div(bmap);
11888 if (k < 0)
11889 goto error;
11890 isl_int_set_si(bmap->div[k][0], 0);
11892 for (i = 0; i < eq->n_row; ++i) {
11893 l = isl_basic_map_alloc_equality(bmap);
11894 if (l < 0)
11895 goto error;
11896 for (j = 0, pos = 0; j < 5; ++j) {
11897 int off = isl_basic_map_offset(bmap, c[j]);
11898 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11899 isl_int_set(bmap->eq[l][off + k],
11900 eq->row[i][pos]);
11901 ++pos;
11905 for (i = 0; i < ineq->n_row; ++i) {
11906 l = isl_basic_map_alloc_inequality(bmap);
11907 if (l < 0)
11908 goto error;
11909 for (j = 0, pos = 0; j < 5; ++j) {
11910 int off = isl_basic_map_offset(bmap, c[j]);
11911 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11912 isl_int_set(bmap->ineq[l][off + k],
11913 ineq->row[i][pos]);
11914 ++pos;
11919 isl_space_free(space);
11920 isl_mat_free(eq);
11921 isl_mat_free(ineq);
11923 bmap = isl_basic_map_simplify(bmap);
11924 return isl_basic_map_finalize(bmap);
11925 error:
11926 isl_space_free(space);
11927 isl_mat_free(eq);
11928 isl_mat_free(ineq);
11929 isl_basic_map_free(bmap);
11930 return NULL;
11933 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11934 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11935 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11937 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11938 c1, c2, c3, c4, isl_dim_in);
11941 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11942 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11943 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11945 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11946 c1, c2, c3, c4, isl_dim_in);
11949 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11950 __isl_take isl_space *dim,
11951 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11952 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11954 isl_basic_map *bmap;
11955 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11956 c1, c2, c3, c4, isl_dim_in);
11957 return bset_from_bmap(bmap);
11960 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11962 if (!bmap)
11963 return isl_bool_error;
11965 return isl_space_can_zip(bmap->dim);
11968 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11970 if (!map)
11971 return isl_bool_error;
11973 return isl_space_can_zip(map->dim);
11976 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11977 * (A -> C) -> (B -> D).
11979 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11981 unsigned pos;
11982 unsigned n1;
11983 unsigned n2;
11985 if (!bmap)
11986 return NULL;
11988 if (!isl_basic_map_can_zip(bmap))
11989 isl_die(bmap->ctx, isl_error_invalid,
11990 "basic map cannot be zipped", goto error);
11991 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11992 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11993 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11994 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11995 bmap = isl_basic_map_cow(bmap);
11996 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11997 if (!bmap)
11998 return NULL;
11999 bmap->dim = isl_space_zip(bmap->dim);
12000 if (!bmap->dim)
12001 goto error;
12002 bmap = isl_basic_map_mark_final(bmap);
12003 return bmap;
12004 error:
12005 isl_basic_map_free(bmap);
12006 return NULL;
12009 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12010 * (A -> C) -> (B -> D).
12012 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12014 int i;
12016 if (!map)
12017 return NULL;
12019 if (!isl_map_can_zip(map))
12020 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12021 goto error);
12023 map = isl_map_cow(map);
12024 if (!map)
12025 return NULL;
12027 for (i = 0; i < map->n; ++i) {
12028 map->p[i] = isl_basic_map_zip(map->p[i]);
12029 if (!map->p[i])
12030 goto error;
12033 map->dim = isl_space_zip(map->dim);
12034 if (!map->dim)
12035 goto error;
12037 return map;
12038 error:
12039 isl_map_free(map);
12040 return NULL;
12043 /* Can we apply isl_basic_map_curry to "bmap"?
12044 * That is, does it have a nested relation in its domain?
12046 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12048 if (!bmap)
12049 return isl_bool_error;
12051 return isl_space_can_curry(bmap->dim);
12054 /* Can we apply isl_map_curry to "map"?
12055 * That is, does it have a nested relation in its domain?
12057 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12059 if (!map)
12060 return isl_bool_error;
12062 return isl_space_can_curry(map->dim);
12065 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12066 * A -> (B -> C).
12068 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12071 if (!bmap)
12072 return NULL;
12074 if (!isl_basic_map_can_curry(bmap))
12075 isl_die(bmap->ctx, isl_error_invalid,
12076 "basic map cannot be curried", goto error);
12077 bmap = isl_basic_map_cow(bmap);
12078 if (!bmap)
12079 return NULL;
12080 bmap->dim = isl_space_curry(bmap->dim);
12081 if (!bmap->dim)
12082 goto error;
12083 bmap = isl_basic_map_mark_final(bmap);
12084 return bmap;
12085 error:
12086 isl_basic_map_free(bmap);
12087 return NULL;
12090 /* Given a map (A -> B) -> C, return the corresponding map
12091 * A -> (B -> C).
12093 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12095 return isl_map_change_space(map, &isl_map_can_curry,
12096 "map cannot be curried", &isl_space_curry);
12099 /* Can isl_map_range_curry be applied to "map"?
12100 * That is, does it have a nested relation in its range,
12101 * the domain of which is itself a nested relation?
12103 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12105 if (!map)
12106 return isl_bool_error;
12108 return isl_space_can_range_curry(map->dim);
12111 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12112 * A -> (B -> (C -> D)).
12114 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12116 return isl_map_change_space(map, &isl_map_can_range_curry,
12117 "map range cannot be curried",
12118 &isl_space_range_curry);
12121 /* Can we apply isl_basic_map_uncurry to "bmap"?
12122 * That is, does it have a nested relation in its domain?
12124 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12126 if (!bmap)
12127 return isl_bool_error;
12129 return isl_space_can_uncurry(bmap->dim);
12132 /* Can we apply isl_map_uncurry to "map"?
12133 * That is, does it have a nested relation in its domain?
12135 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12137 if (!map)
12138 return isl_bool_error;
12140 return isl_space_can_uncurry(map->dim);
12143 /* Given a basic map A -> (B -> C), return the corresponding basic map
12144 * (A -> B) -> C.
12146 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12149 if (!bmap)
12150 return NULL;
12152 if (!isl_basic_map_can_uncurry(bmap))
12153 isl_die(bmap->ctx, isl_error_invalid,
12154 "basic map cannot be uncurried",
12155 return isl_basic_map_free(bmap));
12156 bmap = isl_basic_map_cow(bmap);
12157 if (!bmap)
12158 return NULL;
12159 bmap->dim = isl_space_uncurry(bmap->dim);
12160 if (!bmap->dim)
12161 return isl_basic_map_free(bmap);
12162 bmap = isl_basic_map_mark_final(bmap);
12163 return bmap;
12166 /* Given a map A -> (B -> C), return the corresponding map
12167 * (A -> B) -> C.
12169 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12171 return isl_map_change_space(map, &isl_map_can_uncurry,
12172 "map cannot be uncurried", &isl_space_uncurry);
12175 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12176 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12178 return isl_map_equate(set, type1, pos1, type2, pos2);
12181 /* Construct a basic map where the given dimensions are equal to each other.
12183 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12184 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12186 isl_basic_map *bmap = NULL;
12187 int i;
12189 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12190 isl_space_check_range(space, type2, pos2, 1) < 0)
12191 goto error;
12193 if (type1 == type2 && pos1 == pos2)
12194 return isl_basic_map_universe(space);
12196 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12197 i = isl_basic_map_alloc_equality(bmap);
12198 if (i < 0)
12199 goto error;
12200 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12201 pos1 += isl_basic_map_offset(bmap, type1);
12202 pos2 += isl_basic_map_offset(bmap, type2);
12203 isl_int_set_si(bmap->eq[i][pos1], -1);
12204 isl_int_set_si(bmap->eq[i][pos2], 1);
12205 bmap = isl_basic_map_finalize(bmap);
12206 isl_space_free(space);
12207 return bmap;
12208 error:
12209 isl_space_free(space);
12210 isl_basic_map_free(bmap);
12211 return NULL;
12214 /* Add a constraint imposing that the given two dimensions are equal.
12216 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12217 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12219 isl_basic_map *eq;
12221 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12223 bmap = isl_basic_map_intersect(bmap, eq);
12225 return bmap;
12228 /* Add a constraint imposing that the given two dimensions are equal.
12230 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12231 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12233 isl_basic_map *bmap;
12235 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12237 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12239 return map;
12242 /* Add a constraint imposing that the given two dimensions have opposite values.
12244 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12245 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12247 isl_basic_map *bmap = NULL;
12248 int i;
12250 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12251 return isl_map_free(map);
12252 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12253 return isl_map_free(map);
12255 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12256 i = isl_basic_map_alloc_equality(bmap);
12257 if (i < 0)
12258 goto error;
12259 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12260 pos1 += isl_basic_map_offset(bmap, type1);
12261 pos2 += isl_basic_map_offset(bmap, type2);
12262 isl_int_set_si(bmap->eq[i][pos1], 1);
12263 isl_int_set_si(bmap->eq[i][pos2], 1);
12264 bmap = isl_basic_map_finalize(bmap);
12266 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12268 return map;
12269 error:
12270 isl_basic_map_free(bmap);
12271 isl_map_free(map);
12272 return NULL;
12275 /* Construct a constraint imposing that the value of the first dimension is
12276 * greater than or equal to that of the second.
12278 static __isl_give isl_constraint *constraint_order_ge(
12279 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12280 enum isl_dim_type type2, int pos2)
12282 isl_constraint *c;
12284 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12285 isl_space_check_range(space, type2, pos2, 1) < 0)
12286 space = isl_space_free(space);
12287 if (!space)
12288 return NULL;
12290 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12292 if (type1 == type2 && pos1 == pos2)
12293 return c;
12295 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12296 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12298 return c;
12301 /* Add a constraint imposing that the value of the first dimension is
12302 * greater than or equal to that of the second.
12304 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12305 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12307 isl_constraint *c;
12308 isl_space *space;
12310 if (type1 == type2 && pos1 == pos2)
12311 return bmap;
12312 space = isl_basic_map_get_space(bmap);
12313 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12314 bmap = isl_basic_map_add_constraint(bmap, c);
12316 return bmap;
12319 /* Add a constraint imposing that the value of the first dimension is
12320 * greater than or equal to that of the second.
12322 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12323 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12325 isl_constraint *c;
12326 isl_space *space;
12328 if (type1 == type2 && pos1 == pos2)
12329 return map;
12330 space = isl_map_get_space(map);
12331 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12332 map = isl_map_add_constraint(map, c);
12334 return map;
12337 /* Add a constraint imposing that the value of the first dimension is
12338 * less than or equal to that of the second.
12340 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12341 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12343 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12346 /* Construct a basic map where the value of the first dimension is
12347 * greater than that of the second.
12349 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12350 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12352 isl_basic_map *bmap = NULL;
12353 int i;
12355 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12356 isl_space_check_range(space, type2, pos2, 1) < 0)
12357 goto error;
12359 if (type1 == type2 && pos1 == pos2)
12360 return isl_basic_map_empty(space);
12362 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12363 i = isl_basic_map_alloc_inequality(bmap);
12364 if (i < 0)
12365 return isl_basic_map_free(bmap);
12366 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12367 pos1 += isl_basic_map_offset(bmap, type1);
12368 pos2 += isl_basic_map_offset(bmap, type2);
12369 isl_int_set_si(bmap->ineq[i][pos1], 1);
12370 isl_int_set_si(bmap->ineq[i][pos2], -1);
12371 isl_int_set_si(bmap->ineq[i][0], -1);
12372 bmap = isl_basic_map_finalize(bmap);
12374 return bmap;
12375 error:
12376 isl_space_free(space);
12377 isl_basic_map_free(bmap);
12378 return NULL;
12381 /* Add a constraint imposing that the value of the first dimension is
12382 * greater than that of the second.
12384 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12385 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12387 isl_basic_map *gt;
12389 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12391 bmap = isl_basic_map_intersect(bmap, gt);
12393 return bmap;
12396 /* Add a constraint imposing that the value of the first dimension is
12397 * greater than that of the second.
12399 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12400 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12402 isl_basic_map *bmap;
12404 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12406 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12408 return map;
12411 /* Add a constraint imposing that the value of the first dimension is
12412 * smaller than that of the second.
12414 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12415 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12417 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12420 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12421 int pos)
12423 isl_aff *div;
12424 isl_local_space *ls;
12426 if (!bmap)
12427 return NULL;
12429 if (!isl_basic_map_divs_known(bmap))
12430 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12431 "some divs are unknown", return NULL);
12433 ls = isl_basic_map_get_local_space(bmap);
12434 div = isl_local_space_get_div(ls, pos);
12435 isl_local_space_free(ls);
12437 return div;
12440 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12441 int pos)
12443 return isl_basic_map_get_div(bset, pos);
12446 /* Plug in "subs" for dimension "type", "pos" of "bset".
12448 * Let i be the dimension to replace and let "subs" be of the form
12450 * f/d
12452 * Any integer division with a non-zero coefficient for i,
12454 * floor((a i + g)/m)
12456 * is replaced by
12458 * floor((a f + d g)/(m d))
12460 * Constraints of the form
12462 * a i + g
12464 * are replaced by
12466 * a f + d g
12468 * We currently require that "subs" is an integral expression.
12469 * Handling rational expressions may require us to add stride constraints
12470 * as we do in isl_basic_set_preimage_multi_aff.
12472 __isl_give isl_basic_set *isl_basic_set_substitute(
12473 __isl_take isl_basic_set *bset,
12474 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12476 int i;
12477 isl_int v;
12478 isl_ctx *ctx;
12480 if (bset && isl_basic_set_plain_is_empty(bset))
12481 return bset;
12483 bset = isl_basic_set_cow(bset);
12484 if (!bset || !subs)
12485 goto error;
12487 ctx = isl_basic_set_get_ctx(bset);
12488 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12489 isl_die(ctx, isl_error_invalid,
12490 "spaces don't match", goto error);
12491 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12492 isl_die(ctx, isl_error_unsupported,
12493 "cannot handle divs yet", goto error);
12494 if (!isl_int_is_one(subs->v->el[0]))
12495 isl_die(ctx, isl_error_invalid,
12496 "can only substitute integer expressions", goto error);
12498 pos += isl_basic_set_offset(bset, type);
12500 isl_int_init(v);
12502 for (i = 0; i < bset->n_eq; ++i) {
12503 if (isl_int_is_zero(bset->eq[i][pos]))
12504 continue;
12505 isl_int_set(v, bset->eq[i][pos]);
12506 isl_int_set_si(bset->eq[i][pos], 0);
12507 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12508 v, subs->v->el + 1, subs->v->size - 1);
12511 for (i = 0; i < bset->n_ineq; ++i) {
12512 if (isl_int_is_zero(bset->ineq[i][pos]))
12513 continue;
12514 isl_int_set(v, bset->ineq[i][pos]);
12515 isl_int_set_si(bset->ineq[i][pos], 0);
12516 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12517 v, subs->v->el + 1, subs->v->size - 1);
12520 for (i = 0; i < bset->n_div; ++i) {
12521 if (isl_int_is_zero(bset->div[i][1 + pos]))
12522 continue;
12523 isl_int_set(v, bset->div[i][1 + pos]);
12524 isl_int_set_si(bset->div[i][1 + pos], 0);
12525 isl_seq_combine(bset->div[i] + 1,
12526 subs->v->el[0], bset->div[i] + 1,
12527 v, subs->v->el + 1, subs->v->size - 1);
12528 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12531 isl_int_clear(v);
12533 bset = isl_basic_set_simplify(bset);
12534 return isl_basic_set_finalize(bset);
12535 error:
12536 isl_basic_set_free(bset);
12537 return NULL;
12540 /* Plug in "subs" for dimension "type", "pos" of "set".
12542 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12543 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12545 int i;
12547 if (set && isl_set_plain_is_empty(set))
12548 return set;
12550 set = isl_set_cow(set);
12551 if (!set || !subs)
12552 goto error;
12554 for (i = set->n - 1; i >= 0; --i) {
12555 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12556 set = set_from_map(remove_if_empty(set_to_map(set), i));
12557 if (!set)
12558 return NULL;
12561 return set;
12562 error:
12563 isl_set_free(set);
12564 return NULL;
12567 /* Check if the range of "ma" is compatible with the domain or range
12568 * (depending on "type") of "bmap".
12570 static isl_stat check_basic_map_compatible_range_multi_aff(
12571 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12572 __isl_keep isl_multi_aff *ma)
12574 isl_bool m;
12575 isl_space *ma_space;
12577 ma_space = isl_multi_aff_get_space(ma);
12579 m = isl_space_has_equal_params(bmap->dim, ma_space);
12580 if (m < 0)
12581 goto error;
12582 if (!m)
12583 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12584 "parameters don't match", goto error);
12585 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12586 if (m < 0)
12587 goto error;
12588 if (!m)
12589 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12590 "spaces don't match", goto error);
12592 isl_space_free(ma_space);
12593 return isl_stat_ok;
12594 error:
12595 isl_space_free(ma_space);
12596 return isl_stat_error;
12599 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12600 * coefficients before the transformed range of dimensions,
12601 * the "n_after" coefficients after the transformed range of dimensions
12602 * and the coefficients of the other divs in "bmap".
12604 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
12605 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12607 int i;
12608 int n_param;
12609 int n_set;
12610 isl_local_space *ls;
12612 if (n_div == 0)
12613 return bmap;
12615 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12616 if (!ls)
12617 return isl_basic_map_free(bmap);
12619 n_param = isl_local_space_dim(ls, isl_dim_param);
12620 n_set = isl_local_space_dim(ls, isl_dim_set);
12621 for (i = 0; i < n_div; ++i) {
12622 int o_bmap = 0, o_ls = 0;
12624 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12625 o_bmap += 1 + 1 + n_param;
12626 o_ls += 1 + 1 + n_param;
12627 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12628 o_bmap += n_before;
12629 isl_seq_cpy(bmap->div[i] + o_bmap,
12630 ls->div->row[i] + o_ls, n_set);
12631 o_bmap += n_set;
12632 o_ls += n_set;
12633 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12634 o_bmap += n_after;
12635 isl_seq_cpy(bmap->div[i] + o_bmap,
12636 ls->div->row[i] + o_ls, n_div);
12637 o_bmap += n_div;
12638 o_ls += n_div;
12639 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12640 bmap = isl_basic_map_add_div_constraints(bmap, i);
12641 if (!bmap)
12642 goto error;
12645 isl_local_space_free(ls);
12646 return bmap;
12647 error:
12648 isl_local_space_free(ls);
12649 return isl_basic_map_free(bmap);
12652 /* How many stride constraints does "ma" enforce?
12653 * That is, how many of the affine expressions have a denominator
12654 * different from one?
12656 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12658 int i;
12659 int strides = 0;
12661 for (i = 0; i < ma->n; ++i)
12662 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12663 strides++;
12665 return strides;
12668 /* For each affine expression in ma of the form
12670 * x_i = (f_i y + h_i)/m_i
12672 * with m_i different from one, add a constraint to "bmap"
12673 * of the form
12675 * f_i y + h_i = m_i alpha_i
12677 * with alpha_i an additional existentially quantified variable.
12679 * The input variables of "ma" correspond to a subset of the variables
12680 * of "bmap". There are "n_before" variables in "bmap" before this
12681 * subset and "n_after" variables after this subset.
12682 * The integer divisions of the affine expressions in "ma" are assumed
12683 * to have been aligned. There are "n_div_ma" of them and
12684 * they appear first in "bmap", straight after the "n_after" variables.
12686 static __isl_give isl_basic_map *add_ma_strides(
12687 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12688 int n_before, int n_after, int n_div_ma)
12690 int i, k;
12691 int div;
12692 int total;
12693 int n_param;
12694 int n_in;
12696 total = isl_basic_map_total_dim(bmap);
12697 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12698 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12699 for (i = 0; i < ma->n; ++i) {
12700 int o_bmap = 0, o_ma = 1;
12702 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12703 continue;
12704 div = isl_basic_map_alloc_div(bmap);
12705 k = isl_basic_map_alloc_equality(bmap);
12706 if (div < 0 || k < 0)
12707 goto error;
12708 isl_int_set_si(bmap->div[div][0], 0);
12709 isl_seq_cpy(bmap->eq[k] + o_bmap,
12710 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12711 o_bmap += 1 + n_param;
12712 o_ma += 1 + n_param;
12713 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12714 o_bmap += n_before;
12715 isl_seq_cpy(bmap->eq[k] + o_bmap,
12716 ma->u.p[i]->v->el + o_ma, n_in);
12717 o_bmap += n_in;
12718 o_ma += n_in;
12719 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12720 o_bmap += n_after;
12721 isl_seq_cpy(bmap->eq[k] + o_bmap,
12722 ma->u.p[i]->v->el + o_ma, n_div_ma);
12723 o_bmap += n_div_ma;
12724 o_ma += n_div_ma;
12725 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12726 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12727 total++;
12730 return bmap;
12731 error:
12732 isl_basic_map_free(bmap);
12733 return NULL;
12736 /* Replace the domain or range space (depending on "type) of "space" by "set".
12738 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12739 enum isl_dim_type type, __isl_take isl_space *set)
12741 if (type == isl_dim_in) {
12742 space = isl_space_range(space);
12743 space = isl_space_map_from_domain_and_range(set, space);
12744 } else {
12745 space = isl_space_domain(space);
12746 space = isl_space_map_from_domain_and_range(space, set);
12749 return space;
12752 /* Compute the preimage of the domain or range (depending on "type")
12753 * of "bmap" under the function represented by "ma".
12754 * In other words, plug in "ma" in the domain or range of "bmap".
12755 * The result is a basic map that lives in the same space as "bmap"
12756 * except that the domain or range has been replaced by
12757 * the domain space of "ma".
12759 * If bmap is represented by
12761 * A(p) + S u + B x + T v + C(divs) >= 0,
12763 * where u and x are input and output dimensions if type == isl_dim_out
12764 * while x and v are input and output dimensions if type == isl_dim_in,
12765 * and ma is represented by
12767 * x = D(p) + F(y) + G(divs')
12769 * then the result is
12771 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12773 * The divs in the input set are similarly adjusted.
12774 * In particular
12776 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12778 * becomes
12780 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12781 * B_i G(divs') + c_i(divs))/n_i)
12783 * If bmap is not a rational map and if F(y) involves any denominators
12785 * x_i = (f_i y + h_i)/m_i
12787 * then additional constraints are added to ensure that we only
12788 * map back integer points. That is we enforce
12790 * f_i y + h_i = m_i alpha_i
12792 * with alpha_i an additional existentially quantified variable.
12794 * We first copy over the divs from "ma".
12795 * Then we add the modified constraints and divs from "bmap".
12796 * Finally, we add the stride constraints, if needed.
12798 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12799 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12800 __isl_take isl_multi_aff *ma)
12802 int i, k;
12803 isl_space *space;
12804 isl_basic_map *res = NULL;
12805 int n_before, n_after, n_div_bmap, n_div_ma;
12806 isl_int f, c1, c2, g;
12807 isl_bool rational;
12808 int strides;
12810 isl_int_init(f);
12811 isl_int_init(c1);
12812 isl_int_init(c2);
12813 isl_int_init(g);
12815 ma = isl_multi_aff_align_divs(ma);
12816 if (!bmap || !ma)
12817 goto error;
12818 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12819 goto error;
12821 if (type == isl_dim_in) {
12822 n_before = 0;
12823 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12824 } else {
12825 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12826 n_after = 0;
12828 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12829 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12831 space = isl_multi_aff_get_domain_space(ma);
12832 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12833 rational = isl_basic_map_is_rational(bmap);
12834 strides = rational ? 0 : multi_aff_strides(ma);
12835 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12836 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12837 if (rational)
12838 res = isl_basic_map_set_rational(res);
12840 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12841 if (isl_basic_map_alloc_div(res) < 0)
12842 goto error;
12844 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
12845 if (!res)
12846 goto error;
12848 for (i = 0; i < bmap->n_eq; ++i) {
12849 k = isl_basic_map_alloc_equality(res);
12850 if (k < 0)
12851 goto error;
12852 if (isl_seq_preimage(res->eq[k], bmap->eq[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_ineq; ++i) {
12859 k = isl_basic_map_alloc_inequality(res);
12860 if (k < 0)
12861 goto error;
12862 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12863 n_after, n_div_ma, n_div_bmap,
12864 f, c1, c2, g, 0) < 0)
12865 goto error;
12868 for (i = 0; i < bmap->n_div; ++i) {
12869 if (isl_int_is_zero(bmap->div[i][0])) {
12870 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12871 continue;
12873 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12874 n_before, n_after, n_div_ma, n_div_bmap,
12875 f, c1, c2, g, 1) < 0)
12876 goto error;
12879 if (strides)
12880 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12882 isl_int_clear(f);
12883 isl_int_clear(c1);
12884 isl_int_clear(c2);
12885 isl_int_clear(g);
12886 isl_basic_map_free(bmap);
12887 isl_multi_aff_free(ma);
12888 res = isl_basic_map_simplify(res);
12889 return isl_basic_map_finalize(res);
12890 error:
12891 isl_int_clear(f);
12892 isl_int_clear(c1);
12893 isl_int_clear(c2);
12894 isl_int_clear(g);
12895 isl_basic_map_free(bmap);
12896 isl_multi_aff_free(ma);
12897 isl_basic_map_free(res);
12898 return NULL;
12901 /* Compute the preimage of "bset" under the function represented by "ma".
12902 * In other words, plug in "ma" in "bset". The result is a basic set
12903 * that lives in the domain space of "ma".
12905 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12906 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12908 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12911 /* Compute the preimage of the domain of "bmap" under the function
12912 * represented by "ma".
12913 * In other words, plug in "ma" in the domain of "bmap".
12914 * The result is a basic map that lives in the same space as "bmap"
12915 * except that the domain has been replaced by the domain space of "ma".
12917 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12918 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12920 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12923 /* Compute the preimage of the range of "bmap" under the function
12924 * represented by "ma".
12925 * In other words, plug in "ma" in the range of "bmap".
12926 * The result is a basic map that lives in the same space as "bmap"
12927 * except that the range has been replaced by the domain space of "ma".
12929 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12930 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12932 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12935 /* Check if the range of "ma" is compatible with the domain or range
12936 * (depending on "type") of "map".
12937 * Return isl_stat_error if anything is wrong.
12939 static isl_stat check_map_compatible_range_multi_aff(
12940 __isl_keep isl_map *map, enum isl_dim_type type,
12941 __isl_keep isl_multi_aff *ma)
12943 isl_bool m;
12944 isl_space *ma_space;
12946 ma_space = isl_multi_aff_get_space(ma);
12947 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12948 isl_space_free(ma_space);
12949 if (m < 0)
12950 return isl_stat_error;
12951 if (!m)
12952 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12953 "spaces don't match", return isl_stat_error);
12954 return isl_stat_ok;
12957 /* Compute the preimage of the domain or range (depending on "type")
12958 * of "map" under the function represented by "ma".
12959 * In other words, plug in "ma" in the domain or range of "map".
12960 * The result is a map that lives in the same space as "map"
12961 * except that the domain or range has been replaced by
12962 * the domain space of "ma".
12964 * The parameters are assumed to have been aligned.
12966 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12967 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12969 int i;
12970 isl_space *space;
12972 map = isl_map_cow(map);
12973 ma = isl_multi_aff_align_divs(ma);
12974 if (!map || !ma)
12975 goto error;
12976 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12977 goto error;
12979 for (i = 0; i < map->n; ++i) {
12980 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12981 isl_multi_aff_copy(ma));
12982 if (!map->p[i])
12983 goto error;
12986 space = isl_multi_aff_get_domain_space(ma);
12987 space = isl_space_set(isl_map_get_space(map), type, space);
12989 isl_space_free(map->dim);
12990 map->dim = space;
12991 if (!map->dim)
12992 goto error;
12994 isl_multi_aff_free(ma);
12995 if (map->n > 1)
12996 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12997 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12998 return map;
12999 error:
13000 isl_multi_aff_free(ma);
13001 isl_map_free(map);
13002 return NULL;
13005 /* Compute the preimage of the domain or range (depending on "type")
13006 * of "map" under the function represented by "ma".
13007 * In other words, plug in "ma" in the domain or range of "map".
13008 * The result is a map that lives in the same space as "map"
13009 * except that the domain or range has been replaced by
13010 * the domain space of "ma".
13012 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13013 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13015 isl_bool aligned;
13017 if (!map || !ma)
13018 goto error;
13020 aligned = isl_map_space_has_equal_params(map, ma->space);
13021 if (aligned < 0)
13022 goto error;
13023 if (aligned)
13024 return map_preimage_multi_aff(map, type, ma);
13026 if (isl_map_check_named_params(map) < 0)
13027 goto error;
13028 if (!isl_space_has_named_params(ma->space))
13029 isl_die(map->ctx, isl_error_invalid,
13030 "unaligned unnamed parameters", goto error);
13031 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13032 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13034 return map_preimage_multi_aff(map, type, ma);
13035 error:
13036 isl_multi_aff_free(ma);
13037 return isl_map_free(map);
13040 /* Compute the preimage of "set" under the function represented by "ma".
13041 * In other words, plug in "ma" in "set". The result is a set
13042 * that lives in the domain space of "ma".
13044 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13045 __isl_take isl_multi_aff *ma)
13047 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13050 /* Compute the preimage of the domain of "map" under the function
13051 * represented by "ma".
13052 * In other words, plug in "ma" in the domain of "map".
13053 * The result is a map that lives in the same space as "map"
13054 * except that the domain has been replaced by the domain space of "ma".
13056 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13057 __isl_take isl_multi_aff *ma)
13059 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13062 /* Compute the preimage of the range of "map" under the function
13063 * represented by "ma".
13064 * In other words, plug in "ma" in the range of "map".
13065 * The result is a map that lives in the same space as "map"
13066 * except that the range has been replaced by the domain space of "ma".
13068 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13069 __isl_take isl_multi_aff *ma)
13071 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13074 /* Compute the preimage of "map" under the function represented by "pma".
13075 * In other words, plug in "pma" in the domain or range of "map".
13076 * The result is a map that lives in the same space as "map",
13077 * except that the space of type "type" has been replaced by
13078 * the domain space of "pma".
13080 * The parameters of "map" and "pma" are assumed to have been aligned.
13082 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13083 __isl_take isl_map *map, enum isl_dim_type type,
13084 __isl_take isl_pw_multi_aff *pma)
13086 int i;
13087 isl_map *res;
13089 if (!pma)
13090 goto error;
13092 if (pma->n == 0) {
13093 isl_pw_multi_aff_free(pma);
13094 res = isl_map_empty(isl_map_get_space(map));
13095 isl_map_free(map);
13096 return res;
13099 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13100 isl_multi_aff_copy(pma->p[0].maff));
13101 if (type == isl_dim_in)
13102 res = isl_map_intersect_domain(res,
13103 isl_map_copy(pma->p[0].set));
13104 else
13105 res = isl_map_intersect_range(res,
13106 isl_map_copy(pma->p[0].set));
13108 for (i = 1; i < pma->n; ++i) {
13109 isl_map *res_i;
13111 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13112 isl_multi_aff_copy(pma->p[i].maff));
13113 if (type == isl_dim_in)
13114 res_i = isl_map_intersect_domain(res_i,
13115 isl_map_copy(pma->p[i].set));
13116 else
13117 res_i = isl_map_intersect_range(res_i,
13118 isl_map_copy(pma->p[i].set));
13119 res = isl_map_union(res, res_i);
13122 isl_pw_multi_aff_free(pma);
13123 isl_map_free(map);
13124 return res;
13125 error:
13126 isl_pw_multi_aff_free(pma);
13127 isl_map_free(map);
13128 return NULL;
13131 /* Compute the preimage of "map" under the function represented by "pma".
13132 * In other words, plug in "pma" in the domain or range of "map".
13133 * The result is a map that lives in the same space as "map",
13134 * except that the space of type "type" has been replaced by
13135 * the domain space of "pma".
13137 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13138 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13140 isl_bool aligned;
13142 if (!map || !pma)
13143 goto error;
13145 aligned = isl_map_space_has_equal_params(map, pma->dim);
13146 if (aligned < 0)
13147 goto error;
13148 if (aligned)
13149 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13151 if (isl_map_check_named_params(map) < 0)
13152 goto error;
13153 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13154 goto error;
13155 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13156 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13158 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13159 error:
13160 isl_pw_multi_aff_free(pma);
13161 return isl_map_free(map);
13164 /* Compute the preimage of "set" under the function represented by "pma".
13165 * In other words, plug in "pma" in "set". The result is a set
13166 * that lives in the domain space of "pma".
13168 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13169 __isl_take isl_pw_multi_aff *pma)
13171 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13174 /* Compute the preimage of the domain of "map" under the function
13175 * represented by "pma".
13176 * In other words, plug in "pma" in the domain of "map".
13177 * The result is a map that lives in the same space as "map",
13178 * except that domain space has been replaced by the domain space of "pma".
13180 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13181 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13183 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13186 /* Compute the preimage of the range of "map" under the function
13187 * represented by "pma".
13188 * In other words, plug in "pma" in the range of "map".
13189 * The result is a map that lives in the same space as "map",
13190 * except that range space has been replaced by the domain space of "pma".
13192 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13193 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13195 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13198 /* Compute the preimage of "map" under the function represented by "mpa".
13199 * In other words, plug in "mpa" in the domain or range of "map".
13200 * The result is a map that lives in the same space as "map",
13201 * except that the space of type "type" has been replaced by
13202 * the domain space of "mpa".
13204 * If the map does not involve any constraints that refer to the
13205 * dimensions of the substituted space, then the only possible
13206 * effect of "mpa" on the map is to map the space to a different space.
13207 * We create a separate isl_multi_aff to effectuate this change
13208 * in order to avoid spurious splitting of the map along the pieces
13209 * of "mpa".
13210 * If "mpa" has a non-trivial explicit domain, however,
13211 * then the full substitution should be performed.
13213 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13214 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13216 int n;
13217 isl_bool full;
13218 isl_pw_multi_aff *pma;
13220 if (!map || !mpa)
13221 goto error;
13223 n = isl_map_dim(map, type);
13224 full = isl_map_involves_dims(map, type, 0, n);
13225 if (full >= 0 && !full)
13226 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13227 if (full < 0)
13228 goto error;
13229 if (!full) {
13230 isl_space *space;
13231 isl_multi_aff *ma;
13233 space = isl_multi_pw_aff_get_space(mpa);
13234 isl_multi_pw_aff_free(mpa);
13235 ma = isl_multi_aff_zero(space);
13236 return isl_map_preimage_multi_aff(map, type, ma);
13239 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13240 return isl_map_preimage_pw_multi_aff(map, type, pma);
13241 error:
13242 isl_map_free(map);
13243 isl_multi_pw_aff_free(mpa);
13244 return NULL;
13247 /* Compute the preimage of "map" under the function represented by "mpa".
13248 * In other words, plug in "mpa" in the domain "map".
13249 * The result is a map that lives in the same space as "map",
13250 * except that domain space has been replaced by the domain space of "mpa".
13252 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13253 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13255 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13258 /* Compute the preimage of "set" by the function represented by "mpa".
13259 * In other words, plug in "mpa" in "set".
13261 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13262 __isl_take isl_multi_pw_aff *mpa)
13264 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13267 /* Return a copy of the equality constraints of "bset" as a matrix.
13269 __isl_give isl_mat *isl_basic_set_extract_equalities(
13270 __isl_keep isl_basic_set *bset)
13272 isl_ctx *ctx;
13273 unsigned total;
13275 if (!bset)
13276 return NULL;
13278 ctx = isl_basic_set_get_ctx(bset);
13279 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13280 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13283 /* Are the "n" "coefficients" starting at "first" of the integer division
13284 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13285 * to each other?
13286 * The "coefficient" at position 0 is the denominator.
13287 * The "coefficient" at position 1 is the constant term.
13289 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13290 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13291 unsigned first, unsigned n)
13293 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13294 return isl_bool_error;
13295 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13296 return isl_bool_error;
13297 return isl_seq_eq(bmap1->div[pos1] + first,
13298 bmap2->div[pos2] + first, n);
13301 /* Are the integer division expressions at position "pos1" in "bmap1" and
13302 * "pos2" in "bmap2" equal to each other, except that the constant terms
13303 * are different?
13305 isl_bool isl_basic_map_equal_div_expr_except_constant(
13306 __isl_keep isl_basic_map *bmap1, int pos1,
13307 __isl_keep isl_basic_map *bmap2, int pos2)
13309 isl_bool equal;
13310 unsigned total;
13312 if (!bmap1 || !bmap2)
13313 return isl_bool_error;
13314 total = isl_basic_map_total_dim(bmap1);
13315 if (total != isl_basic_map_total_dim(bmap2))
13316 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13317 "incomparable div expressions", return isl_bool_error);
13318 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13319 0, 1);
13320 if (equal < 0 || !equal)
13321 return equal;
13322 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13323 1, 1);
13324 if (equal < 0 || equal)
13325 return isl_bool_not(equal);
13326 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13327 2, total);
13330 /* Replace the numerator of the constant term of the integer division
13331 * expression at position "div" in "bmap" by "value".
13332 * The caller guarantees that this does not change the meaning
13333 * of the input.
13335 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13336 __isl_take isl_basic_map *bmap, int div, int value)
13338 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13339 return isl_basic_map_free(bmap);
13341 isl_int_set_si(bmap->div[div][1], value);
13343 return bmap;
13346 /* Is the point "inner" internal to inequality constraint "ineq"
13347 * of "bset"?
13348 * The point is considered to be internal to the inequality constraint,
13349 * if it strictly lies on the positive side of the inequality constraint,
13350 * or if it lies on the constraint and the constraint is lexico-positive.
13352 static isl_bool is_internal(__isl_keep isl_vec *inner,
13353 __isl_keep isl_basic_set *bset, int ineq)
13355 isl_ctx *ctx;
13356 int pos;
13357 unsigned total;
13359 if (!inner || !bset)
13360 return isl_bool_error;
13362 ctx = isl_basic_set_get_ctx(bset);
13363 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13364 &ctx->normalize_gcd);
13365 if (!isl_int_is_zero(ctx->normalize_gcd))
13366 return isl_int_is_nonneg(ctx->normalize_gcd);
13368 total = isl_basic_set_dim(bset, isl_dim_all);
13369 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13370 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13373 /* Tighten the inequality constraints of "bset" that are outward with respect
13374 * to the point "vec".
13375 * That is, tighten the constraints that are not satisfied by "vec".
13377 * "vec" is a point internal to some superset S of "bset" that is used
13378 * to make the subsets of S disjoint, by tightening one half of the constraints
13379 * that separate two subsets. In particular, the constraints of S
13380 * are all satisfied by "vec" and should not be tightened.
13381 * Of the internal constraints, those that have "vec" on the outside
13382 * are tightened. The shared facet is included in the adjacent subset
13383 * with the opposite constraint.
13384 * For constraints that saturate "vec", this criterion cannot be used
13385 * to determine which of the two sides should be tightened.
13386 * Instead, the sign of the first non-zero coefficient is used
13387 * to make this choice. Note that this second criterion is never used
13388 * on the constraints of S since "vec" is interior to "S".
13390 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13391 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13393 int j;
13395 bset = isl_basic_set_cow(bset);
13396 if (!bset)
13397 return NULL;
13398 for (j = 0; j < bset->n_ineq; ++j) {
13399 isl_bool internal;
13401 internal = is_internal(vec, bset, j);
13402 if (internal < 0)
13403 return isl_basic_set_free(bset);
13404 if (internal)
13405 continue;
13406 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13409 return bset;
13412 /* Replace the variables x of type "type" starting at "first" in "bmap"
13413 * by x' with x = M x' with M the matrix trans.
13414 * That is, replace the corresponding coefficients c by c M.
13416 * The transformation matrix should be a square matrix.
13418 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13419 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13420 __isl_take isl_mat *trans)
13422 unsigned pos;
13424 bmap = isl_basic_map_cow(bmap);
13425 if (!bmap || !trans)
13426 goto error;
13428 if (trans->n_row != trans->n_col)
13429 isl_die(trans->ctx, isl_error_invalid,
13430 "expecting square transformation matrix", goto error);
13431 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13432 goto error;
13434 pos = isl_basic_map_offset(bmap, type) + first;
13436 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13437 isl_mat_copy(trans)) < 0)
13438 goto error;
13439 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13440 isl_mat_copy(trans)) < 0)
13441 goto error;
13442 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13443 isl_mat_copy(trans)) < 0)
13444 goto error;
13446 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13447 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13449 isl_mat_free(trans);
13450 return bmap;
13451 error:
13452 isl_mat_free(trans);
13453 isl_basic_map_free(bmap);
13454 return NULL;
13457 /* Replace the variables x of type "type" starting at "first" in "bset"
13458 * by x' with x = M x' with M the matrix trans.
13459 * That is, replace the corresponding coefficients c by c M.
13461 * The transformation matrix should be a square matrix.
13463 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13464 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13465 __isl_take isl_mat *trans)
13467 return isl_basic_map_transform_dims(bset, type, first, trans);