isl_multi_*_from_*_list: fix parameter alignment
[isl.git] / isl_map.c
blobce4e29b5b6ba43273b4aa4a13655c988641d4131
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/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl/map.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
35 #include <isl_sort.h>
36 #include "isl_tab.h"
37 #include <isl/vec.h>
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
47 #include <bset_to_bmap.c>
48 #include <bset_from_bmap.c>
49 #include <set_to_map.c>
50 #include <set_from_map.c>
52 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
54 switch (type) {
55 case isl_dim_param: return dim->nparam;
56 case isl_dim_in: return dim->n_in;
57 case isl_dim_out: return dim->n_out;
58 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
59 default: return 0;
63 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
65 switch (type) {
66 case isl_dim_param: return 1;
67 case isl_dim_in: return 1 + dim->nparam;
68 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
69 default: return 0;
73 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
74 enum isl_dim_type type)
76 if (!bmap)
77 return 0;
78 switch (type) {
79 case isl_dim_cst: return 1;
80 case isl_dim_param:
81 case isl_dim_in:
82 case isl_dim_out: return isl_space_dim(bmap->dim, type);
83 case isl_dim_div: return bmap->n_div;
84 case isl_dim_all: return isl_basic_map_total_dim(bmap);
85 default: return 0;
89 /* Return the space of "map".
91 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
93 return map ? map->dim : NULL;
96 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
98 return map ? n(map->dim, type) : 0;
101 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
103 return set ? n(set->dim, type) : 0;
106 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
107 enum isl_dim_type type)
109 isl_space *space;
111 if (!bmap)
112 return 0;
114 space = bmap->dim;
115 switch (type) {
116 case isl_dim_cst: return 0;
117 case isl_dim_param: return 1;
118 case isl_dim_in: return 1 + space->nparam;
119 case isl_dim_out: return 1 + space->nparam + space->n_in;
120 case isl_dim_div: return 1 + space->nparam + space->n_in +
121 space->n_out;
122 default: return 0;
126 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
127 enum isl_dim_type type)
129 return isl_basic_map_offset(bset, type);
132 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
134 return pos(map->dim, type);
137 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
138 enum isl_dim_type type)
140 return isl_basic_map_dim(bset, type);
143 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
145 return isl_basic_set_dim(bset, isl_dim_set);
148 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
150 return isl_basic_set_dim(bset, isl_dim_param);
153 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
155 if (!bset)
156 return 0;
157 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
160 unsigned isl_set_n_dim(__isl_keep isl_set *set)
162 return isl_set_dim(set, isl_dim_set);
165 unsigned isl_set_n_param(__isl_keep isl_set *set)
167 return isl_set_dim(set, isl_dim_param);
170 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
172 return bmap ? bmap->dim->n_in : 0;
175 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
177 return bmap ? bmap->dim->n_out : 0;
180 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
182 return bmap ? bmap->dim->nparam : 0;
185 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
187 return bmap ? bmap->n_div : 0;
190 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
192 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
195 unsigned isl_map_n_in(__isl_keep const isl_map *map)
197 return map ? map->dim->n_in : 0;
200 unsigned isl_map_n_out(__isl_keep const isl_map *map)
202 return map ? map->dim->n_out : 0;
205 unsigned isl_map_n_param(__isl_keep const isl_map *map)
207 return map ? map->dim->nparam : 0;
210 /* Return the number of equality constraints in the description of "bmap".
211 * Return -1 on error.
213 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
215 if (!bmap)
216 return -1;
217 return bmap->n_eq;
220 /* Return the number of equality constraints in the description of "bset".
221 * Return -1 on error.
223 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
225 return isl_basic_map_n_equality(bset_to_bmap(bset));
228 /* Return the number of inequality constraints in the description of "bmap".
229 * Return -1 on error.
231 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
233 if (!bmap)
234 return -1;
235 return bmap->n_ineq;
238 /* Return the number of inequality constraints in the description of "bset".
239 * Return -1 on error.
241 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
243 return isl_basic_map_n_inequality(bset_to_bmap(bset));
246 /* Do "bmap1" and "bmap2" have the same parameters?
248 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
249 __isl_keep isl_basic_map *bmap2)
251 isl_space *space1, *space2;
253 space1 = isl_basic_map_peek_space(bmap1);
254 space2 = isl_basic_map_peek_space(bmap2);
255 return isl_space_has_equal_params(space1, space2);
258 /* Do "map1" and "map2" have the same parameters?
260 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
261 __isl_keep isl_map *map2)
263 isl_space *space1, *space2;
265 space1 = isl_map_peek_space(map1);
266 space2 = isl_map_peek_space(map2);
267 return isl_space_has_equal_params(space1, space2);
270 /* Do "map" and "set" have the same parameters?
272 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
273 __isl_keep isl_set *set)
275 return isl_map_has_equal_params(map, set_to_map(set));
278 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
279 __isl_keep isl_set *set)
281 isl_bool m;
282 if (!map || !set)
283 return isl_bool_error;
284 m = isl_map_has_equal_params(map, set_to_map(set));
285 if (m < 0 || !m)
286 return m;
287 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
288 set->dim, isl_dim_set);
291 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
292 __isl_keep isl_basic_set *bset)
294 isl_bool m;
295 if (!bmap || !bset)
296 return isl_bool_error;
297 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
298 if (m < 0 || !m)
299 return m;
300 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
301 bset->dim, isl_dim_set);
304 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
305 __isl_keep isl_set *set)
307 isl_bool m;
308 if (!map || !set)
309 return isl_bool_error;
310 m = isl_map_has_equal_params(map, set_to_map(set));
311 if (m < 0 || !m)
312 return m;
313 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
314 set->dim, isl_dim_set);
317 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
318 __isl_keep isl_basic_set *bset)
320 isl_bool m;
321 if (!bmap || !bset)
322 return isl_bool_error;
323 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
324 if (m < 0 || !m)
325 return m;
326 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
327 bset->dim, isl_dim_set);
330 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
332 return bmap ? bmap->ctx : NULL;
335 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
337 return bset ? bset->ctx : NULL;
340 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
342 return map ? map->ctx : NULL;
345 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
347 return set ? set->ctx : NULL;
350 /* Return the space of "bmap".
352 __isl_keep isl_space *isl_basic_map_peek_space(
353 __isl_keep const isl_basic_map *bmap)
355 return bmap ? bmap->dim : NULL;
358 /* Return the space of "bset".
360 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
362 return isl_basic_map_peek_space(bset_to_bmap(bset));
365 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
367 return isl_space_copy(isl_basic_map_peek_space(bmap));
370 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
372 return isl_basic_map_get_space(bset_to_bmap(bset));
375 /* Extract the divs in "bmap" as a matrix.
377 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
379 int i;
380 isl_ctx *ctx;
381 isl_mat *div;
382 unsigned total;
383 unsigned cols;
385 if (!bmap)
386 return NULL;
388 ctx = isl_basic_map_get_ctx(bmap);
389 total = isl_space_dim(bmap->dim, isl_dim_all);
390 cols = 1 + 1 + total + bmap->n_div;
391 div = isl_mat_alloc(ctx, bmap->n_div, cols);
392 if (!div)
393 return NULL;
395 for (i = 0; i < bmap->n_div; ++i)
396 isl_seq_cpy(div->row[i], bmap->div[i], cols);
398 return div;
401 /* Extract the divs in "bset" as a matrix.
403 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
405 return isl_basic_map_get_divs(bset);
408 __isl_give isl_local_space *isl_basic_map_get_local_space(
409 __isl_keep isl_basic_map *bmap)
411 isl_mat *div;
413 if (!bmap)
414 return NULL;
416 div = isl_basic_map_get_divs(bmap);
417 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
420 __isl_give isl_local_space *isl_basic_set_get_local_space(
421 __isl_keep isl_basic_set *bset)
423 return isl_basic_map_get_local_space(bset);
426 /* For each known div d = floor(f/m), add the constraints
428 * f - m d >= 0
429 * -(f-(m-1)) + m d >= 0
431 * Do not finalize the result.
433 static __isl_give isl_basic_map *add_known_div_constraints(
434 __isl_take isl_basic_map *bmap)
436 int i;
437 unsigned n_div;
439 if (!bmap)
440 return NULL;
441 n_div = isl_basic_map_dim(bmap, isl_dim_div);
442 if (n_div == 0)
443 return bmap;
444 bmap = isl_basic_map_cow(bmap);
445 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
446 if (!bmap)
447 return NULL;
448 for (i = 0; i < n_div; ++i) {
449 if (isl_int_is_zero(bmap->div[i][0]))
450 continue;
451 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
452 return isl_basic_map_free(bmap);
455 return bmap;
458 __isl_give isl_basic_map *isl_basic_map_from_local_space(
459 __isl_take isl_local_space *ls)
461 int i;
462 int n_div;
463 isl_basic_map *bmap;
465 if (!ls)
466 return NULL;
468 n_div = isl_local_space_dim(ls, isl_dim_div);
469 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
470 n_div, 0, 2 * n_div);
472 for (i = 0; i < n_div; ++i)
473 if (isl_basic_map_alloc_div(bmap) < 0)
474 goto error;
476 for (i = 0; i < n_div; ++i)
477 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
478 bmap = add_known_div_constraints(bmap);
480 isl_local_space_free(ls);
481 return bmap;
482 error:
483 isl_local_space_free(ls);
484 isl_basic_map_free(bmap);
485 return NULL;
488 __isl_give isl_basic_set *isl_basic_set_from_local_space(
489 __isl_take isl_local_space *ls)
491 return isl_basic_map_from_local_space(ls);
494 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
496 return isl_space_copy(isl_map_peek_space(map));
499 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
501 if (!set)
502 return NULL;
503 return isl_space_copy(set->dim);
506 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
507 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
509 bmap = isl_basic_map_cow(bmap);
510 if (!bmap)
511 return NULL;
512 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
513 if (!bmap->dim)
514 goto error;
515 bmap = isl_basic_map_finalize(bmap);
516 return bmap;
517 error:
518 isl_basic_map_free(bmap);
519 return NULL;
522 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
523 __isl_take isl_basic_set *bset, const char *s)
525 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
528 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
529 enum isl_dim_type type)
531 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
534 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
535 enum isl_dim_type type, const char *s)
537 int i;
539 map = isl_map_cow(map);
540 if (!map)
541 return NULL;
543 map->dim = isl_space_set_tuple_name(map->dim, type, s);
544 if (!map->dim)
545 goto error;
547 for (i = 0; i < map->n; ++i) {
548 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
549 if (!map->p[i])
550 goto error;
553 return map;
554 error:
555 isl_map_free(map);
556 return NULL;
559 /* Replace the identifier of the tuple of type "type" by "id".
561 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
562 __isl_take isl_basic_map *bmap,
563 enum isl_dim_type type, __isl_take isl_id *id)
565 bmap = isl_basic_map_cow(bmap);
566 if (!bmap)
567 goto error;
568 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
569 if (!bmap->dim)
570 return isl_basic_map_free(bmap);
571 bmap = isl_basic_map_finalize(bmap);
572 return bmap;
573 error:
574 isl_id_free(id);
575 return NULL;
578 /* Replace the identifier of the tuple by "id".
580 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
581 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
583 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
586 /* Does the input or output tuple have a name?
588 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
590 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
593 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
594 enum isl_dim_type type)
596 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
599 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
600 const char *s)
602 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
603 isl_dim_set, s));
606 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
607 enum isl_dim_type type, __isl_take isl_id *id)
609 map = isl_map_cow(map);
610 if (!map)
611 goto error;
613 map->dim = isl_space_set_tuple_id(map->dim, type, id);
615 return isl_map_reset_space(map, isl_space_copy(map->dim));
616 error:
617 isl_id_free(id);
618 return NULL;
621 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
622 __isl_take isl_id *id)
624 return isl_map_set_tuple_id(set, isl_dim_set, id);
627 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
628 enum isl_dim_type type)
630 map = isl_map_cow(map);
631 if (!map)
632 return NULL;
634 map->dim = isl_space_reset_tuple_id(map->dim, type);
636 return isl_map_reset_space(map, isl_space_copy(map->dim));
639 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
641 return isl_map_reset_tuple_id(set, isl_dim_set);
644 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
646 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
649 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
650 enum isl_dim_type type)
652 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
655 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
657 return isl_map_has_tuple_id(set, isl_dim_set);
660 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
662 return isl_map_get_tuple_id(set, isl_dim_set);
665 /* Does the set tuple have a name?
667 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
669 if (!set)
670 return isl_bool_error;
671 return isl_space_has_tuple_name(set->dim, isl_dim_set);
675 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
677 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
680 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
682 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
685 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
686 enum isl_dim_type type, unsigned pos)
688 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
691 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
692 enum isl_dim_type type, unsigned pos)
694 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
697 /* Does the given dimension have a name?
699 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
700 enum isl_dim_type type, unsigned pos)
702 if (!map)
703 return isl_bool_error;
704 return isl_space_has_dim_name(map->dim, type, pos);
707 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
708 enum isl_dim_type type, unsigned pos)
710 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
713 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
714 enum isl_dim_type type, unsigned pos)
716 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
719 /* Does the given dimension have a name?
721 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
722 enum isl_dim_type type, unsigned pos)
724 if (!set)
725 return isl_bool_error;
726 return isl_space_has_dim_name(set->dim, type, pos);
729 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
730 __isl_take isl_basic_map *bmap,
731 enum isl_dim_type type, unsigned pos, const char *s)
733 bmap = isl_basic_map_cow(bmap);
734 if (!bmap)
735 return NULL;
736 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
737 if (!bmap->dim)
738 goto error;
739 return isl_basic_map_finalize(bmap);
740 error:
741 isl_basic_map_free(bmap);
742 return NULL;
745 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
746 enum isl_dim_type type, unsigned pos, const char *s)
748 int i;
750 map = isl_map_cow(map);
751 if (!map)
752 return NULL;
754 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
755 if (!map->dim)
756 goto error;
758 for (i = 0; i < map->n; ++i) {
759 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
760 if (!map->p[i])
761 goto error;
764 return map;
765 error:
766 isl_map_free(map);
767 return NULL;
770 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
771 __isl_take isl_basic_set *bset,
772 enum isl_dim_type type, unsigned pos, const char *s)
774 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
775 type, pos, s));
778 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
779 enum isl_dim_type type, unsigned pos, const char *s)
781 return set_from_map(isl_map_set_dim_name(set_to_map(set),
782 type, pos, s));
785 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
786 enum isl_dim_type type, unsigned pos)
788 if (!bmap)
789 return isl_bool_error;
790 return isl_space_has_dim_id(bmap->dim, type, pos);
793 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
794 enum isl_dim_type type, unsigned pos)
796 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
799 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
800 enum isl_dim_type type, unsigned pos)
802 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
805 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
806 enum isl_dim_type type, unsigned pos)
808 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
811 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
812 enum isl_dim_type type, unsigned pos)
814 return isl_map_has_dim_id(set, type, pos);
817 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
818 enum isl_dim_type type, unsigned pos)
820 return isl_map_get_dim_id(set, type, pos);
823 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
824 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
826 map = isl_map_cow(map);
827 if (!map)
828 goto error;
830 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
832 return isl_map_reset_space(map, isl_space_copy(map->dim));
833 error:
834 isl_id_free(id);
835 return NULL;
838 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
839 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
841 return isl_map_set_dim_id(set, type, pos, id);
844 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
845 __isl_keep isl_id *id)
847 if (!map)
848 return -1;
849 return isl_space_find_dim_by_id(map->dim, type, id);
852 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
853 __isl_keep isl_id *id)
855 return isl_map_find_dim_by_id(set, type, id);
858 /* Return the position of the dimension of the given type and name
859 * in "bmap".
860 * Return -1 if no such dimension can be found.
862 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
863 enum isl_dim_type type, const char *name)
865 if (!bmap)
866 return -1;
867 return isl_space_find_dim_by_name(bmap->dim, type, name);
870 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
871 const char *name)
873 if (!map)
874 return -1;
875 return isl_space_find_dim_by_name(map->dim, type, name);
878 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
879 const char *name)
881 return isl_map_find_dim_by_name(set, type, name);
884 /* Check whether equality i of bset is a pure stride constraint
885 * on a single dimension, i.e., of the form
887 * v = k e
889 * with k a constant and e an existentially quantified variable.
891 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
893 unsigned nparam;
894 unsigned d;
895 unsigned n_div;
896 int pos1;
897 int pos2;
899 if (!bset)
900 return isl_bool_error;
902 if (!isl_int_is_zero(bset->eq[i][0]))
903 return isl_bool_false;
905 nparam = isl_basic_set_dim(bset, isl_dim_param);
906 d = isl_basic_set_dim(bset, isl_dim_set);
907 n_div = isl_basic_set_dim(bset, isl_dim_div);
909 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
910 return isl_bool_false;
911 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
912 if (pos1 == -1)
913 return isl_bool_false;
914 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
915 d - pos1 - 1) != -1)
916 return isl_bool_false;
918 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
919 if (pos2 == -1)
920 return isl_bool_false;
921 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
922 n_div - pos2 - 1) != -1)
923 return isl_bool_false;
924 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
925 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
926 return isl_bool_false;
928 return isl_bool_true;
931 /* Reset the user pointer on all identifiers of parameters and tuples
932 * of the space of "map".
934 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
936 isl_space *space;
938 space = isl_map_get_space(map);
939 space = isl_space_reset_user(space);
940 map = isl_map_reset_space(map, space);
942 return map;
945 /* Reset the user pointer on all identifiers of parameters and tuples
946 * of the space of "set".
948 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
950 return isl_map_reset_user(set);
953 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
955 if (!bmap)
956 return isl_bool_error;
957 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
960 /* Has "map" been marked as a rational map?
961 * In particular, have all basic maps in "map" been marked this way?
962 * An empty map is not considered to be rational.
963 * Maps where only some of the basic maps are marked rational
964 * are not allowed.
966 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
968 int i;
969 isl_bool rational;
971 if (!map)
972 return isl_bool_error;
973 if (map->n == 0)
974 return isl_bool_false;
975 rational = isl_basic_map_is_rational(map->p[0]);
976 if (rational < 0)
977 return rational;
978 for (i = 1; i < map->n; ++i) {
979 isl_bool rational_i;
981 rational_i = isl_basic_map_is_rational(map->p[i]);
982 if (rational_i < 0)
983 return rational_i;
984 if (rational != rational_i)
985 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
986 "mixed rational and integer basic maps "
987 "not supported", return isl_bool_error);
990 return rational;
993 /* Has "set" been marked as a rational set?
994 * In particular, have all basic set in "set" been marked this way?
995 * An empty set is not considered to be rational.
996 * Sets where only some of the basic sets are marked rational
997 * are not allowed.
999 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1001 return isl_map_is_rational(set);
1004 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1006 return isl_basic_map_is_rational(bset);
1009 /* Does "bmap" contain any rational points?
1011 * If "bmap" has an equality for each dimension, equating the dimension
1012 * to an integer constant, then it has no rational points, even if it
1013 * is marked as rational.
1015 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1017 isl_bool has_rational = isl_bool_true;
1018 unsigned total;
1020 if (!bmap)
1021 return isl_bool_error;
1022 if (isl_basic_map_plain_is_empty(bmap))
1023 return isl_bool_false;
1024 if (!isl_basic_map_is_rational(bmap))
1025 return isl_bool_false;
1026 bmap = isl_basic_map_copy(bmap);
1027 bmap = isl_basic_map_implicit_equalities(bmap);
1028 if (!bmap)
1029 return isl_bool_error;
1030 total = isl_basic_map_total_dim(bmap);
1031 if (bmap->n_eq == total) {
1032 int i, j;
1033 for (i = 0; i < bmap->n_eq; ++i) {
1034 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1035 if (j < 0)
1036 break;
1037 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1038 !isl_int_is_negone(bmap->eq[i][1 + j]))
1039 break;
1040 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1041 total - j - 1);
1042 if (j >= 0)
1043 break;
1045 if (i == bmap->n_eq)
1046 has_rational = isl_bool_false;
1048 isl_basic_map_free(bmap);
1050 return has_rational;
1053 /* Does "map" contain any rational points?
1055 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1057 int i;
1058 isl_bool has_rational;
1060 if (!map)
1061 return isl_bool_error;
1062 for (i = 0; i < map->n; ++i) {
1063 has_rational = isl_basic_map_has_rational(map->p[i]);
1064 if (has_rational < 0 || has_rational)
1065 return has_rational;
1067 return isl_bool_false;
1070 /* Does "set" contain any rational points?
1072 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1074 return isl_map_has_rational(set);
1077 /* Is this basic set a parameter domain?
1079 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1081 if (!bset)
1082 return isl_bool_error;
1083 return isl_space_is_params(bset->dim);
1086 /* Is this set a parameter domain?
1088 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1090 if (!set)
1091 return isl_bool_error;
1092 return isl_space_is_params(set->dim);
1095 /* Is this map actually a parameter domain?
1096 * Users should never call this function. Outside of isl,
1097 * a map can never be a parameter domain.
1099 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1101 if (!map)
1102 return isl_bool_error;
1103 return isl_space_is_params(map->dim);
1106 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
1107 struct isl_basic_map *bmap, unsigned extra,
1108 unsigned n_eq, unsigned n_ineq)
1110 int i;
1111 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1113 bmap->ctx = ctx;
1114 isl_ctx_ref(ctx);
1116 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1117 if (isl_blk_is_error(bmap->block))
1118 goto error;
1120 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1121 if ((n_ineq + n_eq) && !bmap->ineq)
1122 goto error;
1124 if (extra == 0) {
1125 bmap->block2 = isl_blk_empty();
1126 bmap->div = NULL;
1127 } else {
1128 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1129 if (isl_blk_is_error(bmap->block2))
1130 goto error;
1132 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1133 if (!bmap->div)
1134 goto error;
1137 for (i = 0; i < n_ineq + n_eq; ++i)
1138 bmap->ineq[i] = bmap->block.data + i * row_size;
1140 for (i = 0; i < extra; ++i)
1141 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1143 bmap->ref = 1;
1144 bmap->flags = 0;
1145 bmap->c_size = n_eq + n_ineq;
1146 bmap->eq = bmap->ineq + n_ineq;
1147 bmap->extra = extra;
1148 bmap->n_eq = 0;
1149 bmap->n_ineq = 0;
1150 bmap->n_div = 0;
1151 bmap->sample = NULL;
1153 return bmap;
1154 error:
1155 isl_basic_map_free(bmap);
1156 return NULL;
1159 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1160 unsigned nparam, unsigned dim, unsigned extra,
1161 unsigned n_eq, unsigned n_ineq)
1163 struct isl_basic_map *bmap;
1164 isl_space *space;
1166 space = isl_space_set_alloc(ctx, nparam, dim);
1167 if (!space)
1168 return NULL;
1170 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1171 return bset_from_bmap(bmap);
1174 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1175 unsigned extra, unsigned n_eq, unsigned n_ineq)
1177 struct isl_basic_map *bmap;
1178 if (!dim)
1179 return NULL;
1180 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1181 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1182 return bset_from_bmap(bmap);
1183 error:
1184 isl_space_free(dim);
1185 return NULL;
1188 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1189 unsigned extra, unsigned n_eq, unsigned n_ineq)
1191 struct isl_basic_map *bmap;
1193 if (!dim)
1194 return NULL;
1195 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1196 if (!bmap)
1197 goto error;
1198 bmap->dim = dim;
1200 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1201 error:
1202 isl_space_free(dim);
1203 return NULL;
1206 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1207 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1208 unsigned n_eq, unsigned n_ineq)
1210 struct isl_basic_map *bmap;
1211 isl_space *dim;
1213 dim = isl_space_alloc(ctx, nparam, in, out);
1214 if (!dim)
1215 return NULL;
1217 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1218 return bmap;
1221 static void dup_constraints(
1222 struct isl_basic_map *dst, struct isl_basic_map *src)
1224 int i;
1225 unsigned total = isl_basic_map_total_dim(src);
1227 for (i = 0; i < src->n_eq; ++i) {
1228 int j = isl_basic_map_alloc_equality(dst);
1229 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1232 for (i = 0; i < src->n_ineq; ++i) {
1233 int j = isl_basic_map_alloc_inequality(dst);
1234 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1237 for (i = 0; i < src->n_div; ++i) {
1238 int j = isl_basic_map_alloc_div(dst);
1239 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1241 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1244 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1246 struct isl_basic_map *dup;
1248 if (!bmap)
1249 return NULL;
1250 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1251 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1252 if (!dup)
1253 return NULL;
1254 dup_constraints(dup, bmap);
1255 dup->flags = bmap->flags;
1256 dup->sample = isl_vec_copy(bmap->sample);
1257 return dup;
1260 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1262 struct isl_basic_map *dup;
1264 dup = isl_basic_map_dup(bset_to_bmap(bset));
1265 return bset_from_bmap(dup);
1268 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1270 if (!bset)
1271 return NULL;
1273 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1274 bset->ref++;
1275 return bset;
1277 return isl_basic_set_dup(bset);
1280 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1282 if (!set)
1283 return NULL;
1285 set->ref++;
1286 return set;
1289 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1291 if (!bmap)
1292 return NULL;
1294 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1295 bmap->ref++;
1296 return bmap;
1298 bmap = isl_basic_map_dup(bmap);
1299 if (bmap)
1300 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1301 return bmap;
1304 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1306 if (!map)
1307 return NULL;
1309 map->ref++;
1310 return map;
1313 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1315 if (!bmap)
1316 return NULL;
1318 if (--bmap->ref > 0)
1319 return NULL;
1321 isl_ctx_deref(bmap->ctx);
1322 free(bmap->div);
1323 isl_blk_free(bmap->ctx, bmap->block2);
1324 free(bmap->ineq);
1325 isl_blk_free(bmap->ctx, bmap->block);
1326 isl_vec_free(bmap->sample);
1327 isl_space_free(bmap->dim);
1328 free(bmap);
1330 return NULL;
1333 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1335 return isl_basic_map_free(bset_to_bmap(bset));
1338 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1340 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1343 /* Check that "map" has only named parameters, reporting an error
1344 * if it does not.
1346 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1348 return isl_space_check_named_params(isl_map_peek_space(map));
1351 /* Check that "bmap1" and "bmap2" have the same parameters,
1352 * reporting an error if they do not.
1354 static isl_stat isl_basic_map_check_equal_params(
1355 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1357 isl_bool match;
1359 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1360 if (match < 0)
1361 return isl_stat_error;
1362 if (!match)
1363 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1364 "parameters don't match", return isl_stat_error);
1365 return isl_stat_ok;
1368 __isl_give isl_map *isl_map_align_params_map_map_and(
1369 __isl_take isl_map *map1, __isl_take isl_map *map2,
1370 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1371 __isl_take isl_map *map2))
1373 if (!map1 || !map2)
1374 goto error;
1375 if (isl_map_has_equal_params(map1, map2))
1376 return fn(map1, map2);
1377 if (isl_map_check_named_params(map1) < 0)
1378 goto error;
1379 if (isl_map_check_named_params(map2) < 0)
1380 goto error;
1381 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1382 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1383 return fn(map1, map2);
1384 error:
1385 isl_map_free(map1);
1386 isl_map_free(map2);
1387 return NULL;
1390 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1391 __isl_keep isl_map *map2,
1392 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1394 isl_bool r;
1396 if (!map1 || !map2)
1397 return isl_bool_error;
1398 if (isl_map_has_equal_params(map1, map2))
1399 return fn(map1, map2);
1400 if (isl_map_check_named_params(map1) < 0)
1401 return isl_bool_error;
1402 if (isl_map_check_named_params(map2) < 0)
1403 return isl_bool_error;
1404 map1 = isl_map_copy(map1);
1405 map2 = isl_map_copy(map2);
1406 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1407 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1408 r = fn(map1, map2);
1409 isl_map_free(map1);
1410 isl_map_free(map2);
1411 return r;
1414 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1416 struct isl_ctx *ctx;
1417 if (!bmap)
1418 return -1;
1419 ctx = bmap->ctx;
1420 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1421 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1422 return -1);
1423 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1424 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1425 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1426 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1427 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1428 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1429 isl_int *t;
1430 int j = isl_basic_map_alloc_inequality(bmap);
1431 if (j < 0)
1432 return -1;
1433 t = bmap->ineq[j];
1434 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1435 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1436 bmap->eq[-1] = t;
1437 bmap->n_eq++;
1438 bmap->n_ineq--;
1439 bmap->eq--;
1440 return 0;
1442 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1443 bmap->extra - bmap->n_div);
1444 return bmap->n_eq++;
1447 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1449 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1452 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1454 if (!bmap)
1455 return -1;
1456 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1457 bmap->n_eq -= n;
1458 return 0;
1461 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1463 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1466 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1468 isl_int *t;
1469 if (!bmap)
1470 return -1;
1471 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1473 if (pos != bmap->n_eq - 1) {
1474 t = bmap->eq[pos];
1475 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1476 bmap->eq[bmap->n_eq - 1] = t;
1478 bmap->n_eq--;
1479 return 0;
1482 /* Turn inequality "pos" of "bmap" into an equality.
1484 * In particular, we move the inequality in front of the equalities
1485 * and move the last inequality in the position of the moved inequality.
1486 * Note that isl_tab_make_equalities_explicit depends on this particular
1487 * change in the ordering of the constraints.
1489 void isl_basic_map_inequality_to_equality(
1490 struct isl_basic_map *bmap, unsigned pos)
1492 isl_int *t;
1494 t = bmap->ineq[pos];
1495 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1496 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1497 bmap->eq[-1] = t;
1498 bmap->n_eq++;
1499 bmap->n_ineq--;
1500 bmap->eq--;
1501 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1502 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1503 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1504 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1507 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1509 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1512 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1514 struct isl_ctx *ctx;
1515 if (!bmap)
1516 return -1;
1517 ctx = bmap->ctx;
1518 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1519 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1520 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1521 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1522 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1523 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1524 1 + isl_basic_map_total_dim(bmap),
1525 bmap->extra - bmap->n_div);
1526 return bmap->n_ineq++;
1529 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1531 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1534 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1536 if (!bmap)
1537 return -1;
1538 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1539 bmap->n_ineq -= n;
1540 return 0;
1543 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1545 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1548 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1550 isl_int *t;
1551 if (!bmap)
1552 return -1;
1553 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1555 if (pos != bmap->n_ineq - 1) {
1556 t = bmap->ineq[pos];
1557 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1558 bmap->ineq[bmap->n_ineq - 1] = t;
1559 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1561 bmap->n_ineq--;
1562 return 0;
1565 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1567 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1570 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1571 isl_int *eq)
1573 int k;
1575 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1576 if (!bmap)
1577 return NULL;
1578 k = isl_basic_map_alloc_equality(bmap);
1579 if (k < 0)
1580 goto error;
1581 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1582 return bmap;
1583 error:
1584 isl_basic_map_free(bmap);
1585 return NULL;
1588 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1589 isl_int *eq)
1591 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1594 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1595 isl_int *ineq)
1597 int k;
1599 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1600 if (!bmap)
1601 return NULL;
1602 k = isl_basic_map_alloc_inequality(bmap);
1603 if (k < 0)
1604 goto error;
1605 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1606 return bmap;
1607 error:
1608 isl_basic_map_free(bmap);
1609 return NULL;
1612 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1613 isl_int *ineq)
1615 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1618 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1620 if (!bmap)
1621 return -1;
1622 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1623 isl_seq_clr(bmap->div[bmap->n_div] +
1624 1 + 1 + isl_basic_map_total_dim(bmap),
1625 bmap->extra - bmap->n_div);
1626 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1627 return bmap->n_div++;
1630 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1632 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1635 /* Check that there are "n" dimensions of type "type" starting at "first"
1636 * in "bmap".
1638 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1639 enum isl_dim_type type, unsigned first, unsigned n)
1641 unsigned dim;
1643 if (!bmap)
1644 return isl_stat_error;
1645 dim = isl_basic_map_dim(bmap, type);
1646 if (first + n > dim || first + n < first)
1647 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1648 "position or range out of bounds",
1649 return isl_stat_error);
1650 return isl_stat_ok;
1653 /* Insert an extra integer division, prescribed by "div", to "bmap"
1654 * at (integer division) position "pos".
1656 * The integer division is first added at the end and then moved
1657 * into the right position.
1659 __isl_give isl_basic_map *isl_basic_map_insert_div(
1660 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1662 int i, k;
1664 bmap = isl_basic_map_cow(bmap);
1665 if (!bmap || !div)
1666 return isl_basic_map_free(bmap);
1668 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1669 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1670 "unexpected size", return isl_basic_map_free(bmap));
1671 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1672 return isl_basic_map_free(bmap);
1674 bmap = isl_basic_map_extend_space(bmap,
1675 isl_basic_map_get_space(bmap), 1, 0, 2);
1676 k = isl_basic_map_alloc_div(bmap);
1677 if (k < 0)
1678 return isl_basic_map_free(bmap);
1679 isl_seq_cpy(bmap->div[k], div->el, div->size);
1680 isl_int_set_si(bmap->div[k][div->size], 0);
1682 for (i = k; i > pos; --i)
1683 isl_basic_map_swap_div(bmap, i, i - 1);
1685 return bmap;
1688 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1690 if (!bmap)
1691 return isl_stat_error;
1692 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1693 bmap->n_div -= n;
1694 return isl_stat_ok;
1697 /* Copy constraint from src to dst, putting the vars of src at offset
1698 * dim_off in dst and the divs of src at offset div_off in dst.
1699 * If both sets are actually map, then dim_off applies to the input
1700 * variables.
1702 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1703 struct isl_basic_map *src_map, isl_int *src,
1704 unsigned in_off, unsigned out_off, unsigned div_off)
1706 unsigned src_nparam = isl_basic_map_dim(src_map, isl_dim_param);
1707 unsigned dst_nparam = isl_basic_map_dim(dst_map, isl_dim_param);
1708 unsigned src_in = isl_basic_map_dim(src_map, isl_dim_in);
1709 unsigned dst_in = isl_basic_map_dim(dst_map, isl_dim_in);
1710 unsigned src_out = isl_basic_map_dim(src_map, isl_dim_out);
1711 unsigned dst_out = isl_basic_map_dim(dst_map, isl_dim_out);
1712 isl_int_set(dst[0], src[0]);
1713 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1714 if (dst_nparam > src_nparam)
1715 isl_seq_clr(dst+1+src_nparam,
1716 dst_nparam - src_nparam);
1717 isl_seq_clr(dst+1+dst_nparam, in_off);
1718 isl_seq_cpy(dst+1+dst_nparam+in_off,
1719 src+1+src_nparam,
1720 isl_min(dst_in-in_off, src_in));
1721 if (dst_in-in_off > src_in)
1722 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1723 dst_in - in_off - src_in);
1724 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1725 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1726 src+1+src_nparam+src_in,
1727 isl_min(dst_out-out_off, src_out));
1728 if (dst_out-out_off > src_out)
1729 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1730 dst_out - out_off - src_out);
1731 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1732 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1733 src+1+src_nparam+src_in+src_out,
1734 isl_min(dst_map->extra-div_off, src_map->n_div));
1735 if (dst_map->n_div-div_off > src_map->n_div)
1736 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1737 div_off+src_map->n_div,
1738 dst_map->n_div - div_off - src_map->n_div);
1741 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1742 struct isl_basic_map *src_map, isl_int *src,
1743 unsigned in_off, unsigned out_off, unsigned div_off)
1745 isl_int_set(dst[0], src[0]);
1746 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1749 static __isl_give isl_basic_map *add_constraints(
1750 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1751 unsigned i_pos, unsigned o_pos)
1753 int i;
1754 unsigned div_off;
1756 if (!bmap1 || !bmap2)
1757 goto error;
1759 div_off = bmap1->n_div;
1761 for (i = 0; i < bmap2->n_eq; ++i) {
1762 int i1 = isl_basic_map_alloc_equality(bmap1);
1763 if (i1 < 0)
1764 goto error;
1765 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1766 i_pos, o_pos, div_off);
1769 for (i = 0; i < bmap2->n_ineq; ++i) {
1770 int i1 = isl_basic_map_alloc_inequality(bmap1);
1771 if (i1 < 0)
1772 goto error;
1773 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1774 i_pos, o_pos, div_off);
1777 for (i = 0; i < bmap2->n_div; ++i) {
1778 int i1 = isl_basic_map_alloc_div(bmap1);
1779 if (i1 < 0)
1780 goto error;
1781 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1782 i_pos, o_pos, div_off);
1785 isl_basic_map_free(bmap2);
1787 return bmap1;
1789 error:
1790 isl_basic_map_free(bmap1);
1791 isl_basic_map_free(bmap2);
1792 return NULL;
1795 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1796 struct isl_basic_set *bset2, unsigned pos)
1798 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1799 bset_to_bmap(bset2), 0, pos));
1802 __isl_give isl_basic_map *isl_basic_map_extend_space(
1803 __isl_take isl_basic_map *base, __isl_take isl_space *dim,
1804 unsigned extra, unsigned n_eq, unsigned n_ineq)
1806 struct isl_basic_map *ext;
1807 unsigned flags;
1808 int dims_ok;
1810 if (!dim)
1811 goto error;
1813 if (!base)
1814 goto error;
1816 dims_ok = isl_space_is_equal(base->dim, dim) &&
1817 base->extra >= base->n_div + extra;
1819 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1820 room_for_ineq(base, n_ineq)) {
1821 isl_space_free(dim);
1822 return base;
1825 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1826 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1827 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1828 extra += base->extra;
1829 n_eq += base->n_eq;
1830 n_ineq += base->n_ineq;
1832 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1833 dim = NULL;
1834 if (!ext)
1835 goto error;
1837 if (dims_ok)
1838 ext->sample = isl_vec_copy(base->sample);
1839 flags = base->flags;
1840 ext = add_constraints(ext, base, 0, 0);
1841 if (ext) {
1842 ext->flags = flags;
1843 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1846 return ext;
1848 error:
1849 isl_space_free(dim);
1850 isl_basic_map_free(base);
1851 return NULL;
1854 __isl_give isl_basic_set *isl_basic_set_extend_space(
1855 __isl_take isl_basic_set *base,
1856 __isl_take isl_space *dim, unsigned extra,
1857 unsigned n_eq, unsigned n_ineq)
1859 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1860 dim, extra, n_eq, n_ineq));
1863 struct isl_basic_map *isl_basic_map_extend_constraints(
1864 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1866 if (!base)
1867 return NULL;
1868 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1869 0, n_eq, n_ineq);
1872 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1873 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1874 unsigned n_eq, unsigned n_ineq)
1876 struct isl_basic_map *bmap;
1877 isl_space *dim;
1879 if (!base)
1880 return NULL;
1881 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1882 if (!dim)
1883 goto error;
1885 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1886 return bmap;
1887 error:
1888 isl_basic_map_free(base);
1889 return NULL;
1892 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1893 unsigned nparam, unsigned dim, unsigned extra,
1894 unsigned n_eq, unsigned n_ineq)
1896 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1897 nparam, 0, dim, extra, n_eq, n_ineq));
1900 struct isl_basic_set *isl_basic_set_extend_constraints(
1901 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1903 isl_basic_map *bmap = bset_to_bmap(base);
1904 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1905 return bset_from_bmap(bmap);
1908 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1910 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1913 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1915 if (!bmap)
1916 return NULL;
1918 if (bmap->ref > 1) {
1919 bmap->ref--;
1920 bmap = isl_basic_map_dup(bmap);
1922 if (bmap) {
1923 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1924 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1926 return bmap;
1929 /* Clear all cached information in "map", either because it is about
1930 * to be modified or because it is being freed.
1931 * Always return the same pointer that is passed in.
1932 * This is needed for the use in isl_map_free.
1934 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1936 isl_basic_map_free(map->cached_simple_hull[0]);
1937 isl_basic_map_free(map->cached_simple_hull[1]);
1938 map->cached_simple_hull[0] = NULL;
1939 map->cached_simple_hull[1] = NULL;
1940 return map;
1943 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1945 return isl_map_cow(set);
1948 /* Return an isl_map that is equal to "map" and that has only
1949 * a single reference.
1951 * If the original input already has only one reference, then
1952 * simply return it, but clear all cached information, since
1953 * it may be rendered invalid by the operations that will be
1954 * performed on the result.
1956 * Otherwise, create a duplicate (without any cached information).
1958 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1960 if (!map)
1961 return NULL;
1963 if (map->ref == 1)
1964 return clear_caches(map);
1965 map->ref--;
1966 return isl_map_dup(map);
1969 static void swap_vars(struct isl_blk blk, isl_int *a,
1970 unsigned a_len, unsigned b_len)
1972 isl_seq_cpy(blk.data, a+a_len, b_len);
1973 isl_seq_cpy(blk.data+b_len, a, a_len);
1974 isl_seq_cpy(a, blk.data, b_len+a_len);
1977 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1978 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1980 int i;
1981 struct isl_blk blk;
1983 if (!bmap)
1984 goto error;
1986 isl_assert(bmap->ctx,
1987 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1989 if (n1 == 0 || n2 == 0)
1990 return bmap;
1992 bmap = isl_basic_map_cow(bmap);
1993 if (!bmap)
1994 return NULL;
1996 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1997 if (isl_blk_is_error(blk))
1998 goto error;
2000 for (i = 0; i < bmap->n_eq; ++i)
2001 swap_vars(blk,
2002 bmap->eq[i] + pos, n1, n2);
2004 for (i = 0; i < bmap->n_ineq; ++i)
2005 swap_vars(blk,
2006 bmap->ineq[i] + pos, n1, n2);
2008 for (i = 0; i < bmap->n_div; ++i)
2009 swap_vars(blk,
2010 bmap->div[i]+1 + pos, n1, n2);
2012 isl_blk_free(bmap->ctx, blk);
2014 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
2015 bmap = isl_basic_map_gauss(bmap, NULL);
2016 return isl_basic_map_finalize(bmap);
2017 error:
2018 isl_basic_map_free(bmap);
2019 return NULL;
2022 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2023 __isl_take isl_basic_map *bmap)
2025 int i = 0;
2026 unsigned total;
2027 if (!bmap)
2028 goto error;
2029 total = isl_basic_map_total_dim(bmap);
2030 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2031 return isl_basic_map_free(bmap);
2032 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2033 if (bmap->n_eq > 0)
2034 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2035 else {
2036 i = isl_basic_map_alloc_equality(bmap);
2037 if (i < 0)
2038 goto error;
2040 isl_int_set_si(bmap->eq[i][0], 1);
2041 isl_seq_clr(bmap->eq[i]+1, total);
2042 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2043 isl_vec_free(bmap->sample);
2044 bmap->sample = NULL;
2045 return isl_basic_map_finalize(bmap);
2046 error:
2047 isl_basic_map_free(bmap);
2048 return NULL;
2051 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2052 __isl_take isl_basic_set *bset)
2054 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2057 __isl_give isl_basic_map *isl_basic_map_set_rational(
2058 __isl_take isl_basic_map *bmap)
2060 if (!bmap)
2061 return NULL;
2063 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2064 return bmap;
2066 bmap = isl_basic_map_cow(bmap);
2067 if (!bmap)
2068 return NULL;
2070 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2072 return isl_basic_map_finalize(bmap);
2075 __isl_give isl_basic_set *isl_basic_set_set_rational(
2076 __isl_take isl_basic_set *bset)
2078 return isl_basic_map_set_rational(bset);
2081 __isl_give isl_basic_set *isl_basic_set_set_integral(
2082 __isl_take isl_basic_set *bset)
2084 if (!bset)
2085 return NULL;
2087 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2088 return bset;
2090 bset = isl_basic_set_cow(bset);
2091 if (!bset)
2092 return NULL;
2094 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2096 return isl_basic_set_finalize(bset);
2099 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2101 int i;
2103 map = isl_map_cow(map);
2104 if (!map)
2105 return NULL;
2106 for (i = 0; i < map->n; ++i) {
2107 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2108 if (!map->p[i])
2109 goto error;
2111 return map;
2112 error:
2113 isl_map_free(map);
2114 return NULL;
2117 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2119 return isl_map_set_rational(set);
2122 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2123 * of "bmap").
2125 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2127 isl_int *t = bmap->div[a];
2128 bmap->div[a] = bmap->div[b];
2129 bmap->div[b] = t;
2132 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2133 * div definitions accordingly.
2135 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2137 int i;
2138 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2140 swap_div(bmap, a, b);
2142 for (i = 0; i < bmap->n_eq; ++i)
2143 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2145 for (i = 0; i < bmap->n_ineq; ++i)
2146 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2148 for (i = 0; i < bmap->n_div; ++i)
2149 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2150 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2153 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
2154 * div definitions accordingly.
2156 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
2158 isl_basic_map_swap_div(bset, a, b);
2161 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2163 isl_seq_cpy(c, c + n, rem);
2164 isl_seq_clr(c + rem, n);
2167 /* Drop n dimensions starting at first.
2169 * In principle, this frees up some extra variables as the number
2170 * of columns remains constant, but we would have to extend
2171 * the div array too as the number of rows in this array is assumed
2172 * to be equal to extra.
2174 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2175 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2177 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2180 /* Move "n" divs starting at "first" to the end of the list of divs.
2182 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2183 unsigned first, unsigned n)
2185 isl_int **div;
2186 int i;
2188 if (first + n == bmap->n_div)
2189 return bmap;
2191 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2192 if (!div)
2193 goto error;
2194 for (i = 0; i < n; ++i)
2195 div[i] = bmap->div[first + i];
2196 for (i = 0; i < bmap->n_div - first - n; ++i)
2197 bmap->div[first + i] = bmap->div[first + n + i];
2198 for (i = 0; i < n; ++i)
2199 bmap->div[bmap->n_div - n + i] = div[i];
2200 free(div);
2201 return bmap;
2202 error:
2203 isl_basic_map_free(bmap);
2204 return NULL;
2207 /* Drop "n" dimensions of type "type" starting at "first".
2209 * In principle, this frees up some extra variables as the number
2210 * of columns remains constant, but we would have to extend
2211 * the div array too as the number of rows in this array is assumed
2212 * to be equal to extra.
2214 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2215 enum isl_dim_type type, unsigned first, unsigned n)
2217 int i;
2218 unsigned dim;
2219 unsigned offset;
2220 unsigned left;
2222 if (!bmap)
2223 goto error;
2225 dim = isl_basic_map_dim(bmap, type);
2226 isl_assert(bmap->ctx, first + n <= dim, goto error);
2228 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2229 return bmap;
2231 bmap = isl_basic_map_cow(bmap);
2232 if (!bmap)
2233 return NULL;
2235 offset = isl_basic_map_offset(bmap, type) + first;
2236 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2237 for (i = 0; i < bmap->n_eq; ++i)
2238 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2240 for (i = 0; i < bmap->n_ineq; ++i)
2241 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2243 for (i = 0; i < bmap->n_div; ++i)
2244 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2246 if (type == isl_dim_div) {
2247 bmap = move_divs_last(bmap, first, n);
2248 if (!bmap)
2249 goto error;
2250 if (isl_basic_map_free_div(bmap, n) < 0)
2251 return isl_basic_map_free(bmap);
2252 } else
2253 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2254 if (!bmap->dim)
2255 goto error;
2257 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2258 bmap = isl_basic_map_simplify(bmap);
2259 return isl_basic_map_finalize(bmap);
2260 error:
2261 isl_basic_map_free(bmap);
2262 return NULL;
2265 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2266 enum isl_dim_type type, unsigned first, unsigned n)
2268 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2269 type, first, n));
2272 /* No longer consider "map" to be normalized.
2274 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2276 if (!map)
2277 return NULL;
2278 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2279 return map;
2282 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2283 enum isl_dim_type type, unsigned first, unsigned n)
2285 int i;
2287 if (!map)
2288 goto error;
2290 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2292 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2293 return map;
2294 map = isl_map_cow(map);
2295 if (!map)
2296 goto error;
2297 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2298 if (!map->dim)
2299 goto error;
2301 for (i = 0; i < map->n; ++i) {
2302 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2303 if (!map->p[i])
2304 goto error;
2306 map = isl_map_unmark_normalized(map);
2308 return map;
2309 error:
2310 isl_map_free(map);
2311 return NULL;
2314 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2315 enum isl_dim_type type, unsigned first, unsigned n)
2317 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2321 * We don't cow, as the div is assumed to be redundant.
2323 __isl_give isl_basic_map *isl_basic_map_drop_div(
2324 __isl_take isl_basic_map *bmap, unsigned div)
2326 int i;
2327 unsigned pos;
2329 if (!bmap)
2330 goto error;
2332 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
2334 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
2336 for (i = 0; i < bmap->n_eq; ++i)
2337 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
2339 for (i = 0; i < bmap->n_ineq; ++i) {
2340 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
2341 isl_basic_map_drop_inequality(bmap, i);
2342 --i;
2343 continue;
2345 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
2348 for (i = 0; i < bmap->n_div; ++i)
2349 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
2351 if (div != bmap->n_div - 1) {
2352 int j;
2353 isl_int *t = bmap->div[div];
2355 for (j = div; j < bmap->n_div - 1; ++j)
2356 bmap->div[j] = bmap->div[j+1];
2358 bmap->div[bmap->n_div - 1] = t;
2360 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2361 if (isl_basic_map_free_div(bmap, 1) < 0)
2362 return isl_basic_map_free(bmap);
2364 return bmap;
2365 error:
2366 isl_basic_map_free(bmap);
2367 return NULL;
2370 /* Eliminate the specified n dimensions starting at first from the
2371 * constraints, without removing the dimensions from the space.
2372 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2374 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2375 enum isl_dim_type type, unsigned first, unsigned n)
2377 int i;
2379 if (!map)
2380 return NULL;
2381 if (n == 0)
2382 return map;
2384 if (first + n > isl_map_dim(map, type) || first + n < first)
2385 isl_die(map->ctx, isl_error_invalid,
2386 "index out of bounds", goto error);
2388 map = isl_map_cow(map);
2389 if (!map)
2390 return NULL;
2392 for (i = 0; i < map->n; ++i) {
2393 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2394 if (!map->p[i])
2395 goto error;
2397 return map;
2398 error:
2399 isl_map_free(map);
2400 return NULL;
2403 /* Eliminate the specified n dimensions starting at first from the
2404 * constraints, without removing the dimensions from the space.
2405 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2407 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2408 enum isl_dim_type type, unsigned first, unsigned n)
2410 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2413 /* Eliminate the specified n dimensions starting at first from the
2414 * constraints, without removing the dimensions from the space.
2415 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2417 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2418 unsigned first, unsigned n)
2420 return isl_set_eliminate(set, isl_dim_set, first, n);
2423 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2424 __isl_take isl_basic_map *bmap)
2426 if (!bmap)
2427 return NULL;
2428 bmap = isl_basic_map_eliminate_vars(bmap,
2429 isl_space_dim(bmap->dim, isl_dim_all), 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 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2613 return bmap;
2614 error:
2615 isl_basic_map_free(bmap);
2616 return NULL;
2619 /* This function is called right before "div" is eliminated from "bmap"
2620 * using Fourier-Motzkin.
2621 * Look through the constraints of "bmap" for constraints on the argument
2622 * of the integer division and use them to construct constraints on the
2623 * integer division itself. These constraints can then be combined
2624 * during the Fourier-Motzkin elimination.
2625 * Note that it is only useful to introduce lower bounds on "div"
2626 * if "bmap" already contains upper bounds on "div" as the newly
2627 * introduce lower bounds can then be combined with the pre-existing
2628 * upper bounds. Similarly for upper bounds.
2629 * We therefore first check if "bmap" contains any lower and/or upper bounds
2630 * on "div".
2632 * It is interesting to note that the introduction of these constraints
2633 * can indeed lead to more accurate results, even when compared to
2634 * deriving constraints on the argument of "div" from constraints on "div".
2635 * Consider, for example, the set
2637 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2639 * The second constraint can be rewritten as
2641 * 2 * [(-i-2j+3)/4] + k >= 0
2643 * from which we can derive
2645 * -i - 2j + 3 >= -2k
2647 * or
2649 * i + 2j <= 3 + 2k
2651 * Combined with the first constraint, we obtain
2653 * -3 <= 3 + 2k or k >= -3
2655 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2656 * the first constraint, we obtain
2658 * [(i + 2j)/4] >= [-3/4] = -1
2660 * Combining this constraint with the second constraint, we obtain
2662 * k >= -2
2664 static __isl_give isl_basic_map *insert_bounds_on_div(
2665 __isl_take isl_basic_map *bmap, int div)
2667 int i;
2668 int check_lb, check_ub;
2669 isl_int v;
2670 unsigned total;
2672 if (!bmap)
2673 return NULL;
2675 if (isl_int_is_zero(bmap->div[div][0]))
2676 return bmap;
2678 total = isl_space_dim(bmap->dim, isl_dim_all);
2680 check_lb = 0;
2681 check_ub = 0;
2682 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2683 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2684 if (s > 0)
2685 check_ub = 1;
2686 if (s < 0)
2687 check_lb = 1;
2690 if (!check_lb && !check_ub)
2691 return bmap;
2693 isl_int_init(v);
2695 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2696 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2697 continue;
2699 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2700 check_lb, check_ub);
2703 isl_int_clear(v);
2705 return bmap;
2708 /* Remove all divs (recursively) involving any of the given dimensions
2709 * in their definitions.
2711 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2712 __isl_take isl_basic_map *bmap,
2713 enum isl_dim_type type, unsigned first, unsigned n)
2715 int i;
2717 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2718 return isl_basic_map_free(bmap);
2719 first += isl_basic_map_offset(bmap, type);
2721 for (i = bmap->n_div - 1; i >= 0; --i) {
2722 isl_bool involves;
2724 involves = div_involves_vars(bmap, i, first, n);
2725 if (involves < 0)
2726 return isl_basic_map_free(bmap);
2727 if (!involves)
2728 continue;
2729 bmap = insert_bounds_on_div(bmap, i);
2730 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2731 if (!bmap)
2732 return NULL;
2733 i = bmap->n_div;
2736 return bmap;
2739 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2740 __isl_take isl_basic_set *bset,
2741 enum isl_dim_type type, unsigned first, unsigned n)
2743 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2746 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2747 enum isl_dim_type type, unsigned first, unsigned n)
2749 int i;
2751 if (!map)
2752 return NULL;
2753 if (map->n == 0)
2754 return map;
2756 map = isl_map_cow(map);
2757 if (!map)
2758 return NULL;
2760 for (i = 0; i < map->n; ++i) {
2761 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2762 type, first, n);
2763 if (!map->p[i])
2764 goto error;
2766 return map;
2767 error:
2768 isl_map_free(map);
2769 return NULL;
2772 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2773 enum isl_dim_type type, unsigned first, unsigned n)
2775 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2776 type, first, n));
2779 /* Does the description of "bmap" depend on the specified dimensions?
2780 * We also check whether the dimensions appear in any of the div definitions.
2781 * In principle there is no need for this check. If the dimensions appear
2782 * in a div definition, they also appear in the defining constraints of that
2783 * div.
2785 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2786 enum isl_dim_type type, unsigned first, unsigned n)
2788 int i;
2790 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2791 return isl_bool_error;
2793 first += isl_basic_map_offset(bmap, type);
2794 for (i = 0; i < bmap->n_eq; ++i)
2795 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2796 return isl_bool_true;
2797 for (i = 0; i < bmap->n_ineq; ++i)
2798 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2799 return isl_bool_true;
2800 for (i = 0; i < bmap->n_div; ++i) {
2801 if (isl_int_is_zero(bmap->div[i][0]))
2802 continue;
2803 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2804 return isl_bool_true;
2807 return isl_bool_false;
2810 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2811 enum isl_dim_type type, unsigned first, unsigned n)
2813 int i;
2815 if (!map)
2816 return isl_bool_error;
2818 if (first + n > isl_map_dim(map, type))
2819 isl_die(map->ctx, isl_error_invalid,
2820 "index out of bounds", return isl_bool_error);
2822 for (i = 0; i < map->n; ++i) {
2823 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2824 type, first, n);
2825 if (involves < 0 || involves)
2826 return involves;
2829 return isl_bool_false;
2832 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2833 enum isl_dim_type type, unsigned first, unsigned n)
2835 return isl_basic_map_involves_dims(bset, type, first, n);
2838 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2839 enum isl_dim_type type, unsigned first, unsigned n)
2841 return isl_map_involves_dims(set, type, first, n);
2844 /* Drop all constraints in bmap that involve any of the dimensions
2845 * first to first+n-1.
2847 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2848 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2850 int i;
2852 if (n == 0)
2853 return bmap;
2855 bmap = isl_basic_map_cow(bmap);
2857 if (!bmap)
2858 return NULL;
2860 for (i = bmap->n_eq - 1; i >= 0; --i) {
2861 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2862 continue;
2863 isl_basic_map_drop_equality(bmap, i);
2866 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2867 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2868 continue;
2869 isl_basic_map_drop_inequality(bmap, i);
2872 bmap = isl_basic_map_add_known_div_constraints(bmap);
2873 return bmap;
2876 /* Drop all constraints in bset that involve any of the dimensions
2877 * first to first+n-1.
2879 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2880 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2882 return isl_basic_map_drop_constraints_involving(bset, first, n);
2885 /* Drop all constraints in bmap that do not involve any of the dimensions
2886 * first to first + n - 1 of the given type.
2888 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2889 __isl_take isl_basic_map *bmap,
2890 enum isl_dim_type type, unsigned first, unsigned n)
2892 int i;
2894 if (n == 0) {
2895 isl_space *space = isl_basic_map_get_space(bmap);
2896 isl_basic_map_free(bmap);
2897 return isl_basic_map_universe(space);
2899 bmap = isl_basic_map_cow(bmap);
2900 if (!bmap)
2901 return NULL;
2903 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2904 return isl_basic_map_free(bmap);
2906 first += isl_basic_map_offset(bmap, type) - 1;
2908 for (i = bmap->n_eq - 1; i >= 0; --i) {
2909 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2910 continue;
2911 isl_basic_map_drop_equality(bmap, i);
2914 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2915 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2916 continue;
2917 isl_basic_map_drop_inequality(bmap, i);
2920 bmap = isl_basic_map_add_known_div_constraints(bmap);
2921 return bmap;
2924 /* Drop all constraints in bset that do not involve any of the dimensions
2925 * first to first + n - 1 of the given type.
2927 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2928 __isl_take isl_basic_set *bset,
2929 enum isl_dim_type type, unsigned first, unsigned n)
2931 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2932 type, first, n);
2935 /* Drop all constraints in bmap that involve any of the dimensions
2936 * first to first + n - 1 of the given type.
2938 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2939 __isl_take isl_basic_map *bmap,
2940 enum isl_dim_type type, unsigned first, unsigned n)
2942 if (!bmap)
2943 return NULL;
2944 if (n == 0)
2945 return bmap;
2947 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2948 return isl_basic_map_free(bmap);
2950 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2951 first += isl_basic_map_offset(bmap, type) - 1;
2952 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2955 /* Drop all constraints in bset that involve any of the dimensions
2956 * first to first + n - 1 of the given type.
2958 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2959 __isl_take isl_basic_set *bset,
2960 enum isl_dim_type type, unsigned first, unsigned n)
2962 return isl_basic_map_drop_constraints_involving_dims(bset,
2963 type, first, n);
2966 /* Drop constraints from "map" by applying "drop" to each basic map.
2968 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2969 enum isl_dim_type type, unsigned first, unsigned n,
2970 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2971 enum isl_dim_type type, unsigned first, unsigned n))
2973 int i;
2974 unsigned dim;
2976 if (!map)
2977 return NULL;
2979 dim = isl_map_dim(map, type);
2980 if (first + n > dim || first + n < first)
2981 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2982 "index out of bounds", return isl_map_free(map));
2984 map = isl_map_cow(map);
2985 if (!map)
2986 return NULL;
2988 for (i = 0; i < map->n; ++i) {
2989 map->p[i] = drop(map->p[i], type, first, n);
2990 if (!map->p[i])
2991 return isl_map_free(map);
2994 if (map->n > 1)
2995 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2997 return map;
3000 /* Drop all constraints in map that involve any of the dimensions
3001 * first to first + n - 1 of the given type.
3003 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3004 __isl_take isl_map *map,
3005 enum isl_dim_type type, unsigned first, unsigned n)
3007 if (n == 0)
3008 return map;
3009 return drop_constraints(map, type, first, n,
3010 &isl_basic_map_drop_constraints_involving_dims);
3013 /* Drop all constraints in "map" that do not involve any of the dimensions
3014 * first to first + n - 1 of the given type.
3016 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3017 __isl_take isl_map *map,
3018 enum isl_dim_type type, unsigned first, unsigned n)
3020 if (n == 0) {
3021 isl_space *space = isl_map_get_space(map);
3022 isl_map_free(map);
3023 return isl_map_universe(space);
3025 return drop_constraints(map, type, first, n,
3026 &isl_basic_map_drop_constraints_not_involving_dims);
3029 /* Drop all constraints in set that involve any of the dimensions
3030 * first to first + n - 1 of the given type.
3032 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3033 __isl_take isl_set *set,
3034 enum isl_dim_type type, unsigned first, unsigned n)
3036 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3039 /* Drop all constraints in "set" that do not involve any of the dimensions
3040 * first to first + n - 1 of the given type.
3042 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3043 __isl_take isl_set *set,
3044 enum isl_dim_type type, unsigned first, unsigned n)
3046 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3049 /* Does local variable "div" of "bmap" have a complete explicit representation?
3050 * Having a complete explicit representation requires not only
3051 * an explicit representation, but also that all local variables
3052 * that appear in this explicit representation in turn have
3053 * a complete explicit representation.
3055 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3057 int i;
3058 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3059 isl_bool marked;
3061 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3062 if (marked < 0 || marked)
3063 return isl_bool_not(marked);
3065 for (i = bmap->n_div - 1; i >= 0; --i) {
3066 isl_bool known;
3068 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3069 continue;
3070 known = isl_basic_map_div_is_known(bmap, i);
3071 if (known < 0 || !known)
3072 return known;
3075 return isl_bool_true;
3078 /* Remove all divs that are unknown or defined in terms of unknown divs.
3080 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3081 __isl_take isl_basic_map *bmap)
3083 int i;
3085 if (!bmap)
3086 return NULL;
3088 for (i = bmap->n_div - 1; i >= 0; --i) {
3089 if (isl_basic_map_div_is_known(bmap, i))
3090 continue;
3091 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3092 if (!bmap)
3093 return NULL;
3094 i = bmap->n_div;
3097 return bmap;
3100 /* Remove all divs that are unknown or defined in terms of unknown divs.
3102 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3103 __isl_take isl_basic_set *bset)
3105 return isl_basic_map_remove_unknown_divs(bset);
3108 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3110 int i;
3112 if (!map)
3113 return NULL;
3114 if (map->n == 0)
3115 return map;
3117 map = isl_map_cow(map);
3118 if (!map)
3119 return NULL;
3121 for (i = 0; i < map->n; ++i) {
3122 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3123 if (!map->p[i])
3124 goto error;
3126 return map;
3127 error:
3128 isl_map_free(map);
3129 return NULL;
3132 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3134 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3137 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3138 __isl_take isl_basic_set *bset,
3139 enum isl_dim_type type, unsigned first, unsigned n)
3141 isl_basic_map *bmap = bset_to_bmap(bset);
3142 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3143 return bset_from_bmap(bmap);
3146 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3147 enum isl_dim_type type, unsigned first, unsigned n)
3149 int i;
3151 if (n == 0)
3152 return map;
3154 map = isl_map_cow(map);
3155 if (!map)
3156 return NULL;
3157 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3159 for (i = 0; i < map->n; ++i) {
3160 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3161 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3162 if (!map->p[i])
3163 goto error;
3165 map = isl_map_drop(map, type, first, n);
3166 return map;
3167 error:
3168 isl_map_free(map);
3169 return NULL;
3172 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3173 enum isl_dim_type type, unsigned first, unsigned n)
3175 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3176 type, first, n));
3179 /* Project out n inputs starting at first using Fourier-Motzkin */
3180 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3181 unsigned first, unsigned n)
3183 return isl_map_remove_dims(map, isl_dim_in, first, n);
3186 static void dump_term(struct isl_basic_map *bmap,
3187 isl_int c, int pos, FILE *out)
3189 const char *name;
3190 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3191 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3192 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3193 if (!pos)
3194 isl_int_print(out, c, 0);
3195 else {
3196 if (!isl_int_is_one(c))
3197 isl_int_print(out, c, 0);
3198 if (pos < 1 + nparam) {
3199 name = isl_space_get_dim_name(bmap->dim,
3200 isl_dim_param, pos - 1);
3201 if (name)
3202 fprintf(out, "%s", name);
3203 else
3204 fprintf(out, "p%d", pos - 1);
3205 } else if (pos < 1 + nparam + in)
3206 fprintf(out, "i%d", pos - 1 - nparam);
3207 else if (pos < 1 + nparam + dim)
3208 fprintf(out, "o%d", pos - 1 - nparam - in);
3209 else
3210 fprintf(out, "e%d", pos - 1 - nparam - dim);
3214 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3215 int sign, FILE *out)
3217 int i;
3218 int first;
3219 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3220 isl_int v;
3222 isl_int_init(v);
3223 for (i = 0, first = 1; i < len; ++i) {
3224 if (isl_int_sgn(c[i]) * sign <= 0)
3225 continue;
3226 if (!first)
3227 fprintf(out, " + ");
3228 first = 0;
3229 isl_int_abs(v, c[i]);
3230 dump_term(bmap, v, i, out);
3232 isl_int_clear(v);
3233 if (first)
3234 fprintf(out, "0");
3237 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3238 const char *op, FILE *out, int indent)
3240 int i;
3242 fprintf(out, "%*s", indent, "");
3244 dump_constraint_sign(bmap, c, 1, out);
3245 fprintf(out, " %s ", op);
3246 dump_constraint_sign(bmap, c, -1, out);
3248 fprintf(out, "\n");
3250 for (i = bmap->n_div; i < bmap->extra; ++i) {
3251 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3252 continue;
3253 fprintf(out, "%*s", indent, "");
3254 fprintf(out, "ERROR: unused div coefficient not zero\n");
3255 abort();
3259 static void dump_constraints(struct isl_basic_map *bmap,
3260 isl_int **c, unsigned n,
3261 const char *op, FILE *out, int indent)
3263 int i;
3265 for (i = 0; i < n; ++i)
3266 dump_constraint(bmap, c[i], op, out, indent);
3269 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3271 int j;
3272 int first = 1;
3273 unsigned total = isl_basic_map_total_dim(bmap);
3275 for (j = 0; j < 1 + total; ++j) {
3276 if (isl_int_is_zero(exp[j]))
3277 continue;
3278 if (!first && isl_int_is_pos(exp[j]))
3279 fprintf(out, "+");
3280 dump_term(bmap, exp[j], j, out);
3281 first = 0;
3285 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3287 int i;
3289 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3290 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3292 for (i = 0; i < bmap->n_div; ++i) {
3293 fprintf(out, "%*s", indent, "");
3294 fprintf(out, "e%d = [(", i);
3295 dump_affine(bmap, bmap->div[i]+1, out);
3296 fprintf(out, ")/");
3297 isl_int_print(out, bmap->div[i][0], 0);
3298 fprintf(out, "]\n");
3302 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3303 FILE *out, int indent)
3305 if (!bset) {
3306 fprintf(out, "null basic set\n");
3307 return;
3310 fprintf(out, "%*s", indent, "");
3311 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3312 bset->ref, bset->dim->nparam, bset->dim->n_out,
3313 bset->extra, bset->flags);
3314 dump(bset_to_bmap(bset), out, indent);
3317 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3318 FILE *out, int indent)
3320 if (!bmap) {
3321 fprintf(out, "null basic map\n");
3322 return;
3325 fprintf(out, "%*s", indent, "");
3326 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3327 "flags: %x, n_name: %d\n",
3328 bmap->ref,
3329 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3330 bmap->extra, bmap->flags, bmap->dim->n_id);
3331 dump(bmap, out, indent);
3334 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3336 unsigned total;
3337 if (!bmap)
3338 return -1;
3339 total = isl_basic_map_total_dim(bmap);
3340 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3341 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3342 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3343 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3344 return 0;
3347 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3348 unsigned flags)
3350 if (!space)
3351 return NULL;
3352 if (isl_space_dim(space, isl_dim_in) != 0)
3353 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3354 "set cannot have input dimensions", goto error);
3355 return isl_map_alloc_space(space, n, flags);
3356 error:
3357 isl_space_free(space);
3358 return NULL;
3361 /* Make sure "map" has room for at least "n" more basic maps.
3363 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3365 int i;
3366 struct isl_map *grown = NULL;
3368 if (!map)
3369 return NULL;
3370 isl_assert(map->ctx, n >= 0, goto error);
3371 if (map->n + n <= map->size)
3372 return map;
3373 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3374 if (!grown)
3375 goto error;
3376 for (i = 0; i < map->n; ++i) {
3377 grown->p[i] = isl_basic_map_copy(map->p[i]);
3378 if (!grown->p[i])
3379 goto error;
3380 grown->n++;
3382 isl_map_free(map);
3383 return grown;
3384 error:
3385 isl_map_free(grown);
3386 isl_map_free(map);
3387 return NULL;
3390 /* Make sure "set" has room for at least "n" more basic sets.
3392 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3394 return set_from_map(isl_map_grow(set_to_map(set), n));
3397 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3399 return isl_map_from_basic_map(bset);
3402 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3404 struct isl_map *map;
3406 if (!bmap)
3407 return NULL;
3409 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3410 return isl_map_add_basic_map(map, bmap);
3413 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3414 __isl_take isl_basic_set *bset)
3416 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3417 bset_to_bmap(bset)));
3420 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3422 return isl_map_free(set);
3425 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3427 int i;
3429 if (!set) {
3430 fprintf(out, "null set\n");
3431 return;
3434 fprintf(out, "%*s", indent, "");
3435 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3436 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3437 set->flags);
3438 for (i = 0; i < set->n; ++i) {
3439 fprintf(out, "%*s", indent, "");
3440 fprintf(out, "basic set %d:\n", i);
3441 isl_basic_set_print_internal(set->p[i], out, indent+4);
3445 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3447 int i;
3449 if (!map) {
3450 fprintf(out, "null map\n");
3451 return;
3454 fprintf(out, "%*s", indent, "");
3455 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3456 "flags: %x, n_name: %d\n",
3457 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3458 map->dim->n_out, map->flags, map->dim->n_id);
3459 for (i = 0; i < map->n; ++i) {
3460 fprintf(out, "%*s", indent, "");
3461 fprintf(out, "basic map %d:\n", i);
3462 isl_basic_map_print_internal(map->p[i], out, indent+4);
3466 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3467 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3469 struct isl_basic_map *bmap_domain;
3471 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3472 goto error;
3474 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3475 isl_assert(bset->ctx,
3476 isl_basic_map_compatible_domain(bmap, bset), goto error);
3478 bmap = isl_basic_map_cow(bmap);
3479 if (!bmap)
3480 goto error;
3481 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3482 bset->n_div, bset->n_eq, bset->n_ineq);
3483 bmap_domain = isl_basic_map_from_domain(bset);
3484 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3486 bmap = isl_basic_map_simplify(bmap);
3487 return isl_basic_map_finalize(bmap);
3488 error:
3489 isl_basic_map_free(bmap);
3490 isl_basic_set_free(bset);
3491 return NULL;
3494 /* Check that the space of "bset" is the same as that of the range of "bmap".
3496 static isl_stat isl_basic_map_check_compatible_range(
3497 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3499 isl_bool ok;
3501 ok = isl_basic_map_compatible_range(bmap, bset);
3502 if (ok < 0)
3503 return isl_stat_error;
3504 if (!ok)
3505 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3506 "incompatible spaces", return isl_stat_error);
3508 return isl_stat_ok;
3511 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3512 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3514 struct isl_basic_map *bmap_range;
3516 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3517 goto error;
3519 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3520 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3521 goto error;
3523 if (isl_basic_set_plain_is_universe(bset)) {
3524 isl_basic_set_free(bset);
3525 return bmap;
3528 bmap = isl_basic_map_cow(bmap);
3529 if (!bmap)
3530 goto error;
3531 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3532 bset->n_div, bset->n_eq, bset->n_ineq);
3533 bmap_range = bset_to_bmap(bset);
3534 bmap = add_constraints(bmap, bmap_range, 0, 0);
3536 bmap = isl_basic_map_simplify(bmap);
3537 return isl_basic_map_finalize(bmap);
3538 error:
3539 isl_basic_map_free(bmap);
3540 isl_basic_set_free(bset);
3541 return NULL;
3544 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3545 __isl_keep isl_vec *vec)
3547 int i;
3548 unsigned total;
3549 isl_int s;
3551 if (!bmap || !vec)
3552 return isl_bool_error;
3554 total = 1 + isl_basic_map_total_dim(bmap);
3555 if (total != vec->size)
3556 return isl_bool_false;
3558 isl_int_init(s);
3560 for (i = 0; i < bmap->n_eq; ++i) {
3561 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3562 if (!isl_int_is_zero(s)) {
3563 isl_int_clear(s);
3564 return isl_bool_false;
3568 for (i = 0; i < bmap->n_ineq; ++i) {
3569 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3570 if (isl_int_is_neg(s)) {
3571 isl_int_clear(s);
3572 return isl_bool_false;
3576 isl_int_clear(s);
3578 return isl_bool_true;
3581 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3582 __isl_keep isl_vec *vec)
3584 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3587 __isl_give isl_basic_map *isl_basic_map_intersect(
3588 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3590 struct isl_vec *sample = NULL;
3592 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3593 goto error;
3594 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3595 isl_space_dim(bmap1->dim, isl_dim_param) &&
3596 isl_space_dim(bmap2->dim, isl_dim_all) !=
3597 isl_space_dim(bmap2->dim, isl_dim_param))
3598 return isl_basic_map_intersect(bmap2, bmap1);
3600 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3601 isl_space_dim(bmap2->dim, isl_dim_param))
3602 isl_assert(bmap1->ctx,
3603 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3605 if (isl_basic_map_plain_is_empty(bmap1)) {
3606 isl_basic_map_free(bmap2);
3607 return bmap1;
3609 if (isl_basic_map_plain_is_empty(bmap2)) {
3610 isl_basic_map_free(bmap1);
3611 return bmap2;
3614 if (bmap1->sample &&
3615 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3616 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3617 sample = isl_vec_copy(bmap1->sample);
3618 else if (bmap2->sample &&
3619 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3620 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3621 sample = isl_vec_copy(bmap2->sample);
3623 bmap1 = isl_basic_map_cow(bmap1);
3624 if (!bmap1)
3625 goto error;
3626 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3627 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3628 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3630 if (!bmap1)
3631 isl_vec_free(sample);
3632 else if (sample) {
3633 isl_vec_free(bmap1->sample);
3634 bmap1->sample = sample;
3637 bmap1 = isl_basic_map_simplify(bmap1);
3638 return isl_basic_map_finalize(bmap1);
3639 error:
3640 if (sample)
3641 isl_vec_free(sample);
3642 isl_basic_map_free(bmap1);
3643 isl_basic_map_free(bmap2);
3644 return NULL;
3647 struct isl_basic_set *isl_basic_set_intersect(
3648 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3650 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3651 bset_to_bmap(bset2)));
3654 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3655 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3657 return isl_basic_set_intersect(bset1, bset2);
3660 /* Special case of isl_map_intersect, where both map1 and map2
3661 * are convex, without any divs and such that either map1 or map2
3662 * contains a single constraint. This constraint is then simply
3663 * added to the other map.
3665 static __isl_give isl_map *map_intersect_add_constraint(
3666 __isl_take isl_map *map1, __isl_take isl_map *map2)
3668 isl_assert(map1->ctx, map1->n == 1, goto error);
3669 isl_assert(map2->ctx, map1->n == 1, goto error);
3670 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3671 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3673 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3674 return isl_map_intersect(map2, map1);
3676 map1 = isl_map_cow(map1);
3677 if (!map1)
3678 goto error;
3679 if (isl_map_plain_is_empty(map1)) {
3680 isl_map_free(map2);
3681 return map1;
3683 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3684 if (map2->p[0]->n_eq == 1)
3685 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3686 else
3687 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3688 map2->p[0]->ineq[0]);
3690 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3691 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3692 if (!map1->p[0])
3693 goto error;
3695 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3696 isl_basic_map_free(map1->p[0]);
3697 map1->n = 0;
3700 isl_map_free(map2);
3702 return map1;
3703 error:
3704 isl_map_free(map1);
3705 isl_map_free(map2);
3706 return NULL;
3709 /* map2 may be either a parameter domain or a map living in the same
3710 * space as map1.
3712 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3713 __isl_take isl_map *map2)
3715 unsigned flags = 0;
3716 isl_map *result;
3717 int i, j;
3719 if (!map1 || !map2)
3720 goto error;
3722 if ((isl_map_plain_is_empty(map1) ||
3723 isl_map_plain_is_universe(map2)) &&
3724 isl_space_is_equal(map1->dim, map2->dim)) {
3725 isl_map_free(map2);
3726 return map1;
3728 if ((isl_map_plain_is_empty(map2) ||
3729 isl_map_plain_is_universe(map1)) &&
3730 isl_space_is_equal(map1->dim, map2->dim)) {
3731 isl_map_free(map1);
3732 return map2;
3735 if (map1->n == 1 && map2->n == 1 &&
3736 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3737 isl_space_is_equal(map1->dim, map2->dim) &&
3738 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3739 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3740 return map_intersect_add_constraint(map1, map2);
3742 if (isl_space_dim(map2->dim, isl_dim_all) !=
3743 isl_space_dim(map2->dim, isl_dim_param))
3744 isl_assert(map1->ctx,
3745 isl_space_is_equal(map1->dim, map2->dim), goto error);
3747 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3748 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3749 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3751 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3752 map1->n * map2->n, flags);
3753 if (!result)
3754 goto error;
3755 for (i = 0; i < map1->n; ++i)
3756 for (j = 0; j < map2->n; ++j) {
3757 struct isl_basic_map *part;
3758 part = isl_basic_map_intersect(
3759 isl_basic_map_copy(map1->p[i]),
3760 isl_basic_map_copy(map2->p[j]));
3761 if (isl_basic_map_is_empty(part) < 0)
3762 part = isl_basic_map_free(part);
3763 result = isl_map_add_basic_map(result, part);
3764 if (!result)
3765 goto error;
3767 isl_map_free(map1);
3768 isl_map_free(map2);
3769 return result;
3770 error:
3771 isl_map_free(map1);
3772 isl_map_free(map2);
3773 return NULL;
3776 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3777 __isl_take isl_map *map2)
3779 if (!map1 || !map2)
3780 goto error;
3781 if (!isl_space_is_equal(map1->dim, map2->dim))
3782 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3783 "spaces don't match", goto error);
3784 return map_intersect_internal(map1, map2);
3785 error:
3786 isl_map_free(map1);
3787 isl_map_free(map2);
3788 return NULL;
3791 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3792 __isl_take isl_map *map2)
3794 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3797 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3799 return set_from_map(isl_map_intersect(set_to_map(set1),
3800 set_to_map(set2)));
3803 /* map_intersect_internal accepts intersections
3804 * with parameter domains, so we can just call that function.
3806 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3807 __isl_take isl_set *params)
3809 return map_intersect_internal(map, params);
3812 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3813 __isl_take isl_map *map2)
3815 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3818 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3819 __isl_take isl_set *params)
3821 return isl_map_intersect_params(set, params);
3824 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3826 isl_space *space;
3827 unsigned pos, n1, n2;
3829 if (!bmap)
3830 return NULL;
3831 bmap = isl_basic_map_cow(bmap);
3832 if (!bmap)
3833 return NULL;
3834 space = isl_space_reverse(isl_space_copy(bmap->dim));
3835 pos = isl_basic_map_offset(bmap, isl_dim_in);
3836 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3837 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3838 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3839 return isl_basic_map_reset_space(bmap, space);
3842 static __isl_give isl_basic_map *basic_map_space_reset(
3843 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3845 isl_space *space;
3847 if (!bmap)
3848 return NULL;
3849 if (!isl_space_is_named_or_nested(bmap->dim, type))
3850 return bmap;
3852 space = isl_basic_map_get_space(bmap);
3853 space = isl_space_reset(space, type);
3854 bmap = isl_basic_map_reset_space(bmap, space);
3855 return bmap;
3858 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3859 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3860 unsigned pos, unsigned n)
3862 isl_bool rational;
3863 isl_space *res_dim;
3864 struct isl_basic_map *res;
3865 struct isl_dim_map *dim_map;
3866 unsigned total, off;
3867 enum isl_dim_type t;
3869 if (n == 0)
3870 return basic_map_space_reset(bmap, type);
3872 if (!bmap)
3873 return NULL;
3875 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3877 total = isl_basic_map_total_dim(bmap) + n;
3878 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3879 off = 0;
3880 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3881 if (t != type) {
3882 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3883 } else {
3884 unsigned size = isl_basic_map_dim(bmap, t);
3885 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3886 0, pos, off);
3887 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3888 pos, size - pos, off + pos + n);
3890 off += isl_space_dim(res_dim, t);
3892 isl_dim_map_div(dim_map, bmap, off);
3894 res = isl_basic_map_alloc_space(res_dim,
3895 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3896 rational = isl_basic_map_is_rational(bmap);
3897 if (rational < 0)
3898 res = isl_basic_map_free(res);
3899 if (rational)
3900 res = isl_basic_map_set_rational(res);
3901 if (isl_basic_map_plain_is_empty(bmap)) {
3902 isl_basic_map_free(bmap);
3903 free(dim_map);
3904 return isl_basic_map_set_to_empty(res);
3906 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3907 return isl_basic_map_finalize(res);
3910 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3911 __isl_take isl_basic_set *bset,
3912 enum isl_dim_type type, unsigned pos, unsigned n)
3914 return isl_basic_map_insert_dims(bset, type, pos, n);
3917 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3918 enum isl_dim_type type, unsigned n)
3920 if (!bmap)
3921 return NULL;
3922 return isl_basic_map_insert_dims(bmap, type,
3923 isl_basic_map_dim(bmap, type), n);
3926 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3927 enum isl_dim_type type, unsigned n)
3929 if (!bset)
3930 return NULL;
3931 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3932 return isl_basic_map_add_dims(bset, type, n);
3933 error:
3934 isl_basic_set_free(bset);
3935 return NULL;
3938 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3939 enum isl_dim_type type)
3941 isl_space *space;
3943 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3944 return map;
3946 space = isl_map_get_space(map);
3947 space = isl_space_reset(space, type);
3948 map = isl_map_reset_space(map, space);
3949 return map;
3952 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3953 enum isl_dim_type type, unsigned pos, unsigned n)
3955 int i;
3957 if (n == 0)
3958 return map_space_reset(map, type);
3960 map = isl_map_cow(map);
3961 if (!map)
3962 return NULL;
3964 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3965 if (!map->dim)
3966 goto error;
3968 for (i = 0; i < map->n; ++i) {
3969 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3970 if (!map->p[i])
3971 goto error;
3974 return map;
3975 error:
3976 isl_map_free(map);
3977 return NULL;
3980 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3981 enum isl_dim_type type, unsigned pos, unsigned n)
3983 return isl_map_insert_dims(set, type, pos, n);
3986 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3987 enum isl_dim_type type, unsigned n)
3989 if (!map)
3990 return NULL;
3991 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3994 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3995 enum isl_dim_type type, unsigned n)
3997 if (!set)
3998 return NULL;
3999 isl_assert(set->ctx, type != isl_dim_in, goto error);
4000 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4001 error:
4002 isl_set_free(set);
4003 return NULL;
4006 __isl_give isl_basic_map *isl_basic_map_move_dims(
4007 __isl_take isl_basic_map *bmap,
4008 enum isl_dim_type dst_type, unsigned dst_pos,
4009 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4011 struct isl_dim_map *dim_map;
4012 struct isl_basic_map *res;
4013 enum isl_dim_type t;
4014 unsigned total, off;
4016 if (!bmap)
4017 return NULL;
4018 if (n == 0) {
4019 bmap = isl_basic_map_reset(bmap, src_type);
4020 bmap = isl_basic_map_reset(bmap, dst_type);
4021 return bmap;
4024 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4025 return isl_basic_map_free(bmap);
4027 if (dst_type == src_type && dst_pos == src_pos)
4028 return bmap;
4030 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4032 if (pos(bmap->dim, dst_type) + dst_pos ==
4033 pos(bmap->dim, src_type) + src_pos +
4034 ((src_type < dst_type) ? n : 0)) {
4035 bmap = isl_basic_map_cow(bmap);
4036 if (!bmap)
4037 return NULL;
4039 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4040 src_type, src_pos, n);
4041 if (!bmap->dim)
4042 goto error;
4044 bmap = isl_basic_map_finalize(bmap);
4046 return bmap;
4049 total = isl_basic_map_total_dim(bmap);
4050 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4052 off = 0;
4053 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4054 unsigned size = isl_space_dim(bmap->dim, t);
4055 if (t == dst_type) {
4056 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4057 0, dst_pos, off);
4058 off += dst_pos;
4059 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4060 src_pos, n, off);
4061 off += n;
4062 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4063 dst_pos, size - dst_pos, off);
4064 off += size - dst_pos;
4065 } else if (t == src_type) {
4066 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4067 0, src_pos, off);
4068 off += src_pos;
4069 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4070 src_pos + n, size - src_pos - n, off);
4071 off += size - src_pos - n;
4072 } else {
4073 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4074 off += size;
4077 isl_dim_map_div(dim_map, bmap, off);
4079 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4080 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4081 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4082 if (!bmap)
4083 goto error;
4085 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4086 src_type, src_pos, n);
4087 if (!bmap->dim)
4088 goto error;
4090 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
4091 bmap = isl_basic_map_gauss(bmap, NULL);
4092 bmap = isl_basic_map_finalize(bmap);
4094 return bmap;
4095 error:
4096 isl_basic_map_free(bmap);
4097 return NULL;
4100 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4101 enum isl_dim_type dst_type, unsigned dst_pos,
4102 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4104 isl_basic_map *bmap = bset_to_bmap(bset);
4105 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4106 src_type, src_pos, n);
4107 return bset_from_bmap(bmap);
4110 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4111 enum isl_dim_type dst_type, unsigned dst_pos,
4112 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4114 if (!set)
4115 return NULL;
4116 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4117 return set_from_map(isl_map_move_dims(set_to_map(set),
4118 dst_type, dst_pos, src_type, src_pos, n));
4119 error:
4120 isl_set_free(set);
4121 return NULL;
4124 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4125 enum isl_dim_type dst_type, unsigned dst_pos,
4126 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4128 int i;
4130 if (!map)
4131 return NULL;
4132 if (n == 0) {
4133 map = isl_map_reset(map, src_type);
4134 map = isl_map_reset(map, dst_type);
4135 return map;
4138 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
4139 goto error);
4141 if (dst_type == src_type && dst_pos == src_pos)
4142 return map;
4144 isl_assert(map->ctx, dst_type != src_type, goto error);
4146 map = isl_map_cow(map);
4147 if (!map)
4148 return NULL;
4150 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4151 if (!map->dim)
4152 goto error;
4154 for (i = 0; i < map->n; ++i) {
4155 map->p[i] = isl_basic_map_move_dims(map->p[i],
4156 dst_type, dst_pos,
4157 src_type, src_pos, n);
4158 if (!map->p[i])
4159 goto error;
4162 return map;
4163 error:
4164 isl_map_free(map);
4165 return NULL;
4168 /* Move the specified dimensions to the last columns right before
4169 * the divs. Don't change the dimension specification of bmap.
4170 * That's the responsibility of the caller.
4172 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4173 enum isl_dim_type type, unsigned first, unsigned n)
4175 struct isl_dim_map *dim_map;
4176 struct isl_basic_map *res;
4177 enum isl_dim_type t;
4178 unsigned total, off;
4180 if (!bmap)
4181 return NULL;
4182 if (pos(bmap->dim, type) + first + n ==
4183 1 + isl_space_dim(bmap->dim, isl_dim_all))
4184 return bmap;
4186 total = isl_basic_map_total_dim(bmap);
4187 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4189 off = 0;
4190 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4191 unsigned size = isl_space_dim(bmap->dim, t);
4192 if (t == type) {
4193 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4194 0, first, off);
4195 off += first;
4196 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4197 first, n, total - bmap->n_div - n);
4198 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4199 first + n, size - (first + n), off);
4200 off += size - (first + n);
4201 } else {
4202 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4203 off += size;
4206 isl_dim_map_div(dim_map, bmap, off + n);
4208 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4209 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4210 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4211 return res;
4214 /* Insert "n" rows in the divs of "bmap".
4216 * The number of columns is not changed, which means that the last
4217 * dimensions of "bmap" are being reintepreted as the new divs.
4218 * The space of "bmap" is not adjusted, however, which means
4219 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4220 * from the space of "bmap" is the responsibility of the caller.
4222 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4223 int n)
4225 int i;
4226 size_t row_size;
4227 isl_int **new_div;
4228 isl_int *old;
4230 bmap = isl_basic_map_cow(bmap);
4231 if (!bmap)
4232 return NULL;
4234 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4235 old = bmap->block2.data;
4236 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4237 (bmap->extra + n) * (1 + row_size));
4238 if (!bmap->block2.data)
4239 return isl_basic_map_free(bmap);
4240 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4241 if (!new_div)
4242 return isl_basic_map_free(bmap);
4243 for (i = 0; i < n; ++i) {
4244 new_div[i] = bmap->block2.data +
4245 (bmap->extra + i) * (1 + row_size);
4246 isl_seq_clr(new_div[i], 1 + row_size);
4248 for (i = 0; i < bmap->extra; ++i)
4249 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4250 free(bmap->div);
4251 bmap->div = new_div;
4252 bmap->n_div += n;
4253 bmap->extra += n;
4255 return bmap;
4258 /* Drop constraints from "bmap" that only involve the variables
4259 * of "type" in the range [first, first + n] that are not related
4260 * to any of the variables outside that interval.
4261 * These constraints cannot influence the values for the variables
4262 * outside the interval, except in case they cause "bmap" to be empty.
4263 * Only drop the constraints if "bmap" is known to be non-empty.
4265 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4266 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4267 unsigned first, unsigned n)
4269 int i;
4270 int *groups;
4271 unsigned dim, n_div;
4272 isl_bool non_empty;
4274 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4275 if (non_empty < 0)
4276 return isl_basic_map_free(bmap);
4277 if (!non_empty)
4278 return bmap;
4280 dim = isl_basic_map_dim(bmap, isl_dim_all);
4281 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4282 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4283 if (!groups)
4284 return isl_basic_map_free(bmap);
4285 first += isl_basic_map_offset(bmap, type) - 1;
4286 for (i = 0; i < first; ++i)
4287 groups[i] = -1;
4288 for (i = first + n; i < dim - n_div; ++i)
4289 groups[i] = -1;
4291 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4293 return bmap;
4296 /* Turn the n dimensions of type type, starting at first
4297 * into existentially quantified variables.
4299 * If a subset of the projected out variables are unrelated
4300 * to any of the variables that remain, then the constraints
4301 * involving this subset are simply dropped first.
4303 __isl_give isl_basic_map *isl_basic_map_project_out(
4304 __isl_take isl_basic_map *bmap,
4305 enum isl_dim_type type, unsigned first, unsigned n)
4307 isl_bool empty;
4309 if (n == 0)
4310 return basic_map_space_reset(bmap, type);
4311 if (type == isl_dim_div)
4312 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4313 "cannot project out existentially quantified variables",
4314 return isl_basic_map_free(bmap));
4316 empty = isl_basic_map_plain_is_empty(bmap);
4317 if (empty < 0)
4318 return isl_basic_map_free(bmap);
4319 if (empty)
4320 bmap = isl_basic_map_set_to_empty(bmap);
4322 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4323 if (!bmap)
4324 return NULL;
4326 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4327 return isl_basic_map_remove_dims(bmap, type, first, n);
4329 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4330 return isl_basic_map_free(bmap);
4332 bmap = move_last(bmap, type, first, n);
4333 bmap = isl_basic_map_cow(bmap);
4334 bmap = insert_div_rows(bmap, n);
4335 if (!bmap)
4336 return NULL;
4338 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4339 if (!bmap->dim)
4340 goto error;
4341 bmap = isl_basic_map_simplify(bmap);
4342 bmap = isl_basic_map_drop_redundant_divs(bmap);
4343 return isl_basic_map_finalize(bmap);
4344 error:
4345 isl_basic_map_free(bmap);
4346 return NULL;
4349 /* Turn the n dimensions of type type, starting at first
4350 * into existentially quantified variables.
4352 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4353 enum isl_dim_type type, unsigned first, unsigned n)
4355 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4356 type, first, n));
4359 /* Turn the n dimensions of type type, starting at first
4360 * into existentially quantified variables.
4362 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4363 enum isl_dim_type type, unsigned first, unsigned n)
4365 int i;
4367 if (!map)
4368 return NULL;
4370 if (n == 0)
4371 return map_space_reset(map, type);
4373 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
4375 map = isl_map_cow(map);
4376 if (!map)
4377 return NULL;
4379 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4380 if (!map->dim)
4381 goto error;
4383 for (i = 0; i < map->n; ++i) {
4384 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4385 if (!map->p[i])
4386 goto error;
4389 return map;
4390 error:
4391 isl_map_free(map);
4392 return NULL;
4395 /* Turn the n dimensions of type type, starting at first
4396 * into existentially quantified variables.
4398 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4399 enum isl_dim_type type, unsigned first, unsigned n)
4401 return set_from_map(isl_map_project_out(set_to_map(set),
4402 type, first, n));
4405 /* Return a map that projects the elements in "set" onto their
4406 * "n" set dimensions starting at "first".
4407 * "type" should be equal to isl_dim_set.
4409 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4410 enum isl_dim_type type, unsigned first, unsigned n)
4412 int i;
4413 int dim;
4414 isl_map *map;
4416 if (!set)
4417 return NULL;
4418 if (type != isl_dim_set)
4419 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4420 "only set dimensions can be projected out", goto error);
4421 dim = isl_set_dim(set, isl_dim_set);
4422 if (first + n > dim || first + n < first)
4423 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4424 "index out of bounds", goto error);
4426 map = isl_map_from_domain(set);
4427 map = isl_map_add_dims(map, isl_dim_out, n);
4428 for (i = 0; i < n; ++i)
4429 map = isl_map_equate(map, isl_dim_in, first + i,
4430 isl_dim_out, i);
4431 return map;
4432 error:
4433 isl_set_free(set);
4434 return NULL;
4437 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4439 int i, j;
4441 for (i = 0; i < n; ++i) {
4442 j = isl_basic_map_alloc_div(bmap);
4443 if (j < 0)
4444 goto error;
4445 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4447 return bmap;
4448 error:
4449 isl_basic_map_free(bmap);
4450 return NULL;
4453 struct isl_basic_map *isl_basic_map_apply_range(
4454 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4456 isl_space *dim_result = NULL;
4457 struct isl_basic_map *bmap;
4458 unsigned n_in, n_out, n, nparam, total, pos;
4459 struct isl_dim_map *dim_map1, *dim_map2;
4461 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4462 goto error;
4463 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4464 bmap2->dim, isl_dim_in))
4465 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4466 "spaces don't match", goto error);
4468 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4469 isl_space_copy(bmap2->dim));
4471 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4472 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4473 n = isl_basic_map_dim(bmap1, isl_dim_out);
4474 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4476 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4477 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4478 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4479 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4480 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4481 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4482 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4483 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4484 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4485 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4486 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4488 bmap = isl_basic_map_alloc_space(dim_result,
4489 bmap1->n_div + bmap2->n_div + n,
4490 bmap1->n_eq + bmap2->n_eq,
4491 bmap1->n_ineq + bmap2->n_ineq);
4492 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4493 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4494 bmap = add_divs(bmap, n);
4495 bmap = isl_basic_map_simplify(bmap);
4496 bmap = isl_basic_map_drop_redundant_divs(bmap);
4497 return isl_basic_map_finalize(bmap);
4498 error:
4499 isl_basic_map_free(bmap1);
4500 isl_basic_map_free(bmap2);
4501 return NULL;
4504 struct isl_basic_set *isl_basic_set_apply(
4505 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4507 if (!bset || !bmap)
4508 goto error;
4510 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4511 goto error);
4513 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4514 bmap));
4515 error:
4516 isl_basic_set_free(bset);
4517 isl_basic_map_free(bmap);
4518 return NULL;
4521 struct isl_basic_map *isl_basic_map_apply_domain(
4522 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4524 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4525 goto error;
4526 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4527 bmap2->dim, isl_dim_in))
4528 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4529 "spaces don't match", goto error);
4531 bmap1 = isl_basic_map_reverse(bmap1);
4532 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4533 return isl_basic_map_reverse(bmap1);
4534 error:
4535 isl_basic_map_free(bmap1);
4536 isl_basic_map_free(bmap2);
4537 return NULL;
4540 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4541 * A \cap B -> f(A) + f(B)
4543 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4544 __isl_take isl_basic_map *bmap2)
4546 unsigned n_in, n_out, nparam, total, pos;
4547 struct isl_basic_map *bmap = NULL;
4548 struct isl_dim_map *dim_map1, *dim_map2;
4549 int i;
4551 if (!bmap1 || !bmap2)
4552 goto error;
4554 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4555 goto error);
4557 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4558 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4559 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4561 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4562 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4563 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4564 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4565 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4566 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4567 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4568 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4569 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4570 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4571 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4573 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4574 bmap1->n_div + bmap2->n_div + 2 * n_out,
4575 bmap1->n_eq + bmap2->n_eq + n_out,
4576 bmap1->n_ineq + bmap2->n_ineq);
4577 for (i = 0; i < n_out; ++i) {
4578 int j = isl_basic_map_alloc_equality(bmap);
4579 if (j < 0)
4580 goto error;
4581 isl_seq_clr(bmap->eq[j], 1+total);
4582 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4583 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4584 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4586 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4587 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4588 bmap = add_divs(bmap, 2 * n_out);
4590 bmap = isl_basic_map_simplify(bmap);
4591 return isl_basic_map_finalize(bmap);
4592 error:
4593 isl_basic_map_free(bmap);
4594 isl_basic_map_free(bmap1);
4595 isl_basic_map_free(bmap2);
4596 return NULL;
4599 /* Given two maps A -> f(A) and B -> g(B), construct a map
4600 * A \cap B -> f(A) + f(B)
4602 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4603 __isl_take isl_map *map2)
4605 struct isl_map *result;
4606 int i, j;
4608 if (!map1 || !map2)
4609 goto error;
4611 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4613 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4614 map1->n * map2->n, 0);
4615 if (!result)
4616 goto error;
4617 for (i = 0; i < map1->n; ++i)
4618 for (j = 0; j < map2->n; ++j) {
4619 struct isl_basic_map *part;
4620 part = isl_basic_map_sum(
4621 isl_basic_map_copy(map1->p[i]),
4622 isl_basic_map_copy(map2->p[j]));
4623 if (isl_basic_map_is_empty(part))
4624 isl_basic_map_free(part);
4625 else
4626 result = isl_map_add_basic_map(result, part);
4627 if (!result)
4628 goto error;
4630 isl_map_free(map1);
4631 isl_map_free(map2);
4632 return result;
4633 error:
4634 isl_map_free(map1);
4635 isl_map_free(map2);
4636 return NULL;
4639 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4640 __isl_take isl_set *set2)
4642 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4645 /* Given a basic map A -> f(A), construct A -> -f(A).
4647 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4649 int i, j;
4650 unsigned off, n;
4652 bmap = isl_basic_map_cow(bmap);
4653 if (!bmap)
4654 return NULL;
4656 n = isl_basic_map_dim(bmap, isl_dim_out);
4657 off = isl_basic_map_offset(bmap, isl_dim_out);
4658 for (i = 0; i < bmap->n_eq; ++i)
4659 for (j = 0; j < n; ++j)
4660 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4661 for (i = 0; i < bmap->n_ineq; ++i)
4662 for (j = 0; j < n; ++j)
4663 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4664 for (i = 0; i < bmap->n_div; ++i)
4665 for (j = 0; j < n; ++j)
4666 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4667 bmap = isl_basic_map_gauss(bmap, NULL);
4668 return isl_basic_map_finalize(bmap);
4671 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4673 return isl_basic_map_neg(bset);
4676 /* Given a map A -> f(A), construct A -> -f(A).
4678 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4680 int i;
4682 map = isl_map_cow(map);
4683 if (!map)
4684 return NULL;
4686 for (i = 0; i < map->n; ++i) {
4687 map->p[i] = isl_basic_map_neg(map->p[i]);
4688 if (!map->p[i])
4689 goto error;
4692 return map;
4693 error:
4694 isl_map_free(map);
4695 return NULL;
4698 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4700 return set_from_map(isl_map_neg(set_to_map(set)));
4703 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4704 * A -> floor(f(A)/d).
4706 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4707 isl_int d)
4709 unsigned n_in, n_out, nparam, total, pos;
4710 struct isl_basic_map *result = NULL;
4711 struct isl_dim_map *dim_map;
4712 int i;
4714 if (!bmap)
4715 return NULL;
4717 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4718 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4719 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4721 total = nparam + n_in + n_out + bmap->n_div + n_out;
4722 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4723 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4724 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4725 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4726 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4728 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4729 bmap->n_div + n_out,
4730 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4731 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4732 result = add_divs(result, n_out);
4733 for (i = 0; i < n_out; ++i) {
4734 int j;
4735 j = isl_basic_map_alloc_inequality(result);
4736 if (j < 0)
4737 goto error;
4738 isl_seq_clr(result->ineq[j], 1+total);
4739 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4740 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4741 j = isl_basic_map_alloc_inequality(result);
4742 if (j < 0)
4743 goto error;
4744 isl_seq_clr(result->ineq[j], 1+total);
4745 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4746 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4747 isl_int_sub_ui(result->ineq[j][0], d, 1);
4750 result = isl_basic_map_simplify(result);
4751 return isl_basic_map_finalize(result);
4752 error:
4753 isl_basic_map_free(result);
4754 return NULL;
4757 /* Given a map A -> f(A) and an integer d, construct a map
4758 * A -> floor(f(A)/d).
4760 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4762 int i;
4764 map = isl_map_cow(map);
4765 if (!map)
4766 return NULL;
4768 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4769 for (i = 0; i < map->n; ++i) {
4770 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4771 if (!map->p[i])
4772 goto error;
4774 map = isl_map_unmark_normalized(map);
4776 return map;
4777 error:
4778 isl_map_free(map);
4779 return NULL;
4782 /* Given a map A -> f(A) and an integer d, construct a map
4783 * A -> floor(f(A)/d).
4785 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4786 __isl_take isl_val *d)
4788 if (!map || !d)
4789 goto error;
4790 if (!isl_val_is_int(d))
4791 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4792 "expecting integer denominator", goto error);
4793 map = isl_map_floordiv(map, d->n);
4794 isl_val_free(d);
4795 return map;
4796 error:
4797 isl_map_free(map);
4798 isl_val_free(d);
4799 return NULL;
4802 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4803 unsigned pos)
4805 int i;
4806 unsigned nparam;
4807 unsigned n_in;
4809 i = isl_basic_map_alloc_equality(bmap);
4810 if (i < 0)
4811 goto error;
4812 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4813 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4814 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4815 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4816 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4817 return isl_basic_map_finalize(bmap);
4818 error:
4819 isl_basic_map_free(bmap);
4820 return NULL;
4823 /* Add a constraint to "bmap" expressing i_pos < o_pos
4825 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4826 unsigned pos)
4828 int i;
4829 unsigned nparam;
4830 unsigned n_in;
4832 i = isl_basic_map_alloc_inequality(bmap);
4833 if (i < 0)
4834 goto error;
4835 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4836 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4837 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4838 isl_int_set_si(bmap->ineq[i][0], -1);
4839 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4840 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4841 return isl_basic_map_finalize(bmap);
4842 error:
4843 isl_basic_map_free(bmap);
4844 return NULL;
4847 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4849 static __isl_give isl_basic_map *var_less_or_equal(
4850 __isl_take isl_basic_map *bmap, unsigned pos)
4852 int i;
4853 unsigned nparam;
4854 unsigned n_in;
4856 i = isl_basic_map_alloc_inequality(bmap);
4857 if (i < 0)
4858 goto error;
4859 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4860 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4861 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4862 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4863 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4864 return isl_basic_map_finalize(bmap);
4865 error:
4866 isl_basic_map_free(bmap);
4867 return NULL;
4870 /* Add a constraint to "bmap" expressing i_pos > o_pos
4872 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4873 unsigned pos)
4875 int i;
4876 unsigned nparam;
4877 unsigned n_in;
4879 i = isl_basic_map_alloc_inequality(bmap);
4880 if (i < 0)
4881 goto error;
4882 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4883 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4884 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4885 isl_int_set_si(bmap->ineq[i][0], -1);
4886 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4887 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4888 return isl_basic_map_finalize(bmap);
4889 error:
4890 isl_basic_map_free(bmap);
4891 return NULL;
4894 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4896 static __isl_give isl_basic_map *var_more_or_equal(
4897 __isl_take isl_basic_map *bmap, unsigned pos)
4899 int i;
4900 unsigned nparam;
4901 unsigned n_in;
4903 i = isl_basic_map_alloc_inequality(bmap);
4904 if (i < 0)
4905 goto error;
4906 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4907 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4908 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4909 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4910 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4911 return isl_basic_map_finalize(bmap);
4912 error:
4913 isl_basic_map_free(bmap);
4914 return NULL;
4917 __isl_give isl_basic_map *isl_basic_map_equal(
4918 __isl_take isl_space *dim, unsigned n_equal)
4920 int i;
4921 struct isl_basic_map *bmap;
4922 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4923 if (!bmap)
4924 return NULL;
4925 for (i = 0; i < n_equal && bmap; ++i)
4926 bmap = var_equal(bmap, i);
4927 return isl_basic_map_finalize(bmap);
4930 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4932 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4933 unsigned pos)
4935 int i;
4936 struct isl_basic_map *bmap;
4937 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4938 if (!bmap)
4939 return NULL;
4940 for (i = 0; i < pos && bmap; ++i)
4941 bmap = var_equal(bmap, i);
4942 if (bmap)
4943 bmap = var_less(bmap, pos);
4944 return isl_basic_map_finalize(bmap);
4947 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4949 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4950 __isl_take isl_space *dim, unsigned pos)
4952 int i;
4953 isl_basic_map *bmap;
4955 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4956 for (i = 0; i < pos; ++i)
4957 bmap = var_equal(bmap, i);
4958 bmap = var_less_or_equal(bmap, pos);
4959 return isl_basic_map_finalize(bmap);
4962 /* Return a relation on "dim" expressing i_pos > o_pos
4964 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4965 unsigned pos)
4967 int i;
4968 struct isl_basic_map *bmap;
4969 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4970 if (!bmap)
4971 return NULL;
4972 for (i = 0; i < pos && bmap; ++i)
4973 bmap = var_equal(bmap, i);
4974 if (bmap)
4975 bmap = var_more(bmap, pos);
4976 return isl_basic_map_finalize(bmap);
4979 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4981 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4982 __isl_take isl_space *dim, unsigned pos)
4984 int i;
4985 isl_basic_map *bmap;
4987 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4988 for (i = 0; i < pos; ++i)
4989 bmap = var_equal(bmap, i);
4990 bmap = var_more_or_equal(bmap, pos);
4991 return isl_basic_map_finalize(bmap);
4994 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4995 unsigned n, int equal)
4997 struct isl_map *map;
4998 int i;
5000 if (n == 0 && equal)
5001 return isl_map_universe(dims);
5003 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
5005 for (i = 0; i + 1 < n; ++i)
5006 map = isl_map_add_basic_map(map,
5007 isl_basic_map_less_at(isl_space_copy(dims), i));
5008 if (n > 0) {
5009 if (equal)
5010 map = isl_map_add_basic_map(map,
5011 isl_basic_map_less_or_equal_at(dims, n - 1));
5012 else
5013 map = isl_map_add_basic_map(map,
5014 isl_basic_map_less_at(dims, n - 1));
5015 } else
5016 isl_space_free(dims);
5018 return map;
5021 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
5023 if (!dims)
5024 return NULL;
5025 return map_lex_lte_first(dims, dims->n_out, equal);
5028 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5030 return map_lex_lte_first(dim, n, 0);
5033 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5035 return map_lex_lte_first(dim, n, 1);
5038 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5040 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5043 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5045 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5048 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
5049 unsigned n, int equal)
5051 struct isl_map *map;
5052 int i;
5054 if (n == 0 && equal)
5055 return isl_map_universe(dims);
5057 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
5059 for (i = 0; i + 1 < n; ++i)
5060 map = isl_map_add_basic_map(map,
5061 isl_basic_map_more_at(isl_space_copy(dims), i));
5062 if (n > 0) {
5063 if (equal)
5064 map = isl_map_add_basic_map(map,
5065 isl_basic_map_more_or_equal_at(dims, n - 1));
5066 else
5067 map = isl_map_add_basic_map(map,
5068 isl_basic_map_more_at(dims, n - 1));
5069 } else
5070 isl_space_free(dims);
5072 return map;
5075 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
5077 if (!dims)
5078 return NULL;
5079 return map_lex_gte_first(dims, dims->n_out, equal);
5082 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5084 return map_lex_gte_first(dim, n, 0);
5087 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5089 return map_lex_gte_first(dim, n, 1);
5092 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5094 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5097 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5099 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5102 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5103 __isl_take isl_set *set2)
5105 isl_map *map;
5106 map = isl_map_lex_le(isl_set_get_space(set1));
5107 map = isl_map_intersect_domain(map, set1);
5108 map = isl_map_intersect_range(map, set2);
5109 return map;
5112 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5113 __isl_take isl_set *set2)
5115 isl_map *map;
5116 map = isl_map_lex_lt(isl_set_get_space(set1));
5117 map = isl_map_intersect_domain(map, set1);
5118 map = isl_map_intersect_range(map, set2);
5119 return map;
5122 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5123 __isl_take isl_set *set2)
5125 isl_map *map;
5126 map = isl_map_lex_ge(isl_set_get_space(set1));
5127 map = isl_map_intersect_domain(map, set1);
5128 map = isl_map_intersect_range(map, set2);
5129 return map;
5132 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5133 __isl_take isl_set *set2)
5135 isl_map *map;
5136 map = isl_map_lex_gt(isl_set_get_space(set1));
5137 map = isl_map_intersect_domain(map, set1);
5138 map = isl_map_intersect_range(map, set2);
5139 return map;
5142 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5143 __isl_take isl_map *map2)
5145 isl_map *map;
5146 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5147 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5148 map = isl_map_apply_range(map, isl_map_reverse(map2));
5149 return map;
5152 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5153 __isl_take isl_map *map2)
5155 isl_map *map;
5156 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5157 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5158 map = isl_map_apply_range(map, isl_map_reverse(map2));
5159 return map;
5162 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5163 __isl_take isl_map *map2)
5165 isl_map *map;
5166 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5167 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5168 map = isl_map_apply_range(map, isl_map_reverse(map2));
5169 return map;
5172 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5173 __isl_take isl_map *map2)
5175 isl_map *map;
5176 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5177 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5178 map = isl_map_apply_range(map, isl_map_reverse(map2));
5179 return map;
5182 /* For a div d = floor(f/m), add the constraint
5184 * f - m d >= 0
5186 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5187 unsigned pos, isl_int *div)
5189 int i;
5190 unsigned total = isl_basic_map_total_dim(bmap);
5192 i = isl_basic_map_alloc_inequality(bmap);
5193 if (i < 0)
5194 return isl_stat_error;
5195 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5196 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5198 return isl_stat_ok;
5201 /* For a div d = floor(f/m), add the constraint
5203 * -(f-(m-1)) + m d >= 0
5205 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5206 unsigned pos, isl_int *div)
5208 int i;
5209 unsigned total = isl_basic_map_total_dim(bmap);
5211 i = isl_basic_map_alloc_inequality(bmap);
5212 if (i < 0)
5213 return isl_stat_error;
5214 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5215 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5216 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5217 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5219 return isl_stat_ok;
5222 /* For a div d = floor(f/m), add the constraints
5224 * f - m d >= 0
5225 * -(f-(m-1)) + m d >= 0
5227 * Note that the second constraint is the negation of
5229 * f - m d >= m
5231 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5232 unsigned pos, isl_int *div)
5234 if (add_upper_div_constraint(bmap, pos, div) < 0)
5235 return -1;
5236 if (add_lower_div_constraint(bmap, pos, div) < 0)
5237 return -1;
5238 return 0;
5241 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5242 unsigned pos, isl_int *div)
5244 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5245 pos, div);
5248 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
5250 unsigned total = isl_basic_map_total_dim(bmap);
5251 unsigned div_pos = total - bmap->n_div + div;
5253 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5254 bmap->div[div]);
5257 /* For each known div d = floor(f/m), add the constraints
5259 * f - m d >= 0
5260 * -(f-(m-1)) + m d >= 0
5262 * Remove duplicate constraints in case of some these div constraints
5263 * already appear in "bmap".
5265 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5266 __isl_take isl_basic_map *bmap)
5268 unsigned n_div;
5270 if (!bmap)
5271 return NULL;
5272 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5273 if (n_div == 0)
5274 return bmap;
5276 bmap = add_known_div_constraints(bmap);
5277 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5278 bmap = isl_basic_map_finalize(bmap);
5279 return bmap;
5282 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5284 * In particular, if this div is of the form d = floor(f/m),
5285 * then add the constraint
5287 * f - m d >= 0
5289 * if sign < 0 or the constraint
5291 * -(f-(m-1)) + m d >= 0
5293 * if sign > 0.
5295 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5296 unsigned div, int sign)
5298 unsigned total;
5299 unsigned div_pos;
5301 if (!bmap)
5302 return -1;
5304 total = isl_basic_map_total_dim(bmap);
5305 div_pos = total - bmap->n_div + div;
5307 if (sign < 0)
5308 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5309 else
5310 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5313 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5314 __isl_take isl_basic_map *bmap)
5316 if (!bmap)
5317 goto error;
5318 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5319 bmap->n_div == 0 &&
5320 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5321 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5322 return bset_from_bmap(bmap);
5323 bmap = isl_basic_map_cow(bmap);
5324 if (!bmap)
5325 goto error;
5326 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5327 if (!bmap->dim)
5328 goto error;
5329 bmap->extra -= bmap->n_div;
5330 bmap->n_div = 0;
5331 bmap = isl_basic_map_finalize(bmap);
5332 return bset_from_bmap(bmap);
5333 error:
5334 isl_basic_map_free(bmap);
5335 return NULL;
5338 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5339 __isl_take isl_basic_set *bset)
5341 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5344 /* Replace each element in "list" by the result of applying
5345 * isl_basic_map_underlying_set to the element.
5347 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5348 __isl_take isl_basic_map_list *list)
5350 int i, n;
5352 if (!list)
5353 return NULL;
5355 n = isl_basic_map_list_n_basic_map(list);
5356 for (i = 0; i < n; ++i) {
5357 isl_basic_map *bmap;
5358 isl_basic_set *bset;
5360 bmap = isl_basic_map_list_get_basic_map(list, i);
5361 bset = isl_basic_set_underlying_set(bmap);
5362 list = isl_basic_set_list_set_basic_set(list, i, bset);
5365 return list;
5368 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5369 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5371 struct isl_basic_map *bmap;
5372 struct isl_ctx *ctx;
5373 unsigned total;
5374 int i;
5376 if (!bset || !like)
5377 goto error;
5378 ctx = bset->ctx;
5379 isl_assert(ctx, bset->n_div == 0, goto error);
5380 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5381 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5382 goto error);
5383 if (like->n_div == 0) {
5384 isl_space *space = isl_basic_map_get_space(like);
5385 isl_basic_map_free(like);
5386 return isl_basic_map_reset_space(bset, space);
5388 bset = isl_basic_set_cow(bset);
5389 if (!bset)
5390 goto error;
5391 total = bset->dim->n_out + bset->extra;
5392 bmap = bset_to_bmap(bset);
5393 isl_space_free(bmap->dim);
5394 bmap->dim = isl_space_copy(like->dim);
5395 if (!bmap->dim)
5396 goto error;
5397 bmap->n_div = like->n_div;
5398 bmap->extra += like->n_div;
5399 if (bmap->extra) {
5400 unsigned ltotal;
5401 isl_int **div;
5402 ltotal = total - bmap->extra + like->extra;
5403 if (ltotal > total)
5404 ltotal = total;
5405 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5406 bmap->extra * (1 + 1 + total));
5407 if (isl_blk_is_error(bmap->block2))
5408 goto error;
5409 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5410 if (!div)
5411 goto error;
5412 bmap->div = div;
5413 for (i = 0; i < bmap->extra; ++i)
5414 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5415 for (i = 0; i < like->n_div; ++i) {
5416 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5417 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5419 bmap = isl_basic_map_add_known_div_constraints(bmap);
5421 isl_basic_map_free(like);
5422 bmap = isl_basic_map_simplify(bmap);
5423 bmap = isl_basic_map_finalize(bmap);
5424 return bmap;
5425 error:
5426 isl_basic_map_free(like);
5427 isl_basic_set_free(bset);
5428 return NULL;
5431 struct isl_basic_set *isl_basic_set_from_underlying_set(
5432 struct isl_basic_set *bset, struct isl_basic_set *like)
5434 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5435 bset_to_bmap(like)));
5438 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5440 int i;
5442 map = isl_map_cow(map);
5443 if (!map)
5444 return NULL;
5445 map->dim = isl_space_cow(map->dim);
5446 if (!map->dim)
5447 goto error;
5449 for (i = 1; i < map->n; ++i)
5450 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5451 goto error);
5452 for (i = 0; i < map->n; ++i) {
5453 map->p[i] = bset_to_bmap(
5454 isl_basic_map_underlying_set(map->p[i]));
5455 if (!map->p[i])
5456 goto error;
5458 if (map->n == 0)
5459 map->dim = isl_space_underlying(map->dim, 0);
5460 else {
5461 isl_space_free(map->dim);
5462 map->dim = isl_space_copy(map->p[0]->dim);
5464 if (!map->dim)
5465 goto error;
5466 return set_from_map(map);
5467 error:
5468 isl_map_free(map);
5469 return NULL;
5472 /* Replace the space of "bmap" by "space".
5474 * If the space of "bmap" is identical to "space" (including the identifiers
5475 * of the input and output dimensions), then simply return the original input.
5477 __isl_give isl_basic_map *isl_basic_map_reset_space(
5478 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5480 isl_bool equal;
5481 isl_space *bmap_space;
5483 bmap_space = isl_basic_map_peek_space(bmap);
5484 equal = isl_space_is_equal(bmap_space, space);
5485 if (equal >= 0 && equal)
5486 equal = isl_space_has_equal_ids(bmap_space, space);
5487 if (equal < 0)
5488 goto error;
5489 if (equal) {
5490 isl_space_free(space);
5491 return bmap;
5493 bmap = isl_basic_map_cow(bmap);
5494 if (!bmap || !space)
5495 goto error;
5497 isl_space_free(bmap->dim);
5498 bmap->dim = space;
5500 bmap = isl_basic_map_finalize(bmap);
5502 return bmap;
5503 error:
5504 isl_basic_map_free(bmap);
5505 isl_space_free(space);
5506 return NULL;
5509 __isl_give isl_basic_set *isl_basic_set_reset_space(
5510 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5512 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5513 dim));
5516 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5517 __isl_take isl_space *dim)
5519 int i;
5521 map = isl_map_cow(map);
5522 if (!map || !dim)
5523 goto error;
5525 for (i = 0; i < map->n; ++i) {
5526 map->p[i] = isl_basic_map_reset_space(map->p[i],
5527 isl_space_copy(dim));
5528 if (!map->p[i])
5529 goto error;
5531 isl_space_free(map->dim);
5532 map->dim = dim;
5534 return map;
5535 error:
5536 isl_map_free(map);
5537 isl_space_free(dim);
5538 return NULL;
5541 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5542 __isl_take isl_space *dim)
5544 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5547 /* Compute the parameter domain of the given basic set.
5549 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5551 isl_bool is_params;
5552 isl_space *space;
5553 unsigned n;
5555 is_params = isl_basic_set_is_params(bset);
5556 if (is_params < 0)
5557 return isl_basic_set_free(bset);
5558 if (is_params)
5559 return bset;
5561 n = isl_basic_set_dim(bset, isl_dim_set);
5562 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5563 space = isl_basic_set_get_space(bset);
5564 space = isl_space_params(space);
5565 bset = isl_basic_set_reset_space(bset, space);
5566 return bset;
5569 /* Construct a zero-dimensional basic set with the given parameter domain.
5571 __isl_give isl_basic_set *isl_basic_set_from_params(
5572 __isl_take isl_basic_set *bset)
5574 isl_space *space;
5575 space = isl_basic_set_get_space(bset);
5576 space = isl_space_set_from_params(space);
5577 bset = isl_basic_set_reset_space(bset, space);
5578 return bset;
5581 /* Compute the parameter domain of the given set.
5583 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5585 isl_space *space;
5586 unsigned n;
5588 if (isl_set_is_params(set))
5589 return set;
5591 n = isl_set_dim(set, isl_dim_set);
5592 set = isl_set_project_out(set, isl_dim_set, 0, n);
5593 space = isl_set_get_space(set);
5594 space = isl_space_params(space);
5595 set = isl_set_reset_space(set, space);
5596 return set;
5599 /* Construct a zero-dimensional set with the given parameter domain.
5601 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5603 isl_space *space;
5604 space = isl_set_get_space(set);
5605 space = isl_space_set_from_params(space);
5606 set = isl_set_reset_space(set, space);
5607 return set;
5610 /* Compute the parameter domain of the given map.
5612 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5614 isl_space *space;
5615 unsigned n;
5617 n = isl_map_dim(map, isl_dim_in);
5618 map = isl_map_project_out(map, isl_dim_in, 0, n);
5619 n = isl_map_dim(map, isl_dim_out);
5620 map = isl_map_project_out(map, isl_dim_out, 0, n);
5621 space = isl_map_get_space(map);
5622 space = isl_space_params(space);
5623 map = isl_map_reset_space(map, space);
5624 return map;
5627 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5629 isl_space *space;
5630 unsigned n_out;
5632 if (!bmap)
5633 return NULL;
5634 space = isl_space_domain(isl_basic_map_get_space(bmap));
5636 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5637 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5639 return isl_basic_map_reset_space(bmap, space);
5642 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5644 if (!bmap)
5645 return isl_bool_error;
5646 return isl_space_may_be_set(bmap->dim);
5649 /* Is this basic map actually a set?
5650 * Users should never call this function. Outside of isl,
5651 * the type should indicate whether something is a set or a map.
5653 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5655 if (!bmap)
5656 return isl_bool_error;
5657 return isl_space_is_set(bmap->dim);
5660 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5662 isl_bool is_set;
5664 is_set = isl_basic_map_is_set(bmap);
5665 if (is_set < 0)
5666 goto error;
5667 if (is_set)
5668 return bmap;
5669 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5670 error:
5671 isl_basic_map_free(bmap);
5672 return NULL;
5675 __isl_give isl_basic_map *isl_basic_map_domain_map(
5676 __isl_take isl_basic_map *bmap)
5678 int i;
5679 isl_space *dim;
5680 isl_basic_map *domain;
5681 int nparam, n_in, n_out;
5683 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5684 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5685 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5687 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5688 domain = isl_basic_map_universe(dim);
5690 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5691 bmap = isl_basic_map_apply_range(bmap, domain);
5692 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5694 for (i = 0; i < n_in; ++i)
5695 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5696 isl_dim_out, i);
5698 bmap = isl_basic_map_gauss(bmap, NULL);
5699 return isl_basic_map_finalize(bmap);
5702 __isl_give isl_basic_map *isl_basic_map_range_map(
5703 __isl_take isl_basic_map *bmap)
5705 int i;
5706 isl_space *dim;
5707 isl_basic_map *range;
5708 int nparam, n_in, n_out;
5710 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5711 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5712 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5714 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5715 range = isl_basic_map_universe(dim);
5717 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5718 bmap = isl_basic_map_apply_range(bmap, range);
5719 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5721 for (i = 0; i < n_out; ++i)
5722 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5723 isl_dim_out, i);
5725 bmap = isl_basic_map_gauss(bmap, NULL);
5726 return isl_basic_map_finalize(bmap);
5729 int isl_map_may_be_set(__isl_keep isl_map *map)
5731 if (!map)
5732 return -1;
5733 return isl_space_may_be_set(map->dim);
5736 /* Is this map actually a set?
5737 * Users should never call this function. Outside of isl,
5738 * the type should indicate whether something is a set or a map.
5740 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5742 if (!map)
5743 return isl_bool_error;
5744 return isl_space_is_set(map->dim);
5747 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5749 int i;
5750 isl_bool is_set;
5751 struct isl_set *set;
5753 is_set = isl_map_is_set(map);
5754 if (is_set < 0)
5755 goto error;
5756 if (is_set)
5757 return set_from_map(map);
5759 map = isl_map_cow(map);
5760 if (!map)
5761 goto error;
5763 set = set_from_map(map);
5764 set->dim = isl_space_range(set->dim);
5765 if (!set->dim)
5766 goto error;
5767 for (i = 0; i < map->n; ++i) {
5768 set->p[i] = isl_basic_map_range(map->p[i]);
5769 if (!set->p[i])
5770 goto error;
5772 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5773 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5774 return set;
5775 error:
5776 isl_map_free(map);
5777 return NULL;
5780 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5782 int i;
5784 map = isl_map_cow(map);
5785 if (!map)
5786 return NULL;
5788 map->dim = isl_space_domain_map(map->dim);
5789 if (!map->dim)
5790 goto error;
5791 for (i = 0; i < map->n; ++i) {
5792 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5793 if (!map->p[i])
5794 goto error;
5796 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5797 map = isl_map_unmark_normalized(map);
5798 return map;
5799 error:
5800 isl_map_free(map);
5801 return NULL;
5804 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5806 int i;
5807 isl_space *range_dim;
5809 map = isl_map_cow(map);
5810 if (!map)
5811 return NULL;
5813 range_dim = isl_space_range(isl_map_get_space(map));
5814 range_dim = isl_space_from_range(range_dim);
5815 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5816 map->dim = isl_space_join(map->dim, range_dim);
5817 if (!map->dim)
5818 goto error;
5819 for (i = 0; i < map->n; ++i) {
5820 map->p[i] = isl_basic_map_range_map(map->p[i]);
5821 if (!map->p[i])
5822 goto error;
5824 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5825 map = isl_map_unmark_normalized(map);
5826 return map;
5827 error:
5828 isl_map_free(map);
5829 return NULL;
5832 /* Given a wrapped map of the form A[B -> C],
5833 * return the map A[B -> C] -> B.
5835 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5837 isl_id *id;
5838 isl_map *map;
5840 if (!set)
5841 return NULL;
5842 if (!isl_set_has_tuple_id(set))
5843 return isl_map_domain_map(isl_set_unwrap(set));
5845 id = isl_set_get_tuple_id(set);
5846 map = isl_map_domain_map(isl_set_unwrap(set));
5847 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5849 return map;
5852 __isl_give isl_basic_map *isl_basic_map_from_domain(
5853 __isl_take isl_basic_set *bset)
5855 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5858 __isl_give isl_basic_map *isl_basic_map_from_range(
5859 __isl_take isl_basic_set *bset)
5861 isl_space *space;
5862 space = isl_basic_set_get_space(bset);
5863 space = isl_space_from_range(space);
5864 bset = isl_basic_set_reset_space(bset, space);
5865 return bset_to_bmap(bset);
5868 /* Create a relation with the given set as range.
5869 * The domain of the created relation is a zero-dimensional
5870 * flat anonymous space.
5872 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5874 isl_space *space;
5875 space = isl_set_get_space(set);
5876 space = isl_space_from_range(space);
5877 set = isl_set_reset_space(set, space);
5878 return set_to_map(set);
5881 /* Create a relation with the given set as domain.
5882 * The range of the created relation is a zero-dimensional
5883 * flat anonymous space.
5885 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5887 return isl_map_reverse(isl_map_from_range(set));
5890 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5891 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5893 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5896 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5897 __isl_take isl_set *range)
5899 return isl_map_apply_range(isl_map_reverse(domain), range);
5902 /* Return a newly allocated isl_map with given space and flags and
5903 * room for "n" basic maps.
5904 * Make sure that all cached information is cleared.
5906 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5907 unsigned flags)
5909 struct isl_map *map;
5911 if (!space)
5912 return NULL;
5913 if (n < 0)
5914 isl_die(space->ctx, isl_error_internal,
5915 "negative number of basic maps", goto error);
5916 map = isl_calloc(space->ctx, struct isl_map,
5917 sizeof(struct isl_map) +
5918 (n - 1) * sizeof(struct isl_basic_map *));
5919 if (!map)
5920 goto error;
5922 map->ctx = space->ctx;
5923 isl_ctx_ref(map->ctx);
5924 map->ref = 1;
5925 map->size = n;
5926 map->n = 0;
5927 map->dim = space;
5928 map->flags = flags;
5929 return map;
5930 error:
5931 isl_space_free(space);
5932 return NULL;
5935 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5937 struct isl_basic_map *bmap;
5938 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5939 bmap = isl_basic_map_set_to_empty(bmap);
5940 return bmap;
5943 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5945 struct isl_basic_set *bset;
5946 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5947 bset = isl_basic_set_set_to_empty(bset);
5948 return bset;
5951 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5953 struct isl_basic_map *bmap;
5954 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5955 bmap = isl_basic_map_finalize(bmap);
5956 return bmap;
5959 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
5961 struct isl_basic_set *bset;
5962 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5963 bset = isl_basic_set_finalize(bset);
5964 return bset;
5967 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5969 int i;
5970 unsigned total = isl_space_dim(dim, isl_dim_all);
5971 isl_basic_map *bmap;
5973 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5974 for (i = 0; i < total; ++i) {
5975 int k = isl_basic_map_alloc_inequality(bmap);
5976 if (k < 0)
5977 goto error;
5978 isl_seq_clr(bmap->ineq[k], 1 + total);
5979 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5981 return bmap;
5982 error:
5983 isl_basic_map_free(bmap);
5984 return NULL;
5987 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5989 return isl_basic_map_nat_universe(dim);
5992 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5994 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5997 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5999 return isl_map_nat_universe(dim);
6002 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6004 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6007 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6009 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6012 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6014 struct isl_map *map;
6015 if (!space)
6016 return NULL;
6017 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6018 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6019 return map;
6022 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6024 struct isl_set *set;
6025 if (!space)
6026 return NULL;
6027 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6028 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6029 return set;
6032 struct isl_map *isl_map_dup(struct isl_map *map)
6034 int i;
6035 struct isl_map *dup;
6037 if (!map)
6038 return NULL;
6039 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6040 for (i = 0; i < map->n; ++i)
6041 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6042 return dup;
6045 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6046 __isl_take isl_basic_map *bmap)
6048 if (!bmap || !map)
6049 goto error;
6050 if (isl_basic_map_plain_is_empty(bmap)) {
6051 isl_basic_map_free(bmap);
6052 return map;
6054 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6055 isl_assert(map->ctx, map->n < map->size, goto error);
6056 map->p[map->n] = bmap;
6057 map->n++;
6058 map = isl_map_unmark_normalized(map);
6059 return map;
6060 error:
6061 if (map)
6062 isl_map_free(map);
6063 if (bmap)
6064 isl_basic_map_free(bmap);
6065 return NULL;
6068 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6070 int i;
6072 if (!map)
6073 return NULL;
6075 if (--map->ref > 0)
6076 return NULL;
6078 clear_caches(map);
6079 isl_ctx_deref(map->ctx);
6080 for (i = 0; i < map->n; ++i)
6081 isl_basic_map_free(map->p[i]);
6082 isl_space_free(map->dim);
6083 free(map);
6085 return NULL;
6088 static struct isl_basic_map *isl_basic_map_fix_pos_si(
6089 struct isl_basic_map *bmap, unsigned pos, int value)
6091 int j;
6093 bmap = isl_basic_map_cow(bmap);
6094 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6095 j = isl_basic_map_alloc_equality(bmap);
6096 if (j < 0)
6097 goto error;
6098 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6099 isl_int_set_si(bmap->eq[j][pos], -1);
6100 isl_int_set_si(bmap->eq[j][0], value);
6101 bmap = isl_basic_map_simplify(bmap);
6102 return isl_basic_map_finalize(bmap);
6103 error:
6104 isl_basic_map_free(bmap);
6105 return NULL;
6108 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6109 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6111 int j;
6113 bmap = isl_basic_map_cow(bmap);
6114 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6115 j = isl_basic_map_alloc_equality(bmap);
6116 if (j < 0)
6117 goto error;
6118 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6119 isl_int_set_si(bmap->eq[j][pos], -1);
6120 isl_int_set(bmap->eq[j][0], value);
6121 bmap = isl_basic_map_simplify(bmap);
6122 return isl_basic_map_finalize(bmap);
6123 error:
6124 isl_basic_map_free(bmap);
6125 return NULL;
6128 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6129 enum isl_dim_type type, unsigned pos, int value)
6131 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6132 return isl_basic_map_free(bmap);
6133 return isl_basic_map_fix_pos_si(bmap,
6134 isl_basic_map_offset(bmap, type) + pos, value);
6137 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6138 enum isl_dim_type type, unsigned pos, isl_int value)
6140 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6141 return isl_basic_map_free(bmap);
6142 return isl_basic_map_fix_pos(bmap,
6143 isl_basic_map_offset(bmap, type) + pos, value);
6146 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6147 * to be equal to "v".
6149 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6150 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6152 if (!bmap || !v)
6153 goto error;
6154 if (!isl_val_is_int(v))
6155 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6156 "expecting integer value", goto error);
6157 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6158 goto error;
6159 pos += isl_basic_map_offset(bmap, type);
6160 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6161 isl_val_free(v);
6162 return bmap;
6163 error:
6164 isl_basic_map_free(bmap);
6165 isl_val_free(v);
6166 return NULL;
6169 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6170 * to be equal to "v".
6172 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6173 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6175 return isl_basic_map_fix_val(bset, type, pos, v);
6178 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6179 enum isl_dim_type type, unsigned pos, int value)
6181 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6182 type, pos, value));
6185 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6186 enum isl_dim_type type, unsigned pos, isl_int value)
6188 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6189 type, pos, value));
6192 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6193 unsigned input, int value)
6195 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6198 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6199 unsigned dim, int value)
6201 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6202 isl_dim_set, dim, value));
6205 /* Remove the basic map at position "i" from "map" if this basic map
6206 * is (obviously) empty.
6208 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6210 isl_bool empty;
6212 if (!map)
6213 return NULL;
6215 empty = isl_basic_map_plain_is_empty(map->p[i]);
6216 if (empty < 0)
6217 return isl_map_free(map);
6218 if (!empty)
6219 return map;
6221 isl_basic_map_free(map->p[i]);
6222 map->n--;
6223 if (i != map->n) {
6224 map->p[i] = map->p[map->n];
6225 map = isl_map_unmark_normalized(map);
6229 return map;
6232 /* Perform "fn" on each basic map of "map", where we may not be holding
6233 * the only reference to "map".
6234 * In particular, "fn" should be a semantics preserving operation
6235 * that we want to apply to all copies of "map". We therefore need
6236 * to be careful not to modify "map" in a way that breaks "map"
6237 * in case anything goes wrong.
6239 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6240 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6242 struct isl_basic_map *bmap;
6243 int i;
6245 if (!map)
6246 return NULL;
6248 for (i = map->n - 1; i >= 0; --i) {
6249 bmap = isl_basic_map_copy(map->p[i]);
6250 bmap = fn(bmap);
6251 if (!bmap)
6252 goto error;
6253 isl_basic_map_free(map->p[i]);
6254 map->p[i] = bmap;
6255 map = remove_if_empty(map, i);
6256 if (!map)
6257 return NULL;
6260 return map;
6261 error:
6262 isl_map_free(map);
6263 return NULL;
6266 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6267 enum isl_dim_type type, unsigned pos, int value)
6269 int i;
6271 map = isl_map_cow(map);
6272 if (!map)
6273 return NULL;
6275 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6276 for (i = map->n - 1; i >= 0; --i) {
6277 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6278 map = remove_if_empty(map, i);
6279 if (!map)
6280 return NULL;
6282 map = isl_map_unmark_normalized(map);
6283 return map;
6284 error:
6285 isl_map_free(map);
6286 return NULL;
6289 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6290 enum isl_dim_type type, unsigned pos, int value)
6292 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6295 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6296 enum isl_dim_type type, unsigned pos, isl_int value)
6298 int i;
6300 map = isl_map_cow(map);
6301 if (!map)
6302 return NULL;
6304 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6305 for (i = 0; i < map->n; ++i) {
6306 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6307 if (!map->p[i])
6308 goto error;
6310 map = isl_map_unmark_normalized(map);
6311 return map;
6312 error:
6313 isl_map_free(map);
6314 return NULL;
6317 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6318 enum isl_dim_type type, unsigned pos, isl_int value)
6320 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6323 /* Fix the value of the variable at position "pos" of type "type" of "map"
6324 * to be equal to "v".
6326 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6327 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6329 int i;
6331 map = isl_map_cow(map);
6332 if (!map || !v)
6333 goto error;
6335 if (!isl_val_is_int(v))
6336 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6337 "expecting integer value", goto error);
6338 if (pos >= isl_map_dim(map, type))
6339 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6340 "index out of bounds", goto error);
6341 for (i = map->n - 1; i >= 0; --i) {
6342 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6343 isl_val_copy(v));
6344 map = remove_if_empty(map, i);
6345 if (!map)
6346 goto error;
6348 map = isl_map_unmark_normalized(map);
6349 isl_val_free(v);
6350 return map;
6351 error:
6352 isl_map_free(map);
6353 isl_val_free(v);
6354 return NULL;
6357 /* Fix the value of the variable at position "pos" of type "type" of "set"
6358 * to be equal to "v".
6360 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6361 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6363 return isl_map_fix_val(set, type, pos, v);
6366 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6367 unsigned input, int value)
6369 return isl_map_fix_si(map, isl_dim_in, input, value);
6372 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6374 return set_from_map(isl_map_fix_si(set_to_map(set),
6375 isl_dim_set, dim, value));
6378 static __isl_give isl_basic_map *basic_map_bound_si(
6379 __isl_take isl_basic_map *bmap,
6380 enum isl_dim_type type, unsigned pos, int value, int upper)
6382 int j;
6384 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6385 return isl_basic_map_free(bmap);
6386 pos += isl_basic_map_offset(bmap, type);
6387 bmap = isl_basic_map_cow(bmap);
6388 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6389 j = isl_basic_map_alloc_inequality(bmap);
6390 if (j < 0)
6391 goto error;
6392 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6393 if (upper) {
6394 isl_int_set_si(bmap->ineq[j][pos], -1);
6395 isl_int_set_si(bmap->ineq[j][0], value);
6396 } else {
6397 isl_int_set_si(bmap->ineq[j][pos], 1);
6398 isl_int_set_si(bmap->ineq[j][0], -value);
6400 bmap = isl_basic_map_simplify(bmap);
6401 return isl_basic_map_finalize(bmap);
6402 error:
6403 isl_basic_map_free(bmap);
6404 return NULL;
6407 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6408 __isl_take isl_basic_map *bmap,
6409 enum isl_dim_type type, unsigned pos, int value)
6411 return basic_map_bound_si(bmap, type, pos, value, 0);
6414 /* Constrain the values of the given dimension to be no greater than "value".
6416 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6417 __isl_take isl_basic_map *bmap,
6418 enum isl_dim_type type, unsigned pos, int value)
6420 return basic_map_bound_si(bmap, type, pos, value, 1);
6423 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6424 enum isl_dim_type type, unsigned pos, int value, int upper)
6426 int i;
6428 map = isl_map_cow(map);
6429 if (!map)
6430 return NULL;
6432 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6433 for (i = 0; i < map->n; ++i) {
6434 map->p[i] = basic_map_bound_si(map->p[i],
6435 type, pos, value, upper);
6436 if (!map->p[i])
6437 goto error;
6439 map = isl_map_unmark_normalized(map);
6440 return map;
6441 error:
6442 isl_map_free(map);
6443 return NULL;
6446 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6447 enum isl_dim_type type, unsigned pos, int value)
6449 return map_bound_si(map, type, pos, value, 0);
6452 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6453 enum isl_dim_type type, unsigned pos, int value)
6455 return map_bound_si(map, type, pos, value, 1);
6458 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6459 enum isl_dim_type type, unsigned pos, int value)
6461 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6462 type, pos, value));
6465 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6466 enum isl_dim_type type, unsigned pos, int value)
6468 return isl_map_upper_bound_si(set, type, pos, value);
6471 /* Bound the given variable of "bmap" from below (or above is "upper"
6472 * is set) to "value".
6474 static __isl_give isl_basic_map *basic_map_bound(
6475 __isl_take isl_basic_map *bmap,
6476 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6478 int j;
6480 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6481 return isl_basic_map_free(bmap);
6482 pos += isl_basic_map_offset(bmap, type);
6483 bmap = isl_basic_map_cow(bmap);
6484 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6485 j = isl_basic_map_alloc_inequality(bmap);
6486 if (j < 0)
6487 goto error;
6488 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6489 if (upper) {
6490 isl_int_set_si(bmap->ineq[j][pos], -1);
6491 isl_int_set(bmap->ineq[j][0], value);
6492 } else {
6493 isl_int_set_si(bmap->ineq[j][pos], 1);
6494 isl_int_neg(bmap->ineq[j][0], value);
6496 bmap = isl_basic_map_simplify(bmap);
6497 return isl_basic_map_finalize(bmap);
6498 error:
6499 isl_basic_map_free(bmap);
6500 return NULL;
6503 /* Bound the given variable of "map" from below (or above is "upper"
6504 * is set) to "value".
6506 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6507 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6509 int i;
6511 map = isl_map_cow(map);
6512 if (!map)
6513 return NULL;
6515 if (pos >= isl_map_dim(map, type))
6516 isl_die(map->ctx, isl_error_invalid,
6517 "index out of bounds", goto error);
6518 for (i = map->n - 1; i >= 0; --i) {
6519 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6520 map = remove_if_empty(map, i);
6521 if (!map)
6522 return NULL;
6524 map = isl_map_unmark_normalized(map);
6525 return map;
6526 error:
6527 isl_map_free(map);
6528 return NULL;
6531 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6532 enum isl_dim_type type, unsigned pos, isl_int value)
6534 return map_bound(map, type, pos, value, 0);
6537 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6538 enum isl_dim_type type, unsigned pos, isl_int value)
6540 return map_bound(map, type, pos, value, 1);
6543 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6544 enum isl_dim_type type, unsigned pos, isl_int value)
6546 return isl_map_lower_bound(set, type, pos, value);
6549 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6550 enum isl_dim_type type, unsigned pos, isl_int value)
6552 return isl_map_upper_bound(set, type, pos, value);
6555 /* Force the values of the variable at position "pos" of type "type" of "set"
6556 * to be no smaller than "value".
6558 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6559 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6561 if (!value)
6562 goto error;
6563 if (!isl_val_is_int(value))
6564 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6565 "expecting integer value", goto error);
6566 set = isl_set_lower_bound(set, type, pos, value->n);
6567 isl_val_free(value);
6568 return set;
6569 error:
6570 isl_val_free(value);
6571 isl_set_free(set);
6572 return NULL;
6575 /* Force the values of the variable at position "pos" of type "type" of "set"
6576 * to be no greater than "value".
6578 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6579 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6581 if (!value)
6582 goto error;
6583 if (!isl_val_is_int(value))
6584 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6585 "expecting integer value", goto error);
6586 set = isl_set_upper_bound(set, type, pos, value->n);
6587 isl_val_free(value);
6588 return set;
6589 error:
6590 isl_val_free(value);
6591 isl_set_free(set);
6592 return NULL;
6595 /* Bound the given variable of "bset" from below (or above is "upper"
6596 * is set) to "value".
6598 static __isl_give isl_basic_set *isl_basic_set_bound(
6599 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6600 isl_int value, int upper)
6602 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6603 type, pos, value, upper));
6606 /* Bound the given variable of "bset" from below (or above is "upper"
6607 * is set) to "value".
6609 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6610 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6611 __isl_take isl_val *value, int upper)
6613 if (!value)
6614 goto error;
6615 if (!isl_val_is_int(value))
6616 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6617 "expecting integer value", goto error);
6618 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6619 isl_val_free(value);
6620 return bset;
6621 error:
6622 isl_val_free(value);
6623 isl_basic_set_free(bset);
6624 return NULL;
6627 /* Bound the given variable of "bset" from below to "value".
6629 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6630 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6631 __isl_take isl_val *value)
6633 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6636 /* Bound the given variable of "bset" from above to "value".
6638 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6639 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6640 __isl_take isl_val *value)
6642 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6645 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6647 int i;
6649 map = isl_map_cow(map);
6650 if (!map)
6651 return NULL;
6653 map->dim = isl_space_reverse(map->dim);
6654 if (!map->dim)
6655 goto error;
6656 for (i = 0; i < map->n; ++i) {
6657 map->p[i] = isl_basic_map_reverse(map->p[i]);
6658 if (!map->p[i])
6659 goto error;
6661 map = isl_map_unmark_normalized(map);
6662 return map;
6663 error:
6664 isl_map_free(map);
6665 return NULL;
6668 #undef TYPE
6669 #define TYPE isl_pw_multi_aff
6670 #undef SUFFIX
6671 #define SUFFIX _pw_multi_aff
6672 #undef EMPTY
6673 #define EMPTY isl_pw_multi_aff_empty
6674 #undef ADD
6675 #define ADD isl_pw_multi_aff_union_add
6676 #include "isl_map_lexopt_templ.c"
6678 /* Given a map "map", compute the lexicographically minimal
6679 * (or maximal) image element for each domain element in dom,
6680 * in the form of an isl_pw_multi_aff.
6681 * If "empty" is not NULL, then set *empty to those elements in dom that
6682 * do not have an image element.
6683 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6684 * should be computed over the domain of "map". "empty" is also NULL
6685 * in this case.
6687 * We first compute the lexicographically minimal or maximal element
6688 * in the first basic map. This results in a partial solution "res"
6689 * and a subset "todo" of dom that still need to be handled.
6690 * We then consider each of the remaining maps in "map" and successively
6691 * update both "res" and "todo".
6692 * If "empty" is NULL, then the todo sets are not needed and therefore
6693 * also not computed.
6695 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6696 __isl_take isl_map *map, __isl_take isl_set *dom,
6697 __isl_give isl_set **empty, unsigned flags)
6699 int i;
6700 int full;
6701 isl_pw_multi_aff *res;
6702 isl_set *todo;
6704 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6705 if (!map || (!full && !dom))
6706 goto error;
6708 if (isl_map_plain_is_empty(map)) {
6709 if (empty)
6710 *empty = dom;
6711 else
6712 isl_set_free(dom);
6713 return isl_pw_multi_aff_from_map(map);
6716 res = basic_map_partial_lexopt_pw_multi_aff(
6717 isl_basic_map_copy(map->p[0]),
6718 isl_set_copy(dom), empty, flags);
6720 if (empty)
6721 todo = *empty;
6722 for (i = 1; i < map->n; ++i) {
6723 isl_pw_multi_aff *res_i;
6725 res_i = basic_map_partial_lexopt_pw_multi_aff(
6726 isl_basic_map_copy(map->p[i]),
6727 isl_set_copy(dom), empty, flags);
6729 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6730 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6731 else
6732 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6734 if (empty)
6735 todo = isl_set_intersect(todo, *empty);
6738 isl_set_free(dom);
6739 isl_map_free(map);
6741 if (empty)
6742 *empty = todo;
6744 return res;
6745 error:
6746 if (empty)
6747 *empty = NULL;
6748 isl_set_free(dom);
6749 isl_map_free(map);
6750 return NULL;
6753 #undef TYPE
6754 #define TYPE isl_map
6755 #undef SUFFIX
6756 #define SUFFIX
6757 #undef EMPTY
6758 #define EMPTY isl_map_empty
6759 #undef ADD
6760 #define ADD isl_map_union_disjoint
6761 #include "isl_map_lexopt_templ.c"
6763 /* Given a map "map", compute the lexicographically minimal
6764 * (or maximal) image element for each domain element in "dom",
6765 * in the form of an isl_map.
6766 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6767 * do not have an image element.
6768 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6769 * should be computed over the domain of "map". "empty" is also NULL
6770 * in this case.
6772 * If the input consists of more than one disjunct, then first
6773 * compute the desired result in the form of an isl_pw_multi_aff and
6774 * then convert that into an isl_map.
6776 * This function used to have an explicit implementation in terms
6777 * of isl_maps, but it would continually intersect the domains of
6778 * partial results with the complement of the domain of the next
6779 * partial solution, potentially leading to an explosion in the number
6780 * of disjuncts if there are several disjuncts in the input.
6781 * An even earlier implementation of this function would look for
6782 * better results in the domain of the partial result and for extra
6783 * results in the complement of this domain, which would lead to
6784 * even more splintering.
6786 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6787 __isl_take isl_map *map, __isl_take isl_set *dom,
6788 __isl_give isl_set **empty, unsigned flags)
6790 int full;
6791 struct isl_map *res;
6792 isl_pw_multi_aff *pma;
6794 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6795 if (!map || (!full && !dom))
6796 goto error;
6798 if (isl_map_plain_is_empty(map)) {
6799 if (empty)
6800 *empty = dom;
6801 else
6802 isl_set_free(dom);
6803 return map;
6806 if (map->n == 1) {
6807 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6808 dom, empty, flags);
6809 isl_map_free(map);
6810 return res;
6813 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6814 flags);
6815 return isl_map_from_pw_multi_aff(pma);
6816 error:
6817 if (empty)
6818 *empty = NULL;
6819 isl_set_free(dom);
6820 isl_map_free(map);
6821 return NULL;
6824 __isl_give isl_map *isl_map_partial_lexmax(
6825 __isl_take isl_map *map, __isl_take isl_set *dom,
6826 __isl_give isl_set **empty)
6828 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6831 __isl_give isl_map *isl_map_partial_lexmin(
6832 __isl_take isl_map *map, __isl_take isl_set *dom,
6833 __isl_give isl_set **empty)
6835 return isl_map_partial_lexopt(map, dom, empty, 0);
6838 __isl_give isl_set *isl_set_partial_lexmin(
6839 __isl_take isl_set *set, __isl_take isl_set *dom,
6840 __isl_give isl_set **empty)
6842 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6843 dom, empty));
6846 __isl_give isl_set *isl_set_partial_lexmax(
6847 __isl_take isl_set *set, __isl_take isl_set *dom,
6848 __isl_give isl_set **empty)
6850 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6851 dom, empty));
6854 /* Compute the lexicographic minimum (or maximum if "flags" includes
6855 * ISL_OPT_MAX) of "bset" over its parametric domain.
6857 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6858 unsigned flags)
6860 return isl_basic_map_lexopt(bset, flags);
6863 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6865 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6868 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6870 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6873 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6875 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6878 /* Compute the lexicographic minimum of "bset" over its parametric domain
6879 * for the purpose of quantifier elimination.
6880 * That is, find an explicit representation for all the existentially
6881 * quantified variables in "bset" by computing their lexicographic
6882 * minimum.
6884 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6885 __isl_take isl_basic_set *bset)
6887 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6890 /* Given a basic map with one output dimension, compute the minimum or
6891 * maximum of that dimension as an isl_pw_aff.
6893 * Compute the optimum as a lexicographic optimum over the single
6894 * output dimension and extract the single isl_pw_aff from the result.
6896 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6897 int max)
6899 isl_pw_multi_aff *pma;
6900 isl_pw_aff *pwaff;
6902 bmap = isl_basic_map_copy(bmap);
6903 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6904 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6905 isl_pw_multi_aff_free(pma);
6907 return pwaff;
6910 /* Compute the minimum or maximum of the given output dimension
6911 * as a function of the parameters and the input dimensions,
6912 * but independently of the other output dimensions.
6914 * We first project out the other output dimension and then compute
6915 * the "lexicographic" maximum in each basic map, combining the results
6916 * using isl_pw_aff_union_max.
6918 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6919 int max)
6921 int i;
6922 isl_pw_aff *pwaff;
6923 unsigned n_out;
6925 n_out = isl_map_dim(map, isl_dim_out);
6926 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6927 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6928 if (!map)
6929 return NULL;
6931 if (map->n == 0) {
6932 isl_space *dim = isl_map_get_space(map);
6933 isl_map_free(map);
6934 return isl_pw_aff_empty(dim);
6937 pwaff = basic_map_dim_opt(map->p[0], max);
6938 for (i = 1; i < map->n; ++i) {
6939 isl_pw_aff *pwaff_i;
6941 pwaff_i = basic_map_dim_opt(map->p[i], max);
6942 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6945 isl_map_free(map);
6947 return pwaff;
6950 /* Compute the minimum of the given output dimension as a function of the
6951 * parameters and input dimensions, but independently of
6952 * the other output dimensions.
6954 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6956 return map_dim_opt(map, pos, 0);
6959 /* Compute the maximum of the given output dimension as a function of the
6960 * parameters and input dimensions, but independently of
6961 * the other output dimensions.
6963 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6965 return map_dim_opt(map, pos, 1);
6968 /* Compute the minimum or maximum of the given set dimension
6969 * as a function of the parameters,
6970 * but independently of the other set dimensions.
6972 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6973 int max)
6975 return map_dim_opt(set, pos, max);
6978 /* Compute the maximum of the given set dimension as a function of the
6979 * parameters, but independently of the other set dimensions.
6981 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6983 return set_dim_opt(set, pos, 1);
6986 /* Compute the minimum of the given set dimension as a function of the
6987 * parameters, but independently of the other set dimensions.
6989 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6991 return set_dim_opt(set, pos, 0);
6994 /* Apply a preimage specified by "mat" on the parameters of "bset".
6995 * bset is assumed to have only parameters and divs.
6997 static __isl_give isl_basic_set *basic_set_parameter_preimage(
6998 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7000 unsigned nparam;
7002 if (!bset || !mat)
7003 goto error;
7005 bset->dim = isl_space_cow(bset->dim);
7006 if (!bset->dim)
7007 goto error;
7009 nparam = isl_basic_set_dim(bset, isl_dim_param);
7011 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7013 bset->dim->nparam = 0;
7014 bset->dim->n_out = nparam;
7015 bset = isl_basic_set_preimage(bset, mat);
7016 if (bset) {
7017 bset->dim->nparam = bset->dim->n_out;
7018 bset->dim->n_out = 0;
7020 return bset;
7021 error:
7022 isl_mat_free(mat);
7023 isl_basic_set_free(bset);
7024 return NULL;
7027 /* Apply a preimage specified by "mat" on the parameters of "set".
7028 * set is assumed to have only parameters and divs.
7030 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7031 __isl_take isl_mat *mat)
7033 isl_space *space;
7034 unsigned nparam;
7036 if (!set || !mat)
7037 goto error;
7039 nparam = isl_set_dim(set, isl_dim_param);
7041 if (mat->n_row != 1 + nparam)
7042 isl_die(isl_set_get_ctx(set), isl_error_internal,
7043 "unexpected number of rows", goto error);
7045 space = isl_set_get_space(set);
7046 space = isl_space_move_dims(space, isl_dim_set, 0,
7047 isl_dim_param, 0, nparam);
7048 set = isl_set_reset_space(set, space);
7049 set = isl_set_preimage(set, mat);
7050 nparam = isl_set_dim(set, isl_dim_out);
7051 space = isl_set_get_space(set);
7052 space = isl_space_move_dims(space, isl_dim_param, 0,
7053 isl_dim_out, 0, nparam);
7054 set = isl_set_reset_space(set, space);
7055 return set;
7056 error:
7057 isl_mat_free(mat);
7058 isl_set_free(set);
7059 return NULL;
7062 /* Intersect the basic set "bset" with the affine space specified by the
7063 * equalities in "eq".
7065 static __isl_give isl_basic_set *basic_set_append_equalities(
7066 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7068 int i, k;
7069 unsigned len;
7071 if (!bset || !eq)
7072 goto error;
7074 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7075 eq->n_row, 0);
7076 if (!bset)
7077 goto error;
7079 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7080 for (i = 0; i < eq->n_row; ++i) {
7081 k = isl_basic_set_alloc_equality(bset);
7082 if (k < 0)
7083 goto error;
7084 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7085 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7087 isl_mat_free(eq);
7089 bset = isl_basic_set_gauss(bset, NULL);
7090 bset = isl_basic_set_finalize(bset);
7092 return bset;
7093 error:
7094 isl_mat_free(eq);
7095 isl_basic_set_free(bset);
7096 return NULL;
7099 /* Intersect the set "set" with the affine space specified by the
7100 * equalities in "eq".
7102 static struct isl_set *set_append_equalities(struct isl_set *set,
7103 struct isl_mat *eq)
7105 int i;
7107 if (!set || !eq)
7108 goto error;
7110 for (i = 0; i < set->n; ++i) {
7111 set->p[i] = basic_set_append_equalities(set->p[i],
7112 isl_mat_copy(eq));
7113 if (!set->p[i])
7114 goto error;
7116 isl_mat_free(eq);
7117 return set;
7118 error:
7119 isl_mat_free(eq);
7120 isl_set_free(set);
7121 return NULL;
7124 /* Given a basic set "bset" that only involves parameters and existentially
7125 * quantified variables, return the index of the first equality
7126 * that only involves parameters. If there is no such equality then
7127 * return bset->n_eq.
7129 * This function assumes that isl_basic_set_gauss has been called on "bset".
7131 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7133 int i, j;
7134 unsigned nparam, n_div;
7136 if (!bset)
7137 return -1;
7139 nparam = isl_basic_set_dim(bset, isl_dim_param);
7140 n_div = isl_basic_set_dim(bset, isl_dim_div);
7142 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7143 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7144 ++i;
7147 return i;
7150 /* Compute an explicit representation for the existentially quantified
7151 * variables in "bset" by computing the "minimal value" of the set
7152 * variables. Since there are no set variables, the computation of
7153 * the minimal value essentially computes an explicit representation
7154 * of the non-empty part(s) of "bset".
7156 * The input only involves parameters and existentially quantified variables.
7157 * All equalities among parameters have been removed.
7159 * Since the existentially quantified variables in the result are in general
7160 * going to be different from those in the input, we first replace
7161 * them by the minimal number of variables based on their equalities.
7162 * This should simplify the parametric integer programming.
7164 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7166 isl_morph *morph1, *morph2;
7167 isl_set *set;
7168 unsigned n;
7170 if (!bset)
7171 return NULL;
7172 if (bset->n_eq == 0)
7173 return isl_basic_set_lexmin_compute_divs(bset);
7175 morph1 = isl_basic_set_parameter_compression(bset);
7176 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7177 bset = isl_basic_set_lift(bset);
7178 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7179 bset = isl_morph_basic_set(morph2, bset);
7180 n = isl_basic_set_dim(bset, isl_dim_set);
7181 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7183 set = isl_basic_set_lexmin_compute_divs(bset);
7185 set = isl_morph_set(isl_morph_inverse(morph1), set);
7187 return set;
7190 /* Project the given basic set onto its parameter domain, possibly introducing
7191 * new, explicit, existential variables in the constraints.
7192 * The input has parameters and (possibly implicit) existential variables.
7193 * The output has the same parameters, but only
7194 * explicit existentially quantified variables.
7196 * The actual projection is performed by pip, but pip doesn't seem
7197 * to like equalities very much, so we first remove the equalities
7198 * among the parameters by performing a variable compression on
7199 * the parameters. Afterward, an inverse transformation is performed
7200 * and the equalities among the parameters are inserted back in.
7202 * The variable compression on the parameters may uncover additional
7203 * equalities that were only implicit before. We therefore check
7204 * if there are any new parameter equalities in the result and
7205 * if so recurse. The removal of parameter equalities is required
7206 * for the parameter compression performed by base_compute_divs.
7208 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7210 int i;
7211 struct isl_mat *eq;
7212 struct isl_mat *T, *T2;
7213 struct isl_set *set;
7214 unsigned nparam;
7216 bset = isl_basic_set_cow(bset);
7217 if (!bset)
7218 return NULL;
7220 if (bset->n_eq == 0)
7221 return base_compute_divs(bset);
7223 bset = isl_basic_set_gauss(bset, NULL);
7224 if (!bset)
7225 return NULL;
7226 if (isl_basic_set_plain_is_empty(bset))
7227 return isl_set_from_basic_set(bset);
7229 i = first_parameter_equality(bset);
7230 if (i == bset->n_eq)
7231 return base_compute_divs(bset);
7233 nparam = isl_basic_set_dim(bset, isl_dim_param);
7234 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7235 0, 1 + nparam);
7236 eq = isl_mat_cow(eq);
7237 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7238 if (T && T->n_col == 0) {
7239 isl_mat_free(T);
7240 isl_mat_free(T2);
7241 isl_mat_free(eq);
7242 bset = isl_basic_set_set_to_empty(bset);
7243 return isl_set_from_basic_set(bset);
7245 bset = basic_set_parameter_preimage(bset, T);
7247 i = first_parameter_equality(bset);
7248 if (!bset)
7249 set = NULL;
7250 else if (i == bset->n_eq)
7251 set = base_compute_divs(bset);
7252 else
7253 set = parameter_compute_divs(bset);
7254 set = set_parameter_preimage(set, T2);
7255 set = set_append_equalities(set, eq);
7256 return set;
7259 /* Insert the divs from "ls" before those of "bmap".
7261 * The number of columns is not changed, which means that the last
7262 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7263 * The caller is responsible for removing the same number of dimensions
7264 * from the space of "bmap".
7266 static __isl_give isl_basic_map *insert_divs_from_local_space(
7267 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7269 int i;
7270 int n_div;
7271 int old_n_div;
7273 n_div = isl_local_space_dim(ls, isl_dim_div);
7274 if (n_div == 0)
7275 return bmap;
7277 old_n_div = bmap->n_div;
7278 bmap = insert_div_rows(bmap, n_div);
7279 if (!bmap)
7280 return NULL;
7282 for (i = 0; i < n_div; ++i) {
7283 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7284 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7287 return bmap;
7290 /* Replace the space of "bmap" by the space and divs of "ls".
7292 * If "ls" has any divs, then we simplify the result since we may
7293 * have discovered some additional equalities that could simplify
7294 * the div expressions.
7296 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7297 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7299 int n_div;
7301 bmap = isl_basic_map_cow(bmap);
7302 if (!bmap || !ls)
7303 goto error;
7305 n_div = isl_local_space_dim(ls, isl_dim_div);
7306 bmap = insert_divs_from_local_space(bmap, ls);
7307 if (!bmap)
7308 goto error;
7310 isl_space_free(bmap->dim);
7311 bmap->dim = isl_local_space_get_space(ls);
7312 if (!bmap->dim)
7313 goto error;
7315 isl_local_space_free(ls);
7316 if (n_div > 0)
7317 bmap = isl_basic_map_simplify(bmap);
7318 bmap = isl_basic_map_finalize(bmap);
7319 return bmap;
7320 error:
7321 isl_basic_map_free(bmap);
7322 isl_local_space_free(ls);
7323 return NULL;
7326 /* Replace the space of "map" by the space and divs of "ls".
7328 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7329 __isl_take isl_local_space *ls)
7331 int i;
7333 map = isl_map_cow(map);
7334 if (!map || !ls)
7335 goto error;
7337 for (i = 0; i < map->n; ++i) {
7338 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7339 isl_local_space_copy(ls));
7340 if (!map->p[i])
7341 goto error;
7343 isl_space_free(map->dim);
7344 map->dim = isl_local_space_get_space(ls);
7345 if (!map->dim)
7346 goto error;
7348 isl_local_space_free(ls);
7349 return map;
7350 error:
7351 isl_local_space_free(ls);
7352 isl_map_free(map);
7353 return NULL;
7356 /* Compute an explicit representation for the existentially
7357 * quantified variables for which do not know any explicit representation yet.
7359 * We first sort the existentially quantified variables so that the
7360 * existentially quantified variables for which we already have an explicit
7361 * representation are placed before those for which we do not.
7362 * The input dimensions, the output dimensions and the existentially
7363 * quantified variables for which we already have an explicit
7364 * representation are then turned into parameters.
7365 * compute_divs returns a map with the same parameters and
7366 * no input or output dimensions and the dimension specification
7367 * is reset to that of the input, including the existentially quantified
7368 * variables for which we already had an explicit representation.
7370 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7372 struct isl_basic_set *bset;
7373 struct isl_set *set;
7374 struct isl_map *map;
7375 isl_space *dim;
7376 isl_local_space *ls;
7377 unsigned nparam;
7378 unsigned n_in;
7379 unsigned n_out;
7380 int n_known;
7381 int i;
7383 bmap = isl_basic_map_sort_divs(bmap);
7384 bmap = isl_basic_map_cow(bmap);
7385 if (!bmap)
7386 return NULL;
7388 n_known = isl_basic_map_first_unknown_div(bmap);
7389 if (n_known < 0)
7390 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7392 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7393 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7394 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7395 dim = isl_space_set_alloc(bmap->ctx,
7396 nparam + n_in + n_out + n_known, 0);
7397 if (!dim)
7398 goto error;
7400 ls = isl_basic_map_get_local_space(bmap);
7401 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7402 n_known, bmap->n_div - n_known);
7403 if (n_known > 0) {
7404 for (i = n_known; i < bmap->n_div; ++i)
7405 swap_div(bmap, i - n_known, i);
7406 bmap->n_div -= n_known;
7407 bmap->extra -= n_known;
7409 bmap = isl_basic_map_reset_space(bmap, dim);
7410 bset = bset_from_bmap(bmap);
7412 set = parameter_compute_divs(bset);
7413 map = set_to_map(set);
7414 map = replace_space_by_local_space(map, ls);
7416 return map;
7417 error:
7418 isl_basic_map_free(bmap);
7419 return NULL;
7422 /* Remove the explicit representation of local variable "div",
7423 * if there is any.
7425 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7426 __isl_take isl_basic_map *bmap, int div)
7428 isl_bool unknown;
7430 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7431 if (unknown < 0)
7432 return isl_basic_map_free(bmap);
7433 if (unknown)
7434 return bmap;
7436 bmap = isl_basic_map_cow(bmap);
7437 if (!bmap)
7438 return NULL;
7439 isl_int_set_si(bmap->div[div][0], 0);
7440 return bmap;
7443 /* Is local variable "div" of "bmap" marked as not having an explicit
7444 * representation?
7445 * Note that even if "div" is not marked in this way and therefore
7446 * has an explicit representation, this representation may still
7447 * depend (indirectly) on other local variables that do not
7448 * have an explicit representation.
7450 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7451 int div)
7453 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7454 return isl_bool_error;
7455 return isl_int_is_zero(bmap->div[div][0]);
7458 /* Return the position of the first local variable that does not
7459 * have an explicit representation.
7460 * Return the total number of local variables if they all have
7461 * an explicit representation.
7462 * Return -1 on error.
7464 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7466 int i;
7468 if (!bmap)
7469 return -1;
7471 for (i = 0; i < bmap->n_div; ++i) {
7472 if (!isl_basic_map_div_is_known(bmap, i))
7473 return i;
7475 return bmap->n_div;
7478 /* Return the position of the first local variable that does not
7479 * have an explicit representation.
7480 * Return the total number of local variables if they all have
7481 * an explicit representation.
7482 * Return -1 on error.
7484 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7486 return isl_basic_map_first_unknown_div(bset);
7489 /* Does "bmap" have an explicit representation for all local variables?
7491 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7493 int first, n;
7495 n = isl_basic_map_dim(bmap, isl_dim_div);
7496 first = isl_basic_map_first_unknown_div(bmap);
7497 if (first < 0)
7498 return isl_bool_error;
7499 return first == n;
7502 /* Do all basic maps in "map" have an explicit representation
7503 * for all local variables?
7505 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7507 int i;
7509 if (!map)
7510 return isl_bool_error;
7512 for (i = 0; i < map->n; ++i) {
7513 int known = isl_basic_map_divs_known(map->p[i]);
7514 if (known <= 0)
7515 return known;
7518 return isl_bool_true;
7521 /* If bmap contains any unknown divs, then compute explicit
7522 * expressions for them. However, this computation may be
7523 * quite expensive, so first try to remove divs that aren't
7524 * strictly needed.
7526 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7528 int known;
7529 struct isl_map *map;
7531 known = isl_basic_map_divs_known(bmap);
7532 if (known < 0)
7533 goto error;
7534 if (known)
7535 return isl_map_from_basic_map(bmap);
7537 bmap = isl_basic_map_drop_redundant_divs(bmap);
7539 known = isl_basic_map_divs_known(bmap);
7540 if (known < 0)
7541 goto error;
7542 if (known)
7543 return isl_map_from_basic_map(bmap);
7545 map = compute_divs(bmap);
7546 return map;
7547 error:
7548 isl_basic_map_free(bmap);
7549 return NULL;
7552 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7554 int i;
7555 int known;
7556 struct isl_map *res;
7558 if (!map)
7559 return NULL;
7560 if (map->n == 0)
7561 return map;
7563 known = isl_map_divs_known(map);
7564 if (known < 0) {
7565 isl_map_free(map);
7566 return NULL;
7568 if (known)
7569 return map;
7571 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7572 for (i = 1 ; i < map->n; ++i) {
7573 struct isl_map *r2;
7574 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7575 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7576 res = isl_map_union_disjoint(res, r2);
7577 else
7578 res = isl_map_union(res, r2);
7580 isl_map_free(map);
7582 return res;
7585 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7587 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7590 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7592 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7595 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7597 int i;
7598 struct isl_set *set;
7600 if (!map)
7601 goto error;
7603 map = isl_map_cow(map);
7604 if (!map)
7605 return NULL;
7607 set = set_from_map(map);
7608 set->dim = isl_space_domain(set->dim);
7609 if (!set->dim)
7610 goto error;
7611 for (i = 0; i < map->n; ++i) {
7612 set->p[i] = isl_basic_map_domain(map->p[i]);
7613 if (!set->p[i])
7614 goto error;
7616 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7617 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7618 return set;
7619 error:
7620 isl_map_free(map);
7621 return NULL;
7624 /* Return the union of "map1" and "map2", where we assume for now that
7625 * "map1" and "map2" are disjoint. Note that the basic maps inside
7626 * "map1" or "map2" may not be disjoint from each other.
7627 * Also note that this function is also called from isl_map_union,
7628 * which takes care of handling the situation where "map1" and "map2"
7629 * may not be disjoint.
7631 * If one of the inputs is empty, we can simply return the other input.
7632 * Similarly, if one of the inputs is universal, then it is equal to the union.
7634 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7635 __isl_take isl_map *map2)
7637 int i;
7638 unsigned flags = 0;
7639 struct isl_map *map = NULL;
7640 int is_universe;
7642 if (!map1 || !map2)
7643 goto error;
7645 if (!isl_space_is_equal(map1->dim, map2->dim))
7646 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7647 "spaces don't match", goto error);
7649 if (map1->n == 0) {
7650 isl_map_free(map1);
7651 return map2;
7653 if (map2->n == 0) {
7654 isl_map_free(map2);
7655 return map1;
7658 is_universe = isl_map_plain_is_universe(map1);
7659 if (is_universe < 0)
7660 goto error;
7661 if (is_universe) {
7662 isl_map_free(map2);
7663 return map1;
7666 is_universe = isl_map_plain_is_universe(map2);
7667 if (is_universe < 0)
7668 goto error;
7669 if (is_universe) {
7670 isl_map_free(map1);
7671 return map2;
7674 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7675 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7676 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7678 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7679 map1->n + map2->n, flags);
7680 if (!map)
7681 goto error;
7682 for (i = 0; i < map1->n; ++i) {
7683 map = isl_map_add_basic_map(map,
7684 isl_basic_map_copy(map1->p[i]));
7685 if (!map)
7686 goto error;
7688 for (i = 0; i < map2->n; ++i) {
7689 map = isl_map_add_basic_map(map,
7690 isl_basic_map_copy(map2->p[i]));
7691 if (!map)
7692 goto error;
7694 isl_map_free(map1);
7695 isl_map_free(map2);
7696 return map;
7697 error:
7698 isl_map_free(map);
7699 isl_map_free(map1);
7700 isl_map_free(map2);
7701 return NULL;
7704 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7705 * guaranteed to be disjoint by the caller.
7707 * Note that this functions is called from within isl_map_make_disjoint,
7708 * so we have to be careful not to touch the constraints of the inputs
7709 * in any way.
7711 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7712 __isl_take isl_map *map2)
7714 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7717 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7718 * not be disjoint. The parameters are assumed to have been aligned.
7720 * We currently simply call map_union_disjoint, the internal operation
7721 * of which does not really depend on the inputs being disjoint.
7722 * If the result contains more than one basic map, then we clear
7723 * the disjoint flag since the result may contain basic maps from
7724 * both inputs and these are not guaranteed to be disjoint.
7726 * As a special case, if "map1" and "map2" are obviously equal,
7727 * then we simply return "map1".
7729 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7730 __isl_take isl_map *map2)
7732 int equal;
7734 if (!map1 || !map2)
7735 goto error;
7737 equal = isl_map_plain_is_equal(map1, map2);
7738 if (equal < 0)
7739 goto error;
7740 if (equal) {
7741 isl_map_free(map2);
7742 return map1;
7745 map1 = map_union_disjoint(map1, map2);
7746 if (!map1)
7747 return NULL;
7748 if (map1->n > 1)
7749 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7750 return map1;
7751 error:
7752 isl_map_free(map1);
7753 isl_map_free(map2);
7754 return NULL;
7757 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7758 * not be disjoint.
7760 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7761 __isl_take isl_map *map2)
7763 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7766 __isl_give isl_set *isl_set_union_disjoint(
7767 __isl_take isl_set *set1, __isl_take isl_set *set2)
7769 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7770 set_to_map(set2)));
7773 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7775 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7778 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7779 * the results.
7781 * "map" and "set" are assumed to be compatible and non-NULL.
7783 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7784 __isl_take isl_set *set,
7785 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7786 __isl_take isl_basic_set *bset))
7788 unsigned flags = 0;
7789 struct isl_map *result;
7790 int i, j;
7792 if (isl_set_plain_is_universe(set)) {
7793 isl_set_free(set);
7794 return map;
7797 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7798 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7799 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7801 result = isl_map_alloc_space(isl_space_copy(map->dim),
7802 map->n * set->n, flags);
7803 for (i = 0; result && i < map->n; ++i)
7804 for (j = 0; j < set->n; ++j) {
7805 result = isl_map_add_basic_map(result,
7806 fn(isl_basic_map_copy(map->p[i]),
7807 isl_basic_set_copy(set->p[j])));
7808 if (!result)
7809 break;
7812 isl_map_free(map);
7813 isl_set_free(set);
7814 return result;
7817 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7818 __isl_take isl_set *set)
7820 isl_bool ok;
7822 ok = isl_map_compatible_range(map, set);
7823 if (ok < 0)
7824 goto error;
7825 if (!ok)
7826 isl_die(set->ctx, isl_error_invalid,
7827 "incompatible spaces", goto error);
7829 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7830 error:
7831 isl_map_free(map);
7832 isl_set_free(set);
7833 return NULL;
7836 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7837 __isl_take isl_set *set)
7839 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7842 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7843 __isl_take isl_set *set)
7845 isl_bool ok;
7847 ok = isl_map_compatible_domain(map, set);
7848 if (ok < 0)
7849 goto error;
7850 if (!ok)
7851 isl_die(set->ctx, isl_error_invalid,
7852 "incompatible spaces", goto error);
7854 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7855 error:
7856 isl_map_free(map);
7857 isl_set_free(set);
7858 return NULL;
7861 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7862 __isl_take isl_set *set)
7864 return isl_map_align_params_map_map_and(map, set,
7865 &map_intersect_domain);
7868 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7869 * in the space B -> C, return the intersection.
7870 * The parameters are assumed to have been aligned.
7872 * The map "factor" is first extended to a map living in the space
7873 * [A -> B] -> C and then a regular intersection is computed.
7875 static __isl_give isl_map *map_intersect_domain_factor_range(
7876 __isl_take isl_map *map, __isl_take isl_map *factor)
7878 isl_space *space;
7879 isl_map *ext_factor;
7881 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7882 ext_factor = isl_map_universe(space);
7883 ext_factor = isl_map_domain_product(ext_factor, factor);
7884 return map_intersect(map, ext_factor);
7887 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7888 * in the space B -> C, return the intersection.
7890 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7891 __isl_take isl_map *map, __isl_take isl_map *factor)
7893 return isl_map_align_params_map_map_and(map, factor,
7894 &map_intersect_domain_factor_range);
7897 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7898 * in the space A -> C, return the intersection.
7900 * The map "factor" is first extended to a map living in the space
7901 * A -> [B -> C] and then a regular intersection is computed.
7903 static __isl_give isl_map *map_intersect_range_factor_range(
7904 __isl_take isl_map *map, __isl_take isl_map *factor)
7906 isl_space *space;
7907 isl_map *ext_factor;
7909 space = isl_space_range_factor_domain(isl_map_get_space(map));
7910 ext_factor = isl_map_universe(space);
7911 ext_factor = isl_map_range_product(ext_factor, factor);
7912 return isl_map_intersect(map, ext_factor);
7915 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7916 * in the space A -> C, return the intersection.
7918 __isl_give isl_map *isl_map_intersect_range_factor_range(
7919 __isl_take isl_map *map, __isl_take isl_map *factor)
7921 return isl_map_align_params_map_map_and(map, factor,
7922 &map_intersect_range_factor_range);
7925 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7926 __isl_take isl_map *map2)
7928 if (!map1 || !map2)
7929 goto error;
7930 map1 = isl_map_reverse(map1);
7931 map1 = isl_map_apply_range(map1, map2);
7932 return isl_map_reverse(map1);
7933 error:
7934 isl_map_free(map1);
7935 isl_map_free(map2);
7936 return NULL;
7939 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7940 __isl_take isl_map *map2)
7942 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7945 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7946 __isl_take isl_map *map2)
7948 isl_space *dim_result;
7949 struct isl_map *result;
7950 int i, j;
7952 if (!map1 || !map2)
7953 goto error;
7955 dim_result = isl_space_join(isl_space_copy(map1->dim),
7956 isl_space_copy(map2->dim));
7958 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7959 if (!result)
7960 goto error;
7961 for (i = 0; i < map1->n; ++i)
7962 for (j = 0; j < map2->n; ++j) {
7963 result = isl_map_add_basic_map(result,
7964 isl_basic_map_apply_range(
7965 isl_basic_map_copy(map1->p[i]),
7966 isl_basic_map_copy(map2->p[j])));
7967 if (!result)
7968 goto error;
7970 isl_map_free(map1);
7971 isl_map_free(map2);
7972 if (result && result->n <= 1)
7973 ISL_F_SET(result, ISL_MAP_DISJOINT);
7974 return result;
7975 error:
7976 isl_map_free(map1);
7977 isl_map_free(map2);
7978 return NULL;
7981 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7982 __isl_take isl_map *map2)
7984 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7988 * returns range - domain
7990 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
7992 isl_space *target_space;
7993 struct isl_basic_set *bset;
7994 unsigned dim;
7995 unsigned nparam;
7996 int i;
7998 if (!bmap)
7999 goto error;
8000 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8001 bmap->dim, isl_dim_out),
8002 goto error);
8003 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8004 dim = isl_basic_map_dim(bmap, isl_dim_in);
8005 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8006 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8007 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8008 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8009 for (i = 0; i < dim; ++i) {
8010 int j = isl_basic_map_alloc_equality(bmap);
8011 if (j < 0) {
8012 bmap = isl_basic_map_free(bmap);
8013 break;
8015 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8016 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8017 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8018 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8020 bset = isl_basic_map_domain(bmap);
8021 bset = isl_basic_set_reset_space(bset, target_space);
8022 return bset;
8023 error:
8024 isl_basic_map_free(bmap);
8025 return NULL;
8029 * returns range - domain
8031 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8033 int i;
8034 isl_space *dim;
8035 struct isl_set *result;
8037 if (!map)
8038 return NULL;
8040 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8041 map->dim, isl_dim_out),
8042 goto error);
8043 dim = isl_map_get_space(map);
8044 dim = isl_space_domain(dim);
8045 result = isl_set_alloc_space(dim, map->n, 0);
8046 if (!result)
8047 goto error;
8048 for (i = 0; i < map->n; ++i)
8049 result = isl_set_add_basic_set(result,
8050 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8051 isl_map_free(map);
8052 return result;
8053 error:
8054 isl_map_free(map);
8055 return NULL;
8059 * returns [domain -> range] -> range - domain
8061 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8062 __isl_take isl_basic_map *bmap)
8064 int i, k;
8065 isl_space *dim;
8066 isl_basic_map *domain;
8067 int nparam, n;
8068 unsigned total;
8070 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8071 bmap->dim, isl_dim_out))
8072 isl_die(bmap->ctx, isl_error_invalid,
8073 "domain and range don't match", goto error);
8075 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8076 n = isl_basic_map_dim(bmap, isl_dim_in);
8078 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
8079 domain = isl_basic_map_universe(dim);
8081 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8082 bmap = isl_basic_map_apply_range(bmap, domain);
8083 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8085 total = isl_basic_map_total_dim(bmap);
8087 for (i = 0; i < n; ++i) {
8088 k = isl_basic_map_alloc_equality(bmap);
8089 if (k < 0)
8090 goto error;
8091 isl_seq_clr(bmap->eq[k], 1 + total);
8092 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8093 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8094 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8097 bmap = isl_basic_map_gauss(bmap, NULL);
8098 return isl_basic_map_finalize(bmap);
8099 error:
8100 isl_basic_map_free(bmap);
8101 return NULL;
8105 * returns [domain -> range] -> range - domain
8107 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8109 int i;
8110 isl_space *domain_dim;
8112 if (!map)
8113 return NULL;
8115 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8116 map->dim, isl_dim_out))
8117 isl_die(map->ctx, isl_error_invalid,
8118 "domain and range don't match", goto error);
8120 map = isl_map_cow(map);
8121 if (!map)
8122 return NULL;
8124 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
8125 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8126 map->dim = isl_space_join(map->dim, domain_dim);
8127 if (!map->dim)
8128 goto error;
8129 for (i = 0; i < map->n; ++i) {
8130 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8131 if (!map->p[i])
8132 goto error;
8134 map = isl_map_unmark_normalized(map);
8135 return map;
8136 error:
8137 isl_map_free(map);
8138 return NULL;
8141 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
8143 struct isl_basic_map *bmap;
8144 unsigned nparam;
8145 unsigned dim;
8146 int i;
8148 if (!dims)
8149 return NULL;
8151 nparam = dims->nparam;
8152 dim = dims->n_out;
8153 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
8154 if (!bmap)
8155 goto error;
8157 for (i = 0; i < dim; ++i) {
8158 int j = isl_basic_map_alloc_equality(bmap);
8159 if (j < 0)
8160 goto error;
8161 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8162 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8163 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
8165 return isl_basic_map_finalize(bmap);
8166 error:
8167 isl_basic_map_free(bmap);
8168 return NULL;
8171 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
8173 if (!dim)
8174 return NULL;
8175 if (dim->n_in != dim->n_out)
8176 isl_die(dim->ctx, isl_error_invalid,
8177 "number of input and output dimensions needs to be "
8178 "the same", goto error);
8179 return basic_map_identity(dim);
8180 error:
8181 isl_space_free(dim);
8182 return NULL;
8185 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8187 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8190 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8192 isl_space *dim = isl_set_get_space(set);
8193 isl_map *id;
8194 id = isl_map_identity(isl_space_map_from_set(dim));
8195 return isl_map_intersect_range(id, set);
8198 /* Construct a basic set with all set dimensions having only non-negative
8199 * values.
8201 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8202 __isl_take isl_space *space)
8204 int i;
8205 unsigned nparam;
8206 unsigned dim;
8207 struct isl_basic_set *bset;
8209 if (!space)
8210 return NULL;
8211 nparam = space->nparam;
8212 dim = space->n_out;
8213 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8214 if (!bset)
8215 return NULL;
8216 for (i = 0; i < dim; ++i) {
8217 int k = isl_basic_set_alloc_inequality(bset);
8218 if (k < 0)
8219 goto error;
8220 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8221 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8223 return bset;
8224 error:
8225 isl_basic_set_free(bset);
8226 return NULL;
8229 /* Construct the half-space x_pos >= 0.
8231 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
8232 int pos)
8234 int k;
8235 isl_basic_set *nonneg;
8237 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8238 k = isl_basic_set_alloc_inequality(nonneg);
8239 if (k < 0)
8240 goto error;
8241 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8242 isl_int_set_si(nonneg->ineq[k][pos], 1);
8244 return isl_basic_set_finalize(nonneg);
8245 error:
8246 isl_basic_set_free(nonneg);
8247 return NULL;
8250 /* Construct the half-space x_pos <= -1.
8252 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
8254 int k;
8255 isl_basic_set *neg;
8257 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8258 k = isl_basic_set_alloc_inequality(neg);
8259 if (k < 0)
8260 goto error;
8261 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8262 isl_int_set_si(neg->ineq[k][0], -1);
8263 isl_int_set_si(neg->ineq[k][pos], -1);
8265 return isl_basic_set_finalize(neg);
8266 error:
8267 isl_basic_set_free(neg);
8268 return NULL;
8271 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8272 enum isl_dim_type type, unsigned first, unsigned n)
8274 int i;
8275 unsigned offset;
8276 isl_basic_set *nonneg;
8277 isl_basic_set *neg;
8279 if (!set)
8280 return NULL;
8281 if (n == 0)
8282 return set;
8284 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
8286 offset = pos(set->dim, type);
8287 for (i = 0; i < n; ++i) {
8288 nonneg = nonneg_halfspace(isl_set_get_space(set),
8289 offset + first + i);
8290 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8292 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8295 return set;
8296 error:
8297 isl_set_free(set);
8298 return NULL;
8301 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8302 int len,
8303 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8304 void *user)
8306 isl_set *half;
8308 if (!set)
8309 return isl_stat_error;
8310 if (isl_set_plain_is_empty(set)) {
8311 isl_set_free(set);
8312 return isl_stat_ok;
8314 if (first == len)
8315 return fn(set, signs, user);
8317 signs[first] = 1;
8318 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8319 1 + first));
8320 half = isl_set_intersect(half, isl_set_copy(set));
8321 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8322 goto error;
8324 signs[first] = -1;
8325 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8326 1 + first));
8327 half = isl_set_intersect(half, set);
8328 return foreach_orthant(half, signs, first + 1, len, fn, user);
8329 error:
8330 isl_set_free(set);
8331 return isl_stat_error;
8334 /* Call "fn" on the intersections of "set" with each of the orthants
8335 * (except for obviously empty intersections). The orthant is identified
8336 * by the signs array, with each entry having value 1 or -1 according
8337 * to the sign of the corresponding variable.
8339 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8340 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8341 void *user)
8343 unsigned nparam;
8344 unsigned nvar;
8345 int *signs;
8346 isl_stat r;
8348 if (!set)
8349 return isl_stat_error;
8350 if (isl_set_plain_is_empty(set))
8351 return isl_stat_ok;
8353 nparam = isl_set_dim(set, isl_dim_param);
8354 nvar = isl_set_dim(set, isl_dim_set);
8356 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8358 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8359 fn, user);
8361 free(signs);
8363 return r;
8366 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8368 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8371 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8372 __isl_keep isl_basic_map *bmap2)
8374 int is_subset;
8375 struct isl_map *map1;
8376 struct isl_map *map2;
8378 if (!bmap1 || !bmap2)
8379 return isl_bool_error;
8381 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8382 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8384 is_subset = isl_map_is_subset(map1, map2);
8386 isl_map_free(map1);
8387 isl_map_free(map2);
8389 return is_subset;
8392 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8393 __isl_keep isl_basic_set *bset2)
8395 return isl_basic_map_is_subset(bset1, bset2);
8398 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8399 __isl_keep isl_basic_map *bmap2)
8401 isl_bool is_subset;
8403 if (!bmap1 || !bmap2)
8404 return isl_bool_error;
8405 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8406 if (is_subset != isl_bool_true)
8407 return is_subset;
8408 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8409 return is_subset;
8412 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8413 __isl_keep isl_basic_set *bset2)
8415 return isl_basic_map_is_equal(
8416 bset_to_bmap(bset1), bset_to_bmap(bset2));
8419 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8421 int i;
8422 int is_empty;
8424 if (!map)
8425 return isl_bool_error;
8426 for (i = 0; i < map->n; ++i) {
8427 is_empty = isl_basic_map_is_empty(map->p[i]);
8428 if (is_empty < 0)
8429 return isl_bool_error;
8430 if (!is_empty)
8431 return isl_bool_false;
8433 return isl_bool_true;
8436 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8438 return map ? map->n == 0 : isl_bool_error;
8441 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8443 return set ? set->n == 0 : isl_bool_error;
8446 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8448 return isl_map_is_empty(set_to_map(set));
8451 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8452 __isl_keep isl_map *map2)
8454 if (!map1 || !map2)
8455 return isl_bool_error;
8457 return isl_space_is_equal(map1->dim, map2->dim);
8460 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8461 __isl_keep isl_set *set2)
8463 if (!set1 || !set2)
8464 return isl_bool_error;
8466 return isl_space_is_equal(set1->dim, set2->dim);
8469 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8471 isl_bool is_subset;
8473 if (!map1 || !map2)
8474 return isl_bool_error;
8475 is_subset = isl_map_is_subset(map1, map2);
8476 if (is_subset != isl_bool_true)
8477 return is_subset;
8478 is_subset = isl_map_is_subset(map2, map1);
8479 return is_subset;
8482 /* Is "map1" equal to "map2"?
8484 * First check if they are obviously equal.
8485 * If not, then perform a more detailed analysis.
8487 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8489 isl_bool equal;
8491 equal = isl_map_plain_is_equal(map1, map2);
8492 if (equal < 0 || equal)
8493 return equal;
8494 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8497 isl_bool isl_basic_map_is_strict_subset(
8498 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8500 isl_bool is_subset;
8502 if (!bmap1 || !bmap2)
8503 return isl_bool_error;
8504 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8505 if (is_subset != isl_bool_true)
8506 return is_subset;
8507 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8508 if (is_subset == isl_bool_error)
8509 return is_subset;
8510 return !is_subset;
8513 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8514 __isl_keep isl_map *map2)
8516 isl_bool is_subset;
8518 if (!map1 || !map2)
8519 return isl_bool_error;
8520 is_subset = isl_map_is_subset(map1, map2);
8521 if (is_subset != isl_bool_true)
8522 return is_subset;
8523 is_subset = isl_map_is_subset(map2, map1);
8524 if (is_subset == isl_bool_error)
8525 return is_subset;
8526 return !is_subset;
8529 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8530 __isl_keep isl_set *set2)
8532 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8535 /* Is "bmap" obviously equal to the universe with the same space?
8537 * That is, does it not have any constraints?
8539 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8541 if (!bmap)
8542 return isl_bool_error;
8543 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8546 /* Is "bset" obviously equal to the universe with the same space?
8548 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8550 return isl_basic_map_plain_is_universe(bset);
8553 /* If "c" does not involve any existentially quantified variables,
8554 * then set *univ to false and abort
8556 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8558 isl_bool *univ = user;
8559 unsigned n;
8561 n = isl_constraint_dim(c, isl_dim_div);
8562 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8563 isl_constraint_free(c);
8564 if (*univ < 0 || !*univ)
8565 return isl_stat_error;
8566 return isl_stat_ok;
8569 /* Is "bmap" equal to the universe with the same space?
8571 * First check if it is obviously equal to the universe.
8572 * If not and if there are any constraints not involving
8573 * existentially quantified variables, then it is certainly
8574 * not equal to the universe.
8575 * Otherwise, check if the universe is a subset of "bmap".
8577 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8579 isl_bool univ;
8580 isl_basic_map *test;
8582 univ = isl_basic_map_plain_is_universe(bmap);
8583 if (univ < 0 || univ)
8584 return univ;
8585 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8586 return isl_bool_false;
8587 univ = isl_bool_true;
8588 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8589 univ)
8590 return isl_bool_error;
8591 if (univ < 0 || !univ)
8592 return univ;
8593 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8594 univ = isl_basic_map_is_subset(test, bmap);
8595 isl_basic_map_free(test);
8596 return univ;
8599 /* Is "bset" equal to the universe with the same space?
8601 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8603 return isl_basic_map_is_universe(bset);
8606 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8608 int i;
8610 if (!map)
8611 return isl_bool_error;
8613 for (i = 0; i < map->n; ++i) {
8614 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8615 if (r < 0 || r)
8616 return r;
8619 return isl_bool_false;
8622 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8624 return isl_map_plain_is_universe(set_to_map(set));
8627 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8629 struct isl_basic_set *bset = NULL;
8630 struct isl_vec *sample = NULL;
8631 isl_bool empty, non_empty;
8633 if (!bmap)
8634 return isl_bool_error;
8636 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8637 return isl_bool_true;
8639 if (isl_basic_map_plain_is_universe(bmap))
8640 return isl_bool_false;
8642 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8643 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8644 copy = isl_basic_map_remove_redundancies(copy);
8645 empty = isl_basic_map_plain_is_empty(copy);
8646 isl_basic_map_free(copy);
8647 return empty;
8650 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8651 if (non_empty < 0)
8652 return isl_bool_error;
8653 if (non_empty)
8654 return isl_bool_false;
8655 isl_vec_free(bmap->sample);
8656 bmap->sample = NULL;
8657 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8658 if (!bset)
8659 return isl_bool_error;
8660 sample = isl_basic_set_sample_vec(bset);
8661 if (!sample)
8662 return isl_bool_error;
8663 empty = sample->size == 0;
8664 isl_vec_free(bmap->sample);
8665 bmap->sample = sample;
8666 if (empty)
8667 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8669 return empty;
8672 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8674 if (!bmap)
8675 return isl_bool_error;
8676 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8679 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8681 if (!bset)
8682 return isl_bool_error;
8683 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8686 /* Is "bmap" known to be non-empty?
8688 * That is, is the cached sample still valid?
8690 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8692 unsigned total;
8694 if (!bmap)
8695 return isl_bool_error;
8696 if (!bmap->sample)
8697 return isl_bool_false;
8698 total = 1 + isl_basic_map_total_dim(bmap);
8699 if (bmap->sample->size != total)
8700 return isl_bool_false;
8701 return isl_basic_map_contains(bmap, bmap->sample);
8704 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8706 return isl_basic_map_is_empty(bset_to_bmap(bset));
8709 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8710 __isl_take isl_basic_map *bmap2)
8712 struct isl_map *map;
8713 if (!bmap1 || !bmap2)
8714 goto error;
8716 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8718 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8719 if (!map)
8720 goto error;
8721 map = isl_map_add_basic_map(map, bmap1);
8722 map = isl_map_add_basic_map(map, bmap2);
8723 return map;
8724 error:
8725 isl_basic_map_free(bmap1);
8726 isl_basic_map_free(bmap2);
8727 return NULL;
8730 struct isl_set *isl_basic_set_union(
8731 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8733 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8734 bset_to_bmap(bset2)));
8737 /* Order divs such that any div only depends on previous divs */
8738 __isl_give isl_basic_map *isl_basic_map_order_divs(
8739 __isl_take isl_basic_map *bmap)
8741 int i;
8742 unsigned off;
8744 if (!bmap)
8745 return NULL;
8747 off = isl_space_dim(bmap->dim, isl_dim_all);
8749 for (i = 0; i < bmap->n_div; ++i) {
8750 int pos;
8751 if (isl_int_is_zero(bmap->div[i][0]))
8752 continue;
8753 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8754 bmap->n_div-i);
8755 if (pos == -1)
8756 continue;
8757 if (pos == 0)
8758 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8759 "integer division depends on itself",
8760 return isl_basic_map_free(bmap));
8761 isl_basic_map_swap_div(bmap, i, i + pos);
8762 --i;
8764 return bmap;
8767 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8769 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8772 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8774 int i;
8776 if (!map)
8777 return 0;
8779 for (i = 0; i < map->n; ++i) {
8780 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8781 if (!map->p[i])
8782 goto error;
8785 return map;
8786 error:
8787 isl_map_free(map);
8788 return NULL;
8791 /* Sort the local variables of "bset".
8793 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8794 __isl_take isl_basic_set *bset)
8796 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8799 /* Apply the expansion computed by isl_merge_divs.
8800 * The expansion itself is given by "exp" while the resulting
8801 * list of divs is given by "div".
8803 * Move the integer divisions of "bmap" into the right position
8804 * according to "exp" and then introduce the additional integer
8805 * divisions, adding div constraints.
8806 * The moving should be done first to avoid moving coefficients
8807 * in the definitions of the extra integer divisions.
8809 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8810 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8812 int i, j;
8813 int n_div;
8815 bmap = isl_basic_map_cow(bmap);
8816 if (!bmap || !div)
8817 goto error;
8819 if (div->n_row < bmap->n_div)
8820 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8821 "not an expansion", goto error);
8823 n_div = bmap->n_div;
8824 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8825 div->n_row - n_div, 0,
8826 2 * (div->n_row - n_div));
8828 for (i = n_div; i < div->n_row; ++i)
8829 if (isl_basic_map_alloc_div(bmap) < 0)
8830 goto error;
8832 for (j = n_div - 1; j >= 0; --j) {
8833 if (exp[j] == j)
8834 break;
8835 isl_basic_map_swap_div(bmap, j, exp[j]);
8837 j = 0;
8838 for (i = 0; i < div->n_row; ++i) {
8839 if (j < n_div && exp[j] == i) {
8840 j++;
8841 } else {
8842 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8843 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8844 continue;
8845 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8846 goto error;
8850 isl_mat_free(div);
8851 return bmap;
8852 error:
8853 isl_basic_map_free(bmap);
8854 isl_mat_free(div);
8855 return NULL;
8858 /* Apply the expansion computed by isl_merge_divs.
8859 * The expansion itself is given by "exp" while the resulting
8860 * list of divs is given by "div".
8862 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8863 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8865 return isl_basic_map_expand_divs(bset, div, exp);
8868 /* Look for a div in dst that corresponds to the div "div" in src.
8869 * The divs before "div" in src and dst are assumed to be the same.
8871 * Returns -1 if no corresponding div was found and the position
8872 * of the corresponding div in dst otherwise.
8874 static int find_div(__isl_keep isl_basic_map *dst,
8875 __isl_keep isl_basic_map *src, unsigned div)
8877 int i;
8879 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8881 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8882 for (i = div; i < dst->n_div; ++i)
8883 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8884 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8885 dst->n_div - div) == -1)
8886 return i;
8887 return -1;
8890 /* Align the divs of "dst" to those of "src", adding divs from "src"
8891 * if needed. That is, make sure that the first src->n_div divs
8892 * of the result are equal to those of src.
8894 * The result is not finalized as by design it will have redundant
8895 * divs if any divs from "src" were copied.
8897 __isl_give isl_basic_map *isl_basic_map_align_divs(
8898 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8900 int i;
8901 int known, extended;
8902 unsigned total;
8904 if (!dst || !src)
8905 return isl_basic_map_free(dst);
8907 if (src->n_div == 0)
8908 return dst;
8910 known = isl_basic_map_divs_known(src);
8911 if (known < 0)
8912 return isl_basic_map_free(dst);
8913 if (!known)
8914 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8915 "some src divs are unknown",
8916 return isl_basic_map_free(dst));
8918 src = isl_basic_map_order_divs(src);
8920 extended = 0;
8921 total = isl_space_dim(src->dim, isl_dim_all);
8922 for (i = 0; i < src->n_div; ++i) {
8923 int j = find_div(dst, src, i);
8924 if (j < 0) {
8925 if (!extended) {
8926 int extra = src->n_div - i;
8927 dst = isl_basic_map_cow(dst);
8928 if (!dst)
8929 return NULL;
8930 dst = isl_basic_map_extend_space(dst,
8931 isl_space_copy(dst->dim),
8932 extra, 0, 2 * extra);
8933 extended = 1;
8935 j = isl_basic_map_alloc_div(dst);
8936 if (j < 0)
8937 return isl_basic_map_free(dst);
8938 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8939 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8940 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8941 return isl_basic_map_free(dst);
8943 if (j != i)
8944 isl_basic_map_swap_div(dst, i, j);
8946 return dst;
8949 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8951 int i;
8953 if (!map)
8954 return NULL;
8955 if (map->n == 0)
8956 return map;
8957 map = isl_map_compute_divs(map);
8958 map = isl_map_cow(map);
8959 if (!map)
8960 return NULL;
8962 for (i = 1; i < map->n; ++i)
8963 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8964 for (i = 1; i < map->n; ++i) {
8965 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8966 if (!map->p[i])
8967 return isl_map_free(map);
8970 map = isl_map_unmark_normalized(map);
8971 return map;
8974 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8976 return isl_map_align_divs_internal(map);
8979 struct isl_set *isl_set_align_divs(struct isl_set *set)
8981 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8984 /* Align the divs of the basic maps in "map" to those
8985 * of the basic maps in "list", as well as to the other basic maps in "map".
8986 * The elements in "list" are assumed to have known divs.
8988 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8989 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8991 int i, n;
8993 map = isl_map_compute_divs(map);
8994 map = isl_map_cow(map);
8995 if (!map || !list)
8996 return isl_map_free(map);
8997 if (map->n == 0)
8998 return map;
9000 n = isl_basic_map_list_n_basic_map(list);
9001 for (i = 0; i < n; ++i) {
9002 isl_basic_map *bmap;
9004 bmap = isl_basic_map_list_get_basic_map(list, i);
9005 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9006 isl_basic_map_free(bmap);
9008 if (!map->p[0])
9009 return isl_map_free(map);
9011 return isl_map_align_divs_internal(map);
9014 /* Align the divs of each element of "list" to those of "bmap".
9015 * Both "bmap" and the elements of "list" are assumed to have known divs.
9017 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9018 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9020 int i, n;
9022 if (!list || !bmap)
9023 return isl_basic_map_list_free(list);
9025 n = isl_basic_map_list_n_basic_map(list);
9026 for (i = 0; i < n; ++i) {
9027 isl_basic_map *bmap_i;
9029 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9030 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9031 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9034 return list;
9037 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9038 __isl_take isl_map *map)
9040 isl_bool ok;
9042 ok = isl_map_compatible_domain(map, set);
9043 if (ok < 0)
9044 goto error;
9045 if (!ok)
9046 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9047 "incompatible spaces", goto error);
9048 map = isl_map_intersect_domain(map, set);
9049 set = isl_map_range(map);
9050 return set;
9051 error:
9052 isl_set_free(set);
9053 isl_map_free(map);
9054 return NULL;
9057 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9058 __isl_take isl_map *map)
9060 return isl_map_align_params_map_map_and(set, map, &set_apply);
9063 /* There is no need to cow as removing empty parts doesn't change
9064 * the meaning of the set.
9066 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9068 int i;
9070 if (!map)
9071 return NULL;
9073 for (i = map->n - 1; i >= 0; --i)
9074 map = remove_if_empty(map, i);
9076 return map;
9079 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9081 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9084 /* Given two basic sets bset1 and bset2, compute the maximal difference
9085 * between the values of dimension pos in bset1 and those in bset2
9086 * for any common value of the parameters and dimensions preceding pos.
9088 static enum isl_lp_result basic_set_maximal_difference_at(
9089 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9090 int pos, isl_int *opt)
9092 isl_basic_map *bmap1;
9093 isl_basic_map *bmap2;
9094 struct isl_ctx *ctx;
9095 struct isl_vec *obj;
9096 unsigned total;
9097 unsigned nparam;
9098 unsigned dim1;
9099 enum isl_lp_result res;
9101 if (!bset1 || !bset2)
9102 return isl_lp_error;
9104 nparam = isl_basic_set_n_param(bset1);
9105 dim1 = isl_basic_set_n_dim(bset1);
9107 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9108 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9109 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9110 isl_dim_out, 0, pos);
9111 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9112 isl_dim_out, 0, pos);
9113 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
9114 if (!bmap1)
9115 return isl_lp_error;
9117 total = isl_basic_map_total_dim(bmap1);
9118 ctx = bmap1->ctx;
9119 obj = isl_vec_alloc(ctx, 1 + total);
9120 if (!obj)
9121 goto error;
9122 isl_seq_clr(obj->block.data, 1 + total);
9123 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9124 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9125 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9126 opt, NULL, NULL);
9127 isl_basic_map_free(bmap1);
9128 isl_vec_free(obj);
9129 return res;
9130 error:
9131 isl_basic_map_free(bmap1);
9132 return isl_lp_error;
9135 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9136 * for any common value of the parameters and dimensions preceding pos
9137 * in both basic sets, the values of dimension pos in bset1 are
9138 * smaller or larger than those in bset2.
9140 * Returns
9141 * 1 if bset1 follows bset2
9142 * -1 if bset1 precedes bset2
9143 * 0 if bset1 and bset2 are incomparable
9144 * -2 if some error occurred.
9146 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
9147 struct isl_basic_set *bset2, int pos)
9149 isl_int opt;
9150 enum isl_lp_result res;
9151 int cmp;
9153 isl_int_init(opt);
9155 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9157 if (res == isl_lp_empty)
9158 cmp = 0;
9159 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9160 res == isl_lp_unbounded)
9161 cmp = 1;
9162 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9163 cmp = -1;
9164 else
9165 cmp = -2;
9167 isl_int_clear(opt);
9168 return cmp;
9171 /* Given two basic sets bset1 and bset2, check whether
9172 * for any common value of the parameters and dimensions preceding pos
9173 * there is a value of dimension pos in bset1 that is larger
9174 * than a value of the same dimension in bset2.
9176 * Return
9177 * 1 if there exists such a pair
9178 * 0 if there is no such pair, but there is a pair of equal values
9179 * -1 otherwise
9180 * -2 if some error occurred.
9182 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9183 __isl_keep isl_basic_set *bset2, int pos)
9185 isl_int opt;
9186 enum isl_lp_result res;
9187 int cmp;
9189 isl_int_init(opt);
9191 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9193 if (res == isl_lp_empty)
9194 cmp = -1;
9195 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9196 res == isl_lp_unbounded)
9197 cmp = 1;
9198 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9199 cmp = -1;
9200 else if (res == isl_lp_ok)
9201 cmp = 0;
9202 else
9203 cmp = -2;
9205 isl_int_clear(opt);
9206 return cmp;
9209 /* Given two sets set1 and set2, check whether
9210 * for any common value of the parameters and dimensions preceding pos
9211 * there is a value of dimension pos in set1 that is larger
9212 * than a value of the same dimension in set2.
9214 * Return
9215 * 1 if there exists such a pair
9216 * 0 if there is no such pair, but there is a pair of equal values
9217 * -1 otherwise
9218 * -2 if some error occurred.
9220 int isl_set_follows_at(__isl_keep isl_set *set1,
9221 __isl_keep isl_set *set2, int pos)
9223 int i, j;
9224 int follows = -1;
9226 if (!set1 || !set2)
9227 return -2;
9229 for (i = 0; i < set1->n; ++i)
9230 for (j = 0; j < set2->n; ++j) {
9231 int f;
9232 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9233 if (f == 1 || f == -2)
9234 return f;
9235 if (f > follows)
9236 follows = f;
9239 return follows;
9242 static isl_bool isl_basic_map_plain_has_fixed_var(
9243 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9245 int i;
9246 int d;
9247 unsigned total;
9249 if (!bmap)
9250 return isl_bool_error;
9251 total = isl_basic_map_total_dim(bmap);
9252 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9253 for (; d+1 > pos; --d)
9254 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9255 break;
9256 if (d != pos)
9257 continue;
9258 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9259 return isl_bool_false;
9260 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9261 return isl_bool_false;
9262 if (!isl_int_is_one(bmap->eq[i][1+d]))
9263 return isl_bool_false;
9264 if (val)
9265 isl_int_neg(*val, bmap->eq[i][0]);
9266 return isl_bool_true;
9268 return isl_bool_false;
9271 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9272 unsigned pos, isl_int *val)
9274 int i;
9275 isl_int v;
9276 isl_int tmp;
9277 isl_bool fixed;
9279 if (!map)
9280 return isl_bool_error;
9281 if (map->n == 0)
9282 return isl_bool_false;
9283 if (map->n == 1)
9284 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9285 isl_int_init(v);
9286 isl_int_init(tmp);
9287 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9288 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9289 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9290 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9291 fixed = isl_bool_false;
9293 if (val)
9294 isl_int_set(*val, v);
9295 isl_int_clear(tmp);
9296 isl_int_clear(v);
9297 return fixed;
9300 static isl_bool isl_basic_set_plain_has_fixed_var(
9301 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9303 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9304 pos, val);
9307 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9308 enum isl_dim_type type, unsigned pos, isl_int *val)
9310 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9311 return isl_bool_error;
9312 return isl_basic_map_plain_has_fixed_var(bmap,
9313 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9316 /* If "bmap" obviously lies on a hyperplane where the given dimension
9317 * has a fixed value, then return that value.
9318 * Otherwise return NaN.
9320 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9321 __isl_keep isl_basic_map *bmap,
9322 enum isl_dim_type type, unsigned pos)
9324 isl_ctx *ctx;
9325 isl_val *v;
9326 isl_bool fixed;
9328 if (!bmap)
9329 return NULL;
9330 ctx = isl_basic_map_get_ctx(bmap);
9331 v = isl_val_alloc(ctx);
9332 if (!v)
9333 return NULL;
9334 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9335 if (fixed < 0)
9336 return isl_val_free(v);
9337 if (fixed) {
9338 isl_int_set_si(v->d, 1);
9339 return v;
9341 isl_val_free(v);
9342 return isl_val_nan(ctx);
9345 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9346 enum isl_dim_type type, unsigned pos, isl_int *val)
9348 if (pos >= isl_map_dim(map, type))
9349 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9350 "position out of bounds", return isl_bool_error);
9351 return isl_map_plain_has_fixed_var(map,
9352 map_offset(map, type) - 1 + pos, val);
9355 /* If "map" obviously lies on a hyperplane where the given dimension
9356 * has a fixed value, then return that value.
9357 * Otherwise return NaN.
9359 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9360 enum isl_dim_type type, unsigned pos)
9362 isl_ctx *ctx;
9363 isl_val *v;
9364 isl_bool fixed;
9366 if (!map)
9367 return NULL;
9368 ctx = isl_map_get_ctx(map);
9369 v = isl_val_alloc(ctx);
9370 if (!v)
9371 return NULL;
9372 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9373 if (fixed < 0)
9374 return isl_val_free(v);
9375 if (fixed) {
9376 isl_int_set_si(v->d, 1);
9377 return v;
9379 isl_val_free(v);
9380 return isl_val_nan(ctx);
9383 /* If "set" obviously lies on a hyperplane where the given dimension
9384 * has a fixed value, then return that value.
9385 * Otherwise return NaN.
9387 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9388 enum isl_dim_type type, unsigned pos)
9390 return isl_map_plain_get_val_if_fixed(set, type, pos);
9393 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9394 * then return this fixed value in *val.
9396 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9397 unsigned dim, isl_int *val)
9399 return isl_basic_set_plain_has_fixed_var(bset,
9400 isl_basic_set_n_param(bset) + dim, val);
9403 /* Return -1 if the constraint "c1" should be sorted before "c2"
9404 * and 1 if it should be sorted after "c2".
9405 * Return 0 if the two constraints are the same (up to the constant term).
9407 * In particular, if a constraint involves later variables than another
9408 * then it is sorted after this other constraint.
9409 * uset_gist depends on constraints without existentially quantified
9410 * variables sorting first.
9412 * For constraints that have the same latest variable, those
9413 * with the same coefficient for this latest variable (first in absolute value
9414 * and then in actual value) are grouped together.
9415 * This is useful for detecting pairs of constraints that can
9416 * be chained in their printed representation.
9418 * Finally, within a group, constraints are sorted according to
9419 * their coefficients (excluding the constant term).
9421 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9423 isl_int **c1 = (isl_int **) p1;
9424 isl_int **c2 = (isl_int **) p2;
9425 int l1, l2;
9426 unsigned size = *(unsigned *) arg;
9427 int cmp;
9429 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9430 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9432 if (l1 != l2)
9433 return l1 - l2;
9435 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9436 if (cmp != 0)
9437 return cmp;
9438 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9439 if (cmp != 0)
9440 return -cmp;
9442 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9445 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9446 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9447 * and 0 if the two constraints are the same (up to the constant term).
9449 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9450 isl_int *c1, isl_int *c2)
9452 unsigned total;
9454 if (!bmap)
9455 return -2;
9456 total = isl_basic_map_total_dim(bmap);
9457 return sort_constraint_cmp(&c1, &c2, &total);
9460 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9461 __isl_take isl_basic_map *bmap)
9463 unsigned total;
9465 if (!bmap)
9466 return NULL;
9467 if (bmap->n_ineq == 0)
9468 return bmap;
9469 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9470 return bmap;
9471 total = isl_basic_map_total_dim(bmap);
9472 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9473 &sort_constraint_cmp, &total) < 0)
9474 return isl_basic_map_free(bmap);
9475 return bmap;
9478 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9479 __isl_take isl_basic_set *bset)
9481 isl_basic_map *bmap = bset_to_bmap(bset);
9482 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9485 __isl_give isl_basic_map *isl_basic_map_normalize(
9486 __isl_take isl_basic_map *bmap)
9488 if (!bmap)
9489 return NULL;
9490 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9491 return bmap;
9492 bmap = isl_basic_map_remove_redundancies(bmap);
9493 bmap = isl_basic_map_sort_constraints(bmap);
9494 if (bmap)
9495 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9496 return bmap;
9498 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9499 __isl_keep isl_basic_map *bmap2)
9501 int i, cmp;
9502 unsigned total;
9503 isl_space *space1, *space2;
9505 if (!bmap1 || !bmap2)
9506 return -1;
9508 if (bmap1 == bmap2)
9509 return 0;
9510 space1 = isl_basic_map_peek_space(bmap1);
9511 space2 = isl_basic_map_peek_space(bmap2);
9512 cmp = isl_space_cmp(space1, space2);
9513 if (cmp)
9514 return cmp;
9515 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9516 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9517 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9518 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9519 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9520 return 0;
9521 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9522 return 1;
9523 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9524 return -1;
9525 if (bmap1->n_eq != bmap2->n_eq)
9526 return bmap1->n_eq - bmap2->n_eq;
9527 if (bmap1->n_ineq != bmap2->n_ineq)
9528 return bmap1->n_ineq - bmap2->n_ineq;
9529 if (bmap1->n_div != bmap2->n_div)
9530 return bmap1->n_div - bmap2->n_div;
9531 total = isl_basic_map_total_dim(bmap1);
9532 for (i = 0; i < bmap1->n_eq; ++i) {
9533 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9534 if (cmp)
9535 return cmp;
9537 for (i = 0; i < bmap1->n_ineq; ++i) {
9538 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9539 if (cmp)
9540 return cmp;
9542 for (i = 0; i < bmap1->n_div; ++i) {
9543 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9544 if (cmp)
9545 return cmp;
9547 return 0;
9550 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9551 __isl_keep isl_basic_set *bset2)
9553 return isl_basic_map_plain_cmp(bset1, bset2);
9556 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9558 int i, cmp;
9560 if (set1 == set2)
9561 return 0;
9562 if (set1->n != set2->n)
9563 return set1->n - set2->n;
9565 for (i = 0; i < set1->n; ++i) {
9566 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9567 if (cmp)
9568 return cmp;
9571 return 0;
9574 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9575 __isl_keep isl_basic_map *bmap2)
9577 if (!bmap1 || !bmap2)
9578 return isl_bool_error;
9579 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9582 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9583 __isl_keep isl_basic_set *bset2)
9585 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9586 bset_to_bmap(bset2));
9589 static int qsort_bmap_cmp(const void *p1, const void *p2)
9591 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9592 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9594 return isl_basic_map_plain_cmp(bmap1, bmap2);
9597 /* Sort the basic maps of "map" and remove duplicate basic maps.
9599 * While removing basic maps, we make sure that the basic maps remain
9600 * sorted because isl_map_normalize expects the basic maps of the result
9601 * to be sorted.
9603 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9605 int i, j;
9607 map = isl_map_remove_empty_parts(map);
9608 if (!map)
9609 return NULL;
9610 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9611 for (i = map->n - 1; i >= 1; --i) {
9612 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9613 continue;
9614 isl_basic_map_free(map->p[i-1]);
9615 for (j = i; j < map->n; ++j)
9616 map->p[j - 1] = map->p[j];
9617 map->n--;
9620 return map;
9623 /* Remove obvious duplicates among the basic maps of "map".
9625 * Unlike isl_map_normalize, this function does not remove redundant
9626 * constraints and only removes duplicates that have exactly the same
9627 * constraints in the input. It does sort the constraints and
9628 * the basic maps to ease the detection of duplicates.
9630 * If "map" has already been normalized or if the basic maps are
9631 * disjoint, then there can be no duplicates.
9633 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9635 int i;
9636 isl_basic_map *bmap;
9638 if (!map)
9639 return NULL;
9640 if (map->n <= 1)
9641 return map;
9642 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9643 return map;
9644 for (i = 0; i < map->n; ++i) {
9645 bmap = isl_basic_map_copy(map->p[i]);
9646 bmap = isl_basic_map_sort_constraints(bmap);
9647 if (!bmap)
9648 return isl_map_free(map);
9649 isl_basic_map_free(map->p[i]);
9650 map->p[i] = bmap;
9653 map = sort_and_remove_duplicates(map);
9654 return map;
9657 /* We normalize in place, but if anything goes wrong we need
9658 * to return NULL, so we need to make sure we don't change the
9659 * meaning of any possible other copies of map.
9661 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9663 int i;
9664 struct isl_basic_map *bmap;
9666 if (!map)
9667 return NULL;
9668 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9669 return map;
9670 for (i = 0; i < map->n; ++i) {
9671 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9672 if (!bmap)
9673 goto error;
9674 isl_basic_map_free(map->p[i]);
9675 map->p[i] = bmap;
9678 map = sort_and_remove_duplicates(map);
9679 if (map)
9680 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9681 return map;
9682 error:
9683 isl_map_free(map);
9684 return NULL;
9687 struct isl_set *isl_set_normalize(struct isl_set *set)
9689 return set_from_map(isl_map_normalize(set_to_map(set)));
9692 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9693 __isl_keep isl_map *map2)
9695 int i;
9696 isl_bool equal;
9698 if (!map1 || !map2)
9699 return isl_bool_error;
9701 if (map1 == map2)
9702 return isl_bool_true;
9703 if (!isl_space_is_equal(map1->dim, map2->dim))
9704 return isl_bool_false;
9706 map1 = isl_map_copy(map1);
9707 map2 = isl_map_copy(map2);
9708 map1 = isl_map_normalize(map1);
9709 map2 = isl_map_normalize(map2);
9710 if (!map1 || !map2)
9711 goto error;
9712 equal = map1->n == map2->n;
9713 for (i = 0; equal && i < map1->n; ++i) {
9714 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9715 if (equal < 0)
9716 goto error;
9718 isl_map_free(map1);
9719 isl_map_free(map2);
9720 return equal;
9721 error:
9722 isl_map_free(map1);
9723 isl_map_free(map2);
9724 return isl_bool_error;
9727 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9728 __isl_keep isl_set *set2)
9730 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9733 /* Return the basic maps in "map" as a list.
9735 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9736 __isl_keep isl_map *map)
9738 int i;
9739 isl_ctx *ctx;
9740 isl_basic_map_list *list;
9742 if (!map)
9743 return NULL;
9744 ctx = isl_map_get_ctx(map);
9745 list = isl_basic_map_list_alloc(ctx, map->n);
9747 for (i = 0; i < map->n; ++i) {
9748 isl_basic_map *bmap;
9750 bmap = isl_basic_map_copy(map->p[i]);
9751 list = isl_basic_map_list_add(list, bmap);
9754 return list;
9757 /* Return the intersection of the elements in the non-empty list "list".
9758 * All elements are assumed to live in the same space.
9760 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9761 __isl_take isl_basic_map_list *list)
9763 int i, n;
9764 isl_basic_map *bmap;
9766 if (!list)
9767 return NULL;
9768 n = isl_basic_map_list_n_basic_map(list);
9769 if (n < 1)
9770 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9771 "expecting non-empty list", goto error);
9773 bmap = isl_basic_map_list_get_basic_map(list, 0);
9774 for (i = 1; i < n; ++i) {
9775 isl_basic_map *bmap_i;
9777 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9778 bmap = isl_basic_map_intersect(bmap, bmap_i);
9781 isl_basic_map_list_free(list);
9782 return bmap;
9783 error:
9784 isl_basic_map_list_free(list);
9785 return NULL;
9788 /* Return the intersection of the elements in the non-empty list "list".
9789 * All elements are assumed to live in the same space.
9791 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9792 __isl_take isl_basic_set_list *list)
9794 return isl_basic_map_list_intersect(list);
9797 /* Return the union of the elements of "list".
9798 * The list is required to have at least one element.
9800 __isl_give isl_set *isl_basic_set_list_union(
9801 __isl_take isl_basic_set_list *list)
9803 int i, n;
9804 isl_space *space;
9805 isl_basic_set *bset;
9806 isl_set *set;
9808 if (!list)
9809 return NULL;
9810 n = isl_basic_set_list_n_basic_set(list);
9811 if (n < 1)
9812 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9813 "expecting non-empty list", goto error);
9815 bset = isl_basic_set_list_get_basic_set(list, 0);
9816 space = isl_basic_set_get_space(bset);
9817 isl_basic_set_free(bset);
9819 set = isl_set_alloc_space(space, n, 0);
9820 for (i = 0; i < n; ++i) {
9821 bset = isl_basic_set_list_get_basic_set(list, i);
9822 set = isl_set_add_basic_set(set, bset);
9825 isl_basic_set_list_free(list);
9826 return set;
9827 error:
9828 isl_basic_set_list_free(list);
9829 return NULL;
9832 /* Return the union of the elements in the non-empty list "list".
9833 * All elements are assumed to live in the same space.
9835 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9837 int i, n;
9838 isl_set *set;
9840 if (!list)
9841 return NULL;
9842 n = isl_set_list_n_set(list);
9843 if (n < 1)
9844 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9845 "expecting non-empty list", goto error);
9847 set = isl_set_list_get_set(list, 0);
9848 for (i = 1; i < n; ++i) {
9849 isl_set *set_i;
9851 set_i = isl_set_list_get_set(list, i);
9852 set = isl_set_union(set, set_i);
9855 isl_set_list_free(list);
9856 return set;
9857 error:
9858 isl_set_list_free(list);
9859 return NULL;
9862 __isl_give isl_basic_map *isl_basic_map_product(
9863 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9865 isl_space *dim_result = NULL;
9866 struct isl_basic_map *bmap;
9867 unsigned in1, in2, out1, out2, nparam, total, pos;
9868 struct isl_dim_map *dim_map1, *dim_map2;
9870 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9871 goto error;
9872 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9873 isl_space_copy(bmap2->dim));
9875 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9876 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9877 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9878 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9879 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9881 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9882 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9883 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9884 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9885 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9886 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9887 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9888 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9889 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9890 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9891 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9893 bmap = isl_basic_map_alloc_space(dim_result,
9894 bmap1->n_div + bmap2->n_div,
9895 bmap1->n_eq + bmap2->n_eq,
9896 bmap1->n_ineq + bmap2->n_ineq);
9897 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9898 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9899 bmap = isl_basic_map_simplify(bmap);
9900 return isl_basic_map_finalize(bmap);
9901 error:
9902 isl_basic_map_free(bmap1);
9903 isl_basic_map_free(bmap2);
9904 return NULL;
9907 __isl_give isl_basic_map *isl_basic_map_flat_product(
9908 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9910 isl_basic_map *prod;
9912 prod = isl_basic_map_product(bmap1, bmap2);
9913 prod = isl_basic_map_flatten(prod);
9914 return prod;
9917 __isl_give isl_basic_set *isl_basic_set_flat_product(
9918 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9920 return isl_basic_map_flat_range_product(bset1, bset2);
9923 __isl_give isl_basic_map *isl_basic_map_domain_product(
9924 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9926 isl_space *space_result = NULL;
9927 isl_basic_map *bmap;
9928 unsigned in1, in2, out, nparam, total, pos;
9929 struct isl_dim_map *dim_map1, *dim_map2;
9931 if (!bmap1 || !bmap2)
9932 goto error;
9934 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9935 isl_space_copy(bmap2->dim));
9937 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9938 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9939 out = isl_basic_map_dim(bmap1, isl_dim_out);
9940 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9942 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9943 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9944 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9945 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9946 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9947 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9948 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9949 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9950 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9951 isl_dim_map_div(dim_map1, bmap1, pos += out);
9952 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9954 bmap = isl_basic_map_alloc_space(space_result,
9955 bmap1->n_div + bmap2->n_div,
9956 bmap1->n_eq + bmap2->n_eq,
9957 bmap1->n_ineq + bmap2->n_ineq);
9958 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9959 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9960 bmap = isl_basic_map_simplify(bmap);
9961 return isl_basic_map_finalize(bmap);
9962 error:
9963 isl_basic_map_free(bmap1);
9964 isl_basic_map_free(bmap2);
9965 return NULL;
9968 __isl_give isl_basic_map *isl_basic_map_range_product(
9969 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9971 isl_bool rational;
9972 isl_space *dim_result = NULL;
9973 isl_basic_map *bmap;
9974 unsigned in, out1, out2, nparam, total, pos;
9975 struct isl_dim_map *dim_map1, *dim_map2;
9977 rational = isl_basic_map_is_rational(bmap1);
9978 if (rational >= 0 && rational)
9979 rational = isl_basic_map_is_rational(bmap2);
9980 if (!bmap1 || !bmap2 || rational < 0)
9981 goto error;
9983 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9984 goto error;
9986 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9987 isl_space_copy(bmap2->dim));
9989 in = isl_basic_map_dim(bmap1, isl_dim_in);
9990 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9991 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9992 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9994 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9995 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9996 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9997 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9998 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9999 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10000 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10001 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10002 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10003 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10004 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10006 bmap = isl_basic_map_alloc_space(dim_result,
10007 bmap1->n_div + bmap2->n_div,
10008 bmap1->n_eq + bmap2->n_eq,
10009 bmap1->n_ineq + bmap2->n_ineq);
10010 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10011 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10012 if (rational)
10013 bmap = isl_basic_map_set_rational(bmap);
10014 bmap = isl_basic_map_simplify(bmap);
10015 return isl_basic_map_finalize(bmap);
10016 error:
10017 isl_basic_map_free(bmap1);
10018 isl_basic_map_free(bmap2);
10019 return NULL;
10022 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10023 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10025 isl_basic_map *prod;
10027 prod = isl_basic_map_range_product(bmap1, bmap2);
10028 prod = isl_basic_map_flatten_range(prod);
10029 return prod;
10032 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10033 * and collect the results.
10034 * The result live in the space obtained by calling "space_product"
10035 * on the spaces of "map1" and "map2".
10036 * If "remove_duplicates" is set then the result may contain duplicates
10037 * (even if the inputs do not) and so we try and remove the obvious
10038 * duplicates.
10040 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10041 __isl_take isl_map *map2,
10042 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10043 __isl_take isl_space *right),
10044 __isl_give isl_basic_map *(*basic_map_product)(
10045 __isl_take isl_basic_map *left,
10046 __isl_take isl_basic_map *right),
10047 int remove_duplicates)
10049 unsigned flags = 0;
10050 struct isl_map *result;
10051 int i, j;
10052 isl_bool m;
10054 m = isl_map_has_equal_params(map1, map2);
10055 if (m < 0)
10056 goto error;
10057 if (!m)
10058 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10059 "parameters don't match", goto error);
10061 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10062 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10063 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10065 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10066 isl_space_copy(map2->dim)),
10067 map1->n * map2->n, flags);
10068 if (!result)
10069 goto error;
10070 for (i = 0; i < map1->n; ++i)
10071 for (j = 0; j < map2->n; ++j) {
10072 struct isl_basic_map *part;
10073 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10074 isl_basic_map_copy(map2->p[j]));
10075 if (isl_basic_map_is_empty(part))
10076 isl_basic_map_free(part);
10077 else
10078 result = isl_map_add_basic_map(result, part);
10079 if (!result)
10080 goto error;
10082 if (remove_duplicates)
10083 result = isl_map_remove_obvious_duplicates(result);
10084 isl_map_free(map1);
10085 isl_map_free(map2);
10086 return result;
10087 error:
10088 isl_map_free(map1);
10089 isl_map_free(map2);
10090 return NULL;
10093 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10095 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10096 __isl_take isl_map *map2)
10098 return map_product(map1, map2, &isl_space_product,
10099 &isl_basic_map_product, 0);
10102 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10103 __isl_take isl_map *map2)
10105 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10108 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10110 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10111 __isl_take isl_map *map2)
10113 isl_map *prod;
10115 prod = isl_map_product(map1, map2);
10116 prod = isl_map_flatten(prod);
10117 return prod;
10120 /* Given two set A and B, construct its Cartesian product A x B.
10122 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10124 return isl_map_range_product(set1, set2);
10127 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10128 __isl_take isl_set *set2)
10130 return isl_map_flat_range_product(set1, set2);
10133 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10135 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10136 __isl_take isl_map *map2)
10138 return map_product(map1, map2, &isl_space_domain_product,
10139 &isl_basic_map_domain_product, 1);
10142 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10144 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10145 __isl_take isl_map *map2)
10147 return map_product(map1, map2, &isl_space_range_product,
10148 &isl_basic_map_range_product, 1);
10151 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10152 __isl_take isl_map *map2)
10154 return isl_map_align_params_map_map_and(map1, map2,
10155 &map_domain_product_aligned);
10158 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10159 __isl_take isl_map *map2)
10161 return isl_map_align_params_map_map_and(map1, map2,
10162 &map_range_product_aligned);
10165 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10167 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10169 isl_space *space;
10170 int total1, keep1, total2, keep2;
10172 if (!map)
10173 return NULL;
10174 if (!isl_space_domain_is_wrapping(map->dim) ||
10175 !isl_space_range_is_wrapping(map->dim))
10176 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10177 "not a product", return isl_map_free(map));
10179 space = isl_map_get_space(map);
10180 total1 = isl_space_dim(space, isl_dim_in);
10181 total2 = isl_space_dim(space, isl_dim_out);
10182 space = isl_space_factor_domain(space);
10183 keep1 = isl_space_dim(space, isl_dim_in);
10184 keep2 = isl_space_dim(space, isl_dim_out);
10185 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10186 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10187 map = isl_map_reset_space(map, space);
10189 return map;
10192 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10194 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10196 isl_space *space;
10197 int total1, keep1, total2, keep2;
10199 if (!map)
10200 return NULL;
10201 if (!isl_space_domain_is_wrapping(map->dim) ||
10202 !isl_space_range_is_wrapping(map->dim))
10203 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10204 "not a product", return isl_map_free(map));
10206 space = isl_map_get_space(map);
10207 total1 = isl_space_dim(space, isl_dim_in);
10208 total2 = isl_space_dim(space, isl_dim_out);
10209 space = isl_space_factor_range(space);
10210 keep1 = isl_space_dim(space, isl_dim_in);
10211 keep2 = isl_space_dim(space, isl_dim_out);
10212 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10213 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10214 map = isl_map_reset_space(map, space);
10216 return map;
10219 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10221 __isl_give isl_map *isl_map_domain_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_domain_is_wrapping(map->dim))
10229 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10230 "domain is not a product", return isl_map_free(map));
10232 space = isl_map_get_space(map);
10233 total = isl_space_dim(space, isl_dim_in);
10234 space = isl_space_domain_factor_domain(space);
10235 keep = isl_space_dim(space, isl_dim_in);
10236 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10237 map = isl_map_reset_space(map, space);
10239 return map;
10242 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10244 __isl_give isl_map *isl_map_domain_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_domain_is_wrapping(map->dim))
10252 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10253 "domain is not a product", return isl_map_free(map));
10255 space = isl_map_get_space(map);
10256 total = isl_space_dim(space, isl_dim_in);
10257 space = isl_space_domain_factor_range(space);
10258 keep = isl_space_dim(space, isl_dim_in);
10259 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10260 map = isl_map_reset_space(map, space);
10262 return map;
10265 /* Given a map A -> [B -> C], extract the map A -> B.
10267 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10269 isl_space *space;
10270 int total, keep;
10272 if (!map)
10273 return NULL;
10274 if (!isl_space_range_is_wrapping(map->dim))
10275 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10276 "range is not a product", return isl_map_free(map));
10278 space = isl_map_get_space(map);
10279 total = isl_space_dim(space, isl_dim_out);
10280 space = isl_space_range_factor_domain(space);
10281 keep = isl_space_dim(space, isl_dim_out);
10282 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10283 map = isl_map_reset_space(map, space);
10285 return map;
10288 /* Given a map A -> [B -> C], extract the map A -> C.
10290 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10292 isl_space *space;
10293 int total, keep;
10295 if (!map)
10296 return NULL;
10297 if (!isl_space_range_is_wrapping(map->dim))
10298 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10299 "range is not a product", return isl_map_free(map));
10301 space = isl_map_get_space(map);
10302 total = isl_space_dim(space, isl_dim_out);
10303 space = isl_space_range_factor_range(space);
10304 keep = isl_space_dim(space, isl_dim_out);
10305 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10306 map = isl_map_reset_space(map, space);
10308 return map;
10311 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10313 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10314 __isl_take isl_map *map2)
10316 isl_map *prod;
10318 prod = isl_map_domain_product(map1, map2);
10319 prod = isl_map_flatten_domain(prod);
10320 return prod;
10323 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10325 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10326 __isl_take isl_map *map2)
10328 isl_map *prod;
10330 prod = isl_map_range_product(map1, map2);
10331 prod = isl_map_flatten_range(prod);
10332 return prod;
10335 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10337 int i;
10338 uint32_t hash = isl_hash_init();
10339 unsigned total;
10341 if (!bmap)
10342 return 0;
10343 bmap = isl_basic_map_copy(bmap);
10344 bmap = isl_basic_map_normalize(bmap);
10345 if (!bmap)
10346 return 0;
10347 total = isl_basic_map_total_dim(bmap);
10348 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10349 for (i = 0; i < bmap->n_eq; ++i) {
10350 uint32_t c_hash;
10351 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10352 isl_hash_hash(hash, c_hash);
10354 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10355 for (i = 0; i < bmap->n_ineq; ++i) {
10356 uint32_t c_hash;
10357 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10358 isl_hash_hash(hash, c_hash);
10360 isl_hash_byte(hash, bmap->n_div & 0xFF);
10361 for (i = 0; i < bmap->n_div; ++i) {
10362 uint32_t c_hash;
10363 if (isl_int_is_zero(bmap->div[i][0]))
10364 continue;
10365 isl_hash_byte(hash, i & 0xFF);
10366 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10367 isl_hash_hash(hash, c_hash);
10369 isl_basic_map_free(bmap);
10370 return hash;
10373 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10375 return isl_basic_map_get_hash(bset_to_bmap(bset));
10378 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10380 int i;
10381 uint32_t hash;
10383 if (!map)
10384 return 0;
10385 map = isl_map_copy(map);
10386 map = isl_map_normalize(map);
10387 if (!map)
10388 return 0;
10390 hash = isl_hash_init();
10391 for (i = 0; i < map->n; ++i) {
10392 uint32_t bmap_hash;
10393 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10394 isl_hash_hash(hash, bmap_hash);
10397 isl_map_free(map);
10399 return hash;
10402 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10404 return isl_map_get_hash(set_to_map(set));
10407 /* Return the number of basic maps in the (current) representation of "map".
10409 int isl_map_n_basic_map(__isl_keep isl_map *map)
10411 return map ? map->n : 0;
10414 int isl_set_n_basic_set(__isl_keep isl_set *set)
10416 return set ? set->n : 0;
10419 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10420 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10422 int i;
10424 if (!map)
10425 return isl_stat_error;
10427 for (i = 0; i < map->n; ++i)
10428 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10429 return isl_stat_error;
10431 return isl_stat_ok;
10434 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10435 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10437 int i;
10439 if (!set)
10440 return isl_stat_error;
10442 for (i = 0; i < set->n; ++i)
10443 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10444 return isl_stat_error;
10446 return isl_stat_ok;
10449 /* Return a list of basic sets, the union of which is equal to "set".
10451 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10452 __isl_keep isl_set *set)
10454 int i;
10455 isl_basic_set_list *list;
10457 if (!set)
10458 return NULL;
10460 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10461 for (i = 0; i < set->n; ++i) {
10462 isl_basic_set *bset;
10464 bset = isl_basic_set_copy(set->p[i]);
10465 list = isl_basic_set_list_add(list, bset);
10468 return list;
10471 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10473 isl_space *dim;
10475 if (!bset)
10476 return NULL;
10478 bset = isl_basic_set_cow(bset);
10479 if (!bset)
10480 return NULL;
10482 dim = isl_basic_set_get_space(bset);
10483 dim = isl_space_lift(dim, bset->n_div);
10484 if (!dim)
10485 goto error;
10486 isl_space_free(bset->dim);
10487 bset->dim = dim;
10488 bset->extra -= bset->n_div;
10489 bset->n_div = 0;
10491 bset = isl_basic_set_finalize(bset);
10493 return bset;
10494 error:
10495 isl_basic_set_free(bset);
10496 return NULL;
10499 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10501 int i;
10502 isl_space *dim;
10503 unsigned n_div;
10505 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10507 if (!set)
10508 return NULL;
10510 set = isl_set_cow(set);
10511 if (!set)
10512 return NULL;
10514 n_div = set->p[0]->n_div;
10515 dim = isl_set_get_space(set);
10516 dim = isl_space_lift(dim, n_div);
10517 if (!dim)
10518 goto error;
10519 isl_space_free(set->dim);
10520 set->dim = dim;
10522 for (i = 0; i < set->n; ++i) {
10523 set->p[i] = isl_basic_set_lift(set->p[i]);
10524 if (!set->p[i])
10525 goto error;
10528 return set;
10529 error:
10530 isl_set_free(set);
10531 return NULL;
10534 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10536 unsigned dim;
10537 int size = 0;
10539 if (!bset)
10540 return -1;
10542 dim = isl_basic_set_total_dim(bset);
10543 size += bset->n_eq * (1 + dim);
10544 size += bset->n_ineq * (1 + dim);
10545 size += bset->n_div * (2 + dim);
10547 return size;
10550 int isl_set_size(__isl_keep isl_set *set)
10552 int i;
10553 int size = 0;
10555 if (!set)
10556 return -1;
10558 for (i = 0; i < set->n; ++i)
10559 size += isl_basic_set_size(set->p[i]);
10561 return size;
10564 /* Check if there is any lower bound (if lower == 0) and/or upper
10565 * bound (if upper == 0) on the specified dim.
10567 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10568 enum isl_dim_type type, unsigned pos, int lower, int upper)
10570 int i;
10572 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10573 return isl_bool_error;
10575 pos += isl_basic_map_offset(bmap, type);
10577 for (i = 0; i < bmap->n_div; ++i) {
10578 if (isl_int_is_zero(bmap->div[i][0]))
10579 continue;
10580 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10581 return isl_bool_true;
10584 for (i = 0; i < bmap->n_eq; ++i)
10585 if (!isl_int_is_zero(bmap->eq[i][pos]))
10586 return isl_bool_true;
10588 for (i = 0; i < bmap->n_ineq; ++i) {
10589 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10590 if (sgn > 0)
10591 lower = 1;
10592 if (sgn < 0)
10593 upper = 1;
10596 return lower && upper;
10599 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10600 enum isl_dim_type type, unsigned pos)
10602 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10605 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10606 enum isl_dim_type type, unsigned pos)
10608 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10611 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10612 enum isl_dim_type type, unsigned pos)
10614 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10617 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10618 enum isl_dim_type type, unsigned pos)
10620 int i;
10622 if (!map)
10623 return isl_bool_error;
10625 for (i = 0; i < map->n; ++i) {
10626 isl_bool bounded;
10627 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10628 if (bounded < 0 || !bounded)
10629 return bounded;
10632 return isl_bool_true;
10635 /* Return true if the specified dim is involved in both an upper bound
10636 * and a lower bound.
10638 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10639 enum isl_dim_type type, unsigned pos)
10641 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10644 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10646 static isl_bool has_any_bound(__isl_keep isl_map *map,
10647 enum isl_dim_type type, unsigned pos,
10648 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10649 enum isl_dim_type type, unsigned pos))
10651 int i;
10653 if (!map)
10654 return isl_bool_error;
10656 for (i = 0; i < map->n; ++i) {
10657 isl_bool bounded;
10658 bounded = fn(map->p[i], type, pos);
10659 if (bounded < 0 || bounded)
10660 return bounded;
10663 return isl_bool_false;
10666 /* Return 1 if the specified dim is involved in any lower bound.
10668 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10669 enum isl_dim_type type, unsigned pos)
10671 return has_any_bound(set, type, pos,
10672 &isl_basic_map_dim_has_lower_bound);
10675 /* Return 1 if the specified dim is involved in any upper bound.
10677 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10678 enum isl_dim_type type, unsigned pos)
10680 return has_any_bound(set, type, pos,
10681 &isl_basic_map_dim_has_upper_bound);
10684 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10686 static isl_bool has_bound(__isl_keep isl_map *map,
10687 enum isl_dim_type type, unsigned pos,
10688 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10689 enum isl_dim_type type, unsigned pos))
10691 int i;
10693 if (!map)
10694 return isl_bool_error;
10696 for (i = 0; i < map->n; ++i) {
10697 isl_bool bounded;
10698 bounded = fn(map->p[i], type, pos);
10699 if (bounded < 0 || !bounded)
10700 return bounded;
10703 return isl_bool_true;
10706 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10708 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10709 enum isl_dim_type type, unsigned pos)
10711 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10714 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10716 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10717 enum isl_dim_type type, unsigned pos)
10719 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10722 /* For each of the "n" variables starting at "first", determine
10723 * the sign of the variable and put the results in the first "n"
10724 * elements of the array "signs".
10725 * Sign
10726 * 1 means that the variable is non-negative
10727 * -1 means that the variable is non-positive
10728 * 0 means the variable attains both positive and negative values.
10730 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10731 unsigned first, unsigned n, int *signs)
10733 isl_vec *bound = NULL;
10734 struct isl_tab *tab = NULL;
10735 struct isl_tab_undo *snap;
10736 int i;
10738 if (!bset || !signs)
10739 return isl_stat_error;
10741 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10742 tab = isl_tab_from_basic_set(bset, 0);
10743 if (!bound || !tab)
10744 goto error;
10746 isl_seq_clr(bound->el, bound->size);
10747 isl_int_set_si(bound->el[0], -1);
10749 snap = isl_tab_snap(tab);
10750 for (i = 0; i < n; ++i) {
10751 int empty;
10753 isl_int_set_si(bound->el[1 + first + i], -1);
10754 if (isl_tab_add_ineq(tab, bound->el) < 0)
10755 goto error;
10756 empty = tab->empty;
10757 isl_int_set_si(bound->el[1 + first + i], 0);
10758 if (isl_tab_rollback(tab, snap) < 0)
10759 goto error;
10761 if (empty) {
10762 signs[i] = 1;
10763 continue;
10766 isl_int_set_si(bound->el[1 + first + i], 1);
10767 if (isl_tab_add_ineq(tab, bound->el) < 0)
10768 goto error;
10769 empty = tab->empty;
10770 isl_int_set_si(bound->el[1 + first + i], 0);
10771 if (isl_tab_rollback(tab, snap) < 0)
10772 goto error;
10774 signs[i] = empty ? -1 : 0;
10777 isl_tab_free(tab);
10778 isl_vec_free(bound);
10779 return isl_stat_ok;
10780 error:
10781 isl_tab_free(tab);
10782 isl_vec_free(bound);
10783 return isl_stat_error;
10786 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10787 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10789 if (!bset || !signs)
10790 return isl_stat_error;
10791 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10792 return isl_stat_error);
10794 first += pos(bset->dim, type) - 1;
10795 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10798 /* Is it possible for the integer division "div" to depend (possibly
10799 * indirectly) on any output dimensions?
10801 * If the div is undefined, then we conservatively assume that it
10802 * may depend on them.
10803 * Otherwise, we check if it actually depends on them or on any integer
10804 * divisions that may depend on them.
10806 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10808 int i;
10809 unsigned n_out, o_out;
10810 unsigned n_div, o_div;
10812 if (isl_int_is_zero(bmap->div[div][0]))
10813 return isl_bool_true;
10815 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10816 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10818 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10819 return isl_bool_true;
10821 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10822 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10824 for (i = 0; i < n_div; ++i) {
10825 isl_bool may_involve;
10827 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10828 continue;
10829 may_involve = div_may_involve_output(bmap, i);
10830 if (may_involve < 0 || may_involve)
10831 return may_involve;
10834 return isl_bool_false;
10837 /* Return the first integer division of "bmap" in the range
10838 * [first, first + n[ that may depend on any output dimensions and
10839 * that has a non-zero coefficient in "c" (where the first coefficient
10840 * in "c" corresponds to integer division "first").
10842 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10843 isl_int *c, int first, int n)
10845 int k;
10847 if (!bmap)
10848 return -1;
10850 for (k = first; k < first + n; ++k) {
10851 isl_bool may_involve;
10853 if (isl_int_is_zero(c[k]))
10854 continue;
10855 may_involve = div_may_involve_output(bmap, k);
10856 if (may_involve < 0)
10857 return -1;
10858 if (may_involve)
10859 return k;
10862 return first + n;
10865 /* Look for a pair of inequality constraints in "bmap" of the form
10867 * -l + i >= 0 or i >= l
10868 * and
10869 * n + l - i >= 0 or i <= l + n
10871 * with n < "m" and i the output dimension at position "pos".
10872 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10873 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10874 * and earlier output dimensions, as well as integer divisions that do
10875 * not involve any of the output dimensions.
10877 * Return the index of the first inequality constraint or bmap->n_ineq
10878 * if no such pair can be found.
10880 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10881 int pos, isl_int m)
10883 int i, j;
10884 isl_ctx *ctx;
10885 unsigned total;
10886 unsigned n_div, o_div;
10887 unsigned n_out, o_out;
10888 int less;
10890 if (!bmap)
10891 return -1;
10893 ctx = isl_basic_map_get_ctx(bmap);
10894 total = isl_basic_map_total_dim(bmap);
10895 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10896 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10897 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10898 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10899 for (i = 0; i < bmap->n_ineq; ++i) {
10900 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10901 continue;
10902 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10903 n_out - (pos + 1)) != -1)
10904 continue;
10905 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10906 0, n_div) < n_div)
10907 continue;
10908 for (j = i + 1; j < bmap->n_ineq; ++j) {
10909 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10910 ctx->one))
10911 continue;
10912 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10913 bmap->ineq[j] + 1, total))
10914 continue;
10915 break;
10917 if (j >= bmap->n_ineq)
10918 continue;
10919 isl_int_add(bmap->ineq[i][0],
10920 bmap->ineq[i][0], bmap->ineq[j][0]);
10921 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10922 isl_int_sub(bmap->ineq[i][0],
10923 bmap->ineq[i][0], bmap->ineq[j][0]);
10924 if (!less)
10925 continue;
10926 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10927 return i;
10928 else
10929 return j;
10932 return bmap->n_ineq;
10935 /* Return the index of the equality of "bmap" that defines
10936 * the output dimension "pos" in terms of earlier dimensions.
10937 * The equality may also involve integer divisions, as long
10938 * as those integer divisions are defined in terms of
10939 * parameters or input dimensions.
10940 * In this case, *div is set to the number of integer divisions and
10941 * *ineq is set to the number of inequality constraints (provided
10942 * div and ineq are not NULL).
10944 * The equality may also involve a single integer division involving
10945 * the output dimensions (typically only output dimension "pos") as
10946 * long as the coefficient of output dimension "pos" is 1 or -1 and
10947 * there is a pair of constraints i >= l and i <= l + n, with i referring
10948 * to output dimension "pos", l an expression involving only earlier
10949 * dimensions and n smaller than the coefficient of the integer division
10950 * in the equality. In this case, the output dimension can be defined
10951 * in terms of a modulo expression that does not involve the integer division.
10952 * *div is then set to this single integer division and
10953 * *ineq is set to the index of constraint i >= l.
10955 * Return bmap->n_eq if there is no such equality.
10956 * Return -1 on error.
10958 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10959 int pos, int *div, int *ineq)
10961 int j, k, l;
10962 unsigned n_out, o_out;
10963 unsigned n_div, o_div;
10965 if (!bmap)
10966 return -1;
10968 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10969 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10970 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10971 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10973 if (ineq)
10974 *ineq = bmap->n_ineq;
10975 if (div)
10976 *div = n_div;
10977 for (j = 0; j < bmap->n_eq; ++j) {
10978 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10979 continue;
10980 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10981 n_out - (pos + 1)) != -1)
10982 continue;
10983 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10984 0, n_div);
10985 if (k >= n_div)
10986 return j;
10987 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10988 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10989 continue;
10990 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10991 k + 1, n_div - (k+1)) < n_div)
10992 continue;
10993 l = find_modulo_constraint_pair(bmap, pos,
10994 bmap->eq[j][o_div + k]);
10995 if (l < 0)
10996 return -1;
10997 if (l >= bmap->n_ineq)
10998 continue;
10999 if (div)
11000 *div = k;
11001 if (ineq)
11002 *ineq = l;
11003 return j;
11006 return bmap->n_eq;
11009 /* Check if the given basic map is obviously single-valued.
11010 * In particular, for each output dimension, check that there is
11011 * an equality that defines the output dimension in terms of
11012 * earlier dimensions.
11014 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11016 int i;
11017 unsigned n_out;
11019 if (!bmap)
11020 return isl_bool_error;
11022 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11024 for (i = 0; i < n_out; ++i) {
11025 int eq;
11027 eq = isl_basic_map_output_defining_equality(bmap, i,
11028 NULL, NULL);
11029 if (eq < 0)
11030 return isl_bool_error;
11031 if (eq >= bmap->n_eq)
11032 return isl_bool_false;
11035 return isl_bool_true;
11038 /* Check if the given basic map is single-valued.
11039 * We simply compute
11041 * M \circ M^-1
11043 * and check if the result is a subset of the identity mapping.
11045 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11047 isl_space *space;
11048 isl_basic_map *test;
11049 isl_basic_map *id;
11050 isl_bool sv;
11052 sv = isl_basic_map_plain_is_single_valued(bmap);
11053 if (sv < 0 || sv)
11054 return sv;
11056 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11057 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11059 space = isl_basic_map_get_space(bmap);
11060 space = isl_space_map_from_set(isl_space_range(space));
11061 id = isl_basic_map_identity(space);
11063 sv = isl_basic_map_is_subset(test, id);
11065 isl_basic_map_free(test);
11066 isl_basic_map_free(id);
11068 return sv;
11071 /* Check if the given map is obviously single-valued.
11073 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11075 if (!map)
11076 return isl_bool_error;
11077 if (map->n == 0)
11078 return isl_bool_true;
11079 if (map->n >= 2)
11080 return isl_bool_false;
11082 return isl_basic_map_plain_is_single_valued(map->p[0]);
11085 /* Check if the given map is single-valued.
11086 * We simply compute
11088 * M \circ M^-1
11090 * and check if the result is a subset of the identity mapping.
11092 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11094 isl_space *dim;
11095 isl_map *test;
11096 isl_map *id;
11097 isl_bool sv;
11099 sv = isl_map_plain_is_single_valued(map);
11100 if (sv < 0 || sv)
11101 return sv;
11103 test = isl_map_reverse(isl_map_copy(map));
11104 test = isl_map_apply_range(test, isl_map_copy(map));
11106 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11107 id = isl_map_identity(dim);
11109 sv = isl_map_is_subset(test, id);
11111 isl_map_free(test);
11112 isl_map_free(id);
11114 return sv;
11117 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11119 isl_bool in;
11121 map = isl_map_copy(map);
11122 map = isl_map_reverse(map);
11123 in = isl_map_is_single_valued(map);
11124 isl_map_free(map);
11126 return in;
11129 /* Check if the given map is obviously injective.
11131 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11133 isl_bool in;
11135 map = isl_map_copy(map);
11136 map = isl_map_reverse(map);
11137 in = isl_map_plain_is_single_valued(map);
11138 isl_map_free(map);
11140 return in;
11143 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11145 isl_bool sv;
11147 sv = isl_map_is_single_valued(map);
11148 if (sv < 0 || !sv)
11149 return sv;
11151 return isl_map_is_injective(map);
11154 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11156 return isl_map_is_single_valued(set_to_map(set));
11159 /* Does "map" only map elements to themselves?
11161 * If the domain and range spaces are different, then "map"
11162 * is considered not to be an identity relation, even if it is empty.
11163 * Otherwise, construct the maximal identity relation and
11164 * check whether "map" is a subset of this relation.
11166 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11168 isl_space *space;
11169 isl_map *id;
11170 isl_bool equal, is_identity;
11172 space = isl_map_get_space(map);
11173 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11174 isl_space_free(space);
11175 if (equal < 0 || !equal)
11176 return equal;
11178 id = isl_map_identity(isl_map_get_space(map));
11179 is_identity = isl_map_is_subset(map, id);
11180 isl_map_free(id);
11182 return is_identity;
11185 int isl_map_is_translation(__isl_keep isl_map *map)
11187 int ok;
11188 isl_set *delta;
11190 delta = isl_map_deltas(isl_map_copy(map));
11191 ok = isl_set_is_singleton(delta);
11192 isl_set_free(delta);
11194 return ok;
11197 static int unique(isl_int *p, unsigned pos, unsigned len)
11199 if (isl_seq_first_non_zero(p, pos) != -1)
11200 return 0;
11201 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11202 return 0;
11203 return 1;
11206 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11208 int i, j;
11209 unsigned nvar;
11210 unsigned ovar;
11212 if (!bset)
11213 return isl_bool_error;
11215 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11216 return isl_bool_false;
11218 nvar = isl_basic_set_dim(bset, isl_dim_set);
11219 ovar = isl_space_offset(bset->dim, isl_dim_set);
11220 for (j = 0; j < nvar; ++j) {
11221 int lower = 0, upper = 0;
11222 for (i = 0; i < bset->n_eq; ++i) {
11223 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11224 continue;
11225 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11226 return isl_bool_false;
11227 break;
11229 if (i < bset->n_eq)
11230 continue;
11231 for (i = 0; i < bset->n_ineq; ++i) {
11232 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11233 continue;
11234 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11235 return isl_bool_false;
11236 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11237 lower = 1;
11238 else
11239 upper = 1;
11241 if (!lower || !upper)
11242 return isl_bool_false;
11245 return isl_bool_true;
11248 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11250 if (!set)
11251 return isl_bool_error;
11252 if (set->n != 1)
11253 return isl_bool_false;
11255 return isl_basic_set_is_box(set->p[0]);
11258 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11260 if (!bset)
11261 return isl_bool_error;
11263 return isl_space_is_wrapping(bset->dim);
11266 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11268 if (!set)
11269 return isl_bool_error;
11271 return isl_space_is_wrapping(set->dim);
11274 /* Modify the space of "map" through a call to "change".
11275 * If "can_change" is set (not NULL), then first call it to check
11276 * if the modification is allowed, printing the error message "cannot_change"
11277 * if it is not.
11279 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11280 isl_bool (*can_change)(__isl_keep isl_map *map),
11281 const char *cannot_change,
11282 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11284 isl_bool ok;
11285 isl_space *space;
11287 if (!map)
11288 return NULL;
11290 ok = can_change ? can_change(map) : isl_bool_true;
11291 if (ok < 0)
11292 return isl_map_free(map);
11293 if (!ok)
11294 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11295 return isl_map_free(map));
11297 space = change(isl_map_get_space(map));
11298 map = isl_map_reset_space(map, space);
11300 return map;
11303 /* Is the domain of "map" a wrapped relation?
11305 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11307 if (!map)
11308 return isl_bool_error;
11310 return isl_space_domain_is_wrapping(map->dim);
11313 /* Does "map" have a wrapped relation in both domain and range?
11315 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11317 return isl_space_is_product(isl_map_peek_space(map));
11320 /* Is the range of "map" a wrapped relation?
11322 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11324 if (!map)
11325 return isl_bool_error;
11327 return isl_space_range_is_wrapping(map->dim);
11330 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11332 bmap = isl_basic_map_cow(bmap);
11333 if (!bmap)
11334 return NULL;
11336 bmap->dim = isl_space_wrap(bmap->dim);
11337 if (!bmap->dim)
11338 goto error;
11340 bmap = isl_basic_map_finalize(bmap);
11342 return bset_from_bmap(bmap);
11343 error:
11344 isl_basic_map_free(bmap);
11345 return NULL;
11348 /* Given a map A -> B, return the set (A -> B).
11350 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11352 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11355 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11357 bset = isl_basic_set_cow(bset);
11358 if (!bset)
11359 return NULL;
11361 bset->dim = isl_space_unwrap(bset->dim);
11362 if (!bset->dim)
11363 goto error;
11365 bset = isl_basic_set_finalize(bset);
11367 return bset_to_bmap(bset);
11368 error:
11369 isl_basic_set_free(bset);
11370 return NULL;
11373 /* Given a set (A -> B), return the map A -> B.
11374 * Error out if "set" is not of the form (A -> B).
11376 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11378 return isl_map_change_space(set, &isl_set_is_wrapping,
11379 "not a wrapping set", &isl_space_unwrap);
11382 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11383 enum isl_dim_type type)
11385 if (!bmap)
11386 return NULL;
11388 if (!isl_space_is_named_or_nested(bmap->dim, type))
11389 return bmap;
11391 bmap = isl_basic_map_cow(bmap);
11392 if (!bmap)
11393 return NULL;
11395 bmap->dim = isl_space_reset(bmap->dim, type);
11396 if (!bmap->dim)
11397 goto error;
11399 bmap = isl_basic_map_finalize(bmap);
11401 return bmap;
11402 error:
11403 isl_basic_map_free(bmap);
11404 return NULL;
11407 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11408 enum isl_dim_type type)
11410 int i;
11412 if (!map)
11413 return NULL;
11415 if (!isl_space_is_named_or_nested(map->dim, type))
11416 return map;
11418 map = isl_map_cow(map);
11419 if (!map)
11420 return NULL;
11422 for (i = 0; i < map->n; ++i) {
11423 map->p[i] = isl_basic_map_reset(map->p[i], type);
11424 if (!map->p[i])
11425 goto error;
11427 map->dim = isl_space_reset(map->dim, type);
11428 if (!map->dim)
11429 goto error;
11431 return map;
11432 error:
11433 isl_map_free(map);
11434 return NULL;
11437 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11439 if (!bmap)
11440 return NULL;
11442 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11443 return bmap;
11445 bmap = isl_basic_map_cow(bmap);
11446 if (!bmap)
11447 return NULL;
11449 bmap->dim = isl_space_flatten(bmap->dim);
11450 if (!bmap->dim)
11451 goto error;
11453 bmap = isl_basic_map_finalize(bmap);
11455 return bmap;
11456 error:
11457 isl_basic_map_free(bmap);
11458 return NULL;
11461 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11463 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11466 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11467 __isl_take isl_basic_map *bmap)
11469 if (!bmap)
11470 return NULL;
11472 if (!bmap->dim->nested[0])
11473 return bmap;
11475 bmap = isl_basic_map_cow(bmap);
11476 if (!bmap)
11477 return NULL;
11479 bmap->dim = isl_space_flatten_domain(bmap->dim);
11480 if (!bmap->dim)
11481 goto error;
11483 bmap = isl_basic_map_finalize(bmap);
11485 return bmap;
11486 error:
11487 isl_basic_map_free(bmap);
11488 return NULL;
11491 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11492 __isl_take isl_basic_map *bmap)
11494 if (!bmap)
11495 return NULL;
11497 if (!bmap->dim->nested[1])
11498 return bmap;
11500 bmap = isl_basic_map_cow(bmap);
11501 if (!bmap)
11502 return NULL;
11504 bmap->dim = isl_space_flatten_range(bmap->dim);
11505 if (!bmap->dim)
11506 goto error;
11508 bmap = isl_basic_map_finalize(bmap);
11510 return bmap;
11511 error:
11512 isl_basic_map_free(bmap);
11513 return NULL;
11516 /* Remove any internal structure from the spaces of domain and range of "map".
11518 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11520 if (!map)
11521 return NULL;
11523 if (!map->dim->nested[0] && !map->dim->nested[1])
11524 return map;
11526 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11529 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11531 return set_from_map(isl_map_flatten(set_to_map(set)));
11534 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11536 isl_space *dim, *flat_dim;
11537 isl_map *map;
11539 dim = isl_set_get_space(set);
11540 flat_dim = isl_space_flatten(isl_space_copy(dim));
11541 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11542 map = isl_map_intersect_domain(map, set);
11544 return map;
11547 /* Remove any internal structure from the space of the domain of "map".
11549 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11551 if (!map)
11552 return NULL;
11554 if (!map->dim->nested[0])
11555 return map;
11557 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11560 /* Remove any internal structure from the space of the range of "map".
11562 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11564 if (!map)
11565 return NULL;
11567 if (!map->dim->nested[1])
11568 return map;
11570 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11573 /* Reorder the dimensions of "bmap" according to the given dim_map
11574 * and set the dimension specification to "dim" and
11575 * perform Gaussian elimination on the result.
11577 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11578 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11580 isl_basic_map *res;
11581 unsigned flags;
11583 bmap = isl_basic_map_cow(bmap);
11584 if (!bmap || !dim || !dim_map)
11585 goto error;
11587 flags = bmap->flags;
11588 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11589 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11590 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11591 res = isl_basic_map_alloc_space(dim,
11592 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11593 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11594 if (res)
11595 res->flags = flags;
11596 res = isl_basic_map_gauss(res, NULL);
11597 res = isl_basic_map_finalize(res);
11598 return res;
11599 error:
11600 free(dim_map);
11601 isl_basic_map_free(bmap);
11602 isl_space_free(dim);
11603 return NULL;
11606 /* Reorder the dimensions of "map" according to given reordering.
11608 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11609 __isl_take isl_reordering *r)
11611 int i;
11612 struct isl_dim_map *dim_map;
11614 map = isl_map_cow(map);
11615 dim_map = isl_dim_map_from_reordering(r);
11616 if (!map || !r || !dim_map)
11617 goto error;
11619 for (i = 0; i < map->n; ++i) {
11620 struct isl_dim_map *dim_map_i;
11622 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11624 map->p[i] = isl_basic_map_realign(map->p[i],
11625 isl_space_copy(r->dim), dim_map_i);
11627 if (!map->p[i])
11628 goto error;
11631 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11632 map = isl_map_unmark_normalized(map);
11634 isl_reordering_free(r);
11635 free(dim_map);
11636 return map;
11637 error:
11638 free(dim_map);
11639 isl_map_free(map);
11640 isl_reordering_free(r);
11641 return NULL;
11644 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11645 __isl_take isl_reordering *r)
11647 return set_from_map(isl_map_realign(set_to_map(set), r));
11650 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11651 __isl_take isl_space *model)
11653 isl_ctx *ctx;
11654 isl_bool aligned;
11656 if (!map || !model)
11657 goto error;
11659 ctx = isl_space_get_ctx(model);
11660 if (!isl_space_has_named_params(model))
11661 isl_die(ctx, isl_error_invalid,
11662 "model has unnamed parameters", goto error);
11663 if (isl_map_check_named_params(map) < 0)
11664 goto error;
11665 aligned = isl_map_space_has_equal_params(map, model);
11666 if (aligned < 0)
11667 goto error;
11668 if (!aligned) {
11669 isl_reordering *exp;
11671 model = isl_space_drop_dims(model, isl_dim_in,
11672 0, isl_space_dim(model, isl_dim_in));
11673 model = isl_space_drop_dims(model, isl_dim_out,
11674 0, isl_space_dim(model, isl_dim_out));
11675 exp = isl_parameter_alignment_reordering(map->dim, model);
11676 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11677 map = isl_map_realign(map, exp);
11680 isl_space_free(model);
11681 return map;
11682 error:
11683 isl_space_free(model);
11684 isl_map_free(map);
11685 return NULL;
11688 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11689 __isl_take isl_space *model)
11691 return isl_map_align_params(set, model);
11694 /* Align the parameters of "bmap" to those of "model", introducing
11695 * additional parameters if needed.
11697 __isl_give isl_basic_map *isl_basic_map_align_params(
11698 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11700 isl_ctx *ctx;
11701 isl_bool equal_params;
11703 if (!bmap || !model)
11704 goto error;
11706 ctx = isl_space_get_ctx(model);
11707 if (!isl_space_has_named_params(model))
11708 isl_die(ctx, isl_error_invalid,
11709 "model has unnamed parameters", goto error);
11710 if (!isl_space_has_named_params(bmap->dim))
11711 isl_die(ctx, isl_error_invalid,
11712 "relation has unnamed parameters", goto error);
11713 equal_params = isl_space_has_equal_params(bmap->dim, model);
11714 if (equal_params < 0)
11715 goto error;
11716 if (!equal_params) {
11717 isl_reordering *exp;
11718 struct isl_dim_map *dim_map;
11720 model = isl_space_drop_dims(model, isl_dim_in,
11721 0, isl_space_dim(model, isl_dim_in));
11722 model = isl_space_drop_dims(model, isl_dim_out,
11723 0, isl_space_dim(model, isl_dim_out));
11724 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11725 exp = isl_reordering_extend_space(exp,
11726 isl_basic_map_get_space(bmap));
11727 dim_map = isl_dim_map_from_reordering(exp);
11728 bmap = isl_basic_map_realign(bmap,
11729 exp ? isl_space_copy(exp->dim) : NULL,
11730 isl_dim_map_extend(dim_map, bmap));
11731 isl_reordering_free(exp);
11732 free(dim_map);
11735 isl_space_free(model);
11736 return bmap;
11737 error:
11738 isl_space_free(model);
11739 isl_basic_map_free(bmap);
11740 return NULL;
11743 /* Do "bset" and "space" have the same parameters?
11745 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11746 __isl_keep isl_space *space)
11748 isl_space *bset_space;
11750 bset_space = isl_basic_set_peek_space(bset);
11751 return isl_space_has_equal_params(bset_space, space);
11754 /* Do "map" and "space" have the same parameters?
11756 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11757 __isl_keep isl_space *space)
11759 isl_space *map_space;
11761 map_space = isl_map_peek_space(map);
11762 return isl_space_has_equal_params(map_space, space);
11765 /* Do "set" and "space" have the same parameters?
11767 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11768 __isl_keep isl_space *space)
11770 return isl_map_space_has_equal_params(set_to_map(set), space);
11773 /* Align the parameters of "bset" to those of "model", introducing
11774 * additional parameters if needed.
11776 __isl_give isl_basic_set *isl_basic_set_align_params(
11777 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11779 return isl_basic_map_align_params(bset, model);
11782 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11783 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11784 enum isl_dim_type c2, enum isl_dim_type c3,
11785 enum isl_dim_type c4, enum isl_dim_type c5)
11787 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11788 struct isl_mat *mat;
11789 int i, j, k;
11790 int pos;
11792 if (!bmap)
11793 return NULL;
11794 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11795 isl_basic_map_total_dim(bmap) + 1);
11796 if (!mat)
11797 return NULL;
11798 for (i = 0; i < bmap->n_eq; ++i)
11799 for (j = 0, pos = 0; j < 5; ++j) {
11800 int off = isl_basic_map_offset(bmap, c[j]);
11801 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11802 isl_int_set(mat->row[i][pos],
11803 bmap->eq[i][off + k]);
11804 ++pos;
11808 return mat;
11811 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11812 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11813 enum isl_dim_type c2, enum isl_dim_type c3,
11814 enum isl_dim_type c4, enum isl_dim_type c5)
11816 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11817 struct isl_mat *mat;
11818 int i, j, k;
11819 int pos;
11821 if (!bmap)
11822 return NULL;
11823 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11824 isl_basic_map_total_dim(bmap) + 1);
11825 if (!mat)
11826 return NULL;
11827 for (i = 0; i < bmap->n_ineq; ++i)
11828 for (j = 0, pos = 0; j < 5; ++j) {
11829 int off = isl_basic_map_offset(bmap, c[j]);
11830 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11831 isl_int_set(mat->row[i][pos],
11832 bmap->ineq[i][off + k]);
11833 ++pos;
11837 return mat;
11840 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11841 __isl_take isl_space *dim,
11842 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11843 enum isl_dim_type c2, enum isl_dim_type c3,
11844 enum isl_dim_type c4, enum isl_dim_type c5)
11846 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11847 isl_basic_map *bmap;
11848 unsigned total;
11849 unsigned extra;
11850 int i, j, k, l;
11851 int pos;
11853 if (!dim || !eq || !ineq)
11854 goto error;
11856 if (eq->n_col != ineq->n_col)
11857 isl_die(dim->ctx, isl_error_invalid,
11858 "equalities and inequalities matrices should have "
11859 "same number of columns", goto error);
11861 total = 1 + isl_space_dim(dim, isl_dim_all);
11863 if (eq->n_col < total)
11864 isl_die(dim->ctx, isl_error_invalid,
11865 "number of columns too small", goto error);
11867 extra = eq->n_col - total;
11869 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11870 eq->n_row, ineq->n_row);
11871 if (!bmap)
11872 goto error;
11873 for (i = 0; i < extra; ++i) {
11874 k = isl_basic_map_alloc_div(bmap);
11875 if (k < 0)
11876 goto error;
11877 isl_int_set_si(bmap->div[k][0], 0);
11879 for (i = 0; i < eq->n_row; ++i) {
11880 l = isl_basic_map_alloc_equality(bmap);
11881 if (l < 0)
11882 goto error;
11883 for (j = 0, pos = 0; j < 5; ++j) {
11884 int off = isl_basic_map_offset(bmap, c[j]);
11885 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11886 isl_int_set(bmap->eq[l][off + k],
11887 eq->row[i][pos]);
11888 ++pos;
11892 for (i = 0; i < ineq->n_row; ++i) {
11893 l = isl_basic_map_alloc_inequality(bmap);
11894 if (l < 0)
11895 goto error;
11896 for (j = 0, pos = 0; j < 5; ++j) {
11897 int off = isl_basic_map_offset(bmap, c[j]);
11898 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11899 isl_int_set(bmap->ineq[l][off + k],
11900 ineq->row[i][pos]);
11901 ++pos;
11906 isl_space_free(dim);
11907 isl_mat_free(eq);
11908 isl_mat_free(ineq);
11910 bmap = isl_basic_map_simplify(bmap);
11911 return isl_basic_map_finalize(bmap);
11912 error:
11913 isl_space_free(dim);
11914 isl_mat_free(eq);
11915 isl_mat_free(ineq);
11916 return NULL;
11919 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11920 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11921 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11923 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11924 c1, c2, c3, c4, isl_dim_in);
11927 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11928 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11929 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11931 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11932 c1, c2, c3, c4, isl_dim_in);
11935 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11936 __isl_take isl_space *dim,
11937 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11938 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11940 isl_basic_map *bmap;
11941 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11942 c1, c2, c3, c4, isl_dim_in);
11943 return bset_from_bmap(bmap);
11946 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11948 if (!bmap)
11949 return isl_bool_error;
11951 return isl_space_can_zip(bmap->dim);
11954 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11956 if (!map)
11957 return isl_bool_error;
11959 return isl_space_can_zip(map->dim);
11962 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11963 * (A -> C) -> (B -> D).
11965 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11967 unsigned pos;
11968 unsigned n1;
11969 unsigned n2;
11971 if (!bmap)
11972 return NULL;
11974 if (!isl_basic_map_can_zip(bmap))
11975 isl_die(bmap->ctx, isl_error_invalid,
11976 "basic map cannot be zipped", goto error);
11977 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11978 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11979 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11980 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11981 bmap = isl_basic_map_cow(bmap);
11982 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11983 if (!bmap)
11984 return NULL;
11985 bmap->dim = isl_space_zip(bmap->dim);
11986 if (!bmap->dim)
11987 goto error;
11988 bmap = isl_basic_map_mark_final(bmap);
11989 return bmap;
11990 error:
11991 isl_basic_map_free(bmap);
11992 return NULL;
11995 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11996 * (A -> C) -> (B -> D).
11998 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12000 int i;
12002 if (!map)
12003 return NULL;
12005 if (!isl_map_can_zip(map))
12006 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12007 goto error);
12009 map = isl_map_cow(map);
12010 if (!map)
12011 return NULL;
12013 for (i = 0; i < map->n; ++i) {
12014 map->p[i] = isl_basic_map_zip(map->p[i]);
12015 if (!map->p[i])
12016 goto error;
12019 map->dim = isl_space_zip(map->dim);
12020 if (!map->dim)
12021 goto error;
12023 return map;
12024 error:
12025 isl_map_free(map);
12026 return NULL;
12029 /* Can we apply isl_basic_map_curry to "bmap"?
12030 * That is, does it have a nested relation in its domain?
12032 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12034 if (!bmap)
12035 return isl_bool_error;
12037 return isl_space_can_curry(bmap->dim);
12040 /* Can we apply isl_map_curry to "map"?
12041 * That is, does it have a nested relation in its domain?
12043 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12045 if (!map)
12046 return isl_bool_error;
12048 return isl_space_can_curry(map->dim);
12051 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12052 * A -> (B -> C).
12054 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12057 if (!bmap)
12058 return NULL;
12060 if (!isl_basic_map_can_curry(bmap))
12061 isl_die(bmap->ctx, isl_error_invalid,
12062 "basic map cannot be curried", goto error);
12063 bmap = isl_basic_map_cow(bmap);
12064 if (!bmap)
12065 return NULL;
12066 bmap->dim = isl_space_curry(bmap->dim);
12067 if (!bmap->dim)
12068 goto error;
12069 bmap = isl_basic_map_mark_final(bmap);
12070 return bmap;
12071 error:
12072 isl_basic_map_free(bmap);
12073 return NULL;
12076 /* Given a map (A -> B) -> C, return the corresponding map
12077 * A -> (B -> C).
12079 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12081 return isl_map_change_space(map, &isl_map_can_curry,
12082 "map cannot be curried", &isl_space_curry);
12085 /* Can isl_map_range_curry be applied to "map"?
12086 * That is, does it have a nested relation in its range,
12087 * the domain of which is itself a nested relation?
12089 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12091 if (!map)
12092 return isl_bool_error;
12094 return isl_space_can_range_curry(map->dim);
12097 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12098 * A -> (B -> (C -> D)).
12100 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12102 return isl_map_change_space(map, &isl_map_can_range_curry,
12103 "map range cannot be curried",
12104 &isl_space_range_curry);
12107 /* Can we apply isl_basic_map_uncurry to "bmap"?
12108 * That is, does it have a nested relation in its domain?
12110 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12112 if (!bmap)
12113 return isl_bool_error;
12115 return isl_space_can_uncurry(bmap->dim);
12118 /* Can we apply isl_map_uncurry to "map"?
12119 * That is, does it have a nested relation in its domain?
12121 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12123 if (!map)
12124 return isl_bool_error;
12126 return isl_space_can_uncurry(map->dim);
12129 /* Given a basic map A -> (B -> C), return the corresponding basic map
12130 * (A -> B) -> C.
12132 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12135 if (!bmap)
12136 return NULL;
12138 if (!isl_basic_map_can_uncurry(bmap))
12139 isl_die(bmap->ctx, isl_error_invalid,
12140 "basic map cannot be uncurried",
12141 return isl_basic_map_free(bmap));
12142 bmap = isl_basic_map_cow(bmap);
12143 if (!bmap)
12144 return NULL;
12145 bmap->dim = isl_space_uncurry(bmap->dim);
12146 if (!bmap->dim)
12147 return isl_basic_map_free(bmap);
12148 bmap = isl_basic_map_mark_final(bmap);
12149 return bmap;
12152 /* Given a map A -> (B -> C), return the corresponding map
12153 * (A -> B) -> C.
12155 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12157 return isl_map_change_space(map, &isl_map_can_uncurry,
12158 "map cannot be uncurried", &isl_space_uncurry);
12161 /* Construct a basic map mapping the domain of the affine expression
12162 * to a one-dimensional range prescribed by the affine expression.
12163 * If "rational" is set, then construct a rational basic map.
12165 * A NaN affine expression cannot be converted to a basic map.
12167 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12168 __isl_take isl_aff *aff, int rational)
12170 int k;
12171 int pos;
12172 isl_bool is_nan;
12173 isl_local_space *ls;
12174 isl_basic_map *bmap = NULL;
12176 if (!aff)
12177 return NULL;
12178 is_nan = isl_aff_is_nan(aff);
12179 if (is_nan < 0)
12180 goto error;
12181 if (is_nan)
12182 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12183 "cannot convert NaN", goto error);
12185 ls = isl_aff_get_local_space(aff);
12186 bmap = isl_basic_map_from_local_space(ls);
12187 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12188 k = isl_basic_map_alloc_equality(bmap);
12189 if (k < 0)
12190 goto error;
12192 pos = isl_basic_map_offset(bmap, isl_dim_out);
12193 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12194 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12195 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12196 aff->v->size - (pos + 1));
12198 isl_aff_free(aff);
12199 if (rational)
12200 bmap = isl_basic_map_set_rational(bmap);
12201 bmap = isl_basic_map_gauss(bmap, NULL);
12202 bmap = isl_basic_map_finalize(bmap);
12203 return bmap;
12204 error:
12205 isl_aff_free(aff);
12206 isl_basic_map_free(bmap);
12207 return NULL;
12210 /* Construct a basic map mapping the domain of the affine expression
12211 * to a one-dimensional range prescribed by the affine expression.
12213 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12215 return isl_basic_map_from_aff2(aff, 0);
12218 /* Construct a map mapping the domain of the affine expression
12219 * to a one-dimensional range prescribed by the affine expression.
12221 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12223 isl_basic_map *bmap;
12225 bmap = isl_basic_map_from_aff(aff);
12226 return isl_map_from_basic_map(bmap);
12229 /* Construct a basic map mapping the domain the multi-affine expression
12230 * to its range, with each dimension in the range equated to the
12231 * corresponding affine expression.
12232 * If "rational" is set, then construct a rational basic map.
12234 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12235 __isl_take isl_multi_aff *maff, int rational)
12237 int i;
12238 isl_space *space;
12239 isl_basic_map *bmap;
12241 if (!maff)
12242 return NULL;
12244 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12245 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12246 "invalid space", goto error);
12248 space = isl_space_domain(isl_multi_aff_get_space(maff));
12249 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12250 if (rational)
12251 bmap = isl_basic_map_set_rational(bmap);
12253 for (i = 0; i < maff->n; ++i) {
12254 isl_aff *aff;
12255 isl_basic_map *bmap_i;
12257 aff = isl_aff_copy(maff->p[i]);
12258 bmap_i = isl_basic_map_from_aff2(aff, rational);
12260 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12263 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12265 isl_multi_aff_free(maff);
12266 return bmap;
12267 error:
12268 isl_multi_aff_free(maff);
12269 return NULL;
12272 /* Construct a basic map mapping the domain the multi-affine expression
12273 * to its range, with each dimension in the range equated to the
12274 * corresponding affine expression.
12276 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12277 __isl_take isl_multi_aff *ma)
12279 return isl_basic_map_from_multi_aff2(ma, 0);
12282 /* Construct a map mapping the domain the multi-affine expression
12283 * to its range, with each dimension in the range equated to the
12284 * corresponding affine expression.
12286 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12288 isl_basic_map *bmap;
12290 bmap = isl_basic_map_from_multi_aff(maff);
12291 return isl_map_from_basic_map(bmap);
12294 /* Construct a basic map mapping a domain in the given space to
12295 * to an n-dimensional range, with n the number of elements in the list,
12296 * where each coordinate in the range is prescribed by the
12297 * corresponding affine expression.
12298 * The domains of all affine expressions in the list are assumed to match
12299 * domain_dim.
12301 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12302 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12304 int i;
12305 isl_space *dim;
12306 isl_basic_map *bmap;
12308 if (!list)
12309 return NULL;
12311 dim = isl_space_from_domain(domain_dim);
12312 bmap = isl_basic_map_universe(dim);
12314 for (i = 0; i < list->n; ++i) {
12315 isl_aff *aff;
12316 isl_basic_map *bmap_i;
12318 aff = isl_aff_copy(list->p[i]);
12319 bmap_i = isl_basic_map_from_aff(aff);
12321 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12324 isl_aff_list_free(list);
12325 return bmap;
12328 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12329 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12331 return isl_map_equate(set, type1, pos1, type2, pos2);
12334 /* Construct a basic map where the given dimensions are equal to each other.
12336 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12337 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12339 isl_basic_map *bmap = NULL;
12340 int i;
12342 if (!space)
12343 return NULL;
12345 if (pos1 >= isl_space_dim(space, type1))
12346 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12347 "index out of bounds", goto error);
12348 if (pos2 >= isl_space_dim(space, type2))
12349 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12350 "index out of bounds", goto error);
12352 if (type1 == type2 && pos1 == pos2)
12353 return isl_basic_map_universe(space);
12355 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12356 i = isl_basic_map_alloc_equality(bmap);
12357 if (i < 0)
12358 goto error;
12359 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12360 pos1 += isl_basic_map_offset(bmap, type1);
12361 pos2 += isl_basic_map_offset(bmap, type2);
12362 isl_int_set_si(bmap->eq[i][pos1], -1);
12363 isl_int_set_si(bmap->eq[i][pos2], 1);
12364 bmap = isl_basic_map_finalize(bmap);
12365 isl_space_free(space);
12366 return bmap;
12367 error:
12368 isl_space_free(space);
12369 isl_basic_map_free(bmap);
12370 return NULL;
12373 /* Add a constraint imposing that the given two dimensions are equal.
12375 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12376 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12378 isl_basic_map *eq;
12380 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12382 bmap = isl_basic_map_intersect(bmap, eq);
12384 return bmap;
12387 /* Add a constraint imposing that the given two dimensions are equal.
12389 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12390 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12392 isl_basic_map *bmap;
12394 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12396 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12398 return map;
12401 /* Add a constraint imposing that the given two dimensions have opposite values.
12403 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12404 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12406 isl_basic_map *bmap = NULL;
12407 int i;
12409 if (!map)
12410 return NULL;
12412 if (pos1 >= isl_map_dim(map, type1))
12413 isl_die(map->ctx, isl_error_invalid,
12414 "index out of bounds", goto error);
12415 if (pos2 >= isl_map_dim(map, type2))
12416 isl_die(map->ctx, isl_error_invalid,
12417 "index out of bounds", goto error);
12419 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12420 i = isl_basic_map_alloc_equality(bmap);
12421 if (i < 0)
12422 goto error;
12423 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12424 pos1 += isl_basic_map_offset(bmap, type1);
12425 pos2 += isl_basic_map_offset(bmap, type2);
12426 isl_int_set_si(bmap->eq[i][pos1], 1);
12427 isl_int_set_si(bmap->eq[i][pos2], 1);
12428 bmap = isl_basic_map_finalize(bmap);
12430 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12432 return map;
12433 error:
12434 isl_basic_map_free(bmap);
12435 isl_map_free(map);
12436 return NULL;
12439 /* Construct a constraint imposing that the value of the first dimension is
12440 * greater than or equal to that of the second.
12442 static __isl_give isl_constraint *constraint_order_ge(
12443 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12444 enum isl_dim_type type2, int pos2)
12446 isl_constraint *c;
12448 if (!space)
12449 return NULL;
12451 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12453 if (pos1 >= isl_constraint_dim(c, type1))
12454 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12455 "index out of bounds", return isl_constraint_free(c));
12456 if (pos2 >= isl_constraint_dim(c, type2))
12457 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12458 "index out of bounds", return isl_constraint_free(c));
12460 if (type1 == type2 && pos1 == pos2)
12461 return c;
12463 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12464 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12466 return c;
12469 /* Add a constraint imposing that the value of the first dimension is
12470 * greater than or equal to that of the second.
12472 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12473 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12475 isl_constraint *c;
12476 isl_space *space;
12478 if (type1 == type2 && pos1 == pos2)
12479 return bmap;
12480 space = isl_basic_map_get_space(bmap);
12481 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12482 bmap = isl_basic_map_add_constraint(bmap, c);
12484 return bmap;
12487 /* Add a constraint imposing that the value of the first dimension is
12488 * greater than or equal to that of the second.
12490 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12491 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12493 isl_constraint *c;
12494 isl_space *space;
12496 if (type1 == type2 && pos1 == pos2)
12497 return map;
12498 space = isl_map_get_space(map);
12499 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12500 map = isl_map_add_constraint(map, c);
12502 return map;
12505 /* Add a constraint imposing that the value of the first dimension is
12506 * less than or equal to that of the second.
12508 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12509 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12511 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12514 /* Construct a basic map where the value of the first dimension is
12515 * greater than that of the second.
12517 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12518 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12520 isl_basic_map *bmap = NULL;
12521 int i;
12523 if (!space)
12524 return NULL;
12526 if (pos1 >= isl_space_dim(space, type1))
12527 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12528 "index out of bounds", goto error);
12529 if (pos2 >= isl_space_dim(space, type2))
12530 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12531 "index out of bounds", goto error);
12533 if (type1 == type2 && pos1 == pos2)
12534 return isl_basic_map_empty(space);
12536 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12537 i = isl_basic_map_alloc_inequality(bmap);
12538 if (i < 0)
12539 return isl_basic_map_free(bmap);
12540 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12541 pos1 += isl_basic_map_offset(bmap, type1);
12542 pos2 += isl_basic_map_offset(bmap, type2);
12543 isl_int_set_si(bmap->ineq[i][pos1], 1);
12544 isl_int_set_si(bmap->ineq[i][pos2], -1);
12545 isl_int_set_si(bmap->ineq[i][0], -1);
12546 bmap = isl_basic_map_finalize(bmap);
12548 return bmap;
12549 error:
12550 isl_space_free(space);
12551 isl_basic_map_free(bmap);
12552 return NULL;
12555 /* Add a constraint imposing that the value of the first dimension is
12556 * greater than that of the second.
12558 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12559 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12561 isl_basic_map *gt;
12563 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12565 bmap = isl_basic_map_intersect(bmap, gt);
12567 return bmap;
12570 /* Add a constraint imposing that the value of the first dimension is
12571 * greater than that of the second.
12573 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12574 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12576 isl_basic_map *bmap;
12578 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12580 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12582 return map;
12585 /* Add a constraint imposing that the value of the first dimension is
12586 * smaller than that of the second.
12588 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12589 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12591 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12594 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12595 int pos)
12597 isl_aff *div;
12598 isl_local_space *ls;
12600 if (!bmap)
12601 return NULL;
12603 if (!isl_basic_map_divs_known(bmap))
12604 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12605 "some divs are unknown", return NULL);
12607 ls = isl_basic_map_get_local_space(bmap);
12608 div = isl_local_space_get_div(ls, pos);
12609 isl_local_space_free(ls);
12611 return div;
12614 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12615 int pos)
12617 return isl_basic_map_get_div(bset, pos);
12620 /* Plug in "subs" for dimension "type", "pos" of "bset".
12622 * Let i be the dimension to replace and let "subs" be of the form
12624 * f/d
12626 * Any integer division with a non-zero coefficient for i,
12628 * floor((a i + g)/m)
12630 * is replaced by
12632 * floor((a f + d g)/(m d))
12634 * Constraints of the form
12636 * a i + g
12638 * are replaced by
12640 * a f + d g
12642 * We currently require that "subs" is an integral expression.
12643 * Handling rational expressions may require us to add stride constraints
12644 * as we do in isl_basic_set_preimage_multi_aff.
12646 __isl_give isl_basic_set *isl_basic_set_substitute(
12647 __isl_take isl_basic_set *bset,
12648 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12650 int i;
12651 isl_int v;
12652 isl_ctx *ctx;
12654 if (bset && isl_basic_set_plain_is_empty(bset))
12655 return bset;
12657 bset = isl_basic_set_cow(bset);
12658 if (!bset || !subs)
12659 goto error;
12661 ctx = isl_basic_set_get_ctx(bset);
12662 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12663 isl_die(ctx, isl_error_invalid,
12664 "spaces don't match", goto error);
12665 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12666 isl_die(ctx, isl_error_unsupported,
12667 "cannot handle divs yet", goto error);
12668 if (!isl_int_is_one(subs->v->el[0]))
12669 isl_die(ctx, isl_error_invalid,
12670 "can only substitute integer expressions", goto error);
12672 pos += isl_basic_set_offset(bset, type);
12674 isl_int_init(v);
12676 for (i = 0; i < bset->n_eq; ++i) {
12677 if (isl_int_is_zero(bset->eq[i][pos]))
12678 continue;
12679 isl_int_set(v, bset->eq[i][pos]);
12680 isl_int_set_si(bset->eq[i][pos], 0);
12681 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12682 v, subs->v->el + 1, subs->v->size - 1);
12685 for (i = 0; i < bset->n_ineq; ++i) {
12686 if (isl_int_is_zero(bset->ineq[i][pos]))
12687 continue;
12688 isl_int_set(v, bset->ineq[i][pos]);
12689 isl_int_set_si(bset->ineq[i][pos], 0);
12690 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12691 v, subs->v->el + 1, subs->v->size - 1);
12694 for (i = 0; i < bset->n_div; ++i) {
12695 if (isl_int_is_zero(bset->div[i][1 + pos]))
12696 continue;
12697 isl_int_set(v, bset->div[i][1 + pos]);
12698 isl_int_set_si(bset->div[i][1 + pos], 0);
12699 isl_seq_combine(bset->div[i] + 1,
12700 subs->v->el[0], bset->div[i] + 1,
12701 v, subs->v->el + 1, subs->v->size - 1);
12702 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12705 isl_int_clear(v);
12707 bset = isl_basic_set_simplify(bset);
12708 return isl_basic_set_finalize(bset);
12709 error:
12710 isl_basic_set_free(bset);
12711 return NULL;
12714 /* Plug in "subs" for dimension "type", "pos" of "set".
12716 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12717 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12719 int i;
12721 if (set && isl_set_plain_is_empty(set))
12722 return set;
12724 set = isl_set_cow(set);
12725 if (!set || !subs)
12726 goto error;
12728 for (i = set->n - 1; i >= 0; --i) {
12729 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12730 set = set_from_map(remove_if_empty(set_to_map(set), i));
12731 if (!set)
12732 return NULL;
12735 return set;
12736 error:
12737 isl_set_free(set);
12738 return NULL;
12741 /* Check if the range of "ma" is compatible with the domain or range
12742 * (depending on "type") of "bmap".
12744 static isl_stat check_basic_map_compatible_range_multi_aff(
12745 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12746 __isl_keep isl_multi_aff *ma)
12748 isl_bool m;
12749 isl_space *ma_space;
12751 ma_space = isl_multi_aff_get_space(ma);
12753 m = isl_space_has_equal_params(bmap->dim, ma_space);
12754 if (m < 0)
12755 goto error;
12756 if (!m)
12757 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12758 "parameters don't match", goto error);
12759 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12760 if (m < 0)
12761 goto error;
12762 if (!m)
12763 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12764 "spaces don't match", goto error);
12766 isl_space_free(ma_space);
12767 return isl_stat_ok;
12768 error:
12769 isl_space_free(ma_space);
12770 return isl_stat_error;
12773 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12774 * coefficients before the transformed range of dimensions,
12775 * the "n_after" coefficients after the transformed range of dimensions
12776 * and the coefficients of the other divs in "bmap".
12778 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12779 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12781 int i;
12782 int n_param;
12783 int n_set;
12784 isl_local_space *ls;
12786 if (n_div == 0)
12787 return 0;
12789 ls = isl_aff_get_domain_local_space(ma->p[0]);
12790 if (!ls)
12791 return -1;
12793 n_param = isl_local_space_dim(ls, isl_dim_param);
12794 n_set = isl_local_space_dim(ls, isl_dim_set);
12795 for (i = 0; i < n_div; ++i) {
12796 int o_bmap = 0, o_ls = 0;
12798 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12799 o_bmap += 1 + 1 + n_param;
12800 o_ls += 1 + 1 + n_param;
12801 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12802 o_bmap += n_before;
12803 isl_seq_cpy(bmap->div[i] + o_bmap,
12804 ls->div->row[i] + o_ls, n_set);
12805 o_bmap += n_set;
12806 o_ls += n_set;
12807 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12808 o_bmap += n_after;
12809 isl_seq_cpy(bmap->div[i] + o_bmap,
12810 ls->div->row[i] + o_ls, n_div);
12811 o_bmap += n_div;
12812 o_ls += n_div;
12813 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12814 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12815 goto error;
12818 isl_local_space_free(ls);
12819 return 0;
12820 error:
12821 isl_local_space_free(ls);
12822 return -1;
12825 /* How many stride constraints does "ma" enforce?
12826 * That is, how many of the affine expressions have a denominator
12827 * different from one?
12829 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12831 int i;
12832 int strides = 0;
12834 for (i = 0; i < ma->n; ++i)
12835 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12836 strides++;
12838 return strides;
12841 /* For each affine expression in ma of the form
12843 * x_i = (f_i y + h_i)/m_i
12845 * with m_i different from one, add a constraint to "bmap"
12846 * of the form
12848 * f_i y + h_i = m_i alpha_i
12850 * with alpha_i an additional existentially quantified variable.
12852 * The input variables of "ma" correspond to a subset of the variables
12853 * of "bmap". There are "n_before" variables in "bmap" before this
12854 * subset and "n_after" variables after this subset.
12855 * The integer divisions of the affine expressions in "ma" are assumed
12856 * to have been aligned. There are "n_div_ma" of them and
12857 * they appear first in "bmap", straight after the "n_after" variables.
12859 static __isl_give isl_basic_map *add_ma_strides(
12860 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12861 int n_before, int n_after, int n_div_ma)
12863 int i, k;
12864 int div;
12865 int total;
12866 int n_param;
12867 int n_in;
12869 total = isl_basic_map_total_dim(bmap);
12870 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12871 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12872 for (i = 0; i < ma->n; ++i) {
12873 int o_bmap = 0, o_ma = 1;
12875 if (isl_int_is_one(ma->p[i]->v->el[0]))
12876 continue;
12877 div = isl_basic_map_alloc_div(bmap);
12878 k = isl_basic_map_alloc_equality(bmap);
12879 if (div < 0 || k < 0)
12880 goto error;
12881 isl_int_set_si(bmap->div[div][0], 0);
12882 isl_seq_cpy(bmap->eq[k] + o_bmap,
12883 ma->p[i]->v->el + o_ma, 1 + n_param);
12884 o_bmap += 1 + n_param;
12885 o_ma += 1 + n_param;
12886 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12887 o_bmap += n_before;
12888 isl_seq_cpy(bmap->eq[k] + o_bmap,
12889 ma->p[i]->v->el + o_ma, n_in);
12890 o_bmap += n_in;
12891 o_ma += n_in;
12892 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12893 o_bmap += n_after;
12894 isl_seq_cpy(bmap->eq[k] + o_bmap,
12895 ma->p[i]->v->el + o_ma, n_div_ma);
12896 o_bmap += n_div_ma;
12897 o_ma += n_div_ma;
12898 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12899 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12900 total++;
12903 return bmap;
12904 error:
12905 isl_basic_map_free(bmap);
12906 return NULL;
12909 /* Replace the domain or range space (depending on "type) of "space" by "set".
12911 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12912 enum isl_dim_type type, __isl_take isl_space *set)
12914 if (type == isl_dim_in) {
12915 space = isl_space_range(space);
12916 space = isl_space_map_from_domain_and_range(set, space);
12917 } else {
12918 space = isl_space_domain(space);
12919 space = isl_space_map_from_domain_and_range(space, set);
12922 return space;
12925 /* Compute the preimage of the domain or range (depending on "type")
12926 * of "bmap" under the function represented by "ma".
12927 * In other words, plug in "ma" in the domain or range of "bmap".
12928 * The result is a basic map that lives in the same space as "bmap"
12929 * except that the domain or range has been replaced by
12930 * the domain space of "ma".
12932 * If bmap is represented by
12934 * A(p) + S u + B x + T v + C(divs) >= 0,
12936 * where u and x are input and output dimensions if type == isl_dim_out
12937 * while x and v are input and output dimensions if type == isl_dim_in,
12938 * and ma is represented by
12940 * x = D(p) + F(y) + G(divs')
12942 * then the result is
12944 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12946 * The divs in the input set are similarly adjusted.
12947 * In particular
12949 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12951 * becomes
12953 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12954 * B_i G(divs') + c_i(divs))/n_i)
12956 * If bmap is not a rational map and if F(y) involves any denominators
12958 * x_i = (f_i y + h_i)/m_i
12960 * then additional constraints are added to ensure that we only
12961 * map back integer points. That is we enforce
12963 * f_i y + h_i = m_i alpha_i
12965 * with alpha_i an additional existentially quantified variable.
12967 * We first copy over the divs from "ma".
12968 * Then we add the modified constraints and divs from "bmap".
12969 * Finally, we add the stride constraints, if needed.
12971 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12972 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12973 __isl_take isl_multi_aff *ma)
12975 int i, k;
12976 isl_space *space;
12977 isl_basic_map *res = NULL;
12978 int n_before, n_after, n_div_bmap, n_div_ma;
12979 isl_int f, c1, c2, g;
12980 isl_bool rational;
12981 int strides;
12983 isl_int_init(f);
12984 isl_int_init(c1);
12985 isl_int_init(c2);
12986 isl_int_init(g);
12988 ma = isl_multi_aff_align_divs(ma);
12989 if (!bmap || !ma)
12990 goto error;
12991 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12992 goto error;
12994 if (type == isl_dim_in) {
12995 n_before = 0;
12996 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12997 } else {
12998 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12999 n_after = 0;
13001 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13002 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
13004 space = isl_multi_aff_get_domain_space(ma);
13005 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13006 rational = isl_basic_map_is_rational(bmap);
13007 strides = rational ? 0 : multi_aff_strides(ma);
13008 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13009 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13010 if (rational)
13011 res = isl_basic_map_set_rational(res);
13013 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13014 if (isl_basic_map_alloc_div(res) < 0)
13015 goto error;
13017 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
13018 goto error;
13020 for (i = 0; i < bmap->n_eq; ++i) {
13021 k = isl_basic_map_alloc_equality(res);
13022 if (k < 0)
13023 goto error;
13024 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13025 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13028 for (i = 0; i < bmap->n_ineq; ++i) {
13029 k = isl_basic_map_alloc_inequality(res);
13030 if (k < 0)
13031 goto error;
13032 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13033 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13036 for (i = 0; i < bmap->n_div; ++i) {
13037 if (isl_int_is_zero(bmap->div[i][0])) {
13038 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13039 continue;
13041 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13042 n_before, n_after, n_div_ma, n_div_bmap,
13043 f, c1, c2, g, 1);
13046 if (strides)
13047 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13049 isl_int_clear(f);
13050 isl_int_clear(c1);
13051 isl_int_clear(c2);
13052 isl_int_clear(g);
13053 isl_basic_map_free(bmap);
13054 isl_multi_aff_free(ma);
13055 res = isl_basic_map_simplify(res);
13056 return isl_basic_map_finalize(res);
13057 error:
13058 isl_int_clear(f);
13059 isl_int_clear(c1);
13060 isl_int_clear(c2);
13061 isl_int_clear(g);
13062 isl_basic_map_free(bmap);
13063 isl_multi_aff_free(ma);
13064 isl_basic_map_free(res);
13065 return NULL;
13068 /* Compute the preimage of "bset" under the function represented by "ma".
13069 * In other words, plug in "ma" in "bset". The result is a basic set
13070 * that lives in the domain space of "ma".
13072 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13073 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13075 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13078 /* Compute the preimage of the domain of "bmap" under the function
13079 * represented by "ma".
13080 * In other words, plug in "ma" in the domain of "bmap".
13081 * The result is a basic map that lives in the same space as "bmap"
13082 * except that the domain has been replaced by the domain space of "ma".
13084 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13085 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13087 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13090 /* Compute the preimage of the range of "bmap" under the function
13091 * represented by "ma".
13092 * In other words, plug in "ma" in the range of "bmap".
13093 * The result is a basic map that lives in the same space as "bmap"
13094 * except that the range has been replaced by the domain space of "ma".
13096 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13097 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13099 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13102 /* Check if the range of "ma" is compatible with the domain or range
13103 * (depending on "type") of "map".
13104 * Return isl_stat_error if anything is wrong.
13106 static isl_stat check_map_compatible_range_multi_aff(
13107 __isl_keep isl_map *map, enum isl_dim_type type,
13108 __isl_keep isl_multi_aff *ma)
13110 isl_bool m;
13111 isl_space *ma_space;
13113 ma_space = isl_multi_aff_get_space(ma);
13114 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13115 isl_space_free(ma_space);
13116 if (m < 0)
13117 return isl_stat_error;
13118 if (!m)
13119 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13120 "spaces don't match", return isl_stat_error);
13121 return isl_stat_ok;
13124 /* Compute the preimage of the domain or range (depending on "type")
13125 * of "map" under the function represented by "ma".
13126 * In other words, plug in "ma" in the domain or range of "map".
13127 * The result is a map that lives in the same space as "map"
13128 * except that the domain or range has been replaced by
13129 * the domain space of "ma".
13131 * The parameters are assumed to have been aligned.
13133 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13134 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13136 int i;
13137 isl_space *space;
13139 map = isl_map_cow(map);
13140 ma = isl_multi_aff_align_divs(ma);
13141 if (!map || !ma)
13142 goto error;
13143 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13144 goto error;
13146 for (i = 0; i < map->n; ++i) {
13147 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13148 isl_multi_aff_copy(ma));
13149 if (!map->p[i])
13150 goto error;
13153 space = isl_multi_aff_get_domain_space(ma);
13154 space = isl_space_set(isl_map_get_space(map), type, space);
13156 isl_space_free(map->dim);
13157 map->dim = space;
13158 if (!map->dim)
13159 goto error;
13161 isl_multi_aff_free(ma);
13162 if (map->n > 1)
13163 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13164 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13165 return map;
13166 error:
13167 isl_multi_aff_free(ma);
13168 isl_map_free(map);
13169 return NULL;
13172 /* Compute the preimage of the domain or range (depending on "type")
13173 * of "map" under the function represented by "ma".
13174 * In other words, plug in "ma" in the domain or range of "map".
13175 * The result is a map that lives in the same space as "map"
13176 * except that the domain or range has been replaced by
13177 * the domain space of "ma".
13179 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13180 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13182 isl_bool aligned;
13184 if (!map || !ma)
13185 goto error;
13187 aligned = isl_map_space_has_equal_params(map, ma->space);
13188 if (aligned < 0)
13189 goto error;
13190 if (aligned)
13191 return map_preimage_multi_aff(map, type, ma);
13193 if (isl_map_check_named_params(map) < 0)
13194 goto error;
13195 if (!isl_space_has_named_params(ma->space))
13196 isl_die(map->ctx, isl_error_invalid,
13197 "unaligned unnamed parameters", goto error);
13198 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13199 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13201 return map_preimage_multi_aff(map, type, ma);
13202 error:
13203 isl_multi_aff_free(ma);
13204 return isl_map_free(map);
13207 /* Compute the preimage of "set" under the function represented by "ma".
13208 * In other words, plug in "ma" in "set". The result is a set
13209 * that lives in the domain space of "ma".
13211 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13212 __isl_take isl_multi_aff *ma)
13214 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13217 /* Compute the preimage of the domain of "map" under the function
13218 * represented by "ma".
13219 * In other words, plug in "ma" in the domain of "map".
13220 * The result is a map that lives in the same space as "map"
13221 * except that the domain has been replaced by the domain space of "ma".
13223 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13224 __isl_take isl_multi_aff *ma)
13226 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13229 /* Compute the preimage of the range of "map" under the function
13230 * represented by "ma".
13231 * In other words, plug in "ma" in the range of "map".
13232 * The result is a map that lives in the same space as "map"
13233 * except that the range has been replaced by the domain space of "ma".
13235 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13236 __isl_take isl_multi_aff *ma)
13238 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13241 /* Compute the preimage of "map" under the function represented by "pma".
13242 * In other words, plug in "pma" in the domain or range of "map".
13243 * The result is a map that lives in the same space as "map",
13244 * except that the space of type "type" has been replaced by
13245 * the domain space of "pma".
13247 * The parameters of "map" and "pma" are assumed to have been aligned.
13249 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13250 __isl_take isl_map *map, enum isl_dim_type type,
13251 __isl_take isl_pw_multi_aff *pma)
13253 int i;
13254 isl_map *res;
13256 if (!pma)
13257 goto error;
13259 if (pma->n == 0) {
13260 isl_pw_multi_aff_free(pma);
13261 res = isl_map_empty(isl_map_get_space(map));
13262 isl_map_free(map);
13263 return res;
13266 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13267 isl_multi_aff_copy(pma->p[0].maff));
13268 if (type == isl_dim_in)
13269 res = isl_map_intersect_domain(res,
13270 isl_map_copy(pma->p[0].set));
13271 else
13272 res = isl_map_intersect_range(res,
13273 isl_map_copy(pma->p[0].set));
13275 for (i = 1; i < pma->n; ++i) {
13276 isl_map *res_i;
13278 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13279 isl_multi_aff_copy(pma->p[i].maff));
13280 if (type == isl_dim_in)
13281 res_i = isl_map_intersect_domain(res_i,
13282 isl_map_copy(pma->p[i].set));
13283 else
13284 res_i = isl_map_intersect_range(res_i,
13285 isl_map_copy(pma->p[i].set));
13286 res = isl_map_union(res, res_i);
13289 isl_pw_multi_aff_free(pma);
13290 isl_map_free(map);
13291 return res;
13292 error:
13293 isl_pw_multi_aff_free(pma);
13294 isl_map_free(map);
13295 return NULL;
13298 /* Compute the preimage of "map" under the function represented by "pma".
13299 * In other words, plug in "pma" in the domain or range of "map".
13300 * The result is a map that lives in the same space as "map",
13301 * except that the space of type "type" has been replaced by
13302 * the domain space of "pma".
13304 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13305 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13307 isl_bool aligned;
13309 if (!map || !pma)
13310 goto error;
13312 aligned = isl_map_space_has_equal_params(map, pma->dim);
13313 if (aligned < 0)
13314 goto error;
13315 if (aligned)
13316 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13318 if (isl_map_check_named_params(map) < 0)
13319 goto error;
13320 if (!isl_space_has_named_params(pma->dim))
13321 isl_die(map->ctx, isl_error_invalid,
13322 "unaligned unnamed parameters", goto error);
13323 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13324 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13326 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13327 error:
13328 isl_pw_multi_aff_free(pma);
13329 return isl_map_free(map);
13332 /* Compute the preimage of "set" under the function represented by "pma".
13333 * In other words, plug in "pma" in "set". The result is a set
13334 * that lives in the domain space of "pma".
13336 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13337 __isl_take isl_pw_multi_aff *pma)
13339 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13342 /* Compute the preimage of the domain of "map" under the function
13343 * represented by "pma".
13344 * In other words, plug in "pma" in the domain of "map".
13345 * The result is a map that lives in the same space as "map",
13346 * except that domain space has been replaced by the domain space of "pma".
13348 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13349 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13351 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13354 /* Compute the preimage of the range of "map" under the function
13355 * represented by "pma".
13356 * In other words, plug in "pma" in the range of "map".
13357 * The result is a map that lives in the same space as "map",
13358 * except that range space has been replaced by the domain space of "pma".
13360 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13361 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13363 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13366 /* Compute the preimage of "map" under the function represented by "mpa".
13367 * In other words, plug in "mpa" in the domain or range of "map".
13368 * The result is a map that lives in the same space as "map",
13369 * except that the space of type "type" has been replaced by
13370 * the domain space of "mpa".
13372 * If the map does not involve any constraints that refer to the
13373 * dimensions of the substituted space, then the only possible
13374 * effect of "mpa" on the map is to map the space to a different space.
13375 * We create a separate isl_multi_aff to effectuate this change
13376 * in order to avoid spurious splitting of the map along the pieces
13377 * of "mpa".
13379 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13380 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13382 int n;
13383 isl_pw_multi_aff *pma;
13385 if (!map || !mpa)
13386 goto error;
13388 n = isl_map_dim(map, type);
13389 if (!isl_map_involves_dims(map, type, 0, n)) {
13390 isl_space *space;
13391 isl_multi_aff *ma;
13393 space = isl_multi_pw_aff_get_space(mpa);
13394 isl_multi_pw_aff_free(mpa);
13395 ma = isl_multi_aff_zero(space);
13396 return isl_map_preimage_multi_aff(map, type, ma);
13399 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13400 return isl_map_preimage_pw_multi_aff(map, type, pma);
13401 error:
13402 isl_map_free(map);
13403 isl_multi_pw_aff_free(mpa);
13404 return NULL;
13407 /* Compute the preimage of "map" under the function represented by "mpa".
13408 * In other words, plug in "mpa" in the domain "map".
13409 * The result is a map that lives in the same space as "map",
13410 * except that domain space has been replaced by the domain space of "mpa".
13412 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13413 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13415 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13418 /* Compute the preimage of "set" by the function represented by "mpa".
13419 * In other words, plug in "mpa" in "set".
13421 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13422 __isl_take isl_multi_pw_aff *mpa)
13424 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13427 /* Return a copy of the equality constraints of "bset" as a matrix.
13429 __isl_give isl_mat *isl_basic_set_extract_equalities(
13430 __isl_keep isl_basic_set *bset)
13432 isl_ctx *ctx;
13433 unsigned total;
13435 if (!bset)
13436 return NULL;
13438 ctx = isl_basic_set_get_ctx(bset);
13439 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13440 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13443 /* Are the "n" "coefficients" starting at "first" of the integer division
13444 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13445 * to each other?
13446 * The "coefficient" at position 0 is the denominator.
13447 * The "coefficient" at position 1 is the constant term.
13449 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13450 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13451 unsigned first, unsigned n)
13453 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13454 return isl_bool_error;
13455 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13456 return isl_bool_error;
13457 return isl_seq_eq(bmap1->div[pos1] + first,
13458 bmap2->div[pos2] + first, n);
13461 /* Are the integer division expressions at position "pos1" in "bmap1" and
13462 * "pos2" in "bmap2" equal to each other, except that the constant terms
13463 * are different?
13465 isl_bool isl_basic_map_equal_div_expr_except_constant(
13466 __isl_keep isl_basic_map *bmap1, int pos1,
13467 __isl_keep isl_basic_map *bmap2, int pos2)
13469 isl_bool equal;
13470 unsigned total;
13472 if (!bmap1 || !bmap2)
13473 return isl_bool_error;
13474 total = isl_basic_map_total_dim(bmap1);
13475 if (total != isl_basic_map_total_dim(bmap2))
13476 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13477 "incomparable div expressions", return isl_bool_error);
13478 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13479 0, 1);
13480 if (equal < 0 || !equal)
13481 return equal;
13482 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13483 1, 1);
13484 if (equal < 0 || equal)
13485 return isl_bool_not(equal);
13486 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13487 2, total);
13490 /* Replace the numerator of the constant term of the integer division
13491 * expression at position "div" in "bmap" by "value".
13492 * The caller guarantees that this does not change the meaning
13493 * of the input.
13495 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13496 __isl_take isl_basic_map *bmap, int div, int value)
13498 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13499 return isl_basic_map_free(bmap);
13501 isl_int_set_si(bmap->div[div][1], value);
13503 return bmap;
13506 /* Is the point "inner" internal to inequality constraint "ineq"
13507 * of "bset"?
13508 * The point is considered to be internal to the inequality constraint,
13509 * if it strictly lies on the positive side of the inequality constraint,
13510 * or if it lies on the constraint and the constraint is lexico-positive.
13512 static isl_bool is_internal(__isl_keep isl_vec *inner,
13513 __isl_keep isl_basic_set *bset, int ineq)
13515 isl_ctx *ctx;
13516 int pos;
13517 unsigned total;
13519 if (!inner || !bset)
13520 return isl_bool_error;
13522 ctx = isl_basic_set_get_ctx(bset);
13523 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13524 &ctx->normalize_gcd);
13525 if (!isl_int_is_zero(ctx->normalize_gcd))
13526 return isl_int_is_nonneg(ctx->normalize_gcd);
13528 total = isl_basic_set_dim(bset, isl_dim_all);
13529 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13530 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13533 /* Tighten the inequality constraints of "bset" that are outward with respect
13534 * to the point "vec".
13535 * That is, tighten the constraints that are not satisfied by "vec".
13537 * "vec" is a point internal to some superset S of "bset" that is used
13538 * to make the subsets of S disjoint, by tightening one half of the constraints
13539 * that separate two subsets. In particular, the constraints of S
13540 * are all satisfied by "vec" and should not be tightened.
13541 * Of the internal constraints, those that have "vec" on the outside
13542 * are tightened. The shared facet is included in the adjacent subset
13543 * with the opposite constraint.
13544 * For constraints that saturate "vec", this criterion cannot be used
13545 * to determine which of the two sides should be tightened.
13546 * Instead, the sign of the first non-zero coefficient is used
13547 * to make this choice. Note that this second criterion is never used
13548 * on the constraints of S since "vec" is interior to "S".
13550 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13551 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13553 int j;
13555 bset = isl_basic_set_cow(bset);
13556 if (!bset)
13557 return NULL;
13558 for (j = 0; j < bset->n_ineq; ++j) {
13559 isl_bool internal;
13561 internal = is_internal(vec, bset, j);
13562 if (internal < 0)
13563 return isl_basic_set_free(bset);
13564 if (internal)
13565 continue;
13566 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13569 return bset;
13572 /* Replace the variables x of type "type" starting at "first" in "bmap"
13573 * by x' with x = M x' with M the matrix trans.
13574 * That is, replace the corresponding coefficients c by c M.
13576 * The transformation matrix should be a square matrix.
13578 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13579 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13580 __isl_take isl_mat *trans)
13582 unsigned pos;
13584 bmap = isl_basic_map_cow(bmap);
13585 if (!bmap || !trans)
13586 goto error;
13588 if (trans->n_row != trans->n_col)
13589 isl_die(trans->ctx, isl_error_invalid,
13590 "expecting square transformation matrix", goto error);
13591 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
13592 isl_die(trans->ctx, isl_error_invalid,
13593 "oversized transformation matrix", goto error);
13595 pos = isl_basic_map_offset(bmap, type) + first;
13597 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13598 isl_mat_copy(trans)) < 0)
13599 goto error;
13600 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13601 isl_mat_copy(trans)) < 0)
13602 goto error;
13603 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13604 isl_mat_copy(trans)) < 0)
13605 goto error;
13607 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
13608 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13610 isl_mat_free(trans);
13611 return bmap;
13612 error:
13613 isl_mat_free(trans);
13614 isl_basic_map_free(bmap);
13615 return NULL;
13618 /* Replace the variables x of type "type" starting at "first" in "bset"
13619 * by x' with x = M x' with M the matrix trans.
13620 * That is, replace the corresponding coefficients c by c M.
13622 * The transformation matrix should be a square matrix.
13624 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13625 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13626 __isl_take isl_mat *trans)
13628 return isl_basic_map_transform_dims(bset, type, first, trans);