isl_map_simplify.c: better_div_constraint: return isl_bool
[isl.git] / isl_map.c
blob1f933915d98acee0a69429ba0eed009744c28193
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 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
826 if (!bmap)
827 return -1;
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 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
888 int has_rational = 1;
889 unsigned total;
891 if (!bmap)
892 return -1;
893 if (isl_basic_map_plain_is_empty(bmap))
894 return 0;
895 if (!isl_basic_map_is_rational(bmap))
896 return 0;
897 bmap = isl_basic_map_copy(bmap);
898 bmap = isl_basic_map_implicit_equalities(bmap);
899 if (!bmap)
900 return -1;
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 = 0;
919 isl_basic_map_free(bmap);
921 return has_rational;
924 /* Does "map" contain any rational points?
926 int isl_map_has_rational(__isl_keep isl_map *map)
928 int i;
929 int has_rational;
931 if (!map)
932 return -1;
933 for (i = 0; i < map->n; ++i) {
934 has_rational = isl_basic_map_has_rational(map->p[i]);
935 if (has_rational < 0)
936 return -1;
937 if (has_rational)
938 return 1;
940 return 0;
943 /* Does "set" contain any rational points?
945 int isl_set_has_rational(__isl_keep isl_set *set)
947 return isl_map_has_rational(set);
950 /* Is this basic set a parameter domain?
952 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
954 if (!bset)
955 return isl_bool_error;
956 return isl_space_is_params(bset->dim);
959 /* Is this set a parameter domain?
961 isl_bool isl_set_is_params(__isl_keep isl_set *set)
963 if (!set)
964 return isl_bool_error;
965 return isl_space_is_params(set->dim);
968 /* Is this map actually a parameter domain?
969 * Users should never call this function. Outside of isl,
970 * a map can never be a parameter domain.
972 isl_bool isl_map_is_params(__isl_keep isl_map *map)
974 if (!map)
975 return isl_bool_error;
976 return isl_space_is_params(map->dim);
979 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
980 struct isl_basic_map *bmap, unsigned extra,
981 unsigned n_eq, unsigned n_ineq)
983 int i;
984 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
986 bmap->ctx = ctx;
987 isl_ctx_ref(ctx);
989 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
990 if (isl_blk_is_error(bmap->block))
991 goto error;
993 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
994 if ((n_ineq + n_eq) && !bmap->ineq)
995 goto error;
997 if (extra == 0) {
998 bmap->block2 = isl_blk_empty();
999 bmap->div = NULL;
1000 } else {
1001 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1002 if (isl_blk_is_error(bmap->block2))
1003 goto error;
1005 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1006 if (!bmap->div)
1007 goto error;
1010 for (i = 0; i < n_ineq + n_eq; ++i)
1011 bmap->ineq[i] = bmap->block.data + i * row_size;
1013 for (i = 0; i < extra; ++i)
1014 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1016 bmap->ref = 1;
1017 bmap->flags = 0;
1018 bmap->c_size = n_eq + n_ineq;
1019 bmap->eq = bmap->ineq + n_ineq;
1020 bmap->extra = extra;
1021 bmap->n_eq = 0;
1022 bmap->n_ineq = 0;
1023 bmap->n_div = 0;
1024 bmap->sample = NULL;
1026 return bmap;
1027 error:
1028 isl_basic_map_free(bmap);
1029 return NULL;
1032 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1033 unsigned nparam, unsigned dim, unsigned extra,
1034 unsigned n_eq, unsigned n_ineq)
1036 struct isl_basic_map *bmap;
1037 isl_space *space;
1039 space = isl_space_set_alloc(ctx, nparam, dim);
1040 if (!space)
1041 return NULL;
1043 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1044 return bset_from_bmap(bmap);
1047 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1048 unsigned extra, unsigned n_eq, unsigned n_ineq)
1050 struct isl_basic_map *bmap;
1051 if (!dim)
1052 return NULL;
1053 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1054 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1055 return bset_from_bmap(bmap);
1056 error:
1057 isl_space_free(dim);
1058 return NULL;
1061 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1062 unsigned extra, unsigned n_eq, unsigned n_ineq)
1064 struct isl_basic_map *bmap;
1066 if (!dim)
1067 return NULL;
1068 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1069 if (!bmap)
1070 goto error;
1071 bmap->dim = dim;
1073 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1074 error:
1075 isl_space_free(dim);
1076 return NULL;
1079 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1080 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1081 unsigned n_eq, unsigned n_ineq)
1083 struct isl_basic_map *bmap;
1084 isl_space *dim;
1086 dim = isl_space_alloc(ctx, nparam, in, out);
1087 if (!dim)
1088 return NULL;
1090 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1091 return bmap;
1094 static void dup_constraints(
1095 struct isl_basic_map *dst, struct isl_basic_map *src)
1097 int i;
1098 unsigned total = isl_basic_map_total_dim(src);
1100 for (i = 0; i < src->n_eq; ++i) {
1101 int j = isl_basic_map_alloc_equality(dst);
1102 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1105 for (i = 0; i < src->n_ineq; ++i) {
1106 int j = isl_basic_map_alloc_inequality(dst);
1107 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1110 for (i = 0; i < src->n_div; ++i) {
1111 int j = isl_basic_map_alloc_div(dst);
1112 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1114 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1117 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1119 struct isl_basic_map *dup;
1121 if (!bmap)
1122 return NULL;
1123 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1124 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1125 if (!dup)
1126 return NULL;
1127 dup_constraints(dup, bmap);
1128 dup->flags = bmap->flags;
1129 dup->sample = isl_vec_copy(bmap->sample);
1130 return dup;
1133 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1135 struct isl_basic_map *dup;
1137 dup = isl_basic_map_dup(bset_to_bmap(bset));
1138 return bset_from_bmap(dup);
1141 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1143 if (!bset)
1144 return NULL;
1146 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1147 bset->ref++;
1148 return bset;
1150 return isl_basic_set_dup(bset);
1153 struct isl_set *isl_set_copy(struct isl_set *set)
1155 if (!set)
1156 return NULL;
1158 set->ref++;
1159 return set;
1162 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1164 if (!bmap)
1165 return NULL;
1167 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1168 bmap->ref++;
1169 return bmap;
1171 bmap = isl_basic_map_dup(bmap);
1172 if (bmap)
1173 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1174 return bmap;
1177 struct isl_map *isl_map_copy(struct isl_map *map)
1179 if (!map)
1180 return NULL;
1182 map->ref++;
1183 return map;
1186 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1188 if (!bmap)
1189 return NULL;
1191 if (--bmap->ref > 0)
1192 return NULL;
1194 isl_ctx_deref(bmap->ctx);
1195 free(bmap->div);
1196 isl_blk_free(bmap->ctx, bmap->block2);
1197 free(bmap->ineq);
1198 isl_blk_free(bmap->ctx, bmap->block);
1199 isl_vec_free(bmap->sample);
1200 isl_space_free(bmap->dim);
1201 free(bmap);
1203 return NULL;
1206 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1208 return isl_basic_map_free(bset_to_bmap(bset));
1211 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1213 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1216 __isl_give isl_map *isl_map_align_params_map_map_and(
1217 __isl_take isl_map *map1, __isl_take isl_map *map2,
1218 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1219 __isl_take isl_map *map2))
1221 if (!map1 || !map2)
1222 goto error;
1223 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1224 return fn(map1, map2);
1225 if (!isl_space_has_named_params(map1->dim) ||
1226 !isl_space_has_named_params(map2->dim))
1227 isl_die(map1->ctx, isl_error_invalid,
1228 "unaligned unnamed parameters", goto error);
1229 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1230 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1231 return fn(map1, map2);
1232 error:
1233 isl_map_free(map1);
1234 isl_map_free(map2);
1235 return NULL;
1238 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1239 __isl_keep isl_map *map2,
1240 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1242 isl_bool r;
1244 if (!map1 || !map2)
1245 return isl_bool_error;
1246 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1247 return fn(map1, map2);
1248 if (!isl_space_has_named_params(map1->dim) ||
1249 !isl_space_has_named_params(map2->dim))
1250 isl_die(map1->ctx, isl_error_invalid,
1251 "unaligned unnamed parameters", return isl_bool_error);
1252 map1 = isl_map_copy(map1);
1253 map2 = isl_map_copy(map2);
1254 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1255 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1256 r = fn(map1, map2);
1257 isl_map_free(map1);
1258 isl_map_free(map2);
1259 return r;
1262 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1264 struct isl_ctx *ctx;
1265 if (!bmap)
1266 return -1;
1267 ctx = bmap->ctx;
1268 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1269 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1270 return -1);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1274 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1275 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1276 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1277 isl_int *t;
1278 int j = isl_basic_map_alloc_inequality(bmap);
1279 if (j < 0)
1280 return -1;
1281 t = bmap->ineq[j];
1282 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1283 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1284 bmap->eq[-1] = t;
1285 bmap->n_eq++;
1286 bmap->n_ineq--;
1287 bmap->eq--;
1288 return 0;
1290 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1291 bmap->extra - bmap->n_div);
1292 return bmap->n_eq++;
1295 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1297 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1300 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1302 if (!bmap)
1303 return -1;
1304 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1305 bmap->n_eq -= n;
1306 return 0;
1309 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1311 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1314 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1316 isl_int *t;
1317 if (!bmap)
1318 return -1;
1319 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1321 if (pos != bmap->n_eq - 1) {
1322 t = bmap->eq[pos];
1323 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1324 bmap->eq[bmap->n_eq - 1] = t;
1326 bmap->n_eq--;
1327 return 0;
1330 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1332 return isl_basic_map_drop_equality(bset_to_bmap(bset), pos);
1335 /* Turn inequality "pos" of "bmap" into an equality.
1337 * In particular, we move the inequality in front of the equalities
1338 * and move the last inequality in the position of the moved inequality.
1339 * Note that isl_tab_make_equalities_explicit depends on this particular
1340 * change in the ordering of the constraints.
1342 void isl_basic_map_inequality_to_equality(
1343 struct isl_basic_map *bmap, unsigned pos)
1345 isl_int *t;
1347 t = bmap->ineq[pos];
1348 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1349 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1350 bmap->eq[-1] = t;
1351 bmap->n_eq++;
1352 bmap->n_ineq--;
1353 bmap->eq--;
1354 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1355 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1356 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1357 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1360 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1362 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1365 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1367 struct isl_ctx *ctx;
1368 if (!bmap)
1369 return -1;
1370 ctx = bmap->ctx;
1371 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1372 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1373 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1374 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1375 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1376 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1377 1 + isl_basic_map_total_dim(bmap),
1378 bmap->extra - bmap->n_div);
1379 return bmap->n_ineq++;
1382 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1384 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1387 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1389 if (!bmap)
1390 return -1;
1391 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1392 bmap->n_ineq -= n;
1393 return 0;
1396 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1398 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1401 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1403 isl_int *t;
1404 if (!bmap)
1405 return -1;
1406 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1408 if (pos != bmap->n_ineq - 1) {
1409 t = bmap->ineq[pos];
1410 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1411 bmap->ineq[bmap->n_ineq - 1] = t;
1412 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1414 bmap->n_ineq--;
1415 return 0;
1418 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1420 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1423 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1424 isl_int *eq)
1426 int k;
1428 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1429 if (!bmap)
1430 return NULL;
1431 k = isl_basic_map_alloc_equality(bmap);
1432 if (k < 0)
1433 goto error;
1434 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1435 return bmap;
1436 error:
1437 isl_basic_map_free(bmap);
1438 return NULL;
1441 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1442 isl_int *eq)
1444 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1447 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1448 isl_int *ineq)
1450 int k;
1452 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1453 if (!bmap)
1454 return NULL;
1455 k = isl_basic_map_alloc_inequality(bmap);
1456 if (k < 0)
1457 goto error;
1458 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1459 return bmap;
1460 error:
1461 isl_basic_map_free(bmap);
1462 return NULL;
1465 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1466 isl_int *ineq)
1468 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1471 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1473 if (!bmap)
1474 return -1;
1475 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1476 isl_seq_clr(bmap->div[bmap->n_div] +
1477 1 + 1 + isl_basic_map_total_dim(bmap),
1478 bmap->extra - bmap->n_div);
1479 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1480 return bmap->n_div++;
1483 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1485 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1488 /* Check that there are "n" dimensions of type "type" starting at "first"
1489 * in "bmap".
1491 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1492 enum isl_dim_type type, unsigned first, unsigned n)
1494 unsigned dim;
1496 if (!bmap)
1497 return isl_stat_error;
1498 dim = isl_basic_map_dim(bmap, type);
1499 if (first + n > dim || first + n < first)
1500 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1501 "position or range out of bounds",
1502 return isl_stat_error);
1503 return isl_stat_ok;
1506 /* Insert an extra integer division, prescribed by "div", to "bmap"
1507 * at (integer division) position "pos".
1509 * The integer division is first added at the end and then moved
1510 * into the right position.
1512 __isl_give isl_basic_map *isl_basic_map_insert_div(
1513 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1515 int i, k;
1517 bmap = isl_basic_map_cow(bmap);
1518 if (!bmap || !div)
1519 return isl_basic_map_free(bmap);
1521 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1522 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1523 "unexpected size", return isl_basic_map_free(bmap));
1524 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1525 return isl_basic_map_free(bmap);
1527 bmap = isl_basic_map_extend_space(bmap,
1528 isl_basic_map_get_space(bmap), 1, 0, 2);
1529 k = isl_basic_map_alloc_div(bmap);
1530 if (k < 0)
1531 return isl_basic_map_free(bmap);
1532 isl_seq_cpy(bmap->div[k], div->el, div->size);
1533 isl_int_set_si(bmap->div[k][div->size], 0);
1535 for (i = k; i > pos; --i)
1536 isl_basic_map_swap_div(bmap, i, i - 1);
1538 return bmap;
1541 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1543 if (!bmap)
1544 return -1;
1545 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1546 bmap->n_div -= n;
1547 return 0;
1550 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1552 return isl_basic_map_free_div(bset_to_bmap(bset), n);
1555 /* Copy constraint from src to dst, putting the vars of src at offset
1556 * dim_off in dst and the divs of src at offset div_off in dst.
1557 * If both sets are actually map, then dim_off applies to the input
1558 * variables.
1560 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1561 struct isl_basic_map *src_map, isl_int *src,
1562 unsigned in_off, unsigned out_off, unsigned div_off)
1564 unsigned src_nparam = isl_basic_map_n_param(src_map);
1565 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1566 unsigned src_in = isl_basic_map_n_in(src_map);
1567 unsigned dst_in = isl_basic_map_n_in(dst_map);
1568 unsigned src_out = isl_basic_map_n_out(src_map);
1569 unsigned dst_out = isl_basic_map_n_out(dst_map);
1570 isl_int_set(dst[0], src[0]);
1571 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1572 if (dst_nparam > src_nparam)
1573 isl_seq_clr(dst+1+src_nparam,
1574 dst_nparam - src_nparam);
1575 isl_seq_clr(dst+1+dst_nparam, in_off);
1576 isl_seq_cpy(dst+1+dst_nparam+in_off,
1577 src+1+src_nparam,
1578 isl_min(dst_in-in_off, src_in));
1579 if (dst_in-in_off > src_in)
1580 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1581 dst_in - in_off - src_in);
1582 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1583 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1584 src+1+src_nparam+src_in,
1585 isl_min(dst_out-out_off, src_out));
1586 if (dst_out-out_off > src_out)
1587 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1588 dst_out - out_off - src_out);
1589 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1590 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1591 src+1+src_nparam+src_in+src_out,
1592 isl_min(dst_map->extra-div_off, src_map->n_div));
1593 if (dst_map->n_div-div_off > src_map->n_div)
1594 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1595 div_off+src_map->n_div,
1596 dst_map->n_div - div_off - src_map->n_div);
1599 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1600 struct isl_basic_map *src_map, isl_int *src,
1601 unsigned in_off, unsigned out_off, unsigned div_off)
1603 isl_int_set(dst[0], src[0]);
1604 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1607 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1608 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1610 int i;
1611 unsigned div_off;
1613 if (!bmap1 || !bmap2)
1614 goto error;
1616 div_off = bmap1->n_div;
1618 for (i = 0; i < bmap2->n_eq; ++i) {
1619 int i1 = isl_basic_map_alloc_equality(bmap1);
1620 if (i1 < 0)
1621 goto error;
1622 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1623 i_pos, o_pos, div_off);
1626 for (i = 0; i < bmap2->n_ineq; ++i) {
1627 int i1 = isl_basic_map_alloc_inequality(bmap1);
1628 if (i1 < 0)
1629 goto error;
1630 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1631 i_pos, o_pos, div_off);
1634 for (i = 0; i < bmap2->n_div; ++i) {
1635 int i1 = isl_basic_map_alloc_div(bmap1);
1636 if (i1 < 0)
1637 goto error;
1638 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1639 i_pos, o_pos, div_off);
1642 isl_basic_map_free(bmap2);
1644 return bmap1;
1646 error:
1647 isl_basic_map_free(bmap1);
1648 isl_basic_map_free(bmap2);
1649 return NULL;
1652 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1653 struct isl_basic_set *bset2, unsigned pos)
1655 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1656 bset_to_bmap(bset2), 0, pos));
1659 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1660 __isl_take isl_space *dim, unsigned extra,
1661 unsigned n_eq, unsigned n_ineq)
1663 struct isl_basic_map *ext;
1664 unsigned flags;
1665 int dims_ok;
1667 if (!dim)
1668 goto error;
1670 if (!base)
1671 goto error;
1673 dims_ok = isl_space_is_equal(base->dim, dim) &&
1674 base->extra >= base->n_div + extra;
1676 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1677 room_for_ineq(base, n_ineq)) {
1678 isl_space_free(dim);
1679 return base;
1682 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1683 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1684 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1685 extra += base->extra;
1686 n_eq += base->n_eq;
1687 n_ineq += base->n_ineq;
1689 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1690 dim = NULL;
1691 if (!ext)
1692 goto error;
1694 if (dims_ok)
1695 ext->sample = isl_vec_copy(base->sample);
1696 flags = base->flags;
1697 ext = add_constraints(ext, base, 0, 0);
1698 if (ext) {
1699 ext->flags = flags;
1700 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1703 return ext;
1705 error:
1706 isl_space_free(dim);
1707 isl_basic_map_free(base);
1708 return NULL;
1711 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1712 __isl_take isl_space *dim, unsigned extra,
1713 unsigned n_eq, unsigned n_ineq)
1715 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1716 dim, extra, n_eq, n_ineq));
1719 struct isl_basic_map *isl_basic_map_extend_constraints(
1720 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1722 if (!base)
1723 return NULL;
1724 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1725 0, n_eq, n_ineq);
1728 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1729 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1730 unsigned n_eq, unsigned n_ineq)
1732 struct isl_basic_map *bmap;
1733 isl_space *dim;
1735 if (!base)
1736 return NULL;
1737 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1738 if (!dim)
1739 goto error;
1741 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1742 return bmap;
1743 error:
1744 isl_basic_map_free(base);
1745 return NULL;
1748 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1749 unsigned nparam, unsigned dim, unsigned extra,
1750 unsigned n_eq, unsigned n_ineq)
1752 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1753 nparam, 0, dim, extra, n_eq, n_ineq));
1756 struct isl_basic_set *isl_basic_set_extend_constraints(
1757 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1759 isl_basic_map *bmap = bset_to_bmap(base);
1760 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1761 return bset_from_bmap(bmap);
1764 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1766 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1769 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1771 if (!bmap)
1772 return NULL;
1774 if (bmap->ref > 1) {
1775 bmap->ref--;
1776 bmap = isl_basic_map_dup(bmap);
1778 if (bmap) {
1779 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1780 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1782 return bmap;
1785 /* Clear all cached information in "map", either because it is about
1786 * to be modified or because it is being freed.
1787 * Always return the same pointer that is passed in.
1788 * This is needed for the use in isl_map_free.
1790 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1792 isl_basic_map_free(map->cached_simple_hull[0]);
1793 isl_basic_map_free(map->cached_simple_hull[1]);
1794 map->cached_simple_hull[0] = NULL;
1795 map->cached_simple_hull[1] = NULL;
1796 return map;
1799 struct isl_set *isl_set_cow(struct isl_set *set)
1801 return isl_map_cow(set);
1804 /* Return an isl_map that is equal to "map" and that has only
1805 * a single reference.
1807 * If the original input already has only one reference, then
1808 * simply return it, but clear all cached information, since
1809 * it may be rendered invalid by the operations that will be
1810 * performed on the result.
1812 * Otherwise, create a duplicate (without any cached information).
1814 struct isl_map *isl_map_cow(struct isl_map *map)
1816 if (!map)
1817 return NULL;
1819 if (map->ref == 1)
1820 return clear_caches(map);
1821 map->ref--;
1822 return isl_map_dup(map);
1825 static void swap_vars(struct isl_blk blk, isl_int *a,
1826 unsigned a_len, unsigned b_len)
1828 isl_seq_cpy(blk.data, a+a_len, b_len);
1829 isl_seq_cpy(blk.data+b_len, a, a_len);
1830 isl_seq_cpy(a, blk.data, b_len+a_len);
1833 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1834 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1836 int i;
1837 struct isl_blk blk;
1839 if (!bmap)
1840 goto error;
1842 isl_assert(bmap->ctx,
1843 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1845 if (n1 == 0 || n2 == 0)
1846 return bmap;
1848 bmap = isl_basic_map_cow(bmap);
1849 if (!bmap)
1850 return NULL;
1852 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1853 if (isl_blk_is_error(blk))
1854 goto error;
1856 for (i = 0; i < bmap->n_eq; ++i)
1857 swap_vars(blk,
1858 bmap->eq[i] + pos, n1, n2);
1860 for (i = 0; i < bmap->n_ineq; ++i)
1861 swap_vars(blk,
1862 bmap->ineq[i] + pos, n1, n2);
1864 for (i = 0; i < bmap->n_div; ++i)
1865 swap_vars(blk,
1866 bmap->div[i]+1 + pos, n1, n2);
1868 isl_blk_free(bmap->ctx, blk);
1870 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1871 bmap = isl_basic_map_gauss(bmap, NULL);
1872 return isl_basic_map_finalize(bmap);
1873 error:
1874 isl_basic_map_free(bmap);
1875 return NULL;
1878 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1880 int i = 0;
1881 unsigned total;
1882 if (!bmap)
1883 goto error;
1884 total = isl_basic_map_total_dim(bmap);
1885 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1886 return isl_basic_map_free(bmap);
1887 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1888 if (bmap->n_eq > 0)
1889 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1890 else {
1891 i = isl_basic_map_alloc_equality(bmap);
1892 if (i < 0)
1893 goto error;
1895 isl_int_set_si(bmap->eq[i][0], 1);
1896 isl_seq_clr(bmap->eq[i]+1, total);
1897 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1898 isl_vec_free(bmap->sample);
1899 bmap->sample = NULL;
1900 return isl_basic_map_finalize(bmap);
1901 error:
1902 isl_basic_map_free(bmap);
1903 return NULL;
1906 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1908 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1911 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1912 * of "bmap").
1914 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1916 isl_int *t = bmap->div[a];
1917 bmap->div[a] = bmap->div[b];
1918 bmap->div[b] = t;
1921 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1922 * div definitions accordingly.
1924 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1926 int i;
1927 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1929 swap_div(bmap, a, b);
1931 for (i = 0; i < bmap->n_eq; ++i)
1932 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1934 for (i = 0; i < bmap->n_ineq; ++i)
1935 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1937 for (i = 0; i < bmap->n_div; ++i)
1938 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1939 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1942 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1943 * div definitions accordingly.
1945 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1947 isl_basic_map_swap_div(bset, a, b);
1950 /* Eliminate the specified n dimensions starting at first from the
1951 * constraints, without removing the dimensions from the space.
1952 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1954 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1955 enum isl_dim_type type, unsigned first, unsigned n)
1957 int i;
1959 if (!map)
1960 return NULL;
1961 if (n == 0)
1962 return map;
1964 if (first + n > isl_map_dim(map, type) || first + n < first)
1965 isl_die(map->ctx, isl_error_invalid,
1966 "index out of bounds", goto error);
1968 map = isl_map_cow(map);
1969 if (!map)
1970 return NULL;
1972 for (i = 0; i < map->n; ++i) {
1973 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1974 if (!map->p[i])
1975 goto error;
1977 return map;
1978 error:
1979 isl_map_free(map);
1980 return NULL;
1983 /* Eliminate the specified n dimensions starting at first from the
1984 * constraints, without removing the dimensions from the space.
1985 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1987 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1988 enum isl_dim_type type, unsigned first, unsigned n)
1990 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
1993 /* Eliminate the specified n dimensions starting at first from the
1994 * constraints, without removing the dimensions from the space.
1995 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1997 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1998 unsigned first, unsigned n)
2000 return isl_set_eliminate(set, isl_dim_set, first, n);
2003 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2004 __isl_take isl_basic_map *bmap)
2006 if (!bmap)
2007 return NULL;
2008 bmap = isl_basic_map_eliminate_vars(bmap,
2009 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2010 if (!bmap)
2011 return NULL;
2012 bmap->n_div = 0;
2013 return isl_basic_map_finalize(bmap);
2016 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2017 __isl_take isl_basic_set *bset)
2019 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2022 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2024 int i;
2026 if (!map)
2027 return NULL;
2028 if (map->n == 0)
2029 return map;
2031 map = isl_map_cow(map);
2032 if (!map)
2033 return NULL;
2035 for (i = 0; i < map->n; ++i) {
2036 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2037 if (!map->p[i])
2038 goto error;
2040 return map;
2041 error:
2042 isl_map_free(map);
2043 return NULL;
2046 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2048 return isl_map_remove_divs(set);
2051 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2052 enum isl_dim_type type, unsigned first, unsigned n)
2054 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2055 return isl_basic_map_free(bmap);
2056 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2057 return bmap;
2058 bmap = isl_basic_map_eliminate_vars(bmap,
2059 isl_basic_map_offset(bmap, type) - 1 + first, n);
2060 if (!bmap)
2061 return bmap;
2062 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2063 return bmap;
2064 bmap = isl_basic_map_drop(bmap, type, first, n);
2065 return bmap;
2068 /* Return true if the definition of the given div (recursively) involves
2069 * any of the given variables.
2071 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2072 unsigned first, unsigned n)
2074 int i;
2075 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2077 if (isl_int_is_zero(bmap->div[div][0]))
2078 return isl_bool_false;
2079 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2080 return isl_bool_true;
2082 for (i = bmap->n_div - 1; i >= 0; --i) {
2083 isl_bool involves;
2085 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2086 continue;
2087 involves = div_involves_vars(bmap, i, first, n);
2088 if (involves < 0 || involves)
2089 return involves;
2092 return isl_bool_false;
2095 /* Try and add a lower and/or upper bound on "div" to "bmap"
2096 * based on inequality "i".
2097 * "total" is the total number of variables (excluding the divs).
2098 * "v" is a temporary object that can be used during the calculations.
2099 * If "lb" is set, then a lower bound should be constructed.
2100 * If "ub" is set, then an upper bound should be constructed.
2102 * The calling function has already checked that the inequality does not
2103 * reference "div", but we still need to check that the inequality is
2104 * of the right form. We'll consider the case where we want to construct
2105 * a lower bound. The construction of upper bounds is similar.
2107 * Let "div" be of the form
2109 * q = floor((a + f(x))/d)
2111 * We essentially check if constraint "i" is of the form
2113 * b + f(x) >= 0
2115 * so that we can use it to derive a lower bound on "div".
2116 * However, we allow a slightly more general form
2118 * b + g(x) >= 0
2120 * with the condition that the coefficients of g(x) - f(x) are all
2121 * divisible by d.
2122 * Rewriting this constraint as
2124 * 0 >= -b - g(x)
2126 * adding a + f(x) to both sides and dividing by d, we obtain
2128 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2130 * Taking the floor on both sides, we obtain
2132 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2134 * or
2136 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2138 * In the case of an upper bound, we construct the constraint
2140 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2143 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2144 __isl_take isl_basic_map *bmap, int div, int i,
2145 unsigned total, isl_int v, int lb, int ub)
2147 int j;
2149 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2150 if (lb) {
2151 isl_int_sub(v, bmap->ineq[i][1 + j],
2152 bmap->div[div][1 + 1 + j]);
2153 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2155 if (ub) {
2156 isl_int_add(v, bmap->ineq[i][1 + j],
2157 bmap->div[div][1 + 1 + j]);
2158 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2161 if (!lb && !ub)
2162 return bmap;
2164 bmap = isl_basic_map_cow(bmap);
2165 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2166 if (lb) {
2167 int k = isl_basic_map_alloc_inequality(bmap);
2168 if (k < 0)
2169 goto error;
2170 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2171 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2172 bmap->div[div][1 + j]);
2173 isl_int_cdiv_q(bmap->ineq[k][j],
2174 bmap->ineq[k][j], bmap->div[div][0]);
2176 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2178 if (ub) {
2179 int k = isl_basic_map_alloc_inequality(bmap);
2180 if (k < 0)
2181 goto error;
2182 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2183 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2184 bmap->div[div][1 + j]);
2185 isl_int_fdiv_q(bmap->ineq[k][j],
2186 bmap->ineq[k][j], bmap->div[div][0]);
2188 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2191 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2192 return bmap;
2193 error:
2194 isl_basic_map_free(bmap);
2195 return NULL;
2198 /* This function is called right before "div" is eliminated from "bmap"
2199 * using Fourier-Motzkin.
2200 * Look through the constraints of "bmap" for constraints on the argument
2201 * of the integer division and use them to construct constraints on the
2202 * integer division itself. These constraints can then be combined
2203 * during the Fourier-Motzkin elimination.
2204 * Note that it is only useful to introduce lower bounds on "div"
2205 * if "bmap" already contains upper bounds on "div" as the newly
2206 * introduce lower bounds can then be combined with the pre-existing
2207 * upper bounds. Similarly for upper bounds.
2208 * We therefore first check if "bmap" contains any lower and/or upper bounds
2209 * on "div".
2211 * It is interesting to note that the introduction of these constraints
2212 * can indeed lead to more accurate results, even when compared to
2213 * deriving constraints on the argument of "div" from constraints on "div".
2214 * Consider, for example, the set
2216 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2218 * The second constraint can be rewritten as
2220 * 2 * [(-i-2j+3)/4] + k >= 0
2222 * from which we can derive
2224 * -i - 2j + 3 >= -2k
2226 * or
2228 * i + 2j <= 3 + 2k
2230 * Combined with the first constraint, we obtain
2232 * -3 <= 3 + 2k or k >= -3
2234 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2235 * the first constraint, we obtain
2237 * [(i + 2j)/4] >= [-3/4] = -1
2239 * Combining this constraint with the second constraint, we obtain
2241 * k >= -2
2243 static __isl_give isl_basic_map *insert_bounds_on_div(
2244 __isl_take isl_basic_map *bmap, int div)
2246 int i;
2247 int check_lb, check_ub;
2248 isl_int v;
2249 unsigned total;
2251 if (!bmap)
2252 return NULL;
2254 if (isl_int_is_zero(bmap->div[div][0]))
2255 return bmap;
2257 total = isl_space_dim(bmap->dim, isl_dim_all);
2259 check_lb = 0;
2260 check_ub = 0;
2261 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2262 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2263 if (s > 0)
2264 check_ub = 1;
2265 if (s < 0)
2266 check_lb = 1;
2269 if (!check_lb && !check_ub)
2270 return bmap;
2272 isl_int_init(v);
2274 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2275 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2276 continue;
2278 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2279 check_lb, check_ub);
2282 isl_int_clear(v);
2284 return bmap;
2287 /* Remove all divs (recursively) involving any of the given dimensions
2288 * in their definitions.
2290 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2291 __isl_take isl_basic_map *bmap,
2292 enum isl_dim_type type, unsigned first, unsigned n)
2294 int i;
2296 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2297 return isl_basic_map_free(bmap);
2298 first += isl_basic_map_offset(bmap, type);
2300 for (i = bmap->n_div - 1; i >= 0; --i) {
2301 isl_bool involves;
2303 involves = div_involves_vars(bmap, i, first, n);
2304 if (involves < 0)
2305 return isl_basic_map_free(bmap);
2306 if (!involves)
2307 continue;
2308 bmap = insert_bounds_on_div(bmap, i);
2309 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2310 if (!bmap)
2311 return NULL;
2312 i = bmap->n_div;
2315 return bmap;
2318 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2319 __isl_take isl_basic_set *bset,
2320 enum isl_dim_type type, unsigned first, unsigned n)
2322 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2325 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2326 enum isl_dim_type type, unsigned first, unsigned n)
2328 int i;
2330 if (!map)
2331 return NULL;
2332 if (map->n == 0)
2333 return map;
2335 map = isl_map_cow(map);
2336 if (!map)
2337 return NULL;
2339 for (i = 0; i < map->n; ++i) {
2340 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2341 type, first, n);
2342 if (!map->p[i])
2343 goto error;
2345 return map;
2346 error:
2347 isl_map_free(map);
2348 return NULL;
2351 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2352 enum isl_dim_type type, unsigned first, unsigned n)
2354 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2355 type, first, n));
2358 /* Does the description of "bmap" depend on the specified dimensions?
2359 * We also check whether the dimensions appear in any of the div definitions.
2360 * In principle there is no need for this check. If the dimensions appear
2361 * in a div definition, they also appear in the defining constraints of that
2362 * div.
2364 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2365 enum isl_dim_type type, unsigned first, unsigned n)
2367 int i;
2369 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2370 return isl_bool_error;
2372 first += isl_basic_map_offset(bmap, type);
2373 for (i = 0; i < bmap->n_eq; ++i)
2374 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2375 return isl_bool_true;
2376 for (i = 0; i < bmap->n_ineq; ++i)
2377 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2378 return isl_bool_true;
2379 for (i = 0; i < bmap->n_div; ++i) {
2380 if (isl_int_is_zero(bmap->div[i][0]))
2381 continue;
2382 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2383 return isl_bool_true;
2386 return isl_bool_false;
2389 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2390 enum isl_dim_type type, unsigned first, unsigned n)
2392 int i;
2394 if (!map)
2395 return isl_bool_error;
2397 if (first + n > isl_map_dim(map, type))
2398 isl_die(map->ctx, isl_error_invalid,
2399 "index out of bounds", return isl_bool_error);
2401 for (i = 0; i < map->n; ++i) {
2402 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2403 type, first, n);
2404 if (involves < 0 || involves)
2405 return involves;
2408 return isl_bool_false;
2411 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2412 enum isl_dim_type type, unsigned first, unsigned n)
2414 return isl_basic_map_involves_dims(bset, type, first, n);
2417 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2418 enum isl_dim_type type, unsigned first, unsigned n)
2420 return isl_map_involves_dims(set, type, first, n);
2423 /* Drop all constraints in bmap that involve any of the dimensions
2424 * first to first+n-1.
2426 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2427 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2429 int i;
2431 if (n == 0)
2432 return bmap;
2434 bmap = isl_basic_map_cow(bmap);
2436 if (!bmap)
2437 return NULL;
2439 for (i = bmap->n_eq - 1; i >= 0; --i) {
2440 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2441 continue;
2442 isl_basic_map_drop_equality(bmap, i);
2445 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2446 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2447 continue;
2448 isl_basic_map_drop_inequality(bmap, i);
2451 bmap = isl_basic_map_add_known_div_constraints(bmap);
2452 return bmap;
2455 /* Drop all constraints in bset that involve any of the dimensions
2456 * first to first+n-1.
2458 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2459 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2461 return isl_basic_map_drop_constraints_involving(bset, first, n);
2464 /* Drop all constraints in bmap that do not involve any of the dimensions
2465 * first to first + n - 1 of the given type.
2467 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2468 __isl_take isl_basic_map *bmap,
2469 enum isl_dim_type type, unsigned first, unsigned n)
2471 int i;
2473 if (n == 0) {
2474 isl_space *space = isl_basic_map_get_space(bmap);
2475 isl_basic_map_free(bmap);
2476 return isl_basic_map_universe(space);
2478 bmap = isl_basic_map_cow(bmap);
2479 if (!bmap)
2480 return NULL;
2482 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2483 return isl_basic_map_free(bmap);
2485 first += isl_basic_map_offset(bmap, type) - 1;
2487 for (i = bmap->n_eq - 1; i >= 0; --i) {
2488 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2489 continue;
2490 isl_basic_map_drop_equality(bmap, i);
2493 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2494 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2495 continue;
2496 isl_basic_map_drop_inequality(bmap, i);
2499 bmap = isl_basic_map_add_known_div_constraints(bmap);
2500 return bmap;
2503 /* Drop all constraints in bset that do not involve any of the dimensions
2504 * first to first + n - 1 of the given type.
2506 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2507 __isl_take isl_basic_set *bset,
2508 enum isl_dim_type type, unsigned first, unsigned n)
2510 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2511 type, first, n);
2514 /* Drop all constraints in bmap that involve any of the dimensions
2515 * first to first + n - 1 of the given type.
2517 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2518 __isl_take isl_basic_map *bmap,
2519 enum isl_dim_type type, unsigned first, unsigned n)
2521 if (!bmap)
2522 return NULL;
2523 if (n == 0)
2524 return bmap;
2526 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2527 return isl_basic_map_free(bmap);
2529 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2530 first += isl_basic_map_offset(bmap, type) - 1;
2531 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2534 /* Drop all constraints in bset that involve any of the dimensions
2535 * first to first + n - 1 of the given type.
2537 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2538 __isl_take isl_basic_set *bset,
2539 enum isl_dim_type type, unsigned first, unsigned n)
2541 return isl_basic_map_drop_constraints_involving_dims(bset,
2542 type, first, n);
2545 /* Drop constraints from "map" by applying "drop" to each basic map.
2547 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2548 enum isl_dim_type type, unsigned first, unsigned n,
2549 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2550 enum isl_dim_type type, unsigned first, unsigned n))
2552 int i;
2553 unsigned dim;
2555 if (!map)
2556 return NULL;
2558 dim = isl_map_dim(map, type);
2559 if (first + n > dim || first + n < first)
2560 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2561 "index out of bounds", return isl_map_free(map));
2563 map = isl_map_cow(map);
2564 if (!map)
2565 return NULL;
2567 for (i = 0; i < map->n; ++i) {
2568 map->p[i] = drop(map->p[i], type, first, n);
2569 if (!map->p[i])
2570 return isl_map_free(map);
2573 if (map->n > 1)
2574 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2576 return map;
2579 /* Drop all constraints in map that involve any of the dimensions
2580 * first to first + n - 1 of the given type.
2582 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2583 __isl_take isl_map *map,
2584 enum isl_dim_type type, unsigned first, unsigned n)
2586 if (n == 0)
2587 return map;
2588 return drop_constraints(map, type, first, n,
2589 &isl_basic_map_drop_constraints_involving_dims);
2592 /* Drop all constraints in "map" that do not involve any of the dimensions
2593 * first to first + n - 1 of the given type.
2595 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2596 __isl_take isl_map *map,
2597 enum isl_dim_type type, unsigned first, unsigned n)
2599 if (n == 0) {
2600 isl_space *space = isl_map_get_space(map);
2601 isl_map_free(map);
2602 return isl_map_universe(space);
2604 return drop_constraints(map, type, first, n,
2605 &isl_basic_map_drop_constraints_not_involving_dims);
2608 /* Drop all constraints in set that involve any of the dimensions
2609 * first to first + n - 1 of the given type.
2611 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2612 __isl_take isl_set *set,
2613 enum isl_dim_type type, unsigned first, unsigned n)
2615 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2618 /* Drop all constraints in "set" that do not involve any of the dimensions
2619 * first to first + n - 1 of the given type.
2621 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2622 __isl_take isl_set *set,
2623 enum isl_dim_type type, unsigned first, unsigned n)
2625 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2628 /* Does local variable "div" of "bmap" have a complete explicit representation?
2629 * Having a complete explicit representation requires not only
2630 * an explicit representation, but also that all local variables
2631 * that appear in this explicit representation in turn have
2632 * a complete explicit representation.
2634 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2636 int i;
2637 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2638 isl_bool marked;
2640 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2641 if (marked < 0 || marked)
2642 return isl_bool_not(marked);
2644 for (i = bmap->n_div - 1; i >= 0; --i) {
2645 isl_bool known;
2647 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2648 continue;
2649 known = isl_basic_map_div_is_known(bmap, i);
2650 if (known < 0 || !known)
2651 return known;
2654 return isl_bool_true;
2657 /* Does local variable "div" of "bset" have a complete explicit representation?
2659 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2661 return isl_basic_map_div_is_known(bset, div);
2664 /* Remove all divs that are unknown or defined in terms of unknown divs.
2666 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2667 __isl_take isl_basic_map *bmap)
2669 int i;
2671 if (!bmap)
2672 return NULL;
2674 for (i = bmap->n_div - 1; i >= 0; --i) {
2675 if (isl_basic_map_div_is_known(bmap, i))
2676 continue;
2677 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2678 if (!bmap)
2679 return NULL;
2680 i = bmap->n_div;
2683 return bmap;
2686 /* Remove all divs that are unknown or defined in terms of unknown divs.
2688 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2689 __isl_take isl_basic_set *bset)
2691 return isl_basic_map_remove_unknown_divs(bset);
2694 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2696 int i;
2698 if (!map)
2699 return NULL;
2700 if (map->n == 0)
2701 return map;
2703 map = isl_map_cow(map);
2704 if (!map)
2705 return NULL;
2707 for (i = 0; i < map->n; ++i) {
2708 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2709 if (!map->p[i])
2710 goto error;
2712 return map;
2713 error:
2714 isl_map_free(map);
2715 return NULL;
2718 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2720 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2723 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2724 __isl_take isl_basic_set *bset,
2725 enum isl_dim_type type, unsigned first, unsigned n)
2727 isl_basic_map *bmap = bset_to_bmap(bset);
2728 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2729 return bset_from_bmap(bmap);
2732 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2733 enum isl_dim_type type, unsigned first, unsigned n)
2735 int i;
2737 if (n == 0)
2738 return map;
2740 map = isl_map_cow(map);
2741 if (!map)
2742 return NULL;
2743 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2745 for (i = 0; i < map->n; ++i) {
2746 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2747 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2748 if (!map->p[i])
2749 goto error;
2751 map = isl_map_drop(map, type, first, n);
2752 return map;
2753 error:
2754 isl_map_free(map);
2755 return NULL;
2758 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2759 enum isl_dim_type type, unsigned first, unsigned n)
2761 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2762 type, first, n));
2765 /* Project out n inputs starting at first using Fourier-Motzkin */
2766 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2767 unsigned first, unsigned n)
2769 return isl_map_remove_dims(map, isl_dim_in, first, n);
2772 static void dump_term(struct isl_basic_map *bmap,
2773 isl_int c, int pos, FILE *out)
2775 const char *name;
2776 unsigned in = isl_basic_map_n_in(bmap);
2777 unsigned dim = in + isl_basic_map_n_out(bmap);
2778 unsigned nparam = isl_basic_map_n_param(bmap);
2779 if (!pos)
2780 isl_int_print(out, c, 0);
2781 else {
2782 if (!isl_int_is_one(c))
2783 isl_int_print(out, c, 0);
2784 if (pos < 1 + nparam) {
2785 name = isl_space_get_dim_name(bmap->dim,
2786 isl_dim_param, pos - 1);
2787 if (name)
2788 fprintf(out, "%s", name);
2789 else
2790 fprintf(out, "p%d", pos - 1);
2791 } else if (pos < 1 + nparam + in)
2792 fprintf(out, "i%d", pos - 1 - nparam);
2793 else if (pos < 1 + nparam + dim)
2794 fprintf(out, "o%d", pos - 1 - nparam - in);
2795 else
2796 fprintf(out, "e%d", pos - 1 - nparam - dim);
2800 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2801 int sign, FILE *out)
2803 int i;
2804 int first;
2805 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2806 isl_int v;
2808 isl_int_init(v);
2809 for (i = 0, first = 1; i < len; ++i) {
2810 if (isl_int_sgn(c[i]) * sign <= 0)
2811 continue;
2812 if (!first)
2813 fprintf(out, " + ");
2814 first = 0;
2815 isl_int_abs(v, c[i]);
2816 dump_term(bmap, v, i, out);
2818 isl_int_clear(v);
2819 if (first)
2820 fprintf(out, "0");
2823 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2824 const char *op, FILE *out, int indent)
2826 int i;
2828 fprintf(out, "%*s", indent, "");
2830 dump_constraint_sign(bmap, c, 1, out);
2831 fprintf(out, " %s ", op);
2832 dump_constraint_sign(bmap, c, -1, out);
2834 fprintf(out, "\n");
2836 for (i = bmap->n_div; i < bmap->extra; ++i) {
2837 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2838 continue;
2839 fprintf(out, "%*s", indent, "");
2840 fprintf(out, "ERROR: unused div coefficient not zero\n");
2841 abort();
2845 static void dump_constraints(struct isl_basic_map *bmap,
2846 isl_int **c, unsigned n,
2847 const char *op, FILE *out, int indent)
2849 int i;
2851 for (i = 0; i < n; ++i)
2852 dump_constraint(bmap, c[i], op, out, indent);
2855 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2857 int j;
2858 int first = 1;
2859 unsigned total = isl_basic_map_total_dim(bmap);
2861 for (j = 0; j < 1 + total; ++j) {
2862 if (isl_int_is_zero(exp[j]))
2863 continue;
2864 if (!first && isl_int_is_pos(exp[j]))
2865 fprintf(out, "+");
2866 dump_term(bmap, exp[j], j, out);
2867 first = 0;
2871 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2873 int i;
2875 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2876 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2878 for (i = 0; i < bmap->n_div; ++i) {
2879 fprintf(out, "%*s", indent, "");
2880 fprintf(out, "e%d = [(", i);
2881 dump_affine(bmap, bmap->div[i]+1, out);
2882 fprintf(out, ")/");
2883 isl_int_print(out, bmap->div[i][0], 0);
2884 fprintf(out, "]\n");
2888 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2889 FILE *out, int indent)
2891 if (!bset) {
2892 fprintf(out, "null basic set\n");
2893 return;
2896 fprintf(out, "%*s", indent, "");
2897 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2898 bset->ref, bset->dim->nparam, bset->dim->n_out,
2899 bset->extra, bset->flags);
2900 dump(bset_to_bmap(bset), out, indent);
2903 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2904 FILE *out, int indent)
2906 if (!bmap) {
2907 fprintf(out, "null basic map\n");
2908 return;
2911 fprintf(out, "%*s", indent, "");
2912 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2913 "flags: %x, n_name: %d\n",
2914 bmap->ref,
2915 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2916 bmap->extra, bmap->flags, bmap->dim->n_id);
2917 dump(bmap, out, indent);
2920 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2922 unsigned total;
2923 if (!bmap)
2924 return -1;
2925 total = isl_basic_map_total_dim(bmap);
2926 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2927 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2928 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2929 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2930 return 0;
2933 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2934 unsigned flags)
2936 if (!space)
2937 return NULL;
2938 if (isl_space_dim(space, isl_dim_in) != 0)
2939 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2940 "set cannot have input dimensions", goto error);
2941 return isl_map_alloc_space(space, n, flags);
2942 error:
2943 isl_space_free(space);
2944 return NULL;
2947 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2948 unsigned nparam, unsigned dim, int n, unsigned flags)
2950 struct isl_set *set;
2951 isl_space *dims;
2953 dims = isl_space_alloc(ctx, nparam, 0, dim);
2954 if (!dims)
2955 return NULL;
2957 set = isl_set_alloc_space(dims, n, flags);
2958 return set;
2961 /* Make sure "map" has room for at least "n" more basic maps.
2963 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2965 int i;
2966 struct isl_map *grown = NULL;
2968 if (!map)
2969 return NULL;
2970 isl_assert(map->ctx, n >= 0, goto error);
2971 if (map->n + n <= map->size)
2972 return map;
2973 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2974 if (!grown)
2975 goto error;
2976 for (i = 0; i < map->n; ++i) {
2977 grown->p[i] = isl_basic_map_copy(map->p[i]);
2978 if (!grown->p[i])
2979 goto error;
2980 grown->n++;
2982 isl_map_free(map);
2983 return grown;
2984 error:
2985 isl_map_free(grown);
2986 isl_map_free(map);
2987 return NULL;
2990 /* Make sure "set" has room for at least "n" more basic sets.
2992 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2994 return set_from_map(isl_map_grow(set_to_map(set), n));
2997 struct isl_set *isl_set_dup(struct isl_set *set)
2999 int i;
3000 struct isl_set *dup;
3002 if (!set)
3003 return NULL;
3005 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
3006 if (!dup)
3007 return NULL;
3008 for (i = 0; i < set->n; ++i)
3009 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
3010 return dup;
3013 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
3015 return isl_map_from_basic_map(bset);
3018 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
3020 struct isl_map *map;
3022 if (!bmap)
3023 return NULL;
3025 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3026 return isl_map_add_basic_map(map, bmap);
3029 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3030 __isl_take isl_basic_set *bset)
3032 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3033 bset_to_bmap(bset)));
3036 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3038 return isl_map_free(set);
3041 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3043 int i;
3045 if (!set) {
3046 fprintf(out, "null set\n");
3047 return;
3050 fprintf(out, "%*s", indent, "");
3051 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3052 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3053 set->flags);
3054 for (i = 0; i < set->n; ++i) {
3055 fprintf(out, "%*s", indent, "");
3056 fprintf(out, "basic set %d:\n", i);
3057 isl_basic_set_print_internal(set->p[i], out, indent+4);
3061 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3063 int i;
3065 if (!map) {
3066 fprintf(out, "null map\n");
3067 return;
3070 fprintf(out, "%*s", indent, "");
3071 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3072 "flags: %x, n_name: %d\n",
3073 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3074 map->dim->n_out, map->flags, map->dim->n_id);
3075 for (i = 0; i < map->n; ++i) {
3076 fprintf(out, "%*s", indent, "");
3077 fprintf(out, "basic map %d:\n", i);
3078 isl_basic_map_print_internal(map->p[i], out, indent+4);
3082 struct isl_basic_map *isl_basic_map_intersect_domain(
3083 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3085 struct isl_basic_map *bmap_domain;
3087 if (!bmap || !bset)
3088 goto error;
3090 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3091 bset->dim, isl_dim_param), goto error);
3093 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3094 isl_assert(bset->ctx,
3095 isl_basic_map_compatible_domain(bmap, bset), goto error);
3097 bmap = isl_basic_map_cow(bmap);
3098 if (!bmap)
3099 goto error;
3100 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3101 bset->n_div, bset->n_eq, bset->n_ineq);
3102 bmap_domain = isl_basic_map_from_domain(bset);
3103 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3105 bmap = isl_basic_map_simplify(bmap);
3106 return isl_basic_map_finalize(bmap);
3107 error:
3108 isl_basic_map_free(bmap);
3109 isl_basic_set_free(bset);
3110 return NULL;
3113 /* Check that the space of "bset" is the same as that of the range of "bmap".
3115 static isl_stat isl_basic_map_check_compatible_range(
3116 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3118 isl_bool ok;
3120 ok = isl_basic_map_compatible_range(bmap, bset);
3121 if (ok < 0)
3122 return isl_stat_error;
3123 if (!ok)
3124 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3125 "incompatible spaces", return isl_stat_error);
3127 return isl_stat_ok;
3130 struct isl_basic_map *isl_basic_map_intersect_range(
3131 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3133 struct isl_basic_map *bmap_range;
3135 if (!bmap || !bset)
3136 goto error;
3138 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3139 bset->dim, isl_dim_param), goto error);
3141 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3142 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3143 goto error;
3145 if (isl_basic_set_plain_is_universe(bset)) {
3146 isl_basic_set_free(bset);
3147 return bmap;
3150 bmap = isl_basic_map_cow(bmap);
3151 if (!bmap)
3152 goto error;
3153 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3154 bset->n_div, bset->n_eq, bset->n_ineq);
3155 bmap_range = bset_to_bmap(bset);
3156 bmap = add_constraints(bmap, bmap_range, 0, 0);
3158 bmap = isl_basic_map_simplify(bmap);
3159 return isl_basic_map_finalize(bmap);
3160 error:
3161 isl_basic_map_free(bmap);
3162 isl_basic_set_free(bset);
3163 return NULL;
3166 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3167 __isl_keep isl_vec *vec)
3169 int i;
3170 unsigned total;
3171 isl_int s;
3173 if (!bmap || !vec)
3174 return isl_bool_error;
3176 total = 1 + isl_basic_map_total_dim(bmap);
3177 if (total != vec->size)
3178 return isl_bool_false;
3180 isl_int_init(s);
3182 for (i = 0; i < bmap->n_eq; ++i) {
3183 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3184 if (!isl_int_is_zero(s)) {
3185 isl_int_clear(s);
3186 return isl_bool_false;
3190 for (i = 0; i < bmap->n_ineq; ++i) {
3191 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3192 if (isl_int_is_neg(s)) {
3193 isl_int_clear(s);
3194 return isl_bool_false;
3198 isl_int_clear(s);
3200 return isl_bool_true;
3203 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3204 __isl_keep isl_vec *vec)
3206 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3209 struct isl_basic_map *isl_basic_map_intersect(
3210 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3212 struct isl_vec *sample = NULL;
3214 if (!bmap1 || !bmap2)
3215 goto error;
3217 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
3218 bmap2->dim, isl_dim_param), goto error);
3219 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3220 isl_space_dim(bmap1->dim, isl_dim_param) &&
3221 isl_space_dim(bmap2->dim, isl_dim_all) !=
3222 isl_space_dim(bmap2->dim, isl_dim_param))
3223 return isl_basic_map_intersect(bmap2, bmap1);
3225 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3226 isl_space_dim(bmap2->dim, isl_dim_param))
3227 isl_assert(bmap1->ctx,
3228 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3230 if (isl_basic_map_plain_is_empty(bmap1)) {
3231 isl_basic_map_free(bmap2);
3232 return bmap1;
3234 if (isl_basic_map_plain_is_empty(bmap2)) {
3235 isl_basic_map_free(bmap1);
3236 return bmap2;
3239 if (bmap1->sample &&
3240 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3241 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3242 sample = isl_vec_copy(bmap1->sample);
3243 else if (bmap2->sample &&
3244 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3245 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3246 sample = isl_vec_copy(bmap2->sample);
3248 bmap1 = isl_basic_map_cow(bmap1);
3249 if (!bmap1)
3250 goto error;
3251 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3252 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3253 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3255 if (!bmap1)
3256 isl_vec_free(sample);
3257 else if (sample) {
3258 isl_vec_free(bmap1->sample);
3259 bmap1->sample = sample;
3262 bmap1 = isl_basic_map_simplify(bmap1);
3263 return isl_basic_map_finalize(bmap1);
3264 error:
3265 if (sample)
3266 isl_vec_free(sample);
3267 isl_basic_map_free(bmap1);
3268 isl_basic_map_free(bmap2);
3269 return NULL;
3272 struct isl_basic_set *isl_basic_set_intersect(
3273 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3275 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3276 bset_to_bmap(bset2)));
3279 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3280 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3282 return isl_basic_set_intersect(bset1, bset2);
3285 /* Special case of isl_map_intersect, where both map1 and map2
3286 * are convex, without any divs and such that either map1 or map2
3287 * contains a single constraint. This constraint is then simply
3288 * added to the other map.
3290 static __isl_give isl_map *map_intersect_add_constraint(
3291 __isl_take isl_map *map1, __isl_take isl_map *map2)
3293 isl_assert(map1->ctx, map1->n == 1, goto error);
3294 isl_assert(map2->ctx, map1->n == 1, goto error);
3295 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3296 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3298 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3299 return isl_map_intersect(map2, map1);
3301 isl_assert(map2->ctx,
3302 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3304 map1 = isl_map_cow(map1);
3305 if (!map1)
3306 goto error;
3307 if (isl_map_plain_is_empty(map1)) {
3308 isl_map_free(map2);
3309 return map1;
3311 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3312 if (map2->p[0]->n_eq == 1)
3313 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3314 else
3315 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3316 map2->p[0]->ineq[0]);
3318 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3319 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3320 if (!map1->p[0])
3321 goto error;
3323 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3324 isl_basic_map_free(map1->p[0]);
3325 map1->n = 0;
3328 isl_map_free(map2);
3330 return map1;
3331 error:
3332 isl_map_free(map1);
3333 isl_map_free(map2);
3334 return NULL;
3337 /* map2 may be either a parameter domain or a map living in the same
3338 * space as map1.
3340 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3341 __isl_take isl_map *map2)
3343 unsigned flags = 0;
3344 isl_map *result;
3345 int i, j;
3347 if (!map1 || !map2)
3348 goto error;
3350 if ((isl_map_plain_is_empty(map1) ||
3351 isl_map_plain_is_universe(map2)) &&
3352 isl_space_is_equal(map1->dim, map2->dim)) {
3353 isl_map_free(map2);
3354 return map1;
3356 if ((isl_map_plain_is_empty(map2) ||
3357 isl_map_plain_is_universe(map1)) &&
3358 isl_space_is_equal(map1->dim, map2->dim)) {
3359 isl_map_free(map1);
3360 return map2;
3363 if (map1->n == 1 && map2->n == 1 &&
3364 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3365 isl_space_is_equal(map1->dim, map2->dim) &&
3366 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3367 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3368 return map_intersect_add_constraint(map1, map2);
3370 if (isl_space_dim(map2->dim, isl_dim_all) !=
3371 isl_space_dim(map2->dim, isl_dim_param))
3372 isl_assert(map1->ctx,
3373 isl_space_is_equal(map1->dim, map2->dim), goto error);
3375 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3376 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3377 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3379 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3380 map1->n * map2->n, flags);
3381 if (!result)
3382 goto error;
3383 for (i = 0; i < map1->n; ++i)
3384 for (j = 0; j < map2->n; ++j) {
3385 struct isl_basic_map *part;
3386 part = isl_basic_map_intersect(
3387 isl_basic_map_copy(map1->p[i]),
3388 isl_basic_map_copy(map2->p[j]));
3389 if (isl_basic_map_is_empty(part) < 0)
3390 part = isl_basic_map_free(part);
3391 result = isl_map_add_basic_map(result, part);
3392 if (!result)
3393 goto error;
3395 isl_map_free(map1);
3396 isl_map_free(map2);
3397 return result;
3398 error:
3399 isl_map_free(map1);
3400 isl_map_free(map2);
3401 return NULL;
3404 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3405 __isl_take isl_map *map2)
3407 if (!map1 || !map2)
3408 goto error;
3409 if (!isl_space_is_equal(map1->dim, map2->dim))
3410 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3411 "spaces don't match", goto error);
3412 return map_intersect_internal(map1, map2);
3413 error:
3414 isl_map_free(map1);
3415 isl_map_free(map2);
3416 return NULL;
3419 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3420 __isl_take isl_map *map2)
3422 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3425 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3427 return set_from_map(isl_map_intersect(set_to_map(set1),
3428 set_to_map(set2)));
3431 /* map_intersect_internal accepts intersections
3432 * with parameter domains, so we can just call that function.
3434 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3435 __isl_take isl_set *params)
3437 return map_intersect_internal(map, params);
3440 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3441 __isl_take isl_map *map2)
3443 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3446 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3447 __isl_take isl_set *params)
3449 return isl_map_intersect_params(set, params);
3452 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3454 isl_space *space;
3455 unsigned pos, n1, n2;
3457 if (!bmap)
3458 return NULL;
3459 bmap = isl_basic_map_cow(bmap);
3460 if (!bmap)
3461 return NULL;
3462 space = isl_space_reverse(isl_space_copy(bmap->dim));
3463 pos = isl_basic_map_offset(bmap, isl_dim_in);
3464 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3465 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3466 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3467 return isl_basic_map_reset_space(bmap, space);
3470 static __isl_give isl_basic_map *basic_map_space_reset(
3471 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3473 isl_space *space;
3475 if (!bmap)
3476 return NULL;
3477 if (!isl_space_is_named_or_nested(bmap->dim, type))
3478 return bmap;
3480 space = isl_basic_map_get_space(bmap);
3481 space = isl_space_reset(space, type);
3482 bmap = isl_basic_map_reset_space(bmap, space);
3483 return bmap;
3486 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3487 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3488 unsigned pos, unsigned n)
3490 isl_space *res_dim;
3491 struct isl_basic_map *res;
3492 struct isl_dim_map *dim_map;
3493 unsigned total, off;
3494 enum isl_dim_type t;
3496 if (n == 0)
3497 return basic_map_space_reset(bmap, type);
3499 if (!bmap)
3500 return NULL;
3502 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3504 total = isl_basic_map_total_dim(bmap) + n;
3505 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3506 off = 0;
3507 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3508 if (t != type) {
3509 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3510 } else {
3511 unsigned size = isl_basic_map_dim(bmap, t);
3512 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3513 0, pos, off);
3514 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3515 pos, size - pos, off + pos + n);
3517 off += isl_space_dim(res_dim, t);
3519 isl_dim_map_div(dim_map, bmap, off);
3521 res = isl_basic_map_alloc_space(res_dim,
3522 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3523 if (isl_basic_map_is_rational(bmap))
3524 res = isl_basic_map_set_rational(res);
3525 if (isl_basic_map_plain_is_empty(bmap)) {
3526 isl_basic_map_free(bmap);
3527 free(dim_map);
3528 return isl_basic_map_set_to_empty(res);
3530 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3531 return isl_basic_map_finalize(res);
3534 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3535 __isl_take isl_basic_set *bset,
3536 enum isl_dim_type type, unsigned pos, unsigned n)
3538 return isl_basic_map_insert_dims(bset, type, pos, n);
3541 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3542 enum isl_dim_type type, unsigned n)
3544 if (!bmap)
3545 return NULL;
3546 return isl_basic_map_insert_dims(bmap, type,
3547 isl_basic_map_dim(bmap, type), n);
3550 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3551 enum isl_dim_type type, unsigned n)
3553 if (!bset)
3554 return NULL;
3555 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3556 return isl_basic_map_add_dims(bset, type, n);
3557 error:
3558 isl_basic_set_free(bset);
3559 return NULL;
3562 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3563 enum isl_dim_type type)
3565 isl_space *space;
3567 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3568 return map;
3570 space = isl_map_get_space(map);
3571 space = isl_space_reset(space, type);
3572 map = isl_map_reset_space(map, space);
3573 return map;
3576 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3577 enum isl_dim_type type, unsigned pos, unsigned n)
3579 int i;
3581 if (n == 0)
3582 return map_space_reset(map, type);
3584 map = isl_map_cow(map);
3585 if (!map)
3586 return NULL;
3588 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3589 if (!map->dim)
3590 goto error;
3592 for (i = 0; i < map->n; ++i) {
3593 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3594 if (!map->p[i])
3595 goto error;
3598 return map;
3599 error:
3600 isl_map_free(map);
3601 return NULL;
3604 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3605 enum isl_dim_type type, unsigned pos, unsigned n)
3607 return isl_map_insert_dims(set, type, pos, n);
3610 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3611 enum isl_dim_type type, unsigned n)
3613 if (!map)
3614 return NULL;
3615 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3618 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3619 enum isl_dim_type type, unsigned n)
3621 if (!set)
3622 return NULL;
3623 isl_assert(set->ctx, type != isl_dim_in, goto error);
3624 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3625 error:
3626 isl_set_free(set);
3627 return NULL;
3630 __isl_give isl_basic_map *isl_basic_map_move_dims(
3631 __isl_take isl_basic_map *bmap,
3632 enum isl_dim_type dst_type, unsigned dst_pos,
3633 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3635 struct isl_dim_map *dim_map;
3636 struct isl_basic_map *res;
3637 enum isl_dim_type t;
3638 unsigned total, off;
3640 if (!bmap)
3641 return NULL;
3642 if (n == 0)
3643 return bmap;
3645 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3646 return isl_basic_map_free(bmap);
3648 if (dst_type == src_type && dst_pos == src_pos)
3649 return bmap;
3651 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3653 if (pos(bmap->dim, dst_type) + dst_pos ==
3654 pos(bmap->dim, src_type) + src_pos +
3655 ((src_type < dst_type) ? n : 0)) {
3656 bmap = isl_basic_map_cow(bmap);
3657 if (!bmap)
3658 return NULL;
3660 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3661 src_type, src_pos, n);
3662 if (!bmap->dim)
3663 goto error;
3665 bmap = isl_basic_map_finalize(bmap);
3667 return bmap;
3670 total = isl_basic_map_total_dim(bmap);
3671 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3673 off = 0;
3674 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3675 unsigned size = isl_space_dim(bmap->dim, t);
3676 if (t == dst_type) {
3677 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3678 0, dst_pos, off);
3679 off += dst_pos;
3680 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3681 src_pos, n, off);
3682 off += n;
3683 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3684 dst_pos, size - dst_pos, off);
3685 off += size - dst_pos;
3686 } else if (t == src_type) {
3687 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3688 0, src_pos, off);
3689 off += src_pos;
3690 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3691 src_pos + n, size - src_pos - n, off);
3692 off += size - src_pos - n;
3693 } else {
3694 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3695 off += size;
3698 isl_dim_map_div(dim_map, bmap, off);
3700 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3701 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3702 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3703 if (!bmap)
3704 goto error;
3706 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3707 src_type, src_pos, n);
3708 if (!bmap->dim)
3709 goto error;
3711 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3712 bmap = isl_basic_map_gauss(bmap, NULL);
3713 bmap = isl_basic_map_finalize(bmap);
3715 return bmap;
3716 error:
3717 isl_basic_map_free(bmap);
3718 return NULL;
3721 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3722 enum isl_dim_type dst_type, unsigned dst_pos,
3723 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3725 isl_basic_map *bmap = bset_to_bmap(bset);
3726 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3727 src_type, src_pos, n);
3728 return bset_from_bmap(bmap);
3731 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3732 enum isl_dim_type dst_type, unsigned dst_pos,
3733 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3735 if (!set)
3736 return NULL;
3737 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3738 return set_from_map(isl_map_move_dims(set_to_map(set),
3739 dst_type, dst_pos, src_type, src_pos, n));
3740 error:
3741 isl_set_free(set);
3742 return NULL;
3745 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3746 enum isl_dim_type dst_type, unsigned dst_pos,
3747 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3749 int i;
3751 if (!map)
3752 return NULL;
3753 if (n == 0)
3754 return map;
3756 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3757 goto error);
3759 if (dst_type == src_type && dst_pos == src_pos)
3760 return map;
3762 isl_assert(map->ctx, dst_type != src_type, goto error);
3764 map = isl_map_cow(map);
3765 if (!map)
3766 return NULL;
3768 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3769 if (!map->dim)
3770 goto error;
3772 for (i = 0; i < map->n; ++i) {
3773 map->p[i] = isl_basic_map_move_dims(map->p[i],
3774 dst_type, dst_pos,
3775 src_type, src_pos, n);
3776 if (!map->p[i])
3777 goto error;
3780 return map;
3781 error:
3782 isl_map_free(map);
3783 return NULL;
3786 /* Move the specified dimensions to the last columns right before
3787 * the divs. Don't change the dimension specification of bmap.
3788 * That's the responsibility of the caller.
3790 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3791 enum isl_dim_type type, unsigned first, unsigned n)
3793 struct isl_dim_map *dim_map;
3794 struct isl_basic_map *res;
3795 enum isl_dim_type t;
3796 unsigned total, off;
3798 if (!bmap)
3799 return NULL;
3800 if (pos(bmap->dim, type) + first + n ==
3801 1 + isl_space_dim(bmap->dim, isl_dim_all))
3802 return bmap;
3804 total = isl_basic_map_total_dim(bmap);
3805 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3807 off = 0;
3808 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3809 unsigned size = isl_space_dim(bmap->dim, t);
3810 if (t == type) {
3811 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3812 0, first, off);
3813 off += first;
3814 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3815 first, n, total - bmap->n_div - n);
3816 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3817 first + n, size - (first + n), off);
3818 off += size - (first + n);
3819 } else {
3820 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3821 off += size;
3824 isl_dim_map_div(dim_map, bmap, off + n);
3826 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3827 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3828 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3829 return res;
3832 /* Insert "n" rows in the divs of "bmap".
3834 * The number of columns is not changed, which means that the last
3835 * dimensions of "bmap" are being reintepreted as the new divs.
3836 * The space of "bmap" is not adjusted, however, which means
3837 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3838 * from the space of "bmap" is the responsibility of the caller.
3840 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3841 int n)
3843 int i;
3844 size_t row_size;
3845 isl_int **new_div;
3846 isl_int *old;
3848 bmap = isl_basic_map_cow(bmap);
3849 if (!bmap)
3850 return NULL;
3852 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3853 old = bmap->block2.data;
3854 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3855 (bmap->extra + n) * (1 + row_size));
3856 if (!bmap->block2.data)
3857 return isl_basic_map_free(bmap);
3858 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3859 if (!new_div)
3860 return isl_basic_map_free(bmap);
3861 for (i = 0; i < n; ++i) {
3862 new_div[i] = bmap->block2.data +
3863 (bmap->extra + i) * (1 + row_size);
3864 isl_seq_clr(new_div[i], 1 + row_size);
3866 for (i = 0; i < bmap->extra; ++i)
3867 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3868 free(bmap->div);
3869 bmap->div = new_div;
3870 bmap->n_div += n;
3871 bmap->extra += n;
3873 return bmap;
3876 /* Drop constraints from "bmap" that only involve the variables
3877 * of "type" in the range [first, first + n] that are not related
3878 * to any of the variables outside that interval.
3879 * These constraints cannot influence the values for the variables
3880 * outside the interval, except in case they cause "bmap" to be empty.
3881 * Only drop the constraints if "bmap" is known to be non-empty.
3883 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3884 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3885 unsigned first, unsigned n)
3887 int i;
3888 int *groups;
3889 unsigned dim, n_div;
3890 isl_bool non_empty;
3892 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3893 if (non_empty < 0)
3894 return isl_basic_map_free(bmap);
3895 if (!non_empty)
3896 return bmap;
3898 dim = isl_basic_map_dim(bmap, isl_dim_all);
3899 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3900 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3901 if (!groups)
3902 return isl_basic_map_free(bmap);
3903 first += isl_basic_map_offset(bmap, type) - 1;
3904 for (i = 0; i < first; ++i)
3905 groups[i] = -1;
3906 for (i = first + n; i < dim - n_div; ++i)
3907 groups[i] = -1;
3909 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3911 return bmap;
3914 /* Turn the n dimensions of type type, starting at first
3915 * into existentially quantified variables.
3917 * If a subset of the projected out variables are unrelated
3918 * to any of the variables that remain, then the constraints
3919 * involving this subset are simply dropped first.
3921 __isl_give isl_basic_map *isl_basic_map_project_out(
3922 __isl_take isl_basic_map *bmap,
3923 enum isl_dim_type type, unsigned first, unsigned n)
3925 if (n == 0)
3926 return basic_map_space_reset(bmap, type);
3927 if (type == isl_dim_div)
3928 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3929 "cannot project out existentially quantified variables",
3930 return isl_basic_map_free(bmap));
3932 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3933 if (!bmap)
3934 return NULL;
3936 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3937 return isl_basic_map_remove_dims(bmap, type, first, n);
3939 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3940 return isl_basic_map_free(bmap);
3942 bmap = move_last(bmap, type, first, n);
3943 bmap = isl_basic_map_cow(bmap);
3944 bmap = insert_div_rows(bmap, n);
3945 if (!bmap)
3946 return NULL;
3948 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3949 if (!bmap->dim)
3950 goto error;
3951 bmap = isl_basic_map_simplify(bmap);
3952 bmap = isl_basic_map_drop_redundant_divs(bmap);
3953 return isl_basic_map_finalize(bmap);
3954 error:
3955 isl_basic_map_free(bmap);
3956 return NULL;
3959 /* Turn the n dimensions of type type, starting at first
3960 * into existentially quantified variables.
3962 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3963 enum isl_dim_type type, unsigned first, unsigned n)
3965 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3966 type, first, n));
3969 /* Turn the n dimensions of type type, starting at first
3970 * into existentially quantified variables.
3972 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3973 enum isl_dim_type type, unsigned first, unsigned n)
3975 int i;
3977 if (!map)
3978 return NULL;
3980 if (n == 0)
3981 return map_space_reset(map, type);
3983 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3985 map = isl_map_cow(map);
3986 if (!map)
3987 return NULL;
3989 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3990 if (!map->dim)
3991 goto error;
3993 for (i = 0; i < map->n; ++i) {
3994 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3995 if (!map->p[i])
3996 goto error;
3999 return map;
4000 error:
4001 isl_map_free(map);
4002 return NULL;
4005 /* Turn the n dimensions of type type, starting at first
4006 * into existentially quantified variables.
4008 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4009 enum isl_dim_type type, unsigned first, unsigned n)
4011 return set_from_map(isl_map_project_out(set_to_map(set),
4012 type, first, n));
4015 /* Return a map that projects the elements in "set" onto their
4016 * "n" set dimensions starting at "first".
4017 * "type" should be equal to isl_dim_set.
4019 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4020 enum isl_dim_type type, unsigned first, unsigned n)
4022 int i;
4023 int dim;
4024 isl_map *map;
4026 if (!set)
4027 return NULL;
4028 if (type != isl_dim_set)
4029 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4030 "only set dimensions can be projected out", goto error);
4031 dim = isl_set_dim(set, isl_dim_set);
4032 if (first + n > dim || first + n < first)
4033 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4034 "index out of bounds", goto error);
4036 map = isl_map_from_domain(set);
4037 map = isl_map_add_dims(map, isl_dim_out, n);
4038 for (i = 0; i < n; ++i)
4039 map = isl_map_equate(map, isl_dim_in, first + i,
4040 isl_dim_out, i);
4041 return map;
4042 error:
4043 isl_set_free(set);
4044 return NULL;
4047 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4049 int i, j;
4051 for (i = 0; i < n; ++i) {
4052 j = isl_basic_map_alloc_div(bmap);
4053 if (j < 0)
4054 goto error;
4055 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4057 return bmap;
4058 error:
4059 isl_basic_map_free(bmap);
4060 return NULL;
4063 struct isl_basic_map *isl_basic_map_apply_range(
4064 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4066 isl_space *dim_result = NULL;
4067 struct isl_basic_map *bmap;
4068 unsigned n_in, n_out, n, nparam, total, pos;
4069 struct isl_dim_map *dim_map1, *dim_map2;
4071 if (!bmap1 || !bmap2)
4072 goto error;
4073 if (!isl_space_match(bmap1->dim, isl_dim_param,
4074 bmap2->dim, isl_dim_param))
4075 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4076 "parameters don't match", goto error);
4077 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4078 bmap2->dim, isl_dim_in))
4079 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4080 "spaces don't match", goto error);
4082 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4083 isl_space_copy(bmap2->dim));
4085 n_in = isl_basic_map_n_in(bmap1);
4086 n_out = isl_basic_map_n_out(bmap2);
4087 n = isl_basic_map_n_out(bmap1);
4088 nparam = isl_basic_map_n_param(bmap1);
4090 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4091 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4092 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4093 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4094 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4095 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4096 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4097 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4098 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4099 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4100 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4102 bmap = isl_basic_map_alloc_space(dim_result,
4103 bmap1->n_div + bmap2->n_div + n,
4104 bmap1->n_eq + bmap2->n_eq,
4105 bmap1->n_ineq + bmap2->n_ineq);
4106 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4107 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4108 bmap = add_divs(bmap, n);
4109 bmap = isl_basic_map_simplify(bmap);
4110 bmap = isl_basic_map_drop_redundant_divs(bmap);
4111 return isl_basic_map_finalize(bmap);
4112 error:
4113 isl_basic_map_free(bmap1);
4114 isl_basic_map_free(bmap2);
4115 return NULL;
4118 struct isl_basic_set *isl_basic_set_apply(
4119 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4121 if (!bset || !bmap)
4122 goto error;
4124 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4125 goto error);
4127 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4128 bmap));
4129 error:
4130 isl_basic_set_free(bset);
4131 isl_basic_map_free(bmap);
4132 return NULL;
4135 struct isl_basic_map *isl_basic_map_apply_domain(
4136 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4138 if (!bmap1 || !bmap2)
4139 goto error;
4141 isl_assert(bmap1->ctx,
4142 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
4143 isl_assert(bmap1->ctx,
4144 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
4145 goto error);
4147 bmap1 = isl_basic_map_reverse(bmap1);
4148 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4149 return isl_basic_map_reverse(bmap1);
4150 error:
4151 isl_basic_map_free(bmap1);
4152 isl_basic_map_free(bmap2);
4153 return NULL;
4156 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4157 * A \cap B -> f(A) + f(B)
4159 struct isl_basic_map *isl_basic_map_sum(
4160 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4162 unsigned n_in, n_out, nparam, total, pos;
4163 struct isl_basic_map *bmap = NULL;
4164 struct isl_dim_map *dim_map1, *dim_map2;
4165 int i;
4167 if (!bmap1 || !bmap2)
4168 goto error;
4170 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4171 goto error);
4173 nparam = isl_basic_map_n_param(bmap1);
4174 n_in = isl_basic_map_n_in(bmap1);
4175 n_out = isl_basic_map_n_out(bmap1);
4177 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4178 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4179 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4180 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4181 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4182 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4183 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4184 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4185 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4186 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4187 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4189 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4190 bmap1->n_div + bmap2->n_div + 2 * n_out,
4191 bmap1->n_eq + bmap2->n_eq + n_out,
4192 bmap1->n_ineq + bmap2->n_ineq);
4193 for (i = 0; i < n_out; ++i) {
4194 int j = isl_basic_map_alloc_equality(bmap);
4195 if (j < 0)
4196 goto error;
4197 isl_seq_clr(bmap->eq[j], 1+total);
4198 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4199 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4200 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4202 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4203 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4204 bmap = add_divs(bmap, 2 * n_out);
4206 bmap = isl_basic_map_simplify(bmap);
4207 return isl_basic_map_finalize(bmap);
4208 error:
4209 isl_basic_map_free(bmap);
4210 isl_basic_map_free(bmap1);
4211 isl_basic_map_free(bmap2);
4212 return NULL;
4215 /* Given two maps A -> f(A) and B -> g(B), construct a map
4216 * A \cap B -> f(A) + f(B)
4218 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4220 struct isl_map *result;
4221 int i, j;
4223 if (!map1 || !map2)
4224 goto error;
4226 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4228 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4229 map1->n * map2->n, 0);
4230 if (!result)
4231 goto error;
4232 for (i = 0; i < map1->n; ++i)
4233 for (j = 0; j < map2->n; ++j) {
4234 struct isl_basic_map *part;
4235 part = isl_basic_map_sum(
4236 isl_basic_map_copy(map1->p[i]),
4237 isl_basic_map_copy(map2->p[j]));
4238 if (isl_basic_map_is_empty(part))
4239 isl_basic_map_free(part);
4240 else
4241 result = isl_map_add_basic_map(result, part);
4242 if (!result)
4243 goto error;
4245 isl_map_free(map1);
4246 isl_map_free(map2);
4247 return result;
4248 error:
4249 isl_map_free(map1);
4250 isl_map_free(map2);
4251 return NULL;
4254 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4255 __isl_take isl_set *set2)
4257 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4260 /* Given a basic map A -> f(A), construct A -> -f(A).
4262 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4264 int i, j;
4265 unsigned off, n;
4267 bmap = isl_basic_map_cow(bmap);
4268 if (!bmap)
4269 return NULL;
4271 n = isl_basic_map_dim(bmap, isl_dim_out);
4272 off = isl_basic_map_offset(bmap, isl_dim_out);
4273 for (i = 0; i < bmap->n_eq; ++i)
4274 for (j = 0; j < n; ++j)
4275 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4276 for (i = 0; i < bmap->n_ineq; ++i)
4277 for (j = 0; j < n; ++j)
4278 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4279 for (i = 0; i < bmap->n_div; ++i)
4280 for (j = 0; j < n; ++j)
4281 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4282 bmap = isl_basic_map_gauss(bmap, NULL);
4283 return isl_basic_map_finalize(bmap);
4286 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4288 return isl_basic_map_neg(bset);
4291 /* Given a map A -> f(A), construct A -> -f(A).
4293 struct isl_map *isl_map_neg(struct isl_map *map)
4295 int i;
4297 map = isl_map_cow(map);
4298 if (!map)
4299 return NULL;
4301 for (i = 0; i < map->n; ++i) {
4302 map->p[i] = isl_basic_map_neg(map->p[i]);
4303 if (!map->p[i])
4304 goto error;
4307 return map;
4308 error:
4309 isl_map_free(map);
4310 return NULL;
4313 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4315 return set_from_map(isl_map_neg(set_to_map(set)));
4318 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4319 * A -> floor(f(A)/d).
4321 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4322 isl_int d)
4324 unsigned n_in, n_out, nparam, total, pos;
4325 struct isl_basic_map *result = NULL;
4326 struct isl_dim_map *dim_map;
4327 int i;
4329 if (!bmap)
4330 return NULL;
4332 nparam = isl_basic_map_n_param(bmap);
4333 n_in = isl_basic_map_n_in(bmap);
4334 n_out = isl_basic_map_n_out(bmap);
4336 total = nparam + n_in + n_out + bmap->n_div + n_out;
4337 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4338 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4339 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4340 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4341 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4343 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4344 bmap->n_div + n_out,
4345 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4346 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4347 result = add_divs(result, n_out);
4348 for (i = 0; i < n_out; ++i) {
4349 int j;
4350 j = isl_basic_map_alloc_inequality(result);
4351 if (j < 0)
4352 goto error;
4353 isl_seq_clr(result->ineq[j], 1+total);
4354 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4355 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4356 j = isl_basic_map_alloc_inequality(result);
4357 if (j < 0)
4358 goto error;
4359 isl_seq_clr(result->ineq[j], 1+total);
4360 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4361 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4362 isl_int_sub_ui(result->ineq[j][0], d, 1);
4365 result = isl_basic_map_simplify(result);
4366 return isl_basic_map_finalize(result);
4367 error:
4368 isl_basic_map_free(result);
4369 return NULL;
4372 /* Given a map A -> f(A) and an integer d, construct a map
4373 * A -> floor(f(A)/d).
4375 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4377 int i;
4379 map = isl_map_cow(map);
4380 if (!map)
4381 return NULL;
4383 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4384 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4385 for (i = 0; i < map->n; ++i) {
4386 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4387 if (!map->p[i])
4388 goto error;
4391 return map;
4392 error:
4393 isl_map_free(map);
4394 return NULL;
4397 /* Given a map A -> f(A) and an integer d, construct a map
4398 * A -> floor(f(A)/d).
4400 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4401 __isl_take isl_val *d)
4403 if (!map || !d)
4404 goto error;
4405 if (!isl_val_is_int(d))
4406 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4407 "expecting integer denominator", goto error);
4408 map = isl_map_floordiv(map, d->n);
4409 isl_val_free(d);
4410 return map;
4411 error:
4412 isl_map_free(map);
4413 isl_val_free(d);
4414 return NULL;
4417 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4419 int i;
4420 unsigned nparam;
4421 unsigned n_in;
4423 i = isl_basic_map_alloc_equality(bmap);
4424 if (i < 0)
4425 goto error;
4426 nparam = isl_basic_map_n_param(bmap);
4427 n_in = isl_basic_map_n_in(bmap);
4428 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4429 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4430 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4431 return isl_basic_map_finalize(bmap);
4432 error:
4433 isl_basic_map_free(bmap);
4434 return NULL;
4437 /* Add a constraint to "bmap" expressing i_pos < o_pos
4439 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4441 int i;
4442 unsigned nparam;
4443 unsigned n_in;
4445 i = isl_basic_map_alloc_inequality(bmap);
4446 if (i < 0)
4447 goto error;
4448 nparam = isl_basic_map_n_param(bmap);
4449 n_in = isl_basic_map_n_in(bmap);
4450 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4451 isl_int_set_si(bmap->ineq[i][0], -1);
4452 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4453 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4454 return isl_basic_map_finalize(bmap);
4455 error:
4456 isl_basic_map_free(bmap);
4457 return NULL;
4460 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4462 static __isl_give isl_basic_map *var_less_or_equal(
4463 __isl_take isl_basic_map *bmap, unsigned pos)
4465 int i;
4466 unsigned nparam;
4467 unsigned n_in;
4469 i = isl_basic_map_alloc_inequality(bmap);
4470 if (i < 0)
4471 goto error;
4472 nparam = isl_basic_map_n_param(bmap);
4473 n_in = isl_basic_map_n_in(bmap);
4474 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4475 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4476 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4477 return isl_basic_map_finalize(bmap);
4478 error:
4479 isl_basic_map_free(bmap);
4480 return NULL;
4483 /* Add a constraint to "bmap" expressing i_pos > o_pos
4485 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4487 int i;
4488 unsigned nparam;
4489 unsigned n_in;
4491 i = isl_basic_map_alloc_inequality(bmap);
4492 if (i < 0)
4493 goto error;
4494 nparam = isl_basic_map_n_param(bmap);
4495 n_in = isl_basic_map_n_in(bmap);
4496 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4497 isl_int_set_si(bmap->ineq[i][0], -1);
4498 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4499 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4500 return isl_basic_map_finalize(bmap);
4501 error:
4502 isl_basic_map_free(bmap);
4503 return NULL;
4506 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4508 static __isl_give isl_basic_map *var_more_or_equal(
4509 __isl_take isl_basic_map *bmap, unsigned pos)
4511 int i;
4512 unsigned nparam;
4513 unsigned n_in;
4515 i = isl_basic_map_alloc_inequality(bmap);
4516 if (i < 0)
4517 goto error;
4518 nparam = isl_basic_map_n_param(bmap);
4519 n_in = isl_basic_map_n_in(bmap);
4520 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4521 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4522 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4523 return isl_basic_map_finalize(bmap);
4524 error:
4525 isl_basic_map_free(bmap);
4526 return NULL;
4529 __isl_give isl_basic_map *isl_basic_map_equal(
4530 __isl_take isl_space *dim, unsigned n_equal)
4532 int i;
4533 struct isl_basic_map *bmap;
4534 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4535 if (!bmap)
4536 return NULL;
4537 for (i = 0; i < n_equal && bmap; ++i)
4538 bmap = var_equal(bmap, i);
4539 return isl_basic_map_finalize(bmap);
4542 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4544 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4545 unsigned pos)
4547 int i;
4548 struct isl_basic_map *bmap;
4549 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4550 if (!bmap)
4551 return NULL;
4552 for (i = 0; i < pos && bmap; ++i)
4553 bmap = var_equal(bmap, i);
4554 if (bmap)
4555 bmap = var_less(bmap, pos);
4556 return isl_basic_map_finalize(bmap);
4559 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4561 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4562 __isl_take isl_space *dim, unsigned pos)
4564 int i;
4565 isl_basic_map *bmap;
4567 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4568 for (i = 0; i < pos; ++i)
4569 bmap = var_equal(bmap, i);
4570 bmap = var_less_or_equal(bmap, pos);
4571 return isl_basic_map_finalize(bmap);
4574 /* Return a relation on "dim" expressing i_pos > o_pos
4576 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4577 unsigned pos)
4579 int i;
4580 struct isl_basic_map *bmap;
4581 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4582 if (!bmap)
4583 return NULL;
4584 for (i = 0; i < pos && bmap; ++i)
4585 bmap = var_equal(bmap, i);
4586 if (bmap)
4587 bmap = var_more(bmap, pos);
4588 return isl_basic_map_finalize(bmap);
4591 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4593 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4594 __isl_take isl_space *dim, unsigned pos)
4596 int i;
4597 isl_basic_map *bmap;
4599 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4600 for (i = 0; i < pos; ++i)
4601 bmap = var_equal(bmap, i);
4602 bmap = var_more_or_equal(bmap, pos);
4603 return isl_basic_map_finalize(bmap);
4606 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4607 unsigned n, int equal)
4609 struct isl_map *map;
4610 int i;
4612 if (n == 0 && equal)
4613 return isl_map_universe(dims);
4615 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4617 for (i = 0; i + 1 < n; ++i)
4618 map = isl_map_add_basic_map(map,
4619 isl_basic_map_less_at(isl_space_copy(dims), i));
4620 if (n > 0) {
4621 if (equal)
4622 map = isl_map_add_basic_map(map,
4623 isl_basic_map_less_or_equal_at(dims, n - 1));
4624 else
4625 map = isl_map_add_basic_map(map,
4626 isl_basic_map_less_at(dims, n - 1));
4627 } else
4628 isl_space_free(dims);
4630 return map;
4633 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4635 if (!dims)
4636 return NULL;
4637 return map_lex_lte_first(dims, dims->n_out, equal);
4640 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4642 return map_lex_lte_first(dim, n, 0);
4645 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4647 return map_lex_lte_first(dim, n, 1);
4650 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4652 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4655 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4657 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4660 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4661 unsigned n, int equal)
4663 struct isl_map *map;
4664 int i;
4666 if (n == 0 && equal)
4667 return isl_map_universe(dims);
4669 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4671 for (i = 0; i + 1 < n; ++i)
4672 map = isl_map_add_basic_map(map,
4673 isl_basic_map_more_at(isl_space_copy(dims), i));
4674 if (n > 0) {
4675 if (equal)
4676 map = isl_map_add_basic_map(map,
4677 isl_basic_map_more_or_equal_at(dims, n - 1));
4678 else
4679 map = isl_map_add_basic_map(map,
4680 isl_basic_map_more_at(dims, n - 1));
4681 } else
4682 isl_space_free(dims);
4684 return map;
4687 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4689 if (!dims)
4690 return NULL;
4691 return map_lex_gte_first(dims, dims->n_out, equal);
4694 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4696 return map_lex_gte_first(dim, n, 0);
4699 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4701 return map_lex_gte_first(dim, n, 1);
4704 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4706 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4709 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4711 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4714 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4715 __isl_take isl_set *set2)
4717 isl_map *map;
4718 map = isl_map_lex_le(isl_set_get_space(set1));
4719 map = isl_map_intersect_domain(map, set1);
4720 map = isl_map_intersect_range(map, set2);
4721 return map;
4724 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4725 __isl_take isl_set *set2)
4727 isl_map *map;
4728 map = isl_map_lex_lt(isl_set_get_space(set1));
4729 map = isl_map_intersect_domain(map, set1);
4730 map = isl_map_intersect_range(map, set2);
4731 return map;
4734 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4735 __isl_take isl_set *set2)
4737 isl_map *map;
4738 map = isl_map_lex_ge(isl_set_get_space(set1));
4739 map = isl_map_intersect_domain(map, set1);
4740 map = isl_map_intersect_range(map, set2);
4741 return map;
4744 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4745 __isl_take isl_set *set2)
4747 isl_map *map;
4748 map = isl_map_lex_gt(isl_set_get_space(set1));
4749 map = isl_map_intersect_domain(map, set1);
4750 map = isl_map_intersect_range(map, set2);
4751 return map;
4754 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4755 __isl_take isl_map *map2)
4757 isl_map *map;
4758 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4759 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4760 map = isl_map_apply_range(map, isl_map_reverse(map2));
4761 return map;
4764 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4765 __isl_take isl_map *map2)
4767 isl_map *map;
4768 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4769 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4770 map = isl_map_apply_range(map, isl_map_reverse(map2));
4771 return map;
4774 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4775 __isl_take isl_map *map2)
4777 isl_map *map;
4778 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4779 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4780 map = isl_map_apply_range(map, isl_map_reverse(map2));
4781 return map;
4784 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4785 __isl_take isl_map *map2)
4787 isl_map *map;
4788 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4789 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4790 map = isl_map_apply_range(map, isl_map_reverse(map2));
4791 return map;
4794 static __isl_give isl_basic_map *basic_map_from_basic_set(
4795 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4797 struct isl_basic_map *bmap;
4799 bset = isl_basic_set_cow(bset);
4800 if (!bset || !dim)
4801 goto error;
4803 isl_assert(bset->ctx, isl_space_compatible_internal(bset->dim, dim),
4804 goto error);
4805 isl_space_free(bset->dim);
4806 bmap = bset_to_bmap(bset);
4807 bmap->dim = dim;
4808 return isl_basic_map_finalize(bmap);
4809 error:
4810 isl_basic_set_free(bset);
4811 isl_space_free(dim);
4812 return NULL;
4815 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4816 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
4818 return basic_map_from_basic_set(bset, space);
4821 /* For a div d = floor(f/m), add the constraint
4823 * f - m d >= 0
4825 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4826 unsigned pos, isl_int *div)
4828 int i;
4829 unsigned total = isl_basic_map_total_dim(bmap);
4831 i = isl_basic_map_alloc_inequality(bmap);
4832 if (i < 0)
4833 return -1;
4834 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4835 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4837 return 0;
4840 /* For a div d = floor(f/m), add the constraint
4842 * -(f-(m-1)) + m d >= 0
4844 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4845 unsigned pos, isl_int *div)
4847 int i;
4848 unsigned total = isl_basic_map_total_dim(bmap);
4850 i = isl_basic_map_alloc_inequality(bmap);
4851 if (i < 0)
4852 return -1;
4853 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4854 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4855 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4856 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4858 return 0;
4861 /* For a div d = floor(f/m), add the constraints
4863 * f - m d >= 0
4864 * -(f-(m-1)) + m d >= 0
4866 * Note that the second constraint is the negation of
4868 * f - m d >= m
4870 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4871 unsigned pos, isl_int *div)
4873 if (add_upper_div_constraint(bmap, pos, div) < 0)
4874 return -1;
4875 if (add_lower_div_constraint(bmap, pos, div) < 0)
4876 return -1;
4877 return 0;
4880 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4881 unsigned pos, isl_int *div)
4883 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4884 pos, div);
4887 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4889 unsigned total = isl_basic_map_total_dim(bmap);
4890 unsigned div_pos = total - bmap->n_div + div;
4892 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4893 bmap->div[div]);
4896 /* For each known div d = floor(f/m), add the constraints
4898 * f - m d >= 0
4899 * -(f-(m-1)) + m d >= 0
4901 * Remove duplicate constraints in case of some these div constraints
4902 * already appear in "bmap".
4904 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4905 __isl_take isl_basic_map *bmap)
4907 unsigned n_div;
4909 if (!bmap)
4910 return NULL;
4911 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4912 if (n_div == 0)
4913 return bmap;
4915 bmap = add_known_div_constraints(bmap);
4916 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4917 bmap = isl_basic_map_finalize(bmap);
4918 return bmap;
4921 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4923 * In particular, if this div is of the form d = floor(f/m),
4924 * then add the constraint
4926 * f - m d >= 0
4928 * if sign < 0 or the constraint
4930 * -(f-(m-1)) + m d >= 0
4932 * if sign > 0.
4934 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4935 unsigned div, int sign)
4937 unsigned total;
4938 unsigned div_pos;
4940 if (!bmap)
4941 return -1;
4943 total = isl_basic_map_total_dim(bmap);
4944 div_pos = total - bmap->n_div + div;
4946 if (sign < 0)
4947 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4948 else
4949 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4952 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4954 return isl_basic_map_add_div_constraints(bset, div);
4957 struct isl_basic_set *isl_basic_map_underlying_set(
4958 struct isl_basic_map *bmap)
4960 if (!bmap)
4961 goto error;
4962 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4963 bmap->n_div == 0 &&
4964 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4965 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4966 return bset_from_bmap(bmap);
4967 bmap = isl_basic_map_cow(bmap);
4968 if (!bmap)
4969 goto error;
4970 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4971 if (!bmap->dim)
4972 goto error;
4973 bmap->extra -= bmap->n_div;
4974 bmap->n_div = 0;
4975 bmap = isl_basic_map_finalize(bmap);
4976 return bset_from_bmap(bmap);
4977 error:
4978 isl_basic_map_free(bmap);
4979 return NULL;
4982 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4983 __isl_take isl_basic_set *bset)
4985 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4988 /* Replace each element in "list" by the result of applying
4989 * isl_basic_map_underlying_set to the element.
4991 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4992 __isl_take isl_basic_map_list *list)
4994 int i, n;
4996 if (!list)
4997 return NULL;
4999 n = isl_basic_map_list_n_basic_map(list);
5000 for (i = 0; i < n; ++i) {
5001 isl_basic_map *bmap;
5002 isl_basic_set *bset;
5004 bmap = isl_basic_map_list_get_basic_map(list, i);
5005 bset = isl_basic_set_underlying_set(bmap);
5006 list = isl_basic_set_list_set_basic_set(list, i, bset);
5009 return list;
5012 struct isl_basic_map *isl_basic_map_overlying_set(
5013 struct isl_basic_set *bset, struct isl_basic_map *like)
5015 struct isl_basic_map *bmap;
5016 struct isl_ctx *ctx;
5017 unsigned total;
5018 int i;
5020 if (!bset || !like)
5021 goto error;
5022 ctx = bset->ctx;
5023 isl_assert(ctx, bset->n_div == 0, goto error);
5024 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5025 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5026 goto error);
5027 if (like->n_div == 0) {
5028 isl_space *space = isl_basic_map_get_space(like);
5029 isl_basic_map_free(like);
5030 return isl_basic_map_reset_space(bset, space);
5032 bset = isl_basic_set_cow(bset);
5033 if (!bset)
5034 goto error;
5035 total = bset->dim->n_out + bset->extra;
5036 bmap = bset_to_bmap(bset);
5037 isl_space_free(bmap->dim);
5038 bmap->dim = isl_space_copy(like->dim);
5039 if (!bmap->dim)
5040 goto error;
5041 bmap->n_div = like->n_div;
5042 bmap->extra += like->n_div;
5043 if (bmap->extra) {
5044 unsigned ltotal;
5045 isl_int **div;
5046 ltotal = total - bmap->extra + like->extra;
5047 if (ltotal > total)
5048 ltotal = total;
5049 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5050 bmap->extra * (1 + 1 + total));
5051 if (isl_blk_is_error(bmap->block2))
5052 goto error;
5053 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5054 if (!div)
5055 goto error;
5056 bmap->div = div;
5057 for (i = 0; i < bmap->extra; ++i)
5058 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5059 for (i = 0; i < like->n_div; ++i) {
5060 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5061 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5063 bmap = isl_basic_map_add_known_div_constraints(bmap);
5065 isl_basic_map_free(like);
5066 bmap = isl_basic_map_simplify(bmap);
5067 bmap = isl_basic_map_finalize(bmap);
5068 return bmap;
5069 error:
5070 isl_basic_map_free(like);
5071 isl_basic_set_free(bset);
5072 return NULL;
5075 struct isl_basic_set *isl_basic_set_from_underlying_set(
5076 struct isl_basic_set *bset, struct isl_basic_set *like)
5078 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5079 bset_to_bmap(like)));
5082 struct isl_set *isl_set_from_underlying_set(
5083 struct isl_set *set, struct isl_basic_set *like)
5085 int i;
5087 if (!set || !like)
5088 goto error;
5089 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
5090 goto error);
5091 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
5092 isl_basic_set_free(like);
5093 return set;
5095 set = isl_set_cow(set);
5096 if (!set)
5097 goto error;
5098 for (i = 0; i < set->n; ++i) {
5099 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
5100 isl_basic_set_copy(like));
5101 if (!set->p[i])
5102 goto error;
5104 isl_space_free(set->dim);
5105 set->dim = isl_space_copy(like->dim);
5106 if (!set->dim)
5107 goto error;
5108 isl_basic_set_free(like);
5109 return set;
5110 error:
5111 isl_basic_set_free(like);
5112 isl_set_free(set);
5113 return NULL;
5116 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5118 int i;
5120 map = isl_map_cow(map);
5121 if (!map)
5122 return NULL;
5123 map->dim = isl_space_cow(map->dim);
5124 if (!map->dim)
5125 goto error;
5127 for (i = 1; i < map->n; ++i)
5128 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5129 goto error);
5130 for (i = 0; i < map->n; ++i) {
5131 map->p[i] = bset_to_bmap(
5132 isl_basic_map_underlying_set(map->p[i]));
5133 if (!map->p[i])
5134 goto error;
5136 if (map->n == 0)
5137 map->dim = isl_space_underlying(map->dim, 0);
5138 else {
5139 isl_space_free(map->dim);
5140 map->dim = isl_space_copy(map->p[0]->dim);
5142 if (!map->dim)
5143 goto error;
5144 return set_from_map(map);
5145 error:
5146 isl_map_free(map);
5147 return NULL;
5150 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
5152 return set_from_map(isl_map_underlying_set(set_to_map(set)));
5155 /* Replace the space of "bmap" by "space".
5157 * If the space of "bmap" is identical to "space" (including the identifiers
5158 * of the input and output dimensions), then simply return the original input.
5160 __isl_give isl_basic_map *isl_basic_map_reset_space(
5161 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5163 isl_bool equal;
5165 if (!bmap)
5166 goto error;
5167 equal = isl_space_is_equal(bmap->dim, space);
5168 if (equal >= 0 && equal)
5169 equal = isl_space_match(bmap->dim, isl_dim_in,
5170 space, isl_dim_in);
5171 if (equal >= 0 && equal)
5172 equal = isl_space_match(bmap->dim, isl_dim_out,
5173 space, isl_dim_out);
5174 if (equal < 0)
5175 goto error;
5176 if (equal) {
5177 isl_space_free(space);
5178 return bmap;
5180 bmap = isl_basic_map_cow(bmap);
5181 if (!bmap || !space)
5182 goto error;
5184 isl_space_free(bmap->dim);
5185 bmap->dim = space;
5187 bmap = isl_basic_map_finalize(bmap);
5189 return bmap;
5190 error:
5191 isl_basic_map_free(bmap);
5192 isl_space_free(space);
5193 return NULL;
5196 __isl_give isl_basic_set *isl_basic_set_reset_space(
5197 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5199 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5200 dim));
5203 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5204 __isl_take isl_space *dim)
5206 int i;
5208 map = isl_map_cow(map);
5209 if (!map || !dim)
5210 goto error;
5212 for (i = 0; i < map->n; ++i) {
5213 map->p[i] = isl_basic_map_reset_space(map->p[i],
5214 isl_space_copy(dim));
5215 if (!map->p[i])
5216 goto error;
5218 isl_space_free(map->dim);
5219 map->dim = dim;
5221 return map;
5222 error:
5223 isl_map_free(map);
5224 isl_space_free(dim);
5225 return NULL;
5228 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5229 __isl_take isl_space *dim)
5231 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5234 /* Compute the parameter domain of the given basic set.
5236 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5238 isl_bool is_params;
5239 isl_space *space;
5240 unsigned n;
5242 is_params = isl_basic_set_is_params(bset);
5243 if (is_params < 0)
5244 return isl_basic_set_free(bset);
5245 if (is_params)
5246 return bset;
5248 n = isl_basic_set_dim(bset, isl_dim_set);
5249 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5250 space = isl_basic_set_get_space(bset);
5251 space = isl_space_params(space);
5252 bset = isl_basic_set_reset_space(bset, space);
5253 return bset;
5256 /* Construct a zero-dimensional basic set with the given parameter domain.
5258 __isl_give isl_basic_set *isl_basic_set_from_params(
5259 __isl_take isl_basic_set *bset)
5261 isl_space *space;
5262 space = isl_basic_set_get_space(bset);
5263 space = isl_space_set_from_params(space);
5264 bset = isl_basic_set_reset_space(bset, space);
5265 return bset;
5268 /* Compute the parameter domain of the given set.
5270 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5272 isl_space *space;
5273 unsigned n;
5275 if (isl_set_is_params(set))
5276 return set;
5278 n = isl_set_dim(set, isl_dim_set);
5279 set = isl_set_project_out(set, isl_dim_set, 0, n);
5280 space = isl_set_get_space(set);
5281 space = isl_space_params(space);
5282 set = isl_set_reset_space(set, space);
5283 return set;
5286 /* Construct a zero-dimensional set with the given parameter domain.
5288 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5290 isl_space *space;
5291 space = isl_set_get_space(set);
5292 space = isl_space_set_from_params(space);
5293 set = isl_set_reset_space(set, space);
5294 return set;
5297 /* Compute the parameter domain of the given map.
5299 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5301 isl_space *space;
5302 unsigned n;
5304 n = isl_map_dim(map, isl_dim_in);
5305 map = isl_map_project_out(map, isl_dim_in, 0, n);
5306 n = isl_map_dim(map, isl_dim_out);
5307 map = isl_map_project_out(map, isl_dim_out, 0, n);
5308 space = isl_map_get_space(map);
5309 space = isl_space_params(space);
5310 map = isl_map_reset_space(map, space);
5311 return map;
5314 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5316 isl_space *space;
5317 unsigned n_out;
5319 if (!bmap)
5320 return NULL;
5321 space = isl_space_domain(isl_basic_map_get_space(bmap));
5323 n_out = isl_basic_map_n_out(bmap);
5324 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5326 return isl_basic_map_reset_space(bmap, space);
5329 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5331 if (!bmap)
5332 return isl_bool_error;
5333 return isl_space_may_be_set(bmap->dim);
5336 /* Is this basic map actually a set?
5337 * Users should never call this function. Outside of isl,
5338 * the type should indicate whether something is a set or a map.
5340 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5342 if (!bmap)
5343 return isl_bool_error;
5344 return isl_space_is_set(bmap->dim);
5347 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5349 isl_bool is_set;
5351 is_set = isl_basic_map_is_set(bmap);
5352 if (is_set < 0)
5353 goto error;
5354 if (is_set)
5355 return bmap;
5356 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5357 error:
5358 isl_basic_map_free(bmap);
5359 return NULL;
5362 __isl_give isl_basic_map *isl_basic_map_domain_map(
5363 __isl_take isl_basic_map *bmap)
5365 int i;
5366 isl_space *dim;
5367 isl_basic_map *domain;
5368 int nparam, n_in, n_out;
5370 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5371 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5372 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5374 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5375 domain = isl_basic_map_universe(dim);
5377 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5378 bmap = isl_basic_map_apply_range(bmap, domain);
5379 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5381 for (i = 0; i < n_in; ++i)
5382 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5383 isl_dim_out, i);
5385 bmap = isl_basic_map_gauss(bmap, NULL);
5386 return isl_basic_map_finalize(bmap);
5389 __isl_give isl_basic_map *isl_basic_map_range_map(
5390 __isl_take isl_basic_map *bmap)
5392 int i;
5393 isl_space *dim;
5394 isl_basic_map *range;
5395 int nparam, n_in, n_out;
5397 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5398 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5399 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5401 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5402 range = isl_basic_map_universe(dim);
5404 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5405 bmap = isl_basic_map_apply_range(bmap, range);
5406 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5408 for (i = 0; i < n_out; ++i)
5409 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5410 isl_dim_out, i);
5412 bmap = isl_basic_map_gauss(bmap, NULL);
5413 return isl_basic_map_finalize(bmap);
5416 int isl_map_may_be_set(__isl_keep isl_map *map)
5418 if (!map)
5419 return -1;
5420 return isl_space_may_be_set(map->dim);
5423 /* Is this map actually a set?
5424 * Users should never call this function. Outside of isl,
5425 * the type should indicate whether something is a set or a map.
5427 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5429 if (!map)
5430 return isl_bool_error;
5431 return isl_space_is_set(map->dim);
5434 struct isl_set *isl_map_range(struct isl_map *map)
5436 int i;
5437 isl_bool is_set;
5438 struct isl_set *set;
5440 is_set = isl_map_is_set(map);
5441 if (is_set < 0)
5442 goto error;
5443 if (is_set)
5444 return set_from_map(map);
5446 map = isl_map_cow(map);
5447 if (!map)
5448 goto error;
5450 set = set_from_map(map);
5451 set->dim = isl_space_range(set->dim);
5452 if (!set->dim)
5453 goto error;
5454 for (i = 0; i < map->n; ++i) {
5455 set->p[i] = isl_basic_map_range(map->p[i]);
5456 if (!set->p[i])
5457 goto error;
5459 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5460 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5461 return set;
5462 error:
5463 isl_map_free(map);
5464 return NULL;
5467 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5469 int i;
5471 map = isl_map_cow(map);
5472 if (!map)
5473 return NULL;
5475 map->dim = isl_space_domain_map(map->dim);
5476 if (!map->dim)
5477 goto error;
5478 for (i = 0; i < map->n; ++i) {
5479 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5480 if (!map->p[i])
5481 goto error;
5483 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5484 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5485 return map;
5486 error:
5487 isl_map_free(map);
5488 return NULL;
5491 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5493 int i;
5494 isl_space *range_dim;
5496 map = isl_map_cow(map);
5497 if (!map)
5498 return NULL;
5500 range_dim = isl_space_range(isl_map_get_space(map));
5501 range_dim = isl_space_from_range(range_dim);
5502 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5503 map->dim = isl_space_join(map->dim, range_dim);
5504 if (!map->dim)
5505 goto error;
5506 for (i = 0; i < map->n; ++i) {
5507 map->p[i] = isl_basic_map_range_map(map->p[i]);
5508 if (!map->p[i])
5509 goto error;
5511 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5512 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5513 return map;
5514 error:
5515 isl_map_free(map);
5516 return NULL;
5519 /* Given a wrapped map of the form A[B -> C],
5520 * return the map A[B -> C] -> B.
5522 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5524 isl_id *id;
5525 isl_map *map;
5527 if (!set)
5528 return NULL;
5529 if (!isl_set_has_tuple_id(set))
5530 return isl_map_domain_map(isl_set_unwrap(set));
5532 id = isl_set_get_tuple_id(set);
5533 map = isl_map_domain_map(isl_set_unwrap(set));
5534 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5536 return map;
5539 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5540 __isl_take isl_space *dim)
5542 int i;
5543 struct isl_map *map = NULL;
5545 set = isl_set_cow(set);
5546 if (!set || !dim)
5547 goto error;
5548 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5549 goto error);
5550 map = set_to_map(set);
5551 for (i = 0; i < set->n; ++i) {
5552 map->p[i] = basic_map_from_basic_set(
5553 set->p[i], isl_space_copy(dim));
5554 if (!map->p[i])
5555 goto error;
5557 isl_space_free(map->dim);
5558 map->dim = dim;
5559 return map;
5560 error:
5561 isl_space_free(dim);
5562 isl_set_free(set);
5563 return NULL;
5566 __isl_give isl_basic_map *isl_basic_map_from_domain(
5567 __isl_take isl_basic_set *bset)
5569 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5572 __isl_give isl_basic_map *isl_basic_map_from_range(
5573 __isl_take isl_basic_set *bset)
5575 isl_space *space;
5576 space = isl_basic_set_get_space(bset);
5577 space = isl_space_from_range(space);
5578 bset = isl_basic_set_reset_space(bset, space);
5579 return bset_to_bmap(bset);
5582 /* Create a relation with the given set as range.
5583 * The domain of the created relation is a zero-dimensional
5584 * flat anonymous space.
5586 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5588 isl_space *space;
5589 space = isl_set_get_space(set);
5590 space = isl_space_from_range(space);
5591 set = isl_set_reset_space(set, space);
5592 return set_to_map(set);
5595 /* Create a relation with the given set as domain.
5596 * The range of the created relation is a zero-dimensional
5597 * flat anonymous space.
5599 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5601 return isl_map_reverse(isl_map_from_range(set));
5604 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5605 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5607 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5610 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5611 __isl_take isl_set *range)
5613 return isl_map_apply_range(isl_map_reverse(domain), range);
5616 /* Return a newly allocated isl_map with given space and flags and
5617 * room for "n" basic maps.
5618 * Make sure that all cached information is cleared.
5620 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5621 unsigned flags)
5623 struct isl_map *map;
5625 if (!space)
5626 return NULL;
5627 if (n < 0)
5628 isl_die(space->ctx, isl_error_internal,
5629 "negative number of basic maps", goto error);
5630 map = isl_calloc(space->ctx, struct isl_map,
5631 sizeof(struct isl_map) +
5632 (n - 1) * sizeof(struct isl_basic_map *));
5633 if (!map)
5634 goto error;
5636 map->ctx = space->ctx;
5637 isl_ctx_ref(map->ctx);
5638 map->ref = 1;
5639 map->size = n;
5640 map->n = 0;
5641 map->dim = space;
5642 map->flags = flags;
5643 return map;
5644 error:
5645 isl_space_free(space);
5646 return NULL;
5649 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5650 unsigned nparam, unsigned in, unsigned out, int n,
5651 unsigned flags)
5653 struct isl_map *map;
5654 isl_space *dims;
5656 dims = isl_space_alloc(ctx, nparam, in, out);
5657 if (!dims)
5658 return NULL;
5660 map = isl_map_alloc_space(dims, n, flags);
5661 return map;
5664 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5666 struct isl_basic_map *bmap;
5667 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5668 bmap = isl_basic_map_set_to_empty(bmap);
5669 return bmap;
5672 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5674 struct isl_basic_set *bset;
5675 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5676 bset = isl_basic_set_set_to_empty(bset);
5677 return bset;
5680 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5682 struct isl_basic_map *bmap;
5683 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5684 bmap = isl_basic_map_finalize(bmap);
5685 return bmap;
5688 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5690 struct isl_basic_set *bset;
5691 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5692 bset = isl_basic_set_finalize(bset);
5693 return bset;
5696 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5698 int i;
5699 unsigned total = isl_space_dim(dim, isl_dim_all);
5700 isl_basic_map *bmap;
5702 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5703 for (i = 0; i < total; ++i) {
5704 int k = isl_basic_map_alloc_inequality(bmap);
5705 if (k < 0)
5706 goto error;
5707 isl_seq_clr(bmap->ineq[k], 1 + total);
5708 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5710 return bmap;
5711 error:
5712 isl_basic_map_free(bmap);
5713 return NULL;
5716 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5718 return isl_basic_map_nat_universe(dim);
5721 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5723 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5726 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5728 return isl_map_nat_universe(dim);
5731 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5733 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5736 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5738 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5741 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5743 struct isl_map *map;
5744 if (!dim)
5745 return NULL;
5746 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5747 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5748 return map;
5751 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5753 struct isl_set *set;
5754 if (!dim)
5755 return NULL;
5756 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5757 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5758 return set;
5761 struct isl_map *isl_map_dup(struct isl_map *map)
5763 int i;
5764 struct isl_map *dup;
5766 if (!map)
5767 return NULL;
5768 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5769 for (i = 0; i < map->n; ++i)
5770 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5771 return dup;
5774 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5775 __isl_take isl_basic_map *bmap)
5777 if (!bmap || !map)
5778 goto error;
5779 if (isl_basic_map_plain_is_empty(bmap)) {
5780 isl_basic_map_free(bmap);
5781 return map;
5783 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5784 isl_assert(map->ctx, map->n < map->size, goto error);
5785 map->p[map->n] = bmap;
5786 map->n++;
5787 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5788 return map;
5789 error:
5790 if (map)
5791 isl_map_free(map);
5792 if (bmap)
5793 isl_basic_map_free(bmap);
5794 return NULL;
5797 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5799 int i;
5801 if (!map)
5802 return NULL;
5804 if (--map->ref > 0)
5805 return NULL;
5807 clear_caches(map);
5808 isl_ctx_deref(map->ctx);
5809 for (i = 0; i < map->n; ++i)
5810 isl_basic_map_free(map->p[i]);
5811 isl_space_free(map->dim);
5812 free(map);
5814 return NULL;
5817 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5818 struct isl_basic_map *bmap, unsigned pos, int value)
5820 int j;
5822 bmap = isl_basic_map_cow(bmap);
5823 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5824 j = isl_basic_map_alloc_equality(bmap);
5825 if (j < 0)
5826 goto error;
5827 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5828 isl_int_set_si(bmap->eq[j][pos], -1);
5829 isl_int_set_si(bmap->eq[j][0], value);
5830 bmap = isl_basic_map_simplify(bmap);
5831 return isl_basic_map_finalize(bmap);
5832 error:
5833 isl_basic_map_free(bmap);
5834 return NULL;
5837 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5838 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5840 int j;
5842 bmap = isl_basic_map_cow(bmap);
5843 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5844 j = isl_basic_map_alloc_equality(bmap);
5845 if (j < 0)
5846 goto error;
5847 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5848 isl_int_set_si(bmap->eq[j][pos], -1);
5849 isl_int_set(bmap->eq[j][0], value);
5850 bmap = isl_basic_map_simplify(bmap);
5851 return isl_basic_map_finalize(bmap);
5852 error:
5853 isl_basic_map_free(bmap);
5854 return NULL;
5857 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5858 enum isl_dim_type type, unsigned pos, int value)
5860 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5861 return isl_basic_map_free(bmap);
5862 return isl_basic_map_fix_pos_si(bmap,
5863 isl_basic_map_offset(bmap, type) + pos, value);
5866 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5867 enum isl_dim_type type, unsigned pos, isl_int value)
5869 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5870 return isl_basic_map_free(bmap);
5871 return isl_basic_map_fix_pos(bmap,
5872 isl_basic_map_offset(bmap, type) + pos, value);
5875 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5876 * to be equal to "v".
5878 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5879 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5881 if (!bmap || !v)
5882 goto error;
5883 if (!isl_val_is_int(v))
5884 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5885 "expecting integer value", goto error);
5886 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5887 goto error;
5888 pos += isl_basic_map_offset(bmap, type);
5889 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5890 isl_val_free(v);
5891 return bmap;
5892 error:
5893 isl_basic_map_free(bmap);
5894 isl_val_free(v);
5895 return NULL;
5898 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5899 * to be equal to "v".
5901 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5902 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5904 return isl_basic_map_fix_val(bset, type, pos, v);
5907 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5908 enum isl_dim_type type, unsigned pos, int value)
5910 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5911 type, pos, value));
5914 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5915 enum isl_dim_type type, unsigned pos, isl_int value)
5917 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5918 type, pos, value));
5921 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5922 unsigned input, int value)
5924 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5927 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5928 unsigned dim, int value)
5930 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5931 isl_dim_set, dim, value));
5934 static int remove_if_empty(__isl_keep isl_map *map, int i)
5936 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5938 if (empty < 0)
5939 return -1;
5940 if (!empty)
5941 return 0;
5943 isl_basic_map_free(map->p[i]);
5944 if (i != map->n - 1) {
5945 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5946 map->p[i] = map->p[map->n - 1];
5948 map->n--;
5950 return 0;
5953 /* Perform "fn" on each basic map of "map", where we may not be holding
5954 * the only reference to "map".
5955 * In particular, "fn" should be a semantics preserving operation
5956 * that we want to apply to all copies of "map". We therefore need
5957 * to be careful not to modify "map" in a way that breaks "map"
5958 * in case anything goes wrong.
5960 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5961 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5963 struct isl_basic_map *bmap;
5964 int i;
5966 if (!map)
5967 return NULL;
5969 for (i = map->n - 1; i >= 0; --i) {
5970 bmap = isl_basic_map_copy(map->p[i]);
5971 bmap = fn(bmap);
5972 if (!bmap)
5973 goto error;
5974 isl_basic_map_free(map->p[i]);
5975 map->p[i] = bmap;
5976 if (remove_if_empty(map, i) < 0)
5977 goto error;
5980 return map;
5981 error:
5982 isl_map_free(map);
5983 return NULL;
5986 struct isl_map *isl_map_fix_si(struct isl_map *map,
5987 enum isl_dim_type type, unsigned pos, int value)
5989 int i;
5991 map = isl_map_cow(map);
5992 if (!map)
5993 return NULL;
5995 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5996 for (i = map->n - 1; i >= 0; --i) {
5997 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5998 if (remove_if_empty(map, i) < 0)
5999 goto error;
6001 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6002 return map;
6003 error:
6004 isl_map_free(map);
6005 return NULL;
6008 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6009 enum isl_dim_type type, unsigned pos, int value)
6011 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6014 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6015 enum isl_dim_type type, unsigned pos, isl_int value)
6017 int i;
6019 map = isl_map_cow(map);
6020 if (!map)
6021 return NULL;
6023 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6024 for (i = 0; i < map->n; ++i) {
6025 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6026 if (!map->p[i])
6027 goto error;
6029 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6030 return map;
6031 error:
6032 isl_map_free(map);
6033 return NULL;
6036 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6037 enum isl_dim_type type, unsigned pos, isl_int value)
6039 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6042 /* Fix the value of the variable at position "pos" of type "type" of "map"
6043 * to be equal to "v".
6045 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6046 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6048 int i;
6050 map = isl_map_cow(map);
6051 if (!map || !v)
6052 goto error;
6054 if (!isl_val_is_int(v))
6055 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6056 "expecting integer value", goto error);
6057 if (pos >= isl_map_dim(map, type))
6058 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6059 "index out of bounds", goto error);
6060 for (i = map->n - 1; i >= 0; --i) {
6061 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6062 isl_val_copy(v));
6063 if (remove_if_empty(map, i) < 0)
6064 goto error;
6066 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6067 isl_val_free(v);
6068 return map;
6069 error:
6070 isl_map_free(map);
6071 isl_val_free(v);
6072 return NULL;
6075 /* Fix the value of the variable at position "pos" of type "type" of "set"
6076 * to be equal to "v".
6078 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6079 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6081 return isl_map_fix_val(set, type, pos, v);
6084 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6085 unsigned input, int value)
6087 return isl_map_fix_si(map, isl_dim_in, input, value);
6090 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6092 return set_from_map(isl_map_fix_si(set_to_map(set),
6093 isl_dim_set, dim, value));
6096 static __isl_give isl_basic_map *basic_map_bound_si(
6097 __isl_take isl_basic_map *bmap,
6098 enum isl_dim_type type, unsigned pos, int value, int upper)
6100 int j;
6102 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6103 return isl_basic_map_free(bmap);
6104 pos += isl_basic_map_offset(bmap, type);
6105 bmap = isl_basic_map_cow(bmap);
6106 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6107 j = isl_basic_map_alloc_inequality(bmap);
6108 if (j < 0)
6109 goto error;
6110 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6111 if (upper) {
6112 isl_int_set_si(bmap->ineq[j][pos], -1);
6113 isl_int_set_si(bmap->ineq[j][0], value);
6114 } else {
6115 isl_int_set_si(bmap->ineq[j][pos], 1);
6116 isl_int_set_si(bmap->ineq[j][0], -value);
6118 bmap = isl_basic_map_simplify(bmap);
6119 return isl_basic_map_finalize(bmap);
6120 error:
6121 isl_basic_map_free(bmap);
6122 return NULL;
6125 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6126 __isl_take isl_basic_map *bmap,
6127 enum isl_dim_type type, unsigned pos, int value)
6129 return basic_map_bound_si(bmap, type, pos, value, 0);
6132 /* Constrain the values of the given dimension to be no greater than "value".
6134 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6135 __isl_take isl_basic_map *bmap,
6136 enum isl_dim_type type, unsigned pos, int value)
6138 return basic_map_bound_si(bmap, type, pos, value, 1);
6141 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
6142 unsigned dim, isl_int value)
6144 int j;
6146 bset = isl_basic_set_cow(bset);
6147 bset = isl_basic_set_extend_constraints(bset, 0, 1);
6148 j = isl_basic_set_alloc_inequality(bset);
6149 if (j < 0)
6150 goto error;
6151 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
6152 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
6153 isl_int_neg(bset->ineq[j][0], value);
6154 bset = isl_basic_set_simplify(bset);
6155 return isl_basic_set_finalize(bset);
6156 error:
6157 isl_basic_set_free(bset);
6158 return NULL;
6161 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6162 enum isl_dim_type type, unsigned pos, int value, int upper)
6164 int i;
6166 map = isl_map_cow(map);
6167 if (!map)
6168 return NULL;
6170 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6171 for (i = 0; i < map->n; ++i) {
6172 map->p[i] = basic_map_bound_si(map->p[i],
6173 type, pos, value, upper);
6174 if (!map->p[i])
6175 goto error;
6177 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6178 return map;
6179 error:
6180 isl_map_free(map);
6181 return NULL;
6184 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6185 enum isl_dim_type type, unsigned pos, int value)
6187 return map_bound_si(map, type, pos, value, 0);
6190 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6191 enum isl_dim_type type, unsigned pos, int value)
6193 return map_bound_si(map, type, pos, value, 1);
6196 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6197 enum isl_dim_type type, unsigned pos, int value)
6199 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6200 type, pos, value));
6203 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6204 enum isl_dim_type type, unsigned pos, int value)
6206 return isl_map_upper_bound_si(set, type, pos, value);
6209 /* Bound the given variable of "bmap" from below (or above is "upper"
6210 * is set) to "value".
6212 static __isl_give isl_basic_map *basic_map_bound(
6213 __isl_take isl_basic_map *bmap,
6214 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6216 int j;
6218 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6219 return isl_basic_map_free(bmap);
6220 pos += isl_basic_map_offset(bmap, type);
6221 bmap = isl_basic_map_cow(bmap);
6222 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6223 j = isl_basic_map_alloc_inequality(bmap);
6224 if (j < 0)
6225 goto error;
6226 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6227 if (upper) {
6228 isl_int_set_si(bmap->ineq[j][pos], -1);
6229 isl_int_set(bmap->ineq[j][0], value);
6230 } else {
6231 isl_int_set_si(bmap->ineq[j][pos], 1);
6232 isl_int_neg(bmap->ineq[j][0], value);
6234 bmap = isl_basic_map_simplify(bmap);
6235 return isl_basic_map_finalize(bmap);
6236 error:
6237 isl_basic_map_free(bmap);
6238 return NULL;
6241 /* Bound the given variable of "map" from below (or above is "upper"
6242 * is set) to "value".
6244 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6245 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6247 int i;
6249 map = isl_map_cow(map);
6250 if (!map)
6251 return NULL;
6253 if (pos >= isl_map_dim(map, type))
6254 isl_die(map->ctx, isl_error_invalid,
6255 "index out of bounds", goto error);
6256 for (i = map->n - 1; i >= 0; --i) {
6257 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6258 if (remove_if_empty(map, i) < 0)
6259 goto error;
6261 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6262 return map;
6263 error:
6264 isl_map_free(map);
6265 return NULL;
6268 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6269 enum isl_dim_type type, unsigned pos, isl_int value)
6271 return map_bound(map, type, pos, value, 0);
6274 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6275 enum isl_dim_type type, unsigned pos, isl_int value)
6277 return map_bound(map, type, pos, value, 1);
6280 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6281 enum isl_dim_type type, unsigned pos, isl_int value)
6283 return isl_map_lower_bound(set, type, pos, value);
6286 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6287 enum isl_dim_type type, unsigned pos, isl_int value)
6289 return isl_map_upper_bound(set, type, pos, value);
6292 /* Force the values of the variable at position "pos" of type "type" of "set"
6293 * to be no smaller than "value".
6295 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6296 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6298 if (!value)
6299 goto error;
6300 if (!isl_val_is_int(value))
6301 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6302 "expecting integer value", goto error);
6303 set = isl_set_lower_bound(set, type, pos, value->n);
6304 isl_val_free(value);
6305 return set;
6306 error:
6307 isl_val_free(value);
6308 isl_set_free(set);
6309 return NULL;
6312 /* Force the values of the variable at position "pos" of type "type" of "set"
6313 * to be no greater than "value".
6315 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6316 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6318 if (!value)
6319 goto error;
6320 if (!isl_val_is_int(value))
6321 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6322 "expecting integer value", goto error);
6323 set = isl_set_upper_bound(set, type, pos, value->n);
6324 isl_val_free(value);
6325 return set;
6326 error:
6327 isl_val_free(value);
6328 isl_set_free(set);
6329 return NULL;
6332 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6333 isl_int value)
6335 int i;
6337 set = isl_set_cow(set);
6338 if (!set)
6339 return NULL;
6341 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6342 for (i = 0; i < set->n; ++i) {
6343 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6344 if (!set->p[i])
6345 goto error;
6347 return set;
6348 error:
6349 isl_set_free(set);
6350 return NULL;
6353 struct isl_map *isl_map_reverse(struct isl_map *map)
6355 int i;
6357 map = isl_map_cow(map);
6358 if (!map)
6359 return NULL;
6361 map->dim = isl_space_reverse(map->dim);
6362 if (!map->dim)
6363 goto error;
6364 for (i = 0; i < map->n; ++i) {
6365 map->p[i] = isl_basic_map_reverse(map->p[i]);
6366 if (!map->p[i])
6367 goto error;
6369 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6370 return map;
6371 error:
6372 isl_map_free(map);
6373 return NULL;
6376 #undef TYPE
6377 #define TYPE isl_pw_multi_aff
6378 #undef SUFFIX
6379 #define SUFFIX _pw_multi_aff
6380 #undef EMPTY
6381 #define EMPTY isl_pw_multi_aff_empty
6382 #undef ADD
6383 #define ADD isl_pw_multi_aff_union_add
6384 #include "isl_map_lexopt_templ.c"
6386 /* Given a map "map", compute the lexicographically minimal
6387 * (or maximal) image element for each domain element in dom,
6388 * in the form of an isl_pw_multi_aff.
6389 * If "empty" is not NULL, then set *empty to those elements in dom that
6390 * do not have an image element.
6391 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6392 * should be computed over the domain of "map". "empty" is also NULL
6393 * in this case.
6395 * We first compute the lexicographically minimal or maximal element
6396 * in the first basic map. This results in a partial solution "res"
6397 * and a subset "todo" of dom that still need to be handled.
6398 * We then consider each of the remaining maps in "map" and successively
6399 * update both "res" and "todo".
6400 * If "empty" is NULL, then the todo sets are not needed and therefore
6401 * also not computed.
6403 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6404 __isl_take isl_map *map, __isl_take isl_set *dom,
6405 __isl_give isl_set **empty, unsigned flags)
6407 int i;
6408 int full;
6409 isl_pw_multi_aff *res;
6410 isl_set *todo;
6412 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6413 if (!map || (!full && !dom))
6414 goto error;
6416 if (isl_map_plain_is_empty(map)) {
6417 if (empty)
6418 *empty = dom;
6419 else
6420 isl_set_free(dom);
6421 return isl_pw_multi_aff_from_map(map);
6424 res = basic_map_partial_lexopt_pw_multi_aff(
6425 isl_basic_map_copy(map->p[0]),
6426 isl_set_copy(dom), empty, flags);
6428 if (empty)
6429 todo = *empty;
6430 for (i = 1; i < map->n; ++i) {
6431 isl_pw_multi_aff *res_i;
6433 res_i = basic_map_partial_lexopt_pw_multi_aff(
6434 isl_basic_map_copy(map->p[i]),
6435 isl_set_copy(dom), empty, flags);
6437 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6438 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6439 else
6440 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6442 if (empty)
6443 todo = isl_set_intersect(todo, *empty);
6446 isl_set_free(dom);
6447 isl_map_free(map);
6449 if (empty)
6450 *empty = todo;
6452 return res;
6453 error:
6454 if (empty)
6455 *empty = NULL;
6456 isl_set_free(dom);
6457 isl_map_free(map);
6458 return NULL;
6461 #undef TYPE
6462 #define TYPE isl_map
6463 #undef SUFFIX
6464 #define SUFFIX
6465 #undef EMPTY
6466 #define EMPTY isl_map_empty
6467 #undef ADD
6468 #define ADD isl_map_union_disjoint
6469 #include "isl_map_lexopt_templ.c"
6471 /* Given a map "map", compute the lexicographically minimal
6472 * (or maximal) image element for each domain element in "dom",
6473 * in the form of an isl_map.
6474 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6475 * do not have an image element.
6476 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6477 * should be computed over the domain of "map". "empty" is also NULL
6478 * in this case.
6480 * If the input consists of more than one disjunct, then first
6481 * compute the desired result in the form of an isl_pw_multi_aff and
6482 * then convert that into an isl_map.
6484 * This function used to have an explicit implementation in terms
6485 * of isl_maps, but it would continually intersect the domains of
6486 * partial results with the complement of the domain of the next
6487 * partial solution, potentially leading to an explosion in the number
6488 * of disjuncts if there are several disjuncts in the input.
6489 * An even earlier implementation of this function would look for
6490 * better results in the domain of the partial result and for extra
6491 * results in the complement of this domain, which would lead to
6492 * even more splintering.
6494 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6495 __isl_take isl_map *map, __isl_take isl_set *dom,
6496 __isl_give isl_set **empty, unsigned flags)
6498 int full;
6499 struct isl_map *res;
6500 isl_pw_multi_aff *pma;
6502 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6503 if (!map || (!full && !dom))
6504 goto error;
6506 if (isl_map_plain_is_empty(map)) {
6507 if (empty)
6508 *empty = dom;
6509 else
6510 isl_set_free(dom);
6511 return map;
6514 if (map->n == 1) {
6515 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6516 dom, empty, flags);
6517 isl_map_free(map);
6518 return res;
6521 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6522 flags);
6523 return isl_map_from_pw_multi_aff(pma);
6524 error:
6525 if (empty)
6526 *empty = NULL;
6527 isl_set_free(dom);
6528 isl_map_free(map);
6529 return NULL;
6532 __isl_give isl_map *isl_map_partial_lexmax(
6533 __isl_take isl_map *map, __isl_take isl_set *dom,
6534 __isl_give isl_set **empty)
6536 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6539 __isl_give isl_map *isl_map_partial_lexmin(
6540 __isl_take isl_map *map, __isl_take isl_set *dom,
6541 __isl_give isl_set **empty)
6543 return isl_map_partial_lexopt(map, dom, empty, 0);
6546 __isl_give isl_set *isl_set_partial_lexmin(
6547 __isl_take isl_set *set, __isl_take isl_set *dom,
6548 __isl_give isl_set **empty)
6550 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6551 dom, empty));
6554 __isl_give isl_set *isl_set_partial_lexmax(
6555 __isl_take isl_set *set, __isl_take isl_set *dom,
6556 __isl_give isl_set **empty)
6558 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6559 dom, empty));
6562 /* Compute the lexicographic minimum (or maximum if "flags" includes
6563 * ISL_OPT_MAX) of "bset" over its parametric domain.
6565 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6566 unsigned flags)
6568 return isl_basic_map_lexopt(bset, flags);
6571 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6573 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6576 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6578 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6581 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6583 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6586 /* Compute the lexicographic minimum of "bset" over its parametric domain
6587 * for the purpose of quantifier elimination.
6588 * That is, find an explicit representation for all the existentially
6589 * quantified variables in "bset" by computing their lexicographic
6590 * minimum.
6592 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6593 __isl_take isl_basic_set *bset)
6595 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6598 /* Extract the first and only affine expression from list
6599 * and then add it to *pwaff with the given dom.
6600 * This domain is known to be disjoint from other domains
6601 * because of the way isl_basic_map_foreach_lexmax works.
6603 static int update_dim_opt(__isl_take isl_basic_set *dom,
6604 __isl_take isl_aff_list *list, void *user)
6606 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6607 isl_aff *aff;
6608 isl_pw_aff **pwaff = user;
6609 isl_pw_aff *pwaff_i;
6611 if (!list)
6612 goto error;
6613 if (isl_aff_list_n_aff(list) != 1)
6614 isl_die(ctx, isl_error_internal,
6615 "expecting single element list", goto error);
6617 aff = isl_aff_list_get_aff(list, 0);
6618 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6620 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6622 isl_aff_list_free(list);
6624 return 0;
6625 error:
6626 isl_basic_set_free(dom);
6627 isl_aff_list_free(list);
6628 return -1;
6631 /* Given a basic map with one output dimension, compute the minimum or
6632 * maximum of that dimension as an isl_pw_aff.
6634 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6635 * call update_dim_opt on each leaf of the result.
6637 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6638 int max)
6640 isl_space *dim = isl_basic_map_get_space(bmap);
6641 isl_pw_aff *pwaff;
6642 int r;
6644 dim = isl_space_from_domain(isl_space_domain(dim));
6645 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6646 pwaff = isl_pw_aff_empty(dim);
6648 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6649 if (r < 0)
6650 return isl_pw_aff_free(pwaff);
6652 return pwaff;
6655 /* Compute the minimum or maximum of the given output dimension
6656 * as a function of the parameters and the input dimensions,
6657 * but independently of the other output dimensions.
6659 * We first project out the other output dimension and then compute
6660 * the "lexicographic" maximum in each basic map, combining the results
6661 * using isl_pw_aff_union_max.
6663 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6664 int max)
6666 int i;
6667 isl_pw_aff *pwaff;
6668 unsigned n_out;
6670 n_out = isl_map_dim(map, isl_dim_out);
6671 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6672 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6673 if (!map)
6674 return NULL;
6676 if (map->n == 0) {
6677 isl_space *dim = isl_map_get_space(map);
6678 isl_map_free(map);
6679 return isl_pw_aff_empty(dim);
6682 pwaff = basic_map_dim_opt(map->p[0], max);
6683 for (i = 1; i < map->n; ++i) {
6684 isl_pw_aff *pwaff_i;
6686 pwaff_i = basic_map_dim_opt(map->p[i], max);
6687 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6690 isl_map_free(map);
6692 return pwaff;
6695 /* Compute the minimum of the given output dimension as a function of the
6696 * parameters and input dimensions, but independently of
6697 * the other output dimensions.
6699 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6701 return map_dim_opt(map, pos, 0);
6704 /* Compute the maximum of the given output dimension as a function of the
6705 * parameters and input dimensions, but independently of
6706 * the other output dimensions.
6708 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6710 return map_dim_opt(map, pos, 1);
6713 /* Compute the minimum or maximum of the given set dimension
6714 * as a function of the parameters,
6715 * but independently of the other set dimensions.
6717 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6718 int max)
6720 return map_dim_opt(set, pos, max);
6723 /* Compute the maximum of the given set dimension as a function of the
6724 * parameters, but independently of the other set dimensions.
6726 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6728 return set_dim_opt(set, pos, 1);
6731 /* Compute the minimum of the given set dimension as a function of the
6732 * parameters, but independently of the other set dimensions.
6734 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6736 return set_dim_opt(set, pos, 0);
6739 /* Apply a preimage specified by "mat" on the parameters of "bset".
6740 * bset is assumed to have only parameters and divs.
6742 static struct isl_basic_set *basic_set_parameter_preimage(
6743 struct isl_basic_set *bset, struct isl_mat *mat)
6745 unsigned nparam;
6747 if (!bset || !mat)
6748 goto error;
6750 bset->dim = isl_space_cow(bset->dim);
6751 if (!bset->dim)
6752 goto error;
6754 nparam = isl_basic_set_dim(bset, isl_dim_param);
6756 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6758 bset->dim->nparam = 0;
6759 bset->dim->n_out = nparam;
6760 bset = isl_basic_set_preimage(bset, mat);
6761 if (bset) {
6762 bset->dim->nparam = bset->dim->n_out;
6763 bset->dim->n_out = 0;
6765 return bset;
6766 error:
6767 isl_mat_free(mat);
6768 isl_basic_set_free(bset);
6769 return NULL;
6772 /* Apply a preimage specified by "mat" on the parameters of "set".
6773 * set is assumed to have only parameters and divs.
6775 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6776 __isl_take isl_mat *mat)
6778 isl_space *space;
6779 unsigned nparam;
6781 if (!set || !mat)
6782 goto error;
6784 nparam = isl_set_dim(set, isl_dim_param);
6786 if (mat->n_row != 1 + nparam)
6787 isl_die(isl_set_get_ctx(set), isl_error_internal,
6788 "unexpected number of rows", goto error);
6790 space = isl_set_get_space(set);
6791 space = isl_space_move_dims(space, isl_dim_set, 0,
6792 isl_dim_param, 0, nparam);
6793 set = isl_set_reset_space(set, space);
6794 set = isl_set_preimage(set, mat);
6795 nparam = isl_set_dim(set, isl_dim_out);
6796 space = isl_set_get_space(set);
6797 space = isl_space_move_dims(space, isl_dim_param, 0,
6798 isl_dim_out, 0, nparam);
6799 set = isl_set_reset_space(set, space);
6800 return set;
6801 error:
6802 isl_mat_free(mat);
6803 isl_set_free(set);
6804 return NULL;
6807 /* Intersect the basic set "bset" with the affine space specified by the
6808 * equalities in "eq".
6810 static struct isl_basic_set *basic_set_append_equalities(
6811 struct isl_basic_set *bset, struct isl_mat *eq)
6813 int i, k;
6814 unsigned len;
6816 if (!bset || !eq)
6817 goto error;
6819 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6820 eq->n_row, 0);
6821 if (!bset)
6822 goto error;
6824 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6825 for (i = 0; i < eq->n_row; ++i) {
6826 k = isl_basic_set_alloc_equality(bset);
6827 if (k < 0)
6828 goto error;
6829 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6830 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6832 isl_mat_free(eq);
6834 bset = isl_basic_set_gauss(bset, NULL);
6835 bset = isl_basic_set_finalize(bset);
6837 return bset;
6838 error:
6839 isl_mat_free(eq);
6840 isl_basic_set_free(bset);
6841 return NULL;
6844 /* Intersect the set "set" with the affine space specified by the
6845 * equalities in "eq".
6847 static struct isl_set *set_append_equalities(struct isl_set *set,
6848 struct isl_mat *eq)
6850 int i;
6852 if (!set || !eq)
6853 goto error;
6855 for (i = 0; i < set->n; ++i) {
6856 set->p[i] = basic_set_append_equalities(set->p[i],
6857 isl_mat_copy(eq));
6858 if (!set->p[i])
6859 goto error;
6861 isl_mat_free(eq);
6862 return set;
6863 error:
6864 isl_mat_free(eq);
6865 isl_set_free(set);
6866 return NULL;
6869 /* Given a basic set "bset" that only involves parameters and existentially
6870 * quantified variables, return the index of the first equality
6871 * that only involves parameters. If there is no such equality then
6872 * return bset->n_eq.
6874 * This function assumes that isl_basic_set_gauss has been called on "bset".
6876 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6878 int i, j;
6879 unsigned nparam, n_div;
6881 if (!bset)
6882 return -1;
6884 nparam = isl_basic_set_dim(bset, isl_dim_param);
6885 n_div = isl_basic_set_dim(bset, isl_dim_div);
6887 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6888 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6889 ++i;
6892 return i;
6895 /* Compute an explicit representation for the existentially quantified
6896 * variables in "bset" by computing the "minimal value" of the set
6897 * variables. Since there are no set variables, the computation of
6898 * the minimal value essentially computes an explicit representation
6899 * of the non-empty part(s) of "bset".
6901 * The input only involves parameters and existentially quantified variables.
6902 * All equalities among parameters have been removed.
6904 * Since the existentially quantified variables in the result are in general
6905 * going to be different from those in the input, we first replace
6906 * them by the minimal number of variables based on their equalities.
6907 * This should simplify the parametric integer programming.
6909 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6911 isl_morph *morph1, *morph2;
6912 isl_set *set;
6913 unsigned n;
6915 if (!bset)
6916 return NULL;
6917 if (bset->n_eq == 0)
6918 return isl_basic_set_lexmin_compute_divs(bset);
6920 morph1 = isl_basic_set_parameter_compression(bset);
6921 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6922 bset = isl_basic_set_lift(bset);
6923 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6924 bset = isl_morph_basic_set(morph2, bset);
6925 n = isl_basic_set_dim(bset, isl_dim_set);
6926 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6928 set = isl_basic_set_lexmin_compute_divs(bset);
6930 set = isl_morph_set(isl_morph_inverse(morph1), set);
6932 return set;
6935 /* Project the given basic set onto its parameter domain, possibly introducing
6936 * new, explicit, existential variables in the constraints.
6937 * The input has parameters and (possibly implicit) existential variables.
6938 * The output has the same parameters, but only
6939 * explicit existentially quantified variables.
6941 * The actual projection is performed by pip, but pip doesn't seem
6942 * to like equalities very much, so we first remove the equalities
6943 * among the parameters by performing a variable compression on
6944 * the parameters. Afterward, an inverse transformation is performed
6945 * and the equalities among the parameters are inserted back in.
6947 * The variable compression on the parameters may uncover additional
6948 * equalities that were only implicit before. We therefore check
6949 * if there are any new parameter equalities in the result and
6950 * if so recurse. The removal of parameter equalities is required
6951 * for the parameter compression performed by base_compute_divs.
6953 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6955 int i;
6956 struct isl_mat *eq;
6957 struct isl_mat *T, *T2;
6958 struct isl_set *set;
6959 unsigned nparam;
6961 bset = isl_basic_set_cow(bset);
6962 if (!bset)
6963 return NULL;
6965 if (bset->n_eq == 0)
6966 return base_compute_divs(bset);
6968 bset = isl_basic_set_gauss(bset, NULL);
6969 if (!bset)
6970 return NULL;
6971 if (isl_basic_set_plain_is_empty(bset))
6972 return isl_set_from_basic_set(bset);
6974 i = first_parameter_equality(bset);
6975 if (i == bset->n_eq)
6976 return base_compute_divs(bset);
6978 nparam = isl_basic_set_dim(bset, isl_dim_param);
6979 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6980 0, 1 + nparam);
6981 eq = isl_mat_cow(eq);
6982 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6983 if (T && T->n_col == 0) {
6984 isl_mat_free(T);
6985 isl_mat_free(T2);
6986 isl_mat_free(eq);
6987 bset = isl_basic_set_set_to_empty(bset);
6988 return isl_set_from_basic_set(bset);
6990 bset = basic_set_parameter_preimage(bset, T);
6992 i = first_parameter_equality(bset);
6993 if (!bset)
6994 set = NULL;
6995 else if (i == bset->n_eq)
6996 set = base_compute_divs(bset);
6997 else
6998 set = parameter_compute_divs(bset);
6999 set = set_parameter_preimage(set, T2);
7000 set = set_append_equalities(set, eq);
7001 return set;
7004 /* Insert the divs from "ls" before those of "bmap".
7006 * The number of columns is not changed, which means that the last
7007 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7008 * The caller is responsible for removing the same number of dimensions
7009 * from the space of "bmap".
7011 static __isl_give isl_basic_map *insert_divs_from_local_space(
7012 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7014 int i;
7015 int n_div;
7016 int old_n_div;
7018 n_div = isl_local_space_dim(ls, isl_dim_div);
7019 if (n_div == 0)
7020 return bmap;
7022 old_n_div = bmap->n_div;
7023 bmap = insert_div_rows(bmap, n_div);
7024 if (!bmap)
7025 return NULL;
7027 for (i = 0; i < n_div; ++i) {
7028 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7029 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7032 return bmap;
7035 /* Replace the space of "bmap" by the space and divs of "ls".
7037 * If "ls" has any divs, then we simplify the result since we may
7038 * have discovered some additional equalities that could simplify
7039 * the div expressions.
7041 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7042 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7044 int n_div;
7046 bmap = isl_basic_map_cow(bmap);
7047 if (!bmap || !ls)
7048 goto error;
7050 n_div = isl_local_space_dim(ls, isl_dim_div);
7051 bmap = insert_divs_from_local_space(bmap, ls);
7052 if (!bmap)
7053 goto error;
7055 isl_space_free(bmap->dim);
7056 bmap->dim = isl_local_space_get_space(ls);
7057 if (!bmap->dim)
7058 goto error;
7060 isl_local_space_free(ls);
7061 if (n_div > 0)
7062 bmap = isl_basic_map_simplify(bmap);
7063 bmap = isl_basic_map_finalize(bmap);
7064 return bmap;
7065 error:
7066 isl_basic_map_free(bmap);
7067 isl_local_space_free(ls);
7068 return NULL;
7071 /* Replace the space of "map" by the space and divs of "ls".
7073 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7074 __isl_take isl_local_space *ls)
7076 int i;
7078 map = isl_map_cow(map);
7079 if (!map || !ls)
7080 goto error;
7082 for (i = 0; i < map->n; ++i) {
7083 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7084 isl_local_space_copy(ls));
7085 if (!map->p[i])
7086 goto error;
7088 isl_space_free(map->dim);
7089 map->dim = isl_local_space_get_space(ls);
7090 if (!map->dim)
7091 goto error;
7093 isl_local_space_free(ls);
7094 return map;
7095 error:
7096 isl_local_space_free(ls);
7097 isl_map_free(map);
7098 return NULL;
7101 /* Compute an explicit representation for the existentially
7102 * quantified variables for which do not know any explicit representation yet.
7104 * We first sort the existentially quantified variables so that the
7105 * existentially quantified variables for which we already have an explicit
7106 * representation are placed before those for which we do not.
7107 * The input dimensions, the output dimensions and the existentially
7108 * quantified variables for which we already have an explicit
7109 * representation are then turned into parameters.
7110 * compute_divs returns a map with the same parameters and
7111 * no input or output dimensions and the dimension specification
7112 * is reset to that of the input, including the existentially quantified
7113 * variables for which we already had an explicit representation.
7115 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7117 struct isl_basic_set *bset;
7118 struct isl_set *set;
7119 struct isl_map *map;
7120 isl_space *dim;
7121 isl_local_space *ls;
7122 unsigned nparam;
7123 unsigned n_in;
7124 unsigned n_out;
7125 int n_known;
7126 int i;
7128 bmap = isl_basic_map_sort_divs(bmap);
7129 bmap = isl_basic_map_cow(bmap);
7130 if (!bmap)
7131 return NULL;
7133 n_known = isl_basic_map_first_unknown_div(bmap);
7134 if (n_known < 0)
7135 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7137 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7138 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7139 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7140 dim = isl_space_set_alloc(bmap->ctx,
7141 nparam + n_in + n_out + n_known, 0);
7142 if (!dim)
7143 goto error;
7145 ls = isl_basic_map_get_local_space(bmap);
7146 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7147 n_known, bmap->n_div - n_known);
7148 if (n_known > 0) {
7149 for (i = n_known; i < bmap->n_div; ++i)
7150 swap_div(bmap, i - n_known, i);
7151 bmap->n_div -= n_known;
7152 bmap->extra -= n_known;
7154 bmap = isl_basic_map_reset_space(bmap, dim);
7155 bset = bset_from_bmap(bmap);
7157 set = parameter_compute_divs(bset);
7158 map = set_to_map(set);
7159 map = replace_space_by_local_space(map, ls);
7161 return map;
7162 error:
7163 isl_basic_map_free(bmap);
7164 return NULL;
7167 /* Remove the explicit representation of local variable "div",
7168 * if there is any.
7170 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7171 __isl_take isl_basic_map *bmap, int div)
7173 isl_bool unknown;
7175 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7176 if (unknown < 0)
7177 return isl_basic_map_free(bmap);
7178 if (unknown)
7179 return bmap;
7181 bmap = isl_basic_map_cow(bmap);
7182 if (!bmap)
7183 return NULL;
7184 isl_int_set_si(bmap->div[div][0], 0);
7185 return bmap;
7188 /* Is local variable "div" of "bmap" marked as not having an explicit
7189 * representation?
7190 * Note that even if "div" is not marked in this way and therefore
7191 * has an explicit representation, this representation may still
7192 * depend (indirectly) on other local variables that do not
7193 * have an explicit representation.
7195 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7196 int div)
7198 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7199 return isl_bool_error;
7200 return isl_int_is_zero(bmap->div[div][0]);
7203 /* Return the position of the first local variable that does not
7204 * have an explicit representation.
7205 * Return the total number of local variables if they all have
7206 * an explicit representation.
7207 * Return -1 on error.
7209 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7211 int i;
7213 if (!bmap)
7214 return -1;
7216 for (i = 0; i < bmap->n_div; ++i) {
7217 if (!isl_basic_map_div_is_known(bmap, i))
7218 return i;
7220 return bmap->n_div;
7223 /* Return the position of the first local variable that does not
7224 * have an explicit representation.
7225 * Return the total number of local variables if they all have
7226 * an explicit representation.
7227 * Return -1 on error.
7229 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7231 return isl_basic_map_first_unknown_div(bset);
7234 /* Does "bmap" have an explicit representation for all local variables?
7236 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7238 int first, n;
7240 n = isl_basic_map_dim(bmap, isl_dim_div);
7241 first = isl_basic_map_first_unknown_div(bmap);
7242 if (first < 0)
7243 return isl_bool_error;
7244 return first == n;
7247 /* Do all basic maps in "map" have an explicit representation
7248 * for all local variables?
7250 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7252 int i;
7254 if (!map)
7255 return isl_bool_error;
7257 for (i = 0; i < map->n; ++i) {
7258 int known = isl_basic_map_divs_known(map->p[i]);
7259 if (known <= 0)
7260 return known;
7263 return isl_bool_true;
7266 /* If bmap contains any unknown divs, then compute explicit
7267 * expressions for them. However, this computation may be
7268 * quite expensive, so first try to remove divs that aren't
7269 * strictly needed.
7271 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7273 int known;
7274 struct isl_map *map;
7276 known = isl_basic_map_divs_known(bmap);
7277 if (known < 0)
7278 goto error;
7279 if (known)
7280 return isl_map_from_basic_map(bmap);
7282 bmap = isl_basic_map_drop_redundant_divs(bmap);
7284 known = isl_basic_map_divs_known(bmap);
7285 if (known < 0)
7286 goto error;
7287 if (known)
7288 return isl_map_from_basic_map(bmap);
7290 map = compute_divs(bmap);
7291 return map;
7292 error:
7293 isl_basic_map_free(bmap);
7294 return NULL;
7297 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7299 int i;
7300 int known;
7301 struct isl_map *res;
7303 if (!map)
7304 return NULL;
7305 if (map->n == 0)
7306 return map;
7308 known = isl_map_divs_known(map);
7309 if (known < 0) {
7310 isl_map_free(map);
7311 return NULL;
7313 if (known)
7314 return map;
7316 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7317 for (i = 1 ; i < map->n; ++i) {
7318 struct isl_map *r2;
7319 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7320 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7321 res = isl_map_union_disjoint(res, r2);
7322 else
7323 res = isl_map_union(res, r2);
7325 isl_map_free(map);
7327 return res;
7330 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7332 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7335 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7337 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7340 struct isl_set *isl_map_domain(struct isl_map *map)
7342 int i;
7343 struct isl_set *set;
7345 if (!map)
7346 goto error;
7348 map = isl_map_cow(map);
7349 if (!map)
7350 return NULL;
7352 set = set_from_map(map);
7353 set->dim = isl_space_domain(set->dim);
7354 if (!set->dim)
7355 goto error;
7356 for (i = 0; i < map->n; ++i) {
7357 set->p[i] = isl_basic_map_domain(map->p[i]);
7358 if (!set->p[i])
7359 goto error;
7361 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7362 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7363 return set;
7364 error:
7365 isl_map_free(map);
7366 return NULL;
7369 /* Return the union of "map1" and "map2", where we assume for now that
7370 * "map1" and "map2" are disjoint. Note that the basic maps inside
7371 * "map1" or "map2" may not be disjoint from each other.
7372 * Also note that this function is also called from isl_map_union,
7373 * which takes care of handling the situation where "map1" and "map2"
7374 * may not be disjoint.
7376 * If one of the inputs is empty, we can simply return the other input.
7377 * Similarly, if one of the inputs is universal, then it is equal to the union.
7379 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7380 __isl_take isl_map *map2)
7382 int i;
7383 unsigned flags = 0;
7384 struct isl_map *map = NULL;
7385 int is_universe;
7387 if (!map1 || !map2)
7388 goto error;
7390 if (!isl_space_is_equal(map1->dim, map2->dim))
7391 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7392 "spaces don't match", goto error);
7394 if (map1->n == 0) {
7395 isl_map_free(map1);
7396 return map2;
7398 if (map2->n == 0) {
7399 isl_map_free(map2);
7400 return map1;
7403 is_universe = isl_map_plain_is_universe(map1);
7404 if (is_universe < 0)
7405 goto error;
7406 if (is_universe) {
7407 isl_map_free(map2);
7408 return map1;
7411 is_universe = isl_map_plain_is_universe(map2);
7412 if (is_universe < 0)
7413 goto error;
7414 if (is_universe) {
7415 isl_map_free(map1);
7416 return map2;
7419 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7420 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7421 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7423 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7424 map1->n + map2->n, flags);
7425 if (!map)
7426 goto error;
7427 for (i = 0; i < map1->n; ++i) {
7428 map = isl_map_add_basic_map(map,
7429 isl_basic_map_copy(map1->p[i]));
7430 if (!map)
7431 goto error;
7433 for (i = 0; i < map2->n; ++i) {
7434 map = isl_map_add_basic_map(map,
7435 isl_basic_map_copy(map2->p[i]));
7436 if (!map)
7437 goto error;
7439 isl_map_free(map1);
7440 isl_map_free(map2);
7441 return map;
7442 error:
7443 isl_map_free(map);
7444 isl_map_free(map1);
7445 isl_map_free(map2);
7446 return NULL;
7449 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7450 * guaranteed to be disjoint by the caller.
7452 * Note that this functions is called from within isl_map_make_disjoint,
7453 * so we have to be careful not to touch the constraints of the inputs
7454 * in any way.
7456 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7457 __isl_take isl_map *map2)
7459 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7462 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7463 * not be disjoint. The parameters are assumed to have been aligned.
7465 * We currently simply call map_union_disjoint, the internal operation
7466 * of which does not really depend on the inputs being disjoint.
7467 * If the result contains more than one basic map, then we clear
7468 * the disjoint flag since the result may contain basic maps from
7469 * both inputs and these are not guaranteed to be disjoint.
7471 * As a special case, if "map1" and "map2" are obviously equal,
7472 * then we simply return "map1".
7474 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7475 __isl_take isl_map *map2)
7477 int equal;
7479 if (!map1 || !map2)
7480 goto error;
7482 equal = isl_map_plain_is_equal(map1, map2);
7483 if (equal < 0)
7484 goto error;
7485 if (equal) {
7486 isl_map_free(map2);
7487 return map1;
7490 map1 = map_union_disjoint(map1, map2);
7491 if (!map1)
7492 return NULL;
7493 if (map1->n > 1)
7494 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7495 return map1;
7496 error:
7497 isl_map_free(map1);
7498 isl_map_free(map2);
7499 return NULL;
7502 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7503 * not be disjoint.
7505 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7506 __isl_take isl_map *map2)
7508 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7511 struct isl_set *isl_set_union_disjoint(
7512 struct isl_set *set1, struct isl_set *set2)
7514 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7515 set_to_map(set2)));
7518 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7520 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7523 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7524 * the results.
7526 * "map" and "set" are assumed to be compatible and non-NULL.
7528 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7529 __isl_take isl_set *set,
7530 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7531 __isl_take isl_basic_set *bset))
7533 unsigned flags = 0;
7534 struct isl_map *result;
7535 int i, j;
7537 if (isl_set_plain_is_universe(set)) {
7538 isl_set_free(set);
7539 return map;
7542 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7543 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7544 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7546 result = isl_map_alloc_space(isl_space_copy(map->dim),
7547 map->n * set->n, flags);
7548 for (i = 0; result && i < map->n; ++i)
7549 for (j = 0; j < set->n; ++j) {
7550 result = isl_map_add_basic_map(result,
7551 fn(isl_basic_map_copy(map->p[i]),
7552 isl_basic_set_copy(set->p[j])));
7553 if (!result)
7554 break;
7557 isl_map_free(map);
7558 isl_set_free(set);
7559 return result;
7562 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7563 __isl_take isl_set *set)
7565 isl_bool ok;
7567 ok = isl_map_compatible_range(map, set);
7568 if (ok < 0)
7569 goto error;
7570 if (!ok)
7571 isl_die(set->ctx, isl_error_invalid,
7572 "incompatible spaces", goto error);
7574 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7575 error:
7576 isl_map_free(map);
7577 isl_set_free(set);
7578 return NULL;
7581 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7582 __isl_take isl_set *set)
7584 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7587 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7588 __isl_take isl_set *set)
7590 isl_bool ok;
7592 ok = isl_map_compatible_domain(map, set);
7593 if (ok < 0)
7594 goto error;
7595 if (!ok)
7596 isl_die(set->ctx, isl_error_invalid,
7597 "incompatible spaces", goto error);
7599 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7600 error:
7601 isl_map_free(map);
7602 isl_set_free(set);
7603 return NULL;
7606 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7607 __isl_take isl_set *set)
7609 return isl_map_align_params_map_map_and(map, set,
7610 &map_intersect_domain);
7613 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7614 __isl_take isl_map *map2)
7616 if (!map1 || !map2)
7617 goto error;
7618 map1 = isl_map_reverse(map1);
7619 map1 = isl_map_apply_range(map1, map2);
7620 return isl_map_reverse(map1);
7621 error:
7622 isl_map_free(map1);
7623 isl_map_free(map2);
7624 return NULL;
7627 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7628 __isl_take isl_map *map2)
7630 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7633 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7634 __isl_take isl_map *map2)
7636 isl_space *dim_result;
7637 struct isl_map *result;
7638 int i, j;
7640 if (!map1 || !map2)
7641 goto error;
7643 dim_result = isl_space_join(isl_space_copy(map1->dim),
7644 isl_space_copy(map2->dim));
7646 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7647 if (!result)
7648 goto error;
7649 for (i = 0; i < map1->n; ++i)
7650 for (j = 0; j < map2->n; ++j) {
7651 result = isl_map_add_basic_map(result,
7652 isl_basic_map_apply_range(
7653 isl_basic_map_copy(map1->p[i]),
7654 isl_basic_map_copy(map2->p[j])));
7655 if (!result)
7656 goto error;
7658 isl_map_free(map1);
7659 isl_map_free(map2);
7660 if (result && result->n <= 1)
7661 ISL_F_SET(result, ISL_MAP_DISJOINT);
7662 return result;
7663 error:
7664 isl_map_free(map1);
7665 isl_map_free(map2);
7666 return NULL;
7669 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7670 __isl_take isl_map *map2)
7672 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7676 * returns range - domain
7678 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7680 isl_space *target_space;
7681 struct isl_basic_set *bset;
7682 unsigned dim;
7683 unsigned nparam;
7684 int i;
7686 if (!bmap)
7687 goto error;
7688 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7689 bmap->dim, isl_dim_out),
7690 goto error);
7691 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7692 dim = isl_basic_map_n_in(bmap);
7693 nparam = isl_basic_map_n_param(bmap);
7694 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7695 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7696 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7697 for (i = 0; i < dim; ++i) {
7698 int j = isl_basic_map_alloc_equality(bmap);
7699 if (j < 0) {
7700 bmap = isl_basic_map_free(bmap);
7701 break;
7703 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7704 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7705 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7706 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7708 bset = isl_basic_map_domain(bmap);
7709 bset = isl_basic_set_reset_space(bset, target_space);
7710 return bset;
7711 error:
7712 isl_basic_map_free(bmap);
7713 return NULL;
7717 * returns range - domain
7719 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7721 int i;
7722 isl_space *dim;
7723 struct isl_set *result;
7725 if (!map)
7726 return NULL;
7728 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7729 map->dim, isl_dim_out),
7730 goto error);
7731 dim = isl_map_get_space(map);
7732 dim = isl_space_domain(dim);
7733 result = isl_set_alloc_space(dim, map->n, 0);
7734 if (!result)
7735 goto error;
7736 for (i = 0; i < map->n; ++i)
7737 result = isl_set_add_basic_set(result,
7738 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7739 isl_map_free(map);
7740 return result;
7741 error:
7742 isl_map_free(map);
7743 return NULL;
7747 * returns [domain -> range] -> range - domain
7749 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7750 __isl_take isl_basic_map *bmap)
7752 int i, k;
7753 isl_space *dim;
7754 isl_basic_map *domain;
7755 int nparam, n;
7756 unsigned total;
7758 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7759 bmap->dim, isl_dim_out))
7760 isl_die(bmap->ctx, isl_error_invalid,
7761 "domain and range don't match", goto error);
7763 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7764 n = isl_basic_map_dim(bmap, isl_dim_in);
7766 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7767 domain = isl_basic_map_universe(dim);
7769 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7770 bmap = isl_basic_map_apply_range(bmap, domain);
7771 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7773 total = isl_basic_map_total_dim(bmap);
7775 for (i = 0; i < n; ++i) {
7776 k = isl_basic_map_alloc_equality(bmap);
7777 if (k < 0)
7778 goto error;
7779 isl_seq_clr(bmap->eq[k], 1 + total);
7780 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7781 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7782 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7785 bmap = isl_basic_map_gauss(bmap, NULL);
7786 return isl_basic_map_finalize(bmap);
7787 error:
7788 isl_basic_map_free(bmap);
7789 return NULL;
7793 * returns [domain -> range] -> range - domain
7795 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7797 int i;
7798 isl_space *domain_dim;
7800 if (!map)
7801 return NULL;
7803 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7804 map->dim, isl_dim_out))
7805 isl_die(map->ctx, isl_error_invalid,
7806 "domain and range don't match", goto error);
7808 map = isl_map_cow(map);
7809 if (!map)
7810 return NULL;
7812 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7813 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7814 map->dim = isl_space_join(map->dim, domain_dim);
7815 if (!map->dim)
7816 goto error;
7817 for (i = 0; i < map->n; ++i) {
7818 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7819 if (!map->p[i])
7820 goto error;
7822 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7823 return map;
7824 error:
7825 isl_map_free(map);
7826 return NULL;
7829 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7831 struct isl_basic_map *bmap;
7832 unsigned nparam;
7833 unsigned dim;
7834 int i;
7836 if (!dims)
7837 return NULL;
7839 nparam = dims->nparam;
7840 dim = dims->n_out;
7841 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7842 if (!bmap)
7843 goto error;
7845 for (i = 0; i < dim; ++i) {
7846 int j = isl_basic_map_alloc_equality(bmap);
7847 if (j < 0)
7848 goto error;
7849 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7850 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7851 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7853 return isl_basic_map_finalize(bmap);
7854 error:
7855 isl_basic_map_free(bmap);
7856 return NULL;
7859 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7861 if (!dim)
7862 return NULL;
7863 if (dim->n_in != dim->n_out)
7864 isl_die(dim->ctx, isl_error_invalid,
7865 "number of input and output dimensions needs to be "
7866 "the same", goto error);
7867 return basic_map_identity(dim);
7868 error:
7869 isl_space_free(dim);
7870 return NULL;
7873 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7875 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7878 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7880 isl_space *dim = isl_set_get_space(set);
7881 isl_map *id;
7882 id = isl_map_identity(isl_space_map_from_set(dim));
7883 return isl_map_intersect_range(id, set);
7886 /* Construct a basic set with all set dimensions having only non-negative
7887 * values.
7889 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7890 __isl_take isl_space *space)
7892 int i;
7893 unsigned nparam;
7894 unsigned dim;
7895 struct isl_basic_set *bset;
7897 if (!space)
7898 return NULL;
7899 nparam = space->nparam;
7900 dim = space->n_out;
7901 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7902 if (!bset)
7903 return NULL;
7904 for (i = 0; i < dim; ++i) {
7905 int k = isl_basic_set_alloc_inequality(bset);
7906 if (k < 0)
7907 goto error;
7908 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7909 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7911 return bset;
7912 error:
7913 isl_basic_set_free(bset);
7914 return NULL;
7917 /* Construct the half-space x_pos >= 0.
7919 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7920 int pos)
7922 int k;
7923 isl_basic_set *nonneg;
7925 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7926 k = isl_basic_set_alloc_inequality(nonneg);
7927 if (k < 0)
7928 goto error;
7929 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7930 isl_int_set_si(nonneg->ineq[k][pos], 1);
7932 return isl_basic_set_finalize(nonneg);
7933 error:
7934 isl_basic_set_free(nonneg);
7935 return NULL;
7938 /* Construct the half-space x_pos <= -1.
7940 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7942 int k;
7943 isl_basic_set *neg;
7945 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7946 k = isl_basic_set_alloc_inequality(neg);
7947 if (k < 0)
7948 goto error;
7949 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7950 isl_int_set_si(neg->ineq[k][0], -1);
7951 isl_int_set_si(neg->ineq[k][pos], -1);
7953 return isl_basic_set_finalize(neg);
7954 error:
7955 isl_basic_set_free(neg);
7956 return NULL;
7959 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7960 enum isl_dim_type type, unsigned first, unsigned n)
7962 int i;
7963 unsigned offset;
7964 isl_basic_set *nonneg;
7965 isl_basic_set *neg;
7967 if (!set)
7968 return NULL;
7969 if (n == 0)
7970 return set;
7972 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7974 offset = pos(set->dim, type);
7975 for (i = 0; i < n; ++i) {
7976 nonneg = nonneg_halfspace(isl_set_get_space(set),
7977 offset + first + i);
7978 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7980 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7983 return set;
7984 error:
7985 isl_set_free(set);
7986 return NULL;
7989 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7990 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7991 void *user)
7993 isl_set *half;
7995 if (!set)
7996 return -1;
7997 if (isl_set_plain_is_empty(set)) {
7998 isl_set_free(set);
7999 return 0;
8001 if (first == len)
8002 return fn(set, signs, user);
8004 signs[first] = 1;
8005 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8006 1 + first));
8007 half = isl_set_intersect(half, isl_set_copy(set));
8008 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8009 goto error;
8011 signs[first] = -1;
8012 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8013 1 + first));
8014 half = isl_set_intersect(half, set);
8015 return foreach_orthant(half, signs, first + 1, len, fn, user);
8016 error:
8017 isl_set_free(set);
8018 return -1;
8021 /* Call "fn" on the intersections of "set" with each of the orthants
8022 * (except for obviously empty intersections). The orthant is identified
8023 * by the signs array, with each entry having value 1 or -1 according
8024 * to the sign of the corresponding variable.
8026 int isl_set_foreach_orthant(__isl_keep isl_set *set,
8027 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8028 void *user)
8030 unsigned nparam;
8031 unsigned nvar;
8032 int *signs;
8033 int r;
8035 if (!set)
8036 return -1;
8037 if (isl_set_plain_is_empty(set))
8038 return 0;
8040 nparam = isl_set_dim(set, isl_dim_param);
8041 nvar = isl_set_dim(set, isl_dim_set);
8043 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8045 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8046 fn, user);
8048 free(signs);
8050 return r;
8053 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8055 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8058 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8059 __isl_keep isl_basic_map *bmap2)
8061 int is_subset;
8062 struct isl_map *map1;
8063 struct isl_map *map2;
8065 if (!bmap1 || !bmap2)
8066 return isl_bool_error;
8068 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8069 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8071 is_subset = isl_map_is_subset(map1, map2);
8073 isl_map_free(map1);
8074 isl_map_free(map2);
8076 return is_subset;
8079 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8080 __isl_keep isl_basic_set *bset2)
8082 return isl_basic_map_is_subset(bset1, bset2);
8085 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8086 __isl_keep isl_basic_map *bmap2)
8088 isl_bool is_subset;
8090 if (!bmap1 || !bmap2)
8091 return isl_bool_error;
8092 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8093 if (is_subset != isl_bool_true)
8094 return is_subset;
8095 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8096 return is_subset;
8099 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8100 __isl_keep isl_basic_set *bset2)
8102 return isl_basic_map_is_equal(
8103 bset_to_bmap(bset1), bset_to_bmap(bset2));
8106 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8108 int i;
8109 int is_empty;
8111 if (!map)
8112 return isl_bool_error;
8113 for (i = 0; i < map->n; ++i) {
8114 is_empty = isl_basic_map_is_empty(map->p[i]);
8115 if (is_empty < 0)
8116 return isl_bool_error;
8117 if (!is_empty)
8118 return isl_bool_false;
8120 return isl_bool_true;
8123 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8125 return map ? map->n == 0 : isl_bool_error;
8128 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8130 return set ? set->n == 0 : isl_bool_error;
8133 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8135 return isl_map_is_empty(set_to_map(set));
8138 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8139 __isl_keep isl_map *map2)
8141 if (!map1 || !map2)
8142 return isl_bool_error;
8144 return isl_space_is_equal(map1->dim, map2->dim);
8147 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8148 __isl_keep isl_set *set2)
8150 if (!set1 || !set2)
8151 return isl_bool_error;
8153 return isl_space_is_equal(set1->dim, set2->dim);
8156 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8158 isl_bool is_subset;
8160 if (!map1 || !map2)
8161 return isl_bool_error;
8162 is_subset = isl_map_is_subset(map1, map2);
8163 if (is_subset != isl_bool_true)
8164 return is_subset;
8165 is_subset = isl_map_is_subset(map2, map1);
8166 return is_subset;
8169 /* Is "map1" equal to "map2"?
8171 * First check if they are obviously equal.
8172 * If not, then perform a more detailed analysis.
8174 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8176 isl_bool equal;
8178 equal = isl_map_plain_is_equal(map1, map2);
8179 if (equal < 0 || equal)
8180 return equal;
8181 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8184 isl_bool isl_basic_map_is_strict_subset(
8185 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8187 isl_bool is_subset;
8189 if (!bmap1 || !bmap2)
8190 return isl_bool_error;
8191 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8192 if (is_subset != isl_bool_true)
8193 return is_subset;
8194 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8195 if (is_subset == isl_bool_error)
8196 return is_subset;
8197 return !is_subset;
8200 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8201 __isl_keep isl_map *map2)
8203 isl_bool is_subset;
8205 if (!map1 || !map2)
8206 return isl_bool_error;
8207 is_subset = isl_map_is_subset(map1, map2);
8208 if (is_subset != isl_bool_true)
8209 return is_subset;
8210 is_subset = isl_map_is_subset(map2, map1);
8211 if (is_subset == isl_bool_error)
8212 return is_subset;
8213 return !is_subset;
8216 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8217 __isl_keep isl_set *set2)
8219 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8222 /* Is "bmap" obviously equal to the universe with the same space?
8224 * That is, does it not have any constraints?
8226 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8228 if (!bmap)
8229 return isl_bool_error;
8230 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8233 /* Is "bset" obviously equal to the universe with the same space?
8235 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8237 return isl_basic_map_plain_is_universe(bset);
8240 /* If "c" does not involve any existentially quantified variables,
8241 * then set *univ to false and abort
8243 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8245 isl_bool *univ = user;
8246 unsigned n;
8248 n = isl_constraint_dim(c, isl_dim_div);
8249 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8250 isl_constraint_free(c);
8251 if (*univ < 0 || !*univ)
8252 return isl_stat_error;
8253 return isl_stat_ok;
8256 /* Is "bmap" equal to the universe with the same space?
8258 * First check if it is obviously equal to the universe.
8259 * If not and if there are any constraints not involving
8260 * existentially quantified variables, then it is certainly
8261 * not equal to the universe.
8262 * Otherwise, check if the universe is a subset of "bmap".
8264 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8266 isl_bool univ;
8267 isl_basic_map *test;
8269 univ = isl_basic_map_plain_is_universe(bmap);
8270 if (univ < 0 || univ)
8271 return univ;
8272 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8273 return isl_bool_false;
8274 univ = isl_bool_true;
8275 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8276 univ)
8277 return isl_bool_error;
8278 if (univ < 0 || !univ)
8279 return univ;
8280 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8281 univ = isl_basic_map_is_subset(test, bmap);
8282 isl_basic_map_free(test);
8283 return univ;
8286 /* Is "bset" equal to the universe with the same space?
8288 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8290 return isl_basic_map_is_universe(bset);
8293 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8295 int i;
8297 if (!map)
8298 return isl_bool_error;
8300 for (i = 0; i < map->n; ++i) {
8301 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8302 if (r < 0 || r)
8303 return r;
8306 return isl_bool_false;
8309 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8311 return isl_map_plain_is_universe(set_to_map(set));
8314 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8316 struct isl_basic_set *bset = NULL;
8317 struct isl_vec *sample = NULL;
8318 isl_bool empty, non_empty;
8320 if (!bmap)
8321 return isl_bool_error;
8323 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8324 return isl_bool_true;
8326 if (isl_basic_map_plain_is_universe(bmap))
8327 return isl_bool_false;
8329 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8330 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8331 copy = isl_basic_map_remove_redundancies(copy);
8332 empty = isl_basic_map_plain_is_empty(copy);
8333 isl_basic_map_free(copy);
8334 return empty;
8337 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8338 if (non_empty < 0)
8339 return isl_bool_error;
8340 if (non_empty)
8341 return isl_bool_false;
8342 isl_vec_free(bmap->sample);
8343 bmap->sample = NULL;
8344 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8345 if (!bset)
8346 return isl_bool_error;
8347 sample = isl_basic_set_sample_vec(bset);
8348 if (!sample)
8349 return isl_bool_error;
8350 empty = sample->size == 0;
8351 isl_vec_free(bmap->sample);
8352 bmap->sample = sample;
8353 if (empty)
8354 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8356 return empty;
8359 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8361 if (!bmap)
8362 return isl_bool_error;
8363 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8366 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8368 if (!bset)
8369 return isl_bool_error;
8370 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8373 /* Is "bmap" known to be non-empty?
8375 * That is, is the cached sample still valid?
8377 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8379 unsigned total;
8381 if (!bmap)
8382 return isl_bool_error;
8383 if (!bmap->sample)
8384 return isl_bool_false;
8385 total = 1 + isl_basic_map_total_dim(bmap);
8386 if (bmap->sample->size != total)
8387 return isl_bool_false;
8388 return isl_basic_map_contains(bmap, bmap->sample);
8391 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8393 return isl_basic_map_is_empty(bset_to_bmap(bset));
8396 struct isl_map *isl_basic_map_union(
8397 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8399 struct isl_map *map;
8400 if (!bmap1 || !bmap2)
8401 goto error;
8403 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8405 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8406 if (!map)
8407 goto error;
8408 map = isl_map_add_basic_map(map, bmap1);
8409 map = isl_map_add_basic_map(map, bmap2);
8410 return map;
8411 error:
8412 isl_basic_map_free(bmap1);
8413 isl_basic_map_free(bmap2);
8414 return NULL;
8417 struct isl_set *isl_basic_set_union(
8418 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8420 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8421 bset_to_bmap(bset2)));
8424 /* Order divs such that any div only depends on previous divs */
8425 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8427 int i;
8428 unsigned off;
8430 if (!bmap)
8431 return NULL;
8433 off = isl_space_dim(bmap->dim, isl_dim_all);
8435 for (i = 0; i < bmap->n_div; ++i) {
8436 int pos;
8437 if (isl_int_is_zero(bmap->div[i][0]))
8438 continue;
8439 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8440 bmap->n_div-i);
8441 if (pos == -1)
8442 continue;
8443 if (pos == 0)
8444 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8445 "integer division depends on itself",
8446 return isl_basic_map_free(bmap));
8447 isl_basic_map_swap_div(bmap, i, i + pos);
8448 --i;
8450 return bmap;
8453 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8455 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8458 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8460 int i;
8462 if (!map)
8463 return 0;
8465 for (i = 0; i < map->n; ++i) {
8466 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8467 if (!map->p[i])
8468 goto error;
8471 return map;
8472 error:
8473 isl_map_free(map);
8474 return NULL;
8477 /* Sort the local variables of "bset".
8479 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8480 __isl_take isl_basic_set *bset)
8482 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8485 /* Apply the expansion computed by isl_merge_divs.
8486 * The expansion itself is given by "exp" while the resulting
8487 * list of divs is given by "div".
8489 * Move the integer divisions of "bmap" into the right position
8490 * according to "exp" and then introduce the additional integer
8491 * divisions, adding div constraints.
8492 * The moving should be done first to avoid moving coefficients
8493 * in the definitions of the extra integer divisions.
8495 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8496 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8498 int i, j;
8499 int n_div;
8501 bmap = isl_basic_map_cow(bmap);
8502 if (!bmap || !div)
8503 goto error;
8505 if (div->n_row < bmap->n_div)
8506 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8507 "not an expansion", goto error);
8509 n_div = bmap->n_div;
8510 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8511 div->n_row - n_div, 0,
8512 2 * (div->n_row - n_div));
8514 for (i = n_div; i < div->n_row; ++i)
8515 if (isl_basic_map_alloc_div(bmap) < 0)
8516 goto error;
8518 for (j = n_div - 1; j >= 0; --j) {
8519 if (exp[j] == j)
8520 break;
8521 isl_basic_map_swap_div(bmap, j, exp[j]);
8523 j = 0;
8524 for (i = 0; i < div->n_row; ++i) {
8525 if (j < n_div && exp[j] == i) {
8526 j++;
8527 } else {
8528 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8529 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8530 continue;
8531 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8532 goto error;
8536 isl_mat_free(div);
8537 return bmap;
8538 error:
8539 isl_basic_map_free(bmap);
8540 isl_mat_free(div);
8541 return NULL;
8544 /* Apply the expansion computed by isl_merge_divs.
8545 * The expansion itself is given by "exp" while the resulting
8546 * list of divs is given by "div".
8548 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8549 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8551 return isl_basic_map_expand_divs(bset, div, exp);
8554 /* Look for a div in dst that corresponds to the div "div" in src.
8555 * The divs before "div" in src and dst are assumed to be the same.
8557 * Returns -1 if no corresponding div was found and the position
8558 * of the corresponding div in dst otherwise.
8560 static int find_div(struct isl_basic_map *dst,
8561 struct isl_basic_map *src, unsigned div)
8563 int i;
8565 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8567 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8568 for (i = div; i < dst->n_div; ++i)
8569 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8570 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8571 dst->n_div - div) == -1)
8572 return i;
8573 return -1;
8576 /* Align the divs of "dst" to those of "src", adding divs from "src"
8577 * if needed. That is, make sure that the first src->n_div divs
8578 * of the result are equal to those of src.
8580 * The result is not finalized as by design it will have redundant
8581 * divs if any divs from "src" were copied.
8583 __isl_give isl_basic_map *isl_basic_map_align_divs(
8584 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8586 int i;
8587 int known, extended;
8588 unsigned total;
8590 if (!dst || !src)
8591 return isl_basic_map_free(dst);
8593 if (src->n_div == 0)
8594 return dst;
8596 known = isl_basic_map_divs_known(src);
8597 if (known < 0)
8598 return isl_basic_map_free(dst);
8599 if (!known)
8600 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8601 "some src divs are unknown",
8602 return isl_basic_map_free(dst));
8604 src = isl_basic_map_order_divs(src);
8606 extended = 0;
8607 total = isl_space_dim(src->dim, isl_dim_all);
8608 for (i = 0; i < src->n_div; ++i) {
8609 int j = find_div(dst, src, i);
8610 if (j < 0) {
8611 if (!extended) {
8612 int extra = src->n_div - i;
8613 dst = isl_basic_map_cow(dst);
8614 if (!dst)
8615 return NULL;
8616 dst = isl_basic_map_extend_space(dst,
8617 isl_space_copy(dst->dim),
8618 extra, 0, 2 * extra);
8619 extended = 1;
8621 j = isl_basic_map_alloc_div(dst);
8622 if (j < 0)
8623 return isl_basic_map_free(dst);
8624 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8625 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8626 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8627 return isl_basic_map_free(dst);
8629 if (j != i)
8630 isl_basic_map_swap_div(dst, i, j);
8632 return dst;
8635 struct isl_basic_set *isl_basic_set_align_divs(
8636 struct isl_basic_set *dst, struct isl_basic_set *src)
8638 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8639 bset_to_bmap(src)));
8642 struct isl_map *isl_map_align_divs(struct isl_map *map)
8644 int i;
8646 if (!map)
8647 return NULL;
8648 if (map->n == 0)
8649 return map;
8650 map = isl_map_compute_divs(map);
8651 map = isl_map_cow(map);
8652 if (!map)
8653 return NULL;
8655 for (i = 1; i < map->n; ++i)
8656 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8657 for (i = 1; i < map->n; ++i) {
8658 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8659 if (!map->p[i])
8660 return isl_map_free(map);
8663 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8664 return map;
8667 struct isl_set *isl_set_align_divs(struct isl_set *set)
8669 return set_from_map(isl_map_align_divs(set_to_map(set)));
8672 /* Align the divs of the basic maps in "map" to those
8673 * of the basic maps in "list", as well as to the other basic maps in "map".
8674 * The elements in "list" are assumed to have known divs.
8676 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8677 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8679 int i, n;
8681 map = isl_map_compute_divs(map);
8682 map = isl_map_cow(map);
8683 if (!map || !list)
8684 return isl_map_free(map);
8685 if (map->n == 0)
8686 return map;
8688 n = isl_basic_map_list_n_basic_map(list);
8689 for (i = 0; i < n; ++i) {
8690 isl_basic_map *bmap;
8692 bmap = isl_basic_map_list_get_basic_map(list, i);
8693 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8694 isl_basic_map_free(bmap);
8696 if (!map->p[0])
8697 return isl_map_free(map);
8699 return isl_map_align_divs(map);
8702 /* Align the divs of each element of "list" to those of "bmap".
8703 * Both "bmap" and the elements of "list" are assumed to have known divs.
8705 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8706 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8708 int i, n;
8710 if (!list || !bmap)
8711 return isl_basic_map_list_free(list);
8713 n = isl_basic_map_list_n_basic_map(list);
8714 for (i = 0; i < n; ++i) {
8715 isl_basic_map *bmap_i;
8717 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8718 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8719 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8722 return list;
8725 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8726 __isl_take isl_map *map)
8728 isl_bool ok;
8730 ok = isl_map_compatible_domain(map, set);
8731 if (ok < 0)
8732 goto error;
8733 if (!ok)
8734 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8735 "incompatible spaces", goto error);
8736 map = isl_map_intersect_domain(map, set);
8737 set = isl_map_range(map);
8738 return set;
8739 error:
8740 isl_set_free(set);
8741 isl_map_free(map);
8742 return NULL;
8745 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8746 __isl_take isl_map *map)
8748 return isl_map_align_params_map_map_and(set, map, &set_apply);
8751 /* There is no need to cow as removing empty parts doesn't change
8752 * the meaning of the set.
8754 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8756 int i;
8758 if (!map)
8759 return NULL;
8761 for (i = map->n - 1; i >= 0; --i)
8762 remove_if_empty(map, i);
8764 return map;
8767 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8769 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8772 static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
8774 struct isl_basic_map *bmap;
8775 if (!map || map->n == 0)
8776 return NULL;
8777 bmap = map->p[map->n-1];
8778 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8779 return isl_basic_map_copy(bmap);
8782 __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
8784 return map_copy_basic_map(map);
8787 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8789 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8792 static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
8793 __isl_keep isl_basic_map *bmap)
8795 int i;
8797 if (!map || !bmap)
8798 goto error;
8799 for (i = map->n-1; i >= 0; --i) {
8800 if (map->p[i] != bmap)
8801 continue;
8802 map = isl_map_cow(map);
8803 if (!map)
8804 goto error;
8805 isl_basic_map_free(map->p[i]);
8806 if (i != map->n-1) {
8807 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8808 map->p[i] = map->p[map->n-1];
8810 map->n--;
8811 return map;
8813 return map;
8814 error:
8815 isl_map_free(map);
8816 return NULL;
8819 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8820 __isl_keep isl_basic_map *bmap)
8822 return map_drop_basic_map(map, bmap);
8825 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8826 struct isl_basic_set *bset)
8828 return set_from_map(map_drop_basic_map(set_to_map(set),
8829 bset_to_bmap(bset)));
8832 /* Given two basic sets bset1 and bset2, compute the maximal difference
8833 * between the values of dimension pos in bset1 and those in bset2
8834 * for any common value of the parameters and dimensions preceding pos.
8836 static enum isl_lp_result basic_set_maximal_difference_at(
8837 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8838 int pos, isl_int *opt)
8840 isl_basic_map *bmap1;
8841 isl_basic_map *bmap2;
8842 struct isl_ctx *ctx;
8843 struct isl_vec *obj;
8844 unsigned total;
8845 unsigned nparam;
8846 unsigned dim1;
8847 enum isl_lp_result res;
8849 if (!bset1 || !bset2)
8850 return isl_lp_error;
8852 nparam = isl_basic_set_n_param(bset1);
8853 dim1 = isl_basic_set_n_dim(bset1);
8855 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8856 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8857 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8858 isl_dim_out, 0, pos);
8859 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8860 isl_dim_out, 0, pos);
8861 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8862 if (!bmap1)
8863 return isl_lp_error;
8865 total = isl_basic_map_total_dim(bmap1);
8866 ctx = bmap1->ctx;
8867 obj = isl_vec_alloc(ctx, 1 + total);
8868 if (!obj)
8869 goto error;
8870 isl_seq_clr(obj->block.data, 1 + total);
8871 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8872 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8873 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8874 opt, NULL, NULL);
8875 isl_basic_map_free(bmap1);
8876 isl_vec_free(obj);
8877 return res;
8878 error:
8879 isl_basic_map_free(bmap1);
8880 return isl_lp_error;
8883 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8884 * for any common value of the parameters and dimensions preceding pos
8885 * in both basic sets, the values of dimension pos in bset1 are
8886 * smaller or larger than those in bset2.
8888 * Returns
8889 * 1 if bset1 follows bset2
8890 * -1 if bset1 precedes bset2
8891 * 0 if bset1 and bset2 are incomparable
8892 * -2 if some error occurred.
8894 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8895 struct isl_basic_set *bset2, int pos)
8897 isl_int opt;
8898 enum isl_lp_result res;
8899 int cmp;
8901 isl_int_init(opt);
8903 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8905 if (res == isl_lp_empty)
8906 cmp = 0;
8907 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8908 res == isl_lp_unbounded)
8909 cmp = 1;
8910 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8911 cmp = -1;
8912 else
8913 cmp = -2;
8915 isl_int_clear(opt);
8916 return cmp;
8919 /* Given two basic sets bset1 and bset2, check whether
8920 * for any common value of the parameters and dimensions preceding pos
8921 * there is a value of dimension pos in bset1 that is larger
8922 * than a value of the same dimension in bset2.
8924 * Return
8925 * 1 if there exists such a pair
8926 * 0 if there is no such pair, but there is a pair of equal values
8927 * -1 otherwise
8928 * -2 if some error occurred.
8930 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8931 __isl_keep isl_basic_set *bset2, int pos)
8933 isl_int opt;
8934 enum isl_lp_result res;
8935 int cmp;
8937 isl_int_init(opt);
8939 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8941 if (res == isl_lp_empty)
8942 cmp = -1;
8943 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8944 res == isl_lp_unbounded)
8945 cmp = 1;
8946 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8947 cmp = -1;
8948 else if (res == isl_lp_ok)
8949 cmp = 0;
8950 else
8951 cmp = -2;
8953 isl_int_clear(opt);
8954 return cmp;
8957 /* Given two sets set1 and set2, check whether
8958 * for any common value of the parameters and dimensions preceding pos
8959 * there is a value of dimension pos in set1 that is larger
8960 * than a value of the same dimension in set2.
8962 * Return
8963 * 1 if there exists such a pair
8964 * 0 if there is no such pair, but there is a pair of equal values
8965 * -1 otherwise
8966 * -2 if some error occurred.
8968 int isl_set_follows_at(__isl_keep isl_set *set1,
8969 __isl_keep isl_set *set2, int pos)
8971 int i, j;
8972 int follows = -1;
8974 if (!set1 || !set2)
8975 return -2;
8977 for (i = 0; i < set1->n; ++i)
8978 for (j = 0; j < set2->n; ++j) {
8979 int f;
8980 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8981 if (f == 1 || f == -2)
8982 return f;
8983 if (f > follows)
8984 follows = f;
8987 return follows;
8990 static isl_bool isl_basic_map_plain_has_fixed_var(
8991 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
8993 int i;
8994 int d;
8995 unsigned total;
8997 if (!bmap)
8998 return isl_bool_error;
8999 total = isl_basic_map_total_dim(bmap);
9000 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9001 for (; d+1 > pos; --d)
9002 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9003 break;
9004 if (d != pos)
9005 continue;
9006 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9007 return isl_bool_false;
9008 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9009 return isl_bool_false;
9010 if (!isl_int_is_one(bmap->eq[i][1+d]))
9011 return isl_bool_false;
9012 if (val)
9013 isl_int_neg(*val, bmap->eq[i][0]);
9014 return isl_bool_true;
9016 return isl_bool_false;
9019 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9020 unsigned pos, isl_int *val)
9022 int i;
9023 isl_int v;
9024 isl_int tmp;
9025 isl_bool fixed;
9027 if (!map)
9028 return isl_bool_error;
9029 if (map->n == 0)
9030 return isl_bool_false;
9031 if (map->n == 1)
9032 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9033 isl_int_init(v);
9034 isl_int_init(tmp);
9035 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9036 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9037 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9038 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9039 fixed = isl_bool_false;
9041 if (val)
9042 isl_int_set(*val, v);
9043 isl_int_clear(tmp);
9044 isl_int_clear(v);
9045 return fixed;
9048 static isl_bool isl_basic_set_plain_has_fixed_var(
9049 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9051 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9052 pos, val);
9055 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
9056 isl_int *val)
9058 return isl_map_plain_has_fixed_var(set_to_map(set), pos, val);
9061 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9062 enum isl_dim_type type, unsigned pos, isl_int *val)
9064 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9065 return isl_bool_error;
9066 return isl_basic_map_plain_has_fixed_var(bmap,
9067 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9070 /* If "bmap" obviously lies on a hyperplane where the given dimension
9071 * has a fixed value, then return that value.
9072 * Otherwise return NaN.
9074 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9075 __isl_keep isl_basic_map *bmap,
9076 enum isl_dim_type type, unsigned pos)
9078 isl_ctx *ctx;
9079 isl_val *v;
9080 isl_bool fixed;
9082 if (!bmap)
9083 return NULL;
9084 ctx = isl_basic_map_get_ctx(bmap);
9085 v = isl_val_alloc(ctx);
9086 if (!v)
9087 return NULL;
9088 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9089 if (fixed < 0)
9090 return isl_val_free(v);
9091 if (fixed) {
9092 isl_int_set_si(v->d, 1);
9093 return v;
9095 isl_val_free(v);
9096 return isl_val_nan(ctx);
9099 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9100 enum isl_dim_type type, unsigned pos, isl_int *val)
9102 if (pos >= isl_map_dim(map, type))
9103 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9104 "position out of bounds", return isl_bool_error);
9105 return isl_map_plain_has_fixed_var(map,
9106 map_offset(map, type) - 1 + pos, val);
9109 /* If "map" obviously lies on a hyperplane where the given dimension
9110 * has a fixed value, then return that value.
9111 * Otherwise return NaN.
9113 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9114 enum isl_dim_type type, unsigned pos)
9116 isl_ctx *ctx;
9117 isl_val *v;
9118 isl_bool fixed;
9120 if (!map)
9121 return NULL;
9122 ctx = isl_map_get_ctx(map);
9123 v = isl_val_alloc(ctx);
9124 if (!v)
9125 return NULL;
9126 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9127 if (fixed < 0)
9128 return isl_val_free(v);
9129 if (fixed) {
9130 isl_int_set_si(v->d, 1);
9131 return v;
9133 isl_val_free(v);
9134 return isl_val_nan(ctx);
9137 /* If "set" obviously lies on a hyperplane where the given dimension
9138 * has a fixed value, then return that value.
9139 * Otherwise return NaN.
9141 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9142 enum isl_dim_type type, unsigned pos)
9144 return isl_map_plain_get_val_if_fixed(set, type, pos);
9147 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
9148 enum isl_dim_type type, unsigned pos, isl_int *val)
9150 return isl_map_plain_is_fixed(set, type, pos, val);
9153 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9154 * then return this fixed value in *val.
9156 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9157 unsigned dim, isl_int *val)
9159 return isl_basic_set_plain_has_fixed_var(bset,
9160 isl_basic_set_n_param(bset) + dim, val);
9163 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9164 * then return this fixed value in *val.
9166 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
9167 unsigned dim, isl_int *val)
9169 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
9172 /* Check if input variable in has fixed value and if so and if val is not NULL,
9173 * then return this fixed value in *val.
9175 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
9176 unsigned in, isl_int *val)
9178 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
9181 /* Check if dimension dim has an (obvious) fixed lower bound and if so
9182 * and if val is not NULL, then return this lower bound in *val.
9184 int isl_basic_set_plain_dim_has_fixed_lower_bound(
9185 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
9187 int i, i_eq = -1, i_ineq = -1;
9188 isl_int *c;
9189 unsigned total;
9190 unsigned nparam;
9192 if (!bset)
9193 return -1;
9194 total = isl_basic_set_total_dim(bset);
9195 nparam = isl_basic_set_n_param(bset);
9196 for (i = 0; i < bset->n_eq; ++i) {
9197 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
9198 continue;
9199 if (i_eq != -1)
9200 return 0;
9201 i_eq = i;
9203 for (i = 0; i < bset->n_ineq; ++i) {
9204 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
9205 continue;
9206 if (i_eq != -1 || i_ineq != -1)
9207 return 0;
9208 i_ineq = i;
9210 if (i_eq == -1 && i_ineq == -1)
9211 return 0;
9212 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
9213 /* The coefficient should always be one due to normalization. */
9214 if (!isl_int_is_one(c[1+nparam+dim]))
9215 return 0;
9216 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
9217 return 0;
9218 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
9219 total - nparam - dim - 1) != -1)
9220 return 0;
9221 if (val)
9222 isl_int_neg(*val, c[0]);
9223 return 1;
9226 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
9227 unsigned dim, isl_int *val)
9229 int i;
9230 isl_int v;
9231 isl_int tmp;
9232 int fixed;
9234 if (!set)
9235 return -1;
9236 if (set->n == 0)
9237 return 0;
9238 if (set->n == 1)
9239 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9240 dim, val);
9241 isl_int_init(v);
9242 isl_int_init(tmp);
9243 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9244 dim, &v);
9245 for (i = 1; fixed == 1 && i < set->n; ++i) {
9246 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9247 dim, &tmp);
9248 if (fixed == 1 && isl_int_ne(tmp, v))
9249 fixed = 0;
9251 if (val)
9252 isl_int_set(*val, v);
9253 isl_int_clear(tmp);
9254 isl_int_clear(v);
9255 return fixed;
9258 /* Return -1 if the constraint "c1" should be sorted before "c2"
9259 * and 1 if it should be sorted after "c2".
9260 * Return 0 if the two constraints are the same (up to the constant term).
9262 * In particular, if a constraint involves later variables than another
9263 * then it is sorted after this other constraint.
9264 * uset_gist depends on constraints without existentially quantified
9265 * variables sorting first.
9267 * For constraints that have the same latest variable, those
9268 * with the same coefficient for this latest variable (first in absolute value
9269 * and then in actual value) are grouped together.
9270 * This is useful for detecting pairs of constraints that can
9271 * be chained in their printed representation.
9273 * Finally, within a group, constraints are sorted according to
9274 * their coefficients (excluding the constant term).
9276 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9278 isl_int **c1 = (isl_int **) p1;
9279 isl_int **c2 = (isl_int **) p2;
9280 int l1, l2;
9281 unsigned size = *(unsigned *) arg;
9282 int cmp;
9284 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9285 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9287 if (l1 != l2)
9288 return l1 - l2;
9290 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9291 if (cmp != 0)
9292 return cmp;
9293 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9294 if (cmp != 0)
9295 return -cmp;
9297 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9300 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9301 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9302 * and 0 if the two constraints are the same (up to the constant term).
9304 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9305 isl_int *c1, isl_int *c2)
9307 unsigned total;
9309 if (!bmap)
9310 return -2;
9311 total = isl_basic_map_total_dim(bmap);
9312 return sort_constraint_cmp(&c1, &c2, &total);
9315 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9316 __isl_take isl_basic_map *bmap)
9318 unsigned total;
9320 if (!bmap)
9321 return NULL;
9322 if (bmap->n_ineq == 0)
9323 return bmap;
9324 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9325 return bmap;
9326 total = isl_basic_map_total_dim(bmap);
9327 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9328 &sort_constraint_cmp, &total) < 0)
9329 return isl_basic_map_free(bmap);
9330 return bmap;
9333 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9334 __isl_take isl_basic_set *bset)
9336 isl_basic_map *bmap = bset_to_bmap(bset);
9337 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9340 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9342 if (!bmap)
9343 return NULL;
9344 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9345 return bmap;
9346 bmap = isl_basic_map_remove_redundancies(bmap);
9347 bmap = isl_basic_map_sort_constraints(bmap);
9348 if (bmap)
9349 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9350 return bmap;
9353 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9355 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
9358 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9359 const __isl_keep isl_basic_map *bmap2)
9361 int i, cmp;
9362 unsigned total;
9364 if (!bmap1 || !bmap2)
9365 return -1;
9367 if (bmap1 == bmap2)
9368 return 0;
9369 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9370 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9371 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9372 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9373 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9374 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9375 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9376 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9377 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9378 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9379 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9380 return 0;
9381 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9382 return 1;
9383 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9384 return -1;
9385 if (bmap1->n_eq != bmap2->n_eq)
9386 return bmap1->n_eq - bmap2->n_eq;
9387 if (bmap1->n_ineq != bmap2->n_ineq)
9388 return bmap1->n_ineq - bmap2->n_ineq;
9389 if (bmap1->n_div != bmap2->n_div)
9390 return bmap1->n_div - bmap2->n_div;
9391 total = isl_basic_map_total_dim(bmap1);
9392 for (i = 0; i < bmap1->n_eq; ++i) {
9393 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9394 if (cmp)
9395 return cmp;
9397 for (i = 0; i < bmap1->n_ineq; ++i) {
9398 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9399 if (cmp)
9400 return cmp;
9402 for (i = 0; i < bmap1->n_div; ++i) {
9403 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9404 if (cmp)
9405 return cmp;
9407 return 0;
9410 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9411 const __isl_keep isl_basic_set *bset2)
9413 return isl_basic_map_plain_cmp(bset1, bset2);
9416 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9418 int i, cmp;
9420 if (set1 == set2)
9421 return 0;
9422 if (set1->n != set2->n)
9423 return set1->n - set2->n;
9425 for (i = 0; i < set1->n; ++i) {
9426 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9427 if (cmp)
9428 return cmp;
9431 return 0;
9434 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9435 __isl_keep isl_basic_map *bmap2)
9437 if (!bmap1 || !bmap2)
9438 return isl_bool_error;
9439 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9442 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9443 __isl_keep isl_basic_set *bset2)
9445 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9446 bset_to_bmap(bset2));
9449 static int qsort_bmap_cmp(const void *p1, const void *p2)
9451 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9452 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9454 return isl_basic_map_plain_cmp(bmap1, bmap2);
9457 /* Sort the basic maps of "map" and remove duplicate basic maps.
9459 * While removing basic maps, we make sure that the basic maps remain
9460 * sorted because isl_map_normalize expects the basic maps of the result
9461 * to be sorted.
9463 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9465 int i, j;
9467 map = isl_map_remove_empty_parts(map);
9468 if (!map)
9469 return NULL;
9470 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9471 for (i = map->n - 1; i >= 1; --i) {
9472 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9473 continue;
9474 isl_basic_map_free(map->p[i-1]);
9475 for (j = i; j < map->n; ++j)
9476 map->p[j - 1] = map->p[j];
9477 map->n--;
9480 return map;
9483 /* Remove obvious duplicates among the basic maps of "map".
9485 * Unlike isl_map_normalize, this function does not remove redundant
9486 * constraints and only removes duplicates that have exactly the same
9487 * constraints in the input. It does sort the constraints and
9488 * the basic maps to ease the detection of duplicates.
9490 * If "map" has already been normalized or if the basic maps are
9491 * disjoint, then there can be no duplicates.
9493 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9495 int i;
9496 isl_basic_map *bmap;
9498 if (!map)
9499 return NULL;
9500 if (map->n <= 1)
9501 return map;
9502 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9503 return map;
9504 for (i = 0; i < map->n; ++i) {
9505 bmap = isl_basic_map_copy(map->p[i]);
9506 bmap = isl_basic_map_sort_constraints(bmap);
9507 if (!bmap)
9508 return isl_map_free(map);
9509 isl_basic_map_free(map->p[i]);
9510 map->p[i] = bmap;
9513 map = sort_and_remove_duplicates(map);
9514 return map;
9517 /* We normalize in place, but if anything goes wrong we need
9518 * to return NULL, so we need to make sure we don't change the
9519 * meaning of any possible other copies of map.
9521 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9523 int i;
9524 struct isl_basic_map *bmap;
9526 if (!map)
9527 return NULL;
9528 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9529 return map;
9530 for (i = 0; i < map->n; ++i) {
9531 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9532 if (!bmap)
9533 goto error;
9534 isl_basic_map_free(map->p[i]);
9535 map->p[i] = bmap;
9538 map = sort_and_remove_duplicates(map);
9539 if (map)
9540 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9541 return map;
9542 error:
9543 isl_map_free(map);
9544 return NULL;
9547 struct isl_set *isl_set_normalize(struct isl_set *set)
9549 return set_from_map(isl_map_normalize(set_to_map(set)));
9552 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9553 __isl_keep isl_map *map2)
9555 int i;
9556 isl_bool equal;
9558 if (!map1 || !map2)
9559 return isl_bool_error;
9561 if (map1 == map2)
9562 return isl_bool_true;
9563 if (!isl_space_is_equal(map1->dim, map2->dim))
9564 return isl_bool_false;
9566 map1 = isl_map_copy(map1);
9567 map2 = isl_map_copy(map2);
9568 map1 = isl_map_normalize(map1);
9569 map2 = isl_map_normalize(map2);
9570 if (!map1 || !map2)
9571 goto error;
9572 equal = map1->n == map2->n;
9573 for (i = 0; equal && i < map1->n; ++i) {
9574 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9575 if (equal < 0)
9576 goto error;
9578 isl_map_free(map1);
9579 isl_map_free(map2);
9580 return equal;
9581 error:
9582 isl_map_free(map1);
9583 isl_map_free(map2);
9584 return isl_bool_error;
9587 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9588 __isl_keep isl_set *set2)
9590 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9593 /* Return an interval that ranges from min to max (inclusive)
9595 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9596 isl_int min, isl_int max)
9598 int k;
9599 struct isl_basic_set *bset = NULL;
9601 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9602 if (!bset)
9603 goto error;
9605 k = isl_basic_set_alloc_inequality(bset);
9606 if (k < 0)
9607 goto error;
9608 isl_int_set_si(bset->ineq[k][1], 1);
9609 isl_int_neg(bset->ineq[k][0], min);
9611 k = isl_basic_set_alloc_inequality(bset);
9612 if (k < 0)
9613 goto error;
9614 isl_int_set_si(bset->ineq[k][1], -1);
9615 isl_int_set(bset->ineq[k][0], max);
9617 return bset;
9618 error:
9619 isl_basic_set_free(bset);
9620 return NULL;
9623 /* Return the basic maps in "map" as a list.
9625 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9626 __isl_keep isl_map *map)
9628 int i;
9629 isl_ctx *ctx;
9630 isl_basic_map_list *list;
9632 if (!map)
9633 return NULL;
9634 ctx = isl_map_get_ctx(map);
9635 list = isl_basic_map_list_alloc(ctx, map->n);
9637 for (i = 0; i < map->n; ++i) {
9638 isl_basic_map *bmap;
9640 bmap = isl_basic_map_copy(map->p[i]);
9641 list = isl_basic_map_list_add(list, bmap);
9644 return list;
9647 /* Return the intersection of the elements in the non-empty list "list".
9648 * All elements are assumed to live in the same space.
9650 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9651 __isl_take isl_basic_map_list *list)
9653 int i, n;
9654 isl_basic_map *bmap;
9656 if (!list)
9657 return NULL;
9658 n = isl_basic_map_list_n_basic_map(list);
9659 if (n < 1)
9660 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9661 "expecting non-empty list", goto error);
9663 bmap = isl_basic_map_list_get_basic_map(list, 0);
9664 for (i = 1; i < n; ++i) {
9665 isl_basic_map *bmap_i;
9667 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9668 bmap = isl_basic_map_intersect(bmap, bmap_i);
9671 isl_basic_map_list_free(list);
9672 return bmap;
9673 error:
9674 isl_basic_map_list_free(list);
9675 return NULL;
9678 /* Return the intersection of the elements in the non-empty list "list".
9679 * All elements are assumed to live in the same space.
9681 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9682 __isl_take isl_basic_set_list *list)
9684 return isl_basic_map_list_intersect(list);
9687 /* Return the union of the elements of "list".
9688 * The list is required to have at least one element.
9690 __isl_give isl_set *isl_basic_set_list_union(
9691 __isl_take isl_basic_set_list *list)
9693 int i, n;
9694 isl_space *space;
9695 isl_basic_set *bset;
9696 isl_set *set;
9698 if (!list)
9699 return NULL;
9700 n = isl_basic_set_list_n_basic_set(list);
9701 if (n < 1)
9702 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9703 "expecting non-empty list", goto error);
9705 bset = isl_basic_set_list_get_basic_set(list, 0);
9706 space = isl_basic_set_get_space(bset);
9707 isl_basic_set_free(bset);
9709 set = isl_set_alloc_space(space, n, 0);
9710 for (i = 0; i < n; ++i) {
9711 bset = isl_basic_set_list_get_basic_set(list, i);
9712 set = isl_set_add_basic_set(set, bset);
9715 isl_basic_set_list_free(list);
9716 return set;
9717 error:
9718 isl_basic_set_list_free(list);
9719 return NULL;
9722 /* Return the union of the elements in the non-empty list "list".
9723 * All elements are assumed to live in the same space.
9725 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9727 int i, n;
9728 isl_set *set;
9730 if (!list)
9731 return NULL;
9732 n = isl_set_list_n_set(list);
9733 if (n < 1)
9734 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9735 "expecting non-empty list", goto error);
9737 set = isl_set_list_get_set(list, 0);
9738 for (i = 1; i < n; ++i) {
9739 isl_set *set_i;
9741 set_i = isl_set_list_get_set(list, i);
9742 set = isl_set_union(set, set_i);
9745 isl_set_list_free(list);
9746 return set;
9747 error:
9748 isl_set_list_free(list);
9749 return NULL;
9752 /* Return the Cartesian product of the basic sets in list (in the given order).
9754 __isl_give isl_basic_set *isl_basic_set_list_product(
9755 __isl_take struct isl_basic_set_list *list)
9757 int i;
9758 unsigned dim;
9759 unsigned nparam;
9760 unsigned extra;
9761 unsigned n_eq;
9762 unsigned n_ineq;
9763 struct isl_basic_set *product = NULL;
9765 if (!list)
9766 goto error;
9767 isl_assert(list->ctx, list->n > 0, goto error);
9768 isl_assert(list->ctx, list->p[0], goto error);
9769 nparam = isl_basic_set_n_param(list->p[0]);
9770 dim = isl_basic_set_n_dim(list->p[0]);
9771 extra = list->p[0]->n_div;
9772 n_eq = list->p[0]->n_eq;
9773 n_ineq = list->p[0]->n_ineq;
9774 for (i = 1; i < list->n; ++i) {
9775 isl_assert(list->ctx, list->p[i], goto error);
9776 isl_assert(list->ctx,
9777 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9778 dim += isl_basic_set_n_dim(list->p[i]);
9779 extra += list->p[i]->n_div;
9780 n_eq += list->p[i]->n_eq;
9781 n_ineq += list->p[i]->n_ineq;
9783 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9784 n_eq, n_ineq);
9785 if (!product)
9786 goto error;
9787 dim = 0;
9788 for (i = 0; i < list->n; ++i) {
9789 isl_basic_set_add_constraints(product,
9790 isl_basic_set_copy(list->p[i]), dim);
9791 dim += isl_basic_set_n_dim(list->p[i]);
9793 isl_basic_set_list_free(list);
9794 return product;
9795 error:
9796 isl_basic_set_free(product);
9797 isl_basic_set_list_free(list);
9798 return NULL;
9801 struct isl_basic_map *isl_basic_map_product(
9802 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9804 isl_space *dim_result = NULL;
9805 struct isl_basic_map *bmap;
9806 unsigned in1, in2, out1, out2, nparam, total, pos;
9807 struct isl_dim_map *dim_map1, *dim_map2;
9809 if (!bmap1 || !bmap2)
9810 goto error;
9812 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9813 bmap2->dim, isl_dim_param), goto error);
9814 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9815 isl_space_copy(bmap2->dim));
9817 in1 = isl_basic_map_n_in(bmap1);
9818 in2 = isl_basic_map_n_in(bmap2);
9819 out1 = isl_basic_map_n_out(bmap1);
9820 out2 = isl_basic_map_n_out(bmap2);
9821 nparam = isl_basic_map_n_param(bmap1);
9823 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9824 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9825 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9826 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9827 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9828 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9829 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9830 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9831 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9832 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9833 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9835 bmap = isl_basic_map_alloc_space(dim_result,
9836 bmap1->n_div + bmap2->n_div,
9837 bmap1->n_eq + bmap2->n_eq,
9838 bmap1->n_ineq + bmap2->n_ineq);
9839 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9840 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9841 bmap = isl_basic_map_simplify(bmap);
9842 return isl_basic_map_finalize(bmap);
9843 error:
9844 isl_basic_map_free(bmap1);
9845 isl_basic_map_free(bmap2);
9846 return NULL;
9849 __isl_give isl_basic_map *isl_basic_map_flat_product(
9850 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9852 isl_basic_map *prod;
9854 prod = isl_basic_map_product(bmap1, bmap2);
9855 prod = isl_basic_map_flatten(prod);
9856 return prod;
9859 __isl_give isl_basic_set *isl_basic_set_flat_product(
9860 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9862 return isl_basic_map_flat_range_product(bset1, bset2);
9865 __isl_give isl_basic_map *isl_basic_map_domain_product(
9866 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9868 isl_space *space_result = NULL;
9869 isl_basic_map *bmap;
9870 unsigned in1, in2, out, nparam, total, pos;
9871 struct isl_dim_map *dim_map1, *dim_map2;
9873 if (!bmap1 || !bmap2)
9874 goto error;
9876 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9877 isl_space_copy(bmap2->dim));
9879 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9880 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9881 out = isl_basic_map_dim(bmap1, isl_dim_out);
9882 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9884 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9885 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9886 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9887 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9888 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9889 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9890 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9891 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9892 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9893 isl_dim_map_div(dim_map1, bmap1, pos += out);
9894 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9896 bmap = isl_basic_map_alloc_space(space_result,
9897 bmap1->n_div + bmap2->n_div,
9898 bmap1->n_eq + bmap2->n_eq,
9899 bmap1->n_ineq + bmap2->n_ineq);
9900 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9901 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9902 bmap = isl_basic_map_simplify(bmap);
9903 return isl_basic_map_finalize(bmap);
9904 error:
9905 isl_basic_map_free(bmap1);
9906 isl_basic_map_free(bmap2);
9907 return NULL;
9910 __isl_give isl_basic_map *isl_basic_map_range_product(
9911 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9913 int rational;
9914 isl_space *dim_result = NULL;
9915 isl_basic_map *bmap;
9916 unsigned in, out1, out2, nparam, total, pos;
9917 struct isl_dim_map *dim_map1, *dim_map2;
9919 rational = isl_basic_map_is_rational(bmap1);
9920 if (rational >= 0 && rational)
9921 rational = isl_basic_map_is_rational(bmap2);
9922 if (!bmap1 || !bmap2 || rational < 0)
9923 goto error;
9925 if (!isl_space_match(bmap1->dim, isl_dim_param,
9926 bmap2->dim, isl_dim_param))
9927 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9928 "parameters don't match", goto error);
9930 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9931 isl_space_copy(bmap2->dim));
9933 in = isl_basic_map_dim(bmap1, isl_dim_in);
9934 out1 = isl_basic_map_n_out(bmap1);
9935 out2 = isl_basic_map_n_out(bmap2);
9936 nparam = isl_basic_map_n_param(bmap1);
9938 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9939 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9940 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9941 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9942 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9943 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9944 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9945 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9946 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9947 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9948 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9950 bmap = isl_basic_map_alloc_space(dim_result,
9951 bmap1->n_div + bmap2->n_div,
9952 bmap1->n_eq + bmap2->n_eq,
9953 bmap1->n_ineq + bmap2->n_ineq);
9954 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9955 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9956 if (rational)
9957 bmap = isl_basic_map_set_rational(bmap);
9958 bmap = isl_basic_map_simplify(bmap);
9959 return isl_basic_map_finalize(bmap);
9960 error:
9961 isl_basic_map_free(bmap1);
9962 isl_basic_map_free(bmap2);
9963 return NULL;
9966 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9967 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9969 isl_basic_map *prod;
9971 prod = isl_basic_map_range_product(bmap1, bmap2);
9972 prod = isl_basic_map_flatten_range(prod);
9973 return prod;
9976 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9977 * and collect the results.
9978 * The result live in the space obtained by calling "space_product"
9979 * on the spaces of "map1" and "map2".
9980 * If "remove_duplicates" is set then the result may contain duplicates
9981 * (even if the inputs do not) and so we try and remove the obvious
9982 * duplicates.
9984 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9985 __isl_take isl_map *map2,
9986 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9987 __isl_take isl_space *right),
9988 __isl_give isl_basic_map *(*basic_map_product)(
9989 __isl_take isl_basic_map *left,
9990 __isl_take isl_basic_map *right),
9991 int remove_duplicates)
9993 unsigned flags = 0;
9994 struct isl_map *result;
9995 int i, j;
9997 if (!map1 || !map2)
9998 goto error;
10000 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
10001 map2->dim, isl_dim_param), goto error);
10003 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10004 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10005 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10007 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10008 isl_space_copy(map2->dim)),
10009 map1->n * map2->n, flags);
10010 if (!result)
10011 goto error;
10012 for (i = 0; i < map1->n; ++i)
10013 for (j = 0; j < map2->n; ++j) {
10014 struct isl_basic_map *part;
10015 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10016 isl_basic_map_copy(map2->p[j]));
10017 if (isl_basic_map_is_empty(part))
10018 isl_basic_map_free(part);
10019 else
10020 result = isl_map_add_basic_map(result, part);
10021 if (!result)
10022 goto error;
10024 if (remove_duplicates)
10025 result = isl_map_remove_obvious_duplicates(result);
10026 isl_map_free(map1);
10027 isl_map_free(map2);
10028 return result;
10029 error:
10030 isl_map_free(map1);
10031 isl_map_free(map2);
10032 return NULL;
10035 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10037 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10038 __isl_take isl_map *map2)
10040 return map_product(map1, map2, &isl_space_product,
10041 &isl_basic_map_product, 0);
10044 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10045 __isl_take isl_map *map2)
10047 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10050 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10052 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10053 __isl_take isl_map *map2)
10055 isl_map *prod;
10057 prod = isl_map_product(map1, map2);
10058 prod = isl_map_flatten(prod);
10059 return prod;
10062 /* Given two set A and B, construct its Cartesian product A x B.
10064 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10066 return isl_map_range_product(set1, set2);
10069 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10070 __isl_take isl_set *set2)
10072 return isl_map_flat_range_product(set1, set2);
10075 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10077 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10078 __isl_take isl_map *map2)
10080 return map_product(map1, map2, &isl_space_domain_product,
10081 &isl_basic_map_domain_product, 1);
10084 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10086 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10087 __isl_take isl_map *map2)
10089 return map_product(map1, map2, &isl_space_range_product,
10090 &isl_basic_map_range_product, 1);
10093 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10094 __isl_take isl_map *map2)
10096 return isl_map_align_params_map_map_and(map1, map2,
10097 &map_domain_product_aligned);
10100 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10101 __isl_take isl_map *map2)
10103 return isl_map_align_params_map_map_and(map1, map2,
10104 &map_range_product_aligned);
10107 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10109 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10111 isl_space *space;
10112 int total1, keep1, total2, keep2;
10114 if (!map)
10115 return NULL;
10116 if (!isl_space_domain_is_wrapping(map->dim) ||
10117 !isl_space_range_is_wrapping(map->dim))
10118 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10119 "not a product", return isl_map_free(map));
10121 space = isl_map_get_space(map);
10122 total1 = isl_space_dim(space, isl_dim_in);
10123 total2 = isl_space_dim(space, isl_dim_out);
10124 space = isl_space_factor_domain(space);
10125 keep1 = isl_space_dim(space, isl_dim_in);
10126 keep2 = isl_space_dim(space, isl_dim_out);
10127 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10128 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10129 map = isl_map_reset_space(map, space);
10131 return map;
10134 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10136 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10138 isl_space *space;
10139 int total1, keep1, total2, keep2;
10141 if (!map)
10142 return NULL;
10143 if (!isl_space_domain_is_wrapping(map->dim) ||
10144 !isl_space_range_is_wrapping(map->dim))
10145 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10146 "not a product", return isl_map_free(map));
10148 space = isl_map_get_space(map);
10149 total1 = isl_space_dim(space, isl_dim_in);
10150 total2 = isl_space_dim(space, isl_dim_out);
10151 space = isl_space_factor_range(space);
10152 keep1 = isl_space_dim(space, isl_dim_in);
10153 keep2 = isl_space_dim(space, isl_dim_out);
10154 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10155 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10156 map = isl_map_reset_space(map, space);
10158 return map;
10161 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10163 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10165 isl_space *space;
10166 int total, keep;
10168 if (!map)
10169 return NULL;
10170 if (!isl_space_domain_is_wrapping(map->dim))
10171 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10172 "domain is not a product", return isl_map_free(map));
10174 space = isl_map_get_space(map);
10175 total = isl_space_dim(space, isl_dim_in);
10176 space = isl_space_domain_factor_domain(space);
10177 keep = isl_space_dim(space, isl_dim_in);
10178 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10179 map = isl_map_reset_space(map, space);
10181 return map;
10184 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10186 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10188 isl_space *space;
10189 int total, keep;
10191 if (!map)
10192 return NULL;
10193 if (!isl_space_domain_is_wrapping(map->dim))
10194 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10195 "domain is not a product", return isl_map_free(map));
10197 space = isl_map_get_space(map);
10198 total = isl_space_dim(space, isl_dim_in);
10199 space = isl_space_domain_factor_range(space);
10200 keep = isl_space_dim(space, isl_dim_in);
10201 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10202 map = isl_map_reset_space(map, space);
10204 return map;
10207 /* Given a map A -> [B -> C], extract the map A -> B.
10209 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10211 isl_space *space;
10212 int total, keep;
10214 if (!map)
10215 return NULL;
10216 if (!isl_space_range_is_wrapping(map->dim))
10217 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10218 "range is not a product", return isl_map_free(map));
10220 space = isl_map_get_space(map);
10221 total = isl_space_dim(space, isl_dim_out);
10222 space = isl_space_range_factor_domain(space);
10223 keep = isl_space_dim(space, isl_dim_out);
10224 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10225 map = isl_map_reset_space(map, space);
10227 return map;
10230 /* Given a map A -> [B -> C], extract the map A -> C.
10232 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10234 isl_space *space;
10235 int total, keep;
10237 if (!map)
10238 return NULL;
10239 if (!isl_space_range_is_wrapping(map->dim))
10240 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10241 "range is not a product", return isl_map_free(map));
10243 space = isl_map_get_space(map);
10244 total = isl_space_dim(space, isl_dim_out);
10245 space = isl_space_range_factor_range(space);
10246 keep = isl_space_dim(space, isl_dim_out);
10247 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10248 map = isl_map_reset_space(map, space);
10250 return map;
10253 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10255 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10256 __isl_take isl_map *map2)
10258 isl_map *prod;
10260 prod = isl_map_domain_product(map1, map2);
10261 prod = isl_map_flatten_domain(prod);
10262 return prod;
10265 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10267 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10268 __isl_take isl_map *map2)
10270 isl_map *prod;
10272 prod = isl_map_range_product(map1, map2);
10273 prod = isl_map_flatten_range(prod);
10274 return prod;
10277 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10279 int i;
10280 uint32_t hash = isl_hash_init();
10281 unsigned total;
10283 if (!bmap)
10284 return 0;
10285 bmap = isl_basic_map_copy(bmap);
10286 bmap = isl_basic_map_normalize(bmap);
10287 if (!bmap)
10288 return 0;
10289 total = isl_basic_map_total_dim(bmap);
10290 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10291 for (i = 0; i < bmap->n_eq; ++i) {
10292 uint32_t c_hash;
10293 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10294 isl_hash_hash(hash, c_hash);
10296 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10297 for (i = 0; i < bmap->n_ineq; ++i) {
10298 uint32_t c_hash;
10299 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10300 isl_hash_hash(hash, c_hash);
10302 isl_hash_byte(hash, bmap->n_div & 0xFF);
10303 for (i = 0; i < bmap->n_div; ++i) {
10304 uint32_t c_hash;
10305 if (isl_int_is_zero(bmap->div[i][0]))
10306 continue;
10307 isl_hash_byte(hash, i & 0xFF);
10308 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10309 isl_hash_hash(hash, c_hash);
10311 isl_basic_map_free(bmap);
10312 return hash;
10315 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10317 return isl_basic_map_get_hash(bset_to_bmap(bset));
10320 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10322 int i;
10323 uint32_t hash;
10325 if (!map)
10326 return 0;
10327 map = isl_map_copy(map);
10328 map = isl_map_normalize(map);
10329 if (!map)
10330 return 0;
10332 hash = isl_hash_init();
10333 for (i = 0; i < map->n; ++i) {
10334 uint32_t bmap_hash;
10335 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10336 isl_hash_hash(hash, bmap_hash);
10339 isl_map_free(map);
10341 return hash;
10344 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10346 return isl_map_get_hash(set_to_map(set));
10349 /* Check if the value for dimension dim is completely determined
10350 * by the values of the other parameters and variables.
10351 * That is, check if dimension dim is involved in an equality.
10353 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10355 int i;
10356 unsigned nparam;
10358 if (!bset)
10359 return -1;
10360 nparam = isl_basic_set_n_param(bset);
10361 for (i = 0; i < bset->n_eq; ++i)
10362 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10363 return 1;
10364 return 0;
10367 /* Check if the value for dimension dim is completely determined
10368 * by the values of the other parameters and variables.
10369 * That is, check if dimension dim is involved in an equality
10370 * for each of the subsets.
10372 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10374 int i;
10376 if (!set)
10377 return -1;
10378 for (i = 0; i < set->n; ++i) {
10379 int unique;
10380 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10381 if (unique != 1)
10382 return unique;
10384 return 1;
10387 /* Return the number of basic maps in the (current) representation of "map".
10389 int isl_map_n_basic_map(__isl_keep isl_map *map)
10391 return map ? map->n : 0;
10394 int isl_set_n_basic_set(__isl_keep isl_set *set)
10396 return set ? set->n : 0;
10399 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10400 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10402 int i;
10404 if (!map)
10405 return isl_stat_error;
10407 for (i = 0; i < map->n; ++i)
10408 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10409 return isl_stat_error;
10411 return isl_stat_ok;
10414 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10415 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10417 int i;
10419 if (!set)
10420 return isl_stat_error;
10422 for (i = 0; i < set->n; ++i)
10423 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10424 return isl_stat_error;
10426 return isl_stat_ok;
10429 /* Return a list of basic sets, the union of which is equal to "set".
10431 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10432 __isl_keep isl_set *set)
10434 int i;
10435 isl_basic_set_list *list;
10437 if (!set)
10438 return NULL;
10440 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10441 for (i = 0; i < set->n; ++i) {
10442 isl_basic_set *bset;
10444 bset = isl_basic_set_copy(set->p[i]);
10445 list = isl_basic_set_list_add(list, bset);
10448 return list;
10451 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10453 isl_space *dim;
10455 if (!bset)
10456 return NULL;
10458 bset = isl_basic_set_cow(bset);
10459 if (!bset)
10460 return NULL;
10462 dim = isl_basic_set_get_space(bset);
10463 dim = isl_space_lift(dim, bset->n_div);
10464 if (!dim)
10465 goto error;
10466 isl_space_free(bset->dim);
10467 bset->dim = dim;
10468 bset->extra -= bset->n_div;
10469 bset->n_div = 0;
10471 bset = isl_basic_set_finalize(bset);
10473 return bset;
10474 error:
10475 isl_basic_set_free(bset);
10476 return NULL;
10479 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10481 int i;
10482 isl_space *dim;
10483 unsigned n_div;
10485 set = isl_set_align_divs(set);
10487 if (!set)
10488 return NULL;
10490 set = isl_set_cow(set);
10491 if (!set)
10492 return NULL;
10494 n_div = set->p[0]->n_div;
10495 dim = isl_set_get_space(set);
10496 dim = isl_space_lift(dim, n_div);
10497 if (!dim)
10498 goto error;
10499 isl_space_free(set->dim);
10500 set->dim = dim;
10502 for (i = 0; i < set->n; ++i) {
10503 set->p[i] = isl_basic_set_lift(set->p[i]);
10504 if (!set->p[i])
10505 goto error;
10508 return set;
10509 error:
10510 isl_set_free(set);
10511 return NULL;
10514 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10516 isl_space *dim;
10517 struct isl_basic_map *bmap;
10518 unsigned n_set;
10519 unsigned n_div;
10520 unsigned n_param;
10521 unsigned total;
10522 int i, k, l;
10524 set = isl_set_align_divs(set);
10526 if (!set)
10527 return NULL;
10529 dim = isl_set_get_space(set);
10530 if (set->n == 0 || set->p[0]->n_div == 0) {
10531 isl_set_free(set);
10532 return isl_map_identity(isl_space_map_from_set(dim));
10535 n_div = set->p[0]->n_div;
10536 dim = isl_space_map_from_set(dim);
10537 n_param = isl_space_dim(dim, isl_dim_param);
10538 n_set = isl_space_dim(dim, isl_dim_in);
10539 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10540 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10541 for (i = 0; i < n_set; ++i)
10542 bmap = var_equal(bmap, i);
10544 total = n_param + n_set + n_set + n_div;
10545 for (i = 0; i < n_div; ++i) {
10546 k = isl_basic_map_alloc_inequality(bmap);
10547 if (k < 0)
10548 goto error;
10549 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10550 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10551 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10552 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10553 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10554 set->p[0]->div[i][0]);
10556 l = isl_basic_map_alloc_inequality(bmap);
10557 if (l < 0)
10558 goto error;
10559 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10560 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10561 set->p[0]->div[i][0]);
10562 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10565 isl_set_free(set);
10566 bmap = isl_basic_map_simplify(bmap);
10567 bmap = isl_basic_map_finalize(bmap);
10568 return isl_map_from_basic_map(bmap);
10569 error:
10570 isl_set_free(set);
10571 isl_basic_map_free(bmap);
10572 return NULL;
10575 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10577 unsigned dim;
10578 int size = 0;
10580 if (!bset)
10581 return -1;
10583 dim = isl_basic_set_total_dim(bset);
10584 size += bset->n_eq * (1 + dim);
10585 size += bset->n_ineq * (1 + dim);
10586 size += bset->n_div * (2 + dim);
10588 return size;
10591 int isl_set_size(__isl_keep isl_set *set)
10593 int i;
10594 int size = 0;
10596 if (!set)
10597 return -1;
10599 for (i = 0; i < set->n; ++i)
10600 size += isl_basic_set_size(set->p[i]);
10602 return size;
10605 /* Check if there is any lower bound (if lower == 0) and/or upper
10606 * bound (if upper == 0) on the specified dim.
10608 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10609 enum isl_dim_type type, unsigned pos, int lower, int upper)
10611 int i;
10613 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10614 return isl_bool_error;
10616 pos += isl_basic_map_offset(bmap, type);
10618 for (i = 0; i < bmap->n_div; ++i) {
10619 if (isl_int_is_zero(bmap->div[i][0]))
10620 continue;
10621 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10622 return isl_bool_true;
10625 for (i = 0; i < bmap->n_eq; ++i)
10626 if (!isl_int_is_zero(bmap->eq[i][pos]))
10627 return isl_bool_true;
10629 for (i = 0; i < bmap->n_ineq; ++i) {
10630 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10631 if (sgn > 0)
10632 lower = 1;
10633 if (sgn < 0)
10634 upper = 1;
10637 return lower && upper;
10640 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10641 enum isl_dim_type type, unsigned pos)
10643 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10646 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10647 enum isl_dim_type type, unsigned pos)
10649 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10652 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10653 enum isl_dim_type type, unsigned pos)
10655 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10658 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10659 enum isl_dim_type type, unsigned pos)
10661 int i;
10663 if (!map)
10664 return isl_bool_error;
10666 for (i = 0; i < map->n; ++i) {
10667 isl_bool bounded;
10668 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10669 if (bounded < 0 || !bounded)
10670 return bounded;
10673 return isl_bool_true;
10676 /* Return true if the specified dim is involved in both an upper bound
10677 * and a lower bound.
10679 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10680 enum isl_dim_type type, unsigned pos)
10682 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10685 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10687 static isl_bool has_any_bound(__isl_keep isl_map *map,
10688 enum isl_dim_type type, unsigned pos,
10689 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10690 enum isl_dim_type type, unsigned pos))
10692 int i;
10694 if (!map)
10695 return isl_bool_error;
10697 for (i = 0; i < map->n; ++i) {
10698 isl_bool bounded;
10699 bounded = fn(map->p[i], type, pos);
10700 if (bounded < 0 || bounded)
10701 return bounded;
10704 return isl_bool_false;
10707 /* Return 1 if the specified dim is involved in any lower bound.
10709 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10710 enum isl_dim_type type, unsigned pos)
10712 return has_any_bound(set, type, pos,
10713 &isl_basic_map_dim_has_lower_bound);
10716 /* Return 1 if the specified dim is involved in any upper bound.
10718 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10719 enum isl_dim_type type, unsigned pos)
10721 return has_any_bound(set, type, pos,
10722 &isl_basic_map_dim_has_upper_bound);
10725 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10727 static isl_bool has_bound(__isl_keep isl_map *map,
10728 enum isl_dim_type type, unsigned pos,
10729 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10730 enum isl_dim_type type, unsigned pos))
10732 int i;
10734 if (!map)
10735 return isl_bool_error;
10737 for (i = 0; i < map->n; ++i) {
10738 isl_bool bounded;
10739 bounded = fn(map->p[i], type, pos);
10740 if (bounded < 0 || !bounded)
10741 return bounded;
10744 return isl_bool_true;
10747 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10749 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10750 enum isl_dim_type type, unsigned pos)
10752 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10755 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10757 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10758 enum isl_dim_type type, unsigned pos)
10760 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10763 /* For each of the "n" variables starting at "first", determine
10764 * the sign of the variable and put the results in the first "n"
10765 * elements of the array "signs".
10766 * Sign
10767 * 1 means that the variable is non-negative
10768 * -1 means that the variable is non-positive
10769 * 0 means the variable attains both positive and negative values.
10771 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10772 unsigned first, unsigned n, int *signs)
10774 isl_vec *bound = NULL;
10775 struct isl_tab *tab = NULL;
10776 struct isl_tab_undo *snap;
10777 int i;
10779 if (!bset || !signs)
10780 return -1;
10782 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10783 tab = isl_tab_from_basic_set(bset, 0);
10784 if (!bound || !tab)
10785 goto error;
10787 isl_seq_clr(bound->el, bound->size);
10788 isl_int_set_si(bound->el[0], -1);
10790 snap = isl_tab_snap(tab);
10791 for (i = 0; i < n; ++i) {
10792 int empty;
10794 isl_int_set_si(bound->el[1 + first + i], -1);
10795 if (isl_tab_add_ineq(tab, bound->el) < 0)
10796 goto error;
10797 empty = tab->empty;
10798 isl_int_set_si(bound->el[1 + first + i], 0);
10799 if (isl_tab_rollback(tab, snap) < 0)
10800 goto error;
10802 if (empty) {
10803 signs[i] = 1;
10804 continue;
10807 isl_int_set_si(bound->el[1 + first + i], 1);
10808 if (isl_tab_add_ineq(tab, bound->el) < 0)
10809 goto error;
10810 empty = tab->empty;
10811 isl_int_set_si(bound->el[1 + first + i], 0);
10812 if (isl_tab_rollback(tab, snap) < 0)
10813 goto error;
10815 signs[i] = empty ? -1 : 0;
10818 isl_tab_free(tab);
10819 isl_vec_free(bound);
10820 return 0;
10821 error:
10822 isl_tab_free(tab);
10823 isl_vec_free(bound);
10824 return -1;
10827 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10828 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10830 if (!bset || !signs)
10831 return -1;
10832 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10833 return -1);
10835 first += pos(bset->dim, type) - 1;
10836 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10839 /* Is it possible for the integer division "div" to depend (possibly
10840 * indirectly) on any output dimensions?
10842 * If the div is undefined, then we conservatively assume that it
10843 * may depend on them.
10844 * Otherwise, we check if it actually depends on them or on any integer
10845 * divisions that may depend on them.
10847 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10849 int i;
10850 unsigned n_out, o_out;
10851 unsigned n_div, o_div;
10853 if (isl_int_is_zero(bmap->div[div][0]))
10854 return isl_bool_true;
10856 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10857 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10859 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10860 return isl_bool_true;
10862 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10863 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10865 for (i = 0; i < n_div; ++i) {
10866 isl_bool may_involve;
10868 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10869 continue;
10870 may_involve = div_may_involve_output(bmap, i);
10871 if (may_involve < 0 || may_involve)
10872 return may_involve;
10875 return isl_bool_false;
10878 /* Return the first integer division of "bmap" in the range
10879 * [first, first + n[ that may depend on any output dimensions and
10880 * that has a non-zero coefficient in "c" (where the first coefficient
10881 * in "c" corresponds to integer division "first").
10883 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10884 isl_int *c, int first, int n)
10886 int k;
10888 if (!bmap)
10889 return -1;
10891 for (k = first; k < first + n; ++k) {
10892 isl_bool may_involve;
10894 if (isl_int_is_zero(c[k]))
10895 continue;
10896 may_involve = div_may_involve_output(bmap, k);
10897 if (may_involve < 0)
10898 return -1;
10899 if (may_involve)
10900 return k;
10903 return first + n;
10906 /* Look for a pair of inequality constraints in "bmap" of the form
10908 * -l + i >= 0 or i >= l
10909 * and
10910 * n + l - i >= 0 or i <= l + n
10912 * with n < "m" and i the output dimension at position "pos".
10913 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10914 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10915 * and earlier output dimensions, as well as integer divisions that do
10916 * not involve any of the output dimensions.
10918 * Return the index of the first inequality constraint or bmap->n_ineq
10919 * if no such pair can be found.
10921 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10922 int pos, isl_int m)
10924 int i, j;
10925 isl_ctx *ctx;
10926 unsigned total;
10927 unsigned n_div, o_div;
10928 unsigned n_out, o_out;
10929 int less;
10931 if (!bmap)
10932 return -1;
10934 ctx = isl_basic_map_get_ctx(bmap);
10935 total = isl_basic_map_total_dim(bmap);
10936 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10937 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10938 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10939 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10940 for (i = 0; i < bmap->n_ineq; ++i) {
10941 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10942 continue;
10943 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10944 n_out - (pos + 1)) != -1)
10945 continue;
10946 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10947 0, n_div) < n_div)
10948 continue;
10949 for (j = i + 1; j < bmap->n_ineq; ++j) {
10950 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10951 ctx->one))
10952 continue;
10953 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10954 bmap->ineq[j] + 1, total))
10955 continue;
10956 break;
10958 if (j >= bmap->n_ineq)
10959 continue;
10960 isl_int_add(bmap->ineq[i][0],
10961 bmap->ineq[i][0], bmap->ineq[j][0]);
10962 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10963 isl_int_sub(bmap->ineq[i][0],
10964 bmap->ineq[i][0], bmap->ineq[j][0]);
10965 if (!less)
10966 continue;
10967 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10968 return i;
10969 else
10970 return j;
10973 return bmap->n_ineq;
10976 /* Return the index of the equality of "bmap" that defines
10977 * the output dimension "pos" in terms of earlier dimensions.
10978 * The equality may also involve integer divisions, as long
10979 * as those integer divisions are defined in terms of
10980 * parameters or input dimensions.
10981 * In this case, *div is set to the number of integer divisions and
10982 * *ineq is set to the number of inequality constraints (provided
10983 * div and ineq are not NULL).
10985 * The equality may also involve a single integer division involving
10986 * the output dimensions (typically only output dimension "pos") as
10987 * long as the coefficient of output dimension "pos" is 1 or -1 and
10988 * there is a pair of constraints i >= l and i <= l + n, with i referring
10989 * to output dimension "pos", l an expression involving only earlier
10990 * dimensions and n smaller than the coefficient of the integer division
10991 * in the equality. In this case, the output dimension can be defined
10992 * in terms of a modulo expression that does not involve the integer division.
10993 * *div is then set to this single integer division and
10994 * *ineq is set to the index of constraint i >= l.
10996 * Return bmap->n_eq if there is no such equality.
10997 * Return -1 on error.
10999 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11000 int pos, int *div, int *ineq)
11002 int j, k, l;
11003 unsigned n_out, o_out;
11004 unsigned n_div, o_div;
11006 if (!bmap)
11007 return -1;
11009 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11010 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11011 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11012 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11014 if (ineq)
11015 *ineq = bmap->n_ineq;
11016 if (div)
11017 *div = n_div;
11018 for (j = 0; j < bmap->n_eq; ++j) {
11019 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11020 continue;
11021 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11022 n_out - (pos + 1)) != -1)
11023 continue;
11024 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11025 0, n_div);
11026 if (k >= n_div)
11027 return j;
11028 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11029 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11030 continue;
11031 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11032 k + 1, n_div - (k+1)) < n_div)
11033 continue;
11034 l = find_modulo_constraint_pair(bmap, pos,
11035 bmap->eq[j][o_div + k]);
11036 if (l < 0)
11037 return -1;
11038 if (l >= bmap->n_ineq)
11039 continue;
11040 if (div)
11041 *div = k;
11042 if (ineq)
11043 *ineq = l;
11044 return j;
11047 return bmap->n_eq;
11050 /* Check if the given basic map is obviously single-valued.
11051 * In particular, for each output dimension, check that there is
11052 * an equality that defines the output dimension in terms of
11053 * earlier dimensions.
11055 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11057 int i;
11058 unsigned n_out;
11060 if (!bmap)
11061 return isl_bool_error;
11063 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11065 for (i = 0; i < n_out; ++i) {
11066 int eq;
11068 eq = isl_basic_map_output_defining_equality(bmap, i,
11069 NULL, NULL);
11070 if (eq < 0)
11071 return isl_bool_error;
11072 if (eq >= bmap->n_eq)
11073 return isl_bool_false;
11076 return isl_bool_true;
11079 /* Check if the given basic map is single-valued.
11080 * We simply compute
11082 * M \circ M^-1
11084 * and check if the result is a subset of the identity mapping.
11086 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11088 isl_space *space;
11089 isl_basic_map *test;
11090 isl_basic_map *id;
11091 isl_bool sv;
11093 sv = isl_basic_map_plain_is_single_valued(bmap);
11094 if (sv < 0 || sv)
11095 return sv;
11097 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11098 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11100 space = isl_basic_map_get_space(bmap);
11101 space = isl_space_map_from_set(isl_space_range(space));
11102 id = isl_basic_map_identity(space);
11104 sv = isl_basic_map_is_subset(test, id);
11106 isl_basic_map_free(test);
11107 isl_basic_map_free(id);
11109 return sv;
11112 /* Check if the given map is obviously single-valued.
11114 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11116 if (!map)
11117 return isl_bool_error;
11118 if (map->n == 0)
11119 return isl_bool_true;
11120 if (map->n >= 2)
11121 return isl_bool_false;
11123 return isl_basic_map_plain_is_single_valued(map->p[0]);
11126 /* Check if the given map is single-valued.
11127 * We simply compute
11129 * M \circ M^-1
11131 * and check if the result is a subset of the identity mapping.
11133 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11135 isl_space *dim;
11136 isl_map *test;
11137 isl_map *id;
11138 isl_bool sv;
11140 sv = isl_map_plain_is_single_valued(map);
11141 if (sv < 0 || sv)
11142 return sv;
11144 test = isl_map_reverse(isl_map_copy(map));
11145 test = isl_map_apply_range(test, isl_map_copy(map));
11147 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11148 id = isl_map_identity(dim);
11150 sv = isl_map_is_subset(test, id);
11152 isl_map_free(test);
11153 isl_map_free(id);
11155 return sv;
11158 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11160 isl_bool in;
11162 map = isl_map_copy(map);
11163 map = isl_map_reverse(map);
11164 in = isl_map_is_single_valued(map);
11165 isl_map_free(map);
11167 return in;
11170 /* Check if the given map is obviously injective.
11172 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11174 isl_bool in;
11176 map = isl_map_copy(map);
11177 map = isl_map_reverse(map);
11178 in = isl_map_plain_is_single_valued(map);
11179 isl_map_free(map);
11181 return in;
11184 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11186 isl_bool sv;
11188 sv = isl_map_is_single_valued(map);
11189 if (sv < 0 || !sv)
11190 return sv;
11192 return isl_map_is_injective(map);
11195 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11197 return isl_map_is_single_valued(set_to_map(set));
11200 /* Does "map" only map elements to themselves?
11202 * If the domain and range spaces are different, then "map"
11203 * is considered not to be an identity relation, even if it is empty.
11204 * Otherwise, construct the maximal identity relation and
11205 * check whether "map" is a subset of this relation.
11207 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11209 isl_space *space;
11210 isl_map *id;
11211 isl_bool equal, is_identity;
11213 space = isl_map_get_space(map);
11214 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11215 isl_space_free(space);
11216 if (equal < 0 || !equal)
11217 return equal;
11219 id = isl_map_identity(isl_map_get_space(map));
11220 is_identity = isl_map_is_subset(map, id);
11221 isl_map_free(id);
11223 return is_identity;
11226 int isl_map_is_translation(__isl_keep isl_map *map)
11228 int ok;
11229 isl_set *delta;
11231 delta = isl_map_deltas(isl_map_copy(map));
11232 ok = isl_set_is_singleton(delta);
11233 isl_set_free(delta);
11235 return ok;
11238 static int unique(isl_int *p, unsigned pos, unsigned len)
11240 if (isl_seq_first_non_zero(p, pos) != -1)
11241 return 0;
11242 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11243 return 0;
11244 return 1;
11247 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11249 int i, j;
11250 unsigned nvar;
11251 unsigned ovar;
11253 if (!bset)
11254 return isl_bool_error;
11256 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11257 return isl_bool_false;
11259 nvar = isl_basic_set_dim(bset, isl_dim_set);
11260 ovar = isl_space_offset(bset->dim, isl_dim_set);
11261 for (j = 0; j < nvar; ++j) {
11262 int lower = 0, upper = 0;
11263 for (i = 0; i < bset->n_eq; ++i) {
11264 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11265 continue;
11266 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11267 return isl_bool_false;
11268 break;
11270 if (i < bset->n_eq)
11271 continue;
11272 for (i = 0; i < bset->n_ineq; ++i) {
11273 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11274 continue;
11275 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11276 return isl_bool_false;
11277 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11278 lower = 1;
11279 else
11280 upper = 1;
11282 if (!lower || !upper)
11283 return isl_bool_false;
11286 return isl_bool_true;
11289 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11291 if (!set)
11292 return isl_bool_error;
11293 if (set->n != 1)
11294 return isl_bool_false;
11296 return isl_basic_set_is_box(set->p[0]);
11299 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11301 if (!bset)
11302 return isl_bool_error;
11304 return isl_space_is_wrapping(bset->dim);
11307 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11309 if (!set)
11310 return isl_bool_error;
11312 return isl_space_is_wrapping(set->dim);
11315 /* Modify the space of "map" through a call to "change".
11316 * If "can_change" is set (not NULL), then first call it to check
11317 * if the modification is allowed, printing the error message "cannot_change"
11318 * if it is not.
11320 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11321 isl_bool (*can_change)(__isl_keep isl_map *map),
11322 const char *cannot_change,
11323 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11325 isl_bool ok;
11326 isl_space *space;
11328 if (!map)
11329 return NULL;
11331 ok = can_change ? can_change(map) : isl_bool_true;
11332 if (ok < 0)
11333 return isl_map_free(map);
11334 if (!ok)
11335 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11336 return isl_map_free(map));
11338 space = change(isl_map_get_space(map));
11339 map = isl_map_reset_space(map, space);
11341 return map;
11344 /* Is the domain of "map" a wrapped relation?
11346 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11348 if (!map)
11349 return isl_bool_error;
11351 return isl_space_domain_is_wrapping(map->dim);
11354 /* Is the range of "map" a wrapped relation?
11356 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11358 if (!map)
11359 return isl_bool_error;
11361 return isl_space_range_is_wrapping(map->dim);
11364 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11366 bmap = isl_basic_map_cow(bmap);
11367 if (!bmap)
11368 return NULL;
11370 bmap->dim = isl_space_wrap(bmap->dim);
11371 if (!bmap->dim)
11372 goto error;
11374 bmap = isl_basic_map_finalize(bmap);
11376 return bset_from_bmap(bmap);
11377 error:
11378 isl_basic_map_free(bmap);
11379 return NULL;
11382 /* Given a map A -> B, return the set (A -> B).
11384 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11386 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11389 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11391 bset = isl_basic_set_cow(bset);
11392 if (!bset)
11393 return NULL;
11395 bset->dim = isl_space_unwrap(bset->dim);
11396 if (!bset->dim)
11397 goto error;
11399 bset = isl_basic_set_finalize(bset);
11401 return bset_to_bmap(bset);
11402 error:
11403 isl_basic_set_free(bset);
11404 return NULL;
11407 /* Given a set (A -> B), return the map A -> B.
11408 * Error out if "set" is not of the form (A -> B).
11410 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11412 return isl_map_change_space(set, &isl_set_is_wrapping,
11413 "not a wrapping set", &isl_space_unwrap);
11416 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11417 enum isl_dim_type type)
11419 if (!bmap)
11420 return NULL;
11422 if (!isl_space_is_named_or_nested(bmap->dim, type))
11423 return bmap;
11425 bmap = isl_basic_map_cow(bmap);
11426 if (!bmap)
11427 return NULL;
11429 bmap->dim = isl_space_reset(bmap->dim, type);
11430 if (!bmap->dim)
11431 goto error;
11433 bmap = isl_basic_map_finalize(bmap);
11435 return bmap;
11436 error:
11437 isl_basic_map_free(bmap);
11438 return NULL;
11441 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11442 enum isl_dim_type type)
11444 int i;
11446 if (!map)
11447 return NULL;
11449 if (!isl_space_is_named_or_nested(map->dim, type))
11450 return map;
11452 map = isl_map_cow(map);
11453 if (!map)
11454 return NULL;
11456 for (i = 0; i < map->n; ++i) {
11457 map->p[i] = isl_basic_map_reset(map->p[i], type);
11458 if (!map->p[i])
11459 goto error;
11461 map->dim = isl_space_reset(map->dim, type);
11462 if (!map->dim)
11463 goto error;
11465 return map;
11466 error:
11467 isl_map_free(map);
11468 return NULL;
11471 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11473 if (!bmap)
11474 return NULL;
11476 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11477 return bmap;
11479 bmap = isl_basic_map_cow(bmap);
11480 if (!bmap)
11481 return NULL;
11483 bmap->dim = isl_space_flatten(bmap->dim);
11484 if (!bmap->dim)
11485 goto error;
11487 bmap = isl_basic_map_finalize(bmap);
11489 return bmap;
11490 error:
11491 isl_basic_map_free(bmap);
11492 return NULL;
11495 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11497 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11500 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11501 __isl_take isl_basic_map *bmap)
11503 if (!bmap)
11504 return NULL;
11506 if (!bmap->dim->nested[0])
11507 return bmap;
11509 bmap = isl_basic_map_cow(bmap);
11510 if (!bmap)
11511 return NULL;
11513 bmap->dim = isl_space_flatten_domain(bmap->dim);
11514 if (!bmap->dim)
11515 goto error;
11517 bmap = isl_basic_map_finalize(bmap);
11519 return bmap;
11520 error:
11521 isl_basic_map_free(bmap);
11522 return NULL;
11525 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11526 __isl_take isl_basic_map *bmap)
11528 if (!bmap)
11529 return NULL;
11531 if (!bmap->dim->nested[1])
11532 return bmap;
11534 bmap = isl_basic_map_cow(bmap);
11535 if (!bmap)
11536 return NULL;
11538 bmap->dim = isl_space_flatten_range(bmap->dim);
11539 if (!bmap->dim)
11540 goto error;
11542 bmap = isl_basic_map_finalize(bmap);
11544 return bmap;
11545 error:
11546 isl_basic_map_free(bmap);
11547 return NULL;
11550 /* Remove any internal structure from the spaces of domain and range of "map".
11552 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11554 if (!map)
11555 return NULL;
11557 if (!map->dim->nested[0] && !map->dim->nested[1])
11558 return map;
11560 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11563 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11565 return set_from_map(isl_map_flatten(set_to_map(set)));
11568 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11570 isl_space *dim, *flat_dim;
11571 isl_map *map;
11573 dim = isl_set_get_space(set);
11574 flat_dim = isl_space_flatten(isl_space_copy(dim));
11575 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11576 map = isl_map_intersect_domain(map, set);
11578 return map;
11581 /* Remove any internal structure from the space of the domain of "map".
11583 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11585 if (!map)
11586 return NULL;
11588 if (!map->dim->nested[0])
11589 return map;
11591 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11594 /* Remove any internal structure from the space of the range of "map".
11596 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11598 if (!map)
11599 return NULL;
11601 if (!map->dim->nested[1])
11602 return map;
11604 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11607 /* Reorder the dimensions of "bmap" according to the given dim_map
11608 * and set the dimension specification to "dim" and
11609 * perform Gaussian elimination on the result.
11611 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11612 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11614 isl_basic_map *res;
11615 unsigned flags;
11617 bmap = isl_basic_map_cow(bmap);
11618 if (!bmap || !dim || !dim_map)
11619 goto error;
11621 flags = bmap->flags;
11622 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11623 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11624 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11625 res = isl_basic_map_alloc_space(dim,
11626 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11627 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11628 if (res)
11629 res->flags = flags;
11630 res = isl_basic_map_gauss(res, NULL);
11631 res = isl_basic_map_finalize(res);
11632 return res;
11633 error:
11634 free(dim_map);
11635 isl_basic_map_free(bmap);
11636 isl_space_free(dim);
11637 return NULL;
11640 /* Reorder the dimensions of "map" according to given reordering.
11642 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11643 __isl_take isl_reordering *r)
11645 int i;
11646 struct isl_dim_map *dim_map;
11648 map = isl_map_cow(map);
11649 dim_map = isl_dim_map_from_reordering(r);
11650 if (!map || !r || !dim_map)
11651 goto error;
11653 for (i = 0; i < map->n; ++i) {
11654 struct isl_dim_map *dim_map_i;
11656 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11658 map->p[i] = isl_basic_map_realign(map->p[i],
11659 isl_space_copy(r->dim), dim_map_i);
11661 if (!map->p[i])
11662 goto error;
11665 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11667 isl_reordering_free(r);
11668 free(dim_map);
11669 return map;
11670 error:
11671 free(dim_map);
11672 isl_map_free(map);
11673 isl_reordering_free(r);
11674 return NULL;
11677 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11678 __isl_take isl_reordering *r)
11680 return set_from_map(isl_map_realign(set_to_map(set), r));
11683 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11684 __isl_take isl_space *model)
11686 isl_ctx *ctx;
11688 if (!map || !model)
11689 goto error;
11691 ctx = isl_space_get_ctx(model);
11692 if (!isl_space_has_named_params(model))
11693 isl_die(ctx, isl_error_invalid,
11694 "model has unnamed parameters", goto error);
11695 if (!isl_space_has_named_params(map->dim))
11696 isl_die(ctx, isl_error_invalid,
11697 "relation has unnamed parameters", goto error);
11698 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11699 isl_reordering *exp;
11701 model = isl_space_drop_dims(model, isl_dim_in,
11702 0, isl_space_dim(model, isl_dim_in));
11703 model = isl_space_drop_dims(model, isl_dim_out,
11704 0, isl_space_dim(model, isl_dim_out));
11705 exp = isl_parameter_alignment_reordering(map->dim, model);
11706 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11707 map = isl_map_realign(map, exp);
11710 isl_space_free(model);
11711 return map;
11712 error:
11713 isl_space_free(model);
11714 isl_map_free(map);
11715 return NULL;
11718 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11719 __isl_take isl_space *model)
11721 return isl_map_align_params(set, model);
11724 /* Align the parameters of "bmap" to those of "model", introducing
11725 * additional parameters if needed.
11727 __isl_give isl_basic_map *isl_basic_map_align_params(
11728 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11730 isl_ctx *ctx;
11732 if (!bmap || !model)
11733 goto error;
11735 ctx = isl_space_get_ctx(model);
11736 if (!isl_space_has_named_params(model))
11737 isl_die(ctx, isl_error_invalid,
11738 "model has unnamed parameters", goto error);
11739 if (!isl_space_has_named_params(bmap->dim))
11740 isl_die(ctx, isl_error_invalid,
11741 "relation has unnamed parameters", goto error);
11742 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11743 isl_reordering *exp;
11744 struct isl_dim_map *dim_map;
11746 model = isl_space_drop_dims(model, isl_dim_in,
11747 0, isl_space_dim(model, isl_dim_in));
11748 model = isl_space_drop_dims(model, isl_dim_out,
11749 0, isl_space_dim(model, isl_dim_out));
11750 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11751 exp = isl_reordering_extend_space(exp,
11752 isl_basic_map_get_space(bmap));
11753 dim_map = isl_dim_map_from_reordering(exp);
11754 bmap = isl_basic_map_realign(bmap,
11755 exp ? isl_space_copy(exp->dim) : NULL,
11756 isl_dim_map_extend(dim_map, bmap));
11757 isl_reordering_free(exp);
11758 free(dim_map);
11761 isl_space_free(model);
11762 return bmap;
11763 error:
11764 isl_space_free(model);
11765 isl_basic_map_free(bmap);
11766 return NULL;
11769 /* Align the parameters of "bset" to those of "model", introducing
11770 * additional parameters if needed.
11772 __isl_give isl_basic_set *isl_basic_set_align_params(
11773 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11775 return isl_basic_map_align_params(bset, model);
11778 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11779 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11780 enum isl_dim_type c2, enum isl_dim_type c3,
11781 enum isl_dim_type c4, enum isl_dim_type c5)
11783 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11784 struct isl_mat *mat;
11785 int i, j, k;
11786 int pos;
11788 if (!bmap)
11789 return NULL;
11790 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11791 isl_basic_map_total_dim(bmap) + 1);
11792 if (!mat)
11793 return NULL;
11794 for (i = 0; i < bmap->n_eq; ++i)
11795 for (j = 0, pos = 0; j < 5; ++j) {
11796 int off = isl_basic_map_offset(bmap, c[j]);
11797 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11798 isl_int_set(mat->row[i][pos],
11799 bmap->eq[i][off + k]);
11800 ++pos;
11804 return mat;
11807 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11808 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11809 enum isl_dim_type c2, enum isl_dim_type c3,
11810 enum isl_dim_type c4, enum isl_dim_type c5)
11812 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11813 struct isl_mat *mat;
11814 int i, j, k;
11815 int pos;
11817 if (!bmap)
11818 return NULL;
11819 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11820 isl_basic_map_total_dim(bmap) + 1);
11821 if (!mat)
11822 return NULL;
11823 for (i = 0; i < bmap->n_ineq; ++i)
11824 for (j = 0, pos = 0; j < 5; ++j) {
11825 int off = isl_basic_map_offset(bmap, c[j]);
11826 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11827 isl_int_set(mat->row[i][pos],
11828 bmap->ineq[i][off + k]);
11829 ++pos;
11833 return mat;
11836 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11837 __isl_take isl_space *dim,
11838 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11839 enum isl_dim_type c2, enum isl_dim_type c3,
11840 enum isl_dim_type c4, enum isl_dim_type c5)
11842 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11843 isl_basic_map *bmap;
11844 unsigned total;
11845 unsigned extra;
11846 int i, j, k, l;
11847 int pos;
11849 if (!dim || !eq || !ineq)
11850 goto error;
11852 if (eq->n_col != ineq->n_col)
11853 isl_die(dim->ctx, isl_error_invalid,
11854 "equalities and inequalities matrices should have "
11855 "same number of columns", goto error);
11857 total = 1 + isl_space_dim(dim, isl_dim_all);
11859 if (eq->n_col < total)
11860 isl_die(dim->ctx, isl_error_invalid,
11861 "number of columns too small", goto error);
11863 extra = eq->n_col - total;
11865 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11866 eq->n_row, ineq->n_row);
11867 if (!bmap)
11868 goto error;
11869 for (i = 0; i < extra; ++i) {
11870 k = isl_basic_map_alloc_div(bmap);
11871 if (k < 0)
11872 goto error;
11873 isl_int_set_si(bmap->div[k][0], 0);
11875 for (i = 0; i < eq->n_row; ++i) {
11876 l = isl_basic_map_alloc_equality(bmap);
11877 if (l < 0)
11878 goto error;
11879 for (j = 0, pos = 0; j < 5; ++j) {
11880 int off = isl_basic_map_offset(bmap, c[j]);
11881 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11882 isl_int_set(bmap->eq[l][off + k],
11883 eq->row[i][pos]);
11884 ++pos;
11888 for (i = 0; i < ineq->n_row; ++i) {
11889 l = isl_basic_map_alloc_inequality(bmap);
11890 if (l < 0)
11891 goto error;
11892 for (j = 0, pos = 0; j < 5; ++j) {
11893 int off = isl_basic_map_offset(bmap, c[j]);
11894 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11895 isl_int_set(bmap->ineq[l][off + k],
11896 ineq->row[i][pos]);
11897 ++pos;
11902 isl_space_free(dim);
11903 isl_mat_free(eq);
11904 isl_mat_free(ineq);
11906 bmap = isl_basic_map_simplify(bmap);
11907 return isl_basic_map_finalize(bmap);
11908 error:
11909 isl_space_free(dim);
11910 isl_mat_free(eq);
11911 isl_mat_free(ineq);
11912 return NULL;
11915 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11916 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11917 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11919 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11920 c1, c2, c3, c4, isl_dim_in);
11923 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11924 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11925 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11927 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11928 c1, c2, c3, c4, isl_dim_in);
11931 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11932 __isl_take isl_space *dim,
11933 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11934 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11936 isl_basic_map *bmap;
11937 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11938 c1, c2, c3, c4, isl_dim_in);
11939 return bset_from_bmap(bmap);
11942 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11944 if (!bmap)
11945 return isl_bool_error;
11947 return isl_space_can_zip(bmap->dim);
11950 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11952 if (!map)
11953 return isl_bool_error;
11955 return isl_space_can_zip(map->dim);
11958 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11959 * (A -> C) -> (B -> D).
11961 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11963 unsigned pos;
11964 unsigned n1;
11965 unsigned n2;
11967 if (!bmap)
11968 return NULL;
11970 if (!isl_basic_map_can_zip(bmap))
11971 isl_die(bmap->ctx, isl_error_invalid,
11972 "basic map cannot be zipped", goto error);
11973 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11974 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11975 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11976 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11977 bmap = isl_basic_map_cow(bmap);
11978 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11979 if (!bmap)
11980 return NULL;
11981 bmap->dim = isl_space_zip(bmap->dim);
11982 if (!bmap->dim)
11983 goto error;
11984 bmap = isl_basic_map_mark_final(bmap);
11985 return bmap;
11986 error:
11987 isl_basic_map_free(bmap);
11988 return NULL;
11991 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11992 * (A -> C) -> (B -> D).
11994 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11996 int i;
11998 if (!map)
11999 return NULL;
12001 if (!isl_map_can_zip(map))
12002 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12003 goto error);
12005 map = isl_map_cow(map);
12006 if (!map)
12007 return NULL;
12009 for (i = 0; i < map->n; ++i) {
12010 map->p[i] = isl_basic_map_zip(map->p[i]);
12011 if (!map->p[i])
12012 goto error;
12015 map->dim = isl_space_zip(map->dim);
12016 if (!map->dim)
12017 goto error;
12019 return map;
12020 error:
12021 isl_map_free(map);
12022 return NULL;
12025 /* Can we apply isl_basic_map_curry to "bmap"?
12026 * That is, does it have a nested relation in its domain?
12028 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12030 if (!bmap)
12031 return isl_bool_error;
12033 return isl_space_can_curry(bmap->dim);
12036 /* Can we apply isl_map_curry to "map"?
12037 * That is, does it have a nested relation in its domain?
12039 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12041 if (!map)
12042 return isl_bool_error;
12044 return isl_space_can_curry(map->dim);
12047 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12048 * A -> (B -> C).
12050 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12053 if (!bmap)
12054 return NULL;
12056 if (!isl_basic_map_can_curry(bmap))
12057 isl_die(bmap->ctx, isl_error_invalid,
12058 "basic map cannot be curried", goto error);
12059 bmap = isl_basic_map_cow(bmap);
12060 if (!bmap)
12061 return NULL;
12062 bmap->dim = isl_space_curry(bmap->dim);
12063 if (!bmap->dim)
12064 goto error;
12065 bmap = isl_basic_map_mark_final(bmap);
12066 return bmap;
12067 error:
12068 isl_basic_map_free(bmap);
12069 return NULL;
12072 /* Given a map (A -> B) -> C, return the corresponding map
12073 * A -> (B -> C).
12075 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12077 return isl_map_change_space(map, &isl_map_can_curry,
12078 "map cannot be curried", &isl_space_curry);
12081 /* Can isl_map_range_curry be applied to "map"?
12082 * That is, does it have a nested relation in its range,
12083 * the domain of which is itself a nested relation?
12085 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12087 if (!map)
12088 return isl_bool_error;
12090 return isl_space_can_range_curry(map->dim);
12093 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12094 * A -> (B -> (C -> D)).
12096 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12098 return isl_map_change_space(map, &isl_map_can_range_curry,
12099 "map range cannot be curried",
12100 &isl_space_range_curry);
12103 /* Can we apply isl_basic_map_uncurry to "bmap"?
12104 * That is, does it have a nested relation in its domain?
12106 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12108 if (!bmap)
12109 return isl_bool_error;
12111 return isl_space_can_uncurry(bmap->dim);
12114 /* Can we apply isl_map_uncurry to "map"?
12115 * That is, does it have a nested relation in its domain?
12117 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12119 if (!map)
12120 return isl_bool_error;
12122 return isl_space_can_uncurry(map->dim);
12125 /* Given a basic map A -> (B -> C), return the corresponding basic map
12126 * (A -> B) -> C.
12128 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12131 if (!bmap)
12132 return NULL;
12134 if (!isl_basic_map_can_uncurry(bmap))
12135 isl_die(bmap->ctx, isl_error_invalid,
12136 "basic map cannot be uncurried",
12137 return isl_basic_map_free(bmap));
12138 bmap = isl_basic_map_cow(bmap);
12139 if (!bmap)
12140 return NULL;
12141 bmap->dim = isl_space_uncurry(bmap->dim);
12142 if (!bmap->dim)
12143 return isl_basic_map_free(bmap);
12144 bmap = isl_basic_map_mark_final(bmap);
12145 return bmap;
12148 /* Given a map A -> (B -> C), return the corresponding map
12149 * (A -> B) -> C.
12151 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12153 return isl_map_change_space(map, &isl_map_can_uncurry,
12154 "map cannot be uncurried", &isl_space_uncurry);
12157 /* Construct a basic map mapping the domain of the affine expression
12158 * to a one-dimensional range prescribed by the affine expression.
12159 * If "rational" is set, then construct a rational basic map.
12161 * A NaN affine expression cannot be converted to a basic map.
12163 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12164 __isl_take isl_aff *aff, int rational)
12166 int k;
12167 int pos;
12168 isl_bool is_nan;
12169 isl_local_space *ls;
12170 isl_basic_map *bmap = NULL;
12172 if (!aff)
12173 return NULL;
12174 is_nan = isl_aff_is_nan(aff);
12175 if (is_nan < 0)
12176 goto error;
12177 if (is_nan)
12178 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12179 "cannot convert NaN", goto error);
12181 ls = isl_aff_get_local_space(aff);
12182 bmap = isl_basic_map_from_local_space(ls);
12183 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12184 k = isl_basic_map_alloc_equality(bmap);
12185 if (k < 0)
12186 goto error;
12188 pos = isl_basic_map_offset(bmap, isl_dim_out);
12189 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12190 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12191 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12192 aff->v->size - (pos + 1));
12194 isl_aff_free(aff);
12195 if (rational)
12196 bmap = isl_basic_map_set_rational(bmap);
12197 bmap = isl_basic_map_finalize(bmap);
12198 return bmap;
12199 error:
12200 isl_aff_free(aff);
12201 isl_basic_map_free(bmap);
12202 return NULL;
12205 /* Construct a basic map mapping the domain of the affine expression
12206 * to a one-dimensional range prescribed by the affine expression.
12208 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12210 return isl_basic_map_from_aff2(aff, 0);
12213 /* Construct a map mapping the domain of the affine expression
12214 * to a one-dimensional range prescribed by the affine expression.
12216 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12218 isl_basic_map *bmap;
12220 bmap = isl_basic_map_from_aff(aff);
12221 return isl_map_from_basic_map(bmap);
12224 /* Construct a basic map mapping the domain the multi-affine expression
12225 * to its range, with each dimension in the range equated to the
12226 * corresponding affine expression.
12227 * If "rational" is set, then construct a rational basic map.
12229 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12230 __isl_take isl_multi_aff *maff, int rational)
12232 int i;
12233 isl_space *space;
12234 isl_basic_map *bmap;
12236 if (!maff)
12237 return NULL;
12239 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12240 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12241 "invalid space", goto error);
12243 space = isl_space_domain(isl_multi_aff_get_space(maff));
12244 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12245 if (rational)
12246 bmap = isl_basic_map_set_rational(bmap);
12248 for (i = 0; i < maff->n; ++i) {
12249 isl_aff *aff;
12250 isl_basic_map *bmap_i;
12252 aff = isl_aff_copy(maff->p[i]);
12253 bmap_i = isl_basic_map_from_aff2(aff, rational);
12255 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12258 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12260 isl_multi_aff_free(maff);
12261 return bmap;
12262 error:
12263 isl_multi_aff_free(maff);
12264 return NULL;
12267 /* Construct a basic map mapping the domain the multi-affine expression
12268 * to its range, with each dimension in the range equated to the
12269 * corresponding affine expression.
12271 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12272 __isl_take isl_multi_aff *ma)
12274 return isl_basic_map_from_multi_aff2(ma, 0);
12277 /* Construct a map mapping the domain the multi-affine expression
12278 * to its range, with each dimension in the range equated to the
12279 * corresponding affine expression.
12281 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12283 isl_basic_map *bmap;
12285 bmap = isl_basic_map_from_multi_aff(maff);
12286 return isl_map_from_basic_map(bmap);
12289 /* Construct a basic map mapping a domain in the given space to
12290 * to an n-dimensional range, with n the number of elements in the list,
12291 * where each coordinate in the range is prescribed by the
12292 * corresponding affine expression.
12293 * The domains of all affine expressions in the list are assumed to match
12294 * domain_dim.
12296 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12297 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12299 int i;
12300 isl_space *dim;
12301 isl_basic_map *bmap;
12303 if (!list)
12304 return NULL;
12306 dim = isl_space_from_domain(domain_dim);
12307 bmap = isl_basic_map_universe(dim);
12309 for (i = 0; i < list->n; ++i) {
12310 isl_aff *aff;
12311 isl_basic_map *bmap_i;
12313 aff = isl_aff_copy(list->p[i]);
12314 bmap_i = isl_basic_map_from_aff(aff);
12316 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12319 isl_aff_list_free(list);
12320 return bmap;
12323 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12324 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12326 return isl_map_equate(set, type1, pos1, type2, pos2);
12329 /* Construct a basic map where the given dimensions are equal to each other.
12331 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12332 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12334 isl_basic_map *bmap = NULL;
12335 int i;
12337 if (!space)
12338 return NULL;
12340 if (pos1 >= isl_space_dim(space, type1))
12341 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12342 "index out of bounds", goto error);
12343 if (pos2 >= isl_space_dim(space, type2))
12344 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12345 "index out of bounds", goto error);
12347 if (type1 == type2 && pos1 == pos2)
12348 return isl_basic_map_universe(space);
12350 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12351 i = isl_basic_map_alloc_equality(bmap);
12352 if (i < 0)
12353 goto error;
12354 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12355 pos1 += isl_basic_map_offset(bmap, type1);
12356 pos2 += isl_basic_map_offset(bmap, type2);
12357 isl_int_set_si(bmap->eq[i][pos1], -1);
12358 isl_int_set_si(bmap->eq[i][pos2], 1);
12359 bmap = isl_basic_map_finalize(bmap);
12360 isl_space_free(space);
12361 return bmap;
12362 error:
12363 isl_space_free(space);
12364 isl_basic_map_free(bmap);
12365 return NULL;
12368 /* Add a constraint imposing that the given two dimensions are equal.
12370 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12371 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12373 isl_basic_map *eq;
12375 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12377 bmap = isl_basic_map_intersect(bmap, eq);
12379 return bmap;
12382 /* Add a constraint imposing that the given two dimensions are equal.
12384 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12385 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12387 isl_basic_map *bmap;
12389 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12391 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12393 return map;
12396 /* Add a constraint imposing that the given two dimensions have opposite values.
12398 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12399 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12401 isl_basic_map *bmap = NULL;
12402 int i;
12404 if (!map)
12405 return NULL;
12407 if (pos1 >= isl_map_dim(map, type1))
12408 isl_die(map->ctx, isl_error_invalid,
12409 "index out of bounds", goto error);
12410 if (pos2 >= isl_map_dim(map, type2))
12411 isl_die(map->ctx, isl_error_invalid,
12412 "index out of bounds", goto error);
12414 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12415 i = isl_basic_map_alloc_equality(bmap);
12416 if (i < 0)
12417 goto error;
12418 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12419 pos1 += isl_basic_map_offset(bmap, type1);
12420 pos2 += isl_basic_map_offset(bmap, type2);
12421 isl_int_set_si(bmap->eq[i][pos1], 1);
12422 isl_int_set_si(bmap->eq[i][pos2], 1);
12423 bmap = isl_basic_map_finalize(bmap);
12425 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12427 return map;
12428 error:
12429 isl_basic_map_free(bmap);
12430 isl_map_free(map);
12431 return NULL;
12434 /* Construct a constraint imposing that the value of the first dimension is
12435 * greater than or equal to that of the second.
12437 static __isl_give isl_constraint *constraint_order_ge(
12438 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12439 enum isl_dim_type type2, int pos2)
12441 isl_constraint *c;
12443 if (!space)
12444 return NULL;
12446 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12448 if (pos1 >= isl_constraint_dim(c, type1))
12449 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12450 "index out of bounds", return isl_constraint_free(c));
12451 if (pos2 >= isl_constraint_dim(c, type2))
12452 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12453 "index out of bounds", return isl_constraint_free(c));
12455 if (type1 == type2 && pos1 == pos2)
12456 return c;
12458 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12459 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12461 return c;
12464 /* Add a constraint imposing that the value of the first dimension is
12465 * greater than or equal to that of the second.
12467 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12468 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12470 isl_constraint *c;
12471 isl_space *space;
12473 if (type1 == type2 && pos1 == pos2)
12474 return bmap;
12475 space = isl_basic_map_get_space(bmap);
12476 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12477 bmap = isl_basic_map_add_constraint(bmap, c);
12479 return bmap;
12482 /* Add a constraint imposing that the value of the first dimension is
12483 * greater than or equal to that of the second.
12485 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12486 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12488 isl_constraint *c;
12489 isl_space *space;
12491 if (type1 == type2 && pos1 == pos2)
12492 return map;
12493 space = isl_map_get_space(map);
12494 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12495 map = isl_map_add_constraint(map, c);
12497 return map;
12500 /* Add a constraint imposing that the value of the first dimension is
12501 * less than or equal to that of the second.
12503 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12504 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12506 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12509 /* Construct a basic map where the value of the first dimension is
12510 * greater than that of the second.
12512 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12513 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12515 isl_basic_map *bmap = NULL;
12516 int i;
12518 if (!space)
12519 return NULL;
12521 if (pos1 >= isl_space_dim(space, type1))
12522 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12523 "index out of bounds", goto error);
12524 if (pos2 >= isl_space_dim(space, type2))
12525 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12526 "index out of bounds", goto error);
12528 if (type1 == type2 && pos1 == pos2)
12529 return isl_basic_map_empty(space);
12531 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12532 i = isl_basic_map_alloc_inequality(bmap);
12533 if (i < 0)
12534 return isl_basic_map_free(bmap);
12535 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12536 pos1 += isl_basic_map_offset(bmap, type1);
12537 pos2 += isl_basic_map_offset(bmap, type2);
12538 isl_int_set_si(bmap->ineq[i][pos1], 1);
12539 isl_int_set_si(bmap->ineq[i][pos2], -1);
12540 isl_int_set_si(bmap->ineq[i][0], -1);
12541 bmap = isl_basic_map_finalize(bmap);
12543 return bmap;
12544 error:
12545 isl_space_free(space);
12546 isl_basic_map_free(bmap);
12547 return NULL;
12550 /* Add a constraint imposing that the value of the first dimension is
12551 * greater than that of the second.
12553 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12554 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12556 isl_basic_map *gt;
12558 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12560 bmap = isl_basic_map_intersect(bmap, gt);
12562 return bmap;
12565 /* Add a constraint imposing that the value of the first dimension is
12566 * greater than that of the second.
12568 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12569 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12571 isl_basic_map *bmap;
12573 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12575 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12577 return map;
12580 /* Add a constraint imposing that the value of the first dimension is
12581 * smaller than that of the second.
12583 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12584 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12586 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12589 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12590 int pos)
12592 isl_aff *div;
12593 isl_local_space *ls;
12595 if (!bmap)
12596 return NULL;
12598 if (!isl_basic_map_divs_known(bmap))
12599 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12600 "some divs are unknown", return NULL);
12602 ls = isl_basic_map_get_local_space(bmap);
12603 div = isl_local_space_get_div(ls, pos);
12604 isl_local_space_free(ls);
12606 return div;
12609 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12610 int pos)
12612 return isl_basic_map_get_div(bset, pos);
12615 /* Plug in "subs" for dimension "type", "pos" of "bset".
12617 * Let i be the dimension to replace and let "subs" be of the form
12619 * f/d
12621 * Any integer division with a non-zero coefficient for i,
12623 * floor((a i + g)/m)
12625 * is replaced by
12627 * floor((a f + d g)/(m d))
12629 * Constraints of the form
12631 * a i + g
12633 * are replaced by
12635 * a f + d g
12637 * We currently require that "subs" is an integral expression.
12638 * Handling rational expressions may require us to add stride constraints
12639 * as we do in isl_basic_set_preimage_multi_aff.
12641 __isl_give isl_basic_set *isl_basic_set_substitute(
12642 __isl_take isl_basic_set *bset,
12643 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12645 int i;
12646 isl_int v;
12647 isl_ctx *ctx;
12649 if (bset && isl_basic_set_plain_is_empty(bset))
12650 return bset;
12652 bset = isl_basic_set_cow(bset);
12653 if (!bset || !subs)
12654 goto error;
12656 ctx = isl_basic_set_get_ctx(bset);
12657 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12658 isl_die(ctx, isl_error_invalid,
12659 "spaces don't match", goto error);
12660 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12661 isl_die(ctx, isl_error_unsupported,
12662 "cannot handle divs yet", goto error);
12663 if (!isl_int_is_one(subs->v->el[0]))
12664 isl_die(ctx, isl_error_invalid,
12665 "can only substitute integer expressions", goto error);
12667 pos += isl_basic_set_offset(bset, type);
12669 isl_int_init(v);
12671 for (i = 0; i < bset->n_eq; ++i) {
12672 if (isl_int_is_zero(bset->eq[i][pos]))
12673 continue;
12674 isl_int_set(v, bset->eq[i][pos]);
12675 isl_int_set_si(bset->eq[i][pos], 0);
12676 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12677 v, subs->v->el + 1, subs->v->size - 1);
12680 for (i = 0; i < bset->n_ineq; ++i) {
12681 if (isl_int_is_zero(bset->ineq[i][pos]))
12682 continue;
12683 isl_int_set(v, bset->ineq[i][pos]);
12684 isl_int_set_si(bset->ineq[i][pos], 0);
12685 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12686 v, subs->v->el + 1, subs->v->size - 1);
12689 for (i = 0; i < bset->n_div; ++i) {
12690 if (isl_int_is_zero(bset->div[i][1 + pos]))
12691 continue;
12692 isl_int_set(v, bset->div[i][1 + pos]);
12693 isl_int_set_si(bset->div[i][1 + pos], 0);
12694 isl_seq_combine(bset->div[i] + 1,
12695 subs->v->el[0], bset->div[i] + 1,
12696 v, subs->v->el + 1, subs->v->size - 1);
12697 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12700 isl_int_clear(v);
12702 bset = isl_basic_set_simplify(bset);
12703 return isl_basic_set_finalize(bset);
12704 error:
12705 isl_basic_set_free(bset);
12706 return NULL;
12709 /* Plug in "subs" for dimension "type", "pos" of "set".
12711 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12712 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12714 int i;
12716 if (set && isl_set_plain_is_empty(set))
12717 return set;
12719 set = isl_set_cow(set);
12720 if (!set || !subs)
12721 goto error;
12723 for (i = set->n - 1; i >= 0; --i) {
12724 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12725 if (remove_if_empty(set, i) < 0)
12726 goto error;
12729 return set;
12730 error:
12731 isl_set_free(set);
12732 return NULL;
12735 /* Check if the range of "ma" is compatible with the domain or range
12736 * (depending on "type") of "bmap".
12737 * Return -1 if anything is wrong.
12739 static int check_basic_map_compatible_range_multi_aff(
12740 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12741 __isl_keep isl_multi_aff *ma)
12743 int m;
12744 isl_space *ma_space;
12746 ma_space = isl_multi_aff_get_space(ma);
12748 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12749 if (m < 0)
12750 goto error;
12751 if (!m)
12752 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12753 "parameters don't match", goto error);
12754 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12755 if (m < 0)
12756 goto error;
12757 if (!m)
12758 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12759 "spaces don't match", goto error);
12761 isl_space_free(ma_space);
12762 return m;
12763 error:
12764 isl_space_free(ma_space);
12765 return -1;
12768 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12769 * coefficients before the transformed range of dimensions,
12770 * the "n_after" coefficients after the transformed range of dimensions
12771 * and the coefficients of the other divs in "bmap".
12773 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12774 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12776 int i;
12777 int n_param;
12778 int n_set;
12779 isl_local_space *ls;
12781 if (n_div == 0)
12782 return 0;
12784 ls = isl_aff_get_domain_local_space(ma->p[0]);
12785 if (!ls)
12786 return -1;
12788 n_param = isl_local_space_dim(ls, isl_dim_param);
12789 n_set = isl_local_space_dim(ls, isl_dim_set);
12790 for (i = 0; i < n_div; ++i) {
12791 int o_bmap = 0, o_ls = 0;
12793 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12794 o_bmap += 1 + 1 + n_param;
12795 o_ls += 1 + 1 + n_param;
12796 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12797 o_bmap += n_before;
12798 isl_seq_cpy(bmap->div[i] + o_bmap,
12799 ls->div->row[i] + o_ls, n_set);
12800 o_bmap += n_set;
12801 o_ls += n_set;
12802 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12803 o_bmap += n_after;
12804 isl_seq_cpy(bmap->div[i] + o_bmap,
12805 ls->div->row[i] + o_ls, n_div);
12806 o_bmap += n_div;
12807 o_ls += n_div;
12808 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12809 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12810 goto error;
12813 isl_local_space_free(ls);
12814 return 0;
12815 error:
12816 isl_local_space_free(ls);
12817 return -1;
12820 /* How many stride constraints does "ma" enforce?
12821 * That is, how many of the affine expressions have a denominator
12822 * different from one?
12824 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12826 int i;
12827 int strides = 0;
12829 for (i = 0; i < ma->n; ++i)
12830 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12831 strides++;
12833 return strides;
12836 /* For each affine expression in ma of the form
12838 * x_i = (f_i y + h_i)/m_i
12840 * with m_i different from one, add a constraint to "bmap"
12841 * of the form
12843 * f_i y + h_i = m_i alpha_i
12845 * with alpha_i an additional existentially quantified variable.
12847 * The input variables of "ma" correspond to a subset of the variables
12848 * of "bmap". There are "n_before" variables in "bmap" before this
12849 * subset and "n_after" variables after this subset.
12850 * The integer divisions of the affine expressions in "ma" are assumed
12851 * to have been aligned. There are "n_div_ma" of them and
12852 * they appear first in "bmap", straight after the "n_after" variables.
12854 static __isl_give isl_basic_map *add_ma_strides(
12855 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12856 int n_before, int n_after, int n_div_ma)
12858 int i, k;
12859 int div;
12860 int total;
12861 int n_param;
12862 int n_in;
12864 total = isl_basic_map_total_dim(bmap);
12865 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12866 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12867 for (i = 0; i < ma->n; ++i) {
12868 int o_bmap = 0, o_ma = 1;
12870 if (isl_int_is_one(ma->p[i]->v->el[0]))
12871 continue;
12872 div = isl_basic_map_alloc_div(bmap);
12873 k = isl_basic_map_alloc_equality(bmap);
12874 if (div < 0 || k < 0)
12875 goto error;
12876 isl_int_set_si(bmap->div[div][0], 0);
12877 isl_seq_cpy(bmap->eq[k] + o_bmap,
12878 ma->p[i]->v->el + o_ma, 1 + n_param);
12879 o_bmap += 1 + n_param;
12880 o_ma += 1 + n_param;
12881 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12882 o_bmap += n_before;
12883 isl_seq_cpy(bmap->eq[k] + o_bmap,
12884 ma->p[i]->v->el + o_ma, n_in);
12885 o_bmap += n_in;
12886 o_ma += n_in;
12887 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12888 o_bmap += n_after;
12889 isl_seq_cpy(bmap->eq[k] + o_bmap,
12890 ma->p[i]->v->el + o_ma, n_div_ma);
12891 o_bmap += n_div_ma;
12892 o_ma += n_div_ma;
12893 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12894 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12895 total++;
12898 return bmap;
12899 error:
12900 isl_basic_map_free(bmap);
12901 return NULL;
12904 /* Replace the domain or range space (depending on "type) of "space" by "set".
12906 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12907 enum isl_dim_type type, __isl_take isl_space *set)
12909 if (type == isl_dim_in) {
12910 space = isl_space_range(space);
12911 space = isl_space_map_from_domain_and_range(set, space);
12912 } else {
12913 space = isl_space_domain(space);
12914 space = isl_space_map_from_domain_and_range(space, set);
12917 return space;
12920 /* Compute the preimage of the domain or range (depending on "type")
12921 * of "bmap" under the function represented by "ma".
12922 * In other words, plug in "ma" in the domain or range of "bmap".
12923 * The result is a basic map that lives in the same space as "bmap"
12924 * except that the domain or range has been replaced by
12925 * the domain space of "ma".
12927 * If bmap is represented by
12929 * A(p) + S u + B x + T v + C(divs) >= 0,
12931 * where u and x are input and output dimensions if type == isl_dim_out
12932 * while x and v are input and output dimensions if type == isl_dim_in,
12933 * and ma is represented by
12935 * x = D(p) + F(y) + G(divs')
12937 * then the result is
12939 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12941 * The divs in the input set are similarly adjusted.
12942 * In particular
12944 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12946 * becomes
12948 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12949 * B_i G(divs') + c_i(divs))/n_i)
12951 * If bmap is not a rational map and if F(y) involves any denominators
12953 * x_i = (f_i y + h_i)/m_i
12955 * then additional constraints are added to ensure that we only
12956 * map back integer points. That is we enforce
12958 * f_i y + h_i = m_i alpha_i
12960 * with alpha_i an additional existentially quantified variable.
12962 * We first copy over the divs from "ma".
12963 * Then we add the modified constraints and divs from "bmap".
12964 * Finally, we add the stride constraints, if needed.
12966 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12967 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12968 __isl_take isl_multi_aff *ma)
12970 int i, k;
12971 isl_space *space;
12972 isl_basic_map *res = NULL;
12973 int n_before, n_after, n_div_bmap, n_div_ma;
12974 isl_int f, c1, c2, g;
12975 int rational, strides;
12977 isl_int_init(f);
12978 isl_int_init(c1);
12979 isl_int_init(c2);
12980 isl_int_init(g);
12982 ma = isl_multi_aff_align_divs(ma);
12983 if (!bmap || !ma)
12984 goto error;
12985 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12986 goto error;
12988 if (type == isl_dim_in) {
12989 n_before = 0;
12990 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12991 } else {
12992 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12993 n_after = 0;
12995 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12996 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12998 space = isl_multi_aff_get_domain_space(ma);
12999 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13000 rational = isl_basic_map_is_rational(bmap);
13001 strides = rational ? 0 : multi_aff_strides(ma);
13002 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13003 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13004 if (rational)
13005 res = isl_basic_map_set_rational(res);
13007 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13008 if (isl_basic_map_alloc_div(res) < 0)
13009 goto error;
13011 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
13012 goto error;
13014 for (i = 0; i < bmap->n_eq; ++i) {
13015 k = isl_basic_map_alloc_equality(res);
13016 if (k < 0)
13017 goto error;
13018 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13019 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13022 for (i = 0; i < bmap->n_ineq; ++i) {
13023 k = isl_basic_map_alloc_inequality(res);
13024 if (k < 0)
13025 goto error;
13026 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13027 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13030 for (i = 0; i < bmap->n_div; ++i) {
13031 if (isl_int_is_zero(bmap->div[i][0])) {
13032 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13033 continue;
13035 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13036 n_before, n_after, n_div_ma, n_div_bmap,
13037 f, c1, c2, g, 1);
13040 if (strides)
13041 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13043 isl_int_clear(f);
13044 isl_int_clear(c1);
13045 isl_int_clear(c2);
13046 isl_int_clear(g);
13047 isl_basic_map_free(bmap);
13048 isl_multi_aff_free(ma);
13049 res = isl_basic_set_simplify(res);
13050 return isl_basic_map_finalize(res);
13051 error:
13052 isl_int_clear(f);
13053 isl_int_clear(c1);
13054 isl_int_clear(c2);
13055 isl_int_clear(g);
13056 isl_basic_map_free(bmap);
13057 isl_multi_aff_free(ma);
13058 isl_basic_map_free(res);
13059 return NULL;
13062 /* Compute the preimage of "bset" under the function represented by "ma".
13063 * In other words, plug in "ma" in "bset". The result is a basic set
13064 * that lives in the domain space of "ma".
13066 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13067 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13069 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13072 /* Compute the preimage of the domain of "bmap" under the function
13073 * represented by "ma".
13074 * In other words, plug in "ma" in the domain of "bmap".
13075 * The result is a basic map that lives in the same space as "bmap"
13076 * except that the domain has been replaced by the domain space of "ma".
13078 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13079 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13081 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13084 /* Compute the preimage of the range of "bmap" under the function
13085 * represented by "ma".
13086 * In other words, plug in "ma" in the range of "bmap".
13087 * The result is a basic map that lives in the same space as "bmap"
13088 * except that the range has been replaced by the domain space of "ma".
13090 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13091 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13093 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13096 /* Check if the range of "ma" is compatible with the domain or range
13097 * (depending on "type") of "map".
13098 * Return -1 if anything is wrong.
13100 static int check_map_compatible_range_multi_aff(
13101 __isl_keep isl_map *map, enum isl_dim_type type,
13102 __isl_keep isl_multi_aff *ma)
13104 int m;
13105 isl_space *ma_space;
13107 ma_space = isl_multi_aff_get_space(ma);
13108 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13109 isl_space_free(ma_space);
13110 if (m >= 0 && !m)
13111 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13112 "spaces don't match", return -1);
13113 return m;
13116 /* Compute the preimage of the domain or range (depending on "type")
13117 * of "map" under the function represented by "ma".
13118 * In other words, plug in "ma" in the domain or range of "map".
13119 * The result is a map that lives in the same space as "map"
13120 * except that the domain or range has been replaced by
13121 * the domain space of "ma".
13123 * The parameters are assumed to have been aligned.
13125 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13126 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13128 int i;
13129 isl_space *space;
13131 map = isl_map_cow(map);
13132 ma = isl_multi_aff_align_divs(ma);
13133 if (!map || !ma)
13134 goto error;
13135 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13136 goto error;
13138 for (i = 0; i < map->n; ++i) {
13139 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13140 isl_multi_aff_copy(ma));
13141 if (!map->p[i])
13142 goto error;
13145 space = isl_multi_aff_get_domain_space(ma);
13146 space = isl_space_set(isl_map_get_space(map), type, space);
13148 isl_space_free(map->dim);
13149 map->dim = space;
13150 if (!map->dim)
13151 goto error;
13153 isl_multi_aff_free(ma);
13154 if (map->n > 1)
13155 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13156 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13157 return map;
13158 error:
13159 isl_multi_aff_free(ma);
13160 isl_map_free(map);
13161 return NULL;
13164 /* Compute the preimage of the domain or range (depending on "type")
13165 * of "map" under the function represented by "ma".
13166 * In other words, plug in "ma" in the domain or range of "map".
13167 * The result is a map that lives in the same space as "map"
13168 * except that the domain or range has been replaced by
13169 * the domain space of "ma".
13171 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13172 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13174 if (!map || !ma)
13175 goto error;
13177 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
13178 return map_preimage_multi_aff(map, type, ma);
13180 if (!isl_space_has_named_params(map->dim) ||
13181 !isl_space_has_named_params(ma->space))
13182 isl_die(map->ctx, isl_error_invalid,
13183 "unaligned unnamed parameters", goto error);
13184 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13185 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13187 return map_preimage_multi_aff(map, type, ma);
13188 error:
13189 isl_multi_aff_free(ma);
13190 return isl_map_free(map);
13193 /* Compute the preimage of "set" under the function represented by "ma".
13194 * In other words, plug in "ma" in "set". The result is a set
13195 * that lives in the domain space of "ma".
13197 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13198 __isl_take isl_multi_aff *ma)
13200 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13203 /* Compute the preimage of the domain of "map" under the function
13204 * represented by "ma".
13205 * In other words, plug in "ma" in the domain of "map".
13206 * The result is a map that lives in the same space as "map"
13207 * except that the domain has been replaced by the domain space of "ma".
13209 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13210 __isl_take isl_multi_aff *ma)
13212 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13215 /* Compute the preimage of the range of "map" under the function
13216 * represented by "ma".
13217 * In other words, plug in "ma" in the range of "map".
13218 * The result is a map that lives in the same space as "map"
13219 * except that the range has been replaced by the domain space of "ma".
13221 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13222 __isl_take isl_multi_aff *ma)
13224 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13227 /* Compute the preimage of "map" under the function represented by "pma".
13228 * In other words, plug in "pma" in the domain or range of "map".
13229 * The result is a map that lives in the same space as "map",
13230 * except that the space of type "type" has been replaced by
13231 * the domain space of "pma".
13233 * The parameters of "map" and "pma" are assumed to have been aligned.
13235 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13236 __isl_take isl_map *map, enum isl_dim_type type,
13237 __isl_take isl_pw_multi_aff *pma)
13239 int i;
13240 isl_map *res;
13242 if (!pma)
13243 goto error;
13245 if (pma->n == 0) {
13246 isl_pw_multi_aff_free(pma);
13247 res = isl_map_empty(isl_map_get_space(map));
13248 isl_map_free(map);
13249 return res;
13252 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13253 isl_multi_aff_copy(pma->p[0].maff));
13254 if (type == isl_dim_in)
13255 res = isl_map_intersect_domain(res,
13256 isl_map_copy(pma->p[0].set));
13257 else
13258 res = isl_map_intersect_range(res,
13259 isl_map_copy(pma->p[0].set));
13261 for (i = 1; i < pma->n; ++i) {
13262 isl_map *res_i;
13264 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13265 isl_multi_aff_copy(pma->p[i].maff));
13266 if (type == isl_dim_in)
13267 res_i = isl_map_intersect_domain(res_i,
13268 isl_map_copy(pma->p[i].set));
13269 else
13270 res_i = isl_map_intersect_range(res_i,
13271 isl_map_copy(pma->p[i].set));
13272 res = isl_map_union(res, res_i);
13275 isl_pw_multi_aff_free(pma);
13276 isl_map_free(map);
13277 return res;
13278 error:
13279 isl_pw_multi_aff_free(pma);
13280 isl_map_free(map);
13281 return NULL;
13284 /* Compute the preimage of "map" under the function represented by "pma".
13285 * In other words, plug in "pma" in the domain or range of "map".
13286 * The result is a map that lives in the same space as "map",
13287 * except that the space of type "type" has been replaced by
13288 * the domain space of "pma".
13290 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13291 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13293 if (!map || !pma)
13294 goto error;
13296 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13297 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13299 if (!isl_space_has_named_params(map->dim) ||
13300 !isl_space_has_named_params(pma->dim))
13301 isl_die(map->ctx, isl_error_invalid,
13302 "unaligned unnamed parameters", goto error);
13303 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13304 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13306 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13307 error:
13308 isl_pw_multi_aff_free(pma);
13309 return isl_map_free(map);
13312 /* Compute the preimage of "set" under the function represented by "pma".
13313 * In other words, plug in "pma" in "set". The result is a set
13314 * that lives in the domain space of "pma".
13316 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13317 __isl_take isl_pw_multi_aff *pma)
13319 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13322 /* Compute the preimage of the domain of "map" under the function
13323 * represented by "pma".
13324 * In other words, plug in "pma" in the domain of "map".
13325 * The result is a map that lives in the same space as "map",
13326 * except that domain space has been replaced by the domain space of "pma".
13328 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13329 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13331 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13334 /* Compute the preimage of the range of "map" under the function
13335 * represented by "pma".
13336 * In other words, plug in "pma" in the range of "map".
13337 * The result is a map that lives in the same space as "map",
13338 * except that range space has been replaced by the domain space of "pma".
13340 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13341 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13343 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13346 /* Compute the preimage of "map" under the function represented by "mpa".
13347 * In other words, plug in "mpa" in the domain or range of "map".
13348 * The result is a map that lives in the same space as "map",
13349 * except that the space of type "type" has been replaced by
13350 * the domain space of "mpa".
13352 * If the map does not involve any constraints that refer to the
13353 * dimensions of the substituted space, then the only possible
13354 * effect of "mpa" on the map is to map the space to a different space.
13355 * We create a separate isl_multi_aff to effectuate this change
13356 * in order to avoid spurious splitting of the map along the pieces
13357 * of "mpa".
13359 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13360 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13362 int n;
13363 isl_pw_multi_aff *pma;
13365 if (!map || !mpa)
13366 goto error;
13368 n = isl_map_dim(map, type);
13369 if (!isl_map_involves_dims(map, type, 0, n)) {
13370 isl_space *space;
13371 isl_multi_aff *ma;
13373 space = isl_multi_pw_aff_get_space(mpa);
13374 isl_multi_pw_aff_free(mpa);
13375 ma = isl_multi_aff_zero(space);
13376 return isl_map_preimage_multi_aff(map, type, ma);
13379 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13380 return isl_map_preimage_pw_multi_aff(map, type, pma);
13381 error:
13382 isl_map_free(map);
13383 isl_multi_pw_aff_free(mpa);
13384 return NULL;
13387 /* Compute the preimage of "map" under the function represented by "mpa".
13388 * In other words, plug in "mpa" in the domain "map".
13389 * The result is a map that lives in the same space as "map",
13390 * except that domain space has been replaced by the domain space of "mpa".
13392 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13393 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13395 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13398 /* Compute the preimage of "set" by the function represented by "mpa".
13399 * In other words, plug in "mpa" in "set".
13401 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13402 __isl_take isl_multi_pw_aff *mpa)
13404 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13407 /* Are the "n" "coefficients" starting at "first" of the integer division
13408 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13409 * to each other?
13410 * The "coefficient" at position 0 is the denominator.
13411 * The "coefficient" at position 1 is the constant term.
13413 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13414 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13415 unsigned first, unsigned n)
13417 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13418 return isl_bool_error;
13419 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13420 return isl_bool_error;
13421 return isl_seq_eq(bmap1->div[pos1] + first,
13422 bmap2->div[pos2] + first, n);
13425 /* Are the integer division expressions at position "pos1" in "bmap1" and
13426 * "pos2" in "bmap2" equal to each other, except that the constant terms
13427 * are different?
13429 isl_bool isl_basic_map_equal_div_expr_except_constant(
13430 __isl_keep isl_basic_map *bmap1, int pos1,
13431 __isl_keep isl_basic_map *bmap2, int pos2)
13433 isl_bool equal;
13434 unsigned total;
13436 if (!bmap1 || !bmap2)
13437 return isl_bool_error;
13438 total = isl_basic_map_total_dim(bmap1);
13439 if (total != isl_basic_map_total_dim(bmap2))
13440 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13441 "incomparable div expressions", return isl_bool_error);
13442 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13443 0, 1);
13444 if (equal < 0 || !equal)
13445 return equal;
13446 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13447 1, 1);
13448 if (equal < 0 || equal)
13449 return isl_bool_not(equal);
13450 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13451 2, total);
13454 /* Replace the numerator of the constant term of the integer division
13455 * expression at position "div" in "bmap" by "value".
13456 * The caller guarantees that this does not change the meaning
13457 * of the input.
13459 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13460 __isl_take isl_basic_map *bmap, int div, int value)
13462 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13463 return isl_basic_map_free(bmap);
13465 isl_int_set_si(bmap->div[div][1], value);
13467 return bmap;
13470 /* Is the point "inner" internal to inequality constraint "ineq"
13471 * of "bset"?
13472 * The point is considered to be internal to the inequality constraint,
13473 * if it strictly lies on the positive side of the inequality constraint,
13474 * or if it lies on the constraint and the constraint is lexico-positive.
13476 static isl_bool is_internal(__isl_keep isl_vec *inner,
13477 __isl_keep isl_basic_set *bset, int ineq)
13479 isl_ctx *ctx;
13480 int pos;
13481 unsigned total;
13483 if (!inner || !bset)
13484 return isl_bool_error;
13486 ctx = isl_basic_set_get_ctx(bset);
13487 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13488 &ctx->normalize_gcd);
13489 if (!isl_int_is_zero(ctx->normalize_gcd))
13490 return isl_int_is_nonneg(ctx->normalize_gcd);
13492 total = isl_basic_set_dim(bset, isl_dim_all);
13493 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13494 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13497 /* Tighten the inequality constraints of "bset" that are outward with respect
13498 * to the point "vec".
13499 * That is, tighten the constraints that are not satisfied by "vec".
13501 * "vec" is a point internal to some superset S of "bset" that is used
13502 * to make the subsets of S disjoint, by tightening one half of the constraints
13503 * that separate two subsets. In particular, the constraints of S
13504 * are all satisfied by "vec" and should not be tightened.
13505 * Of the internal constraints, those that have "vec" on the outside
13506 * are tightened. The shared facet is included in the adjacent subset
13507 * with the opposite constraint.
13508 * For constraints that saturate "vec", this criterion cannot be used
13509 * to determine which of the two sides should be tightened.
13510 * Instead, the sign of the first non-zero coefficient is used
13511 * to make this choice. Note that this second criterion is never used
13512 * on the constraints of S since "vec" is interior to "S".
13514 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13515 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13517 int j;
13519 bset = isl_basic_set_cow(bset);
13520 if (!bset)
13521 return NULL;
13522 for (j = 0; j < bset->n_ineq; ++j) {
13523 isl_bool internal;
13525 internal = is_internal(vec, bset, j);
13526 if (internal < 0)
13527 return isl_basic_set_free(bset);
13528 if (internal)
13529 continue;
13530 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13533 return bset;