isl_map.c: basic_set_append_equalities: use isl_basic_set_offset
[isl.git] / isl_map.c
blobfdd1c8fbef3e4588db9baaed474c92df974b3b6e
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 unsigned off;
8662 if (!bmap)
8663 return NULL;
8665 off = isl_space_dim(bmap->dim, isl_dim_all);
8667 for (i = 0; i < bmap->n_div; ++i) {
8668 int pos;
8669 if (isl_int_is_zero(bmap->div[i][0]))
8670 continue;
8671 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8672 bmap->n_div-i);
8673 if (pos == -1)
8674 continue;
8675 if (pos == 0)
8676 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8677 "integer division depends on itself",
8678 return isl_basic_map_free(bmap));
8679 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8680 if (!bmap)
8681 return NULL;
8682 --i;
8684 return bmap;
8687 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8689 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8692 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8694 int i;
8696 if (!map)
8697 return 0;
8699 for (i = 0; i < map->n; ++i) {
8700 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8701 if (!map->p[i])
8702 goto error;
8705 return map;
8706 error:
8707 isl_map_free(map);
8708 return NULL;
8711 /* Sort the local variables of "bset".
8713 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8714 __isl_take isl_basic_set *bset)
8716 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8719 /* Apply the expansion computed by isl_merge_divs.
8720 * The expansion itself is given by "exp" while the resulting
8721 * list of divs is given by "div".
8723 * Move the integer divisions of "bmap" into the right position
8724 * according to "exp" and then introduce the additional integer
8725 * divisions, adding div constraints.
8726 * The moving should be done first to avoid moving coefficients
8727 * in the definitions of the extra integer divisions.
8729 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8730 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8732 int i, j;
8733 int n_div;
8735 bmap = isl_basic_map_cow(bmap);
8736 if (!bmap || !div)
8737 goto error;
8739 if (div->n_row < bmap->n_div)
8740 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8741 "not an expansion", goto error);
8743 n_div = bmap->n_div;
8744 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8745 div->n_row - n_div, 0,
8746 2 * (div->n_row - n_div));
8748 for (i = n_div; i < div->n_row; ++i)
8749 if (isl_basic_map_alloc_div(bmap) < 0)
8750 goto error;
8752 for (j = n_div - 1; j >= 0; --j) {
8753 if (exp[j] == j)
8754 break;
8755 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8756 if (!bmap)
8757 goto error;
8759 j = 0;
8760 for (i = 0; i < div->n_row; ++i) {
8761 if (j < n_div && exp[j] == i) {
8762 j++;
8763 } else {
8764 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8765 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8766 continue;
8767 bmap = isl_basic_map_add_div_constraints(bmap, i);
8768 if (!bmap)
8769 goto error;
8773 isl_mat_free(div);
8774 return bmap;
8775 error:
8776 isl_basic_map_free(bmap);
8777 isl_mat_free(div);
8778 return NULL;
8781 /* Apply the expansion computed by isl_merge_divs.
8782 * The expansion itself is given by "exp" while the resulting
8783 * list of divs is given by "div".
8785 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8786 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8788 return isl_basic_map_expand_divs(bset, div, exp);
8791 /* Look for a div in dst that corresponds to the div "div" in src.
8792 * The divs before "div" in src and dst are assumed to be the same.
8794 * Return the position of the corresponding div in dst
8795 * if there is one. Otherwise, return a position beyond the integer divisions.
8796 * Return -1 on error.
8798 static int find_div(__isl_keep isl_basic_map *dst,
8799 __isl_keep isl_basic_map *src, unsigned div)
8801 int i;
8802 unsigned n_div;
8803 unsigned total;
8805 if (!dst || !src)
8806 return -1;
8808 total = isl_space_dim(src->dim, isl_dim_all);
8809 n_div = isl_basic_map_dim(dst, isl_dim_div);
8810 isl_assert(dst->ctx, div <= n_div, return -1);
8811 for (i = div; i < n_div; ++i)
8812 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8813 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8814 n_div - div) == -1)
8815 return i;
8816 return n_div;
8819 /* Align the divs of "dst" to those of "src", adding divs from "src"
8820 * if needed. That is, make sure that the first src->n_div divs
8821 * of the result are equal to those of src.
8823 * The result is not finalized as by design it will have redundant
8824 * divs if any divs from "src" were copied.
8826 __isl_give isl_basic_map *isl_basic_map_align_divs(
8827 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8829 int i;
8830 isl_bool known;
8831 int extended;
8832 unsigned total, 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 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8849 if (!src)
8850 return isl_basic_map_free(dst);
8852 extended = 0;
8853 total = isl_space_dim(src->dim, isl_dim_all);
8854 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
8855 for (i = 0; i < src->n_div; ++i) {
8856 int j = find_div(dst, src, i);
8857 if (j < 0)
8858 dst = isl_basic_map_free(dst);
8859 if (j == dst_n_div) {
8860 if (!extended) {
8861 int extra = src->n_div - i;
8862 dst = isl_basic_map_cow(dst);
8863 if (!dst)
8864 goto error;
8865 dst = isl_basic_map_extend_space(dst,
8866 isl_space_copy(dst->dim),
8867 extra, 0, 2 * extra);
8868 extended = 1;
8870 j = isl_basic_map_alloc_div(dst);
8871 if (j < 0)
8872 goto error;
8873 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8874 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8875 dst_n_div++;
8876 dst = isl_basic_map_add_div_constraints(dst, j);
8877 if (!dst)
8878 goto error;
8880 if (j != i)
8881 dst = isl_basic_map_swap_div(dst, i, j);
8882 if (!dst)
8883 goto error;
8885 isl_basic_map_free(src);
8886 return dst;
8887 error:
8888 isl_basic_map_free(src);
8889 isl_basic_map_free(dst);
8890 return NULL;
8893 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8895 int i;
8897 if (!map)
8898 return NULL;
8899 if (map->n == 0)
8900 return map;
8901 map = isl_map_compute_divs(map);
8902 map = isl_map_cow(map);
8903 if (!map)
8904 return NULL;
8906 for (i = 1; i < map->n; ++i)
8907 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8908 for (i = 1; i < map->n; ++i) {
8909 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8910 if (!map->p[i])
8911 return isl_map_free(map);
8914 map = isl_map_unmark_normalized(map);
8915 return map;
8918 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8920 return isl_map_align_divs_internal(map);
8923 struct isl_set *isl_set_align_divs(struct isl_set *set)
8925 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8928 /* Align the divs of the basic maps in "map" to those
8929 * of the basic maps in "list", as well as to the other basic maps in "map".
8930 * The elements in "list" are assumed to have known divs.
8932 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8933 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8935 int i, n;
8937 map = isl_map_compute_divs(map);
8938 map = isl_map_cow(map);
8939 if (!map || !list)
8940 return isl_map_free(map);
8941 if (map->n == 0)
8942 return map;
8944 n = isl_basic_map_list_n_basic_map(list);
8945 for (i = 0; i < n; ++i) {
8946 isl_basic_map *bmap;
8948 bmap = isl_basic_map_list_get_basic_map(list, i);
8949 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8950 isl_basic_map_free(bmap);
8952 if (!map->p[0])
8953 return isl_map_free(map);
8955 return isl_map_align_divs_internal(map);
8958 /* Align the divs of each element of "list" to those of "bmap".
8959 * Both "bmap" and the elements of "list" are assumed to have known divs.
8961 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8962 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8964 int i, n;
8966 if (!list || !bmap)
8967 return isl_basic_map_list_free(list);
8969 n = isl_basic_map_list_n_basic_map(list);
8970 for (i = 0; i < n; ++i) {
8971 isl_basic_map *bmap_i;
8973 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8974 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8975 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8978 return list;
8981 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8982 __isl_take isl_map *map)
8984 isl_bool ok;
8986 ok = isl_map_compatible_domain(map, set);
8987 if (ok < 0)
8988 goto error;
8989 if (!ok)
8990 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8991 "incompatible spaces", goto error);
8992 map = isl_map_intersect_domain(map, set);
8993 set = isl_map_range(map);
8994 return set;
8995 error:
8996 isl_set_free(set);
8997 isl_map_free(map);
8998 return NULL;
9001 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9002 __isl_take isl_map *map)
9004 return isl_map_align_params_map_map_and(set, map, &set_apply);
9007 /* There is no need to cow as removing empty parts doesn't change
9008 * the meaning of the set.
9010 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9012 int i;
9014 if (!map)
9015 return NULL;
9017 for (i = map->n - 1; i >= 0; --i)
9018 map = remove_if_empty(map, i);
9020 return map;
9023 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9025 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9028 /* Create a binary relation that maps the shared initial "pos" dimensions
9029 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9031 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9032 __isl_keep isl_basic_set *bset2, int pos)
9034 isl_basic_map *bmap1;
9035 isl_basic_map *bmap2;
9037 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9038 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9039 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9040 isl_dim_out, 0, pos);
9041 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9042 isl_dim_out, 0, pos);
9043 return isl_basic_map_range_product(bmap1, bmap2);
9046 /* Given two basic sets bset1 and bset2, compute the maximal difference
9047 * between the values of dimension pos in bset1 and those in bset2
9048 * for any common value of the parameters and dimensions preceding pos.
9050 static enum isl_lp_result basic_set_maximal_difference_at(
9051 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9052 int pos, isl_int *opt)
9054 isl_basic_map *bmap1;
9055 struct isl_ctx *ctx;
9056 struct isl_vec *obj;
9057 unsigned total;
9058 unsigned nparam;
9059 unsigned dim1;
9060 enum isl_lp_result res;
9062 if (!bset1 || !bset2)
9063 return isl_lp_error;
9065 nparam = isl_basic_set_n_param(bset1);
9066 dim1 = isl_basic_set_n_dim(bset1);
9068 bmap1 = join_initial(bset1, bset2, pos);
9069 if (!bmap1)
9070 return isl_lp_error;
9072 total = isl_basic_map_total_dim(bmap1);
9073 ctx = bmap1->ctx;
9074 obj = isl_vec_alloc(ctx, 1 + total);
9075 if (!obj)
9076 goto error;
9077 isl_seq_clr(obj->block.data, 1 + total);
9078 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9079 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9080 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9081 opt, NULL, NULL);
9082 isl_basic_map_free(bmap1);
9083 isl_vec_free(obj);
9084 return res;
9085 error:
9086 isl_basic_map_free(bmap1);
9087 return isl_lp_error;
9090 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9091 * for any common value of the parameters and dimensions preceding pos
9092 * in both basic sets, the values of dimension pos in bset1 are
9093 * smaller or larger than those in bset2.
9095 * Returns
9096 * 1 if bset1 follows bset2
9097 * -1 if bset1 precedes bset2
9098 * 0 if bset1 and bset2 are incomparable
9099 * -2 if some error occurred.
9101 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9102 __isl_keep isl_basic_set *bset2, int pos)
9104 isl_int opt;
9105 enum isl_lp_result res;
9106 int cmp;
9108 isl_int_init(opt);
9110 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9112 if (res == isl_lp_empty)
9113 cmp = 0;
9114 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9115 res == isl_lp_unbounded)
9116 cmp = 1;
9117 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9118 cmp = -1;
9119 else
9120 cmp = -2;
9122 isl_int_clear(opt);
9123 return cmp;
9126 /* Given two basic sets bset1 and bset2, check whether
9127 * for any common value of the parameters and dimensions preceding pos
9128 * there is a value of dimension pos in bset1 that is larger
9129 * than a value of the same dimension in bset2.
9131 * Return
9132 * 1 if there exists such a pair
9133 * 0 if there is no such pair, but there is a pair of equal values
9134 * -1 otherwise
9135 * -2 if some error occurred.
9137 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9138 __isl_keep isl_basic_set *bset2, int pos)
9140 isl_bool empty;
9141 isl_basic_map *bmap;
9142 unsigned dim1;
9144 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9145 bmap = join_initial(bset1, bset2, pos);
9146 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9147 isl_dim_out, dim1 - pos);
9148 empty = isl_basic_map_is_empty(bmap);
9149 if (empty < 0)
9150 goto error;
9151 if (empty) {
9152 isl_basic_map_free(bmap);
9153 return -1;
9155 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9156 isl_dim_out, dim1 - pos);
9157 empty = isl_basic_map_is_empty(bmap);
9158 if (empty < 0)
9159 goto error;
9160 isl_basic_map_free(bmap);
9161 if (empty)
9162 return 0;
9163 return 1;
9164 error:
9165 isl_basic_map_free(bmap);
9166 return -2;
9169 /* Given two sets set1 and set2, check whether
9170 * for any common value of the parameters and dimensions preceding pos
9171 * there is a value of dimension pos in set1 that is larger
9172 * than a value of the same dimension in set2.
9174 * Return
9175 * 1 if there exists such a pair
9176 * 0 if there is no such pair, but there is a pair of equal values
9177 * -1 otherwise
9178 * -2 if some error occurred.
9180 int isl_set_follows_at(__isl_keep isl_set *set1,
9181 __isl_keep isl_set *set2, int pos)
9183 int i, j;
9184 int follows = -1;
9186 if (!set1 || !set2)
9187 return -2;
9189 for (i = 0; i < set1->n; ++i)
9190 for (j = 0; j < set2->n; ++j) {
9191 int f;
9192 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9193 if (f == 1 || f == -2)
9194 return f;
9195 if (f > follows)
9196 follows = f;
9199 return follows;
9202 static isl_bool isl_basic_map_plain_has_fixed_var(
9203 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9205 int i;
9206 int d;
9207 unsigned total;
9209 if (!bmap)
9210 return isl_bool_error;
9211 total = isl_basic_map_total_dim(bmap);
9212 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9213 for (; d+1 > pos; --d)
9214 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9215 break;
9216 if (d != pos)
9217 continue;
9218 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9219 return isl_bool_false;
9220 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9221 return isl_bool_false;
9222 if (!isl_int_is_one(bmap->eq[i][1+d]))
9223 return isl_bool_false;
9224 if (val)
9225 isl_int_neg(*val, bmap->eq[i][0]);
9226 return isl_bool_true;
9228 return isl_bool_false;
9231 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9232 unsigned pos, isl_int *val)
9234 int i;
9235 isl_int v;
9236 isl_int tmp;
9237 isl_bool fixed;
9239 if (!map)
9240 return isl_bool_error;
9241 if (map->n == 0)
9242 return isl_bool_false;
9243 if (map->n == 1)
9244 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9245 isl_int_init(v);
9246 isl_int_init(tmp);
9247 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9248 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9249 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9250 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9251 fixed = isl_bool_false;
9253 if (val)
9254 isl_int_set(*val, v);
9255 isl_int_clear(tmp);
9256 isl_int_clear(v);
9257 return fixed;
9260 static isl_bool isl_basic_set_plain_has_fixed_var(
9261 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9263 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9264 pos, val);
9267 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9268 enum isl_dim_type type, unsigned pos, isl_int *val)
9270 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9271 return isl_bool_error;
9272 return isl_basic_map_plain_has_fixed_var(bmap,
9273 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9276 /* If "bmap" obviously lies on a hyperplane where the given dimension
9277 * has a fixed value, then return that value.
9278 * Otherwise return NaN.
9280 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9281 __isl_keep isl_basic_map *bmap,
9282 enum isl_dim_type type, unsigned pos)
9284 isl_ctx *ctx;
9285 isl_val *v;
9286 isl_bool fixed;
9288 if (!bmap)
9289 return NULL;
9290 ctx = isl_basic_map_get_ctx(bmap);
9291 v = isl_val_alloc(ctx);
9292 if (!v)
9293 return NULL;
9294 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9295 if (fixed < 0)
9296 return isl_val_free(v);
9297 if (fixed) {
9298 isl_int_set_si(v->d, 1);
9299 return v;
9301 isl_val_free(v);
9302 return isl_val_nan(ctx);
9305 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9306 enum isl_dim_type type, unsigned pos, isl_int *val)
9308 if (isl_map_check_range(map, type, pos, 1) < 0)
9309 return isl_bool_error;
9310 return isl_map_plain_has_fixed_var(map,
9311 map_offset(map, type) - 1 + pos, val);
9314 /* If "map" obviously lies on a hyperplane where the given dimension
9315 * has a fixed value, then return that value.
9316 * Otherwise return NaN.
9318 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9319 enum isl_dim_type type, unsigned pos)
9321 isl_ctx *ctx;
9322 isl_val *v;
9323 isl_bool fixed;
9325 if (!map)
9326 return NULL;
9327 ctx = isl_map_get_ctx(map);
9328 v = isl_val_alloc(ctx);
9329 if (!v)
9330 return NULL;
9331 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9332 if (fixed < 0)
9333 return isl_val_free(v);
9334 if (fixed) {
9335 isl_int_set_si(v->d, 1);
9336 return v;
9338 isl_val_free(v);
9339 return isl_val_nan(ctx);
9342 /* If "set" obviously lies on a hyperplane where the given dimension
9343 * has a fixed value, then return that value.
9344 * Otherwise return NaN.
9346 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9347 enum isl_dim_type type, unsigned pos)
9349 return isl_map_plain_get_val_if_fixed(set, type, pos);
9352 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9353 * then return this fixed value in *val.
9355 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9356 unsigned dim, isl_int *val)
9358 return isl_basic_set_plain_has_fixed_var(bset,
9359 isl_basic_set_n_param(bset) + dim, val);
9362 /* Return -1 if the constraint "c1" should be sorted before "c2"
9363 * and 1 if it should be sorted after "c2".
9364 * Return 0 if the two constraints are the same (up to the constant term).
9366 * In particular, if a constraint involves later variables than another
9367 * then it is sorted after this other constraint.
9368 * uset_gist depends on constraints without existentially quantified
9369 * variables sorting first.
9371 * For constraints that have the same latest variable, those
9372 * with the same coefficient for this latest variable (first in absolute value
9373 * and then in actual value) are grouped together.
9374 * This is useful for detecting pairs of constraints that can
9375 * be chained in their printed representation.
9377 * Finally, within a group, constraints are sorted according to
9378 * their coefficients (excluding the constant term).
9380 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9382 isl_int **c1 = (isl_int **) p1;
9383 isl_int **c2 = (isl_int **) p2;
9384 int l1, l2;
9385 unsigned size = *(unsigned *) arg;
9386 int cmp;
9388 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9389 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9391 if (l1 != l2)
9392 return l1 - l2;
9394 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9395 if (cmp != 0)
9396 return cmp;
9397 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9398 if (cmp != 0)
9399 return -cmp;
9401 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9404 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9405 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9406 * and 0 if the two constraints are the same (up to the constant term).
9408 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9409 isl_int *c1, isl_int *c2)
9411 unsigned total;
9413 if (!bmap)
9414 return -2;
9415 total = isl_basic_map_total_dim(bmap);
9416 return sort_constraint_cmp(&c1, &c2, &total);
9419 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9420 __isl_take isl_basic_map *bmap)
9422 unsigned total;
9424 if (!bmap)
9425 return NULL;
9426 if (bmap->n_ineq == 0)
9427 return bmap;
9428 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9429 return bmap;
9430 total = isl_basic_map_total_dim(bmap);
9431 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9432 &sort_constraint_cmp, &total) < 0)
9433 return isl_basic_map_free(bmap);
9434 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9435 return bmap;
9438 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9439 __isl_take isl_basic_set *bset)
9441 isl_basic_map *bmap = bset_to_bmap(bset);
9442 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9445 __isl_give isl_basic_map *isl_basic_map_normalize(
9446 __isl_take isl_basic_map *bmap)
9448 bmap = isl_basic_map_remove_redundancies(bmap);
9449 bmap = isl_basic_map_sort_constraints(bmap);
9450 return bmap;
9452 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9453 __isl_keep isl_basic_map *bmap2)
9455 int i, cmp;
9456 unsigned total;
9457 isl_space *space1, *space2;
9459 if (!bmap1 || !bmap2)
9460 return -1;
9462 if (bmap1 == bmap2)
9463 return 0;
9464 space1 = isl_basic_map_peek_space(bmap1);
9465 space2 = isl_basic_map_peek_space(bmap2);
9466 cmp = isl_space_cmp(space1, space2);
9467 if (cmp)
9468 return cmp;
9469 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9470 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9471 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9472 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9473 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9474 return 0;
9475 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9476 return 1;
9477 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9478 return -1;
9479 if (bmap1->n_eq != bmap2->n_eq)
9480 return bmap1->n_eq - bmap2->n_eq;
9481 if (bmap1->n_ineq != bmap2->n_ineq)
9482 return bmap1->n_ineq - bmap2->n_ineq;
9483 if (bmap1->n_div != bmap2->n_div)
9484 return bmap1->n_div - bmap2->n_div;
9485 total = isl_basic_map_total_dim(bmap1);
9486 for (i = 0; i < bmap1->n_eq; ++i) {
9487 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9488 if (cmp)
9489 return cmp;
9491 for (i = 0; i < bmap1->n_ineq; ++i) {
9492 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9493 if (cmp)
9494 return cmp;
9496 for (i = 0; i < bmap1->n_div; ++i) {
9497 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9498 if (cmp)
9499 return cmp;
9501 return 0;
9504 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9505 __isl_keep isl_basic_set *bset2)
9507 return isl_basic_map_plain_cmp(bset1, bset2);
9510 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9512 int i, cmp;
9514 if (set1 == set2)
9515 return 0;
9516 if (set1->n != set2->n)
9517 return set1->n - set2->n;
9519 for (i = 0; i < set1->n; ++i) {
9520 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9521 if (cmp)
9522 return cmp;
9525 return 0;
9528 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9529 __isl_keep isl_basic_map *bmap2)
9531 if (!bmap1 || !bmap2)
9532 return isl_bool_error;
9533 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9536 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9537 __isl_keep isl_basic_set *bset2)
9539 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9540 bset_to_bmap(bset2));
9543 static int qsort_bmap_cmp(const void *p1, const void *p2)
9545 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9546 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9548 return isl_basic_map_plain_cmp(bmap1, bmap2);
9551 /* Sort the basic maps of "map" and remove duplicate basic maps.
9553 * While removing basic maps, we make sure that the basic maps remain
9554 * sorted because isl_map_normalize expects the basic maps of the result
9555 * to be sorted.
9557 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9559 int i, j;
9561 map = isl_map_remove_empty_parts(map);
9562 if (!map)
9563 return NULL;
9564 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9565 for (i = map->n - 1; i >= 1; --i) {
9566 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9567 continue;
9568 isl_basic_map_free(map->p[i-1]);
9569 for (j = i; j < map->n; ++j)
9570 map->p[j - 1] = map->p[j];
9571 map->n--;
9574 return map;
9577 /* Remove obvious duplicates among the basic maps of "map".
9579 * Unlike isl_map_normalize, this function does not remove redundant
9580 * constraints and only removes duplicates that have exactly the same
9581 * constraints in the input. It does sort the constraints and
9582 * the basic maps to ease the detection of duplicates.
9584 * If "map" has already been normalized or if the basic maps are
9585 * disjoint, then there can be no duplicates.
9587 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9589 int i;
9590 isl_basic_map *bmap;
9592 if (!map)
9593 return NULL;
9594 if (map->n <= 1)
9595 return map;
9596 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9597 return map;
9598 for (i = 0; i < map->n; ++i) {
9599 bmap = isl_basic_map_copy(map->p[i]);
9600 bmap = isl_basic_map_sort_constraints(bmap);
9601 if (!bmap)
9602 return isl_map_free(map);
9603 isl_basic_map_free(map->p[i]);
9604 map->p[i] = bmap;
9607 map = sort_and_remove_duplicates(map);
9608 return map;
9611 /* We normalize in place, but if anything goes wrong we need
9612 * to return NULL, so we need to make sure we don't change the
9613 * meaning of any possible other copies of map.
9615 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9617 int i;
9618 struct isl_basic_map *bmap;
9620 if (!map)
9621 return NULL;
9622 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9623 return map;
9624 for (i = 0; i < map->n; ++i) {
9625 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9626 if (!bmap)
9627 goto error;
9628 isl_basic_map_free(map->p[i]);
9629 map->p[i] = bmap;
9632 map = sort_and_remove_duplicates(map);
9633 if (map)
9634 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9635 return map;
9636 error:
9637 isl_map_free(map);
9638 return NULL;
9641 struct isl_set *isl_set_normalize(struct isl_set *set)
9643 return set_from_map(isl_map_normalize(set_to_map(set)));
9646 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9647 __isl_keep isl_map *map2)
9649 int i;
9650 isl_bool equal;
9652 if (!map1 || !map2)
9653 return isl_bool_error;
9655 if (map1 == map2)
9656 return isl_bool_true;
9657 if (!isl_space_is_equal(map1->dim, map2->dim))
9658 return isl_bool_false;
9660 map1 = isl_map_copy(map1);
9661 map2 = isl_map_copy(map2);
9662 map1 = isl_map_normalize(map1);
9663 map2 = isl_map_normalize(map2);
9664 if (!map1 || !map2)
9665 goto error;
9666 equal = map1->n == map2->n;
9667 for (i = 0; equal && i < map1->n; ++i) {
9668 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9669 if (equal < 0)
9670 goto error;
9672 isl_map_free(map1);
9673 isl_map_free(map2);
9674 return equal;
9675 error:
9676 isl_map_free(map1);
9677 isl_map_free(map2);
9678 return isl_bool_error;
9681 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9682 __isl_keep isl_set *set2)
9684 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9687 /* Return the basic maps in "map" as a list.
9689 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9690 __isl_keep isl_map *map)
9692 int i;
9693 isl_ctx *ctx;
9694 isl_basic_map_list *list;
9696 if (!map)
9697 return NULL;
9698 ctx = isl_map_get_ctx(map);
9699 list = isl_basic_map_list_alloc(ctx, map->n);
9701 for (i = 0; i < map->n; ++i) {
9702 isl_basic_map *bmap;
9704 bmap = isl_basic_map_copy(map->p[i]);
9705 list = isl_basic_map_list_add(list, bmap);
9708 return list;
9711 /* Return the intersection of the elements in the non-empty list "list".
9712 * All elements are assumed to live in the same space.
9714 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9715 __isl_take isl_basic_map_list *list)
9717 int i, n;
9718 isl_basic_map *bmap;
9720 if (!list)
9721 return NULL;
9722 n = isl_basic_map_list_n_basic_map(list);
9723 if (n < 1)
9724 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9725 "expecting non-empty list", goto error);
9727 bmap = isl_basic_map_list_get_basic_map(list, 0);
9728 for (i = 1; i < n; ++i) {
9729 isl_basic_map *bmap_i;
9731 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9732 bmap = isl_basic_map_intersect(bmap, bmap_i);
9735 isl_basic_map_list_free(list);
9736 return bmap;
9737 error:
9738 isl_basic_map_list_free(list);
9739 return NULL;
9742 /* Return the intersection of the elements in the non-empty list "list".
9743 * All elements are assumed to live in the same space.
9745 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9746 __isl_take isl_basic_set_list *list)
9748 return isl_basic_map_list_intersect(list);
9751 /* Return the union of the elements of "list".
9752 * The list is required to have at least one element.
9754 __isl_give isl_set *isl_basic_set_list_union(
9755 __isl_take isl_basic_set_list *list)
9757 int i, n;
9758 isl_space *space;
9759 isl_basic_set *bset;
9760 isl_set *set;
9762 if (!list)
9763 return NULL;
9764 n = isl_basic_set_list_n_basic_set(list);
9765 if (n < 1)
9766 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9767 "expecting non-empty list", goto error);
9769 bset = isl_basic_set_list_get_basic_set(list, 0);
9770 space = isl_basic_set_get_space(bset);
9771 isl_basic_set_free(bset);
9773 set = isl_set_alloc_space(space, n, 0);
9774 for (i = 0; i < n; ++i) {
9775 bset = isl_basic_set_list_get_basic_set(list, i);
9776 set = isl_set_add_basic_set(set, bset);
9779 isl_basic_set_list_free(list);
9780 return set;
9781 error:
9782 isl_basic_set_list_free(list);
9783 return NULL;
9786 /* Return the union of the elements in the non-empty list "list".
9787 * All elements are assumed to live in the same space.
9789 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9791 int i, n;
9792 isl_set *set;
9794 if (!list)
9795 return NULL;
9796 n = isl_set_list_n_set(list);
9797 if (n < 1)
9798 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9799 "expecting non-empty list", goto error);
9801 set = isl_set_list_get_set(list, 0);
9802 for (i = 1; i < n; ++i) {
9803 isl_set *set_i;
9805 set_i = isl_set_list_get_set(list, i);
9806 set = isl_set_union(set, set_i);
9809 isl_set_list_free(list);
9810 return set;
9811 error:
9812 isl_set_list_free(list);
9813 return NULL;
9816 __isl_give isl_basic_map *isl_basic_map_product(
9817 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9819 isl_space *space_result = NULL;
9820 struct isl_basic_map *bmap;
9821 unsigned in1, in2, out1, out2, nparam, total, pos;
9822 struct isl_dim_map *dim_map1, *dim_map2;
9824 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9825 goto error;
9826 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9827 isl_space_copy(bmap2->dim));
9829 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9830 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9831 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9832 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9833 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9835 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9836 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9837 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9838 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9839 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9840 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9841 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9842 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9843 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9844 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9845 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9847 bmap = isl_basic_map_alloc_space(space_result,
9848 bmap1->n_div + bmap2->n_div,
9849 bmap1->n_eq + bmap2->n_eq,
9850 bmap1->n_ineq + bmap2->n_ineq);
9851 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9852 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9853 bmap = isl_basic_map_simplify(bmap);
9854 return isl_basic_map_finalize(bmap);
9855 error:
9856 isl_basic_map_free(bmap1);
9857 isl_basic_map_free(bmap2);
9858 return NULL;
9861 __isl_give isl_basic_map *isl_basic_map_flat_product(
9862 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9864 isl_basic_map *prod;
9866 prod = isl_basic_map_product(bmap1, bmap2);
9867 prod = isl_basic_map_flatten(prod);
9868 return prod;
9871 __isl_give isl_basic_set *isl_basic_set_flat_product(
9872 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9874 return isl_basic_map_flat_range_product(bset1, bset2);
9877 __isl_give isl_basic_map *isl_basic_map_domain_product(
9878 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9880 isl_space *space_result = NULL;
9881 isl_basic_map *bmap;
9882 unsigned in1, in2, out, nparam, total, pos;
9883 struct isl_dim_map *dim_map1, *dim_map2;
9885 if (!bmap1 || !bmap2)
9886 goto error;
9888 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9889 isl_space_copy(bmap2->dim));
9891 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9892 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9893 out = isl_basic_map_dim(bmap1, isl_dim_out);
9894 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9896 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9897 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9898 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9899 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9900 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9901 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9902 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9903 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9904 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9905 isl_dim_map_div(dim_map1, bmap1, pos += out);
9906 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9908 bmap = isl_basic_map_alloc_space(space_result,
9909 bmap1->n_div + bmap2->n_div,
9910 bmap1->n_eq + bmap2->n_eq,
9911 bmap1->n_ineq + bmap2->n_ineq);
9912 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9913 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9914 bmap = isl_basic_map_simplify(bmap);
9915 return isl_basic_map_finalize(bmap);
9916 error:
9917 isl_basic_map_free(bmap1);
9918 isl_basic_map_free(bmap2);
9919 return NULL;
9922 __isl_give isl_basic_map *isl_basic_map_range_product(
9923 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9925 isl_bool rational;
9926 isl_space *space_result = NULL;
9927 isl_basic_map *bmap;
9928 unsigned in, out1, out2, nparam, total, pos;
9929 struct isl_dim_map *dim_map1, *dim_map2;
9931 rational = isl_basic_map_is_rational(bmap1);
9932 if (rational >= 0 && rational)
9933 rational = isl_basic_map_is_rational(bmap2);
9934 if (!bmap1 || !bmap2 || rational < 0)
9935 goto error;
9937 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9938 goto error;
9940 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9941 isl_space_copy(bmap2->dim));
9943 in = isl_basic_map_dim(bmap1, isl_dim_in);
9944 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9945 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9946 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9948 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9949 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9950 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9951 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9952 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9953 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9954 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9955 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9956 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9957 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9958 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9960 bmap = isl_basic_map_alloc_space(space_result,
9961 bmap1->n_div + bmap2->n_div,
9962 bmap1->n_eq + bmap2->n_eq,
9963 bmap1->n_ineq + bmap2->n_ineq);
9964 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9965 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9966 if (rational)
9967 bmap = isl_basic_map_set_rational(bmap);
9968 bmap = isl_basic_map_simplify(bmap);
9969 return isl_basic_map_finalize(bmap);
9970 error:
9971 isl_basic_map_free(bmap1);
9972 isl_basic_map_free(bmap2);
9973 return NULL;
9976 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9977 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9979 isl_basic_map *prod;
9981 prod = isl_basic_map_range_product(bmap1, bmap2);
9982 prod = isl_basic_map_flatten_range(prod);
9983 return prod;
9986 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9987 * and collect the results.
9988 * The result live in the space obtained by calling "space_product"
9989 * on the spaces of "map1" and "map2".
9990 * If "remove_duplicates" is set then the result may contain duplicates
9991 * (even if the inputs do not) and so we try and remove the obvious
9992 * duplicates.
9994 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9995 __isl_take isl_map *map2,
9996 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9997 __isl_take isl_space *right),
9998 __isl_give isl_basic_map *(*basic_map_product)(
9999 __isl_take isl_basic_map *left,
10000 __isl_take isl_basic_map *right),
10001 int remove_duplicates)
10003 unsigned flags = 0;
10004 struct isl_map *result;
10005 int i, j;
10006 isl_bool m;
10008 m = isl_map_has_equal_params(map1, map2);
10009 if (m < 0)
10010 goto error;
10011 if (!m)
10012 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10013 "parameters don't match", goto error);
10015 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10016 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10017 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10019 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10020 isl_space_copy(map2->dim)),
10021 map1->n * map2->n, flags);
10022 if (!result)
10023 goto error;
10024 for (i = 0; i < map1->n; ++i)
10025 for (j = 0; j < map2->n; ++j) {
10026 struct isl_basic_map *part;
10027 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10028 isl_basic_map_copy(map2->p[j]));
10029 if (isl_basic_map_is_empty(part))
10030 isl_basic_map_free(part);
10031 else
10032 result = isl_map_add_basic_map(result, part);
10033 if (!result)
10034 goto error;
10036 if (remove_duplicates)
10037 result = isl_map_remove_obvious_duplicates(result);
10038 isl_map_free(map1);
10039 isl_map_free(map2);
10040 return result;
10041 error:
10042 isl_map_free(map1);
10043 isl_map_free(map2);
10044 return NULL;
10047 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10049 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10050 __isl_take isl_map *map2)
10052 return map_product(map1, map2, &isl_space_product,
10053 &isl_basic_map_product, 0);
10056 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10057 __isl_take isl_map *map2)
10059 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10062 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10064 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10065 __isl_take isl_map *map2)
10067 isl_map *prod;
10069 prod = isl_map_product(map1, map2);
10070 prod = isl_map_flatten(prod);
10071 return prod;
10074 /* Given two set A and B, construct its Cartesian product A x B.
10076 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10078 return isl_map_range_product(set1, set2);
10081 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10082 __isl_take isl_set *set2)
10084 return isl_map_flat_range_product(set1, set2);
10087 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10089 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10090 __isl_take isl_map *map2)
10092 return map_product(map1, map2, &isl_space_domain_product,
10093 &isl_basic_map_domain_product, 1);
10096 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10098 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10099 __isl_take isl_map *map2)
10101 return map_product(map1, map2, &isl_space_range_product,
10102 &isl_basic_map_range_product, 1);
10105 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10106 __isl_take isl_map *map2)
10108 return isl_map_align_params_map_map_and(map1, map2,
10109 &map_domain_product_aligned);
10112 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10113 __isl_take isl_map *map2)
10115 return isl_map_align_params_map_map_and(map1, map2,
10116 &map_range_product_aligned);
10119 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10121 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10123 isl_space *space;
10124 int total1, keep1, total2, keep2;
10126 if (!map)
10127 return NULL;
10128 if (!isl_space_domain_is_wrapping(map->dim) ||
10129 !isl_space_range_is_wrapping(map->dim))
10130 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10131 "not a product", return isl_map_free(map));
10133 space = isl_map_get_space(map);
10134 total1 = isl_space_dim(space, isl_dim_in);
10135 total2 = isl_space_dim(space, isl_dim_out);
10136 space = isl_space_factor_domain(space);
10137 keep1 = isl_space_dim(space, isl_dim_in);
10138 keep2 = isl_space_dim(space, isl_dim_out);
10139 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10140 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10141 map = isl_map_reset_space(map, space);
10143 return map;
10146 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10148 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10150 isl_space *space;
10151 int total1, keep1, total2, keep2;
10153 if (!map)
10154 return NULL;
10155 if (!isl_space_domain_is_wrapping(map->dim) ||
10156 !isl_space_range_is_wrapping(map->dim))
10157 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10158 "not a product", return isl_map_free(map));
10160 space = isl_map_get_space(map);
10161 total1 = isl_space_dim(space, isl_dim_in);
10162 total2 = isl_space_dim(space, isl_dim_out);
10163 space = isl_space_factor_range(space);
10164 keep1 = isl_space_dim(space, isl_dim_in);
10165 keep2 = isl_space_dim(space, isl_dim_out);
10166 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10167 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10168 map = isl_map_reset_space(map, space);
10170 return map;
10173 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10175 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10177 isl_space *space;
10178 int total, keep;
10180 if (!map)
10181 return NULL;
10182 if (!isl_space_domain_is_wrapping(map->dim))
10183 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10184 "domain is not a product", return isl_map_free(map));
10186 space = isl_map_get_space(map);
10187 total = isl_space_dim(space, isl_dim_in);
10188 space = isl_space_domain_factor_domain(space);
10189 keep = isl_space_dim(space, isl_dim_in);
10190 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10191 map = isl_map_reset_space(map, space);
10193 return map;
10196 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10198 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10200 isl_space *space;
10201 int total, keep;
10203 if (!map)
10204 return NULL;
10205 if (!isl_space_domain_is_wrapping(map->dim))
10206 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10207 "domain is not a product", return isl_map_free(map));
10209 space = isl_map_get_space(map);
10210 total = isl_space_dim(space, isl_dim_in);
10211 space = isl_space_domain_factor_range(space);
10212 keep = isl_space_dim(space, isl_dim_in);
10213 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10214 map = isl_map_reset_space(map, space);
10216 return map;
10219 /* Given a map A -> [B -> C], extract the map A -> B.
10221 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10223 isl_space *space;
10224 int total, keep;
10226 if (!map)
10227 return NULL;
10228 if (!isl_space_range_is_wrapping(map->dim))
10229 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10230 "range is not a product", return isl_map_free(map));
10232 space = isl_map_get_space(map);
10233 total = isl_space_dim(space, isl_dim_out);
10234 space = isl_space_range_factor_domain(space);
10235 keep = isl_space_dim(space, isl_dim_out);
10236 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10237 map = isl_map_reset_space(map, space);
10239 return map;
10242 /* Given a map A -> [B -> C], extract the map A -> C.
10244 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10246 isl_space *space;
10247 int total, keep;
10249 if (!map)
10250 return NULL;
10251 if (!isl_space_range_is_wrapping(map->dim))
10252 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10253 "range is not a product", return isl_map_free(map));
10255 space = isl_map_get_space(map);
10256 total = isl_space_dim(space, isl_dim_out);
10257 space = isl_space_range_factor_range(space);
10258 keep = isl_space_dim(space, isl_dim_out);
10259 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10260 map = isl_map_reset_space(map, space);
10262 return map;
10265 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10267 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10268 __isl_take isl_map *map2)
10270 isl_map *prod;
10272 prod = isl_map_domain_product(map1, map2);
10273 prod = isl_map_flatten_domain(prod);
10274 return prod;
10277 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10279 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10280 __isl_take isl_map *map2)
10282 isl_map *prod;
10284 prod = isl_map_range_product(map1, map2);
10285 prod = isl_map_flatten_range(prod);
10286 return prod;
10289 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10291 int i;
10292 uint32_t hash = isl_hash_init();
10293 unsigned total;
10295 if (!bmap)
10296 return 0;
10297 bmap = isl_basic_map_copy(bmap);
10298 bmap = isl_basic_map_normalize(bmap);
10299 if (!bmap)
10300 return 0;
10301 total = isl_basic_map_total_dim(bmap);
10302 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10303 for (i = 0; i < bmap->n_eq; ++i) {
10304 uint32_t c_hash;
10305 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10306 isl_hash_hash(hash, c_hash);
10308 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10309 for (i = 0; i < bmap->n_ineq; ++i) {
10310 uint32_t c_hash;
10311 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10312 isl_hash_hash(hash, c_hash);
10314 isl_hash_byte(hash, bmap->n_div & 0xFF);
10315 for (i = 0; i < bmap->n_div; ++i) {
10316 uint32_t c_hash;
10317 if (isl_int_is_zero(bmap->div[i][0]))
10318 continue;
10319 isl_hash_byte(hash, i & 0xFF);
10320 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10321 isl_hash_hash(hash, c_hash);
10323 isl_basic_map_free(bmap);
10324 return hash;
10327 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10329 return isl_basic_map_get_hash(bset_to_bmap(bset));
10332 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10334 int i;
10335 uint32_t hash;
10337 if (!map)
10338 return 0;
10339 map = isl_map_copy(map);
10340 map = isl_map_normalize(map);
10341 if (!map)
10342 return 0;
10344 hash = isl_hash_init();
10345 for (i = 0; i < map->n; ++i) {
10346 uint32_t bmap_hash;
10347 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10348 isl_hash_hash(hash, bmap_hash);
10351 isl_map_free(map);
10353 return hash;
10356 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10358 return isl_map_get_hash(set_to_map(set));
10361 /* Return the number of basic maps in the (current) representation of "map".
10363 int isl_map_n_basic_map(__isl_keep isl_map *map)
10365 return map ? map->n : 0;
10368 int isl_set_n_basic_set(__isl_keep isl_set *set)
10370 return set ? set->n : 0;
10373 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10374 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10376 int i;
10378 if (!map)
10379 return isl_stat_error;
10381 for (i = 0; i < map->n; ++i)
10382 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10383 return isl_stat_error;
10385 return isl_stat_ok;
10388 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10389 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10391 int i;
10393 if (!set)
10394 return isl_stat_error;
10396 for (i = 0; i < set->n; ++i)
10397 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10398 return isl_stat_error;
10400 return isl_stat_ok;
10403 /* Return a list of basic sets, the union of which is equal to "set".
10405 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10406 __isl_keep isl_set *set)
10408 int i;
10409 isl_basic_set_list *list;
10411 if (!set)
10412 return NULL;
10414 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10415 for (i = 0; i < set->n; ++i) {
10416 isl_basic_set *bset;
10418 bset = isl_basic_set_copy(set->p[i]);
10419 list = isl_basic_set_list_add(list, bset);
10422 return list;
10425 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10427 isl_space *space;
10429 if (!bset)
10430 return NULL;
10432 bset = isl_basic_set_cow(bset);
10433 if (!bset)
10434 return NULL;
10436 space = isl_basic_set_get_space(bset);
10437 space = isl_space_lift(space, bset->n_div);
10438 if (!space)
10439 goto error;
10440 isl_space_free(bset->dim);
10441 bset->dim = space;
10442 bset->extra -= bset->n_div;
10443 bset->n_div = 0;
10445 bset = isl_basic_set_finalize(bset);
10447 return bset;
10448 error:
10449 isl_basic_set_free(bset);
10450 return NULL;
10453 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10455 int i;
10456 isl_space *space;
10457 unsigned n_div;
10459 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10461 if (!set)
10462 return NULL;
10464 set = isl_set_cow(set);
10465 if (!set)
10466 return NULL;
10468 n_div = set->p[0]->n_div;
10469 space = isl_set_get_space(set);
10470 space = isl_space_lift(space, n_div);
10471 if (!space)
10472 goto error;
10473 isl_space_free(set->dim);
10474 set->dim = space;
10476 for (i = 0; i < set->n; ++i) {
10477 set->p[i] = isl_basic_set_lift(set->p[i]);
10478 if (!set->p[i])
10479 goto error;
10482 return set;
10483 error:
10484 isl_set_free(set);
10485 return NULL;
10488 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10490 unsigned dim;
10491 int size = 0;
10493 if (!bset)
10494 return -1;
10496 dim = isl_basic_set_total_dim(bset);
10497 size += bset->n_eq * (1 + dim);
10498 size += bset->n_ineq * (1 + dim);
10499 size += bset->n_div * (2 + dim);
10501 return size;
10504 int isl_set_size(__isl_keep isl_set *set)
10506 int i;
10507 int size = 0;
10509 if (!set)
10510 return -1;
10512 for (i = 0; i < set->n; ++i)
10513 size += isl_basic_set_size(set->p[i]);
10515 return size;
10518 /* Check if there is any lower bound (if lower == 0) and/or upper
10519 * bound (if upper == 0) on the specified dim.
10521 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10522 enum isl_dim_type type, unsigned pos, int lower, int upper)
10524 int i;
10526 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10527 return isl_bool_error;
10529 pos += isl_basic_map_offset(bmap, type);
10531 for (i = 0; i < bmap->n_div; ++i) {
10532 if (isl_int_is_zero(bmap->div[i][0]))
10533 continue;
10534 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10535 return isl_bool_true;
10538 for (i = 0; i < bmap->n_eq; ++i)
10539 if (!isl_int_is_zero(bmap->eq[i][pos]))
10540 return isl_bool_true;
10542 for (i = 0; i < bmap->n_ineq; ++i) {
10543 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10544 if (sgn > 0)
10545 lower = 1;
10546 if (sgn < 0)
10547 upper = 1;
10550 return lower && upper;
10553 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10554 enum isl_dim_type type, unsigned pos)
10556 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10559 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10560 enum isl_dim_type type, unsigned pos)
10562 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10565 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10566 enum isl_dim_type type, unsigned pos)
10568 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10571 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10572 enum isl_dim_type type, unsigned pos)
10574 int i;
10576 if (!map)
10577 return isl_bool_error;
10579 for (i = 0; i < map->n; ++i) {
10580 isl_bool bounded;
10581 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10582 if (bounded < 0 || !bounded)
10583 return bounded;
10586 return isl_bool_true;
10589 /* Return true if the specified dim is involved in both an upper bound
10590 * and a lower bound.
10592 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10593 enum isl_dim_type type, unsigned pos)
10595 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10598 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10600 static isl_bool has_any_bound(__isl_keep isl_map *map,
10601 enum isl_dim_type type, unsigned pos,
10602 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10603 enum isl_dim_type type, unsigned pos))
10605 int i;
10607 if (!map)
10608 return isl_bool_error;
10610 for (i = 0; i < map->n; ++i) {
10611 isl_bool bounded;
10612 bounded = fn(map->p[i], type, pos);
10613 if (bounded < 0 || bounded)
10614 return bounded;
10617 return isl_bool_false;
10620 /* Return 1 if the specified dim is involved in any lower bound.
10622 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10623 enum isl_dim_type type, unsigned pos)
10625 return has_any_bound(set, type, pos,
10626 &isl_basic_map_dim_has_lower_bound);
10629 /* Return 1 if the specified dim is involved in any upper bound.
10631 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10632 enum isl_dim_type type, unsigned pos)
10634 return has_any_bound(set, type, pos,
10635 &isl_basic_map_dim_has_upper_bound);
10638 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10640 static isl_bool has_bound(__isl_keep isl_map *map,
10641 enum isl_dim_type type, unsigned pos,
10642 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10643 enum isl_dim_type type, unsigned pos))
10645 int i;
10647 if (!map)
10648 return isl_bool_error;
10650 for (i = 0; i < map->n; ++i) {
10651 isl_bool bounded;
10652 bounded = fn(map->p[i], type, pos);
10653 if (bounded < 0 || !bounded)
10654 return bounded;
10657 return isl_bool_true;
10660 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10662 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10663 enum isl_dim_type type, unsigned pos)
10665 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10668 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10670 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10671 enum isl_dim_type type, unsigned pos)
10673 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10676 /* For each of the "n" variables starting at "first", determine
10677 * the sign of the variable and put the results in the first "n"
10678 * elements of the array "signs".
10679 * Sign
10680 * 1 means that the variable is non-negative
10681 * -1 means that the variable is non-positive
10682 * 0 means the variable attains both positive and negative values.
10684 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10685 unsigned first, unsigned n, int *signs)
10687 isl_vec *bound = NULL;
10688 struct isl_tab *tab = NULL;
10689 struct isl_tab_undo *snap;
10690 int i;
10692 if (!bset || !signs)
10693 return isl_stat_error;
10695 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10696 tab = isl_tab_from_basic_set(bset, 0);
10697 if (!bound || !tab)
10698 goto error;
10700 isl_seq_clr(bound->el, bound->size);
10701 isl_int_set_si(bound->el[0], -1);
10703 snap = isl_tab_snap(tab);
10704 for (i = 0; i < n; ++i) {
10705 int empty;
10707 isl_int_set_si(bound->el[1 + first + i], -1);
10708 if (isl_tab_add_ineq(tab, bound->el) < 0)
10709 goto error;
10710 empty = tab->empty;
10711 isl_int_set_si(bound->el[1 + first + i], 0);
10712 if (isl_tab_rollback(tab, snap) < 0)
10713 goto error;
10715 if (empty) {
10716 signs[i] = 1;
10717 continue;
10720 isl_int_set_si(bound->el[1 + first + i], 1);
10721 if (isl_tab_add_ineq(tab, bound->el) < 0)
10722 goto error;
10723 empty = tab->empty;
10724 isl_int_set_si(bound->el[1 + first + i], 0);
10725 if (isl_tab_rollback(tab, snap) < 0)
10726 goto error;
10728 signs[i] = empty ? -1 : 0;
10731 isl_tab_free(tab);
10732 isl_vec_free(bound);
10733 return isl_stat_ok;
10734 error:
10735 isl_tab_free(tab);
10736 isl_vec_free(bound);
10737 return isl_stat_error;
10740 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10741 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10743 if (!bset || !signs)
10744 return isl_stat_error;
10745 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10746 return isl_stat_error;
10748 first += pos(bset->dim, type) - 1;
10749 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10752 /* Is it possible for the integer division "div" to depend (possibly
10753 * indirectly) on any output dimensions?
10755 * If the div is undefined, then we conservatively assume that it
10756 * may depend on them.
10757 * Otherwise, we check if it actually depends on them or on any integer
10758 * divisions that may depend on them.
10760 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10762 int i;
10763 unsigned n_out, o_out;
10764 unsigned n_div, o_div;
10766 if (isl_int_is_zero(bmap->div[div][0]))
10767 return isl_bool_true;
10769 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10770 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10772 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10773 return isl_bool_true;
10775 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10776 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10778 for (i = 0; i < n_div; ++i) {
10779 isl_bool may_involve;
10781 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10782 continue;
10783 may_involve = div_may_involve_output(bmap, i);
10784 if (may_involve < 0 || may_involve)
10785 return may_involve;
10788 return isl_bool_false;
10791 /* Return the first integer division of "bmap" in the range
10792 * [first, first + n[ that may depend on any output dimensions and
10793 * that has a non-zero coefficient in "c" (where the first coefficient
10794 * in "c" corresponds to integer division "first").
10796 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10797 isl_int *c, int first, int n)
10799 int k;
10801 if (!bmap)
10802 return -1;
10804 for (k = first; k < first + n; ++k) {
10805 isl_bool may_involve;
10807 if (isl_int_is_zero(c[k]))
10808 continue;
10809 may_involve = div_may_involve_output(bmap, k);
10810 if (may_involve < 0)
10811 return -1;
10812 if (may_involve)
10813 return k;
10816 return first + n;
10819 /* Look for a pair of inequality constraints in "bmap" of the form
10821 * -l + i >= 0 or i >= l
10822 * and
10823 * n + l - i >= 0 or i <= l + n
10825 * with n < "m" and i the output dimension at position "pos".
10826 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10827 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10828 * and earlier output dimensions, as well as integer divisions that do
10829 * not involve any of the output dimensions.
10831 * Return the index of the first inequality constraint or bmap->n_ineq
10832 * if no such pair can be found.
10834 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10835 int pos, isl_int m)
10837 int i, j;
10838 isl_ctx *ctx;
10839 unsigned total;
10840 unsigned n_div, o_div;
10841 unsigned n_out, o_out;
10842 int less;
10844 if (!bmap)
10845 return -1;
10847 ctx = isl_basic_map_get_ctx(bmap);
10848 total = isl_basic_map_total_dim(bmap);
10849 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10850 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10851 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10852 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10853 for (i = 0; i < bmap->n_ineq; ++i) {
10854 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10855 continue;
10856 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10857 n_out - (pos + 1)) != -1)
10858 continue;
10859 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10860 0, n_div) < n_div)
10861 continue;
10862 for (j = i + 1; j < bmap->n_ineq; ++j) {
10863 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10864 ctx->one))
10865 continue;
10866 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10867 bmap->ineq[j] + 1, total))
10868 continue;
10869 break;
10871 if (j >= bmap->n_ineq)
10872 continue;
10873 isl_int_add(bmap->ineq[i][0],
10874 bmap->ineq[i][0], bmap->ineq[j][0]);
10875 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10876 isl_int_sub(bmap->ineq[i][0],
10877 bmap->ineq[i][0], bmap->ineq[j][0]);
10878 if (!less)
10879 continue;
10880 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10881 return i;
10882 else
10883 return j;
10886 return bmap->n_ineq;
10889 /* Return the index of the equality of "bmap" that defines
10890 * the output dimension "pos" in terms of earlier dimensions.
10891 * The equality may also involve integer divisions, as long
10892 * as those integer divisions are defined in terms of
10893 * parameters or input dimensions.
10894 * In this case, *div is set to the number of integer divisions and
10895 * *ineq is set to the number of inequality constraints (provided
10896 * div and ineq are not NULL).
10898 * The equality may also involve a single integer division involving
10899 * the output dimensions (typically only output dimension "pos") as
10900 * long as the coefficient of output dimension "pos" is 1 or -1 and
10901 * there is a pair of constraints i >= l and i <= l + n, with i referring
10902 * to output dimension "pos", l an expression involving only earlier
10903 * dimensions and n smaller than the coefficient of the integer division
10904 * in the equality. In this case, the output dimension can be defined
10905 * in terms of a modulo expression that does not involve the integer division.
10906 * *div is then set to this single integer division and
10907 * *ineq is set to the index of constraint i >= l.
10909 * Return bmap->n_eq if there is no such equality.
10910 * Return -1 on error.
10912 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10913 int pos, int *div, int *ineq)
10915 int j, k, l;
10916 unsigned n_out, o_out;
10917 unsigned n_div, o_div;
10919 if (!bmap)
10920 return -1;
10922 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10923 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10924 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10925 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10927 if (ineq)
10928 *ineq = bmap->n_ineq;
10929 if (div)
10930 *div = n_div;
10931 for (j = 0; j < bmap->n_eq; ++j) {
10932 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10933 continue;
10934 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10935 n_out - (pos + 1)) != -1)
10936 continue;
10937 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10938 0, n_div);
10939 if (k >= n_div)
10940 return j;
10941 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10942 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10943 continue;
10944 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10945 k + 1, n_div - (k+1)) < n_div)
10946 continue;
10947 l = find_modulo_constraint_pair(bmap, pos,
10948 bmap->eq[j][o_div + k]);
10949 if (l < 0)
10950 return -1;
10951 if (l >= bmap->n_ineq)
10952 continue;
10953 if (div)
10954 *div = k;
10955 if (ineq)
10956 *ineq = l;
10957 return j;
10960 return bmap->n_eq;
10963 /* Check if the given basic map is obviously single-valued.
10964 * In particular, for each output dimension, check that there is
10965 * an equality that defines the output dimension in terms of
10966 * earlier dimensions.
10968 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10970 int i;
10971 unsigned n_out;
10973 if (!bmap)
10974 return isl_bool_error;
10976 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10978 for (i = 0; i < n_out; ++i) {
10979 int eq;
10981 eq = isl_basic_map_output_defining_equality(bmap, i,
10982 NULL, NULL);
10983 if (eq < 0)
10984 return isl_bool_error;
10985 if (eq >= bmap->n_eq)
10986 return isl_bool_false;
10989 return isl_bool_true;
10992 /* Check if the given basic map is single-valued.
10993 * We simply compute
10995 * M \circ M^-1
10997 * and check if the result is a subset of the identity mapping.
10999 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11001 isl_space *space;
11002 isl_basic_map *test;
11003 isl_basic_map *id;
11004 isl_bool sv;
11006 sv = isl_basic_map_plain_is_single_valued(bmap);
11007 if (sv < 0 || sv)
11008 return sv;
11010 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11011 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11013 space = isl_basic_map_get_space(bmap);
11014 space = isl_space_map_from_set(isl_space_range(space));
11015 id = isl_basic_map_identity(space);
11017 sv = isl_basic_map_is_subset(test, id);
11019 isl_basic_map_free(test);
11020 isl_basic_map_free(id);
11022 return sv;
11025 /* Check if the given map is obviously single-valued.
11027 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11029 if (!map)
11030 return isl_bool_error;
11031 if (map->n == 0)
11032 return isl_bool_true;
11033 if (map->n >= 2)
11034 return isl_bool_false;
11036 return isl_basic_map_plain_is_single_valued(map->p[0]);
11039 /* Check if the given map is single-valued.
11040 * We simply compute
11042 * M \circ M^-1
11044 * and check if the result is a subset of the identity mapping.
11046 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11048 isl_space *dim;
11049 isl_map *test;
11050 isl_map *id;
11051 isl_bool sv;
11053 sv = isl_map_plain_is_single_valued(map);
11054 if (sv < 0 || sv)
11055 return sv;
11057 test = isl_map_reverse(isl_map_copy(map));
11058 test = isl_map_apply_range(test, isl_map_copy(map));
11060 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11061 id = isl_map_identity(dim);
11063 sv = isl_map_is_subset(test, id);
11065 isl_map_free(test);
11066 isl_map_free(id);
11068 return sv;
11071 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11073 isl_bool in;
11075 map = isl_map_copy(map);
11076 map = isl_map_reverse(map);
11077 in = isl_map_is_single_valued(map);
11078 isl_map_free(map);
11080 return in;
11083 /* Check if the given map is obviously injective.
11085 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11087 isl_bool in;
11089 map = isl_map_copy(map);
11090 map = isl_map_reverse(map);
11091 in = isl_map_plain_is_single_valued(map);
11092 isl_map_free(map);
11094 return in;
11097 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11099 isl_bool sv;
11101 sv = isl_map_is_single_valued(map);
11102 if (sv < 0 || !sv)
11103 return sv;
11105 return isl_map_is_injective(map);
11108 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11110 return isl_map_is_single_valued(set_to_map(set));
11113 /* Does "map" only map elements to themselves?
11115 * If the domain and range spaces are different, then "map"
11116 * is considered not to be an identity relation, even if it is empty.
11117 * Otherwise, construct the maximal identity relation and
11118 * check whether "map" is a subset of this relation.
11120 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11122 isl_space *space;
11123 isl_map *id;
11124 isl_bool equal, is_identity;
11126 space = isl_map_get_space(map);
11127 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11128 isl_space_free(space);
11129 if (equal < 0 || !equal)
11130 return equal;
11132 id = isl_map_identity(isl_map_get_space(map));
11133 is_identity = isl_map_is_subset(map, id);
11134 isl_map_free(id);
11136 return is_identity;
11139 int isl_map_is_translation(__isl_keep isl_map *map)
11141 int ok;
11142 isl_set *delta;
11144 delta = isl_map_deltas(isl_map_copy(map));
11145 ok = isl_set_is_singleton(delta);
11146 isl_set_free(delta);
11148 return ok;
11151 static int unique(isl_int *p, unsigned pos, unsigned len)
11153 if (isl_seq_first_non_zero(p, pos) != -1)
11154 return 0;
11155 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11156 return 0;
11157 return 1;
11160 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11162 int i, j;
11163 unsigned nvar;
11164 unsigned ovar;
11166 if (!bset)
11167 return isl_bool_error;
11169 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11170 return isl_bool_false;
11172 nvar = isl_basic_set_dim(bset, isl_dim_set);
11173 ovar = isl_space_offset(bset->dim, isl_dim_set);
11174 for (j = 0; j < nvar; ++j) {
11175 int lower = 0, upper = 0;
11176 for (i = 0; i < bset->n_eq; ++i) {
11177 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11178 continue;
11179 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11180 return isl_bool_false;
11181 break;
11183 if (i < bset->n_eq)
11184 continue;
11185 for (i = 0; i < bset->n_ineq; ++i) {
11186 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11187 continue;
11188 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11189 return isl_bool_false;
11190 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11191 lower = 1;
11192 else
11193 upper = 1;
11195 if (!lower || !upper)
11196 return isl_bool_false;
11199 return isl_bool_true;
11202 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11204 if (!set)
11205 return isl_bool_error;
11206 if (set->n != 1)
11207 return isl_bool_false;
11209 return isl_basic_set_is_box(set->p[0]);
11212 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11214 if (!bset)
11215 return isl_bool_error;
11217 return isl_space_is_wrapping(bset->dim);
11220 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11222 if (!set)
11223 return isl_bool_error;
11225 return isl_space_is_wrapping(set->dim);
11228 /* Modify the space of "map" through a call to "change".
11229 * If "can_change" is set (not NULL), then first call it to check
11230 * if the modification is allowed, printing the error message "cannot_change"
11231 * if it is not.
11233 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11234 isl_bool (*can_change)(__isl_keep isl_map *map),
11235 const char *cannot_change,
11236 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11238 isl_bool ok;
11239 isl_space *space;
11241 if (!map)
11242 return NULL;
11244 ok = can_change ? can_change(map) : isl_bool_true;
11245 if (ok < 0)
11246 return isl_map_free(map);
11247 if (!ok)
11248 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11249 return isl_map_free(map));
11251 space = change(isl_map_get_space(map));
11252 map = isl_map_reset_space(map, space);
11254 return map;
11257 /* Is the domain of "map" a wrapped relation?
11259 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11261 if (!map)
11262 return isl_bool_error;
11264 return isl_space_domain_is_wrapping(map->dim);
11267 /* Does "map" have a wrapped relation in both domain and range?
11269 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11271 return isl_space_is_product(isl_map_peek_space(map));
11274 /* Is the range of "map" a wrapped relation?
11276 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11278 if (!map)
11279 return isl_bool_error;
11281 return isl_space_range_is_wrapping(map->dim);
11284 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11286 bmap = isl_basic_map_cow(bmap);
11287 if (!bmap)
11288 return NULL;
11290 bmap->dim = isl_space_wrap(bmap->dim);
11291 if (!bmap->dim)
11292 goto error;
11294 bmap = isl_basic_map_finalize(bmap);
11296 return bset_from_bmap(bmap);
11297 error:
11298 isl_basic_map_free(bmap);
11299 return NULL;
11302 /* Given a map A -> B, return the set (A -> B).
11304 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11306 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11309 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11311 bset = isl_basic_set_cow(bset);
11312 if (!bset)
11313 return NULL;
11315 bset->dim = isl_space_unwrap(bset->dim);
11316 if (!bset->dim)
11317 goto error;
11319 bset = isl_basic_set_finalize(bset);
11321 return bset_to_bmap(bset);
11322 error:
11323 isl_basic_set_free(bset);
11324 return NULL;
11327 /* Given a set (A -> B), return the map A -> B.
11328 * Error out if "set" is not of the form (A -> B).
11330 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11332 return isl_map_change_space(set, &isl_set_is_wrapping,
11333 "not a wrapping set", &isl_space_unwrap);
11336 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11337 enum isl_dim_type type)
11339 if (!bmap)
11340 return NULL;
11342 if (!isl_space_is_named_or_nested(bmap->dim, type))
11343 return bmap;
11345 bmap = isl_basic_map_cow(bmap);
11346 if (!bmap)
11347 return NULL;
11349 bmap->dim = isl_space_reset(bmap->dim, type);
11350 if (!bmap->dim)
11351 goto error;
11353 bmap = isl_basic_map_finalize(bmap);
11355 return bmap;
11356 error:
11357 isl_basic_map_free(bmap);
11358 return NULL;
11361 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11362 enum isl_dim_type type)
11364 int i;
11366 if (!map)
11367 return NULL;
11369 if (!isl_space_is_named_or_nested(map->dim, type))
11370 return map;
11372 map = isl_map_cow(map);
11373 if (!map)
11374 return NULL;
11376 for (i = 0; i < map->n; ++i) {
11377 map->p[i] = isl_basic_map_reset(map->p[i], type);
11378 if (!map->p[i])
11379 goto error;
11381 map->dim = isl_space_reset(map->dim, type);
11382 if (!map->dim)
11383 goto error;
11385 return map;
11386 error:
11387 isl_map_free(map);
11388 return NULL;
11391 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11393 if (!bmap)
11394 return NULL;
11396 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11397 return bmap;
11399 bmap = isl_basic_map_cow(bmap);
11400 if (!bmap)
11401 return NULL;
11403 bmap->dim = isl_space_flatten(bmap->dim);
11404 if (!bmap->dim)
11405 goto error;
11407 bmap = isl_basic_map_finalize(bmap);
11409 return bmap;
11410 error:
11411 isl_basic_map_free(bmap);
11412 return NULL;
11415 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11417 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11420 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11421 __isl_take isl_basic_map *bmap)
11423 if (!bmap)
11424 return NULL;
11426 if (!bmap->dim->nested[0])
11427 return bmap;
11429 bmap = isl_basic_map_cow(bmap);
11430 if (!bmap)
11431 return NULL;
11433 bmap->dim = isl_space_flatten_domain(bmap->dim);
11434 if (!bmap->dim)
11435 goto error;
11437 bmap = isl_basic_map_finalize(bmap);
11439 return bmap;
11440 error:
11441 isl_basic_map_free(bmap);
11442 return NULL;
11445 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11446 __isl_take isl_basic_map *bmap)
11448 if (!bmap)
11449 return NULL;
11451 if (!bmap->dim->nested[1])
11452 return bmap;
11454 bmap = isl_basic_map_cow(bmap);
11455 if (!bmap)
11456 return NULL;
11458 bmap->dim = isl_space_flatten_range(bmap->dim);
11459 if (!bmap->dim)
11460 goto error;
11462 bmap = isl_basic_map_finalize(bmap);
11464 return bmap;
11465 error:
11466 isl_basic_map_free(bmap);
11467 return NULL;
11470 /* Remove any internal structure from the spaces of domain and range of "map".
11472 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11474 if (!map)
11475 return NULL;
11477 if (!map->dim->nested[0] && !map->dim->nested[1])
11478 return map;
11480 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11483 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11485 return set_from_map(isl_map_flatten(set_to_map(set)));
11488 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11490 isl_space *space, *flat_space;
11491 isl_map *map;
11493 space = isl_set_get_space(set);
11494 flat_space = isl_space_flatten(isl_space_copy(space));
11495 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11496 flat_space));
11497 map = isl_map_intersect_domain(map, set);
11499 return map;
11502 /* Remove any internal structure from the space of the domain of "map".
11504 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11506 if (!map)
11507 return NULL;
11509 if (!map->dim->nested[0])
11510 return map;
11512 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11515 /* Remove any internal structure from the space of the range of "map".
11517 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11519 if (!map)
11520 return NULL;
11522 if (!map->dim->nested[1])
11523 return map;
11525 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11528 /* Reorder the dimensions of "bmap" according to the given dim_map
11529 * and set the dimension specification to "space" and
11530 * perform Gaussian elimination on the result.
11532 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11533 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11535 isl_basic_map *res;
11536 unsigned flags;
11537 unsigned n_div;
11539 if (!bmap || !space || !dim_map)
11540 goto error;
11542 flags = bmap->flags;
11543 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11544 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11545 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11546 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11547 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11548 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11549 if (res)
11550 res->flags = flags;
11551 res = isl_basic_map_gauss(res, NULL);
11552 res = isl_basic_map_finalize(res);
11553 return res;
11554 error:
11555 isl_dim_map_free(dim_map);
11556 isl_basic_map_free(bmap);
11557 isl_space_free(space);
11558 return NULL;
11561 /* Reorder the dimensions of "map" according to given reordering.
11563 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11564 __isl_take isl_reordering *r)
11566 int i;
11567 struct isl_dim_map *dim_map;
11569 map = isl_map_cow(map);
11570 dim_map = isl_dim_map_from_reordering(r);
11571 if (!map || !r || !dim_map)
11572 goto error;
11574 for (i = 0; i < map->n; ++i) {
11575 struct isl_dim_map *dim_map_i;
11576 isl_space *space;
11578 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11580 space = isl_reordering_get_space(r);
11581 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11583 if (!map->p[i])
11584 goto error;
11587 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11588 map = isl_map_unmark_normalized(map);
11590 isl_reordering_free(r);
11591 isl_dim_map_free(dim_map);
11592 return map;
11593 error:
11594 isl_dim_map_free(dim_map);
11595 isl_map_free(map);
11596 isl_reordering_free(r);
11597 return NULL;
11600 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11601 __isl_take isl_reordering *r)
11603 return set_from_map(isl_map_realign(set_to_map(set), r));
11606 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11607 __isl_take isl_space *model)
11609 isl_ctx *ctx;
11610 isl_bool aligned;
11612 if (!map || !model)
11613 goto error;
11615 ctx = isl_space_get_ctx(model);
11616 if (!isl_space_has_named_params(model))
11617 isl_die(ctx, isl_error_invalid,
11618 "model has unnamed parameters", goto error);
11619 if (isl_map_check_named_params(map) < 0)
11620 goto error;
11621 aligned = isl_map_space_has_equal_params(map, model);
11622 if (aligned < 0)
11623 goto error;
11624 if (!aligned) {
11625 isl_reordering *exp;
11627 exp = isl_parameter_alignment_reordering(map->dim, model);
11628 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11629 map = isl_map_realign(map, exp);
11632 isl_space_free(model);
11633 return map;
11634 error:
11635 isl_space_free(model);
11636 isl_map_free(map);
11637 return NULL;
11640 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11641 __isl_take isl_space *model)
11643 return isl_map_align_params(set, model);
11646 /* Align the parameters of "bmap" to those of "model", introducing
11647 * additional parameters if needed.
11649 __isl_give isl_basic_map *isl_basic_map_align_params(
11650 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11652 isl_ctx *ctx;
11653 isl_bool equal_params;
11655 if (!bmap || !model)
11656 goto error;
11658 ctx = isl_space_get_ctx(model);
11659 if (!isl_space_has_named_params(model))
11660 isl_die(ctx, isl_error_invalid,
11661 "model has unnamed parameters", goto error);
11662 if (isl_basic_map_check_named_params(bmap) < 0)
11663 goto error;
11664 equal_params = isl_space_has_equal_params(bmap->dim, model);
11665 if (equal_params < 0)
11666 goto error;
11667 if (!equal_params) {
11668 isl_reordering *exp;
11669 struct isl_dim_map *dim_map;
11671 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11672 exp = isl_reordering_extend_space(exp,
11673 isl_basic_map_get_space(bmap));
11674 dim_map = isl_dim_map_from_reordering(exp);
11675 bmap = isl_basic_map_realign(bmap,
11676 isl_reordering_get_space(exp),
11677 isl_dim_map_extend(dim_map, bmap));
11678 isl_reordering_free(exp);
11679 isl_dim_map_free(dim_map);
11682 isl_space_free(model);
11683 return bmap;
11684 error:
11685 isl_space_free(model);
11686 isl_basic_map_free(bmap);
11687 return NULL;
11690 /* Do "bset" and "space" have the same parameters?
11692 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11693 __isl_keep isl_space *space)
11695 isl_space *bset_space;
11697 bset_space = isl_basic_set_peek_space(bset);
11698 return isl_space_has_equal_params(bset_space, space);
11701 /* Do "map" and "space" have the same parameters?
11703 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11704 __isl_keep isl_space *space)
11706 isl_space *map_space;
11708 map_space = isl_map_peek_space(map);
11709 return isl_space_has_equal_params(map_space, space);
11712 /* Do "set" and "space" have the same parameters?
11714 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11715 __isl_keep isl_space *space)
11717 return isl_map_space_has_equal_params(set_to_map(set), space);
11720 /* Align the parameters of "bset" to those of "model", introducing
11721 * additional parameters if needed.
11723 __isl_give isl_basic_set *isl_basic_set_align_params(
11724 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11726 return isl_basic_map_align_params(bset, model);
11729 /* Drop all parameters not referenced by "map".
11731 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11733 int i;
11735 if (isl_map_check_named_params(map) < 0)
11736 return isl_map_free(map);
11738 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11739 isl_bool involves;
11741 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11742 if (involves < 0)
11743 return isl_map_free(map);
11744 if (!involves)
11745 map = isl_map_project_out(map, isl_dim_param, i, 1);
11748 return map;
11751 /* Drop all parameters not referenced by "set".
11753 __isl_give isl_set *isl_set_drop_unused_params(
11754 __isl_take isl_set *set)
11756 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11759 /* Drop all parameters not referenced by "bmap".
11761 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11762 __isl_take isl_basic_map *bmap)
11764 int i;
11766 if (isl_basic_map_check_named_params(bmap) < 0)
11767 return isl_basic_map_free(bmap);
11769 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11770 isl_bool involves;
11772 involves = isl_basic_map_involves_dims(bmap,
11773 isl_dim_param, i, 1);
11774 if (involves < 0)
11775 return isl_basic_map_free(bmap);
11776 if (!involves)
11777 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11780 return bmap;
11783 /* Drop all parameters not referenced by "bset".
11785 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11786 __isl_take isl_basic_set *bset)
11788 return bset_from_bmap(isl_basic_map_drop_unused_params(
11789 bset_to_bmap(bset)));
11792 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11793 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11794 enum isl_dim_type c2, enum isl_dim_type c3,
11795 enum isl_dim_type c4, enum isl_dim_type c5)
11797 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11798 struct isl_mat *mat;
11799 int i, j, k;
11800 int pos;
11802 if (!bmap)
11803 return NULL;
11804 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11805 isl_basic_map_total_dim(bmap) + 1);
11806 if (!mat)
11807 return NULL;
11808 for (i = 0; i < bmap->n_eq; ++i)
11809 for (j = 0, pos = 0; j < 5; ++j) {
11810 int off = isl_basic_map_offset(bmap, c[j]);
11811 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11812 isl_int_set(mat->row[i][pos],
11813 bmap->eq[i][off + k]);
11814 ++pos;
11818 return mat;
11821 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11822 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11823 enum isl_dim_type c2, enum isl_dim_type c3,
11824 enum isl_dim_type c4, enum isl_dim_type c5)
11826 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11827 struct isl_mat *mat;
11828 int i, j, k;
11829 int pos;
11831 if (!bmap)
11832 return NULL;
11833 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11834 isl_basic_map_total_dim(bmap) + 1);
11835 if (!mat)
11836 return NULL;
11837 for (i = 0; i < bmap->n_ineq; ++i)
11838 for (j = 0, pos = 0; j < 5; ++j) {
11839 int off = isl_basic_map_offset(bmap, c[j]);
11840 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11841 isl_int_set(mat->row[i][pos],
11842 bmap->ineq[i][off + k]);
11843 ++pos;
11847 return mat;
11850 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11851 __isl_take isl_space *space,
11852 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11853 enum isl_dim_type c2, enum isl_dim_type c3,
11854 enum isl_dim_type c4, enum isl_dim_type c5)
11856 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11857 isl_basic_map *bmap = NULL;
11858 unsigned total;
11859 unsigned extra;
11860 int i, j, k, l;
11861 int pos;
11863 if (!space || !eq || !ineq)
11864 goto error;
11866 if (eq->n_col != ineq->n_col)
11867 isl_die(space->ctx, isl_error_invalid,
11868 "equalities and inequalities matrices should have "
11869 "same number of columns", goto error);
11871 total = 1 + isl_space_dim(space, isl_dim_all);
11873 if (eq->n_col < total)
11874 isl_die(space->ctx, isl_error_invalid,
11875 "number of columns too small", goto error);
11877 extra = eq->n_col - total;
11879 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11880 eq->n_row, ineq->n_row);
11881 if (!bmap)
11882 goto error;
11883 for (i = 0; i < extra; ++i) {
11884 k = isl_basic_map_alloc_div(bmap);
11885 if (k < 0)
11886 goto error;
11887 isl_int_set_si(bmap->div[k][0], 0);
11889 for (i = 0; i < eq->n_row; ++i) {
11890 l = isl_basic_map_alloc_equality(bmap);
11891 if (l < 0)
11892 goto error;
11893 for (j = 0, pos = 0; j < 5; ++j) {
11894 int off = isl_basic_map_offset(bmap, c[j]);
11895 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11896 isl_int_set(bmap->eq[l][off + k],
11897 eq->row[i][pos]);
11898 ++pos;
11902 for (i = 0; i < ineq->n_row; ++i) {
11903 l = isl_basic_map_alloc_inequality(bmap);
11904 if (l < 0)
11905 goto error;
11906 for (j = 0, pos = 0; j < 5; ++j) {
11907 int off = isl_basic_map_offset(bmap, c[j]);
11908 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11909 isl_int_set(bmap->ineq[l][off + k],
11910 ineq->row[i][pos]);
11911 ++pos;
11916 isl_space_free(space);
11917 isl_mat_free(eq);
11918 isl_mat_free(ineq);
11920 bmap = isl_basic_map_simplify(bmap);
11921 return isl_basic_map_finalize(bmap);
11922 error:
11923 isl_space_free(space);
11924 isl_mat_free(eq);
11925 isl_mat_free(ineq);
11926 isl_basic_map_free(bmap);
11927 return NULL;
11930 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11931 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11932 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11934 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11935 c1, c2, c3, c4, isl_dim_in);
11938 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11939 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11940 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11942 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11943 c1, c2, c3, c4, isl_dim_in);
11946 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11947 __isl_take isl_space *dim,
11948 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11949 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11951 isl_basic_map *bmap;
11952 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11953 c1, c2, c3, c4, isl_dim_in);
11954 return bset_from_bmap(bmap);
11957 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11959 if (!bmap)
11960 return isl_bool_error;
11962 return isl_space_can_zip(bmap->dim);
11965 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11967 if (!map)
11968 return isl_bool_error;
11970 return isl_space_can_zip(map->dim);
11973 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11974 * (A -> C) -> (B -> D).
11976 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11978 unsigned pos;
11979 unsigned n1;
11980 unsigned n2;
11982 if (!bmap)
11983 return NULL;
11985 if (!isl_basic_map_can_zip(bmap))
11986 isl_die(bmap->ctx, isl_error_invalid,
11987 "basic map cannot be zipped", goto error);
11988 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11989 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11990 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11991 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11992 bmap = isl_basic_map_cow(bmap);
11993 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11994 if (!bmap)
11995 return NULL;
11996 bmap->dim = isl_space_zip(bmap->dim);
11997 if (!bmap->dim)
11998 goto error;
11999 bmap = isl_basic_map_mark_final(bmap);
12000 return bmap;
12001 error:
12002 isl_basic_map_free(bmap);
12003 return NULL;
12006 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12007 * (A -> C) -> (B -> D).
12009 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12011 int i;
12013 if (!map)
12014 return NULL;
12016 if (!isl_map_can_zip(map))
12017 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12018 goto error);
12020 map = isl_map_cow(map);
12021 if (!map)
12022 return NULL;
12024 for (i = 0; i < map->n; ++i) {
12025 map->p[i] = isl_basic_map_zip(map->p[i]);
12026 if (!map->p[i])
12027 goto error;
12030 map->dim = isl_space_zip(map->dim);
12031 if (!map->dim)
12032 goto error;
12034 return map;
12035 error:
12036 isl_map_free(map);
12037 return NULL;
12040 /* Can we apply isl_basic_map_curry to "bmap"?
12041 * That is, does it have a nested relation in its domain?
12043 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12045 if (!bmap)
12046 return isl_bool_error;
12048 return isl_space_can_curry(bmap->dim);
12051 /* Can we apply isl_map_curry to "map"?
12052 * That is, does it have a nested relation in its domain?
12054 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12056 if (!map)
12057 return isl_bool_error;
12059 return isl_space_can_curry(map->dim);
12062 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12063 * A -> (B -> C).
12065 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12068 if (!bmap)
12069 return NULL;
12071 if (!isl_basic_map_can_curry(bmap))
12072 isl_die(bmap->ctx, isl_error_invalid,
12073 "basic map cannot be curried", goto error);
12074 bmap = isl_basic_map_cow(bmap);
12075 if (!bmap)
12076 return NULL;
12077 bmap->dim = isl_space_curry(bmap->dim);
12078 if (!bmap->dim)
12079 goto error;
12080 bmap = isl_basic_map_mark_final(bmap);
12081 return bmap;
12082 error:
12083 isl_basic_map_free(bmap);
12084 return NULL;
12087 /* Given a map (A -> B) -> C, return the corresponding map
12088 * A -> (B -> C).
12090 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12092 return isl_map_change_space(map, &isl_map_can_curry,
12093 "map cannot be curried", &isl_space_curry);
12096 /* Can isl_map_range_curry be applied to "map"?
12097 * That is, does it have a nested relation in its range,
12098 * the domain of which is itself a nested relation?
12100 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12102 if (!map)
12103 return isl_bool_error;
12105 return isl_space_can_range_curry(map->dim);
12108 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12109 * A -> (B -> (C -> D)).
12111 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12113 return isl_map_change_space(map, &isl_map_can_range_curry,
12114 "map range cannot be curried",
12115 &isl_space_range_curry);
12118 /* Can we apply isl_basic_map_uncurry to "bmap"?
12119 * That is, does it have a nested relation in its domain?
12121 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12123 if (!bmap)
12124 return isl_bool_error;
12126 return isl_space_can_uncurry(bmap->dim);
12129 /* Can we apply isl_map_uncurry to "map"?
12130 * That is, does it have a nested relation in its domain?
12132 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12134 if (!map)
12135 return isl_bool_error;
12137 return isl_space_can_uncurry(map->dim);
12140 /* Given a basic map A -> (B -> C), return the corresponding basic map
12141 * (A -> B) -> C.
12143 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12146 if (!bmap)
12147 return NULL;
12149 if (!isl_basic_map_can_uncurry(bmap))
12150 isl_die(bmap->ctx, isl_error_invalid,
12151 "basic map cannot be uncurried",
12152 return isl_basic_map_free(bmap));
12153 bmap = isl_basic_map_cow(bmap);
12154 if (!bmap)
12155 return NULL;
12156 bmap->dim = isl_space_uncurry(bmap->dim);
12157 if (!bmap->dim)
12158 return isl_basic_map_free(bmap);
12159 bmap = isl_basic_map_mark_final(bmap);
12160 return bmap;
12163 /* Given a map A -> (B -> C), return the corresponding map
12164 * (A -> B) -> C.
12166 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12168 return isl_map_change_space(map, &isl_map_can_uncurry,
12169 "map cannot be uncurried", &isl_space_uncurry);
12172 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12173 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12175 return isl_map_equate(set, type1, pos1, type2, pos2);
12178 /* Construct a basic map where the given dimensions are equal to each other.
12180 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12181 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12183 isl_basic_map *bmap = NULL;
12184 int i;
12186 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12187 isl_space_check_range(space, type2, pos2, 1) < 0)
12188 goto error;
12190 if (type1 == type2 && pos1 == pos2)
12191 return isl_basic_map_universe(space);
12193 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12194 i = isl_basic_map_alloc_equality(bmap);
12195 if (i < 0)
12196 goto error;
12197 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12198 pos1 += isl_basic_map_offset(bmap, type1);
12199 pos2 += isl_basic_map_offset(bmap, type2);
12200 isl_int_set_si(bmap->eq[i][pos1], -1);
12201 isl_int_set_si(bmap->eq[i][pos2], 1);
12202 bmap = isl_basic_map_finalize(bmap);
12203 isl_space_free(space);
12204 return bmap;
12205 error:
12206 isl_space_free(space);
12207 isl_basic_map_free(bmap);
12208 return NULL;
12211 /* Add a constraint imposing that the given two dimensions are equal.
12213 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12214 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12216 isl_basic_map *eq;
12218 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12220 bmap = isl_basic_map_intersect(bmap, eq);
12222 return bmap;
12225 /* Add a constraint imposing that the given two dimensions are equal.
12227 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12228 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12230 isl_basic_map *bmap;
12232 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12234 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12236 return map;
12239 /* Add a constraint imposing that the given two dimensions have opposite values.
12241 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12242 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12244 isl_basic_map *bmap = NULL;
12245 int i;
12247 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12248 return isl_map_free(map);
12249 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12250 return isl_map_free(map);
12252 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12253 i = isl_basic_map_alloc_equality(bmap);
12254 if (i < 0)
12255 goto error;
12256 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12257 pos1 += isl_basic_map_offset(bmap, type1);
12258 pos2 += isl_basic_map_offset(bmap, type2);
12259 isl_int_set_si(bmap->eq[i][pos1], 1);
12260 isl_int_set_si(bmap->eq[i][pos2], 1);
12261 bmap = isl_basic_map_finalize(bmap);
12263 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12265 return map;
12266 error:
12267 isl_basic_map_free(bmap);
12268 isl_map_free(map);
12269 return NULL;
12272 /* Construct a constraint imposing that the value of the first dimension is
12273 * greater than or equal to that of the second.
12275 static __isl_give isl_constraint *constraint_order_ge(
12276 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12277 enum isl_dim_type type2, int pos2)
12279 isl_constraint *c;
12281 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12282 isl_space_check_range(space, type2, pos2, 1) < 0)
12283 space = isl_space_free(space);
12284 if (!space)
12285 return NULL;
12287 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12289 if (type1 == type2 && pos1 == pos2)
12290 return c;
12292 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12293 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12295 return c;
12298 /* Add a constraint imposing that the value of the first dimension is
12299 * greater than or equal to that of the second.
12301 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12302 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12304 isl_constraint *c;
12305 isl_space *space;
12307 if (type1 == type2 && pos1 == pos2)
12308 return bmap;
12309 space = isl_basic_map_get_space(bmap);
12310 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12311 bmap = isl_basic_map_add_constraint(bmap, c);
12313 return bmap;
12316 /* Add a constraint imposing that the value of the first dimension is
12317 * greater than or equal to that of the second.
12319 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12320 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12322 isl_constraint *c;
12323 isl_space *space;
12325 if (type1 == type2 && pos1 == pos2)
12326 return map;
12327 space = isl_map_get_space(map);
12328 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12329 map = isl_map_add_constraint(map, c);
12331 return map;
12334 /* Add a constraint imposing that the value of the first dimension is
12335 * less than or equal to that of the second.
12337 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12338 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12340 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12343 /* Construct a basic map where the value of the first dimension is
12344 * greater than that of the second.
12346 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12347 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12349 isl_basic_map *bmap = NULL;
12350 int i;
12352 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12353 isl_space_check_range(space, type2, pos2, 1) < 0)
12354 goto error;
12356 if (type1 == type2 && pos1 == pos2)
12357 return isl_basic_map_empty(space);
12359 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12360 i = isl_basic_map_alloc_inequality(bmap);
12361 if (i < 0)
12362 return isl_basic_map_free(bmap);
12363 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12364 pos1 += isl_basic_map_offset(bmap, type1);
12365 pos2 += isl_basic_map_offset(bmap, type2);
12366 isl_int_set_si(bmap->ineq[i][pos1], 1);
12367 isl_int_set_si(bmap->ineq[i][pos2], -1);
12368 isl_int_set_si(bmap->ineq[i][0], -1);
12369 bmap = isl_basic_map_finalize(bmap);
12371 return bmap;
12372 error:
12373 isl_space_free(space);
12374 isl_basic_map_free(bmap);
12375 return NULL;
12378 /* Add a constraint imposing that the value of the first dimension is
12379 * greater than that of the second.
12381 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12382 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12384 isl_basic_map *gt;
12386 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12388 bmap = isl_basic_map_intersect(bmap, gt);
12390 return bmap;
12393 /* Add a constraint imposing that the value of the first dimension is
12394 * greater than that of the second.
12396 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12397 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12399 isl_basic_map *bmap;
12401 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12403 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12405 return map;
12408 /* Add a constraint imposing that the value of the first dimension is
12409 * smaller than that of the second.
12411 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12412 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12414 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12417 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12418 int pos)
12420 isl_aff *div;
12421 isl_local_space *ls;
12423 if (!bmap)
12424 return NULL;
12426 if (!isl_basic_map_divs_known(bmap))
12427 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12428 "some divs are unknown", return NULL);
12430 ls = isl_basic_map_get_local_space(bmap);
12431 div = isl_local_space_get_div(ls, pos);
12432 isl_local_space_free(ls);
12434 return div;
12437 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12438 int pos)
12440 return isl_basic_map_get_div(bset, pos);
12443 /* Plug in "subs" for dimension "type", "pos" of "bset".
12445 * Let i be the dimension to replace and let "subs" be of the form
12447 * f/d
12449 * Any integer division with a non-zero coefficient for i,
12451 * floor((a i + g)/m)
12453 * is replaced by
12455 * floor((a f + d g)/(m d))
12457 * Constraints of the form
12459 * a i + g
12461 * are replaced by
12463 * a f + d g
12465 * We currently require that "subs" is an integral expression.
12466 * Handling rational expressions may require us to add stride constraints
12467 * as we do in isl_basic_set_preimage_multi_aff.
12469 __isl_give isl_basic_set *isl_basic_set_substitute(
12470 __isl_take isl_basic_set *bset,
12471 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12473 int i;
12474 isl_int v;
12475 isl_ctx *ctx;
12477 if (bset && isl_basic_set_plain_is_empty(bset))
12478 return bset;
12480 bset = isl_basic_set_cow(bset);
12481 if (!bset || !subs)
12482 goto error;
12484 ctx = isl_basic_set_get_ctx(bset);
12485 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12486 isl_die(ctx, isl_error_invalid,
12487 "spaces don't match", goto error);
12488 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12489 isl_die(ctx, isl_error_unsupported,
12490 "cannot handle divs yet", goto error);
12491 if (!isl_int_is_one(subs->v->el[0]))
12492 isl_die(ctx, isl_error_invalid,
12493 "can only substitute integer expressions", goto error);
12495 pos += isl_basic_set_offset(bset, type);
12497 isl_int_init(v);
12499 for (i = 0; i < bset->n_eq; ++i) {
12500 if (isl_int_is_zero(bset->eq[i][pos]))
12501 continue;
12502 isl_int_set(v, bset->eq[i][pos]);
12503 isl_int_set_si(bset->eq[i][pos], 0);
12504 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12505 v, subs->v->el + 1, subs->v->size - 1);
12508 for (i = 0; i < bset->n_ineq; ++i) {
12509 if (isl_int_is_zero(bset->ineq[i][pos]))
12510 continue;
12511 isl_int_set(v, bset->ineq[i][pos]);
12512 isl_int_set_si(bset->ineq[i][pos], 0);
12513 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12514 v, subs->v->el + 1, subs->v->size - 1);
12517 for (i = 0; i < bset->n_div; ++i) {
12518 if (isl_int_is_zero(bset->div[i][1 + pos]))
12519 continue;
12520 isl_int_set(v, bset->div[i][1 + pos]);
12521 isl_int_set_si(bset->div[i][1 + pos], 0);
12522 isl_seq_combine(bset->div[i] + 1,
12523 subs->v->el[0], bset->div[i] + 1,
12524 v, subs->v->el + 1, subs->v->size - 1);
12525 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12528 isl_int_clear(v);
12530 bset = isl_basic_set_simplify(bset);
12531 return isl_basic_set_finalize(bset);
12532 error:
12533 isl_basic_set_free(bset);
12534 return NULL;
12537 /* Plug in "subs" for dimension "type", "pos" of "set".
12539 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12540 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12542 int i;
12544 if (set && isl_set_plain_is_empty(set))
12545 return set;
12547 set = isl_set_cow(set);
12548 if (!set || !subs)
12549 goto error;
12551 for (i = set->n - 1; i >= 0; --i) {
12552 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12553 set = set_from_map(remove_if_empty(set_to_map(set), i));
12554 if (!set)
12555 return NULL;
12558 return set;
12559 error:
12560 isl_set_free(set);
12561 return NULL;
12564 /* Check if the range of "ma" is compatible with the domain or range
12565 * (depending on "type") of "bmap".
12567 static isl_stat check_basic_map_compatible_range_multi_aff(
12568 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12569 __isl_keep isl_multi_aff *ma)
12571 isl_bool m;
12572 isl_space *ma_space;
12574 ma_space = isl_multi_aff_get_space(ma);
12576 m = isl_space_has_equal_params(bmap->dim, ma_space);
12577 if (m < 0)
12578 goto error;
12579 if (!m)
12580 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12581 "parameters don't match", goto error);
12582 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12583 if (m < 0)
12584 goto error;
12585 if (!m)
12586 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12587 "spaces don't match", goto error);
12589 isl_space_free(ma_space);
12590 return isl_stat_ok;
12591 error:
12592 isl_space_free(ma_space);
12593 return isl_stat_error;
12596 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12597 * coefficients before the transformed range of dimensions,
12598 * the "n_after" coefficients after the transformed range of dimensions
12599 * and the coefficients of the other divs in "bmap".
12601 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
12602 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12604 int i;
12605 int n_param;
12606 int n_set;
12607 isl_local_space *ls;
12609 if (n_div == 0)
12610 return bmap;
12612 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12613 if (!ls)
12614 return isl_basic_map_free(bmap);
12616 n_param = isl_local_space_dim(ls, isl_dim_param);
12617 n_set = isl_local_space_dim(ls, isl_dim_set);
12618 for (i = 0; i < n_div; ++i) {
12619 int o_bmap = 0, o_ls = 0;
12621 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12622 o_bmap += 1 + 1 + n_param;
12623 o_ls += 1 + 1 + n_param;
12624 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12625 o_bmap += n_before;
12626 isl_seq_cpy(bmap->div[i] + o_bmap,
12627 ls->div->row[i] + o_ls, n_set);
12628 o_bmap += n_set;
12629 o_ls += n_set;
12630 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12631 o_bmap += n_after;
12632 isl_seq_cpy(bmap->div[i] + o_bmap,
12633 ls->div->row[i] + o_ls, n_div);
12634 o_bmap += n_div;
12635 o_ls += n_div;
12636 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12637 bmap = isl_basic_map_add_div_constraints(bmap, i);
12638 if (!bmap)
12639 goto error;
12642 isl_local_space_free(ls);
12643 return bmap;
12644 error:
12645 isl_local_space_free(ls);
12646 return isl_basic_map_free(bmap);
12649 /* How many stride constraints does "ma" enforce?
12650 * That is, how many of the affine expressions have a denominator
12651 * different from one?
12653 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12655 int i;
12656 int strides = 0;
12658 for (i = 0; i < ma->n; ++i)
12659 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12660 strides++;
12662 return strides;
12665 /* For each affine expression in ma of the form
12667 * x_i = (f_i y + h_i)/m_i
12669 * with m_i different from one, add a constraint to "bmap"
12670 * of the form
12672 * f_i y + h_i = m_i alpha_i
12674 * with alpha_i an additional existentially quantified variable.
12676 * The input variables of "ma" correspond to a subset of the variables
12677 * of "bmap". There are "n_before" variables in "bmap" before this
12678 * subset and "n_after" variables after this subset.
12679 * The integer divisions of the affine expressions in "ma" are assumed
12680 * to have been aligned. There are "n_div_ma" of them and
12681 * they appear first in "bmap", straight after the "n_after" variables.
12683 static __isl_give isl_basic_map *add_ma_strides(
12684 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12685 int n_before, int n_after, int n_div_ma)
12687 int i, k;
12688 int div;
12689 int total;
12690 int n_param;
12691 int n_in;
12693 total = isl_basic_map_total_dim(bmap);
12694 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12695 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12696 for (i = 0; i < ma->n; ++i) {
12697 int o_bmap = 0, o_ma = 1;
12699 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12700 continue;
12701 div = isl_basic_map_alloc_div(bmap);
12702 k = isl_basic_map_alloc_equality(bmap);
12703 if (div < 0 || k < 0)
12704 goto error;
12705 isl_int_set_si(bmap->div[div][0], 0);
12706 isl_seq_cpy(bmap->eq[k] + o_bmap,
12707 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12708 o_bmap += 1 + n_param;
12709 o_ma += 1 + n_param;
12710 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12711 o_bmap += n_before;
12712 isl_seq_cpy(bmap->eq[k] + o_bmap,
12713 ma->u.p[i]->v->el + o_ma, n_in);
12714 o_bmap += n_in;
12715 o_ma += n_in;
12716 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12717 o_bmap += n_after;
12718 isl_seq_cpy(bmap->eq[k] + o_bmap,
12719 ma->u.p[i]->v->el + o_ma, n_div_ma);
12720 o_bmap += n_div_ma;
12721 o_ma += n_div_ma;
12722 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12723 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12724 total++;
12727 return bmap;
12728 error:
12729 isl_basic_map_free(bmap);
12730 return NULL;
12733 /* Replace the domain or range space (depending on "type) of "space" by "set".
12735 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12736 enum isl_dim_type type, __isl_take isl_space *set)
12738 if (type == isl_dim_in) {
12739 space = isl_space_range(space);
12740 space = isl_space_map_from_domain_and_range(set, space);
12741 } else {
12742 space = isl_space_domain(space);
12743 space = isl_space_map_from_domain_and_range(space, set);
12746 return space;
12749 /* Compute the preimage of the domain or range (depending on "type")
12750 * of "bmap" under the function represented by "ma".
12751 * In other words, plug in "ma" in the domain or range of "bmap".
12752 * The result is a basic map that lives in the same space as "bmap"
12753 * except that the domain or range has been replaced by
12754 * the domain space of "ma".
12756 * If bmap is represented by
12758 * A(p) + S u + B x + T v + C(divs) >= 0,
12760 * where u and x are input and output dimensions if type == isl_dim_out
12761 * while x and v are input and output dimensions if type == isl_dim_in,
12762 * and ma is represented by
12764 * x = D(p) + F(y) + G(divs')
12766 * then the result is
12768 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12770 * The divs in the input set are similarly adjusted.
12771 * In particular
12773 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12775 * becomes
12777 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12778 * B_i G(divs') + c_i(divs))/n_i)
12780 * If bmap is not a rational map and if F(y) involves any denominators
12782 * x_i = (f_i y + h_i)/m_i
12784 * then additional constraints are added to ensure that we only
12785 * map back integer points. That is we enforce
12787 * f_i y + h_i = m_i alpha_i
12789 * with alpha_i an additional existentially quantified variable.
12791 * We first copy over the divs from "ma".
12792 * Then we add the modified constraints and divs from "bmap".
12793 * Finally, we add the stride constraints, if needed.
12795 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12796 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12797 __isl_take isl_multi_aff *ma)
12799 int i, k;
12800 isl_space *space;
12801 isl_basic_map *res = NULL;
12802 int n_before, n_after, n_div_bmap, n_div_ma;
12803 isl_int f, c1, c2, g;
12804 isl_bool rational;
12805 int strides;
12807 isl_int_init(f);
12808 isl_int_init(c1);
12809 isl_int_init(c2);
12810 isl_int_init(g);
12812 ma = isl_multi_aff_align_divs(ma);
12813 if (!bmap || !ma)
12814 goto error;
12815 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12816 goto error;
12818 if (type == isl_dim_in) {
12819 n_before = 0;
12820 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12821 } else {
12822 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12823 n_after = 0;
12825 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12826 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12828 space = isl_multi_aff_get_domain_space(ma);
12829 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12830 rational = isl_basic_map_is_rational(bmap);
12831 strides = rational ? 0 : multi_aff_strides(ma);
12832 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12833 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12834 if (rational)
12835 res = isl_basic_map_set_rational(res);
12837 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12838 if (isl_basic_map_alloc_div(res) < 0)
12839 goto error;
12841 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
12842 if (!res)
12843 goto error;
12845 for (i = 0; i < bmap->n_eq; ++i) {
12846 k = isl_basic_map_alloc_equality(res);
12847 if (k < 0)
12848 goto error;
12849 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12850 n_after, n_div_ma, n_div_bmap,
12851 f, c1, c2, g, 0) < 0)
12852 goto error;
12855 for (i = 0; i < bmap->n_ineq; ++i) {
12856 k = isl_basic_map_alloc_inequality(res);
12857 if (k < 0)
12858 goto error;
12859 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12860 n_after, n_div_ma, n_div_bmap,
12861 f, c1, c2, g, 0) < 0)
12862 goto error;
12865 for (i = 0; i < bmap->n_div; ++i) {
12866 if (isl_int_is_zero(bmap->div[i][0])) {
12867 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12868 continue;
12870 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12871 n_before, n_after, n_div_ma, n_div_bmap,
12872 f, c1, c2, g, 1) < 0)
12873 goto error;
12876 if (strides)
12877 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12879 isl_int_clear(f);
12880 isl_int_clear(c1);
12881 isl_int_clear(c2);
12882 isl_int_clear(g);
12883 isl_basic_map_free(bmap);
12884 isl_multi_aff_free(ma);
12885 res = isl_basic_map_simplify(res);
12886 return isl_basic_map_finalize(res);
12887 error:
12888 isl_int_clear(f);
12889 isl_int_clear(c1);
12890 isl_int_clear(c2);
12891 isl_int_clear(g);
12892 isl_basic_map_free(bmap);
12893 isl_multi_aff_free(ma);
12894 isl_basic_map_free(res);
12895 return NULL;
12898 /* Compute the preimage of "bset" under the function represented by "ma".
12899 * In other words, plug in "ma" in "bset". The result is a basic set
12900 * that lives in the domain space of "ma".
12902 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12903 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12905 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12908 /* Compute the preimage of the domain of "bmap" under the function
12909 * represented by "ma".
12910 * In other words, plug in "ma" in the domain of "bmap".
12911 * The result is a basic map that lives in the same space as "bmap"
12912 * except that the domain has been replaced by the domain space of "ma".
12914 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12915 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12917 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12920 /* Compute the preimage of the range of "bmap" under the function
12921 * represented by "ma".
12922 * In other words, plug in "ma" in the range of "bmap".
12923 * The result is a basic map that lives in the same space as "bmap"
12924 * except that the range has been replaced by the domain space of "ma".
12926 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12927 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12929 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12932 /* Check if the range of "ma" is compatible with the domain or range
12933 * (depending on "type") of "map".
12934 * Return isl_stat_error if anything is wrong.
12936 static isl_stat check_map_compatible_range_multi_aff(
12937 __isl_keep isl_map *map, enum isl_dim_type type,
12938 __isl_keep isl_multi_aff *ma)
12940 isl_bool m;
12941 isl_space *ma_space;
12943 ma_space = isl_multi_aff_get_space(ma);
12944 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12945 isl_space_free(ma_space);
12946 if (m < 0)
12947 return isl_stat_error;
12948 if (!m)
12949 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12950 "spaces don't match", return isl_stat_error);
12951 return isl_stat_ok;
12954 /* Compute the preimage of the domain or range (depending on "type")
12955 * of "map" under the function represented by "ma".
12956 * In other words, plug in "ma" in the domain or range of "map".
12957 * The result is a map that lives in the same space as "map"
12958 * except that the domain or range has been replaced by
12959 * the domain space of "ma".
12961 * The parameters are assumed to have been aligned.
12963 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12964 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12966 int i;
12967 isl_space *space;
12969 map = isl_map_cow(map);
12970 ma = isl_multi_aff_align_divs(ma);
12971 if (!map || !ma)
12972 goto error;
12973 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12974 goto error;
12976 for (i = 0; i < map->n; ++i) {
12977 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12978 isl_multi_aff_copy(ma));
12979 if (!map->p[i])
12980 goto error;
12983 space = isl_multi_aff_get_domain_space(ma);
12984 space = isl_space_set(isl_map_get_space(map), type, space);
12986 isl_space_free(map->dim);
12987 map->dim = space;
12988 if (!map->dim)
12989 goto error;
12991 isl_multi_aff_free(ma);
12992 if (map->n > 1)
12993 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12994 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12995 return map;
12996 error:
12997 isl_multi_aff_free(ma);
12998 isl_map_free(map);
12999 return NULL;
13002 /* Compute the preimage of the domain or range (depending on "type")
13003 * of "map" under the function represented by "ma".
13004 * In other words, plug in "ma" in the domain or range of "map".
13005 * The result is a map that lives in the same space as "map"
13006 * except that the domain or range has been replaced by
13007 * the domain space of "ma".
13009 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13010 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13012 isl_bool aligned;
13014 if (!map || !ma)
13015 goto error;
13017 aligned = isl_map_space_has_equal_params(map, ma->space);
13018 if (aligned < 0)
13019 goto error;
13020 if (aligned)
13021 return map_preimage_multi_aff(map, type, ma);
13023 if (isl_map_check_named_params(map) < 0)
13024 goto error;
13025 if (!isl_space_has_named_params(ma->space))
13026 isl_die(map->ctx, isl_error_invalid,
13027 "unaligned unnamed parameters", goto error);
13028 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13029 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13031 return map_preimage_multi_aff(map, type, ma);
13032 error:
13033 isl_multi_aff_free(ma);
13034 return isl_map_free(map);
13037 /* Compute the preimage of "set" under the function represented by "ma".
13038 * In other words, plug in "ma" in "set". The result is a set
13039 * that lives in the domain space of "ma".
13041 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13042 __isl_take isl_multi_aff *ma)
13044 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13047 /* Compute the preimage of the domain of "map" under the function
13048 * represented by "ma".
13049 * In other words, plug in "ma" in the domain of "map".
13050 * The result is a map that lives in the same space as "map"
13051 * except that the domain has been replaced by the domain space of "ma".
13053 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13054 __isl_take isl_multi_aff *ma)
13056 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13059 /* Compute the preimage of the range of "map" under the function
13060 * represented by "ma".
13061 * In other words, plug in "ma" in the range of "map".
13062 * The result is a map that lives in the same space as "map"
13063 * except that the range has been replaced by the domain space of "ma".
13065 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13066 __isl_take isl_multi_aff *ma)
13068 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13071 /* Compute the preimage of "map" under the function represented by "pma".
13072 * In other words, plug in "pma" in the domain or range of "map".
13073 * The result is a map that lives in the same space as "map",
13074 * except that the space of type "type" has been replaced by
13075 * the domain space of "pma".
13077 * The parameters of "map" and "pma" are assumed to have been aligned.
13079 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13080 __isl_take isl_map *map, enum isl_dim_type type,
13081 __isl_take isl_pw_multi_aff *pma)
13083 int i;
13084 isl_map *res;
13086 if (!pma)
13087 goto error;
13089 if (pma->n == 0) {
13090 isl_pw_multi_aff_free(pma);
13091 res = isl_map_empty(isl_map_get_space(map));
13092 isl_map_free(map);
13093 return res;
13096 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13097 isl_multi_aff_copy(pma->p[0].maff));
13098 if (type == isl_dim_in)
13099 res = isl_map_intersect_domain(res,
13100 isl_map_copy(pma->p[0].set));
13101 else
13102 res = isl_map_intersect_range(res,
13103 isl_map_copy(pma->p[0].set));
13105 for (i = 1; i < pma->n; ++i) {
13106 isl_map *res_i;
13108 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13109 isl_multi_aff_copy(pma->p[i].maff));
13110 if (type == isl_dim_in)
13111 res_i = isl_map_intersect_domain(res_i,
13112 isl_map_copy(pma->p[i].set));
13113 else
13114 res_i = isl_map_intersect_range(res_i,
13115 isl_map_copy(pma->p[i].set));
13116 res = isl_map_union(res, res_i);
13119 isl_pw_multi_aff_free(pma);
13120 isl_map_free(map);
13121 return res;
13122 error:
13123 isl_pw_multi_aff_free(pma);
13124 isl_map_free(map);
13125 return NULL;
13128 /* Compute the preimage of "map" under the function represented by "pma".
13129 * In other words, plug in "pma" in the domain or range of "map".
13130 * The result is a map that lives in the same space as "map",
13131 * except that the space of type "type" has been replaced by
13132 * the domain space of "pma".
13134 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13135 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13137 isl_bool aligned;
13139 if (!map || !pma)
13140 goto error;
13142 aligned = isl_map_space_has_equal_params(map, pma->dim);
13143 if (aligned < 0)
13144 goto error;
13145 if (aligned)
13146 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13148 if (isl_map_check_named_params(map) < 0)
13149 goto error;
13150 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13151 goto error;
13152 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13153 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13155 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13156 error:
13157 isl_pw_multi_aff_free(pma);
13158 return isl_map_free(map);
13161 /* Compute the preimage of "set" under the function represented by "pma".
13162 * In other words, plug in "pma" in "set". The result is a set
13163 * that lives in the domain space of "pma".
13165 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13166 __isl_take isl_pw_multi_aff *pma)
13168 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13171 /* Compute the preimage of the domain of "map" under the function
13172 * represented by "pma".
13173 * In other words, plug in "pma" in the domain of "map".
13174 * The result is a map that lives in the same space as "map",
13175 * except that domain space has been replaced by the domain space of "pma".
13177 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13178 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13180 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13183 /* Compute the preimage of the range of "map" under the function
13184 * represented by "pma".
13185 * In other words, plug in "pma" in the range of "map".
13186 * The result is a map that lives in the same space as "map",
13187 * except that range space has been replaced by the domain space of "pma".
13189 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13190 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13192 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13195 /* Compute the preimage of "map" under the function represented by "mpa".
13196 * In other words, plug in "mpa" in the domain or range of "map".
13197 * The result is a map that lives in the same space as "map",
13198 * except that the space of type "type" has been replaced by
13199 * the domain space of "mpa".
13201 * If the map does not involve any constraints that refer to the
13202 * dimensions of the substituted space, then the only possible
13203 * effect of "mpa" on the map is to map the space to a different space.
13204 * We create a separate isl_multi_aff to effectuate this change
13205 * in order to avoid spurious splitting of the map along the pieces
13206 * of "mpa".
13207 * If "mpa" has a non-trivial explicit domain, however,
13208 * then the full substitution should be performed.
13210 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13211 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13213 int n;
13214 isl_bool full;
13215 isl_pw_multi_aff *pma;
13217 if (!map || !mpa)
13218 goto error;
13220 n = isl_map_dim(map, type);
13221 full = isl_map_involves_dims(map, type, 0, n);
13222 if (full >= 0 && !full)
13223 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13224 if (full < 0)
13225 goto error;
13226 if (!full) {
13227 isl_space *space;
13228 isl_multi_aff *ma;
13230 space = isl_multi_pw_aff_get_space(mpa);
13231 isl_multi_pw_aff_free(mpa);
13232 ma = isl_multi_aff_zero(space);
13233 return isl_map_preimage_multi_aff(map, type, ma);
13236 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13237 return isl_map_preimage_pw_multi_aff(map, type, pma);
13238 error:
13239 isl_map_free(map);
13240 isl_multi_pw_aff_free(mpa);
13241 return NULL;
13244 /* Compute the preimage of "map" under the function represented by "mpa".
13245 * In other words, plug in "mpa" in the domain "map".
13246 * The result is a map that lives in the same space as "map",
13247 * except that domain space has been replaced by the domain space of "mpa".
13249 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13250 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13252 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13255 /* Compute the preimage of "set" by the function represented by "mpa".
13256 * In other words, plug in "mpa" in "set".
13258 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13259 __isl_take isl_multi_pw_aff *mpa)
13261 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13264 /* Return a copy of the equality constraints of "bset" as a matrix.
13266 __isl_give isl_mat *isl_basic_set_extract_equalities(
13267 __isl_keep isl_basic_set *bset)
13269 isl_ctx *ctx;
13270 unsigned total;
13272 if (!bset)
13273 return NULL;
13275 ctx = isl_basic_set_get_ctx(bset);
13276 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13277 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13280 /* Are the "n" "coefficients" starting at "first" of the integer division
13281 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13282 * to each other?
13283 * The "coefficient" at position 0 is the denominator.
13284 * The "coefficient" at position 1 is the constant term.
13286 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13287 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13288 unsigned first, unsigned n)
13290 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13291 return isl_bool_error;
13292 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13293 return isl_bool_error;
13294 return isl_seq_eq(bmap1->div[pos1] + first,
13295 bmap2->div[pos2] + first, n);
13298 /* Are the integer division expressions at position "pos1" in "bmap1" and
13299 * "pos2" in "bmap2" equal to each other, except that the constant terms
13300 * are different?
13302 isl_bool isl_basic_map_equal_div_expr_except_constant(
13303 __isl_keep isl_basic_map *bmap1, int pos1,
13304 __isl_keep isl_basic_map *bmap2, int pos2)
13306 isl_bool equal;
13307 unsigned total;
13309 if (!bmap1 || !bmap2)
13310 return isl_bool_error;
13311 total = isl_basic_map_total_dim(bmap1);
13312 if (total != isl_basic_map_total_dim(bmap2))
13313 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13314 "incomparable div expressions", return isl_bool_error);
13315 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13316 0, 1);
13317 if (equal < 0 || !equal)
13318 return equal;
13319 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13320 1, 1);
13321 if (equal < 0 || equal)
13322 return isl_bool_not(equal);
13323 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13324 2, total);
13327 /* Replace the numerator of the constant term of the integer division
13328 * expression at position "div" in "bmap" by "value".
13329 * The caller guarantees that this does not change the meaning
13330 * of the input.
13332 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13333 __isl_take isl_basic_map *bmap, int div, int value)
13335 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13336 return isl_basic_map_free(bmap);
13338 isl_int_set_si(bmap->div[div][1], value);
13340 return bmap;
13343 /* Is the point "inner" internal to inequality constraint "ineq"
13344 * of "bset"?
13345 * The point is considered to be internal to the inequality constraint,
13346 * if it strictly lies on the positive side of the inequality constraint,
13347 * or if it lies on the constraint and the constraint is lexico-positive.
13349 static isl_bool is_internal(__isl_keep isl_vec *inner,
13350 __isl_keep isl_basic_set *bset, int ineq)
13352 isl_ctx *ctx;
13353 int pos;
13354 unsigned total;
13356 if (!inner || !bset)
13357 return isl_bool_error;
13359 ctx = isl_basic_set_get_ctx(bset);
13360 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13361 &ctx->normalize_gcd);
13362 if (!isl_int_is_zero(ctx->normalize_gcd))
13363 return isl_int_is_nonneg(ctx->normalize_gcd);
13365 total = isl_basic_set_dim(bset, isl_dim_all);
13366 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13367 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13370 /* Tighten the inequality constraints of "bset" that are outward with respect
13371 * to the point "vec".
13372 * That is, tighten the constraints that are not satisfied by "vec".
13374 * "vec" is a point internal to some superset S of "bset" that is used
13375 * to make the subsets of S disjoint, by tightening one half of the constraints
13376 * that separate two subsets. In particular, the constraints of S
13377 * are all satisfied by "vec" and should not be tightened.
13378 * Of the internal constraints, those that have "vec" on the outside
13379 * are tightened. The shared facet is included in the adjacent subset
13380 * with the opposite constraint.
13381 * For constraints that saturate "vec", this criterion cannot be used
13382 * to determine which of the two sides should be tightened.
13383 * Instead, the sign of the first non-zero coefficient is used
13384 * to make this choice. Note that this second criterion is never used
13385 * on the constraints of S since "vec" is interior to "S".
13387 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13388 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13390 int j;
13392 bset = isl_basic_set_cow(bset);
13393 if (!bset)
13394 return NULL;
13395 for (j = 0; j < bset->n_ineq; ++j) {
13396 isl_bool internal;
13398 internal = is_internal(vec, bset, j);
13399 if (internal < 0)
13400 return isl_basic_set_free(bset);
13401 if (internal)
13402 continue;
13403 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13406 return bset;
13409 /* Replace the variables x of type "type" starting at "first" in "bmap"
13410 * by x' with x = M x' with M the matrix trans.
13411 * That is, replace the corresponding coefficients c by c M.
13413 * The transformation matrix should be a square matrix.
13415 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13416 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13417 __isl_take isl_mat *trans)
13419 unsigned pos;
13421 bmap = isl_basic_map_cow(bmap);
13422 if (!bmap || !trans)
13423 goto error;
13425 if (trans->n_row != trans->n_col)
13426 isl_die(trans->ctx, isl_error_invalid,
13427 "expecting square transformation matrix", goto error);
13428 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13429 goto error;
13431 pos = isl_basic_map_offset(bmap, type) + first;
13433 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13434 isl_mat_copy(trans)) < 0)
13435 goto error;
13436 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13437 isl_mat_copy(trans)) < 0)
13438 goto error;
13439 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13440 isl_mat_copy(trans)) < 0)
13441 goto error;
13443 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13444 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13446 isl_mat_free(trans);
13447 return bmap;
13448 error:
13449 isl_mat_free(trans);
13450 isl_basic_map_free(bmap);
13451 return NULL;
13454 /* Replace the variables x of type "type" starting at "first" in "bset"
13455 * by x' with x = M x' with M the matrix trans.
13456 * That is, replace the corresponding coefficients c by c M.
13458 * The transformation matrix should be a square matrix.
13460 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13461 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13462 __isl_take isl_mat *trans)
13464 return isl_basic_map_transform_dims(bset, type, first, trans);