add isl_basic_set_n_equality
[isl.git] / isl_map.c
blob9e83931f9a2b0b23d942bf475ec88401ac38642c
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>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
56 switch (type) {
57 case isl_dim_param: return dim->nparam;
58 case isl_dim_in: return dim->n_in;
59 case isl_dim_out: return dim->n_out;
60 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
61 default: return 0;
65 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
67 switch (type) {
68 case isl_dim_param: return 1;
69 case isl_dim_in: return 1 + dim->nparam;
70 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
71 default: return 0;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
76 enum isl_dim_type type)
78 if (!bmap)
79 return 0;
80 switch (type) {
81 case isl_dim_cst: return 1;
82 case isl_dim_param:
83 case isl_dim_in:
84 case isl_dim_out: return isl_space_dim(bmap->dim, type);
85 case isl_dim_div: return bmap->n_div;
86 case isl_dim_all: return isl_basic_map_total_dim(bmap);
87 default: return 0;
91 /* Return the space of "map".
93 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
95 return map ? map->dim : NULL;
98 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
100 return map ? n(map->dim, type) : 0;
103 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
105 return set ? n(set->dim, type) : 0;
108 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
109 enum isl_dim_type type)
111 isl_space *space;
113 if (!bmap)
114 return 0;
116 space = bmap->dim;
117 switch (type) {
118 case isl_dim_cst: return 0;
119 case isl_dim_param: return 1;
120 case isl_dim_in: return 1 + space->nparam;
121 case isl_dim_out: return 1 + space->nparam + space->n_in;
122 case isl_dim_div: return 1 + space->nparam + space->n_in +
123 space->n_out;
124 default: return 0;
128 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
129 enum isl_dim_type type)
131 return isl_basic_map_offset(bset, type);
134 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
136 return pos(map->dim, type);
139 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
140 enum isl_dim_type type)
142 return isl_basic_map_dim(bset, type);
145 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
147 return isl_basic_set_dim(bset, isl_dim_set);
150 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
152 return isl_basic_set_dim(bset, isl_dim_param);
155 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
157 if (!bset)
158 return 0;
159 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
162 unsigned isl_set_n_dim(__isl_keep isl_set *set)
164 return isl_set_dim(set, isl_dim_set);
167 unsigned isl_set_n_param(__isl_keep isl_set *set)
169 return isl_set_dim(set, isl_dim_param);
172 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
174 return bmap ? bmap->dim->n_in : 0;
177 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
179 return bmap ? bmap->dim->n_out : 0;
182 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
184 return bmap ? bmap->dim->nparam : 0;
187 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
189 return bmap ? bmap->n_div : 0;
192 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
194 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
197 unsigned isl_map_n_in(__isl_keep const isl_map *map)
199 return map ? map->dim->n_in : 0;
202 unsigned isl_map_n_out(__isl_keep const isl_map *map)
204 return map ? map->dim->n_out : 0;
207 unsigned isl_map_n_param(__isl_keep const isl_map *map)
209 return map ? map->dim->nparam : 0;
212 /* Return the number of equality constraints in the description of "bmap".
213 * Return -1 on error.
215 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
217 if (!bmap)
218 return -1;
219 return bmap->n_eq;
222 /* Return the number of equality constraints in the description of "bset".
223 * Return -1 on error.
225 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
227 return isl_basic_map_n_equality(bset_to_bmap(bset));
230 /* Do "bmap1" and "bmap2" have the same parameters?
232 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
233 __isl_keep isl_basic_map *bmap2)
235 isl_space *space1, *space2;
237 space1 = isl_basic_map_peek_space(bmap1);
238 space2 = isl_basic_map_peek_space(bmap2);
239 return isl_space_has_equal_params(space1, space2);
242 /* Do "map1" and "map2" have the same parameters?
244 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
245 __isl_keep isl_map *map2)
247 isl_space *space1, *space2;
249 space1 = isl_map_peek_space(map1);
250 space2 = isl_map_peek_space(map2);
251 return isl_space_has_equal_params(space1, space2);
254 /* Do "map" and "set" have the same parameters?
256 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
257 __isl_keep isl_set *set)
259 return isl_map_has_equal_params(map, set_to_map(set));
262 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
263 __isl_keep isl_set *set)
265 isl_bool m;
266 if (!map || !set)
267 return isl_bool_error;
268 m = isl_map_has_equal_params(map, set_to_map(set));
269 if (m < 0 || !m)
270 return m;
271 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
272 set->dim, isl_dim_set);
275 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
276 __isl_keep isl_basic_set *bset)
278 isl_bool m;
279 if (!bmap || !bset)
280 return isl_bool_error;
281 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
282 if (m < 0 || !m)
283 return m;
284 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
285 bset->dim, isl_dim_set);
288 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
289 __isl_keep isl_set *set)
291 isl_bool m;
292 if (!map || !set)
293 return isl_bool_error;
294 m = isl_map_has_equal_params(map, set_to_map(set));
295 if (m < 0 || !m)
296 return m;
297 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
298 set->dim, isl_dim_set);
301 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
302 __isl_keep isl_basic_set *bset)
304 isl_bool m;
305 if (!bmap || !bset)
306 return isl_bool_error;
307 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
308 if (m < 0 || !m)
309 return m;
310 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
311 bset->dim, isl_dim_set);
314 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
316 return bmap ? bmap->ctx : NULL;
319 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
321 return bset ? bset->ctx : NULL;
324 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
326 return map ? map->ctx : NULL;
329 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
331 return set ? set->ctx : NULL;
334 /* Return the space of "bmap".
336 __isl_keep isl_space *isl_basic_map_peek_space(
337 __isl_keep const isl_basic_map *bmap)
339 return bmap ? bmap->dim : NULL;
342 /* Return the space of "bset".
344 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
346 return isl_basic_map_peek_space(bset_to_bmap(bset));
349 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
351 return isl_space_copy(isl_basic_map_peek_space(bmap));
354 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
356 return isl_basic_map_get_space(bset_to_bmap(bset));
359 /* Extract the divs in "bmap" as a matrix.
361 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
363 int i;
364 isl_ctx *ctx;
365 isl_mat *div;
366 unsigned total;
367 unsigned cols;
369 if (!bmap)
370 return NULL;
372 ctx = isl_basic_map_get_ctx(bmap);
373 total = isl_space_dim(bmap->dim, isl_dim_all);
374 cols = 1 + 1 + total + bmap->n_div;
375 div = isl_mat_alloc(ctx, bmap->n_div, cols);
376 if (!div)
377 return NULL;
379 for (i = 0; i < bmap->n_div; ++i)
380 isl_seq_cpy(div->row[i], bmap->div[i], cols);
382 return div;
385 /* Extract the divs in "bset" as a matrix.
387 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
389 return isl_basic_map_get_divs(bset);
392 __isl_give isl_local_space *isl_basic_map_get_local_space(
393 __isl_keep isl_basic_map *bmap)
395 isl_mat *div;
397 if (!bmap)
398 return NULL;
400 div = isl_basic_map_get_divs(bmap);
401 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
404 __isl_give isl_local_space *isl_basic_set_get_local_space(
405 __isl_keep isl_basic_set *bset)
407 return isl_basic_map_get_local_space(bset);
410 /* For each known div d = floor(f/m), add the constraints
412 * f - m d >= 0
413 * -(f-(m-1)) + m d >= 0
415 * Do not finalize the result.
417 static __isl_give isl_basic_map *add_known_div_constraints(
418 __isl_take isl_basic_map *bmap)
420 int i;
421 unsigned n_div;
423 if (!bmap)
424 return NULL;
425 n_div = isl_basic_map_dim(bmap, isl_dim_div);
426 if (n_div == 0)
427 return bmap;
428 bmap = isl_basic_map_cow(bmap);
429 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
430 if (!bmap)
431 return NULL;
432 for (i = 0; i < n_div; ++i) {
433 if (isl_int_is_zero(bmap->div[i][0]))
434 continue;
435 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
436 return isl_basic_map_free(bmap);
439 return bmap;
442 __isl_give isl_basic_map *isl_basic_map_from_local_space(
443 __isl_take isl_local_space *ls)
445 int i;
446 int n_div;
447 isl_basic_map *bmap;
449 if (!ls)
450 return NULL;
452 n_div = isl_local_space_dim(ls, isl_dim_div);
453 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
454 n_div, 0, 2 * n_div);
456 for (i = 0; i < n_div; ++i)
457 if (isl_basic_map_alloc_div(bmap) < 0)
458 goto error;
460 for (i = 0; i < n_div; ++i)
461 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
462 bmap = add_known_div_constraints(bmap);
464 isl_local_space_free(ls);
465 return bmap;
466 error:
467 isl_local_space_free(ls);
468 isl_basic_map_free(bmap);
469 return NULL;
472 __isl_give isl_basic_set *isl_basic_set_from_local_space(
473 __isl_take isl_local_space *ls)
475 return isl_basic_map_from_local_space(ls);
478 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
480 return isl_space_copy(isl_map_peek_space(map));
483 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
485 if (!set)
486 return NULL;
487 return isl_space_copy(set->dim);
490 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
491 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
493 bmap = isl_basic_map_cow(bmap);
494 if (!bmap)
495 return NULL;
496 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
497 if (!bmap->dim)
498 goto error;
499 bmap = isl_basic_map_finalize(bmap);
500 return bmap;
501 error:
502 isl_basic_map_free(bmap);
503 return NULL;
506 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
507 __isl_take isl_basic_set *bset, const char *s)
509 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
512 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
513 enum isl_dim_type type)
515 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
518 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
519 enum isl_dim_type type, const char *s)
521 int i;
523 map = isl_map_cow(map);
524 if (!map)
525 return NULL;
527 map->dim = isl_space_set_tuple_name(map->dim, type, s);
528 if (!map->dim)
529 goto error;
531 for (i = 0; i < map->n; ++i) {
532 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
533 if (!map->p[i])
534 goto error;
537 return map;
538 error:
539 isl_map_free(map);
540 return NULL;
543 /* Replace the identifier of the tuple of type "type" by "id".
545 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
546 __isl_take isl_basic_map *bmap,
547 enum isl_dim_type type, __isl_take isl_id *id)
549 bmap = isl_basic_map_cow(bmap);
550 if (!bmap)
551 goto error;
552 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
553 if (!bmap->dim)
554 return isl_basic_map_free(bmap);
555 bmap = isl_basic_map_finalize(bmap);
556 return bmap;
557 error:
558 isl_id_free(id);
559 return NULL;
562 /* Replace the identifier of the tuple by "id".
564 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
565 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
567 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
570 /* Does the input or output tuple have a name?
572 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
574 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
577 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
578 enum isl_dim_type type)
580 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
583 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
584 const char *s)
586 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
587 isl_dim_set, s));
590 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
591 enum isl_dim_type type, __isl_take isl_id *id)
593 map = isl_map_cow(map);
594 if (!map)
595 goto error;
597 map->dim = isl_space_set_tuple_id(map->dim, type, id);
599 return isl_map_reset_space(map, isl_space_copy(map->dim));
600 error:
601 isl_id_free(id);
602 return NULL;
605 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
606 __isl_take isl_id *id)
608 return isl_map_set_tuple_id(set, isl_dim_set, id);
611 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
612 enum isl_dim_type type)
614 map = isl_map_cow(map);
615 if (!map)
616 return NULL;
618 map->dim = isl_space_reset_tuple_id(map->dim, type);
620 return isl_map_reset_space(map, isl_space_copy(map->dim));
623 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
625 return isl_map_reset_tuple_id(set, isl_dim_set);
628 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
630 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
633 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
634 enum isl_dim_type type)
636 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
639 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
641 return isl_map_has_tuple_id(set, isl_dim_set);
644 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
646 return isl_map_get_tuple_id(set, isl_dim_set);
649 /* Does the set tuple have a name?
651 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
653 if (!set)
654 return isl_bool_error;
655 return isl_space_has_tuple_name(set->dim, isl_dim_set);
659 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
661 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
664 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
666 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
669 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
670 enum isl_dim_type type, unsigned pos)
672 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
675 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
676 enum isl_dim_type type, unsigned pos)
678 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
681 /* Does the given dimension have a name?
683 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
684 enum isl_dim_type type, unsigned pos)
686 if (!map)
687 return isl_bool_error;
688 return isl_space_has_dim_name(map->dim, type, pos);
691 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
692 enum isl_dim_type type, unsigned pos)
694 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
697 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
698 enum isl_dim_type type, unsigned pos)
700 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
703 /* Does the given dimension have a name?
705 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
706 enum isl_dim_type type, unsigned pos)
708 if (!set)
709 return isl_bool_error;
710 return isl_space_has_dim_name(set->dim, type, pos);
713 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
714 __isl_take isl_basic_map *bmap,
715 enum isl_dim_type type, unsigned pos, const char *s)
717 bmap = isl_basic_map_cow(bmap);
718 if (!bmap)
719 return NULL;
720 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
721 if (!bmap->dim)
722 goto error;
723 return isl_basic_map_finalize(bmap);
724 error:
725 isl_basic_map_free(bmap);
726 return NULL;
729 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
730 enum isl_dim_type type, unsigned pos, const char *s)
732 int i;
734 map = isl_map_cow(map);
735 if (!map)
736 return NULL;
738 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
739 if (!map->dim)
740 goto error;
742 for (i = 0; i < map->n; ++i) {
743 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
744 if (!map->p[i])
745 goto error;
748 return map;
749 error:
750 isl_map_free(map);
751 return NULL;
754 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
755 __isl_take isl_basic_set *bset,
756 enum isl_dim_type type, unsigned pos, const char *s)
758 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
759 type, pos, s));
762 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
763 enum isl_dim_type type, unsigned pos, const char *s)
765 return set_from_map(isl_map_set_dim_name(set_to_map(set),
766 type, pos, s));
769 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
770 enum isl_dim_type type, unsigned pos)
772 if (!bmap)
773 return isl_bool_error;
774 return isl_space_has_dim_id(bmap->dim, type, pos);
777 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
778 enum isl_dim_type type, unsigned pos)
780 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
783 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
784 enum isl_dim_type type, unsigned pos)
786 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
789 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
790 enum isl_dim_type type, unsigned pos)
792 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
795 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
796 enum isl_dim_type type, unsigned pos)
798 return isl_map_has_dim_id(set, type, pos);
801 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
802 enum isl_dim_type type, unsigned pos)
804 return isl_map_get_dim_id(set, type, pos);
807 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
808 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
810 map = isl_map_cow(map);
811 if (!map)
812 goto error;
814 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
816 return isl_map_reset_space(map, isl_space_copy(map->dim));
817 error:
818 isl_id_free(id);
819 return NULL;
822 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
823 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
825 return isl_map_set_dim_id(set, type, pos, id);
828 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
829 __isl_keep isl_id *id)
831 if (!map)
832 return -1;
833 return isl_space_find_dim_by_id(map->dim, type, id);
836 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
837 __isl_keep isl_id *id)
839 return isl_map_find_dim_by_id(set, type, id);
842 /* Return the position of the dimension of the given type and name
843 * in "bmap".
844 * Return -1 if no such dimension can be found.
846 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
847 enum isl_dim_type type, const char *name)
849 if (!bmap)
850 return -1;
851 return isl_space_find_dim_by_name(bmap->dim, type, name);
854 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
855 const char *name)
857 if (!map)
858 return -1;
859 return isl_space_find_dim_by_name(map->dim, type, name);
862 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
863 const char *name)
865 return isl_map_find_dim_by_name(set, type, name);
868 /* Check whether equality i of bset is a pure stride constraint
869 * on a single dimension, i.e., of the form
871 * v = k e
873 * with k a constant and e an existentially quantified variable.
875 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
877 unsigned nparam;
878 unsigned d;
879 unsigned n_div;
880 int pos1;
881 int pos2;
883 if (!bset)
884 return isl_bool_error;
886 if (!isl_int_is_zero(bset->eq[i][0]))
887 return isl_bool_false;
889 nparam = isl_basic_set_dim(bset, isl_dim_param);
890 d = isl_basic_set_dim(bset, isl_dim_set);
891 n_div = isl_basic_set_dim(bset, isl_dim_div);
893 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
894 return isl_bool_false;
895 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
896 if (pos1 == -1)
897 return isl_bool_false;
898 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
899 d - pos1 - 1) != -1)
900 return isl_bool_false;
902 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
903 if (pos2 == -1)
904 return isl_bool_false;
905 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
906 n_div - pos2 - 1) != -1)
907 return isl_bool_false;
908 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
909 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
910 return isl_bool_false;
912 return isl_bool_true;
915 /* Reset the user pointer on all identifiers of parameters and tuples
916 * of the space of "map".
918 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
920 isl_space *space;
922 space = isl_map_get_space(map);
923 space = isl_space_reset_user(space);
924 map = isl_map_reset_space(map, space);
926 return map;
929 /* Reset the user pointer on all identifiers of parameters and tuples
930 * of the space of "set".
932 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
934 return isl_map_reset_user(set);
937 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
939 if (!bmap)
940 return isl_bool_error;
941 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
944 /* Has "map" been marked as a rational map?
945 * In particular, have all basic maps in "map" been marked this way?
946 * An empty map is not considered to be rational.
947 * Maps where only some of the basic maps are marked rational
948 * are not allowed.
950 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
952 int i;
953 isl_bool rational;
955 if (!map)
956 return isl_bool_error;
957 if (map->n == 0)
958 return isl_bool_false;
959 rational = isl_basic_map_is_rational(map->p[0]);
960 if (rational < 0)
961 return rational;
962 for (i = 1; i < map->n; ++i) {
963 isl_bool rational_i;
965 rational_i = isl_basic_map_is_rational(map->p[i]);
966 if (rational_i < 0)
967 return rational_i;
968 if (rational != rational_i)
969 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
970 "mixed rational and integer basic maps "
971 "not supported", return isl_bool_error);
974 return rational;
977 /* Has "set" been marked as a rational set?
978 * In particular, have all basic set in "set" been marked this way?
979 * An empty set is not considered to be rational.
980 * Sets where only some of the basic sets are marked rational
981 * are not allowed.
983 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
985 return isl_map_is_rational(set);
988 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
990 return isl_basic_map_is_rational(bset);
993 /* Does "bmap" contain any rational points?
995 * If "bmap" has an equality for each dimension, equating the dimension
996 * to an integer constant, then it has no rational points, even if it
997 * is marked as rational.
999 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1001 isl_bool has_rational = isl_bool_true;
1002 unsigned total;
1004 if (!bmap)
1005 return isl_bool_error;
1006 if (isl_basic_map_plain_is_empty(bmap))
1007 return isl_bool_false;
1008 if (!isl_basic_map_is_rational(bmap))
1009 return isl_bool_false;
1010 bmap = isl_basic_map_copy(bmap);
1011 bmap = isl_basic_map_implicit_equalities(bmap);
1012 if (!bmap)
1013 return isl_bool_error;
1014 total = isl_basic_map_total_dim(bmap);
1015 if (bmap->n_eq == total) {
1016 int i, j;
1017 for (i = 0; i < bmap->n_eq; ++i) {
1018 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1019 if (j < 0)
1020 break;
1021 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1022 !isl_int_is_negone(bmap->eq[i][1 + j]))
1023 break;
1024 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1025 total - j - 1);
1026 if (j >= 0)
1027 break;
1029 if (i == bmap->n_eq)
1030 has_rational = isl_bool_false;
1032 isl_basic_map_free(bmap);
1034 return has_rational;
1037 /* Does "map" contain any rational points?
1039 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1041 int i;
1042 isl_bool has_rational;
1044 if (!map)
1045 return isl_bool_error;
1046 for (i = 0; i < map->n; ++i) {
1047 has_rational = isl_basic_map_has_rational(map->p[i]);
1048 if (has_rational < 0 || has_rational)
1049 return has_rational;
1051 return isl_bool_false;
1054 /* Does "set" contain any rational points?
1056 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1058 return isl_map_has_rational(set);
1061 /* Is this basic set a parameter domain?
1063 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1065 if (!bset)
1066 return isl_bool_error;
1067 return isl_space_is_params(bset->dim);
1070 /* Is this set a parameter domain?
1072 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1074 if (!set)
1075 return isl_bool_error;
1076 return isl_space_is_params(set->dim);
1079 /* Is this map actually a parameter domain?
1080 * Users should never call this function. Outside of isl,
1081 * a map can never be a parameter domain.
1083 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1085 if (!map)
1086 return isl_bool_error;
1087 return isl_space_is_params(map->dim);
1090 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
1091 struct isl_basic_map *bmap, unsigned extra,
1092 unsigned n_eq, unsigned n_ineq)
1094 int i;
1095 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1097 bmap->ctx = ctx;
1098 isl_ctx_ref(ctx);
1100 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1101 if (isl_blk_is_error(bmap->block))
1102 goto error;
1104 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1105 if ((n_ineq + n_eq) && !bmap->ineq)
1106 goto error;
1108 if (extra == 0) {
1109 bmap->block2 = isl_blk_empty();
1110 bmap->div = NULL;
1111 } else {
1112 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1113 if (isl_blk_is_error(bmap->block2))
1114 goto error;
1116 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1117 if (!bmap->div)
1118 goto error;
1121 for (i = 0; i < n_ineq + n_eq; ++i)
1122 bmap->ineq[i] = bmap->block.data + i * row_size;
1124 for (i = 0; i < extra; ++i)
1125 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1127 bmap->ref = 1;
1128 bmap->flags = 0;
1129 bmap->c_size = n_eq + n_ineq;
1130 bmap->eq = bmap->ineq + n_ineq;
1131 bmap->extra = extra;
1132 bmap->n_eq = 0;
1133 bmap->n_ineq = 0;
1134 bmap->n_div = 0;
1135 bmap->sample = NULL;
1137 return bmap;
1138 error:
1139 isl_basic_map_free(bmap);
1140 return NULL;
1143 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1144 unsigned nparam, unsigned dim, unsigned extra,
1145 unsigned n_eq, unsigned n_ineq)
1147 struct isl_basic_map *bmap;
1148 isl_space *space;
1150 space = isl_space_set_alloc(ctx, nparam, dim);
1151 if (!space)
1152 return NULL;
1154 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1155 return bset_from_bmap(bmap);
1158 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1159 unsigned extra, unsigned n_eq, unsigned n_ineq)
1161 struct isl_basic_map *bmap;
1162 if (!dim)
1163 return NULL;
1164 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1165 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1166 return bset_from_bmap(bmap);
1167 error:
1168 isl_space_free(dim);
1169 return NULL;
1172 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1173 unsigned extra, unsigned n_eq, unsigned n_ineq)
1175 struct isl_basic_map *bmap;
1177 if (!dim)
1178 return NULL;
1179 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1180 if (!bmap)
1181 goto error;
1182 bmap->dim = dim;
1184 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1185 error:
1186 isl_space_free(dim);
1187 return NULL;
1190 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1191 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1192 unsigned n_eq, unsigned n_ineq)
1194 struct isl_basic_map *bmap;
1195 isl_space *dim;
1197 dim = isl_space_alloc(ctx, nparam, in, out);
1198 if (!dim)
1199 return NULL;
1201 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1202 return bmap;
1205 static void dup_constraints(
1206 struct isl_basic_map *dst, struct isl_basic_map *src)
1208 int i;
1209 unsigned total = isl_basic_map_total_dim(src);
1211 for (i = 0; i < src->n_eq; ++i) {
1212 int j = isl_basic_map_alloc_equality(dst);
1213 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1216 for (i = 0; i < src->n_ineq; ++i) {
1217 int j = isl_basic_map_alloc_inequality(dst);
1218 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1221 for (i = 0; i < src->n_div; ++i) {
1222 int j = isl_basic_map_alloc_div(dst);
1223 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1225 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1228 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1230 struct isl_basic_map *dup;
1232 if (!bmap)
1233 return NULL;
1234 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1235 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1236 if (!dup)
1237 return NULL;
1238 dup_constraints(dup, bmap);
1239 dup->flags = bmap->flags;
1240 dup->sample = isl_vec_copy(bmap->sample);
1241 return dup;
1244 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1246 struct isl_basic_map *dup;
1248 dup = isl_basic_map_dup(bset_to_bmap(bset));
1249 return bset_from_bmap(dup);
1252 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1254 if (!bset)
1255 return NULL;
1257 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1258 bset->ref++;
1259 return bset;
1261 return isl_basic_set_dup(bset);
1264 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1266 if (!set)
1267 return NULL;
1269 set->ref++;
1270 return set;
1273 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1275 if (!bmap)
1276 return NULL;
1278 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1279 bmap->ref++;
1280 return bmap;
1282 bmap = isl_basic_map_dup(bmap);
1283 if (bmap)
1284 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1285 return bmap;
1288 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1290 if (!map)
1291 return NULL;
1293 map->ref++;
1294 return map;
1297 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1299 if (!bmap)
1300 return NULL;
1302 if (--bmap->ref > 0)
1303 return NULL;
1305 isl_ctx_deref(bmap->ctx);
1306 free(bmap->div);
1307 isl_blk_free(bmap->ctx, bmap->block2);
1308 free(bmap->ineq);
1309 isl_blk_free(bmap->ctx, bmap->block);
1310 isl_vec_free(bmap->sample);
1311 isl_space_free(bmap->dim);
1312 free(bmap);
1314 return NULL;
1317 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1319 return isl_basic_map_free(bset_to_bmap(bset));
1322 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1324 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1327 /* Check that "map" has only named parameters, reporting an error
1328 * if it does not.
1330 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1332 return isl_space_check_named_params(isl_map_peek_space(map));
1335 /* Check that "bmap1" and "bmap2" have the same parameters,
1336 * reporting an error if they do not.
1338 static isl_stat isl_basic_map_check_equal_params(
1339 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1341 isl_bool match;
1343 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1344 if (match < 0)
1345 return isl_stat_error;
1346 if (!match)
1347 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1348 "parameters don't match", return isl_stat_error);
1349 return isl_stat_ok;
1352 __isl_give isl_map *isl_map_align_params_map_map_and(
1353 __isl_take isl_map *map1, __isl_take isl_map *map2,
1354 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1355 __isl_take isl_map *map2))
1357 if (!map1 || !map2)
1358 goto error;
1359 if (isl_map_has_equal_params(map1, map2))
1360 return fn(map1, map2);
1361 if (isl_map_check_named_params(map1) < 0)
1362 goto error;
1363 if (isl_map_check_named_params(map2) < 0)
1364 goto error;
1365 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1366 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1367 return fn(map1, map2);
1368 error:
1369 isl_map_free(map1);
1370 isl_map_free(map2);
1371 return NULL;
1374 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1375 __isl_keep isl_map *map2,
1376 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1378 isl_bool r;
1380 if (!map1 || !map2)
1381 return isl_bool_error;
1382 if (isl_map_has_equal_params(map1, map2))
1383 return fn(map1, map2);
1384 if (isl_map_check_named_params(map1) < 0)
1385 return isl_bool_error;
1386 if (isl_map_check_named_params(map2) < 0)
1387 return isl_bool_error;
1388 map1 = isl_map_copy(map1);
1389 map2 = isl_map_copy(map2);
1390 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1391 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1392 r = fn(map1, map2);
1393 isl_map_free(map1);
1394 isl_map_free(map2);
1395 return r;
1398 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1400 struct isl_ctx *ctx;
1401 if (!bmap)
1402 return -1;
1403 ctx = bmap->ctx;
1404 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1405 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1406 return -1);
1407 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1408 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1409 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1410 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1411 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1412 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1413 isl_int *t;
1414 int j = isl_basic_map_alloc_inequality(bmap);
1415 if (j < 0)
1416 return -1;
1417 t = bmap->ineq[j];
1418 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1419 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1420 bmap->eq[-1] = t;
1421 bmap->n_eq++;
1422 bmap->n_ineq--;
1423 bmap->eq--;
1424 return 0;
1426 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1427 bmap->extra - bmap->n_div);
1428 return bmap->n_eq++;
1431 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1433 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1436 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1438 if (!bmap)
1439 return -1;
1440 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1441 bmap->n_eq -= n;
1442 return 0;
1445 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1447 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1450 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1452 isl_int *t;
1453 if (!bmap)
1454 return -1;
1455 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1457 if (pos != bmap->n_eq - 1) {
1458 t = bmap->eq[pos];
1459 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1460 bmap->eq[bmap->n_eq - 1] = t;
1462 bmap->n_eq--;
1463 return 0;
1466 /* Turn inequality "pos" of "bmap" into an equality.
1468 * In particular, we move the inequality in front of the equalities
1469 * and move the last inequality in the position of the moved inequality.
1470 * Note that isl_tab_make_equalities_explicit depends on this particular
1471 * change in the ordering of the constraints.
1473 void isl_basic_map_inequality_to_equality(
1474 struct isl_basic_map *bmap, unsigned pos)
1476 isl_int *t;
1478 t = bmap->ineq[pos];
1479 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1480 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1481 bmap->eq[-1] = t;
1482 bmap->n_eq++;
1483 bmap->n_ineq--;
1484 bmap->eq--;
1485 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1486 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1487 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1488 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1491 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1493 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1496 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1498 struct isl_ctx *ctx;
1499 if (!bmap)
1500 return -1;
1501 ctx = bmap->ctx;
1502 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1503 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1504 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1505 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1506 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1507 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1508 1 + isl_basic_map_total_dim(bmap),
1509 bmap->extra - bmap->n_div);
1510 return bmap->n_ineq++;
1513 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1515 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1518 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1520 if (!bmap)
1521 return -1;
1522 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1523 bmap->n_ineq -= n;
1524 return 0;
1527 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1529 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1532 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1534 isl_int *t;
1535 if (!bmap)
1536 return -1;
1537 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1539 if (pos != bmap->n_ineq - 1) {
1540 t = bmap->ineq[pos];
1541 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1542 bmap->ineq[bmap->n_ineq - 1] = t;
1543 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1545 bmap->n_ineq--;
1546 return 0;
1549 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1551 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1554 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1555 isl_int *eq)
1557 int k;
1559 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1560 if (!bmap)
1561 return NULL;
1562 k = isl_basic_map_alloc_equality(bmap);
1563 if (k < 0)
1564 goto error;
1565 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1566 return bmap;
1567 error:
1568 isl_basic_map_free(bmap);
1569 return NULL;
1572 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1573 isl_int *eq)
1575 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1578 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1579 isl_int *ineq)
1581 int k;
1583 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1584 if (!bmap)
1585 return NULL;
1586 k = isl_basic_map_alloc_inequality(bmap);
1587 if (k < 0)
1588 goto error;
1589 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1590 return bmap;
1591 error:
1592 isl_basic_map_free(bmap);
1593 return NULL;
1596 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1597 isl_int *ineq)
1599 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1602 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1604 if (!bmap)
1605 return -1;
1606 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1607 isl_seq_clr(bmap->div[bmap->n_div] +
1608 1 + 1 + isl_basic_map_total_dim(bmap),
1609 bmap->extra - bmap->n_div);
1610 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1611 return bmap->n_div++;
1614 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1616 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1619 /* Check that there are "n" dimensions of type "type" starting at "first"
1620 * in "bmap".
1622 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1623 enum isl_dim_type type, unsigned first, unsigned n)
1625 unsigned dim;
1627 if (!bmap)
1628 return isl_stat_error;
1629 dim = isl_basic_map_dim(bmap, type);
1630 if (first + n > dim || first + n < first)
1631 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1632 "position or range out of bounds",
1633 return isl_stat_error);
1634 return isl_stat_ok;
1637 /* Insert an extra integer division, prescribed by "div", to "bmap"
1638 * at (integer division) position "pos".
1640 * The integer division is first added at the end and then moved
1641 * into the right position.
1643 __isl_give isl_basic_map *isl_basic_map_insert_div(
1644 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1646 int i, k;
1648 bmap = isl_basic_map_cow(bmap);
1649 if (!bmap || !div)
1650 return isl_basic_map_free(bmap);
1652 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1653 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1654 "unexpected size", return isl_basic_map_free(bmap));
1655 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1656 return isl_basic_map_free(bmap);
1658 bmap = isl_basic_map_extend_space(bmap,
1659 isl_basic_map_get_space(bmap), 1, 0, 2);
1660 k = isl_basic_map_alloc_div(bmap);
1661 if (k < 0)
1662 return isl_basic_map_free(bmap);
1663 isl_seq_cpy(bmap->div[k], div->el, div->size);
1664 isl_int_set_si(bmap->div[k][div->size], 0);
1666 for (i = k; i > pos; --i)
1667 isl_basic_map_swap_div(bmap, i, i - 1);
1669 return bmap;
1672 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1674 if (!bmap)
1675 return isl_stat_error;
1676 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1677 bmap->n_div -= n;
1678 return isl_stat_ok;
1681 /* Copy constraint from src to dst, putting the vars of src at offset
1682 * dim_off in dst and the divs of src at offset div_off in dst.
1683 * If both sets are actually map, then dim_off applies to the input
1684 * variables.
1686 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1687 struct isl_basic_map *src_map, isl_int *src,
1688 unsigned in_off, unsigned out_off, unsigned div_off)
1690 unsigned src_nparam = isl_basic_map_dim(src_map, isl_dim_param);
1691 unsigned dst_nparam = isl_basic_map_dim(dst_map, isl_dim_param);
1692 unsigned src_in = isl_basic_map_dim(src_map, isl_dim_in);
1693 unsigned dst_in = isl_basic_map_dim(dst_map, isl_dim_in);
1694 unsigned src_out = isl_basic_map_dim(src_map, isl_dim_out);
1695 unsigned dst_out = isl_basic_map_dim(dst_map, isl_dim_out);
1696 isl_int_set(dst[0], src[0]);
1697 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1698 if (dst_nparam > src_nparam)
1699 isl_seq_clr(dst+1+src_nparam,
1700 dst_nparam - src_nparam);
1701 isl_seq_clr(dst+1+dst_nparam, in_off);
1702 isl_seq_cpy(dst+1+dst_nparam+in_off,
1703 src+1+src_nparam,
1704 isl_min(dst_in-in_off, src_in));
1705 if (dst_in-in_off > src_in)
1706 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1707 dst_in - in_off - src_in);
1708 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1709 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1710 src+1+src_nparam+src_in,
1711 isl_min(dst_out-out_off, src_out));
1712 if (dst_out-out_off > src_out)
1713 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1714 dst_out - out_off - src_out);
1715 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1716 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1717 src+1+src_nparam+src_in+src_out,
1718 isl_min(dst_map->extra-div_off, src_map->n_div));
1719 if (dst_map->n_div-div_off > src_map->n_div)
1720 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1721 div_off+src_map->n_div,
1722 dst_map->n_div - div_off - src_map->n_div);
1725 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1726 struct isl_basic_map *src_map, isl_int *src,
1727 unsigned in_off, unsigned out_off, unsigned div_off)
1729 isl_int_set(dst[0], src[0]);
1730 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1733 static __isl_give isl_basic_map *add_constraints(
1734 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1735 unsigned i_pos, unsigned o_pos)
1737 int i;
1738 unsigned div_off;
1740 if (!bmap1 || !bmap2)
1741 goto error;
1743 div_off = bmap1->n_div;
1745 for (i = 0; i < bmap2->n_eq; ++i) {
1746 int i1 = isl_basic_map_alloc_equality(bmap1);
1747 if (i1 < 0)
1748 goto error;
1749 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1750 i_pos, o_pos, div_off);
1753 for (i = 0; i < bmap2->n_ineq; ++i) {
1754 int i1 = isl_basic_map_alloc_inequality(bmap1);
1755 if (i1 < 0)
1756 goto error;
1757 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1758 i_pos, o_pos, div_off);
1761 for (i = 0; i < bmap2->n_div; ++i) {
1762 int i1 = isl_basic_map_alloc_div(bmap1);
1763 if (i1 < 0)
1764 goto error;
1765 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1766 i_pos, o_pos, div_off);
1769 isl_basic_map_free(bmap2);
1771 return bmap1;
1773 error:
1774 isl_basic_map_free(bmap1);
1775 isl_basic_map_free(bmap2);
1776 return NULL;
1779 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1780 struct isl_basic_set *bset2, unsigned pos)
1782 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1783 bset_to_bmap(bset2), 0, pos));
1786 __isl_give isl_basic_map *isl_basic_map_extend_space(
1787 __isl_take isl_basic_map *base, __isl_take isl_space *dim,
1788 unsigned extra, unsigned n_eq, unsigned n_ineq)
1790 struct isl_basic_map *ext;
1791 unsigned flags;
1792 int dims_ok;
1794 if (!dim)
1795 goto error;
1797 if (!base)
1798 goto error;
1800 dims_ok = isl_space_is_equal(base->dim, dim) &&
1801 base->extra >= base->n_div + extra;
1803 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1804 room_for_ineq(base, n_ineq)) {
1805 isl_space_free(dim);
1806 return base;
1809 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1810 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1811 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1812 extra += base->extra;
1813 n_eq += base->n_eq;
1814 n_ineq += base->n_ineq;
1816 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1817 dim = NULL;
1818 if (!ext)
1819 goto error;
1821 if (dims_ok)
1822 ext->sample = isl_vec_copy(base->sample);
1823 flags = base->flags;
1824 ext = add_constraints(ext, base, 0, 0);
1825 if (ext) {
1826 ext->flags = flags;
1827 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1830 return ext;
1832 error:
1833 isl_space_free(dim);
1834 isl_basic_map_free(base);
1835 return NULL;
1838 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1839 __isl_take isl_space *dim, unsigned extra,
1840 unsigned n_eq, unsigned n_ineq)
1842 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1843 dim, extra, n_eq, n_ineq));
1846 struct isl_basic_map *isl_basic_map_extend_constraints(
1847 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1849 if (!base)
1850 return NULL;
1851 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1852 0, n_eq, n_ineq);
1855 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1856 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1857 unsigned n_eq, unsigned n_ineq)
1859 struct isl_basic_map *bmap;
1860 isl_space *dim;
1862 if (!base)
1863 return NULL;
1864 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1865 if (!dim)
1866 goto error;
1868 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1869 return bmap;
1870 error:
1871 isl_basic_map_free(base);
1872 return NULL;
1875 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1876 unsigned nparam, unsigned dim, unsigned extra,
1877 unsigned n_eq, unsigned n_ineq)
1879 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1880 nparam, 0, dim, extra, n_eq, n_ineq));
1883 struct isl_basic_set *isl_basic_set_extend_constraints(
1884 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1886 isl_basic_map *bmap = bset_to_bmap(base);
1887 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1888 return bset_from_bmap(bmap);
1891 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1893 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1896 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1898 if (!bmap)
1899 return NULL;
1901 if (bmap->ref > 1) {
1902 bmap->ref--;
1903 bmap = isl_basic_map_dup(bmap);
1905 if (bmap) {
1906 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1907 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1909 return bmap;
1912 /* Clear all cached information in "map", either because it is about
1913 * to be modified or because it is being freed.
1914 * Always return the same pointer that is passed in.
1915 * This is needed for the use in isl_map_free.
1917 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1919 isl_basic_map_free(map->cached_simple_hull[0]);
1920 isl_basic_map_free(map->cached_simple_hull[1]);
1921 map->cached_simple_hull[0] = NULL;
1922 map->cached_simple_hull[1] = NULL;
1923 return map;
1926 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1928 return isl_map_cow(set);
1931 /* Return an isl_map that is equal to "map" and that has only
1932 * a single reference.
1934 * If the original input already has only one reference, then
1935 * simply return it, but clear all cached information, since
1936 * it may be rendered invalid by the operations that will be
1937 * performed on the result.
1939 * Otherwise, create a duplicate (without any cached information).
1941 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1943 if (!map)
1944 return NULL;
1946 if (map->ref == 1)
1947 return clear_caches(map);
1948 map->ref--;
1949 return isl_map_dup(map);
1952 static void swap_vars(struct isl_blk blk, isl_int *a,
1953 unsigned a_len, unsigned b_len)
1955 isl_seq_cpy(blk.data, a+a_len, b_len);
1956 isl_seq_cpy(blk.data+b_len, a, a_len);
1957 isl_seq_cpy(a, blk.data, b_len+a_len);
1960 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1961 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1963 int i;
1964 struct isl_blk blk;
1966 if (!bmap)
1967 goto error;
1969 isl_assert(bmap->ctx,
1970 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1972 if (n1 == 0 || n2 == 0)
1973 return bmap;
1975 bmap = isl_basic_map_cow(bmap);
1976 if (!bmap)
1977 return NULL;
1979 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1980 if (isl_blk_is_error(blk))
1981 goto error;
1983 for (i = 0; i < bmap->n_eq; ++i)
1984 swap_vars(blk,
1985 bmap->eq[i] + pos, n1, n2);
1987 for (i = 0; i < bmap->n_ineq; ++i)
1988 swap_vars(blk,
1989 bmap->ineq[i] + pos, n1, n2);
1991 for (i = 0; i < bmap->n_div; ++i)
1992 swap_vars(blk,
1993 bmap->div[i]+1 + pos, n1, n2);
1995 isl_blk_free(bmap->ctx, blk);
1997 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1998 bmap = isl_basic_map_gauss(bmap, NULL);
1999 return isl_basic_map_finalize(bmap);
2000 error:
2001 isl_basic_map_free(bmap);
2002 return NULL;
2005 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2006 __isl_take isl_basic_map *bmap)
2008 int i = 0;
2009 unsigned total;
2010 if (!bmap)
2011 goto error;
2012 total = isl_basic_map_total_dim(bmap);
2013 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2014 return isl_basic_map_free(bmap);
2015 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2016 if (bmap->n_eq > 0)
2017 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2018 else {
2019 i = isl_basic_map_alloc_equality(bmap);
2020 if (i < 0)
2021 goto error;
2023 isl_int_set_si(bmap->eq[i][0], 1);
2024 isl_seq_clr(bmap->eq[i]+1, total);
2025 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2026 isl_vec_free(bmap->sample);
2027 bmap->sample = NULL;
2028 return isl_basic_map_finalize(bmap);
2029 error:
2030 isl_basic_map_free(bmap);
2031 return NULL;
2034 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
2036 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2039 __isl_give isl_basic_map *isl_basic_map_set_rational(
2040 __isl_take isl_basic_map *bmap)
2042 if (!bmap)
2043 return NULL;
2045 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2046 return bmap;
2048 bmap = isl_basic_map_cow(bmap);
2049 if (!bmap)
2050 return NULL;
2052 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2054 return isl_basic_map_finalize(bmap);
2057 __isl_give isl_basic_set *isl_basic_set_set_rational(
2058 __isl_take isl_basic_set *bset)
2060 return isl_basic_map_set_rational(bset);
2063 __isl_give isl_basic_set *isl_basic_set_set_integral(
2064 __isl_take isl_basic_set *bset)
2066 if (!bset)
2067 return NULL;
2069 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2070 return bset;
2072 bset = isl_basic_set_cow(bset);
2073 if (!bset)
2074 return NULL;
2076 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2078 return isl_basic_set_finalize(bset);
2081 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2083 int i;
2085 map = isl_map_cow(map);
2086 if (!map)
2087 return NULL;
2088 for (i = 0; i < map->n; ++i) {
2089 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2090 if (!map->p[i])
2091 goto error;
2093 return map;
2094 error:
2095 isl_map_free(map);
2096 return NULL;
2099 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2101 return isl_map_set_rational(set);
2104 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2105 * of "bmap").
2107 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2109 isl_int *t = bmap->div[a];
2110 bmap->div[a] = bmap->div[b];
2111 bmap->div[b] = t;
2114 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2115 * div definitions accordingly.
2117 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2119 int i;
2120 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2122 swap_div(bmap, a, b);
2124 for (i = 0; i < bmap->n_eq; ++i)
2125 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2127 for (i = 0; i < bmap->n_ineq; ++i)
2128 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2130 for (i = 0; i < bmap->n_div; ++i)
2131 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2132 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2135 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
2136 * div definitions accordingly.
2138 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
2140 isl_basic_map_swap_div(bset, a, b);
2143 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2145 isl_seq_cpy(c, c + n, rem);
2146 isl_seq_clr(c + rem, n);
2149 /* Drop n dimensions starting at first.
2151 * In principle, this frees up some extra variables as the number
2152 * of columns remains constant, but we would have to extend
2153 * the div array too as the number of rows in this array is assumed
2154 * to be equal to extra.
2156 struct isl_basic_set *isl_basic_set_drop_dims(
2157 struct isl_basic_set *bset, unsigned first, unsigned n)
2159 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2162 /* Move "n" divs starting at "first" to the end of the list of divs.
2164 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2165 unsigned first, unsigned n)
2167 isl_int **div;
2168 int i;
2170 if (first + n == bmap->n_div)
2171 return bmap;
2173 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2174 if (!div)
2175 goto error;
2176 for (i = 0; i < n; ++i)
2177 div[i] = bmap->div[first + i];
2178 for (i = 0; i < bmap->n_div - first - n; ++i)
2179 bmap->div[first + i] = bmap->div[first + n + i];
2180 for (i = 0; i < n; ++i)
2181 bmap->div[bmap->n_div - n + i] = div[i];
2182 free(div);
2183 return bmap;
2184 error:
2185 isl_basic_map_free(bmap);
2186 return NULL;
2189 /* Drop "n" dimensions of type "type" starting at "first".
2191 * In principle, this frees up some extra variables as the number
2192 * of columns remains constant, but we would have to extend
2193 * the div array too as the number of rows in this array is assumed
2194 * to be equal to extra.
2196 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2197 enum isl_dim_type type, unsigned first, unsigned n)
2199 int i;
2200 unsigned dim;
2201 unsigned offset;
2202 unsigned left;
2204 if (!bmap)
2205 goto error;
2207 dim = isl_basic_map_dim(bmap, type);
2208 isl_assert(bmap->ctx, first + n <= dim, goto error);
2210 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2211 return bmap;
2213 bmap = isl_basic_map_cow(bmap);
2214 if (!bmap)
2215 return NULL;
2217 offset = isl_basic_map_offset(bmap, type) + first;
2218 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2219 for (i = 0; i < bmap->n_eq; ++i)
2220 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2222 for (i = 0; i < bmap->n_ineq; ++i)
2223 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2225 for (i = 0; i < bmap->n_div; ++i)
2226 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2228 if (type == isl_dim_div) {
2229 bmap = move_divs_last(bmap, first, n);
2230 if (!bmap)
2231 goto error;
2232 if (isl_basic_map_free_div(bmap, n) < 0)
2233 return isl_basic_map_free(bmap);
2234 } else
2235 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2236 if (!bmap->dim)
2237 goto error;
2239 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2240 bmap = isl_basic_map_simplify(bmap);
2241 return isl_basic_map_finalize(bmap);
2242 error:
2243 isl_basic_map_free(bmap);
2244 return NULL;
2247 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2248 enum isl_dim_type type, unsigned first, unsigned n)
2250 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2251 type, first, n));
2254 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2255 enum isl_dim_type type, unsigned first, unsigned n)
2257 int i;
2259 if (!map)
2260 goto error;
2262 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2264 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2265 return map;
2266 map = isl_map_cow(map);
2267 if (!map)
2268 goto error;
2269 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2270 if (!map->dim)
2271 goto error;
2273 for (i = 0; i < map->n; ++i) {
2274 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2275 if (!map->p[i])
2276 goto error;
2278 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2280 return map;
2281 error:
2282 isl_map_free(map);
2283 return NULL;
2286 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2287 enum isl_dim_type type, unsigned first, unsigned n)
2289 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2293 * We don't cow, as the div is assumed to be redundant.
2295 __isl_give isl_basic_map *isl_basic_map_drop_div(
2296 __isl_take isl_basic_map *bmap, unsigned div)
2298 int i;
2299 unsigned pos;
2301 if (!bmap)
2302 goto error;
2304 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
2306 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
2308 for (i = 0; i < bmap->n_eq; ++i)
2309 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
2311 for (i = 0; i < bmap->n_ineq; ++i) {
2312 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
2313 isl_basic_map_drop_inequality(bmap, i);
2314 --i;
2315 continue;
2317 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
2320 for (i = 0; i < bmap->n_div; ++i)
2321 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
2323 if (div != bmap->n_div - 1) {
2324 int j;
2325 isl_int *t = bmap->div[div];
2327 for (j = div; j < bmap->n_div - 1; ++j)
2328 bmap->div[j] = bmap->div[j+1];
2330 bmap->div[bmap->n_div - 1] = t;
2332 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2333 if (isl_basic_map_free_div(bmap, 1) < 0)
2334 return isl_basic_map_free(bmap);
2336 return bmap;
2337 error:
2338 isl_basic_map_free(bmap);
2339 return NULL;
2342 /* Eliminate the specified n dimensions starting at first from the
2343 * constraints, without removing the dimensions from the space.
2344 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2346 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2347 enum isl_dim_type type, unsigned first, unsigned n)
2349 int i;
2351 if (!map)
2352 return NULL;
2353 if (n == 0)
2354 return map;
2356 if (first + n > isl_map_dim(map, type) || first + n < first)
2357 isl_die(map->ctx, isl_error_invalid,
2358 "index out of bounds", goto error);
2360 map = isl_map_cow(map);
2361 if (!map)
2362 return NULL;
2364 for (i = 0; i < map->n; ++i) {
2365 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2366 if (!map->p[i])
2367 goto error;
2369 return map;
2370 error:
2371 isl_map_free(map);
2372 return NULL;
2375 /* Eliminate the specified n dimensions starting at first from the
2376 * constraints, without removing the dimensions from the space.
2377 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2379 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2380 enum isl_dim_type type, unsigned first, unsigned n)
2382 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2385 /* Eliminate the specified n dimensions starting at first from the
2386 * constraints, without removing the dimensions from the space.
2387 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2389 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2390 unsigned first, unsigned n)
2392 return isl_set_eliminate(set, isl_dim_set, first, n);
2395 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2396 __isl_take isl_basic_map *bmap)
2398 if (!bmap)
2399 return NULL;
2400 bmap = isl_basic_map_eliminate_vars(bmap,
2401 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2402 if (!bmap)
2403 return NULL;
2404 bmap->n_div = 0;
2405 return isl_basic_map_finalize(bmap);
2408 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2409 __isl_take isl_basic_set *bset)
2411 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2414 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2416 int i;
2418 if (!map)
2419 return NULL;
2420 if (map->n == 0)
2421 return map;
2423 map = isl_map_cow(map);
2424 if (!map)
2425 return NULL;
2427 for (i = 0; i < map->n; ++i) {
2428 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2429 if (!map->p[i])
2430 goto error;
2432 return map;
2433 error:
2434 isl_map_free(map);
2435 return NULL;
2438 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2440 return isl_map_remove_divs(set);
2443 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2444 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2445 unsigned first, unsigned n)
2447 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2448 return isl_basic_map_free(bmap);
2449 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2450 return bmap;
2451 bmap = isl_basic_map_eliminate_vars(bmap,
2452 isl_basic_map_offset(bmap, type) - 1 + first, n);
2453 if (!bmap)
2454 return bmap;
2455 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2456 return bmap;
2457 bmap = isl_basic_map_drop(bmap, type, first, n);
2458 return bmap;
2461 /* Return true if the definition of the given div (recursively) involves
2462 * any of the given variables.
2464 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2465 unsigned first, unsigned n)
2467 int i;
2468 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2470 if (isl_int_is_zero(bmap->div[div][0]))
2471 return isl_bool_false;
2472 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2473 return isl_bool_true;
2475 for (i = bmap->n_div - 1; i >= 0; --i) {
2476 isl_bool involves;
2478 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2479 continue;
2480 involves = div_involves_vars(bmap, i, first, n);
2481 if (involves < 0 || involves)
2482 return involves;
2485 return isl_bool_false;
2488 /* Try and add a lower and/or upper bound on "div" to "bmap"
2489 * based on inequality "i".
2490 * "total" is the total number of variables (excluding the divs).
2491 * "v" is a temporary object that can be used during the calculations.
2492 * If "lb" is set, then a lower bound should be constructed.
2493 * If "ub" is set, then an upper bound should be constructed.
2495 * The calling function has already checked that the inequality does not
2496 * reference "div", but we still need to check that the inequality is
2497 * of the right form. We'll consider the case where we want to construct
2498 * a lower bound. The construction of upper bounds is similar.
2500 * Let "div" be of the form
2502 * q = floor((a + f(x))/d)
2504 * We essentially check if constraint "i" is of the form
2506 * b + f(x) >= 0
2508 * so that we can use it to derive a lower bound on "div".
2509 * However, we allow a slightly more general form
2511 * b + g(x) >= 0
2513 * with the condition that the coefficients of g(x) - f(x) are all
2514 * divisible by d.
2515 * Rewriting this constraint as
2517 * 0 >= -b - g(x)
2519 * adding a + f(x) to both sides and dividing by d, we obtain
2521 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2523 * Taking the floor on both sides, we obtain
2525 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2527 * or
2529 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2531 * In the case of an upper bound, we construct the constraint
2533 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2536 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2537 __isl_take isl_basic_map *bmap, int div, int i,
2538 unsigned total, isl_int v, int lb, int ub)
2540 int j;
2542 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2543 if (lb) {
2544 isl_int_sub(v, bmap->ineq[i][1 + j],
2545 bmap->div[div][1 + 1 + j]);
2546 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2548 if (ub) {
2549 isl_int_add(v, bmap->ineq[i][1 + j],
2550 bmap->div[div][1 + 1 + j]);
2551 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2554 if (!lb && !ub)
2555 return bmap;
2557 bmap = isl_basic_map_cow(bmap);
2558 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2559 if (lb) {
2560 int k = isl_basic_map_alloc_inequality(bmap);
2561 if (k < 0)
2562 goto error;
2563 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2564 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2565 bmap->div[div][1 + j]);
2566 isl_int_cdiv_q(bmap->ineq[k][j],
2567 bmap->ineq[k][j], bmap->div[div][0]);
2569 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2571 if (ub) {
2572 int k = isl_basic_map_alloc_inequality(bmap);
2573 if (k < 0)
2574 goto error;
2575 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2576 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2577 bmap->div[div][1 + j]);
2578 isl_int_fdiv_q(bmap->ineq[k][j],
2579 bmap->ineq[k][j], bmap->div[div][0]);
2581 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2584 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2585 return bmap;
2586 error:
2587 isl_basic_map_free(bmap);
2588 return NULL;
2591 /* This function is called right before "div" is eliminated from "bmap"
2592 * using Fourier-Motzkin.
2593 * Look through the constraints of "bmap" for constraints on the argument
2594 * of the integer division and use them to construct constraints on the
2595 * integer division itself. These constraints can then be combined
2596 * during the Fourier-Motzkin elimination.
2597 * Note that it is only useful to introduce lower bounds on "div"
2598 * if "bmap" already contains upper bounds on "div" as the newly
2599 * introduce lower bounds can then be combined with the pre-existing
2600 * upper bounds. Similarly for upper bounds.
2601 * We therefore first check if "bmap" contains any lower and/or upper bounds
2602 * on "div".
2604 * It is interesting to note that the introduction of these constraints
2605 * can indeed lead to more accurate results, even when compared to
2606 * deriving constraints on the argument of "div" from constraints on "div".
2607 * Consider, for example, the set
2609 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2611 * The second constraint can be rewritten as
2613 * 2 * [(-i-2j+3)/4] + k >= 0
2615 * from which we can derive
2617 * -i - 2j + 3 >= -2k
2619 * or
2621 * i + 2j <= 3 + 2k
2623 * Combined with the first constraint, we obtain
2625 * -3 <= 3 + 2k or k >= -3
2627 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2628 * the first constraint, we obtain
2630 * [(i + 2j)/4] >= [-3/4] = -1
2632 * Combining this constraint with the second constraint, we obtain
2634 * k >= -2
2636 static __isl_give isl_basic_map *insert_bounds_on_div(
2637 __isl_take isl_basic_map *bmap, int div)
2639 int i;
2640 int check_lb, check_ub;
2641 isl_int v;
2642 unsigned total;
2644 if (!bmap)
2645 return NULL;
2647 if (isl_int_is_zero(bmap->div[div][0]))
2648 return bmap;
2650 total = isl_space_dim(bmap->dim, isl_dim_all);
2652 check_lb = 0;
2653 check_ub = 0;
2654 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2655 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2656 if (s > 0)
2657 check_ub = 1;
2658 if (s < 0)
2659 check_lb = 1;
2662 if (!check_lb && !check_ub)
2663 return bmap;
2665 isl_int_init(v);
2667 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2668 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2669 continue;
2671 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2672 check_lb, check_ub);
2675 isl_int_clear(v);
2677 return bmap;
2680 /* Remove all divs (recursively) involving any of the given dimensions
2681 * in their definitions.
2683 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2684 __isl_take isl_basic_map *bmap,
2685 enum isl_dim_type type, unsigned first, unsigned n)
2687 int i;
2689 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2690 return isl_basic_map_free(bmap);
2691 first += isl_basic_map_offset(bmap, type);
2693 for (i = bmap->n_div - 1; i >= 0; --i) {
2694 isl_bool involves;
2696 involves = div_involves_vars(bmap, i, first, n);
2697 if (involves < 0)
2698 return isl_basic_map_free(bmap);
2699 if (!involves)
2700 continue;
2701 bmap = insert_bounds_on_div(bmap, i);
2702 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2703 if (!bmap)
2704 return NULL;
2705 i = bmap->n_div;
2708 return bmap;
2711 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2712 __isl_take isl_basic_set *bset,
2713 enum isl_dim_type type, unsigned first, unsigned n)
2715 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2718 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2719 enum isl_dim_type type, unsigned first, unsigned n)
2721 int i;
2723 if (!map)
2724 return NULL;
2725 if (map->n == 0)
2726 return map;
2728 map = isl_map_cow(map);
2729 if (!map)
2730 return NULL;
2732 for (i = 0; i < map->n; ++i) {
2733 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2734 type, first, n);
2735 if (!map->p[i])
2736 goto error;
2738 return map;
2739 error:
2740 isl_map_free(map);
2741 return NULL;
2744 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2745 enum isl_dim_type type, unsigned first, unsigned n)
2747 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2748 type, first, n));
2751 /* Does the description of "bmap" depend on the specified dimensions?
2752 * We also check whether the dimensions appear in any of the div definitions.
2753 * In principle there is no need for this check. If the dimensions appear
2754 * in a div definition, they also appear in the defining constraints of that
2755 * div.
2757 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2758 enum isl_dim_type type, unsigned first, unsigned n)
2760 int i;
2762 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2763 return isl_bool_error;
2765 first += isl_basic_map_offset(bmap, type);
2766 for (i = 0; i < bmap->n_eq; ++i)
2767 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2768 return isl_bool_true;
2769 for (i = 0; i < bmap->n_ineq; ++i)
2770 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2771 return isl_bool_true;
2772 for (i = 0; i < bmap->n_div; ++i) {
2773 if (isl_int_is_zero(bmap->div[i][0]))
2774 continue;
2775 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2776 return isl_bool_true;
2779 return isl_bool_false;
2782 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2783 enum isl_dim_type type, unsigned first, unsigned n)
2785 int i;
2787 if (!map)
2788 return isl_bool_error;
2790 if (first + n > isl_map_dim(map, type))
2791 isl_die(map->ctx, isl_error_invalid,
2792 "index out of bounds", return isl_bool_error);
2794 for (i = 0; i < map->n; ++i) {
2795 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2796 type, first, n);
2797 if (involves < 0 || involves)
2798 return involves;
2801 return isl_bool_false;
2804 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2805 enum isl_dim_type type, unsigned first, unsigned n)
2807 return isl_basic_map_involves_dims(bset, type, first, n);
2810 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2811 enum isl_dim_type type, unsigned first, unsigned n)
2813 return isl_map_involves_dims(set, type, first, n);
2816 /* Drop all constraints in bmap that involve any of the dimensions
2817 * first to first+n-1.
2819 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2820 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2822 int i;
2824 if (n == 0)
2825 return bmap;
2827 bmap = isl_basic_map_cow(bmap);
2829 if (!bmap)
2830 return NULL;
2832 for (i = bmap->n_eq - 1; i >= 0; --i) {
2833 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2834 continue;
2835 isl_basic_map_drop_equality(bmap, i);
2838 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2839 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2840 continue;
2841 isl_basic_map_drop_inequality(bmap, i);
2844 bmap = isl_basic_map_add_known_div_constraints(bmap);
2845 return bmap;
2848 /* Drop all constraints in bset that involve any of the dimensions
2849 * first to first+n-1.
2851 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2852 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2854 return isl_basic_map_drop_constraints_involving(bset, first, n);
2857 /* Drop all constraints in bmap that do not involve any of the dimensions
2858 * first to first + n - 1 of the given type.
2860 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2861 __isl_take isl_basic_map *bmap,
2862 enum isl_dim_type type, unsigned first, unsigned n)
2864 int i;
2866 if (n == 0) {
2867 isl_space *space = isl_basic_map_get_space(bmap);
2868 isl_basic_map_free(bmap);
2869 return isl_basic_map_universe(space);
2871 bmap = isl_basic_map_cow(bmap);
2872 if (!bmap)
2873 return NULL;
2875 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2876 return isl_basic_map_free(bmap);
2878 first += isl_basic_map_offset(bmap, type) - 1;
2880 for (i = bmap->n_eq - 1; i >= 0; --i) {
2881 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2882 continue;
2883 isl_basic_map_drop_equality(bmap, i);
2886 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2887 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2888 continue;
2889 isl_basic_map_drop_inequality(bmap, i);
2892 bmap = isl_basic_map_add_known_div_constraints(bmap);
2893 return bmap;
2896 /* Drop all constraints in bset that do not involve any of the dimensions
2897 * first to first + n - 1 of the given type.
2899 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2900 __isl_take isl_basic_set *bset,
2901 enum isl_dim_type type, unsigned first, unsigned n)
2903 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2904 type, first, n);
2907 /* Drop all constraints in bmap that involve any of the dimensions
2908 * first to first + n - 1 of the given type.
2910 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2911 __isl_take isl_basic_map *bmap,
2912 enum isl_dim_type type, unsigned first, unsigned n)
2914 if (!bmap)
2915 return NULL;
2916 if (n == 0)
2917 return bmap;
2919 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2920 return isl_basic_map_free(bmap);
2922 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2923 first += isl_basic_map_offset(bmap, type) - 1;
2924 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2927 /* Drop all constraints in bset that involve any of the dimensions
2928 * first to first + n - 1 of the given type.
2930 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2931 __isl_take isl_basic_set *bset,
2932 enum isl_dim_type type, unsigned first, unsigned n)
2934 return isl_basic_map_drop_constraints_involving_dims(bset,
2935 type, first, n);
2938 /* Drop constraints from "map" by applying "drop" to each basic map.
2940 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2941 enum isl_dim_type type, unsigned first, unsigned n,
2942 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2943 enum isl_dim_type type, unsigned first, unsigned n))
2945 int i;
2946 unsigned dim;
2948 if (!map)
2949 return NULL;
2951 dim = isl_map_dim(map, type);
2952 if (first + n > dim || first + n < first)
2953 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2954 "index out of bounds", return isl_map_free(map));
2956 map = isl_map_cow(map);
2957 if (!map)
2958 return NULL;
2960 for (i = 0; i < map->n; ++i) {
2961 map->p[i] = drop(map->p[i], type, first, n);
2962 if (!map->p[i])
2963 return isl_map_free(map);
2966 if (map->n > 1)
2967 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2969 return map;
2972 /* Drop all constraints in map that involve any of the dimensions
2973 * first to first + n - 1 of the given type.
2975 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2976 __isl_take isl_map *map,
2977 enum isl_dim_type type, unsigned first, unsigned n)
2979 if (n == 0)
2980 return map;
2981 return drop_constraints(map, type, first, n,
2982 &isl_basic_map_drop_constraints_involving_dims);
2985 /* Drop all constraints in "map" that do not involve any of the dimensions
2986 * first to first + n - 1 of the given type.
2988 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2989 __isl_take isl_map *map,
2990 enum isl_dim_type type, unsigned first, unsigned n)
2992 if (n == 0) {
2993 isl_space *space = isl_map_get_space(map);
2994 isl_map_free(map);
2995 return isl_map_universe(space);
2997 return drop_constraints(map, type, first, n,
2998 &isl_basic_map_drop_constraints_not_involving_dims);
3001 /* Drop all constraints in set that involve any of the dimensions
3002 * first to first + n - 1 of the given type.
3004 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3005 __isl_take isl_set *set,
3006 enum isl_dim_type type, unsigned first, unsigned n)
3008 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3011 /* Drop all constraints in "set" that do not involve any of the dimensions
3012 * first to first + n - 1 of the given type.
3014 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3015 __isl_take isl_set *set,
3016 enum isl_dim_type type, unsigned first, unsigned n)
3018 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3021 /* Does local variable "div" of "bmap" have a complete explicit representation?
3022 * Having a complete explicit representation requires not only
3023 * an explicit representation, but also that all local variables
3024 * that appear in this explicit representation in turn have
3025 * a complete explicit representation.
3027 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3029 int i;
3030 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3031 isl_bool marked;
3033 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3034 if (marked < 0 || marked)
3035 return isl_bool_not(marked);
3037 for (i = bmap->n_div - 1; i >= 0; --i) {
3038 isl_bool known;
3040 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3041 continue;
3042 known = isl_basic_map_div_is_known(bmap, i);
3043 if (known < 0 || !known)
3044 return known;
3047 return isl_bool_true;
3050 /* Remove all divs that are unknown or defined in terms of unknown divs.
3052 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3053 __isl_take isl_basic_map *bmap)
3055 int i;
3057 if (!bmap)
3058 return NULL;
3060 for (i = bmap->n_div - 1; i >= 0; --i) {
3061 if (isl_basic_map_div_is_known(bmap, i))
3062 continue;
3063 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3064 if (!bmap)
3065 return NULL;
3066 i = bmap->n_div;
3069 return bmap;
3072 /* Remove all divs that are unknown or defined in terms of unknown divs.
3074 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3075 __isl_take isl_basic_set *bset)
3077 return isl_basic_map_remove_unknown_divs(bset);
3080 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3082 int i;
3084 if (!map)
3085 return NULL;
3086 if (map->n == 0)
3087 return map;
3089 map = isl_map_cow(map);
3090 if (!map)
3091 return NULL;
3093 for (i = 0; i < map->n; ++i) {
3094 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3095 if (!map->p[i])
3096 goto error;
3098 return map;
3099 error:
3100 isl_map_free(map);
3101 return NULL;
3104 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3106 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3109 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3110 __isl_take isl_basic_set *bset,
3111 enum isl_dim_type type, unsigned first, unsigned n)
3113 isl_basic_map *bmap = bset_to_bmap(bset);
3114 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3115 return bset_from_bmap(bmap);
3118 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3119 enum isl_dim_type type, unsigned first, unsigned n)
3121 int i;
3123 if (n == 0)
3124 return map;
3126 map = isl_map_cow(map);
3127 if (!map)
3128 return NULL;
3129 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3131 for (i = 0; i < map->n; ++i) {
3132 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3133 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3134 if (!map->p[i])
3135 goto error;
3137 map = isl_map_drop(map, type, first, n);
3138 return map;
3139 error:
3140 isl_map_free(map);
3141 return NULL;
3144 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3145 enum isl_dim_type type, unsigned first, unsigned n)
3147 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3148 type, first, n));
3151 /* Project out n inputs starting at first using Fourier-Motzkin */
3152 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3153 unsigned first, unsigned n)
3155 return isl_map_remove_dims(map, isl_dim_in, first, n);
3158 static void dump_term(struct isl_basic_map *bmap,
3159 isl_int c, int pos, FILE *out)
3161 const char *name;
3162 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3163 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3164 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3165 if (!pos)
3166 isl_int_print(out, c, 0);
3167 else {
3168 if (!isl_int_is_one(c))
3169 isl_int_print(out, c, 0);
3170 if (pos < 1 + nparam) {
3171 name = isl_space_get_dim_name(bmap->dim,
3172 isl_dim_param, pos - 1);
3173 if (name)
3174 fprintf(out, "%s", name);
3175 else
3176 fprintf(out, "p%d", pos - 1);
3177 } else if (pos < 1 + nparam + in)
3178 fprintf(out, "i%d", pos - 1 - nparam);
3179 else if (pos < 1 + nparam + dim)
3180 fprintf(out, "o%d", pos - 1 - nparam - in);
3181 else
3182 fprintf(out, "e%d", pos - 1 - nparam - dim);
3186 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3187 int sign, FILE *out)
3189 int i;
3190 int first;
3191 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3192 isl_int v;
3194 isl_int_init(v);
3195 for (i = 0, first = 1; i < len; ++i) {
3196 if (isl_int_sgn(c[i]) * sign <= 0)
3197 continue;
3198 if (!first)
3199 fprintf(out, " + ");
3200 first = 0;
3201 isl_int_abs(v, c[i]);
3202 dump_term(bmap, v, i, out);
3204 isl_int_clear(v);
3205 if (first)
3206 fprintf(out, "0");
3209 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3210 const char *op, FILE *out, int indent)
3212 int i;
3214 fprintf(out, "%*s", indent, "");
3216 dump_constraint_sign(bmap, c, 1, out);
3217 fprintf(out, " %s ", op);
3218 dump_constraint_sign(bmap, c, -1, out);
3220 fprintf(out, "\n");
3222 for (i = bmap->n_div; i < bmap->extra; ++i) {
3223 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3224 continue;
3225 fprintf(out, "%*s", indent, "");
3226 fprintf(out, "ERROR: unused div coefficient not zero\n");
3227 abort();
3231 static void dump_constraints(struct isl_basic_map *bmap,
3232 isl_int **c, unsigned n,
3233 const char *op, FILE *out, int indent)
3235 int i;
3237 for (i = 0; i < n; ++i)
3238 dump_constraint(bmap, c[i], op, out, indent);
3241 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3243 int j;
3244 int first = 1;
3245 unsigned total = isl_basic_map_total_dim(bmap);
3247 for (j = 0; j < 1 + total; ++j) {
3248 if (isl_int_is_zero(exp[j]))
3249 continue;
3250 if (!first && isl_int_is_pos(exp[j]))
3251 fprintf(out, "+");
3252 dump_term(bmap, exp[j], j, out);
3253 first = 0;
3257 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3259 int i;
3261 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3262 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3264 for (i = 0; i < bmap->n_div; ++i) {
3265 fprintf(out, "%*s", indent, "");
3266 fprintf(out, "e%d = [(", i);
3267 dump_affine(bmap, bmap->div[i]+1, out);
3268 fprintf(out, ")/");
3269 isl_int_print(out, bmap->div[i][0], 0);
3270 fprintf(out, "]\n");
3274 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3275 FILE *out, int indent)
3277 if (!bset) {
3278 fprintf(out, "null basic set\n");
3279 return;
3282 fprintf(out, "%*s", indent, "");
3283 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3284 bset->ref, bset->dim->nparam, bset->dim->n_out,
3285 bset->extra, bset->flags);
3286 dump(bset_to_bmap(bset), out, indent);
3289 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3290 FILE *out, int indent)
3292 if (!bmap) {
3293 fprintf(out, "null basic map\n");
3294 return;
3297 fprintf(out, "%*s", indent, "");
3298 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3299 "flags: %x, n_name: %d\n",
3300 bmap->ref,
3301 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3302 bmap->extra, bmap->flags, bmap->dim->n_id);
3303 dump(bmap, out, indent);
3306 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3308 unsigned total;
3309 if (!bmap)
3310 return -1;
3311 total = isl_basic_map_total_dim(bmap);
3312 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3313 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3314 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3315 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3316 return 0;
3319 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3320 unsigned flags)
3322 if (!space)
3323 return NULL;
3324 if (isl_space_dim(space, isl_dim_in) != 0)
3325 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3326 "set cannot have input dimensions", goto error);
3327 return isl_map_alloc_space(space, n, flags);
3328 error:
3329 isl_space_free(space);
3330 return NULL;
3333 /* Make sure "map" has room for at least "n" more basic maps.
3335 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3337 int i;
3338 struct isl_map *grown = NULL;
3340 if (!map)
3341 return NULL;
3342 isl_assert(map->ctx, n >= 0, goto error);
3343 if (map->n + n <= map->size)
3344 return map;
3345 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3346 if (!grown)
3347 goto error;
3348 for (i = 0; i < map->n; ++i) {
3349 grown->p[i] = isl_basic_map_copy(map->p[i]);
3350 if (!grown->p[i])
3351 goto error;
3352 grown->n++;
3354 isl_map_free(map);
3355 return grown;
3356 error:
3357 isl_map_free(grown);
3358 isl_map_free(map);
3359 return NULL;
3362 /* Make sure "set" has room for at least "n" more basic sets.
3364 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3366 return set_from_map(isl_map_grow(set_to_map(set), n));
3369 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3371 return isl_map_from_basic_map(bset);
3374 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3376 struct isl_map *map;
3378 if (!bmap)
3379 return NULL;
3381 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3382 return isl_map_add_basic_map(map, bmap);
3385 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3386 __isl_take isl_basic_set *bset)
3388 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3389 bset_to_bmap(bset)));
3392 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3394 return isl_map_free(set);
3397 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3399 int i;
3401 if (!set) {
3402 fprintf(out, "null set\n");
3403 return;
3406 fprintf(out, "%*s", indent, "");
3407 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3408 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3409 set->flags);
3410 for (i = 0; i < set->n; ++i) {
3411 fprintf(out, "%*s", indent, "");
3412 fprintf(out, "basic set %d:\n", i);
3413 isl_basic_set_print_internal(set->p[i], out, indent+4);
3417 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3419 int i;
3421 if (!map) {
3422 fprintf(out, "null map\n");
3423 return;
3426 fprintf(out, "%*s", indent, "");
3427 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3428 "flags: %x, n_name: %d\n",
3429 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3430 map->dim->n_out, map->flags, map->dim->n_id);
3431 for (i = 0; i < map->n; ++i) {
3432 fprintf(out, "%*s", indent, "");
3433 fprintf(out, "basic map %d:\n", i);
3434 isl_basic_map_print_internal(map->p[i], out, indent+4);
3438 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3439 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3441 struct isl_basic_map *bmap_domain;
3443 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3444 goto error;
3446 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3447 isl_assert(bset->ctx,
3448 isl_basic_map_compatible_domain(bmap, bset), goto error);
3450 bmap = isl_basic_map_cow(bmap);
3451 if (!bmap)
3452 goto error;
3453 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3454 bset->n_div, bset->n_eq, bset->n_ineq);
3455 bmap_domain = isl_basic_map_from_domain(bset);
3456 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3458 bmap = isl_basic_map_simplify(bmap);
3459 return isl_basic_map_finalize(bmap);
3460 error:
3461 isl_basic_map_free(bmap);
3462 isl_basic_set_free(bset);
3463 return NULL;
3466 /* Check that the space of "bset" is the same as that of the range of "bmap".
3468 static isl_stat isl_basic_map_check_compatible_range(
3469 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3471 isl_bool ok;
3473 ok = isl_basic_map_compatible_range(bmap, bset);
3474 if (ok < 0)
3475 return isl_stat_error;
3476 if (!ok)
3477 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3478 "incompatible spaces", return isl_stat_error);
3480 return isl_stat_ok;
3483 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3484 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3486 struct isl_basic_map *bmap_range;
3488 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3489 goto error;
3491 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3492 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3493 goto error;
3495 if (isl_basic_set_plain_is_universe(bset)) {
3496 isl_basic_set_free(bset);
3497 return bmap;
3500 bmap = isl_basic_map_cow(bmap);
3501 if (!bmap)
3502 goto error;
3503 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3504 bset->n_div, bset->n_eq, bset->n_ineq);
3505 bmap_range = bset_to_bmap(bset);
3506 bmap = add_constraints(bmap, bmap_range, 0, 0);
3508 bmap = isl_basic_map_simplify(bmap);
3509 return isl_basic_map_finalize(bmap);
3510 error:
3511 isl_basic_map_free(bmap);
3512 isl_basic_set_free(bset);
3513 return NULL;
3516 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3517 __isl_keep isl_vec *vec)
3519 int i;
3520 unsigned total;
3521 isl_int s;
3523 if (!bmap || !vec)
3524 return isl_bool_error;
3526 total = 1 + isl_basic_map_total_dim(bmap);
3527 if (total != vec->size)
3528 return isl_bool_false;
3530 isl_int_init(s);
3532 for (i = 0; i < bmap->n_eq; ++i) {
3533 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3534 if (!isl_int_is_zero(s)) {
3535 isl_int_clear(s);
3536 return isl_bool_false;
3540 for (i = 0; i < bmap->n_ineq; ++i) {
3541 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3542 if (isl_int_is_neg(s)) {
3543 isl_int_clear(s);
3544 return isl_bool_false;
3548 isl_int_clear(s);
3550 return isl_bool_true;
3553 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3554 __isl_keep isl_vec *vec)
3556 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3559 __isl_give isl_basic_map *isl_basic_map_intersect(
3560 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3562 struct isl_vec *sample = NULL;
3564 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3565 goto error;
3566 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3567 isl_space_dim(bmap1->dim, isl_dim_param) &&
3568 isl_space_dim(bmap2->dim, isl_dim_all) !=
3569 isl_space_dim(bmap2->dim, isl_dim_param))
3570 return isl_basic_map_intersect(bmap2, bmap1);
3572 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3573 isl_space_dim(bmap2->dim, isl_dim_param))
3574 isl_assert(bmap1->ctx,
3575 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3577 if (isl_basic_map_plain_is_empty(bmap1)) {
3578 isl_basic_map_free(bmap2);
3579 return bmap1;
3581 if (isl_basic_map_plain_is_empty(bmap2)) {
3582 isl_basic_map_free(bmap1);
3583 return bmap2;
3586 if (bmap1->sample &&
3587 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3588 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3589 sample = isl_vec_copy(bmap1->sample);
3590 else if (bmap2->sample &&
3591 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3592 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3593 sample = isl_vec_copy(bmap2->sample);
3595 bmap1 = isl_basic_map_cow(bmap1);
3596 if (!bmap1)
3597 goto error;
3598 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3599 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3600 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3602 if (!bmap1)
3603 isl_vec_free(sample);
3604 else if (sample) {
3605 isl_vec_free(bmap1->sample);
3606 bmap1->sample = sample;
3609 bmap1 = isl_basic_map_simplify(bmap1);
3610 return isl_basic_map_finalize(bmap1);
3611 error:
3612 if (sample)
3613 isl_vec_free(sample);
3614 isl_basic_map_free(bmap1);
3615 isl_basic_map_free(bmap2);
3616 return NULL;
3619 struct isl_basic_set *isl_basic_set_intersect(
3620 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3622 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3623 bset_to_bmap(bset2)));
3626 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3627 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3629 return isl_basic_set_intersect(bset1, bset2);
3632 /* Special case of isl_map_intersect, where both map1 and map2
3633 * are convex, without any divs and such that either map1 or map2
3634 * contains a single constraint. This constraint is then simply
3635 * added to the other map.
3637 static __isl_give isl_map *map_intersect_add_constraint(
3638 __isl_take isl_map *map1, __isl_take isl_map *map2)
3640 isl_assert(map1->ctx, map1->n == 1, goto error);
3641 isl_assert(map2->ctx, map1->n == 1, goto error);
3642 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3643 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3645 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3646 return isl_map_intersect(map2, map1);
3648 map1 = isl_map_cow(map1);
3649 if (!map1)
3650 goto error;
3651 if (isl_map_plain_is_empty(map1)) {
3652 isl_map_free(map2);
3653 return map1;
3655 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3656 if (map2->p[0]->n_eq == 1)
3657 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3658 else
3659 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3660 map2->p[0]->ineq[0]);
3662 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3663 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3664 if (!map1->p[0])
3665 goto error;
3667 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3668 isl_basic_map_free(map1->p[0]);
3669 map1->n = 0;
3672 isl_map_free(map2);
3674 return map1;
3675 error:
3676 isl_map_free(map1);
3677 isl_map_free(map2);
3678 return NULL;
3681 /* map2 may be either a parameter domain or a map living in the same
3682 * space as map1.
3684 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3685 __isl_take isl_map *map2)
3687 unsigned flags = 0;
3688 isl_map *result;
3689 int i, j;
3691 if (!map1 || !map2)
3692 goto error;
3694 if ((isl_map_plain_is_empty(map1) ||
3695 isl_map_plain_is_universe(map2)) &&
3696 isl_space_is_equal(map1->dim, map2->dim)) {
3697 isl_map_free(map2);
3698 return map1;
3700 if ((isl_map_plain_is_empty(map2) ||
3701 isl_map_plain_is_universe(map1)) &&
3702 isl_space_is_equal(map1->dim, map2->dim)) {
3703 isl_map_free(map1);
3704 return map2;
3707 if (map1->n == 1 && map2->n == 1 &&
3708 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3709 isl_space_is_equal(map1->dim, map2->dim) &&
3710 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3711 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3712 return map_intersect_add_constraint(map1, map2);
3714 if (isl_space_dim(map2->dim, isl_dim_all) !=
3715 isl_space_dim(map2->dim, isl_dim_param))
3716 isl_assert(map1->ctx,
3717 isl_space_is_equal(map1->dim, map2->dim), goto error);
3719 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3720 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3721 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3723 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3724 map1->n * map2->n, flags);
3725 if (!result)
3726 goto error;
3727 for (i = 0; i < map1->n; ++i)
3728 for (j = 0; j < map2->n; ++j) {
3729 struct isl_basic_map *part;
3730 part = isl_basic_map_intersect(
3731 isl_basic_map_copy(map1->p[i]),
3732 isl_basic_map_copy(map2->p[j]));
3733 if (isl_basic_map_is_empty(part) < 0)
3734 part = isl_basic_map_free(part);
3735 result = isl_map_add_basic_map(result, part);
3736 if (!result)
3737 goto error;
3739 isl_map_free(map1);
3740 isl_map_free(map2);
3741 return result;
3742 error:
3743 isl_map_free(map1);
3744 isl_map_free(map2);
3745 return NULL;
3748 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3749 __isl_take isl_map *map2)
3751 if (!map1 || !map2)
3752 goto error;
3753 if (!isl_space_is_equal(map1->dim, map2->dim))
3754 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3755 "spaces don't match", goto error);
3756 return map_intersect_internal(map1, map2);
3757 error:
3758 isl_map_free(map1);
3759 isl_map_free(map2);
3760 return NULL;
3763 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3764 __isl_take isl_map *map2)
3766 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3769 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3771 return set_from_map(isl_map_intersect(set_to_map(set1),
3772 set_to_map(set2)));
3775 /* map_intersect_internal accepts intersections
3776 * with parameter domains, so we can just call that function.
3778 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3779 __isl_take isl_set *params)
3781 return map_intersect_internal(map, params);
3784 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3785 __isl_take isl_map *map2)
3787 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3790 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3791 __isl_take isl_set *params)
3793 return isl_map_intersect_params(set, params);
3796 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3798 isl_space *space;
3799 unsigned pos, n1, n2;
3801 if (!bmap)
3802 return NULL;
3803 bmap = isl_basic_map_cow(bmap);
3804 if (!bmap)
3805 return NULL;
3806 space = isl_space_reverse(isl_space_copy(bmap->dim));
3807 pos = isl_basic_map_offset(bmap, isl_dim_in);
3808 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3809 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3810 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3811 return isl_basic_map_reset_space(bmap, space);
3814 static __isl_give isl_basic_map *basic_map_space_reset(
3815 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3817 isl_space *space;
3819 if (!bmap)
3820 return NULL;
3821 if (!isl_space_is_named_or_nested(bmap->dim, type))
3822 return bmap;
3824 space = isl_basic_map_get_space(bmap);
3825 space = isl_space_reset(space, type);
3826 bmap = isl_basic_map_reset_space(bmap, space);
3827 return bmap;
3830 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3831 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3832 unsigned pos, unsigned n)
3834 isl_bool rational;
3835 isl_space *res_dim;
3836 struct isl_basic_map *res;
3837 struct isl_dim_map *dim_map;
3838 unsigned total, off;
3839 enum isl_dim_type t;
3841 if (n == 0)
3842 return basic_map_space_reset(bmap, type);
3844 if (!bmap)
3845 return NULL;
3847 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3849 total = isl_basic_map_total_dim(bmap) + n;
3850 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3851 off = 0;
3852 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3853 if (t != type) {
3854 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3855 } else {
3856 unsigned size = isl_basic_map_dim(bmap, t);
3857 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3858 0, pos, off);
3859 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3860 pos, size - pos, off + pos + n);
3862 off += isl_space_dim(res_dim, t);
3864 isl_dim_map_div(dim_map, bmap, off);
3866 res = isl_basic_map_alloc_space(res_dim,
3867 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3868 rational = isl_basic_map_is_rational(bmap);
3869 if (rational < 0)
3870 res = isl_basic_map_free(res);
3871 if (rational)
3872 res = isl_basic_map_set_rational(res);
3873 if (isl_basic_map_plain_is_empty(bmap)) {
3874 isl_basic_map_free(bmap);
3875 free(dim_map);
3876 return isl_basic_map_set_to_empty(res);
3878 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3879 return isl_basic_map_finalize(res);
3882 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3883 __isl_take isl_basic_set *bset,
3884 enum isl_dim_type type, unsigned pos, unsigned n)
3886 return isl_basic_map_insert_dims(bset, type, pos, n);
3889 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3890 enum isl_dim_type type, unsigned n)
3892 if (!bmap)
3893 return NULL;
3894 return isl_basic_map_insert_dims(bmap, type,
3895 isl_basic_map_dim(bmap, type), n);
3898 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3899 enum isl_dim_type type, unsigned n)
3901 if (!bset)
3902 return NULL;
3903 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3904 return isl_basic_map_add_dims(bset, type, n);
3905 error:
3906 isl_basic_set_free(bset);
3907 return NULL;
3910 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3911 enum isl_dim_type type)
3913 isl_space *space;
3915 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3916 return map;
3918 space = isl_map_get_space(map);
3919 space = isl_space_reset(space, type);
3920 map = isl_map_reset_space(map, space);
3921 return map;
3924 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3925 enum isl_dim_type type, unsigned pos, unsigned n)
3927 int i;
3929 if (n == 0)
3930 return map_space_reset(map, type);
3932 map = isl_map_cow(map);
3933 if (!map)
3934 return NULL;
3936 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3937 if (!map->dim)
3938 goto error;
3940 for (i = 0; i < map->n; ++i) {
3941 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3942 if (!map->p[i])
3943 goto error;
3946 return map;
3947 error:
3948 isl_map_free(map);
3949 return NULL;
3952 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3953 enum isl_dim_type type, unsigned pos, unsigned n)
3955 return isl_map_insert_dims(set, type, pos, n);
3958 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3959 enum isl_dim_type type, unsigned n)
3961 if (!map)
3962 return NULL;
3963 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3966 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3967 enum isl_dim_type type, unsigned n)
3969 if (!set)
3970 return NULL;
3971 isl_assert(set->ctx, type != isl_dim_in, goto error);
3972 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3973 error:
3974 isl_set_free(set);
3975 return NULL;
3978 __isl_give isl_basic_map *isl_basic_map_move_dims(
3979 __isl_take isl_basic_map *bmap,
3980 enum isl_dim_type dst_type, unsigned dst_pos,
3981 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3983 struct isl_dim_map *dim_map;
3984 struct isl_basic_map *res;
3985 enum isl_dim_type t;
3986 unsigned total, off;
3988 if (!bmap)
3989 return NULL;
3990 if (n == 0) {
3991 bmap = isl_basic_map_reset(bmap, src_type);
3992 bmap = isl_basic_map_reset(bmap, dst_type);
3993 return bmap;
3996 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3997 return isl_basic_map_free(bmap);
3999 if (dst_type == src_type && dst_pos == src_pos)
4000 return bmap;
4002 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4004 if (pos(bmap->dim, dst_type) + dst_pos ==
4005 pos(bmap->dim, src_type) + src_pos +
4006 ((src_type < dst_type) ? n : 0)) {
4007 bmap = isl_basic_map_cow(bmap);
4008 if (!bmap)
4009 return NULL;
4011 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4012 src_type, src_pos, n);
4013 if (!bmap->dim)
4014 goto error;
4016 bmap = isl_basic_map_finalize(bmap);
4018 return bmap;
4021 total = isl_basic_map_total_dim(bmap);
4022 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4024 off = 0;
4025 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4026 unsigned size = isl_space_dim(bmap->dim, t);
4027 if (t == dst_type) {
4028 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4029 0, dst_pos, off);
4030 off += dst_pos;
4031 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4032 src_pos, n, off);
4033 off += n;
4034 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4035 dst_pos, size - dst_pos, off);
4036 off += size - dst_pos;
4037 } else if (t == src_type) {
4038 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4039 0, src_pos, off);
4040 off += src_pos;
4041 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4042 src_pos + n, size - src_pos - n, off);
4043 off += size - src_pos - n;
4044 } else {
4045 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4046 off += size;
4049 isl_dim_map_div(dim_map, bmap, off);
4051 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4052 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4053 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4054 if (!bmap)
4055 goto error;
4057 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4058 src_type, src_pos, n);
4059 if (!bmap->dim)
4060 goto error;
4062 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
4063 bmap = isl_basic_map_gauss(bmap, NULL);
4064 bmap = isl_basic_map_finalize(bmap);
4066 return bmap;
4067 error:
4068 isl_basic_map_free(bmap);
4069 return NULL;
4072 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4073 enum isl_dim_type dst_type, unsigned dst_pos,
4074 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4076 isl_basic_map *bmap = bset_to_bmap(bset);
4077 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4078 src_type, src_pos, n);
4079 return bset_from_bmap(bmap);
4082 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4083 enum isl_dim_type dst_type, unsigned dst_pos,
4084 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4086 if (!set)
4087 return NULL;
4088 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4089 return set_from_map(isl_map_move_dims(set_to_map(set),
4090 dst_type, dst_pos, src_type, src_pos, n));
4091 error:
4092 isl_set_free(set);
4093 return NULL;
4096 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4097 enum isl_dim_type dst_type, unsigned dst_pos,
4098 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4100 int i;
4102 if (!map)
4103 return NULL;
4104 if (n == 0) {
4105 map = isl_map_reset(map, src_type);
4106 map = isl_map_reset(map, dst_type);
4107 return map;
4110 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
4111 goto error);
4113 if (dst_type == src_type && dst_pos == src_pos)
4114 return map;
4116 isl_assert(map->ctx, dst_type != src_type, goto error);
4118 map = isl_map_cow(map);
4119 if (!map)
4120 return NULL;
4122 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4123 if (!map->dim)
4124 goto error;
4126 for (i = 0; i < map->n; ++i) {
4127 map->p[i] = isl_basic_map_move_dims(map->p[i],
4128 dst_type, dst_pos,
4129 src_type, src_pos, n);
4130 if (!map->p[i])
4131 goto error;
4134 return map;
4135 error:
4136 isl_map_free(map);
4137 return NULL;
4140 /* Move the specified dimensions to the last columns right before
4141 * the divs. Don't change the dimension specification of bmap.
4142 * That's the responsibility of the caller.
4144 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4145 enum isl_dim_type type, unsigned first, unsigned n)
4147 struct isl_dim_map *dim_map;
4148 struct isl_basic_map *res;
4149 enum isl_dim_type t;
4150 unsigned total, off;
4152 if (!bmap)
4153 return NULL;
4154 if (pos(bmap->dim, type) + first + n ==
4155 1 + isl_space_dim(bmap->dim, isl_dim_all))
4156 return bmap;
4158 total = isl_basic_map_total_dim(bmap);
4159 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4161 off = 0;
4162 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4163 unsigned size = isl_space_dim(bmap->dim, t);
4164 if (t == type) {
4165 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4166 0, first, off);
4167 off += first;
4168 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4169 first, n, total - bmap->n_div - n);
4170 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4171 first + n, size - (first + n), off);
4172 off += size - (first + n);
4173 } else {
4174 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4175 off += size;
4178 isl_dim_map_div(dim_map, bmap, off + n);
4180 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4181 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4182 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4183 return res;
4186 /* Insert "n" rows in the divs of "bmap".
4188 * The number of columns is not changed, which means that the last
4189 * dimensions of "bmap" are being reintepreted as the new divs.
4190 * The space of "bmap" is not adjusted, however, which means
4191 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4192 * from the space of "bmap" is the responsibility of the caller.
4194 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4195 int n)
4197 int i;
4198 size_t row_size;
4199 isl_int **new_div;
4200 isl_int *old;
4202 bmap = isl_basic_map_cow(bmap);
4203 if (!bmap)
4204 return NULL;
4206 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4207 old = bmap->block2.data;
4208 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4209 (bmap->extra + n) * (1 + row_size));
4210 if (!bmap->block2.data)
4211 return isl_basic_map_free(bmap);
4212 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4213 if (!new_div)
4214 return isl_basic_map_free(bmap);
4215 for (i = 0; i < n; ++i) {
4216 new_div[i] = bmap->block2.data +
4217 (bmap->extra + i) * (1 + row_size);
4218 isl_seq_clr(new_div[i], 1 + row_size);
4220 for (i = 0; i < bmap->extra; ++i)
4221 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4222 free(bmap->div);
4223 bmap->div = new_div;
4224 bmap->n_div += n;
4225 bmap->extra += n;
4227 return bmap;
4230 /* Drop constraints from "bmap" that only involve the variables
4231 * of "type" in the range [first, first + n] that are not related
4232 * to any of the variables outside that interval.
4233 * These constraints cannot influence the values for the variables
4234 * outside the interval, except in case they cause "bmap" to be empty.
4235 * Only drop the constraints if "bmap" is known to be non-empty.
4237 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4238 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4239 unsigned first, unsigned n)
4241 int i;
4242 int *groups;
4243 unsigned dim, n_div;
4244 isl_bool non_empty;
4246 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4247 if (non_empty < 0)
4248 return isl_basic_map_free(bmap);
4249 if (!non_empty)
4250 return bmap;
4252 dim = isl_basic_map_dim(bmap, isl_dim_all);
4253 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4254 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4255 if (!groups)
4256 return isl_basic_map_free(bmap);
4257 first += isl_basic_map_offset(bmap, type) - 1;
4258 for (i = 0; i < first; ++i)
4259 groups[i] = -1;
4260 for (i = first + n; i < dim - n_div; ++i)
4261 groups[i] = -1;
4263 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4265 return bmap;
4268 /* Turn the n dimensions of type type, starting at first
4269 * into existentially quantified variables.
4271 * If a subset of the projected out variables are unrelated
4272 * to any of the variables that remain, then the constraints
4273 * involving this subset are simply dropped first.
4275 __isl_give isl_basic_map *isl_basic_map_project_out(
4276 __isl_take isl_basic_map *bmap,
4277 enum isl_dim_type type, unsigned first, unsigned n)
4279 isl_bool empty;
4281 if (n == 0)
4282 return basic_map_space_reset(bmap, type);
4283 if (type == isl_dim_div)
4284 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4285 "cannot project out existentially quantified variables",
4286 return isl_basic_map_free(bmap));
4288 empty = isl_basic_map_plain_is_empty(bmap);
4289 if (empty < 0)
4290 return isl_basic_map_free(bmap);
4291 if (empty)
4292 bmap = isl_basic_map_set_to_empty(bmap);
4294 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4295 if (!bmap)
4296 return NULL;
4298 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4299 return isl_basic_map_remove_dims(bmap, type, first, n);
4301 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4302 return isl_basic_map_free(bmap);
4304 bmap = move_last(bmap, type, first, n);
4305 bmap = isl_basic_map_cow(bmap);
4306 bmap = insert_div_rows(bmap, n);
4307 if (!bmap)
4308 return NULL;
4310 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4311 if (!bmap->dim)
4312 goto error;
4313 bmap = isl_basic_map_simplify(bmap);
4314 bmap = isl_basic_map_drop_redundant_divs(bmap);
4315 return isl_basic_map_finalize(bmap);
4316 error:
4317 isl_basic_map_free(bmap);
4318 return NULL;
4321 /* Turn the n dimensions of type type, starting at first
4322 * into existentially quantified variables.
4324 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4325 enum isl_dim_type type, unsigned first, unsigned n)
4327 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4328 type, first, n));
4331 /* Turn the n dimensions of type type, starting at first
4332 * into existentially quantified variables.
4334 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4335 enum isl_dim_type type, unsigned first, unsigned n)
4337 int i;
4339 if (!map)
4340 return NULL;
4342 if (n == 0)
4343 return map_space_reset(map, type);
4345 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
4347 map = isl_map_cow(map);
4348 if (!map)
4349 return NULL;
4351 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4352 if (!map->dim)
4353 goto error;
4355 for (i = 0; i < map->n; ++i) {
4356 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4357 if (!map->p[i])
4358 goto error;
4361 return map;
4362 error:
4363 isl_map_free(map);
4364 return NULL;
4367 /* Turn the n dimensions of type type, starting at first
4368 * into existentially quantified variables.
4370 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4371 enum isl_dim_type type, unsigned first, unsigned n)
4373 return set_from_map(isl_map_project_out(set_to_map(set),
4374 type, first, n));
4377 /* Return a map that projects the elements in "set" onto their
4378 * "n" set dimensions starting at "first".
4379 * "type" should be equal to isl_dim_set.
4381 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4382 enum isl_dim_type type, unsigned first, unsigned n)
4384 int i;
4385 int dim;
4386 isl_map *map;
4388 if (!set)
4389 return NULL;
4390 if (type != isl_dim_set)
4391 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4392 "only set dimensions can be projected out", goto error);
4393 dim = isl_set_dim(set, isl_dim_set);
4394 if (first + n > dim || first + n < first)
4395 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4396 "index out of bounds", goto error);
4398 map = isl_map_from_domain(set);
4399 map = isl_map_add_dims(map, isl_dim_out, n);
4400 for (i = 0; i < n; ++i)
4401 map = isl_map_equate(map, isl_dim_in, first + i,
4402 isl_dim_out, i);
4403 return map;
4404 error:
4405 isl_set_free(set);
4406 return NULL;
4409 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4411 int i, j;
4413 for (i = 0; i < n; ++i) {
4414 j = isl_basic_map_alloc_div(bmap);
4415 if (j < 0)
4416 goto error;
4417 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4419 return bmap;
4420 error:
4421 isl_basic_map_free(bmap);
4422 return NULL;
4425 struct isl_basic_map *isl_basic_map_apply_range(
4426 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4428 isl_space *dim_result = NULL;
4429 struct isl_basic_map *bmap;
4430 unsigned n_in, n_out, n, nparam, total, pos;
4431 struct isl_dim_map *dim_map1, *dim_map2;
4433 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4434 goto error;
4435 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4436 bmap2->dim, isl_dim_in))
4437 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4438 "spaces don't match", goto error);
4440 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4441 isl_space_copy(bmap2->dim));
4443 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4444 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4445 n = isl_basic_map_dim(bmap1, isl_dim_out);
4446 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4448 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4449 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4450 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4451 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4452 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4453 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4454 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4455 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4456 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4457 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4458 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4460 bmap = isl_basic_map_alloc_space(dim_result,
4461 bmap1->n_div + bmap2->n_div + n,
4462 bmap1->n_eq + bmap2->n_eq,
4463 bmap1->n_ineq + bmap2->n_ineq);
4464 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4465 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4466 bmap = add_divs(bmap, n);
4467 bmap = isl_basic_map_simplify(bmap);
4468 bmap = isl_basic_map_drop_redundant_divs(bmap);
4469 return isl_basic_map_finalize(bmap);
4470 error:
4471 isl_basic_map_free(bmap1);
4472 isl_basic_map_free(bmap2);
4473 return NULL;
4476 struct isl_basic_set *isl_basic_set_apply(
4477 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4479 if (!bset || !bmap)
4480 goto error;
4482 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4483 goto error);
4485 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4486 bmap));
4487 error:
4488 isl_basic_set_free(bset);
4489 isl_basic_map_free(bmap);
4490 return NULL;
4493 struct isl_basic_map *isl_basic_map_apply_domain(
4494 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4496 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4497 goto error;
4498 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4499 bmap2->dim, isl_dim_in))
4500 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4501 "spaces don't match", goto error);
4503 bmap1 = isl_basic_map_reverse(bmap1);
4504 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4505 return isl_basic_map_reverse(bmap1);
4506 error:
4507 isl_basic_map_free(bmap1);
4508 isl_basic_map_free(bmap2);
4509 return NULL;
4512 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4513 * A \cap B -> f(A) + f(B)
4515 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4516 __isl_take isl_basic_map *bmap2)
4518 unsigned n_in, n_out, nparam, total, pos;
4519 struct isl_basic_map *bmap = NULL;
4520 struct isl_dim_map *dim_map1, *dim_map2;
4521 int i;
4523 if (!bmap1 || !bmap2)
4524 goto error;
4526 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4527 goto error);
4529 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4530 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4531 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4533 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4534 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4535 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4536 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4537 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4538 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4539 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4540 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4541 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4542 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4543 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4545 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4546 bmap1->n_div + bmap2->n_div + 2 * n_out,
4547 bmap1->n_eq + bmap2->n_eq + n_out,
4548 bmap1->n_ineq + bmap2->n_ineq);
4549 for (i = 0; i < n_out; ++i) {
4550 int j = isl_basic_map_alloc_equality(bmap);
4551 if (j < 0)
4552 goto error;
4553 isl_seq_clr(bmap->eq[j], 1+total);
4554 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4555 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4556 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4558 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4559 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4560 bmap = add_divs(bmap, 2 * n_out);
4562 bmap = isl_basic_map_simplify(bmap);
4563 return isl_basic_map_finalize(bmap);
4564 error:
4565 isl_basic_map_free(bmap);
4566 isl_basic_map_free(bmap1);
4567 isl_basic_map_free(bmap2);
4568 return NULL;
4571 /* Given two maps A -> f(A) and B -> g(B), construct a map
4572 * A \cap B -> f(A) + f(B)
4574 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4575 __isl_take isl_map *map2)
4577 struct isl_map *result;
4578 int i, j;
4580 if (!map1 || !map2)
4581 goto error;
4583 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4585 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4586 map1->n * map2->n, 0);
4587 if (!result)
4588 goto error;
4589 for (i = 0; i < map1->n; ++i)
4590 for (j = 0; j < map2->n; ++j) {
4591 struct isl_basic_map *part;
4592 part = isl_basic_map_sum(
4593 isl_basic_map_copy(map1->p[i]),
4594 isl_basic_map_copy(map2->p[j]));
4595 if (isl_basic_map_is_empty(part))
4596 isl_basic_map_free(part);
4597 else
4598 result = isl_map_add_basic_map(result, part);
4599 if (!result)
4600 goto error;
4602 isl_map_free(map1);
4603 isl_map_free(map2);
4604 return result;
4605 error:
4606 isl_map_free(map1);
4607 isl_map_free(map2);
4608 return NULL;
4611 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4612 __isl_take isl_set *set2)
4614 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4617 /* Given a basic map A -> f(A), construct A -> -f(A).
4619 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4621 int i, j;
4622 unsigned off, n;
4624 bmap = isl_basic_map_cow(bmap);
4625 if (!bmap)
4626 return NULL;
4628 n = isl_basic_map_dim(bmap, isl_dim_out);
4629 off = isl_basic_map_offset(bmap, isl_dim_out);
4630 for (i = 0; i < bmap->n_eq; ++i)
4631 for (j = 0; j < n; ++j)
4632 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4633 for (i = 0; i < bmap->n_ineq; ++i)
4634 for (j = 0; j < n; ++j)
4635 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4636 for (i = 0; i < bmap->n_div; ++i)
4637 for (j = 0; j < n; ++j)
4638 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4639 bmap = isl_basic_map_gauss(bmap, NULL);
4640 return isl_basic_map_finalize(bmap);
4643 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4645 return isl_basic_map_neg(bset);
4648 /* Given a map A -> f(A), construct A -> -f(A).
4650 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4652 int i;
4654 map = isl_map_cow(map);
4655 if (!map)
4656 return NULL;
4658 for (i = 0; i < map->n; ++i) {
4659 map->p[i] = isl_basic_map_neg(map->p[i]);
4660 if (!map->p[i])
4661 goto error;
4664 return map;
4665 error:
4666 isl_map_free(map);
4667 return NULL;
4670 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4672 return set_from_map(isl_map_neg(set_to_map(set)));
4675 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4676 * A -> floor(f(A)/d).
4678 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4679 isl_int d)
4681 unsigned n_in, n_out, nparam, total, pos;
4682 struct isl_basic_map *result = NULL;
4683 struct isl_dim_map *dim_map;
4684 int i;
4686 if (!bmap)
4687 return NULL;
4689 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4690 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4691 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4693 total = nparam + n_in + n_out + bmap->n_div + n_out;
4694 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4695 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4696 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4697 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4698 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4700 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4701 bmap->n_div + n_out,
4702 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4703 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4704 result = add_divs(result, n_out);
4705 for (i = 0; i < n_out; ++i) {
4706 int j;
4707 j = isl_basic_map_alloc_inequality(result);
4708 if (j < 0)
4709 goto error;
4710 isl_seq_clr(result->ineq[j], 1+total);
4711 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4712 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4713 j = isl_basic_map_alloc_inequality(result);
4714 if (j < 0)
4715 goto error;
4716 isl_seq_clr(result->ineq[j], 1+total);
4717 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4718 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4719 isl_int_sub_ui(result->ineq[j][0], d, 1);
4722 result = isl_basic_map_simplify(result);
4723 return isl_basic_map_finalize(result);
4724 error:
4725 isl_basic_map_free(result);
4726 return NULL;
4729 /* Given a map A -> f(A) and an integer d, construct a map
4730 * A -> floor(f(A)/d).
4732 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4734 int i;
4736 map = isl_map_cow(map);
4737 if (!map)
4738 return NULL;
4740 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4741 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4742 for (i = 0; i < map->n; ++i) {
4743 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4744 if (!map->p[i])
4745 goto error;
4748 return map;
4749 error:
4750 isl_map_free(map);
4751 return NULL;
4754 /* Given a map A -> f(A) and an integer d, construct a map
4755 * A -> floor(f(A)/d).
4757 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4758 __isl_take isl_val *d)
4760 if (!map || !d)
4761 goto error;
4762 if (!isl_val_is_int(d))
4763 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4764 "expecting integer denominator", goto error);
4765 map = isl_map_floordiv(map, d->n);
4766 isl_val_free(d);
4767 return map;
4768 error:
4769 isl_map_free(map);
4770 isl_val_free(d);
4771 return NULL;
4774 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4775 unsigned pos)
4777 int i;
4778 unsigned nparam;
4779 unsigned n_in;
4781 i = isl_basic_map_alloc_equality(bmap);
4782 if (i < 0)
4783 goto error;
4784 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4785 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4786 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4787 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4788 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4789 return isl_basic_map_finalize(bmap);
4790 error:
4791 isl_basic_map_free(bmap);
4792 return NULL;
4795 /* Add a constraint to "bmap" expressing i_pos < o_pos
4797 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4798 unsigned pos)
4800 int i;
4801 unsigned nparam;
4802 unsigned n_in;
4804 i = isl_basic_map_alloc_inequality(bmap);
4805 if (i < 0)
4806 goto error;
4807 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4808 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4809 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4810 isl_int_set_si(bmap->ineq[i][0], -1);
4811 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4812 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4813 return isl_basic_map_finalize(bmap);
4814 error:
4815 isl_basic_map_free(bmap);
4816 return NULL;
4819 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4821 static __isl_give isl_basic_map *var_less_or_equal(
4822 __isl_take isl_basic_map *bmap, unsigned pos)
4824 int i;
4825 unsigned nparam;
4826 unsigned n_in;
4828 i = isl_basic_map_alloc_inequality(bmap);
4829 if (i < 0)
4830 goto error;
4831 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4832 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4833 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4834 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4835 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4836 return isl_basic_map_finalize(bmap);
4837 error:
4838 isl_basic_map_free(bmap);
4839 return NULL;
4842 /* Add a constraint to "bmap" expressing i_pos > o_pos
4844 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4845 unsigned pos)
4847 int i;
4848 unsigned nparam;
4849 unsigned n_in;
4851 i = isl_basic_map_alloc_inequality(bmap);
4852 if (i < 0)
4853 goto error;
4854 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4855 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4856 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4857 isl_int_set_si(bmap->ineq[i][0], -1);
4858 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4859 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4860 return isl_basic_map_finalize(bmap);
4861 error:
4862 isl_basic_map_free(bmap);
4863 return NULL;
4866 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4868 static __isl_give isl_basic_map *var_more_or_equal(
4869 __isl_take isl_basic_map *bmap, unsigned pos)
4871 int i;
4872 unsigned nparam;
4873 unsigned n_in;
4875 i = isl_basic_map_alloc_inequality(bmap);
4876 if (i < 0)
4877 goto error;
4878 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4879 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4880 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4881 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4882 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4883 return isl_basic_map_finalize(bmap);
4884 error:
4885 isl_basic_map_free(bmap);
4886 return NULL;
4889 __isl_give isl_basic_map *isl_basic_map_equal(
4890 __isl_take isl_space *dim, unsigned n_equal)
4892 int i;
4893 struct isl_basic_map *bmap;
4894 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4895 if (!bmap)
4896 return NULL;
4897 for (i = 0; i < n_equal && bmap; ++i)
4898 bmap = var_equal(bmap, i);
4899 return isl_basic_map_finalize(bmap);
4902 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4904 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4905 unsigned pos)
4907 int i;
4908 struct isl_basic_map *bmap;
4909 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4910 if (!bmap)
4911 return NULL;
4912 for (i = 0; i < pos && bmap; ++i)
4913 bmap = var_equal(bmap, i);
4914 if (bmap)
4915 bmap = var_less(bmap, pos);
4916 return isl_basic_map_finalize(bmap);
4919 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4921 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4922 __isl_take isl_space *dim, unsigned pos)
4924 int i;
4925 isl_basic_map *bmap;
4927 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4928 for (i = 0; i < pos; ++i)
4929 bmap = var_equal(bmap, i);
4930 bmap = var_less_or_equal(bmap, pos);
4931 return isl_basic_map_finalize(bmap);
4934 /* Return a relation on "dim" expressing i_pos > o_pos
4936 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4937 unsigned pos)
4939 int i;
4940 struct isl_basic_map *bmap;
4941 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4942 if (!bmap)
4943 return NULL;
4944 for (i = 0; i < pos && bmap; ++i)
4945 bmap = var_equal(bmap, i);
4946 if (bmap)
4947 bmap = var_more(bmap, pos);
4948 return isl_basic_map_finalize(bmap);
4951 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4953 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4954 __isl_take isl_space *dim, unsigned pos)
4956 int i;
4957 isl_basic_map *bmap;
4959 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4960 for (i = 0; i < pos; ++i)
4961 bmap = var_equal(bmap, i);
4962 bmap = var_more_or_equal(bmap, pos);
4963 return isl_basic_map_finalize(bmap);
4966 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4967 unsigned n, int equal)
4969 struct isl_map *map;
4970 int i;
4972 if (n == 0 && equal)
4973 return isl_map_universe(dims);
4975 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4977 for (i = 0; i + 1 < n; ++i)
4978 map = isl_map_add_basic_map(map,
4979 isl_basic_map_less_at(isl_space_copy(dims), i));
4980 if (n > 0) {
4981 if (equal)
4982 map = isl_map_add_basic_map(map,
4983 isl_basic_map_less_or_equal_at(dims, n - 1));
4984 else
4985 map = isl_map_add_basic_map(map,
4986 isl_basic_map_less_at(dims, n - 1));
4987 } else
4988 isl_space_free(dims);
4990 return map;
4993 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4995 if (!dims)
4996 return NULL;
4997 return map_lex_lte_first(dims, dims->n_out, equal);
5000 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5002 return map_lex_lte_first(dim, n, 0);
5005 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5007 return map_lex_lte_first(dim, n, 1);
5010 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5012 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5015 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5017 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5020 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
5021 unsigned n, int equal)
5023 struct isl_map *map;
5024 int i;
5026 if (n == 0 && equal)
5027 return isl_map_universe(dims);
5029 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
5031 for (i = 0; i + 1 < n; ++i)
5032 map = isl_map_add_basic_map(map,
5033 isl_basic_map_more_at(isl_space_copy(dims), i));
5034 if (n > 0) {
5035 if (equal)
5036 map = isl_map_add_basic_map(map,
5037 isl_basic_map_more_or_equal_at(dims, n - 1));
5038 else
5039 map = isl_map_add_basic_map(map,
5040 isl_basic_map_more_at(dims, n - 1));
5041 } else
5042 isl_space_free(dims);
5044 return map;
5047 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
5049 if (!dims)
5050 return NULL;
5051 return map_lex_gte_first(dims, dims->n_out, equal);
5054 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5056 return map_lex_gte_first(dim, n, 0);
5059 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5061 return map_lex_gte_first(dim, n, 1);
5064 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5066 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5069 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5071 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5074 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5075 __isl_take isl_set *set2)
5077 isl_map *map;
5078 map = isl_map_lex_le(isl_set_get_space(set1));
5079 map = isl_map_intersect_domain(map, set1);
5080 map = isl_map_intersect_range(map, set2);
5081 return map;
5084 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5085 __isl_take isl_set *set2)
5087 isl_map *map;
5088 map = isl_map_lex_lt(isl_set_get_space(set1));
5089 map = isl_map_intersect_domain(map, set1);
5090 map = isl_map_intersect_range(map, set2);
5091 return map;
5094 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5095 __isl_take isl_set *set2)
5097 isl_map *map;
5098 map = isl_map_lex_ge(isl_set_get_space(set1));
5099 map = isl_map_intersect_domain(map, set1);
5100 map = isl_map_intersect_range(map, set2);
5101 return map;
5104 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5105 __isl_take isl_set *set2)
5107 isl_map *map;
5108 map = isl_map_lex_gt(isl_set_get_space(set1));
5109 map = isl_map_intersect_domain(map, set1);
5110 map = isl_map_intersect_range(map, set2);
5111 return map;
5114 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5115 __isl_take isl_map *map2)
5117 isl_map *map;
5118 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5119 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5120 map = isl_map_apply_range(map, isl_map_reverse(map2));
5121 return map;
5124 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5125 __isl_take isl_map *map2)
5127 isl_map *map;
5128 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5129 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5130 map = isl_map_apply_range(map, isl_map_reverse(map2));
5131 return map;
5134 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5135 __isl_take isl_map *map2)
5137 isl_map *map;
5138 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5139 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5140 map = isl_map_apply_range(map, isl_map_reverse(map2));
5141 return map;
5144 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5145 __isl_take isl_map *map2)
5147 isl_map *map;
5148 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5149 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5150 map = isl_map_apply_range(map, isl_map_reverse(map2));
5151 return map;
5154 /* For a div d = floor(f/m), add the constraint
5156 * f - m d >= 0
5158 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5159 unsigned pos, isl_int *div)
5161 int i;
5162 unsigned total = isl_basic_map_total_dim(bmap);
5164 i = isl_basic_map_alloc_inequality(bmap);
5165 if (i < 0)
5166 return isl_stat_error;
5167 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5168 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5170 return isl_stat_ok;
5173 /* For a div d = floor(f/m), add the constraint
5175 * -(f-(m-1)) + m d >= 0
5177 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5178 unsigned pos, isl_int *div)
5180 int i;
5181 unsigned total = isl_basic_map_total_dim(bmap);
5183 i = isl_basic_map_alloc_inequality(bmap);
5184 if (i < 0)
5185 return isl_stat_error;
5186 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5187 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5188 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5189 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5191 return isl_stat_ok;
5194 /* For a div d = floor(f/m), add the constraints
5196 * f - m d >= 0
5197 * -(f-(m-1)) + m d >= 0
5199 * Note that the second constraint is the negation of
5201 * f - m d >= m
5203 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5204 unsigned pos, isl_int *div)
5206 if (add_upper_div_constraint(bmap, pos, div) < 0)
5207 return -1;
5208 if (add_lower_div_constraint(bmap, pos, div) < 0)
5209 return -1;
5210 return 0;
5213 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5214 unsigned pos, isl_int *div)
5216 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5217 pos, div);
5220 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
5222 unsigned total = isl_basic_map_total_dim(bmap);
5223 unsigned div_pos = total - bmap->n_div + div;
5225 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5226 bmap->div[div]);
5229 /* For each known div d = floor(f/m), add the constraints
5231 * f - m d >= 0
5232 * -(f-(m-1)) + m d >= 0
5234 * Remove duplicate constraints in case of some these div constraints
5235 * already appear in "bmap".
5237 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5238 __isl_take isl_basic_map *bmap)
5240 unsigned n_div;
5242 if (!bmap)
5243 return NULL;
5244 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5245 if (n_div == 0)
5246 return bmap;
5248 bmap = add_known_div_constraints(bmap);
5249 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5250 bmap = isl_basic_map_finalize(bmap);
5251 return bmap;
5254 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5256 * In particular, if this div is of the form d = floor(f/m),
5257 * then add the constraint
5259 * f - m d >= 0
5261 * if sign < 0 or the constraint
5263 * -(f-(m-1)) + m d >= 0
5265 * if sign > 0.
5267 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5268 unsigned div, int sign)
5270 unsigned total;
5271 unsigned div_pos;
5273 if (!bmap)
5274 return -1;
5276 total = isl_basic_map_total_dim(bmap);
5277 div_pos = total - bmap->n_div + div;
5279 if (sign < 0)
5280 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5281 else
5282 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5285 struct isl_basic_set *isl_basic_map_underlying_set(
5286 struct isl_basic_map *bmap)
5288 if (!bmap)
5289 goto error;
5290 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5291 bmap->n_div == 0 &&
5292 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5293 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5294 return bset_from_bmap(bmap);
5295 bmap = isl_basic_map_cow(bmap);
5296 if (!bmap)
5297 goto error;
5298 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5299 if (!bmap->dim)
5300 goto error;
5301 bmap->extra -= bmap->n_div;
5302 bmap->n_div = 0;
5303 bmap = isl_basic_map_finalize(bmap);
5304 return bset_from_bmap(bmap);
5305 error:
5306 isl_basic_map_free(bmap);
5307 return NULL;
5310 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5311 __isl_take isl_basic_set *bset)
5313 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5316 /* Replace each element in "list" by the result of applying
5317 * isl_basic_map_underlying_set to the element.
5319 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5320 __isl_take isl_basic_map_list *list)
5322 int i, n;
5324 if (!list)
5325 return NULL;
5327 n = isl_basic_map_list_n_basic_map(list);
5328 for (i = 0; i < n; ++i) {
5329 isl_basic_map *bmap;
5330 isl_basic_set *bset;
5332 bmap = isl_basic_map_list_get_basic_map(list, i);
5333 bset = isl_basic_set_underlying_set(bmap);
5334 list = isl_basic_set_list_set_basic_set(list, i, bset);
5337 return list;
5340 struct isl_basic_map *isl_basic_map_overlying_set(
5341 struct isl_basic_set *bset, struct isl_basic_map *like)
5343 struct isl_basic_map *bmap;
5344 struct isl_ctx *ctx;
5345 unsigned total;
5346 int i;
5348 if (!bset || !like)
5349 goto error;
5350 ctx = bset->ctx;
5351 isl_assert(ctx, bset->n_div == 0, goto error);
5352 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5353 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5354 goto error);
5355 if (like->n_div == 0) {
5356 isl_space *space = isl_basic_map_get_space(like);
5357 isl_basic_map_free(like);
5358 return isl_basic_map_reset_space(bset, space);
5360 bset = isl_basic_set_cow(bset);
5361 if (!bset)
5362 goto error;
5363 total = bset->dim->n_out + bset->extra;
5364 bmap = bset_to_bmap(bset);
5365 isl_space_free(bmap->dim);
5366 bmap->dim = isl_space_copy(like->dim);
5367 if (!bmap->dim)
5368 goto error;
5369 bmap->n_div = like->n_div;
5370 bmap->extra += like->n_div;
5371 if (bmap->extra) {
5372 unsigned ltotal;
5373 isl_int **div;
5374 ltotal = total - bmap->extra + like->extra;
5375 if (ltotal > total)
5376 ltotal = total;
5377 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5378 bmap->extra * (1 + 1 + total));
5379 if (isl_blk_is_error(bmap->block2))
5380 goto error;
5381 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5382 if (!div)
5383 goto error;
5384 bmap->div = div;
5385 for (i = 0; i < bmap->extra; ++i)
5386 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5387 for (i = 0; i < like->n_div; ++i) {
5388 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5389 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5391 bmap = isl_basic_map_add_known_div_constraints(bmap);
5393 isl_basic_map_free(like);
5394 bmap = isl_basic_map_simplify(bmap);
5395 bmap = isl_basic_map_finalize(bmap);
5396 return bmap;
5397 error:
5398 isl_basic_map_free(like);
5399 isl_basic_set_free(bset);
5400 return NULL;
5403 struct isl_basic_set *isl_basic_set_from_underlying_set(
5404 struct isl_basic_set *bset, struct isl_basic_set *like)
5406 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5407 bset_to_bmap(like)));
5410 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5412 int i;
5414 map = isl_map_cow(map);
5415 if (!map)
5416 return NULL;
5417 map->dim = isl_space_cow(map->dim);
5418 if (!map->dim)
5419 goto error;
5421 for (i = 1; i < map->n; ++i)
5422 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5423 goto error);
5424 for (i = 0; i < map->n; ++i) {
5425 map->p[i] = bset_to_bmap(
5426 isl_basic_map_underlying_set(map->p[i]));
5427 if (!map->p[i])
5428 goto error;
5430 if (map->n == 0)
5431 map->dim = isl_space_underlying(map->dim, 0);
5432 else {
5433 isl_space_free(map->dim);
5434 map->dim = isl_space_copy(map->p[0]->dim);
5436 if (!map->dim)
5437 goto error;
5438 return set_from_map(map);
5439 error:
5440 isl_map_free(map);
5441 return NULL;
5444 /* Replace the space of "bmap" by "space".
5446 * If the space of "bmap" is identical to "space" (including the identifiers
5447 * of the input and output dimensions), then simply return the original input.
5449 __isl_give isl_basic_map *isl_basic_map_reset_space(
5450 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5452 isl_bool equal;
5453 isl_space *bmap_space;
5455 bmap_space = isl_basic_map_peek_space(bmap);
5456 equal = isl_space_is_equal(bmap_space, space);
5457 if (equal >= 0 && equal)
5458 equal = isl_space_has_equal_ids(bmap_space, space);
5459 if (equal < 0)
5460 goto error;
5461 if (equal) {
5462 isl_space_free(space);
5463 return bmap;
5465 bmap = isl_basic_map_cow(bmap);
5466 if (!bmap || !space)
5467 goto error;
5469 isl_space_free(bmap->dim);
5470 bmap->dim = space;
5472 bmap = isl_basic_map_finalize(bmap);
5474 return bmap;
5475 error:
5476 isl_basic_map_free(bmap);
5477 isl_space_free(space);
5478 return NULL;
5481 __isl_give isl_basic_set *isl_basic_set_reset_space(
5482 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5484 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5485 dim));
5488 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5489 __isl_take isl_space *dim)
5491 int i;
5493 map = isl_map_cow(map);
5494 if (!map || !dim)
5495 goto error;
5497 for (i = 0; i < map->n; ++i) {
5498 map->p[i] = isl_basic_map_reset_space(map->p[i],
5499 isl_space_copy(dim));
5500 if (!map->p[i])
5501 goto error;
5503 isl_space_free(map->dim);
5504 map->dim = dim;
5506 return map;
5507 error:
5508 isl_map_free(map);
5509 isl_space_free(dim);
5510 return NULL;
5513 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5514 __isl_take isl_space *dim)
5516 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5519 /* Compute the parameter domain of the given basic set.
5521 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5523 isl_bool is_params;
5524 isl_space *space;
5525 unsigned n;
5527 is_params = isl_basic_set_is_params(bset);
5528 if (is_params < 0)
5529 return isl_basic_set_free(bset);
5530 if (is_params)
5531 return bset;
5533 n = isl_basic_set_dim(bset, isl_dim_set);
5534 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5535 space = isl_basic_set_get_space(bset);
5536 space = isl_space_params(space);
5537 bset = isl_basic_set_reset_space(bset, space);
5538 return bset;
5541 /* Construct a zero-dimensional basic set with the given parameter domain.
5543 __isl_give isl_basic_set *isl_basic_set_from_params(
5544 __isl_take isl_basic_set *bset)
5546 isl_space *space;
5547 space = isl_basic_set_get_space(bset);
5548 space = isl_space_set_from_params(space);
5549 bset = isl_basic_set_reset_space(bset, space);
5550 return bset;
5553 /* Compute the parameter domain of the given set.
5555 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5557 isl_space *space;
5558 unsigned n;
5560 if (isl_set_is_params(set))
5561 return set;
5563 n = isl_set_dim(set, isl_dim_set);
5564 set = isl_set_project_out(set, isl_dim_set, 0, n);
5565 space = isl_set_get_space(set);
5566 space = isl_space_params(space);
5567 set = isl_set_reset_space(set, space);
5568 return set;
5571 /* Construct a zero-dimensional set with the given parameter domain.
5573 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5575 isl_space *space;
5576 space = isl_set_get_space(set);
5577 space = isl_space_set_from_params(space);
5578 set = isl_set_reset_space(set, space);
5579 return set;
5582 /* Compute the parameter domain of the given map.
5584 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5586 isl_space *space;
5587 unsigned n;
5589 n = isl_map_dim(map, isl_dim_in);
5590 map = isl_map_project_out(map, isl_dim_in, 0, n);
5591 n = isl_map_dim(map, isl_dim_out);
5592 map = isl_map_project_out(map, isl_dim_out, 0, n);
5593 space = isl_map_get_space(map);
5594 space = isl_space_params(space);
5595 map = isl_map_reset_space(map, space);
5596 return map;
5599 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5601 isl_space *space;
5602 unsigned n_out;
5604 if (!bmap)
5605 return NULL;
5606 space = isl_space_domain(isl_basic_map_get_space(bmap));
5608 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5609 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5611 return isl_basic_map_reset_space(bmap, space);
5614 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5616 if (!bmap)
5617 return isl_bool_error;
5618 return isl_space_may_be_set(bmap->dim);
5621 /* Is this basic map actually a set?
5622 * Users should never call this function. Outside of isl,
5623 * the type should indicate whether something is a set or a map.
5625 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5627 if (!bmap)
5628 return isl_bool_error;
5629 return isl_space_is_set(bmap->dim);
5632 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5634 isl_bool is_set;
5636 is_set = isl_basic_map_is_set(bmap);
5637 if (is_set < 0)
5638 goto error;
5639 if (is_set)
5640 return bmap;
5641 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5642 error:
5643 isl_basic_map_free(bmap);
5644 return NULL;
5647 __isl_give isl_basic_map *isl_basic_map_domain_map(
5648 __isl_take isl_basic_map *bmap)
5650 int i;
5651 isl_space *dim;
5652 isl_basic_map *domain;
5653 int nparam, n_in, n_out;
5655 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5656 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5657 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5659 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5660 domain = isl_basic_map_universe(dim);
5662 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5663 bmap = isl_basic_map_apply_range(bmap, domain);
5664 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5666 for (i = 0; i < n_in; ++i)
5667 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5668 isl_dim_out, i);
5670 bmap = isl_basic_map_gauss(bmap, NULL);
5671 return isl_basic_map_finalize(bmap);
5674 __isl_give isl_basic_map *isl_basic_map_range_map(
5675 __isl_take isl_basic_map *bmap)
5677 int i;
5678 isl_space *dim;
5679 isl_basic_map *range;
5680 int nparam, n_in, n_out;
5682 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5683 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5684 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5686 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5687 range = isl_basic_map_universe(dim);
5689 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5690 bmap = isl_basic_map_apply_range(bmap, range);
5691 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5693 for (i = 0; i < n_out; ++i)
5694 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5695 isl_dim_out, i);
5697 bmap = isl_basic_map_gauss(bmap, NULL);
5698 return isl_basic_map_finalize(bmap);
5701 int isl_map_may_be_set(__isl_keep isl_map *map)
5703 if (!map)
5704 return -1;
5705 return isl_space_may_be_set(map->dim);
5708 /* Is this map actually a set?
5709 * Users should never call this function. Outside of isl,
5710 * the type should indicate whether something is a set or a map.
5712 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5714 if (!map)
5715 return isl_bool_error;
5716 return isl_space_is_set(map->dim);
5719 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5721 int i;
5722 isl_bool is_set;
5723 struct isl_set *set;
5725 is_set = isl_map_is_set(map);
5726 if (is_set < 0)
5727 goto error;
5728 if (is_set)
5729 return set_from_map(map);
5731 map = isl_map_cow(map);
5732 if (!map)
5733 goto error;
5735 set = set_from_map(map);
5736 set->dim = isl_space_range(set->dim);
5737 if (!set->dim)
5738 goto error;
5739 for (i = 0; i < map->n; ++i) {
5740 set->p[i] = isl_basic_map_range(map->p[i]);
5741 if (!set->p[i])
5742 goto error;
5744 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5745 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5746 return set;
5747 error:
5748 isl_map_free(map);
5749 return NULL;
5752 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5754 int i;
5756 map = isl_map_cow(map);
5757 if (!map)
5758 return NULL;
5760 map->dim = isl_space_domain_map(map->dim);
5761 if (!map->dim)
5762 goto error;
5763 for (i = 0; i < map->n; ++i) {
5764 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5765 if (!map->p[i])
5766 goto error;
5768 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5769 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5770 return map;
5771 error:
5772 isl_map_free(map);
5773 return NULL;
5776 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5778 int i;
5779 isl_space *range_dim;
5781 map = isl_map_cow(map);
5782 if (!map)
5783 return NULL;
5785 range_dim = isl_space_range(isl_map_get_space(map));
5786 range_dim = isl_space_from_range(range_dim);
5787 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5788 map->dim = isl_space_join(map->dim, range_dim);
5789 if (!map->dim)
5790 goto error;
5791 for (i = 0; i < map->n; ++i) {
5792 map->p[i] = isl_basic_map_range_map(map->p[i]);
5793 if (!map->p[i])
5794 goto error;
5796 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5797 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5798 return map;
5799 error:
5800 isl_map_free(map);
5801 return NULL;
5804 /* Given a wrapped map of the form A[B -> C],
5805 * return the map A[B -> C] -> B.
5807 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5809 isl_id *id;
5810 isl_map *map;
5812 if (!set)
5813 return NULL;
5814 if (!isl_set_has_tuple_id(set))
5815 return isl_map_domain_map(isl_set_unwrap(set));
5817 id = isl_set_get_tuple_id(set);
5818 map = isl_map_domain_map(isl_set_unwrap(set));
5819 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5821 return map;
5824 __isl_give isl_basic_map *isl_basic_map_from_domain(
5825 __isl_take isl_basic_set *bset)
5827 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5830 __isl_give isl_basic_map *isl_basic_map_from_range(
5831 __isl_take isl_basic_set *bset)
5833 isl_space *space;
5834 space = isl_basic_set_get_space(bset);
5835 space = isl_space_from_range(space);
5836 bset = isl_basic_set_reset_space(bset, space);
5837 return bset_to_bmap(bset);
5840 /* Create a relation with the given set as range.
5841 * The domain of the created relation is a zero-dimensional
5842 * flat anonymous space.
5844 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5846 isl_space *space;
5847 space = isl_set_get_space(set);
5848 space = isl_space_from_range(space);
5849 set = isl_set_reset_space(set, space);
5850 return set_to_map(set);
5853 /* Create a relation with the given set as domain.
5854 * The range of the created relation is a zero-dimensional
5855 * flat anonymous space.
5857 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5859 return isl_map_reverse(isl_map_from_range(set));
5862 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5863 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5865 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5868 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5869 __isl_take isl_set *range)
5871 return isl_map_apply_range(isl_map_reverse(domain), range);
5874 /* Return a newly allocated isl_map with given space and flags and
5875 * room for "n" basic maps.
5876 * Make sure that all cached information is cleared.
5878 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5879 unsigned flags)
5881 struct isl_map *map;
5883 if (!space)
5884 return NULL;
5885 if (n < 0)
5886 isl_die(space->ctx, isl_error_internal,
5887 "negative number of basic maps", goto error);
5888 map = isl_calloc(space->ctx, struct isl_map,
5889 sizeof(struct isl_map) +
5890 (n - 1) * sizeof(struct isl_basic_map *));
5891 if (!map)
5892 goto error;
5894 map->ctx = space->ctx;
5895 isl_ctx_ref(map->ctx);
5896 map->ref = 1;
5897 map->size = n;
5898 map->n = 0;
5899 map->dim = space;
5900 map->flags = flags;
5901 return map;
5902 error:
5903 isl_space_free(space);
5904 return NULL;
5907 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5909 struct isl_basic_map *bmap;
5910 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5911 bmap = isl_basic_map_set_to_empty(bmap);
5912 return bmap;
5915 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5917 struct isl_basic_set *bset;
5918 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5919 bset = isl_basic_set_set_to_empty(bset);
5920 return bset;
5923 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5925 struct isl_basic_map *bmap;
5926 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5927 bmap = isl_basic_map_finalize(bmap);
5928 return bmap;
5931 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5933 struct isl_basic_set *bset;
5934 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5935 bset = isl_basic_set_finalize(bset);
5936 return bset;
5939 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5941 int i;
5942 unsigned total = isl_space_dim(dim, isl_dim_all);
5943 isl_basic_map *bmap;
5945 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5946 for (i = 0; i < total; ++i) {
5947 int k = isl_basic_map_alloc_inequality(bmap);
5948 if (k < 0)
5949 goto error;
5950 isl_seq_clr(bmap->ineq[k], 1 + total);
5951 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5953 return bmap;
5954 error:
5955 isl_basic_map_free(bmap);
5956 return NULL;
5959 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5961 return isl_basic_map_nat_universe(dim);
5964 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5966 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5969 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5971 return isl_map_nat_universe(dim);
5974 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5976 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5979 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5981 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5984 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5986 struct isl_map *map;
5987 if (!dim)
5988 return NULL;
5989 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5990 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5991 return map;
5994 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5996 struct isl_set *set;
5997 if (!dim)
5998 return NULL;
5999 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
6000 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
6001 return set;
6004 struct isl_map *isl_map_dup(struct isl_map *map)
6006 int i;
6007 struct isl_map *dup;
6009 if (!map)
6010 return NULL;
6011 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6012 for (i = 0; i < map->n; ++i)
6013 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6014 return dup;
6017 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6018 __isl_take isl_basic_map *bmap)
6020 if (!bmap || !map)
6021 goto error;
6022 if (isl_basic_map_plain_is_empty(bmap)) {
6023 isl_basic_map_free(bmap);
6024 return map;
6026 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6027 isl_assert(map->ctx, map->n < map->size, goto error);
6028 map->p[map->n] = bmap;
6029 map->n++;
6030 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6031 return map;
6032 error:
6033 if (map)
6034 isl_map_free(map);
6035 if (bmap)
6036 isl_basic_map_free(bmap);
6037 return NULL;
6040 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6042 int i;
6044 if (!map)
6045 return NULL;
6047 if (--map->ref > 0)
6048 return NULL;
6050 clear_caches(map);
6051 isl_ctx_deref(map->ctx);
6052 for (i = 0; i < map->n; ++i)
6053 isl_basic_map_free(map->p[i]);
6054 isl_space_free(map->dim);
6055 free(map);
6057 return NULL;
6060 static struct isl_basic_map *isl_basic_map_fix_pos_si(
6061 struct isl_basic_map *bmap, unsigned pos, int value)
6063 int j;
6065 bmap = isl_basic_map_cow(bmap);
6066 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6067 j = isl_basic_map_alloc_equality(bmap);
6068 if (j < 0)
6069 goto error;
6070 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6071 isl_int_set_si(bmap->eq[j][pos], -1);
6072 isl_int_set_si(bmap->eq[j][0], value);
6073 bmap = isl_basic_map_simplify(bmap);
6074 return isl_basic_map_finalize(bmap);
6075 error:
6076 isl_basic_map_free(bmap);
6077 return NULL;
6080 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6081 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6083 int j;
6085 bmap = isl_basic_map_cow(bmap);
6086 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6087 j = isl_basic_map_alloc_equality(bmap);
6088 if (j < 0)
6089 goto error;
6090 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6091 isl_int_set_si(bmap->eq[j][pos], -1);
6092 isl_int_set(bmap->eq[j][0], value);
6093 bmap = isl_basic_map_simplify(bmap);
6094 return isl_basic_map_finalize(bmap);
6095 error:
6096 isl_basic_map_free(bmap);
6097 return NULL;
6100 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6101 enum isl_dim_type type, unsigned pos, int value)
6103 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6104 return isl_basic_map_free(bmap);
6105 return isl_basic_map_fix_pos_si(bmap,
6106 isl_basic_map_offset(bmap, type) + pos, value);
6109 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6110 enum isl_dim_type type, unsigned pos, isl_int value)
6112 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6113 return isl_basic_map_free(bmap);
6114 return isl_basic_map_fix_pos(bmap,
6115 isl_basic_map_offset(bmap, type) + pos, value);
6118 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6119 * to be equal to "v".
6121 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6122 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6124 if (!bmap || !v)
6125 goto error;
6126 if (!isl_val_is_int(v))
6127 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6128 "expecting integer value", goto error);
6129 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6130 goto error;
6131 pos += isl_basic_map_offset(bmap, type);
6132 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6133 isl_val_free(v);
6134 return bmap;
6135 error:
6136 isl_basic_map_free(bmap);
6137 isl_val_free(v);
6138 return NULL;
6141 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6142 * to be equal to "v".
6144 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6145 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6147 return isl_basic_map_fix_val(bset, type, pos, v);
6150 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6151 enum isl_dim_type type, unsigned pos, int value)
6153 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6154 type, pos, value));
6157 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6158 enum isl_dim_type type, unsigned pos, isl_int value)
6160 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6161 type, pos, value));
6164 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6165 unsigned input, int value)
6167 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6170 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6171 unsigned dim, int value)
6173 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6174 isl_dim_set, dim, value));
6177 static int remove_if_empty(__isl_keep isl_map *map, int i)
6179 int empty = isl_basic_map_plain_is_empty(map->p[i]);
6181 if (empty < 0)
6182 return -1;
6183 if (!empty)
6184 return 0;
6186 isl_basic_map_free(map->p[i]);
6187 if (i != map->n - 1) {
6188 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6189 map->p[i] = map->p[map->n - 1];
6191 map->n--;
6193 return 0;
6196 /* Perform "fn" on each basic map of "map", where we may not be holding
6197 * the only reference to "map".
6198 * In particular, "fn" should be a semantics preserving operation
6199 * that we want to apply to all copies of "map". We therefore need
6200 * to be careful not to modify "map" in a way that breaks "map"
6201 * in case anything goes wrong.
6203 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6204 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6206 struct isl_basic_map *bmap;
6207 int i;
6209 if (!map)
6210 return NULL;
6212 for (i = map->n - 1; i >= 0; --i) {
6213 bmap = isl_basic_map_copy(map->p[i]);
6214 bmap = fn(bmap);
6215 if (!bmap)
6216 goto error;
6217 isl_basic_map_free(map->p[i]);
6218 map->p[i] = bmap;
6219 if (remove_if_empty(map, i) < 0)
6220 goto error;
6223 return map;
6224 error:
6225 isl_map_free(map);
6226 return NULL;
6229 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6230 enum isl_dim_type type, unsigned pos, int value)
6232 int i;
6234 map = isl_map_cow(map);
6235 if (!map)
6236 return NULL;
6238 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6239 for (i = map->n - 1; i >= 0; --i) {
6240 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6241 if (remove_if_empty(map, i) < 0)
6242 goto error;
6244 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6245 return map;
6246 error:
6247 isl_map_free(map);
6248 return NULL;
6251 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6252 enum isl_dim_type type, unsigned pos, int value)
6254 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6257 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6258 enum isl_dim_type type, unsigned pos, isl_int value)
6260 int i;
6262 map = isl_map_cow(map);
6263 if (!map)
6264 return NULL;
6266 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6267 for (i = 0; i < map->n; ++i) {
6268 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6269 if (!map->p[i])
6270 goto error;
6272 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6273 return map;
6274 error:
6275 isl_map_free(map);
6276 return NULL;
6279 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6280 enum isl_dim_type type, unsigned pos, isl_int value)
6282 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6285 /* Fix the value of the variable at position "pos" of type "type" of "map"
6286 * to be equal to "v".
6288 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6289 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6291 int i;
6293 map = isl_map_cow(map);
6294 if (!map || !v)
6295 goto error;
6297 if (!isl_val_is_int(v))
6298 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6299 "expecting integer value", goto error);
6300 if (pos >= isl_map_dim(map, type))
6301 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6302 "index out of bounds", goto error);
6303 for (i = map->n - 1; i >= 0; --i) {
6304 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6305 isl_val_copy(v));
6306 if (remove_if_empty(map, i) < 0)
6307 goto error;
6309 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6310 isl_val_free(v);
6311 return map;
6312 error:
6313 isl_map_free(map);
6314 isl_val_free(v);
6315 return NULL;
6318 /* Fix the value of the variable at position "pos" of type "type" of "set"
6319 * to be equal to "v".
6321 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6322 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6324 return isl_map_fix_val(set, type, pos, v);
6327 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6328 unsigned input, int value)
6330 return isl_map_fix_si(map, isl_dim_in, input, value);
6333 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6335 return set_from_map(isl_map_fix_si(set_to_map(set),
6336 isl_dim_set, dim, value));
6339 static __isl_give isl_basic_map *basic_map_bound_si(
6340 __isl_take isl_basic_map *bmap,
6341 enum isl_dim_type type, unsigned pos, int value, int upper)
6343 int j;
6345 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6346 return isl_basic_map_free(bmap);
6347 pos += isl_basic_map_offset(bmap, type);
6348 bmap = isl_basic_map_cow(bmap);
6349 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6350 j = isl_basic_map_alloc_inequality(bmap);
6351 if (j < 0)
6352 goto error;
6353 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6354 if (upper) {
6355 isl_int_set_si(bmap->ineq[j][pos], -1);
6356 isl_int_set_si(bmap->ineq[j][0], value);
6357 } else {
6358 isl_int_set_si(bmap->ineq[j][pos], 1);
6359 isl_int_set_si(bmap->ineq[j][0], -value);
6361 bmap = isl_basic_map_simplify(bmap);
6362 return isl_basic_map_finalize(bmap);
6363 error:
6364 isl_basic_map_free(bmap);
6365 return NULL;
6368 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6369 __isl_take isl_basic_map *bmap,
6370 enum isl_dim_type type, unsigned pos, int value)
6372 return basic_map_bound_si(bmap, type, pos, value, 0);
6375 /* Constrain the values of the given dimension to be no greater than "value".
6377 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6378 __isl_take isl_basic_map *bmap,
6379 enum isl_dim_type type, unsigned pos, int value)
6381 return basic_map_bound_si(bmap, type, pos, value, 1);
6384 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6385 enum isl_dim_type type, unsigned pos, int value, int upper)
6387 int i;
6389 map = isl_map_cow(map);
6390 if (!map)
6391 return NULL;
6393 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6394 for (i = 0; i < map->n; ++i) {
6395 map->p[i] = basic_map_bound_si(map->p[i],
6396 type, pos, value, upper);
6397 if (!map->p[i])
6398 goto error;
6400 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6401 return map;
6402 error:
6403 isl_map_free(map);
6404 return NULL;
6407 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6408 enum isl_dim_type type, unsigned pos, int value)
6410 return map_bound_si(map, type, pos, value, 0);
6413 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6414 enum isl_dim_type type, unsigned pos, int value)
6416 return map_bound_si(map, type, pos, value, 1);
6419 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6420 enum isl_dim_type type, unsigned pos, int value)
6422 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6423 type, pos, value));
6426 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6427 enum isl_dim_type type, unsigned pos, int value)
6429 return isl_map_upper_bound_si(set, type, pos, value);
6432 /* Bound the given variable of "bmap" from below (or above is "upper"
6433 * is set) to "value".
6435 static __isl_give isl_basic_map *basic_map_bound(
6436 __isl_take isl_basic_map *bmap,
6437 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6439 int j;
6441 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6442 return isl_basic_map_free(bmap);
6443 pos += isl_basic_map_offset(bmap, type);
6444 bmap = isl_basic_map_cow(bmap);
6445 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6446 j = isl_basic_map_alloc_inequality(bmap);
6447 if (j < 0)
6448 goto error;
6449 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6450 if (upper) {
6451 isl_int_set_si(bmap->ineq[j][pos], -1);
6452 isl_int_set(bmap->ineq[j][0], value);
6453 } else {
6454 isl_int_set_si(bmap->ineq[j][pos], 1);
6455 isl_int_neg(bmap->ineq[j][0], value);
6457 bmap = isl_basic_map_simplify(bmap);
6458 return isl_basic_map_finalize(bmap);
6459 error:
6460 isl_basic_map_free(bmap);
6461 return NULL;
6464 /* Bound the given variable of "map" from below (or above is "upper"
6465 * is set) to "value".
6467 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6468 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6470 int i;
6472 map = isl_map_cow(map);
6473 if (!map)
6474 return NULL;
6476 if (pos >= isl_map_dim(map, type))
6477 isl_die(map->ctx, isl_error_invalid,
6478 "index out of bounds", goto error);
6479 for (i = map->n - 1; i >= 0; --i) {
6480 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6481 if (remove_if_empty(map, i) < 0)
6482 goto error;
6484 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6485 return map;
6486 error:
6487 isl_map_free(map);
6488 return NULL;
6491 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6492 enum isl_dim_type type, unsigned pos, isl_int value)
6494 return map_bound(map, type, pos, value, 0);
6497 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6498 enum isl_dim_type type, unsigned pos, isl_int value)
6500 return map_bound(map, type, pos, value, 1);
6503 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6504 enum isl_dim_type type, unsigned pos, isl_int value)
6506 return isl_map_lower_bound(set, type, pos, value);
6509 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6510 enum isl_dim_type type, unsigned pos, isl_int value)
6512 return isl_map_upper_bound(set, type, pos, value);
6515 /* Force the values of the variable at position "pos" of type "type" of "set"
6516 * to be no smaller than "value".
6518 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6519 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6521 if (!value)
6522 goto error;
6523 if (!isl_val_is_int(value))
6524 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6525 "expecting integer value", goto error);
6526 set = isl_set_lower_bound(set, type, pos, value->n);
6527 isl_val_free(value);
6528 return set;
6529 error:
6530 isl_val_free(value);
6531 isl_set_free(set);
6532 return NULL;
6535 /* Force the values of the variable at position "pos" of type "type" of "set"
6536 * to be no greater than "value".
6538 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6539 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6541 if (!value)
6542 goto error;
6543 if (!isl_val_is_int(value))
6544 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6545 "expecting integer value", goto error);
6546 set = isl_set_upper_bound(set, type, pos, value->n);
6547 isl_val_free(value);
6548 return set;
6549 error:
6550 isl_val_free(value);
6551 isl_set_free(set);
6552 return NULL;
6555 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6557 int i;
6559 map = isl_map_cow(map);
6560 if (!map)
6561 return NULL;
6563 map->dim = isl_space_reverse(map->dim);
6564 if (!map->dim)
6565 goto error;
6566 for (i = 0; i < map->n; ++i) {
6567 map->p[i] = isl_basic_map_reverse(map->p[i]);
6568 if (!map->p[i])
6569 goto error;
6571 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6572 return map;
6573 error:
6574 isl_map_free(map);
6575 return NULL;
6578 #undef TYPE
6579 #define TYPE isl_pw_multi_aff
6580 #undef SUFFIX
6581 #define SUFFIX _pw_multi_aff
6582 #undef EMPTY
6583 #define EMPTY isl_pw_multi_aff_empty
6584 #undef ADD
6585 #define ADD isl_pw_multi_aff_union_add
6586 #include "isl_map_lexopt_templ.c"
6588 /* Given a map "map", compute the lexicographically minimal
6589 * (or maximal) image element for each domain element in dom,
6590 * in the form of an isl_pw_multi_aff.
6591 * If "empty" is not NULL, then set *empty to those elements in dom that
6592 * do not have an image element.
6593 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6594 * should be computed over the domain of "map". "empty" is also NULL
6595 * in this case.
6597 * We first compute the lexicographically minimal or maximal element
6598 * in the first basic map. This results in a partial solution "res"
6599 * and a subset "todo" of dom that still need to be handled.
6600 * We then consider each of the remaining maps in "map" and successively
6601 * update both "res" and "todo".
6602 * If "empty" is NULL, then the todo sets are not needed and therefore
6603 * also not computed.
6605 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6606 __isl_take isl_map *map, __isl_take isl_set *dom,
6607 __isl_give isl_set **empty, unsigned flags)
6609 int i;
6610 int full;
6611 isl_pw_multi_aff *res;
6612 isl_set *todo;
6614 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6615 if (!map || (!full && !dom))
6616 goto error;
6618 if (isl_map_plain_is_empty(map)) {
6619 if (empty)
6620 *empty = dom;
6621 else
6622 isl_set_free(dom);
6623 return isl_pw_multi_aff_from_map(map);
6626 res = basic_map_partial_lexopt_pw_multi_aff(
6627 isl_basic_map_copy(map->p[0]),
6628 isl_set_copy(dom), empty, flags);
6630 if (empty)
6631 todo = *empty;
6632 for (i = 1; i < map->n; ++i) {
6633 isl_pw_multi_aff *res_i;
6635 res_i = basic_map_partial_lexopt_pw_multi_aff(
6636 isl_basic_map_copy(map->p[i]),
6637 isl_set_copy(dom), empty, flags);
6639 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6640 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6641 else
6642 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6644 if (empty)
6645 todo = isl_set_intersect(todo, *empty);
6648 isl_set_free(dom);
6649 isl_map_free(map);
6651 if (empty)
6652 *empty = todo;
6654 return res;
6655 error:
6656 if (empty)
6657 *empty = NULL;
6658 isl_set_free(dom);
6659 isl_map_free(map);
6660 return NULL;
6663 #undef TYPE
6664 #define TYPE isl_map
6665 #undef SUFFIX
6666 #define SUFFIX
6667 #undef EMPTY
6668 #define EMPTY isl_map_empty
6669 #undef ADD
6670 #define ADD isl_map_union_disjoint
6671 #include "isl_map_lexopt_templ.c"
6673 /* Given a map "map", compute the lexicographically minimal
6674 * (or maximal) image element for each domain element in "dom",
6675 * in the form of an isl_map.
6676 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6677 * do not have an image element.
6678 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6679 * should be computed over the domain of "map". "empty" is also NULL
6680 * in this case.
6682 * If the input consists of more than one disjunct, then first
6683 * compute the desired result in the form of an isl_pw_multi_aff and
6684 * then convert that into an isl_map.
6686 * This function used to have an explicit implementation in terms
6687 * of isl_maps, but it would continually intersect the domains of
6688 * partial results with the complement of the domain of the next
6689 * partial solution, potentially leading to an explosion in the number
6690 * of disjuncts if there are several disjuncts in the input.
6691 * An even earlier implementation of this function would look for
6692 * better results in the domain of the partial result and for extra
6693 * results in the complement of this domain, which would lead to
6694 * even more splintering.
6696 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6697 __isl_take isl_map *map, __isl_take isl_set *dom,
6698 __isl_give isl_set **empty, unsigned flags)
6700 int full;
6701 struct isl_map *res;
6702 isl_pw_multi_aff *pma;
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 map;
6716 if (map->n == 1) {
6717 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6718 dom, empty, flags);
6719 isl_map_free(map);
6720 return res;
6723 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6724 flags);
6725 return isl_map_from_pw_multi_aff(pma);
6726 error:
6727 if (empty)
6728 *empty = NULL;
6729 isl_set_free(dom);
6730 isl_map_free(map);
6731 return NULL;
6734 __isl_give isl_map *isl_map_partial_lexmax(
6735 __isl_take isl_map *map, __isl_take isl_set *dom,
6736 __isl_give isl_set **empty)
6738 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6741 __isl_give isl_map *isl_map_partial_lexmin(
6742 __isl_take isl_map *map, __isl_take isl_set *dom,
6743 __isl_give isl_set **empty)
6745 return isl_map_partial_lexopt(map, dom, empty, 0);
6748 __isl_give isl_set *isl_set_partial_lexmin(
6749 __isl_take isl_set *set, __isl_take isl_set *dom,
6750 __isl_give isl_set **empty)
6752 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6753 dom, empty));
6756 __isl_give isl_set *isl_set_partial_lexmax(
6757 __isl_take isl_set *set, __isl_take isl_set *dom,
6758 __isl_give isl_set **empty)
6760 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6761 dom, empty));
6764 /* Compute the lexicographic minimum (or maximum if "flags" includes
6765 * ISL_OPT_MAX) of "bset" over its parametric domain.
6767 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6768 unsigned flags)
6770 return isl_basic_map_lexopt(bset, flags);
6773 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6775 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6778 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6780 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6783 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6785 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6788 /* Compute the lexicographic minimum of "bset" over its parametric domain
6789 * for the purpose of quantifier elimination.
6790 * That is, find an explicit representation for all the existentially
6791 * quantified variables in "bset" by computing their lexicographic
6792 * minimum.
6794 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6795 __isl_take isl_basic_set *bset)
6797 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6800 /* Extract the first and only affine expression from list
6801 * and then add it to *pwaff with the given dom.
6802 * This domain is known to be disjoint from other domains
6803 * because of the way isl_basic_map_foreach_lexmax works.
6805 static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
6806 __isl_take isl_aff_list *list, void *user)
6808 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6809 isl_aff *aff;
6810 isl_pw_aff **pwaff = user;
6811 isl_pw_aff *pwaff_i;
6813 if (!list)
6814 goto error;
6815 if (isl_aff_list_n_aff(list) != 1)
6816 isl_die(ctx, isl_error_internal,
6817 "expecting single element list", goto error);
6819 aff = isl_aff_list_get_aff(list, 0);
6820 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6822 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6824 isl_aff_list_free(list);
6826 return isl_stat_ok;
6827 error:
6828 isl_basic_set_free(dom);
6829 isl_aff_list_free(list);
6830 return isl_stat_error;
6833 /* Given a basic map with one output dimension, compute the minimum or
6834 * maximum of that dimension as an isl_pw_aff.
6836 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6837 * call update_dim_opt on each leaf of the result.
6839 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6840 int max)
6842 isl_space *dim = isl_basic_map_get_space(bmap);
6843 isl_pw_aff *pwaff;
6844 isl_stat r;
6846 dim = isl_space_from_domain(isl_space_domain(dim));
6847 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6848 pwaff = isl_pw_aff_empty(dim);
6850 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6851 if (r < 0)
6852 return isl_pw_aff_free(pwaff);
6854 return pwaff;
6857 /* Compute the minimum or maximum of the given output dimension
6858 * as a function of the parameters and the input dimensions,
6859 * but independently of the other output dimensions.
6861 * We first project out the other output dimension and then compute
6862 * the "lexicographic" maximum in each basic map, combining the results
6863 * using isl_pw_aff_union_max.
6865 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6866 int max)
6868 int i;
6869 isl_pw_aff *pwaff;
6870 unsigned n_out;
6872 n_out = isl_map_dim(map, isl_dim_out);
6873 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6874 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6875 if (!map)
6876 return NULL;
6878 if (map->n == 0) {
6879 isl_space *dim = isl_map_get_space(map);
6880 isl_map_free(map);
6881 return isl_pw_aff_empty(dim);
6884 pwaff = basic_map_dim_opt(map->p[0], max);
6885 for (i = 1; i < map->n; ++i) {
6886 isl_pw_aff *pwaff_i;
6888 pwaff_i = basic_map_dim_opt(map->p[i], max);
6889 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6892 isl_map_free(map);
6894 return pwaff;
6897 /* Compute the minimum of the given output dimension as a function of the
6898 * parameters and input dimensions, but independently of
6899 * the other output dimensions.
6901 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6903 return map_dim_opt(map, pos, 0);
6906 /* Compute the maximum of the given output dimension as a function of the
6907 * parameters and input dimensions, but independently of
6908 * the other output dimensions.
6910 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6912 return map_dim_opt(map, pos, 1);
6915 /* Compute the minimum or maximum of the given set dimension
6916 * as a function of the parameters,
6917 * but independently of the other set dimensions.
6919 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6920 int max)
6922 return map_dim_opt(set, pos, max);
6925 /* Compute the maximum of the given set dimension as a function of the
6926 * parameters, but independently of the other set dimensions.
6928 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6930 return set_dim_opt(set, pos, 1);
6933 /* Compute the minimum of the given set dimension as a function of the
6934 * parameters, but independently of the other set dimensions.
6936 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6938 return set_dim_opt(set, pos, 0);
6941 /* Apply a preimage specified by "mat" on the parameters of "bset".
6942 * bset is assumed to have only parameters and divs.
6944 static __isl_give isl_basic_set *basic_set_parameter_preimage(
6945 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
6947 unsigned nparam;
6949 if (!bset || !mat)
6950 goto error;
6952 bset->dim = isl_space_cow(bset->dim);
6953 if (!bset->dim)
6954 goto error;
6956 nparam = isl_basic_set_dim(bset, isl_dim_param);
6958 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6960 bset->dim->nparam = 0;
6961 bset->dim->n_out = nparam;
6962 bset = isl_basic_set_preimage(bset, mat);
6963 if (bset) {
6964 bset->dim->nparam = bset->dim->n_out;
6965 bset->dim->n_out = 0;
6967 return bset;
6968 error:
6969 isl_mat_free(mat);
6970 isl_basic_set_free(bset);
6971 return NULL;
6974 /* Apply a preimage specified by "mat" on the parameters of "set".
6975 * set is assumed to have only parameters and divs.
6977 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6978 __isl_take isl_mat *mat)
6980 isl_space *space;
6981 unsigned nparam;
6983 if (!set || !mat)
6984 goto error;
6986 nparam = isl_set_dim(set, isl_dim_param);
6988 if (mat->n_row != 1 + nparam)
6989 isl_die(isl_set_get_ctx(set), isl_error_internal,
6990 "unexpected number of rows", goto error);
6992 space = isl_set_get_space(set);
6993 space = isl_space_move_dims(space, isl_dim_set, 0,
6994 isl_dim_param, 0, nparam);
6995 set = isl_set_reset_space(set, space);
6996 set = isl_set_preimage(set, mat);
6997 nparam = isl_set_dim(set, isl_dim_out);
6998 space = isl_set_get_space(set);
6999 space = isl_space_move_dims(space, isl_dim_param, 0,
7000 isl_dim_out, 0, nparam);
7001 set = isl_set_reset_space(set, space);
7002 return set;
7003 error:
7004 isl_mat_free(mat);
7005 isl_set_free(set);
7006 return NULL;
7009 /* Intersect the basic set "bset" with the affine space specified by the
7010 * equalities in "eq".
7012 static __isl_give isl_basic_set *basic_set_append_equalities(
7013 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7015 int i, k;
7016 unsigned len;
7018 if (!bset || !eq)
7019 goto error;
7021 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7022 eq->n_row, 0);
7023 if (!bset)
7024 goto error;
7026 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7027 for (i = 0; i < eq->n_row; ++i) {
7028 k = isl_basic_set_alloc_equality(bset);
7029 if (k < 0)
7030 goto error;
7031 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7032 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7034 isl_mat_free(eq);
7036 bset = isl_basic_set_gauss(bset, NULL);
7037 bset = isl_basic_set_finalize(bset);
7039 return bset;
7040 error:
7041 isl_mat_free(eq);
7042 isl_basic_set_free(bset);
7043 return NULL;
7046 /* Intersect the set "set" with the affine space specified by the
7047 * equalities in "eq".
7049 static struct isl_set *set_append_equalities(struct isl_set *set,
7050 struct isl_mat *eq)
7052 int i;
7054 if (!set || !eq)
7055 goto error;
7057 for (i = 0; i < set->n; ++i) {
7058 set->p[i] = basic_set_append_equalities(set->p[i],
7059 isl_mat_copy(eq));
7060 if (!set->p[i])
7061 goto error;
7063 isl_mat_free(eq);
7064 return set;
7065 error:
7066 isl_mat_free(eq);
7067 isl_set_free(set);
7068 return NULL;
7071 /* Given a basic set "bset" that only involves parameters and existentially
7072 * quantified variables, return the index of the first equality
7073 * that only involves parameters. If there is no such equality then
7074 * return bset->n_eq.
7076 * This function assumes that isl_basic_set_gauss has been called on "bset".
7078 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7080 int i, j;
7081 unsigned nparam, n_div;
7083 if (!bset)
7084 return -1;
7086 nparam = isl_basic_set_dim(bset, isl_dim_param);
7087 n_div = isl_basic_set_dim(bset, isl_dim_div);
7089 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7090 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7091 ++i;
7094 return i;
7097 /* Compute an explicit representation for the existentially quantified
7098 * variables in "bset" by computing the "minimal value" of the set
7099 * variables. Since there are no set variables, the computation of
7100 * the minimal value essentially computes an explicit representation
7101 * of the non-empty part(s) of "bset".
7103 * The input only involves parameters and existentially quantified variables.
7104 * All equalities among parameters have been removed.
7106 * Since the existentially quantified variables in the result are in general
7107 * going to be different from those in the input, we first replace
7108 * them by the minimal number of variables based on their equalities.
7109 * This should simplify the parametric integer programming.
7111 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7113 isl_morph *morph1, *morph2;
7114 isl_set *set;
7115 unsigned n;
7117 if (!bset)
7118 return NULL;
7119 if (bset->n_eq == 0)
7120 return isl_basic_set_lexmin_compute_divs(bset);
7122 morph1 = isl_basic_set_parameter_compression(bset);
7123 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7124 bset = isl_basic_set_lift(bset);
7125 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7126 bset = isl_morph_basic_set(morph2, bset);
7127 n = isl_basic_set_dim(bset, isl_dim_set);
7128 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7130 set = isl_basic_set_lexmin_compute_divs(bset);
7132 set = isl_morph_set(isl_morph_inverse(morph1), set);
7134 return set;
7137 /* Project the given basic set onto its parameter domain, possibly introducing
7138 * new, explicit, existential variables in the constraints.
7139 * The input has parameters and (possibly implicit) existential variables.
7140 * The output has the same parameters, but only
7141 * explicit existentially quantified variables.
7143 * The actual projection is performed by pip, but pip doesn't seem
7144 * to like equalities very much, so we first remove the equalities
7145 * among the parameters by performing a variable compression on
7146 * the parameters. Afterward, an inverse transformation is performed
7147 * and the equalities among the parameters are inserted back in.
7149 * The variable compression on the parameters may uncover additional
7150 * equalities that were only implicit before. We therefore check
7151 * if there are any new parameter equalities in the result and
7152 * if so recurse. The removal of parameter equalities is required
7153 * for the parameter compression performed by base_compute_divs.
7155 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7157 int i;
7158 struct isl_mat *eq;
7159 struct isl_mat *T, *T2;
7160 struct isl_set *set;
7161 unsigned nparam;
7163 bset = isl_basic_set_cow(bset);
7164 if (!bset)
7165 return NULL;
7167 if (bset->n_eq == 0)
7168 return base_compute_divs(bset);
7170 bset = isl_basic_set_gauss(bset, NULL);
7171 if (!bset)
7172 return NULL;
7173 if (isl_basic_set_plain_is_empty(bset))
7174 return isl_set_from_basic_set(bset);
7176 i = first_parameter_equality(bset);
7177 if (i == bset->n_eq)
7178 return base_compute_divs(bset);
7180 nparam = isl_basic_set_dim(bset, isl_dim_param);
7181 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7182 0, 1 + nparam);
7183 eq = isl_mat_cow(eq);
7184 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7185 if (T && T->n_col == 0) {
7186 isl_mat_free(T);
7187 isl_mat_free(T2);
7188 isl_mat_free(eq);
7189 bset = isl_basic_set_set_to_empty(bset);
7190 return isl_set_from_basic_set(bset);
7192 bset = basic_set_parameter_preimage(bset, T);
7194 i = first_parameter_equality(bset);
7195 if (!bset)
7196 set = NULL;
7197 else if (i == bset->n_eq)
7198 set = base_compute_divs(bset);
7199 else
7200 set = parameter_compute_divs(bset);
7201 set = set_parameter_preimage(set, T2);
7202 set = set_append_equalities(set, eq);
7203 return set;
7206 /* Insert the divs from "ls" before those of "bmap".
7208 * The number of columns is not changed, which means that the last
7209 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7210 * The caller is responsible for removing the same number of dimensions
7211 * from the space of "bmap".
7213 static __isl_give isl_basic_map *insert_divs_from_local_space(
7214 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7216 int i;
7217 int n_div;
7218 int old_n_div;
7220 n_div = isl_local_space_dim(ls, isl_dim_div);
7221 if (n_div == 0)
7222 return bmap;
7224 old_n_div = bmap->n_div;
7225 bmap = insert_div_rows(bmap, n_div);
7226 if (!bmap)
7227 return NULL;
7229 for (i = 0; i < n_div; ++i) {
7230 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7231 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7234 return bmap;
7237 /* Replace the space of "bmap" by the space and divs of "ls".
7239 * If "ls" has any divs, then we simplify the result since we may
7240 * have discovered some additional equalities that could simplify
7241 * the div expressions.
7243 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7244 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7246 int n_div;
7248 bmap = isl_basic_map_cow(bmap);
7249 if (!bmap || !ls)
7250 goto error;
7252 n_div = isl_local_space_dim(ls, isl_dim_div);
7253 bmap = insert_divs_from_local_space(bmap, ls);
7254 if (!bmap)
7255 goto error;
7257 isl_space_free(bmap->dim);
7258 bmap->dim = isl_local_space_get_space(ls);
7259 if (!bmap->dim)
7260 goto error;
7262 isl_local_space_free(ls);
7263 if (n_div > 0)
7264 bmap = isl_basic_map_simplify(bmap);
7265 bmap = isl_basic_map_finalize(bmap);
7266 return bmap;
7267 error:
7268 isl_basic_map_free(bmap);
7269 isl_local_space_free(ls);
7270 return NULL;
7273 /* Replace the space of "map" by the space and divs of "ls".
7275 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7276 __isl_take isl_local_space *ls)
7278 int i;
7280 map = isl_map_cow(map);
7281 if (!map || !ls)
7282 goto error;
7284 for (i = 0; i < map->n; ++i) {
7285 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7286 isl_local_space_copy(ls));
7287 if (!map->p[i])
7288 goto error;
7290 isl_space_free(map->dim);
7291 map->dim = isl_local_space_get_space(ls);
7292 if (!map->dim)
7293 goto error;
7295 isl_local_space_free(ls);
7296 return map;
7297 error:
7298 isl_local_space_free(ls);
7299 isl_map_free(map);
7300 return NULL;
7303 /* Compute an explicit representation for the existentially
7304 * quantified variables for which do not know any explicit representation yet.
7306 * We first sort the existentially quantified variables so that the
7307 * existentially quantified variables for which we already have an explicit
7308 * representation are placed before those for which we do not.
7309 * The input dimensions, the output dimensions and the existentially
7310 * quantified variables for which we already have an explicit
7311 * representation are then turned into parameters.
7312 * compute_divs returns a map with the same parameters and
7313 * no input or output dimensions and the dimension specification
7314 * is reset to that of the input, including the existentially quantified
7315 * variables for which we already had an explicit representation.
7317 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7319 struct isl_basic_set *bset;
7320 struct isl_set *set;
7321 struct isl_map *map;
7322 isl_space *dim;
7323 isl_local_space *ls;
7324 unsigned nparam;
7325 unsigned n_in;
7326 unsigned n_out;
7327 int n_known;
7328 int i;
7330 bmap = isl_basic_map_sort_divs(bmap);
7331 bmap = isl_basic_map_cow(bmap);
7332 if (!bmap)
7333 return NULL;
7335 n_known = isl_basic_map_first_unknown_div(bmap);
7336 if (n_known < 0)
7337 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7339 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7340 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7341 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7342 dim = isl_space_set_alloc(bmap->ctx,
7343 nparam + n_in + n_out + n_known, 0);
7344 if (!dim)
7345 goto error;
7347 ls = isl_basic_map_get_local_space(bmap);
7348 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7349 n_known, bmap->n_div - n_known);
7350 if (n_known > 0) {
7351 for (i = n_known; i < bmap->n_div; ++i)
7352 swap_div(bmap, i - n_known, i);
7353 bmap->n_div -= n_known;
7354 bmap->extra -= n_known;
7356 bmap = isl_basic_map_reset_space(bmap, dim);
7357 bset = bset_from_bmap(bmap);
7359 set = parameter_compute_divs(bset);
7360 map = set_to_map(set);
7361 map = replace_space_by_local_space(map, ls);
7363 return map;
7364 error:
7365 isl_basic_map_free(bmap);
7366 return NULL;
7369 /* Remove the explicit representation of local variable "div",
7370 * if there is any.
7372 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7373 __isl_take isl_basic_map *bmap, int div)
7375 isl_bool unknown;
7377 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7378 if (unknown < 0)
7379 return isl_basic_map_free(bmap);
7380 if (unknown)
7381 return bmap;
7383 bmap = isl_basic_map_cow(bmap);
7384 if (!bmap)
7385 return NULL;
7386 isl_int_set_si(bmap->div[div][0], 0);
7387 return bmap;
7390 /* Is local variable "div" of "bmap" marked as not having an explicit
7391 * representation?
7392 * Note that even if "div" is not marked in this way and therefore
7393 * has an explicit representation, this representation may still
7394 * depend (indirectly) on other local variables that do not
7395 * have an explicit representation.
7397 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7398 int div)
7400 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7401 return isl_bool_error;
7402 return isl_int_is_zero(bmap->div[div][0]);
7405 /* Return the position of the first local variable that does not
7406 * have an explicit representation.
7407 * Return the total number of local variables if they all have
7408 * an explicit representation.
7409 * Return -1 on error.
7411 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7413 int i;
7415 if (!bmap)
7416 return -1;
7418 for (i = 0; i < bmap->n_div; ++i) {
7419 if (!isl_basic_map_div_is_known(bmap, i))
7420 return i;
7422 return bmap->n_div;
7425 /* Return the position of the first local variable that does not
7426 * have an explicit representation.
7427 * Return the total number of local variables if they all have
7428 * an explicit representation.
7429 * Return -1 on error.
7431 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7433 return isl_basic_map_first_unknown_div(bset);
7436 /* Does "bmap" have an explicit representation for all local variables?
7438 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7440 int first, n;
7442 n = isl_basic_map_dim(bmap, isl_dim_div);
7443 first = isl_basic_map_first_unknown_div(bmap);
7444 if (first < 0)
7445 return isl_bool_error;
7446 return first == n;
7449 /* Do all basic maps in "map" have an explicit representation
7450 * for all local variables?
7452 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7454 int i;
7456 if (!map)
7457 return isl_bool_error;
7459 for (i = 0; i < map->n; ++i) {
7460 int known = isl_basic_map_divs_known(map->p[i]);
7461 if (known <= 0)
7462 return known;
7465 return isl_bool_true;
7468 /* If bmap contains any unknown divs, then compute explicit
7469 * expressions for them. However, this computation may be
7470 * quite expensive, so first try to remove divs that aren't
7471 * strictly needed.
7473 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7475 int known;
7476 struct isl_map *map;
7478 known = isl_basic_map_divs_known(bmap);
7479 if (known < 0)
7480 goto error;
7481 if (known)
7482 return isl_map_from_basic_map(bmap);
7484 bmap = isl_basic_map_drop_redundant_divs(bmap);
7486 known = isl_basic_map_divs_known(bmap);
7487 if (known < 0)
7488 goto error;
7489 if (known)
7490 return isl_map_from_basic_map(bmap);
7492 map = compute_divs(bmap);
7493 return map;
7494 error:
7495 isl_basic_map_free(bmap);
7496 return NULL;
7499 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7501 int i;
7502 int known;
7503 struct isl_map *res;
7505 if (!map)
7506 return NULL;
7507 if (map->n == 0)
7508 return map;
7510 known = isl_map_divs_known(map);
7511 if (known < 0) {
7512 isl_map_free(map);
7513 return NULL;
7515 if (known)
7516 return map;
7518 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7519 for (i = 1 ; i < map->n; ++i) {
7520 struct isl_map *r2;
7521 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7522 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7523 res = isl_map_union_disjoint(res, r2);
7524 else
7525 res = isl_map_union(res, r2);
7527 isl_map_free(map);
7529 return res;
7532 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7534 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7537 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7539 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7542 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7544 int i;
7545 struct isl_set *set;
7547 if (!map)
7548 goto error;
7550 map = isl_map_cow(map);
7551 if (!map)
7552 return NULL;
7554 set = set_from_map(map);
7555 set->dim = isl_space_domain(set->dim);
7556 if (!set->dim)
7557 goto error;
7558 for (i = 0; i < map->n; ++i) {
7559 set->p[i] = isl_basic_map_domain(map->p[i]);
7560 if (!set->p[i])
7561 goto error;
7563 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7564 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7565 return set;
7566 error:
7567 isl_map_free(map);
7568 return NULL;
7571 /* Return the union of "map1" and "map2", where we assume for now that
7572 * "map1" and "map2" are disjoint. Note that the basic maps inside
7573 * "map1" or "map2" may not be disjoint from each other.
7574 * Also note that this function is also called from isl_map_union,
7575 * which takes care of handling the situation where "map1" and "map2"
7576 * may not be disjoint.
7578 * If one of the inputs is empty, we can simply return the other input.
7579 * Similarly, if one of the inputs is universal, then it is equal to the union.
7581 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7582 __isl_take isl_map *map2)
7584 int i;
7585 unsigned flags = 0;
7586 struct isl_map *map = NULL;
7587 int is_universe;
7589 if (!map1 || !map2)
7590 goto error;
7592 if (!isl_space_is_equal(map1->dim, map2->dim))
7593 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7594 "spaces don't match", goto error);
7596 if (map1->n == 0) {
7597 isl_map_free(map1);
7598 return map2;
7600 if (map2->n == 0) {
7601 isl_map_free(map2);
7602 return map1;
7605 is_universe = isl_map_plain_is_universe(map1);
7606 if (is_universe < 0)
7607 goto error;
7608 if (is_universe) {
7609 isl_map_free(map2);
7610 return map1;
7613 is_universe = isl_map_plain_is_universe(map2);
7614 if (is_universe < 0)
7615 goto error;
7616 if (is_universe) {
7617 isl_map_free(map1);
7618 return map2;
7621 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7622 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7623 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7625 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7626 map1->n + map2->n, flags);
7627 if (!map)
7628 goto error;
7629 for (i = 0; i < map1->n; ++i) {
7630 map = isl_map_add_basic_map(map,
7631 isl_basic_map_copy(map1->p[i]));
7632 if (!map)
7633 goto error;
7635 for (i = 0; i < map2->n; ++i) {
7636 map = isl_map_add_basic_map(map,
7637 isl_basic_map_copy(map2->p[i]));
7638 if (!map)
7639 goto error;
7641 isl_map_free(map1);
7642 isl_map_free(map2);
7643 return map;
7644 error:
7645 isl_map_free(map);
7646 isl_map_free(map1);
7647 isl_map_free(map2);
7648 return NULL;
7651 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7652 * guaranteed to be disjoint by the caller.
7654 * Note that this functions is called from within isl_map_make_disjoint,
7655 * so we have to be careful not to touch the constraints of the inputs
7656 * in any way.
7658 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7659 __isl_take isl_map *map2)
7661 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7664 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7665 * not be disjoint. The parameters are assumed to have been aligned.
7667 * We currently simply call map_union_disjoint, the internal operation
7668 * of which does not really depend on the inputs being disjoint.
7669 * If the result contains more than one basic map, then we clear
7670 * the disjoint flag since the result may contain basic maps from
7671 * both inputs and these are not guaranteed to be disjoint.
7673 * As a special case, if "map1" and "map2" are obviously equal,
7674 * then we simply return "map1".
7676 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7677 __isl_take isl_map *map2)
7679 int equal;
7681 if (!map1 || !map2)
7682 goto error;
7684 equal = isl_map_plain_is_equal(map1, map2);
7685 if (equal < 0)
7686 goto error;
7687 if (equal) {
7688 isl_map_free(map2);
7689 return map1;
7692 map1 = map_union_disjoint(map1, map2);
7693 if (!map1)
7694 return NULL;
7695 if (map1->n > 1)
7696 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7697 return map1;
7698 error:
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" may
7705 * not be disjoint.
7707 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7708 __isl_take isl_map *map2)
7710 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7713 struct isl_set *isl_set_union_disjoint(
7714 struct isl_set *set1, struct isl_set *set2)
7716 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7717 set_to_map(set2)));
7720 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7722 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7725 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7726 * the results.
7728 * "map" and "set" are assumed to be compatible and non-NULL.
7730 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7731 __isl_take isl_set *set,
7732 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7733 __isl_take isl_basic_set *bset))
7735 unsigned flags = 0;
7736 struct isl_map *result;
7737 int i, j;
7739 if (isl_set_plain_is_universe(set)) {
7740 isl_set_free(set);
7741 return map;
7744 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7745 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7746 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7748 result = isl_map_alloc_space(isl_space_copy(map->dim),
7749 map->n * set->n, flags);
7750 for (i = 0; result && i < map->n; ++i)
7751 for (j = 0; j < set->n; ++j) {
7752 result = isl_map_add_basic_map(result,
7753 fn(isl_basic_map_copy(map->p[i]),
7754 isl_basic_set_copy(set->p[j])));
7755 if (!result)
7756 break;
7759 isl_map_free(map);
7760 isl_set_free(set);
7761 return result;
7764 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7765 __isl_take isl_set *set)
7767 isl_bool ok;
7769 ok = isl_map_compatible_range(map, set);
7770 if (ok < 0)
7771 goto error;
7772 if (!ok)
7773 isl_die(set->ctx, isl_error_invalid,
7774 "incompatible spaces", goto error);
7776 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7777 error:
7778 isl_map_free(map);
7779 isl_set_free(set);
7780 return NULL;
7783 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7784 __isl_take isl_set *set)
7786 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7789 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7790 __isl_take isl_set *set)
7792 isl_bool ok;
7794 ok = isl_map_compatible_domain(map, set);
7795 if (ok < 0)
7796 goto error;
7797 if (!ok)
7798 isl_die(set->ctx, isl_error_invalid,
7799 "incompatible spaces", goto error);
7801 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7802 error:
7803 isl_map_free(map);
7804 isl_set_free(set);
7805 return NULL;
7808 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7809 __isl_take isl_set *set)
7811 return isl_map_align_params_map_map_and(map, set,
7812 &map_intersect_domain);
7815 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7816 __isl_take isl_map *map2)
7818 if (!map1 || !map2)
7819 goto error;
7820 map1 = isl_map_reverse(map1);
7821 map1 = isl_map_apply_range(map1, map2);
7822 return isl_map_reverse(map1);
7823 error:
7824 isl_map_free(map1);
7825 isl_map_free(map2);
7826 return NULL;
7829 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7830 __isl_take isl_map *map2)
7832 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7835 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7836 __isl_take isl_map *map2)
7838 isl_space *dim_result;
7839 struct isl_map *result;
7840 int i, j;
7842 if (!map1 || !map2)
7843 goto error;
7845 dim_result = isl_space_join(isl_space_copy(map1->dim),
7846 isl_space_copy(map2->dim));
7848 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7849 if (!result)
7850 goto error;
7851 for (i = 0; i < map1->n; ++i)
7852 for (j = 0; j < map2->n; ++j) {
7853 result = isl_map_add_basic_map(result,
7854 isl_basic_map_apply_range(
7855 isl_basic_map_copy(map1->p[i]),
7856 isl_basic_map_copy(map2->p[j])));
7857 if (!result)
7858 goto error;
7860 isl_map_free(map1);
7861 isl_map_free(map2);
7862 if (result && result->n <= 1)
7863 ISL_F_SET(result, ISL_MAP_DISJOINT);
7864 return result;
7865 error:
7866 isl_map_free(map1);
7867 isl_map_free(map2);
7868 return NULL;
7871 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7872 __isl_take isl_map *map2)
7874 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7878 * returns range - domain
7880 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
7882 isl_space *target_space;
7883 struct isl_basic_set *bset;
7884 unsigned dim;
7885 unsigned nparam;
7886 int i;
7888 if (!bmap)
7889 goto error;
7890 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7891 bmap->dim, isl_dim_out),
7892 goto error);
7893 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7894 dim = isl_basic_map_dim(bmap, isl_dim_in);
7895 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7896 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7897 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7898 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7899 for (i = 0; i < dim; ++i) {
7900 int j = isl_basic_map_alloc_equality(bmap);
7901 if (j < 0) {
7902 bmap = isl_basic_map_free(bmap);
7903 break;
7905 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7906 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7907 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7908 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7910 bset = isl_basic_map_domain(bmap);
7911 bset = isl_basic_set_reset_space(bset, target_space);
7912 return bset;
7913 error:
7914 isl_basic_map_free(bmap);
7915 return NULL;
7919 * returns range - domain
7921 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7923 int i;
7924 isl_space *dim;
7925 struct isl_set *result;
7927 if (!map)
7928 return NULL;
7930 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7931 map->dim, isl_dim_out),
7932 goto error);
7933 dim = isl_map_get_space(map);
7934 dim = isl_space_domain(dim);
7935 result = isl_set_alloc_space(dim, map->n, 0);
7936 if (!result)
7937 goto error;
7938 for (i = 0; i < map->n; ++i)
7939 result = isl_set_add_basic_set(result,
7940 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7941 isl_map_free(map);
7942 return result;
7943 error:
7944 isl_map_free(map);
7945 return NULL;
7949 * returns [domain -> range] -> range - domain
7951 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7952 __isl_take isl_basic_map *bmap)
7954 int i, k;
7955 isl_space *dim;
7956 isl_basic_map *domain;
7957 int nparam, n;
7958 unsigned total;
7960 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7961 bmap->dim, isl_dim_out))
7962 isl_die(bmap->ctx, isl_error_invalid,
7963 "domain and range don't match", goto error);
7965 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7966 n = isl_basic_map_dim(bmap, isl_dim_in);
7968 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7969 domain = isl_basic_map_universe(dim);
7971 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7972 bmap = isl_basic_map_apply_range(bmap, domain);
7973 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7975 total = isl_basic_map_total_dim(bmap);
7977 for (i = 0; i < n; ++i) {
7978 k = isl_basic_map_alloc_equality(bmap);
7979 if (k < 0)
7980 goto error;
7981 isl_seq_clr(bmap->eq[k], 1 + total);
7982 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7983 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7984 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7987 bmap = isl_basic_map_gauss(bmap, NULL);
7988 return isl_basic_map_finalize(bmap);
7989 error:
7990 isl_basic_map_free(bmap);
7991 return NULL;
7995 * returns [domain -> range] -> range - domain
7997 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7999 int i;
8000 isl_space *domain_dim;
8002 if (!map)
8003 return NULL;
8005 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8006 map->dim, isl_dim_out))
8007 isl_die(map->ctx, isl_error_invalid,
8008 "domain and range don't match", goto error);
8010 map = isl_map_cow(map);
8011 if (!map)
8012 return NULL;
8014 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
8015 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8016 map->dim = isl_space_join(map->dim, domain_dim);
8017 if (!map->dim)
8018 goto error;
8019 for (i = 0; i < map->n; ++i) {
8020 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8021 if (!map->p[i])
8022 goto error;
8024 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8025 return map;
8026 error:
8027 isl_map_free(map);
8028 return NULL;
8031 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
8033 struct isl_basic_map *bmap;
8034 unsigned nparam;
8035 unsigned dim;
8036 int i;
8038 if (!dims)
8039 return NULL;
8041 nparam = dims->nparam;
8042 dim = dims->n_out;
8043 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
8044 if (!bmap)
8045 goto error;
8047 for (i = 0; i < dim; ++i) {
8048 int j = isl_basic_map_alloc_equality(bmap);
8049 if (j < 0)
8050 goto error;
8051 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8052 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8053 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
8055 return isl_basic_map_finalize(bmap);
8056 error:
8057 isl_basic_map_free(bmap);
8058 return NULL;
8061 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
8063 if (!dim)
8064 return NULL;
8065 if (dim->n_in != dim->n_out)
8066 isl_die(dim->ctx, isl_error_invalid,
8067 "number of input and output dimensions needs to be "
8068 "the same", goto error);
8069 return basic_map_identity(dim);
8070 error:
8071 isl_space_free(dim);
8072 return NULL;
8075 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8077 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8080 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8082 isl_space *dim = isl_set_get_space(set);
8083 isl_map *id;
8084 id = isl_map_identity(isl_space_map_from_set(dim));
8085 return isl_map_intersect_range(id, set);
8088 /* Construct a basic set with all set dimensions having only non-negative
8089 * values.
8091 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8092 __isl_take isl_space *space)
8094 int i;
8095 unsigned nparam;
8096 unsigned dim;
8097 struct isl_basic_set *bset;
8099 if (!space)
8100 return NULL;
8101 nparam = space->nparam;
8102 dim = space->n_out;
8103 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8104 if (!bset)
8105 return NULL;
8106 for (i = 0; i < dim; ++i) {
8107 int k = isl_basic_set_alloc_inequality(bset);
8108 if (k < 0)
8109 goto error;
8110 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8111 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8113 return bset;
8114 error:
8115 isl_basic_set_free(bset);
8116 return NULL;
8119 /* Construct the half-space x_pos >= 0.
8121 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
8122 int pos)
8124 int k;
8125 isl_basic_set *nonneg;
8127 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8128 k = isl_basic_set_alloc_inequality(nonneg);
8129 if (k < 0)
8130 goto error;
8131 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8132 isl_int_set_si(nonneg->ineq[k][pos], 1);
8134 return isl_basic_set_finalize(nonneg);
8135 error:
8136 isl_basic_set_free(nonneg);
8137 return NULL;
8140 /* Construct the half-space x_pos <= -1.
8142 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
8144 int k;
8145 isl_basic_set *neg;
8147 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8148 k = isl_basic_set_alloc_inequality(neg);
8149 if (k < 0)
8150 goto error;
8151 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8152 isl_int_set_si(neg->ineq[k][0], -1);
8153 isl_int_set_si(neg->ineq[k][pos], -1);
8155 return isl_basic_set_finalize(neg);
8156 error:
8157 isl_basic_set_free(neg);
8158 return NULL;
8161 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8162 enum isl_dim_type type, unsigned first, unsigned n)
8164 int i;
8165 unsigned offset;
8166 isl_basic_set *nonneg;
8167 isl_basic_set *neg;
8169 if (!set)
8170 return NULL;
8171 if (n == 0)
8172 return set;
8174 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
8176 offset = pos(set->dim, type);
8177 for (i = 0; i < n; ++i) {
8178 nonneg = nonneg_halfspace(isl_set_get_space(set),
8179 offset + first + i);
8180 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8182 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8185 return set;
8186 error:
8187 isl_set_free(set);
8188 return NULL;
8191 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8192 int len,
8193 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8194 void *user)
8196 isl_set *half;
8198 if (!set)
8199 return isl_stat_error;
8200 if (isl_set_plain_is_empty(set)) {
8201 isl_set_free(set);
8202 return isl_stat_ok;
8204 if (first == len)
8205 return fn(set, signs, user);
8207 signs[first] = 1;
8208 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8209 1 + first));
8210 half = isl_set_intersect(half, isl_set_copy(set));
8211 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8212 goto error;
8214 signs[first] = -1;
8215 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8216 1 + first));
8217 half = isl_set_intersect(half, set);
8218 return foreach_orthant(half, signs, first + 1, len, fn, user);
8219 error:
8220 isl_set_free(set);
8221 return isl_stat_error;
8224 /* Call "fn" on the intersections of "set" with each of the orthants
8225 * (except for obviously empty intersections). The orthant is identified
8226 * by the signs array, with each entry having value 1 or -1 according
8227 * to the sign of the corresponding variable.
8229 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8230 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8231 void *user)
8233 unsigned nparam;
8234 unsigned nvar;
8235 int *signs;
8236 isl_stat r;
8238 if (!set)
8239 return isl_stat_error;
8240 if (isl_set_plain_is_empty(set))
8241 return isl_stat_ok;
8243 nparam = isl_set_dim(set, isl_dim_param);
8244 nvar = isl_set_dim(set, isl_dim_set);
8246 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8248 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8249 fn, user);
8251 free(signs);
8253 return r;
8256 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8258 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8261 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8262 __isl_keep isl_basic_map *bmap2)
8264 int is_subset;
8265 struct isl_map *map1;
8266 struct isl_map *map2;
8268 if (!bmap1 || !bmap2)
8269 return isl_bool_error;
8271 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8272 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8274 is_subset = isl_map_is_subset(map1, map2);
8276 isl_map_free(map1);
8277 isl_map_free(map2);
8279 return is_subset;
8282 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8283 __isl_keep isl_basic_set *bset2)
8285 return isl_basic_map_is_subset(bset1, bset2);
8288 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8289 __isl_keep isl_basic_map *bmap2)
8291 isl_bool is_subset;
8293 if (!bmap1 || !bmap2)
8294 return isl_bool_error;
8295 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8296 if (is_subset != isl_bool_true)
8297 return is_subset;
8298 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8299 return is_subset;
8302 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8303 __isl_keep isl_basic_set *bset2)
8305 return isl_basic_map_is_equal(
8306 bset_to_bmap(bset1), bset_to_bmap(bset2));
8309 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8311 int i;
8312 int is_empty;
8314 if (!map)
8315 return isl_bool_error;
8316 for (i = 0; i < map->n; ++i) {
8317 is_empty = isl_basic_map_is_empty(map->p[i]);
8318 if (is_empty < 0)
8319 return isl_bool_error;
8320 if (!is_empty)
8321 return isl_bool_false;
8323 return isl_bool_true;
8326 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8328 return map ? map->n == 0 : isl_bool_error;
8331 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8333 return set ? set->n == 0 : isl_bool_error;
8336 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8338 return isl_map_is_empty(set_to_map(set));
8341 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8342 __isl_keep isl_map *map2)
8344 if (!map1 || !map2)
8345 return isl_bool_error;
8347 return isl_space_is_equal(map1->dim, map2->dim);
8350 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8351 __isl_keep isl_set *set2)
8353 if (!set1 || !set2)
8354 return isl_bool_error;
8356 return isl_space_is_equal(set1->dim, set2->dim);
8359 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8361 isl_bool is_subset;
8363 if (!map1 || !map2)
8364 return isl_bool_error;
8365 is_subset = isl_map_is_subset(map1, map2);
8366 if (is_subset != isl_bool_true)
8367 return is_subset;
8368 is_subset = isl_map_is_subset(map2, map1);
8369 return is_subset;
8372 /* Is "map1" equal to "map2"?
8374 * First check if they are obviously equal.
8375 * If not, then perform a more detailed analysis.
8377 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8379 isl_bool equal;
8381 equal = isl_map_plain_is_equal(map1, map2);
8382 if (equal < 0 || equal)
8383 return equal;
8384 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8387 isl_bool isl_basic_map_is_strict_subset(
8388 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8390 isl_bool is_subset;
8392 if (!bmap1 || !bmap2)
8393 return isl_bool_error;
8394 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8395 if (is_subset != isl_bool_true)
8396 return is_subset;
8397 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8398 if (is_subset == isl_bool_error)
8399 return is_subset;
8400 return !is_subset;
8403 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8404 __isl_keep isl_map *map2)
8406 isl_bool is_subset;
8408 if (!map1 || !map2)
8409 return isl_bool_error;
8410 is_subset = isl_map_is_subset(map1, map2);
8411 if (is_subset != isl_bool_true)
8412 return is_subset;
8413 is_subset = isl_map_is_subset(map2, map1);
8414 if (is_subset == isl_bool_error)
8415 return is_subset;
8416 return !is_subset;
8419 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8420 __isl_keep isl_set *set2)
8422 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8425 /* Is "bmap" obviously equal to the universe with the same space?
8427 * That is, does it not have any constraints?
8429 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8431 if (!bmap)
8432 return isl_bool_error;
8433 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8436 /* Is "bset" obviously equal to the universe with the same space?
8438 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8440 return isl_basic_map_plain_is_universe(bset);
8443 /* If "c" does not involve any existentially quantified variables,
8444 * then set *univ to false and abort
8446 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8448 isl_bool *univ = user;
8449 unsigned n;
8451 n = isl_constraint_dim(c, isl_dim_div);
8452 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8453 isl_constraint_free(c);
8454 if (*univ < 0 || !*univ)
8455 return isl_stat_error;
8456 return isl_stat_ok;
8459 /* Is "bmap" equal to the universe with the same space?
8461 * First check if it is obviously equal to the universe.
8462 * If not and if there are any constraints not involving
8463 * existentially quantified variables, then it is certainly
8464 * not equal to the universe.
8465 * Otherwise, check if the universe is a subset of "bmap".
8467 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8469 isl_bool univ;
8470 isl_basic_map *test;
8472 univ = isl_basic_map_plain_is_universe(bmap);
8473 if (univ < 0 || univ)
8474 return univ;
8475 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8476 return isl_bool_false;
8477 univ = isl_bool_true;
8478 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8479 univ)
8480 return isl_bool_error;
8481 if (univ < 0 || !univ)
8482 return univ;
8483 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8484 univ = isl_basic_map_is_subset(test, bmap);
8485 isl_basic_map_free(test);
8486 return univ;
8489 /* Is "bset" equal to the universe with the same space?
8491 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8493 return isl_basic_map_is_universe(bset);
8496 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8498 int i;
8500 if (!map)
8501 return isl_bool_error;
8503 for (i = 0; i < map->n; ++i) {
8504 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8505 if (r < 0 || r)
8506 return r;
8509 return isl_bool_false;
8512 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8514 return isl_map_plain_is_universe(set_to_map(set));
8517 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8519 struct isl_basic_set *bset = NULL;
8520 struct isl_vec *sample = NULL;
8521 isl_bool empty, non_empty;
8523 if (!bmap)
8524 return isl_bool_error;
8526 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8527 return isl_bool_true;
8529 if (isl_basic_map_plain_is_universe(bmap))
8530 return isl_bool_false;
8532 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8533 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8534 copy = isl_basic_map_remove_redundancies(copy);
8535 empty = isl_basic_map_plain_is_empty(copy);
8536 isl_basic_map_free(copy);
8537 return empty;
8540 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8541 if (non_empty < 0)
8542 return isl_bool_error;
8543 if (non_empty)
8544 return isl_bool_false;
8545 isl_vec_free(bmap->sample);
8546 bmap->sample = NULL;
8547 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8548 if (!bset)
8549 return isl_bool_error;
8550 sample = isl_basic_set_sample_vec(bset);
8551 if (!sample)
8552 return isl_bool_error;
8553 empty = sample->size == 0;
8554 isl_vec_free(bmap->sample);
8555 bmap->sample = sample;
8556 if (empty)
8557 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8559 return empty;
8562 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8564 if (!bmap)
8565 return isl_bool_error;
8566 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8569 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8571 if (!bset)
8572 return isl_bool_error;
8573 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8576 /* Is "bmap" known to be non-empty?
8578 * That is, is the cached sample still valid?
8580 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8582 unsigned total;
8584 if (!bmap)
8585 return isl_bool_error;
8586 if (!bmap->sample)
8587 return isl_bool_false;
8588 total = 1 + isl_basic_map_total_dim(bmap);
8589 if (bmap->sample->size != total)
8590 return isl_bool_false;
8591 return isl_basic_map_contains(bmap, bmap->sample);
8594 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8596 return isl_basic_map_is_empty(bset_to_bmap(bset));
8599 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8600 __isl_take isl_basic_map *bmap2)
8602 struct isl_map *map;
8603 if (!bmap1 || !bmap2)
8604 goto error;
8606 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8608 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8609 if (!map)
8610 goto error;
8611 map = isl_map_add_basic_map(map, bmap1);
8612 map = isl_map_add_basic_map(map, bmap2);
8613 return map;
8614 error:
8615 isl_basic_map_free(bmap1);
8616 isl_basic_map_free(bmap2);
8617 return NULL;
8620 struct isl_set *isl_basic_set_union(
8621 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8623 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8624 bset_to_bmap(bset2)));
8627 /* Order divs such that any div only depends on previous divs */
8628 __isl_give isl_basic_map *isl_basic_map_order_divs(
8629 __isl_take isl_basic_map *bmap)
8631 int i;
8632 unsigned off;
8634 if (!bmap)
8635 return NULL;
8637 off = isl_space_dim(bmap->dim, isl_dim_all);
8639 for (i = 0; i < bmap->n_div; ++i) {
8640 int pos;
8641 if (isl_int_is_zero(bmap->div[i][0]))
8642 continue;
8643 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8644 bmap->n_div-i);
8645 if (pos == -1)
8646 continue;
8647 if (pos == 0)
8648 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8649 "integer division depends on itself",
8650 return isl_basic_map_free(bmap));
8651 isl_basic_map_swap_div(bmap, i, i + pos);
8652 --i;
8654 return bmap;
8657 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8659 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8662 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8664 int i;
8666 if (!map)
8667 return 0;
8669 for (i = 0; i < map->n; ++i) {
8670 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8671 if (!map->p[i])
8672 goto error;
8675 return map;
8676 error:
8677 isl_map_free(map);
8678 return NULL;
8681 /* Sort the local variables of "bset".
8683 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8684 __isl_take isl_basic_set *bset)
8686 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8689 /* Apply the expansion computed by isl_merge_divs.
8690 * The expansion itself is given by "exp" while the resulting
8691 * list of divs is given by "div".
8693 * Move the integer divisions of "bmap" into the right position
8694 * according to "exp" and then introduce the additional integer
8695 * divisions, adding div constraints.
8696 * The moving should be done first to avoid moving coefficients
8697 * in the definitions of the extra integer divisions.
8699 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8700 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8702 int i, j;
8703 int n_div;
8705 bmap = isl_basic_map_cow(bmap);
8706 if (!bmap || !div)
8707 goto error;
8709 if (div->n_row < bmap->n_div)
8710 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8711 "not an expansion", goto error);
8713 n_div = bmap->n_div;
8714 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8715 div->n_row - n_div, 0,
8716 2 * (div->n_row - n_div));
8718 for (i = n_div; i < div->n_row; ++i)
8719 if (isl_basic_map_alloc_div(bmap) < 0)
8720 goto error;
8722 for (j = n_div - 1; j >= 0; --j) {
8723 if (exp[j] == j)
8724 break;
8725 isl_basic_map_swap_div(bmap, j, exp[j]);
8727 j = 0;
8728 for (i = 0; i < div->n_row; ++i) {
8729 if (j < n_div && exp[j] == i) {
8730 j++;
8731 } else {
8732 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8733 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8734 continue;
8735 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8736 goto error;
8740 isl_mat_free(div);
8741 return bmap;
8742 error:
8743 isl_basic_map_free(bmap);
8744 isl_mat_free(div);
8745 return NULL;
8748 /* Apply the expansion computed by isl_merge_divs.
8749 * The expansion itself is given by "exp" while the resulting
8750 * list of divs is given by "div".
8752 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8753 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8755 return isl_basic_map_expand_divs(bset, div, exp);
8758 /* Look for a div in dst that corresponds to the div "div" in src.
8759 * The divs before "div" in src and dst are assumed to be the same.
8761 * Returns -1 if no corresponding div was found and the position
8762 * of the corresponding div in dst otherwise.
8764 static int find_div(__isl_keep isl_basic_map *dst,
8765 __isl_keep isl_basic_map *src, unsigned div)
8767 int i;
8769 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8771 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8772 for (i = div; i < dst->n_div; ++i)
8773 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8774 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8775 dst->n_div - div) == -1)
8776 return i;
8777 return -1;
8780 /* Align the divs of "dst" to those of "src", adding divs from "src"
8781 * if needed. That is, make sure that the first src->n_div divs
8782 * of the result are equal to those of src.
8784 * The result is not finalized as by design it will have redundant
8785 * divs if any divs from "src" were copied.
8787 __isl_give isl_basic_map *isl_basic_map_align_divs(
8788 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8790 int i;
8791 int known, extended;
8792 unsigned total;
8794 if (!dst || !src)
8795 return isl_basic_map_free(dst);
8797 if (src->n_div == 0)
8798 return dst;
8800 known = isl_basic_map_divs_known(src);
8801 if (known < 0)
8802 return isl_basic_map_free(dst);
8803 if (!known)
8804 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8805 "some src divs are unknown",
8806 return isl_basic_map_free(dst));
8808 src = isl_basic_map_order_divs(src);
8810 extended = 0;
8811 total = isl_space_dim(src->dim, isl_dim_all);
8812 for (i = 0; i < src->n_div; ++i) {
8813 int j = find_div(dst, src, i);
8814 if (j < 0) {
8815 if (!extended) {
8816 int extra = src->n_div - i;
8817 dst = isl_basic_map_cow(dst);
8818 if (!dst)
8819 return NULL;
8820 dst = isl_basic_map_extend_space(dst,
8821 isl_space_copy(dst->dim),
8822 extra, 0, 2 * extra);
8823 extended = 1;
8825 j = isl_basic_map_alloc_div(dst);
8826 if (j < 0)
8827 return isl_basic_map_free(dst);
8828 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8829 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8830 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8831 return isl_basic_map_free(dst);
8833 if (j != i)
8834 isl_basic_map_swap_div(dst, i, j);
8836 return dst;
8839 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8841 int i;
8843 if (!map)
8844 return NULL;
8845 if (map->n == 0)
8846 return map;
8847 map = isl_map_compute_divs(map);
8848 map = isl_map_cow(map);
8849 if (!map)
8850 return NULL;
8852 for (i = 1; i < map->n; ++i)
8853 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8854 for (i = 1; i < map->n; ++i) {
8855 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8856 if (!map->p[i])
8857 return isl_map_free(map);
8860 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8861 return map;
8864 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8866 return isl_map_align_divs_internal(map);
8869 struct isl_set *isl_set_align_divs(struct isl_set *set)
8871 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8874 /* Align the divs of the basic maps in "map" to those
8875 * of the basic maps in "list", as well as to the other basic maps in "map".
8876 * The elements in "list" are assumed to have known divs.
8878 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8879 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8881 int i, n;
8883 map = isl_map_compute_divs(map);
8884 map = isl_map_cow(map);
8885 if (!map || !list)
8886 return isl_map_free(map);
8887 if (map->n == 0)
8888 return map;
8890 n = isl_basic_map_list_n_basic_map(list);
8891 for (i = 0; i < n; ++i) {
8892 isl_basic_map *bmap;
8894 bmap = isl_basic_map_list_get_basic_map(list, i);
8895 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8896 isl_basic_map_free(bmap);
8898 if (!map->p[0])
8899 return isl_map_free(map);
8901 return isl_map_align_divs_internal(map);
8904 /* Align the divs of each element of "list" to those of "bmap".
8905 * Both "bmap" and the elements of "list" are assumed to have known divs.
8907 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8908 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8910 int i, n;
8912 if (!list || !bmap)
8913 return isl_basic_map_list_free(list);
8915 n = isl_basic_map_list_n_basic_map(list);
8916 for (i = 0; i < n; ++i) {
8917 isl_basic_map *bmap_i;
8919 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8920 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8921 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8924 return list;
8927 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8928 __isl_take isl_map *map)
8930 isl_bool ok;
8932 ok = isl_map_compatible_domain(map, set);
8933 if (ok < 0)
8934 goto error;
8935 if (!ok)
8936 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8937 "incompatible spaces", goto error);
8938 map = isl_map_intersect_domain(map, set);
8939 set = isl_map_range(map);
8940 return set;
8941 error:
8942 isl_set_free(set);
8943 isl_map_free(map);
8944 return NULL;
8947 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8948 __isl_take isl_map *map)
8950 return isl_map_align_params_map_map_and(set, map, &set_apply);
8953 /* There is no need to cow as removing empty parts doesn't change
8954 * the meaning of the set.
8956 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
8958 int i;
8960 if (!map)
8961 return NULL;
8963 for (i = map->n - 1; i >= 0; --i)
8964 remove_if_empty(map, i);
8966 return map;
8969 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8971 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8974 /* Given two basic sets bset1 and bset2, compute the maximal difference
8975 * between the values of dimension pos in bset1 and those in bset2
8976 * for any common value of the parameters and dimensions preceding pos.
8978 static enum isl_lp_result basic_set_maximal_difference_at(
8979 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8980 int pos, isl_int *opt)
8982 isl_basic_map *bmap1;
8983 isl_basic_map *bmap2;
8984 struct isl_ctx *ctx;
8985 struct isl_vec *obj;
8986 unsigned total;
8987 unsigned nparam;
8988 unsigned dim1;
8989 enum isl_lp_result res;
8991 if (!bset1 || !bset2)
8992 return isl_lp_error;
8994 nparam = isl_basic_set_n_param(bset1);
8995 dim1 = isl_basic_set_n_dim(bset1);
8997 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8998 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8999 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9000 isl_dim_out, 0, pos);
9001 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9002 isl_dim_out, 0, pos);
9003 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
9004 if (!bmap1)
9005 return isl_lp_error;
9007 total = isl_basic_map_total_dim(bmap1);
9008 ctx = bmap1->ctx;
9009 obj = isl_vec_alloc(ctx, 1 + total);
9010 if (!obj)
9011 goto error;
9012 isl_seq_clr(obj->block.data, 1 + total);
9013 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9014 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9015 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9016 opt, NULL, NULL);
9017 isl_basic_map_free(bmap1);
9018 isl_vec_free(obj);
9019 return res;
9020 error:
9021 isl_basic_map_free(bmap1);
9022 return isl_lp_error;
9025 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9026 * for any common value of the parameters and dimensions preceding pos
9027 * in both basic sets, the values of dimension pos in bset1 are
9028 * smaller or larger than those in bset2.
9030 * Returns
9031 * 1 if bset1 follows bset2
9032 * -1 if bset1 precedes bset2
9033 * 0 if bset1 and bset2 are incomparable
9034 * -2 if some error occurred.
9036 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
9037 struct isl_basic_set *bset2, int pos)
9039 isl_int opt;
9040 enum isl_lp_result res;
9041 int cmp;
9043 isl_int_init(opt);
9045 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9047 if (res == isl_lp_empty)
9048 cmp = 0;
9049 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9050 res == isl_lp_unbounded)
9051 cmp = 1;
9052 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9053 cmp = -1;
9054 else
9055 cmp = -2;
9057 isl_int_clear(opt);
9058 return cmp;
9061 /* Given two basic sets bset1 and bset2, check whether
9062 * for any common value of the parameters and dimensions preceding pos
9063 * there is a value of dimension pos in bset1 that is larger
9064 * than a value of the same dimension in bset2.
9066 * Return
9067 * 1 if there exists such a pair
9068 * 0 if there is no such pair, but there is a pair of equal values
9069 * -1 otherwise
9070 * -2 if some error occurred.
9072 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9073 __isl_keep isl_basic_set *bset2, int pos)
9075 isl_int opt;
9076 enum isl_lp_result res;
9077 int cmp;
9079 isl_int_init(opt);
9081 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9083 if (res == isl_lp_empty)
9084 cmp = -1;
9085 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9086 res == isl_lp_unbounded)
9087 cmp = 1;
9088 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9089 cmp = -1;
9090 else if (res == isl_lp_ok)
9091 cmp = 0;
9092 else
9093 cmp = -2;
9095 isl_int_clear(opt);
9096 return cmp;
9099 /* Given two sets set1 and set2, check whether
9100 * for any common value of the parameters and dimensions preceding pos
9101 * there is a value of dimension pos in set1 that is larger
9102 * than a value of the same dimension in set2.
9104 * Return
9105 * 1 if there exists such a pair
9106 * 0 if there is no such pair, but there is a pair of equal values
9107 * -1 otherwise
9108 * -2 if some error occurred.
9110 int isl_set_follows_at(__isl_keep isl_set *set1,
9111 __isl_keep isl_set *set2, int pos)
9113 int i, j;
9114 int follows = -1;
9116 if (!set1 || !set2)
9117 return -2;
9119 for (i = 0; i < set1->n; ++i)
9120 for (j = 0; j < set2->n; ++j) {
9121 int f;
9122 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9123 if (f == 1 || f == -2)
9124 return f;
9125 if (f > follows)
9126 follows = f;
9129 return follows;
9132 static isl_bool isl_basic_map_plain_has_fixed_var(
9133 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9135 int i;
9136 int d;
9137 unsigned total;
9139 if (!bmap)
9140 return isl_bool_error;
9141 total = isl_basic_map_total_dim(bmap);
9142 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9143 for (; d+1 > pos; --d)
9144 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9145 break;
9146 if (d != pos)
9147 continue;
9148 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9149 return isl_bool_false;
9150 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9151 return isl_bool_false;
9152 if (!isl_int_is_one(bmap->eq[i][1+d]))
9153 return isl_bool_false;
9154 if (val)
9155 isl_int_neg(*val, bmap->eq[i][0]);
9156 return isl_bool_true;
9158 return isl_bool_false;
9161 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9162 unsigned pos, isl_int *val)
9164 int i;
9165 isl_int v;
9166 isl_int tmp;
9167 isl_bool fixed;
9169 if (!map)
9170 return isl_bool_error;
9171 if (map->n == 0)
9172 return isl_bool_false;
9173 if (map->n == 1)
9174 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9175 isl_int_init(v);
9176 isl_int_init(tmp);
9177 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9178 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9179 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9180 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9181 fixed = isl_bool_false;
9183 if (val)
9184 isl_int_set(*val, v);
9185 isl_int_clear(tmp);
9186 isl_int_clear(v);
9187 return fixed;
9190 static isl_bool isl_basic_set_plain_has_fixed_var(
9191 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9193 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9194 pos, val);
9197 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9198 enum isl_dim_type type, unsigned pos, isl_int *val)
9200 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9201 return isl_bool_error;
9202 return isl_basic_map_plain_has_fixed_var(bmap,
9203 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9206 /* If "bmap" obviously lies on a hyperplane where the given dimension
9207 * has a fixed value, then return that value.
9208 * Otherwise return NaN.
9210 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9211 __isl_keep isl_basic_map *bmap,
9212 enum isl_dim_type type, unsigned pos)
9214 isl_ctx *ctx;
9215 isl_val *v;
9216 isl_bool fixed;
9218 if (!bmap)
9219 return NULL;
9220 ctx = isl_basic_map_get_ctx(bmap);
9221 v = isl_val_alloc(ctx);
9222 if (!v)
9223 return NULL;
9224 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9225 if (fixed < 0)
9226 return isl_val_free(v);
9227 if (fixed) {
9228 isl_int_set_si(v->d, 1);
9229 return v;
9231 isl_val_free(v);
9232 return isl_val_nan(ctx);
9235 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9236 enum isl_dim_type type, unsigned pos, isl_int *val)
9238 if (pos >= isl_map_dim(map, type))
9239 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9240 "position out of bounds", return isl_bool_error);
9241 return isl_map_plain_has_fixed_var(map,
9242 map_offset(map, type) - 1 + pos, val);
9245 /* If "map" obviously lies on a hyperplane where the given dimension
9246 * has a fixed value, then return that value.
9247 * Otherwise return NaN.
9249 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9250 enum isl_dim_type type, unsigned pos)
9252 isl_ctx *ctx;
9253 isl_val *v;
9254 isl_bool fixed;
9256 if (!map)
9257 return NULL;
9258 ctx = isl_map_get_ctx(map);
9259 v = isl_val_alloc(ctx);
9260 if (!v)
9261 return NULL;
9262 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9263 if (fixed < 0)
9264 return isl_val_free(v);
9265 if (fixed) {
9266 isl_int_set_si(v->d, 1);
9267 return v;
9269 isl_val_free(v);
9270 return isl_val_nan(ctx);
9273 /* If "set" obviously lies on a hyperplane where the given dimension
9274 * has a fixed value, then return that value.
9275 * Otherwise return NaN.
9277 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9278 enum isl_dim_type type, unsigned pos)
9280 return isl_map_plain_get_val_if_fixed(set, type, pos);
9283 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
9284 enum isl_dim_type type, unsigned pos, isl_int *val)
9286 return isl_map_plain_is_fixed(set, type, pos, val);
9289 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9290 * then return this fixed value in *val.
9292 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9293 unsigned dim, isl_int *val)
9295 return isl_basic_set_plain_has_fixed_var(bset,
9296 isl_basic_set_n_param(bset) + dim, val);
9299 /* Return -1 if the constraint "c1" should be sorted before "c2"
9300 * and 1 if it should be sorted after "c2".
9301 * Return 0 if the two constraints are the same (up to the constant term).
9303 * In particular, if a constraint involves later variables than another
9304 * then it is sorted after this other constraint.
9305 * uset_gist depends on constraints without existentially quantified
9306 * variables sorting first.
9308 * For constraints that have the same latest variable, those
9309 * with the same coefficient for this latest variable (first in absolute value
9310 * and then in actual value) are grouped together.
9311 * This is useful for detecting pairs of constraints that can
9312 * be chained in their printed representation.
9314 * Finally, within a group, constraints are sorted according to
9315 * their coefficients (excluding the constant term).
9317 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9319 isl_int **c1 = (isl_int **) p1;
9320 isl_int **c2 = (isl_int **) p2;
9321 int l1, l2;
9322 unsigned size = *(unsigned *) arg;
9323 int cmp;
9325 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9326 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9328 if (l1 != l2)
9329 return l1 - l2;
9331 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9332 if (cmp != 0)
9333 return cmp;
9334 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9335 if (cmp != 0)
9336 return -cmp;
9338 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9341 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9342 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9343 * and 0 if the two constraints are the same (up to the constant term).
9345 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9346 isl_int *c1, isl_int *c2)
9348 unsigned total;
9350 if (!bmap)
9351 return -2;
9352 total = isl_basic_map_total_dim(bmap);
9353 return sort_constraint_cmp(&c1, &c2, &total);
9356 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9357 __isl_take isl_basic_map *bmap)
9359 unsigned total;
9361 if (!bmap)
9362 return NULL;
9363 if (bmap->n_ineq == 0)
9364 return bmap;
9365 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9366 return bmap;
9367 total = isl_basic_map_total_dim(bmap);
9368 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9369 &sort_constraint_cmp, &total) < 0)
9370 return isl_basic_map_free(bmap);
9371 return bmap;
9374 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9375 __isl_take isl_basic_set *bset)
9377 isl_basic_map *bmap = bset_to_bmap(bset);
9378 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9381 __isl_give isl_basic_map *isl_basic_map_normalize(
9382 __isl_take isl_basic_map *bmap)
9384 if (!bmap)
9385 return NULL;
9386 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9387 return bmap;
9388 bmap = isl_basic_map_remove_redundancies(bmap);
9389 bmap = isl_basic_map_sort_constraints(bmap);
9390 if (bmap)
9391 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9392 return bmap;
9394 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9395 __isl_keep isl_basic_map *bmap2)
9397 int i, cmp;
9398 unsigned total;
9399 isl_space *space1, *space2;
9401 if (!bmap1 || !bmap2)
9402 return -1;
9404 if (bmap1 == bmap2)
9405 return 0;
9406 space1 = isl_basic_map_peek_space(bmap1);
9407 space2 = isl_basic_map_peek_space(bmap2);
9408 cmp = isl_space_cmp(space1, space2);
9409 if (cmp)
9410 return cmp;
9411 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9412 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9413 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9414 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9415 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9416 return 0;
9417 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9418 return 1;
9419 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9420 return -1;
9421 if (bmap1->n_eq != bmap2->n_eq)
9422 return bmap1->n_eq - bmap2->n_eq;
9423 if (bmap1->n_ineq != bmap2->n_ineq)
9424 return bmap1->n_ineq - bmap2->n_ineq;
9425 if (bmap1->n_div != bmap2->n_div)
9426 return bmap1->n_div - bmap2->n_div;
9427 total = isl_basic_map_total_dim(bmap1);
9428 for (i = 0; i < bmap1->n_eq; ++i) {
9429 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9430 if (cmp)
9431 return cmp;
9433 for (i = 0; i < bmap1->n_ineq; ++i) {
9434 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9435 if (cmp)
9436 return cmp;
9438 for (i = 0; i < bmap1->n_div; ++i) {
9439 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9440 if (cmp)
9441 return cmp;
9443 return 0;
9446 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9447 __isl_keep isl_basic_set *bset2)
9449 return isl_basic_map_plain_cmp(bset1, bset2);
9452 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9454 int i, cmp;
9456 if (set1 == set2)
9457 return 0;
9458 if (set1->n != set2->n)
9459 return set1->n - set2->n;
9461 for (i = 0; i < set1->n; ++i) {
9462 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9463 if (cmp)
9464 return cmp;
9467 return 0;
9470 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9471 __isl_keep isl_basic_map *bmap2)
9473 if (!bmap1 || !bmap2)
9474 return isl_bool_error;
9475 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9478 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9479 __isl_keep isl_basic_set *bset2)
9481 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9482 bset_to_bmap(bset2));
9485 static int qsort_bmap_cmp(const void *p1, const void *p2)
9487 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9488 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9490 return isl_basic_map_plain_cmp(bmap1, bmap2);
9493 /* Sort the basic maps of "map" and remove duplicate basic maps.
9495 * While removing basic maps, we make sure that the basic maps remain
9496 * sorted because isl_map_normalize expects the basic maps of the result
9497 * to be sorted.
9499 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9501 int i, j;
9503 map = isl_map_remove_empty_parts(map);
9504 if (!map)
9505 return NULL;
9506 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9507 for (i = map->n - 1; i >= 1; --i) {
9508 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9509 continue;
9510 isl_basic_map_free(map->p[i-1]);
9511 for (j = i; j < map->n; ++j)
9512 map->p[j - 1] = map->p[j];
9513 map->n--;
9516 return map;
9519 /* Remove obvious duplicates among the basic maps of "map".
9521 * Unlike isl_map_normalize, this function does not remove redundant
9522 * constraints and only removes duplicates that have exactly the same
9523 * constraints in the input. It does sort the constraints and
9524 * the basic maps to ease the detection of duplicates.
9526 * If "map" has already been normalized or if the basic maps are
9527 * disjoint, then there can be no duplicates.
9529 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9531 int i;
9532 isl_basic_map *bmap;
9534 if (!map)
9535 return NULL;
9536 if (map->n <= 1)
9537 return map;
9538 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9539 return map;
9540 for (i = 0; i < map->n; ++i) {
9541 bmap = isl_basic_map_copy(map->p[i]);
9542 bmap = isl_basic_map_sort_constraints(bmap);
9543 if (!bmap)
9544 return isl_map_free(map);
9545 isl_basic_map_free(map->p[i]);
9546 map->p[i] = bmap;
9549 map = sort_and_remove_duplicates(map);
9550 return map;
9553 /* We normalize in place, but if anything goes wrong we need
9554 * to return NULL, so we need to make sure we don't change the
9555 * meaning of any possible other copies of map.
9557 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9559 int i;
9560 struct isl_basic_map *bmap;
9562 if (!map)
9563 return NULL;
9564 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9565 return map;
9566 for (i = 0; i < map->n; ++i) {
9567 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9568 if (!bmap)
9569 goto error;
9570 isl_basic_map_free(map->p[i]);
9571 map->p[i] = bmap;
9574 map = sort_and_remove_duplicates(map);
9575 if (map)
9576 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9577 return map;
9578 error:
9579 isl_map_free(map);
9580 return NULL;
9583 struct isl_set *isl_set_normalize(struct isl_set *set)
9585 return set_from_map(isl_map_normalize(set_to_map(set)));
9588 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9589 __isl_keep isl_map *map2)
9591 int i;
9592 isl_bool equal;
9594 if (!map1 || !map2)
9595 return isl_bool_error;
9597 if (map1 == map2)
9598 return isl_bool_true;
9599 if (!isl_space_is_equal(map1->dim, map2->dim))
9600 return isl_bool_false;
9602 map1 = isl_map_copy(map1);
9603 map2 = isl_map_copy(map2);
9604 map1 = isl_map_normalize(map1);
9605 map2 = isl_map_normalize(map2);
9606 if (!map1 || !map2)
9607 goto error;
9608 equal = map1->n == map2->n;
9609 for (i = 0; equal && i < map1->n; ++i) {
9610 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9611 if (equal < 0)
9612 goto error;
9614 isl_map_free(map1);
9615 isl_map_free(map2);
9616 return equal;
9617 error:
9618 isl_map_free(map1);
9619 isl_map_free(map2);
9620 return isl_bool_error;
9623 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9624 __isl_keep isl_set *set2)
9626 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9629 /* Return the basic maps in "map" as a list.
9631 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9632 __isl_keep isl_map *map)
9634 int i;
9635 isl_ctx *ctx;
9636 isl_basic_map_list *list;
9638 if (!map)
9639 return NULL;
9640 ctx = isl_map_get_ctx(map);
9641 list = isl_basic_map_list_alloc(ctx, map->n);
9643 for (i = 0; i < map->n; ++i) {
9644 isl_basic_map *bmap;
9646 bmap = isl_basic_map_copy(map->p[i]);
9647 list = isl_basic_map_list_add(list, bmap);
9650 return list;
9653 /* Return the intersection of the elements in the non-empty list "list".
9654 * All elements are assumed to live in the same space.
9656 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9657 __isl_take isl_basic_map_list *list)
9659 int i, n;
9660 isl_basic_map *bmap;
9662 if (!list)
9663 return NULL;
9664 n = isl_basic_map_list_n_basic_map(list);
9665 if (n < 1)
9666 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9667 "expecting non-empty list", goto error);
9669 bmap = isl_basic_map_list_get_basic_map(list, 0);
9670 for (i = 1; i < n; ++i) {
9671 isl_basic_map *bmap_i;
9673 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9674 bmap = isl_basic_map_intersect(bmap, bmap_i);
9677 isl_basic_map_list_free(list);
9678 return bmap;
9679 error:
9680 isl_basic_map_list_free(list);
9681 return NULL;
9684 /* Return the intersection of the elements in the non-empty list "list".
9685 * All elements are assumed to live in the same space.
9687 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9688 __isl_take isl_basic_set_list *list)
9690 return isl_basic_map_list_intersect(list);
9693 /* Return the union of the elements of "list".
9694 * The list is required to have at least one element.
9696 __isl_give isl_set *isl_basic_set_list_union(
9697 __isl_take isl_basic_set_list *list)
9699 int i, n;
9700 isl_space *space;
9701 isl_basic_set *bset;
9702 isl_set *set;
9704 if (!list)
9705 return NULL;
9706 n = isl_basic_set_list_n_basic_set(list);
9707 if (n < 1)
9708 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9709 "expecting non-empty list", goto error);
9711 bset = isl_basic_set_list_get_basic_set(list, 0);
9712 space = isl_basic_set_get_space(bset);
9713 isl_basic_set_free(bset);
9715 set = isl_set_alloc_space(space, n, 0);
9716 for (i = 0; i < n; ++i) {
9717 bset = isl_basic_set_list_get_basic_set(list, i);
9718 set = isl_set_add_basic_set(set, bset);
9721 isl_basic_set_list_free(list);
9722 return set;
9723 error:
9724 isl_basic_set_list_free(list);
9725 return NULL;
9728 /* Return the union of the elements in the non-empty list "list".
9729 * All elements are assumed to live in the same space.
9731 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9733 int i, n;
9734 isl_set *set;
9736 if (!list)
9737 return NULL;
9738 n = isl_set_list_n_set(list);
9739 if (n < 1)
9740 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9741 "expecting non-empty list", goto error);
9743 set = isl_set_list_get_set(list, 0);
9744 for (i = 1; i < n; ++i) {
9745 isl_set *set_i;
9747 set_i = isl_set_list_get_set(list, i);
9748 set = isl_set_union(set, set_i);
9751 isl_set_list_free(list);
9752 return set;
9753 error:
9754 isl_set_list_free(list);
9755 return NULL;
9758 __isl_give isl_basic_map *isl_basic_map_product(
9759 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9761 isl_space *dim_result = NULL;
9762 struct isl_basic_map *bmap;
9763 unsigned in1, in2, out1, out2, nparam, total, pos;
9764 struct isl_dim_map *dim_map1, *dim_map2;
9766 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9767 goto error;
9768 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9769 isl_space_copy(bmap2->dim));
9771 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9772 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9773 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9774 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9775 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9777 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9778 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9779 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9780 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9781 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9782 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9783 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9784 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9785 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9786 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9787 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9789 bmap = isl_basic_map_alloc_space(dim_result,
9790 bmap1->n_div + bmap2->n_div,
9791 bmap1->n_eq + bmap2->n_eq,
9792 bmap1->n_ineq + bmap2->n_ineq);
9793 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9794 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9795 bmap = isl_basic_map_simplify(bmap);
9796 return isl_basic_map_finalize(bmap);
9797 error:
9798 isl_basic_map_free(bmap1);
9799 isl_basic_map_free(bmap2);
9800 return NULL;
9803 __isl_give isl_basic_map *isl_basic_map_flat_product(
9804 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9806 isl_basic_map *prod;
9808 prod = isl_basic_map_product(bmap1, bmap2);
9809 prod = isl_basic_map_flatten(prod);
9810 return prod;
9813 __isl_give isl_basic_set *isl_basic_set_flat_product(
9814 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9816 return isl_basic_map_flat_range_product(bset1, bset2);
9819 __isl_give isl_basic_map *isl_basic_map_domain_product(
9820 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9822 isl_space *space_result = NULL;
9823 isl_basic_map *bmap;
9824 unsigned in1, in2, out, nparam, total, pos;
9825 struct isl_dim_map *dim_map1, *dim_map2;
9827 if (!bmap1 || !bmap2)
9828 goto error;
9830 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9831 isl_space_copy(bmap2->dim));
9833 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9834 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9835 out = isl_basic_map_dim(bmap1, isl_dim_out);
9836 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9838 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9839 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9840 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9841 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9842 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9843 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9844 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9845 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9846 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9847 isl_dim_map_div(dim_map1, bmap1, pos += out);
9848 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9850 bmap = isl_basic_map_alloc_space(space_result,
9851 bmap1->n_div + bmap2->n_div,
9852 bmap1->n_eq + bmap2->n_eq,
9853 bmap1->n_ineq + bmap2->n_ineq);
9854 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9855 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9856 bmap = isl_basic_map_simplify(bmap);
9857 return isl_basic_map_finalize(bmap);
9858 error:
9859 isl_basic_map_free(bmap1);
9860 isl_basic_map_free(bmap2);
9861 return NULL;
9864 __isl_give isl_basic_map *isl_basic_map_range_product(
9865 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9867 isl_bool rational;
9868 isl_space *dim_result = NULL;
9869 isl_basic_map *bmap;
9870 unsigned in, out1, out2, nparam, total, pos;
9871 struct isl_dim_map *dim_map1, *dim_map2;
9873 rational = isl_basic_map_is_rational(bmap1);
9874 if (rational >= 0 && rational)
9875 rational = isl_basic_map_is_rational(bmap2);
9876 if (!bmap1 || !bmap2 || rational < 0)
9877 goto error;
9879 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9880 goto error;
9882 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9883 isl_space_copy(bmap2->dim));
9885 in = isl_basic_map_dim(bmap1, isl_dim_in);
9886 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9887 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9888 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9890 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9891 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9892 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9893 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9894 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9895 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9896 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9897 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9898 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9899 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9900 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9902 bmap = isl_basic_map_alloc_space(dim_result,
9903 bmap1->n_div + bmap2->n_div,
9904 bmap1->n_eq + bmap2->n_eq,
9905 bmap1->n_ineq + bmap2->n_ineq);
9906 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9907 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9908 if (rational)
9909 bmap = isl_basic_map_set_rational(bmap);
9910 bmap = isl_basic_map_simplify(bmap);
9911 return isl_basic_map_finalize(bmap);
9912 error:
9913 isl_basic_map_free(bmap1);
9914 isl_basic_map_free(bmap2);
9915 return NULL;
9918 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9919 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9921 isl_basic_map *prod;
9923 prod = isl_basic_map_range_product(bmap1, bmap2);
9924 prod = isl_basic_map_flatten_range(prod);
9925 return prod;
9928 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9929 * and collect the results.
9930 * The result live in the space obtained by calling "space_product"
9931 * on the spaces of "map1" and "map2".
9932 * If "remove_duplicates" is set then the result may contain duplicates
9933 * (even if the inputs do not) and so we try and remove the obvious
9934 * duplicates.
9936 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9937 __isl_take isl_map *map2,
9938 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9939 __isl_take isl_space *right),
9940 __isl_give isl_basic_map *(*basic_map_product)(
9941 __isl_take isl_basic_map *left,
9942 __isl_take isl_basic_map *right),
9943 int remove_duplicates)
9945 unsigned flags = 0;
9946 struct isl_map *result;
9947 int i, j;
9948 isl_bool m;
9950 m = isl_map_has_equal_params(map1, map2);
9951 if (m < 0)
9952 goto error;
9953 if (!m)
9954 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
9955 "parameters don't match", goto error);
9957 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9958 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9959 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9961 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9962 isl_space_copy(map2->dim)),
9963 map1->n * map2->n, flags);
9964 if (!result)
9965 goto error;
9966 for (i = 0; i < map1->n; ++i)
9967 for (j = 0; j < map2->n; ++j) {
9968 struct isl_basic_map *part;
9969 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9970 isl_basic_map_copy(map2->p[j]));
9971 if (isl_basic_map_is_empty(part))
9972 isl_basic_map_free(part);
9973 else
9974 result = isl_map_add_basic_map(result, part);
9975 if (!result)
9976 goto error;
9978 if (remove_duplicates)
9979 result = isl_map_remove_obvious_duplicates(result);
9980 isl_map_free(map1);
9981 isl_map_free(map2);
9982 return result;
9983 error:
9984 isl_map_free(map1);
9985 isl_map_free(map2);
9986 return NULL;
9989 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9991 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9992 __isl_take isl_map *map2)
9994 return map_product(map1, map2, &isl_space_product,
9995 &isl_basic_map_product, 0);
9998 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9999 __isl_take isl_map *map2)
10001 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10004 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10006 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10007 __isl_take isl_map *map2)
10009 isl_map *prod;
10011 prod = isl_map_product(map1, map2);
10012 prod = isl_map_flatten(prod);
10013 return prod;
10016 /* Given two set A and B, construct its Cartesian product A x B.
10018 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10020 return isl_map_range_product(set1, set2);
10023 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10024 __isl_take isl_set *set2)
10026 return isl_map_flat_range_product(set1, set2);
10029 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10031 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10032 __isl_take isl_map *map2)
10034 return map_product(map1, map2, &isl_space_domain_product,
10035 &isl_basic_map_domain_product, 1);
10038 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10040 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10041 __isl_take isl_map *map2)
10043 return map_product(map1, map2, &isl_space_range_product,
10044 &isl_basic_map_range_product, 1);
10047 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10048 __isl_take isl_map *map2)
10050 return isl_map_align_params_map_map_and(map1, map2,
10051 &map_domain_product_aligned);
10054 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10055 __isl_take isl_map *map2)
10057 return isl_map_align_params_map_map_and(map1, map2,
10058 &map_range_product_aligned);
10061 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10063 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10065 isl_space *space;
10066 int total1, keep1, total2, keep2;
10068 if (!map)
10069 return NULL;
10070 if (!isl_space_domain_is_wrapping(map->dim) ||
10071 !isl_space_range_is_wrapping(map->dim))
10072 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10073 "not a product", return isl_map_free(map));
10075 space = isl_map_get_space(map);
10076 total1 = isl_space_dim(space, isl_dim_in);
10077 total2 = isl_space_dim(space, isl_dim_out);
10078 space = isl_space_factor_domain(space);
10079 keep1 = isl_space_dim(space, isl_dim_in);
10080 keep2 = isl_space_dim(space, isl_dim_out);
10081 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10082 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10083 map = isl_map_reset_space(map, space);
10085 return map;
10088 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10090 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10092 isl_space *space;
10093 int total1, keep1, total2, keep2;
10095 if (!map)
10096 return NULL;
10097 if (!isl_space_domain_is_wrapping(map->dim) ||
10098 !isl_space_range_is_wrapping(map->dim))
10099 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10100 "not a product", return isl_map_free(map));
10102 space = isl_map_get_space(map);
10103 total1 = isl_space_dim(space, isl_dim_in);
10104 total2 = isl_space_dim(space, isl_dim_out);
10105 space = isl_space_factor_range(space);
10106 keep1 = isl_space_dim(space, isl_dim_in);
10107 keep2 = isl_space_dim(space, isl_dim_out);
10108 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10109 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10110 map = isl_map_reset_space(map, space);
10112 return map;
10115 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10117 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10119 isl_space *space;
10120 int total, keep;
10122 if (!map)
10123 return NULL;
10124 if (!isl_space_domain_is_wrapping(map->dim))
10125 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10126 "domain is not a product", return isl_map_free(map));
10128 space = isl_map_get_space(map);
10129 total = isl_space_dim(space, isl_dim_in);
10130 space = isl_space_domain_factor_domain(space);
10131 keep = isl_space_dim(space, isl_dim_in);
10132 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10133 map = isl_map_reset_space(map, space);
10135 return map;
10138 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10140 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10142 isl_space *space;
10143 int total, keep;
10145 if (!map)
10146 return NULL;
10147 if (!isl_space_domain_is_wrapping(map->dim))
10148 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10149 "domain is not a product", return isl_map_free(map));
10151 space = isl_map_get_space(map);
10152 total = isl_space_dim(space, isl_dim_in);
10153 space = isl_space_domain_factor_range(space);
10154 keep = isl_space_dim(space, isl_dim_in);
10155 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10156 map = isl_map_reset_space(map, space);
10158 return map;
10161 /* Given a map A -> [B -> C], extract the map A -> B.
10163 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10165 isl_space *space;
10166 int total, keep;
10168 if (!map)
10169 return NULL;
10170 if (!isl_space_range_is_wrapping(map->dim))
10171 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10172 "range is not a product", return isl_map_free(map));
10174 space = isl_map_get_space(map);
10175 total = isl_space_dim(space, isl_dim_out);
10176 space = isl_space_range_factor_domain(space);
10177 keep = isl_space_dim(space, isl_dim_out);
10178 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10179 map = isl_map_reset_space(map, space);
10181 return map;
10184 /* Given a map A -> [B -> C], extract the map A -> C.
10186 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10188 isl_space *space;
10189 int total, keep;
10191 if (!map)
10192 return NULL;
10193 if (!isl_space_range_is_wrapping(map->dim))
10194 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10195 "range is not a product", return isl_map_free(map));
10197 space = isl_map_get_space(map);
10198 total = isl_space_dim(space, isl_dim_out);
10199 space = isl_space_range_factor_range(space);
10200 keep = isl_space_dim(space, isl_dim_out);
10201 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10202 map = isl_map_reset_space(map, space);
10204 return map;
10207 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10209 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10210 __isl_take isl_map *map2)
10212 isl_map *prod;
10214 prod = isl_map_domain_product(map1, map2);
10215 prod = isl_map_flatten_domain(prod);
10216 return prod;
10219 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10221 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10222 __isl_take isl_map *map2)
10224 isl_map *prod;
10226 prod = isl_map_range_product(map1, map2);
10227 prod = isl_map_flatten_range(prod);
10228 return prod;
10231 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10233 int i;
10234 uint32_t hash = isl_hash_init();
10235 unsigned total;
10237 if (!bmap)
10238 return 0;
10239 bmap = isl_basic_map_copy(bmap);
10240 bmap = isl_basic_map_normalize(bmap);
10241 if (!bmap)
10242 return 0;
10243 total = isl_basic_map_total_dim(bmap);
10244 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10245 for (i = 0; i < bmap->n_eq; ++i) {
10246 uint32_t c_hash;
10247 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10248 isl_hash_hash(hash, c_hash);
10250 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10251 for (i = 0; i < bmap->n_ineq; ++i) {
10252 uint32_t c_hash;
10253 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10254 isl_hash_hash(hash, c_hash);
10256 isl_hash_byte(hash, bmap->n_div & 0xFF);
10257 for (i = 0; i < bmap->n_div; ++i) {
10258 uint32_t c_hash;
10259 if (isl_int_is_zero(bmap->div[i][0]))
10260 continue;
10261 isl_hash_byte(hash, i & 0xFF);
10262 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10263 isl_hash_hash(hash, c_hash);
10265 isl_basic_map_free(bmap);
10266 return hash;
10269 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10271 return isl_basic_map_get_hash(bset_to_bmap(bset));
10274 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10276 int i;
10277 uint32_t hash;
10279 if (!map)
10280 return 0;
10281 map = isl_map_copy(map);
10282 map = isl_map_normalize(map);
10283 if (!map)
10284 return 0;
10286 hash = isl_hash_init();
10287 for (i = 0; i < map->n; ++i) {
10288 uint32_t bmap_hash;
10289 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10290 isl_hash_hash(hash, bmap_hash);
10293 isl_map_free(map);
10295 return hash;
10298 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10300 return isl_map_get_hash(set_to_map(set));
10303 /* Return the number of basic maps in the (current) representation of "map".
10305 int isl_map_n_basic_map(__isl_keep isl_map *map)
10307 return map ? map->n : 0;
10310 int isl_set_n_basic_set(__isl_keep isl_set *set)
10312 return set ? set->n : 0;
10315 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10316 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10318 int i;
10320 if (!map)
10321 return isl_stat_error;
10323 for (i = 0; i < map->n; ++i)
10324 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10325 return isl_stat_error;
10327 return isl_stat_ok;
10330 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10331 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10333 int i;
10335 if (!set)
10336 return isl_stat_error;
10338 for (i = 0; i < set->n; ++i)
10339 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10340 return isl_stat_error;
10342 return isl_stat_ok;
10345 /* Return a list of basic sets, the union of which is equal to "set".
10347 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10348 __isl_keep isl_set *set)
10350 int i;
10351 isl_basic_set_list *list;
10353 if (!set)
10354 return NULL;
10356 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10357 for (i = 0; i < set->n; ++i) {
10358 isl_basic_set *bset;
10360 bset = isl_basic_set_copy(set->p[i]);
10361 list = isl_basic_set_list_add(list, bset);
10364 return list;
10367 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10369 isl_space *dim;
10371 if (!bset)
10372 return NULL;
10374 bset = isl_basic_set_cow(bset);
10375 if (!bset)
10376 return NULL;
10378 dim = isl_basic_set_get_space(bset);
10379 dim = isl_space_lift(dim, bset->n_div);
10380 if (!dim)
10381 goto error;
10382 isl_space_free(bset->dim);
10383 bset->dim = dim;
10384 bset->extra -= bset->n_div;
10385 bset->n_div = 0;
10387 bset = isl_basic_set_finalize(bset);
10389 return bset;
10390 error:
10391 isl_basic_set_free(bset);
10392 return NULL;
10395 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10397 int i;
10398 isl_space *dim;
10399 unsigned n_div;
10401 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10403 if (!set)
10404 return NULL;
10406 set = isl_set_cow(set);
10407 if (!set)
10408 return NULL;
10410 n_div = set->p[0]->n_div;
10411 dim = isl_set_get_space(set);
10412 dim = isl_space_lift(dim, n_div);
10413 if (!dim)
10414 goto error;
10415 isl_space_free(set->dim);
10416 set->dim = dim;
10418 for (i = 0; i < set->n; ++i) {
10419 set->p[i] = isl_basic_set_lift(set->p[i]);
10420 if (!set->p[i])
10421 goto error;
10424 return set;
10425 error:
10426 isl_set_free(set);
10427 return NULL;
10430 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10432 unsigned dim;
10433 int size = 0;
10435 if (!bset)
10436 return -1;
10438 dim = isl_basic_set_total_dim(bset);
10439 size += bset->n_eq * (1 + dim);
10440 size += bset->n_ineq * (1 + dim);
10441 size += bset->n_div * (2 + dim);
10443 return size;
10446 int isl_set_size(__isl_keep isl_set *set)
10448 int i;
10449 int size = 0;
10451 if (!set)
10452 return -1;
10454 for (i = 0; i < set->n; ++i)
10455 size += isl_basic_set_size(set->p[i]);
10457 return size;
10460 /* Check if there is any lower bound (if lower == 0) and/or upper
10461 * bound (if upper == 0) on the specified dim.
10463 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10464 enum isl_dim_type type, unsigned pos, int lower, int upper)
10466 int i;
10468 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10469 return isl_bool_error;
10471 pos += isl_basic_map_offset(bmap, type);
10473 for (i = 0; i < bmap->n_div; ++i) {
10474 if (isl_int_is_zero(bmap->div[i][0]))
10475 continue;
10476 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10477 return isl_bool_true;
10480 for (i = 0; i < bmap->n_eq; ++i)
10481 if (!isl_int_is_zero(bmap->eq[i][pos]))
10482 return isl_bool_true;
10484 for (i = 0; i < bmap->n_ineq; ++i) {
10485 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10486 if (sgn > 0)
10487 lower = 1;
10488 if (sgn < 0)
10489 upper = 1;
10492 return lower && upper;
10495 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10496 enum isl_dim_type type, unsigned pos)
10498 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10501 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10502 enum isl_dim_type type, unsigned pos)
10504 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10507 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10508 enum isl_dim_type type, unsigned pos)
10510 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10513 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10514 enum isl_dim_type type, unsigned pos)
10516 int i;
10518 if (!map)
10519 return isl_bool_error;
10521 for (i = 0; i < map->n; ++i) {
10522 isl_bool bounded;
10523 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10524 if (bounded < 0 || !bounded)
10525 return bounded;
10528 return isl_bool_true;
10531 /* Return true if the specified dim is involved in both an upper bound
10532 * and a lower bound.
10534 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10535 enum isl_dim_type type, unsigned pos)
10537 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10540 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10542 static isl_bool has_any_bound(__isl_keep isl_map *map,
10543 enum isl_dim_type type, unsigned pos,
10544 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10545 enum isl_dim_type type, unsigned pos))
10547 int i;
10549 if (!map)
10550 return isl_bool_error;
10552 for (i = 0; i < map->n; ++i) {
10553 isl_bool bounded;
10554 bounded = fn(map->p[i], type, pos);
10555 if (bounded < 0 || bounded)
10556 return bounded;
10559 return isl_bool_false;
10562 /* Return 1 if the specified dim is involved in any lower bound.
10564 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10565 enum isl_dim_type type, unsigned pos)
10567 return has_any_bound(set, type, pos,
10568 &isl_basic_map_dim_has_lower_bound);
10571 /* Return 1 if the specified dim is involved in any upper bound.
10573 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10574 enum isl_dim_type type, unsigned pos)
10576 return has_any_bound(set, type, pos,
10577 &isl_basic_map_dim_has_upper_bound);
10580 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10582 static isl_bool has_bound(__isl_keep isl_map *map,
10583 enum isl_dim_type type, unsigned pos,
10584 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10585 enum isl_dim_type type, unsigned pos))
10587 int i;
10589 if (!map)
10590 return isl_bool_error;
10592 for (i = 0; i < map->n; ++i) {
10593 isl_bool bounded;
10594 bounded = fn(map->p[i], type, pos);
10595 if (bounded < 0 || !bounded)
10596 return bounded;
10599 return isl_bool_true;
10602 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10604 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10605 enum isl_dim_type type, unsigned pos)
10607 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10610 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10612 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10613 enum isl_dim_type type, unsigned pos)
10615 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10618 /* For each of the "n" variables starting at "first", determine
10619 * the sign of the variable and put the results in the first "n"
10620 * elements of the array "signs".
10621 * Sign
10622 * 1 means that the variable is non-negative
10623 * -1 means that the variable is non-positive
10624 * 0 means the variable attains both positive and negative values.
10626 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10627 unsigned first, unsigned n, int *signs)
10629 isl_vec *bound = NULL;
10630 struct isl_tab *tab = NULL;
10631 struct isl_tab_undo *snap;
10632 int i;
10634 if (!bset || !signs)
10635 return isl_stat_error;
10637 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10638 tab = isl_tab_from_basic_set(bset, 0);
10639 if (!bound || !tab)
10640 goto error;
10642 isl_seq_clr(bound->el, bound->size);
10643 isl_int_set_si(bound->el[0], -1);
10645 snap = isl_tab_snap(tab);
10646 for (i = 0; i < n; ++i) {
10647 int empty;
10649 isl_int_set_si(bound->el[1 + first + i], -1);
10650 if (isl_tab_add_ineq(tab, bound->el) < 0)
10651 goto error;
10652 empty = tab->empty;
10653 isl_int_set_si(bound->el[1 + first + i], 0);
10654 if (isl_tab_rollback(tab, snap) < 0)
10655 goto error;
10657 if (empty) {
10658 signs[i] = 1;
10659 continue;
10662 isl_int_set_si(bound->el[1 + first + i], 1);
10663 if (isl_tab_add_ineq(tab, bound->el) < 0)
10664 goto error;
10665 empty = tab->empty;
10666 isl_int_set_si(bound->el[1 + first + i], 0);
10667 if (isl_tab_rollback(tab, snap) < 0)
10668 goto error;
10670 signs[i] = empty ? -1 : 0;
10673 isl_tab_free(tab);
10674 isl_vec_free(bound);
10675 return isl_stat_ok;
10676 error:
10677 isl_tab_free(tab);
10678 isl_vec_free(bound);
10679 return isl_stat_error;
10682 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10683 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10685 if (!bset || !signs)
10686 return isl_stat_error;
10687 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10688 return isl_stat_error);
10690 first += pos(bset->dim, type) - 1;
10691 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10694 /* Is it possible for the integer division "div" to depend (possibly
10695 * indirectly) on any output dimensions?
10697 * If the div is undefined, then we conservatively assume that it
10698 * may depend on them.
10699 * Otherwise, we check if it actually depends on them or on any integer
10700 * divisions that may depend on them.
10702 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10704 int i;
10705 unsigned n_out, o_out;
10706 unsigned n_div, o_div;
10708 if (isl_int_is_zero(bmap->div[div][0]))
10709 return isl_bool_true;
10711 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10712 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10714 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10715 return isl_bool_true;
10717 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10718 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10720 for (i = 0; i < n_div; ++i) {
10721 isl_bool may_involve;
10723 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10724 continue;
10725 may_involve = div_may_involve_output(bmap, i);
10726 if (may_involve < 0 || may_involve)
10727 return may_involve;
10730 return isl_bool_false;
10733 /* Return the first integer division of "bmap" in the range
10734 * [first, first + n[ that may depend on any output dimensions and
10735 * that has a non-zero coefficient in "c" (where the first coefficient
10736 * in "c" corresponds to integer division "first").
10738 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10739 isl_int *c, int first, int n)
10741 int k;
10743 if (!bmap)
10744 return -1;
10746 for (k = first; k < first + n; ++k) {
10747 isl_bool may_involve;
10749 if (isl_int_is_zero(c[k]))
10750 continue;
10751 may_involve = div_may_involve_output(bmap, k);
10752 if (may_involve < 0)
10753 return -1;
10754 if (may_involve)
10755 return k;
10758 return first + n;
10761 /* Look for a pair of inequality constraints in "bmap" of the form
10763 * -l + i >= 0 or i >= l
10764 * and
10765 * n + l - i >= 0 or i <= l + n
10767 * with n < "m" and i the output dimension at position "pos".
10768 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10769 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10770 * and earlier output dimensions, as well as integer divisions that do
10771 * not involve any of the output dimensions.
10773 * Return the index of the first inequality constraint or bmap->n_ineq
10774 * if no such pair can be found.
10776 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10777 int pos, isl_int m)
10779 int i, j;
10780 isl_ctx *ctx;
10781 unsigned total;
10782 unsigned n_div, o_div;
10783 unsigned n_out, o_out;
10784 int less;
10786 if (!bmap)
10787 return -1;
10789 ctx = isl_basic_map_get_ctx(bmap);
10790 total = isl_basic_map_total_dim(bmap);
10791 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10792 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10793 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10794 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10795 for (i = 0; i < bmap->n_ineq; ++i) {
10796 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10797 continue;
10798 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10799 n_out - (pos + 1)) != -1)
10800 continue;
10801 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10802 0, n_div) < n_div)
10803 continue;
10804 for (j = i + 1; j < bmap->n_ineq; ++j) {
10805 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10806 ctx->one))
10807 continue;
10808 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10809 bmap->ineq[j] + 1, total))
10810 continue;
10811 break;
10813 if (j >= bmap->n_ineq)
10814 continue;
10815 isl_int_add(bmap->ineq[i][0],
10816 bmap->ineq[i][0], bmap->ineq[j][0]);
10817 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10818 isl_int_sub(bmap->ineq[i][0],
10819 bmap->ineq[i][0], bmap->ineq[j][0]);
10820 if (!less)
10821 continue;
10822 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10823 return i;
10824 else
10825 return j;
10828 return bmap->n_ineq;
10831 /* Return the index of the equality of "bmap" that defines
10832 * the output dimension "pos" in terms of earlier dimensions.
10833 * The equality may also involve integer divisions, as long
10834 * as those integer divisions are defined in terms of
10835 * parameters or input dimensions.
10836 * In this case, *div is set to the number of integer divisions and
10837 * *ineq is set to the number of inequality constraints (provided
10838 * div and ineq are not NULL).
10840 * The equality may also involve a single integer division involving
10841 * the output dimensions (typically only output dimension "pos") as
10842 * long as the coefficient of output dimension "pos" is 1 or -1 and
10843 * there is a pair of constraints i >= l and i <= l + n, with i referring
10844 * to output dimension "pos", l an expression involving only earlier
10845 * dimensions and n smaller than the coefficient of the integer division
10846 * in the equality. In this case, the output dimension can be defined
10847 * in terms of a modulo expression that does not involve the integer division.
10848 * *div is then set to this single integer division and
10849 * *ineq is set to the index of constraint i >= l.
10851 * Return bmap->n_eq if there is no such equality.
10852 * Return -1 on error.
10854 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10855 int pos, int *div, int *ineq)
10857 int j, k, l;
10858 unsigned n_out, o_out;
10859 unsigned n_div, o_div;
10861 if (!bmap)
10862 return -1;
10864 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10865 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10866 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10867 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10869 if (ineq)
10870 *ineq = bmap->n_ineq;
10871 if (div)
10872 *div = n_div;
10873 for (j = 0; j < bmap->n_eq; ++j) {
10874 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10875 continue;
10876 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10877 n_out - (pos + 1)) != -1)
10878 continue;
10879 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10880 0, n_div);
10881 if (k >= n_div)
10882 return j;
10883 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10884 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10885 continue;
10886 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10887 k + 1, n_div - (k+1)) < n_div)
10888 continue;
10889 l = find_modulo_constraint_pair(bmap, pos,
10890 bmap->eq[j][o_div + k]);
10891 if (l < 0)
10892 return -1;
10893 if (l >= bmap->n_ineq)
10894 continue;
10895 if (div)
10896 *div = k;
10897 if (ineq)
10898 *ineq = l;
10899 return j;
10902 return bmap->n_eq;
10905 /* Check if the given basic map is obviously single-valued.
10906 * In particular, for each output dimension, check that there is
10907 * an equality that defines the output dimension in terms of
10908 * earlier dimensions.
10910 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10912 int i;
10913 unsigned n_out;
10915 if (!bmap)
10916 return isl_bool_error;
10918 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10920 for (i = 0; i < n_out; ++i) {
10921 int eq;
10923 eq = isl_basic_map_output_defining_equality(bmap, i,
10924 NULL, NULL);
10925 if (eq < 0)
10926 return isl_bool_error;
10927 if (eq >= bmap->n_eq)
10928 return isl_bool_false;
10931 return isl_bool_true;
10934 /* Check if the given basic map is single-valued.
10935 * We simply compute
10937 * M \circ M^-1
10939 * and check if the result is a subset of the identity mapping.
10941 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10943 isl_space *space;
10944 isl_basic_map *test;
10945 isl_basic_map *id;
10946 isl_bool sv;
10948 sv = isl_basic_map_plain_is_single_valued(bmap);
10949 if (sv < 0 || sv)
10950 return sv;
10952 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10953 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10955 space = isl_basic_map_get_space(bmap);
10956 space = isl_space_map_from_set(isl_space_range(space));
10957 id = isl_basic_map_identity(space);
10959 sv = isl_basic_map_is_subset(test, id);
10961 isl_basic_map_free(test);
10962 isl_basic_map_free(id);
10964 return sv;
10967 /* Check if the given map is obviously single-valued.
10969 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10971 if (!map)
10972 return isl_bool_error;
10973 if (map->n == 0)
10974 return isl_bool_true;
10975 if (map->n >= 2)
10976 return isl_bool_false;
10978 return isl_basic_map_plain_is_single_valued(map->p[0]);
10981 /* Check if the given map is single-valued.
10982 * We simply compute
10984 * M \circ M^-1
10986 * and check if the result is a subset of the identity mapping.
10988 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10990 isl_space *dim;
10991 isl_map *test;
10992 isl_map *id;
10993 isl_bool sv;
10995 sv = isl_map_plain_is_single_valued(map);
10996 if (sv < 0 || sv)
10997 return sv;
10999 test = isl_map_reverse(isl_map_copy(map));
11000 test = isl_map_apply_range(test, isl_map_copy(map));
11002 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11003 id = isl_map_identity(dim);
11005 sv = isl_map_is_subset(test, id);
11007 isl_map_free(test);
11008 isl_map_free(id);
11010 return sv;
11013 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11015 isl_bool in;
11017 map = isl_map_copy(map);
11018 map = isl_map_reverse(map);
11019 in = isl_map_is_single_valued(map);
11020 isl_map_free(map);
11022 return in;
11025 /* Check if the given map is obviously injective.
11027 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11029 isl_bool in;
11031 map = isl_map_copy(map);
11032 map = isl_map_reverse(map);
11033 in = isl_map_plain_is_single_valued(map);
11034 isl_map_free(map);
11036 return in;
11039 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11041 isl_bool sv;
11043 sv = isl_map_is_single_valued(map);
11044 if (sv < 0 || !sv)
11045 return sv;
11047 return isl_map_is_injective(map);
11050 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11052 return isl_map_is_single_valued(set_to_map(set));
11055 /* Does "map" only map elements to themselves?
11057 * If the domain and range spaces are different, then "map"
11058 * is considered not to be an identity relation, even if it is empty.
11059 * Otherwise, construct the maximal identity relation and
11060 * check whether "map" is a subset of this relation.
11062 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11064 isl_space *space;
11065 isl_map *id;
11066 isl_bool equal, is_identity;
11068 space = isl_map_get_space(map);
11069 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11070 isl_space_free(space);
11071 if (equal < 0 || !equal)
11072 return equal;
11074 id = isl_map_identity(isl_map_get_space(map));
11075 is_identity = isl_map_is_subset(map, id);
11076 isl_map_free(id);
11078 return is_identity;
11081 int isl_map_is_translation(__isl_keep isl_map *map)
11083 int ok;
11084 isl_set *delta;
11086 delta = isl_map_deltas(isl_map_copy(map));
11087 ok = isl_set_is_singleton(delta);
11088 isl_set_free(delta);
11090 return ok;
11093 static int unique(isl_int *p, unsigned pos, unsigned len)
11095 if (isl_seq_first_non_zero(p, pos) != -1)
11096 return 0;
11097 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11098 return 0;
11099 return 1;
11102 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11104 int i, j;
11105 unsigned nvar;
11106 unsigned ovar;
11108 if (!bset)
11109 return isl_bool_error;
11111 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11112 return isl_bool_false;
11114 nvar = isl_basic_set_dim(bset, isl_dim_set);
11115 ovar = isl_space_offset(bset->dim, isl_dim_set);
11116 for (j = 0; j < nvar; ++j) {
11117 int lower = 0, upper = 0;
11118 for (i = 0; i < bset->n_eq; ++i) {
11119 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11120 continue;
11121 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11122 return isl_bool_false;
11123 break;
11125 if (i < bset->n_eq)
11126 continue;
11127 for (i = 0; i < bset->n_ineq; ++i) {
11128 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11129 continue;
11130 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11131 return isl_bool_false;
11132 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11133 lower = 1;
11134 else
11135 upper = 1;
11137 if (!lower || !upper)
11138 return isl_bool_false;
11141 return isl_bool_true;
11144 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11146 if (!set)
11147 return isl_bool_error;
11148 if (set->n != 1)
11149 return isl_bool_false;
11151 return isl_basic_set_is_box(set->p[0]);
11154 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11156 if (!bset)
11157 return isl_bool_error;
11159 return isl_space_is_wrapping(bset->dim);
11162 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11164 if (!set)
11165 return isl_bool_error;
11167 return isl_space_is_wrapping(set->dim);
11170 /* Modify the space of "map" through a call to "change".
11171 * If "can_change" is set (not NULL), then first call it to check
11172 * if the modification is allowed, printing the error message "cannot_change"
11173 * if it is not.
11175 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11176 isl_bool (*can_change)(__isl_keep isl_map *map),
11177 const char *cannot_change,
11178 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11180 isl_bool ok;
11181 isl_space *space;
11183 if (!map)
11184 return NULL;
11186 ok = can_change ? can_change(map) : isl_bool_true;
11187 if (ok < 0)
11188 return isl_map_free(map);
11189 if (!ok)
11190 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11191 return isl_map_free(map));
11193 space = change(isl_map_get_space(map));
11194 map = isl_map_reset_space(map, space);
11196 return map;
11199 /* Is the domain of "map" a wrapped relation?
11201 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11203 if (!map)
11204 return isl_bool_error;
11206 return isl_space_domain_is_wrapping(map->dim);
11209 /* Is the range of "map" a wrapped relation?
11211 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11213 if (!map)
11214 return isl_bool_error;
11216 return isl_space_range_is_wrapping(map->dim);
11219 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11221 bmap = isl_basic_map_cow(bmap);
11222 if (!bmap)
11223 return NULL;
11225 bmap->dim = isl_space_wrap(bmap->dim);
11226 if (!bmap->dim)
11227 goto error;
11229 bmap = isl_basic_map_finalize(bmap);
11231 return bset_from_bmap(bmap);
11232 error:
11233 isl_basic_map_free(bmap);
11234 return NULL;
11237 /* Given a map A -> B, return the set (A -> B).
11239 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11241 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11244 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11246 bset = isl_basic_set_cow(bset);
11247 if (!bset)
11248 return NULL;
11250 bset->dim = isl_space_unwrap(bset->dim);
11251 if (!bset->dim)
11252 goto error;
11254 bset = isl_basic_set_finalize(bset);
11256 return bset_to_bmap(bset);
11257 error:
11258 isl_basic_set_free(bset);
11259 return NULL;
11262 /* Given a set (A -> B), return the map A -> B.
11263 * Error out if "set" is not of the form (A -> B).
11265 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11267 return isl_map_change_space(set, &isl_set_is_wrapping,
11268 "not a wrapping set", &isl_space_unwrap);
11271 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11272 enum isl_dim_type type)
11274 if (!bmap)
11275 return NULL;
11277 if (!isl_space_is_named_or_nested(bmap->dim, type))
11278 return bmap;
11280 bmap = isl_basic_map_cow(bmap);
11281 if (!bmap)
11282 return NULL;
11284 bmap->dim = isl_space_reset(bmap->dim, type);
11285 if (!bmap->dim)
11286 goto error;
11288 bmap = isl_basic_map_finalize(bmap);
11290 return bmap;
11291 error:
11292 isl_basic_map_free(bmap);
11293 return NULL;
11296 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11297 enum isl_dim_type type)
11299 int i;
11301 if (!map)
11302 return NULL;
11304 if (!isl_space_is_named_or_nested(map->dim, type))
11305 return map;
11307 map = isl_map_cow(map);
11308 if (!map)
11309 return NULL;
11311 for (i = 0; i < map->n; ++i) {
11312 map->p[i] = isl_basic_map_reset(map->p[i], type);
11313 if (!map->p[i])
11314 goto error;
11316 map->dim = isl_space_reset(map->dim, type);
11317 if (!map->dim)
11318 goto error;
11320 return map;
11321 error:
11322 isl_map_free(map);
11323 return NULL;
11326 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11328 if (!bmap)
11329 return NULL;
11331 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11332 return bmap;
11334 bmap = isl_basic_map_cow(bmap);
11335 if (!bmap)
11336 return NULL;
11338 bmap->dim = isl_space_flatten(bmap->dim);
11339 if (!bmap->dim)
11340 goto error;
11342 bmap = isl_basic_map_finalize(bmap);
11344 return bmap;
11345 error:
11346 isl_basic_map_free(bmap);
11347 return NULL;
11350 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11352 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11355 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11356 __isl_take isl_basic_map *bmap)
11358 if (!bmap)
11359 return NULL;
11361 if (!bmap->dim->nested[0])
11362 return bmap;
11364 bmap = isl_basic_map_cow(bmap);
11365 if (!bmap)
11366 return NULL;
11368 bmap->dim = isl_space_flatten_domain(bmap->dim);
11369 if (!bmap->dim)
11370 goto error;
11372 bmap = isl_basic_map_finalize(bmap);
11374 return bmap;
11375 error:
11376 isl_basic_map_free(bmap);
11377 return NULL;
11380 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11381 __isl_take isl_basic_map *bmap)
11383 if (!bmap)
11384 return NULL;
11386 if (!bmap->dim->nested[1])
11387 return bmap;
11389 bmap = isl_basic_map_cow(bmap);
11390 if (!bmap)
11391 return NULL;
11393 bmap->dim = isl_space_flatten_range(bmap->dim);
11394 if (!bmap->dim)
11395 goto error;
11397 bmap = isl_basic_map_finalize(bmap);
11399 return bmap;
11400 error:
11401 isl_basic_map_free(bmap);
11402 return NULL;
11405 /* Remove any internal structure from the spaces of domain and range of "map".
11407 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11409 if (!map)
11410 return NULL;
11412 if (!map->dim->nested[0] && !map->dim->nested[1])
11413 return map;
11415 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11418 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11420 return set_from_map(isl_map_flatten(set_to_map(set)));
11423 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11425 isl_space *dim, *flat_dim;
11426 isl_map *map;
11428 dim = isl_set_get_space(set);
11429 flat_dim = isl_space_flatten(isl_space_copy(dim));
11430 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11431 map = isl_map_intersect_domain(map, set);
11433 return map;
11436 /* Remove any internal structure from the space of the domain of "map".
11438 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11440 if (!map)
11441 return NULL;
11443 if (!map->dim->nested[0])
11444 return map;
11446 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11449 /* Remove any internal structure from the space of the range of "map".
11451 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11453 if (!map)
11454 return NULL;
11456 if (!map->dim->nested[1])
11457 return map;
11459 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11462 /* Reorder the dimensions of "bmap" according to the given dim_map
11463 * and set the dimension specification to "dim" and
11464 * perform Gaussian elimination on the result.
11466 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11467 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11469 isl_basic_map *res;
11470 unsigned flags;
11472 bmap = isl_basic_map_cow(bmap);
11473 if (!bmap || !dim || !dim_map)
11474 goto error;
11476 flags = bmap->flags;
11477 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11478 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11479 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11480 res = isl_basic_map_alloc_space(dim,
11481 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11482 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11483 if (res)
11484 res->flags = flags;
11485 res = isl_basic_map_gauss(res, NULL);
11486 res = isl_basic_map_finalize(res);
11487 return res;
11488 error:
11489 free(dim_map);
11490 isl_basic_map_free(bmap);
11491 isl_space_free(dim);
11492 return NULL;
11495 /* Reorder the dimensions of "map" according to given reordering.
11497 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11498 __isl_take isl_reordering *r)
11500 int i;
11501 struct isl_dim_map *dim_map;
11503 map = isl_map_cow(map);
11504 dim_map = isl_dim_map_from_reordering(r);
11505 if (!map || !r || !dim_map)
11506 goto error;
11508 for (i = 0; i < map->n; ++i) {
11509 struct isl_dim_map *dim_map_i;
11511 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11513 map->p[i] = isl_basic_map_realign(map->p[i],
11514 isl_space_copy(r->dim), dim_map_i);
11516 if (!map->p[i])
11517 goto error;
11520 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11522 isl_reordering_free(r);
11523 free(dim_map);
11524 return map;
11525 error:
11526 free(dim_map);
11527 isl_map_free(map);
11528 isl_reordering_free(r);
11529 return NULL;
11532 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11533 __isl_take isl_reordering *r)
11535 return set_from_map(isl_map_realign(set_to_map(set), r));
11538 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11539 __isl_take isl_space *model)
11541 isl_ctx *ctx;
11542 isl_bool aligned;
11544 if (!map || !model)
11545 goto error;
11547 ctx = isl_space_get_ctx(model);
11548 if (!isl_space_has_named_params(model))
11549 isl_die(ctx, isl_error_invalid,
11550 "model has unnamed parameters", goto error);
11551 if (isl_map_check_named_params(map) < 0)
11552 goto error;
11553 aligned = isl_map_space_has_equal_params(map, model);
11554 if (aligned < 0)
11555 goto error;
11556 if (!aligned) {
11557 isl_reordering *exp;
11559 model = isl_space_drop_dims(model, isl_dim_in,
11560 0, isl_space_dim(model, isl_dim_in));
11561 model = isl_space_drop_dims(model, isl_dim_out,
11562 0, isl_space_dim(model, isl_dim_out));
11563 exp = isl_parameter_alignment_reordering(map->dim, model);
11564 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11565 map = isl_map_realign(map, exp);
11568 isl_space_free(model);
11569 return map;
11570 error:
11571 isl_space_free(model);
11572 isl_map_free(map);
11573 return NULL;
11576 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11577 __isl_take isl_space *model)
11579 return isl_map_align_params(set, model);
11582 /* Align the parameters of "bmap" to those of "model", introducing
11583 * additional parameters if needed.
11585 __isl_give isl_basic_map *isl_basic_map_align_params(
11586 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11588 isl_ctx *ctx;
11589 isl_bool equal_params;
11591 if (!bmap || !model)
11592 goto error;
11594 ctx = isl_space_get_ctx(model);
11595 if (!isl_space_has_named_params(model))
11596 isl_die(ctx, isl_error_invalid,
11597 "model has unnamed parameters", goto error);
11598 if (!isl_space_has_named_params(bmap->dim))
11599 isl_die(ctx, isl_error_invalid,
11600 "relation has unnamed parameters", goto error);
11601 equal_params = isl_space_has_equal_params(bmap->dim, model);
11602 if (equal_params < 0)
11603 goto error;
11604 if (!equal_params) {
11605 isl_reordering *exp;
11606 struct isl_dim_map *dim_map;
11608 model = isl_space_drop_dims(model, isl_dim_in,
11609 0, isl_space_dim(model, isl_dim_in));
11610 model = isl_space_drop_dims(model, isl_dim_out,
11611 0, isl_space_dim(model, isl_dim_out));
11612 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11613 exp = isl_reordering_extend_space(exp,
11614 isl_basic_map_get_space(bmap));
11615 dim_map = isl_dim_map_from_reordering(exp);
11616 bmap = isl_basic_map_realign(bmap,
11617 exp ? isl_space_copy(exp->dim) : NULL,
11618 isl_dim_map_extend(dim_map, bmap));
11619 isl_reordering_free(exp);
11620 free(dim_map);
11623 isl_space_free(model);
11624 return bmap;
11625 error:
11626 isl_space_free(model);
11627 isl_basic_map_free(bmap);
11628 return NULL;
11631 /* Do "bset" and "space" have the same parameters?
11633 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11634 __isl_keep isl_space *space)
11636 isl_space *bset_space;
11638 bset_space = isl_basic_set_peek_space(bset);
11639 return isl_space_has_equal_params(bset_space, space);
11642 /* Do "map" and "space" have the same parameters?
11644 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11645 __isl_keep isl_space *space)
11647 isl_space *map_space;
11649 map_space = isl_map_peek_space(map);
11650 return isl_space_has_equal_params(map_space, space);
11653 /* Do "set" and "space" have the same parameters?
11655 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11656 __isl_keep isl_space *space)
11658 return isl_map_space_has_equal_params(set_to_map(set), space);
11661 /* Align the parameters of "bset" to those of "model", introducing
11662 * additional parameters if needed.
11664 __isl_give isl_basic_set *isl_basic_set_align_params(
11665 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11667 return isl_basic_map_align_params(bset, model);
11670 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11671 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11672 enum isl_dim_type c2, enum isl_dim_type c3,
11673 enum isl_dim_type c4, enum isl_dim_type c5)
11675 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11676 struct isl_mat *mat;
11677 int i, j, k;
11678 int pos;
11680 if (!bmap)
11681 return NULL;
11682 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11683 isl_basic_map_total_dim(bmap) + 1);
11684 if (!mat)
11685 return NULL;
11686 for (i = 0; i < bmap->n_eq; ++i)
11687 for (j = 0, pos = 0; j < 5; ++j) {
11688 int off = isl_basic_map_offset(bmap, c[j]);
11689 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11690 isl_int_set(mat->row[i][pos],
11691 bmap->eq[i][off + k]);
11692 ++pos;
11696 return mat;
11699 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11700 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11701 enum isl_dim_type c2, enum isl_dim_type c3,
11702 enum isl_dim_type c4, enum isl_dim_type c5)
11704 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11705 struct isl_mat *mat;
11706 int i, j, k;
11707 int pos;
11709 if (!bmap)
11710 return NULL;
11711 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11712 isl_basic_map_total_dim(bmap) + 1);
11713 if (!mat)
11714 return NULL;
11715 for (i = 0; i < bmap->n_ineq; ++i)
11716 for (j = 0, pos = 0; j < 5; ++j) {
11717 int off = isl_basic_map_offset(bmap, c[j]);
11718 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11719 isl_int_set(mat->row[i][pos],
11720 bmap->ineq[i][off + k]);
11721 ++pos;
11725 return mat;
11728 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11729 __isl_take isl_space *dim,
11730 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11731 enum isl_dim_type c2, enum isl_dim_type c3,
11732 enum isl_dim_type c4, enum isl_dim_type c5)
11734 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11735 isl_basic_map *bmap;
11736 unsigned total;
11737 unsigned extra;
11738 int i, j, k, l;
11739 int pos;
11741 if (!dim || !eq || !ineq)
11742 goto error;
11744 if (eq->n_col != ineq->n_col)
11745 isl_die(dim->ctx, isl_error_invalid,
11746 "equalities and inequalities matrices should have "
11747 "same number of columns", goto error);
11749 total = 1 + isl_space_dim(dim, isl_dim_all);
11751 if (eq->n_col < total)
11752 isl_die(dim->ctx, isl_error_invalid,
11753 "number of columns too small", goto error);
11755 extra = eq->n_col - total;
11757 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11758 eq->n_row, ineq->n_row);
11759 if (!bmap)
11760 goto error;
11761 for (i = 0; i < extra; ++i) {
11762 k = isl_basic_map_alloc_div(bmap);
11763 if (k < 0)
11764 goto error;
11765 isl_int_set_si(bmap->div[k][0], 0);
11767 for (i = 0; i < eq->n_row; ++i) {
11768 l = isl_basic_map_alloc_equality(bmap);
11769 if (l < 0)
11770 goto error;
11771 for (j = 0, pos = 0; j < 5; ++j) {
11772 int off = isl_basic_map_offset(bmap, c[j]);
11773 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11774 isl_int_set(bmap->eq[l][off + k],
11775 eq->row[i][pos]);
11776 ++pos;
11780 for (i = 0; i < ineq->n_row; ++i) {
11781 l = isl_basic_map_alloc_inequality(bmap);
11782 if (l < 0)
11783 goto error;
11784 for (j = 0, pos = 0; j < 5; ++j) {
11785 int off = isl_basic_map_offset(bmap, c[j]);
11786 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11787 isl_int_set(bmap->ineq[l][off + k],
11788 ineq->row[i][pos]);
11789 ++pos;
11794 isl_space_free(dim);
11795 isl_mat_free(eq);
11796 isl_mat_free(ineq);
11798 bmap = isl_basic_map_simplify(bmap);
11799 return isl_basic_map_finalize(bmap);
11800 error:
11801 isl_space_free(dim);
11802 isl_mat_free(eq);
11803 isl_mat_free(ineq);
11804 return NULL;
11807 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11808 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11809 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11811 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11812 c1, c2, c3, c4, isl_dim_in);
11815 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11816 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11817 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11819 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11820 c1, c2, c3, c4, isl_dim_in);
11823 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11824 __isl_take isl_space *dim,
11825 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11826 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11828 isl_basic_map *bmap;
11829 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11830 c1, c2, c3, c4, isl_dim_in);
11831 return bset_from_bmap(bmap);
11834 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11836 if (!bmap)
11837 return isl_bool_error;
11839 return isl_space_can_zip(bmap->dim);
11842 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11844 if (!map)
11845 return isl_bool_error;
11847 return isl_space_can_zip(map->dim);
11850 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11851 * (A -> C) -> (B -> D).
11853 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11855 unsigned pos;
11856 unsigned n1;
11857 unsigned n2;
11859 if (!bmap)
11860 return NULL;
11862 if (!isl_basic_map_can_zip(bmap))
11863 isl_die(bmap->ctx, isl_error_invalid,
11864 "basic map cannot be zipped", goto error);
11865 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11866 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11867 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11868 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11869 bmap = isl_basic_map_cow(bmap);
11870 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11871 if (!bmap)
11872 return NULL;
11873 bmap->dim = isl_space_zip(bmap->dim);
11874 if (!bmap->dim)
11875 goto error;
11876 bmap = isl_basic_map_mark_final(bmap);
11877 return bmap;
11878 error:
11879 isl_basic_map_free(bmap);
11880 return NULL;
11883 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11884 * (A -> C) -> (B -> D).
11886 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11888 int i;
11890 if (!map)
11891 return NULL;
11893 if (!isl_map_can_zip(map))
11894 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11895 goto error);
11897 map = isl_map_cow(map);
11898 if (!map)
11899 return NULL;
11901 for (i = 0; i < map->n; ++i) {
11902 map->p[i] = isl_basic_map_zip(map->p[i]);
11903 if (!map->p[i])
11904 goto error;
11907 map->dim = isl_space_zip(map->dim);
11908 if (!map->dim)
11909 goto error;
11911 return map;
11912 error:
11913 isl_map_free(map);
11914 return NULL;
11917 /* Can we apply isl_basic_map_curry to "bmap"?
11918 * That is, does it have a nested relation in its domain?
11920 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11922 if (!bmap)
11923 return isl_bool_error;
11925 return isl_space_can_curry(bmap->dim);
11928 /* Can we apply isl_map_curry to "map"?
11929 * That is, does it have a nested relation in its domain?
11931 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11933 if (!map)
11934 return isl_bool_error;
11936 return isl_space_can_curry(map->dim);
11939 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11940 * A -> (B -> C).
11942 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11945 if (!bmap)
11946 return NULL;
11948 if (!isl_basic_map_can_curry(bmap))
11949 isl_die(bmap->ctx, isl_error_invalid,
11950 "basic map cannot be curried", goto error);
11951 bmap = isl_basic_map_cow(bmap);
11952 if (!bmap)
11953 return NULL;
11954 bmap->dim = isl_space_curry(bmap->dim);
11955 if (!bmap->dim)
11956 goto error;
11957 bmap = isl_basic_map_mark_final(bmap);
11958 return bmap;
11959 error:
11960 isl_basic_map_free(bmap);
11961 return NULL;
11964 /* Given a map (A -> B) -> C, return the corresponding map
11965 * A -> (B -> C).
11967 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11969 return isl_map_change_space(map, &isl_map_can_curry,
11970 "map cannot be curried", &isl_space_curry);
11973 /* Can isl_map_range_curry be applied to "map"?
11974 * That is, does it have a nested relation in its range,
11975 * the domain of which is itself a nested relation?
11977 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11979 if (!map)
11980 return isl_bool_error;
11982 return isl_space_can_range_curry(map->dim);
11985 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11986 * A -> (B -> (C -> D)).
11988 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11990 return isl_map_change_space(map, &isl_map_can_range_curry,
11991 "map range cannot be curried",
11992 &isl_space_range_curry);
11995 /* Can we apply isl_basic_map_uncurry to "bmap"?
11996 * That is, does it have a nested relation in its domain?
11998 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12000 if (!bmap)
12001 return isl_bool_error;
12003 return isl_space_can_uncurry(bmap->dim);
12006 /* Can we apply isl_map_uncurry to "map"?
12007 * That is, does it have a nested relation in its domain?
12009 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12011 if (!map)
12012 return isl_bool_error;
12014 return isl_space_can_uncurry(map->dim);
12017 /* Given a basic map A -> (B -> C), return the corresponding basic map
12018 * (A -> B) -> C.
12020 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12023 if (!bmap)
12024 return NULL;
12026 if (!isl_basic_map_can_uncurry(bmap))
12027 isl_die(bmap->ctx, isl_error_invalid,
12028 "basic map cannot be uncurried",
12029 return isl_basic_map_free(bmap));
12030 bmap = isl_basic_map_cow(bmap);
12031 if (!bmap)
12032 return NULL;
12033 bmap->dim = isl_space_uncurry(bmap->dim);
12034 if (!bmap->dim)
12035 return isl_basic_map_free(bmap);
12036 bmap = isl_basic_map_mark_final(bmap);
12037 return bmap;
12040 /* Given a map A -> (B -> C), return the corresponding map
12041 * (A -> B) -> C.
12043 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12045 return isl_map_change_space(map, &isl_map_can_uncurry,
12046 "map cannot be uncurried", &isl_space_uncurry);
12049 /* Construct a basic map mapping the domain of the affine expression
12050 * to a one-dimensional range prescribed by the affine expression.
12051 * If "rational" is set, then construct a rational basic map.
12053 * A NaN affine expression cannot be converted to a basic map.
12055 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12056 __isl_take isl_aff *aff, int rational)
12058 int k;
12059 int pos;
12060 isl_bool is_nan;
12061 isl_local_space *ls;
12062 isl_basic_map *bmap = NULL;
12064 if (!aff)
12065 return NULL;
12066 is_nan = isl_aff_is_nan(aff);
12067 if (is_nan < 0)
12068 goto error;
12069 if (is_nan)
12070 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12071 "cannot convert NaN", goto error);
12073 ls = isl_aff_get_local_space(aff);
12074 bmap = isl_basic_map_from_local_space(ls);
12075 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12076 k = isl_basic_map_alloc_equality(bmap);
12077 if (k < 0)
12078 goto error;
12080 pos = isl_basic_map_offset(bmap, isl_dim_out);
12081 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12082 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12083 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12084 aff->v->size - (pos + 1));
12086 isl_aff_free(aff);
12087 if (rational)
12088 bmap = isl_basic_map_set_rational(bmap);
12089 bmap = isl_basic_map_gauss(bmap, NULL);
12090 bmap = isl_basic_map_finalize(bmap);
12091 return bmap;
12092 error:
12093 isl_aff_free(aff);
12094 isl_basic_map_free(bmap);
12095 return NULL;
12098 /* Construct a basic map mapping the domain of the affine expression
12099 * to a one-dimensional range prescribed by the affine expression.
12101 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12103 return isl_basic_map_from_aff2(aff, 0);
12106 /* Construct a map mapping the domain of the affine expression
12107 * to a one-dimensional range prescribed by the affine expression.
12109 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12111 isl_basic_map *bmap;
12113 bmap = isl_basic_map_from_aff(aff);
12114 return isl_map_from_basic_map(bmap);
12117 /* Construct a basic map mapping the domain the multi-affine expression
12118 * to its range, with each dimension in the range equated to the
12119 * corresponding affine expression.
12120 * If "rational" is set, then construct a rational basic map.
12122 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12123 __isl_take isl_multi_aff *maff, int rational)
12125 int i;
12126 isl_space *space;
12127 isl_basic_map *bmap;
12129 if (!maff)
12130 return NULL;
12132 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12133 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12134 "invalid space", goto error);
12136 space = isl_space_domain(isl_multi_aff_get_space(maff));
12137 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12138 if (rational)
12139 bmap = isl_basic_map_set_rational(bmap);
12141 for (i = 0; i < maff->n; ++i) {
12142 isl_aff *aff;
12143 isl_basic_map *bmap_i;
12145 aff = isl_aff_copy(maff->p[i]);
12146 bmap_i = isl_basic_map_from_aff2(aff, rational);
12148 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12151 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12153 isl_multi_aff_free(maff);
12154 return bmap;
12155 error:
12156 isl_multi_aff_free(maff);
12157 return NULL;
12160 /* Construct a basic map mapping the domain the multi-affine expression
12161 * to its range, with each dimension in the range equated to the
12162 * corresponding affine expression.
12164 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12165 __isl_take isl_multi_aff *ma)
12167 return isl_basic_map_from_multi_aff2(ma, 0);
12170 /* Construct a map mapping the domain the multi-affine expression
12171 * to its range, with each dimension in the range equated to the
12172 * corresponding affine expression.
12174 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12176 isl_basic_map *bmap;
12178 bmap = isl_basic_map_from_multi_aff(maff);
12179 return isl_map_from_basic_map(bmap);
12182 /* Construct a basic map mapping a domain in the given space to
12183 * to an n-dimensional range, with n the number of elements in the list,
12184 * where each coordinate in the range is prescribed by the
12185 * corresponding affine expression.
12186 * The domains of all affine expressions in the list are assumed to match
12187 * domain_dim.
12189 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12190 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12192 int i;
12193 isl_space *dim;
12194 isl_basic_map *bmap;
12196 if (!list)
12197 return NULL;
12199 dim = isl_space_from_domain(domain_dim);
12200 bmap = isl_basic_map_universe(dim);
12202 for (i = 0; i < list->n; ++i) {
12203 isl_aff *aff;
12204 isl_basic_map *bmap_i;
12206 aff = isl_aff_copy(list->p[i]);
12207 bmap_i = isl_basic_map_from_aff(aff);
12209 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12212 isl_aff_list_free(list);
12213 return bmap;
12216 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12217 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12219 return isl_map_equate(set, type1, pos1, type2, pos2);
12222 /* Construct a basic map where the given dimensions are equal to each other.
12224 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12225 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12227 isl_basic_map *bmap = NULL;
12228 int i;
12230 if (!space)
12231 return NULL;
12233 if (pos1 >= isl_space_dim(space, type1))
12234 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12235 "index out of bounds", goto error);
12236 if (pos2 >= isl_space_dim(space, type2))
12237 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12238 "index out of bounds", goto error);
12240 if (type1 == type2 && pos1 == pos2)
12241 return isl_basic_map_universe(space);
12243 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12244 i = isl_basic_map_alloc_equality(bmap);
12245 if (i < 0)
12246 goto error;
12247 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12248 pos1 += isl_basic_map_offset(bmap, type1);
12249 pos2 += isl_basic_map_offset(bmap, type2);
12250 isl_int_set_si(bmap->eq[i][pos1], -1);
12251 isl_int_set_si(bmap->eq[i][pos2], 1);
12252 bmap = isl_basic_map_finalize(bmap);
12253 isl_space_free(space);
12254 return bmap;
12255 error:
12256 isl_space_free(space);
12257 isl_basic_map_free(bmap);
12258 return NULL;
12261 /* Add a constraint imposing that the given two dimensions are equal.
12263 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12264 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12266 isl_basic_map *eq;
12268 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12270 bmap = isl_basic_map_intersect(bmap, eq);
12272 return bmap;
12275 /* Add a constraint imposing that the given two dimensions are equal.
12277 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12278 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12280 isl_basic_map *bmap;
12282 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12284 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12286 return map;
12289 /* Add a constraint imposing that the given two dimensions have opposite values.
12291 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12292 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12294 isl_basic_map *bmap = NULL;
12295 int i;
12297 if (!map)
12298 return NULL;
12300 if (pos1 >= isl_map_dim(map, type1))
12301 isl_die(map->ctx, isl_error_invalid,
12302 "index out of bounds", goto error);
12303 if (pos2 >= isl_map_dim(map, type2))
12304 isl_die(map->ctx, isl_error_invalid,
12305 "index out of bounds", goto error);
12307 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12308 i = isl_basic_map_alloc_equality(bmap);
12309 if (i < 0)
12310 goto error;
12311 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12312 pos1 += isl_basic_map_offset(bmap, type1);
12313 pos2 += isl_basic_map_offset(bmap, type2);
12314 isl_int_set_si(bmap->eq[i][pos1], 1);
12315 isl_int_set_si(bmap->eq[i][pos2], 1);
12316 bmap = isl_basic_map_finalize(bmap);
12318 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12320 return map;
12321 error:
12322 isl_basic_map_free(bmap);
12323 isl_map_free(map);
12324 return NULL;
12327 /* Construct a constraint imposing that the value of the first dimension is
12328 * greater than or equal to that of the second.
12330 static __isl_give isl_constraint *constraint_order_ge(
12331 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12332 enum isl_dim_type type2, int pos2)
12334 isl_constraint *c;
12336 if (!space)
12337 return NULL;
12339 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12341 if (pos1 >= isl_constraint_dim(c, type1))
12342 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12343 "index out of bounds", return isl_constraint_free(c));
12344 if (pos2 >= isl_constraint_dim(c, type2))
12345 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12346 "index out of bounds", return isl_constraint_free(c));
12348 if (type1 == type2 && pos1 == pos2)
12349 return c;
12351 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12352 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12354 return c;
12357 /* Add a constraint imposing that the value of the first dimension is
12358 * greater than or equal to that of the second.
12360 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12361 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12363 isl_constraint *c;
12364 isl_space *space;
12366 if (type1 == type2 && pos1 == pos2)
12367 return bmap;
12368 space = isl_basic_map_get_space(bmap);
12369 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12370 bmap = isl_basic_map_add_constraint(bmap, c);
12372 return bmap;
12375 /* Add a constraint imposing that the value of the first dimension is
12376 * greater than or equal to that of the second.
12378 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12379 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12381 isl_constraint *c;
12382 isl_space *space;
12384 if (type1 == type2 && pos1 == pos2)
12385 return map;
12386 space = isl_map_get_space(map);
12387 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12388 map = isl_map_add_constraint(map, c);
12390 return map;
12393 /* Add a constraint imposing that the value of the first dimension is
12394 * less than or equal to that of the second.
12396 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12397 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12399 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12402 /* Construct a basic map where the value of the first dimension is
12403 * greater than that of the second.
12405 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12406 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12408 isl_basic_map *bmap = NULL;
12409 int i;
12411 if (!space)
12412 return NULL;
12414 if (pos1 >= isl_space_dim(space, type1))
12415 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12416 "index out of bounds", goto error);
12417 if (pos2 >= isl_space_dim(space, type2))
12418 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12419 "index out of bounds", goto error);
12421 if (type1 == type2 && pos1 == pos2)
12422 return isl_basic_map_empty(space);
12424 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12425 i = isl_basic_map_alloc_inequality(bmap);
12426 if (i < 0)
12427 return isl_basic_map_free(bmap);
12428 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12429 pos1 += isl_basic_map_offset(bmap, type1);
12430 pos2 += isl_basic_map_offset(bmap, type2);
12431 isl_int_set_si(bmap->ineq[i][pos1], 1);
12432 isl_int_set_si(bmap->ineq[i][pos2], -1);
12433 isl_int_set_si(bmap->ineq[i][0], -1);
12434 bmap = isl_basic_map_finalize(bmap);
12436 return bmap;
12437 error:
12438 isl_space_free(space);
12439 isl_basic_map_free(bmap);
12440 return NULL;
12443 /* Add a constraint imposing that the value of the first dimension is
12444 * greater than that of the second.
12446 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12447 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12449 isl_basic_map *gt;
12451 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12453 bmap = isl_basic_map_intersect(bmap, gt);
12455 return bmap;
12458 /* Add a constraint imposing that the value of the first dimension is
12459 * greater than that of the second.
12461 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12462 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12464 isl_basic_map *bmap;
12466 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12468 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12470 return map;
12473 /* Add a constraint imposing that the value of the first dimension is
12474 * smaller than that of the second.
12476 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12477 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12479 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12482 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12483 int pos)
12485 isl_aff *div;
12486 isl_local_space *ls;
12488 if (!bmap)
12489 return NULL;
12491 if (!isl_basic_map_divs_known(bmap))
12492 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12493 "some divs are unknown", return NULL);
12495 ls = isl_basic_map_get_local_space(bmap);
12496 div = isl_local_space_get_div(ls, pos);
12497 isl_local_space_free(ls);
12499 return div;
12502 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12503 int pos)
12505 return isl_basic_map_get_div(bset, pos);
12508 /* Plug in "subs" for dimension "type", "pos" of "bset".
12510 * Let i be the dimension to replace and let "subs" be of the form
12512 * f/d
12514 * Any integer division with a non-zero coefficient for i,
12516 * floor((a i + g)/m)
12518 * is replaced by
12520 * floor((a f + d g)/(m d))
12522 * Constraints of the form
12524 * a i + g
12526 * are replaced by
12528 * a f + d g
12530 * We currently require that "subs" is an integral expression.
12531 * Handling rational expressions may require us to add stride constraints
12532 * as we do in isl_basic_set_preimage_multi_aff.
12534 __isl_give isl_basic_set *isl_basic_set_substitute(
12535 __isl_take isl_basic_set *bset,
12536 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12538 int i;
12539 isl_int v;
12540 isl_ctx *ctx;
12542 if (bset && isl_basic_set_plain_is_empty(bset))
12543 return bset;
12545 bset = isl_basic_set_cow(bset);
12546 if (!bset || !subs)
12547 goto error;
12549 ctx = isl_basic_set_get_ctx(bset);
12550 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12551 isl_die(ctx, isl_error_invalid,
12552 "spaces don't match", goto error);
12553 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12554 isl_die(ctx, isl_error_unsupported,
12555 "cannot handle divs yet", goto error);
12556 if (!isl_int_is_one(subs->v->el[0]))
12557 isl_die(ctx, isl_error_invalid,
12558 "can only substitute integer expressions", goto error);
12560 pos += isl_basic_set_offset(bset, type);
12562 isl_int_init(v);
12564 for (i = 0; i < bset->n_eq; ++i) {
12565 if (isl_int_is_zero(bset->eq[i][pos]))
12566 continue;
12567 isl_int_set(v, bset->eq[i][pos]);
12568 isl_int_set_si(bset->eq[i][pos], 0);
12569 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12570 v, subs->v->el + 1, subs->v->size - 1);
12573 for (i = 0; i < bset->n_ineq; ++i) {
12574 if (isl_int_is_zero(bset->ineq[i][pos]))
12575 continue;
12576 isl_int_set(v, bset->ineq[i][pos]);
12577 isl_int_set_si(bset->ineq[i][pos], 0);
12578 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12579 v, subs->v->el + 1, subs->v->size - 1);
12582 for (i = 0; i < bset->n_div; ++i) {
12583 if (isl_int_is_zero(bset->div[i][1 + pos]))
12584 continue;
12585 isl_int_set(v, bset->div[i][1 + pos]);
12586 isl_int_set_si(bset->div[i][1 + pos], 0);
12587 isl_seq_combine(bset->div[i] + 1,
12588 subs->v->el[0], bset->div[i] + 1,
12589 v, subs->v->el + 1, subs->v->size - 1);
12590 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12593 isl_int_clear(v);
12595 bset = isl_basic_set_simplify(bset);
12596 return isl_basic_set_finalize(bset);
12597 error:
12598 isl_basic_set_free(bset);
12599 return NULL;
12602 /* Plug in "subs" for dimension "type", "pos" of "set".
12604 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12605 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12607 int i;
12609 if (set && isl_set_plain_is_empty(set))
12610 return set;
12612 set = isl_set_cow(set);
12613 if (!set || !subs)
12614 goto error;
12616 for (i = set->n - 1; i >= 0; --i) {
12617 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12618 if (remove_if_empty(set, i) < 0)
12619 goto error;
12622 return set;
12623 error:
12624 isl_set_free(set);
12625 return NULL;
12628 /* Check if the range of "ma" is compatible with the domain or range
12629 * (depending on "type") of "bmap".
12631 static isl_stat check_basic_map_compatible_range_multi_aff(
12632 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12633 __isl_keep isl_multi_aff *ma)
12635 isl_bool m;
12636 isl_space *ma_space;
12638 ma_space = isl_multi_aff_get_space(ma);
12640 m = isl_space_has_equal_params(bmap->dim, ma_space);
12641 if (m < 0)
12642 goto error;
12643 if (!m)
12644 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12645 "parameters don't match", goto error);
12646 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12647 if (m < 0)
12648 goto error;
12649 if (!m)
12650 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12651 "spaces don't match", goto error);
12653 isl_space_free(ma_space);
12654 return isl_stat_ok;
12655 error:
12656 isl_space_free(ma_space);
12657 return isl_stat_error;
12660 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12661 * coefficients before the transformed range of dimensions,
12662 * the "n_after" coefficients after the transformed range of dimensions
12663 * and the coefficients of the other divs in "bmap".
12665 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12666 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12668 int i;
12669 int n_param;
12670 int n_set;
12671 isl_local_space *ls;
12673 if (n_div == 0)
12674 return 0;
12676 ls = isl_aff_get_domain_local_space(ma->p[0]);
12677 if (!ls)
12678 return -1;
12680 n_param = isl_local_space_dim(ls, isl_dim_param);
12681 n_set = isl_local_space_dim(ls, isl_dim_set);
12682 for (i = 0; i < n_div; ++i) {
12683 int o_bmap = 0, o_ls = 0;
12685 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12686 o_bmap += 1 + 1 + n_param;
12687 o_ls += 1 + 1 + n_param;
12688 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12689 o_bmap += n_before;
12690 isl_seq_cpy(bmap->div[i] + o_bmap,
12691 ls->div->row[i] + o_ls, n_set);
12692 o_bmap += n_set;
12693 o_ls += n_set;
12694 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12695 o_bmap += n_after;
12696 isl_seq_cpy(bmap->div[i] + o_bmap,
12697 ls->div->row[i] + o_ls, n_div);
12698 o_bmap += n_div;
12699 o_ls += n_div;
12700 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12701 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12702 goto error;
12705 isl_local_space_free(ls);
12706 return 0;
12707 error:
12708 isl_local_space_free(ls);
12709 return -1;
12712 /* How many stride constraints does "ma" enforce?
12713 * That is, how many of the affine expressions have a denominator
12714 * different from one?
12716 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12718 int i;
12719 int strides = 0;
12721 for (i = 0; i < ma->n; ++i)
12722 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12723 strides++;
12725 return strides;
12728 /* For each affine expression in ma of the form
12730 * x_i = (f_i y + h_i)/m_i
12732 * with m_i different from one, add a constraint to "bmap"
12733 * of the form
12735 * f_i y + h_i = m_i alpha_i
12737 * with alpha_i an additional existentially quantified variable.
12739 * The input variables of "ma" correspond to a subset of the variables
12740 * of "bmap". There are "n_before" variables in "bmap" before this
12741 * subset and "n_after" variables after this subset.
12742 * The integer divisions of the affine expressions in "ma" are assumed
12743 * to have been aligned. There are "n_div_ma" of them and
12744 * they appear first in "bmap", straight after the "n_after" variables.
12746 static __isl_give isl_basic_map *add_ma_strides(
12747 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12748 int n_before, int n_after, int n_div_ma)
12750 int i, k;
12751 int div;
12752 int total;
12753 int n_param;
12754 int n_in;
12756 total = isl_basic_map_total_dim(bmap);
12757 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12758 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12759 for (i = 0; i < ma->n; ++i) {
12760 int o_bmap = 0, o_ma = 1;
12762 if (isl_int_is_one(ma->p[i]->v->el[0]))
12763 continue;
12764 div = isl_basic_map_alloc_div(bmap);
12765 k = isl_basic_map_alloc_equality(bmap);
12766 if (div < 0 || k < 0)
12767 goto error;
12768 isl_int_set_si(bmap->div[div][0], 0);
12769 isl_seq_cpy(bmap->eq[k] + o_bmap,
12770 ma->p[i]->v->el + o_ma, 1 + n_param);
12771 o_bmap += 1 + n_param;
12772 o_ma += 1 + n_param;
12773 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12774 o_bmap += n_before;
12775 isl_seq_cpy(bmap->eq[k] + o_bmap,
12776 ma->p[i]->v->el + o_ma, n_in);
12777 o_bmap += n_in;
12778 o_ma += n_in;
12779 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12780 o_bmap += n_after;
12781 isl_seq_cpy(bmap->eq[k] + o_bmap,
12782 ma->p[i]->v->el + o_ma, n_div_ma);
12783 o_bmap += n_div_ma;
12784 o_ma += n_div_ma;
12785 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12786 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12787 total++;
12790 return bmap;
12791 error:
12792 isl_basic_map_free(bmap);
12793 return NULL;
12796 /* Replace the domain or range space (depending on "type) of "space" by "set".
12798 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12799 enum isl_dim_type type, __isl_take isl_space *set)
12801 if (type == isl_dim_in) {
12802 space = isl_space_range(space);
12803 space = isl_space_map_from_domain_and_range(set, space);
12804 } else {
12805 space = isl_space_domain(space);
12806 space = isl_space_map_from_domain_and_range(space, set);
12809 return space;
12812 /* Compute the preimage of the domain or range (depending on "type")
12813 * of "bmap" under the function represented by "ma".
12814 * In other words, plug in "ma" in the domain or range of "bmap".
12815 * The result is a basic map that lives in the same space as "bmap"
12816 * except that the domain or range has been replaced by
12817 * the domain space of "ma".
12819 * If bmap is represented by
12821 * A(p) + S u + B x + T v + C(divs) >= 0,
12823 * where u and x are input and output dimensions if type == isl_dim_out
12824 * while x and v are input and output dimensions if type == isl_dim_in,
12825 * and ma is represented by
12827 * x = D(p) + F(y) + G(divs')
12829 * then the result is
12831 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12833 * The divs in the input set are similarly adjusted.
12834 * In particular
12836 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12838 * becomes
12840 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12841 * B_i G(divs') + c_i(divs))/n_i)
12843 * If bmap is not a rational map and if F(y) involves any denominators
12845 * x_i = (f_i y + h_i)/m_i
12847 * then additional constraints are added to ensure that we only
12848 * map back integer points. That is we enforce
12850 * f_i y + h_i = m_i alpha_i
12852 * with alpha_i an additional existentially quantified variable.
12854 * We first copy over the divs from "ma".
12855 * Then we add the modified constraints and divs from "bmap".
12856 * Finally, we add the stride constraints, if needed.
12858 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12859 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12860 __isl_take isl_multi_aff *ma)
12862 int i, k;
12863 isl_space *space;
12864 isl_basic_map *res = NULL;
12865 int n_before, n_after, n_div_bmap, n_div_ma;
12866 isl_int f, c1, c2, g;
12867 isl_bool rational;
12868 int strides;
12870 isl_int_init(f);
12871 isl_int_init(c1);
12872 isl_int_init(c2);
12873 isl_int_init(g);
12875 ma = isl_multi_aff_align_divs(ma);
12876 if (!bmap || !ma)
12877 goto error;
12878 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12879 goto error;
12881 if (type == isl_dim_in) {
12882 n_before = 0;
12883 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12884 } else {
12885 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12886 n_after = 0;
12888 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12889 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12891 space = isl_multi_aff_get_domain_space(ma);
12892 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12893 rational = isl_basic_map_is_rational(bmap);
12894 strides = rational ? 0 : multi_aff_strides(ma);
12895 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12896 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12897 if (rational)
12898 res = isl_basic_map_set_rational(res);
12900 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12901 if (isl_basic_map_alloc_div(res) < 0)
12902 goto error;
12904 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12905 goto error;
12907 for (i = 0; i < bmap->n_eq; ++i) {
12908 k = isl_basic_map_alloc_equality(res);
12909 if (k < 0)
12910 goto error;
12911 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12912 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12915 for (i = 0; i < bmap->n_ineq; ++i) {
12916 k = isl_basic_map_alloc_inequality(res);
12917 if (k < 0)
12918 goto error;
12919 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12920 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12923 for (i = 0; i < bmap->n_div; ++i) {
12924 if (isl_int_is_zero(bmap->div[i][0])) {
12925 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12926 continue;
12928 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12929 n_before, n_after, n_div_ma, n_div_bmap,
12930 f, c1, c2, g, 1);
12933 if (strides)
12934 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12936 isl_int_clear(f);
12937 isl_int_clear(c1);
12938 isl_int_clear(c2);
12939 isl_int_clear(g);
12940 isl_basic_map_free(bmap);
12941 isl_multi_aff_free(ma);
12942 res = isl_basic_map_simplify(res);
12943 return isl_basic_map_finalize(res);
12944 error:
12945 isl_int_clear(f);
12946 isl_int_clear(c1);
12947 isl_int_clear(c2);
12948 isl_int_clear(g);
12949 isl_basic_map_free(bmap);
12950 isl_multi_aff_free(ma);
12951 isl_basic_map_free(res);
12952 return NULL;
12955 /* Compute the preimage of "bset" under the function represented by "ma".
12956 * In other words, plug in "ma" in "bset". The result is a basic set
12957 * that lives in the domain space of "ma".
12959 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12960 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12962 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12965 /* Compute the preimage of the domain of "bmap" under the function
12966 * represented by "ma".
12967 * In other words, plug in "ma" in the domain of "bmap".
12968 * The result is a basic map that lives in the same space as "bmap"
12969 * except that the domain has been replaced by the domain space of "ma".
12971 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12972 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12974 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12977 /* Compute the preimage of the range of "bmap" under the function
12978 * represented by "ma".
12979 * In other words, plug in "ma" in the range of "bmap".
12980 * The result is a basic map that lives in the same space as "bmap"
12981 * except that the range has been replaced by the domain space of "ma".
12983 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12984 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12986 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12989 /* Check if the range of "ma" is compatible with the domain or range
12990 * (depending on "type") of "map".
12991 * Return isl_stat_error if anything is wrong.
12993 static isl_stat check_map_compatible_range_multi_aff(
12994 __isl_keep isl_map *map, enum isl_dim_type type,
12995 __isl_keep isl_multi_aff *ma)
12997 isl_bool m;
12998 isl_space *ma_space;
13000 ma_space = isl_multi_aff_get_space(ma);
13001 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13002 isl_space_free(ma_space);
13003 if (m < 0)
13004 return isl_stat_error;
13005 if (!m)
13006 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13007 "spaces don't match", return isl_stat_error);
13008 return isl_stat_ok;
13011 /* Compute the preimage of the domain or range (depending on "type")
13012 * of "map" under the function represented by "ma".
13013 * In other words, plug in "ma" in the domain or range of "map".
13014 * The result is a map that lives in the same space as "map"
13015 * except that the domain or range has been replaced by
13016 * the domain space of "ma".
13018 * The parameters are assumed to have been aligned.
13020 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13021 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13023 int i;
13024 isl_space *space;
13026 map = isl_map_cow(map);
13027 ma = isl_multi_aff_align_divs(ma);
13028 if (!map || !ma)
13029 goto error;
13030 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13031 goto error;
13033 for (i = 0; i < map->n; ++i) {
13034 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13035 isl_multi_aff_copy(ma));
13036 if (!map->p[i])
13037 goto error;
13040 space = isl_multi_aff_get_domain_space(ma);
13041 space = isl_space_set(isl_map_get_space(map), type, space);
13043 isl_space_free(map->dim);
13044 map->dim = space;
13045 if (!map->dim)
13046 goto error;
13048 isl_multi_aff_free(ma);
13049 if (map->n > 1)
13050 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13051 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13052 return map;
13053 error:
13054 isl_multi_aff_free(ma);
13055 isl_map_free(map);
13056 return NULL;
13059 /* Compute the preimage of the domain or range (depending on "type")
13060 * of "map" under the function represented by "ma".
13061 * In other words, plug in "ma" in the domain or range of "map".
13062 * The result is a map that lives in the same space as "map"
13063 * except that the domain or range has been replaced by
13064 * the domain space of "ma".
13066 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13067 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13069 isl_bool aligned;
13071 if (!map || !ma)
13072 goto error;
13074 aligned = isl_map_space_has_equal_params(map, ma->space);
13075 if (aligned < 0)
13076 goto error;
13077 if (aligned)
13078 return map_preimage_multi_aff(map, type, ma);
13080 if (isl_map_check_named_params(map) < 0)
13081 goto error;
13082 if (!isl_space_has_named_params(ma->space))
13083 isl_die(map->ctx, isl_error_invalid,
13084 "unaligned unnamed parameters", goto error);
13085 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13086 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13088 return map_preimage_multi_aff(map, type, ma);
13089 error:
13090 isl_multi_aff_free(ma);
13091 return isl_map_free(map);
13094 /* Compute the preimage of "set" under the function represented by "ma".
13095 * In other words, plug in "ma" in "set". The result is a set
13096 * that lives in the domain space of "ma".
13098 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13099 __isl_take isl_multi_aff *ma)
13101 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13104 /* Compute the preimage of the domain of "map" under the function
13105 * represented by "ma".
13106 * In other words, plug in "ma" in the domain of "map".
13107 * The result is a map that lives in the same space as "map"
13108 * except that the domain has been replaced by the domain space of "ma".
13110 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13111 __isl_take isl_multi_aff *ma)
13113 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13116 /* Compute the preimage of the range of "map" under the function
13117 * represented by "ma".
13118 * In other words, plug in "ma" in the range of "map".
13119 * The result is a map that lives in the same space as "map"
13120 * except that the range has been replaced by the domain space of "ma".
13122 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13123 __isl_take isl_multi_aff *ma)
13125 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13128 /* Compute the preimage of "map" under the function represented by "pma".
13129 * In other words, plug in "pma" in the domain or range of "map".
13130 * The result is a map that lives in the same space as "map",
13131 * except that the space of type "type" has been replaced by
13132 * the domain space of "pma".
13134 * The parameters of "map" and "pma" are assumed to have been aligned.
13136 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13137 __isl_take isl_map *map, enum isl_dim_type type,
13138 __isl_take isl_pw_multi_aff *pma)
13140 int i;
13141 isl_map *res;
13143 if (!pma)
13144 goto error;
13146 if (pma->n == 0) {
13147 isl_pw_multi_aff_free(pma);
13148 res = isl_map_empty(isl_map_get_space(map));
13149 isl_map_free(map);
13150 return res;
13153 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13154 isl_multi_aff_copy(pma->p[0].maff));
13155 if (type == isl_dim_in)
13156 res = isl_map_intersect_domain(res,
13157 isl_map_copy(pma->p[0].set));
13158 else
13159 res = isl_map_intersect_range(res,
13160 isl_map_copy(pma->p[0].set));
13162 for (i = 1; i < pma->n; ++i) {
13163 isl_map *res_i;
13165 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13166 isl_multi_aff_copy(pma->p[i].maff));
13167 if (type == isl_dim_in)
13168 res_i = isl_map_intersect_domain(res_i,
13169 isl_map_copy(pma->p[i].set));
13170 else
13171 res_i = isl_map_intersect_range(res_i,
13172 isl_map_copy(pma->p[i].set));
13173 res = isl_map_union(res, res_i);
13176 isl_pw_multi_aff_free(pma);
13177 isl_map_free(map);
13178 return res;
13179 error:
13180 isl_pw_multi_aff_free(pma);
13181 isl_map_free(map);
13182 return NULL;
13185 /* Compute the preimage of "map" under the function represented by "pma".
13186 * In other words, plug in "pma" in the domain or range of "map".
13187 * The result is a map that lives in the same space as "map",
13188 * except that the space of type "type" has been replaced by
13189 * the domain space of "pma".
13191 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13192 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13194 isl_bool aligned;
13196 if (!map || !pma)
13197 goto error;
13199 aligned = isl_map_space_has_equal_params(map, pma->dim);
13200 if (aligned < 0)
13201 goto error;
13202 if (aligned)
13203 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13205 if (isl_map_check_named_params(map) < 0)
13206 goto error;
13207 if (!isl_space_has_named_params(pma->dim))
13208 isl_die(map->ctx, isl_error_invalid,
13209 "unaligned unnamed parameters", goto error);
13210 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13211 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13213 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13214 error:
13215 isl_pw_multi_aff_free(pma);
13216 return isl_map_free(map);
13219 /* Compute the preimage of "set" under the function represented by "pma".
13220 * In other words, plug in "pma" in "set". The result is a set
13221 * that lives in the domain space of "pma".
13223 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13224 __isl_take isl_pw_multi_aff *pma)
13226 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13229 /* Compute the preimage of the domain of "map" under the function
13230 * represented by "pma".
13231 * In other words, plug in "pma" in the domain of "map".
13232 * The result is a map that lives in the same space as "map",
13233 * except that domain space has been replaced by the domain space of "pma".
13235 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13236 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13238 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13241 /* Compute the preimage of the range of "map" under the function
13242 * represented by "pma".
13243 * In other words, plug in "pma" in the range of "map".
13244 * The result is a map that lives in the same space as "map",
13245 * except that range space has been replaced by the domain space of "pma".
13247 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13248 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13250 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13253 /* Compute the preimage of "map" under the function represented by "mpa".
13254 * In other words, plug in "mpa" in the domain or range of "map".
13255 * The result is a map that lives in the same space as "map",
13256 * except that the space of type "type" has been replaced by
13257 * the domain space of "mpa".
13259 * If the map does not involve any constraints that refer to the
13260 * dimensions of the substituted space, then the only possible
13261 * effect of "mpa" on the map is to map the space to a different space.
13262 * We create a separate isl_multi_aff to effectuate this change
13263 * in order to avoid spurious splitting of the map along the pieces
13264 * of "mpa".
13266 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13267 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13269 int n;
13270 isl_pw_multi_aff *pma;
13272 if (!map || !mpa)
13273 goto error;
13275 n = isl_map_dim(map, type);
13276 if (!isl_map_involves_dims(map, type, 0, n)) {
13277 isl_space *space;
13278 isl_multi_aff *ma;
13280 space = isl_multi_pw_aff_get_space(mpa);
13281 isl_multi_pw_aff_free(mpa);
13282 ma = isl_multi_aff_zero(space);
13283 return isl_map_preimage_multi_aff(map, type, ma);
13286 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13287 return isl_map_preimage_pw_multi_aff(map, type, pma);
13288 error:
13289 isl_map_free(map);
13290 isl_multi_pw_aff_free(mpa);
13291 return NULL;
13294 /* Compute the preimage of "map" under the function represented by "mpa".
13295 * In other words, plug in "mpa" in the domain "map".
13296 * The result is a map that lives in the same space as "map",
13297 * except that domain space has been replaced by the domain space of "mpa".
13299 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13300 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13302 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13305 /* Compute the preimage of "set" by the function represented by "mpa".
13306 * In other words, plug in "mpa" in "set".
13308 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13309 __isl_take isl_multi_pw_aff *mpa)
13311 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13314 /* Are the "n" "coefficients" starting at "first" of the integer division
13315 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13316 * to each other?
13317 * The "coefficient" at position 0 is the denominator.
13318 * The "coefficient" at position 1 is the constant term.
13320 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13321 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13322 unsigned first, unsigned n)
13324 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13325 return isl_bool_error;
13326 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13327 return isl_bool_error;
13328 return isl_seq_eq(bmap1->div[pos1] + first,
13329 bmap2->div[pos2] + first, n);
13332 /* Are the integer division expressions at position "pos1" in "bmap1" and
13333 * "pos2" in "bmap2" equal to each other, except that the constant terms
13334 * are different?
13336 isl_bool isl_basic_map_equal_div_expr_except_constant(
13337 __isl_keep isl_basic_map *bmap1, int pos1,
13338 __isl_keep isl_basic_map *bmap2, int pos2)
13340 isl_bool equal;
13341 unsigned total;
13343 if (!bmap1 || !bmap2)
13344 return isl_bool_error;
13345 total = isl_basic_map_total_dim(bmap1);
13346 if (total != isl_basic_map_total_dim(bmap2))
13347 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13348 "incomparable div expressions", return isl_bool_error);
13349 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13350 0, 1);
13351 if (equal < 0 || !equal)
13352 return equal;
13353 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13354 1, 1);
13355 if (equal < 0 || equal)
13356 return isl_bool_not(equal);
13357 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13358 2, total);
13361 /* Replace the numerator of the constant term of the integer division
13362 * expression at position "div" in "bmap" by "value".
13363 * The caller guarantees that this does not change the meaning
13364 * of the input.
13366 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13367 __isl_take isl_basic_map *bmap, int div, int value)
13369 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13370 return isl_basic_map_free(bmap);
13372 isl_int_set_si(bmap->div[div][1], value);
13374 return bmap;
13377 /* Is the point "inner" internal to inequality constraint "ineq"
13378 * of "bset"?
13379 * The point is considered to be internal to the inequality constraint,
13380 * if it strictly lies on the positive side of the inequality constraint,
13381 * or if it lies on the constraint and the constraint is lexico-positive.
13383 static isl_bool is_internal(__isl_keep isl_vec *inner,
13384 __isl_keep isl_basic_set *bset, int ineq)
13386 isl_ctx *ctx;
13387 int pos;
13388 unsigned total;
13390 if (!inner || !bset)
13391 return isl_bool_error;
13393 ctx = isl_basic_set_get_ctx(bset);
13394 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13395 &ctx->normalize_gcd);
13396 if (!isl_int_is_zero(ctx->normalize_gcd))
13397 return isl_int_is_nonneg(ctx->normalize_gcd);
13399 total = isl_basic_set_dim(bset, isl_dim_all);
13400 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13401 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13404 /* Tighten the inequality constraints of "bset" that are outward with respect
13405 * to the point "vec".
13406 * That is, tighten the constraints that are not satisfied by "vec".
13408 * "vec" is a point internal to some superset S of "bset" that is used
13409 * to make the subsets of S disjoint, by tightening one half of the constraints
13410 * that separate two subsets. In particular, the constraints of S
13411 * are all satisfied by "vec" and should not be tightened.
13412 * Of the internal constraints, those that have "vec" on the outside
13413 * are tightened. The shared facet is included in the adjacent subset
13414 * with the opposite constraint.
13415 * For constraints that saturate "vec", this criterion cannot be used
13416 * to determine which of the two sides should be tightened.
13417 * Instead, the sign of the first non-zero coefficient is used
13418 * to make this choice. Note that this second criterion is never used
13419 * on the constraints of S since "vec" is interior to "S".
13421 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13422 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13424 int j;
13426 bset = isl_basic_set_cow(bset);
13427 if (!bset)
13428 return NULL;
13429 for (j = 0; j < bset->n_ineq; ++j) {
13430 isl_bool internal;
13432 internal = is_internal(vec, bset, j);
13433 if (internal < 0)
13434 return isl_basic_set_free(bset);
13435 if (internal)
13436 continue;
13437 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13440 return bset;
13443 /* Replace the variables x of type "type" starting at "first" in "bmap"
13444 * by x' with x = M x' with M the matrix trans.
13445 * That is, replace the corresponding coefficients c by c M.
13447 * The transformation matrix should be a square matrix.
13449 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13450 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13451 __isl_take isl_mat *trans)
13453 unsigned pos;
13455 bmap = isl_basic_map_cow(bmap);
13456 if (!bmap || !trans)
13457 goto error;
13459 if (trans->n_row != trans->n_col)
13460 isl_die(trans->ctx, isl_error_invalid,
13461 "expecting square transformation matrix", goto error);
13462 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
13463 isl_die(trans->ctx, isl_error_invalid,
13464 "oversized transformation matrix", goto error);
13466 pos = isl_basic_map_offset(bmap, type) + first;
13468 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13469 isl_mat_copy(trans)) < 0)
13470 goto error;
13471 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13472 isl_mat_copy(trans)) < 0)
13473 goto error;
13474 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13475 isl_mat_copy(trans)) < 0)
13476 goto error;
13478 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
13479 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13481 isl_mat_free(trans);
13482 return bmap;
13483 error:
13484 isl_mat_free(trans);
13485 isl_basic_map_free(bmap);
13486 return NULL;
13489 /* Replace the variables x of type "type" starting at "first" in "bset"
13490 * by x' with x = M x' with M the matrix trans.
13491 * That is, replace the corresponding coefficients c by c M.
13493 * The transformation matrix should be a square matrix.
13495 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13496 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13497 __isl_take isl_mat *trans)
13499 return isl_basic_map_transform_dims(bset, type, first, trans);