drop isl_space_as_set_space
[isl.git] / isl_map.c
blobeaa52161bd5f3b4e808b8bbea2893048375b54a2
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 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
93 return map ? n(map->dim, type) : 0;
96 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
98 return set ? n(set->dim, type) : 0;
101 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
102 enum isl_dim_type type)
104 isl_space *space;
106 if (!bmap)
107 return 0;
109 space = bmap->dim;
110 switch (type) {
111 case isl_dim_cst: return 0;
112 case isl_dim_param: return 1;
113 case isl_dim_in: return 1 + space->nparam;
114 case isl_dim_out: return 1 + space->nparam + space->n_in;
115 case isl_dim_div: return 1 + space->nparam + space->n_in +
116 space->n_out;
117 default: return 0;
121 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
122 enum isl_dim_type type)
124 return isl_basic_map_offset(bset, type);
127 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
129 return pos(map->dim, type);
132 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
133 enum isl_dim_type type)
135 return isl_basic_map_dim(bset, type);
138 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
140 return isl_basic_set_dim(bset, isl_dim_set);
143 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
145 return isl_basic_set_dim(bset, isl_dim_param);
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
150 if (!bset)
151 return 0;
152 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
155 unsigned isl_set_n_dim(__isl_keep isl_set *set)
157 return isl_set_dim(set, isl_dim_set);
160 unsigned isl_set_n_param(__isl_keep isl_set *set)
162 return isl_set_dim(set, isl_dim_param);
165 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
167 return bmap ? bmap->dim->n_in : 0;
170 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
172 return bmap ? bmap->dim->n_out : 0;
175 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
177 return bmap ? bmap->dim->nparam : 0;
180 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
182 return bmap ? bmap->n_div : 0;
185 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
187 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
190 unsigned isl_map_n_in(const struct isl_map *map)
192 return map ? map->dim->n_in : 0;
195 unsigned isl_map_n_out(const struct isl_map *map)
197 return map ? map->dim->n_out : 0;
200 unsigned isl_map_n_param(const struct isl_map *map)
202 return map ? map->dim->nparam : 0;
205 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
206 __isl_keep isl_set *set)
208 isl_bool m;
209 if (!map || !set)
210 return isl_bool_error;
211 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
212 if (m < 0 || !m)
213 return m;
214 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
215 set->dim, isl_dim_set);
218 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
219 __isl_keep isl_basic_set *bset)
221 isl_bool m;
222 if (!bmap || !bset)
223 return isl_bool_error;
224 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
225 if (m < 0 || !m)
226 return m;
227 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
228 bset->dim, isl_dim_set);
231 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
232 __isl_keep isl_set *set)
234 isl_bool m;
235 if (!map || !set)
236 return isl_bool_error;
237 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
238 if (m < 0 || !m)
239 return m;
240 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
241 set->dim, isl_dim_set);
244 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
245 __isl_keep isl_basic_set *bset)
247 isl_bool m;
248 if (!bmap || !bset)
249 return isl_bool_error;
250 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
251 if (m < 0 || !m)
252 return m;
253 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
254 bset->dim, isl_dim_set);
257 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
259 return bmap ? bmap->ctx : NULL;
262 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
264 return bset ? bset->ctx : NULL;
267 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
269 return map ? map->ctx : NULL;
272 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
274 return set ? set->ctx : NULL;
277 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
279 if (!bmap)
280 return NULL;
281 return isl_space_copy(bmap->dim);
284 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
286 if (!bset)
287 return NULL;
288 return isl_space_copy(bset->dim);
291 /* Extract the divs in "bmap" as a matrix.
293 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
295 int i;
296 isl_ctx *ctx;
297 isl_mat *div;
298 unsigned total;
299 unsigned cols;
301 if (!bmap)
302 return NULL;
304 ctx = isl_basic_map_get_ctx(bmap);
305 total = isl_space_dim(bmap->dim, isl_dim_all);
306 cols = 1 + 1 + total + bmap->n_div;
307 div = isl_mat_alloc(ctx, bmap->n_div, cols);
308 if (!div)
309 return NULL;
311 for (i = 0; i < bmap->n_div; ++i)
312 isl_seq_cpy(div->row[i], bmap->div[i], cols);
314 return div;
317 /* Extract the divs in "bset" as a matrix.
319 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
321 return isl_basic_map_get_divs(bset);
324 __isl_give isl_local_space *isl_basic_map_get_local_space(
325 __isl_keep isl_basic_map *bmap)
327 isl_mat *div;
329 if (!bmap)
330 return NULL;
332 div = isl_basic_map_get_divs(bmap);
333 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
336 __isl_give isl_local_space *isl_basic_set_get_local_space(
337 __isl_keep isl_basic_set *bset)
339 return isl_basic_map_get_local_space(bset);
342 /* For each known div d = floor(f/m), add the constraints
344 * f - m d >= 0
345 * -(f-(m-1)) + m d >= 0
347 * Do not finalize the result.
349 static __isl_give isl_basic_map *add_known_div_constraints(
350 __isl_take isl_basic_map *bmap)
352 int i;
353 unsigned n_div;
355 if (!bmap)
356 return NULL;
357 n_div = isl_basic_map_dim(bmap, isl_dim_div);
358 if (n_div == 0)
359 return bmap;
360 bmap = isl_basic_map_cow(bmap);
361 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
362 if (!bmap)
363 return NULL;
364 for (i = 0; i < n_div; ++i) {
365 if (isl_int_is_zero(bmap->div[i][0]))
366 continue;
367 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
368 return isl_basic_map_free(bmap);
371 return bmap;
374 __isl_give isl_basic_map *isl_basic_map_from_local_space(
375 __isl_take isl_local_space *ls)
377 int i;
378 int n_div;
379 isl_basic_map *bmap;
381 if (!ls)
382 return NULL;
384 n_div = isl_local_space_dim(ls, isl_dim_div);
385 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
386 n_div, 0, 2 * n_div);
388 for (i = 0; i < n_div; ++i)
389 if (isl_basic_map_alloc_div(bmap) < 0)
390 goto error;
392 for (i = 0; i < n_div; ++i)
393 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
394 bmap = add_known_div_constraints(bmap);
396 isl_local_space_free(ls);
397 return bmap;
398 error:
399 isl_local_space_free(ls);
400 isl_basic_map_free(bmap);
401 return NULL;
404 __isl_give isl_basic_set *isl_basic_set_from_local_space(
405 __isl_take isl_local_space *ls)
407 return isl_basic_map_from_local_space(ls);
410 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
412 if (!map)
413 return NULL;
414 return isl_space_copy(map->dim);
417 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
419 if (!set)
420 return NULL;
421 return isl_space_copy(set->dim);
424 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
425 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
427 bmap = isl_basic_map_cow(bmap);
428 if (!bmap)
429 return NULL;
430 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
431 if (!bmap->dim)
432 goto error;
433 bmap = isl_basic_map_finalize(bmap);
434 return bmap;
435 error:
436 isl_basic_map_free(bmap);
437 return NULL;
440 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
441 __isl_take isl_basic_set *bset, const char *s)
443 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
446 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
447 enum isl_dim_type type)
449 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
452 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
453 enum isl_dim_type type, const char *s)
455 int i;
457 map = isl_map_cow(map);
458 if (!map)
459 return NULL;
461 map->dim = isl_space_set_tuple_name(map->dim, type, s);
462 if (!map->dim)
463 goto error;
465 for (i = 0; i < map->n; ++i) {
466 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
467 if (!map->p[i])
468 goto error;
471 return map;
472 error:
473 isl_map_free(map);
474 return NULL;
477 /* Replace the identifier of the tuple of type "type" by "id".
479 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
480 __isl_take isl_basic_map *bmap,
481 enum isl_dim_type type, __isl_take isl_id *id)
483 bmap = isl_basic_map_cow(bmap);
484 if (!bmap)
485 goto error;
486 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
487 if (!bmap->dim)
488 return isl_basic_map_free(bmap);
489 bmap = isl_basic_map_finalize(bmap);
490 return bmap;
491 error:
492 isl_id_free(id);
493 return NULL;
496 /* Replace the identifier of the tuple by "id".
498 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
499 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
501 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
504 /* Does the input or output tuple have a name?
506 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
508 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
511 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
512 enum isl_dim_type type)
514 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
517 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
518 const char *s)
520 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
521 isl_dim_set, s));
524 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
525 enum isl_dim_type type, __isl_take isl_id *id)
527 map = isl_map_cow(map);
528 if (!map)
529 goto error;
531 map->dim = isl_space_set_tuple_id(map->dim, type, id);
533 return isl_map_reset_space(map, isl_space_copy(map->dim));
534 error:
535 isl_id_free(id);
536 return NULL;
539 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
540 __isl_take isl_id *id)
542 return isl_map_set_tuple_id(set, isl_dim_set, id);
545 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
546 enum isl_dim_type type)
548 map = isl_map_cow(map);
549 if (!map)
550 return NULL;
552 map->dim = isl_space_reset_tuple_id(map->dim, type);
554 return isl_map_reset_space(map, isl_space_copy(map->dim));
557 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
559 return isl_map_reset_tuple_id(set, isl_dim_set);
562 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
564 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
567 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
568 enum isl_dim_type type)
570 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
573 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
575 return isl_map_has_tuple_id(set, isl_dim_set);
578 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
580 return isl_map_get_tuple_id(set, isl_dim_set);
583 /* Does the set tuple have a name?
585 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
587 if (!set)
588 return isl_bool_error;
589 return isl_space_has_tuple_name(set->dim, isl_dim_set);
593 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
595 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
598 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
600 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
603 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
604 enum isl_dim_type type, unsigned pos)
606 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
609 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
610 enum isl_dim_type type, unsigned pos)
612 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
615 /* Does the given dimension have a name?
617 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
618 enum isl_dim_type type, unsigned pos)
620 if (!map)
621 return isl_bool_error;
622 return isl_space_has_dim_name(map->dim, type, pos);
625 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
626 enum isl_dim_type type, unsigned pos)
628 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
631 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
632 enum isl_dim_type type, unsigned pos)
634 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
637 /* Does the given dimension have a name?
639 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
640 enum isl_dim_type type, unsigned pos)
642 if (!set)
643 return isl_bool_error;
644 return isl_space_has_dim_name(set->dim, type, pos);
647 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
648 __isl_take isl_basic_map *bmap,
649 enum isl_dim_type type, unsigned pos, const char *s)
651 bmap = isl_basic_map_cow(bmap);
652 if (!bmap)
653 return NULL;
654 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
655 if (!bmap->dim)
656 goto error;
657 return isl_basic_map_finalize(bmap);
658 error:
659 isl_basic_map_free(bmap);
660 return NULL;
663 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
664 enum isl_dim_type type, unsigned pos, const char *s)
666 int i;
668 map = isl_map_cow(map);
669 if (!map)
670 return NULL;
672 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
673 if (!map->dim)
674 goto error;
676 for (i = 0; i < map->n; ++i) {
677 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
678 if (!map->p[i])
679 goto error;
682 return map;
683 error:
684 isl_map_free(map);
685 return NULL;
688 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
689 __isl_take isl_basic_set *bset,
690 enum isl_dim_type type, unsigned pos, const char *s)
692 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
693 type, pos, s));
696 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
697 enum isl_dim_type type, unsigned pos, const char *s)
699 return set_from_map(isl_map_set_dim_name(set_to_map(set),
700 type, pos, s));
703 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
704 enum isl_dim_type type, unsigned pos)
706 if (!bmap)
707 return isl_bool_error;
708 return isl_space_has_dim_id(bmap->dim, type, pos);
711 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
712 enum isl_dim_type type, unsigned pos)
714 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
717 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
718 enum isl_dim_type type, unsigned pos)
720 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
723 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
724 enum isl_dim_type type, unsigned pos)
726 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
729 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
730 enum isl_dim_type type, unsigned pos)
732 return isl_map_has_dim_id(set, type, pos);
735 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
736 enum isl_dim_type type, unsigned pos)
738 return isl_map_get_dim_id(set, type, pos);
741 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
742 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
744 map = isl_map_cow(map);
745 if (!map)
746 goto error;
748 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
750 return isl_map_reset_space(map, isl_space_copy(map->dim));
751 error:
752 isl_id_free(id);
753 return NULL;
756 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
757 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
759 return isl_map_set_dim_id(set, type, pos, id);
762 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
763 __isl_keep isl_id *id)
765 if (!map)
766 return -1;
767 return isl_space_find_dim_by_id(map->dim, type, id);
770 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
771 __isl_keep isl_id *id)
773 return isl_map_find_dim_by_id(set, type, id);
776 /* Return the position of the dimension of the given type and name
777 * in "bmap".
778 * Return -1 if no such dimension can be found.
780 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
781 enum isl_dim_type type, const char *name)
783 if (!bmap)
784 return -1;
785 return isl_space_find_dim_by_name(bmap->dim, type, name);
788 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
789 const char *name)
791 if (!map)
792 return -1;
793 return isl_space_find_dim_by_name(map->dim, type, name);
796 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
797 const char *name)
799 return isl_map_find_dim_by_name(set, type, name);
802 /* Reset the user pointer on all identifiers of parameters and tuples
803 * of the space of "map".
805 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
807 isl_space *space;
809 space = isl_map_get_space(map);
810 space = isl_space_reset_user(space);
811 map = isl_map_reset_space(map, space);
813 return map;
816 /* Reset the user pointer on all identifiers of parameters and tuples
817 * of the space of "set".
819 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
821 return isl_map_reset_user(set);
824 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
826 if (!bmap)
827 return isl_bool_error;
828 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
831 /* Has "map" been marked as a rational map?
832 * In particular, have all basic maps in "map" been marked this way?
833 * An empty map is not considered to be rational.
834 * Maps where only some of the basic maps are marked rational
835 * are not allowed.
837 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
839 int i;
840 isl_bool rational;
842 if (!map)
843 return isl_bool_error;
844 if (map->n == 0)
845 return isl_bool_false;
846 rational = isl_basic_map_is_rational(map->p[0]);
847 if (rational < 0)
848 return rational;
849 for (i = 1; i < map->n; ++i) {
850 isl_bool rational_i;
852 rational_i = isl_basic_map_is_rational(map->p[i]);
853 if (rational_i < 0)
854 return rational_i;
855 if (rational != rational_i)
856 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
857 "mixed rational and integer basic maps "
858 "not supported", return isl_bool_error);
861 return rational;
864 /* Has "set" been marked as a rational set?
865 * In particular, have all basic set in "set" been marked this way?
866 * An empty set is not considered to be rational.
867 * Sets where only some of the basic sets are marked rational
868 * are not allowed.
870 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
872 return isl_map_is_rational(set);
875 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
877 return isl_basic_map_is_rational(bset);
880 /* Does "bmap" contain any rational points?
882 * If "bmap" has an equality for each dimension, equating the dimension
883 * to an integer constant, then it has no rational points, even if it
884 * is marked as rational.
886 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
888 isl_bool has_rational = isl_bool_true;
889 unsigned total;
891 if (!bmap)
892 return isl_bool_error;
893 if (isl_basic_map_plain_is_empty(bmap))
894 return isl_bool_false;
895 if (!isl_basic_map_is_rational(bmap))
896 return isl_bool_false;
897 bmap = isl_basic_map_copy(bmap);
898 bmap = isl_basic_map_implicit_equalities(bmap);
899 if (!bmap)
900 return isl_bool_error;
901 total = isl_basic_map_total_dim(bmap);
902 if (bmap->n_eq == total) {
903 int i, j;
904 for (i = 0; i < bmap->n_eq; ++i) {
905 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
906 if (j < 0)
907 break;
908 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
909 !isl_int_is_negone(bmap->eq[i][1 + j]))
910 break;
911 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
912 total - j - 1);
913 if (j >= 0)
914 break;
916 if (i == bmap->n_eq)
917 has_rational = isl_bool_false;
919 isl_basic_map_free(bmap);
921 return has_rational;
924 /* Does "map" contain any rational points?
926 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
928 int i;
929 isl_bool has_rational;
931 if (!map)
932 return isl_bool_error;
933 for (i = 0; i < map->n; ++i) {
934 has_rational = isl_basic_map_has_rational(map->p[i]);
935 if (has_rational < 0 || has_rational)
936 return has_rational;
938 return isl_bool_false;
941 /* Does "set" contain any rational points?
943 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
945 return isl_map_has_rational(set);
948 /* Is this basic set a parameter domain?
950 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
952 if (!bset)
953 return isl_bool_error;
954 return isl_space_is_params(bset->dim);
957 /* Is this set a parameter domain?
959 isl_bool isl_set_is_params(__isl_keep isl_set *set)
961 if (!set)
962 return isl_bool_error;
963 return isl_space_is_params(set->dim);
966 /* Is this map actually a parameter domain?
967 * Users should never call this function. Outside of isl,
968 * a map can never be a parameter domain.
970 isl_bool isl_map_is_params(__isl_keep isl_map *map)
972 if (!map)
973 return isl_bool_error;
974 return isl_space_is_params(map->dim);
977 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
978 struct isl_basic_map *bmap, unsigned extra,
979 unsigned n_eq, unsigned n_ineq)
981 int i;
982 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
984 bmap->ctx = ctx;
985 isl_ctx_ref(ctx);
987 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
988 if (isl_blk_is_error(bmap->block))
989 goto error;
991 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
992 if ((n_ineq + n_eq) && !bmap->ineq)
993 goto error;
995 if (extra == 0) {
996 bmap->block2 = isl_blk_empty();
997 bmap->div = NULL;
998 } else {
999 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1000 if (isl_blk_is_error(bmap->block2))
1001 goto error;
1003 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1004 if (!bmap->div)
1005 goto error;
1008 for (i = 0; i < n_ineq + n_eq; ++i)
1009 bmap->ineq[i] = bmap->block.data + i * row_size;
1011 for (i = 0; i < extra; ++i)
1012 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1014 bmap->ref = 1;
1015 bmap->flags = 0;
1016 bmap->c_size = n_eq + n_ineq;
1017 bmap->eq = bmap->ineq + n_ineq;
1018 bmap->extra = extra;
1019 bmap->n_eq = 0;
1020 bmap->n_ineq = 0;
1021 bmap->n_div = 0;
1022 bmap->sample = NULL;
1024 return bmap;
1025 error:
1026 isl_basic_map_free(bmap);
1027 return NULL;
1030 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1031 unsigned nparam, unsigned dim, unsigned extra,
1032 unsigned n_eq, unsigned n_ineq)
1034 struct isl_basic_map *bmap;
1035 isl_space *space;
1037 space = isl_space_set_alloc(ctx, nparam, dim);
1038 if (!space)
1039 return NULL;
1041 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1042 return bset_from_bmap(bmap);
1045 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1046 unsigned extra, unsigned n_eq, unsigned n_ineq)
1048 struct isl_basic_map *bmap;
1049 if (!dim)
1050 return NULL;
1051 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1052 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1053 return bset_from_bmap(bmap);
1054 error:
1055 isl_space_free(dim);
1056 return NULL;
1059 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1060 unsigned extra, unsigned n_eq, unsigned n_ineq)
1062 struct isl_basic_map *bmap;
1064 if (!dim)
1065 return NULL;
1066 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1067 if (!bmap)
1068 goto error;
1069 bmap->dim = dim;
1071 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1072 error:
1073 isl_space_free(dim);
1074 return NULL;
1077 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1078 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1079 unsigned n_eq, unsigned n_ineq)
1081 struct isl_basic_map *bmap;
1082 isl_space *dim;
1084 dim = isl_space_alloc(ctx, nparam, in, out);
1085 if (!dim)
1086 return NULL;
1088 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1089 return bmap;
1092 static void dup_constraints(
1093 struct isl_basic_map *dst, struct isl_basic_map *src)
1095 int i;
1096 unsigned total = isl_basic_map_total_dim(src);
1098 for (i = 0; i < src->n_eq; ++i) {
1099 int j = isl_basic_map_alloc_equality(dst);
1100 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1103 for (i = 0; i < src->n_ineq; ++i) {
1104 int j = isl_basic_map_alloc_inequality(dst);
1105 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1108 for (i = 0; i < src->n_div; ++i) {
1109 int j = isl_basic_map_alloc_div(dst);
1110 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1112 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1115 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1117 struct isl_basic_map *dup;
1119 if (!bmap)
1120 return NULL;
1121 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1122 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1123 if (!dup)
1124 return NULL;
1125 dup_constraints(dup, bmap);
1126 dup->flags = bmap->flags;
1127 dup->sample = isl_vec_copy(bmap->sample);
1128 return dup;
1131 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1133 struct isl_basic_map *dup;
1135 dup = isl_basic_map_dup(bset_to_bmap(bset));
1136 return bset_from_bmap(dup);
1139 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1141 if (!bset)
1142 return NULL;
1144 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1145 bset->ref++;
1146 return bset;
1148 return isl_basic_set_dup(bset);
1151 struct isl_set *isl_set_copy(struct isl_set *set)
1153 if (!set)
1154 return NULL;
1156 set->ref++;
1157 return set;
1160 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1162 if (!bmap)
1163 return NULL;
1165 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1166 bmap->ref++;
1167 return bmap;
1169 bmap = isl_basic_map_dup(bmap);
1170 if (bmap)
1171 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1172 return bmap;
1175 struct isl_map *isl_map_copy(struct isl_map *map)
1177 if (!map)
1178 return NULL;
1180 map->ref++;
1181 return map;
1184 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1186 if (!bmap)
1187 return NULL;
1189 if (--bmap->ref > 0)
1190 return NULL;
1192 isl_ctx_deref(bmap->ctx);
1193 free(bmap->div);
1194 isl_blk_free(bmap->ctx, bmap->block2);
1195 free(bmap->ineq);
1196 isl_blk_free(bmap->ctx, bmap->block);
1197 isl_vec_free(bmap->sample);
1198 isl_space_free(bmap->dim);
1199 free(bmap);
1201 return NULL;
1204 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1206 return isl_basic_map_free(bset_to_bmap(bset));
1209 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1211 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1214 __isl_give isl_map *isl_map_align_params_map_map_and(
1215 __isl_take isl_map *map1, __isl_take isl_map *map2,
1216 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1217 __isl_take isl_map *map2))
1219 if (!map1 || !map2)
1220 goto error;
1221 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1222 return fn(map1, map2);
1223 if (!isl_space_has_named_params(map1->dim) ||
1224 !isl_space_has_named_params(map2->dim))
1225 isl_die(map1->ctx, isl_error_invalid,
1226 "unaligned unnamed parameters", goto error);
1227 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1228 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1229 return fn(map1, map2);
1230 error:
1231 isl_map_free(map1);
1232 isl_map_free(map2);
1233 return NULL;
1236 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1237 __isl_keep isl_map *map2,
1238 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1240 isl_bool r;
1242 if (!map1 || !map2)
1243 return isl_bool_error;
1244 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1245 return fn(map1, map2);
1246 if (!isl_space_has_named_params(map1->dim) ||
1247 !isl_space_has_named_params(map2->dim))
1248 isl_die(map1->ctx, isl_error_invalid,
1249 "unaligned unnamed parameters", return isl_bool_error);
1250 map1 = isl_map_copy(map1);
1251 map2 = isl_map_copy(map2);
1252 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1253 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1254 r = fn(map1, map2);
1255 isl_map_free(map1);
1256 isl_map_free(map2);
1257 return r;
1260 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1262 struct isl_ctx *ctx;
1263 if (!bmap)
1264 return -1;
1265 ctx = bmap->ctx;
1266 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1267 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1268 return -1);
1269 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1274 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1275 isl_int *t;
1276 int j = isl_basic_map_alloc_inequality(bmap);
1277 if (j < 0)
1278 return -1;
1279 t = bmap->ineq[j];
1280 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1281 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1282 bmap->eq[-1] = t;
1283 bmap->n_eq++;
1284 bmap->n_ineq--;
1285 bmap->eq--;
1286 return 0;
1288 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1289 bmap->extra - bmap->n_div);
1290 return bmap->n_eq++;
1293 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1295 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1298 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1300 if (!bmap)
1301 return -1;
1302 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1303 bmap->n_eq -= n;
1304 return 0;
1307 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1309 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1312 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1314 isl_int *t;
1315 if (!bmap)
1316 return -1;
1317 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1319 if (pos != bmap->n_eq - 1) {
1320 t = bmap->eq[pos];
1321 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1322 bmap->eq[bmap->n_eq - 1] = t;
1324 bmap->n_eq--;
1325 return 0;
1328 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1330 return isl_basic_map_drop_equality(bset_to_bmap(bset), pos);
1333 /* Turn inequality "pos" of "bmap" into an equality.
1335 * In particular, we move the inequality in front of the equalities
1336 * and move the last inequality in the position of the moved inequality.
1337 * Note that isl_tab_make_equalities_explicit depends on this particular
1338 * change in the ordering of the constraints.
1340 void isl_basic_map_inequality_to_equality(
1341 struct isl_basic_map *bmap, unsigned pos)
1343 isl_int *t;
1345 t = bmap->ineq[pos];
1346 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1347 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1348 bmap->eq[-1] = t;
1349 bmap->n_eq++;
1350 bmap->n_ineq--;
1351 bmap->eq--;
1352 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1353 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1354 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1355 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1358 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1360 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1363 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1365 struct isl_ctx *ctx;
1366 if (!bmap)
1367 return -1;
1368 ctx = bmap->ctx;
1369 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1370 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1371 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1372 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1373 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1374 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1375 1 + isl_basic_map_total_dim(bmap),
1376 bmap->extra - bmap->n_div);
1377 return bmap->n_ineq++;
1380 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1382 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1385 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1387 if (!bmap)
1388 return -1;
1389 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1390 bmap->n_ineq -= n;
1391 return 0;
1394 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1396 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1399 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1401 isl_int *t;
1402 if (!bmap)
1403 return -1;
1404 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1406 if (pos != bmap->n_ineq - 1) {
1407 t = bmap->ineq[pos];
1408 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1409 bmap->ineq[bmap->n_ineq - 1] = t;
1410 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1412 bmap->n_ineq--;
1413 return 0;
1416 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1418 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1421 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1422 isl_int *eq)
1424 int k;
1426 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1427 if (!bmap)
1428 return NULL;
1429 k = isl_basic_map_alloc_equality(bmap);
1430 if (k < 0)
1431 goto error;
1432 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1433 return bmap;
1434 error:
1435 isl_basic_map_free(bmap);
1436 return NULL;
1439 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1440 isl_int *eq)
1442 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1445 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1446 isl_int *ineq)
1448 int k;
1450 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1451 if (!bmap)
1452 return NULL;
1453 k = isl_basic_map_alloc_inequality(bmap);
1454 if (k < 0)
1455 goto error;
1456 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1457 return bmap;
1458 error:
1459 isl_basic_map_free(bmap);
1460 return NULL;
1463 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1464 isl_int *ineq)
1466 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1469 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1471 if (!bmap)
1472 return -1;
1473 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1474 isl_seq_clr(bmap->div[bmap->n_div] +
1475 1 + 1 + isl_basic_map_total_dim(bmap),
1476 bmap->extra - bmap->n_div);
1477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1478 return bmap->n_div++;
1481 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1483 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1486 /* Check that there are "n" dimensions of type "type" starting at "first"
1487 * in "bmap".
1489 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1490 enum isl_dim_type type, unsigned first, unsigned n)
1492 unsigned dim;
1494 if (!bmap)
1495 return isl_stat_error;
1496 dim = isl_basic_map_dim(bmap, type);
1497 if (first + n > dim || first + n < first)
1498 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1499 "position or range out of bounds",
1500 return isl_stat_error);
1501 return isl_stat_ok;
1504 /* Insert an extra integer division, prescribed by "div", to "bmap"
1505 * at (integer division) position "pos".
1507 * The integer division is first added at the end and then moved
1508 * into the right position.
1510 __isl_give isl_basic_map *isl_basic_map_insert_div(
1511 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1513 int i, k;
1515 bmap = isl_basic_map_cow(bmap);
1516 if (!bmap || !div)
1517 return isl_basic_map_free(bmap);
1519 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1520 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1521 "unexpected size", return isl_basic_map_free(bmap));
1522 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1523 return isl_basic_map_free(bmap);
1525 bmap = isl_basic_map_extend_space(bmap,
1526 isl_basic_map_get_space(bmap), 1, 0, 2);
1527 k = isl_basic_map_alloc_div(bmap);
1528 if (k < 0)
1529 return isl_basic_map_free(bmap);
1530 isl_seq_cpy(bmap->div[k], div->el, div->size);
1531 isl_int_set_si(bmap->div[k][div->size], 0);
1533 for (i = k; i > pos; --i)
1534 isl_basic_map_swap_div(bmap, i, i - 1);
1536 return bmap;
1539 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1541 if (!bmap)
1542 return isl_stat_error;
1543 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1544 bmap->n_div -= n;
1545 return isl_stat_ok;
1548 /* Copy constraint from src to dst, putting the vars of src at offset
1549 * dim_off in dst and the divs of src at offset div_off in dst.
1550 * If both sets are actually map, then dim_off applies to the input
1551 * variables.
1553 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1554 struct isl_basic_map *src_map, isl_int *src,
1555 unsigned in_off, unsigned out_off, unsigned div_off)
1557 unsigned src_nparam = isl_basic_map_n_param(src_map);
1558 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1559 unsigned src_in = isl_basic_map_n_in(src_map);
1560 unsigned dst_in = isl_basic_map_n_in(dst_map);
1561 unsigned src_out = isl_basic_map_n_out(src_map);
1562 unsigned dst_out = isl_basic_map_n_out(dst_map);
1563 isl_int_set(dst[0], src[0]);
1564 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1565 if (dst_nparam > src_nparam)
1566 isl_seq_clr(dst+1+src_nparam,
1567 dst_nparam - src_nparam);
1568 isl_seq_clr(dst+1+dst_nparam, in_off);
1569 isl_seq_cpy(dst+1+dst_nparam+in_off,
1570 src+1+src_nparam,
1571 isl_min(dst_in-in_off, src_in));
1572 if (dst_in-in_off > src_in)
1573 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1574 dst_in - in_off - src_in);
1575 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1576 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1577 src+1+src_nparam+src_in,
1578 isl_min(dst_out-out_off, src_out));
1579 if (dst_out-out_off > src_out)
1580 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1581 dst_out - out_off - src_out);
1582 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1583 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1584 src+1+src_nparam+src_in+src_out,
1585 isl_min(dst_map->extra-div_off, src_map->n_div));
1586 if (dst_map->n_div-div_off > src_map->n_div)
1587 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1588 div_off+src_map->n_div,
1589 dst_map->n_div - div_off - src_map->n_div);
1592 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1593 struct isl_basic_map *src_map, isl_int *src,
1594 unsigned in_off, unsigned out_off, unsigned div_off)
1596 isl_int_set(dst[0], src[0]);
1597 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1600 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1601 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1603 int i;
1604 unsigned div_off;
1606 if (!bmap1 || !bmap2)
1607 goto error;
1609 div_off = bmap1->n_div;
1611 for (i = 0; i < bmap2->n_eq; ++i) {
1612 int i1 = isl_basic_map_alloc_equality(bmap1);
1613 if (i1 < 0)
1614 goto error;
1615 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1616 i_pos, o_pos, div_off);
1619 for (i = 0; i < bmap2->n_ineq; ++i) {
1620 int i1 = isl_basic_map_alloc_inequality(bmap1);
1621 if (i1 < 0)
1622 goto error;
1623 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1624 i_pos, o_pos, div_off);
1627 for (i = 0; i < bmap2->n_div; ++i) {
1628 int i1 = isl_basic_map_alloc_div(bmap1);
1629 if (i1 < 0)
1630 goto error;
1631 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1632 i_pos, o_pos, div_off);
1635 isl_basic_map_free(bmap2);
1637 return bmap1;
1639 error:
1640 isl_basic_map_free(bmap1);
1641 isl_basic_map_free(bmap2);
1642 return NULL;
1645 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1646 struct isl_basic_set *bset2, unsigned pos)
1648 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1649 bset_to_bmap(bset2), 0, pos));
1652 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1653 __isl_take isl_space *dim, unsigned extra,
1654 unsigned n_eq, unsigned n_ineq)
1656 struct isl_basic_map *ext;
1657 unsigned flags;
1658 int dims_ok;
1660 if (!dim)
1661 goto error;
1663 if (!base)
1664 goto error;
1666 dims_ok = isl_space_is_equal(base->dim, dim) &&
1667 base->extra >= base->n_div + extra;
1669 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1670 room_for_ineq(base, n_ineq)) {
1671 isl_space_free(dim);
1672 return base;
1675 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1676 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1677 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1678 extra += base->extra;
1679 n_eq += base->n_eq;
1680 n_ineq += base->n_ineq;
1682 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1683 dim = NULL;
1684 if (!ext)
1685 goto error;
1687 if (dims_ok)
1688 ext->sample = isl_vec_copy(base->sample);
1689 flags = base->flags;
1690 ext = add_constraints(ext, base, 0, 0);
1691 if (ext) {
1692 ext->flags = flags;
1693 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1696 return ext;
1698 error:
1699 isl_space_free(dim);
1700 isl_basic_map_free(base);
1701 return NULL;
1704 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1705 __isl_take isl_space *dim, unsigned extra,
1706 unsigned n_eq, unsigned n_ineq)
1708 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1709 dim, extra, n_eq, n_ineq));
1712 struct isl_basic_map *isl_basic_map_extend_constraints(
1713 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1715 if (!base)
1716 return NULL;
1717 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1718 0, n_eq, n_ineq);
1721 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1722 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1723 unsigned n_eq, unsigned n_ineq)
1725 struct isl_basic_map *bmap;
1726 isl_space *dim;
1728 if (!base)
1729 return NULL;
1730 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1731 if (!dim)
1732 goto error;
1734 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1735 return bmap;
1736 error:
1737 isl_basic_map_free(base);
1738 return NULL;
1741 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1742 unsigned nparam, unsigned dim, unsigned extra,
1743 unsigned n_eq, unsigned n_ineq)
1745 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1746 nparam, 0, dim, extra, n_eq, n_ineq));
1749 struct isl_basic_set *isl_basic_set_extend_constraints(
1750 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1752 isl_basic_map *bmap = bset_to_bmap(base);
1753 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1754 return bset_from_bmap(bmap);
1757 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1759 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1762 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1764 if (!bmap)
1765 return NULL;
1767 if (bmap->ref > 1) {
1768 bmap->ref--;
1769 bmap = isl_basic_map_dup(bmap);
1771 if (bmap) {
1772 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1773 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1775 return bmap;
1778 /* Clear all cached information in "map", either because it is about
1779 * to be modified or because it is being freed.
1780 * Always return the same pointer that is passed in.
1781 * This is needed for the use in isl_map_free.
1783 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1785 isl_basic_map_free(map->cached_simple_hull[0]);
1786 isl_basic_map_free(map->cached_simple_hull[1]);
1787 map->cached_simple_hull[0] = NULL;
1788 map->cached_simple_hull[1] = NULL;
1789 return map;
1792 struct isl_set *isl_set_cow(struct isl_set *set)
1794 return isl_map_cow(set);
1797 /* Return an isl_map that is equal to "map" and that has only
1798 * a single reference.
1800 * If the original input already has only one reference, then
1801 * simply return it, but clear all cached information, since
1802 * it may be rendered invalid by the operations that will be
1803 * performed on the result.
1805 * Otherwise, create a duplicate (without any cached information).
1807 struct isl_map *isl_map_cow(struct isl_map *map)
1809 if (!map)
1810 return NULL;
1812 if (map->ref == 1)
1813 return clear_caches(map);
1814 map->ref--;
1815 return isl_map_dup(map);
1818 static void swap_vars(struct isl_blk blk, isl_int *a,
1819 unsigned a_len, unsigned b_len)
1821 isl_seq_cpy(blk.data, a+a_len, b_len);
1822 isl_seq_cpy(blk.data+b_len, a, a_len);
1823 isl_seq_cpy(a, blk.data, b_len+a_len);
1826 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1827 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1829 int i;
1830 struct isl_blk blk;
1832 if (!bmap)
1833 goto error;
1835 isl_assert(bmap->ctx,
1836 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1838 if (n1 == 0 || n2 == 0)
1839 return bmap;
1841 bmap = isl_basic_map_cow(bmap);
1842 if (!bmap)
1843 return NULL;
1845 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1846 if (isl_blk_is_error(blk))
1847 goto error;
1849 for (i = 0; i < bmap->n_eq; ++i)
1850 swap_vars(blk,
1851 bmap->eq[i] + pos, n1, n2);
1853 for (i = 0; i < bmap->n_ineq; ++i)
1854 swap_vars(blk,
1855 bmap->ineq[i] + pos, n1, n2);
1857 for (i = 0; i < bmap->n_div; ++i)
1858 swap_vars(blk,
1859 bmap->div[i]+1 + pos, n1, n2);
1861 isl_blk_free(bmap->ctx, blk);
1863 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1864 bmap = isl_basic_map_gauss(bmap, NULL);
1865 return isl_basic_map_finalize(bmap);
1866 error:
1867 isl_basic_map_free(bmap);
1868 return NULL;
1871 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1873 int i = 0;
1874 unsigned total;
1875 if (!bmap)
1876 goto error;
1877 total = isl_basic_map_total_dim(bmap);
1878 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1879 return isl_basic_map_free(bmap);
1880 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1881 if (bmap->n_eq > 0)
1882 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1883 else {
1884 i = isl_basic_map_alloc_equality(bmap);
1885 if (i < 0)
1886 goto error;
1888 isl_int_set_si(bmap->eq[i][0], 1);
1889 isl_seq_clr(bmap->eq[i]+1, total);
1890 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1891 isl_vec_free(bmap->sample);
1892 bmap->sample = NULL;
1893 return isl_basic_map_finalize(bmap);
1894 error:
1895 isl_basic_map_free(bmap);
1896 return NULL;
1899 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1901 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1904 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1905 * of "bmap").
1907 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1909 isl_int *t = bmap->div[a];
1910 bmap->div[a] = bmap->div[b];
1911 bmap->div[b] = t;
1914 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1915 * div definitions accordingly.
1917 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1919 int i;
1920 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1922 swap_div(bmap, a, b);
1924 for (i = 0; i < bmap->n_eq; ++i)
1925 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1927 for (i = 0; i < bmap->n_ineq; ++i)
1928 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1930 for (i = 0; i < bmap->n_div; ++i)
1931 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1932 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1935 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1936 * div definitions accordingly.
1938 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1940 isl_basic_map_swap_div(bset, a, b);
1943 /* Eliminate the specified n dimensions starting at first from the
1944 * constraints, without removing the dimensions from the space.
1945 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1947 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1948 enum isl_dim_type type, unsigned first, unsigned n)
1950 int i;
1952 if (!map)
1953 return NULL;
1954 if (n == 0)
1955 return map;
1957 if (first + n > isl_map_dim(map, type) || first + n < first)
1958 isl_die(map->ctx, isl_error_invalid,
1959 "index out of bounds", goto error);
1961 map = isl_map_cow(map);
1962 if (!map)
1963 return NULL;
1965 for (i = 0; i < map->n; ++i) {
1966 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1967 if (!map->p[i])
1968 goto error;
1970 return map;
1971 error:
1972 isl_map_free(map);
1973 return NULL;
1976 /* Eliminate the specified n dimensions starting at first from the
1977 * constraints, without removing the dimensions from the space.
1978 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1980 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1981 enum isl_dim_type type, unsigned first, unsigned n)
1983 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
1986 /* Eliminate the specified n dimensions starting at first from the
1987 * constraints, without removing the dimensions from the space.
1988 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1990 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1991 unsigned first, unsigned n)
1993 return isl_set_eliminate(set, isl_dim_set, first, n);
1996 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1997 __isl_take isl_basic_map *bmap)
1999 if (!bmap)
2000 return NULL;
2001 bmap = isl_basic_map_eliminate_vars(bmap,
2002 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2003 if (!bmap)
2004 return NULL;
2005 bmap->n_div = 0;
2006 return isl_basic_map_finalize(bmap);
2009 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2010 __isl_take isl_basic_set *bset)
2012 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2015 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2017 int i;
2019 if (!map)
2020 return NULL;
2021 if (map->n == 0)
2022 return map;
2024 map = isl_map_cow(map);
2025 if (!map)
2026 return NULL;
2028 for (i = 0; i < map->n; ++i) {
2029 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2030 if (!map->p[i])
2031 goto error;
2033 return map;
2034 error:
2035 isl_map_free(map);
2036 return NULL;
2039 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2041 return isl_map_remove_divs(set);
2044 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2045 enum isl_dim_type type, unsigned first, unsigned n)
2047 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2048 return isl_basic_map_free(bmap);
2049 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2050 return bmap;
2051 bmap = isl_basic_map_eliminate_vars(bmap,
2052 isl_basic_map_offset(bmap, type) - 1 + first, n);
2053 if (!bmap)
2054 return bmap;
2055 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2056 return bmap;
2057 bmap = isl_basic_map_drop(bmap, type, first, n);
2058 return bmap;
2061 /* Return true if the definition of the given div (recursively) involves
2062 * any of the given variables.
2064 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2065 unsigned first, unsigned n)
2067 int i;
2068 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2070 if (isl_int_is_zero(bmap->div[div][0]))
2071 return isl_bool_false;
2072 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2073 return isl_bool_true;
2075 for (i = bmap->n_div - 1; i >= 0; --i) {
2076 isl_bool involves;
2078 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2079 continue;
2080 involves = div_involves_vars(bmap, i, first, n);
2081 if (involves < 0 || involves)
2082 return involves;
2085 return isl_bool_false;
2088 /* Try and add a lower and/or upper bound on "div" to "bmap"
2089 * based on inequality "i".
2090 * "total" is the total number of variables (excluding the divs).
2091 * "v" is a temporary object that can be used during the calculations.
2092 * If "lb" is set, then a lower bound should be constructed.
2093 * If "ub" is set, then an upper bound should be constructed.
2095 * The calling function has already checked that the inequality does not
2096 * reference "div", but we still need to check that the inequality is
2097 * of the right form. We'll consider the case where we want to construct
2098 * a lower bound. The construction of upper bounds is similar.
2100 * Let "div" be of the form
2102 * q = floor((a + f(x))/d)
2104 * We essentially check if constraint "i" is of the form
2106 * b + f(x) >= 0
2108 * so that we can use it to derive a lower bound on "div".
2109 * However, we allow a slightly more general form
2111 * b + g(x) >= 0
2113 * with the condition that the coefficients of g(x) - f(x) are all
2114 * divisible by d.
2115 * Rewriting this constraint as
2117 * 0 >= -b - g(x)
2119 * adding a + f(x) to both sides and dividing by d, we obtain
2121 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2123 * Taking the floor on both sides, we obtain
2125 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2127 * or
2129 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2131 * In the case of an upper bound, we construct the constraint
2133 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2136 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2137 __isl_take isl_basic_map *bmap, int div, int i,
2138 unsigned total, isl_int v, int lb, int ub)
2140 int j;
2142 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2143 if (lb) {
2144 isl_int_sub(v, bmap->ineq[i][1 + j],
2145 bmap->div[div][1 + 1 + j]);
2146 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2148 if (ub) {
2149 isl_int_add(v, bmap->ineq[i][1 + j],
2150 bmap->div[div][1 + 1 + j]);
2151 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2154 if (!lb && !ub)
2155 return bmap;
2157 bmap = isl_basic_map_cow(bmap);
2158 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2159 if (lb) {
2160 int k = isl_basic_map_alloc_inequality(bmap);
2161 if (k < 0)
2162 goto error;
2163 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2164 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2165 bmap->div[div][1 + j]);
2166 isl_int_cdiv_q(bmap->ineq[k][j],
2167 bmap->ineq[k][j], bmap->div[div][0]);
2169 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2171 if (ub) {
2172 int k = isl_basic_map_alloc_inequality(bmap);
2173 if (k < 0)
2174 goto error;
2175 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2176 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2177 bmap->div[div][1 + j]);
2178 isl_int_fdiv_q(bmap->ineq[k][j],
2179 bmap->ineq[k][j], bmap->div[div][0]);
2181 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2184 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2185 return bmap;
2186 error:
2187 isl_basic_map_free(bmap);
2188 return NULL;
2191 /* This function is called right before "div" is eliminated from "bmap"
2192 * using Fourier-Motzkin.
2193 * Look through the constraints of "bmap" for constraints on the argument
2194 * of the integer division and use them to construct constraints on the
2195 * integer division itself. These constraints can then be combined
2196 * during the Fourier-Motzkin elimination.
2197 * Note that it is only useful to introduce lower bounds on "div"
2198 * if "bmap" already contains upper bounds on "div" as the newly
2199 * introduce lower bounds can then be combined with the pre-existing
2200 * upper bounds. Similarly for upper bounds.
2201 * We therefore first check if "bmap" contains any lower and/or upper bounds
2202 * on "div".
2204 * It is interesting to note that the introduction of these constraints
2205 * can indeed lead to more accurate results, even when compared to
2206 * deriving constraints on the argument of "div" from constraints on "div".
2207 * Consider, for example, the set
2209 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2211 * The second constraint can be rewritten as
2213 * 2 * [(-i-2j+3)/4] + k >= 0
2215 * from which we can derive
2217 * -i - 2j + 3 >= -2k
2219 * or
2221 * i + 2j <= 3 + 2k
2223 * Combined with the first constraint, we obtain
2225 * -3 <= 3 + 2k or k >= -3
2227 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2228 * the first constraint, we obtain
2230 * [(i + 2j)/4] >= [-3/4] = -1
2232 * Combining this constraint with the second constraint, we obtain
2234 * k >= -2
2236 static __isl_give isl_basic_map *insert_bounds_on_div(
2237 __isl_take isl_basic_map *bmap, int div)
2239 int i;
2240 int check_lb, check_ub;
2241 isl_int v;
2242 unsigned total;
2244 if (!bmap)
2245 return NULL;
2247 if (isl_int_is_zero(bmap->div[div][0]))
2248 return bmap;
2250 total = isl_space_dim(bmap->dim, isl_dim_all);
2252 check_lb = 0;
2253 check_ub = 0;
2254 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2255 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2256 if (s > 0)
2257 check_ub = 1;
2258 if (s < 0)
2259 check_lb = 1;
2262 if (!check_lb && !check_ub)
2263 return bmap;
2265 isl_int_init(v);
2267 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2268 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2269 continue;
2271 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2272 check_lb, check_ub);
2275 isl_int_clear(v);
2277 return bmap;
2280 /* Remove all divs (recursively) involving any of the given dimensions
2281 * in their definitions.
2283 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2284 __isl_take isl_basic_map *bmap,
2285 enum isl_dim_type type, unsigned first, unsigned n)
2287 int i;
2289 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2290 return isl_basic_map_free(bmap);
2291 first += isl_basic_map_offset(bmap, type);
2293 for (i = bmap->n_div - 1; i >= 0; --i) {
2294 isl_bool involves;
2296 involves = div_involves_vars(bmap, i, first, n);
2297 if (involves < 0)
2298 return isl_basic_map_free(bmap);
2299 if (!involves)
2300 continue;
2301 bmap = insert_bounds_on_div(bmap, i);
2302 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2303 if (!bmap)
2304 return NULL;
2305 i = bmap->n_div;
2308 return bmap;
2311 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2312 __isl_take isl_basic_set *bset,
2313 enum isl_dim_type type, unsigned first, unsigned n)
2315 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2318 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2319 enum isl_dim_type type, unsigned first, unsigned n)
2321 int i;
2323 if (!map)
2324 return NULL;
2325 if (map->n == 0)
2326 return map;
2328 map = isl_map_cow(map);
2329 if (!map)
2330 return NULL;
2332 for (i = 0; i < map->n; ++i) {
2333 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2334 type, first, n);
2335 if (!map->p[i])
2336 goto error;
2338 return map;
2339 error:
2340 isl_map_free(map);
2341 return NULL;
2344 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2345 enum isl_dim_type type, unsigned first, unsigned n)
2347 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2348 type, first, n));
2351 /* Does the description of "bmap" depend on the specified dimensions?
2352 * We also check whether the dimensions appear in any of the div definitions.
2353 * In principle there is no need for this check. If the dimensions appear
2354 * in a div definition, they also appear in the defining constraints of that
2355 * div.
2357 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2358 enum isl_dim_type type, unsigned first, unsigned n)
2360 int i;
2362 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2363 return isl_bool_error;
2365 first += isl_basic_map_offset(bmap, type);
2366 for (i = 0; i < bmap->n_eq; ++i)
2367 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2368 return isl_bool_true;
2369 for (i = 0; i < bmap->n_ineq; ++i)
2370 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2371 return isl_bool_true;
2372 for (i = 0; i < bmap->n_div; ++i) {
2373 if (isl_int_is_zero(bmap->div[i][0]))
2374 continue;
2375 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2376 return isl_bool_true;
2379 return isl_bool_false;
2382 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2383 enum isl_dim_type type, unsigned first, unsigned n)
2385 int i;
2387 if (!map)
2388 return isl_bool_error;
2390 if (first + n > isl_map_dim(map, type))
2391 isl_die(map->ctx, isl_error_invalid,
2392 "index out of bounds", return isl_bool_error);
2394 for (i = 0; i < map->n; ++i) {
2395 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2396 type, first, n);
2397 if (involves < 0 || involves)
2398 return involves;
2401 return isl_bool_false;
2404 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2405 enum isl_dim_type type, unsigned first, unsigned n)
2407 return isl_basic_map_involves_dims(bset, type, first, n);
2410 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2411 enum isl_dim_type type, unsigned first, unsigned n)
2413 return isl_map_involves_dims(set, type, first, n);
2416 /* Drop all constraints in bmap that involve any of the dimensions
2417 * first to first+n-1.
2419 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2420 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2422 int i;
2424 if (n == 0)
2425 return bmap;
2427 bmap = isl_basic_map_cow(bmap);
2429 if (!bmap)
2430 return NULL;
2432 for (i = bmap->n_eq - 1; i >= 0; --i) {
2433 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2434 continue;
2435 isl_basic_map_drop_equality(bmap, i);
2438 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2439 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2440 continue;
2441 isl_basic_map_drop_inequality(bmap, i);
2444 bmap = isl_basic_map_add_known_div_constraints(bmap);
2445 return bmap;
2448 /* Drop all constraints in bset that involve any of the dimensions
2449 * first to first+n-1.
2451 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2452 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2454 return isl_basic_map_drop_constraints_involving(bset, first, n);
2457 /* Drop all constraints in bmap that do not involve any of the dimensions
2458 * first to first + n - 1 of the given type.
2460 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2461 __isl_take isl_basic_map *bmap,
2462 enum isl_dim_type type, unsigned first, unsigned n)
2464 int i;
2466 if (n == 0) {
2467 isl_space *space = isl_basic_map_get_space(bmap);
2468 isl_basic_map_free(bmap);
2469 return isl_basic_map_universe(space);
2471 bmap = isl_basic_map_cow(bmap);
2472 if (!bmap)
2473 return NULL;
2475 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2476 return isl_basic_map_free(bmap);
2478 first += isl_basic_map_offset(bmap, type) - 1;
2480 for (i = bmap->n_eq - 1; i >= 0; --i) {
2481 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2482 continue;
2483 isl_basic_map_drop_equality(bmap, i);
2486 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2487 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2488 continue;
2489 isl_basic_map_drop_inequality(bmap, i);
2492 bmap = isl_basic_map_add_known_div_constraints(bmap);
2493 return bmap;
2496 /* Drop all constraints in bset that do not involve any of the dimensions
2497 * first to first + n - 1 of the given type.
2499 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2500 __isl_take isl_basic_set *bset,
2501 enum isl_dim_type type, unsigned first, unsigned n)
2503 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2504 type, first, n);
2507 /* Drop all constraints in bmap that involve any of the dimensions
2508 * first to first + n - 1 of the given type.
2510 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2511 __isl_take isl_basic_map *bmap,
2512 enum isl_dim_type type, unsigned first, unsigned n)
2514 if (!bmap)
2515 return NULL;
2516 if (n == 0)
2517 return bmap;
2519 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2520 return isl_basic_map_free(bmap);
2522 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2523 first += isl_basic_map_offset(bmap, type) - 1;
2524 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2527 /* Drop all constraints in bset that involve any of the dimensions
2528 * first to first + n - 1 of the given type.
2530 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2531 __isl_take isl_basic_set *bset,
2532 enum isl_dim_type type, unsigned first, unsigned n)
2534 return isl_basic_map_drop_constraints_involving_dims(bset,
2535 type, first, n);
2538 /* Drop constraints from "map" by applying "drop" to each basic map.
2540 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2541 enum isl_dim_type type, unsigned first, unsigned n,
2542 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2543 enum isl_dim_type type, unsigned first, unsigned n))
2545 int i;
2546 unsigned dim;
2548 if (!map)
2549 return NULL;
2551 dim = isl_map_dim(map, type);
2552 if (first + n > dim || first + n < first)
2553 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2554 "index out of bounds", return isl_map_free(map));
2556 map = isl_map_cow(map);
2557 if (!map)
2558 return NULL;
2560 for (i = 0; i < map->n; ++i) {
2561 map->p[i] = drop(map->p[i], type, first, n);
2562 if (!map->p[i])
2563 return isl_map_free(map);
2566 if (map->n > 1)
2567 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2569 return map;
2572 /* Drop all constraints in map that involve any of the dimensions
2573 * first to first + n - 1 of the given type.
2575 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2576 __isl_take isl_map *map,
2577 enum isl_dim_type type, unsigned first, unsigned n)
2579 if (n == 0)
2580 return map;
2581 return drop_constraints(map, type, first, n,
2582 &isl_basic_map_drop_constraints_involving_dims);
2585 /* Drop all constraints in "map" that do not involve any of the dimensions
2586 * first to first + n - 1 of the given type.
2588 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2589 __isl_take isl_map *map,
2590 enum isl_dim_type type, unsigned first, unsigned n)
2592 if (n == 0) {
2593 isl_space *space = isl_map_get_space(map);
2594 isl_map_free(map);
2595 return isl_map_universe(space);
2597 return drop_constraints(map, type, first, n,
2598 &isl_basic_map_drop_constraints_not_involving_dims);
2601 /* Drop all constraints in set that involve any of the dimensions
2602 * first to first + n - 1 of the given type.
2604 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2605 __isl_take isl_set *set,
2606 enum isl_dim_type type, unsigned first, unsigned n)
2608 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2611 /* Drop all constraints in "set" that do not involve any of the dimensions
2612 * first to first + n - 1 of the given type.
2614 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2615 __isl_take isl_set *set,
2616 enum isl_dim_type type, unsigned first, unsigned n)
2618 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2621 /* Does local variable "div" of "bmap" have a complete explicit representation?
2622 * Having a complete explicit representation requires not only
2623 * an explicit representation, but also that all local variables
2624 * that appear in this explicit representation in turn have
2625 * a complete explicit representation.
2627 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2629 int i;
2630 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2631 isl_bool marked;
2633 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2634 if (marked < 0 || marked)
2635 return isl_bool_not(marked);
2637 for (i = bmap->n_div - 1; i >= 0; --i) {
2638 isl_bool known;
2640 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2641 continue;
2642 known = isl_basic_map_div_is_known(bmap, i);
2643 if (known < 0 || !known)
2644 return known;
2647 return isl_bool_true;
2650 /* Remove all divs that are unknown or defined in terms of unknown divs.
2652 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2653 __isl_take isl_basic_map *bmap)
2655 int i;
2657 if (!bmap)
2658 return NULL;
2660 for (i = bmap->n_div - 1; i >= 0; --i) {
2661 if (isl_basic_map_div_is_known(bmap, i))
2662 continue;
2663 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2664 if (!bmap)
2665 return NULL;
2666 i = bmap->n_div;
2669 return bmap;
2672 /* Remove all divs that are unknown or defined in terms of unknown divs.
2674 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2675 __isl_take isl_basic_set *bset)
2677 return isl_basic_map_remove_unknown_divs(bset);
2680 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2682 int i;
2684 if (!map)
2685 return NULL;
2686 if (map->n == 0)
2687 return map;
2689 map = isl_map_cow(map);
2690 if (!map)
2691 return NULL;
2693 for (i = 0; i < map->n; ++i) {
2694 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2695 if (!map->p[i])
2696 goto error;
2698 return map;
2699 error:
2700 isl_map_free(map);
2701 return NULL;
2704 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2706 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2709 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2710 __isl_take isl_basic_set *bset,
2711 enum isl_dim_type type, unsigned first, unsigned n)
2713 isl_basic_map *bmap = bset_to_bmap(bset);
2714 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2715 return bset_from_bmap(bmap);
2718 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2719 enum isl_dim_type type, unsigned first, unsigned n)
2721 int i;
2723 if (n == 0)
2724 return map;
2726 map = isl_map_cow(map);
2727 if (!map)
2728 return NULL;
2729 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2731 for (i = 0; i < map->n; ++i) {
2732 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2733 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2734 if (!map->p[i])
2735 goto error;
2737 map = isl_map_drop(map, type, first, n);
2738 return map;
2739 error:
2740 isl_map_free(map);
2741 return NULL;
2744 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2745 enum isl_dim_type type, unsigned first, unsigned n)
2747 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2748 type, first, n));
2751 /* Project out n inputs starting at first using Fourier-Motzkin */
2752 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2753 unsigned first, unsigned n)
2755 return isl_map_remove_dims(map, isl_dim_in, first, n);
2758 static void dump_term(struct isl_basic_map *bmap,
2759 isl_int c, int pos, FILE *out)
2761 const char *name;
2762 unsigned in = isl_basic_map_n_in(bmap);
2763 unsigned dim = in + isl_basic_map_n_out(bmap);
2764 unsigned nparam = isl_basic_map_n_param(bmap);
2765 if (!pos)
2766 isl_int_print(out, c, 0);
2767 else {
2768 if (!isl_int_is_one(c))
2769 isl_int_print(out, c, 0);
2770 if (pos < 1 + nparam) {
2771 name = isl_space_get_dim_name(bmap->dim,
2772 isl_dim_param, pos - 1);
2773 if (name)
2774 fprintf(out, "%s", name);
2775 else
2776 fprintf(out, "p%d", pos - 1);
2777 } else if (pos < 1 + nparam + in)
2778 fprintf(out, "i%d", pos - 1 - nparam);
2779 else if (pos < 1 + nparam + dim)
2780 fprintf(out, "o%d", pos - 1 - nparam - in);
2781 else
2782 fprintf(out, "e%d", pos - 1 - nparam - dim);
2786 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2787 int sign, FILE *out)
2789 int i;
2790 int first;
2791 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2792 isl_int v;
2794 isl_int_init(v);
2795 for (i = 0, first = 1; i < len; ++i) {
2796 if (isl_int_sgn(c[i]) * sign <= 0)
2797 continue;
2798 if (!first)
2799 fprintf(out, " + ");
2800 first = 0;
2801 isl_int_abs(v, c[i]);
2802 dump_term(bmap, v, i, out);
2804 isl_int_clear(v);
2805 if (first)
2806 fprintf(out, "0");
2809 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2810 const char *op, FILE *out, int indent)
2812 int i;
2814 fprintf(out, "%*s", indent, "");
2816 dump_constraint_sign(bmap, c, 1, out);
2817 fprintf(out, " %s ", op);
2818 dump_constraint_sign(bmap, c, -1, out);
2820 fprintf(out, "\n");
2822 for (i = bmap->n_div; i < bmap->extra; ++i) {
2823 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2824 continue;
2825 fprintf(out, "%*s", indent, "");
2826 fprintf(out, "ERROR: unused div coefficient not zero\n");
2827 abort();
2831 static void dump_constraints(struct isl_basic_map *bmap,
2832 isl_int **c, unsigned n,
2833 const char *op, FILE *out, int indent)
2835 int i;
2837 for (i = 0; i < n; ++i)
2838 dump_constraint(bmap, c[i], op, out, indent);
2841 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2843 int j;
2844 int first = 1;
2845 unsigned total = isl_basic_map_total_dim(bmap);
2847 for (j = 0; j < 1 + total; ++j) {
2848 if (isl_int_is_zero(exp[j]))
2849 continue;
2850 if (!first && isl_int_is_pos(exp[j]))
2851 fprintf(out, "+");
2852 dump_term(bmap, exp[j], j, out);
2853 first = 0;
2857 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2859 int i;
2861 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2862 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2864 for (i = 0; i < bmap->n_div; ++i) {
2865 fprintf(out, "%*s", indent, "");
2866 fprintf(out, "e%d = [(", i);
2867 dump_affine(bmap, bmap->div[i]+1, out);
2868 fprintf(out, ")/");
2869 isl_int_print(out, bmap->div[i][0], 0);
2870 fprintf(out, "]\n");
2874 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2875 FILE *out, int indent)
2877 if (!bset) {
2878 fprintf(out, "null basic set\n");
2879 return;
2882 fprintf(out, "%*s", indent, "");
2883 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2884 bset->ref, bset->dim->nparam, bset->dim->n_out,
2885 bset->extra, bset->flags);
2886 dump(bset_to_bmap(bset), out, indent);
2889 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2890 FILE *out, int indent)
2892 if (!bmap) {
2893 fprintf(out, "null basic map\n");
2894 return;
2897 fprintf(out, "%*s", indent, "");
2898 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2899 "flags: %x, n_name: %d\n",
2900 bmap->ref,
2901 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2902 bmap->extra, bmap->flags, bmap->dim->n_id);
2903 dump(bmap, out, indent);
2906 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2908 unsigned total;
2909 if (!bmap)
2910 return -1;
2911 total = isl_basic_map_total_dim(bmap);
2912 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2913 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2914 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2915 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2916 return 0;
2919 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2920 unsigned flags)
2922 if (!space)
2923 return NULL;
2924 if (isl_space_dim(space, isl_dim_in) != 0)
2925 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2926 "set cannot have input dimensions", goto error);
2927 return isl_map_alloc_space(space, n, flags);
2928 error:
2929 isl_space_free(space);
2930 return NULL;
2933 /* Make sure "map" has room for at least "n" more basic maps.
2935 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2937 int i;
2938 struct isl_map *grown = NULL;
2940 if (!map)
2941 return NULL;
2942 isl_assert(map->ctx, n >= 0, goto error);
2943 if (map->n + n <= map->size)
2944 return map;
2945 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2946 if (!grown)
2947 goto error;
2948 for (i = 0; i < map->n; ++i) {
2949 grown->p[i] = isl_basic_map_copy(map->p[i]);
2950 if (!grown->p[i])
2951 goto error;
2952 grown->n++;
2954 isl_map_free(map);
2955 return grown;
2956 error:
2957 isl_map_free(grown);
2958 isl_map_free(map);
2959 return NULL;
2962 /* Make sure "set" has room for at least "n" more basic sets.
2964 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2966 return set_from_map(isl_map_grow(set_to_map(set), n));
2969 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2971 return isl_map_from_basic_map(bset);
2974 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2976 struct isl_map *map;
2978 if (!bmap)
2979 return NULL;
2981 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2982 return isl_map_add_basic_map(map, bmap);
2985 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2986 __isl_take isl_basic_set *bset)
2988 return set_from_map(isl_map_add_basic_map(set_to_map(set),
2989 bset_to_bmap(bset)));
2992 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2994 return isl_map_free(set);
2997 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2999 int i;
3001 if (!set) {
3002 fprintf(out, "null set\n");
3003 return;
3006 fprintf(out, "%*s", indent, "");
3007 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3008 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3009 set->flags);
3010 for (i = 0; i < set->n; ++i) {
3011 fprintf(out, "%*s", indent, "");
3012 fprintf(out, "basic set %d:\n", i);
3013 isl_basic_set_print_internal(set->p[i], out, indent+4);
3017 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3019 int i;
3021 if (!map) {
3022 fprintf(out, "null map\n");
3023 return;
3026 fprintf(out, "%*s", indent, "");
3027 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3028 "flags: %x, n_name: %d\n",
3029 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3030 map->dim->n_out, map->flags, map->dim->n_id);
3031 for (i = 0; i < map->n; ++i) {
3032 fprintf(out, "%*s", indent, "");
3033 fprintf(out, "basic map %d:\n", i);
3034 isl_basic_map_print_internal(map->p[i], out, indent+4);
3038 struct isl_basic_map *isl_basic_map_intersect_domain(
3039 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3041 struct isl_basic_map *bmap_domain;
3043 if (!bmap || !bset)
3044 goto error;
3046 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3047 bset->dim, isl_dim_param), goto error);
3049 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3050 isl_assert(bset->ctx,
3051 isl_basic_map_compatible_domain(bmap, bset), goto error);
3053 bmap = isl_basic_map_cow(bmap);
3054 if (!bmap)
3055 goto error;
3056 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3057 bset->n_div, bset->n_eq, bset->n_ineq);
3058 bmap_domain = isl_basic_map_from_domain(bset);
3059 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3061 bmap = isl_basic_map_simplify(bmap);
3062 return isl_basic_map_finalize(bmap);
3063 error:
3064 isl_basic_map_free(bmap);
3065 isl_basic_set_free(bset);
3066 return NULL;
3069 /* Check that the space of "bset" is the same as that of the range of "bmap".
3071 static isl_stat isl_basic_map_check_compatible_range(
3072 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3074 isl_bool ok;
3076 ok = isl_basic_map_compatible_range(bmap, bset);
3077 if (ok < 0)
3078 return isl_stat_error;
3079 if (!ok)
3080 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3081 "incompatible spaces", return isl_stat_error);
3083 return isl_stat_ok;
3086 struct isl_basic_map *isl_basic_map_intersect_range(
3087 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3089 struct isl_basic_map *bmap_range;
3091 if (!bmap || !bset)
3092 goto error;
3094 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3095 bset->dim, isl_dim_param), goto error);
3097 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3098 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3099 goto error;
3101 if (isl_basic_set_plain_is_universe(bset)) {
3102 isl_basic_set_free(bset);
3103 return bmap;
3106 bmap = isl_basic_map_cow(bmap);
3107 if (!bmap)
3108 goto error;
3109 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3110 bset->n_div, bset->n_eq, bset->n_ineq);
3111 bmap_range = bset_to_bmap(bset);
3112 bmap = add_constraints(bmap, bmap_range, 0, 0);
3114 bmap = isl_basic_map_simplify(bmap);
3115 return isl_basic_map_finalize(bmap);
3116 error:
3117 isl_basic_map_free(bmap);
3118 isl_basic_set_free(bset);
3119 return NULL;
3122 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3123 __isl_keep isl_vec *vec)
3125 int i;
3126 unsigned total;
3127 isl_int s;
3129 if (!bmap || !vec)
3130 return isl_bool_error;
3132 total = 1 + isl_basic_map_total_dim(bmap);
3133 if (total != vec->size)
3134 return isl_bool_false;
3136 isl_int_init(s);
3138 for (i = 0; i < bmap->n_eq; ++i) {
3139 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3140 if (!isl_int_is_zero(s)) {
3141 isl_int_clear(s);
3142 return isl_bool_false;
3146 for (i = 0; i < bmap->n_ineq; ++i) {
3147 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3148 if (isl_int_is_neg(s)) {
3149 isl_int_clear(s);
3150 return isl_bool_false;
3154 isl_int_clear(s);
3156 return isl_bool_true;
3159 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3160 __isl_keep isl_vec *vec)
3162 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3165 struct isl_basic_map *isl_basic_map_intersect(
3166 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3168 struct isl_vec *sample = NULL;
3170 if (!bmap1 || !bmap2)
3171 goto error;
3173 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
3174 bmap2->dim, isl_dim_param), goto error);
3175 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3176 isl_space_dim(bmap1->dim, isl_dim_param) &&
3177 isl_space_dim(bmap2->dim, isl_dim_all) !=
3178 isl_space_dim(bmap2->dim, isl_dim_param))
3179 return isl_basic_map_intersect(bmap2, bmap1);
3181 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3182 isl_space_dim(bmap2->dim, isl_dim_param))
3183 isl_assert(bmap1->ctx,
3184 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3186 if (isl_basic_map_plain_is_empty(bmap1)) {
3187 isl_basic_map_free(bmap2);
3188 return bmap1;
3190 if (isl_basic_map_plain_is_empty(bmap2)) {
3191 isl_basic_map_free(bmap1);
3192 return bmap2;
3195 if (bmap1->sample &&
3196 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3197 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3198 sample = isl_vec_copy(bmap1->sample);
3199 else if (bmap2->sample &&
3200 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3201 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3202 sample = isl_vec_copy(bmap2->sample);
3204 bmap1 = isl_basic_map_cow(bmap1);
3205 if (!bmap1)
3206 goto error;
3207 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3208 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3209 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3211 if (!bmap1)
3212 isl_vec_free(sample);
3213 else if (sample) {
3214 isl_vec_free(bmap1->sample);
3215 bmap1->sample = sample;
3218 bmap1 = isl_basic_map_simplify(bmap1);
3219 return isl_basic_map_finalize(bmap1);
3220 error:
3221 if (sample)
3222 isl_vec_free(sample);
3223 isl_basic_map_free(bmap1);
3224 isl_basic_map_free(bmap2);
3225 return NULL;
3228 struct isl_basic_set *isl_basic_set_intersect(
3229 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3231 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3232 bset_to_bmap(bset2)));
3235 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3236 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3238 return isl_basic_set_intersect(bset1, bset2);
3241 /* Special case of isl_map_intersect, where both map1 and map2
3242 * are convex, without any divs and such that either map1 or map2
3243 * contains a single constraint. This constraint is then simply
3244 * added to the other map.
3246 static __isl_give isl_map *map_intersect_add_constraint(
3247 __isl_take isl_map *map1, __isl_take isl_map *map2)
3249 isl_assert(map1->ctx, map1->n == 1, goto error);
3250 isl_assert(map2->ctx, map1->n == 1, goto error);
3251 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3252 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3254 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3255 return isl_map_intersect(map2, map1);
3257 map1 = isl_map_cow(map1);
3258 if (!map1)
3259 goto error;
3260 if (isl_map_plain_is_empty(map1)) {
3261 isl_map_free(map2);
3262 return map1;
3264 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3265 if (map2->p[0]->n_eq == 1)
3266 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3267 else
3268 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3269 map2->p[0]->ineq[0]);
3271 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3272 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3273 if (!map1->p[0])
3274 goto error;
3276 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3277 isl_basic_map_free(map1->p[0]);
3278 map1->n = 0;
3281 isl_map_free(map2);
3283 return map1;
3284 error:
3285 isl_map_free(map1);
3286 isl_map_free(map2);
3287 return NULL;
3290 /* map2 may be either a parameter domain or a map living in the same
3291 * space as map1.
3293 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3294 __isl_take isl_map *map2)
3296 unsigned flags = 0;
3297 isl_map *result;
3298 int i, j;
3300 if (!map1 || !map2)
3301 goto error;
3303 if ((isl_map_plain_is_empty(map1) ||
3304 isl_map_plain_is_universe(map2)) &&
3305 isl_space_is_equal(map1->dim, map2->dim)) {
3306 isl_map_free(map2);
3307 return map1;
3309 if ((isl_map_plain_is_empty(map2) ||
3310 isl_map_plain_is_universe(map1)) &&
3311 isl_space_is_equal(map1->dim, map2->dim)) {
3312 isl_map_free(map1);
3313 return map2;
3316 if (map1->n == 1 && map2->n == 1 &&
3317 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3318 isl_space_is_equal(map1->dim, map2->dim) &&
3319 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3320 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3321 return map_intersect_add_constraint(map1, map2);
3323 if (isl_space_dim(map2->dim, isl_dim_all) !=
3324 isl_space_dim(map2->dim, isl_dim_param))
3325 isl_assert(map1->ctx,
3326 isl_space_is_equal(map1->dim, map2->dim), goto error);
3328 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3329 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3330 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3332 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3333 map1->n * map2->n, flags);
3334 if (!result)
3335 goto error;
3336 for (i = 0; i < map1->n; ++i)
3337 for (j = 0; j < map2->n; ++j) {
3338 struct isl_basic_map *part;
3339 part = isl_basic_map_intersect(
3340 isl_basic_map_copy(map1->p[i]),
3341 isl_basic_map_copy(map2->p[j]));
3342 if (isl_basic_map_is_empty(part) < 0)
3343 part = isl_basic_map_free(part);
3344 result = isl_map_add_basic_map(result, part);
3345 if (!result)
3346 goto error;
3348 isl_map_free(map1);
3349 isl_map_free(map2);
3350 return result;
3351 error:
3352 isl_map_free(map1);
3353 isl_map_free(map2);
3354 return NULL;
3357 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3358 __isl_take isl_map *map2)
3360 if (!map1 || !map2)
3361 goto error;
3362 if (!isl_space_is_equal(map1->dim, map2->dim))
3363 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3364 "spaces don't match", goto error);
3365 return map_intersect_internal(map1, map2);
3366 error:
3367 isl_map_free(map1);
3368 isl_map_free(map2);
3369 return NULL;
3372 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3373 __isl_take isl_map *map2)
3375 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3378 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3380 return set_from_map(isl_map_intersect(set_to_map(set1),
3381 set_to_map(set2)));
3384 /* map_intersect_internal accepts intersections
3385 * with parameter domains, so we can just call that function.
3387 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3388 __isl_take isl_set *params)
3390 return map_intersect_internal(map, params);
3393 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3394 __isl_take isl_map *map2)
3396 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3399 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3400 __isl_take isl_set *params)
3402 return isl_map_intersect_params(set, params);
3405 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3407 isl_space *space;
3408 unsigned pos, n1, n2;
3410 if (!bmap)
3411 return NULL;
3412 bmap = isl_basic_map_cow(bmap);
3413 if (!bmap)
3414 return NULL;
3415 space = isl_space_reverse(isl_space_copy(bmap->dim));
3416 pos = isl_basic_map_offset(bmap, isl_dim_in);
3417 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3418 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3419 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3420 return isl_basic_map_reset_space(bmap, space);
3423 static __isl_give isl_basic_map *basic_map_space_reset(
3424 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3426 isl_space *space;
3428 if (!bmap)
3429 return NULL;
3430 if (!isl_space_is_named_or_nested(bmap->dim, type))
3431 return bmap;
3433 space = isl_basic_map_get_space(bmap);
3434 space = isl_space_reset(space, type);
3435 bmap = isl_basic_map_reset_space(bmap, space);
3436 return bmap;
3439 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3440 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3441 unsigned pos, unsigned n)
3443 isl_bool rational;
3444 isl_space *res_dim;
3445 struct isl_basic_map *res;
3446 struct isl_dim_map *dim_map;
3447 unsigned total, off;
3448 enum isl_dim_type t;
3450 if (n == 0)
3451 return basic_map_space_reset(bmap, type);
3453 if (!bmap)
3454 return NULL;
3456 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3458 total = isl_basic_map_total_dim(bmap) + n;
3459 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3460 off = 0;
3461 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3462 if (t != type) {
3463 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3464 } else {
3465 unsigned size = isl_basic_map_dim(bmap, t);
3466 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3467 0, pos, off);
3468 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3469 pos, size - pos, off + pos + n);
3471 off += isl_space_dim(res_dim, t);
3473 isl_dim_map_div(dim_map, bmap, off);
3475 res = isl_basic_map_alloc_space(res_dim,
3476 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3477 rational = isl_basic_map_is_rational(bmap);
3478 if (rational < 0)
3479 res = isl_basic_map_free(res);
3480 if (rational)
3481 res = isl_basic_map_set_rational(res);
3482 if (isl_basic_map_plain_is_empty(bmap)) {
3483 isl_basic_map_free(bmap);
3484 free(dim_map);
3485 return isl_basic_map_set_to_empty(res);
3487 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3488 return isl_basic_map_finalize(res);
3491 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3492 __isl_take isl_basic_set *bset,
3493 enum isl_dim_type type, unsigned pos, unsigned n)
3495 return isl_basic_map_insert_dims(bset, type, pos, n);
3498 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3499 enum isl_dim_type type, unsigned n)
3501 if (!bmap)
3502 return NULL;
3503 return isl_basic_map_insert_dims(bmap, type,
3504 isl_basic_map_dim(bmap, type), n);
3507 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3508 enum isl_dim_type type, unsigned n)
3510 if (!bset)
3511 return NULL;
3512 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3513 return isl_basic_map_add_dims(bset, type, n);
3514 error:
3515 isl_basic_set_free(bset);
3516 return NULL;
3519 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3520 enum isl_dim_type type)
3522 isl_space *space;
3524 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3525 return map;
3527 space = isl_map_get_space(map);
3528 space = isl_space_reset(space, type);
3529 map = isl_map_reset_space(map, space);
3530 return map;
3533 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3534 enum isl_dim_type type, unsigned pos, unsigned n)
3536 int i;
3538 if (n == 0)
3539 return map_space_reset(map, type);
3541 map = isl_map_cow(map);
3542 if (!map)
3543 return NULL;
3545 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3546 if (!map->dim)
3547 goto error;
3549 for (i = 0; i < map->n; ++i) {
3550 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3551 if (!map->p[i])
3552 goto error;
3555 return map;
3556 error:
3557 isl_map_free(map);
3558 return NULL;
3561 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3562 enum isl_dim_type type, unsigned pos, unsigned n)
3564 return isl_map_insert_dims(set, type, pos, n);
3567 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3568 enum isl_dim_type type, unsigned n)
3570 if (!map)
3571 return NULL;
3572 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3575 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3576 enum isl_dim_type type, unsigned n)
3578 if (!set)
3579 return NULL;
3580 isl_assert(set->ctx, type != isl_dim_in, goto error);
3581 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3582 error:
3583 isl_set_free(set);
3584 return NULL;
3587 __isl_give isl_basic_map *isl_basic_map_move_dims(
3588 __isl_take isl_basic_map *bmap,
3589 enum isl_dim_type dst_type, unsigned dst_pos,
3590 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3592 struct isl_dim_map *dim_map;
3593 struct isl_basic_map *res;
3594 enum isl_dim_type t;
3595 unsigned total, off;
3597 if (!bmap)
3598 return NULL;
3599 if (n == 0)
3600 return bmap;
3602 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3603 return isl_basic_map_free(bmap);
3605 if (dst_type == src_type && dst_pos == src_pos)
3606 return bmap;
3608 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3610 if (pos(bmap->dim, dst_type) + dst_pos ==
3611 pos(bmap->dim, src_type) + src_pos +
3612 ((src_type < dst_type) ? n : 0)) {
3613 bmap = isl_basic_map_cow(bmap);
3614 if (!bmap)
3615 return NULL;
3617 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3618 src_type, src_pos, n);
3619 if (!bmap->dim)
3620 goto error;
3622 bmap = isl_basic_map_finalize(bmap);
3624 return bmap;
3627 total = isl_basic_map_total_dim(bmap);
3628 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3630 off = 0;
3631 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3632 unsigned size = isl_space_dim(bmap->dim, t);
3633 if (t == dst_type) {
3634 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3635 0, dst_pos, off);
3636 off += dst_pos;
3637 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3638 src_pos, n, off);
3639 off += n;
3640 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3641 dst_pos, size - dst_pos, off);
3642 off += size - dst_pos;
3643 } else if (t == src_type) {
3644 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3645 0, src_pos, off);
3646 off += src_pos;
3647 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3648 src_pos + n, size - src_pos - n, off);
3649 off += size - src_pos - n;
3650 } else {
3651 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3652 off += size;
3655 isl_dim_map_div(dim_map, bmap, off);
3657 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3658 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3659 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3660 if (!bmap)
3661 goto error;
3663 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3664 src_type, src_pos, n);
3665 if (!bmap->dim)
3666 goto error;
3668 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3669 bmap = isl_basic_map_gauss(bmap, NULL);
3670 bmap = isl_basic_map_finalize(bmap);
3672 return bmap;
3673 error:
3674 isl_basic_map_free(bmap);
3675 return NULL;
3678 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3679 enum isl_dim_type dst_type, unsigned dst_pos,
3680 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3682 isl_basic_map *bmap = bset_to_bmap(bset);
3683 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3684 src_type, src_pos, n);
3685 return bset_from_bmap(bmap);
3688 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3689 enum isl_dim_type dst_type, unsigned dst_pos,
3690 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3692 if (!set)
3693 return NULL;
3694 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3695 return set_from_map(isl_map_move_dims(set_to_map(set),
3696 dst_type, dst_pos, src_type, src_pos, n));
3697 error:
3698 isl_set_free(set);
3699 return NULL;
3702 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3703 enum isl_dim_type dst_type, unsigned dst_pos,
3704 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3706 int i;
3708 if (!map)
3709 return NULL;
3710 if (n == 0)
3711 return map;
3713 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3714 goto error);
3716 if (dst_type == src_type && dst_pos == src_pos)
3717 return map;
3719 isl_assert(map->ctx, dst_type != src_type, goto error);
3721 map = isl_map_cow(map);
3722 if (!map)
3723 return NULL;
3725 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3726 if (!map->dim)
3727 goto error;
3729 for (i = 0; i < map->n; ++i) {
3730 map->p[i] = isl_basic_map_move_dims(map->p[i],
3731 dst_type, dst_pos,
3732 src_type, src_pos, n);
3733 if (!map->p[i])
3734 goto error;
3737 return map;
3738 error:
3739 isl_map_free(map);
3740 return NULL;
3743 /* Move the specified dimensions to the last columns right before
3744 * the divs. Don't change the dimension specification of bmap.
3745 * That's the responsibility of the caller.
3747 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3748 enum isl_dim_type type, unsigned first, unsigned n)
3750 struct isl_dim_map *dim_map;
3751 struct isl_basic_map *res;
3752 enum isl_dim_type t;
3753 unsigned total, off;
3755 if (!bmap)
3756 return NULL;
3757 if (pos(bmap->dim, type) + first + n ==
3758 1 + isl_space_dim(bmap->dim, isl_dim_all))
3759 return bmap;
3761 total = isl_basic_map_total_dim(bmap);
3762 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3764 off = 0;
3765 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3766 unsigned size = isl_space_dim(bmap->dim, t);
3767 if (t == type) {
3768 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3769 0, first, off);
3770 off += first;
3771 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3772 first, n, total - bmap->n_div - n);
3773 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3774 first + n, size - (first + n), off);
3775 off += size - (first + n);
3776 } else {
3777 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3778 off += size;
3781 isl_dim_map_div(dim_map, bmap, off + n);
3783 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3784 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3785 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3786 return res;
3789 /* Insert "n" rows in the divs of "bmap".
3791 * The number of columns is not changed, which means that the last
3792 * dimensions of "bmap" are being reintepreted as the new divs.
3793 * The space of "bmap" is not adjusted, however, which means
3794 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3795 * from the space of "bmap" is the responsibility of the caller.
3797 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3798 int n)
3800 int i;
3801 size_t row_size;
3802 isl_int **new_div;
3803 isl_int *old;
3805 bmap = isl_basic_map_cow(bmap);
3806 if (!bmap)
3807 return NULL;
3809 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3810 old = bmap->block2.data;
3811 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3812 (bmap->extra + n) * (1 + row_size));
3813 if (!bmap->block2.data)
3814 return isl_basic_map_free(bmap);
3815 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3816 if (!new_div)
3817 return isl_basic_map_free(bmap);
3818 for (i = 0; i < n; ++i) {
3819 new_div[i] = bmap->block2.data +
3820 (bmap->extra + i) * (1 + row_size);
3821 isl_seq_clr(new_div[i], 1 + row_size);
3823 for (i = 0; i < bmap->extra; ++i)
3824 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3825 free(bmap->div);
3826 bmap->div = new_div;
3827 bmap->n_div += n;
3828 bmap->extra += n;
3830 return bmap;
3833 /* Drop constraints from "bmap" that only involve the variables
3834 * of "type" in the range [first, first + n] that are not related
3835 * to any of the variables outside that interval.
3836 * These constraints cannot influence the values for the variables
3837 * outside the interval, except in case they cause "bmap" to be empty.
3838 * Only drop the constraints if "bmap" is known to be non-empty.
3840 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3841 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3842 unsigned first, unsigned n)
3844 int i;
3845 int *groups;
3846 unsigned dim, n_div;
3847 isl_bool non_empty;
3849 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3850 if (non_empty < 0)
3851 return isl_basic_map_free(bmap);
3852 if (!non_empty)
3853 return bmap;
3855 dim = isl_basic_map_dim(bmap, isl_dim_all);
3856 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3857 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3858 if (!groups)
3859 return isl_basic_map_free(bmap);
3860 first += isl_basic_map_offset(bmap, type) - 1;
3861 for (i = 0; i < first; ++i)
3862 groups[i] = -1;
3863 for (i = first + n; i < dim - n_div; ++i)
3864 groups[i] = -1;
3866 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3868 return bmap;
3871 /* Turn the n dimensions of type type, starting at first
3872 * into existentially quantified variables.
3874 * If a subset of the projected out variables are unrelated
3875 * to any of the variables that remain, then the constraints
3876 * involving this subset are simply dropped first.
3878 __isl_give isl_basic_map *isl_basic_map_project_out(
3879 __isl_take isl_basic_map *bmap,
3880 enum isl_dim_type type, unsigned first, unsigned n)
3882 if (n == 0)
3883 return basic_map_space_reset(bmap, type);
3884 if (type == isl_dim_div)
3885 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3886 "cannot project out existentially quantified variables",
3887 return isl_basic_map_free(bmap));
3889 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3890 if (!bmap)
3891 return NULL;
3893 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3894 return isl_basic_map_remove_dims(bmap, type, first, n);
3896 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3897 return isl_basic_map_free(bmap);
3899 bmap = move_last(bmap, type, first, n);
3900 bmap = isl_basic_map_cow(bmap);
3901 bmap = insert_div_rows(bmap, n);
3902 if (!bmap)
3903 return NULL;
3905 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3906 if (!bmap->dim)
3907 goto error;
3908 bmap = isl_basic_map_simplify(bmap);
3909 bmap = isl_basic_map_drop_redundant_divs(bmap);
3910 return isl_basic_map_finalize(bmap);
3911 error:
3912 isl_basic_map_free(bmap);
3913 return NULL;
3916 /* Turn the n dimensions of type type, starting at first
3917 * into existentially quantified variables.
3919 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3920 enum isl_dim_type type, unsigned first, unsigned n)
3922 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3923 type, first, n));
3926 /* Turn the n dimensions of type type, starting at first
3927 * into existentially quantified variables.
3929 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3930 enum isl_dim_type type, unsigned first, unsigned n)
3932 int i;
3934 if (!map)
3935 return NULL;
3937 if (n == 0)
3938 return map_space_reset(map, type);
3940 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3942 map = isl_map_cow(map);
3943 if (!map)
3944 return NULL;
3946 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3947 if (!map->dim)
3948 goto error;
3950 for (i = 0; i < map->n; ++i) {
3951 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3952 if (!map->p[i])
3953 goto error;
3956 return map;
3957 error:
3958 isl_map_free(map);
3959 return NULL;
3962 /* Turn the n dimensions of type type, starting at first
3963 * into existentially quantified variables.
3965 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3966 enum isl_dim_type type, unsigned first, unsigned n)
3968 return set_from_map(isl_map_project_out(set_to_map(set),
3969 type, first, n));
3972 /* Return a map that projects the elements in "set" onto their
3973 * "n" set dimensions starting at "first".
3974 * "type" should be equal to isl_dim_set.
3976 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3977 enum isl_dim_type type, unsigned first, unsigned n)
3979 int i;
3980 int dim;
3981 isl_map *map;
3983 if (!set)
3984 return NULL;
3985 if (type != isl_dim_set)
3986 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3987 "only set dimensions can be projected out", goto error);
3988 dim = isl_set_dim(set, isl_dim_set);
3989 if (first + n > dim || first + n < first)
3990 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3991 "index out of bounds", goto error);
3993 map = isl_map_from_domain(set);
3994 map = isl_map_add_dims(map, isl_dim_out, n);
3995 for (i = 0; i < n; ++i)
3996 map = isl_map_equate(map, isl_dim_in, first + i,
3997 isl_dim_out, i);
3998 return map;
3999 error:
4000 isl_set_free(set);
4001 return NULL;
4004 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4006 int i, j;
4008 for (i = 0; i < n; ++i) {
4009 j = isl_basic_map_alloc_div(bmap);
4010 if (j < 0)
4011 goto error;
4012 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4014 return bmap;
4015 error:
4016 isl_basic_map_free(bmap);
4017 return NULL;
4020 struct isl_basic_map *isl_basic_map_apply_range(
4021 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4023 isl_space *dim_result = NULL;
4024 struct isl_basic_map *bmap;
4025 unsigned n_in, n_out, n, nparam, total, pos;
4026 struct isl_dim_map *dim_map1, *dim_map2;
4028 if (!bmap1 || !bmap2)
4029 goto error;
4030 if (!isl_space_match(bmap1->dim, isl_dim_param,
4031 bmap2->dim, isl_dim_param))
4032 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4033 "parameters don't match", goto error);
4034 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4035 bmap2->dim, isl_dim_in))
4036 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4037 "spaces don't match", goto error);
4039 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4040 isl_space_copy(bmap2->dim));
4042 n_in = isl_basic_map_n_in(bmap1);
4043 n_out = isl_basic_map_n_out(bmap2);
4044 n = isl_basic_map_n_out(bmap1);
4045 nparam = isl_basic_map_n_param(bmap1);
4047 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4048 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4049 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4050 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4051 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4052 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4053 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4054 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4055 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4056 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4057 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4059 bmap = isl_basic_map_alloc_space(dim_result,
4060 bmap1->n_div + bmap2->n_div + n,
4061 bmap1->n_eq + bmap2->n_eq,
4062 bmap1->n_ineq + bmap2->n_ineq);
4063 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4064 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4065 bmap = add_divs(bmap, n);
4066 bmap = isl_basic_map_simplify(bmap);
4067 bmap = isl_basic_map_drop_redundant_divs(bmap);
4068 return isl_basic_map_finalize(bmap);
4069 error:
4070 isl_basic_map_free(bmap1);
4071 isl_basic_map_free(bmap2);
4072 return NULL;
4075 struct isl_basic_set *isl_basic_set_apply(
4076 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4078 if (!bset || !bmap)
4079 goto error;
4081 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4082 goto error);
4084 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4085 bmap));
4086 error:
4087 isl_basic_set_free(bset);
4088 isl_basic_map_free(bmap);
4089 return NULL;
4092 struct isl_basic_map *isl_basic_map_apply_domain(
4093 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4095 if (!bmap1 || !bmap2)
4096 goto error;
4098 if (!isl_space_match(bmap1->dim, isl_dim_param,
4099 bmap2->dim, isl_dim_param))
4100 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4101 "parameters don't match", goto error);
4102 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4103 bmap2->dim, isl_dim_in))
4104 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4105 "spaces don't match", goto error);
4107 bmap1 = isl_basic_map_reverse(bmap1);
4108 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4109 return isl_basic_map_reverse(bmap1);
4110 error:
4111 isl_basic_map_free(bmap1);
4112 isl_basic_map_free(bmap2);
4113 return NULL;
4116 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4117 * A \cap B -> f(A) + f(B)
4119 struct isl_basic_map *isl_basic_map_sum(
4120 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4122 unsigned n_in, n_out, nparam, total, pos;
4123 struct isl_basic_map *bmap = NULL;
4124 struct isl_dim_map *dim_map1, *dim_map2;
4125 int i;
4127 if (!bmap1 || !bmap2)
4128 goto error;
4130 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4131 goto error);
4133 nparam = isl_basic_map_n_param(bmap1);
4134 n_in = isl_basic_map_n_in(bmap1);
4135 n_out = isl_basic_map_n_out(bmap1);
4137 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4138 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4139 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4140 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4141 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4142 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4143 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4144 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4145 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4146 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4147 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4149 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4150 bmap1->n_div + bmap2->n_div + 2 * n_out,
4151 bmap1->n_eq + bmap2->n_eq + n_out,
4152 bmap1->n_ineq + bmap2->n_ineq);
4153 for (i = 0; i < n_out; ++i) {
4154 int j = isl_basic_map_alloc_equality(bmap);
4155 if (j < 0)
4156 goto error;
4157 isl_seq_clr(bmap->eq[j], 1+total);
4158 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4159 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4160 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4162 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4163 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4164 bmap = add_divs(bmap, 2 * n_out);
4166 bmap = isl_basic_map_simplify(bmap);
4167 return isl_basic_map_finalize(bmap);
4168 error:
4169 isl_basic_map_free(bmap);
4170 isl_basic_map_free(bmap1);
4171 isl_basic_map_free(bmap2);
4172 return NULL;
4175 /* Given two maps A -> f(A) and B -> g(B), construct a map
4176 * A \cap B -> f(A) + f(B)
4178 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4180 struct isl_map *result;
4181 int i, j;
4183 if (!map1 || !map2)
4184 goto error;
4186 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4188 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4189 map1->n * map2->n, 0);
4190 if (!result)
4191 goto error;
4192 for (i = 0; i < map1->n; ++i)
4193 for (j = 0; j < map2->n; ++j) {
4194 struct isl_basic_map *part;
4195 part = isl_basic_map_sum(
4196 isl_basic_map_copy(map1->p[i]),
4197 isl_basic_map_copy(map2->p[j]));
4198 if (isl_basic_map_is_empty(part))
4199 isl_basic_map_free(part);
4200 else
4201 result = isl_map_add_basic_map(result, part);
4202 if (!result)
4203 goto error;
4205 isl_map_free(map1);
4206 isl_map_free(map2);
4207 return result;
4208 error:
4209 isl_map_free(map1);
4210 isl_map_free(map2);
4211 return NULL;
4214 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4215 __isl_take isl_set *set2)
4217 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4220 /* Given a basic map A -> f(A), construct A -> -f(A).
4222 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4224 int i, j;
4225 unsigned off, n;
4227 bmap = isl_basic_map_cow(bmap);
4228 if (!bmap)
4229 return NULL;
4231 n = isl_basic_map_dim(bmap, isl_dim_out);
4232 off = isl_basic_map_offset(bmap, isl_dim_out);
4233 for (i = 0; i < bmap->n_eq; ++i)
4234 for (j = 0; j < n; ++j)
4235 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4236 for (i = 0; i < bmap->n_ineq; ++i)
4237 for (j = 0; j < n; ++j)
4238 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4239 for (i = 0; i < bmap->n_div; ++i)
4240 for (j = 0; j < n; ++j)
4241 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4242 bmap = isl_basic_map_gauss(bmap, NULL);
4243 return isl_basic_map_finalize(bmap);
4246 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4248 return isl_basic_map_neg(bset);
4251 /* Given a map A -> f(A), construct A -> -f(A).
4253 struct isl_map *isl_map_neg(struct isl_map *map)
4255 int i;
4257 map = isl_map_cow(map);
4258 if (!map)
4259 return NULL;
4261 for (i = 0; i < map->n; ++i) {
4262 map->p[i] = isl_basic_map_neg(map->p[i]);
4263 if (!map->p[i])
4264 goto error;
4267 return map;
4268 error:
4269 isl_map_free(map);
4270 return NULL;
4273 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4275 return set_from_map(isl_map_neg(set_to_map(set)));
4278 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4279 * A -> floor(f(A)/d).
4281 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4282 isl_int d)
4284 unsigned n_in, n_out, nparam, total, pos;
4285 struct isl_basic_map *result = NULL;
4286 struct isl_dim_map *dim_map;
4287 int i;
4289 if (!bmap)
4290 return NULL;
4292 nparam = isl_basic_map_n_param(bmap);
4293 n_in = isl_basic_map_n_in(bmap);
4294 n_out = isl_basic_map_n_out(bmap);
4296 total = nparam + n_in + n_out + bmap->n_div + n_out;
4297 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4298 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4299 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4300 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4301 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4303 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4304 bmap->n_div + n_out,
4305 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4306 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4307 result = add_divs(result, n_out);
4308 for (i = 0; i < n_out; ++i) {
4309 int j;
4310 j = isl_basic_map_alloc_inequality(result);
4311 if (j < 0)
4312 goto error;
4313 isl_seq_clr(result->ineq[j], 1+total);
4314 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4315 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4316 j = isl_basic_map_alloc_inequality(result);
4317 if (j < 0)
4318 goto error;
4319 isl_seq_clr(result->ineq[j], 1+total);
4320 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4321 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4322 isl_int_sub_ui(result->ineq[j][0], d, 1);
4325 result = isl_basic_map_simplify(result);
4326 return isl_basic_map_finalize(result);
4327 error:
4328 isl_basic_map_free(result);
4329 return NULL;
4332 /* Given a map A -> f(A) and an integer d, construct a map
4333 * A -> floor(f(A)/d).
4335 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4337 int i;
4339 map = isl_map_cow(map);
4340 if (!map)
4341 return NULL;
4343 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4344 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4345 for (i = 0; i < map->n; ++i) {
4346 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4347 if (!map->p[i])
4348 goto error;
4351 return map;
4352 error:
4353 isl_map_free(map);
4354 return NULL;
4357 /* Given a map A -> f(A) and an integer d, construct a map
4358 * A -> floor(f(A)/d).
4360 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4361 __isl_take isl_val *d)
4363 if (!map || !d)
4364 goto error;
4365 if (!isl_val_is_int(d))
4366 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4367 "expecting integer denominator", goto error);
4368 map = isl_map_floordiv(map, d->n);
4369 isl_val_free(d);
4370 return map;
4371 error:
4372 isl_map_free(map);
4373 isl_val_free(d);
4374 return NULL;
4377 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4379 int i;
4380 unsigned nparam;
4381 unsigned n_in;
4383 i = isl_basic_map_alloc_equality(bmap);
4384 if (i < 0)
4385 goto error;
4386 nparam = isl_basic_map_n_param(bmap);
4387 n_in = isl_basic_map_n_in(bmap);
4388 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4389 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4390 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4391 return isl_basic_map_finalize(bmap);
4392 error:
4393 isl_basic_map_free(bmap);
4394 return NULL;
4397 /* Add a constraint to "bmap" expressing i_pos < o_pos
4399 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4401 int i;
4402 unsigned nparam;
4403 unsigned n_in;
4405 i = isl_basic_map_alloc_inequality(bmap);
4406 if (i < 0)
4407 goto error;
4408 nparam = isl_basic_map_n_param(bmap);
4409 n_in = isl_basic_map_n_in(bmap);
4410 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4411 isl_int_set_si(bmap->ineq[i][0], -1);
4412 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4413 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4414 return isl_basic_map_finalize(bmap);
4415 error:
4416 isl_basic_map_free(bmap);
4417 return NULL;
4420 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4422 static __isl_give isl_basic_map *var_less_or_equal(
4423 __isl_take isl_basic_map *bmap, unsigned pos)
4425 int i;
4426 unsigned nparam;
4427 unsigned n_in;
4429 i = isl_basic_map_alloc_inequality(bmap);
4430 if (i < 0)
4431 goto error;
4432 nparam = isl_basic_map_n_param(bmap);
4433 n_in = isl_basic_map_n_in(bmap);
4434 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4435 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4436 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4437 return isl_basic_map_finalize(bmap);
4438 error:
4439 isl_basic_map_free(bmap);
4440 return NULL;
4443 /* Add a constraint to "bmap" expressing i_pos > o_pos
4445 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4447 int i;
4448 unsigned nparam;
4449 unsigned n_in;
4451 i = isl_basic_map_alloc_inequality(bmap);
4452 if (i < 0)
4453 goto error;
4454 nparam = isl_basic_map_n_param(bmap);
4455 n_in = isl_basic_map_n_in(bmap);
4456 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4457 isl_int_set_si(bmap->ineq[i][0], -1);
4458 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4459 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4460 return isl_basic_map_finalize(bmap);
4461 error:
4462 isl_basic_map_free(bmap);
4463 return NULL;
4466 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4468 static __isl_give isl_basic_map *var_more_or_equal(
4469 __isl_take isl_basic_map *bmap, unsigned pos)
4471 int i;
4472 unsigned nparam;
4473 unsigned n_in;
4475 i = isl_basic_map_alloc_inequality(bmap);
4476 if (i < 0)
4477 goto error;
4478 nparam = isl_basic_map_n_param(bmap);
4479 n_in = isl_basic_map_n_in(bmap);
4480 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4481 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4482 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4483 return isl_basic_map_finalize(bmap);
4484 error:
4485 isl_basic_map_free(bmap);
4486 return NULL;
4489 __isl_give isl_basic_map *isl_basic_map_equal(
4490 __isl_take isl_space *dim, unsigned n_equal)
4492 int i;
4493 struct isl_basic_map *bmap;
4494 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4495 if (!bmap)
4496 return NULL;
4497 for (i = 0; i < n_equal && bmap; ++i)
4498 bmap = var_equal(bmap, i);
4499 return isl_basic_map_finalize(bmap);
4502 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4504 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4505 unsigned pos)
4507 int i;
4508 struct isl_basic_map *bmap;
4509 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4510 if (!bmap)
4511 return NULL;
4512 for (i = 0; i < pos && bmap; ++i)
4513 bmap = var_equal(bmap, i);
4514 if (bmap)
4515 bmap = var_less(bmap, pos);
4516 return isl_basic_map_finalize(bmap);
4519 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4521 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4522 __isl_take isl_space *dim, unsigned pos)
4524 int i;
4525 isl_basic_map *bmap;
4527 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4528 for (i = 0; i < pos; ++i)
4529 bmap = var_equal(bmap, i);
4530 bmap = var_less_or_equal(bmap, pos);
4531 return isl_basic_map_finalize(bmap);
4534 /* Return a relation on "dim" expressing i_pos > o_pos
4536 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4537 unsigned pos)
4539 int i;
4540 struct isl_basic_map *bmap;
4541 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4542 if (!bmap)
4543 return NULL;
4544 for (i = 0; i < pos && bmap; ++i)
4545 bmap = var_equal(bmap, i);
4546 if (bmap)
4547 bmap = var_more(bmap, pos);
4548 return isl_basic_map_finalize(bmap);
4551 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4553 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4554 __isl_take isl_space *dim, unsigned pos)
4556 int i;
4557 isl_basic_map *bmap;
4559 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4560 for (i = 0; i < pos; ++i)
4561 bmap = var_equal(bmap, i);
4562 bmap = var_more_or_equal(bmap, pos);
4563 return isl_basic_map_finalize(bmap);
4566 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4567 unsigned n, int equal)
4569 struct isl_map *map;
4570 int i;
4572 if (n == 0 && equal)
4573 return isl_map_universe(dims);
4575 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4577 for (i = 0; i + 1 < n; ++i)
4578 map = isl_map_add_basic_map(map,
4579 isl_basic_map_less_at(isl_space_copy(dims), i));
4580 if (n > 0) {
4581 if (equal)
4582 map = isl_map_add_basic_map(map,
4583 isl_basic_map_less_or_equal_at(dims, n - 1));
4584 else
4585 map = isl_map_add_basic_map(map,
4586 isl_basic_map_less_at(dims, n - 1));
4587 } else
4588 isl_space_free(dims);
4590 return map;
4593 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4595 if (!dims)
4596 return NULL;
4597 return map_lex_lte_first(dims, dims->n_out, equal);
4600 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4602 return map_lex_lte_first(dim, n, 0);
4605 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4607 return map_lex_lte_first(dim, n, 1);
4610 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4612 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4615 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4617 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4620 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4621 unsigned n, int equal)
4623 struct isl_map *map;
4624 int i;
4626 if (n == 0 && equal)
4627 return isl_map_universe(dims);
4629 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4631 for (i = 0; i + 1 < n; ++i)
4632 map = isl_map_add_basic_map(map,
4633 isl_basic_map_more_at(isl_space_copy(dims), i));
4634 if (n > 0) {
4635 if (equal)
4636 map = isl_map_add_basic_map(map,
4637 isl_basic_map_more_or_equal_at(dims, n - 1));
4638 else
4639 map = isl_map_add_basic_map(map,
4640 isl_basic_map_more_at(dims, n - 1));
4641 } else
4642 isl_space_free(dims);
4644 return map;
4647 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4649 if (!dims)
4650 return NULL;
4651 return map_lex_gte_first(dims, dims->n_out, equal);
4654 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4656 return map_lex_gte_first(dim, n, 0);
4659 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4661 return map_lex_gte_first(dim, n, 1);
4664 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4666 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4669 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4671 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4674 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4675 __isl_take isl_set *set2)
4677 isl_map *map;
4678 map = isl_map_lex_le(isl_set_get_space(set1));
4679 map = isl_map_intersect_domain(map, set1);
4680 map = isl_map_intersect_range(map, set2);
4681 return map;
4684 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4685 __isl_take isl_set *set2)
4687 isl_map *map;
4688 map = isl_map_lex_lt(isl_set_get_space(set1));
4689 map = isl_map_intersect_domain(map, set1);
4690 map = isl_map_intersect_range(map, set2);
4691 return map;
4694 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4695 __isl_take isl_set *set2)
4697 isl_map *map;
4698 map = isl_map_lex_ge(isl_set_get_space(set1));
4699 map = isl_map_intersect_domain(map, set1);
4700 map = isl_map_intersect_range(map, set2);
4701 return map;
4704 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4705 __isl_take isl_set *set2)
4707 isl_map *map;
4708 map = isl_map_lex_gt(isl_set_get_space(set1));
4709 map = isl_map_intersect_domain(map, set1);
4710 map = isl_map_intersect_range(map, set2);
4711 return map;
4714 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4715 __isl_take isl_map *map2)
4717 isl_map *map;
4718 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4719 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4720 map = isl_map_apply_range(map, isl_map_reverse(map2));
4721 return map;
4724 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4725 __isl_take isl_map *map2)
4727 isl_map *map;
4728 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4729 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4730 map = isl_map_apply_range(map, isl_map_reverse(map2));
4731 return map;
4734 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4735 __isl_take isl_map *map2)
4737 isl_map *map;
4738 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4739 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4740 map = isl_map_apply_range(map, isl_map_reverse(map2));
4741 return map;
4744 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4745 __isl_take isl_map *map2)
4747 isl_map *map;
4748 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4749 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4750 map = isl_map_apply_range(map, isl_map_reverse(map2));
4751 return map;
4754 static __isl_give isl_basic_map *basic_map_from_basic_set(
4755 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4757 struct isl_basic_map *bmap;
4759 bset = isl_basic_set_cow(bset);
4760 if (!bset || !dim)
4761 goto error;
4763 isl_assert(bset->ctx, isl_space_compatible_internal(bset->dim, dim),
4764 goto error);
4765 isl_space_free(bset->dim);
4766 bmap = bset_to_bmap(bset);
4767 bmap->dim = dim;
4768 return isl_basic_map_finalize(bmap);
4769 error:
4770 isl_basic_set_free(bset);
4771 isl_space_free(dim);
4772 return NULL;
4775 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4776 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
4778 return basic_map_from_basic_set(bset, space);
4781 /* For a div d = floor(f/m), add the constraint
4783 * f - m d >= 0
4785 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4786 unsigned pos, isl_int *div)
4788 int i;
4789 unsigned total = isl_basic_map_total_dim(bmap);
4791 i = isl_basic_map_alloc_inequality(bmap);
4792 if (i < 0)
4793 return isl_stat_error;
4794 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4795 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4797 return isl_stat_ok;
4800 /* For a div d = floor(f/m), add the constraint
4802 * -(f-(m-1)) + m d >= 0
4804 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4805 unsigned pos, isl_int *div)
4807 int i;
4808 unsigned total = isl_basic_map_total_dim(bmap);
4810 i = isl_basic_map_alloc_inequality(bmap);
4811 if (i < 0)
4812 return isl_stat_error;
4813 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4814 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4815 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4816 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4818 return isl_stat_ok;
4821 /* For a div d = floor(f/m), add the constraints
4823 * f - m d >= 0
4824 * -(f-(m-1)) + m d >= 0
4826 * Note that the second constraint is the negation of
4828 * f - m d >= m
4830 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4831 unsigned pos, isl_int *div)
4833 if (add_upper_div_constraint(bmap, pos, div) < 0)
4834 return -1;
4835 if (add_lower_div_constraint(bmap, pos, div) < 0)
4836 return -1;
4837 return 0;
4840 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4841 unsigned pos, isl_int *div)
4843 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4844 pos, div);
4847 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4849 unsigned total = isl_basic_map_total_dim(bmap);
4850 unsigned div_pos = total - bmap->n_div + div;
4852 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4853 bmap->div[div]);
4856 /* For each known div d = floor(f/m), add the constraints
4858 * f - m d >= 0
4859 * -(f-(m-1)) + m d >= 0
4861 * Remove duplicate constraints in case of some these div constraints
4862 * already appear in "bmap".
4864 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4865 __isl_take isl_basic_map *bmap)
4867 unsigned n_div;
4869 if (!bmap)
4870 return NULL;
4871 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4872 if (n_div == 0)
4873 return bmap;
4875 bmap = add_known_div_constraints(bmap);
4876 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4877 bmap = isl_basic_map_finalize(bmap);
4878 return bmap;
4881 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4883 * In particular, if this div is of the form d = floor(f/m),
4884 * then add the constraint
4886 * f - m d >= 0
4888 * if sign < 0 or the constraint
4890 * -(f-(m-1)) + m d >= 0
4892 * if sign > 0.
4894 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4895 unsigned div, int sign)
4897 unsigned total;
4898 unsigned div_pos;
4900 if (!bmap)
4901 return -1;
4903 total = isl_basic_map_total_dim(bmap);
4904 div_pos = total - bmap->n_div + div;
4906 if (sign < 0)
4907 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4908 else
4909 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4912 struct isl_basic_set *isl_basic_map_underlying_set(
4913 struct isl_basic_map *bmap)
4915 if (!bmap)
4916 goto error;
4917 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4918 bmap->n_div == 0 &&
4919 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4920 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4921 return bset_from_bmap(bmap);
4922 bmap = isl_basic_map_cow(bmap);
4923 if (!bmap)
4924 goto error;
4925 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4926 if (!bmap->dim)
4927 goto error;
4928 bmap->extra -= bmap->n_div;
4929 bmap->n_div = 0;
4930 bmap = isl_basic_map_finalize(bmap);
4931 return bset_from_bmap(bmap);
4932 error:
4933 isl_basic_map_free(bmap);
4934 return NULL;
4937 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4938 __isl_take isl_basic_set *bset)
4940 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4943 /* Replace each element in "list" by the result of applying
4944 * isl_basic_map_underlying_set to the element.
4946 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4947 __isl_take isl_basic_map_list *list)
4949 int i, n;
4951 if (!list)
4952 return NULL;
4954 n = isl_basic_map_list_n_basic_map(list);
4955 for (i = 0; i < n; ++i) {
4956 isl_basic_map *bmap;
4957 isl_basic_set *bset;
4959 bmap = isl_basic_map_list_get_basic_map(list, i);
4960 bset = isl_basic_set_underlying_set(bmap);
4961 list = isl_basic_set_list_set_basic_set(list, i, bset);
4964 return list;
4967 struct isl_basic_map *isl_basic_map_overlying_set(
4968 struct isl_basic_set *bset, struct isl_basic_map *like)
4970 struct isl_basic_map *bmap;
4971 struct isl_ctx *ctx;
4972 unsigned total;
4973 int i;
4975 if (!bset || !like)
4976 goto error;
4977 ctx = bset->ctx;
4978 isl_assert(ctx, bset->n_div == 0, goto error);
4979 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4980 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4981 goto error);
4982 if (like->n_div == 0) {
4983 isl_space *space = isl_basic_map_get_space(like);
4984 isl_basic_map_free(like);
4985 return isl_basic_map_reset_space(bset, space);
4987 bset = isl_basic_set_cow(bset);
4988 if (!bset)
4989 goto error;
4990 total = bset->dim->n_out + bset->extra;
4991 bmap = bset_to_bmap(bset);
4992 isl_space_free(bmap->dim);
4993 bmap->dim = isl_space_copy(like->dim);
4994 if (!bmap->dim)
4995 goto error;
4996 bmap->n_div = like->n_div;
4997 bmap->extra += like->n_div;
4998 if (bmap->extra) {
4999 unsigned ltotal;
5000 isl_int **div;
5001 ltotal = total - bmap->extra + like->extra;
5002 if (ltotal > total)
5003 ltotal = total;
5004 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5005 bmap->extra * (1 + 1 + total));
5006 if (isl_blk_is_error(bmap->block2))
5007 goto error;
5008 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5009 if (!div)
5010 goto error;
5011 bmap->div = div;
5012 for (i = 0; i < bmap->extra; ++i)
5013 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5014 for (i = 0; i < like->n_div; ++i) {
5015 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5016 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5018 bmap = isl_basic_map_add_known_div_constraints(bmap);
5020 isl_basic_map_free(like);
5021 bmap = isl_basic_map_simplify(bmap);
5022 bmap = isl_basic_map_finalize(bmap);
5023 return bmap;
5024 error:
5025 isl_basic_map_free(like);
5026 isl_basic_set_free(bset);
5027 return NULL;
5030 struct isl_basic_set *isl_basic_set_from_underlying_set(
5031 struct isl_basic_set *bset, struct isl_basic_set *like)
5033 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5034 bset_to_bmap(like)));
5037 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5039 int i;
5041 map = isl_map_cow(map);
5042 if (!map)
5043 return NULL;
5044 map->dim = isl_space_cow(map->dim);
5045 if (!map->dim)
5046 goto error;
5048 for (i = 1; i < map->n; ++i)
5049 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5050 goto error);
5051 for (i = 0; i < map->n; ++i) {
5052 map->p[i] = bset_to_bmap(
5053 isl_basic_map_underlying_set(map->p[i]));
5054 if (!map->p[i])
5055 goto error;
5057 if (map->n == 0)
5058 map->dim = isl_space_underlying(map->dim, 0);
5059 else {
5060 isl_space_free(map->dim);
5061 map->dim = isl_space_copy(map->p[0]->dim);
5063 if (!map->dim)
5064 goto error;
5065 return set_from_map(map);
5066 error:
5067 isl_map_free(map);
5068 return NULL;
5071 /* Replace the space of "bmap" by "space".
5073 * If the space of "bmap" is identical to "space" (including the identifiers
5074 * of the input and output dimensions), then simply return the original input.
5076 __isl_give isl_basic_map *isl_basic_map_reset_space(
5077 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5079 isl_bool equal;
5081 if (!bmap)
5082 goto error;
5083 equal = isl_space_is_equal(bmap->dim, space);
5084 if (equal >= 0 && equal)
5085 equal = isl_space_match(bmap->dim, isl_dim_in,
5086 space, isl_dim_in);
5087 if (equal >= 0 && equal)
5088 equal = isl_space_match(bmap->dim, isl_dim_out,
5089 space, isl_dim_out);
5090 if (equal < 0)
5091 goto error;
5092 if (equal) {
5093 isl_space_free(space);
5094 return bmap;
5096 bmap = isl_basic_map_cow(bmap);
5097 if (!bmap || !space)
5098 goto error;
5100 isl_space_free(bmap->dim);
5101 bmap->dim = space;
5103 bmap = isl_basic_map_finalize(bmap);
5105 return bmap;
5106 error:
5107 isl_basic_map_free(bmap);
5108 isl_space_free(space);
5109 return NULL;
5112 __isl_give isl_basic_set *isl_basic_set_reset_space(
5113 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5115 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5116 dim));
5119 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5120 __isl_take isl_space *dim)
5122 int i;
5124 map = isl_map_cow(map);
5125 if (!map || !dim)
5126 goto error;
5128 for (i = 0; i < map->n; ++i) {
5129 map->p[i] = isl_basic_map_reset_space(map->p[i],
5130 isl_space_copy(dim));
5131 if (!map->p[i])
5132 goto error;
5134 isl_space_free(map->dim);
5135 map->dim = dim;
5137 return map;
5138 error:
5139 isl_map_free(map);
5140 isl_space_free(dim);
5141 return NULL;
5144 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5145 __isl_take isl_space *dim)
5147 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5150 /* Compute the parameter domain of the given basic set.
5152 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5154 isl_bool is_params;
5155 isl_space *space;
5156 unsigned n;
5158 is_params = isl_basic_set_is_params(bset);
5159 if (is_params < 0)
5160 return isl_basic_set_free(bset);
5161 if (is_params)
5162 return bset;
5164 n = isl_basic_set_dim(bset, isl_dim_set);
5165 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5166 space = isl_basic_set_get_space(bset);
5167 space = isl_space_params(space);
5168 bset = isl_basic_set_reset_space(bset, space);
5169 return bset;
5172 /* Construct a zero-dimensional basic set with the given parameter domain.
5174 __isl_give isl_basic_set *isl_basic_set_from_params(
5175 __isl_take isl_basic_set *bset)
5177 isl_space *space;
5178 space = isl_basic_set_get_space(bset);
5179 space = isl_space_set_from_params(space);
5180 bset = isl_basic_set_reset_space(bset, space);
5181 return bset;
5184 /* Compute the parameter domain of the given set.
5186 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5188 isl_space *space;
5189 unsigned n;
5191 if (isl_set_is_params(set))
5192 return set;
5194 n = isl_set_dim(set, isl_dim_set);
5195 set = isl_set_project_out(set, isl_dim_set, 0, n);
5196 space = isl_set_get_space(set);
5197 space = isl_space_params(space);
5198 set = isl_set_reset_space(set, space);
5199 return set;
5202 /* Construct a zero-dimensional set with the given parameter domain.
5204 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5206 isl_space *space;
5207 space = isl_set_get_space(set);
5208 space = isl_space_set_from_params(space);
5209 set = isl_set_reset_space(set, space);
5210 return set;
5213 /* Compute the parameter domain of the given map.
5215 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5217 isl_space *space;
5218 unsigned n;
5220 n = isl_map_dim(map, isl_dim_in);
5221 map = isl_map_project_out(map, isl_dim_in, 0, n);
5222 n = isl_map_dim(map, isl_dim_out);
5223 map = isl_map_project_out(map, isl_dim_out, 0, n);
5224 space = isl_map_get_space(map);
5225 space = isl_space_params(space);
5226 map = isl_map_reset_space(map, space);
5227 return map;
5230 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5232 isl_space *space;
5233 unsigned n_out;
5235 if (!bmap)
5236 return NULL;
5237 space = isl_space_domain(isl_basic_map_get_space(bmap));
5239 n_out = isl_basic_map_n_out(bmap);
5240 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5242 return isl_basic_map_reset_space(bmap, space);
5245 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5247 if (!bmap)
5248 return isl_bool_error;
5249 return isl_space_may_be_set(bmap->dim);
5252 /* Is this basic map actually a set?
5253 * Users should never call this function. Outside of isl,
5254 * the type should indicate whether something is a set or a map.
5256 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5258 if (!bmap)
5259 return isl_bool_error;
5260 return isl_space_is_set(bmap->dim);
5263 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5265 isl_bool is_set;
5267 is_set = isl_basic_map_is_set(bmap);
5268 if (is_set < 0)
5269 goto error;
5270 if (is_set)
5271 return bmap;
5272 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5273 error:
5274 isl_basic_map_free(bmap);
5275 return NULL;
5278 __isl_give isl_basic_map *isl_basic_map_domain_map(
5279 __isl_take isl_basic_map *bmap)
5281 int i;
5282 isl_space *dim;
5283 isl_basic_map *domain;
5284 int nparam, n_in, n_out;
5286 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5287 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5288 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5290 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5291 domain = isl_basic_map_universe(dim);
5293 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5294 bmap = isl_basic_map_apply_range(bmap, domain);
5295 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5297 for (i = 0; i < n_in; ++i)
5298 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5299 isl_dim_out, i);
5301 bmap = isl_basic_map_gauss(bmap, NULL);
5302 return isl_basic_map_finalize(bmap);
5305 __isl_give isl_basic_map *isl_basic_map_range_map(
5306 __isl_take isl_basic_map *bmap)
5308 int i;
5309 isl_space *dim;
5310 isl_basic_map *range;
5311 int nparam, n_in, n_out;
5313 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5314 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5315 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5317 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5318 range = isl_basic_map_universe(dim);
5320 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5321 bmap = isl_basic_map_apply_range(bmap, range);
5322 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5324 for (i = 0; i < n_out; ++i)
5325 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5326 isl_dim_out, i);
5328 bmap = isl_basic_map_gauss(bmap, NULL);
5329 return isl_basic_map_finalize(bmap);
5332 int isl_map_may_be_set(__isl_keep isl_map *map)
5334 if (!map)
5335 return -1;
5336 return isl_space_may_be_set(map->dim);
5339 /* Is this map actually a set?
5340 * Users should never call this function. Outside of isl,
5341 * the type should indicate whether something is a set or a map.
5343 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5345 if (!map)
5346 return isl_bool_error;
5347 return isl_space_is_set(map->dim);
5350 struct isl_set *isl_map_range(struct isl_map *map)
5352 int i;
5353 isl_bool is_set;
5354 struct isl_set *set;
5356 is_set = isl_map_is_set(map);
5357 if (is_set < 0)
5358 goto error;
5359 if (is_set)
5360 return set_from_map(map);
5362 map = isl_map_cow(map);
5363 if (!map)
5364 goto error;
5366 set = set_from_map(map);
5367 set->dim = isl_space_range(set->dim);
5368 if (!set->dim)
5369 goto error;
5370 for (i = 0; i < map->n; ++i) {
5371 set->p[i] = isl_basic_map_range(map->p[i]);
5372 if (!set->p[i])
5373 goto error;
5375 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5376 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5377 return set;
5378 error:
5379 isl_map_free(map);
5380 return NULL;
5383 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5385 int i;
5387 map = isl_map_cow(map);
5388 if (!map)
5389 return NULL;
5391 map->dim = isl_space_domain_map(map->dim);
5392 if (!map->dim)
5393 goto error;
5394 for (i = 0; i < map->n; ++i) {
5395 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5396 if (!map->p[i])
5397 goto error;
5399 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5400 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5401 return map;
5402 error:
5403 isl_map_free(map);
5404 return NULL;
5407 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5409 int i;
5410 isl_space *range_dim;
5412 map = isl_map_cow(map);
5413 if (!map)
5414 return NULL;
5416 range_dim = isl_space_range(isl_map_get_space(map));
5417 range_dim = isl_space_from_range(range_dim);
5418 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5419 map->dim = isl_space_join(map->dim, range_dim);
5420 if (!map->dim)
5421 goto error;
5422 for (i = 0; i < map->n; ++i) {
5423 map->p[i] = isl_basic_map_range_map(map->p[i]);
5424 if (!map->p[i])
5425 goto error;
5427 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5428 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5429 return map;
5430 error:
5431 isl_map_free(map);
5432 return NULL;
5435 /* Given a wrapped map of the form A[B -> C],
5436 * return the map A[B -> C] -> B.
5438 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5440 isl_id *id;
5441 isl_map *map;
5443 if (!set)
5444 return NULL;
5445 if (!isl_set_has_tuple_id(set))
5446 return isl_map_domain_map(isl_set_unwrap(set));
5448 id = isl_set_get_tuple_id(set);
5449 map = isl_map_domain_map(isl_set_unwrap(set));
5450 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5452 return map;
5455 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5456 __isl_take isl_space *dim)
5458 int i;
5459 struct isl_map *map = NULL;
5461 set = isl_set_cow(set);
5462 if (!set || !dim)
5463 goto error;
5464 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5465 goto error);
5466 map = set_to_map(set);
5467 for (i = 0; i < set->n; ++i) {
5468 map->p[i] = basic_map_from_basic_set(
5469 set->p[i], isl_space_copy(dim));
5470 if (!map->p[i])
5471 goto error;
5473 isl_space_free(map->dim);
5474 map->dim = dim;
5475 return map;
5476 error:
5477 isl_space_free(dim);
5478 isl_set_free(set);
5479 return NULL;
5482 __isl_give isl_basic_map *isl_basic_map_from_domain(
5483 __isl_take isl_basic_set *bset)
5485 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5488 __isl_give isl_basic_map *isl_basic_map_from_range(
5489 __isl_take isl_basic_set *bset)
5491 isl_space *space;
5492 space = isl_basic_set_get_space(bset);
5493 space = isl_space_from_range(space);
5494 bset = isl_basic_set_reset_space(bset, space);
5495 return bset_to_bmap(bset);
5498 /* Create a relation with the given set as range.
5499 * The domain of the created relation is a zero-dimensional
5500 * flat anonymous space.
5502 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5504 isl_space *space;
5505 space = isl_set_get_space(set);
5506 space = isl_space_from_range(space);
5507 set = isl_set_reset_space(set, space);
5508 return set_to_map(set);
5511 /* Create a relation with the given set as domain.
5512 * The range of the created relation is a zero-dimensional
5513 * flat anonymous space.
5515 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5517 return isl_map_reverse(isl_map_from_range(set));
5520 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5521 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5523 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5526 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5527 __isl_take isl_set *range)
5529 return isl_map_apply_range(isl_map_reverse(domain), range);
5532 /* Return a newly allocated isl_map with given space and flags and
5533 * room for "n" basic maps.
5534 * Make sure that all cached information is cleared.
5536 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5537 unsigned flags)
5539 struct isl_map *map;
5541 if (!space)
5542 return NULL;
5543 if (n < 0)
5544 isl_die(space->ctx, isl_error_internal,
5545 "negative number of basic maps", goto error);
5546 map = isl_calloc(space->ctx, struct isl_map,
5547 sizeof(struct isl_map) +
5548 (n - 1) * sizeof(struct isl_basic_map *));
5549 if (!map)
5550 goto error;
5552 map->ctx = space->ctx;
5553 isl_ctx_ref(map->ctx);
5554 map->ref = 1;
5555 map->size = n;
5556 map->n = 0;
5557 map->dim = space;
5558 map->flags = flags;
5559 return map;
5560 error:
5561 isl_space_free(space);
5562 return NULL;
5565 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5567 struct isl_basic_map *bmap;
5568 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5569 bmap = isl_basic_map_set_to_empty(bmap);
5570 return bmap;
5573 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5575 struct isl_basic_set *bset;
5576 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5577 bset = isl_basic_set_set_to_empty(bset);
5578 return bset;
5581 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5583 struct isl_basic_map *bmap;
5584 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5585 bmap = isl_basic_map_finalize(bmap);
5586 return bmap;
5589 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5591 struct isl_basic_set *bset;
5592 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5593 bset = isl_basic_set_finalize(bset);
5594 return bset;
5597 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5599 int i;
5600 unsigned total = isl_space_dim(dim, isl_dim_all);
5601 isl_basic_map *bmap;
5603 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5604 for (i = 0; i < total; ++i) {
5605 int k = isl_basic_map_alloc_inequality(bmap);
5606 if (k < 0)
5607 goto error;
5608 isl_seq_clr(bmap->ineq[k], 1 + total);
5609 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5611 return bmap;
5612 error:
5613 isl_basic_map_free(bmap);
5614 return NULL;
5617 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5619 return isl_basic_map_nat_universe(dim);
5622 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5624 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5627 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5629 return isl_map_nat_universe(dim);
5632 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5634 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5637 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5639 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5642 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5644 struct isl_map *map;
5645 if (!dim)
5646 return NULL;
5647 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5648 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5649 return map;
5652 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5654 struct isl_set *set;
5655 if (!dim)
5656 return NULL;
5657 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5658 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5659 return set;
5662 struct isl_map *isl_map_dup(struct isl_map *map)
5664 int i;
5665 struct isl_map *dup;
5667 if (!map)
5668 return NULL;
5669 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5670 for (i = 0; i < map->n; ++i)
5671 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5672 return dup;
5675 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5676 __isl_take isl_basic_map *bmap)
5678 if (!bmap || !map)
5679 goto error;
5680 if (isl_basic_map_plain_is_empty(bmap)) {
5681 isl_basic_map_free(bmap);
5682 return map;
5684 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5685 isl_assert(map->ctx, map->n < map->size, goto error);
5686 map->p[map->n] = bmap;
5687 map->n++;
5688 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5689 return map;
5690 error:
5691 if (map)
5692 isl_map_free(map);
5693 if (bmap)
5694 isl_basic_map_free(bmap);
5695 return NULL;
5698 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5700 int i;
5702 if (!map)
5703 return NULL;
5705 if (--map->ref > 0)
5706 return NULL;
5708 clear_caches(map);
5709 isl_ctx_deref(map->ctx);
5710 for (i = 0; i < map->n; ++i)
5711 isl_basic_map_free(map->p[i]);
5712 isl_space_free(map->dim);
5713 free(map);
5715 return NULL;
5718 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5719 struct isl_basic_map *bmap, unsigned pos, int value)
5721 int j;
5723 bmap = isl_basic_map_cow(bmap);
5724 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5725 j = isl_basic_map_alloc_equality(bmap);
5726 if (j < 0)
5727 goto error;
5728 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5729 isl_int_set_si(bmap->eq[j][pos], -1);
5730 isl_int_set_si(bmap->eq[j][0], value);
5731 bmap = isl_basic_map_simplify(bmap);
5732 return isl_basic_map_finalize(bmap);
5733 error:
5734 isl_basic_map_free(bmap);
5735 return NULL;
5738 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5739 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5741 int j;
5743 bmap = isl_basic_map_cow(bmap);
5744 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5745 j = isl_basic_map_alloc_equality(bmap);
5746 if (j < 0)
5747 goto error;
5748 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5749 isl_int_set_si(bmap->eq[j][pos], -1);
5750 isl_int_set(bmap->eq[j][0], value);
5751 bmap = isl_basic_map_simplify(bmap);
5752 return isl_basic_map_finalize(bmap);
5753 error:
5754 isl_basic_map_free(bmap);
5755 return NULL;
5758 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5759 enum isl_dim_type type, unsigned pos, int value)
5761 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5762 return isl_basic_map_free(bmap);
5763 return isl_basic_map_fix_pos_si(bmap,
5764 isl_basic_map_offset(bmap, type) + pos, value);
5767 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5768 enum isl_dim_type type, unsigned pos, isl_int value)
5770 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5771 return isl_basic_map_free(bmap);
5772 return isl_basic_map_fix_pos(bmap,
5773 isl_basic_map_offset(bmap, type) + pos, value);
5776 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5777 * to be equal to "v".
5779 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5780 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5782 if (!bmap || !v)
5783 goto error;
5784 if (!isl_val_is_int(v))
5785 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5786 "expecting integer value", goto error);
5787 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5788 goto error;
5789 pos += isl_basic_map_offset(bmap, type);
5790 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5791 isl_val_free(v);
5792 return bmap;
5793 error:
5794 isl_basic_map_free(bmap);
5795 isl_val_free(v);
5796 return NULL;
5799 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5800 * to be equal to "v".
5802 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5803 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5805 return isl_basic_map_fix_val(bset, type, pos, v);
5808 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5809 enum isl_dim_type type, unsigned pos, int value)
5811 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5812 type, pos, value));
5815 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5816 enum isl_dim_type type, unsigned pos, isl_int value)
5818 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5819 type, pos, value));
5822 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5823 unsigned input, int value)
5825 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5828 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5829 unsigned dim, int value)
5831 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5832 isl_dim_set, dim, value));
5835 static int remove_if_empty(__isl_keep isl_map *map, int i)
5837 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5839 if (empty < 0)
5840 return -1;
5841 if (!empty)
5842 return 0;
5844 isl_basic_map_free(map->p[i]);
5845 if (i != map->n - 1) {
5846 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5847 map->p[i] = map->p[map->n - 1];
5849 map->n--;
5851 return 0;
5854 /* Perform "fn" on each basic map of "map", where we may not be holding
5855 * the only reference to "map".
5856 * In particular, "fn" should be a semantics preserving operation
5857 * that we want to apply to all copies of "map". We therefore need
5858 * to be careful not to modify "map" in a way that breaks "map"
5859 * in case anything goes wrong.
5861 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5862 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5864 struct isl_basic_map *bmap;
5865 int i;
5867 if (!map)
5868 return NULL;
5870 for (i = map->n - 1; i >= 0; --i) {
5871 bmap = isl_basic_map_copy(map->p[i]);
5872 bmap = fn(bmap);
5873 if (!bmap)
5874 goto error;
5875 isl_basic_map_free(map->p[i]);
5876 map->p[i] = bmap;
5877 if (remove_if_empty(map, i) < 0)
5878 goto error;
5881 return map;
5882 error:
5883 isl_map_free(map);
5884 return NULL;
5887 struct isl_map *isl_map_fix_si(struct isl_map *map,
5888 enum isl_dim_type type, unsigned pos, int value)
5890 int i;
5892 map = isl_map_cow(map);
5893 if (!map)
5894 return NULL;
5896 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5897 for (i = map->n - 1; i >= 0; --i) {
5898 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5899 if (remove_if_empty(map, i) < 0)
5900 goto error;
5902 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5903 return map;
5904 error:
5905 isl_map_free(map);
5906 return NULL;
5909 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5910 enum isl_dim_type type, unsigned pos, int value)
5912 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5915 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5916 enum isl_dim_type type, unsigned pos, isl_int value)
5918 int i;
5920 map = isl_map_cow(map);
5921 if (!map)
5922 return NULL;
5924 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5925 for (i = 0; i < map->n; ++i) {
5926 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5927 if (!map->p[i])
5928 goto error;
5930 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5931 return map;
5932 error:
5933 isl_map_free(map);
5934 return NULL;
5937 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5938 enum isl_dim_type type, unsigned pos, isl_int value)
5940 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
5943 /* Fix the value of the variable at position "pos" of type "type" of "map"
5944 * to be equal to "v".
5946 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5947 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5949 int i;
5951 map = isl_map_cow(map);
5952 if (!map || !v)
5953 goto error;
5955 if (!isl_val_is_int(v))
5956 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5957 "expecting integer value", goto error);
5958 if (pos >= isl_map_dim(map, type))
5959 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5960 "index out of bounds", goto error);
5961 for (i = map->n - 1; i >= 0; --i) {
5962 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5963 isl_val_copy(v));
5964 if (remove_if_empty(map, i) < 0)
5965 goto error;
5967 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5968 isl_val_free(v);
5969 return map;
5970 error:
5971 isl_map_free(map);
5972 isl_val_free(v);
5973 return NULL;
5976 /* Fix the value of the variable at position "pos" of type "type" of "set"
5977 * to be equal to "v".
5979 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5980 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5982 return isl_map_fix_val(set, type, pos, v);
5985 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5986 unsigned input, int value)
5988 return isl_map_fix_si(map, isl_dim_in, input, value);
5991 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5993 return set_from_map(isl_map_fix_si(set_to_map(set),
5994 isl_dim_set, dim, value));
5997 static __isl_give isl_basic_map *basic_map_bound_si(
5998 __isl_take isl_basic_map *bmap,
5999 enum isl_dim_type type, unsigned pos, int value, int upper)
6001 int j;
6003 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6004 return isl_basic_map_free(bmap);
6005 pos += isl_basic_map_offset(bmap, type);
6006 bmap = isl_basic_map_cow(bmap);
6007 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6008 j = isl_basic_map_alloc_inequality(bmap);
6009 if (j < 0)
6010 goto error;
6011 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6012 if (upper) {
6013 isl_int_set_si(bmap->ineq[j][pos], -1);
6014 isl_int_set_si(bmap->ineq[j][0], value);
6015 } else {
6016 isl_int_set_si(bmap->ineq[j][pos], 1);
6017 isl_int_set_si(bmap->ineq[j][0], -value);
6019 bmap = isl_basic_map_simplify(bmap);
6020 return isl_basic_map_finalize(bmap);
6021 error:
6022 isl_basic_map_free(bmap);
6023 return NULL;
6026 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6027 __isl_take isl_basic_map *bmap,
6028 enum isl_dim_type type, unsigned pos, int value)
6030 return basic_map_bound_si(bmap, type, pos, value, 0);
6033 /* Constrain the values of the given dimension to be no greater than "value".
6035 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6036 __isl_take isl_basic_map *bmap,
6037 enum isl_dim_type type, unsigned pos, int value)
6039 return basic_map_bound_si(bmap, type, pos, value, 1);
6042 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6043 enum isl_dim_type type, unsigned pos, int value, int upper)
6045 int i;
6047 map = isl_map_cow(map);
6048 if (!map)
6049 return NULL;
6051 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6052 for (i = 0; i < map->n; ++i) {
6053 map->p[i] = basic_map_bound_si(map->p[i],
6054 type, pos, value, upper);
6055 if (!map->p[i])
6056 goto error;
6058 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6059 return map;
6060 error:
6061 isl_map_free(map);
6062 return NULL;
6065 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6066 enum isl_dim_type type, unsigned pos, int value)
6068 return map_bound_si(map, type, pos, value, 0);
6071 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6072 enum isl_dim_type type, unsigned pos, int value)
6074 return map_bound_si(map, type, pos, value, 1);
6077 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6078 enum isl_dim_type type, unsigned pos, int value)
6080 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6081 type, pos, value));
6084 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6085 enum isl_dim_type type, unsigned pos, int value)
6087 return isl_map_upper_bound_si(set, type, pos, value);
6090 /* Bound the given variable of "bmap" from below (or above is "upper"
6091 * is set) to "value".
6093 static __isl_give isl_basic_map *basic_map_bound(
6094 __isl_take isl_basic_map *bmap,
6095 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6097 int j;
6099 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6100 return isl_basic_map_free(bmap);
6101 pos += isl_basic_map_offset(bmap, type);
6102 bmap = isl_basic_map_cow(bmap);
6103 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6104 j = isl_basic_map_alloc_inequality(bmap);
6105 if (j < 0)
6106 goto error;
6107 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6108 if (upper) {
6109 isl_int_set_si(bmap->ineq[j][pos], -1);
6110 isl_int_set(bmap->ineq[j][0], value);
6111 } else {
6112 isl_int_set_si(bmap->ineq[j][pos], 1);
6113 isl_int_neg(bmap->ineq[j][0], value);
6115 bmap = isl_basic_map_simplify(bmap);
6116 return isl_basic_map_finalize(bmap);
6117 error:
6118 isl_basic_map_free(bmap);
6119 return NULL;
6122 /* Bound the given variable of "map" from below (or above is "upper"
6123 * is set) to "value".
6125 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6126 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6128 int i;
6130 map = isl_map_cow(map);
6131 if (!map)
6132 return NULL;
6134 if (pos >= isl_map_dim(map, type))
6135 isl_die(map->ctx, isl_error_invalid,
6136 "index out of bounds", goto error);
6137 for (i = map->n - 1; i >= 0; --i) {
6138 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6139 if (remove_if_empty(map, i) < 0)
6140 goto error;
6142 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6143 return map;
6144 error:
6145 isl_map_free(map);
6146 return NULL;
6149 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6150 enum isl_dim_type type, unsigned pos, isl_int value)
6152 return map_bound(map, type, pos, value, 0);
6155 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6156 enum isl_dim_type type, unsigned pos, isl_int value)
6158 return map_bound(map, type, pos, value, 1);
6161 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6162 enum isl_dim_type type, unsigned pos, isl_int value)
6164 return isl_map_lower_bound(set, type, pos, value);
6167 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6168 enum isl_dim_type type, unsigned pos, isl_int value)
6170 return isl_map_upper_bound(set, type, pos, value);
6173 /* Force the values of the variable at position "pos" of type "type" of "set"
6174 * to be no smaller than "value".
6176 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6177 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6179 if (!value)
6180 goto error;
6181 if (!isl_val_is_int(value))
6182 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6183 "expecting integer value", goto error);
6184 set = isl_set_lower_bound(set, type, pos, value->n);
6185 isl_val_free(value);
6186 return set;
6187 error:
6188 isl_val_free(value);
6189 isl_set_free(set);
6190 return NULL;
6193 /* Force the values of the variable at position "pos" of type "type" of "set"
6194 * to be no greater than "value".
6196 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6197 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6199 if (!value)
6200 goto error;
6201 if (!isl_val_is_int(value))
6202 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6203 "expecting integer value", goto error);
6204 set = isl_set_upper_bound(set, type, pos, value->n);
6205 isl_val_free(value);
6206 return set;
6207 error:
6208 isl_val_free(value);
6209 isl_set_free(set);
6210 return NULL;
6213 struct isl_map *isl_map_reverse(struct isl_map *map)
6215 int i;
6217 map = isl_map_cow(map);
6218 if (!map)
6219 return NULL;
6221 map->dim = isl_space_reverse(map->dim);
6222 if (!map->dim)
6223 goto error;
6224 for (i = 0; i < map->n; ++i) {
6225 map->p[i] = isl_basic_map_reverse(map->p[i]);
6226 if (!map->p[i])
6227 goto error;
6229 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6230 return map;
6231 error:
6232 isl_map_free(map);
6233 return NULL;
6236 #undef TYPE
6237 #define TYPE isl_pw_multi_aff
6238 #undef SUFFIX
6239 #define SUFFIX _pw_multi_aff
6240 #undef EMPTY
6241 #define EMPTY isl_pw_multi_aff_empty
6242 #undef ADD
6243 #define ADD isl_pw_multi_aff_union_add
6244 #include "isl_map_lexopt_templ.c"
6246 /* Given a map "map", compute the lexicographically minimal
6247 * (or maximal) image element for each domain element in dom,
6248 * in the form of an isl_pw_multi_aff.
6249 * If "empty" is not NULL, then set *empty to those elements in dom that
6250 * do not have an image element.
6251 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6252 * should be computed over the domain of "map". "empty" is also NULL
6253 * in this case.
6255 * We first compute the lexicographically minimal or maximal element
6256 * in the first basic map. This results in a partial solution "res"
6257 * and a subset "todo" of dom that still need to be handled.
6258 * We then consider each of the remaining maps in "map" and successively
6259 * update both "res" and "todo".
6260 * If "empty" is NULL, then the todo sets are not needed and therefore
6261 * also not computed.
6263 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6264 __isl_take isl_map *map, __isl_take isl_set *dom,
6265 __isl_give isl_set **empty, unsigned flags)
6267 int i;
6268 int full;
6269 isl_pw_multi_aff *res;
6270 isl_set *todo;
6272 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6273 if (!map || (!full && !dom))
6274 goto error;
6276 if (isl_map_plain_is_empty(map)) {
6277 if (empty)
6278 *empty = dom;
6279 else
6280 isl_set_free(dom);
6281 return isl_pw_multi_aff_from_map(map);
6284 res = basic_map_partial_lexopt_pw_multi_aff(
6285 isl_basic_map_copy(map->p[0]),
6286 isl_set_copy(dom), empty, flags);
6288 if (empty)
6289 todo = *empty;
6290 for (i = 1; i < map->n; ++i) {
6291 isl_pw_multi_aff *res_i;
6293 res_i = basic_map_partial_lexopt_pw_multi_aff(
6294 isl_basic_map_copy(map->p[i]),
6295 isl_set_copy(dom), empty, flags);
6297 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6298 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6299 else
6300 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6302 if (empty)
6303 todo = isl_set_intersect(todo, *empty);
6306 isl_set_free(dom);
6307 isl_map_free(map);
6309 if (empty)
6310 *empty = todo;
6312 return res;
6313 error:
6314 if (empty)
6315 *empty = NULL;
6316 isl_set_free(dom);
6317 isl_map_free(map);
6318 return NULL;
6321 #undef TYPE
6322 #define TYPE isl_map
6323 #undef SUFFIX
6324 #define SUFFIX
6325 #undef EMPTY
6326 #define EMPTY isl_map_empty
6327 #undef ADD
6328 #define ADD isl_map_union_disjoint
6329 #include "isl_map_lexopt_templ.c"
6331 /* Given a map "map", compute the lexicographically minimal
6332 * (or maximal) image element for each domain element in "dom",
6333 * in the form of an isl_map.
6334 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6335 * do not have an image element.
6336 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6337 * should be computed over the domain of "map". "empty" is also NULL
6338 * in this case.
6340 * If the input consists of more than one disjunct, then first
6341 * compute the desired result in the form of an isl_pw_multi_aff and
6342 * then convert that into an isl_map.
6344 * This function used to have an explicit implementation in terms
6345 * of isl_maps, but it would continually intersect the domains of
6346 * partial results with the complement of the domain of the next
6347 * partial solution, potentially leading to an explosion in the number
6348 * of disjuncts if there are several disjuncts in the input.
6349 * An even earlier implementation of this function would look for
6350 * better results in the domain of the partial result and for extra
6351 * results in the complement of this domain, which would lead to
6352 * even more splintering.
6354 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6355 __isl_take isl_map *map, __isl_take isl_set *dom,
6356 __isl_give isl_set **empty, unsigned flags)
6358 int full;
6359 struct isl_map *res;
6360 isl_pw_multi_aff *pma;
6362 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6363 if (!map || (!full && !dom))
6364 goto error;
6366 if (isl_map_plain_is_empty(map)) {
6367 if (empty)
6368 *empty = dom;
6369 else
6370 isl_set_free(dom);
6371 return map;
6374 if (map->n == 1) {
6375 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6376 dom, empty, flags);
6377 isl_map_free(map);
6378 return res;
6381 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6382 flags);
6383 return isl_map_from_pw_multi_aff(pma);
6384 error:
6385 if (empty)
6386 *empty = NULL;
6387 isl_set_free(dom);
6388 isl_map_free(map);
6389 return NULL;
6392 __isl_give isl_map *isl_map_partial_lexmax(
6393 __isl_take isl_map *map, __isl_take isl_set *dom,
6394 __isl_give isl_set **empty)
6396 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6399 __isl_give isl_map *isl_map_partial_lexmin(
6400 __isl_take isl_map *map, __isl_take isl_set *dom,
6401 __isl_give isl_set **empty)
6403 return isl_map_partial_lexopt(map, dom, empty, 0);
6406 __isl_give isl_set *isl_set_partial_lexmin(
6407 __isl_take isl_set *set, __isl_take isl_set *dom,
6408 __isl_give isl_set **empty)
6410 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6411 dom, empty));
6414 __isl_give isl_set *isl_set_partial_lexmax(
6415 __isl_take isl_set *set, __isl_take isl_set *dom,
6416 __isl_give isl_set **empty)
6418 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6419 dom, empty));
6422 /* Compute the lexicographic minimum (or maximum if "flags" includes
6423 * ISL_OPT_MAX) of "bset" over its parametric domain.
6425 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6426 unsigned flags)
6428 return isl_basic_map_lexopt(bset, flags);
6431 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6433 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6436 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6438 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6441 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6443 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6446 /* Compute the lexicographic minimum of "bset" over its parametric domain
6447 * for the purpose of quantifier elimination.
6448 * That is, find an explicit representation for all the existentially
6449 * quantified variables in "bset" by computing their lexicographic
6450 * minimum.
6452 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6453 __isl_take isl_basic_set *bset)
6455 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6458 /* Extract the first and only affine expression from list
6459 * and then add it to *pwaff with the given dom.
6460 * This domain is known to be disjoint from other domains
6461 * because of the way isl_basic_map_foreach_lexmax works.
6463 static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
6464 __isl_take isl_aff_list *list, void *user)
6466 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6467 isl_aff *aff;
6468 isl_pw_aff **pwaff = user;
6469 isl_pw_aff *pwaff_i;
6471 if (!list)
6472 goto error;
6473 if (isl_aff_list_n_aff(list) != 1)
6474 isl_die(ctx, isl_error_internal,
6475 "expecting single element list", goto error);
6477 aff = isl_aff_list_get_aff(list, 0);
6478 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6480 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6482 isl_aff_list_free(list);
6484 return isl_stat_ok;
6485 error:
6486 isl_basic_set_free(dom);
6487 isl_aff_list_free(list);
6488 return isl_stat_error;
6491 /* Given a basic map with one output dimension, compute the minimum or
6492 * maximum of that dimension as an isl_pw_aff.
6494 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6495 * call update_dim_opt on each leaf of the result.
6497 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6498 int max)
6500 isl_space *dim = isl_basic_map_get_space(bmap);
6501 isl_pw_aff *pwaff;
6502 isl_stat r;
6504 dim = isl_space_from_domain(isl_space_domain(dim));
6505 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6506 pwaff = isl_pw_aff_empty(dim);
6508 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6509 if (r < 0)
6510 return isl_pw_aff_free(pwaff);
6512 return pwaff;
6515 /* Compute the minimum or maximum of the given output dimension
6516 * as a function of the parameters and the input dimensions,
6517 * but independently of the other output dimensions.
6519 * We first project out the other output dimension and then compute
6520 * the "lexicographic" maximum in each basic map, combining the results
6521 * using isl_pw_aff_union_max.
6523 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6524 int max)
6526 int i;
6527 isl_pw_aff *pwaff;
6528 unsigned n_out;
6530 n_out = isl_map_dim(map, isl_dim_out);
6531 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6532 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6533 if (!map)
6534 return NULL;
6536 if (map->n == 0) {
6537 isl_space *dim = isl_map_get_space(map);
6538 isl_map_free(map);
6539 return isl_pw_aff_empty(dim);
6542 pwaff = basic_map_dim_opt(map->p[0], max);
6543 for (i = 1; i < map->n; ++i) {
6544 isl_pw_aff *pwaff_i;
6546 pwaff_i = basic_map_dim_opt(map->p[i], max);
6547 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6550 isl_map_free(map);
6552 return pwaff;
6555 /* Compute the minimum of the given output dimension as a function of the
6556 * parameters and input dimensions, but independently of
6557 * the other output dimensions.
6559 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6561 return map_dim_opt(map, pos, 0);
6564 /* Compute the maximum of the given output dimension as a function of the
6565 * parameters and input dimensions, but independently of
6566 * the other output dimensions.
6568 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6570 return map_dim_opt(map, pos, 1);
6573 /* Compute the minimum or maximum of the given set dimension
6574 * as a function of the parameters,
6575 * but independently of the other set dimensions.
6577 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6578 int max)
6580 return map_dim_opt(set, pos, max);
6583 /* Compute the maximum of the given set dimension as a function of the
6584 * parameters, but independently of the other set dimensions.
6586 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6588 return set_dim_opt(set, pos, 1);
6591 /* Compute the minimum of the given set dimension as a function of the
6592 * parameters, but independently of the other set dimensions.
6594 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6596 return set_dim_opt(set, pos, 0);
6599 /* Apply a preimage specified by "mat" on the parameters of "bset".
6600 * bset is assumed to have only parameters and divs.
6602 static struct isl_basic_set *basic_set_parameter_preimage(
6603 struct isl_basic_set *bset, struct isl_mat *mat)
6605 unsigned nparam;
6607 if (!bset || !mat)
6608 goto error;
6610 bset->dim = isl_space_cow(bset->dim);
6611 if (!bset->dim)
6612 goto error;
6614 nparam = isl_basic_set_dim(bset, isl_dim_param);
6616 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6618 bset->dim->nparam = 0;
6619 bset->dim->n_out = nparam;
6620 bset = isl_basic_set_preimage(bset, mat);
6621 if (bset) {
6622 bset->dim->nparam = bset->dim->n_out;
6623 bset->dim->n_out = 0;
6625 return bset;
6626 error:
6627 isl_mat_free(mat);
6628 isl_basic_set_free(bset);
6629 return NULL;
6632 /* Apply a preimage specified by "mat" on the parameters of "set".
6633 * set is assumed to have only parameters and divs.
6635 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6636 __isl_take isl_mat *mat)
6638 isl_space *space;
6639 unsigned nparam;
6641 if (!set || !mat)
6642 goto error;
6644 nparam = isl_set_dim(set, isl_dim_param);
6646 if (mat->n_row != 1 + nparam)
6647 isl_die(isl_set_get_ctx(set), isl_error_internal,
6648 "unexpected number of rows", goto error);
6650 space = isl_set_get_space(set);
6651 space = isl_space_move_dims(space, isl_dim_set, 0,
6652 isl_dim_param, 0, nparam);
6653 set = isl_set_reset_space(set, space);
6654 set = isl_set_preimage(set, mat);
6655 nparam = isl_set_dim(set, isl_dim_out);
6656 space = isl_set_get_space(set);
6657 space = isl_space_move_dims(space, isl_dim_param, 0,
6658 isl_dim_out, 0, nparam);
6659 set = isl_set_reset_space(set, space);
6660 return set;
6661 error:
6662 isl_mat_free(mat);
6663 isl_set_free(set);
6664 return NULL;
6667 /* Intersect the basic set "bset" with the affine space specified by the
6668 * equalities in "eq".
6670 static struct isl_basic_set *basic_set_append_equalities(
6671 struct isl_basic_set *bset, struct isl_mat *eq)
6673 int i, k;
6674 unsigned len;
6676 if (!bset || !eq)
6677 goto error;
6679 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6680 eq->n_row, 0);
6681 if (!bset)
6682 goto error;
6684 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6685 for (i = 0; i < eq->n_row; ++i) {
6686 k = isl_basic_set_alloc_equality(bset);
6687 if (k < 0)
6688 goto error;
6689 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6690 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6692 isl_mat_free(eq);
6694 bset = isl_basic_set_gauss(bset, NULL);
6695 bset = isl_basic_set_finalize(bset);
6697 return bset;
6698 error:
6699 isl_mat_free(eq);
6700 isl_basic_set_free(bset);
6701 return NULL;
6704 /* Intersect the set "set" with the affine space specified by the
6705 * equalities in "eq".
6707 static struct isl_set *set_append_equalities(struct isl_set *set,
6708 struct isl_mat *eq)
6710 int i;
6712 if (!set || !eq)
6713 goto error;
6715 for (i = 0; i < set->n; ++i) {
6716 set->p[i] = basic_set_append_equalities(set->p[i],
6717 isl_mat_copy(eq));
6718 if (!set->p[i])
6719 goto error;
6721 isl_mat_free(eq);
6722 return set;
6723 error:
6724 isl_mat_free(eq);
6725 isl_set_free(set);
6726 return NULL;
6729 /* Given a basic set "bset" that only involves parameters and existentially
6730 * quantified variables, return the index of the first equality
6731 * that only involves parameters. If there is no such equality then
6732 * return bset->n_eq.
6734 * This function assumes that isl_basic_set_gauss has been called on "bset".
6736 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6738 int i, j;
6739 unsigned nparam, n_div;
6741 if (!bset)
6742 return -1;
6744 nparam = isl_basic_set_dim(bset, isl_dim_param);
6745 n_div = isl_basic_set_dim(bset, isl_dim_div);
6747 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6748 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6749 ++i;
6752 return i;
6755 /* Compute an explicit representation for the existentially quantified
6756 * variables in "bset" by computing the "minimal value" of the set
6757 * variables. Since there are no set variables, the computation of
6758 * the minimal value essentially computes an explicit representation
6759 * of the non-empty part(s) of "bset".
6761 * The input only involves parameters and existentially quantified variables.
6762 * All equalities among parameters have been removed.
6764 * Since the existentially quantified variables in the result are in general
6765 * going to be different from those in the input, we first replace
6766 * them by the minimal number of variables based on their equalities.
6767 * This should simplify the parametric integer programming.
6769 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6771 isl_morph *morph1, *morph2;
6772 isl_set *set;
6773 unsigned n;
6775 if (!bset)
6776 return NULL;
6777 if (bset->n_eq == 0)
6778 return isl_basic_set_lexmin_compute_divs(bset);
6780 morph1 = isl_basic_set_parameter_compression(bset);
6781 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6782 bset = isl_basic_set_lift(bset);
6783 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6784 bset = isl_morph_basic_set(morph2, bset);
6785 n = isl_basic_set_dim(bset, isl_dim_set);
6786 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6788 set = isl_basic_set_lexmin_compute_divs(bset);
6790 set = isl_morph_set(isl_morph_inverse(morph1), set);
6792 return set;
6795 /* Project the given basic set onto its parameter domain, possibly introducing
6796 * new, explicit, existential variables in the constraints.
6797 * The input has parameters and (possibly implicit) existential variables.
6798 * The output has the same parameters, but only
6799 * explicit existentially quantified variables.
6801 * The actual projection is performed by pip, but pip doesn't seem
6802 * to like equalities very much, so we first remove the equalities
6803 * among the parameters by performing a variable compression on
6804 * the parameters. Afterward, an inverse transformation is performed
6805 * and the equalities among the parameters are inserted back in.
6807 * The variable compression on the parameters may uncover additional
6808 * equalities that were only implicit before. We therefore check
6809 * if there are any new parameter equalities in the result and
6810 * if so recurse. The removal of parameter equalities is required
6811 * for the parameter compression performed by base_compute_divs.
6813 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6815 int i;
6816 struct isl_mat *eq;
6817 struct isl_mat *T, *T2;
6818 struct isl_set *set;
6819 unsigned nparam;
6821 bset = isl_basic_set_cow(bset);
6822 if (!bset)
6823 return NULL;
6825 if (bset->n_eq == 0)
6826 return base_compute_divs(bset);
6828 bset = isl_basic_set_gauss(bset, NULL);
6829 if (!bset)
6830 return NULL;
6831 if (isl_basic_set_plain_is_empty(bset))
6832 return isl_set_from_basic_set(bset);
6834 i = first_parameter_equality(bset);
6835 if (i == bset->n_eq)
6836 return base_compute_divs(bset);
6838 nparam = isl_basic_set_dim(bset, isl_dim_param);
6839 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6840 0, 1 + nparam);
6841 eq = isl_mat_cow(eq);
6842 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6843 if (T && T->n_col == 0) {
6844 isl_mat_free(T);
6845 isl_mat_free(T2);
6846 isl_mat_free(eq);
6847 bset = isl_basic_set_set_to_empty(bset);
6848 return isl_set_from_basic_set(bset);
6850 bset = basic_set_parameter_preimage(bset, T);
6852 i = first_parameter_equality(bset);
6853 if (!bset)
6854 set = NULL;
6855 else if (i == bset->n_eq)
6856 set = base_compute_divs(bset);
6857 else
6858 set = parameter_compute_divs(bset);
6859 set = set_parameter_preimage(set, T2);
6860 set = set_append_equalities(set, eq);
6861 return set;
6864 /* Insert the divs from "ls" before those of "bmap".
6866 * The number of columns is not changed, which means that the last
6867 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6868 * The caller is responsible for removing the same number of dimensions
6869 * from the space of "bmap".
6871 static __isl_give isl_basic_map *insert_divs_from_local_space(
6872 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6874 int i;
6875 int n_div;
6876 int old_n_div;
6878 n_div = isl_local_space_dim(ls, isl_dim_div);
6879 if (n_div == 0)
6880 return bmap;
6882 old_n_div = bmap->n_div;
6883 bmap = insert_div_rows(bmap, n_div);
6884 if (!bmap)
6885 return NULL;
6887 for (i = 0; i < n_div; ++i) {
6888 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6889 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6892 return bmap;
6895 /* Replace the space of "bmap" by the space and divs of "ls".
6897 * If "ls" has any divs, then we simplify the result since we may
6898 * have discovered some additional equalities that could simplify
6899 * the div expressions.
6901 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6902 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6904 int n_div;
6906 bmap = isl_basic_map_cow(bmap);
6907 if (!bmap || !ls)
6908 goto error;
6910 n_div = isl_local_space_dim(ls, isl_dim_div);
6911 bmap = insert_divs_from_local_space(bmap, ls);
6912 if (!bmap)
6913 goto error;
6915 isl_space_free(bmap->dim);
6916 bmap->dim = isl_local_space_get_space(ls);
6917 if (!bmap->dim)
6918 goto error;
6920 isl_local_space_free(ls);
6921 if (n_div > 0)
6922 bmap = isl_basic_map_simplify(bmap);
6923 bmap = isl_basic_map_finalize(bmap);
6924 return bmap;
6925 error:
6926 isl_basic_map_free(bmap);
6927 isl_local_space_free(ls);
6928 return NULL;
6931 /* Replace the space of "map" by the space and divs of "ls".
6933 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6934 __isl_take isl_local_space *ls)
6936 int i;
6938 map = isl_map_cow(map);
6939 if (!map || !ls)
6940 goto error;
6942 for (i = 0; i < map->n; ++i) {
6943 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6944 isl_local_space_copy(ls));
6945 if (!map->p[i])
6946 goto error;
6948 isl_space_free(map->dim);
6949 map->dim = isl_local_space_get_space(ls);
6950 if (!map->dim)
6951 goto error;
6953 isl_local_space_free(ls);
6954 return map;
6955 error:
6956 isl_local_space_free(ls);
6957 isl_map_free(map);
6958 return NULL;
6961 /* Compute an explicit representation for the existentially
6962 * quantified variables for which do not know any explicit representation yet.
6964 * We first sort the existentially quantified variables so that the
6965 * existentially quantified variables for which we already have an explicit
6966 * representation are placed before those for which we do not.
6967 * The input dimensions, the output dimensions and the existentially
6968 * quantified variables for which we already have an explicit
6969 * representation are then turned into parameters.
6970 * compute_divs returns a map with the same parameters and
6971 * no input or output dimensions and the dimension specification
6972 * is reset to that of the input, including the existentially quantified
6973 * variables for which we already had an explicit representation.
6975 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6977 struct isl_basic_set *bset;
6978 struct isl_set *set;
6979 struct isl_map *map;
6980 isl_space *dim;
6981 isl_local_space *ls;
6982 unsigned nparam;
6983 unsigned n_in;
6984 unsigned n_out;
6985 int n_known;
6986 int i;
6988 bmap = isl_basic_map_sort_divs(bmap);
6989 bmap = isl_basic_map_cow(bmap);
6990 if (!bmap)
6991 return NULL;
6993 n_known = isl_basic_map_first_unknown_div(bmap);
6994 if (n_known < 0)
6995 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6997 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6998 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6999 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7000 dim = isl_space_set_alloc(bmap->ctx,
7001 nparam + n_in + n_out + n_known, 0);
7002 if (!dim)
7003 goto error;
7005 ls = isl_basic_map_get_local_space(bmap);
7006 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7007 n_known, bmap->n_div - n_known);
7008 if (n_known > 0) {
7009 for (i = n_known; i < bmap->n_div; ++i)
7010 swap_div(bmap, i - n_known, i);
7011 bmap->n_div -= n_known;
7012 bmap->extra -= n_known;
7014 bmap = isl_basic_map_reset_space(bmap, dim);
7015 bset = bset_from_bmap(bmap);
7017 set = parameter_compute_divs(bset);
7018 map = set_to_map(set);
7019 map = replace_space_by_local_space(map, ls);
7021 return map;
7022 error:
7023 isl_basic_map_free(bmap);
7024 return NULL;
7027 /* Remove the explicit representation of local variable "div",
7028 * if there is any.
7030 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7031 __isl_take isl_basic_map *bmap, int div)
7033 isl_bool unknown;
7035 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7036 if (unknown < 0)
7037 return isl_basic_map_free(bmap);
7038 if (unknown)
7039 return bmap;
7041 bmap = isl_basic_map_cow(bmap);
7042 if (!bmap)
7043 return NULL;
7044 isl_int_set_si(bmap->div[div][0], 0);
7045 return bmap;
7048 /* Is local variable "div" of "bmap" marked as not having an explicit
7049 * representation?
7050 * Note that even if "div" is not marked in this way and therefore
7051 * has an explicit representation, this representation may still
7052 * depend (indirectly) on other local variables that do not
7053 * have an explicit representation.
7055 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7056 int div)
7058 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7059 return isl_bool_error;
7060 return isl_int_is_zero(bmap->div[div][0]);
7063 /* Return the position of the first local variable that does not
7064 * have an explicit representation.
7065 * Return the total number of local variables if they all have
7066 * an explicit representation.
7067 * Return -1 on error.
7069 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7071 int i;
7073 if (!bmap)
7074 return -1;
7076 for (i = 0; i < bmap->n_div; ++i) {
7077 if (!isl_basic_map_div_is_known(bmap, i))
7078 return i;
7080 return bmap->n_div;
7083 /* Return the position of the first local variable that does not
7084 * have an explicit representation.
7085 * Return the total number of local variables if they all have
7086 * an explicit representation.
7087 * Return -1 on error.
7089 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7091 return isl_basic_map_first_unknown_div(bset);
7094 /* Does "bmap" have an explicit representation for all local variables?
7096 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7098 int first, n;
7100 n = isl_basic_map_dim(bmap, isl_dim_div);
7101 first = isl_basic_map_first_unknown_div(bmap);
7102 if (first < 0)
7103 return isl_bool_error;
7104 return first == n;
7107 /* Do all basic maps in "map" have an explicit representation
7108 * for all local variables?
7110 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7112 int i;
7114 if (!map)
7115 return isl_bool_error;
7117 for (i = 0; i < map->n; ++i) {
7118 int known = isl_basic_map_divs_known(map->p[i]);
7119 if (known <= 0)
7120 return known;
7123 return isl_bool_true;
7126 /* If bmap contains any unknown divs, then compute explicit
7127 * expressions for them. However, this computation may be
7128 * quite expensive, so first try to remove divs that aren't
7129 * strictly needed.
7131 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7133 int known;
7134 struct isl_map *map;
7136 known = isl_basic_map_divs_known(bmap);
7137 if (known < 0)
7138 goto error;
7139 if (known)
7140 return isl_map_from_basic_map(bmap);
7142 bmap = isl_basic_map_drop_redundant_divs(bmap);
7144 known = isl_basic_map_divs_known(bmap);
7145 if (known < 0)
7146 goto error;
7147 if (known)
7148 return isl_map_from_basic_map(bmap);
7150 map = compute_divs(bmap);
7151 return map;
7152 error:
7153 isl_basic_map_free(bmap);
7154 return NULL;
7157 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7159 int i;
7160 int known;
7161 struct isl_map *res;
7163 if (!map)
7164 return NULL;
7165 if (map->n == 0)
7166 return map;
7168 known = isl_map_divs_known(map);
7169 if (known < 0) {
7170 isl_map_free(map);
7171 return NULL;
7173 if (known)
7174 return map;
7176 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7177 for (i = 1 ; i < map->n; ++i) {
7178 struct isl_map *r2;
7179 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7180 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7181 res = isl_map_union_disjoint(res, r2);
7182 else
7183 res = isl_map_union(res, r2);
7185 isl_map_free(map);
7187 return res;
7190 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7192 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7195 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7197 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7200 struct isl_set *isl_map_domain(struct isl_map *map)
7202 int i;
7203 struct isl_set *set;
7205 if (!map)
7206 goto error;
7208 map = isl_map_cow(map);
7209 if (!map)
7210 return NULL;
7212 set = set_from_map(map);
7213 set->dim = isl_space_domain(set->dim);
7214 if (!set->dim)
7215 goto error;
7216 for (i = 0; i < map->n; ++i) {
7217 set->p[i] = isl_basic_map_domain(map->p[i]);
7218 if (!set->p[i])
7219 goto error;
7221 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7222 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7223 return set;
7224 error:
7225 isl_map_free(map);
7226 return NULL;
7229 /* Return the union of "map1" and "map2", where we assume for now that
7230 * "map1" and "map2" are disjoint. Note that the basic maps inside
7231 * "map1" or "map2" may not be disjoint from each other.
7232 * Also note that this function is also called from isl_map_union,
7233 * which takes care of handling the situation where "map1" and "map2"
7234 * may not be disjoint.
7236 * If one of the inputs is empty, we can simply return the other input.
7237 * Similarly, if one of the inputs is universal, then it is equal to the union.
7239 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7240 __isl_take isl_map *map2)
7242 int i;
7243 unsigned flags = 0;
7244 struct isl_map *map = NULL;
7245 int is_universe;
7247 if (!map1 || !map2)
7248 goto error;
7250 if (!isl_space_is_equal(map1->dim, map2->dim))
7251 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7252 "spaces don't match", goto error);
7254 if (map1->n == 0) {
7255 isl_map_free(map1);
7256 return map2;
7258 if (map2->n == 0) {
7259 isl_map_free(map2);
7260 return map1;
7263 is_universe = isl_map_plain_is_universe(map1);
7264 if (is_universe < 0)
7265 goto error;
7266 if (is_universe) {
7267 isl_map_free(map2);
7268 return map1;
7271 is_universe = isl_map_plain_is_universe(map2);
7272 if (is_universe < 0)
7273 goto error;
7274 if (is_universe) {
7275 isl_map_free(map1);
7276 return map2;
7279 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7280 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7281 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7283 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7284 map1->n + map2->n, flags);
7285 if (!map)
7286 goto error;
7287 for (i = 0; i < map1->n; ++i) {
7288 map = isl_map_add_basic_map(map,
7289 isl_basic_map_copy(map1->p[i]));
7290 if (!map)
7291 goto error;
7293 for (i = 0; i < map2->n; ++i) {
7294 map = isl_map_add_basic_map(map,
7295 isl_basic_map_copy(map2->p[i]));
7296 if (!map)
7297 goto error;
7299 isl_map_free(map1);
7300 isl_map_free(map2);
7301 return map;
7302 error:
7303 isl_map_free(map);
7304 isl_map_free(map1);
7305 isl_map_free(map2);
7306 return NULL;
7309 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7310 * guaranteed to be disjoint by the caller.
7312 * Note that this functions is called from within isl_map_make_disjoint,
7313 * so we have to be careful not to touch the constraints of the inputs
7314 * in any way.
7316 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7317 __isl_take isl_map *map2)
7319 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7322 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7323 * not be disjoint. The parameters are assumed to have been aligned.
7325 * We currently simply call map_union_disjoint, the internal operation
7326 * of which does not really depend on the inputs being disjoint.
7327 * If the result contains more than one basic map, then we clear
7328 * the disjoint flag since the result may contain basic maps from
7329 * both inputs and these are not guaranteed to be disjoint.
7331 * As a special case, if "map1" and "map2" are obviously equal,
7332 * then we simply return "map1".
7334 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7335 __isl_take isl_map *map2)
7337 int equal;
7339 if (!map1 || !map2)
7340 goto error;
7342 equal = isl_map_plain_is_equal(map1, map2);
7343 if (equal < 0)
7344 goto error;
7345 if (equal) {
7346 isl_map_free(map2);
7347 return map1;
7350 map1 = map_union_disjoint(map1, map2);
7351 if (!map1)
7352 return NULL;
7353 if (map1->n > 1)
7354 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7355 return map1;
7356 error:
7357 isl_map_free(map1);
7358 isl_map_free(map2);
7359 return NULL;
7362 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7363 * not be disjoint.
7365 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7366 __isl_take isl_map *map2)
7368 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7371 struct isl_set *isl_set_union_disjoint(
7372 struct isl_set *set1, struct isl_set *set2)
7374 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7375 set_to_map(set2)));
7378 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7380 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7383 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7384 * the results.
7386 * "map" and "set" are assumed to be compatible and non-NULL.
7388 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7389 __isl_take isl_set *set,
7390 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7391 __isl_take isl_basic_set *bset))
7393 unsigned flags = 0;
7394 struct isl_map *result;
7395 int i, j;
7397 if (isl_set_plain_is_universe(set)) {
7398 isl_set_free(set);
7399 return map;
7402 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7403 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7404 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7406 result = isl_map_alloc_space(isl_space_copy(map->dim),
7407 map->n * set->n, flags);
7408 for (i = 0; result && i < map->n; ++i)
7409 for (j = 0; j < set->n; ++j) {
7410 result = isl_map_add_basic_map(result,
7411 fn(isl_basic_map_copy(map->p[i]),
7412 isl_basic_set_copy(set->p[j])));
7413 if (!result)
7414 break;
7417 isl_map_free(map);
7418 isl_set_free(set);
7419 return result;
7422 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7423 __isl_take isl_set *set)
7425 isl_bool ok;
7427 ok = isl_map_compatible_range(map, set);
7428 if (ok < 0)
7429 goto error;
7430 if (!ok)
7431 isl_die(set->ctx, isl_error_invalid,
7432 "incompatible spaces", goto error);
7434 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7435 error:
7436 isl_map_free(map);
7437 isl_set_free(set);
7438 return NULL;
7441 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7442 __isl_take isl_set *set)
7444 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7447 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7448 __isl_take isl_set *set)
7450 isl_bool ok;
7452 ok = isl_map_compatible_domain(map, set);
7453 if (ok < 0)
7454 goto error;
7455 if (!ok)
7456 isl_die(set->ctx, isl_error_invalid,
7457 "incompatible spaces", goto error);
7459 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7460 error:
7461 isl_map_free(map);
7462 isl_set_free(set);
7463 return NULL;
7466 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7467 __isl_take isl_set *set)
7469 return isl_map_align_params_map_map_and(map, set,
7470 &map_intersect_domain);
7473 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7474 __isl_take isl_map *map2)
7476 if (!map1 || !map2)
7477 goto error;
7478 map1 = isl_map_reverse(map1);
7479 map1 = isl_map_apply_range(map1, map2);
7480 return isl_map_reverse(map1);
7481 error:
7482 isl_map_free(map1);
7483 isl_map_free(map2);
7484 return NULL;
7487 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7488 __isl_take isl_map *map2)
7490 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7493 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7494 __isl_take isl_map *map2)
7496 isl_space *dim_result;
7497 struct isl_map *result;
7498 int i, j;
7500 if (!map1 || !map2)
7501 goto error;
7503 dim_result = isl_space_join(isl_space_copy(map1->dim),
7504 isl_space_copy(map2->dim));
7506 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7507 if (!result)
7508 goto error;
7509 for (i = 0; i < map1->n; ++i)
7510 for (j = 0; j < map2->n; ++j) {
7511 result = isl_map_add_basic_map(result,
7512 isl_basic_map_apply_range(
7513 isl_basic_map_copy(map1->p[i]),
7514 isl_basic_map_copy(map2->p[j])));
7515 if (!result)
7516 goto error;
7518 isl_map_free(map1);
7519 isl_map_free(map2);
7520 if (result && result->n <= 1)
7521 ISL_F_SET(result, ISL_MAP_DISJOINT);
7522 return result;
7523 error:
7524 isl_map_free(map1);
7525 isl_map_free(map2);
7526 return NULL;
7529 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7530 __isl_take isl_map *map2)
7532 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7536 * returns range - domain
7538 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7540 isl_space *target_space;
7541 struct isl_basic_set *bset;
7542 unsigned dim;
7543 unsigned nparam;
7544 int i;
7546 if (!bmap)
7547 goto error;
7548 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7549 bmap->dim, isl_dim_out),
7550 goto error);
7551 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7552 dim = isl_basic_map_n_in(bmap);
7553 nparam = isl_basic_map_n_param(bmap);
7554 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7555 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7556 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7557 for (i = 0; i < dim; ++i) {
7558 int j = isl_basic_map_alloc_equality(bmap);
7559 if (j < 0) {
7560 bmap = isl_basic_map_free(bmap);
7561 break;
7563 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7564 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7565 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7566 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7568 bset = isl_basic_map_domain(bmap);
7569 bset = isl_basic_set_reset_space(bset, target_space);
7570 return bset;
7571 error:
7572 isl_basic_map_free(bmap);
7573 return NULL;
7577 * returns range - domain
7579 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7581 int i;
7582 isl_space *dim;
7583 struct isl_set *result;
7585 if (!map)
7586 return NULL;
7588 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7589 map->dim, isl_dim_out),
7590 goto error);
7591 dim = isl_map_get_space(map);
7592 dim = isl_space_domain(dim);
7593 result = isl_set_alloc_space(dim, map->n, 0);
7594 if (!result)
7595 goto error;
7596 for (i = 0; i < map->n; ++i)
7597 result = isl_set_add_basic_set(result,
7598 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7599 isl_map_free(map);
7600 return result;
7601 error:
7602 isl_map_free(map);
7603 return NULL;
7607 * returns [domain -> range] -> range - domain
7609 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7610 __isl_take isl_basic_map *bmap)
7612 int i, k;
7613 isl_space *dim;
7614 isl_basic_map *domain;
7615 int nparam, n;
7616 unsigned total;
7618 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7619 bmap->dim, isl_dim_out))
7620 isl_die(bmap->ctx, isl_error_invalid,
7621 "domain and range don't match", goto error);
7623 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7624 n = isl_basic_map_dim(bmap, isl_dim_in);
7626 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7627 domain = isl_basic_map_universe(dim);
7629 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7630 bmap = isl_basic_map_apply_range(bmap, domain);
7631 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7633 total = isl_basic_map_total_dim(bmap);
7635 for (i = 0; i < n; ++i) {
7636 k = isl_basic_map_alloc_equality(bmap);
7637 if (k < 0)
7638 goto error;
7639 isl_seq_clr(bmap->eq[k], 1 + total);
7640 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7641 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7642 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7645 bmap = isl_basic_map_gauss(bmap, NULL);
7646 return isl_basic_map_finalize(bmap);
7647 error:
7648 isl_basic_map_free(bmap);
7649 return NULL;
7653 * returns [domain -> range] -> range - domain
7655 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7657 int i;
7658 isl_space *domain_dim;
7660 if (!map)
7661 return NULL;
7663 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7664 map->dim, isl_dim_out))
7665 isl_die(map->ctx, isl_error_invalid,
7666 "domain and range don't match", goto error);
7668 map = isl_map_cow(map);
7669 if (!map)
7670 return NULL;
7672 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7673 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7674 map->dim = isl_space_join(map->dim, domain_dim);
7675 if (!map->dim)
7676 goto error;
7677 for (i = 0; i < map->n; ++i) {
7678 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7679 if (!map->p[i])
7680 goto error;
7682 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7683 return map;
7684 error:
7685 isl_map_free(map);
7686 return NULL;
7689 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7691 struct isl_basic_map *bmap;
7692 unsigned nparam;
7693 unsigned dim;
7694 int i;
7696 if (!dims)
7697 return NULL;
7699 nparam = dims->nparam;
7700 dim = dims->n_out;
7701 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7702 if (!bmap)
7703 goto error;
7705 for (i = 0; i < dim; ++i) {
7706 int j = isl_basic_map_alloc_equality(bmap);
7707 if (j < 0)
7708 goto error;
7709 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7710 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7711 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7713 return isl_basic_map_finalize(bmap);
7714 error:
7715 isl_basic_map_free(bmap);
7716 return NULL;
7719 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7721 if (!dim)
7722 return NULL;
7723 if (dim->n_in != dim->n_out)
7724 isl_die(dim->ctx, isl_error_invalid,
7725 "number of input and output dimensions needs to be "
7726 "the same", goto error);
7727 return basic_map_identity(dim);
7728 error:
7729 isl_space_free(dim);
7730 return NULL;
7733 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7735 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7738 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7740 isl_space *dim = isl_set_get_space(set);
7741 isl_map *id;
7742 id = isl_map_identity(isl_space_map_from_set(dim));
7743 return isl_map_intersect_range(id, set);
7746 /* Construct a basic set with all set dimensions having only non-negative
7747 * values.
7749 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7750 __isl_take isl_space *space)
7752 int i;
7753 unsigned nparam;
7754 unsigned dim;
7755 struct isl_basic_set *bset;
7757 if (!space)
7758 return NULL;
7759 nparam = space->nparam;
7760 dim = space->n_out;
7761 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7762 if (!bset)
7763 return NULL;
7764 for (i = 0; i < dim; ++i) {
7765 int k = isl_basic_set_alloc_inequality(bset);
7766 if (k < 0)
7767 goto error;
7768 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7769 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7771 return bset;
7772 error:
7773 isl_basic_set_free(bset);
7774 return NULL;
7777 /* Construct the half-space x_pos >= 0.
7779 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7780 int pos)
7782 int k;
7783 isl_basic_set *nonneg;
7785 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7786 k = isl_basic_set_alloc_inequality(nonneg);
7787 if (k < 0)
7788 goto error;
7789 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7790 isl_int_set_si(nonneg->ineq[k][pos], 1);
7792 return isl_basic_set_finalize(nonneg);
7793 error:
7794 isl_basic_set_free(nonneg);
7795 return NULL;
7798 /* Construct the half-space x_pos <= -1.
7800 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7802 int k;
7803 isl_basic_set *neg;
7805 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7806 k = isl_basic_set_alloc_inequality(neg);
7807 if (k < 0)
7808 goto error;
7809 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7810 isl_int_set_si(neg->ineq[k][0], -1);
7811 isl_int_set_si(neg->ineq[k][pos], -1);
7813 return isl_basic_set_finalize(neg);
7814 error:
7815 isl_basic_set_free(neg);
7816 return NULL;
7819 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7820 enum isl_dim_type type, unsigned first, unsigned n)
7822 int i;
7823 unsigned offset;
7824 isl_basic_set *nonneg;
7825 isl_basic_set *neg;
7827 if (!set)
7828 return NULL;
7829 if (n == 0)
7830 return set;
7832 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7834 offset = pos(set->dim, type);
7835 for (i = 0; i < n; ++i) {
7836 nonneg = nonneg_halfspace(isl_set_get_space(set),
7837 offset + first + i);
7838 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7840 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7843 return set;
7844 error:
7845 isl_set_free(set);
7846 return NULL;
7849 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7850 int len,
7851 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7852 void *user)
7854 isl_set *half;
7856 if (!set)
7857 return isl_stat_error;
7858 if (isl_set_plain_is_empty(set)) {
7859 isl_set_free(set);
7860 return isl_stat_ok;
7862 if (first == len)
7863 return fn(set, signs, user);
7865 signs[first] = 1;
7866 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7867 1 + first));
7868 half = isl_set_intersect(half, isl_set_copy(set));
7869 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7870 goto error;
7872 signs[first] = -1;
7873 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7874 1 + first));
7875 half = isl_set_intersect(half, set);
7876 return foreach_orthant(half, signs, first + 1, len, fn, user);
7877 error:
7878 isl_set_free(set);
7879 return isl_stat_error;
7882 /* Call "fn" on the intersections of "set" with each of the orthants
7883 * (except for obviously empty intersections). The orthant is identified
7884 * by the signs array, with each entry having value 1 or -1 according
7885 * to the sign of the corresponding variable.
7887 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
7888 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7889 void *user)
7891 unsigned nparam;
7892 unsigned nvar;
7893 int *signs;
7894 isl_stat r;
7896 if (!set)
7897 return isl_stat_error;
7898 if (isl_set_plain_is_empty(set))
7899 return isl_stat_ok;
7901 nparam = isl_set_dim(set, isl_dim_param);
7902 nvar = isl_set_dim(set, isl_dim_set);
7904 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7906 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7907 fn, user);
7909 free(signs);
7911 return r;
7914 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7916 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7919 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7920 __isl_keep isl_basic_map *bmap2)
7922 int is_subset;
7923 struct isl_map *map1;
7924 struct isl_map *map2;
7926 if (!bmap1 || !bmap2)
7927 return isl_bool_error;
7929 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7930 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7932 is_subset = isl_map_is_subset(map1, map2);
7934 isl_map_free(map1);
7935 isl_map_free(map2);
7937 return is_subset;
7940 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7941 __isl_keep isl_basic_set *bset2)
7943 return isl_basic_map_is_subset(bset1, bset2);
7946 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7947 __isl_keep isl_basic_map *bmap2)
7949 isl_bool is_subset;
7951 if (!bmap1 || !bmap2)
7952 return isl_bool_error;
7953 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7954 if (is_subset != isl_bool_true)
7955 return is_subset;
7956 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7957 return is_subset;
7960 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7961 __isl_keep isl_basic_set *bset2)
7963 return isl_basic_map_is_equal(
7964 bset_to_bmap(bset1), bset_to_bmap(bset2));
7967 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7969 int i;
7970 int is_empty;
7972 if (!map)
7973 return isl_bool_error;
7974 for (i = 0; i < map->n; ++i) {
7975 is_empty = isl_basic_map_is_empty(map->p[i]);
7976 if (is_empty < 0)
7977 return isl_bool_error;
7978 if (!is_empty)
7979 return isl_bool_false;
7981 return isl_bool_true;
7984 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7986 return map ? map->n == 0 : isl_bool_error;
7989 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7991 return set ? set->n == 0 : isl_bool_error;
7994 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7996 return isl_map_is_empty(set_to_map(set));
7999 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8000 __isl_keep isl_map *map2)
8002 if (!map1 || !map2)
8003 return isl_bool_error;
8005 return isl_space_is_equal(map1->dim, map2->dim);
8008 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8009 __isl_keep isl_set *set2)
8011 if (!set1 || !set2)
8012 return isl_bool_error;
8014 return isl_space_is_equal(set1->dim, set2->dim);
8017 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8019 isl_bool is_subset;
8021 if (!map1 || !map2)
8022 return isl_bool_error;
8023 is_subset = isl_map_is_subset(map1, map2);
8024 if (is_subset != isl_bool_true)
8025 return is_subset;
8026 is_subset = isl_map_is_subset(map2, map1);
8027 return is_subset;
8030 /* Is "map1" equal to "map2"?
8032 * First check if they are obviously equal.
8033 * If not, then perform a more detailed analysis.
8035 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8037 isl_bool equal;
8039 equal = isl_map_plain_is_equal(map1, map2);
8040 if (equal < 0 || equal)
8041 return equal;
8042 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8045 isl_bool isl_basic_map_is_strict_subset(
8046 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8048 isl_bool is_subset;
8050 if (!bmap1 || !bmap2)
8051 return isl_bool_error;
8052 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8053 if (is_subset != isl_bool_true)
8054 return is_subset;
8055 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8056 if (is_subset == isl_bool_error)
8057 return is_subset;
8058 return !is_subset;
8061 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8062 __isl_keep isl_map *map2)
8064 isl_bool is_subset;
8066 if (!map1 || !map2)
8067 return isl_bool_error;
8068 is_subset = isl_map_is_subset(map1, map2);
8069 if (is_subset != isl_bool_true)
8070 return is_subset;
8071 is_subset = isl_map_is_subset(map2, map1);
8072 if (is_subset == isl_bool_error)
8073 return is_subset;
8074 return !is_subset;
8077 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8078 __isl_keep isl_set *set2)
8080 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8083 /* Is "bmap" obviously equal to the universe with the same space?
8085 * That is, does it not have any constraints?
8087 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8089 if (!bmap)
8090 return isl_bool_error;
8091 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8094 /* Is "bset" obviously equal to the universe with the same space?
8096 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8098 return isl_basic_map_plain_is_universe(bset);
8101 /* If "c" does not involve any existentially quantified variables,
8102 * then set *univ to false and abort
8104 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8106 isl_bool *univ = user;
8107 unsigned n;
8109 n = isl_constraint_dim(c, isl_dim_div);
8110 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8111 isl_constraint_free(c);
8112 if (*univ < 0 || !*univ)
8113 return isl_stat_error;
8114 return isl_stat_ok;
8117 /* Is "bmap" equal to the universe with the same space?
8119 * First check if it is obviously equal to the universe.
8120 * If not and if there are any constraints not involving
8121 * existentially quantified variables, then it is certainly
8122 * not equal to the universe.
8123 * Otherwise, check if the universe is a subset of "bmap".
8125 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8127 isl_bool univ;
8128 isl_basic_map *test;
8130 univ = isl_basic_map_plain_is_universe(bmap);
8131 if (univ < 0 || univ)
8132 return univ;
8133 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8134 return isl_bool_false;
8135 univ = isl_bool_true;
8136 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8137 univ)
8138 return isl_bool_error;
8139 if (univ < 0 || !univ)
8140 return univ;
8141 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8142 univ = isl_basic_map_is_subset(test, bmap);
8143 isl_basic_map_free(test);
8144 return univ;
8147 /* Is "bset" equal to the universe with the same space?
8149 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8151 return isl_basic_map_is_universe(bset);
8154 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8156 int i;
8158 if (!map)
8159 return isl_bool_error;
8161 for (i = 0; i < map->n; ++i) {
8162 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8163 if (r < 0 || r)
8164 return r;
8167 return isl_bool_false;
8170 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8172 return isl_map_plain_is_universe(set_to_map(set));
8175 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8177 struct isl_basic_set *bset = NULL;
8178 struct isl_vec *sample = NULL;
8179 isl_bool empty, non_empty;
8181 if (!bmap)
8182 return isl_bool_error;
8184 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8185 return isl_bool_true;
8187 if (isl_basic_map_plain_is_universe(bmap))
8188 return isl_bool_false;
8190 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8191 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8192 copy = isl_basic_map_remove_redundancies(copy);
8193 empty = isl_basic_map_plain_is_empty(copy);
8194 isl_basic_map_free(copy);
8195 return empty;
8198 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8199 if (non_empty < 0)
8200 return isl_bool_error;
8201 if (non_empty)
8202 return isl_bool_false;
8203 isl_vec_free(bmap->sample);
8204 bmap->sample = NULL;
8205 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8206 if (!bset)
8207 return isl_bool_error;
8208 sample = isl_basic_set_sample_vec(bset);
8209 if (!sample)
8210 return isl_bool_error;
8211 empty = sample->size == 0;
8212 isl_vec_free(bmap->sample);
8213 bmap->sample = sample;
8214 if (empty)
8215 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8217 return empty;
8220 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8222 if (!bmap)
8223 return isl_bool_error;
8224 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8227 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8229 if (!bset)
8230 return isl_bool_error;
8231 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8234 /* Is "bmap" known to be non-empty?
8236 * That is, is the cached sample still valid?
8238 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8240 unsigned total;
8242 if (!bmap)
8243 return isl_bool_error;
8244 if (!bmap->sample)
8245 return isl_bool_false;
8246 total = 1 + isl_basic_map_total_dim(bmap);
8247 if (bmap->sample->size != total)
8248 return isl_bool_false;
8249 return isl_basic_map_contains(bmap, bmap->sample);
8252 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8254 return isl_basic_map_is_empty(bset_to_bmap(bset));
8257 struct isl_map *isl_basic_map_union(
8258 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8260 struct isl_map *map;
8261 if (!bmap1 || !bmap2)
8262 goto error;
8264 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8266 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8267 if (!map)
8268 goto error;
8269 map = isl_map_add_basic_map(map, bmap1);
8270 map = isl_map_add_basic_map(map, bmap2);
8271 return map;
8272 error:
8273 isl_basic_map_free(bmap1);
8274 isl_basic_map_free(bmap2);
8275 return NULL;
8278 struct isl_set *isl_basic_set_union(
8279 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8281 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8282 bset_to_bmap(bset2)));
8285 /* Order divs such that any div only depends on previous divs */
8286 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8288 int i;
8289 unsigned off;
8291 if (!bmap)
8292 return NULL;
8294 off = isl_space_dim(bmap->dim, isl_dim_all);
8296 for (i = 0; i < bmap->n_div; ++i) {
8297 int pos;
8298 if (isl_int_is_zero(bmap->div[i][0]))
8299 continue;
8300 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8301 bmap->n_div-i);
8302 if (pos == -1)
8303 continue;
8304 if (pos == 0)
8305 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8306 "integer division depends on itself",
8307 return isl_basic_map_free(bmap));
8308 isl_basic_map_swap_div(bmap, i, i + pos);
8309 --i;
8311 return bmap;
8314 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8316 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8319 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8321 int i;
8323 if (!map)
8324 return 0;
8326 for (i = 0; i < map->n; ++i) {
8327 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8328 if (!map->p[i])
8329 goto error;
8332 return map;
8333 error:
8334 isl_map_free(map);
8335 return NULL;
8338 /* Sort the local variables of "bset".
8340 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8341 __isl_take isl_basic_set *bset)
8343 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8346 /* Apply the expansion computed by isl_merge_divs.
8347 * The expansion itself is given by "exp" while the resulting
8348 * list of divs is given by "div".
8350 * Move the integer divisions of "bmap" into the right position
8351 * according to "exp" and then introduce the additional integer
8352 * divisions, adding div constraints.
8353 * The moving should be done first to avoid moving coefficients
8354 * in the definitions of the extra integer divisions.
8356 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8357 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8359 int i, j;
8360 int n_div;
8362 bmap = isl_basic_map_cow(bmap);
8363 if (!bmap || !div)
8364 goto error;
8366 if (div->n_row < bmap->n_div)
8367 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8368 "not an expansion", goto error);
8370 n_div = bmap->n_div;
8371 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8372 div->n_row - n_div, 0,
8373 2 * (div->n_row - n_div));
8375 for (i = n_div; i < div->n_row; ++i)
8376 if (isl_basic_map_alloc_div(bmap) < 0)
8377 goto error;
8379 for (j = n_div - 1; j >= 0; --j) {
8380 if (exp[j] == j)
8381 break;
8382 isl_basic_map_swap_div(bmap, j, exp[j]);
8384 j = 0;
8385 for (i = 0; i < div->n_row; ++i) {
8386 if (j < n_div && exp[j] == i) {
8387 j++;
8388 } else {
8389 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8390 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8391 continue;
8392 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8393 goto error;
8397 isl_mat_free(div);
8398 return bmap;
8399 error:
8400 isl_basic_map_free(bmap);
8401 isl_mat_free(div);
8402 return NULL;
8405 /* Apply the expansion computed by isl_merge_divs.
8406 * The expansion itself is given by "exp" while the resulting
8407 * list of divs is given by "div".
8409 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8410 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8412 return isl_basic_map_expand_divs(bset, div, exp);
8415 /* Look for a div in dst that corresponds to the div "div" in src.
8416 * The divs before "div" in src and dst are assumed to be the same.
8418 * Returns -1 if no corresponding div was found and the position
8419 * of the corresponding div in dst otherwise.
8421 static int find_div(struct isl_basic_map *dst,
8422 struct isl_basic_map *src, unsigned div)
8424 int i;
8426 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8428 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8429 for (i = div; i < dst->n_div; ++i)
8430 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8431 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8432 dst->n_div - div) == -1)
8433 return i;
8434 return -1;
8437 /* Align the divs of "dst" to those of "src", adding divs from "src"
8438 * if needed. That is, make sure that the first src->n_div divs
8439 * of the result are equal to those of src.
8441 * The result is not finalized as by design it will have redundant
8442 * divs if any divs from "src" were copied.
8444 __isl_give isl_basic_map *isl_basic_map_align_divs(
8445 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8447 int i;
8448 int known, extended;
8449 unsigned total;
8451 if (!dst || !src)
8452 return isl_basic_map_free(dst);
8454 if (src->n_div == 0)
8455 return dst;
8457 known = isl_basic_map_divs_known(src);
8458 if (known < 0)
8459 return isl_basic_map_free(dst);
8460 if (!known)
8461 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8462 "some src divs are unknown",
8463 return isl_basic_map_free(dst));
8465 src = isl_basic_map_order_divs(src);
8467 extended = 0;
8468 total = isl_space_dim(src->dim, isl_dim_all);
8469 for (i = 0; i < src->n_div; ++i) {
8470 int j = find_div(dst, src, i);
8471 if (j < 0) {
8472 if (!extended) {
8473 int extra = src->n_div - i;
8474 dst = isl_basic_map_cow(dst);
8475 if (!dst)
8476 return NULL;
8477 dst = isl_basic_map_extend_space(dst,
8478 isl_space_copy(dst->dim),
8479 extra, 0, 2 * extra);
8480 extended = 1;
8482 j = isl_basic_map_alloc_div(dst);
8483 if (j < 0)
8484 return isl_basic_map_free(dst);
8485 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8486 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8487 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8488 return isl_basic_map_free(dst);
8490 if (j != i)
8491 isl_basic_map_swap_div(dst, i, j);
8493 return dst;
8496 struct isl_map *isl_map_align_divs(struct isl_map *map)
8498 int i;
8500 if (!map)
8501 return NULL;
8502 if (map->n == 0)
8503 return map;
8504 map = isl_map_compute_divs(map);
8505 map = isl_map_cow(map);
8506 if (!map)
8507 return NULL;
8509 for (i = 1; i < map->n; ++i)
8510 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8511 for (i = 1; i < map->n; ++i) {
8512 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8513 if (!map->p[i])
8514 return isl_map_free(map);
8517 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8518 return map;
8521 struct isl_set *isl_set_align_divs(struct isl_set *set)
8523 return set_from_map(isl_map_align_divs(set_to_map(set)));
8526 /* Align the divs of the basic maps in "map" to those
8527 * of the basic maps in "list", as well as to the other basic maps in "map".
8528 * The elements in "list" are assumed to have known divs.
8530 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8531 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8533 int i, n;
8535 map = isl_map_compute_divs(map);
8536 map = isl_map_cow(map);
8537 if (!map || !list)
8538 return isl_map_free(map);
8539 if (map->n == 0)
8540 return map;
8542 n = isl_basic_map_list_n_basic_map(list);
8543 for (i = 0; i < n; ++i) {
8544 isl_basic_map *bmap;
8546 bmap = isl_basic_map_list_get_basic_map(list, i);
8547 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8548 isl_basic_map_free(bmap);
8550 if (!map->p[0])
8551 return isl_map_free(map);
8553 return isl_map_align_divs(map);
8556 /* Align the divs of each element of "list" to those of "bmap".
8557 * Both "bmap" and the elements of "list" are assumed to have known divs.
8559 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8560 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8562 int i, n;
8564 if (!list || !bmap)
8565 return isl_basic_map_list_free(list);
8567 n = isl_basic_map_list_n_basic_map(list);
8568 for (i = 0; i < n; ++i) {
8569 isl_basic_map *bmap_i;
8571 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8572 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8573 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8576 return list;
8579 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8580 __isl_take isl_map *map)
8582 isl_bool ok;
8584 ok = isl_map_compatible_domain(map, set);
8585 if (ok < 0)
8586 goto error;
8587 if (!ok)
8588 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8589 "incompatible spaces", goto error);
8590 map = isl_map_intersect_domain(map, set);
8591 set = isl_map_range(map);
8592 return set;
8593 error:
8594 isl_set_free(set);
8595 isl_map_free(map);
8596 return NULL;
8599 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8600 __isl_take isl_map *map)
8602 return isl_map_align_params_map_map_and(set, map, &set_apply);
8605 /* There is no need to cow as removing empty parts doesn't change
8606 * the meaning of the set.
8608 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8610 int i;
8612 if (!map)
8613 return NULL;
8615 for (i = map->n - 1; i >= 0; --i)
8616 remove_if_empty(map, i);
8618 return map;
8621 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8623 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8626 static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
8628 struct isl_basic_map *bmap;
8629 if (!map || map->n == 0)
8630 return NULL;
8631 bmap = map->p[map->n-1];
8632 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8633 return isl_basic_map_copy(bmap);
8636 __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
8638 return map_copy_basic_map(map);
8641 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8643 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8646 static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
8647 __isl_keep isl_basic_map *bmap)
8649 int i;
8651 if (!map || !bmap)
8652 goto error;
8653 for (i = map->n-1; i >= 0; --i) {
8654 if (map->p[i] != bmap)
8655 continue;
8656 map = isl_map_cow(map);
8657 if (!map)
8658 goto error;
8659 isl_basic_map_free(map->p[i]);
8660 if (i != map->n-1) {
8661 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8662 map->p[i] = map->p[map->n-1];
8664 map->n--;
8665 return map;
8667 return map;
8668 error:
8669 isl_map_free(map);
8670 return NULL;
8673 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8674 __isl_keep isl_basic_map *bmap)
8676 return map_drop_basic_map(map, bmap);
8679 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8680 struct isl_basic_set *bset)
8682 return set_from_map(map_drop_basic_map(set_to_map(set),
8683 bset_to_bmap(bset)));
8686 /* Given two basic sets bset1 and bset2, compute the maximal difference
8687 * between the values of dimension pos in bset1 and those in bset2
8688 * for any common value of the parameters and dimensions preceding pos.
8690 static enum isl_lp_result basic_set_maximal_difference_at(
8691 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8692 int pos, isl_int *opt)
8694 isl_basic_map *bmap1;
8695 isl_basic_map *bmap2;
8696 struct isl_ctx *ctx;
8697 struct isl_vec *obj;
8698 unsigned total;
8699 unsigned nparam;
8700 unsigned dim1;
8701 enum isl_lp_result res;
8703 if (!bset1 || !bset2)
8704 return isl_lp_error;
8706 nparam = isl_basic_set_n_param(bset1);
8707 dim1 = isl_basic_set_n_dim(bset1);
8709 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8710 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8711 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8712 isl_dim_out, 0, pos);
8713 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8714 isl_dim_out, 0, pos);
8715 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8716 if (!bmap1)
8717 return isl_lp_error;
8719 total = isl_basic_map_total_dim(bmap1);
8720 ctx = bmap1->ctx;
8721 obj = isl_vec_alloc(ctx, 1 + total);
8722 if (!obj)
8723 goto error;
8724 isl_seq_clr(obj->block.data, 1 + total);
8725 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8726 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8727 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8728 opt, NULL, NULL);
8729 isl_basic_map_free(bmap1);
8730 isl_vec_free(obj);
8731 return res;
8732 error:
8733 isl_basic_map_free(bmap1);
8734 return isl_lp_error;
8737 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8738 * for any common value of the parameters and dimensions preceding pos
8739 * in both basic sets, the values of dimension pos in bset1 are
8740 * smaller or larger than those in bset2.
8742 * Returns
8743 * 1 if bset1 follows bset2
8744 * -1 if bset1 precedes bset2
8745 * 0 if bset1 and bset2 are incomparable
8746 * -2 if some error occurred.
8748 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8749 struct isl_basic_set *bset2, int pos)
8751 isl_int opt;
8752 enum isl_lp_result res;
8753 int cmp;
8755 isl_int_init(opt);
8757 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8759 if (res == isl_lp_empty)
8760 cmp = 0;
8761 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8762 res == isl_lp_unbounded)
8763 cmp = 1;
8764 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8765 cmp = -1;
8766 else
8767 cmp = -2;
8769 isl_int_clear(opt);
8770 return cmp;
8773 /* Given two basic sets bset1 and bset2, check whether
8774 * for any common value of the parameters and dimensions preceding pos
8775 * there is a value of dimension pos in bset1 that is larger
8776 * than a value of the same dimension in bset2.
8778 * Return
8779 * 1 if there exists such a pair
8780 * 0 if there is no such pair, but there is a pair of equal values
8781 * -1 otherwise
8782 * -2 if some error occurred.
8784 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8785 __isl_keep isl_basic_set *bset2, int pos)
8787 isl_int opt;
8788 enum isl_lp_result res;
8789 int cmp;
8791 isl_int_init(opt);
8793 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8795 if (res == isl_lp_empty)
8796 cmp = -1;
8797 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8798 res == isl_lp_unbounded)
8799 cmp = 1;
8800 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8801 cmp = -1;
8802 else if (res == isl_lp_ok)
8803 cmp = 0;
8804 else
8805 cmp = -2;
8807 isl_int_clear(opt);
8808 return cmp;
8811 /* Given two sets set1 and set2, check whether
8812 * for any common value of the parameters and dimensions preceding pos
8813 * there is a value of dimension pos in set1 that is larger
8814 * than a value of the same dimension in set2.
8816 * Return
8817 * 1 if there exists such a pair
8818 * 0 if there is no such pair, but there is a pair of equal values
8819 * -1 otherwise
8820 * -2 if some error occurred.
8822 int isl_set_follows_at(__isl_keep isl_set *set1,
8823 __isl_keep isl_set *set2, int pos)
8825 int i, j;
8826 int follows = -1;
8828 if (!set1 || !set2)
8829 return -2;
8831 for (i = 0; i < set1->n; ++i)
8832 for (j = 0; j < set2->n; ++j) {
8833 int f;
8834 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8835 if (f == 1 || f == -2)
8836 return f;
8837 if (f > follows)
8838 follows = f;
8841 return follows;
8844 static isl_bool isl_basic_map_plain_has_fixed_var(
8845 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
8847 int i;
8848 int d;
8849 unsigned total;
8851 if (!bmap)
8852 return isl_bool_error;
8853 total = isl_basic_map_total_dim(bmap);
8854 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8855 for (; d+1 > pos; --d)
8856 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8857 break;
8858 if (d != pos)
8859 continue;
8860 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8861 return isl_bool_false;
8862 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8863 return isl_bool_false;
8864 if (!isl_int_is_one(bmap->eq[i][1+d]))
8865 return isl_bool_false;
8866 if (val)
8867 isl_int_neg(*val, bmap->eq[i][0]);
8868 return isl_bool_true;
8870 return isl_bool_false;
8873 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8874 unsigned pos, isl_int *val)
8876 int i;
8877 isl_int v;
8878 isl_int tmp;
8879 isl_bool fixed;
8881 if (!map)
8882 return isl_bool_error;
8883 if (map->n == 0)
8884 return isl_bool_false;
8885 if (map->n == 1)
8886 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8887 isl_int_init(v);
8888 isl_int_init(tmp);
8889 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8890 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
8891 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8892 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
8893 fixed = isl_bool_false;
8895 if (val)
8896 isl_int_set(*val, v);
8897 isl_int_clear(tmp);
8898 isl_int_clear(v);
8899 return fixed;
8902 static isl_bool isl_basic_set_plain_has_fixed_var(
8903 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
8905 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8906 pos, val);
8909 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8910 enum isl_dim_type type, unsigned pos, isl_int *val)
8912 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
8913 return isl_bool_error;
8914 return isl_basic_map_plain_has_fixed_var(bmap,
8915 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8918 /* If "bmap" obviously lies on a hyperplane where the given dimension
8919 * has a fixed value, then return that value.
8920 * Otherwise return NaN.
8922 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8923 __isl_keep isl_basic_map *bmap,
8924 enum isl_dim_type type, unsigned pos)
8926 isl_ctx *ctx;
8927 isl_val *v;
8928 isl_bool fixed;
8930 if (!bmap)
8931 return NULL;
8932 ctx = isl_basic_map_get_ctx(bmap);
8933 v = isl_val_alloc(ctx);
8934 if (!v)
8935 return NULL;
8936 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8937 if (fixed < 0)
8938 return isl_val_free(v);
8939 if (fixed) {
8940 isl_int_set_si(v->d, 1);
8941 return v;
8943 isl_val_free(v);
8944 return isl_val_nan(ctx);
8947 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
8948 enum isl_dim_type type, unsigned pos, isl_int *val)
8950 if (pos >= isl_map_dim(map, type))
8951 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8952 "position out of bounds", return isl_bool_error);
8953 return isl_map_plain_has_fixed_var(map,
8954 map_offset(map, type) - 1 + pos, val);
8957 /* If "map" obviously lies on a hyperplane where the given dimension
8958 * has a fixed value, then return that value.
8959 * Otherwise return NaN.
8961 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8962 enum isl_dim_type type, unsigned pos)
8964 isl_ctx *ctx;
8965 isl_val *v;
8966 isl_bool fixed;
8968 if (!map)
8969 return NULL;
8970 ctx = isl_map_get_ctx(map);
8971 v = isl_val_alloc(ctx);
8972 if (!v)
8973 return NULL;
8974 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8975 if (fixed < 0)
8976 return isl_val_free(v);
8977 if (fixed) {
8978 isl_int_set_si(v->d, 1);
8979 return v;
8981 isl_val_free(v);
8982 return isl_val_nan(ctx);
8985 /* If "set" obviously lies on a hyperplane where the given dimension
8986 * has a fixed value, then return that value.
8987 * Otherwise return NaN.
8989 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8990 enum isl_dim_type type, unsigned pos)
8992 return isl_map_plain_get_val_if_fixed(set, type, pos);
8995 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
8996 enum isl_dim_type type, unsigned pos, isl_int *val)
8998 return isl_map_plain_is_fixed(set, type, pos, val);
9001 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9002 * then return this fixed value in *val.
9004 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9005 unsigned dim, isl_int *val)
9007 return isl_basic_set_plain_has_fixed_var(bset,
9008 isl_basic_set_n_param(bset) + dim, val);
9011 /* Return -1 if the constraint "c1" should be sorted before "c2"
9012 * and 1 if it should be sorted after "c2".
9013 * Return 0 if the two constraints are the same (up to the constant term).
9015 * In particular, if a constraint involves later variables than another
9016 * then it is sorted after this other constraint.
9017 * uset_gist depends on constraints without existentially quantified
9018 * variables sorting first.
9020 * For constraints that have the same latest variable, those
9021 * with the same coefficient for this latest variable (first in absolute value
9022 * and then in actual value) are grouped together.
9023 * This is useful for detecting pairs of constraints that can
9024 * be chained in their printed representation.
9026 * Finally, within a group, constraints are sorted according to
9027 * their coefficients (excluding the constant term).
9029 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9031 isl_int **c1 = (isl_int **) p1;
9032 isl_int **c2 = (isl_int **) p2;
9033 int l1, l2;
9034 unsigned size = *(unsigned *) arg;
9035 int cmp;
9037 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9038 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9040 if (l1 != l2)
9041 return l1 - l2;
9043 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9044 if (cmp != 0)
9045 return cmp;
9046 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9047 if (cmp != 0)
9048 return -cmp;
9050 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9053 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9054 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9055 * and 0 if the two constraints are the same (up to the constant term).
9057 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9058 isl_int *c1, isl_int *c2)
9060 unsigned total;
9062 if (!bmap)
9063 return -2;
9064 total = isl_basic_map_total_dim(bmap);
9065 return sort_constraint_cmp(&c1, &c2, &total);
9068 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9069 __isl_take isl_basic_map *bmap)
9071 unsigned total;
9073 if (!bmap)
9074 return NULL;
9075 if (bmap->n_ineq == 0)
9076 return bmap;
9077 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9078 return bmap;
9079 total = isl_basic_map_total_dim(bmap);
9080 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9081 &sort_constraint_cmp, &total) < 0)
9082 return isl_basic_map_free(bmap);
9083 return bmap;
9086 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9087 __isl_take isl_basic_set *bset)
9089 isl_basic_map *bmap = bset_to_bmap(bset);
9090 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9093 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9095 if (!bmap)
9096 return NULL;
9097 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9098 return bmap;
9099 bmap = isl_basic_map_remove_redundancies(bmap);
9100 bmap = isl_basic_map_sort_constraints(bmap);
9101 if (bmap)
9102 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9103 return bmap;
9105 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9106 __isl_keep isl_basic_map *bmap2)
9108 int i, cmp;
9109 unsigned total;
9111 if (!bmap1 || !bmap2)
9112 return -1;
9114 if (bmap1 == bmap2)
9115 return 0;
9116 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9117 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9118 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9119 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9120 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9121 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9122 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9123 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9124 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9125 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9126 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9127 return 0;
9128 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9129 return 1;
9130 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9131 return -1;
9132 if (bmap1->n_eq != bmap2->n_eq)
9133 return bmap1->n_eq - bmap2->n_eq;
9134 if (bmap1->n_ineq != bmap2->n_ineq)
9135 return bmap1->n_ineq - bmap2->n_ineq;
9136 if (bmap1->n_div != bmap2->n_div)
9137 return bmap1->n_div - bmap2->n_div;
9138 total = isl_basic_map_total_dim(bmap1);
9139 for (i = 0; i < bmap1->n_eq; ++i) {
9140 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9141 if (cmp)
9142 return cmp;
9144 for (i = 0; i < bmap1->n_ineq; ++i) {
9145 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9146 if (cmp)
9147 return cmp;
9149 for (i = 0; i < bmap1->n_div; ++i) {
9150 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9151 if (cmp)
9152 return cmp;
9154 return 0;
9157 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9158 __isl_keep isl_basic_set *bset2)
9160 return isl_basic_map_plain_cmp(bset1, bset2);
9163 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9165 int i, cmp;
9167 if (set1 == set2)
9168 return 0;
9169 if (set1->n != set2->n)
9170 return set1->n - set2->n;
9172 for (i = 0; i < set1->n; ++i) {
9173 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9174 if (cmp)
9175 return cmp;
9178 return 0;
9181 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9182 __isl_keep isl_basic_map *bmap2)
9184 if (!bmap1 || !bmap2)
9185 return isl_bool_error;
9186 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9189 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9190 __isl_keep isl_basic_set *bset2)
9192 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9193 bset_to_bmap(bset2));
9196 static int qsort_bmap_cmp(const void *p1, const void *p2)
9198 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9199 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9201 return isl_basic_map_plain_cmp(bmap1, bmap2);
9204 /* Sort the basic maps of "map" and remove duplicate basic maps.
9206 * While removing basic maps, we make sure that the basic maps remain
9207 * sorted because isl_map_normalize expects the basic maps of the result
9208 * to be sorted.
9210 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9212 int i, j;
9214 map = isl_map_remove_empty_parts(map);
9215 if (!map)
9216 return NULL;
9217 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9218 for (i = map->n - 1; i >= 1; --i) {
9219 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9220 continue;
9221 isl_basic_map_free(map->p[i-1]);
9222 for (j = i; j < map->n; ++j)
9223 map->p[j - 1] = map->p[j];
9224 map->n--;
9227 return map;
9230 /* Remove obvious duplicates among the basic maps of "map".
9232 * Unlike isl_map_normalize, this function does not remove redundant
9233 * constraints and only removes duplicates that have exactly the same
9234 * constraints in the input. It does sort the constraints and
9235 * the basic maps to ease the detection of duplicates.
9237 * If "map" has already been normalized or if the basic maps are
9238 * disjoint, then there can be no duplicates.
9240 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9242 int i;
9243 isl_basic_map *bmap;
9245 if (!map)
9246 return NULL;
9247 if (map->n <= 1)
9248 return map;
9249 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9250 return map;
9251 for (i = 0; i < map->n; ++i) {
9252 bmap = isl_basic_map_copy(map->p[i]);
9253 bmap = isl_basic_map_sort_constraints(bmap);
9254 if (!bmap)
9255 return isl_map_free(map);
9256 isl_basic_map_free(map->p[i]);
9257 map->p[i] = bmap;
9260 map = sort_and_remove_duplicates(map);
9261 return map;
9264 /* We normalize in place, but if anything goes wrong we need
9265 * to return NULL, so we need to make sure we don't change the
9266 * meaning of any possible other copies of map.
9268 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9270 int i;
9271 struct isl_basic_map *bmap;
9273 if (!map)
9274 return NULL;
9275 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9276 return map;
9277 for (i = 0; i < map->n; ++i) {
9278 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9279 if (!bmap)
9280 goto error;
9281 isl_basic_map_free(map->p[i]);
9282 map->p[i] = bmap;
9285 map = sort_and_remove_duplicates(map);
9286 if (map)
9287 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9288 return map;
9289 error:
9290 isl_map_free(map);
9291 return NULL;
9294 struct isl_set *isl_set_normalize(struct isl_set *set)
9296 return set_from_map(isl_map_normalize(set_to_map(set)));
9299 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9300 __isl_keep isl_map *map2)
9302 int i;
9303 isl_bool equal;
9305 if (!map1 || !map2)
9306 return isl_bool_error;
9308 if (map1 == map2)
9309 return isl_bool_true;
9310 if (!isl_space_is_equal(map1->dim, map2->dim))
9311 return isl_bool_false;
9313 map1 = isl_map_copy(map1);
9314 map2 = isl_map_copy(map2);
9315 map1 = isl_map_normalize(map1);
9316 map2 = isl_map_normalize(map2);
9317 if (!map1 || !map2)
9318 goto error;
9319 equal = map1->n == map2->n;
9320 for (i = 0; equal && i < map1->n; ++i) {
9321 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9322 if (equal < 0)
9323 goto error;
9325 isl_map_free(map1);
9326 isl_map_free(map2);
9327 return equal;
9328 error:
9329 isl_map_free(map1);
9330 isl_map_free(map2);
9331 return isl_bool_error;
9334 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9335 __isl_keep isl_set *set2)
9337 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9340 /* Return the basic maps in "map" as a list.
9342 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9343 __isl_keep isl_map *map)
9345 int i;
9346 isl_ctx *ctx;
9347 isl_basic_map_list *list;
9349 if (!map)
9350 return NULL;
9351 ctx = isl_map_get_ctx(map);
9352 list = isl_basic_map_list_alloc(ctx, map->n);
9354 for (i = 0; i < map->n; ++i) {
9355 isl_basic_map *bmap;
9357 bmap = isl_basic_map_copy(map->p[i]);
9358 list = isl_basic_map_list_add(list, bmap);
9361 return list;
9364 /* Return the intersection of the elements in the non-empty list "list".
9365 * All elements are assumed to live in the same space.
9367 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9368 __isl_take isl_basic_map_list *list)
9370 int i, n;
9371 isl_basic_map *bmap;
9373 if (!list)
9374 return NULL;
9375 n = isl_basic_map_list_n_basic_map(list);
9376 if (n < 1)
9377 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9378 "expecting non-empty list", goto error);
9380 bmap = isl_basic_map_list_get_basic_map(list, 0);
9381 for (i = 1; i < n; ++i) {
9382 isl_basic_map *bmap_i;
9384 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9385 bmap = isl_basic_map_intersect(bmap, bmap_i);
9388 isl_basic_map_list_free(list);
9389 return bmap;
9390 error:
9391 isl_basic_map_list_free(list);
9392 return NULL;
9395 /* Return the intersection of the elements in the non-empty list "list".
9396 * All elements are assumed to live in the same space.
9398 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9399 __isl_take isl_basic_set_list *list)
9401 return isl_basic_map_list_intersect(list);
9404 /* Return the union of the elements of "list".
9405 * The list is required to have at least one element.
9407 __isl_give isl_set *isl_basic_set_list_union(
9408 __isl_take isl_basic_set_list *list)
9410 int i, n;
9411 isl_space *space;
9412 isl_basic_set *bset;
9413 isl_set *set;
9415 if (!list)
9416 return NULL;
9417 n = isl_basic_set_list_n_basic_set(list);
9418 if (n < 1)
9419 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9420 "expecting non-empty list", goto error);
9422 bset = isl_basic_set_list_get_basic_set(list, 0);
9423 space = isl_basic_set_get_space(bset);
9424 isl_basic_set_free(bset);
9426 set = isl_set_alloc_space(space, n, 0);
9427 for (i = 0; i < n; ++i) {
9428 bset = isl_basic_set_list_get_basic_set(list, i);
9429 set = isl_set_add_basic_set(set, bset);
9432 isl_basic_set_list_free(list);
9433 return set;
9434 error:
9435 isl_basic_set_list_free(list);
9436 return NULL;
9439 /* Return the union of the elements in the non-empty list "list".
9440 * All elements are assumed to live in the same space.
9442 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9444 int i, n;
9445 isl_set *set;
9447 if (!list)
9448 return NULL;
9449 n = isl_set_list_n_set(list);
9450 if (n < 1)
9451 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9452 "expecting non-empty list", goto error);
9454 set = isl_set_list_get_set(list, 0);
9455 for (i = 1; i < n; ++i) {
9456 isl_set *set_i;
9458 set_i = isl_set_list_get_set(list, i);
9459 set = isl_set_union(set, set_i);
9462 isl_set_list_free(list);
9463 return set;
9464 error:
9465 isl_set_list_free(list);
9466 return NULL;
9469 /* Return the Cartesian product of the basic sets in list (in the given order).
9471 __isl_give isl_basic_set *isl_basic_set_list_product(
9472 __isl_take struct isl_basic_set_list *list)
9474 int i;
9475 unsigned dim;
9476 unsigned nparam;
9477 unsigned extra;
9478 unsigned n_eq;
9479 unsigned n_ineq;
9480 struct isl_basic_set *product = NULL;
9482 if (!list)
9483 goto error;
9484 isl_assert(list->ctx, list->n > 0, goto error);
9485 isl_assert(list->ctx, list->p[0], goto error);
9486 nparam = isl_basic_set_n_param(list->p[0]);
9487 dim = isl_basic_set_n_dim(list->p[0]);
9488 extra = list->p[0]->n_div;
9489 n_eq = list->p[0]->n_eq;
9490 n_ineq = list->p[0]->n_ineq;
9491 for (i = 1; i < list->n; ++i) {
9492 isl_assert(list->ctx, list->p[i], goto error);
9493 isl_assert(list->ctx,
9494 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9495 dim += isl_basic_set_n_dim(list->p[i]);
9496 extra += list->p[i]->n_div;
9497 n_eq += list->p[i]->n_eq;
9498 n_ineq += list->p[i]->n_ineq;
9500 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9501 n_eq, n_ineq);
9502 if (!product)
9503 goto error;
9504 dim = 0;
9505 for (i = 0; i < list->n; ++i) {
9506 isl_basic_set_add_constraints(product,
9507 isl_basic_set_copy(list->p[i]), dim);
9508 dim += isl_basic_set_n_dim(list->p[i]);
9510 isl_basic_set_list_free(list);
9511 return product;
9512 error:
9513 isl_basic_set_free(product);
9514 isl_basic_set_list_free(list);
9515 return NULL;
9518 struct isl_basic_map *isl_basic_map_product(
9519 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9521 isl_space *dim_result = NULL;
9522 struct isl_basic_map *bmap;
9523 unsigned in1, in2, out1, out2, nparam, total, pos;
9524 struct isl_dim_map *dim_map1, *dim_map2;
9526 if (!bmap1 || !bmap2)
9527 goto error;
9529 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9530 bmap2->dim, isl_dim_param), goto error);
9531 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9532 isl_space_copy(bmap2->dim));
9534 in1 = isl_basic_map_n_in(bmap1);
9535 in2 = isl_basic_map_n_in(bmap2);
9536 out1 = isl_basic_map_n_out(bmap1);
9537 out2 = isl_basic_map_n_out(bmap2);
9538 nparam = isl_basic_map_n_param(bmap1);
9540 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9541 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9542 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9543 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9544 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9545 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9546 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9547 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9548 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9549 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9550 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9552 bmap = isl_basic_map_alloc_space(dim_result,
9553 bmap1->n_div + bmap2->n_div,
9554 bmap1->n_eq + bmap2->n_eq,
9555 bmap1->n_ineq + bmap2->n_ineq);
9556 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9557 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9558 bmap = isl_basic_map_simplify(bmap);
9559 return isl_basic_map_finalize(bmap);
9560 error:
9561 isl_basic_map_free(bmap1);
9562 isl_basic_map_free(bmap2);
9563 return NULL;
9566 __isl_give isl_basic_map *isl_basic_map_flat_product(
9567 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9569 isl_basic_map *prod;
9571 prod = isl_basic_map_product(bmap1, bmap2);
9572 prod = isl_basic_map_flatten(prod);
9573 return prod;
9576 __isl_give isl_basic_set *isl_basic_set_flat_product(
9577 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9579 return isl_basic_map_flat_range_product(bset1, bset2);
9582 __isl_give isl_basic_map *isl_basic_map_domain_product(
9583 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9585 isl_space *space_result = NULL;
9586 isl_basic_map *bmap;
9587 unsigned in1, in2, out, nparam, total, pos;
9588 struct isl_dim_map *dim_map1, *dim_map2;
9590 if (!bmap1 || !bmap2)
9591 goto error;
9593 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9594 isl_space_copy(bmap2->dim));
9596 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9597 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9598 out = isl_basic_map_dim(bmap1, isl_dim_out);
9599 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9601 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9602 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9603 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9604 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9605 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9606 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9607 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9608 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9609 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9610 isl_dim_map_div(dim_map1, bmap1, pos += out);
9611 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9613 bmap = isl_basic_map_alloc_space(space_result,
9614 bmap1->n_div + bmap2->n_div,
9615 bmap1->n_eq + bmap2->n_eq,
9616 bmap1->n_ineq + bmap2->n_ineq);
9617 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9618 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9619 bmap = isl_basic_map_simplify(bmap);
9620 return isl_basic_map_finalize(bmap);
9621 error:
9622 isl_basic_map_free(bmap1);
9623 isl_basic_map_free(bmap2);
9624 return NULL;
9627 __isl_give isl_basic_map *isl_basic_map_range_product(
9628 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9630 isl_bool rational;
9631 isl_space *dim_result = NULL;
9632 isl_basic_map *bmap;
9633 unsigned in, out1, out2, nparam, total, pos;
9634 struct isl_dim_map *dim_map1, *dim_map2;
9636 rational = isl_basic_map_is_rational(bmap1);
9637 if (rational >= 0 && rational)
9638 rational = isl_basic_map_is_rational(bmap2);
9639 if (!bmap1 || !bmap2 || rational < 0)
9640 goto error;
9642 if (!isl_space_match(bmap1->dim, isl_dim_param,
9643 bmap2->dim, isl_dim_param))
9644 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9645 "parameters don't match", goto error);
9647 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9648 isl_space_copy(bmap2->dim));
9650 in = isl_basic_map_dim(bmap1, isl_dim_in);
9651 out1 = isl_basic_map_n_out(bmap1);
9652 out2 = isl_basic_map_n_out(bmap2);
9653 nparam = isl_basic_map_n_param(bmap1);
9655 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9656 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9657 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9658 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9659 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9660 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9661 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9662 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9663 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9664 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9665 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9667 bmap = isl_basic_map_alloc_space(dim_result,
9668 bmap1->n_div + bmap2->n_div,
9669 bmap1->n_eq + bmap2->n_eq,
9670 bmap1->n_ineq + bmap2->n_ineq);
9671 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9672 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9673 if (rational)
9674 bmap = isl_basic_map_set_rational(bmap);
9675 bmap = isl_basic_map_simplify(bmap);
9676 return isl_basic_map_finalize(bmap);
9677 error:
9678 isl_basic_map_free(bmap1);
9679 isl_basic_map_free(bmap2);
9680 return NULL;
9683 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9684 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9686 isl_basic_map *prod;
9688 prod = isl_basic_map_range_product(bmap1, bmap2);
9689 prod = isl_basic_map_flatten_range(prod);
9690 return prod;
9693 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9694 * and collect the results.
9695 * The result live in the space obtained by calling "space_product"
9696 * on the spaces of "map1" and "map2".
9697 * If "remove_duplicates" is set then the result may contain duplicates
9698 * (even if the inputs do not) and so we try and remove the obvious
9699 * duplicates.
9701 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9702 __isl_take isl_map *map2,
9703 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9704 __isl_take isl_space *right),
9705 __isl_give isl_basic_map *(*basic_map_product)(
9706 __isl_take isl_basic_map *left,
9707 __isl_take isl_basic_map *right),
9708 int remove_duplicates)
9710 unsigned flags = 0;
9711 struct isl_map *result;
9712 int i, j;
9714 if (!map1 || !map2)
9715 goto error;
9717 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9718 map2->dim, isl_dim_param), goto error);
9720 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9721 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9722 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9724 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9725 isl_space_copy(map2->dim)),
9726 map1->n * map2->n, flags);
9727 if (!result)
9728 goto error;
9729 for (i = 0; i < map1->n; ++i)
9730 for (j = 0; j < map2->n; ++j) {
9731 struct isl_basic_map *part;
9732 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9733 isl_basic_map_copy(map2->p[j]));
9734 if (isl_basic_map_is_empty(part))
9735 isl_basic_map_free(part);
9736 else
9737 result = isl_map_add_basic_map(result, part);
9738 if (!result)
9739 goto error;
9741 if (remove_duplicates)
9742 result = isl_map_remove_obvious_duplicates(result);
9743 isl_map_free(map1);
9744 isl_map_free(map2);
9745 return result;
9746 error:
9747 isl_map_free(map1);
9748 isl_map_free(map2);
9749 return NULL;
9752 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9754 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9755 __isl_take isl_map *map2)
9757 return map_product(map1, map2, &isl_space_product,
9758 &isl_basic_map_product, 0);
9761 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9762 __isl_take isl_map *map2)
9764 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9767 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9769 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9770 __isl_take isl_map *map2)
9772 isl_map *prod;
9774 prod = isl_map_product(map1, map2);
9775 prod = isl_map_flatten(prod);
9776 return prod;
9779 /* Given two set A and B, construct its Cartesian product A x B.
9781 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9783 return isl_map_range_product(set1, set2);
9786 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9787 __isl_take isl_set *set2)
9789 return isl_map_flat_range_product(set1, set2);
9792 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9794 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9795 __isl_take isl_map *map2)
9797 return map_product(map1, map2, &isl_space_domain_product,
9798 &isl_basic_map_domain_product, 1);
9801 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9803 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9804 __isl_take isl_map *map2)
9806 return map_product(map1, map2, &isl_space_range_product,
9807 &isl_basic_map_range_product, 1);
9810 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9811 __isl_take isl_map *map2)
9813 return isl_map_align_params_map_map_and(map1, map2,
9814 &map_domain_product_aligned);
9817 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9818 __isl_take isl_map *map2)
9820 return isl_map_align_params_map_map_and(map1, map2,
9821 &map_range_product_aligned);
9824 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9826 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9828 isl_space *space;
9829 int total1, keep1, total2, keep2;
9831 if (!map)
9832 return NULL;
9833 if (!isl_space_domain_is_wrapping(map->dim) ||
9834 !isl_space_range_is_wrapping(map->dim))
9835 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9836 "not a product", return isl_map_free(map));
9838 space = isl_map_get_space(map);
9839 total1 = isl_space_dim(space, isl_dim_in);
9840 total2 = isl_space_dim(space, isl_dim_out);
9841 space = isl_space_factor_domain(space);
9842 keep1 = isl_space_dim(space, isl_dim_in);
9843 keep2 = isl_space_dim(space, isl_dim_out);
9844 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9845 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9846 map = isl_map_reset_space(map, space);
9848 return map;
9851 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9853 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9855 isl_space *space;
9856 int total1, keep1, total2, keep2;
9858 if (!map)
9859 return NULL;
9860 if (!isl_space_domain_is_wrapping(map->dim) ||
9861 !isl_space_range_is_wrapping(map->dim))
9862 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9863 "not a product", return isl_map_free(map));
9865 space = isl_map_get_space(map);
9866 total1 = isl_space_dim(space, isl_dim_in);
9867 total2 = isl_space_dim(space, isl_dim_out);
9868 space = isl_space_factor_range(space);
9869 keep1 = isl_space_dim(space, isl_dim_in);
9870 keep2 = isl_space_dim(space, isl_dim_out);
9871 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9872 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9873 map = isl_map_reset_space(map, space);
9875 return map;
9878 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9880 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9882 isl_space *space;
9883 int total, keep;
9885 if (!map)
9886 return NULL;
9887 if (!isl_space_domain_is_wrapping(map->dim))
9888 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9889 "domain is not a product", return isl_map_free(map));
9891 space = isl_map_get_space(map);
9892 total = isl_space_dim(space, isl_dim_in);
9893 space = isl_space_domain_factor_domain(space);
9894 keep = isl_space_dim(space, isl_dim_in);
9895 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9896 map = isl_map_reset_space(map, space);
9898 return map;
9901 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9903 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9905 isl_space *space;
9906 int total, keep;
9908 if (!map)
9909 return NULL;
9910 if (!isl_space_domain_is_wrapping(map->dim))
9911 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9912 "domain is not a product", return isl_map_free(map));
9914 space = isl_map_get_space(map);
9915 total = isl_space_dim(space, isl_dim_in);
9916 space = isl_space_domain_factor_range(space);
9917 keep = isl_space_dim(space, isl_dim_in);
9918 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9919 map = isl_map_reset_space(map, space);
9921 return map;
9924 /* Given a map A -> [B -> C], extract the map A -> B.
9926 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9928 isl_space *space;
9929 int total, keep;
9931 if (!map)
9932 return NULL;
9933 if (!isl_space_range_is_wrapping(map->dim))
9934 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9935 "range is not a product", return isl_map_free(map));
9937 space = isl_map_get_space(map);
9938 total = isl_space_dim(space, isl_dim_out);
9939 space = isl_space_range_factor_domain(space);
9940 keep = isl_space_dim(space, isl_dim_out);
9941 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9942 map = isl_map_reset_space(map, space);
9944 return map;
9947 /* Given a map A -> [B -> C], extract the map A -> C.
9949 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9951 isl_space *space;
9952 int total, keep;
9954 if (!map)
9955 return NULL;
9956 if (!isl_space_range_is_wrapping(map->dim))
9957 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9958 "range is not a product", return isl_map_free(map));
9960 space = isl_map_get_space(map);
9961 total = isl_space_dim(space, isl_dim_out);
9962 space = isl_space_range_factor_range(space);
9963 keep = isl_space_dim(space, isl_dim_out);
9964 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9965 map = isl_map_reset_space(map, space);
9967 return map;
9970 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9972 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9973 __isl_take isl_map *map2)
9975 isl_map *prod;
9977 prod = isl_map_domain_product(map1, map2);
9978 prod = isl_map_flatten_domain(prod);
9979 return prod;
9982 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9984 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9985 __isl_take isl_map *map2)
9987 isl_map *prod;
9989 prod = isl_map_range_product(map1, map2);
9990 prod = isl_map_flatten_range(prod);
9991 return prod;
9994 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9996 int i;
9997 uint32_t hash = isl_hash_init();
9998 unsigned total;
10000 if (!bmap)
10001 return 0;
10002 bmap = isl_basic_map_copy(bmap);
10003 bmap = isl_basic_map_normalize(bmap);
10004 if (!bmap)
10005 return 0;
10006 total = isl_basic_map_total_dim(bmap);
10007 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10008 for (i = 0; i < bmap->n_eq; ++i) {
10009 uint32_t c_hash;
10010 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10011 isl_hash_hash(hash, c_hash);
10013 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10014 for (i = 0; i < bmap->n_ineq; ++i) {
10015 uint32_t c_hash;
10016 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10017 isl_hash_hash(hash, c_hash);
10019 isl_hash_byte(hash, bmap->n_div & 0xFF);
10020 for (i = 0; i < bmap->n_div; ++i) {
10021 uint32_t c_hash;
10022 if (isl_int_is_zero(bmap->div[i][0]))
10023 continue;
10024 isl_hash_byte(hash, i & 0xFF);
10025 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10026 isl_hash_hash(hash, c_hash);
10028 isl_basic_map_free(bmap);
10029 return hash;
10032 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10034 return isl_basic_map_get_hash(bset_to_bmap(bset));
10037 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10039 int i;
10040 uint32_t hash;
10042 if (!map)
10043 return 0;
10044 map = isl_map_copy(map);
10045 map = isl_map_normalize(map);
10046 if (!map)
10047 return 0;
10049 hash = isl_hash_init();
10050 for (i = 0; i < map->n; ++i) {
10051 uint32_t bmap_hash;
10052 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10053 isl_hash_hash(hash, bmap_hash);
10056 isl_map_free(map);
10058 return hash;
10061 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10063 return isl_map_get_hash(set_to_map(set));
10066 /* Check if the value for dimension dim is completely determined
10067 * by the values of the other parameters and variables.
10068 * That is, check if dimension dim is involved in an equality.
10070 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10072 int i;
10073 unsigned nparam;
10075 if (!bset)
10076 return -1;
10077 nparam = isl_basic_set_n_param(bset);
10078 for (i = 0; i < bset->n_eq; ++i)
10079 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10080 return 1;
10081 return 0;
10084 /* Check if the value for dimension dim is completely determined
10085 * by the values of the other parameters and variables.
10086 * That is, check if dimension dim is involved in an equality
10087 * for each of the subsets.
10089 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10091 int i;
10093 if (!set)
10094 return -1;
10095 for (i = 0; i < set->n; ++i) {
10096 int unique;
10097 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10098 if (unique != 1)
10099 return unique;
10101 return 1;
10104 /* Return the number of basic maps in the (current) representation of "map".
10106 int isl_map_n_basic_map(__isl_keep isl_map *map)
10108 return map ? map->n : 0;
10111 int isl_set_n_basic_set(__isl_keep isl_set *set)
10113 return set ? set->n : 0;
10116 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10117 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10119 int i;
10121 if (!map)
10122 return isl_stat_error;
10124 for (i = 0; i < map->n; ++i)
10125 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10126 return isl_stat_error;
10128 return isl_stat_ok;
10131 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10132 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10134 int i;
10136 if (!set)
10137 return isl_stat_error;
10139 for (i = 0; i < set->n; ++i)
10140 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10141 return isl_stat_error;
10143 return isl_stat_ok;
10146 /* Return a list of basic sets, the union of which is equal to "set".
10148 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10149 __isl_keep isl_set *set)
10151 int i;
10152 isl_basic_set_list *list;
10154 if (!set)
10155 return NULL;
10157 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10158 for (i = 0; i < set->n; ++i) {
10159 isl_basic_set *bset;
10161 bset = isl_basic_set_copy(set->p[i]);
10162 list = isl_basic_set_list_add(list, bset);
10165 return list;
10168 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10170 isl_space *dim;
10172 if (!bset)
10173 return NULL;
10175 bset = isl_basic_set_cow(bset);
10176 if (!bset)
10177 return NULL;
10179 dim = isl_basic_set_get_space(bset);
10180 dim = isl_space_lift(dim, bset->n_div);
10181 if (!dim)
10182 goto error;
10183 isl_space_free(bset->dim);
10184 bset->dim = dim;
10185 bset->extra -= bset->n_div;
10186 bset->n_div = 0;
10188 bset = isl_basic_set_finalize(bset);
10190 return bset;
10191 error:
10192 isl_basic_set_free(bset);
10193 return NULL;
10196 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10198 int i;
10199 isl_space *dim;
10200 unsigned n_div;
10202 set = isl_set_align_divs(set);
10204 if (!set)
10205 return NULL;
10207 set = isl_set_cow(set);
10208 if (!set)
10209 return NULL;
10211 n_div = set->p[0]->n_div;
10212 dim = isl_set_get_space(set);
10213 dim = isl_space_lift(dim, n_div);
10214 if (!dim)
10215 goto error;
10216 isl_space_free(set->dim);
10217 set->dim = dim;
10219 for (i = 0; i < set->n; ++i) {
10220 set->p[i] = isl_basic_set_lift(set->p[i]);
10221 if (!set->p[i])
10222 goto error;
10225 return set;
10226 error:
10227 isl_set_free(set);
10228 return NULL;
10231 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10233 isl_space *dim;
10234 struct isl_basic_map *bmap;
10235 unsigned n_set;
10236 unsigned n_div;
10237 unsigned n_param;
10238 unsigned total;
10239 int i, k, l;
10241 set = isl_set_align_divs(set);
10243 if (!set)
10244 return NULL;
10246 dim = isl_set_get_space(set);
10247 if (set->n == 0 || set->p[0]->n_div == 0) {
10248 isl_set_free(set);
10249 return isl_map_identity(isl_space_map_from_set(dim));
10252 n_div = set->p[0]->n_div;
10253 dim = isl_space_map_from_set(dim);
10254 n_param = isl_space_dim(dim, isl_dim_param);
10255 n_set = isl_space_dim(dim, isl_dim_in);
10256 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10257 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10258 for (i = 0; i < n_set; ++i)
10259 bmap = var_equal(bmap, i);
10261 total = n_param + n_set + n_set + n_div;
10262 for (i = 0; i < n_div; ++i) {
10263 k = isl_basic_map_alloc_inequality(bmap);
10264 if (k < 0)
10265 goto error;
10266 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10267 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10268 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10269 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10270 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10271 set->p[0]->div[i][0]);
10273 l = isl_basic_map_alloc_inequality(bmap);
10274 if (l < 0)
10275 goto error;
10276 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10277 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10278 set->p[0]->div[i][0]);
10279 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10282 isl_set_free(set);
10283 bmap = isl_basic_map_simplify(bmap);
10284 bmap = isl_basic_map_finalize(bmap);
10285 return isl_map_from_basic_map(bmap);
10286 error:
10287 isl_set_free(set);
10288 isl_basic_map_free(bmap);
10289 return NULL;
10292 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10294 unsigned dim;
10295 int size = 0;
10297 if (!bset)
10298 return -1;
10300 dim = isl_basic_set_total_dim(bset);
10301 size += bset->n_eq * (1 + dim);
10302 size += bset->n_ineq * (1 + dim);
10303 size += bset->n_div * (2 + dim);
10305 return size;
10308 int isl_set_size(__isl_keep isl_set *set)
10310 int i;
10311 int size = 0;
10313 if (!set)
10314 return -1;
10316 for (i = 0; i < set->n; ++i)
10317 size += isl_basic_set_size(set->p[i]);
10319 return size;
10322 /* Check if there is any lower bound (if lower == 0) and/or upper
10323 * bound (if upper == 0) on the specified dim.
10325 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10326 enum isl_dim_type type, unsigned pos, int lower, int upper)
10328 int i;
10330 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10331 return isl_bool_error;
10333 pos += isl_basic_map_offset(bmap, type);
10335 for (i = 0; i < bmap->n_div; ++i) {
10336 if (isl_int_is_zero(bmap->div[i][0]))
10337 continue;
10338 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10339 return isl_bool_true;
10342 for (i = 0; i < bmap->n_eq; ++i)
10343 if (!isl_int_is_zero(bmap->eq[i][pos]))
10344 return isl_bool_true;
10346 for (i = 0; i < bmap->n_ineq; ++i) {
10347 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10348 if (sgn > 0)
10349 lower = 1;
10350 if (sgn < 0)
10351 upper = 1;
10354 return lower && upper;
10357 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10358 enum isl_dim_type type, unsigned pos)
10360 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10363 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10364 enum isl_dim_type type, unsigned pos)
10366 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10369 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10370 enum isl_dim_type type, unsigned pos)
10372 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10375 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10376 enum isl_dim_type type, unsigned pos)
10378 int i;
10380 if (!map)
10381 return isl_bool_error;
10383 for (i = 0; i < map->n; ++i) {
10384 isl_bool bounded;
10385 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10386 if (bounded < 0 || !bounded)
10387 return bounded;
10390 return isl_bool_true;
10393 /* Return true if the specified dim is involved in both an upper bound
10394 * and a lower bound.
10396 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10397 enum isl_dim_type type, unsigned pos)
10399 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10402 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10404 static isl_bool has_any_bound(__isl_keep isl_map *map,
10405 enum isl_dim_type type, unsigned pos,
10406 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10407 enum isl_dim_type type, unsigned pos))
10409 int i;
10411 if (!map)
10412 return isl_bool_error;
10414 for (i = 0; i < map->n; ++i) {
10415 isl_bool bounded;
10416 bounded = fn(map->p[i], type, pos);
10417 if (bounded < 0 || bounded)
10418 return bounded;
10421 return isl_bool_false;
10424 /* Return 1 if the specified dim is involved in any lower bound.
10426 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10427 enum isl_dim_type type, unsigned pos)
10429 return has_any_bound(set, type, pos,
10430 &isl_basic_map_dim_has_lower_bound);
10433 /* Return 1 if the specified dim is involved in any upper bound.
10435 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10436 enum isl_dim_type type, unsigned pos)
10438 return has_any_bound(set, type, pos,
10439 &isl_basic_map_dim_has_upper_bound);
10442 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10444 static isl_bool has_bound(__isl_keep isl_map *map,
10445 enum isl_dim_type type, unsigned pos,
10446 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10447 enum isl_dim_type type, unsigned pos))
10449 int i;
10451 if (!map)
10452 return isl_bool_error;
10454 for (i = 0; i < map->n; ++i) {
10455 isl_bool bounded;
10456 bounded = fn(map->p[i], type, pos);
10457 if (bounded < 0 || !bounded)
10458 return bounded;
10461 return isl_bool_true;
10464 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10466 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10467 enum isl_dim_type type, unsigned pos)
10469 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10472 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10474 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10475 enum isl_dim_type type, unsigned pos)
10477 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10480 /* For each of the "n" variables starting at "first", determine
10481 * the sign of the variable and put the results in the first "n"
10482 * elements of the array "signs".
10483 * Sign
10484 * 1 means that the variable is non-negative
10485 * -1 means that the variable is non-positive
10486 * 0 means the variable attains both positive and negative values.
10488 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10489 unsigned first, unsigned n, int *signs)
10491 isl_vec *bound = NULL;
10492 struct isl_tab *tab = NULL;
10493 struct isl_tab_undo *snap;
10494 int i;
10496 if (!bset || !signs)
10497 return isl_stat_error;
10499 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10500 tab = isl_tab_from_basic_set(bset, 0);
10501 if (!bound || !tab)
10502 goto error;
10504 isl_seq_clr(bound->el, bound->size);
10505 isl_int_set_si(bound->el[0], -1);
10507 snap = isl_tab_snap(tab);
10508 for (i = 0; i < n; ++i) {
10509 int empty;
10511 isl_int_set_si(bound->el[1 + first + i], -1);
10512 if (isl_tab_add_ineq(tab, bound->el) < 0)
10513 goto error;
10514 empty = tab->empty;
10515 isl_int_set_si(bound->el[1 + first + i], 0);
10516 if (isl_tab_rollback(tab, snap) < 0)
10517 goto error;
10519 if (empty) {
10520 signs[i] = 1;
10521 continue;
10524 isl_int_set_si(bound->el[1 + first + i], 1);
10525 if (isl_tab_add_ineq(tab, bound->el) < 0)
10526 goto error;
10527 empty = tab->empty;
10528 isl_int_set_si(bound->el[1 + first + i], 0);
10529 if (isl_tab_rollback(tab, snap) < 0)
10530 goto error;
10532 signs[i] = empty ? -1 : 0;
10535 isl_tab_free(tab);
10536 isl_vec_free(bound);
10537 return isl_stat_ok;
10538 error:
10539 isl_tab_free(tab);
10540 isl_vec_free(bound);
10541 return isl_stat_error;
10544 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10545 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10547 if (!bset || !signs)
10548 return isl_stat_error;
10549 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10550 return isl_stat_error);
10552 first += pos(bset->dim, type) - 1;
10553 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10556 /* Is it possible for the integer division "div" to depend (possibly
10557 * indirectly) on any output dimensions?
10559 * If the div is undefined, then we conservatively assume that it
10560 * may depend on them.
10561 * Otherwise, we check if it actually depends on them or on any integer
10562 * divisions that may depend on them.
10564 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10566 int i;
10567 unsigned n_out, o_out;
10568 unsigned n_div, o_div;
10570 if (isl_int_is_zero(bmap->div[div][0]))
10571 return isl_bool_true;
10573 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10574 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10576 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10577 return isl_bool_true;
10579 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10580 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10582 for (i = 0; i < n_div; ++i) {
10583 isl_bool may_involve;
10585 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10586 continue;
10587 may_involve = div_may_involve_output(bmap, i);
10588 if (may_involve < 0 || may_involve)
10589 return may_involve;
10592 return isl_bool_false;
10595 /* Return the first integer division of "bmap" in the range
10596 * [first, first + n[ that may depend on any output dimensions and
10597 * that has a non-zero coefficient in "c" (where the first coefficient
10598 * in "c" corresponds to integer division "first").
10600 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10601 isl_int *c, int first, int n)
10603 int k;
10605 if (!bmap)
10606 return -1;
10608 for (k = first; k < first + n; ++k) {
10609 isl_bool may_involve;
10611 if (isl_int_is_zero(c[k]))
10612 continue;
10613 may_involve = div_may_involve_output(bmap, k);
10614 if (may_involve < 0)
10615 return -1;
10616 if (may_involve)
10617 return k;
10620 return first + n;
10623 /* Look for a pair of inequality constraints in "bmap" of the form
10625 * -l + i >= 0 or i >= l
10626 * and
10627 * n + l - i >= 0 or i <= l + n
10629 * with n < "m" and i the output dimension at position "pos".
10630 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10631 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10632 * and earlier output dimensions, as well as integer divisions that do
10633 * not involve any of the output dimensions.
10635 * Return the index of the first inequality constraint or bmap->n_ineq
10636 * if no such pair can be found.
10638 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10639 int pos, isl_int m)
10641 int i, j;
10642 isl_ctx *ctx;
10643 unsigned total;
10644 unsigned n_div, o_div;
10645 unsigned n_out, o_out;
10646 int less;
10648 if (!bmap)
10649 return -1;
10651 ctx = isl_basic_map_get_ctx(bmap);
10652 total = isl_basic_map_total_dim(bmap);
10653 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10654 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10655 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10656 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10657 for (i = 0; i < bmap->n_ineq; ++i) {
10658 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10659 continue;
10660 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10661 n_out - (pos + 1)) != -1)
10662 continue;
10663 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10664 0, n_div) < n_div)
10665 continue;
10666 for (j = i + 1; j < bmap->n_ineq; ++j) {
10667 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10668 ctx->one))
10669 continue;
10670 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10671 bmap->ineq[j] + 1, total))
10672 continue;
10673 break;
10675 if (j >= bmap->n_ineq)
10676 continue;
10677 isl_int_add(bmap->ineq[i][0],
10678 bmap->ineq[i][0], bmap->ineq[j][0]);
10679 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10680 isl_int_sub(bmap->ineq[i][0],
10681 bmap->ineq[i][0], bmap->ineq[j][0]);
10682 if (!less)
10683 continue;
10684 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10685 return i;
10686 else
10687 return j;
10690 return bmap->n_ineq;
10693 /* Return the index of the equality of "bmap" that defines
10694 * the output dimension "pos" in terms of earlier dimensions.
10695 * The equality may also involve integer divisions, as long
10696 * as those integer divisions are defined in terms of
10697 * parameters or input dimensions.
10698 * In this case, *div is set to the number of integer divisions and
10699 * *ineq is set to the number of inequality constraints (provided
10700 * div and ineq are not NULL).
10702 * The equality may also involve a single integer division involving
10703 * the output dimensions (typically only output dimension "pos") as
10704 * long as the coefficient of output dimension "pos" is 1 or -1 and
10705 * there is a pair of constraints i >= l and i <= l + n, with i referring
10706 * to output dimension "pos", l an expression involving only earlier
10707 * dimensions and n smaller than the coefficient of the integer division
10708 * in the equality. In this case, the output dimension can be defined
10709 * in terms of a modulo expression that does not involve the integer division.
10710 * *div is then set to this single integer division and
10711 * *ineq is set to the index of constraint i >= l.
10713 * Return bmap->n_eq if there is no such equality.
10714 * Return -1 on error.
10716 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10717 int pos, int *div, int *ineq)
10719 int j, k, l;
10720 unsigned n_out, o_out;
10721 unsigned n_div, o_div;
10723 if (!bmap)
10724 return -1;
10726 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10727 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10728 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10729 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10731 if (ineq)
10732 *ineq = bmap->n_ineq;
10733 if (div)
10734 *div = n_div;
10735 for (j = 0; j < bmap->n_eq; ++j) {
10736 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10737 continue;
10738 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10739 n_out - (pos + 1)) != -1)
10740 continue;
10741 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10742 0, n_div);
10743 if (k >= n_div)
10744 return j;
10745 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10746 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10747 continue;
10748 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10749 k + 1, n_div - (k+1)) < n_div)
10750 continue;
10751 l = find_modulo_constraint_pair(bmap, pos,
10752 bmap->eq[j][o_div + k]);
10753 if (l < 0)
10754 return -1;
10755 if (l >= bmap->n_ineq)
10756 continue;
10757 if (div)
10758 *div = k;
10759 if (ineq)
10760 *ineq = l;
10761 return j;
10764 return bmap->n_eq;
10767 /* Check if the given basic map is obviously single-valued.
10768 * In particular, for each output dimension, check that there is
10769 * an equality that defines the output dimension in terms of
10770 * earlier dimensions.
10772 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10774 int i;
10775 unsigned n_out;
10777 if (!bmap)
10778 return isl_bool_error;
10780 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10782 for (i = 0; i < n_out; ++i) {
10783 int eq;
10785 eq = isl_basic_map_output_defining_equality(bmap, i,
10786 NULL, NULL);
10787 if (eq < 0)
10788 return isl_bool_error;
10789 if (eq >= bmap->n_eq)
10790 return isl_bool_false;
10793 return isl_bool_true;
10796 /* Check if the given basic map is single-valued.
10797 * We simply compute
10799 * M \circ M^-1
10801 * and check if the result is a subset of the identity mapping.
10803 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10805 isl_space *space;
10806 isl_basic_map *test;
10807 isl_basic_map *id;
10808 isl_bool sv;
10810 sv = isl_basic_map_plain_is_single_valued(bmap);
10811 if (sv < 0 || sv)
10812 return sv;
10814 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10815 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10817 space = isl_basic_map_get_space(bmap);
10818 space = isl_space_map_from_set(isl_space_range(space));
10819 id = isl_basic_map_identity(space);
10821 sv = isl_basic_map_is_subset(test, id);
10823 isl_basic_map_free(test);
10824 isl_basic_map_free(id);
10826 return sv;
10829 /* Check if the given map is obviously single-valued.
10831 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10833 if (!map)
10834 return isl_bool_error;
10835 if (map->n == 0)
10836 return isl_bool_true;
10837 if (map->n >= 2)
10838 return isl_bool_false;
10840 return isl_basic_map_plain_is_single_valued(map->p[0]);
10843 /* Check if the given map is single-valued.
10844 * We simply compute
10846 * M \circ M^-1
10848 * and check if the result is a subset of the identity mapping.
10850 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10852 isl_space *dim;
10853 isl_map *test;
10854 isl_map *id;
10855 isl_bool sv;
10857 sv = isl_map_plain_is_single_valued(map);
10858 if (sv < 0 || sv)
10859 return sv;
10861 test = isl_map_reverse(isl_map_copy(map));
10862 test = isl_map_apply_range(test, isl_map_copy(map));
10864 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10865 id = isl_map_identity(dim);
10867 sv = isl_map_is_subset(test, id);
10869 isl_map_free(test);
10870 isl_map_free(id);
10872 return sv;
10875 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10877 isl_bool in;
10879 map = isl_map_copy(map);
10880 map = isl_map_reverse(map);
10881 in = isl_map_is_single_valued(map);
10882 isl_map_free(map);
10884 return in;
10887 /* Check if the given map is obviously injective.
10889 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10891 isl_bool in;
10893 map = isl_map_copy(map);
10894 map = isl_map_reverse(map);
10895 in = isl_map_plain_is_single_valued(map);
10896 isl_map_free(map);
10898 return in;
10901 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10903 isl_bool sv;
10905 sv = isl_map_is_single_valued(map);
10906 if (sv < 0 || !sv)
10907 return sv;
10909 return isl_map_is_injective(map);
10912 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10914 return isl_map_is_single_valued(set_to_map(set));
10917 /* Does "map" only map elements to themselves?
10919 * If the domain and range spaces are different, then "map"
10920 * is considered not to be an identity relation, even if it is empty.
10921 * Otherwise, construct the maximal identity relation and
10922 * check whether "map" is a subset of this relation.
10924 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10926 isl_space *space;
10927 isl_map *id;
10928 isl_bool equal, is_identity;
10930 space = isl_map_get_space(map);
10931 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10932 isl_space_free(space);
10933 if (equal < 0 || !equal)
10934 return equal;
10936 id = isl_map_identity(isl_map_get_space(map));
10937 is_identity = isl_map_is_subset(map, id);
10938 isl_map_free(id);
10940 return is_identity;
10943 int isl_map_is_translation(__isl_keep isl_map *map)
10945 int ok;
10946 isl_set *delta;
10948 delta = isl_map_deltas(isl_map_copy(map));
10949 ok = isl_set_is_singleton(delta);
10950 isl_set_free(delta);
10952 return ok;
10955 static int unique(isl_int *p, unsigned pos, unsigned len)
10957 if (isl_seq_first_non_zero(p, pos) != -1)
10958 return 0;
10959 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10960 return 0;
10961 return 1;
10964 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10966 int i, j;
10967 unsigned nvar;
10968 unsigned ovar;
10970 if (!bset)
10971 return isl_bool_error;
10973 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10974 return isl_bool_false;
10976 nvar = isl_basic_set_dim(bset, isl_dim_set);
10977 ovar = isl_space_offset(bset->dim, isl_dim_set);
10978 for (j = 0; j < nvar; ++j) {
10979 int lower = 0, upper = 0;
10980 for (i = 0; i < bset->n_eq; ++i) {
10981 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10982 continue;
10983 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10984 return isl_bool_false;
10985 break;
10987 if (i < bset->n_eq)
10988 continue;
10989 for (i = 0; i < bset->n_ineq; ++i) {
10990 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10991 continue;
10992 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10993 return isl_bool_false;
10994 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10995 lower = 1;
10996 else
10997 upper = 1;
10999 if (!lower || !upper)
11000 return isl_bool_false;
11003 return isl_bool_true;
11006 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11008 if (!set)
11009 return isl_bool_error;
11010 if (set->n != 1)
11011 return isl_bool_false;
11013 return isl_basic_set_is_box(set->p[0]);
11016 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11018 if (!bset)
11019 return isl_bool_error;
11021 return isl_space_is_wrapping(bset->dim);
11024 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11026 if (!set)
11027 return isl_bool_error;
11029 return isl_space_is_wrapping(set->dim);
11032 /* Modify the space of "map" through a call to "change".
11033 * If "can_change" is set (not NULL), then first call it to check
11034 * if the modification is allowed, printing the error message "cannot_change"
11035 * if it is not.
11037 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11038 isl_bool (*can_change)(__isl_keep isl_map *map),
11039 const char *cannot_change,
11040 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11042 isl_bool ok;
11043 isl_space *space;
11045 if (!map)
11046 return NULL;
11048 ok = can_change ? can_change(map) : isl_bool_true;
11049 if (ok < 0)
11050 return isl_map_free(map);
11051 if (!ok)
11052 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11053 return isl_map_free(map));
11055 space = change(isl_map_get_space(map));
11056 map = isl_map_reset_space(map, space);
11058 return map;
11061 /* Is the domain of "map" a wrapped relation?
11063 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11065 if (!map)
11066 return isl_bool_error;
11068 return isl_space_domain_is_wrapping(map->dim);
11071 /* Is the range of "map" a wrapped relation?
11073 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11075 if (!map)
11076 return isl_bool_error;
11078 return isl_space_range_is_wrapping(map->dim);
11081 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11083 bmap = isl_basic_map_cow(bmap);
11084 if (!bmap)
11085 return NULL;
11087 bmap->dim = isl_space_wrap(bmap->dim);
11088 if (!bmap->dim)
11089 goto error;
11091 bmap = isl_basic_map_finalize(bmap);
11093 return bset_from_bmap(bmap);
11094 error:
11095 isl_basic_map_free(bmap);
11096 return NULL;
11099 /* Given a map A -> B, return the set (A -> B).
11101 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11103 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11106 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11108 bset = isl_basic_set_cow(bset);
11109 if (!bset)
11110 return NULL;
11112 bset->dim = isl_space_unwrap(bset->dim);
11113 if (!bset->dim)
11114 goto error;
11116 bset = isl_basic_set_finalize(bset);
11118 return bset_to_bmap(bset);
11119 error:
11120 isl_basic_set_free(bset);
11121 return NULL;
11124 /* Given a set (A -> B), return the map A -> B.
11125 * Error out if "set" is not of the form (A -> B).
11127 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11129 return isl_map_change_space(set, &isl_set_is_wrapping,
11130 "not a wrapping set", &isl_space_unwrap);
11133 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11134 enum isl_dim_type type)
11136 if (!bmap)
11137 return NULL;
11139 if (!isl_space_is_named_or_nested(bmap->dim, type))
11140 return bmap;
11142 bmap = isl_basic_map_cow(bmap);
11143 if (!bmap)
11144 return NULL;
11146 bmap->dim = isl_space_reset(bmap->dim, type);
11147 if (!bmap->dim)
11148 goto error;
11150 bmap = isl_basic_map_finalize(bmap);
11152 return bmap;
11153 error:
11154 isl_basic_map_free(bmap);
11155 return NULL;
11158 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11159 enum isl_dim_type type)
11161 int i;
11163 if (!map)
11164 return NULL;
11166 if (!isl_space_is_named_or_nested(map->dim, type))
11167 return map;
11169 map = isl_map_cow(map);
11170 if (!map)
11171 return NULL;
11173 for (i = 0; i < map->n; ++i) {
11174 map->p[i] = isl_basic_map_reset(map->p[i], type);
11175 if (!map->p[i])
11176 goto error;
11178 map->dim = isl_space_reset(map->dim, type);
11179 if (!map->dim)
11180 goto error;
11182 return map;
11183 error:
11184 isl_map_free(map);
11185 return NULL;
11188 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11190 if (!bmap)
11191 return NULL;
11193 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11194 return bmap;
11196 bmap = isl_basic_map_cow(bmap);
11197 if (!bmap)
11198 return NULL;
11200 bmap->dim = isl_space_flatten(bmap->dim);
11201 if (!bmap->dim)
11202 goto error;
11204 bmap = isl_basic_map_finalize(bmap);
11206 return bmap;
11207 error:
11208 isl_basic_map_free(bmap);
11209 return NULL;
11212 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11214 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11217 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11218 __isl_take isl_basic_map *bmap)
11220 if (!bmap)
11221 return NULL;
11223 if (!bmap->dim->nested[0])
11224 return bmap;
11226 bmap = isl_basic_map_cow(bmap);
11227 if (!bmap)
11228 return NULL;
11230 bmap->dim = isl_space_flatten_domain(bmap->dim);
11231 if (!bmap->dim)
11232 goto error;
11234 bmap = isl_basic_map_finalize(bmap);
11236 return bmap;
11237 error:
11238 isl_basic_map_free(bmap);
11239 return NULL;
11242 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11243 __isl_take isl_basic_map *bmap)
11245 if (!bmap)
11246 return NULL;
11248 if (!bmap->dim->nested[1])
11249 return bmap;
11251 bmap = isl_basic_map_cow(bmap);
11252 if (!bmap)
11253 return NULL;
11255 bmap->dim = isl_space_flatten_range(bmap->dim);
11256 if (!bmap->dim)
11257 goto error;
11259 bmap = isl_basic_map_finalize(bmap);
11261 return bmap;
11262 error:
11263 isl_basic_map_free(bmap);
11264 return NULL;
11267 /* Remove any internal structure from the spaces of domain and range of "map".
11269 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11271 if (!map)
11272 return NULL;
11274 if (!map->dim->nested[0] && !map->dim->nested[1])
11275 return map;
11277 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11280 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11282 return set_from_map(isl_map_flatten(set_to_map(set)));
11285 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11287 isl_space *dim, *flat_dim;
11288 isl_map *map;
11290 dim = isl_set_get_space(set);
11291 flat_dim = isl_space_flatten(isl_space_copy(dim));
11292 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11293 map = isl_map_intersect_domain(map, set);
11295 return map;
11298 /* Remove any internal structure from the space of the domain of "map".
11300 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11302 if (!map)
11303 return NULL;
11305 if (!map->dim->nested[0])
11306 return map;
11308 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11311 /* Remove any internal structure from the space of the range of "map".
11313 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11315 if (!map)
11316 return NULL;
11318 if (!map->dim->nested[1])
11319 return map;
11321 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11324 /* Reorder the dimensions of "bmap" according to the given dim_map
11325 * and set the dimension specification to "dim" and
11326 * perform Gaussian elimination on the result.
11328 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11329 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11331 isl_basic_map *res;
11332 unsigned flags;
11334 bmap = isl_basic_map_cow(bmap);
11335 if (!bmap || !dim || !dim_map)
11336 goto error;
11338 flags = bmap->flags;
11339 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11340 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11341 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11342 res = isl_basic_map_alloc_space(dim,
11343 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11344 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11345 if (res)
11346 res->flags = flags;
11347 res = isl_basic_map_gauss(res, NULL);
11348 res = isl_basic_map_finalize(res);
11349 return res;
11350 error:
11351 free(dim_map);
11352 isl_basic_map_free(bmap);
11353 isl_space_free(dim);
11354 return NULL;
11357 /* Reorder the dimensions of "map" according to given reordering.
11359 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11360 __isl_take isl_reordering *r)
11362 int i;
11363 struct isl_dim_map *dim_map;
11365 map = isl_map_cow(map);
11366 dim_map = isl_dim_map_from_reordering(r);
11367 if (!map || !r || !dim_map)
11368 goto error;
11370 for (i = 0; i < map->n; ++i) {
11371 struct isl_dim_map *dim_map_i;
11373 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11375 map->p[i] = isl_basic_map_realign(map->p[i],
11376 isl_space_copy(r->dim), dim_map_i);
11378 if (!map->p[i])
11379 goto error;
11382 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11384 isl_reordering_free(r);
11385 free(dim_map);
11386 return map;
11387 error:
11388 free(dim_map);
11389 isl_map_free(map);
11390 isl_reordering_free(r);
11391 return NULL;
11394 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11395 __isl_take isl_reordering *r)
11397 return set_from_map(isl_map_realign(set_to_map(set), r));
11400 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11401 __isl_take isl_space *model)
11403 isl_ctx *ctx;
11405 if (!map || !model)
11406 goto error;
11408 ctx = isl_space_get_ctx(model);
11409 if (!isl_space_has_named_params(model))
11410 isl_die(ctx, isl_error_invalid,
11411 "model has unnamed parameters", goto error);
11412 if (!isl_space_has_named_params(map->dim))
11413 isl_die(ctx, isl_error_invalid,
11414 "relation has unnamed parameters", goto error);
11415 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11416 isl_reordering *exp;
11418 model = isl_space_drop_dims(model, isl_dim_in,
11419 0, isl_space_dim(model, isl_dim_in));
11420 model = isl_space_drop_dims(model, isl_dim_out,
11421 0, isl_space_dim(model, isl_dim_out));
11422 exp = isl_parameter_alignment_reordering(map->dim, model);
11423 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11424 map = isl_map_realign(map, exp);
11427 isl_space_free(model);
11428 return map;
11429 error:
11430 isl_space_free(model);
11431 isl_map_free(map);
11432 return NULL;
11435 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11436 __isl_take isl_space *model)
11438 return isl_map_align_params(set, model);
11441 /* Align the parameters of "bmap" to those of "model", introducing
11442 * additional parameters if needed.
11444 __isl_give isl_basic_map *isl_basic_map_align_params(
11445 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11447 isl_ctx *ctx;
11449 if (!bmap || !model)
11450 goto error;
11452 ctx = isl_space_get_ctx(model);
11453 if (!isl_space_has_named_params(model))
11454 isl_die(ctx, isl_error_invalid,
11455 "model has unnamed parameters", goto error);
11456 if (!isl_space_has_named_params(bmap->dim))
11457 isl_die(ctx, isl_error_invalid,
11458 "relation has unnamed parameters", goto error);
11459 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11460 isl_reordering *exp;
11461 struct isl_dim_map *dim_map;
11463 model = isl_space_drop_dims(model, isl_dim_in,
11464 0, isl_space_dim(model, isl_dim_in));
11465 model = isl_space_drop_dims(model, isl_dim_out,
11466 0, isl_space_dim(model, isl_dim_out));
11467 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11468 exp = isl_reordering_extend_space(exp,
11469 isl_basic_map_get_space(bmap));
11470 dim_map = isl_dim_map_from_reordering(exp);
11471 bmap = isl_basic_map_realign(bmap,
11472 exp ? isl_space_copy(exp->dim) : NULL,
11473 isl_dim_map_extend(dim_map, bmap));
11474 isl_reordering_free(exp);
11475 free(dim_map);
11478 isl_space_free(model);
11479 return bmap;
11480 error:
11481 isl_space_free(model);
11482 isl_basic_map_free(bmap);
11483 return NULL;
11486 /* Align the parameters of "bset" to those of "model", introducing
11487 * additional parameters if needed.
11489 __isl_give isl_basic_set *isl_basic_set_align_params(
11490 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11492 return isl_basic_map_align_params(bset, model);
11495 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11496 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11497 enum isl_dim_type c2, enum isl_dim_type c3,
11498 enum isl_dim_type c4, enum isl_dim_type c5)
11500 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11501 struct isl_mat *mat;
11502 int i, j, k;
11503 int pos;
11505 if (!bmap)
11506 return NULL;
11507 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11508 isl_basic_map_total_dim(bmap) + 1);
11509 if (!mat)
11510 return NULL;
11511 for (i = 0; i < bmap->n_eq; ++i)
11512 for (j = 0, pos = 0; j < 5; ++j) {
11513 int off = isl_basic_map_offset(bmap, c[j]);
11514 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11515 isl_int_set(mat->row[i][pos],
11516 bmap->eq[i][off + k]);
11517 ++pos;
11521 return mat;
11524 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11525 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11526 enum isl_dim_type c2, enum isl_dim_type c3,
11527 enum isl_dim_type c4, enum isl_dim_type c5)
11529 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11530 struct isl_mat *mat;
11531 int i, j, k;
11532 int pos;
11534 if (!bmap)
11535 return NULL;
11536 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11537 isl_basic_map_total_dim(bmap) + 1);
11538 if (!mat)
11539 return NULL;
11540 for (i = 0; i < bmap->n_ineq; ++i)
11541 for (j = 0, pos = 0; j < 5; ++j) {
11542 int off = isl_basic_map_offset(bmap, c[j]);
11543 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11544 isl_int_set(mat->row[i][pos],
11545 bmap->ineq[i][off + k]);
11546 ++pos;
11550 return mat;
11553 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11554 __isl_take isl_space *dim,
11555 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11556 enum isl_dim_type c2, enum isl_dim_type c3,
11557 enum isl_dim_type c4, enum isl_dim_type c5)
11559 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11560 isl_basic_map *bmap;
11561 unsigned total;
11562 unsigned extra;
11563 int i, j, k, l;
11564 int pos;
11566 if (!dim || !eq || !ineq)
11567 goto error;
11569 if (eq->n_col != ineq->n_col)
11570 isl_die(dim->ctx, isl_error_invalid,
11571 "equalities and inequalities matrices should have "
11572 "same number of columns", goto error);
11574 total = 1 + isl_space_dim(dim, isl_dim_all);
11576 if (eq->n_col < total)
11577 isl_die(dim->ctx, isl_error_invalid,
11578 "number of columns too small", goto error);
11580 extra = eq->n_col - total;
11582 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11583 eq->n_row, ineq->n_row);
11584 if (!bmap)
11585 goto error;
11586 for (i = 0; i < extra; ++i) {
11587 k = isl_basic_map_alloc_div(bmap);
11588 if (k < 0)
11589 goto error;
11590 isl_int_set_si(bmap->div[k][0], 0);
11592 for (i = 0; i < eq->n_row; ++i) {
11593 l = isl_basic_map_alloc_equality(bmap);
11594 if (l < 0)
11595 goto error;
11596 for (j = 0, pos = 0; j < 5; ++j) {
11597 int off = isl_basic_map_offset(bmap, c[j]);
11598 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11599 isl_int_set(bmap->eq[l][off + k],
11600 eq->row[i][pos]);
11601 ++pos;
11605 for (i = 0; i < ineq->n_row; ++i) {
11606 l = isl_basic_map_alloc_inequality(bmap);
11607 if (l < 0)
11608 goto error;
11609 for (j = 0, pos = 0; j < 5; ++j) {
11610 int off = isl_basic_map_offset(bmap, c[j]);
11611 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11612 isl_int_set(bmap->ineq[l][off + k],
11613 ineq->row[i][pos]);
11614 ++pos;
11619 isl_space_free(dim);
11620 isl_mat_free(eq);
11621 isl_mat_free(ineq);
11623 bmap = isl_basic_map_simplify(bmap);
11624 return isl_basic_map_finalize(bmap);
11625 error:
11626 isl_space_free(dim);
11627 isl_mat_free(eq);
11628 isl_mat_free(ineq);
11629 return NULL;
11632 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11633 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11634 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11636 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11637 c1, c2, c3, c4, isl_dim_in);
11640 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11641 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11642 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11644 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11645 c1, c2, c3, c4, isl_dim_in);
11648 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11649 __isl_take isl_space *dim,
11650 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11651 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11653 isl_basic_map *bmap;
11654 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11655 c1, c2, c3, c4, isl_dim_in);
11656 return bset_from_bmap(bmap);
11659 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11661 if (!bmap)
11662 return isl_bool_error;
11664 return isl_space_can_zip(bmap->dim);
11667 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11669 if (!map)
11670 return isl_bool_error;
11672 return isl_space_can_zip(map->dim);
11675 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11676 * (A -> C) -> (B -> D).
11678 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11680 unsigned pos;
11681 unsigned n1;
11682 unsigned n2;
11684 if (!bmap)
11685 return NULL;
11687 if (!isl_basic_map_can_zip(bmap))
11688 isl_die(bmap->ctx, isl_error_invalid,
11689 "basic map cannot be zipped", goto error);
11690 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11691 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11692 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11693 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11694 bmap = isl_basic_map_cow(bmap);
11695 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11696 if (!bmap)
11697 return NULL;
11698 bmap->dim = isl_space_zip(bmap->dim);
11699 if (!bmap->dim)
11700 goto error;
11701 bmap = isl_basic_map_mark_final(bmap);
11702 return bmap;
11703 error:
11704 isl_basic_map_free(bmap);
11705 return NULL;
11708 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11709 * (A -> C) -> (B -> D).
11711 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11713 int i;
11715 if (!map)
11716 return NULL;
11718 if (!isl_map_can_zip(map))
11719 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11720 goto error);
11722 map = isl_map_cow(map);
11723 if (!map)
11724 return NULL;
11726 for (i = 0; i < map->n; ++i) {
11727 map->p[i] = isl_basic_map_zip(map->p[i]);
11728 if (!map->p[i])
11729 goto error;
11732 map->dim = isl_space_zip(map->dim);
11733 if (!map->dim)
11734 goto error;
11736 return map;
11737 error:
11738 isl_map_free(map);
11739 return NULL;
11742 /* Can we apply isl_basic_map_curry to "bmap"?
11743 * That is, does it have a nested relation in its domain?
11745 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11747 if (!bmap)
11748 return isl_bool_error;
11750 return isl_space_can_curry(bmap->dim);
11753 /* Can we apply isl_map_curry to "map"?
11754 * That is, does it have a nested relation in its domain?
11756 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11758 if (!map)
11759 return isl_bool_error;
11761 return isl_space_can_curry(map->dim);
11764 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11765 * A -> (B -> C).
11767 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11770 if (!bmap)
11771 return NULL;
11773 if (!isl_basic_map_can_curry(bmap))
11774 isl_die(bmap->ctx, isl_error_invalid,
11775 "basic map cannot be curried", goto error);
11776 bmap = isl_basic_map_cow(bmap);
11777 if (!bmap)
11778 return NULL;
11779 bmap->dim = isl_space_curry(bmap->dim);
11780 if (!bmap->dim)
11781 goto error;
11782 bmap = isl_basic_map_mark_final(bmap);
11783 return bmap;
11784 error:
11785 isl_basic_map_free(bmap);
11786 return NULL;
11789 /* Given a map (A -> B) -> C, return the corresponding map
11790 * A -> (B -> C).
11792 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11794 return isl_map_change_space(map, &isl_map_can_curry,
11795 "map cannot be curried", &isl_space_curry);
11798 /* Can isl_map_range_curry be applied to "map"?
11799 * That is, does it have a nested relation in its range,
11800 * the domain of which is itself a nested relation?
11802 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11804 if (!map)
11805 return isl_bool_error;
11807 return isl_space_can_range_curry(map->dim);
11810 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11811 * A -> (B -> (C -> D)).
11813 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11815 return isl_map_change_space(map, &isl_map_can_range_curry,
11816 "map range cannot be curried",
11817 &isl_space_range_curry);
11820 /* Can we apply isl_basic_map_uncurry to "bmap"?
11821 * That is, does it have a nested relation in its domain?
11823 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11825 if (!bmap)
11826 return isl_bool_error;
11828 return isl_space_can_uncurry(bmap->dim);
11831 /* Can we apply isl_map_uncurry to "map"?
11832 * That is, does it have a nested relation in its domain?
11834 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11836 if (!map)
11837 return isl_bool_error;
11839 return isl_space_can_uncurry(map->dim);
11842 /* Given a basic map A -> (B -> C), return the corresponding basic map
11843 * (A -> B) -> C.
11845 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11848 if (!bmap)
11849 return NULL;
11851 if (!isl_basic_map_can_uncurry(bmap))
11852 isl_die(bmap->ctx, isl_error_invalid,
11853 "basic map cannot be uncurried",
11854 return isl_basic_map_free(bmap));
11855 bmap = isl_basic_map_cow(bmap);
11856 if (!bmap)
11857 return NULL;
11858 bmap->dim = isl_space_uncurry(bmap->dim);
11859 if (!bmap->dim)
11860 return isl_basic_map_free(bmap);
11861 bmap = isl_basic_map_mark_final(bmap);
11862 return bmap;
11865 /* Given a map A -> (B -> C), return the corresponding map
11866 * (A -> B) -> C.
11868 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11870 return isl_map_change_space(map, &isl_map_can_uncurry,
11871 "map cannot be uncurried", &isl_space_uncurry);
11874 /* Construct a basic map mapping the domain of the affine expression
11875 * to a one-dimensional range prescribed by the affine expression.
11876 * If "rational" is set, then construct a rational basic map.
11878 * A NaN affine expression cannot be converted to a basic map.
11880 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11881 __isl_take isl_aff *aff, int rational)
11883 int k;
11884 int pos;
11885 isl_bool is_nan;
11886 isl_local_space *ls;
11887 isl_basic_map *bmap = NULL;
11889 if (!aff)
11890 return NULL;
11891 is_nan = isl_aff_is_nan(aff);
11892 if (is_nan < 0)
11893 goto error;
11894 if (is_nan)
11895 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11896 "cannot convert NaN", goto error);
11898 ls = isl_aff_get_local_space(aff);
11899 bmap = isl_basic_map_from_local_space(ls);
11900 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11901 k = isl_basic_map_alloc_equality(bmap);
11902 if (k < 0)
11903 goto error;
11905 pos = isl_basic_map_offset(bmap, isl_dim_out);
11906 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11907 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11908 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11909 aff->v->size - (pos + 1));
11911 isl_aff_free(aff);
11912 if (rational)
11913 bmap = isl_basic_map_set_rational(bmap);
11914 bmap = isl_basic_map_finalize(bmap);
11915 return bmap;
11916 error:
11917 isl_aff_free(aff);
11918 isl_basic_map_free(bmap);
11919 return NULL;
11922 /* Construct a basic map mapping the domain of the affine expression
11923 * to a one-dimensional range prescribed by the affine expression.
11925 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11927 return isl_basic_map_from_aff2(aff, 0);
11930 /* Construct a map mapping the domain of the affine expression
11931 * to a one-dimensional range prescribed by the affine expression.
11933 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11935 isl_basic_map *bmap;
11937 bmap = isl_basic_map_from_aff(aff);
11938 return isl_map_from_basic_map(bmap);
11941 /* Construct a basic map mapping the domain the multi-affine expression
11942 * to its range, with each dimension in the range equated to the
11943 * corresponding affine expression.
11944 * If "rational" is set, then construct a rational basic map.
11946 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
11947 __isl_take isl_multi_aff *maff, int rational)
11949 int i;
11950 isl_space *space;
11951 isl_basic_map *bmap;
11953 if (!maff)
11954 return NULL;
11956 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11957 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11958 "invalid space", goto error);
11960 space = isl_space_domain(isl_multi_aff_get_space(maff));
11961 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11962 if (rational)
11963 bmap = isl_basic_map_set_rational(bmap);
11965 for (i = 0; i < maff->n; ++i) {
11966 isl_aff *aff;
11967 isl_basic_map *bmap_i;
11969 aff = isl_aff_copy(maff->p[i]);
11970 bmap_i = isl_basic_map_from_aff2(aff, rational);
11972 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11975 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11977 isl_multi_aff_free(maff);
11978 return bmap;
11979 error:
11980 isl_multi_aff_free(maff);
11981 return NULL;
11984 /* Construct a basic map mapping the domain the multi-affine expression
11985 * to its range, with each dimension in the range equated to the
11986 * corresponding affine expression.
11988 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11989 __isl_take isl_multi_aff *ma)
11991 return isl_basic_map_from_multi_aff2(ma, 0);
11994 /* Construct a map mapping the domain the multi-affine expression
11995 * to its range, with each dimension in the range equated to the
11996 * corresponding affine expression.
11998 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12000 isl_basic_map *bmap;
12002 bmap = isl_basic_map_from_multi_aff(maff);
12003 return isl_map_from_basic_map(bmap);
12006 /* Construct a basic map mapping a domain in the given space to
12007 * to an n-dimensional range, with n the number of elements in the list,
12008 * where each coordinate in the range is prescribed by the
12009 * corresponding affine expression.
12010 * The domains of all affine expressions in the list are assumed to match
12011 * domain_dim.
12013 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12014 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12016 int i;
12017 isl_space *dim;
12018 isl_basic_map *bmap;
12020 if (!list)
12021 return NULL;
12023 dim = isl_space_from_domain(domain_dim);
12024 bmap = isl_basic_map_universe(dim);
12026 for (i = 0; i < list->n; ++i) {
12027 isl_aff *aff;
12028 isl_basic_map *bmap_i;
12030 aff = isl_aff_copy(list->p[i]);
12031 bmap_i = isl_basic_map_from_aff(aff);
12033 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12036 isl_aff_list_free(list);
12037 return bmap;
12040 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12041 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12043 return isl_map_equate(set, type1, pos1, type2, pos2);
12046 /* Construct a basic map where the given dimensions are equal to each other.
12048 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12049 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12051 isl_basic_map *bmap = NULL;
12052 int i;
12054 if (!space)
12055 return NULL;
12057 if (pos1 >= isl_space_dim(space, type1))
12058 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12059 "index out of bounds", goto error);
12060 if (pos2 >= isl_space_dim(space, type2))
12061 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12062 "index out of bounds", goto error);
12064 if (type1 == type2 && pos1 == pos2)
12065 return isl_basic_map_universe(space);
12067 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12068 i = isl_basic_map_alloc_equality(bmap);
12069 if (i < 0)
12070 goto error;
12071 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12072 pos1 += isl_basic_map_offset(bmap, type1);
12073 pos2 += isl_basic_map_offset(bmap, type2);
12074 isl_int_set_si(bmap->eq[i][pos1], -1);
12075 isl_int_set_si(bmap->eq[i][pos2], 1);
12076 bmap = isl_basic_map_finalize(bmap);
12077 isl_space_free(space);
12078 return bmap;
12079 error:
12080 isl_space_free(space);
12081 isl_basic_map_free(bmap);
12082 return NULL;
12085 /* Add a constraint imposing that the given two dimensions are equal.
12087 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12088 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12090 isl_basic_map *eq;
12092 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12094 bmap = isl_basic_map_intersect(bmap, eq);
12096 return bmap;
12099 /* Add a constraint imposing that the given two dimensions are equal.
12101 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12102 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12104 isl_basic_map *bmap;
12106 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12108 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12110 return map;
12113 /* Add a constraint imposing that the given two dimensions have opposite values.
12115 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12116 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12118 isl_basic_map *bmap = NULL;
12119 int i;
12121 if (!map)
12122 return NULL;
12124 if (pos1 >= isl_map_dim(map, type1))
12125 isl_die(map->ctx, isl_error_invalid,
12126 "index out of bounds", goto error);
12127 if (pos2 >= isl_map_dim(map, type2))
12128 isl_die(map->ctx, isl_error_invalid,
12129 "index out of bounds", goto error);
12131 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12132 i = isl_basic_map_alloc_equality(bmap);
12133 if (i < 0)
12134 goto error;
12135 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12136 pos1 += isl_basic_map_offset(bmap, type1);
12137 pos2 += isl_basic_map_offset(bmap, type2);
12138 isl_int_set_si(bmap->eq[i][pos1], 1);
12139 isl_int_set_si(bmap->eq[i][pos2], 1);
12140 bmap = isl_basic_map_finalize(bmap);
12142 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12144 return map;
12145 error:
12146 isl_basic_map_free(bmap);
12147 isl_map_free(map);
12148 return NULL;
12151 /* Construct a constraint imposing that the value of the first dimension is
12152 * greater than or equal to that of the second.
12154 static __isl_give isl_constraint *constraint_order_ge(
12155 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12156 enum isl_dim_type type2, int pos2)
12158 isl_constraint *c;
12160 if (!space)
12161 return NULL;
12163 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12165 if (pos1 >= isl_constraint_dim(c, type1))
12166 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12167 "index out of bounds", return isl_constraint_free(c));
12168 if (pos2 >= isl_constraint_dim(c, type2))
12169 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12170 "index out of bounds", return isl_constraint_free(c));
12172 if (type1 == type2 && pos1 == pos2)
12173 return c;
12175 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12176 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12178 return c;
12181 /* Add a constraint imposing that the value of the first dimension is
12182 * greater than or equal to that of the second.
12184 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12185 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12187 isl_constraint *c;
12188 isl_space *space;
12190 if (type1 == type2 && pos1 == pos2)
12191 return bmap;
12192 space = isl_basic_map_get_space(bmap);
12193 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12194 bmap = isl_basic_map_add_constraint(bmap, c);
12196 return bmap;
12199 /* Add a constraint imposing that the value of the first dimension is
12200 * greater than or equal to that of the second.
12202 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12203 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12205 isl_constraint *c;
12206 isl_space *space;
12208 if (type1 == type2 && pos1 == pos2)
12209 return map;
12210 space = isl_map_get_space(map);
12211 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12212 map = isl_map_add_constraint(map, c);
12214 return map;
12217 /* Add a constraint imposing that the value of the first dimension is
12218 * less than or equal to that of the second.
12220 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12221 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12223 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12226 /* Construct a basic map where the value of the first dimension is
12227 * greater than that of the second.
12229 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12230 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12232 isl_basic_map *bmap = NULL;
12233 int i;
12235 if (!space)
12236 return NULL;
12238 if (pos1 >= isl_space_dim(space, type1))
12239 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12240 "index out of bounds", goto error);
12241 if (pos2 >= isl_space_dim(space, type2))
12242 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12243 "index out of bounds", goto error);
12245 if (type1 == type2 && pos1 == pos2)
12246 return isl_basic_map_empty(space);
12248 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12249 i = isl_basic_map_alloc_inequality(bmap);
12250 if (i < 0)
12251 return isl_basic_map_free(bmap);
12252 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12253 pos1 += isl_basic_map_offset(bmap, type1);
12254 pos2 += isl_basic_map_offset(bmap, type2);
12255 isl_int_set_si(bmap->ineq[i][pos1], 1);
12256 isl_int_set_si(bmap->ineq[i][pos2], -1);
12257 isl_int_set_si(bmap->ineq[i][0], -1);
12258 bmap = isl_basic_map_finalize(bmap);
12260 return bmap;
12261 error:
12262 isl_space_free(space);
12263 isl_basic_map_free(bmap);
12264 return NULL;
12267 /* Add a constraint imposing that the value of the first dimension is
12268 * greater than that of the second.
12270 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12271 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12273 isl_basic_map *gt;
12275 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12277 bmap = isl_basic_map_intersect(bmap, gt);
12279 return bmap;
12282 /* Add a constraint imposing that the value of the first dimension is
12283 * greater than that of the second.
12285 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12286 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12288 isl_basic_map *bmap;
12290 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12292 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12294 return map;
12297 /* Add a constraint imposing that the value of the first dimension is
12298 * smaller than that of the second.
12300 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12301 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12303 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12306 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12307 int pos)
12309 isl_aff *div;
12310 isl_local_space *ls;
12312 if (!bmap)
12313 return NULL;
12315 if (!isl_basic_map_divs_known(bmap))
12316 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12317 "some divs are unknown", return NULL);
12319 ls = isl_basic_map_get_local_space(bmap);
12320 div = isl_local_space_get_div(ls, pos);
12321 isl_local_space_free(ls);
12323 return div;
12326 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12327 int pos)
12329 return isl_basic_map_get_div(bset, pos);
12332 /* Plug in "subs" for dimension "type", "pos" of "bset".
12334 * Let i be the dimension to replace and let "subs" be of the form
12336 * f/d
12338 * Any integer division with a non-zero coefficient for i,
12340 * floor((a i + g)/m)
12342 * is replaced by
12344 * floor((a f + d g)/(m d))
12346 * Constraints of the form
12348 * a i + g
12350 * are replaced by
12352 * a f + d g
12354 * We currently require that "subs" is an integral expression.
12355 * Handling rational expressions may require us to add stride constraints
12356 * as we do in isl_basic_set_preimage_multi_aff.
12358 __isl_give isl_basic_set *isl_basic_set_substitute(
12359 __isl_take isl_basic_set *bset,
12360 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12362 int i;
12363 isl_int v;
12364 isl_ctx *ctx;
12366 if (bset && isl_basic_set_plain_is_empty(bset))
12367 return bset;
12369 bset = isl_basic_set_cow(bset);
12370 if (!bset || !subs)
12371 goto error;
12373 ctx = isl_basic_set_get_ctx(bset);
12374 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12375 isl_die(ctx, isl_error_invalid,
12376 "spaces don't match", goto error);
12377 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12378 isl_die(ctx, isl_error_unsupported,
12379 "cannot handle divs yet", goto error);
12380 if (!isl_int_is_one(subs->v->el[0]))
12381 isl_die(ctx, isl_error_invalid,
12382 "can only substitute integer expressions", goto error);
12384 pos += isl_basic_set_offset(bset, type);
12386 isl_int_init(v);
12388 for (i = 0; i < bset->n_eq; ++i) {
12389 if (isl_int_is_zero(bset->eq[i][pos]))
12390 continue;
12391 isl_int_set(v, bset->eq[i][pos]);
12392 isl_int_set_si(bset->eq[i][pos], 0);
12393 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12394 v, subs->v->el + 1, subs->v->size - 1);
12397 for (i = 0; i < bset->n_ineq; ++i) {
12398 if (isl_int_is_zero(bset->ineq[i][pos]))
12399 continue;
12400 isl_int_set(v, bset->ineq[i][pos]);
12401 isl_int_set_si(bset->ineq[i][pos], 0);
12402 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12403 v, subs->v->el + 1, subs->v->size - 1);
12406 for (i = 0; i < bset->n_div; ++i) {
12407 if (isl_int_is_zero(bset->div[i][1 + pos]))
12408 continue;
12409 isl_int_set(v, bset->div[i][1 + pos]);
12410 isl_int_set_si(bset->div[i][1 + pos], 0);
12411 isl_seq_combine(bset->div[i] + 1,
12412 subs->v->el[0], bset->div[i] + 1,
12413 v, subs->v->el + 1, subs->v->size - 1);
12414 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12417 isl_int_clear(v);
12419 bset = isl_basic_set_simplify(bset);
12420 return isl_basic_set_finalize(bset);
12421 error:
12422 isl_basic_set_free(bset);
12423 return NULL;
12426 /* Plug in "subs" for dimension "type", "pos" of "set".
12428 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12429 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12431 int i;
12433 if (set && isl_set_plain_is_empty(set))
12434 return set;
12436 set = isl_set_cow(set);
12437 if (!set || !subs)
12438 goto error;
12440 for (i = set->n - 1; i >= 0; --i) {
12441 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12442 if (remove_if_empty(set, i) < 0)
12443 goto error;
12446 return set;
12447 error:
12448 isl_set_free(set);
12449 return NULL;
12452 /* Check if the range of "ma" is compatible with the domain or range
12453 * (depending on "type") of "bmap".
12455 static isl_stat check_basic_map_compatible_range_multi_aff(
12456 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12457 __isl_keep isl_multi_aff *ma)
12459 isl_bool m;
12460 isl_space *ma_space;
12462 ma_space = isl_multi_aff_get_space(ma);
12464 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12465 if (m < 0)
12466 goto error;
12467 if (!m)
12468 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12469 "parameters don't match", goto error);
12470 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12471 if (m < 0)
12472 goto error;
12473 if (!m)
12474 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12475 "spaces don't match", goto error);
12477 isl_space_free(ma_space);
12478 return isl_stat_ok;
12479 error:
12480 isl_space_free(ma_space);
12481 return isl_stat_error;
12484 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12485 * coefficients before the transformed range of dimensions,
12486 * the "n_after" coefficients after the transformed range of dimensions
12487 * and the coefficients of the other divs in "bmap".
12489 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12490 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12492 int i;
12493 int n_param;
12494 int n_set;
12495 isl_local_space *ls;
12497 if (n_div == 0)
12498 return 0;
12500 ls = isl_aff_get_domain_local_space(ma->p[0]);
12501 if (!ls)
12502 return -1;
12504 n_param = isl_local_space_dim(ls, isl_dim_param);
12505 n_set = isl_local_space_dim(ls, isl_dim_set);
12506 for (i = 0; i < n_div; ++i) {
12507 int o_bmap = 0, o_ls = 0;
12509 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12510 o_bmap += 1 + 1 + n_param;
12511 o_ls += 1 + 1 + n_param;
12512 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12513 o_bmap += n_before;
12514 isl_seq_cpy(bmap->div[i] + o_bmap,
12515 ls->div->row[i] + o_ls, n_set);
12516 o_bmap += n_set;
12517 o_ls += n_set;
12518 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12519 o_bmap += n_after;
12520 isl_seq_cpy(bmap->div[i] + o_bmap,
12521 ls->div->row[i] + o_ls, n_div);
12522 o_bmap += n_div;
12523 o_ls += n_div;
12524 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12525 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12526 goto error;
12529 isl_local_space_free(ls);
12530 return 0;
12531 error:
12532 isl_local_space_free(ls);
12533 return -1;
12536 /* How many stride constraints does "ma" enforce?
12537 * That is, how many of the affine expressions have a denominator
12538 * different from one?
12540 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12542 int i;
12543 int strides = 0;
12545 for (i = 0; i < ma->n; ++i)
12546 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12547 strides++;
12549 return strides;
12552 /* For each affine expression in ma of the form
12554 * x_i = (f_i y + h_i)/m_i
12556 * with m_i different from one, add a constraint to "bmap"
12557 * of the form
12559 * f_i y + h_i = m_i alpha_i
12561 * with alpha_i an additional existentially quantified variable.
12563 * The input variables of "ma" correspond to a subset of the variables
12564 * of "bmap". There are "n_before" variables in "bmap" before this
12565 * subset and "n_after" variables after this subset.
12566 * The integer divisions of the affine expressions in "ma" are assumed
12567 * to have been aligned. There are "n_div_ma" of them and
12568 * they appear first in "bmap", straight after the "n_after" variables.
12570 static __isl_give isl_basic_map *add_ma_strides(
12571 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12572 int n_before, int n_after, int n_div_ma)
12574 int i, k;
12575 int div;
12576 int total;
12577 int n_param;
12578 int n_in;
12580 total = isl_basic_map_total_dim(bmap);
12581 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12582 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12583 for (i = 0; i < ma->n; ++i) {
12584 int o_bmap = 0, o_ma = 1;
12586 if (isl_int_is_one(ma->p[i]->v->el[0]))
12587 continue;
12588 div = isl_basic_map_alloc_div(bmap);
12589 k = isl_basic_map_alloc_equality(bmap);
12590 if (div < 0 || k < 0)
12591 goto error;
12592 isl_int_set_si(bmap->div[div][0], 0);
12593 isl_seq_cpy(bmap->eq[k] + o_bmap,
12594 ma->p[i]->v->el + o_ma, 1 + n_param);
12595 o_bmap += 1 + n_param;
12596 o_ma += 1 + n_param;
12597 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12598 o_bmap += n_before;
12599 isl_seq_cpy(bmap->eq[k] + o_bmap,
12600 ma->p[i]->v->el + o_ma, n_in);
12601 o_bmap += n_in;
12602 o_ma += n_in;
12603 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12604 o_bmap += n_after;
12605 isl_seq_cpy(bmap->eq[k] + o_bmap,
12606 ma->p[i]->v->el + o_ma, n_div_ma);
12607 o_bmap += n_div_ma;
12608 o_ma += n_div_ma;
12609 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12610 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12611 total++;
12614 return bmap;
12615 error:
12616 isl_basic_map_free(bmap);
12617 return NULL;
12620 /* Replace the domain or range space (depending on "type) of "space" by "set".
12622 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12623 enum isl_dim_type type, __isl_take isl_space *set)
12625 if (type == isl_dim_in) {
12626 space = isl_space_range(space);
12627 space = isl_space_map_from_domain_and_range(set, space);
12628 } else {
12629 space = isl_space_domain(space);
12630 space = isl_space_map_from_domain_and_range(space, set);
12633 return space;
12636 /* Compute the preimage of the domain or range (depending on "type")
12637 * of "bmap" under the function represented by "ma".
12638 * In other words, plug in "ma" in the domain or range of "bmap".
12639 * The result is a basic map that lives in the same space as "bmap"
12640 * except that the domain or range has been replaced by
12641 * the domain space of "ma".
12643 * If bmap is represented by
12645 * A(p) + S u + B x + T v + C(divs) >= 0,
12647 * where u and x are input and output dimensions if type == isl_dim_out
12648 * while x and v are input and output dimensions if type == isl_dim_in,
12649 * and ma is represented by
12651 * x = D(p) + F(y) + G(divs')
12653 * then the result is
12655 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12657 * The divs in the input set are similarly adjusted.
12658 * In particular
12660 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12662 * becomes
12664 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12665 * B_i G(divs') + c_i(divs))/n_i)
12667 * If bmap is not a rational map and if F(y) involves any denominators
12669 * x_i = (f_i y + h_i)/m_i
12671 * then additional constraints are added to ensure that we only
12672 * map back integer points. That is we enforce
12674 * f_i y + h_i = m_i alpha_i
12676 * with alpha_i an additional existentially quantified variable.
12678 * We first copy over the divs from "ma".
12679 * Then we add the modified constraints and divs from "bmap".
12680 * Finally, we add the stride constraints, if needed.
12682 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12683 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12684 __isl_take isl_multi_aff *ma)
12686 int i, k;
12687 isl_space *space;
12688 isl_basic_map *res = NULL;
12689 int n_before, n_after, n_div_bmap, n_div_ma;
12690 isl_int f, c1, c2, g;
12691 isl_bool rational;
12692 int strides;
12694 isl_int_init(f);
12695 isl_int_init(c1);
12696 isl_int_init(c2);
12697 isl_int_init(g);
12699 ma = isl_multi_aff_align_divs(ma);
12700 if (!bmap || !ma)
12701 goto error;
12702 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12703 goto error;
12705 if (type == isl_dim_in) {
12706 n_before = 0;
12707 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12708 } else {
12709 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12710 n_after = 0;
12712 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12713 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12715 space = isl_multi_aff_get_domain_space(ma);
12716 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12717 rational = isl_basic_map_is_rational(bmap);
12718 strides = rational ? 0 : multi_aff_strides(ma);
12719 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12720 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12721 if (rational)
12722 res = isl_basic_map_set_rational(res);
12724 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12725 if (isl_basic_map_alloc_div(res) < 0)
12726 goto error;
12728 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12729 goto error;
12731 for (i = 0; i < bmap->n_eq; ++i) {
12732 k = isl_basic_map_alloc_equality(res);
12733 if (k < 0)
12734 goto error;
12735 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12736 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12739 for (i = 0; i < bmap->n_ineq; ++i) {
12740 k = isl_basic_map_alloc_inequality(res);
12741 if (k < 0)
12742 goto error;
12743 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12744 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12747 for (i = 0; i < bmap->n_div; ++i) {
12748 if (isl_int_is_zero(bmap->div[i][0])) {
12749 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12750 continue;
12752 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12753 n_before, n_after, n_div_ma, n_div_bmap,
12754 f, c1, c2, g, 1);
12757 if (strides)
12758 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12760 isl_int_clear(f);
12761 isl_int_clear(c1);
12762 isl_int_clear(c2);
12763 isl_int_clear(g);
12764 isl_basic_map_free(bmap);
12765 isl_multi_aff_free(ma);
12766 res = isl_basic_map_simplify(res);
12767 return isl_basic_map_finalize(res);
12768 error:
12769 isl_int_clear(f);
12770 isl_int_clear(c1);
12771 isl_int_clear(c2);
12772 isl_int_clear(g);
12773 isl_basic_map_free(bmap);
12774 isl_multi_aff_free(ma);
12775 isl_basic_map_free(res);
12776 return NULL;
12779 /* Compute the preimage of "bset" under the function represented by "ma".
12780 * In other words, plug in "ma" in "bset". The result is a basic set
12781 * that lives in the domain space of "ma".
12783 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12784 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12786 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12789 /* Compute the preimage of the domain of "bmap" under the function
12790 * represented by "ma".
12791 * In other words, plug in "ma" in the domain of "bmap".
12792 * The result is a basic map that lives in the same space as "bmap"
12793 * except that the domain has been replaced by the domain space of "ma".
12795 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12796 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12798 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12801 /* Compute the preimage of the range of "bmap" under the function
12802 * represented by "ma".
12803 * In other words, plug in "ma" in the range of "bmap".
12804 * The result is a basic map that lives in the same space as "bmap"
12805 * except that the range has been replaced by the domain space of "ma".
12807 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12808 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12810 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12813 /* Check if the range of "ma" is compatible with the domain or range
12814 * (depending on "type") of "map".
12815 * Return isl_stat_error if anything is wrong.
12817 static isl_stat check_map_compatible_range_multi_aff(
12818 __isl_keep isl_map *map, enum isl_dim_type type,
12819 __isl_keep isl_multi_aff *ma)
12821 isl_bool m;
12822 isl_space *ma_space;
12824 ma_space = isl_multi_aff_get_space(ma);
12825 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12826 isl_space_free(ma_space);
12827 if (m < 0)
12828 return isl_stat_error;
12829 if (!m)
12830 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12831 "spaces don't match", return isl_stat_error);
12832 return isl_stat_ok;
12835 /* Compute the preimage of the domain or range (depending on "type")
12836 * of "map" under the function represented by "ma".
12837 * In other words, plug in "ma" in the domain or range of "map".
12838 * The result is a map that lives in the same space as "map"
12839 * except that the domain or range has been replaced by
12840 * the domain space of "ma".
12842 * The parameters are assumed to have been aligned.
12844 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12845 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12847 int i;
12848 isl_space *space;
12850 map = isl_map_cow(map);
12851 ma = isl_multi_aff_align_divs(ma);
12852 if (!map || !ma)
12853 goto error;
12854 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12855 goto error;
12857 for (i = 0; i < map->n; ++i) {
12858 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12859 isl_multi_aff_copy(ma));
12860 if (!map->p[i])
12861 goto error;
12864 space = isl_multi_aff_get_domain_space(ma);
12865 space = isl_space_set(isl_map_get_space(map), type, space);
12867 isl_space_free(map->dim);
12868 map->dim = space;
12869 if (!map->dim)
12870 goto error;
12872 isl_multi_aff_free(ma);
12873 if (map->n > 1)
12874 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12875 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12876 return map;
12877 error:
12878 isl_multi_aff_free(ma);
12879 isl_map_free(map);
12880 return NULL;
12883 /* Compute the preimage of the domain or range (depending on "type")
12884 * of "map" under the function represented by "ma".
12885 * In other words, plug in "ma" in the domain or range of "map".
12886 * The result is a map that lives in the same space as "map"
12887 * except that the domain or range has been replaced by
12888 * the domain space of "ma".
12890 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12891 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12893 if (!map || !ma)
12894 goto error;
12896 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12897 return map_preimage_multi_aff(map, type, ma);
12899 if (!isl_space_has_named_params(map->dim) ||
12900 !isl_space_has_named_params(ma->space))
12901 isl_die(map->ctx, isl_error_invalid,
12902 "unaligned unnamed parameters", goto error);
12903 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12904 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12906 return map_preimage_multi_aff(map, type, ma);
12907 error:
12908 isl_multi_aff_free(ma);
12909 return isl_map_free(map);
12912 /* Compute the preimage of "set" under the function represented by "ma".
12913 * In other words, plug in "ma" in "set". The result is a set
12914 * that lives in the domain space of "ma".
12916 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12917 __isl_take isl_multi_aff *ma)
12919 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12922 /* Compute the preimage of the domain of "map" under the function
12923 * represented by "ma".
12924 * In other words, plug in "ma" in the domain of "map".
12925 * The result is a map that lives in the same space as "map"
12926 * except that the domain has been replaced by the domain space of "ma".
12928 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12929 __isl_take isl_multi_aff *ma)
12931 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12934 /* Compute the preimage of the range of "map" under the function
12935 * represented by "ma".
12936 * In other words, plug in "ma" in the range of "map".
12937 * The result is a map that lives in the same space as "map"
12938 * except that the range has been replaced by the domain space of "ma".
12940 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12941 __isl_take isl_multi_aff *ma)
12943 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12946 /* Compute the preimage of "map" under the function represented by "pma".
12947 * In other words, plug in "pma" in the domain or range of "map".
12948 * The result is a map that lives in the same space as "map",
12949 * except that the space of type "type" has been replaced by
12950 * the domain space of "pma".
12952 * The parameters of "map" and "pma" are assumed to have been aligned.
12954 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12955 __isl_take isl_map *map, enum isl_dim_type type,
12956 __isl_take isl_pw_multi_aff *pma)
12958 int i;
12959 isl_map *res;
12961 if (!pma)
12962 goto error;
12964 if (pma->n == 0) {
12965 isl_pw_multi_aff_free(pma);
12966 res = isl_map_empty(isl_map_get_space(map));
12967 isl_map_free(map);
12968 return res;
12971 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12972 isl_multi_aff_copy(pma->p[0].maff));
12973 if (type == isl_dim_in)
12974 res = isl_map_intersect_domain(res,
12975 isl_map_copy(pma->p[0].set));
12976 else
12977 res = isl_map_intersect_range(res,
12978 isl_map_copy(pma->p[0].set));
12980 for (i = 1; i < pma->n; ++i) {
12981 isl_map *res_i;
12983 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12984 isl_multi_aff_copy(pma->p[i].maff));
12985 if (type == isl_dim_in)
12986 res_i = isl_map_intersect_domain(res_i,
12987 isl_map_copy(pma->p[i].set));
12988 else
12989 res_i = isl_map_intersect_range(res_i,
12990 isl_map_copy(pma->p[i].set));
12991 res = isl_map_union(res, res_i);
12994 isl_pw_multi_aff_free(pma);
12995 isl_map_free(map);
12996 return res;
12997 error:
12998 isl_pw_multi_aff_free(pma);
12999 isl_map_free(map);
13000 return NULL;
13003 /* Compute the preimage of "map" under the function represented by "pma".
13004 * In other words, plug in "pma" in the domain or range of "map".
13005 * The result is a map that lives in the same space as "map",
13006 * except that the space of type "type" has been replaced by
13007 * the domain space of "pma".
13009 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13010 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13012 if (!map || !pma)
13013 goto error;
13015 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13016 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13018 if (!isl_space_has_named_params(map->dim) ||
13019 !isl_space_has_named_params(pma->dim))
13020 isl_die(map->ctx, isl_error_invalid,
13021 "unaligned unnamed parameters", goto error);
13022 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13023 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13025 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13026 error:
13027 isl_pw_multi_aff_free(pma);
13028 return isl_map_free(map);
13031 /* Compute the preimage of "set" under the function represented by "pma".
13032 * In other words, plug in "pma" in "set". The result is a set
13033 * that lives in the domain space of "pma".
13035 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13036 __isl_take isl_pw_multi_aff *pma)
13038 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13041 /* Compute the preimage of the domain of "map" under the function
13042 * represented by "pma".
13043 * In other words, plug in "pma" in the domain of "map".
13044 * The result is a map that lives in the same space as "map",
13045 * except that domain space has been replaced by the domain space of "pma".
13047 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13048 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13050 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13053 /* Compute the preimage of the range of "map" under the function
13054 * represented by "pma".
13055 * In other words, plug in "pma" in the range of "map".
13056 * The result is a map that lives in the same space as "map",
13057 * except that range space has been replaced by the domain space of "pma".
13059 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13060 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13062 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13065 /* Compute the preimage of "map" under the function represented by "mpa".
13066 * In other words, plug in "mpa" in the domain or range of "map".
13067 * The result is a map that lives in the same space as "map",
13068 * except that the space of type "type" has been replaced by
13069 * the domain space of "mpa".
13071 * If the map does not involve any constraints that refer to the
13072 * dimensions of the substituted space, then the only possible
13073 * effect of "mpa" on the map is to map the space to a different space.
13074 * We create a separate isl_multi_aff to effectuate this change
13075 * in order to avoid spurious splitting of the map along the pieces
13076 * of "mpa".
13078 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13079 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13081 int n;
13082 isl_pw_multi_aff *pma;
13084 if (!map || !mpa)
13085 goto error;
13087 n = isl_map_dim(map, type);
13088 if (!isl_map_involves_dims(map, type, 0, n)) {
13089 isl_space *space;
13090 isl_multi_aff *ma;
13092 space = isl_multi_pw_aff_get_space(mpa);
13093 isl_multi_pw_aff_free(mpa);
13094 ma = isl_multi_aff_zero(space);
13095 return isl_map_preimage_multi_aff(map, type, ma);
13098 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13099 return isl_map_preimage_pw_multi_aff(map, type, pma);
13100 error:
13101 isl_map_free(map);
13102 isl_multi_pw_aff_free(mpa);
13103 return NULL;
13106 /* Compute the preimage of "map" under the function represented by "mpa".
13107 * In other words, plug in "mpa" in the domain "map".
13108 * The result is a map that lives in the same space as "map",
13109 * except that domain space has been replaced by the domain space of "mpa".
13111 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13112 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13114 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13117 /* Compute the preimage of "set" by the function represented by "mpa".
13118 * In other words, plug in "mpa" in "set".
13120 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13121 __isl_take isl_multi_pw_aff *mpa)
13123 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13126 /* Are the "n" "coefficients" starting at "first" of the integer division
13127 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13128 * to each other?
13129 * The "coefficient" at position 0 is the denominator.
13130 * The "coefficient" at position 1 is the constant term.
13132 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13133 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13134 unsigned first, unsigned n)
13136 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13137 return isl_bool_error;
13138 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13139 return isl_bool_error;
13140 return isl_seq_eq(bmap1->div[pos1] + first,
13141 bmap2->div[pos2] + first, n);
13144 /* Are the integer division expressions at position "pos1" in "bmap1" and
13145 * "pos2" in "bmap2" equal to each other, except that the constant terms
13146 * are different?
13148 isl_bool isl_basic_map_equal_div_expr_except_constant(
13149 __isl_keep isl_basic_map *bmap1, int pos1,
13150 __isl_keep isl_basic_map *bmap2, int pos2)
13152 isl_bool equal;
13153 unsigned total;
13155 if (!bmap1 || !bmap2)
13156 return isl_bool_error;
13157 total = isl_basic_map_total_dim(bmap1);
13158 if (total != isl_basic_map_total_dim(bmap2))
13159 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13160 "incomparable div expressions", return isl_bool_error);
13161 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13162 0, 1);
13163 if (equal < 0 || !equal)
13164 return equal;
13165 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13166 1, 1);
13167 if (equal < 0 || equal)
13168 return isl_bool_not(equal);
13169 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13170 2, total);
13173 /* Replace the numerator of the constant term of the integer division
13174 * expression at position "div" in "bmap" by "value".
13175 * The caller guarantees that this does not change the meaning
13176 * of the input.
13178 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13179 __isl_take isl_basic_map *bmap, int div, int value)
13181 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13182 return isl_basic_map_free(bmap);
13184 isl_int_set_si(bmap->div[div][1], value);
13186 return bmap;
13189 /* Is the point "inner" internal to inequality constraint "ineq"
13190 * of "bset"?
13191 * The point is considered to be internal to the inequality constraint,
13192 * if it strictly lies on the positive side of the inequality constraint,
13193 * or if it lies on the constraint and the constraint is lexico-positive.
13195 static isl_bool is_internal(__isl_keep isl_vec *inner,
13196 __isl_keep isl_basic_set *bset, int ineq)
13198 isl_ctx *ctx;
13199 int pos;
13200 unsigned total;
13202 if (!inner || !bset)
13203 return isl_bool_error;
13205 ctx = isl_basic_set_get_ctx(bset);
13206 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13207 &ctx->normalize_gcd);
13208 if (!isl_int_is_zero(ctx->normalize_gcd))
13209 return isl_int_is_nonneg(ctx->normalize_gcd);
13211 total = isl_basic_set_dim(bset, isl_dim_all);
13212 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13213 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13216 /* Tighten the inequality constraints of "bset" that are outward with respect
13217 * to the point "vec".
13218 * That is, tighten the constraints that are not satisfied by "vec".
13220 * "vec" is a point internal to some superset S of "bset" that is used
13221 * to make the subsets of S disjoint, by tightening one half of the constraints
13222 * that separate two subsets. In particular, the constraints of S
13223 * are all satisfied by "vec" and should not be tightened.
13224 * Of the internal constraints, those that have "vec" on the outside
13225 * are tightened. The shared facet is included in the adjacent subset
13226 * with the opposite constraint.
13227 * For constraints that saturate "vec", this criterion cannot be used
13228 * to determine which of the two sides should be tightened.
13229 * Instead, the sign of the first non-zero coefficient is used
13230 * to make this choice. Note that this second criterion is never used
13231 * on the constraints of S since "vec" is interior to "S".
13233 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13234 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13236 int j;
13238 bset = isl_basic_set_cow(bset);
13239 if (!bset)
13240 return NULL;
13241 for (j = 0; j < bset->n_ineq; ++j) {
13242 isl_bool internal;
13244 internal = is_internal(vec, bset, j);
13245 if (internal < 0)
13246 return isl_basic_set_free(bset);
13247 if (internal)
13248 continue;
13249 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13252 return bset;