drop isl_set_alloc
[isl.git] / isl_map.c
blob7af81af261952710965a7fbdba0733190202ae77
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 /* Does local variable "div" of "bset" have a complete explicit representation?
2652 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2654 return isl_basic_map_div_is_known(bset, div);
2657 /* Remove all divs that are unknown or defined in terms of unknown divs.
2659 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2660 __isl_take isl_basic_map *bmap)
2662 int i;
2664 if (!bmap)
2665 return NULL;
2667 for (i = bmap->n_div - 1; i >= 0; --i) {
2668 if (isl_basic_map_div_is_known(bmap, i))
2669 continue;
2670 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2671 if (!bmap)
2672 return NULL;
2673 i = bmap->n_div;
2676 return bmap;
2679 /* Remove all divs that are unknown or defined in terms of unknown divs.
2681 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2682 __isl_take isl_basic_set *bset)
2684 return isl_basic_map_remove_unknown_divs(bset);
2687 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2689 int i;
2691 if (!map)
2692 return NULL;
2693 if (map->n == 0)
2694 return map;
2696 map = isl_map_cow(map);
2697 if (!map)
2698 return NULL;
2700 for (i = 0; i < map->n; ++i) {
2701 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2702 if (!map->p[i])
2703 goto error;
2705 return map;
2706 error:
2707 isl_map_free(map);
2708 return NULL;
2711 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2713 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2716 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2717 __isl_take isl_basic_set *bset,
2718 enum isl_dim_type type, unsigned first, unsigned n)
2720 isl_basic_map *bmap = bset_to_bmap(bset);
2721 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2722 return bset_from_bmap(bmap);
2725 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2726 enum isl_dim_type type, unsigned first, unsigned n)
2728 int i;
2730 if (n == 0)
2731 return map;
2733 map = isl_map_cow(map);
2734 if (!map)
2735 return NULL;
2736 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2738 for (i = 0; i < map->n; ++i) {
2739 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2740 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2741 if (!map->p[i])
2742 goto error;
2744 map = isl_map_drop(map, type, first, n);
2745 return map;
2746 error:
2747 isl_map_free(map);
2748 return NULL;
2751 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2752 enum isl_dim_type type, unsigned first, unsigned n)
2754 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2755 type, first, n));
2758 /* Project out n inputs starting at first using Fourier-Motzkin */
2759 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2760 unsigned first, unsigned n)
2762 return isl_map_remove_dims(map, isl_dim_in, first, n);
2765 static void dump_term(struct isl_basic_map *bmap,
2766 isl_int c, int pos, FILE *out)
2768 const char *name;
2769 unsigned in = isl_basic_map_n_in(bmap);
2770 unsigned dim = in + isl_basic_map_n_out(bmap);
2771 unsigned nparam = isl_basic_map_n_param(bmap);
2772 if (!pos)
2773 isl_int_print(out, c, 0);
2774 else {
2775 if (!isl_int_is_one(c))
2776 isl_int_print(out, c, 0);
2777 if (pos < 1 + nparam) {
2778 name = isl_space_get_dim_name(bmap->dim,
2779 isl_dim_param, pos - 1);
2780 if (name)
2781 fprintf(out, "%s", name);
2782 else
2783 fprintf(out, "p%d", pos - 1);
2784 } else if (pos < 1 + nparam + in)
2785 fprintf(out, "i%d", pos - 1 - nparam);
2786 else if (pos < 1 + nparam + dim)
2787 fprintf(out, "o%d", pos - 1 - nparam - in);
2788 else
2789 fprintf(out, "e%d", pos - 1 - nparam - dim);
2793 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2794 int sign, FILE *out)
2796 int i;
2797 int first;
2798 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2799 isl_int v;
2801 isl_int_init(v);
2802 for (i = 0, first = 1; i < len; ++i) {
2803 if (isl_int_sgn(c[i]) * sign <= 0)
2804 continue;
2805 if (!first)
2806 fprintf(out, " + ");
2807 first = 0;
2808 isl_int_abs(v, c[i]);
2809 dump_term(bmap, v, i, out);
2811 isl_int_clear(v);
2812 if (first)
2813 fprintf(out, "0");
2816 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2817 const char *op, FILE *out, int indent)
2819 int i;
2821 fprintf(out, "%*s", indent, "");
2823 dump_constraint_sign(bmap, c, 1, out);
2824 fprintf(out, " %s ", op);
2825 dump_constraint_sign(bmap, c, -1, out);
2827 fprintf(out, "\n");
2829 for (i = bmap->n_div; i < bmap->extra; ++i) {
2830 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2831 continue;
2832 fprintf(out, "%*s", indent, "");
2833 fprintf(out, "ERROR: unused div coefficient not zero\n");
2834 abort();
2838 static void dump_constraints(struct isl_basic_map *bmap,
2839 isl_int **c, unsigned n,
2840 const char *op, FILE *out, int indent)
2842 int i;
2844 for (i = 0; i < n; ++i)
2845 dump_constraint(bmap, c[i], op, out, indent);
2848 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2850 int j;
2851 int first = 1;
2852 unsigned total = isl_basic_map_total_dim(bmap);
2854 for (j = 0; j < 1 + total; ++j) {
2855 if (isl_int_is_zero(exp[j]))
2856 continue;
2857 if (!first && isl_int_is_pos(exp[j]))
2858 fprintf(out, "+");
2859 dump_term(bmap, exp[j], j, out);
2860 first = 0;
2864 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2866 int i;
2868 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2869 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2871 for (i = 0; i < bmap->n_div; ++i) {
2872 fprintf(out, "%*s", indent, "");
2873 fprintf(out, "e%d = [(", i);
2874 dump_affine(bmap, bmap->div[i]+1, out);
2875 fprintf(out, ")/");
2876 isl_int_print(out, bmap->div[i][0], 0);
2877 fprintf(out, "]\n");
2881 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2882 FILE *out, int indent)
2884 if (!bset) {
2885 fprintf(out, "null basic set\n");
2886 return;
2889 fprintf(out, "%*s", indent, "");
2890 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2891 bset->ref, bset->dim->nparam, bset->dim->n_out,
2892 bset->extra, bset->flags);
2893 dump(bset_to_bmap(bset), out, indent);
2896 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2897 FILE *out, int indent)
2899 if (!bmap) {
2900 fprintf(out, "null basic map\n");
2901 return;
2904 fprintf(out, "%*s", indent, "");
2905 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2906 "flags: %x, n_name: %d\n",
2907 bmap->ref,
2908 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2909 bmap->extra, bmap->flags, bmap->dim->n_id);
2910 dump(bmap, out, indent);
2913 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2915 unsigned total;
2916 if (!bmap)
2917 return -1;
2918 total = isl_basic_map_total_dim(bmap);
2919 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2920 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2921 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2922 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2923 return 0;
2926 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2927 unsigned flags)
2929 if (!space)
2930 return NULL;
2931 if (isl_space_dim(space, isl_dim_in) != 0)
2932 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2933 "set cannot have input dimensions", goto error);
2934 return isl_map_alloc_space(space, n, flags);
2935 error:
2936 isl_space_free(space);
2937 return NULL;
2940 /* Make sure "map" has room for at least "n" more basic maps.
2942 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2944 int i;
2945 struct isl_map *grown = NULL;
2947 if (!map)
2948 return NULL;
2949 isl_assert(map->ctx, n >= 0, goto error);
2950 if (map->n + n <= map->size)
2951 return map;
2952 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2953 if (!grown)
2954 goto error;
2955 for (i = 0; i < map->n; ++i) {
2956 grown->p[i] = isl_basic_map_copy(map->p[i]);
2957 if (!grown->p[i])
2958 goto error;
2959 grown->n++;
2961 isl_map_free(map);
2962 return grown;
2963 error:
2964 isl_map_free(grown);
2965 isl_map_free(map);
2966 return NULL;
2969 /* Make sure "set" has room for at least "n" more basic sets.
2971 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2973 return set_from_map(isl_map_grow(set_to_map(set), n));
2976 struct isl_set *isl_set_dup(struct isl_set *set)
2978 int i;
2979 struct isl_set *dup;
2981 if (!set)
2982 return NULL;
2984 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2985 if (!dup)
2986 return NULL;
2987 for (i = 0; i < set->n; ++i)
2988 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2989 return dup;
2992 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2994 return isl_map_from_basic_map(bset);
2997 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2999 struct isl_map *map;
3001 if (!bmap)
3002 return NULL;
3004 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3005 return isl_map_add_basic_map(map, bmap);
3008 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3009 __isl_take isl_basic_set *bset)
3011 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3012 bset_to_bmap(bset)));
3015 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3017 return isl_map_free(set);
3020 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3022 int i;
3024 if (!set) {
3025 fprintf(out, "null set\n");
3026 return;
3029 fprintf(out, "%*s", indent, "");
3030 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3031 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3032 set->flags);
3033 for (i = 0; i < set->n; ++i) {
3034 fprintf(out, "%*s", indent, "");
3035 fprintf(out, "basic set %d:\n", i);
3036 isl_basic_set_print_internal(set->p[i], out, indent+4);
3040 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3042 int i;
3044 if (!map) {
3045 fprintf(out, "null map\n");
3046 return;
3049 fprintf(out, "%*s", indent, "");
3050 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3051 "flags: %x, n_name: %d\n",
3052 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3053 map->dim->n_out, map->flags, map->dim->n_id);
3054 for (i = 0; i < map->n; ++i) {
3055 fprintf(out, "%*s", indent, "");
3056 fprintf(out, "basic map %d:\n", i);
3057 isl_basic_map_print_internal(map->p[i], out, indent+4);
3061 struct isl_basic_map *isl_basic_map_intersect_domain(
3062 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3064 struct isl_basic_map *bmap_domain;
3066 if (!bmap || !bset)
3067 goto error;
3069 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3070 bset->dim, isl_dim_param), goto error);
3072 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3073 isl_assert(bset->ctx,
3074 isl_basic_map_compatible_domain(bmap, bset), goto error);
3076 bmap = isl_basic_map_cow(bmap);
3077 if (!bmap)
3078 goto error;
3079 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3080 bset->n_div, bset->n_eq, bset->n_ineq);
3081 bmap_domain = isl_basic_map_from_domain(bset);
3082 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3084 bmap = isl_basic_map_simplify(bmap);
3085 return isl_basic_map_finalize(bmap);
3086 error:
3087 isl_basic_map_free(bmap);
3088 isl_basic_set_free(bset);
3089 return NULL;
3092 /* Check that the space of "bset" is the same as that of the range of "bmap".
3094 static isl_stat isl_basic_map_check_compatible_range(
3095 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3097 isl_bool ok;
3099 ok = isl_basic_map_compatible_range(bmap, bset);
3100 if (ok < 0)
3101 return isl_stat_error;
3102 if (!ok)
3103 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3104 "incompatible spaces", return isl_stat_error);
3106 return isl_stat_ok;
3109 struct isl_basic_map *isl_basic_map_intersect_range(
3110 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3112 struct isl_basic_map *bmap_range;
3114 if (!bmap || !bset)
3115 goto error;
3117 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3118 bset->dim, isl_dim_param), goto error);
3120 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3121 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3122 goto error;
3124 if (isl_basic_set_plain_is_universe(bset)) {
3125 isl_basic_set_free(bset);
3126 return bmap;
3129 bmap = isl_basic_map_cow(bmap);
3130 if (!bmap)
3131 goto error;
3132 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3133 bset->n_div, bset->n_eq, bset->n_ineq);
3134 bmap_range = bset_to_bmap(bset);
3135 bmap = add_constraints(bmap, bmap_range, 0, 0);
3137 bmap = isl_basic_map_simplify(bmap);
3138 return isl_basic_map_finalize(bmap);
3139 error:
3140 isl_basic_map_free(bmap);
3141 isl_basic_set_free(bset);
3142 return NULL;
3145 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3146 __isl_keep isl_vec *vec)
3148 int i;
3149 unsigned total;
3150 isl_int s;
3152 if (!bmap || !vec)
3153 return isl_bool_error;
3155 total = 1 + isl_basic_map_total_dim(bmap);
3156 if (total != vec->size)
3157 return isl_bool_false;
3159 isl_int_init(s);
3161 for (i = 0; i < bmap->n_eq; ++i) {
3162 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3163 if (!isl_int_is_zero(s)) {
3164 isl_int_clear(s);
3165 return isl_bool_false;
3169 for (i = 0; i < bmap->n_ineq; ++i) {
3170 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3171 if (isl_int_is_neg(s)) {
3172 isl_int_clear(s);
3173 return isl_bool_false;
3177 isl_int_clear(s);
3179 return isl_bool_true;
3182 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3183 __isl_keep isl_vec *vec)
3185 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3188 struct isl_basic_map *isl_basic_map_intersect(
3189 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3191 struct isl_vec *sample = NULL;
3193 if (!bmap1 || !bmap2)
3194 goto error;
3196 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
3197 bmap2->dim, isl_dim_param), goto error);
3198 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3199 isl_space_dim(bmap1->dim, isl_dim_param) &&
3200 isl_space_dim(bmap2->dim, isl_dim_all) !=
3201 isl_space_dim(bmap2->dim, isl_dim_param))
3202 return isl_basic_map_intersect(bmap2, bmap1);
3204 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3205 isl_space_dim(bmap2->dim, isl_dim_param))
3206 isl_assert(bmap1->ctx,
3207 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3209 if (isl_basic_map_plain_is_empty(bmap1)) {
3210 isl_basic_map_free(bmap2);
3211 return bmap1;
3213 if (isl_basic_map_plain_is_empty(bmap2)) {
3214 isl_basic_map_free(bmap1);
3215 return bmap2;
3218 if (bmap1->sample &&
3219 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3220 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3221 sample = isl_vec_copy(bmap1->sample);
3222 else if (bmap2->sample &&
3223 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3224 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3225 sample = isl_vec_copy(bmap2->sample);
3227 bmap1 = isl_basic_map_cow(bmap1);
3228 if (!bmap1)
3229 goto error;
3230 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3231 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3232 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3234 if (!bmap1)
3235 isl_vec_free(sample);
3236 else if (sample) {
3237 isl_vec_free(bmap1->sample);
3238 bmap1->sample = sample;
3241 bmap1 = isl_basic_map_simplify(bmap1);
3242 return isl_basic_map_finalize(bmap1);
3243 error:
3244 if (sample)
3245 isl_vec_free(sample);
3246 isl_basic_map_free(bmap1);
3247 isl_basic_map_free(bmap2);
3248 return NULL;
3251 struct isl_basic_set *isl_basic_set_intersect(
3252 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3254 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3255 bset_to_bmap(bset2)));
3258 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3259 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3261 return isl_basic_set_intersect(bset1, bset2);
3264 /* Special case of isl_map_intersect, where both map1 and map2
3265 * are convex, without any divs and such that either map1 or map2
3266 * contains a single constraint. This constraint is then simply
3267 * added to the other map.
3269 static __isl_give isl_map *map_intersect_add_constraint(
3270 __isl_take isl_map *map1, __isl_take isl_map *map2)
3272 isl_assert(map1->ctx, map1->n == 1, goto error);
3273 isl_assert(map2->ctx, map1->n == 1, goto error);
3274 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3275 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3277 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3278 return isl_map_intersect(map2, map1);
3280 map1 = isl_map_cow(map1);
3281 if (!map1)
3282 goto error;
3283 if (isl_map_plain_is_empty(map1)) {
3284 isl_map_free(map2);
3285 return map1;
3287 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3288 if (map2->p[0]->n_eq == 1)
3289 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3290 else
3291 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3292 map2->p[0]->ineq[0]);
3294 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3295 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3296 if (!map1->p[0])
3297 goto error;
3299 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3300 isl_basic_map_free(map1->p[0]);
3301 map1->n = 0;
3304 isl_map_free(map2);
3306 return map1;
3307 error:
3308 isl_map_free(map1);
3309 isl_map_free(map2);
3310 return NULL;
3313 /* map2 may be either a parameter domain or a map living in the same
3314 * space as map1.
3316 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3317 __isl_take isl_map *map2)
3319 unsigned flags = 0;
3320 isl_map *result;
3321 int i, j;
3323 if (!map1 || !map2)
3324 goto error;
3326 if ((isl_map_plain_is_empty(map1) ||
3327 isl_map_plain_is_universe(map2)) &&
3328 isl_space_is_equal(map1->dim, map2->dim)) {
3329 isl_map_free(map2);
3330 return map1;
3332 if ((isl_map_plain_is_empty(map2) ||
3333 isl_map_plain_is_universe(map1)) &&
3334 isl_space_is_equal(map1->dim, map2->dim)) {
3335 isl_map_free(map1);
3336 return map2;
3339 if (map1->n == 1 && map2->n == 1 &&
3340 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3341 isl_space_is_equal(map1->dim, map2->dim) &&
3342 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3343 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3344 return map_intersect_add_constraint(map1, map2);
3346 if (isl_space_dim(map2->dim, isl_dim_all) !=
3347 isl_space_dim(map2->dim, isl_dim_param))
3348 isl_assert(map1->ctx,
3349 isl_space_is_equal(map1->dim, map2->dim), goto error);
3351 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3352 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3353 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3355 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3356 map1->n * map2->n, flags);
3357 if (!result)
3358 goto error;
3359 for (i = 0; i < map1->n; ++i)
3360 for (j = 0; j < map2->n; ++j) {
3361 struct isl_basic_map *part;
3362 part = isl_basic_map_intersect(
3363 isl_basic_map_copy(map1->p[i]),
3364 isl_basic_map_copy(map2->p[j]));
3365 if (isl_basic_map_is_empty(part) < 0)
3366 part = isl_basic_map_free(part);
3367 result = isl_map_add_basic_map(result, part);
3368 if (!result)
3369 goto error;
3371 isl_map_free(map1);
3372 isl_map_free(map2);
3373 return result;
3374 error:
3375 isl_map_free(map1);
3376 isl_map_free(map2);
3377 return NULL;
3380 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3381 __isl_take isl_map *map2)
3383 if (!map1 || !map2)
3384 goto error;
3385 if (!isl_space_is_equal(map1->dim, map2->dim))
3386 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3387 "spaces don't match", goto error);
3388 return map_intersect_internal(map1, map2);
3389 error:
3390 isl_map_free(map1);
3391 isl_map_free(map2);
3392 return NULL;
3395 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3396 __isl_take isl_map *map2)
3398 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3401 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3403 return set_from_map(isl_map_intersect(set_to_map(set1),
3404 set_to_map(set2)));
3407 /* map_intersect_internal accepts intersections
3408 * with parameter domains, so we can just call that function.
3410 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3411 __isl_take isl_set *params)
3413 return map_intersect_internal(map, params);
3416 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3417 __isl_take isl_map *map2)
3419 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3422 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3423 __isl_take isl_set *params)
3425 return isl_map_intersect_params(set, params);
3428 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3430 isl_space *space;
3431 unsigned pos, n1, n2;
3433 if (!bmap)
3434 return NULL;
3435 bmap = isl_basic_map_cow(bmap);
3436 if (!bmap)
3437 return NULL;
3438 space = isl_space_reverse(isl_space_copy(bmap->dim));
3439 pos = isl_basic_map_offset(bmap, isl_dim_in);
3440 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3441 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3442 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3443 return isl_basic_map_reset_space(bmap, space);
3446 static __isl_give isl_basic_map *basic_map_space_reset(
3447 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3449 isl_space *space;
3451 if (!bmap)
3452 return NULL;
3453 if (!isl_space_is_named_or_nested(bmap->dim, type))
3454 return bmap;
3456 space = isl_basic_map_get_space(bmap);
3457 space = isl_space_reset(space, type);
3458 bmap = isl_basic_map_reset_space(bmap, space);
3459 return bmap;
3462 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3463 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3464 unsigned pos, unsigned n)
3466 isl_bool rational;
3467 isl_space *res_dim;
3468 struct isl_basic_map *res;
3469 struct isl_dim_map *dim_map;
3470 unsigned total, off;
3471 enum isl_dim_type t;
3473 if (n == 0)
3474 return basic_map_space_reset(bmap, type);
3476 if (!bmap)
3477 return NULL;
3479 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3481 total = isl_basic_map_total_dim(bmap) + n;
3482 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3483 off = 0;
3484 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3485 if (t != type) {
3486 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3487 } else {
3488 unsigned size = isl_basic_map_dim(bmap, t);
3489 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3490 0, pos, off);
3491 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3492 pos, size - pos, off + pos + n);
3494 off += isl_space_dim(res_dim, t);
3496 isl_dim_map_div(dim_map, bmap, off);
3498 res = isl_basic_map_alloc_space(res_dim,
3499 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3500 rational = isl_basic_map_is_rational(bmap);
3501 if (rational < 0)
3502 res = isl_basic_map_free(res);
3503 if (rational)
3504 res = isl_basic_map_set_rational(res);
3505 if (isl_basic_map_plain_is_empty(bmap)) {
3506 isl_basic_map_free(bmap);
3507 free(dim_map);
3508 return isl_basic_map_set_to_empty(res);
3510 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3511 return isl_basic_map_finalize(res);
3514 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3515 __isl_take isl_basic_set *bset,
3516 enum isl_dim_type type, unsigned pos, unsigned n)
3518 return isl_basic_map_insert_dims(bset, type, pos, n);
3521 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3522 enum isl_dim_type type, unsigned n)
3524 if (!bmap)
3525 return NULL;
3526 return isl_basic_map_insert_dims(bmap, type,
3527 isl_basic_map_dim(bmap, type), n);
3530 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3531 enum isl_dim_type type, unsigned n)
3533 if (!bset)
3534 return NULL;
3535 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3536 return isl_basic_map_add_dims(bset, type, n);
3537 error:
3538 isl_basic_set_free(bset);
3539 return NULL;
3542 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3543 enum isl_dim_type type)
3545 isl_space *space;
3547 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3548 return map;
3550 space = isl_map_get_space(map);
3551 space = isl_space_reset(space, type);
3552 map = isl_map_reset_space(map, space);
3553 return map;
3556 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3557 enum isl_dim_type type, unsigned pos, unsigned n)
3559 int i;
3561 if (n == 0)
3562 return map_space_reset(map, type);
3564 map = isl_map_cow(map);
3565 if (!map)
3566 return NULL;
3568 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3569 if (!map->dim)
3570 goto error;
3572 for (i = 0; i < map->n; ++i) {
3573 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3574 if (!map->p[i])
3575 goto error;
3578 return map;
3579 error:
3580 isl_map_free(map);
3581 return NULL;
3584 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3585 enum isl_dim_type type, unsigned pos, unsigned n)
3587 return isl_map_insert_dims(set, type, pos, n);
3590 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3591 enum isl_dim_type type, unsigned n)
3593 if (!map)
3594 return NULL;
3595 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3598 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3599 enum isl_dim_type type, unsigned n)
3601 if (!set)
3602 return NULL;
3603 isl_assert(set->ctx, type != isl_dim_in, goto error);
3604 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3605 error:
3606 isl_set_free(set);
3607 return NULL;
3610 __isl_give isl_basic_map *isl_basic_map_move_dims(
3611 __isl_take isl_basic_map *bmap,
3612 enum isl_dim_type dst_type, unsigned dst_pos,
3613 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3615 struct isl_dim_map *dim_map;
3616 struct isl_basic_map *res;
3617 enum isl_dim_type t;
3618 unsigned total, off;
3620 if (!bmap)
3621 return NULL;
3622 if (n == 0)
3623 return bmap;
3625 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3626 return isl_basic_map_free(bmap);
3628 if (dst_type == src_type && dst_pos == src_pos)
3629 return bmap;
3631 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3633 if (pos(bmap->dim, dst_type) + dst_pos ==
3634 pos(bmap->dim, src_type) + src_pos +
3635 ((src_type < dst_type) ? n : 0)) {
3636 bmap = isl_basic_map_cow(bmap);
3637 if (!bmap)
3638 return NULL;
3640 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3641 src_type, src_pos, n);
3642 if (!bmap->dim)
3643 goto error;
3645 bmap = isl_basic_map_finalize(bmap);
3647 return bmap;
3650 total = isl_basic_map_total_dim(bmap);
3651 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3653 off = 0;
3654 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3655 unsigned size = isl_space_dim(bmap->dim, t);
3656 if (t == dst_type) {
3657 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3658 0, dst_pos, off);
3659 off += dst_pos;
3660 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3661 src_pos, n, off);
3662 off += n;
3663 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3664 dst_pos, size - dst_pos, off);
3665 off += size - dst_pos;
3666 } else if (t == src_type) {
3667 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3668 0, src_pos, off);
3669 off += src_pos;
3670 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3671 src_pos + n, size - src_pos - n, off);
3672 off += size - src_pos - n;
3673 } else {
3674 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3675 off += size;
3678 isl_dim_map_div(dim_map, bmap, off);
3680 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3681 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3682 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3683 if (!bmap)
3684 goto error;
3686 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3687 src_type, src_pos, n);
3688 if (!bmap->dim)
3689 goto error;
3691 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3692 bmap = isl_basic_map_gauss(bmap, NULL);
3693 bmap = isl_basic_map_finalize(bmap);
3695 return bmap;
3696 error:
3697 isl_basic_map_free(bmap);
3698 return NULL;
3701 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3702 enum isl_dim_type dst_type, unsigned dst_pos,
3703 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3705 isl_basic_map *bmap = bset_to_bmap(bset);
3706 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3707 src_type, src_pos, n);
3708 return bset_from_bmap(bmap);
3711 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3712 enum isl_dim_type dst_type, unsigned dst_pos,
3713 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3715 if (!set)
3716 return NULL;
3717 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3718 return set_from_map(isl_map_move_dims(set_to_map(set),
3719 dst_type, dst_pos, src_type, src_pos, n));
3720 error:
3721 isl_set_free(set);
3722 return NULL;
3725 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3726 enum isl_dim_type dst_type, unsigned dst_pos,
3727 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3729 int i;
3731 if (!map)
3732 return NULL;
3733 if (n == 0)
3734 return map;
3736 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3737 goto error);
3739 if (dst_type == src_type && dst_pos == src_pos)
3740 return map;
3742 isl_assert(map->ctx, dst_type != src_type, goto error);
3744 map = isl_map_cow(map);
3745 if (!map)
3746 return NULL;
3748 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3749 if (!map->dim)
3750 goto error;
3752 for (i = 0; i < map->n; ++i) {
3753 map->p[i] = isl_basic_map_move_dims(map->p[i],
3754 dst_type, dst_pos,
3755 src_type, src_pos, n);
3756 if (!map->p[i])
3757 goto error;
3760 return map;
3761 error:
3762 isl_map_free(map);
3763 return NULL;
3766 /* Move the specified dimensions to the last columns right before
3767 * the divs. Don't change the dimension specification of bmap.
3768 * That's the responsibility of the caller.
3770 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3771 enum isl_dim_type type, unsigned first, unsigned n)
3773 struct isl_dim_map *dim_map;
3774 struct isl_basic_map *res;
3775 enum isl_dim_type t;
3776 unsigned total, off;
3778 if (!bmap)
3779 return NULL;
3780 if (pos(bmap->dim, type) + first + n ==
3781 1 + isl_space_dim(bmap->dim, isl_dim_all))
3782 return bmap;
3784 total = isl_basic_map_total_dim(bmap);
3785 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3787 off = 0;
3788 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3789 unsigned size = isl_space_dim(bmap->dim, t);
3790 if (t == type) {
3791 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3792 0, first, off);
3793 off += first;
3794 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3795 first, n, total - bmap->n_div - n);
3796 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3797 first + n, size - (first + n), off);
3798 off += size - (first + n);
3799 } else {
3800 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3801 off += size;
3804 isl_dim_map_div(dim_map, bmap, off + n);
3806 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3807 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3808 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3809 return res;
3812 /* Insert "n" rows in the divs of "bmap".
3814 * The number of columns is not changed, which means that the last
3815 * dimensions of "bmap" are being reintepreted as the new divs.
3816 * The space of "bmap" is not adjusted, however, which means
3817 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3818 * from the space of "bmap" is the responsibility of the caller.
3820 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3821 int n)
3823 int i;
3824 size_t row_size;
3825 isl_int **new_div;
3826 isl_int *old;
3828 bmap = isl_basic_map_cow(bmap);
3829 if (!bmap)
3830 return NULL;
3832 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3833 old = bmap->block2.data;
3834 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3835 (bmap->extra + n) * (1 + row_size));
3836 if (!bmap->block2.data)
3837 return isl_basic_map_free(bmap);
3838 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3839 if (!new_div)
3840 return isl_basic_map_free(bmap);
3841 for (i = 0; i < n; ++i) {
3842 new_div[i] = bmap->block2.data +
3843 (bmap->extra + i) * (1 + row_size);
3844 isl_seq_clr(new_div[i], 1 + row_size);
3846 for (i = 0; i < bmap->extra; ++i)
3847 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3848 free(bmap->div);
3849 bmap->div = new_div;
3850 bmap->n_div += n;
3851 bmap->extra += n;
3853 return bmap;
3856 /* Drop constraints from "bmap" that only involve the variables
3857 * of "type" in the range [first, first + n] that are not related
3858 * to any of the variables outside that interval.
3859 * These constraints cannot influence the values for the variables
3860 * outside the interval, except in case they cause "bmap" to be empty.
3861 * Only drop the constraints if "bmap" is known to be non-empty.
3863 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3864 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3865 unsigned first, unsigned n)
3867 int i;
3868 int *groups;
3869 unsigned dim, n_div;
3870 isl_bool non_empty;
3872 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3873 if (non_empty < 0)
3874 return isl_basic_map_free(bmap);
3875 if (!non_empty)
3876 return bmap;
3878 dim = isl_basic_map_dim(bmap, isl_dim_all);
3879 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3880 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3881 if (!groups)
3882 return isl_basic_map_free(bmap);
3883 first += isl_basic_map_offset(bmap, type) - 1;
3884 for (i = 0; i < first; ++i)
3885 groups[i] = -1;
3886 for (i = first + n; i < dim - n_div; ++i)
3887 groups[i] = -1;
3889 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3891 return bmap;
3894 /* Turn the n dimensions of type type, starting at first
3895 * into existentially quantified variables.
3897 * If a subset of the projected out variables are unrelated
3898 * to any of the variables that remain, then the constraints
3899 * involving this subset are simply dropped first.
3901 __isl_give isl_basic_map *isl_basic_map_project_out(
3902 __isl_take isl_basic_map *bmap,
3903 enum isl_dim_type type, unsigned first, unsigned n)
3905 if (n == 0)
3906 return basic_map_space_reset(bmap, type);
3907 if (type == isl_dim_div)
3908 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3909 "cannot project out existentially quantified variables",
3910 return isl_basic_map_free(bmap));
3912 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3913 if (!bmap)
3914 return NULL;
3916 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3917 return isl_basic_map_remove_dims(bmap, type, first, n);
3919 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3920 return isl_basic_map_free(bmap);
3922 bmap = move_last(bmap, type, first, n);
3923 bmap = isl_basic_map_cow(bmap);
3924 bmap = insert_div_rows(bmap, n);
3925 if (!bmap)
3926 return NULL;
3928 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3929 if (!bmap->dim)
3930 goto error;
3931 bmap = isl_basic_map_simplify(bmap);
3932 bmap = isl_basic_map_drop_redundant_divs(bmap);
3933 return isl_basic_map_finalize(bmap);
3934 error:
3935 isl_basic_map_free(bmap);
3936 return NULL;
3939 /* Turn the n dimensions of type type, starting at first
3940 * into existentially quantified variables.
3942 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3943 enum isl_dim_type type, unsigned first, unsigned n)
3945 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3946 type, first, n));
3949 /* Turn the n dimensions of type type, starting at first
3950 * into existentially quantified variables.
3952 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3953 enum isl_dim_type type, unsigned first, unsigned n)
3955 int i;
3957 if (!map)
3958 return NULL;
3960 if (n == 0)
3961 return map_space_reset(map, type);
3963 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3965 map = isl_map_cow(map);
3966 if (!map)
3967 return NULL;
3969 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3970 if (!map->dim)
3971 goto error;
3973 for (i = 0; i < map->n; ++i) {
3974 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3975 if (!map->p[i])
3976 goto error;
3979 return map;
3980 error:
3981 isl_map_free(map);
3982 return NULL;
3985 /* Turn the n dimensions of type type, starting at first
3986 * into existentially quantified variables.
3988 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3989 enum isl_dim_type type, unsigned first, unsigned n)
3991 return set_from_map(isl_map_project_out(set_to_map(set),
3992 type, first, n));
3995 /* Return a map that projects the elements in "set" onto their
3996 * "n" set dimensions starting at "first".
3997 * "type" should be equal to isl_dim_set.
3999 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4000 enum isl_dim_type type, unsigned first, unsigned n)
4002 int i;
4003 int dim;
4004 isl_map *map;
4006 if (!set)
4007 return NULL;
4008 if (type != isl_dim_set)
4009 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4010 "only set dimensions can be projected out", goto error);
4011 dim = isl_set_dim(set, isl_dim_set);
4012 if (first + n > dim || first + n < first)
4013 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4014 "index out of bounds", goto error);
4016 map = isl_map_from_domain(set);
4017 map = isl_map_add_dims(map, isl_dim_out, n);
4018 for (i = 0; i < n; ++i)
4019 map = isl_map_equate(map, isl_dim_in, first + i,
4020 isl_dim_out, i);
4021 return map;
4022 error:
4023 isl_set_free(set);
4024 return NULL;
4027 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4029 int i, j;
4031 for (i = 0; i < n; ++i) {
4032 j = isl_basic_map_alloc_div(bmap);
4033 if (j < 0)
4034 goto error;
4035 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4037 return bmap;
4038 error:
4039 isl_basic_map_free(bmap);
4040 return NULL;
4043 struct isl_basic_map *isl_basic_map_apply_range(
4044 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4046 isl_space *dim_result = NULL;
4047 struct isl_basic_map *bmap;
4048 unsigned n_in, n_out, n, nparam, total, pos;
4049 struct isl_dim_map *dim_map1, *dim_map2;
4051 if (!bmap1 || !bmap2)
4052 goto error;
4053 if (!isl_space_match(bmap1->dim, isl_dim_param,
4054 bmap2->dim, isl_dim_param))
4055 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4056 "parameters don't match", goto error);
4057 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4058 bmap2->dim, isl_dim_in))
4059 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4060 "spaces don't match", goto error);
4062 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4063 isl_space_copy(bmap2->dim));
4065 n_in = isl_basic_map_n_in(bmap1);
4066 n_out = isl_basic_map_n_out(bmap2);
4067 n = isl_basic_map_n_out(bmap1);
4068 nparam = isl_basic_map_n_param(bmap1);
4070 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4071 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4072 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4073 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4074 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4075 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4076 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4077 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4078 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4079 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4080 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4082 bmap = isl_basic_map_alloc_space(dim_result,
4083 bmap1->n_div + bmap2->n_div + n,
4084 bmap1->n_eq + bmap2->n_eq,
4085 bmap1->n_ineq + bmap2->n_ineq);
4086 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4087 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4088 bmap = add_divs(bmap, n);
4089 bmap = isl_basic_map_simplify(bmap);
4090 bmap = isl_basic_map_drop_redundant_divs(bmap);
4091 return isl_basic_map_finalize(bmap);
4092 error:
4093 isl_basic_map_free(bmap1);
4094 isl_basic_map_free(bmap2);
4095 return NULL;
4098 struct isl_basic_set *isl_basic_set_apply(
4099 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4101 if (!bset || !bmap)
4102 goto error;
4104 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4105 goto error);
4107 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4108 bmap));
4109 error:
4110 isl_basic_set_free(bset);
4111 isl_basic_map_free(bmap);
4112 return NULL;
4115 struct isl_basic_map *isl_basic_map_apply_domain(
4116 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4118 if (!bmap1 || !bmap2)
4119 goto error;
4121 if (!isl_space_match(bmap1->dim, isl_dim_param,
4122 bmap2->dim, isl_dim_param))
4123 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4124 "parameters don't match", goto error);
4125 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4126 bmap2->dim, isl_dim_in))
4127 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4128 "spaces don't match", goto error);
4130 bmap1 = isl_basic_map_reverse(bmap1);
4131 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4132 return isl_basic_map_reverse(bmap1);
4133 error:
4134 isl_basic_map_free(bmap1);
4135 isl_basic_map_free(bmap2);
4136 return NULL;
4139 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4140 * A \cap B -> f(A) + f(B)
4142 struct isl_basic_map *isl_basic_map_sum(
4143 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4145 unsigned n_in, n_out, nparam, total, pos;
4146 struct isl_basic_map *bmap = NULL;
4147 struct isl_dim_map *dim_map1, *dim_map2;
4148 int i;
4150 if (!bmap1 || !bmap2)
4151 goto error;
4153 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4154 goto error);
4156 nparam = isl_basic_map_n_param(bmap1);
4157 n_in = isl_basic_map_n_in(bmap1);
4158 n_out = isl_basic_map_n_out(bmap1);
4160 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4161 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4162 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4163 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4164 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4165 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4166 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4167 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4168 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4169 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4170 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4172 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4173 bmap1->n_div + bmap2->n_div + 2 * n_out,
4174 bmap1->n_eq + bmap2->n_eq + n_out,
4175 bmap1->n_ineq + bmap2->n_ineq);
4176 for (i = 0; i < n_out; ++i) {
4177 int j = isl_basic_map_alloc_equality(bmap);
4178 if (j < 0)
4179 goto error;
4180 isl_seq_clr(bmap->eq[j], 1+total);
4181 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4182 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4183 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4185 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4186 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4187 bmap = add_divs(bmap, 2 * n_out);
4189 bmap = isl_basic_map_simplify(bmap);
4190 return isl_basic_map_finalize(bmap);
4191 error:
4192 isl_basic_map_free(bmap);
4193 isl_basic_map_free(bmap1);
4194 isl_basic_map_free(bmap2);
4195 return NULL;
4198 /* Given two maps A -> f(A) and B -> g(B), construct a map
4199 * A \cap B -> f(A) + f(B)
4201 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4203 struct isl_map *result;
4204 int i, j;
4206 if (!map1 || !map2)
4207 goto error;
4209 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4211 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4212 map1->n * map2->n, 0);
4213 if (!result)
4214 goto error;
4215 for (i = 0; i < map1->n; ++i)
4216 for (j = 0; j < map2->n; ++j) {
4217 struct isl_basic_map *part;
4218 part = isl_basic_map_sum(
4219 isl_basic_map_copy(map1->p[i]),
4220 isl_basic_map_copy(map2->p[j]));
4221 if (isl_basic_map_is_empty(part))
4222 isl_basic_map_free(part);
4223 else
4224 result = isl_map_add_basic_map(result, part);
4225 if (!result)
4226 goto error;
4228 isl_map_free(map1);
4229 isl_map_free(map2);
4230 return result;
4231 error:
4232 isl_map_free(map1);
4233 isl_map_free(map2);
4234 return NULL;
4237 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4238 __isl_take isl_set *set2)
4240 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4243 /* Given a basic map A -> f(A), construct A -> -f(A).
4245 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4247 int i, j;
4248 unsigned off, n;
4250 bmap = isl_basic_map_cow(bmap);
4251 if (!bmap)
4252 return NULL;
4254 n = isl_basic_map_dim(bmap, isl_dim_out);
4255 off = isl_basic_map_offset(bmap, isl_dim_out);
4256 for (i = 0; i < bmap->n_eq; ++i)
4257 for (j = 0; j < n; ++j)
4258 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4259 for (i = 0; i < bmap->n_ineq; ++i)
4260 for (j = 0; j < n; ++j)
4261 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4262 for (i = 0; i < bmap->n_div; ++i)
4263 for (j = 0; j < n; ++j)
4264 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4265 bmap = isl_basic_map_gauss(bmap, NULL);
4266 return isl_basic_map_finalize(bmap);
4269 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4271 return isl_basic_map_neg(bset);
4274 /* Given a map A -> f(A), construct A -> -f(A).
4276 struct isl_map *isl_map_neg(struct isl_map *map)
4278 int i;
4280 map = isl_map_cow(map);
4281 if (!map)
4282 return NULL;
4284 for (i = 0; i < map->n; ++i) {
4285 map->p[i] = isl_basic_map_neg(map->p[i]);
4286 if (!map->p[i])
4287 goto error;
4290 return map;
4291 error:
4292 isl_map_free(map);
4293 return NULL;
4296 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4298 return set_from_map(isl_map_neg(set_to_map(set)));
4301 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4302 * A -> floor(f(A)/d).
4304 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4305 isl_int d)
4307 unsigned n_in, n_out, nparam, total, pos;
4308 struct isl_basic_map *result = NULL;
4309 struct isl_dim_map *dim_map;
4310 int i;
4312 if (!bmap)
4313 return NULL;
4315 nparam = isl_basic_map_n_param(bmap);
4316 n_in = isl_basic_map_n_in(bmap);
4317 n_out = isl_basic_map_n_out(bmap);
4319 total = nparam + n_in + n_out + bmap->n_div + n_out;
4320 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4321 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4322 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4323 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4324 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4326 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4327 bmap->n_div + n_out,
4328 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4329 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4330 result = add_divs(result, n_out);
4331 for (i = 0; i < n_out; ++i) {
4332 int j;
4333 j = isl_basic_map_alloc_inequality(result);
4334 if (j < 0)
4335 goto error;
4336 isl_seq_clr(result->ineq[j], 1+total);
4337 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4338 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4339 j = isl_basic_map_alloc_inequality(result);
4340 if (j < 0)
4341 goto error;
4342 isl_seq_clr(result->ineq[j], 1+total);
4343 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4344 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4345 isl_int_sub_ui(result->ineq[j][0], d, 1);
4348 result = isl_basic_map_simplify(result);
4349 return isl_basic_map_finalize(result);
4350 error:
4351 isl_basic_map_free(result);
4352 return NULL;
4355 /* Given a map A -> f(A) and an integer d, construct a map
4356 * A -> floor(f(A)/d).
4358 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4360 int i;
4362 map = isl_map_cow(map);
4363 if (!map)
4364 return NULL;
4366 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4367 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4368 for (i = 0; i < map->n; ++i) {
4369 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4370 if (!map->p[i])
4371 goto error;
4374 return map;
4375 error:
4376 isl_map_free(map);
4377 return NULL;
4380 /* Given a map A -> f(A) and an integer d, construct a map
4381 * A -> floor(f(A)/d).
4383 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4384 __isl_take isl_val *d)
4386 if (!map || !d)
4387 goto error;
4388 if (!isl_val_is_int(d))
4389 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4390 "expecting integer denominator", goto error);
4391 map = isl_map_floordiv(map, d->n);
4392 isl_val_free(d);
4393 return map;
4394 error:
4395 isl_map_free(map);
4396 isl_val_free(d);
4397 return NULL;
4400 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4402 int i;
4403 unsigned nparam;
4404 unsigned n_in;
4406 i = isl_basic_map_alloc_equality(bmap);
4407 if (i < 0)
4408 goto error;
4409 nparam = isl_basic_map_n_param(bmap);
4410 n_in = isl_basic_map_n_in(bmap);
4411 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4412 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4413 isl_int_set_si(bmap->eq[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 struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4424 int i;
4425 unsigned nparam;
4426 unsigned n_in;
4428 i = isl_basic_map_alloc_inequality(bmap);
4429 if (i < 0)
4430 goto error;
4431 nparam = isl_basic_map_n_param(bmap);
4432 n_in = isl_basic_map_n_in(bmap);
4433 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4434 isl_int_set_si(bmap->ineq[i][0], -1);
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 __isl_give isl_basic_map *var_less_or_equal(
4446 __isl_take isl_basic_map *bmap, unsigned pos)
4448 int i;
4449 unsigned nparam;
4450 unsigned n_in;
4452 i = isl_basic_map_alloc_inequality(bmap);
4453 if (i < 0)
4454 goto error;
4455 nparam = isl_basic_map_n_param(bmap);
4456 n_in = isl_basic_map_n_in(bmap);
4457 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
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 struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4470 int i;
4471 unsigned nparam;
4472 unsigned n_in;
4474 i = isl_basic_map_alloc_inequality(bmap);
4475 if (i < 0)
4476 goto error;
4477 nparam = isl_basic_map_n_param(bmap);
4478 n_in = isl_basic_map_n_in(bmap);
4479 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4480 isl_int_set_si(bmap->ineq[i][0], -1);
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 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4491 static __isl_give isl_basic_map *var_more_or_equal(
4492 __isl_take isl_basic_map *bmap, unsigned pos)
4494 int i;
4495 unsigned nparam;
4496 unsigned n_in;
4498 i = isl_basic_map_alloc_inequality(bmap);
4499 if (i < 0)
4500 goto error;
4501 nparam = isl_basic_map_n_param(bmap);
4502 n_in = isl_basic_map_n_in(bmap);
4503 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4504 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4505 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4506 return isl_basic_map_finalize(bmap);
4507 error:
4508 isl_basic_map_free(bmap);
4509 return NULL;
4512 __isl_give isl_basic_map *isl_basic_map_equal(
4513 __isl_take isl_space *dim, unsigned n_equal)
4515 int i;
4516 struct isl_basic_map *bmap;
4517 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4518 if (!bmap)
4519 return NULL;
4520 for (i = 0; i < n_equal && bmap; ++i)
4521 bmap = var_equal(bmap, i);
4522 return isl_basic_map_finalize(bmap);
4525 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4527 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4528 unsigned pos)
4530 int i;
4531 struct isl_basic_map *bmap;
4532 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4533 if (!bmap)
4534 return NULL;
4535 for (i = 0; i < pos && bmap; ++i)
4536 bmap = var_equal(bmap, i);
4537 if (bmap)
4538 bmap = var_less(bmap, pos);
4539 return isl_basic_map_finalize(bmap);
4542 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4544 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4545 __isl_take isl_space *dim, unsigned pos)
4547 int i;
4548 isl_basic_map *bmap;
4550 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4551 for (i = 0; i < pos; ++i)
4552 bmap = var_equal(bmap, i);
4553 bmap = var_less_or_equal(bmap, pos);
4554 return isl_basic_map_finalize(bmap);
4557 /* Return a relation on "dim" expressing i_pos > o_pos
4559 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4560 unsigned pos)
4562 int i;
4563 struct isl_basic_map *bmap;
4564 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4565 if (!bmap)
4566 return NULL;
4567 for (i = 0; i < pos && bmap; ++i)
4568 bmap = var_equal(bmap, i);
4569 if (bmap)
4570 bmap = var_more(bmap, pos);
4571 return isl_basic_map_finalize(bmap);
4574 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4576 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4577 __isl_take isl_space *dim, unsigned pos)
4579 int i;
4580 isl_basic_map *bmap;
4582 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4583 for (i = 0; i < pos; ++i)
4584 bmap = var_equal(bmap, i);
4585 bmap = var_more_or_equal(bmap, pos);
4586 return isl_basic_map_finalize(bmap);
4589 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4590 unsigned n, int equal)
4592 struct isl_map *map;
4593 int i;
4595 if (n == 0 && equal)
4596 return isl_map_universe(dims);
4598 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4600 for (i = 0; i + 1 < n; ++i)
4601 map = isl_map_add_basic_map(map,
4602 isl_basic_map_less_at(isl_space_copy(dims), i));
4603 if (n > 0) {
4604 if (equal)
4605 map = isl_map_add_basic_map(map,
4606 isl_basic_map_less_or_equal_at(dims, n - 1));
4607 else
4608 map = isl_map_add_basic_map(map,
4609 isl_basic_map_less_at(dims, n - 1));
4610 } else
4611 isl_space_free(dims);
4613 return map;
4616 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4618 if (!dims)
4619 return NULL;
4620 return map_lex_lte_first(dims, dims->n_out, equal);
4623 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4625 return map_lex_lte_first(dim, n, 0);
4628 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4630 return map_lex_lte_first(dim, n, 1);
4633 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4635 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4638 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4640 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4643 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4644 unsigned n, int equal)
4646 struct isl_map *map;
4647 int i;
4649 if (n == 0 && equal)
4650 return isl_map_universe(dims);
4652 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4654 for (i = 0; i + 1 < n; ++i)
4655 map = isl_map_add_basic_map(map,
4656 isl_basic_map_more_at(isl_space_copy(dims), i));
4657 if (n > 0) {
4658 if (equal)
4659 map = isl_map_add_basic_map(map,
4660 isl_basic_map_more_or_equal_at(dims, n - 1));
4661 else
4662 map = isl_map_add_basic_map(map,
4663 isl_basic_map_more_at(dims, n - 1));
4664 } else
4665 isl_space_free(dims);
4667 return map;
4670 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4672 if (!dims)
4673 return NULL;
4674 return map_lex_gte_first(dims, dims->n_out, equal);
4677 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4679 return map_lex_gte_first(dim, n, 0);
4682 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4684 return map_lex_gte_first(dim, n, 1);
4687 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4689 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4692 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4694 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4697 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4698 __isl_take isl_set *set2)
4700 isl_map *map;
4701 map = isl_map_lex_le(isl_set_get_space(set1));
4702 map = isl_map_intersect_domain(map, set1);
4703 map = isl_map_intersect_range(map, set2);
4704 return map;
4707 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4708 __isl_take isl_set *set2)
4710 isl_map *map;
4711 map = isl_map_lex_lt(isl_set_get_space(set1));
4712 map = isl_map_intersect_domain(map, set1);
4713 map = isl_map_intersect_range(map, set2);
4714 return map;
4717 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4718 __isl_take isl_set *set2)
4720 isl_map *map;
4721 map = isl_map_lex_ge(isl_set_get_space(set1));
4722 map = isl_map_intersect_domain(map, set1);
4723 map = isl_map_intersect_range(map, set2);
4724 return map;
4727 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4728 __isl_take isl_set *set2)
4730 isl_map *map;
4731 map = isl_map_lex_gt(isl_set_get_space(set1));
4732 map = isl_map_intersect_domain(map, set1);
4733 map = isl_map_intersect_range(map, set2);
4734 return map;
4737 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4738 __isl_take isl_map *map2)
4740 isl_map *map;
4741 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4742 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4743 map = isl_map_apply_range(map, isl_map_reverse(map2));
4744 return map;
4747 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4748 __isl_take isl_map *map2)
4750 isl_map *map;
4751 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4752 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4753 map = isl_map_apply_range(map, isl_map_reverse(map2));
4754 return map;
4757 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4758 __isl_take isl_map *map2)
4760 isl_map *map;
4761 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4762 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4763 map = isl_map_apply_range(map, isl_map_reverse(map2));
4764 return map;
4767 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4768 __isl_take isl_map *map2)
4770 isl_map *map;
4771 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4772 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4773 map = isl_map_apply_range(map, isl_map_reverse(map2));
4774 return map;
4777 static __isl_give isl_basic_map *basic_map_from_basic_set(
4778 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4780 struct isl_basic_map *bmap;
4782 bset = isl_basic_set_cow(bset);
4783 if (!bset || !dim)
4784 goto error;
4786 isl_assert(bset->ctx, isl_space_compatible_internal(bset->dim, dim),
4787 goto error);
4788 isl_space_free(bset->dim);
4789 bmap = bset_to_bmap(bset);
4790 bmap->dim = dim;
4791 return isl_basic_map_finalize(bmap);
4792 error:
4793 isl_basic_set_free(bset);
4794 isl_space_free(dim);
4795 return NULL;
4798 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4799 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
4801 return basic_map_from_basic_set(bset, space);
4804 /* For a div d = floor(f/m), add the constraint
4806 * f - m d >= 0
4808 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4809 unsigned pos, isl_int *div)
4811 int i;
4812 unsigned total = isl_basic_map_total_dim(bmap);
4814 i = isl_basic_map_alloc_inequality(bmap);
4815 if (i < 0)
4816 return isl_stat_error;
4817 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4818 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4820 return isl_stat_ok;
4823 /* For a div d = floor(f/m), add the constraint
4825 * -(f-(m-1)) + m d >= 0
4827 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4828 unsigned pos, isl_int *div)
4830 int i;
4831 unsigned total = isl_basic_map_total_dim(bmap);
4833 i = isl_basic_map_alloc_inequality(bmap);
4834 if (i < 0)
4835 return isl_stat_error;
4836 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4837 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4838 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4839 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4841 return isl_stat_ok;
4844 /* For a div d = floor(f/m), add the constraints
4846 * f - m d >= 0
4847 * -(f-(m-1)) + m d >= 0
4849 * Note that the second constraint is the negation of
4851 * f - m d >= m
4853 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4854 unsigned pos, isl_int *div)
4856 if (add_upper_div_constraint(bmap, pos, div) < 0)
4857 return -1;
4858 if (add_lower_div_constraint(bmap, pos, div) < 0)
4859 return -1;
4860 return 0;
4863 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4864 unsigned pos, isl_int *div)
4866 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4867 pos, div);
4870 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4872 unsigned total = isl_basic_map_total_dim(bmap);
4873 unsigned div_pos = total - bmap->n_div + div;
4875 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4876 bmap->div[div]);
4879 /* For each known div d = floor(f/m), add the constraints
4881 * f - m d >= 0
4882 * -(f-(m-1)) + m d >= 0
4884 * Remove duplicate constraints in case of some these div constraints
4885 * already appear in "bmap".
4887 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4888 __isl_take isl_basic_map *bmap)
4890 unsigned n_div;
4892 if (!bmap)
4893 return NULL;
4894 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4895 if (n_div == 0)
4896 return bmap;
4898 bmap = add_known_div_constraints(bmap);
4899 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4900 bmap = isl_basic_map_finalize(bmap);
4901 return bmap;
4904 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4906 * In particular, if this div is of the form d = floor(f/m),
4907 * then add the constraint
4909 * f - m d >= 0
4911 * if sign < 0 or the constraint
4913 * -(f-(m-1)) + m d >= 0
4915 * if sign > 0.
4917 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4918 unsigned div, int sign)
4920 unsigned total;
4921 unsigned div_pos;
4923 if (!bmap)
4924 return -1;
4926 total = isl_basic_map_total_dim(bmap);
4927 div_pos = total - bmap->n_div + div;
4929 if (sign < 0)
4930 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4931 else
4932 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4935 struct isl_basic_set *isl_basic_map_underlying_set(
4936 struct isl_basic_map *bmap)
4938 if (!bmap)
4939 goto error;
4940 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4941 bmap->n_div == 0 &&
4942 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4943 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4944 return bset_from_bmap(bmap);
4945 bmap = isl_basic_map_cow(bmap);
4946 if (!bmap)
4947 goto error;
4948 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4949 if (!bmap->dim)
4950 goto error;
4951 bmap->extra -= bmap->n_div;
4952 bmap->n_div = 0;
4953 bmap = isl_basic_map_finalize(bmap);
4954 return bset_from_bmap(bmap);
4955 error:
4956 isl_basic_map_free(bmap);
4957 return NULL;
4960 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4961 __isl_take isl_basic_set *bset)
4963 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4966 /* Replace each element in "list" by the result of applying
4967 * isl_basic_map_underlying_set to the element.
4969 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4970 __isl_take isl_basic_map_list *list)
4972 int i, n;
4974 if (!list)
4975 return NULL;
4977 n = isl_basic_map_list_n_basic_map(list);
4978 for (i = 0; i < n; ++i) {
4979 isl_basic_map *bmap;
4980 isl_basic_set *bset;
4982 bmap = isl_basic_map_list_get_basic_map(list, i);
4983 bset = isl_basic_set_underlying_set(bmap);
4984 list = isl_basic_set_list_set_basic_set(list, i, bset);
4987 return list;
4990 struct isl_basic_map *isl_basic_map_overlying_set(
4991 struct isl_basic_set *bset, struct isl_basic_map *like)
4993 struct isl_basic_map *bmap;
4994 struct isl_ctx *ctx;
4995 unsigned total;
4996 int i;
4998 if (!bset || !like)
4999 goto error;
5000 ctx = bset->ctx;
5001 isl_assert(ctx, bset->n_div == 0, goto error);
5002 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5003 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5004 goto error);
5005 if (like->n_div == 0) {
5006 isl_space *space = isl_basic_map_get_space(like);
5007 isl_basic_map_free(like);
5008 return isl_basic_map_reset_space(bset, space);
5010 bset = isl_basic_set_cow(bset);
5011 if (!bset)
5012 goto error;
5013 total = bset->dim->n_out + bset->extra;
5014 bmap = bset_to_bmap(bset);
5015 isl_space_free(bmap->dim);
5016 bmap->dim = isl_space_copy(like->dim);
5017 if (!bmap->dim)
5018 goto error;
5019 bmap->n_div = like->n_div;
5020 bmap->extra += like->n_div;
5021 if (bmap->extra) {
5022 unsigned ltotal;
5023 isl_int **div;
5024 ltotal = total - bmap->extra + like->extra;
5025 if (ltotal > total)
5026 ltotal = total;
5027 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5028 bmap->extra * (1 + 1 + total));
5029 if (isl_blk_is_error(bmap->block2))
5030 goto error;
5031 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5032 if (!div)
5033 goto error;
5034 bmap->div = div;
5035 for (i = 0; i < bmap->extra; ++i)
5036 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5037 for (i = 0; i < like->n_div; ++i) {
5038 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5039 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5041 bmap = isl_basic_map_add_known_div_constraints(bmap);
5043 isl_basic_map_free(like);
5044 bmap = isl_basic_map_simplify(bmap);
5045 bmap = isl_basic_map_finalize(bmap);
5046 return bmap;
5047 error:
5048 isl_basic_map_free(like);
5049 isl_basic_set_free(bset);
5050 return NULL;
5053 struct isl_basic_set *isl_basic_set_from_underlying_set(
5054 struct isl_basic_set *bset, struct isl_basic_set *like)
5056 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5057 bset_to_bmap(like)));
5060 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5062 int i;
5064 map = isl_map_cow(map);
5065 if (!map)
5066 return NULL;
5067 map->dim = isl_space_cow(map->dim);
5068 if (!map->dim)
5069 goto error;
5071 for (i = 1; i < map->n; ++i)
5072 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5073 goto error);
5074 for (i = 0; i < map->n; ++i) {
5075 map->p[i] = bset_to_bmap(
5076 isl_basic_map_underlying_set(map->p[i]));
5077 if (!map->p[i])
5078 goto error;
5080 if (map->n == 0)
5081 map->dim = isl_space_underlying(map->dim, 0);
5082 else {
5083 isl_space_free(map->dim);
5084 map->dim = isl_space_copy(map->p[0]->dim);
5086 if (!map->dim)
5087 goto error;
5088 return set_from_map(map);
5089 error:
5090 isl_map_free(map);
5091 return NULL;
5094 /* Replace the space of "bmap" by "space".
5096 * If the space of "bmap" is identical to "space" (including the identifiers
5097 * of the input and output dimensions), then simply return the original input.
5099 __isl_give isl_basic_map *isl_basic_map_reset_space(
5100 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5102 isl_bool equal;
5104 if (!bmap)
5105 goto error;
5106 equal = isl_space_is_equal(bmap->dim, space);
5107 if (equal >= 0 && equal)
5108 equal = isl_space_match(bmap->dim, isl_dim_in,
5109 space, isl_dim_in);
5110 if (equal >= 0 && equal)
5111 equal = isl_space_match(bmap->dim, isl_dim_out,
5112 space, isl_dim_out);
5113 if (equal < 0)
5114 goto error;
5115 if (equal) {
5116 isl_space_free(space);
5117 return bmap;
5119 bmap = isl_basic_map_cow(bmap);
5120 if (!bmap || !space)
5121 goto error;
5123 isl_space_free(bmap->dim);
5124 bmap->dim = space;
5126 bmap = isl_basic_map_finalize(bmap);
5128 return bmap;
5129 error:
5130 isl_basic_map_free(bmap);
5131 isl_space_free(space);
5132 return NULL;
5135 __isl_give isl_basic_set *isl_basic_set_reset_space(
5136 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5138 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5139 dim));
5142 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5143 __isl_take isl_space *dim)
5145 int i;
5147 map = isl_map_cow(map);
5148 if (!map || !dim)
5149 goto error;
5151 for (i = 0; i < map->n; ++i) {
5152 map->p[i] = isl_basic_map_reset_space(map->p[i],
5153 isl_space_copy(dim));
5154 if (!map->p[i])
5155 goto error;
5157 isl_space_free(map->dim);
5158 map->dim = dim;
5160 return map;
5161 error:
5162 isl_map_free(map);
5163 isl_space_free(dim);
5164 return NULL;
5167 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5168 __isl_take isl_space *dim)
5170 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5173 /* Compute the parameter domain of the given basic set.
5175 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5177 isl_bool is_params;
5178 isl_space *space;
5179 unsigned n;
5181 is_params = isl_basic_set_is_params(bset);
5182 if (is_params < 0)
5183 return isl_basic_set_free(bset);
5184 if (is_params)
5185 return bset;
5187 n = isl_basic_set_dim(bset, isl_dim_set);
5188 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5189 space = isl_basic_set_get_space(bset);
5190 space = isl_space_params(space);
5191 bset = isl_basic_set_reset_space(bset, space);
5192 return bset;
5195 /* Construct a zero-dimensional basic set with the given parameter domain.
5197 __isl_give isl_basic_set *isl_basic_set_from_params(
5198 __isl_take isl_basic_set *bset)
5200 isl_space *space;
5201 space = isl_basic_set_get_space(bset);
5202 space = isl_space_set_from_params(space);
5203 bset = isl_basic_set_reset_space(bset, space);
5204 return bset;
5207 /* Compute the parameter domain of the given set.
5209 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5211 isl_space *space;
5212 unsigned n;
5214 if (isl_set_is_params(set))
5215 return set;
5217 n = isl_set_dim(set, isl_dim_set);
5218 set = isl_set_project_out(set, isl_dim_set, 0, n);
5219 space = isl_set_get_space(set);
5220 space = isl_space_params(space);
5221 set = isl_set_reset_space(set, space);
5222 return set;
5225 /* Construct a zero-dimensional set with the given parameter domain.
5227 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5229 isl_space *space;
5230 space = isl_set_get_space(set);
5231 space = isl_space_set_from_params(space);
5232 set = isl_set_reset_space(set, space);
5233 return set;
5236 /* Compute the parameter domain of the given map.
5238 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5240 isl_space *space;
5241 unsigned n;
5243 n = isl_map_dim(map, isl_dim_in);
5244 map = isl_map_project_out(map, isl_dim_in, 0, n);
5245 n = isl_map_dim(map, isl_dim_out);
5246 map = isl_map_project_out(map, isl_dim_out, 0, n);
5247 space = isl_map_get_space(map);
5248 space = isl_space_params(space);
5249 map = isl_map_reset_space(map, space);
5250 return map;
5253 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5255 isl_space *space;
5256 unsigned n_out;
5258 if (!bmap)
5259 return NULL;
5260 space = isl_space_domain(isl_basic_map_get_space(bmap));
5262 n_out = isl_basic_map_n_out(bmap);
5263 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5265 return isl_basic_map_reset_space(bmap, space);
5268 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5270 if (!bmap)
5271 return isl_bool_error;
5272 return isl_space_may_be_set(bmap->dim);
5275 /* Is this basic map actually a set?
5276 * Users should never call this function. Outside of isl,
5277 * the type should indicate whether something is a set or a map.
5279 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5281 if (!bmap)
5282 return isl_bool_error;
5283 return isl_space_is_set(bmap->dim);
5286 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5288 isl_bool is_set;
5290 is_set = isl_basic_map_is_set(bmap);
5291 if (is_set < 0)
5292 goto error;
5293 if (is_set)
5294 return bmap;
5295 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5296 error:
5297 isl_basic_map_free(bmap);
5298 return NULL;
5301 __isl_give isl_basic_map *isl_basic_map_domain_map(
5302 __isl_take isl_basic_map *bmap)
5304 int i;
5305 isl_space *dim;
5306 isl_basic_map *domain;
5307 int nparam, n_in, n_out;
5309 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5310 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5311 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5313 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5314 domain = isl_basic_map_universe(dim);
5316 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5317 bmap = isl_basic_map_apply_range(bmap, domain);
5318 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5320 for (i = 0; i < n_in; ++i)
5321 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5322 isl_dim_out, i);
5324 bmap = isl_basic_map_gauss(bmap, NULL);
5325 return isl_basic_map_finalize(bmap);
5328 __isl_give isl_basic_map *isl_basic_map_range_map(
5329 __isl_take isl_basic_map *bmap)
5331 int i;
5332 isl_space *dim;
5333 isl_basic_map *range;
5334 int nparam, n_in, n_out;
5336 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5337 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5338 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5340 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5341 range = isl_basic_map_universe(dim);
5343 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5344 bmap = isl_basic_map_apply_range(bmap, range);
5345 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5347 for (i = 0; i < n_out; ++i)
5348 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5349 isl_dim_out, i);
5351 bmap = isl_basic_map_gauss(bmap, NULL);
5352 return isl_basic_map_finalize(bmap);
5355 int isl_map_may_be_set(__isl_keep isl_map *map)
5357 if (!map)
5358 return -1;
5359 return isl_space_may_be_set(map->dim);
5362 /* Is this map actually a set?
5363 * Users should never call this function. Outside of isl,
5364 * the type should indicate whether something is a set or a map.
5366 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5368 if (!map)
5369 return isl_bool_error;
5370 return isl_space_is_set(map->dim);
5373 struct isl_set *isl_map_range(struct isl_map *map)
5375 int i;
5376 isl_bool is_set;
5377 struct isl_set *set;
5379 is_set = isl_map_is_set(map);
5380 if (is_set < 0)
5381 goto error;
5382 if (is_set)
5383 return set_from_map(map);
5385 map = isl_map_cow(map);
5386 if (!map)
5387 goto error;
5389 set = set_from_map(map);
5390 set->dim = isl_space_range(set->dim);
5391 if (!set->dim)
5392 goto error;
5393 for (i = 0; i < map->n; ++i) {
5394 set->p[i] = isl_basic_map_range(map->p[i]);
5395 if (!set->p[i])
5396 goto error;
5398 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5399 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5400 return set;
5401 error:
5402 isl_map_free(map);
5403 return NULL;
5406 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5408 int i;
5410 map = isl_map_cow(map);
5411 if (!map)
5412 return NULL;
5414 map->dim = isl_space_domain_map(map->dim);
5415 if (!map->dim)
5416 goto error;
5417 for (i = 0; i < map->n; ++i) {
5418 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5419 if (!map->p[i])
5420 goto error;
5422 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5423 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5424 return map;
5425 error:
5426 isl_map_free(map);
5427 return NULL;
5430 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5432 int i;
5433 isl_space *range_dim;
5435 map = isl_map_cow(map);
5436 if (!map)
5437 return NULL;
5439 range_dim = isl_space_range(isl_map_get_space(map));
5440 range_dim = isl_space_from_range(range_dim);
5441 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5442 map->dim = isl_space_join(map->dim, range_dim);
5443 if (!map->dim)
5444 goto error;
5445 for (i = 0; i < map->n; ++i) {
5446 map->p[i] = isl_basic_map_range_map(map->p[i]);
5447 if (!map->p[i])
5448 goto error;
5450 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5451 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5452 return map;
5453 error:
5454 isl_map_free(map);
5455 return NULL;
5458 /* Given a wrapped map of the form A[B -> C],
5459 * return the map A[B -> C] -> B.
5461 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5463 isl_id *id;
5464 isl_map *map;
5466 if (!set)
5467 return NULL;
5468 if (!isl_set_has_tuple_id(set))
5469 return isl_map_domain_map(isl_set_unwrap(set));
5471 id = isl_set_get_tuple_id(set);
5472 map = isl_map_domain_map(isl_set_unwrap(set));
5473 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5475 return map;
5478 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5479 __isl_take isl_space *dim)
5481 int i;
5482 struct isl_map *map = NULL;
5484 set = isl_set_cow(set);
5485 if (!set || !dim)
5486 goto error;
5487 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5488 goto error);
5489 map = set_to_map(set);
5490 for (i = 0; i < set->n; ++i) {
5491 map->p[i] = basic_map_from_basic_set(
5492 set->p[i], isl_space_copy(dim));
5493 if (!map->p[i])
5494 goto error;
5496 isl_space_free(map->dim);
5497 map->dim = dim;
5498 return map;
5499 error:
5500 isl_space_free(dim);
5501 isl_set_free(set);
5502 return NULL;
5505 __isl_give isl_basic_map *isl_basic_map_from_domain(
5506 __isl_take isl_basic_set *bset)
5508 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5511 __isl_give isl_basic_map *isl_basic_map_from_range(
5512 __isl_take isl_basic_set *bset)
5514 isl_space *space;
5515 space = isl_basic_set_get_space(bset);
5516 space = isl_space_from_range(space);
5517 bset = isl_basic_set_reset_space(bset, space);
5518 return bset_to_bmap(bset);
5521 /* Create a relation with the given set as range.
5522 * The domain of the created relation is a zero-dimensional
5523 * flat anonymous space.
5525 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5527 isl_space *space;
5528 space = isl_set_get_space(set);
5529 space = isl_space_from_range(space);
5530 set = isl_set_reset_space(set, space);
5531 return set_to_map(set);
5534 /* Create a relation with the given set as domain.
5535 * The range of the created relation is a zero-dimensional
5536 * flat anonymous space.
5538 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5540 return isl_map_reverse(isl_map_from_range(set));
5543 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5544 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5546 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5549 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5550 __isl_take isl_set *range)
5552 return isl_map_apply_range(isl_map_reverse(domain), range);
5555 /* Return a newly allocated isl_map with given space and flags and
5556 * room for "n" basic maps.
5557 * Make sure that all cached information is cleared.
5559 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5560 unsigned flags)
5562 struct isl_map *map;
5564 if (!space)
5565 return NULL;
5566 if (n < 0)
5567 isl_die(space->ctx, isl_error_internal,
5568 "negative number of basic maps", goto error);
5569 map = isl_calloc(space->ctx, struct isl_map,
5570 sizeof(struct isl_map) +
5571 (n - 1) * sizeof(struct isl_basic_map *));
5572 if (!map)
5573 goto error;
5575 map->ctx = space->ctx;
5576 isl_ctx_ref(map->ctx);
5577 map->ref = 1;
5578 map->size = n;
5579 map->n = 0;
5580 map->dim = space;
5581 map->flags = flags;
5582 return map;
5583 error:
5584 isl_space_free(space);
5585 return NULL;
5588 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5590 struct isl_basic_map *bmap;
5591 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5592 bmap = isl_basic_map_set_to_empty(bmap);
5593 return bmap;
5596 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5598 struct isl_basic_set *bset;
5599 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5600 bset = isl_basic_set_set_to_empty(bset);
5601 return bset;
5604 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5606 struct isl_basic_map *bmap;
5607 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5608 bmap = isl_basic_map_finalize(bmap);
5609 return bmap;
5612 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5614 struct isl_basic_set *bset;
5615 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5616 bset = isl_basic_set_finalize(bset);
5617 return bset;
5620 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5622 int i;
5623 unsigned total = isl_space_dim(dim, isl_dim_all);
5624 isl_basic_map *bmap;
5626 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5627 for (i = 0; i < total; ++i) {
5628 int k = isl_basic_map_alloc_inequality(bmap);
5629 if (k < 0)
5630 goto error;
5631 isl_seq_clr(bmap->ineq[k], 1 + total);
5632 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5634 return bmap;
5635 error:
5636 isl_basic_map_free(bmap);
5637 return NULL;
5640 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5642 return isl_basic_map_nat_universe(dim);
5645 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5647 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5650 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5652 return isl_map_nat_universe(dim);
5655 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5657 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5660 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5662 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5665 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5667 struct isl_map *map;
5668 if (!dim)
5669 return NULL;
5670 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5671 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5672 return map;
5675 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5677 struct isl_set *set;
5678 if (!dim)
5679 return NULL;
5680 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5681 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5682 return set;
5685 struct isl_map *isl_map_dup(struct isl_map *map)
5687 int i;
5688 struct isl_map *dup;
5690 if (!map)
5691 return NULL;
5692 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5693 for (i = 0; i < map->n; ++i)
5694 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5695 return dup;
5698 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5699 __isl_take isl_basic_map *bmap)
5701 if (!bmap || !map)
5702 goto error;
5703 if (isl_basic_map_plain_is_empty(bmap)) {
5704 isl_basic_map_free(bmap);
5705 return map;
5707 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5708 isl_assert(map->ctx, map->n < map->size, goto error);
5709 map->p[map->n] = bmap;
5710 map->n++;
5711 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5712 return map;
5713 error:
5714 if (map)
5715 isl_map_free(map);
5716 if (bmap)
5717 isl_basic_map_free(bmap);
5718 return NULL;
5721 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5723 int i;
5725 if (!map)
5726 return NULL;
5728 if (--map->ref > 0)
5729 return NULL;
5731 clear_caches(map);
5732 isl_ctx_deref(map->ctx);
5733 for (i = 0; i < map->n; ++i)
5734 isl_basic_map_free(map->p[i]);
5735 isl_space_free(map->dim);
5736 free(map);
5738 return NULL;
5741 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5742 struct isl_basic_map *bmap, unsigned pos, int value)
5744 int j;
5746 bmap = isl_basic_map_cow(bmap);
5747 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5748 j = isl_basic_map_alloc_equality(bmap);
5749 if (j < 0)
5750 goto error;
5751 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5752 isl_int_set_si(bmap->eq[j][pos], -1);
5753 isl_int_set_si(bmap->eq[j][0], value);
5754 bmap = isl_basic_map_simplify(bmap);
5755 return isl_basic_map_finalize(bmap);
5756 error:
5757 isl_basic_map_free(bmap);
5758 return NULL;
5761 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5762 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5764 int j;
5766 bmap = isl_basic_map_cow(bmap);
5767 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5768 j = isl_basic_map_alloc_equality(bmap);
5769 if (j < 0)
5770 goto error;
5771 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5772 isl_int_set_si(bmap->eq[j][pos], -1);
5773 isl_int_set(bmap->eq[j][0], value);
5774 bmap = isl_basic_map_simplify(bmap);
5775 return isl_basic_map_finalize(bmap);
5776 error:
5777 isl_basic_map_free(bmap);
5778 return NULL;
5781 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5782 enum isl_dim_type type, unsigned pos, int value)
5784 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5785 return isl_basic_map_free(bmap);
5786 return isl_basic_map_fix_pos_si(bmap,
5787 isl_basic_map_offset(bmap, type) + pos, value);
5790 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5791 enum isl_dim_type type, unsigned pos, isl_int value)
5793 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5794 return isl_basic_map_free(bmap);
5795 return isl_basic_map_fix_pos(bmap,
5796 isl_basic_map_offset(bmap, type) + pos, value);
5799 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5800 * to be equal to "v".
5802 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5803 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5805 if (!bmap || !v)
5806 goto error;
5807 if (!isl_val_is_int(v))
5808 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5809 "expecting integer value", goto error);
5810 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5811 goto error;
5812 pos += isl_basic_map_offset(bmap, type);
5813 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5814 isl_val_free(v);
5815 return bmap;
5816 error:
5817 isl_basic_map_free(bmap);
5818 isl_val_free(v);
5819 return NULL;
5822 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5823 * to be equal to "v".
5825 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5826 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5828 return isl_basic_map_fix_val(bset, type, pos, v);
5831 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5832 enum isl_dim_type type, unsigned pos, int value)
5834 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5835 type, pos, value));
5838 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5839 enum isl_dim_type type, unsigned pos, isl_int value)
5841 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5842 type, pos, value));
5845 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5846 unsigned input, int value)
5848 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5851 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5852 unsigned dim, int value)
5854 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5855 isl_dim_set, dim, value));
5858 static int remove_if_empty(__isl_keep isl_map *map, int i)
5860 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5862 if (empty < 0)
5863 return -1;
5864 if (!empty)
5865 return 0;
5867 isl_basic_map_free(map->p[i]);
5868 if (i != map->n - 1) {
5869 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5870 map->p[i] = map->p[map->n - 1];
5872 map->n--;
5874 return 0;
5877 /* Perform "fn" on each basic map of "map", where we may not be holding
5878 * the only reference to "map".
5879 * In particular, "fn" should be a semantics preserving operation
5880 * that we want to apply to all copies of "map". We therefore need
5881 * to be careful not to modify "map" in a way that breaks "map"
5882 * in case anything goes wrong.
5884 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5885 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5887 struct isl_basic_map *bmap;
5888 int i;
5890 if (!map)
5891 return NULL;
5893 for (i = map->n - 1; i >= 0; --i) {
5894 bmap = isl_basic_map_copy(map->p[i]);
5895 bmap = fn(bmap);
5896 if (!bmap)
5897 goto error;
5898 isl_basic_map_free(map->p[i]);
5899 map->p[i] = bmap;
5900 if (remove_if_empty(map, i) < 0)
5901 goto error;
5904 return map;
5905 error:
5906 isl_map_free(map);
5907 return NULL;
5910 struct isl_map *isl_map_fix_si(struct isl_map *map,
5911 enum isl_dim_type type, unsigned pos, int value)
5913 int i;
5915 map = isl_map_cow(map);
5916 if (!map)
5917 return NULL;
5919 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5920 for (i = map->n - 1; i >= 0; --i) {
5921 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5922 if (remove_if_empty(map, i) < 0)
5923 goto error;
5925 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5926 return map;
5927 error:
5928 isl_map_free(map);
5929 return NULL;
5932 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5933 enum isl_dim_type type, unsigned pos, int value)
5935 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5938 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5939 enum isl_dim_type type, unsigned pos, isl_int value)
5941 int i;
5943 map = isl_map_cow(map);
5944 if (!map)
5945 return NULL;
5947 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5948 for (i = 0; i < map->n; ++i) {
5949 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5950 if (!map->p[i])
5951 goto error;
5953 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5954 return map;
5955 error:
5956 isl_map_free(map);
5957 return NULL;
5960 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5961 enum isl_dim_type type, unsigned pos, isl_int value)
5963 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
5966 /* Fix the value of the variable at position "pos" of type "type" of "map"
5967 * to be equal to "v".
5969 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5970 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5972 int i;
5974 map = isl_map_cow(map);
5975 if (!map || !v)
5976 goto error;
5978 if (!isl_val_is_int(v))
5979 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5980 "expecting integer value", goto error);
5981 if (pos >= isl_map_dim(map, type))
5982 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5983 "index out of bounds", goto error);
5984 for (i = map->n - 1; i >= 0; --i) {
5985 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5986 isl_val_copy(v));
5987 if (remove_if_empty(map, i) < 0)
5988 goto error;
5990 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5991 isl_val_free(v);
5992 return map;
5993 error:
5994 isl_map_free(map);
5995 isl_val_free(v);
5996 return NULL;
5999 /* Fix the value of the variable at position "pos" of type "type" of "set"
6000 * to be equal to "v".
6002 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6003 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6005 return isl_map_fix_val(set, type, pos, v);
6008 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6009 unsigned input, int value)
6011 return isl_map_fix_si(map, isl_dim_in, input, value);
6014 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6016 return set_from_map(isl_map_fix_si(set_to_map(set),
6017 isl_dim_set, dim, value));
6020 static __isl_give isl_basic_map *basic_map_bound_si(
6021 __isl_take isl_basic_map *bmap,
6022 enum isl_dim_type type, unsigned pos, int value, int upper)
6024 int j;
6026 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6027 return isl_basic_map_free(bmap);
6028 pos += isl_basic_map_offset(bmap, type);
6029 bmap = isl_basic_map_cow(bmap);
6030 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6031 j = isl_basic_map_alloc_inequality(bmap);
6032 if (j < 0)
6033 goto error;
6034 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6035 if (upper) {
6036 isl_int_set_si(bmap->ineq[j][pos], -1);
6037 isl_int_set_si(bmap->ineq[j][0], value);
6038 } else {
6039 isl_int_set_si(bmap->ineq[j][pos], 1);
6040 isl_int_set_si(bmap->ineq[j][0], -value);
6042 bmap = isl_basic_map_simplify(bmap);
6043 return isl_basic_map_finalize(bmap);
6044 error:
6045 isl_basic_map_free(bmap);
6046 return NULL;
6049 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6050 __isl_take isl_basic_map *bmap,
6051 enum isl_dim_type type, unsigned pos, int value)
6053 return basic_map_bound_si(bmap, type, pos, value, 0);
6056 /* Constrain the values of the given dimension to be no greater than "value".
6058 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6059 __isl_take isl_basic_map *bmap,
6060 enum isl_dim_type type, unsigned pos, int value)
6062 return basic_map_bound_si(bmap, type, pos, value, 1);
6065 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6066 enum isl_dim_type type, unsigned pos, int value, int upper)
6068 int i;
6070 map = isl_map_cow(map);
6071 if (!map)
6072 return NULL;
6074 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6075 for (i = 0; i < map->n; ++i) {
6076 map->p[i] = basic_map_bound_si(map->p[i],
6077 type, pos, value, upper);
6078 if (!map->p[i])
6079 goto error;
6081 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6082 return map;
6083 error:
6084 isl_map_free(map);
6085 return NULL;
6088 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6089 enum isl_dim_type type, unsigned pos, int value)
6091 return map_bound_si(map, type, pos, value, 0);
6094 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6095 enum isl_dim_type type, unsigned pos, int value)
6097 return map_bound_si(map, type, pos, value, 1);
6100 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6101 enum isl_dim_type type, unsigned pos, int value)
6103 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6104 type, pos, value));
6107 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6108 enum isl_dim_type type, unsigned pos, int value)
6110 return isl_map_upper_bound_si(set, type, pos, value);
6113 /* Bound the given variable of "bmap" from below (or above is "upper"
6114 * is set) to "value".
6116 static __isl_give isl_basic_map *basic_map_bound(
6117 __isl_take isl_basic_map *bmap,
6118 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6120 int j;
6122 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6123 return isl_basic_map_free(bmap);
6124 pos += isl_basic_map_offset(bmap, type);
6125 bmap = isl_basic_map_cow(bmap);
6126 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6127 j = isl_basic_map_alloc_inequality(bmap);
6128 if (j < 0)
6129 goto error;
6130 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6131 if (upper) {
6132 isl_int_set_si(bmap->ineq[j][pos], -1);
6133 isl_int_set(bmap->ineq[j][0], value);
6134 } else {
6135 isl_int_set_si(bmap->ineq[j][pos], 1);
6136 isl_int_neg(bmap->ineq[j][0], value);
6138 bmap = isl_basic_map_simplify(bmap);
6139 return isl_basic_map_finalize(bmap);
6140 error:
6141 isl_basic_map_free(bmap);
6142 return NULL;
6145 /* Bound the given variable of "map" from below (or above is "upper"
6146 * is set) to "value".
6148 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6149 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6151 int i;
6153 map = isl_map_cow(map);
6154 if (!map)
6155 return NULL;
6157 if (pos >= isl_map_dim(map, type))
6158 isl_die(map->ctx, isl_error_invalid,
6159 "index out of bounds", goto error);
6160 for (i = map->n - 1; i >= 0; --i) {
6161 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6162 if (remove_if_empty(map, i) < 0)
6163 goto error;
6165 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6166 return map;
6167 error:
6168 isl_map_free(map);
6169 return NULL;
6172 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6173 enum isl_dim_type type, unsigned pos, isl_int value)
6175 return map_bound(map, type, pos, value, 0);
6178 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6179 enum isl_dim_type type, unsigned pos, isl_int value)
6181 return map_bound(map, type, pos, value, 1);
6184 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6185 enum isl_dim_type type, unsigned pos, isl_int value)
6187 return isl_map_lower_bound(set, type, pos, value);
6190 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6191 enum isl_dim_type type, unsigned pos, isl_int value)
6193 return isl_map_upper_bound(set, type, pos, value);
6196 /* Force the values of the variable at position "pos" of type "type" of "set"
6197 * to be no smaller than "value".
6199 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6200 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6202 if (!value)
6203 goto error;
6204 if (!isl_val_is_int(value))
6205 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6206 "expecting integer value", goto error);
6207 set = isl_set_lower_bound(set, type, pos, value->n);
6208 isl_val_free(value);
6209 return set;
6210 error:
6211 isl_val_free(value);
6212 isl_set_free(set);
6213 return NULL;
6216 /* Force the values of the variable at position "pos" of type "type" of "set"
6217 * to be no greater than "value".
6219 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6220 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6222 if (!value)
6223 goto error;
6224 if (!isl_val_is_int(value))
6225 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6226 "expecting integer value", goto error);
6227 set = isl_set_upper_bound(set, type, pos, value->n);
6228 isl_val_free(value);
6229 return set;
6230 error:
6231 isl_val_free(value);
6232 isl_set_free(set);
6233 return NULL;
6236 struct isl_map *isl_map_reverse(struct isl_map *map)
6238 int i;
6240 map = isl_map_cow(map);
6241 if (!map)
6242 return NULL;
6244 map->dim = isl_space_reverse(map->dim);
6245 if (!map->dim)
6246 goto error;
6247 for (i = 0; i < map->n; ++i) {
6248 map->p[i] = isl_basic_map_reverse(map->p[i]);
6249 if (!map->p[i])
6250 goto error;
6252 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6253 return map;
6254 error:
6255 isl_map_free(map);
6256 return NULL;
6259 #undef TYPE
6260 #define TYPE isl_pw_multi_aff
6261 #undef SUFFIX
6262 #define SUFFIX _pw_multi_aff
6263 #undef EMPTY
6264 #define EMPTY isl_pw_multi_aff_empty
6265 #undef ADD
6266 #define ADD isl_pw_multi_aff_union_add
6267 #include "isl_map_lexopt_templ.c"
6269 /* Given a map "map", compute the lexicographically minimal
6270 * (or maximal) image element for each domain element in dom,
6271 * in the form of an isl_pw_multi_aff.
6272 * If "empty" is not NULL, then set *empty to those elements in dom that
6273 * do not have an image element.
6274 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6275 * should be computed over the domain of "map". "empty" is also NULL
6276 * in this case.
6278 * We first compute the lexicographically minimal or maximal element
6279 * in the first basic map. This results in a partial solution "res"
6280 * and a subset "todo" of dom that still need to be handled.
6281 * We then consider each of the remaining maps in "map" and successively
6282 * update both "res" and "todo".
6283 * If "empty" is NULL, then the todo sets are not needed and therefore
6284 * also not computed.
6286 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6287 __isl_take isl_map *map, __isl_take isl_set *dom,
6288 __isl_give isl_set **empty, unsigned flags)
6290 int i;
6291 int full;
6292 isl_pw_multi_aff *res;
6293 isl_set *todo;
6295 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6296 if (!map || (!full && !dom))
6297 goto error;
6299 if (isl_map_plain_is_empty(map)) {
6300 if (empty)
6301 *empty = dom;
6302 else
6303 isl_set_free(dom);
6304 return isl_pw_multi_aff_from_map(map);
6307 res = basic_map_partial_lexopt_pw_multi_aff(
6308 isl_basic_map_copy(map->p[0]),
6309 isl_set_copy(dom), empty, flags);
6311 if (empty)
6312 todo = *empty;
6313 for (i = 1; i < map->n; ++i) {
6314 isl_pw_multi_aff *res_i;
6316 res_i = basic_map_partial_lexopt_pw_multi_aff(
6317 isl_basic_map_copy(map->p[i]),
6318 isl_set_copy(dom), empty, flags);
6320 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6321 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6322 else
6323 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6325 if (empty)
6326 todo = isl_set_intersect(todo, *empty);
6329 isl_set_free(dom);
6330 isl_map_free(map);
6332 if (empty)
6333 *empty = todo;
6335 return res;
6336 error:
6337 if (empty)
6338 *empty = NULL;
6339 isl_set_free(dom);
6340 isl_map_free(map);
6341 return NULL;
6344 #undef TYPE
6345 #define TYPE isl_map
6346 #undef SUFFIX
6347 #define SUFFIX
6348 #undef EMPTY
6349 #define EMPTY isl_map_empty
6350 #undef ADD
6351 #define ADD isl_map_union_disjoint
6352 #include "isl_map_lexopt_templ.c"
6354 /* Given a map "map", compute the lexicographically minimal
6355 * (or maximal) image element for each domain element in "dom",
6356 * in the form of an isl_map.
6357 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6358 * do not have an image element.
6359 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6360 * should be computed over the domain of "map". "empty" is also NULL
6361 * in this case.
6363 * If the input consists of more than one disjunct, then first
6364 * compute the desired result in the form of an isl_pw_multi_aff and
6365 * then convert that into an isl_map.
6367 * This function used to have an explicit implementation in terms
6368 * of isl_maps, but it would continually intersect the domains of
6369 * partial results with the complement of the domain of the next
6370 * partial solution, potentially leading to an explosion in the number
6371 * of disjuncts if there are several disjuncts in the input.
6372 * An even earlier implementation of this function would look for
6373 * better results in the domain of the partial result and for extra
6374 * results in the complement of this domain, which would lead to
6375 * even more splintering.
6377 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6378 __isl_take isl_map *map, __isl_take isl_set *dom,
6379 __isl_give isl_set **empty, unsigned flags)
6381 int full;
6382 struct isl_map *res;
6383 isl_pw_multi_aff *pma;
6385 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6386 if (!map || (!full && !dom))
6387 goto error;
6389 if (isl_map_plain_is_empty(map)) {
6390 if (empty)
6391 *empty = dom;
6392 else
6393 isl_set_free(dom);
6394 return map;
6397 if (map->n == 1) {
6398 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6399 dom, empty, flags);
6400 isl_map_free(map);
6401 return res;
6404 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6405 flags);
6406 return isl_map_from_pw_multi_aff(pma);
6407 error:
6408 if (empty)
6409 *empty = NULL;
6410 isl_set_free(dom);
6411 isl_map_free(map);
6412 return NULL;
6415 __isl_give isl_map *isl_map_partial_lexmax(
6416 __isl_take isl_map *map, __isl_take isl_set *dom,
6417 __isl_give isl_set **empty)
6419 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6422 __isl_give isl_map *isl_map_partial_lexmin(
6423 __isl_take isl_map *map, __isl_take isl_set *dom,
6424 __isl_give isl_set **empty)
6426 return isl_map_partial_lexopt(map, dom, empty, 0);
6429 __isl_give isl_set *isl_set_partial_lexmin(
6430 __isl_take isl_set *set, __isl_take isl_set *dom,
6431 __isl_give isl_set **empty)
6433 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6434 dom, empty));
6437 __isl_give isl_set *isl_set_partial_lexmax(
6438 __isl_take isl_set *set, __isl_take isl_set *dom,
6439 __isl_give isl_set **empty)
6441 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6442 dom, empty));
6445 /* Compute the lexicographic minimum (or maximum if "flags" includes
6446 * ISL_OPT_MAX) of "bset" over its parametric domain.
6448 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6449 unsigned flags)
6451 return isl_basic_map_lexopt(bset, flags);
6454 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6456 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6459 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6461 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6464 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6466 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6469 /* Compute the lexicographic minimum of "bset" over its parametric domain
6470 * for the purpose of quantifier elimination.
6471 * That is, find an explicit representation for all the existentially
6472 * quantified variables in "bset" by computing their lexicographic
6473 * minimum.
6475 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6476 __isl_take isl_basic_set *bset)
6478 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6481 /* Extract the first and only affine expression from list
6482 * and then add it to *pwaff with the given dom.
6483 * This domain is known to be disjoint from other domains
6484 * because of the way isl_basic_map_foreach_lexmax works.
6486 static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
6487 __isl_take isl_aff_list *list, void *user)
6489 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6490 isl_aff *aff;
6491 isl_pw_aff **pwaff = user;
6492 isl_pw_aff *pwaff_i;
6494 if (!list)
6495 goto error;
6496 if (isl_aff_list_n_aff(list) != 1)
6497 isl_die(ctx, isl_error_internal,
6498 "expecting single element list", goto error);
6500 aff = isl_aff_list_get_aff(list, 0);
6501 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6503 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6505 isl_aff_list_free(list);
6507 return isl_stat_ok;
6508 error:
6509 isl_basic_set_free(dom);
6510 isl_aff_list_free(list);
6511 return isl_stat_error;
6514 /* Given a basic map with one output dimension, compute the minimum or
6515 * maximum of that dimension as an isl_pw_aff.
6517 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6518 * call update_dim_opt on each leaf of the result.
6520 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6521 int max)
6523 isl_space *dim = isl_basic_map_get_space(bmap);
6524 isl_pw_aff *pwaff;
6525 isl_stat r;
6527 dim = isl_space_from_domain(isl_space_domain(dim));
6528 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6529 pwaff = isl_pw_aff_empty(dim);
6531 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6532 if (r < 0)
6533 return isl_pw_aff_free(pwaff);
6535 return pwaff;
6538 /* Compute the minimum or maximum of the given output dimension
6539 * as a function of the parameters and the input dimensions,
6540 * but independently of the other output dimensions.
6542 * We first project out the other output dimension and then compute
6543 * the "lexicographic" maximum in each basic map, combining the results
6544 * using isl_pw_aff_union_max.
6546 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6547 int max)
6549 int i;
6550 isl_pw_aff *pwaff;
6551 unsigned n_out;
6553 n_out = isl_map_dim(map, isl_dim_out);
6554 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6555 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6556 if (!map)
6557 return NULL;
6559 if (map->n == 0) {
6560 isl_space *dim = isl_map_get_space(map);
6561 isl_map_free(map);
6562 return isl_pw_aff_empty(dim);
6565 pwaff = basic_map_dim_opt(map->p[0], max);
6566 for (i = 1; i < map->n; ++i) {
6567 isl_pw_aff *pwaff_i;
6569 pwaff_i = basic_map_dim_opt(map->p[i], max);
6570 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6573 isl_map_free(map);
6575 return pwaff;
6578 /* Compute the minimum of the given output dimension as a function of the
6579 * parameters and input dimensions, but independently of
6580 * the other output dimensions.
6582 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6584 return map_dim_opt(map, pos, 0);
6587 /* Compute the maximum of the given output dimension as a function of the
6588 * parameters and input dimensions, but independently of
6589 * the other output dimensions.
6591 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6593 return map_dim_opt(map, pos, 1);
6596 /* Compute the minimum or maximum of the given set dimension
6597 * as a function of the parameters,
6598 * but independently of the other set dimensions.
6600 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6601 int max)
6603 return map_dim_opt(set, pos, max);
6606 /* Compute the maximum of the given set dimension as a function of the
6607 * parameters, but independently of the other set dimensions.
6609 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6611 return set_dim_opt(set, pos, 1);
6614 /* Compute the minimum of the given set dimension as a function of the
6615 * parameters, but independently of the other set dimensions.
6617 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6619 return set_dim_opt(set, pos, 0);
6622 /* Apply a preimage specified by "mat" on the parameters of "bset".
6623 * bset is assumed to have only parameters and divs.
6625 static struct isl_basic_set *basic_set_parameter_preimage(
6626 struct isl_basic_set *bset, struct isl_mat *mat)
6628 unsigned nparam;
6630 if (!bset || !mat)
6631 goto error;
6633 bset->dim = isl_space_cow(bset->dim);
6634 if (!bset->dim)
6635 goto error;
6637 nparam = isl_basic_set_dim(bset, isl_dim_param);
6639 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6641 bset->dim->nparam = 0;
6642 bset->dim->n_out = nparam;
6643 bset = isl_basic_set_preimage(bset, mat);
6644 if (bset) {
6645 bset->dim->nparam = bset->dim->n_out;
6646 bset->dim->n_out = 0;
6648 return bset;
6649 error:
6650 isl_mat_free(mat);
6651 isl_basic_set_free(bset);
6652 return NULL;
6655 /* Apply a preimage specified by "mat" on the parameters of "set".
6656 * set is assumed to have only parameters and divs.
6658 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6659 __isl_take isl_mat *mat)
6661 isl_space *space;
6662 unsigned nparam;
6664 if (!set || !mat)
6665 goto error;
6667 nparam = isl_set_dim(set, isl_dim_param);
6669 if (mat->n_row != 1 + nparam)
6670 isl_die(isl_set_get_ctx(set), isl_error_internal,
6671 "unexpected number of rows", goto error);
6673 space = isl_set_get_space(set);
6674 space = isl_space_move_dims(space, isl_dim_set, 0,
6675 isl_dim_param, 0, nparam);
6676 set = isl_set_reset_space(set, space);
6677 set = isl_set_preimage(set, mat);
6678 nparam = isl_set_dim(set, isl_dim_out);
6679 space = isl_set_get_space(set);
6680 space = isl_space_move_dims(space, isl_dim_param, 0,
6681 isl_dim_out, 0, nparam);
6682 set = isl_set_reset_space(set, space);
6683 return set;
6684 error:
6685 isl_mat_free(mat);
6686 isl_set_free(set);
6687 return NULL;
6690 /* Intersect the basic set "bset" with the affine space specified by the
6691 * equalities in "eq".
6693 static struct isl_basic_set *basic_set_append_equalities(
6694 struct isl_basic_set *bset, struct isl_mat *eq)
6696 int i, k;
6697 unsigned len;
6699 if (!bset || !eq)
6700 goto error;
6702 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6703 eq->n_row, 0);
6704 if (!bset)
6705 goto error;
6707 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6708 for (i = 0; i < eq->n_row; ++i) {
6709 k = isl_basic_set_alloc_equality(bset);
6710 if (k < 0)
6711 goto error;
6712 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6713 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6715 isl_mat_free(eq);
6717 bset = isl_basic_set_gauss(bset, NULL);
6718 bset = isl_basic_set_finalize(bset);
6720 return bset;
6721 error:
6722 isl_mat_free(eq);
6723 isl_basic_set_free(bset);
6724 return NULL;
6727 /* Intersect the set "set" with the affine space specified by the
6728 * equalities in "eq".
6730 static struct isl_set *set_append_equalities(struct isl_set *set,
6731 struct isl_mat *eq)
6733 int i;
6735 if (!set || !eq)
6736 goto error;
6738 for (i = 0; i < set->n; ++i) {
6739 set->p[i] = basic_set_append_equalities(set->p[i],
6740 isl_mat_copy(eq));
6741 if (!set->p[i])
6742 goto error;
6744 isl_mat_free(eq);
6745 return set;
6746 error:
6747 isl_mat_free(eq);
6748 isl_set_free(set);
6749 return NULL;
6752 /* Given a basic set "bset" that only involves parameters and existentially
6753 * quantified variables, return the index of the first equality
6754 * that only involves parameters. If there is no such equality then
6755 * return bset->n_eq.
6757 * This function assumes that isl_basic_set_gauss has been called on "bset".
6759 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6761 int i, j;
6762 unsigned nparam, n_div;
6764 if (!bset)
6765 return -1;
6767 nparam = isl_basic_set_dim(bset, isl_dim_param);
6768 n_div = isl_basic_set_dim(bset, isl_dim_div);
6770 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6771 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6772 ++i;
6775 return i;
6778 /* Compute an explicit representation for the existentially quantified
6779 * variables in "bset" by computing the "minimal value" of the set
6780 * variables. Since there are no set variables, the computation of
6781 * the minimal value essentially computes an explicit representation
6782 * of the non-empty part(s) of "bset".
6784 * The input only involves parameters and existentially quantified variables.
6785 * All equalities among parameters have been removed.
6787 * Since the existentially quantified variables in the result are in general
6788 * going to be different from those in the input, we first replace
6789 * them by the minimal number of variables based on their equalities.
6790 * This should simplify the parametric integer programming.
6792 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6794 isl_morph *morph1, *morph2;
6795 isl_set *set;
6796 unsigned n;
6798 if (!bset)
6799 return NULL;
6800 if (bset->n_eq == 0)
6801 return isl_basic_set_lexmin_compute_divs(bset);
6803 morph1 = isl_basic_set_parameter_compression(bset);
6804 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6805 bset = isl_basic_set_lift(bset);
6806 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6807 bset = isl_morph_basic_set(morph2, bset);
6808 n = isl_basic_set_dim(bset, isl_dim_set);
6809 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6811 set = isl_basic_set_lexmin_compute_divs(bset);
6813 set = isl_morph_set(isl_morph_inverse(morph1), set);
6815 return set;
6818 /* Project the given basic set onto its parameter domain, possibly introducing
6819 * new, explicit, existential variables in the constraints.
6820 * The input has parameters and (possibly implicit) existential variables.
6821 * The output has the same parameters, but only
6822 * explicit existentially quantified variables.
6824 * The actual projection is performed by pip, but pip doesn't seem
6825 * to like equalities very much, so we first remove the equalities
6826 * among the parameters by performing a variable compression on
6827 * the parameters. Afterward, an inverse transformation is performed
6828 * and the equalities among the parameters are inserted back in.
6830 * The variable compression on the parameters may uncover additional
6831 * equalities that were only implicit before. We therefore check
6832 * if there are any new parameter equalities in the result and
6833 * if so recurse. The removal of parameter equalities is required
6834 * for the parameter compression performed by base_compute_divs.
6836 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6838 int i;
6839 struct isl_mat *eq;
6840 struct isl_mat *T, *T2;
6841 struct isl_set *set;
6842 unsigned nparam;
6844 bset = isl_basic_set_cow(bset);
6845 if (!bset)
6846 return NULL;
6848 if (bset->n_eq == 0)
6849 return base_compute_divs(bset);
6851 bset = isl_basic_set_gauss(bset, NULL);
6852 if (!bset)
6853 return NULL;
6854 if (isl_basic_set_plain_is_empty(bset))
6855 return isl_set_from_basic_set(bset);
6857 i = first_parameter_equality(bset);
6858 if (i == bset->n_eq)
6859 return base_compute_divs(bset);
6861 nparam = isl_basic_set_dim(bset, isl_dim_param);
6862 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6863 0, 1 + nparam);
6864 eq = isl_mat_cow(eq);
6865 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6866 if (T && T->n_col == 0) {
6867 isl_mat_free(T);
6868 isl_mat_free(T2);
6869 isl_mat_free(eq);
6870 bset = isl_basic_set_set_to_empty(bset);
6871 return isl_set_from_basic_set(bset);
6873 bset = basic_set_parameter_preimage(bset, T);
6875 i = first_parameter_equality(bset);
6876 if (!bset)
6877 set = NULL;
6878 else if (i == bset->n_eq)
6879 set = base_compute_divs(bset);
6880 else
6881 set = parameter_compute_divs(bset);
6882 set = set_parameter_preimage(set, T2);
6883 set = set_append_equalities(set, eq);
6884 return set;
6887 /* Insert the divs from "ls" before those of "bmap".
6889 * The number of columns is not changed, which means that the last
6890 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6891 * The caller is responsible for removing the same number of dimensions
6892 * from the space of "bmap".
6894 static __isl_give isl_basic_map *insert_divs_from_local_space(
6895 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6897 int i;
6898 int n_div;
6899 int old_n_div;
6901 n_div = isl_local_space_dim(ls, isl_dim_div);
6902 if (n_div == 0)
6903 return bmap;
6905 old_n_div = bmap->n_div;
6906 bmap = insert_div_rows(bmap, n_div);
6907 if (!bmap)
6908 return NULL;
6910 for (i = 0; i < n_div; ++i) {
6911 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6912 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6915 return bmap;
6918 /* Replace the space of "bmap" by the space and divs of "ls".
6920 * If "ls" has any divs, then we simplify the result since we may
6921 * have discovered some additional equalities that could simplify
6922 * the div expressions.
6924 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6925 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6927 int n_div;
6929 bmap = isl_basic_map_cow(bmap);
6930 if (!bmap || !ls)
6931 goto error;
6933 n_div = isl_local_space_dim(ls, isl_dim_div);
6934 bmap = insert_divs_from_local_space(bmap, ls);
6935 if (!bmap)
6936 goto error;
6938 isl_space_free(bmap->dim);
6939 bmap->dim = isl_local_space_get_space(ls);
6940 if (!bmap->dim)
6941 goto error;
6943 isl_local_space_free(ls);
6944 if (n_div > 0)
6945 bmap = isl_basic_map_simplify(bmap);
6946 bmap = isl_basic_map_finalize(bmap);
6947 return bmap;
6948 error:
6949 isl_basic_map_free(bmap);
6950 isl_local_space_free(ls);
6951 return NULL;
6954 /* Replace the space of "map" by the space and divs of "ls".
6956 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6957 __isl_take isl_local_space *ls)
6959 int i;
6961 map = isl_map_cow(map);
6962 if (!map || !ls)
6963 goto error;
6965 for (i = 0; i < map->n; ++i) {
6966 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6967 isl_local_space_copy(ls));
6968 if (!map->p[i])
6969 goto error;
6971 isl_space_free(map->dim);
6972 map->dim = isl_local_space_get_space(ls);
6973 if (!map->dim)
6974 goto error;
6976 isl_local_space_free(ls);
6977 return map;
6978 error:
6979 isl_local_space_free(ls);
6980 isl_map_free(map);
6981 return NULL;
6984 /* Compute an explicit representation for the existentially
6985 * quantified variables for which do not know any explicit representation yet.
6987 * We first sort the existentially quantified variables so that the
6988 * existentially quantified variables for which we already have an explicit
6989 * representation are placed before those for which we do not.
6990 * The input dimensions, the output dimensions and the existentially
6991 * quantified variables for which we already have an explicit
6992 * representation are then turned into parameters.
6993 * compute_divs returns a map with the same parameters and
6994 * no input or output dimensions and the dimension specification
6995 * is reset to that of the input, including the existentially quantified
6996 * variables for which we already had an explicit representation.
6998 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7000 struct isl_basic_set *bset;
7001 struct isl_set *set;
7002 struct isl_map *map;
7003 isl_space *dim;
7004 isl_local_space *ls;
7005 unsigned nparam;
7006 unsigned n_in;
7007 unsigned n_out;
7008 int n_known;
7009 int i;
7011 bmap = isl_basic_map_sort_divs(bmap);
7012 bmap = isl_basic_map_cow(bmap);
7013 if (!bmap)
7014 return NULL;
7016 n_known = isl_basic_map_first_unknown_div(bmap);
7017 if (n_known < 0)
7018 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7020 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7021 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7022 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7023 dim = isl_space_set_alloc(bmap->ctx,
7024 nparam + n_in + n_out + n_known, 0);
7025 if (!dim)
7026 goto error;
7028 ls = isl_basic_map_get_local_space(bmap);
7029 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7030 n_known, bmap->n_div - n_known);
7031 if (n_known > 0) {
7032 for (i = n_known; i < bmap->n_div; ++i)
7033 swap_div(bmap, i - n_known, i);
7034 bmap->n_div -= n_known;
7035 bmap->extra -= n_known;
7037 bmap = isl_basic_map_reset_space(bmap, dim);
7038 bset = bset_from_bmap(bmap);
7040 set = parameter_compute_divs(bset);
7041 map = set_to_map(set);
7042 map = replace_space_by_local_space(map, ls);
7044 return map;
7045 error:
7046 isl_basic_map_free(bmap);
7047 return NULL;
7050 /* Remove the explicit representation of local variable "div",
7051 * if there is any.
7053 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7054 __isl_take isl_basic_map *bmap, int div)
7056 isl_bool unknown;
7058 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7059 if (unknown < 0)
7060 return isl_basic_map_free(bmap);
7061 if (unknown)
7062 return bmap;
7064 bmap = isl_basic_map_cow(bmap);
7065 if (!bmap)
7066 return NULL;
7067 isl_int_set_si(bmap->div[div][0], 0);
7068 return bmap;
7071 /* Is local variable "div" of "bmap" marked as not having an explicit
7072 * representation?
7073 * Note that even if "div" is not marked in this way and therefore
7074 * has an explicit representation, this representation may still
7075 * depend (indirectly) on other local variables that do not
7076 * have an explicit representation.
7078 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7079 int div)
7081 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7082 return isl_bool_error;
7083 return isl_int_is_zero(bmap->div[div][0]);
7086 /* Return the position of the first local variable that does not
7087 * have an explicit representation.
7088 * Return the total number of local variables if they all have
7089 * an explicit representation.
7090 * Return -1 on error.
7092 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7094 int i;
7096 if (!bmap)
7097 return -1;
7099 for (i = 0; i < bmap->n_div; ++i) {
7100 if (!isl_basic_map_div_is_known(bmap, i))
7101 return i;
7103 return bmap->n_div;
7106 /* Return the position of the first local variable that does not
7107 * have an explicit representation.
7108 * Return the total number of local variables if they all have
7109 * an explicit representation.
7110 * Return -1 on error.
7112 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7114 return isl_basic_map_first_unknown_div(bset);
7117 /* Does "bmap" have an explicit representation for all local variables?
7119 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7121 int first, n;
7123 n = isl_basic_map_dim(bmap, isl_dim_div);
7124 first = isl_basic_map_first_unknown_div(bmap);
7125 if (first < 0)
7126 return isl_bool_error;
7127 return first == n;
7130 /* Do all basic maps in "map" have an explicit representation
7131 * for all local variables?
7133 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7135 int i;
7137 if (!map)
7138 return isl_bool_error;
7140 for (i = 0; i < map->n; ++i) {
7141 int known = isl_basic_map_divs_known(map->p[i]);
7142 if (known <= 0)
7143 return known;
7146 return isl_bool_true;
7149 /* If bmap contains any unknown divs, then compute explicit
7150 * expressions for them. However, this computation may be
7151 * quite expensive, so first try to remove divs that aren't
7152 * strictly needed.
7154 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7156 int known;
7157 struct isl_map *map;
7159 known = isl_basic_map_divs_known(bmap);
7160 if (known < 0)
7161 goto error;
7162 if (known)
7163 return isl_map_from_basic_map(bmap);
7165 bmap = isl_basic_map_drop_redundant_divs(bmap);
7167 known = isl_basic_map_divs_known(bmap);
7168 if (known < 0)
7169 goto error;
7170 if (known)
7171 return isl_map_from_basic_map(bmap);
7173 map = compute_divs(bmap);
7174 return map;
7175 error:
7176 isl_basic_map_free(bmap);
7177 return NULL;
7180 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7182 int i;
7183 int known;
7184 struct isl_map *res;
7186 if (!map)
7187 return NULL;
7188 if (map->n == 0)
7189 return map;
7191 known = isl_map_divs_known(map);
7192 if (known < 0) {
7193 isl_map_free(map);
7194 return NULL;
7196 if (known)
7197 return map;
7199 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7200 for (i = 1 ; i < map->n; ++i) {
7201 struct isl_map *r2;
7202 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7203 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7204 res = isl_map_union_disjoint(res, r2);
7205 else
7206 res = isl_map_union(res, r2);
7208 isl_map_free(map);
7210 return res;
7213 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7215 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7218 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7220 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7223 struct isl_set *isl_map_domain(struct isl_map *map)
7225 int i;
7226 struct isl_set *set;
7228 if (!map)
7229 goto error;
7231 map = isl_map_cow(map);
7232 if (!map)
7233 return NULL;
7235 set = set_from_map(map);
7236 set->dim = isl_space_domain(set->dim);
7237 if (!set->dim)
7238 goto error;
7239 for (i = 0; i < map->n; ++i) {
7240 set->p[i] = isl_basic_map_domain(map->p[i]);
7241 if (!set->p[i])
7242 goto error;
7244 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7245 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7246 return set;
7247 error:
7248 isl_map_free(map);
7249 return NULL;
7252 /* Return the union of "map1" and "map2", where we assume for now that
7253 * "map1" and "map2" are disjoint. Note that the basic maps inside
7254 * "map1" or "map2" may not be disjoint from each other.
7255 * Also note that this function is also called from isl_map_union,
7256 * which takes care of handling the situation where "map1" and "map2"
7257 * may not be disjoint.
7259 * If one of the inputs is empty, we can simply return the other input.
7260 * Similarly, if one of the inputs is universal, then it is equal to the union.
7262 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7263 __isl_take isl_map *map2)
7265 int i;
7266 unsigned flags = 0;
7267 struct isl_map *map = NULL;
7268 int is_universe;
7270 if (!map1 || !map2)
7271 goto error;
7273 if (!isl_space_is_equal(map1->dim, map2->dim))
7274 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7275 "spaces don't match", goto error);
7277 if (map1->n == 0) {
7278 isl_map_free(map1);
7279 return map2;
7281 if (map2->n == 0) {
7282 isl_map_free(map2);
7283 return map1;
7286 is_universe = isl_map_plain_is_universe(map1);
7287 if (is_universe < 0)
7288 goto error;
7289 if (is_universe) {
7290 isl_map_free(map2);
7291 return map1;
7294 is_universe = isl_map_plain_is_universe(map2);
7295 if (is_universe < 0)
7296 goto error;
7297 if (is_universe) {
7298 isl_map_free(map1);
7299 return map2;
7302 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7303 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7304 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7306 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7307 map1->n + map2->n, flags);
7308 if (!map)
7309 goto error;
7310 for (i = 0; i < map1->n; ++i) {
7311 map = isl_map_add_basic_map(map,
7312 isl_basic_map_copy(map1->p[i]));
7313 if (!map)
7314 goto error;
7316 for (i = 0; i < map2->n; ++i) {
7317 map = isl_map_add_basic_map(map,
7318 isl_basic_map_copy(map2->p[i]));
7319 if (!map)
7320 goto error;
7322 isl_map_free(map1);
7323 isl_map_free(map2);
7324 return map;
7325 error:
7326 isl_map_free(map);
7327 isl_map_free(map1);
7328 isl_map_free(map2);
7329 return NULL;
7332 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7333 * guaranteed to be disjoint by the caller.
7335 * Note that this functions is called from within isl_map_make_disjoint,
7336 * so we have to be careful not to touch the constraints of the inputs
7337 * in any way.
7339 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7340 __isl_take isl_map *map2)
7342 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7345 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7346 * not be disjoint. The parameters are assumed to have been aligned.
7348 * We currently simply call map_union_disjoint, the internal operation
7349 * of which does not really depend on the inputs being disjoint.
7350 * If the result contains more than one basic map, then we clear
7351 * the disjoint flag since the result may contain basic maps from
7352 * both inputs and these are not guaranteed to be disjoint.
7354 * As a special case, if "map1" and "map2" are obviously equal,
7355 * then we simply return "map1".
7357 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7358 __isl_take isl_map *map2)
7360 int equal;
7362 if (!map1 || !map2)
7363 goto error;
7365 equal = isl_map_plain_is_equal(map1, map2);
7366 if (equal < 0)
7367 goto error;
7368 if (equal) {
7369 isl_map_free(map2);
7370 return map1;
7373 map1 = map_union_disjoint(map1, map2);
7374 if (!map1)
7375 return NULL;
7376 if (map1->n > 1)
7377 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7378 return map1;
7379 error:
7380 isl_map_free(map1);
7381 isl_map_free(map2);
7382 return NULL;
7385 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7386 * not be disjoint.
7388 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7389 __isl_take isl_map *map2)
7391 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7394 struct isl_set *isl_set_union_disjoint(
7395 struct isl_set *set1, struct isl_set *set2)
7397 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7398 set_to_map(set2)));
7401 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7403 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7406 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7407 * the results.
7409 * "map" and "set" are assumed to be compatible and non-NULL.
7411 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7412 __isl_take isl_set *set,
7413 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7414 __isl_take isl_basic_set *bset))
7416 unsigned flags = 0;
7417 struct isl_map *result;
7418 int i, j;
7420 if (isl_set_plain_is_universe(set)) {
7421 isl_set_free(set);
7422 return map;
7425 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7426 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7427 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7429 result = isl_map_alloc_space(isl_space_copy(map->dim),
7430 map->n * set->n, flags);
7431 for (i = 0; result && i < map->n; ++i)
7432 for (j = 0; j < set->n; ++j) {
7433 result = isl_map_add_basic_map(result,
7434 fn(isl_basic_map_copy(map->p[i]),
7435 isl_basic_set_copy(set->p[j])));
7436 if (!result)
7437 break;
7440 isl_map_free(map);
7441 isl_set_free(set);
7442 return result;
7445 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7446 __isl_take isl_set *set)
7448 isl_bool ok;
7450 ok = isl_map_compatible_range(map, set);
7451 if (ok < 0)
7452 goto error;
7453 if (!ok)
7454 isl_die(set->ctx, isl_error_invalid,
7455 "incompatible spaces", goto error);
7457 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7458 error:
7459 isl_map_free(map);
7460 isl_set_free(set);
7461 return NULL;
7464 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7465 __isl_take isl_set *set)
7467 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7470 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7471 __isl_take isl_set *set)
7473 isl_bool ok;
7475 ok = isl_map_compatible_domain(map, set);
7476 if (ok < 0)
7477 goto error;
7478 if (!ok)
7479 isl_die(set->ctx, isl_error_invalid,
7480 "incompatible spaces", goto error);
7482 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7483 error:
7484 isl_map_free(map);
7485 isl_set_free(set);
7486 return NULL;
7489 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7490 __isl_take isl_set *set)
7492 return isl_map_align_params_map_map_and(map, set,
7493 &map_intersect_domain);
7496 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7497 __isl_take isl_map *map2)
7499 if (!map1 || !map2)
7500 goto error;
7501 map1 = isl_map_reverse(map1);
7502 map1 = isl_map_apply_range(map1, map2);
7503 return isl_map_reverse(map1);
7504 error:
7505 isl_map_free(map1);
7506 isl_map_free(map2);
7507 return NULL;
7510 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7511 __isl_take isl_map *map2)
7513 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7516 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7517 __isl_take isl_map *map2)
7519 isl_space *dim_result;
7520 struct isl_map *result;
7521 int i, j;
7523 if (!map1 || !map2)
7524 goto error;
7526 dim_result = isl_space_join(isl_space_copy(map1->dim),
7527 isl_space_copy(map2->dim));
7529 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7530 if (!result)
7531 goto error;
7532 for (i = 0; i < map1->n; ++i)
7533 for (j = 0; j < map2->n; ++j) {
7534 result = isl_map_add_basic_map(result,
7535 isl_basic_map_apply_range(
7536 isl_basic_map_copy(map1->p[i]),
7537 isl_basic_map_copy(map2->p[j])));
7538 if (!result)
7539 goto error;
7541 isl_map_free(map1);
7542 isl_map_free(map2);
7543 if (result && result->n <= 1)
7544 ISL_F_SET(result, ISL_MAP_DISJOINT);
7545 return result;
7546 error:
7547 isl_map_free(map1);
7548 isl_map_free(map2);
7549 return NULL;
7552 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7553 __isl_take isl_map *map2)
7555 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7559 * returns range - domain
7561 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7563 isl_space *target_space;
7564 struct isl_basic_set *bset;
7565 unsigned dim;
7566 unsigned nparam;
7567 int i;
7569 if (!bmap)
7570 goto error;
7571 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7572 bmap->dim, isl_dim_out),
7573 goto error);
7574 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7575 dim = isl_basic_map_n_in(bmap);
7576 nparam = isl_basic_map_n_param(bmap);
7577 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7578 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7579 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7580 for (i = 0; i < dim; ++i) {
7581 int j = isl_basic_map_alloc_equality(bmap);
7582 if (j < 0) {
7583 bmap = isl_basic_map_free(bmap);
7584 break;
7586 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7587 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7588 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7589 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7591 bset = isl_basic_map_domain(bmap);
7592 bset = isl_basic_set_reset_space(bset, target_space);
7593 return bset;
7594 error:
7595 isl_basic_map_free(bmap);
7596 return NULL;
7600 * returns range - domain
7602 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7604 int i;
7605 isl_space *dim;
7606 struct isl_set *result;
7608 if (!map)
7609 return NULL;
7611 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7612 map->dim, isl_dim_out),
7613 goto error);
7614 dim = isl_map_get_space(map);
7615 dim = isl_space_domain(dim);
7616 result = isl_set_alloc_space(dim, map->n, 0);
7617 if (!result)
7618 goto error;
7619 for (i = 0; i < map->n; ++i)
7620 result = isl_set_add_basic_set(result,
7621 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7622 isl_map_free(map);
7623 return result;
7624 error:
7625 isl_map_free(map);
7626 return NULL;
7630 * returns [domain -> range] -> range - domain
7632 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7633 __isl_take isl_basic_map *bmap)
7635 int i, k;
7636 isl_space *dim;
7637 isl_basic_map *domain;
7638 int nparam, n;
7639 unsigned total;
7641 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7642 bmap->dim, isl_dim_out))
7643 isl_die(bmap->ctx, isl_error_invalid,
7644 "domain and range don't match", goto error);
7646 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7647 n = isl_basic_map_dim(bmap, isl_dim_in);
7649 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7650 domain = isl_basic_map_universe(dim);
7652 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7653 bmap = isl_basic_map_apply_range(bmap, domain);
7654 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7656 total = isl_basic_map_total_dim(bmap);
7658 for (i = 0; i < n; ++i) {
7659 k = isl_basic_map_alloc_equality(bmap);
7660 if (k < 0)
7661 goto error;
7662 isl_seq_clr(bmap->eq[k], 1 + total);
7663 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7664 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7665 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7668 bmap = isl_basic_map_gauss(bmap, NULL);
7669 return isl_basic_map_finalize(bmap);
7670 error:
7671 isl_basic_map_free(bmap);
7672 return NULL;
7676 * returns [domain -> range] -> range - domain
7678 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7680 int i;
7681 isl_space *domain_dim;
7683 if (!map)
7684 return NULL;
7686 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7687 map->dim, isl_dim_out))
7688 isl_die(map->ctx, isl_error_invalid,
7689 "domain and range don't match", goto error);
7691 map = isl_map_cow(map);
7692 if (!map)
7693 return NULL;
7695 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7696 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7697 map->dim = isl_space_join(map->dim, domain_dim);
7698 if (!map->dim)
7699 goto error;
7700 for (i = 0; i < map->n; ++i) {
7701 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7702 if (!map->p[i])
7703 goto error;
7705 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7706 return map;
7707 error:
7708 isl_map_free(map);
7709 return NULL;
7712 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7714 struct isl_basic_map *bmap;
7715 unsigned nparam;
7716 unsigned dim;
7717 int i;
7719 if (!dims)
7720 return NULL;
7722 nparam = dims->nparam;
7723 dim = dims->n_out;
7724 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7725 if (!bmap)
7726 goto error;
7728 for (i = 0; i < dim; ++i) {
7729 int j = isl_basic_map_alloc_equality(bmap);
7730 if (j < 0)
7731 goto error;
7732 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7733 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7734 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7736 return isl_basic_map_finalize(bmap);
7737 error:
7738 isl_basic_map_free(bmap);
7739 return NULL;
7742 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7744 if (!dim)
7745 return NULL;
7746 if (dim->n_in != dim->n_out)
7747 isl_die(dim->ctx, isl_error_invalid,
7748 "number of input and output dimensions needs to be "
7749 "the same", goto error);
7750 return basic_map_identity(dim);
7751 error:
7752 isl_space_free(dim);
7753 return NULL;
7756 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7758 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7761 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7763 isl_space *dim = isl_set_get_space(set);
7764 isl_map *id;
7765 id = isl_map_identity(isl_space_map_from_set(dim));
7766 return isl_map_intersect_range(id, set);
7769 /* Construct a basic set with all set dimensions having only non-negative
7770 * values.
7772 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7773 __isl_take isl_space *space)
7775 int i;
7776 unsigned nparam;
7777 unsigned dim;
7778 struct isl_basic_set *bset;
7780 if (!space)
7781 return NULL;
7782 nparam = space->nparam;
7783 dim = space->n_out;
7784 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7785 if (!bset)
7786 return NULL;
7787 for (i = 0; i < dim; ++i) {
7788 int k = isl_basic_set_alloc_inequality(bset);
7789 if (k < 0)
7790 goto error;
7791 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7792 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7794 return bset;
7795 error:
7796 isl_basic_set_free(bset);
7797 return NULL;
7800 /* Construct the half-space x_pos >= 0.
7802 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7803 int pos)
7805 int k;
7806 isl_basic_set *nonneg;
7808 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7809 k = isl_basic_set_alloc_inequality(nonneg);
7810 if (k < 0)
7811 goto error;
7812 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7813 isl_int_set_si(nonneg->ineq[k][pos], 1);
7815 return isl_basic_set_finalize(nonneg);
7816 error:
7817 isl_basic_set_free(nonneg);
7818 return NULL;
7821 /* Construct the half-space x_pos <= -1.
7823 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7825 int k;
7826 isl_basic_set *neg;
7828 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7829 k = isl_basic_set_alloc_inequality(neg);
7830 if (k < 0)
7831 goto error;
7832 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7833 isl_int_set_si(neg->ineq[k][0], -1);
7834 isl_int_set_si(neg->ineq[k][pos], -1);
7836 return isl_basic_set_finalize(neg);
7837 error:
7838 isl_basic_set_free(neg);
7839 return NULL;
7842 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7843 enum isl_dim_type type, unsigned first, unsigned n)
7845 int i;
7846 unsigned offset;
7847 isl_basic_set *nonneg;
7848 isl_basic_set *neg;
7850 if (!set)
7851 return NULL;
7852 if (n == 0)
7853 return set;
7855 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7857 offset = pos(set->dim, type);
7858 for (i = 0; i < n; ++i) {
7859 nonneg = nonneg_halfspace(isl_set_get_space(set),
7860 offset + first + i);
7861 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7863 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7866 return set;
7867 error:
7868 isl_set_free(set);
7869 return NULL;
7872 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7873 int len,
7874 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7875 void *user)
7877 isl_set *half;
7879 if (!set)
7880 return isl_stat_error;
7881 if (isl_set_plain_is_empty(set)) {
7882 isl_set_free(set);
7883 return isl_stat_ok;
7885 if (first == len)
7886 return fn(set, signs, user);
7888 signs[first] = 1;
7889 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7890 1 + first));
7891 half = isl_set_intersect(half, isl_set_copy(set));
7892 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7893 goto error;
7895 signs[first] = -1;
7896 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7897 1 + first));
7898 half = isl_set_intersect(half, set);
7899 return foreach_orthant(half, signs, first + 1, len, fn, user);
7900 error:
7901 isl_set_free(set);
7902 return isl_stat_error;
7905 /* Call "fn" on the intersections of "set" with each of the orthants
7906 * (except for obviously empty intersections). The orthant is identified
7907 * by the signs array, with each entry having value 1 or -1 according
7908 * to the sign of the corresponding variable.
7910 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
7911 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7912 void *user)
7914 unsigned nparam;
7915 unsigned nvar;
7916 int *signs;
7917 isl_stat r;
7919 if (!set)
7920 return isl_stat_error;
7921 if (isl_set_plain_is_empty(set))
7922 return isl_stat_ok;
7924 nparam = isl_set_dim(set, isl_dim_param);
7925 nvar = isl_set_dim(set, isl_dim_set);
7927 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7929 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7930 fn, user);
7932 free(signs);
7934 return r;
7937 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7939 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7942 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7943 __isl_keep isl_basic_map *bmap2)
7945 int is_subset;
7946 struct isl_map *map1;
7947 struct isl_map *map2;
7949 if (!bmap1 || !bmap2)
7950 return isl_bool_error;
7952 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7953 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7955 is_subset = isl_map_is_subset(map1, map2);
7957 isl_map_free(map1);
7958 isl_map_free(map2);
7960 return is_subset;
7963 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7964 __isl_keep isl_basic_set *bset2)
7966 return isl_basic_map_is_subset(bset1, bset2);
7969 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7970 __isl_keep isl_basic_map *bmap2)
7972 isl_bool is_subset;
7974 if (!bmap1 || !bmap2)
7975 return isl_bool_error;
7976 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7977 if (is_subset != isl_bool_true)
7978 return is_subset;
7979 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7980 return is_subset;
7983 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7984 __isl_keep isl_basic_set *bset2)
7986 return isl_basic_map_is_equal(
7987 bset_to_bmap(bset1), bset_to_bmap(bset2));
7990 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7992 int i;
7993 int is_empty;
7995 if (!map)
7996 return isl_bool_error;
7997 for (i = 0; i < map->n; ++i) {
7998 is_empty = isl_basic_map_is_empty(map->p[i]);
7999 if (is_empty < 0)
8000 return isl_bool_error;
8001 if (!is_empty)
8002 return isl_bool_false;
8004 return isl_bool_true;
8007 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8009 return map ? map->n == 0 : isl_bool_error;
8012 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8014 return set ? set->n == 0 : isl_bool_error;
8017 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8019 return isl_map_is_empty(set_to_map(set));
8022 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8023 __isl_keep isl_map *map2)
8025 if (!map1 || !map2)
8026 return isl_bool_error;
8028 return isl_space_is_equal(map1->dim, map2->dim);
8031 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8032 __isl_keep isl_set *set2)
8034 if (!set1 || !set2)
8035 return isl_bool_error;
8037 return isl_space_is_equal(set1->dim, set2->dim);
8040 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8042 isl_bool is_subset;
8044 if (!map1 || !map2)
8045 return isl_bool_error;
8046 is_subset = isl_map_is_subset(map1, map2);
8047 if (is_subset != isl_bool_true)
8048 return is_subset;
8049 is_subset = isl_map_is_subset(map2, map1);
8050 return is_subset;
8053 /* Is "map1" equal to "map2"?
8055 * First check if they are obviously equal.
8056 * If not, then perform a more detailed analysis.
8058 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8060 isl_bool equal;
8062 equal = isl_map_plain_is_equal(map1, map2);
8063 if (equal < 0 || equal)
8064 return equal;
8065 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8068 isl_bool isl_basic_map_is_strict_subset(
8069 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8071 isl_bool is_subset;
8073 if (!bmap1 || !bmap2)
8074 return isl_bool_error;
8075 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8076 if (is_subset != isl_bool_true)
8077 return is_subset;
8078 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8079 if (is_subset == isl_bool_error)
8080 return is_subset;
8081 return !is_subset;
8084 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8085 __isl_keep isl_map *map2)
8087 isl_bool is_subset;
8089 if (!map1 || !map2)
8090 return isl_bool_error;
8091 is_subset = isl_map_is_subset(map1, map2);
8092 if (is_subset != isl_bool_true)
8093 return is_subset;
8094 is_subset = isl_map_is_subset(map2, map1);
8095 if (is_subset == isl_bool_error)
8096 return is_subset;
8097 return !is_subset;
8100 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8101 __isl_keep isl_set *set2)
8103 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8106 /* Is "bmap" obviously equal to the universe with the same space?
8108 * That is, does it not have any constraints?
8110 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8112 if (!bmap)
8113 return isl_bool_error;
8114 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8117 /* Is "bset" obviously equal to the universe with the same space?
8119 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8121 return isl_basic_map_plain_is_universe(bset);
8124 /* If "c" does not involve any existentially quantified variables,
8125 * then set *univ to false and abort
8127 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8129 isl_bool *univ = user;
8130 unsigned n;
8132 n = isl_constraint_dim(c, isl_dim_div);
8133 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8134 isl_constraint_free(c);
8135 if (*univ < 0 || !*univ)
8136 return isl_stat_error;
8137 return isl_stat_ok;
8140 /* Is "bmap" equal to the universe with the same space?
8142 * First check if it is obviously equal to the universe.
8143 * If not and if there are any constraints not involving
8144 * existentially quantified variables, then it is certainly
8145 * not equal to the universe.
8146 * Otherwise, check if the universe is a subset of "bmap".
8148 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8150 isl_bool univ;
8151 isl_basic_map *test;
8153 univ = isl_basic_map_plain_is_universe(bmap);
8154 if (univ < 0 || univ)
8155 return univ;
8156 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8157 return isl_bool_false;
8158 univ = isl_bool_true;
8159 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8160 univ)
8161 return isl_bool_error;
8162 if (univ < 0 || !univ)
8163 return univ;
8164 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8165 univ = isl_basic_map_is_subset(test, bmap);
8166 isl_basic_map_free(test);
8167 return univ;
8170 /* Is "bset" equal to the universe with the same space?
8172 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8174 return isl_basic_map_is_universe(bset);
8177 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8179 int i;
8181 if (!map)
8182 return isl_bool_error;
8184 for (i = 0; i < map->n; ++i) {
8185 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8186 if (r < 0 || r)
8187 return r;
8190 return isl_bool_false;
8193 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8195 return isl_map_plain_is_universe(set_to_map(set));
8198 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8200 struct isl_basic_set *bset = NULL;
8201 struct isl_vec *sample = NULL;
8202 isl_bool empty, non_empty;
8204 if (!bmap)
8205 return isl_bool_error;
8207 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8208 return isl_bool_true;
8210 if (isl_basic_map_plain_is_universe(bmap))
8211 return isl_bool_false;
8213 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8214 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8215 copy = isl_basic_map_remove_redundancies(copy);
8216 empty = isl_basic_map_plain_is_empty(copy);
8217 isl_basic_map_free(copy);
8218 return empty;
8221 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8222 if (non_empty < 0)
8223 return isl_bool_error;
8224 if (non_empty)
8225 return isl_bool_false;
8226 isl_vec_free(bmap->sample);
8227 bmap->sample = NULL;
8228 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8229 if (!bset)
8230 return isl_bool_error;
8231 sample = isl_basic_set_sample_vec(bset);
8232 if (!sample)
8233 return isl_bool_error;
8234 empty = sample->size == 0;
8235 isl_vec_free(bmap->sample);
8236 bmap->sample = sample;
8237 if (empty)
8238 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8240 return empty;
8243 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8245 if (!bmap)
8246 return isl_bool_error;
8247 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8250 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8252 if (!bset)
8253 return isl_bool_error;
8254 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8257 /* Is "bmap" known to be non-empty?
8259 * That is, is the cached sample still valid?
8261 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8263 unsigned total;
8265 if (!bmap)
8266 return isl_bool_error;
8267 if (!bmap->sample)
8268 return isl_bool_false;
8269 total = 1 + isl_basic_map_total_dim(bmap);
8270 if (bmap->sample->size != total)
8271 return isl_bool_false;
8272 return isl_basic_map_contains(bmap, bmap->sample);
8275 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8277 return isl_basic_map_is_empty(bset_to_bmap(bset));
8280 struct isl_map *isl_basic_map_union(
8281 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8283 struct isl_map *map;
8284 if (!bmap1 || !bmap2)
8285 goto error;
8287 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8289 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8290 if (!map)
8291 goto error;
8292 map = isl_map_add_basic_map(map, bmap1);
8293 map = isl_map_add_basic_map(map, bmap2);
8294 return map;
8295 error:
8296 isl_basic_map_free(bmap1);
8297 isl_basic_map_free(bmap2);
8298 return NULL;
8301 struct isl_set *isl_basic_set_union(
8302 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8304 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8305 bset_to_bmap(bset2)));
8308 /* Order divs such that any div only depends on previous divs */
8309 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8311 int i;
8312 unsigned off;
8314 if (!bmap)
8315 return NULL;
8317 off = isl_space_dim(bmap->dim, isl_dim_all);
8319 for (i = 0; i < bmap->n_div; ++i) {
8320 int pos;
8321 if (isl_int_is_zero(bmap->div[i][0]))
8322 continue;
8323 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8324 bmap->n_div-i);
8325 if (pos == -1)
8326 continue;
8327 if (pos == 0)
8328 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8329 "integer division depends on itself",
8330 return isl_basic_map_free(bmap));
8331 isl_basic_map_swap_div(bmap, i, i + pos);
8332 --i;
8334 return bmap;
8337 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8339 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8342 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8344 int i;
8346 if (!map)
8347 return 0;
8349 for (i = 0; i < map->n; ++i) {
8350 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8351 if (!map->p[i])
8352 goto error;
8355 return map;
8356 error:
8357 isl_map_free(map);
8358 return NULL;
8361 /* Sort the local variables of "bset".
8363 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8364 __isl_take isl_basic_set *bset)
8366 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8369 /* Apply the expansion computed by isl_merge_divs.
8370 * The expansion itself is given by "exp" while the resulting
8371 * list of divs is given by "div".
8373 * Move the integer divisions of "bmap" into the right position
8374 * according to "exp" and then introduce the additional integer
8375 * divisions, adding div constraints.
8376 * The moving should be done first to avoid moving coefficients
8377 * in the definitions of the extra integer divisions.
8379 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8380 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8382 int i, j;
8383 int n_div;
8385 bmap = isl_basic_map_cow(bmap);
8386 if (!bmap || !div)
8387 goto error;
8389 if (div->n_row < bmap->n_div)
8390 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8391 "not an expansion", goto error);
8393 n_div = bmap->n_div;
8394 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8395 div->n_row - n_div, 0,
8396 2 * (div->n_row - n_div));
8398 for (i = n_div; i < div->n_row; ++i)
8399 if (isl_basic_map_alloc_div(bmap) < 0)
8400 goto error;
8402 for (j = n_div - 1; j >= 0; --j) {
8403 if (exp[j] == j)
8404 break;
8405 isl_basic_map_swap_div(bmap, j, exp[j]);
8407 j = 0;
8408 for (i = 0; i < div->n_row; ++i) {
8409 if (j < n_div && exp[j] == i) {
8410 j++;
8411 } else {
8412 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8413 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8414 continue;
8415 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8416 goto error;
8420 isl_mat_free(div);
8421 return bmap;
8422 error:
8423 isl_basic_map_free(bmap);
8424 isl_mat_free(div);
8425 return NULL;
8428 /* Apply the expansion computed by isl_merge_divs.
8429 * The expansion itself is given by "exp" while the resulting
8430 * list of divs is given by "div".
8432 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8433 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8435 return isl_basic_map_expand_divs(bset, div, exp);
8438 /* Look for a div in dst that corresponds to the div "div" in src.
8439 * The divs before "div" in src and dst are assumed to be the same.
8441 * Returns -1 if no corresponding div was found and the position
8442 * of the corresponding div in dst otherwise.
8444 static int find_div(struct isl_basic_map *dst,
8445 struct isl_basic_map *src, unsigned div)
8447 int i;
8449 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8451 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8452 for (i = div; i < dst->n_div; ++i)
8453 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8454 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8455 dst->n_div - div) == -1)
8456 return i;
8457 return -1;
8460 /* Align the divs of "dst" to those of "src", adding divs from "src"
8461 * if needed. That is, make sure that the first src->n_div divs
8462 * of the result are equal to those of src.
8464 * The result is not finalized as by design it will have redundant
8465 * divs if any divs from "src" were copied.
8467 __isl_give isl_basic_map *isl_basic_map_align_divs(
8468 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8470 int i;
8471 int known, extended;
8472 unsigned total;
8474 if (!dst || !src)
8475 return isl_basic_map_free(dst);
8477 if (src->n_div == 0)
8478 return dst;
8480 known = isl_basic_map_divs_known(src);
8481 if (known < 0)
8482 return isl_basic_map_free(dst);
8483 if (!known)
8484 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8485 "some src divs are unknown",
8486 return isl_basic_map_free(dst));
8488 src = isl_basic_map_order_divs(src);
8490 extended = 0;
8491 total = isl_space_dim(src->dim, isl_dim_all);
8492 for (i = 0; i < src->n_div; ++i) {
8493 int j = find_div(dst, src, i);
8494 if (j < 0) {
8495 if (!extended) {
8496 int extra = src->n_div - i;
8497 dst = isl_basic_map_cow(dst);
8498 if (!dst)
8499 return NULL;
8500 dst = isl_basic_map_extend_space(dst,
8501 isl_space_copy(dst->dim),
8502 extra, 0, 2 * extra);
8503 extended = 1;
8505 j = isl_basic_map_alloc_div(dst);
8506 if (j < 0)
8507 return isl_basic_map_free(dst);
8508 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8509 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8510 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8511 return isl_basic_map_free(dst);
8513 if (j != i)
8514 isl_basic_map_swap_div(dst, i, j);
8516 return dst;
8519 struct isl_map *isl_map_align_divs(struct isl_map *map)
8521 int i;
8523 if (!map)
8524 return NULL;
8525 if (map->n == 0)
8526 return map;
8527 map = isl_map_compute_divs(map);
8528 map = isl_map_cow(map);
8529 if (!map)
8530 return NULL;
8532 for (i = 1; i < map->n; ++i)
8533 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8534 for (i = 1; i < map->n; ++i) {
8535 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8536 if (!map->p[i])
8537 return isl_map_free(map);
8540 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8541 return map;
8544 struct isl_set *isl_set_align_divs(struct isl_set *set)
8546 return set_from_map(isl_map_align_divs(set_to_map(set)));
8549 /* Align the divs of the basic maps in "map" to those
8550 * of the basic maps in "list", as well as to the other basic maps in "map".
8551 * The elements in "list" are assumed to have known divs.
8553 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8554 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8556 int i, n;
8558 map = isl_map_compute_divs(map);
8559 map = isl_map_cow(map);
8560 if (!map || !list)
8561 return isl_map_free(map);
8562 if (map->n == 0)
8563 return map;
8565 n = isl_basic_map_list_n_basic_map(list);
8566 for (i = 0; i < n; ++i) {
8567 isl_basic_map *bmap;
8569 bmap = isl_basic_map_list_get_basic_map(list, i);
8570 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8571 isl_basic_map_free(bmap);
8573 if (!map->p[0])
8574 return isl_map_free(map);
8576 return isl_map_align_divs(map);
8579 /* Align the divs of each element of "list" to those of "bmap".
8580 * Both "bmap" and the elements of "list" are assumed to have known divs.
8582 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8583 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8585 int i, n;
8587 if (!list || !bmap)
8588 return isl_basic_map_list_free(list);
8590 n = isl_basic_map_list_n_basic_map(list);
8591 for (i = 0; i < n; ++i) {
8592 isl_basic_map *bmap_i;
8594 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8595 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8596 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8599 return list;
8602 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8603 __isl_take isl_map *map)
8605 isl_bool ok;
8607 ok = isl_map_compatible_domain(map, set);
8608 if (ok < 0)
8609 goto error;
8610 if (!ok)
8611 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8612 "incompatible spaces", goto error);
8613 map = isl_map_intersect_domain(map, set);
8614 set = isl_map_range(map);
8615 return set;
8616 error:
8617 isl_set_free(set);
8618 isl_map_free(map);
8619 return NULL;
8622 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8623 __isl_take isl_map *map)
8625 return isl_map_align_params_map_map_and(set, map, &set_apply);
8628 /* There is no need to cow as removing empty parts doesn't change
8629 * the meaning of the set.
8631 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8633 int i;
8635 if (!map)
8636 return NULL;
8638 for (i = map->n - 1; i >= 0; --i)
8639 remove_if_empty(map, i);
8641 return map;
8644 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8646 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8649 static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
8651 struct isl_basic_map *bmap;
8652 if (!map || map->n == 0)
8653 return NULL;
8654 bmap = map->p[map->n-1];
8655 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8656 return isl_basic_map_copy(bmap);
8659 __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
8661 return map_copy_basic_map(map);
8664 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8666 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8669 static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
8670 __isl_keep isl_basic_map *bmap)
8672 int i;
8674 if (!map || !bmap)
8675 goto error;
8676 for (i = map->n-1; i >= 0; --i) {
8677 if (map->p[i] != bmap)
8678 continue;
8679 map = isl_map_cow(map);
8680 if (!map)
8681 goto error;
8682 isl_basic_map_free(map->p[i]);
8683 if (i != map->n-1) {
8684 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8685 map->p[i] = map->p[map->n-1];
8687 map->n--;
8688 return map;
8690 return map;
8691 error:
8692 isl_map_free(map);
8693 return NULL;
8696 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8697 __isl_keep isl_basic_map *bmap)
8699 return map_drop_basic_map(map, bmap);
8702 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8703 struct isl_basic_set *bset)
8705 return set_from_map(map_drop_basic_map(set_to_map(set),
8706 bset_to_bmap(bset)));
8709 /* Given two basic sets bset1 and bset2, compute the maximal difference
8710 * between the values of dimension pos in bset1 and those in bset2
8711 * for any common value of the parameters and dimensions preceding pos.
8713 static enum isl_lp_result basic_set_maximal_difference_at(
8714 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8715 int pos, isl_int *opt)
8717 isl_basic_map *bmap1;
8718 isl_basic_map *bmap2;
8719 struct isl_ctx *ctx;
8720 struct isl_vec *obj;
8721 unsigned total;
8722 unsigned nparam;
8723 unsigned dim1;
8724 enum isl_lp_result res;
8726 if (!bset1 || !bset2)
8727 return isl_lp_error;
8729 nparam = isl_basic_set_n_param(bset1);
8730 dim1 = isl_basic_set_n_dim(bset1);
8732 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8733 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8734 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8735 isl_dim_out, 0, pos);
8736 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8737 isl_dim_out, 0, pos);
8738 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8739 if (!bmap1)
8740 return isl_lp_error;
8742 total = isl_basic_map_total_dim(bmap1);
8743 ctx = bmap1->ctx;
8744 obj = isl_vec_alloc(ctx, 1 + total);
8745 if (!obj)
8746 goto error;
8747 isl_seq_clr(obj->block.data, 1 + total);
8748 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8749 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8750 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8751 opt, NULL, NULL);
8752 isl_basic_map_free(bmap1);
8753 isl_vec_free(obj);
8754 return res;
8755 error:
8756 isl_basic_map_free(bmap1);
8757 return isl_lp_error;
8760 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8761 * for any common value of the parameters and dimensions preceding pos
8762 * in both basic sets, the values of dimension pos in bset1 are
8763 * smaller or larger than those in bset2.
8765 * Returns
8766 * 1 if bset1 follows bset2
8767 * -1 if bset1 precedes bset2
8768 * 0 if bset1 and bset2 are incomparable
8769 * -2 if some error occurred.
8771 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8772 struct isl_basic_set *bset2, int pos)
8774 isl_int opt;
8775 enum isl_lp_result res;
8776 int cmp;
8778 isl_int_init(opt);
8780 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8782 if (res == isl_lp_empty)
8783 cmp = 0;
8784 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8785 res == isl_lp_unbounded)
8786 cmp = 1;
8787 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8788 cmp = -1;
8789 else
8790 cmp = -2;
8792 isl_int_clear(opt);
8793 return cmp;
8796 /* Given two basic sets bset1 and bset2, check whether
8797 * for any common value of the parameters and dimensions preceding pos
8798 * there is a value of dimension pos in bset1 that is larger
8799 * than a value of the same dimension in bset2.
8801 * Return
8802 * 1 if there exists such a pair
8803 * 0 if there is no such pair, but there is a pair of equal values
8804 * -1 otherwise
8805 * -2 if some error occurred.
8807 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8808 __isl_keep isl_basic_set *bset2, int pos)
8810 isl_int opt;
8811 enum isl_lp_result res;
8812 int cmp;
8814 isl_int_init(opt);
8816 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8818 if (res == isl_lp_empty)
8819 cmp = -1;
8820 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8821 res == isl_lp_unbounded)
8822 cmp = 1;
8823 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8824 cmp = -1;
8825 else if (res == isl_lp_ok)
8826 cmp = 0;
8827 else
8828 cmp = -2;
8830 isl_int_clear(opt);
8831 return cmp;
8834 /* Given two sets set1 and set2, check whether
8835 * for any common value of the parameters and dimensions preceding pos
8836 * there is a value of dimension pos in set1 that is larger
8837 * than a value of the same dimension in set2.
8839 * Return
8840 * 1 if there exists such a pair
8841 * 0 if there is no such pair, but there is a pair of equal values
8842 * -1 otherwise
8843 * -2 if some error occurred.
8845 int isl_set_follows_at(__isl_keep isl_set *set1,
8846 __isl_keep isl_set *set2, int pos)
8848 int i, j;
8849 int follows = -1;
8851 if (!set1 || !set2)
8852 return -2;
8854 for (i = 0; i < set1->n; ++i)
8855 for (j = 0; j < set2->n; ++j) {
8856 int f;
8857 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8858 if (f == 1 || f == -2)
8859 return f;
8860 if (f > follows)
8861 follows = f;
8864 return follows;
8867 static isl_bool isl_basic_map_plain_has_fixed_var(
8868 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
8870 int i;
8871 int d;
8872 unsigned total;
8874 if (!bmap)
8875 return isl_bool_error;
8876 total = isl_basic_map_total_dim(bmap);
8877 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8878 for (; d+1 > pos; --d)
8879 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8880 break;
8881 if (d != pos)
8882 continue;
8883 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8884 return isl_bool_false;
8885 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8886 return isl_bool_false;
8887 if (!isl_int_is_one(bmap->eq[i][1+d]))
8888 return isl_bool_false;
8889 if (val)
8890 isl_int_neg(*val, bmap->eq[i][0]);
8891 return isl_bool_true;
8893 return isl_bool_false;
8896 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8897 unsigned pos, isl_int *val)
8899 int i;
8900 isl_int v;
8901 isl_int tmp;
8902 isl_bool fixed;
8904 if (!map)
8905 return isl_bool_error;
8906 if (map->n == 0)
8907 return isl_bool_false;
8908 if (map->n == 1)
8909 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8910 isl_int_init(v);
8911 isl_int_init(tmp);
8912 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8913 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
8914 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8915 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
8916 fixed = isl_bool_false;
8918 if (val)
8919 isl_int_set(*val, v);
8920 isl_int_clear(tmp);
8921 isl_int_clear(v);
8922 return fixed;
8925 static isl_bool isl_basic_set_plain_has_fixed_var(
8926 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
8928 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8929 pos, val);
8932 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8933 enum isl_dim_type type, unsigned pos, isl_int *val)
8935 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
8936 return isl_bool_error;
8937 return isl_basic_map_plain_has_fixed_var(bmap,
8938 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8941 /* If "bmap" obviously lies on a hyperplane where the given dimension
8942 * has a fixed value, then return that value.
8943 * Otherwise return NaN.
8945 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8946 __isl_keep isl_basic_map *bmap,
8947 enum isl_dim_type type, unsigned pos)
8949 isl_ctx *ctx;
8950 isl_val *v;
8951 isl_bool fixed;
8953 if (!bmap)
8954 return NULL;
8955 ctx = isl_basic_map_get_ctx(bmap);
8956 v = isl_val_alloc(ctx);
8957 if (!v)
8958 return NULL;
8959 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8960 if (fixed < 0)
8961 return isl_val_free(v);
8962 if (fixed) {
8963 isl_int_set_si(v->d, 1);
8964 return v;
8966 isl_val_free(v);
8967 return isl_val_nan(ctx);
8970 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
8971 enum isl_dim_type type, unsigned pos, isl_int *val)
8973 if (pos >= isl_map_dim(map, type))
8974 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8975 "position out of bounds", return isl_bool_error);
8976 return isl_map_plain_has_fixed_var(map,
8977 map_offset(map, type) - 1 + pos, val);
8980 /* If "map" obviously lies on a hyperplane where the given dimension
8981 * has a fixed value, then return that value.
8982 * Otherwise return NaN.
8984 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8985 enum isl_dim_type type, unsigned pos)
8987 isl_ctx *ctx;
8988 isl_val *v;
8989 isl_bool fixed;
8991 if (!map)
8992 return NULL;
8993 ctx = isl_map_get_ctx(map);
8994 v = isl_val_alloc(ctx);
8995 if (!v)
8996 return NULL;
8997 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8998 if (fixed < 0)
8999 return isl_val_free(v);
9000 if (fixed) {
9001 isl_int_set_si(v->d, 1);
9002 return v;
9004 isl_val_free(v);
9005 return isl_val_nan(ctx);
9008 /* If "set" obviously lies on a hyperplane where the given dimension
9009 * has a fixed value, then return that value.
9010 * Otherwise return NaN.
9012 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9013 enum isl_dim_type type, unsigned pos)
9015 return isl_map_plain_get_val_if_fixed(set, type, pos);
9018 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
9019 enum isl_dim_type type, unsigned pos, isl_int *val)
9021 return isl_map_plain_is_fixed(set, type, pos, val);
9024 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9025 * then return this fixed value in *val.
9027 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9028 unsigned dim, isl_int *val)
9030 return isl_basic_set_plain_has_fixed_var(bset,
9031 isl_basic_set_n_param(bset) + dim, val);
9034 /* Return -1 if the constraint "c1" should be sorted before "c2"
9035 * and 1 if it should be sorted after "c2".
9036 * Return 0 if the two constraints are the same (up to the constant term).
9038 * In particular, if a constraint involves later variables than another
9039 * then it is sorted after this other constraint.
9040 * uset_gist depends on constraints without existentially quantified
9041 * variables sorting first.
9043 * For constraints that have the same latest variable, those
9044 * with the same coefficient for this latest variable (first in absolute value
9045 * and then in actual value) are grouped together.
9046 * This is useful for detecting pairs of constraints that can
9047 * be chained in their printed representation.
9049 * Finally, within a group, constraints are sorted according to
9050 * their coefficients (excluding the constant term).
9052 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9054 isl_int **c1 = (isl_int **) p1;
9055 isl_int **c2 = (isl_int **) p2;
9056 int l1, l2;
9057 unsigned size = *(unsigned *) arg;
9058 int cmp;
9060 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9061 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9063 if (l1 != l2)
9064 return l1 - l2;
9066 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9067 if (cmp != 0)
9068 return cmp;
9069 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9070 if (cmp != 0)
9071 return -cmp;
9073 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9076 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9077 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9078 * and 0 if the two constraints are the same (up to the constant term).
9080 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9081 isl_int *c1, isl_int *c2)
9083 unsigned total;
9085 if (!bmap)
9086 return -2;
9087 total = isl_basic_map_total_dim(bmap);
9088 return sort_constraint_cmp(&c1, &c2, &total);
9091 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9092 __isl_take isl_basic_map *bmap)
9094 unsigned total;
9096 if (!bmap)
9097 return NULL;
9098 if (bmap->n_ineq == 0)
9099 return bmap;
9100 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9101 return bmap;
9102 total = isl_basic_map_total_dim(bmap);
9103 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9104 &sort_constraint_cmp, &total) < 0)
9105 return isl_basic_map_free(bmap);
9106 return bmap;
9109 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9110 __isl_take isl_basic_set *bset)
9112 isl_basic_map *bmap = bset_to_bmap(bset);
9113 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9116 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9118 if (!bmap)
9119 return NULL;
9120 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9121 return bmap;
9122 bmap = isl_basic_map_remove_redundancies(bmap);
9123 bmap = isl_basic_map_sort_constraints(bmap);
9124 if (bmap)
9125 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9126 return bmap;
9128 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9129 __isl_keep isl_basic_map *bmap2)
9131 int i, cmp;
9132 unsigned total;
9134 if (!bmap1 || !bmap2)
9135 return -1;
9137 if (bmap1 == bmap2)
9138 return 0;
9139 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9140 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9141 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9142 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9143 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9144 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9145 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9146 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9147 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9148 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9149 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9150 return 0;
9151 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9152 return 1;
9153 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9154 return -1;
9155 if (bmap1->n_eq != bmap2->n_eq)
9156 return bmap1->n_eq - bmap2->n_eq;
9157 if (bmap1->n_ineq != bmap2->n_ineq)
9158 return bmap1->n_ineq - bmap2->n_ineq;
9159 if (bmap1->n_div != bmap2->n_div)
9160 return bmap1->n_div - bmap2->n_div;
9161 total = isl_basic_map_total_dim(bmap1);
9162 for (i = 0; i < bmap1->n_eq; ++i) {
9163 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9164 if (cmp)
9165 return cmp;
9167 for (i = 0; i < bmap1->n_ineq; ++i) {
9168 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9169 if (cmp)
9170 return cmp;
9172 for (i = 0; i < bmap1->n_div; ++i) {
9173 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9174 if (cmp)
9175 return cmp;
9177 return 0;
9180 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9181 __isl_keep isl_basic_set *bset2)
9183 return isl_basic_map_plain_cmp(bset1, bset2);
9186 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9188 int i, cmp;
9190 if (set1 == set2)
9191 return 0;
9192 if (set1->n != set2->n)
9193 return set1->n - set2->n;
9195 for (i = 0; i < set1->n; ++i) {
9196 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9197 if (cmp)
9198 return cmp;
9201 return 0;
9204 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9205 __isl_keep isl_basic_map *bmap2)
9207 if (!bmap1 || !bmap2)
9208 return isl_bool_error;
9209 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9212 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9213 __isl_keep isl_basic_set *bset2)
9215 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9216 bset_to_bmap(bset2));
9219 static int qsort_bmap_cmp(const void *p1, const void *p2)
9221 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9222 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9224 return isl_basic_map_plain_cmp(bmap1, bmap2);
9227 /* Sort the basic maps of "map" and remove duplicate basic maps.
9229 * While removing basic maps, we make sure that the basic maps remain
9230 * sorted because isl_map_normalize expects the basic maps of the result
9231 * to be sorted.
9233 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9235 int i, j;
9237 map = isl_map_remove_empty_parts(map);
9238 if (!map)
9239 return NULL;
9240 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9241 for (i = map->n - 1; i >= 1; --i) {
9242 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9243 continue;
9244 isl_basic_map_free(map->p[i-1]);
9245 for (j = i; j < map->n; ++j)
9246 map->p[j - 1] = map->p[j];
9247 map->n--;
9250 return map;
9253 /* Remove obvious duplicates among the basic maps of "map".
9255 * Unlike isl_map_normalize, this function does not remove redundant
9256 * constraints and only removes duplicates that have exactly the same
9257 * constraints in the input. It does sort the constraints and
9258 * the basic maps to ease the detection of duplicates.
9260 * If "map" has already been normalized or if the basic maps are
9261 * disjoint, then there can be no duplicates.
9263 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9265 int i;
9266 isl_basic_map *bmap;
9268 if (!map)
9269 return NULL;
9270 if (map->n <= 1)
9271 return map;
9272 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9273 return map;
9274 for (i = 0; i < map->n; ++i) {
9275 bmap = isl_basic_map_copy(map->p[i]);
9276 bmap = isl_basic_map_sort_constraints(bmap);
9277 if (!bmap)
9278 return isl_map_free(map);
9279 isl_basic_map_free(map->p[i]);
9280 map->p[i] = bmap;
9283 map = sort_and_remove_duplicates(map);
9284 return map;
9287 /* We normalize in place, but if anything goes wrong we need
9288 * to return NULL, so we need to make sure we don't change the
9289 * meaning of any possible other copies of map.
9291 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9293 int i;
9294 struct isl_basic_map *bmap;
9296 if (!map)
9297 return NULL;
9298 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9299 return map;
9300 for (i = 0; i < map->n; ++i) {
9301 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9302 if (!bmap)
9303 goto error;
9304 isl_basic_map_free(map->p[i]);
9305 map->p[i] = bmap;
9308 map = sort_and_remove_duplicates(map);
9309 if (map)
9310 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9311 return map;
9312 error:
9313 isl_map_free(map);
9314 return NULL;
9317 struct isl_set *isl_set_normalize(struct isl_set *set)
9319 return set_from_map(isl_map_normalize(set_to_map(set)));
9322 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9323 __isl_keep isl_map *map2)
9325 int i;
9326 isl_bool equal;
9328 if (!map1 || !map2)
9329 return isl_bool_error;
9331 if (map1 == map2)
9332 return isl_bool_true;
9333 if (!isl_space_is_equal(map1->dim, map2->dim))
9334 return isl_bool_false;
9336 map1 = isl_map_copy(map1);
9337 map2 = isl_map_copy(map2);
9338 map1 = isl_map_normalize(map1);
9339 map2 = isl_map_normalize(map2);
9340 if (!map1 || !map2)
9341 goto error;
9342 equal = map1->n == map2->n;
9343 for (i = 0; equal && i < map1->n; ++i) {
9344 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9345 if (equal < 0)
9346 goto error;
9348 isl_map_free(map1);
9349 isl_map_free(map2);
9350 return equal;
9351 error:
9352 isl_map_free(map1);
9353 isl_map_free(map2);
9354 return isl_bool_error;
9357 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9358 __isl_keep isl_set *set2)
9360 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9363 /* Return the basic maps in "map" as a list.
9365 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9366 __isl_keep isl_map *map)
9368 int i;
9369 isl_ctx *ctx;
9370 isl_basic_map_list *list;
9372 if (!map)
9373 return NULL;
9374 ctx = isl_map_get_ctx(map);
9375 list = isl_basic_map_list_alloc(ctx, map->n);
9377 for (i = 0; i < map->n; ++i) {
9378 isl_basic_map *bmap;
9380 bmap = isl_basic_map_copy(map->p[i]);
9381 list = isl_basic_map_list_add(list, bmap);
9384 return list;
9387 /* Return the intersection of the elements in the non-empty list "list".
9388 * All elements are assumed to live in the same space.
9390 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9391 __isl_take isl_basic_map_list *list)
9393 int i, n;
9394 isl_basic_map *bmap;
9396 if (!list)
9397 return NULL;
9398 n = isl_basic_map_list_n_basic_map(list);
9399 if (n < 1)
9400 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9401 "expecting non-empty list", goto error);
9403 bmap = isl_basic_map_list_get_basic_map(list, 0);
9404 for (i = 1; i < n; ++i) {
9405 isl_basic_map *bmap_i;
9407 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9408 bmap = isl_basic_map_intersect(bmap, bmap_i);
9411 isl_basic_map_list_free(list);
9412 return bmap;
9413 error:
9414 isl_basic_map_list_free(list);
9415 return NULL;
9418 /* Return the intersection of the elements in the non-empty list "list".
9419 * All elements are assumed to live in the same space.
9421 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9422 __isl_take isl_basic_set_list *list)
9424 return isl_basic_map_list_intersect(list);
9427 /* Return the union of the elements of "list".
9428 * The list is required to have at least one element.
9430 __isl_give isl_set *isl_basic_set_list_union(
9431 __isl_take isl_basic_set_list *list)
9433 int i, n;
9434 isl_space *space;
9435 isl_basic_set *bset;
9436 isl_set *set;
9438 if (!list)
9439 return NULL;
9440 n = isl_basic_set_list_n_basic_set(list);
9441 if (n < 1)
9442 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9443 "expecting non-empty list", goto error);
9445 bset = isl_basic_set_list_get_basic_set(list, 0);
9446 space = isl_basic_set_get_space(bset);
9447 isl_basic_set_free(bset);
9449 set = isl_set_alloc_space(space, n, 0);
9450 for (i = 0; i < n; ++i) {
9451 bset = isl_basic_set_list_get_basic_set(list, i);
9452 set = isl_set_add_basic_set(set, bset);
9455 isl_basic_set_list_free(list);
9456 return set;
9457 error:
9458 isl_basic_set_list_free(list);
9459 return NULL;
9462 /* Return the union of the elements in the non-empty list "list".
9463 * All elements are assumed to live in the same space.
9465 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9467 int i, n;
9468 isl_set *set;
9470 if (!list)
9471 return NULL;
9472 n = isl_set_list_n_set(list);
9473 if (n < 1)
9474 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9475 "expecting non-empty list", goto error);
9477 set = isl_set_list_get_set(list, 0);
9478 for (i = 1; i < n; ++i) {
9479 isl_set *set_i;
9481 set_i = isl_set_list_get_set(list, i);
9482 set = isl_set_union(set, set_i);
9485 isl_set_list_free(list);
9486 return set;
9487 error:
9488 isl_set_list_free(list);
9489 return NULL;
9492 /* Return the Cartesian product of the basic sets in list (in the given order).
9494 __isl_give isl_basic_set *isl_basic_set_list_product(
9495 __isl_take struct isl_basic_set_list *list)
9497 int i;
9498 unsigned dim;
9499 unsigned nparam;
9500 unsigned extra;
9501 unsigned n_eq;
9502 unsigned n_ineq;
9503 struct isl_basic_set *product = NULL;
9505 if (!list)
9506 goto error;
9507 isl_assert(list->ctx, list->n > 0, goto error);
9508 isl_assert(list->ctx, list->p[0], goto error);
9509 nparam = isl_basic_set_n_param(list->p[0]);
9510 dim = isl_basic_set_n_dim(list->p[0]);
9511 extra = list->p[0]->n_div;
9512 n_eq = list->p[0]->n_eq;
9513 n_ineq = list->p[0]->n_ineq;
9514 for (i = 1; i < list->n; ++i) {
9515 isl_assert(list->ctx, list->p[i], goto error);
9516 isl_assert(list->ctx,
9517 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9518 dim += isl_basic_set_n_dim(list->p[i]);
9519 extra += list->p[i]->n_div;
9520 n_eq += list->p[i]->n_eq;
9521 n_ineq += list->p[i]->n_ineq;
9523 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9524 n_eq, n_ineq);
9525 if (!product)
9526 goto error;
9527 dim = 0;
9528 for (i = 0; i < list->n; ++i) {
9529 isl_basic_set_add_constraints(product,
9530 isl_basic_set_copy(list->p[i]), dim);
9531 dim += isl_basic_set_n_dim(list->p[i]);
9533 isl_basic_set_list_free(list);
9534 return product;
9535 error:
9536 isl_basic_set_free(product);
9537 isl_basic_set_list_free(list);
9538 return NULL;
9541 struct isl_basic_map *isl_basic_map_product(
9542 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9544 isl_space *dim_result = NULL;
9545 struct isl_basic_map *bmap;
9546 unsigned in1, in2, out1, out2, nparam, total, pos;
9547 struct isl_dim_map *dim_map1, *dim_map2;
9549 if (!bmap1 || !bmap2)
9550 goto error;
9552 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9553 bmap2->dim, isl_dim_param), goto error);
9554 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9555 isl_space_copy(bmap2->dim));
9557 in1 = isl_basic_map_n_in(bmap1);
9558 in2 = isl_basic_map_n_in(bmap2);
9559 out1 = isl_basic_map_n_out(bmap1);
9560 out2 = isl_basic_map_n_out(bmap2);
9561 nparam = isl_basic_map_n_param(bmap1);
9563 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9564 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9565 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9566 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9567 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9568 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9569 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9570 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9571 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9572 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9573 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9575 bmap = isl_basic_map_alloc_space(dim_result,
9576 bmap1->n_div + bmap2->n_div,
9577 bmap1->n_eq + bmap2->n_eq,
9578 bmap1->n_ineq + bmap2->n_ineq);
9579 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9580 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9581 bmap = isl_basic_map_simplify(bmap);
9582 return isl_basic_map_finalize(bmap);
9583 error:
9584 isl_basic_map_free(bmap1);
9585 isl_basic_map_free(bmap2);
9586 return NULL;
9589 __isl_give isl_basic_map *isl_basic_map_flat_product(
9590 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9592 isl_basic_map *prod;
9594 prod = isl_basic_map_product(bmap1, bmap2);
9595 prod = isl_basic_map_flatten(prod);
9596 return prod;
9599 __isl_give isl_basic_set *isl_basic_set_flat_product(
9600 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9602 return isl_basic_map_flat_range_product(bset1, bset2);
9605 __isl_give isl_basic_map *isl_basic_map_domain_product(
9606 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9608 isl_space *space_result = NULL;
9609 isl_basic_map *bmap;
9610 unsigned in1, in2, out, nparam, total, pos;
9611 struct isl_dim_map *dim_map1, *dim_map2;
9613 if (!bmap1 || !bmap2)
9614 goto error;
9616 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9617 isl_space_copy(bmap2->dim));
9619 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9620 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9621 out = isl_basic_map_dim(bmap1, isl_dim_out);
9622 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9624 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9625 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9626 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9627 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9628 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9629 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9630 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9631 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9632 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9633 isl_dim_map_div(dim_map1, bmap1, pos += out);
9634 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9636 bmap = isl_basic_map_alloc_space(space_result,
9637 bmap1->n_div + bmap2->n_div,
9638 bmap1->n_eq + bmap2->n_eq,
9639 bmap1->n_ineq + bmap2->n_ineq);
9640 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9641 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9642 bmap = isl_basic_map_simplify(bmap);
9643 return isl_basic_map_finalize(bmap);
9644 error:
9645 isl_basic_map_free(bmap1);
9646 isl_basic_map_free(bmap2);
9647 return NULL;
9650 __isl_give isl_basic_map *isl_basic_map_range_product(
9651 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9653 isl_bool rational;
9654 isl_space *dim_result = NULL;
9655 isl_basic_map *bmap;
9656 unsigned in, out1, out2, nparam, total, pos;
9657 struct isl_dim_map *dim_map1, *dim_map2;
9659 rational = isl_basic_map_is_rational(bmap1);
9660 if (rational >= 0 && rational)
9661 rational = isl_basic_map_is_rational(bmap2);
9662 if (!bmap1 || !bmap2 || rational < 0)
9663 goto error;
9665 if (!isl_space_match(bmap1->dim, isl_dim_param,
9666 bmap2->dim, isl_dim_param))
9667 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9668 "parameters don't match", goto error);
9670 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9671 isl_space_copy(bmap2->dim));
9673 in = isl_basic_map_dim(bmap1, isl_dim_in);
9674 out1 = isl_basic_map_n_out(bmap1);
9675 out2 = isl_basic_map_n_out(bmap2);
9676 nparam = isl_basic_map_n_param(bmap1);
9678 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9679 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9680 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9681 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9682 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9683 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9684 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9685 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9686 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9687 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9688 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9690 bmap = isl_basic_map_alloc_space(dim_result,
9691 bmap1->n_div + bmap2->n_div,
9692 bmap1->n_eq + bmap2->n_eq,
9693 bmap1->n_ineq + bmap2->n_ineq);
9694 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9695 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9696 if (rational)
9697 bmap = isl_basic_map_set_rational(bmap);
9698 bmap = isl_basic_map_simplify(bmap);
9699 return isl_basic_map_finalize(bmap);
9700 error:
9701 isl_basic_map_free(bmap1);
9702 isl_basic_map_free(bmap2);
9703 return NULL;
9706 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9707 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9709 isl_basic_map *prod;
9711 prod = isl_basic_map_range_product(bmap1, bmap2);
9712 prod = isl_basic_map_flatten_range(prod);
9713 return prod;
9716 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9717 * and collect the results.
9718 * The result live in the space obtained by calling "space_product"
9719 * on the spaces of "map1" and "map2".
9720 * If "remove_duplicates" is set then the result may contain duplicates
9721 * (even if the inputs do not) and so we try and remove the obvious
9722 * duplicates.
9724 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9725 __isl_take isl_map *map2,
9726 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9727 __isl_take isl_space *right),
9728 __isl_give isl_basic_map *(*basic_map_product)(
9729 __isl_take isl_basic_map *left,
9730 __isl_take isl_basic_map *right),
9731 int remove_duplicates)
9733 unsigned flags = 0;
9734 struct isl_map *result;
9735 int i, j;
9737 if (!map1 || !map2)
9738 goto error;
9740 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9741 map2->dim, isl_dim_param), goto error);
9743 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9744 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9745 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9747 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9748 isl_space_copy(map2->dim)),
9749 map1->n * map2->n, flags);
9750 if (!result)
9751 goto error;
9752 for (i = 0; i < map1->n; ++i)
9753 for (j = 0; j < map2->n; ++j) {
9754 struct isl_basic_map *part;
9755 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9756 isl_basic_map_copy(map2->p[j]));
9757 if (isl_basic_map_is_empty(part))
9758 isl_basic_map_free(part);
9759 else
9760 result = isl_map_add_basic_map(result, part);
9761 if (!result)
9762 goto error;
9764 if (remove_duplicates)
9765 result = isl_map_remove_obvious_duplicates(result);
9766 isl_map_free(map1);
9767 isl_map_free(map2);
9768 return result;
9769 error:
9770 isl_map_free(map1);
9771 isl_map_free(map2);
9772 return NULL;
9775 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9777 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9778 __isl_take isl_map *map2)
9780 return map_product(map1, map2, &isl_space_product,
9781 &isl_basic_map_product, 0);
9784 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9785 __isl_take isl_map *map2)
9787 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9790 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9792 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9793 __isl_take isl_map *map2)
9795 isl_map *prod;
9797 prod = isl_map_product(map1, map2);
9798 prod = isl_map_flatten(prod);
9799 return prod;
9802 /* Given two set A and B, construct its Cartesian product A x B.
9804 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9806 return isl_map_range_product(set1, set2);
9809 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9810 __isl_take isl_set *set2)
9812 return isl_map_flat_range_product(set1, set2);
9815 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9817 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9818 __isl_take isl_map *map2)
9820 return map_product(map1, map2, &isl_space_domain_product,
9821 &isl_basic_map_domain_product, 1);
9824 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9826 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9827 __isl_take isl_map *map2)
9829 return map_product(map1, map2, &isl_space_range_product,
9830 &isl_basic_map_range_product, 1);
9833 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9834 __isl_take isl_map *map2)
9836 return isl_map_align_params_map_map_and(map1, map2,
9837 &map_domain_product_aligned);
9840 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9841 __isl_take isl_map *map2)
9843 return isl_map_align_params_map_map_and(map1, map2,
9844 &map_range_product_aligned);
9847 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9849 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9851 isl_space *space;
9852 int total1, keep1, total2, keep2;
9854 if (!map)
9855 return NULL;
9856 if (!isl_space_domain_is_wrapping(map->dim) ||
9857 !isl_space_range_is_wrapping(map->dim))
9858 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9859 "not a product", return isl_map_free(map));
9861 space = isl_map_get_space(map);
9862 total1 = isl_space_dim(space, isl_dim_in);
9863 total2 = isl_space_dim(space, isl_dim_out);
9864 space = isl_space_factor_domain(space);
9865 keep1 = isl_space_dim(space, isl_dim_in);
9866 keep2 = isl_space_dim(space, isl_dim_out);
9867 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9868 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9869 map = isl_map_reset_space(map, space);
9871 return map;
9874 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9876 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9878 isl_space *space;
9879 int total1, keep1, total2, keep2;
9881 if (!map)
9882 return NULL;
9883 if (!isl_space_domain_is_wrapping(map->dim) ||
9884 !isl_space_range_is_wrapping(map->dim))
9885 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9886 "not a product", return isl_map_free(map));
9888 space = isl_map_get_space(map);
9889 total1 = isl_space_dim(space, isl_dim_in);
9890 total2 = isl_space_dim(space, isl_dim_out);
9891 space = isl_space_factor_range(space);
9892 keep1 = isl_space_dim(space, isl_dim_in);
9893 keep2 = isl_space_dim(space, isl_dim_out);
9894 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9895 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9896 map = isl_map_reset_space(map, space);
9898 return map;
9901 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9903 __isl_give isl_map *isl_map_domain_factor_domain(__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_domain(space);
9917 keep = isl_space_dim(space, isl_dim_in);
9918 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9919 map = isl_map_reset_space(map, space);
9921 return map;
9924 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9926 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9928 isl_space *space;
9929 int total, keep;
9931 if (!map)
9932 return NULL;
9933 if (!isl_space_domain_is_wrapping(map->dim))
9934 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9935 "domain is not a product", return isl_map_free(map));
9937 space = isl_map_get_space(map);
9938 total = isl_space_dim(space, isl_dim_in);
9939 space = isl_space_domain_factor_range(space);
9940 keep = isl_space_dim(space, isl_dim_in);
9941 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9942 map = isl_map_reset_space(map, space);
9944 return map;
9947 /* Given a map A -> [B -> C], extract the map A -> B.
9949 __isl_give isl_map *isl_map_range_factor_domain(__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_domain(space);
9963 keep = isl_space_dim(space, isl_dim_out);
9964 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9965 map = isl_map_reset_space(map, space);
9967 return map;
9970 /* Given a map A -> [B -> C], extract the map A -> C.
9972 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9974 isl_space *space;
9975 int total, keep;
9977 if (!map)
9978 return NULL;
9979 if (!isl_space_range_is_wrapping(map->dim))
9980 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9981 "range is not a product", return isl_map_free(map));
9983 space = isl_map_get_space(map);
9984 total = isl_space_dim(space, isl_dim_out);
9985 space = isl_space_range_factor_range(space);
9986 keep = isl_space_dim(space, isl_dim_out);
9987 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9988 map = isl_map_reset_space(map, space);
9990 return map;
9993 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9995 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9996 __isl_take isl_map *map2)
9998 isl_map *prod;
10000 prod = isl_map_domain_product(map1, map2);
10001 prod = isl_map_flatten_domain(prod);
10002 return prod;
10005 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10007 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10008 __isl_take isl_map *map2)
10010 isl_map *prod;
10012 prod = isl_map_range_product(map1, map2);
10013 prod = isl_map_flatten_range(prod);
10014 return prod;
10017 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10019 int i;
10020 uint32_t hash = isl_hash_init();
10021 unsigned total;
10023 if (!bmap)
10024 return 0;
10025 bmap = isl_basic_map_copy(bmap);
10026 bmap = isl_basic_map_normalize(bmap);
10027 if (!bmap)
10028 return 0;
10029 total = isl_basic_map_total_dim(bmap);
10030 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10031 for (i = 0; i < bmap->n_eq; ++i) {
10032 uint32_t c_hash;
10033 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10034 isl_hash_hash(hash, c_hash);
10036 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10037 for (i = 0; i < bmap->n_ineq; ++i) {
10038 uint32_t c_hash;
10039 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10040 isl_hash_hash(hash, c_hash);
10042 isl_hash_byte(hash, bmap->n_div & 0xFF);
10043 for (i = 0; i < bmap->n_div; ++i) {
10044 uint32_t c_hash;
10045 if (isl_int_is_zero(bmap->div[i][0]))
10046 continue;
10047 isl_hash_byte(hash, i & 0xFF);
10048 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10049 isl_hash_hash(hash, c_hash);
10051 isl_basic_map_free(bmap);
10052 return hash;
10055 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10057 return isl_basic_map_get_hash(bset_to_bmap(bset));
10060 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10062 int i;
10063 uint32_t hash;
10065 if (!map)
10066 return 0;
10067 map = isl_map_copy(map);
10068 map = isl_map_normalize(map);
10069 if (!map)
10070 return 0;
10072 hash = isl_hash_init();
10073 for (i = 0; i < map->n; ++i) {
10074 uint32_t bmap_hash;
10075 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10076 isl_hash_hash(hash, bmap_hash);
10079 isl_map_free(map);
10081 return hash;
10084 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10086 return isl_map_get_hash(set_to_map(set));
10089 /* Check if the value for dimension dim is completely determined
10090 * by the values of the other parameters and variables.
10091 * That is, check if dimension dim is involved in an equality.
10093 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10095 int i;
10096 unsigned nparam;
10098 if (!bset)
10099 return -1;
10100 nparam = isl_basic_set_n_param(bset);
10101 for (i = 0; i < bset->n_eq; ++i)
10102 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10103 return 1;
10104 return 0;
10107 /* Check if the value for dimension dim is completely determined
10108 * by the values of the other parameters and variables.
10109 * That is, check if dimension dim is involved in an equality
10110 * for each of the subsets.
10112 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10114 int i;
10116 if (!set)
10117 return -1;
10118 for (i = 0; i < set->n; ++i) {
10119 int unique;
10120 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10121 if (unique != 1)
10122 return unique;
10124 return 1;
10127 /* Return the number of basic maps in the (current) representation of "map".
10129 int isl_map_n_basic_map(__isl_keep isl_map *map)
10131 return map ? map->n : 0;
10134 int isl_set_n_basic_set(__isl_keep isl_set *set)
10136 return set ? set->n : 0;
10139 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10140 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10142 int i;
10144 if (!map)
10145 return isl_stat_error;
10147 for (i = 0; i < map->n; ++i)
10148 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10149 return isl_stat_error;
10151 return isl_stat_ok;
10154 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10155 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10157 int i;
10159 if (!set)
10160 return isl_stat_error;
10162 for (i = 0; i < set->n; ++i)
10163 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10164 return isl_stat_error;
10166 return isl_stat_ok;
10169 /* Return a list of basic sets, the union of which is equal to "set".
10171 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10172 __isl_keep isl_set *set)
10174 int i;
10175 isl_basic_set_list *list;
10177 if (!set)
10178 return NULL;
10180 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10181 for (i = 0; i < set->n; ++i) {
10182 isl_basic_set *bset;
10184 bset = isl_basic_set_copy(set->p[i]);
10185 list = isl_basic_set_list_add(list, bset);
10188 return list;
10191 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10193 isl_space *dim;
10195 if (!bset)
10196 return NULL;
10198 bset = isl_basic_set_cow(bset);
10199 if (!bset)
10200 return NULL;
10202 dim = isl_basic_set_get_space(bset);
10203 dim = isl_space_lift(dim, bset->n_div);
10204 if (!dim)
10205 goto error;
10206 isl_space_free(bset->dim);
10207 bset->dim = dim;
10208 bset->extra -= bset->n_div;
10209 bset->n_div = 0;
10211 bset = isl_basic_set_finalize(bset);
10213 return bset;
10214 error:
10215 isl_basic_set_free(bset);
10216 return NULL;
10219 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10221 int i;
10222 isl_space *dim;
10223 unsigned n_div;
10225 set = isl_set_align_divs(set);
10227 if (!set)
10228 return NULL;
10230 set = isl_set_cow(set);
10231 if (!set)
10232 return NULL;
10234 n_div = set->p[0]->n_div;
10235 dim = isl_set_get_space(set);
10236 dim = isl_space_lift(dim, n_div);
10237 if (!dim)
10238 goto error;
10239 isl_space_free(set->dim);
10240 set->dim = dim;
10242 for (i = 0; i < set->n; ++i) {
10243 set->p[i] = isl_basic_set_lift(set->p[i]);
10244 if (!set->p[i])
10245 goto error;
10248 return set;
10249 error:
10250 isl_set_free(set);
10251 return NULL;
10254 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10256 isl_space *dim;
10257 struct isl_basic_map *bmap;
10258 unsigned n_set;
10259 unsigned n_div;
10260 unsigned n_param;
10261 unsigned total;
10262 int i, k, l;
10264 set = isl_set_align_divs(set);
10266 if (!set)
10267 return NULL;
10269 dim = isl_set_get_space(set);
10270 if (set->n == 0 || set->p[0]->n_div == 0) {
10271 isl_set_free(set);
10272 return isl_map_identity(isl_space_map_from_set(dim));
10275 n_div = set->p[0]->n_div;
10276 dim = isl_space_map_from_set(dim);
10277 n_param = isl_space_dim(dim, isl_dim_param);
10278 n_set = isl_space_dim(dim, isl_dim_in);
10279 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10280 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10281 for (i = 0; i < n_set; ++i)
10282 bmap = var_equal(bmap, i);
10284 total = n_param + n_set + n_set + n_div;
10285 for (i = 0; i < n_div; ++i) {
10286 k = isl_basic_map_alloc_inequality(bmap);
10287 if (k < 0)
10288 goto error;
10289 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10290 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10291 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10292 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10293 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10294 set->p[0]->div[i][0]);
10296 l = isl_basic_map_alloc_inequality(bmap);
10297 if (l < 0)
10298 goto error;
10299 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10300 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10301 set->p[0]->div[i][0]);
10302 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10305 isl_set_free(set);
10306 bmap = isl_basic_map_simplify(bmap);
10307 bmap = isl_basic_map_finalize(bmap);
10308 return isl_map_from_basic_map(bmap);
10309 error:
10310 isl_set_free(set);
10311 isl_basic_map_free(bmap);
10312 return NULL;
10315 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10317 unsigned dim;
10318 int size = 0;
10320 if (!bset)
10321 return -1;
10323 dim = isl_basic_set_total_dim(bset);
10324 size += bset->n_eq * (1 + dim);
10325 size += bset->n_ineq * (1 + dim);
10326 size += bset->n_div * (2 + dim);
10328 return size;
10331 int isl_set_size(__isl_keep isl_set *set)
10333 int i;
10334 int size = 0;
10336 if (!set)
10337 return -1;
10339 for (i = 0; i < set->n; ++i)
10340 size += isl_basic_set_size(set->p[i]);
10342 return size;
10345 /* Check if there is any lower bound (if lower == 0) and/or upper
10346 * bound (if upper == 0) on the specified dim.
10348 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10349 enum isl_dim_type type, unsigned pos, int lower, int upper)
10351 int i;
10353 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10354 return isl_bool_error;
10356 pos += isl_basic_map_offset(bmap, type);
10358 for (i = 0; i < bmap->n_div; ++i) {
10359 if (isl_int_is_zero(bmap->div[i][0]))
10360 continue;
10361 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10362 return isl_bool_true;
10365 for (i = 0; i < bmap->n_eq; ++i)
10366 if (!isl_int_is_zero(bmap->eq[i][pos]))
10367 return isl_bool_true;
10369 for (i = 0; i < bmap->n_ineq; ++i) {
10370 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10371 if (sgn > 0)
10372 lower = 1;
10373 if (sgn < 0)
10374 upper = 1;
10377 return lower && upper;
10380 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10381 enum isl_dim_type type, unsigned pos)
10383 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10386 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10387 enum isl_dim_type type, unsigned pos)
10389 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10392 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10393 enum isl_dim_type type, unsigned pos)
10395 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10398 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10399 enum isl_dim_type type, unsigned pos)
10401 int i;
10403 if (!map)
10404 return isl_bool_error;
10406 for (i = 0; i < map->n; ++i) {
10407 isl_bool bounded;
10408 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10409 if (bounded < 0 || !bounded)
10410 return bounded;
10413 return isl_bool_true;
10416 /* Return true if the specified dim is involved in both an upper bound
10417 * and a lower bound.
10419 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10420 enum isl_dim_type type, unsigned pos)
10422 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10425 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10427 static isl_bool has_any_bound(__isl_keep isl_map *map,
10428 enum isl_dim_type type, unsigned pos,
10429 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10430 enum isl_dim_type type, unsigned pos))
10432 int i;
10434 if (!map)
10435 return isl_bool_error;
10437 for (i = 0; i < map->n; ++i) {
10438 isl_bool bounded;
10439 bounded = fn(map->p[i], type, pos);
10440 if (bounded < 0 || bounded)
10441 return bounded;
10444 return isl_bool_false;
10447 /* Return 1 if the specified dim is involved in any lower bound.
10449 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10450 enum isl_dim_type type, unsigned pos)
10452 return has_any_bound(set, type, pos,
10453 &isl_basic_map_dim_has_lower_bound);
10456 /* Return 1 if the specified dim is involved in any upper bound.
10458 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10459 enum isl_dim_type type, unsigned pos)
10461 return has_any_bound(set, type, pos,
10462 &isl_basic_map_dim_has_upper_bound);
10465 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10467 static isl_bool has_bound(__isl_keep isl_map *map,
10468 enum isl_dim_type type, unsigned pos,
10469 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10470 enum isl_dim_type type, unsigned pos))
10472 int i;
10474 if (!map)
10475 return isl_bool_error;
10477 for (i = 0; i < map->n; ++i) {
10478 isl_bool bounded;
10479 bounded = fn(map->p[i], type, pos);
10480 if (bounded < 0 || !bounded)
10481 return bounded;
10484 return isl_bool_true;
10487 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10489 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10490 enum isl_dim_type type, unsigned pos)
10492 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10495 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10497 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10498 enum isl_dim_type type, unsigned pos)
10500 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10503 /* For each of the "n" variables starting at "first", determine
10504 * the sign of the variable and put the results in the first "n"
10505 * elements of the array "signs".
10506 * Sign
10507 * 1 means that the variable is non-negative
10508 * -1 means that the variable is non-positive
10509 * 0 means the variable attains both positive and negative values.
10511 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10512 unsigned first, unsigned n, int *signs)
10514 isl_vec *bound = NULL;
10515 struct isl_tab *tab = NULL;
10516 struct isl_tab_undo *snap;
10517 int i;
10519 if (!bset || !signs)
10520 return isl_stat_error;
10522 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10523 tab = isl_tab_from_basic_set(bset, 0);
10524 if (!bound || !tab)
10525 goto error;
10527 isl_seq_clr(bound->el, bound->size);
10528 isl_int_set_si(bound->el[0], -1);
10530 snap = isl_tab_snap(tab);
10531 for (i = 0; i < n; ++i) {
10532 int empty;
10534 isl_int_set_si(bound->el[1 + first + i], -1);
10535 if (isl_tab_add_ineq(tab, bound->el) < 0)
10536 goto error;
10537 empty = tab->empty;
10538 isl_int_set_si(bound->el[1 + first + i], 0);
10539 if (isl_tab_rollback(tab, snap) < 0)
10540 goto error;
10542 if (empty) {
10543 signs[i] = 1;
10544 continue;
10547 isl_int_set_si(bound->el[1 + first + i], 1);
10548 if (isl_tab_add_ineq(tab, bound->el) < 0)
10549 goto error;
10550 empty = tab->empty;
10551 isl_int_set_si(bound->el[1 + first + i], 0);
10552 if (isl_tab_rollback(tab, snap) < 0)
10553 goto error;
10555 signs[i] = empty ? -1 : 0;
10558 isl_tab_free(tab);
10559 isl_vec_free(bound);
10560 return isl_stat_ok;
10561 error:
10562 isl_tab_free(tab);
10563 isl_vec_free(bound);
10564 return isl_stat_error;
10567 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10568 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10570 if (!bset || !signs)
10571 return isl_stat_error;
10572 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10573 return isl_stat_error);
10575 first += pos(bset->dim, type) - 1;
10576 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10579 /* Is it possible for the integer division "div" to depend (possibly
10580 * indirectly) on any output dimensions?
10582 * If the div is undefined, then we conservatively assume that it
10583 * may depend on them.
10584 * Otherwise, we check if it actually depends on them or on any integer
10585 * divisions that may depend on them.
10587 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10589 int i;
10590 unsigned n_out, o_out;
10591 unsigned n_div, o_div;
10593 if (isl_int_is_zero(bmap->div[div][0]))
10594 return isl_bool_true;
10596 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10597 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10599 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10600 return isl_bool_true;
10602 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10603 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10605 for (i = 0; i < n_div; ++i) {
10606 isl_bool may_involve;
10608 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10609 continue;
10610 may_involve = div_may_involve_output(bmap, i);
10611 if (may_involve < 0 || may_involve)
10612 return may_involve;
10615 return isl_bool_false;
10618 /* Return the first integer division of "bmap" in the range
10619 * [first, first + n[ that may depend on any output dimensions and
10620 * that has a non-zero coefficient in "c" (where the first coefficient
10621 * in "c" corresponds to integer division "first").
10623 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10624 isl_int *c, int first, int n)
10626 int k;
10628 if (!bmap)
10629 return -1;
10631 for (k = first; k < first + n; ++k) {
10632 isl_bool may_involve;
10634 if (isl_int_is_zero(c[k]))
10635 continue;
10636 may_involve = div_may_involve_output(bmap, k);
10637 if (may_involve < 0)
10638 return -1;
10639 if (may_involve)
10640 return k;
10643 return first + n;
10646 /* Look for a pair of inequality constraints in "bmap" of the form
10648 * -l + i >= 0 or i >= l
10649 * and
10650 * n + l - i >= 0 or i <= l + n
10652 * with n < "m" and i the output dimension at position "pos".
10653 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10654 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10655 * and earlier output dimensions, as well as integer divisions that do
10656 * not involve any of the output dimensions.
10658 * Return the index of the first inequality constraint or bmap->n_ineq
10659 * if no such pair can be found.
10661 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10662 int pos, isl_int m)
10664 int i, j;
10665 isl_ctx *ctx;
10666 unsigned total;
10667 unsigned n_div, o_div;
10668 unsigned n_out, o_out;
10669 int less;
10671 if (!bmap)
10672 return -1;
10674 ctx = isl_basic_map_get_ctx(bmap);
10675 total = isl_basic_map_total_dim(bmap);
10676 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10677 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10678 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10679 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10680 for (i = 0; i < bmap->n_ineq; ++i) {
10681 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10682 continue;
10683 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10684 n_out - (pos + 1)) != -1)
10685 continue;
10686 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10687 0, n_div) < n_div)
10688 continue;
10689 for (j = i + 1; j < bmap->n_ineq; ++j) {
10690 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10691 ctx->one))
10692 continue;
10693 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10694 bmap->ineq[j] + 1, total))
10695 continue;
10696 break;
10698 if (j >= bmap->n_ineq)
10699 continue;
10700 isl_int_add(bmap->ineq[i][0],
10701 bmap->ineq[i][0], bmap->ineq[j][0]);
10702 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10703 isl_int_sub(bmap->ineq[i][0],
10704 bmap->ineq[i][0], bmap->ineq[j][0]);
10705 if (!less)
10706 continue;
10707 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10708 return i;
10709 else
10710 return j;
10713 return bmap->n_ineq;
10716 /* Return the index of the equality of "bmap" that defines
10717 * the output dimension "pos" in terms of earlier dimensions.
10718 * The equality may also involve integer divisions, as long
10719 * as those integer divisions are defined in terms of
10720 * parameters or input dimensions.
10721 * In this case, *div is set to the number of integer divisions and
10722 * *ineq is set to the number of inequality constraints (provided
10723 * div and ineq are not NULL).
10725 * The equality may also involve a single integer division involving
10726 * the output dimensions (typically only output dimension "pos") as
10727 * long as the coefficient of output dimension "pos" is 1 or -1 and
10728 * there is a pair of constraints i >= l and i <= l + n, with i referring
10729 * to output dimension "pos", l an expression involving only earlier
10730 * dimensions and n smaller than the coefficient of the integer division
10731 * in the equality. In this case, the output dimension can be defined
10732 * in terms of a modulo expression that does not involve the integer division.
10733 * *div is then set to this single integer division and
10734 * *ineq is set to the index of constraint i >= l.
10736 * Return bmap->n_eq if there is no such equality.
10737 * Return -1 on error.
10739 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10740 int pos, int *div, int *ineq)
10742 int j, k, l;
10743 unsigned n_out, o_out;
10744 unsigned n_div, o_div;
10746 if (!bmap)
10747 return -1;
10749 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10750 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10751 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10752 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10754 if (ineq)
10755 *ineq = bmap->n_ineq;
10756 if (div)
10757 *div = n_div;
10758 for (j = 0; j < bmap->n_eq; ++j) {
10759 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10760 continue;
10761 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10762 n_out - (pos + 1)) != -1)
10763 continue;
10764 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10765 0, n_div);
10766 if (k >= n_div)
10767 return j;
10768 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10769 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10770 continue;
10771 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10772 k + 1, n_div - (k+1)) < n_div)
10773 continue;
10774 l = find_modulo_constraint_pair(bmap, pos,
10775 bmap->eq[j][o_div + k]);
10776 if (l < 0)
10777 return -1;
10778 if (l >= bmap->n_ineq)
10779 continue;
10780 if (div)
10781 *div = k;
10782 if (ineq)
10783 *ineq = l;
10784 return j;
10787 return bmap->n_eq;
10790 /* Check if the given basic map is obviously single-valued.
10791 * In particular, for each output dimension, check that there is
10792 * an equality that defines the output dimension in terms of
10793 * earlier dimensions.
10795 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10797 int i;
10798 unsigned n_out;
10800 if (!bmap)
10801 return isl_bool_error;
10803 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10805 for (i = 0; i < n_out; ++i) {
10806 int eq;
10808 eq = isl_basic_map_output_defining_equality(bmap, i,
10809 NULL, NULL);
10810 if (eq < 0)
10811 return isl_bool_error;
10812 if (eq >= bmap->n_eq)
10813 return isl_bool_false;
10816 return isl_bool_true;
10819 /* Check if the given basic map is single-valued.
10820 * We simply compute
10822 * M \circ M^-1
10824 * and check if the result is a subset of the identity mapping.
10826 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10828 isl_space *space;
10829 isl_basic_map *test;
10830 isl_basic_map *id;
10831 isl_bool sv;
10833 sv = isl_basic_map_plain_is_single_valued(bmap);
10834 if (sv < 0 || sv)
10835 return sv;
10837 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10838 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10840 space = isl_basic_map_get_space(bmap);
10841 space = isl_space_map_from_set(isl_space_range(space));
10842 id = isl_basic_map_identity(space);
10844 sv = isl_basic_map_is_subset(test, id);
10846 isl_basic_map_free(test);
10847 isl_basic_map_free(id);
10849 return sv;
10852 /* Check if the given map is obviously single-valued.
10854 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10856 if (!map)
10857 return isl_bool_error;
10858 if (map->n == 0)
10859 return isl_bool_true;
10860 if (map->n >= 2)
10861 return isl_bool_false;
10863 return isl_basic_map_plain_is_single_valued(map->p[0]);
10866 /* Check if the given map is single-valued.
10867 * We simply compute
10869 * M \circ M^-1
10871 * and check if the result is a subset of the identity mapping.
10873 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10875 isl_space *dim;
10876 isl_map *test;
10877 isl_map *id;
10878 isl_bool sv;
10880 sv = isl_map_plain_is_single_valued(map);
10881 if (sv < 0 || sv)
10882 return sv;
10884 test = isl_map_reverse(isl_map_copy(map));
10885 test = isl_map_apply_range(test, isl_map_copy(map));
10887 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10888 id = isl_map_identity(dim);
10890 sv = isl_map_is_subset(test, id);
10892 isl_map_free(test);
10893 isl_map_free(id);
10895 return sv;
10898 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10900 isl_bool in;
10902 map = isl_map_copy(map);
10903 map = isl_map_reverse(map);
10904 in = isl_map_is_single_valued(map);
10905 isl_map_free(map);
10907 return in;
10910 /* Check if the given map is obviously injective.
10912 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10914 isl_bool in;
10916 map = isl_map_copy(map);
10917 map = isl_map_reverse(map);
10918 in = isl_map_plain_is_single_valued(map);
10919 isl_map_free(map);
10921 return in;
10924 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10926 isl_bool sv;
10928 sv = isl_map_is_single_valued(map);
10929 if (sv < 0 || !sv)
10930 return sv;
10932 return isl_map_is_injective(map);
10935 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10937 return isl_map_is_single_valued(set_to_map(set));
10940 /* Does "map" only map elements to themselves?
10942 * If the domain and range spaces are different, then "map"
10943 * is considered not to be an identity relation, even if it is empty.
10944 * Otherwise, construct the maximal identity relation and
10945 * check whether "map" is a subset of this relation.
10947 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10949 isl_space *space;
10950 isl_map *id;
10951 isl_bool equal, is_identity;
10953 space = isl_map_get_space(map);
10954 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10955 isl_space_free(space);
10956 if (equal < 0 || !equal)
10957 return equal;
10959 id = isl_map_identity(isl_map_get_space(map));
10960 is_identity = isl_map_is_subset(map, id);
10961 isl_map_free(id);
10963 return is_identity;
10966 int isl_map_is_translation(__isl_keep isl_map *map)
10968 int ok;
10969 isl_set *delta;
10971 delta = isl_map_deltas(isl_map_copy(map));
10972 ok = isl_set_is_singleton(delta);
10973 isl_set_free(delta);
10975 return ok;
10978 static int unique(isl_int *p, unsigned pos, unsigned len)
10980 if (isl_seq_first_non_zero(p, pos) != -1)
10981 return 0;
10982 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10983 return 0;
10984 return 1;
10987 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10989 int i, j;
10990 unsigned nvar;
10991 unsigned ovar;
10993 if (!bset)
10994 return isl_bool_error;
10996 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10997 return isl_bool_false;
10999 nvar = isl_basic_set_dim(bset, isl_dim_set);
11000 ovar = isl_space_offset(bset->dim, isl_dim_set);
11001 for (j = 0; j < nvar; ++j) {
11002 int lower = 0, upper = 0;
11003 for (i = 0; i < bset->n_eq; ++i) {
11004 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11005 continue;
11006 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11007 return isl_bool_false;
11008 break;
11010 if (i < bset->n_eq)
11011 continue;
11012 for (i = 0; i < bset->n_ineq; ++i) {
11013 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11014 continue;
11015 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11016 return isl_bool_false;
11017 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11018 lower = 1;
11019 else
11020 upper = 1;
11022 if (!lower || !upper)
11023 return isl_bool_false;
11026 return isl_bool_true;
11029 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11031 if (!set)
11032 return isl_bool_error;
11033 if (set->n != 1)
11034 return isl_bool_false;
11036 return isl_basic_set_is_box(set->p[0]);
11039 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11041 if (!bset)
11042 return isl_bool_error;
11044 return isl_space_is_wrapping(bset->dim);
11047 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11049 if (!set)
11050 return isl_bool_error;
11052 return isl_space_is_wrapping(set->dim);
11055 /* Modify the space of "map" through a call to "change".
11056 * If "can_change" is set (not NULL), then first call it to check
11057 * if the modification is allowed, printing the error message "cannot_change"
11058 * if it is not.
11060 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11061 isl_bool (*can_change)(__isl_keep isl_map *map),
11062 const char *cannot_change,
11063 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11065 isl_bool ok;
11066 isl_space *space;
11068 if (!map)
11069 return NULL;
11071 ok = can_change ? can_change(map) : isl_bool_true;
11072 if (ok < 0)
11073 return isl_map_free(map);
11074 if (!ok)
11075 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11076 return isl_map_free(map));
11078 space = change(isl_map_get_space(map));
11079 map = isl_map_reset_space(map, space);
11081 return map;
11084 /* Is the domain of "map" a wrapped relation?
11086 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11088 if (!map)
11089 return isl_bool_error;
11091 return isl_space_domain_is_wrapping(map->dim);
11094 /* Is the range of "map" a wrapped relation?
11096 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11098 if (!map)
11099 return isl_bool_error;
11101 return isl_space_range_is_wrapping(map->dim);
11104 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11106 bmap = isl_basic_map_cow(bmap);
11107 if (!bmap)
11108 return NULL;
11110 bmap->dim = isl_space_wrap(bmap->dim);
11111 if (!bmap->dim)
11112 goto error;
11114 bmap = isl_basic_map_finalize(bmap);
11116 return bset_from_bmap(bmap);
11117 error:
11118 isl_basic_map_free(bmap);
11119 return NULL;
11122 /* Given a map A -> B, return the set (A -> B).
11124 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11126 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11129 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11131 bset = isl_basic_set_cow(bset);
11132 if (!bset)
11133 return NULL;
11135 bset->dim = isl_space_unwrap(bset->dim);
11136 if (!bset->dim)
11137 goto error;
11139 bset = isl_basic_set_finalize(bset);
11141 return bset_to_bmap(bset);
11142 error:
11143 isl_basic_set_free(bset);
11144 return NULL;
11147 /* Given a set (A -> B), return the map A -> B.
11148 * Error out if "set" is not of the form (A -> B).
11150 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11152 return isl_map_change_space(set, &isl_set_is_wrapping,
11153 "not a wrapping set", &isl_space_unwrap);
11156 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11157 enum isl_dim_type type)
11159 if (!bmap)
11160 return NULL;
11162 if (!isl_space_is_named_or_nested(bmap->dim, type))
11163 return bmap;
11165 bmap = isl_basic_map_cow(bmap);
11166 if (!bmap)
11167 return NULL;
11169 bmap->dim = isl_space_reset(bmap->dim, type);
11170 if (!bmap->dim)
11171 goto error;
11173 bmap = isl_basic_map_finalize(bmap);
11175 return bmap;
11176 error:
11177 isl_basic_map_free(bmap);
11178 return NULL;
11181 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11182 enum isl_dim_type type)
11184 int i;
11186 if (!map)
11187 return NULL;
11189 if (!isl_space_is_named_or_nested(map->dim, type))
11190 return map;
11192 map = isl_map_cow(map);
11193 if (!map)
11194 return NULL;
11196 for (i = 0; i < map->n; ++i) {
11197 map->p[i] = isl_basic_map_reset(map->p[i], type);
11198 if (!map->p[i])
11199 goto error;
11201 map->dim = isl_space_reset(map->dim, type);
11202 if (!map->dim)
11203 goto error;
11205 return map;
11206 error:
11207 isl_map_free(map);
11208 return NULL;
11211 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11213 if (!bmap)
11214 return NULL;
11216 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11217 return bmap;
11219 bmap = isl_basic_map_cow(bmap);
11220 if (!bmap)
11221 return NULL;
11223 bmap->dim = isl_space_flatten(bmap->dim);
11224 if (!bmap->dim)
11225 goto error;
11227 bmap = isl_basic_map_finalize(bmap);
11229 return bmap;
11230 error:
11231 isl_basic_map_free(bmap);
11232 return NULL;
11235 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11237 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11240 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11241 __isl_take isl_basic_map *bmap)
11243 if (!bmap)
11244 return NULL;
11246 if (!bmap->dim->nested[0])
11247 return bmap;
11249 bmap = isl_basic_map_cow(bmap);
11250 if (!bmap)
11251 return NULL;
11253 bmap->dim = isl_space_flatten_domain(bmap->dim);
11254 if (!bmap->dim)
11255 goto error;
11257 bmap = isl_basic_map_finalize(bmap);
11259 return bmap;
11260 error:
11261 isl_basic_map_free(bmap);
11262 return NULL;
11265 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11266 __isl_take isl_basic_map *bmap)
11268 if (!bmap)
11269 return NULL;
11271 if (!bmap->dim->nested[1])
11272 return bmap;
11274 bmap = isl_basic_map_cow(bmap);
11275 if (!bmap)
11276 return NULL;
11278 bmap->dim = isl_space_flatten_range(bmap->dim);
11279 if (!bmap->dim)
11280 goto error;
11282 bmap = isl_basic_map_finalize(bmap);
11284 return bmap;
11285 error:
11286 isl_basic_map_free(bmap);
11287 return NULL;
11290 /* Remove any internal structure from the spaces of domain and range of "map".
11292 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11294 if (!map)
11295 return NULL;
11297 if (!map->dim->nested[0] && !map->dim->nested[1])
11298 return map;
11300 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11303 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11305 return set_from_map(isl_map_flatten(set_to_map(set)));
11308 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11310 isl_space *dim, *flat_dim;
11311 isl_map *map;
11313 dim = isl_set_get_space(set);
11314 flat_dim = isl_space_flatten(isl_space_copy(dim));
11315 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11316 map = isl_map_intersect_domain(map, set);
11318 return map;
11321 /* Remove any internal structure from the space of the domain of "map".
11323 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11325 if (!map)
11326 return NULL;
11328 if (!map->dim->nested[0])
11329 return map;
11331 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11334 /* Remove any internal structure from the space of the range of "map".
11336 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11338 if (!map)
11339 return NULL;
11341 if (!map->dim->nested[1])
11342 return map;
11344 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11347 /* Reorder the dimensions of "bmap" according to the given dim_map
11348 * and set the dimension specification to "dim" and
11349 * perform Gaussian elimination on the result.
11351 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11352 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11354 isl_basic_map *res;
11355 unsigned flags;
11357 bmap = isl_basic_map_cow(bmap);
11358 if (!bmap || !dim || !dim_map)
11359 goto error;
11361 flags = bmap->flags;
11362 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11363 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11364 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11365 res = isl_basic_map_alloc_space(dim,
11366 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11367 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11368 if (res)
11369 res->flags = flags;
11370 res = isl_basic_map_gauss(res, NULL);
11371 res = isl_basic_map_finalize(res);
11372 return res;
11373 error:
11374 free(dim_map);
11375 isl_basic_map_free(bmap);
11376 isl_space_free(dim);
11377 return NULL;
11380 /* Reorder the dimensions of "map" according to given reordering.
11382 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11383 __isl_take isl_reordering *r)
11385 int i;
11386 struct isl_dim_map *dim_map;
11388 map = isl_map_cow(map);
11389 dim_map = isl_dim_map_from_reordering(r);
11390 if (!map || !r || !dim_map)
11391 goto error;
11393 for (i = 0; i < map->n; ++i) {
11394 struct isl_dim_map *dim_map_i;
11396 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11398 map->p[i] = isl_basic_map_realign(map->p[i],
11399 isl_space_copy(r->dim), dim_map_i);
11401 if (!map->p[i])
11402 goto error;
11405 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11407 isl_reordering_free(r);
11408 free(dim_map);
11409 return map;
11410 error:
11411 free(dim_map);
11412 isl_map_free(map);
11413 isl_reordering_free(r);
11414 return NULL;
11417 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11418 __isl_take isl_reordering *r)
11420 return set_from_map(isl_map_realign(set_to_map(set), r));
11423 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11424 __isl_take isl_space *model)
11426 isl_ctx *ctx;
11428 if (!map || !model)
11429 goto error;
11431 ctx = isl_space_get_ctx(model);
11432 if (!isl_space_has_named_params(model))
11433 isl_die(ctx, isl_error_invalid,
11434 "model has unnamed parameters", goto error);
11435 if (!isl_space_has_named_params(map->dim))
11436 isl_die(ctx, isl_error_invalid,
11437 "relation has unnamed parameters", goto error);
11438 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11439 isl_reordering *exp;
11441 model = isl_space_drop_dims(model, isl_dim_in,
11442 0, isl_space_dim(model, isl_dim_in));
11443 model = isl_space_drop_dims(model, isl_dim_out,
11444 0, isl_space_dim(model, isl_dim_out));
11445 exp = isl_parameter_alignment_reordering(map->dim, model);
11446 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11447 map = isl_map_realign(map, exp);
11450 isl_space_free(model);
11451 return map;
11452 error:
11453 isl_space_free(model);
11454 isl_map_free(map);
11455 return NULL;
11458 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11459 __isl_take isl_space *model)
11461 return isl_map_align_params(set, model);
11464 /* Align the parameters of "bmap" to those of "model", introducing
11465 * additional parameters if needed.
11467 __isl_give isl_basic_map *isl_basic_map_align_params(
11468 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11470 isl_ctx *ctx;
11472 if (!bmap || !model)
11473 goto error;
11475 ctx = isl_space_get_ctx(model);
11476 if (!isl_space_has_named_params(model))
11477 isl_die(ctx, isl_error_invalid,
11478 "model has unnamed parameters", goto error);
11479 if (!isl_space_has_named_params(bmap->dim))
11480 isl_die(ctx, isl_error_invalid,
11481 "relation has unnamed parameters", goto error);
11482 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11483 isl_reordering *exp;
11484 struct isl_dim_map *dim_map;
11486 model = isl_space_drop_dims(model, isl_dim_in,
11487 0, isl_space_dim(model, isl_dim_in));
11488 model = isl_space_drop_dims(model, isl_dim_out,
11489 0, isl_space_dim(model, isl_dim_out));
11490 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11491 exp = isl_reordering_extend_space(exp,
11492 isl_basic_map_get_space(bmap));
11493 dim_map = isl_dim_map_from_reordering(exp);
11494 bmap = isl_basic_map_realign(bmap,
11495 exp ? isl_space_copy(exp->dim) : NULL,
11496 isl_dim_map_extend(dim_map, bmap));
11497 isl_reordering_free(exp);
11498 free(dim_map);
11501 isl_space_free(model);
11502 return bmap;
11503 error:
11504 isl_space_free(model);
11505 isl_basic_map_free(bmap);
11506 return NULL;
11509 /* Align the parameters of "bset" to those of "model", introducing
11510 * additional parameters if needed.
11512 __isl_give isl_basic_set *isl_basic_set_align_params(
11513 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11515 return isl_basic_map_align_params(bset, model);
11518 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11519 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11520 enum isl_dim_type c2, enum isl_dim_type c3,
11521 enum isl_dim_type c4, enum isl_dim_type c5)
11523 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11524 struct isl_mat *mat;
11525 int i, j, k;
11526 int pos;
11528 if (!bmap)
11529 return NULL;
11530 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11531 isl_basic_map_total_dim(bmap) + 1);
11532 if (!mat)
11533 return NULL;
11534 for (i = 0; i < bmap->n_eq; ++i)
11535 for (j = 0, pos = 0; j < 5; ++j) {
11536 int off = isl_basic_map_offset(bmap, c[j]);
11537 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11538 isl_int_set(mat->row[i][pos],
11539 bmap->eq[i][off + k]);
11540 ++pos;
11544 return mat;
11547 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11548 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11549 enum isl_dim_type c2, enum isl_dim_type c3,
11550 enum isl_dim_type c4, enum isl_dim_type c5)
11552 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11553 struct isl_mat *mat;
11554 int i, j, k;
11555 int pos;
11557 if (!bmap)
11558 return NULL;
11559 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11560 isl_basic_map_total_dim(bmap) + 1);
11561 if (!mat)
11562 return NULL;
11563 for (i = 0; i < bmap->n_ineq; ++i)
11564 for (j = 0, pos = 0; j < 5; ++j) {
11565 int off = isl_basic_map_offset(bmap, c[j]);
11566 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11567 isl_int_set(mat->row[i][pos],
11568 bmap->ineq[i][off + k]);
11569 ++pos;
11573 return mat;
11576 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11577 __isl_take isl_space *dim,
11578 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11579 enum isl_dim_type c2, enum isl_dim_type c3,
11580 enum isl_dim_type c4, enum isl_dim_type c5)
11582 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11583 isl_basic_map *bmap;
11584 unsigned total;
11585 unsigned extra;
11586 int i, j, k, l;
11587 int pos;
11589 if (!dim || !eq || !ineq)
11590 goto error;
11592 if (eq->n_col != ineq->n_col)
11593 isl_die(dim->ctx, isl_error_invalid,
11594 "equalities and inequalities matrices should have "
11595 "same number of columns", goto error);
11597 total = 1 + isl_space_dim(dim, isl_dim_all);
11599 if (eq->n_col < total)
11600 isl_die(dim->ctx, isl_error_invalid,
11601 "number of columns too small", goto error);
11603 extra = eq->n_col - total;
11605 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11606 eq->n_row, ineq->n_row);
11607 if (!bmap)
11608 goto error;
11609 for (i = 0; i < extra; ++i) {
11610 k = isl_basic_map_alloc_div(bmap);
11611 if (k < 0)
11612 goto error;
11613 isl_int_set_si(bmap->div[k][0], 0);
11615 for (i = 0; i < eq->n_row; ++i) {
11616 l = isl_basic_map_alloc_equality(bmap);
11617 if (l < 0)
11618 goto error;
11619 for (j = 0, pos = 0; j < 5; ++j) {
11620 int off = isl_basic_map_offset(bmap, c[j]);
11621 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11622 isl_int_set(bmap->eq[l][off + k],
11623 eq->row[i][pos]);
11624 ++pos;
11628 for (i = 0; i < ineq->n_row; ++i) {
11629 l = isl_basic_map_alloc_inequality(bmap);
11630 if (l < 0)
11631 goto error;
11632 for (j = 0, pos = 0; j < 5; ++j) {
11633 int off = isl_basic_map_offset(bmap, c[j]);
11634 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11635 isl_int_set(bmap->ineq[l][off + k],
11636 ineq->row[i][pos]);
11637 ++pos;
11642 isl_space_free(dim);
11643 isl_mat_free(eq);
11644 isl_mat_free(ineq);
11646 bmap = isl_basic_map_simplify(bmap);
11647 return isl_basic_map_finalize(bmap);
11648 error:
11649 isl_space_free(dim);
11650 isl_mat_free(eq);
11651 isl_mat_free(ineq);
11652 return NULL;
11655 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11656 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11657 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11659 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11660 c1, c2, c3, c4, isl_dim_in);
11663 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11664 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11665 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11667 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11668 c1, c2, c3, c4, isl_dim_in);
11671 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11672 __isl_take isl_space *dim,
11673 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11674 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11676 isl_basic_map *bmap;
11677 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11678 c1, c2, c3, c4, isl_dim_in);
11679 return bset_from_bmap(bmap);
11682 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11684 if (!bmap)
11685 return isl_bool_error;
11687 return isl_space_can_zip(bmap->dim);
11690 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11692 if (!map)
11693 return isl_bool_error;
11695 return isl_space_can_zip(map->dim);
11698 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11699 * (A -> C) -> (B -> D).
11701 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11703 unsigned pos;
11704 unsigned n1;
11705 unsigned n2;
11707 if (!bmap)
11708 return NULL;
11710 if (!isl_basic_map_can_zip(bmap))
11711 isl_die(bmap->ctx, isl_error_invalid,
11712 "basic map cannot be zipped", goto error);
11713 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11714 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11715 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11716 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11717 bmap = isl_basic_map_cow(bmap);
11718 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11719 if (!bmap)
11720 return NULL;
11721 bmap->dim = isl_space_zip(bmap->dim);
11722 if (!bmap->dim)
11723 goto error;
11724 bmap = isl_basic_map_mark_final(bmap);
11725 return bmap;
11726 error:
11727 isl_basic_map_free(bmap);
11728 return NULL;
11731 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11732 * (A -> C) -> (B -> D).
11734 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11736 int i;
11738 if (!map)
11739 return NULL;
11741 if (!isl_map_can_zip(map))
11742 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11743 goto error);
11745 map = isl_map_cow(map);
11746 if (!map)
11747 return NULL;
11749 for (i = 0; i < map->n; ++i) {
11750 map->p[i] = isl_basic_map_zip(map->p[i]);
11751 if (!map->p[i])
11752 goto error;
11755 map->dim = isl_space_zip(map->dim);
11756 if (!map->dim)
11757 goto error;
11759 return map;
11760 error:
11761 isl_map_free(map);
11762 return NULL;
11765 /* Can we apply isl_basic_map_curry to "bmap"?
11766 * That is, does it have a nested relation in its domain?
11768 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11770 if (!bmap)
11771 return isl_bool_error;
11773 return isl_space_can_curry(bmap->dim);
11776 /* Can we apply isl_map_curry to "map"?
11777 * That is, does it have a nested relation in its domain?
11779 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11781 if (!map)
11782 return isl_bool_error;
11784 return isl_space_can_curry(map->dim);
11787 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11788 * A -> (B -> C).
11790 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11793 if (!bmap)
11794 return NULL;
11796 if (!isl_basic_map_can_curry(bmap))
11797 isl_die(bmap->ctx, isl_error_invalid,
11798 "basic map cannot be curried", goto error);
11799 bmap = isl_basic_map_cow(bmap);
11800 if (!bmap)
11801 return NULL;
11802 bmap->dim = isl_space_curry(bmap->dim);
11803 if (!bmap->dim)
11804 goto error;
11805 bmap = isl_basic_map_mark_final(bmap);
11806 return bmap;
11807 error:
11808 isl_basic_map_free(bmap);
11809 return NULL;
11812 /* Given a map (A -> B) -> C, return the corresponding map
11813 * A -> (B -> C).
11815 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11817 return isl_map_change_space(map, &isl_map_can_curry,
11818 "map cannot be curried", &isl_space_curry);
11821 /* Can isl_map_range_curry be applied to "map"?
11822 * That is, does it have a nested relation in its range,
11823 * the domain of which is itself a nested relation?
11825 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11827 if (!map)
11828 return isl_bool_error;
11830 return isl_space_can_range_curry(map->dim);
11833 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11834 * A -> (B -> (C -> D)).
11836 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11838 return isl_map_change_space(map, &isl_map_can_range_curry,
11839 "map range cannot be curried",
11840 &isl_space_range_curry);
11843 /* Can we apply isl_basic_map_uncurry to "bmap"?
11844 * That is, does it have a nested relation in its domain?
11846 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11848 if (!bmap)
11849 return isl_bool_error;
11851 return isl_space_can_uncurry(bmap->dim);
11854 /* Can we apply isl_map_uncurry to "map"?
11855 * That is, does it have a nested relation in its domain?
11857 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11859 if (!map)
11860 return isl_bool_error;
11862 return isl_space_can_uncurry(map->dim);
11865 /* Given a basic map A -> (B -> C), return the corresponding basic map
11866 * (A -> B) -> C.
11868 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11871 if (!bmap)
11872 return NULL;
11874 if (!isl_basic_map_can_uncurry(bmap))
11875 isl_die(bmap->ctx, isl_error_invalid,
11876 "basic map cannot be uncurried",
11877 return isl_basic_map_free(bmap));
11878 bmap = isl_basic_map_cow(bmap);
11879 if (!bmap)
11880 return NULL;
11881 bmap->dim = isl_space_uncurry(bmap->dim);
11882 if (!bmap->dim)
11883 return isl_basic_map_free(bmap);
11884 bmap = isl_basic_map_mark_final(bmap);
11885 return bmap;
11888 /* Given a map A -> (B -> C), return the corresponding map
11889 * (A -> B) -> C.
11891 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11893 return isl_map_change_space(map, &isl_map_can_uncurry,
11894 "map cannot be uncurried", &isl_space_uncurry);
11897 /* Construct a basic map mapping the domain of the affine expression
11898 * to a one-dimensional range prescribed by the affine expression.
11899 * If "rational" is set, then construct a rational basic map.
11901 * A NaN affine expression cannot be converted to a basic map.
11903 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11904 __isl_take isl_aff *aff, int rational)
11906 int k;
11907 int pos;
11908 isl_bool is_nan;
11909 isl_local_space *ls;
11910 isl_basic_map *bmap = NULL;
11912 if (!aff)
11913 return NULL;
11914 is_nan = isl_aff_is_nan(aff);
11915 if (is_nan < 0)
11916 goto error;
11917 if (is_nan)
11918 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11919 "cannot convert NaN", goto error);
11921 ls = isl_aff_get_local_space(aff);
11922 bmap = isl_basic_map_from_local_space(ls);
11923 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11924 k = isl_basic_map_alloc_equality(bmap);
11925 if (k < 0)
11926 goto error;
11928 pos = isl_basic_map_offset(bmap, isl_dim_out);
11929 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11930 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11931 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11932 aff->v->size - (pos + 1));
11934 isl_aff_free(aff);
11935 if (rational)
11936 bmap = isl_basic_map_set_rational(bmap);
11937 bmap = isl_basic_map_finalize(bmap);
11938 return bmap;
11939 error:
11940 isl_aff_free(aff);
11941 isl_basic_map_free(bmap);
11942 return NULL;
11945 /* Construct a basic map mapping the domain of the affine expression
11946 * to a one-dimensional range prescribed by the affine expression.
11948 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11950 return isl_basic_map_from_aff2(aff, 0);
11953 /* Construct a map mapping the domain of the affine expression
11954 * to a one-dimensional range prescribed by the affine expression.
11956 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11958 isl_basic_map *bmap;
11960 bmap = isl_basic_map_from_aff(aff);
11961 return isl_map_from_basic_map(bmap);
11964 /* Construct a basic map mapping the domain the multi-affine expression
11965 * to its range, with each dimension in the range equated to the
11966 * corresponding affine expression.
11967 * If "rational" is set, then construct a rational basic map.
11969 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
11970 __isl_take isl_multi_aff *maff, int rational)
11972 int i;
11973 isl_space *space;
11974 isl_basic_map *bmap;
11976 if (!maff)
11977 return NULL;
11979 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11980 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11981 "invalid space", goto error);
11983 space = isl_space_domain(isl_multi_aff_get_space(maff));
11984 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11985 if (rational)
11986 bmap = isl_basic_map_set_rational(bmap);
11988 for (i = 0; i < maff->n; ++i) {
11989 isl_aff *aff;
11990 isl_basic_map *bmap_i;
11992 aff = isl_aff_copy(maff->p[i]);
11993 bmap_i = isl_basic_map_from_aff2(aff, rational);
11995 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11998 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12000 isl_multi_aff_free(maff);
12001 return bmap;
12002 error:
12003 isl_multi_aff_free(maff);
12004 return NULL;
12007 /* Construct a basic map mapping the domain the multi-affine expression
12008 * to its range, with each dimension in the range equated to the
12009 * corresponding affine expression.
12011 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12012 __isl_take isl_multi_aff *ma)
12014 return isl_basic_map_from_multi_aff2(ma, 0);
12017 /* Construct a map mapping the domain the multi-affine expression
12018 * to its range, with each dimension in the range equated to the
12019 * corresponding affine expression.
12021 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12023 isl_basic_map *bmap;
12025 bmap = isl_basic_map_from_multi_aff(maff);
12026 return isl_map_from_basic_map(bmap);
12029 /* Construct a basic map mapping a domain in the given space to
12030 * to an n-dimensional range, with n the number of elements in the list,
12031 * where each coordinate in the range is prescribed by the
12032 * corresponding affine expression.
12033 * The domains of all affine expressions in the list are assumed to match
12034 * domain_dim.
12036 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12037 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12039 int i;
12040 isl_space *dim;
12041 isl_basic_map *bmap;
12043 if (!list)
12044 return NULL;
12046 dim = isl_space_from_domain(domain_dim);
12047 bmap = isl_basic_map_universe(dim);
12049 for (i = 0; i < list->n; ++i) {
12050 isl_aff *aff;
12051 isl_basic_map *bmap_i;
12053 aff = isl_aff_copy(list->p[i]);
12054 bmap_i = isl_basic_map_from_aff(aff);
12056 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12059 isl_aff_list_free(list);
12060 return bmap;
12063 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12064 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12066 return isl_map_equate(set, type1, pos1, type2, pos2);
12069 /* Construct a basic map where the given dimensions are equal to each other.
12071 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12072 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12074 isl_basic_map *bmap = NULL;
12075 int i;
12077 if (!space)
12078 return NULL;
12080 if (pos1 >= isl_space_dim(space, type1))
12081 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12082 "index out of bounds", goto error);
12083 if (pos2 >= isl_space_dim(space, type2))
12084 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12085 "index out of bounds", goto error);
12087 if (type1 == type2 && pos1 == pos2)
12088 return isl_basic_map_universe(space);
12090 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12091 i = isl_basic_map_alloc_equality(bmap);
12092 if (i < 0)
12093 goto error;
12094 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12095 pos1 += isl_basic_map_offset(bmap, type1);
12096 pos2 += isl_basic_map_offset(bmap, type2);
12097 isl_int_set_si(bmap->eq[i][pos1], -1);
12098 isl_int_set_si(bmap->eq[i][pos2], 1);
12099 bmap = isl_basic_map_finalize(bmap);
12100 isl_space_free(space);
12101 return bmap;
12102 error:
12103 isl_space_free(space);
12104 isl_basic_map_free(bmap);
12105 return NULL;
12108 /* Add a constraint imposing that the given two dimensions are equal.
12110 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12111 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12113 isl_basic_map *eq;
12115 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12117 bmap = isl_basic_map_intersect(bmap, eq);
12119 return bmap;
12122 /* Add a constraint imposing that the given two dimensions are equal.
12124 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12125 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12127 isl_basic_map *bmap;
12129 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12131 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12133 return map;
12136 /* Add a constraint imposing that the given two dimensions have opposite values.
12138 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12139 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12141 isl_basic_map *bmap = NULL;
12142 int i;
12144 if (!map)
12145 return NULL;
12147 if (pos1 >= isl_map_dim(map, type1))
12148 isl_die(map->ctx, isl_error_invalid,
12149 "index out of bounds", goto error);
12150 if (pos2 >= isl_map_dim(map, type2))
12151 isl_die(map->ctx, isl_error_invalid,
12152 "index out of bounds", goto error);
12154 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12155 i = isl_basic_map_alloc_equality(bmap);
12156 if (i < 0)
12157 goto error;
12158 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12159 pos1 += isl_basic_map_offset(bmap, type1);
12160 pos2 += isl_basic_map_offset(bmap, type2);
12161 isl_int_set_si(bmap->eq[i][pos1], 1);
12162 isl_int_set_si(bmap->eq[i][pos2], 1);
12163 bmap = isl_basic_map_finalize(bmap);
12165 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12167 return map;
12168 error:
12169 isl_basic_map_free(bmap);
12170 isl_map_free(map);
12171 return NULL;
12174 /* Construct a constraint imposing that the value of the first dimension is
12175 * greater than or equal to that of the second.
12177 static __isl_give isl_constraint *constraint_order_ge(
12178 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12179 enum isl_dim_type type2, int pos2)
12181 isl_constraint *c;
12183 if (!space)
12184 return NULL;
12186 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12188 if (pos1 >= isl_constraint_dim(c, type1))
12189 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12190 "index out of bounds", return isl_constraint_free(c));
12191 if (pos2 >= isl_constraint_dim(c, type2))
12192 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12193 "index out of bounds", return isl_constraint_free(c));
12195 if (type1 == type2 && pos1 == pos2)
12196 return c;
12198 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12199 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12201 return c;
12204 /* Add a constraint imposing that the value of the first dimension is
12205 * greater than or equal to that of the second.
12207 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12208 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12210 isl_constraint *c;
12211 isl_space *space;
12213 if (type1 == type2 && pos1 == pos2)
12214 return bmap;
12215 space = isl_basic_map_get_space(bmap);
12216 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12217 bmap = isl_basic_map_add_constraint(bmap, c);
12219 return bmap;
12222 /* Add a constraint imposing that the value of the first dimension is
12223 * greater than or equal to that of the second.
12225 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12226 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12228 isl_constraint *c;
12229 isl_space *space;
12231 if (type1 == type2 && pos1 == pos2)
12232 return map;
12233 space = isl_map_get_space(map);
12234 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12235 map = isl_map_add_constraint(map, c);
12237 return map;
12240 /* Add a constraint imposing that the value of the first dimension is
12241 * less than or equal to that of the second.
12243 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12244 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12246 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12249 /* Construct a basic map where the value of the first dimension is
12250 * greater than that of the second.
12252 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12253 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12255 isl_basic_map *bmap = NULL;
12256 int i;
12258 if (!space)
12259 return NULL;
12261 if (pos1 >= isl_space_dim(space, type1))
12262 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12263 "index out of bounds", goto error);
12264 if (pos2 >= isl_space_dim(space, type2))
12265 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12266 "index out of bounds", goto error);
12268 if (type1 == type2 && pos1 == pos2)
12269 return isl_basic_map_empty(space);
12271 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12272 i = isl_basic_map_alloc_inequality(bmap);
12273 if (i < 0)
12274 return isl_basic_map_free(bmap);
12275 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12276 pos1 += isl_basic_map_offset(bmap, type1);
12277 pos2 += isl_basic_map_offset(bmap, type2);
12278 isl_int_set_si(bmap->ineq[i][pos1], 1);
12279 isl_int_set_si(bmap->ineq[i][pos2], -1);
12280 isl_int_set_si(bmap->ineq[i][0], -1);
12281 bmap = isl_basic_map_finalize(bmap);
12283 return bmap;
12284 error:
12285 isl_space_free(space);
12286 isl_basic_map_free(bmap);
12287 return NULL;
12290 /* Add a constraint imposing that the value of the first dimension is
12291 * greater than that of the second.
12293 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12294 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12296 isl_basic_map *gt;
12298 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12300 bmap = isl_basic_map_intersect(bmap, gt);
12302 return bmap;
12305 /* Add a constraint imposing that the value of the first dimension is
12306 * greater than that of the second.
12308 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12309 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12311 isl_basic_map *bmap;
12313 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12315 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12317 return map;
12320 /* Add a constraint imposing that the value of the first dimension is
12321 * smaller than that of the second.
12323 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12324 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12326 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12329 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12330 int pos)
12332 isl_aff *div;
12333 isl_local_space *ls;
12335 if (!bmap)
12336 return NULL;
12338 if (!isl_basic_map_divs_known(bmap))
12339 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12340 "some divs are unknown", return NULL);
12342 ls = isl_basic_map_get_local_space(bmap);
12343 div = isl_local_space_get_div(ls, pos);
12344 isl_local_space_free(ls);
12346 return div;
12349 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12350 int pos)
12352 return isl_basic_map_get_div(bset, pos);
12355 /* Plug in "subs" for dimension "type", "pos" of "bset".
12357 * Let i be the dimension to replace and let "subs" be of the form
12359 * f/d
12361 * Any integer division with a non-zero coefficient for i,
12363 * floor((a i + g)/m)
12365 * is replaced by
12367 * floor((a f + d g)/(m d))
12369 * Constraints of the form
12371 * a i + g
12373 * are replaced by
12375 * a f + d g
12377 * We currently require that "subs" is an integral expression.
12378 * Handling rational expressions may require us to add stride constraints
12379 * as we do in isl_basic_set_preimage_multi_aff.
12381 __isl_give isl_basic_set *isl_basic_set_substitute(
12382 __isl_take isl_basic_set *bset,
12383 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12385 int i;
12386 isl_int v;
12387 isl_ctx *ctx;
12389 if (bset && isl_basic_set_plain_is_empty(bset))
12390 return bset;
12392 bset = isl_basic_set_cow(bset);
12393 if (!bset || !subs)
12394 goto error;
12396 ctx = isl_basic_set_get_ctx(bset);
12397 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12398 isl_die(ctx, isl_error_invalid,
12399 "spaces don't match", goto error);
12400 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12401 isl_die(ctx, isl_error_unsupported,
12402 "cannot handle divs yet", goto error);
12403 if (!isl_int_is_one(subs->v->el[0]))
12404 isl_die(ctx, isl_error_invalid,
12405 "can only substitute integer expressions", goto error);
12407 pos += isl_basic_set_offset(bset, type);
12409 isl_int_init(v);
12411 for (i = 0; i < bset->n_eq; ++i) {
12412 if (isl_int_is_zero(bset->eq[i][pos]))
12413 continue;
12414 isl_int_set(v, bset->eq[i][pos]);
12415 isl_int_set_si(bset->eq[i][pos], 0);
12416 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12417 v, subs->v->el + 1, subs->v->size - 1);
12420 for (i = 0; i < bset->n_ineq; ++i) {
12421 if (isl_int_is_zero(bset->ineq[i][pos]))
12422 continue;
12423 isl_int_set(v, bset->ineq[i][pos]);
12424 isl_int_set_si(bset->ineq[i][pos], 0);
12425 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12426 v, subs->v->el + 1, subs->v->size - 1);
12429 for (i = 0; i < bset->n_div; ++i) {
12430 if (isl_int_is_zero(bset->div[i][1 + pos]))
12431 continue;
12432 isl_int_set(v, bset->div[i][1 + pos]);
12433 isl_int_set_si(bset->div[i][1 + pos], 0);
12434 isl_seq_combine(bset->div[i] + 1,
12435 subs->v->el[0], bset->div[i] + 1,
12436 v, subs->v->el + 1, subs->v->size - 1);
12437 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12440 isl_int_clear(v);
12442 bset = isl_basic_set_simplify(bset);
12443 return isl_basic_set_finalize(bset);
12444 error:
12445 isl_basic_set_free(bset);
12446 return NULL;
12449 /* Plug in "subs" for dimension "type", "pos" of "set".
12451 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12452 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12454 int i;
12456 if (set && isl_set_plain_is_empty(set))
12457 return set;
12459 set = isl_set_cow(set);
12460 if (!set || !subs)
12461 goto error;
12463 for (i = set->n - 1; i >= 0; --i) {
12464 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12465 if (remove_if_empty(set, i) < 0)
12466 goto error;
12469 return set;
12470 error:
12471 isl_set_free(set);
12472 return NULL;
12475 /* Check if the range of "ma" is compatible with the domain or range
12476 * (depending on "type") of "bmap".
12478 static isl_stat check_basic_map_compatible_range_multi_aff(
12479 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12480 __isl_keep isl_multi_aff *ma)
12482 isl_bool m;
12483 isl_space *ma_space;
12485 ma_space = isl_multi_aff_get_space(ma);
12487 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12488 if (m < 0)
12489 goto error;
12490 if (!m)
12491 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12492 "parameters don't match", goto error);
12493 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12494 if (m < 0)
12495 goto error;
12496 if (!m)
12497 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12498 "spaces don't match", goto error);
12500 isl_space_free(ma_space);
12501 return isl_stat_ok;
12502 error:
12503 isl_space_free(ma_space);
12504 return isl_stat_error;
12507 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12508 * coefficients before the transformed range of dimensions,
12509 * the "n_after" coefficients after the transformed range of dimensions
12510 * and the coefficients of the other divs in "bmap".
12512 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12513 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12515 int i;
12516 int n_param;
12517 int n_set;
12518 isl_local_space *ls;
12520 if (n_div == 0)
12521 return 0;
12523 ls = isl_aff_get_domain_local_space(ma->p[0]);
12524 if (!ls)
12525 return -1;
12527 n_param = isl_local_space_dim(ls, isl_dim_param);
12528 n_set = isl_local_space_dim(ls, isl_dim_set);
12529 for (i = 0; i < n_div; ++i) {
12530 int o_bmap = 0, o_ls = 0;
12532 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12533 o_bmap += 1 + 1 + n_param;
12534 o_ls += 1 + 1 + n_param;
12535 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12536 o_bmap += n_before;
12537 isl_seq_cpy(bmap->div[i] + o_bmap,
12538 ls->div->row[i] + o_ls, n_set);
12539 o_bmap += n_set;
12540 o_ls += n_set;
12541 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12542 o_bmap += n_after;
12543 isl_seq_cpy(bmap->div[i] + o_bmap,
12544 ls->div->row[i] + o_ls, n_div);
12545 o_bmap += n_div;
12546 o_ls += n_div;
12547 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12548 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12549 goto error;
12552 isl_local_space_free(ls);
12553 return 0;
12554 error:
12555 isl_local_space_free(ls);
12556 return -1;
12559 /* How many stride constraints does "ma" enforce?
12560 * That is, how many of the affine expressions have a denominator
12561 * different from one?
12563 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12565 int i;
12566 int strides = 0;
12568 for (i = 0; i < ma->n; ++i)
12569 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12570 strides++;
12572 return strides;
12575 /* For each affine expression in ma of the form
12577 * x_i = (f_i y + h_i)/m_i
12579 * with m_i different from one, add a constraint to "bmap"
12580 * of the form
12582 * f_i y + h_i = m_i alpha_i
12584 * with alpha_i an additional existentially quantified variable.
12586 * The input variables of "ma" correspond to a subset of the variables
12587 * of "bmap". There are "n_before" variables in "bmap" before this
12588 * subset and "n_after" variables after this subset.
12589 * The integer divisions of the affine expressions in "ma" are assumed
12590 * to have been aligned. There are "n_div_ma" of them and
12591 * they appear first in "bmap", straight after the "n_after" variables.
12593 static __isl_give isl_basic_map *add_ma_strides(
12594 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12595 int n_before, int n_after, int n_div_ma)
12597 int i, k;
12598 int div;
12599 int total;
12600 int n_param;
12601 int n_in;
12603 total = isl_basic_map_total_dim(bmap);
12604 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12605 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12606 for (i = 0; i < ma->n; ++i) {
12607 int o_bmap = 0, o_ma = 1;
12609 if (isl_int_is_one(ma->p[i]->v->el[0]))
12610 continue;
12611 div = isl_basic_map_alloc_div(bmap);
12612 k = isl_basic_map_alloc_equality(bmap);
12613 if (div < 0 || k < 0)
12614 goto error;
12615 isl_int_set_si(bmap->div[div][0], 0);
12616 isl_seq_cpy(bmap->eq[k] + o_bmap,
12617 ma->p[i]->v->el + o_ma, 1 + n_param);
12618 o_bmap += 1 + n_param;
12619 o_ma += 1 + n_param;
12620 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12621 o_bmap += n_before;
12622 isl_seq_cpy(bmap->eq[k] + o_bmap,
12623 ma->p[i]->v->el + o_ma, n_in);
12624 o_bmap += n_in;
12625 o_ma += n_in;
12626 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12627 o_bmap += n_after;
12628 isl_seq_cpy(bmap->eq[k] + o_bmap,
12629 ma->p[i]->v->el + o_ma, n_div_ma);
12630 o_bmap += n_div_ma;
12631 o_ma += n_div_ma;
12632 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12633 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12634 total++;
12637 return bmap;
12638 error:
12639 isl_basic_map_free(bmap);
12640 return NULL;
12643 /* Replace the domain or range space (depending on "type) of "space" by "set".
12645 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12646 enum isl_dim_type type, __isl_take isl_space *set)
12648 if (type == isl_dim_in) {
12649 space = isl_space_range(space);
12650 space = isl_space_map_from_domain_and_range(set, space);
12651 } else {
12652 space = isl_space_domain(space);
12653 space = isl_space_map_from_domain_and_range(space, set);
12656 return space;
12659 /* Compute the preimage of the domain or range (depending on "type")
12660 * of "bmap" under the function represented by "ma".
12661 * In other words, plug in "ma" in the domain or range of "bmap".
12662 * The result is a basic map that lives in the same space as "bmap"
12663 * except that the domain or range has been replaced by
12664 * the domain space of "ma".
12666 * If bmap is represented by
12668 * A(p) + S u + B x + T v + C(divs) >= 0,
12670 * where u and x are input and output dimensions if type == isl_dim_out
12671 * while x and v are input and output dimensions if type == isl_dim_in,
12672 * and ma is represented by
12674 * x = D(p) + F(y) + G(divs')
12676 * then the result is
12678 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12680 * The divs in the input set are similarly adjusted.
12681 * In particular
12683 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12685 * becomes
12687 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12688 * B_i G(divs') + c_i(divs))/n_i)
12690 * If bmap is not a rational map and if F(y) involves any denominators
12692 * x_i = (f_i y + h_i)/m_i
12694 * then additional constraints are added to ensure that we only
12695 * map back integer points. That is we enforce
12697 * f_i y + h_i = m_i alpha_i
12699 * with alpha_i an additional existentially quantified variable.
12701 * We first copy over the divs from "ma".
12702 * Then we add the modified constraints and divs from "bmap".
12703 * Finally, we add the stride constraints, if needed.
12705 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12706 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12707 __isl_take isl_multi_aff *ma)
12709 int i, k;
12710 isl_space *space;
12711 isl_basic_map *res = NULL;
12712 int n_before, n_after, n_div_bmap, n_div_ma;
12713 isl_int f, c1, c2, g;
12714 isl_bool rational;
12715 int strides;
12717 isl_int_init(f);
12718 isl_int_init(c1);
12719 isl_int_init(c2);
12720 isl_int_init(g);
12722 ma = isl_multi_aff_align_divs(ma);
12723 if (!bmap || !ma)
12724 goto error;
12725 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12726 goto error;
12728 if (type == isl_dim_in) {
12729 n_before = 0;
12730 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12731 } else {
12732 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12733 n_after = 0;
12735 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12736 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12738 space = isl_multi_aff_get_domain_space(ma);
12739 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12740 rational = isl_basic_map_is_rational(bmap);
12741 strides = rational ? 0 : multi_aff_strides(ma);
12742 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12743 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12744 if (rational)
12745 res = isl_basic_map_set_rational(res);
12747 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12748 if (isl_basic_map_alloc_div(res) < 0)
12749 goto error;
12751 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12752 goto error;
12754 for (i = 0; i < bmap->n_eq; ++i) {
12755 k = isl_basic_map_alloc_equality(res);
12756 if (k < 0)
12757 goto error;
12758 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12759 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12762 for (i = 0; i < bmap->n_ineq; ++i) {
12763 k = isl_basic_map_alloc_inequality(res);
12764 if (k < 0)
12765 goto error;
12766 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12767 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12770 for (i = 0; i < bmap->n_div; ++i) {
12771 if (isl_int_is_zero(bmap->div[i][0])) {
12772 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12773 continue;
12775 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12776 n_before, n_after, n_div_ma, n_div_bmap,
12777 f, c1, c2, g, 1);
12780 if (strides)
12781 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12783 isl_int_clear(f);
12784 isl_int_clear(c1);
12785 isl_int_clear(c2);
12786 isl_int_clear(g);
12787 isl_basic_map_free(bmap);
12788 isl_multi_aff_free(ma);
12789 res = isl_basic_map_simplify(res);
12790 return isl_basic_map_finalize(res);
12791 error:
12792 isl_int_clear(f);
12793 isl_int_clear(c1);
12794 isl_int_clear(c2);
12795 isl_int_clear(g);
12796 isl_basic_map_free(bmap);
12797 isl_multi_aff_free(ma);
12798 isl_basic_map_free(res);
12799 return NULL;
12802 /* Compute the preimage of "bset" under the function represented by "ma".
12803 * In other words, plug in "ma" in "bset". The result is a basic set
12804 * that lives in the domain space of "ma".
12806 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12807 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12809 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12812 /* Compute the preimage of the domain of "bmap" under the function
12813 * represented by "ma".
12814 * In other words, plug in "ma" in the domain of "bmap".
12815 * The result is a basic map that lives in the same space as "bmap"
12816 * except that the domain has been replaced by the domain space of "ma".
12818 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12819 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12821 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12824 /* Compute the preimage of the range of "bmap" under the function
12825 * represented by "ma".
12826 * In other words, plug in "ma" in the range of "bmap".
12827 * The result is a basic map that lives in the same space as "bmap"
12828 * except that the range has been replaced by the domain space of "ma".
12830 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12831 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12833 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12836 /* Check if the range of "ma" is compatible with the domain or range
12837 * (depending on "type") of "map".
12838 * Return isl_stat_error if anything is wrong.
12840 static isl_stat check_map_compatible_range_multi_aff(
12841 __isl_keep isl_map *map, enum isl_dim_type type,
12842 __isl_keep isl_multi_aff *ma)
12844 isl_bool m;
12845 isl_space *ma_space;
12847 ma_space = isl_multi_aff_get_space(ma);
12848 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12849 isl_space_free(ma_space);
12850 if (m < 0)
12851 return isl_stat_error;
12852 if (!m)
12853 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12854 "spaces don't match", return isl_stat_error);
12855 return isl_stat_ok;
12858 /* Compute the preimage of the domain or range (depending on "type")
12859 * of "map" under the function represented by "ma".
12860 * In other words, plug in "ma" in the domain or range of "map".
12861 * The result is a map that lives in the same space as "map"
12862 * except that the domain or range has been replaced by
12863 * the domain space of "ma".
12865 * The parameters are assumed to have been aligned.
12867 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12868 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12870 int i;
12871 isl_space *space;
12873 map = isl_map_cow(map);
12874 ma = isl_multi_aff_align_divs(ma);
12875 if (!map || !ma)
12876 goto error;
12877 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12878 goto error;
12880 for (i = 0; i < map->n; ++i) {
12881 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12882 isl_multi_aff_copy(ma));
12883 if (!map->p[i])
12884 goto error;
12887 space = isl_multi_aff_get_domain_space(ma);
12888 space = isl_space_set(isl_map_get_space(map), type, space);
12890 isl_space_free(map->dim);
12891 map->dim = space;
12892 if (!map->dim)
12893 goto error;
12895 isl_multi_aff_free(ma);
12896 if (map->n > 1)
12897 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12898 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12899 return map;
12900 error:
12901 isl_multi_aff_free(ma);
12902 isl_map_free(map);
12903 return NULL;
12906 /* Compute the preimage of the domain or range (depending on "type")
12907 * of "map" under the function represented by "ma".
12908 * In other words, plug in "ma" in the domain or range of "map".
12909 * The result is a map that lives in the same space as "map"
12910 * except that the domain or range has been replaced by
12911 * the domain space of "ma".
12913 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12914 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12916 if (!map || !ma)
12917 goto error;
12919 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12920 return map_preimage_multi_aff(map, type, ma);
12922 if (!isl_space_has_named_params(map->dim) ||
12923 !isl_space_has_named_params(ma->space))
12924 isl_die(map->ctx, isl_error_invalid,
12925 "unaligned unnamed parameters", goto error);
12926 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12927 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12929 return map_preimage_multi_aff(map, type, ma);
12930 error:
12931 isl_multi_aff_free(ma);
12932 return isl_map_free(map);
12935 /* Compute the preimage of "set" under the function represented by "ma".
12936 * In other words, plug in "ma" in "set". The result is a set
12937 * that lives in the domain space of "ma".
12939 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12940 __isl_take isl_multi_aff *ma)
12942 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12945 /* Compute the preimage of the domain of "map" under the function
12946 * represented by "ma".
12947 * In other words, plug in "ma" in the domain of "map".
12948 * The result is a map that lives in the same space as "map"
12949 * except that the domain has been replaced by the domain space of "ma".
12951 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12952 __isl_take isl_multi_aff *ma)
12954 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12957 /* Compute the preimage of the range of "map" under the function
12958 * represented by "ma".
12959 * In other words, plug in "ma" in the range of "map".
12960 * The result is a map that lives in the same space as "map"
12961 * except that the range has been replaced by the domain space of "ma".
12963 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12964 __isl_take isl_multi_aff *ma)
12966 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12969 /* Compute the preimage of "map" under the function represented by "pma".
12970 * In other words, plug in "pma" in the domain or range of "map".
12971 * The result is a map that lives in the same space as "map",
12972 * except that the space of type "type" has been replaced by
12973 * the domain space of "pma".
12975 * The parameters of "map" and "pma" are assumed to have been aligned.
12977 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12978 __isl_take isl_map *map, enum isl_dim_type type,
12979 __isl_take isl_pw_multi_aff *pma)
12981 int i;
12982 isl_map *res;
12984 if (!pma)
12985 goto error;
12987 if (pma->n == 0) {
12988 isl_pw_multi_aff_free(pma);
12989 res = isl_map_empty(isl_map_get_space(map));
12990 isl_map_free(map);
12991 return res;
12994 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12995 isl_multi_aff_copy(pma->p[0].maff));
12996 if (type == isl_dim_in)
12997 res = isl_map_intersect_domain(res,
12998 isl_map_copy(pma->p[0].set));
12999 else
13000 res = isl_map_intersect_range(res,
13001 isl_map_copy(pma->p[0].set));
13003 for (i = 1; i < pma->n; ++i) {
13004 isl_map *res_i;
13006 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13007 isl_multi_aff_copy(pma->p[i].maff));
13008 if (type == isl_dim_in)
13009 res_i = isl_map_intersect_domain(res_i,
13010 isl_map_copy(pma->p[i].set));
13011 else
13012 res_i = isl_map_intersect_range(res_i,
13013 isl_map_copy(pma->p[i].set));
13014 res = isl_map_union(res, res_i);
13017 isl_pw_multi_aff_free(pma);
13018 isl_map_free(map);
13019 return res;
13020 error:
13021 isl_pw_multi_aff_free(pma);
13022 isl_map_free(map);
13023 return NULL;
13026 /* Compute the preimage of "map" under the function represented by "pma".
13027 * In other words, plug in "pma" in the domain or range of "map".
13028 * The result is a map that lives in the same space as "map",
13029 * except that the space of type "type" has been replaced by
13030 * the domain space of "pma".
13032 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13033 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13035 if (!map || !pma)
13036 goto error;
13038 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13039 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13041 if (!isl_space_has_named_params(map->dim) ||
13042 !isl_space_has_named_params(pma->dim))
13043 isl_die(map->ctx, isl_error_invalid,
13044 "unaligned unnamed parameters", goto error);
13045 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13046 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13048 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13049 error:
13050 isl_pw_multi_aff_free(pma);
13051 return isl_map_free(map);
13054 /* Compute the preimage of "set" under the function represented by "pma".
13055 * In other words, plug in "pma" in "set". The result is a set
13056 * that lives in the domain space of "pma".
13058 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13059 __isl_take isl_pw_multi_aff *pma)
13061 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13064 /* Compute the preimage of the domain of "map" under the function
13065 * represented by "pma".
13066 * In other words, plug in "pma" in the domain of "map".
13067 * The result is a map that lives in the same space as "map",
13068 * except that domain space has been replaced by the domain space of "pma".
13070 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13071 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13073 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13076 /* Compute the preimage of the range of "map" under the function
13077 * represented by "pma".
13078 * In other words, plug in "pma" in the range of "map".
13079 * The result is a map that lives in the same space as "map",
13080 * except that range space has been replaced by the domain space of "pma".
13082 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13083 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13085 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13088 /* Compute the preimage of "map" under the function represented by "mpa".
13089 * In other words, plug in "mpa" in the domain or range of "map".
13090 * The result is a map that lives in the same space as "map",
13091 * except that the space of type "type" has been replaced by
13092 * the domain space of "mpa".
13094 * If the map does not involve any constraints that refer to the
13095 * dimensions of the substituted space, then the only possible
13096 * effect of "mpa" on the map is to map the space to a different space.
13097 * We create a separate isl_multi_aff to effectuate this change
13098 * in order to avoid spurious splitting of the map along the pieces
13099 * of "mpa".
13101 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13102 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13104 int n;
13105 isl_pw_multi_aff *pma;
13107 if (!map || !mpa)
13108 goto error;
13110 n = isl_map_dim(map, type);
13111 if (!isl_map_involves_dims(map, type, 0, n)) {
13112 isl_space *space;
13113 isl_multi_aff *ma;
13115 space = isl_multi_pw_aff_get_space(mpa);
13116 isl_multi_pw_aff_free(mpa);
13117 ma = isl_multi_aff_zero(space);
13118 return isl_map_preimage_multi_aff(map, type, ma);
13121 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13122 return isl_map_preimage_pw_multi_aff(map, type, pma);
13123 error:
13124 isl_map_free(map);
13125 isl_multi_pw_aff_free(mpa);
13126 return NULL;
13129 /* Compute the preimage of "map" under the function represented by "mpa".
13130 * In other words, plug in "mpa" in the domain "map".
13131 * The result is a map that lives in the same space as "map",
13132 * except that domain space has been replaced by the domain space of "mpa".
13134 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13135 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13137 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13140 /* Compute the preimage of "set" by the function represented by "mpa".
13141 * In other words, plug in "mpa" in "set".
13143 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13144 __isl_take isl_multi_pw_aff *mpa)
13146 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13149 /* Are the "n" "coefficients" starting at "first" of the integer division
13150 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13151 * to each other?
13152 * The "coefficient" at position 0 is the denominator.
13153 * The "coefficient" at position 1 is the constant term.
13155 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13156 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13157 unsigned first, unsigned n)
13159 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13160 return isl_bool_error;
13161 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13162 return isl_bool_error;
13163 return isl_seq_eq(bmap1->div[pos1] + first,
13164 bmap2->div[pos2] + first, n);
13167 /* Are the integer division expressions at position "pos1" in "bmap1" and
13168 * "pos2" in "bmap2" equal to each other, except that the constant terms
13169 * are different?
13171 isl_bool isl_basic_map_equal_div_expr_except_constant(
13172 __isl_keep isl_basic_map *bmap1, int pos1,
13173 __isl_keep isl_basic_map *bmap2, int pos2)
13175 isl_bool equal;
13176 unsigned total;
13178 if (!bmap1 || !bmap2)
13179 return isl_bool_error;
13180 total = isl_basic_map_total_dim(bmap1);
13181 if (total != isl_basic_map_total_dim(bmap2))
13182 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13183 "incomparable div expressions", return isl_bool_error);
13184 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13185 0, 1);
13186 if (equal < 0 || !equal)
13187 return equal;
13188 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13189 1, 1);
13190 if (equal < 0 || equal)
13191 return isl_bool_not(equal);
13192 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13193 2, total);
13196 /* Replace the numerator of the constant term of the integer division
13197 * expression at position "div" in "bmap" by "value".
13198 * The caller guarantees that this does not change the meaning
13199 * of the input.
13201 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13202 __isl_take isl_basic_map *bmap, int div, int value)
13204 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13205 return isl_basic_map_free(bmap);
13207 isl_int_set_si(bmap->div[div][1], value);
13209 return bmap;
13212 /* Is the point "inner" internal to inequality constraint "ineq"
13213 * of "bset"?
13214 * The point is considered to be internal to the inequality constraint,
13215 * if it strictly lies on the positive side of the inequality constraint,
13216 * or if it lies on the constraint and the constraint is lexico-positive.
13218 static isl_bool is_internal(__isl_keep isl_vec *inner,
13219 __isl_keep isl_basic_set *bset, int ineq)
13221 isl_ctx *ctx;
13222 int pos;
13223 unsigned total;
13225 if (!inner || !bset)
13226 return isl_bool_error;
13228 ctx = isl_basic_set_get_ctx(bset);
13229 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13230 &ctx->normalize_gcd);
13231 if (!isl_int_is_zero(ctx->normalize_gcd))
13232 return isl_int_is_nonneg(ctx->normalize_gcd);
13234 total = isl_basic_set_dim(bset, isl_dim_all);
13235 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13236 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13239 /* Tighten the inequality constraints of "bset" that are outward with respect
13240 * to the point "vec".
13241 * That is, tighten the constraints that are not satisfied by "vec".
13243 * "vec" is a point internal to some superset S of "bset" that is used
13244 * to make the subsets of S disjoint, by tightening one half of the constraints
13245 * that separate two subsets. In particular, the constraints of S
13246 * are all satisfied by "vec" and should not be tightened.
13247 * Of the internal constraints, those that have "vec" on the outside
13248 * are tightened. The shared facet is included in the adjacent subset
13249 * with the opposite constraint.
13250 * For constraints that saturate "vec", this criterion cannot be used
13251 * to determine which of the two sides should be tightened.
13252 * Instead, the sign of the first non-zero coefficient is used
13253 * to make this choice. Note that this second criterion is never used
13254 * on the constraints of S since "vec" is interior to "S".
13256 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13257 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13259 int j;
13261 bset = isl_basic_set_cow(bset);
13262 if (!bset)
13263 return NULL;
13264 for (j = 0; j < bset->n_ineq; ++j) {
13265 isl_bool internal;
13267 internal = is_internal(vec, bset, j);
13268 if (internal < 0)
13269 return isl_basic_set_free(bset);
13270 if (internal)
13271 continue;
13272 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13275 return bset;