isl_union_{map,set}_contains: drop spurious memory management annotation
[isl.git] / isl_map.c
blobf8948e9f7769ad897658fb226123f1d5169ecce2
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl/map.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
35 #include <isl_sort.h>
36 #include "isl_tab.h"
37 #include <isl/vec.h>
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
56 switch (type) {
57 case isl_dim_param: return dim->nparam;
58 case isl_dim_in: return dim->n_in;
59 case isl_dim_out: return dim->n_out;
60 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
61 default: return 0;
65 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
67 switch (type) {
68 case isl_dim_param: return 1;
69 case isl_dim_in: return 1 + dim->nparam;
70 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
71 default: return 0;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
76 enum isl_dim_type type)
78 if (!bmap)
79 return 0;
80 switch (type) {
81 case isl_dim_cst: return 1;
82 case isl_dim_param:
83 case isl_dim_in:
84 case isl_dim_out: return isl_space_dim(bmap->dim, type);
85 case isl_dim_div: return bmap->n_div;
86 case isl_dim_all: return isl_basic_map_total_dim(bmap);
87 default: return 0;
91 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
93 return map ? n(map->dim, type) : 0;
96 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
98 return set ? n(set->dim, type) : 0;
101 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
102 enum isl_dim_type type)
104 isl_space *space;
106 if (!bmap)
107 return 0;
109 space = bmap->dim;
110 switch (type) {
111 case isl_dim_cst: return 0;
112 case isl_dim_param: return 1;
113 case isl_dim_in: return 1 + space->nparam;
114 case isl_dim_out: return 1 + space->nparam + space->n_in;
115 case isl_dim_div: return 1 + space->nparam + space->n_in +
116 space->n_out;
117 default: return 0;
121 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
122 enum isl_dim_type type)
124 return isl_basic_map_offset(bset, type);
127 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
129 return pos(map->dim, type);
132 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
133 enum isl_dim_type type)
135 return isl_basic_map_dim(bset, type);
138 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
140 return isl_basic_set_dim(bset, isl_dim_set);
143 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
145 return isl_basic_set_dim(bset, isl_dim_param);
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
150 if (!bset)
151 return 0;
152 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
155 unsigned isl_set_n_dim(__isl_keep isl_set *set)
157 return isl_set_dim(set, isl_dim_set);
160 unsigned isl_set_n_param(__isl_keep isl_set *set)
162 return isl_set_dim(set, isl_dim_param);
165 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
167 return bmap ? bmap->dim->n_in : 0;
170 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
172 return bmap ? bmap->dim->n_out : 0;
175 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
177 return bmap ? bmap->dim->nparam : 0;
180 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
182 return bmap ? bmap->n_div : 0;
185 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
187 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
190 unsigned isl_map_n_in(const struct isl_map *map)
192 return map ? map->dim->n_in : 0;
195 unsigned isl_map_n_out(const struct isl_map *map)
197 return map ? map->dim->n_out : 0;
200 unsigned isl_map_n_param(const struct isl_map *map)
202 return map ? map->dim->nparam : 0;
205 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
206 __isl_keep isl_set *set)
208 isl_bool m;
209 if (!map || !set)
210 return isl_bool_error;
211 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
212 if (m < 0 || !m)
213 return m;
214 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
215 set->dim, isl_dim_set);
218 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
219 __isl_keep isl_basic_set *bset)
221 isl_bool m;
222 if (!bmap || !bset)
223 return isl_bool_error;
224 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
225 if (m < 0 || !m)
226 return m;
227 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
228 bset->dim, isl_dim_set);
231 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
232 __isl_keep isl_set *set)
234 isl_bool m;
235 if (!map || !set)
236 return isl_bool_error;
237 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
238 if (m < 0 || !m)
239 return m;
240 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
241 set->dim, isl_dim_set);
244 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
245 __isl_keep isl_basic_set *bset)
247 isl_bool m;
248 if (!bmap || !bset)
249 return isl_bool_error;
250 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
251 if (m < 0 || !m)
252 return m;
253 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
254 bset->dim, isl_dim_set);
257 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
259 return bmap ? bmap->ctx : NULL;
262 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
264 return bset ? bset->ctx : NULL;
267 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
269 return map ? map->ctx : NULL;
272 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
274 return set ? set->ctx : NULL;
277 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
279 if (!bmap)
280 return NULL;
281 return isl_space_copy(bmap->dim);
284 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
286 if (!bset)
287 return NULL;
288 return isl_space_copy(bset->dim);
291 /* Extract the divs in "bmap" as a matrix.
293 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
295 int i;
296 isl_ctx *ctx;
297 isl_mat *div;
298 unsigned total;
299 unsigned cols;
301 if (!bmap)
302 return NULL;
304 ctx = isl_basic_map_get_ctx(bmap);
305 total = isl_space_dim(bmap->dim, isl_dim_all);
306 cols = 1 + 1 + total + bmap->n_div;
307 div = isl_mat_alloc(ctx, bmap->n_div, cols);
308 if (!div)
309 return NULL;
311 for (i = 0; i < bmap->n_div; ++i)
312 isl_seq_cpy(div->row[i], bmap->div[i], cols);
314 return div;
317 /* Extract the divs in "bset" as a matrix.
319 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
321 return isl_basic_map_get_divs(bset);
324 __isl_give isl_local_space *isl_basic_map_get_local_space(
325 __isl_keep isl_basic_map *bmap)
327 isl_mat *div;
329 if (!bmap)
330 return NULL;
332 div = isl_basic_map_get_divs(bmap);
333 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
336 __isl_give isl_local_space *isl_basic_set_get_local_space(
337 __isl_keep isl_basic_set *bset)
339 return isl_basic_map_get_local_space(bset);
342 /* For each known div d = floor(f/m), add the constraints
344 * f - m d >= 0
345 * -(f-(m-1)) + m d >= 0
347 * Do not finalize the result.
349 static __isl_give isl_basic_map *add_known_div_constraints(
350 __isl_take isl_basic_map *bmap)
352 int i;
353 unsigned n_div;
355 if (!bmap)
356 return NULL;
357 n_div = isl_basic_map_dim(bmap, isl_dim_div);
358 if (n_div == 0)
359 return bmap;
360 bmap = isl_basic_map_cow(bmap);
361 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
362 if (!bmap)
363 return NULL;
364 for (i = 0; i < n_div; ++i) {
365 if (isl_int_is_zero(bmap->div[i][0]))
366 continue;
367 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
368 return isl_basic_map_free(bmap);
371 return bmap;
374 __isl_give isl_basic_map *isl_basic_map_from_local_space(
375 __isl_take isl_local_space *ls)
377 int i;
378 int n_div;
379 isl_basic_map *bmap;
381 if (!ls)
382 return NULL;
384 n_div = isl_local_space_dim(ls, isl_dim_div);
385 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
386 n_div, 0, 2 * n_div);
388 for (i = 0; i < n_div; ++i)
389 if (isl_basic_map_alloc_div(bmap) < 0)
390 goto error;
392 for (i = 0; i < n_div; ++i)
393 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
394 bmap = add_known_div_constraints(bmap);
396 isl_local_space_free(ls);
397 return bmap;
398 error:
399 isl_local_space_free(ls);
400 isl_basic_map_free(bmap);
401 return NULL;
404 __isl_give isl_basic_set *isl_basic_set_from_local_space(
405 __isl_take isl_local_space *ls)
407 return isl_basic_map_from_local_space(ls);
410 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
412 if (!map)
413 return NULL;
414 return isl_space_copy(map->dim);
417 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
419 if (!set)
420 return NULL;
421 return isl_space_copy(set->dim);
424 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
425 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
427 bmap = isl_basic_map_cow(bmap);
428 if (!bmap)
429 return NULL;
430 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
431 if (!bmap->dim)
432 goto error;
433 bmap = isl_basic_map_finalize(bmap);
434 return bmap;
435 error:
436 isl_basic_map_free(bmap);
437 return NULL;
440 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
441 __isl_take isl_basic_set *bset, const char *s)
443 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
446 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
447 enum isl_dim_type type)
449 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
452 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
453 enum isl_dim_type type, const char *s)
455 int i;
457 map = isl_map_cow(map);
458 if (!map)
459 return NULL;
461 map->dim = isl_space_set_tuple_name(map->dim, type, s);
462 if (!map->dim)
463 goto error;
465 for (i = 0; i < map->n; ++i) {
466 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
467 if (!map->p[i])
468 goto error;
471 return map;
472 error:
473 isl_map_free(map);
474 return NULL;
477 /* Replace the identifier of the tuple of type "type" by "id".
479 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
480 __isl_take isl_basic_map *bmap,
481 enum isl_dim_type type, __isl_take isl_id *id)
483 bmap = isl_basic_map_cow(bmap);
484 if (!bmap)
485 goto error;
486 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
487 if (!bmap->dim)
488 return isl_basic_map_free(bmap);
489 bmap = isl_basic_map_finalize(bmap);
490 return bmap;
491 error:
492 isl_id_free(id);
493 return NULL;
496 /* Replace the identifier of the tuple by "id".
498 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
499 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
501 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
504 /* Does the input or output tuple have a name?
506 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
508 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
511 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
512 enum isl_dim_type type)
514 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
517 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
518 const char *s)
520 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
521 isl_dim_set, s));
524 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
525 enum isl_dim_type type, __isl_take isl_id *id)
527 map = isl_map_cow(map);
528 if (!map)
529 goto error;
531 map->dim = isl_space_set_tuple_id(map->dim, type, id);
533 return isl_map_reset_space(map, isl_space_copy(map->dim));
534 error:
535 isl_id_free(id);
536 return NULL;
539 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
540 __isl_take isl_id *id)
542 return isl_map_set_tuple_id(set, isl_dim_set, id);
545 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
546 enum isl_dim_type type)
548 map = isl_map_cow(map);
549 if (!map)
550 return NULL;
552 map->dim = isl_space_reset_tuple_id(map->dim, type);
554 return isl_map_reset_space(map, isl_space_copy(map->dim));
557 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
559 return isl_map_reset_tuple_id(set, isl_dim_set);
562 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
564 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
567 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
568 enum isl_dim_type type)
570 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
573 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
575 return isl_map_has_tuple_id(set, isl_dim_set);
578 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
580 return isl_map_get_tuple_id(set, isl_dim_set);
583 /* Does the set tuple have a name?
585 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
587 if (!set)
588 return isl_bool_error;
589 return isl_space_has_tuple_name(set->dim, isl_dim_set);
593 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
595 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
598 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
600 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
603 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
604 enum isl_dim_type type, unsigned pos)
606 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
609 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
610 enum isl_dim_type type, unsigned pos)
612 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
615 /* Does the given dimension have a name?
617 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
618 enum isl_dim_type type, unsigned pos)
620 if (!map)
621 return isl_bool_error;
622 return isl_space_has_dim_name(map->dim, type, pos);
625 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
626 enum isl_dim_type type, unsigned pos)
628 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
631 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
632 enum isl_dim_type type, unsigned pos)
634 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
637 /* Does the given dimension have a name?
639 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
640 enum isl_dim_type type, unsigned pos)
642 if (!set)
643 return isl_bool_error;
644 return isl_space_has_dim_name(set->dim, type, pos);
647 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
648 __isl_take isl_basic_map *bmap,
649 enum isl_dim_type type, unsigned pos, const char *s)
651 bmap = isl_basic_map_cow(bmap);
652 if (!bmap)
653 return NULL;
654 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
655 if (!bmap->dim)
656 goto error;
657 return isl_basic_map_finalize(bmap);
658 error:
659 isl_basic_map_free(bmap);
660 return NULL;
663 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
664 enum isl_dim_type type, unsigned pos, const char *s)
666 int i;
668 map = isl_map_cow(map);
669 if (!map)
670 return NULL;
672 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
673 if (!map->dim)
674 goto error;
676 for (i = 0; i < map->n; ++i) {
677 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
678 if (!map->p[i])
679 goto error;
682 return map;
683 error:
684 isl_map_free(map);
685 return NULL;
688 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
689 __isl_take isl_basic_set *bset,
690 enum isl_dim_type type, unsigned pos, const char *s)
692 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
693 type, pos, s));
696 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
697 enum isl_dim_type type, unsigned pos, const char *s)
699 return set_from_map(isl_map_set_dim_name(set_to_map(set),
700 type, pos, s));
703 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
704 enum isl_dim_type type, unsigned pos)
706 if (!bmap)
707 return isl_bool_error;
708 return isl_space_has_dim_id(bmap->dim, type, pos);
711 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
712 enum isl_dim_type type, unsigned pos)
714 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
717 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
718 enum isl_dim_type type, unsigned pos)
720 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
723 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
724 enum isl_dim_type type, unsigned pos)
726 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
729 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
730 enum isl_dim_type type, unsigned pos)
732 return isl_map_has_dim_id(set, type, pos);
735 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
736 enum isl_dim_type type, unsigned pos)
738 return isl_map_get_dim_id(set, type, pos);
741 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
742 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
744 map = isl_map_cow(map);
745 if (!map)
746 goto error;
748 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
750 return isl_map_reset_space(map, isl_space_copy(map->dim));
751 error:
752 isl_id_free(id);
753 return NULL;
756 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
757 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
759 return isl_map_set_dim_id(set, type, pos, id);
762 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
763 __isl_keep isl_id *id)
765 if (!map)
766 return -1;
767 return isl_space_find_dim_by_id(map->dim, type, id);
770 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
771 __isl_keep isl_id *id)
773 return isl_map_find_dim_by_id(set, type, id);
776 /* Return the position of the dimension of the given type and name
777 * in "bmap".
778 * Return -1 if no such dimension can be found.
780 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
781 enum isl_dim_type type, const char *name)
783 if (!bmap)
784 return -1;
785 return isl_space_find_dim_by_name(bmap->dim, type, name);
788 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
789 const char *name)
791 if (!map)
792 return -1;
793 return isl_space_find_dim_by_name(map->dim, type, name);
796 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
797 const char *name)
799 return isl_map_find_dim_by_name(set, type, name);
802 /* Reset the user pointer on all identifiers of parameters and tuples
803 * of the space of "map".
805 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
807 isl_space *space;
809 space = isl_map_get_space(map);
810 space = isl_space_reset_user(space);
811 map = isl_map_reset_space(map, space);
813 return map;
816 /* Reset the user pointer on all identifiers of parameters and tuples
817 * of the space of "set".
819 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
821 return isl_map_reset_user(set);
824 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
826 if (!bmap)
827 return isl_bool_error;
828 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
831 /* Has "map" been marked as a rational map?
832 * In particular, have all basic maps in "map" been marked this way?
833 * An empty map is not considered to be rational.
834 * Maps where only some of the basic maps are marked rational
835 * are not allowed.
837 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
839 int i;
840 isl_bool rational;
842 if (!map)
843 return isl_bool_error;
844 if (map->n == 0)
845 return isl_bool_false;
846 rational = isl_basic_map_is_rational(map->p[0]);
847 if (rational < 0)
848 return rational;
849 for (i = 1; i < map->n; ++i) {
850 isl_bool rational_i;
852 rational_i = isl_basic_map_is_rational(map->p[i]);
853 if (rational_i < 0)
854 return rational_i;
855 if (rational != rational_i)
856 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
857 "mixed rational and integer basic maps "
858 "not supported", return isl_bool_error);
861 return rational;
864 /* Has "set" been marked as a rational set?
865 * In particular, have all basic set in "set" been marked this way?
866 * An empty set is not considered to be rational.
867 * Sets where only some of the basic sets are marked rational
868 * are not allowed.
870 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
872 return isl_map_is_rational(set);
875 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
877 return isl_basic_map_is_rational(bset);
880 /* Does "bmap" contain any rational points?
882 * If "bmap" has an equality for each dimension, equating the dimension
883 * to an integer constant, then it has no rational points, even if it
884 * is marked as rational.
886 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
888 isl_bool has_rational = isl_bool_true;
889 unsigned total;
891 if (!bmap)
892 return isl_bool_error;
893 if (isl_basic_map_plain_is_empty(bmap))
894 return isl_bool_false;
895 if (!isl_basic_map_is_rational(bmap))
896 return isl_bool_false;
897 bmap = isl_basic_map_copy(bmap);
898 bmap = isl_basic_map_implicit_equalities(bmap);
899 if (!bmap)
900 return isl_bool_error;
901 total = isl_basic_map_total_dim(bmap);
902 if (bmap->n_eq == total) {
903 int i, j;
904 for (i = 0; i < bmap->n_eq; ++i) {
905 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
906 if (j < 0)
907 break;
908 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
909 !isl_int_is_negone(bmap->eq[i][1 + j]))
910 break;
911 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
912 total - j - 1);
913 if (j >= 0)
914 break;
916 if (i == bmap->n_eq)
917 has_rational = isl_bool_false;
919 isl_basic_map_free(bmap);
921 return has_rational;
924 /* Does "map" contain any rational points?
926 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
928 int i;
929 isl_bool has_rational;
931 if (!map)
932 return isl_bool_error;
933 for (i = 0; i < map->n; ++i) {
934 has_rational = isl_basic_map_has_rational(map->p[i]);
935 if (has_rational < 0 || has_rational)
936 return has_rational;
938 return isl_bool_false;
941 /* Does "set" contain any rational points?
943 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
945 return isl_map_has_rational(set);
948 /* Is this basic set a parameter domain?
950 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
952 if (!bset)
953 return isl_bool_error;
954 return isl_space_is_params(bset->dim);
957 /* Is this set a parameter domain?
959 isl_bool isl_set_is_params(__isl_keep isl_set *set)
961 if (!set)
962 return isl_bool_error;
963 return isl_space_is_params(set->dim);
966 /* Is this map actually a parameter domain?
967 * Users should never call this function. Outside of isl,
968 * a map can never be a parameter domain.
970 isl_bool isl_map_is_params(__isl_keep isl_map *map)
972 if (!map)
973 return isl_bool_error;
974 return isl_space_is_params(map->dim);
977 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
978 struct isl_basic_map *bmap, unsigned extra,
979 unsigned n_eq, unsigned n_ineq)
981 int i;
982 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
984 bmap->ctx = ctx;
985 isl_ctx_ref(ctx);
987 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
988 if (isl_blk_is_error(bmap->block))
989 goto error;
991 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
992 if ((n_ineq + n_eq) && !bmap->ineq)
993 goto error;
995 if (extra == 0) {
996 bmap->block2 = isl_blk_empty();
997 bmap->div = NULL;
998 } else {
999 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1000 if (isl_blk_is_error(bmap->block2))
1001 goto error;
1003 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1004 if (!bmap->div)
1005 goto error;
1008 for (i = 0; i < n_ineq + n_eq; ++i)
1009 bmap->ineq[i] = bmap->block.data + i * row_size;
1011 for (i = 0; i < extra; ++i)
1012 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1014 bmap->ref = 1;
1015 bmap->flags = 0;
1016 bmap->c_size = n_eq + n_ineq;
1017 bmap->eq = bmap->ineq + n_ineq;
1018 bmap->extra = extra;
1019 bmap->n_eq = 0;
1020 bmap->n_ineq = 0;
1021 bmap->n_div = 0;
1022 bmap->sample = NULL;
1024 return bmap;
1025 error:
1026 isl_basic_map_free(bmap);
1027 return NULL;
1030 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1031 unsigned nparam, unsigned dim, unsigned extra,
1032 unsigned n_eq, unsigned n_ineq)
1034 struct isl_basic_map *bmap;
1035 isl_space *space;
1037 space = isl_space_set_alloc(ctx, nparam, dim);
1038 if (!space)
1039 return NULL;
1041 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1042 return bset_from_bmap(bmap);
1045 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1046 unsigned extra, unsigned n_eq, unsigned n_ineq)
1048 struct isl_basic_map *bmap;
1049 if (!dim)
1050 return NULL;
1051 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1052 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1053 return bset_from_bmap(bmap);
1054 error:
1055 isl_space_free(dim);
1056 return NULL;
1059 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1060 unsigned extra, unsigned n_eq, unsigned n_ineq)
1062 struct isl_basic_map *bmap;
1064 if (!dim)
1065 return NULL;
1066 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1067 if (!bmap)
1068 goto error;
1069 bmap->dim = dim;
1071 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1072 error:
1073 isl_space_free(dim);
1074 return NULL;
1077 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1078 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1079 unsigned n_eq, unsigned n_ineq)
1081 struct isl_basic_map *bmap;
1082 isl_space *dim;
1084 dim = isl_space_alloc(ctx, nparam, in, out);
1085 if (!dim)
1086 return NULL;
1088 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1089 return bmap;
1092 static void dup_constraints(
1093 struct isl_basic_map *dst, struct isl_basic_map *src)
1095 int i;
1096 unsigned total = isl_basic_map_total_dim(src);
1098 for (i = 0; i < src->n_eq; ++i) {
1099 int j = isl_basic_map_alloc_equality(dst);
1100 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1103 for (i = 0; i < src->n_ineq; ++i) {
1104 int j = isl_basic_map_alloc_inequality(dst);
1105 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1108 for (i = 0; i < src->n_div; ++i) {
1109 int j = isl_basic_map_alloc_div(dst);
1110 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1112 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1115 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1117 struct isl_basic_map *dup;
1119 if (!bmap)
1120 return NULL;
1121 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1122 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1123 if (!dup)
1124 return NULL;
1125 dup_constraints(dup, bmap);
1126 dup->flags = bmap->flags;
1127 dup->sample = isl_vec_copy(bmap->sample);
1128 return dup;
1131 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1133 struct isl_basic_map *dup;
1135 dup = isl_basic_map_dup(bset_to_bmap(bset));
1136 return bset_from_bmap(dup);
1139 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1141 if (!bset)
1142 return NULL;
1144 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1145 bset->ref++;
1146 return bset;
1148 return isl_basic_set_dup(bset);
1151 struct isl_set *isl_set_copy(struct isl_set *set)
1153 if (!set)
1154 return NULL;
1156 set->ref++;
1157 return set;
1160 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1162 if (!bmap)
1163 return NULL;
1165 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1166 bmap->ref++;
1167 return bmap;
1169 bmap = isl_basic_map_dup(bmap);
1170 if (bmap)
1171 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1172 return bmap;
1175 struct isl_map *isl_map_copy(struct isl_map *map)
1177 if (!map)
1178 return NULL;
1180 map->ref++;
1181 return map;
1184 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1186 if (!bmap)
1187 return NULL;
1189 if (--bmap->ref > 0)
1190 return NULL;
1192 isl_ctx_deref(bmap->ctx);
1193 free(bmap->div);
1194 isl_blk_free(bmap->ctx, bmap->block2);
1195 free(bmap->ineq);
1196 isl_blk_free(bmap->ctx, bmap->block);
1197 isl_vec_free(bmap->sample);
1198 isl_space_free(bmap->dim);
1199 free(bmap);
1201 return NULL;
1204 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1206 return isl_basic_map_free(bset_to_bmap(bset));
1209 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1211 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1214 __isl_give isl_map *isl_map_align_params_map_map_and(
1215 __isl_take isl_map *map1, __isl_take isl_map *map2,
1216 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1217 __isl_take isl_map *map2))
1219 if (!map1 || !map2)
1220 goto error;
1221 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1222 return fn(map1, map2);
1223 if (!isl_space_has_named_params(map1->dim) ||
1224 !isl_space_has_named_params(map2->dim))
1225 isl_die(map1->ctx, isl_error_invalid,
1226 "unaligned unnamed parameters", goto error);
1227 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1228 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1229 return fn(map1, map2);
1230 error:
1231 isl_map_free(map1);
1232 isl_map_free(map2);
1233 return NULL;
1236 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1237 __isl_keep isl_map *map2,
1238 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1240 isl_bool r;
1242 if (!map1 || !map2)
1243 return isl_bool_error;
1244 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1245 return fn(map1, map2);
1246 if (!isl_space_has_named_params(map1->dim) ||
1247 !isl_space_has_named_params(map2->dim))
1248 isl_die(map1->ctx, isl_error_invalid,
1249 "unaligned unnamed parameters", return isl_bool_error);
1250 map1 = isl_map_copy(map1);
1251 map2 = isl_map_copy(map2);
1252 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1253 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1254 r = fn(map1, map2);
1255 isl_map_free(map1);
1256 isl_map_free(map2);
1257 return r;
1260 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1262 struct isl_ctx *ctx;
1263 if (!bmap)
1264 return -1;
1265 ctx = bmap->ctx;
1266 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1267 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1268 return -1);
1269 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1274 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1275 isl_int *t;
1276 int j = isl_basic_map_alloc_inequality(bmap);
1277 if (j < 0)
1278 return -1;
1279 t = bmap->ineq[j];
1280 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1281 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1282 bmap->eq[-1] = t;
1283 bmap->n_eq++;
1284 bmap->n_ineq--;
1285 bmap->eq--;
1286 return 0;
1288 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1289 bmap->extra - bmap->n_div);
1290 return bmap->n_eq++;
1293 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1295 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1298 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1300 if (!bmap)
1301 return -1;
1302 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1303 bmap->n_eq -= n;
1304 return 0;
1307 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1309 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1312 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1314 isl_int *t;
1315 if (!bmap)
1316 return -1;
1317 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1319 if (pos != bmap->n_eq - 1) {
1320 t = bmap->eq[pos];
1321 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1322 bmap->eq[bmap->n_eq - 1] = t;
1324 bmap->n_eq--;
1325 return 0;
1328 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1330 return isl_basic_map_drop_equality(bset_to_bmap(bset), pos);
1333 /* Turn inequality "pos" of "bmap" into an equality.
1335 * In particular, we move the inequality in front of the equalities
1336 * and move the last inequality in the position of the moved inequality.
1337 * Note that isl_tab_make_equalities_explicit depends on this particular
1338 * change in the ordering of the constraints.
1340 void isl_basic_map_inequality_to_equality(
1341 struct isl_basic_map *bmap, unsigned pos)
1343 isl_int *t;
1345 t = bmap->ineq[pos];
1346 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1347 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1348 bmap->eq[-1] = t;
1349 bmap->n_eq++;
1350 bmap->n_ineq--;
1351 bmap->eq--;
1352 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1353 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1354 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1355 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1358 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1360 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1363 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1365 struct isl_ctx *ctx;
1366 if (!bmap)
1367 return -1;
1368 ctx = bmap->ctx;
1369 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1370 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1371 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1372 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1373 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1374 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1375 1 + isl_basic_map_total_dim(bmap),
1376 bmap->extra - bmap->n_div);
1377 return bmap->n_ineq++;
1380 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1382 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1385 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1387 if (!bmap)
1388 return -1;
1389 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1390 bmap->n_ineq -= n;
1391 return 0;
1394 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1396 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1399 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1401 isl_int *t;
1402 if (!bmap)
1403 return -1;
1404 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1406 if (pos != bmap->n_ineq - 1) {
1407 t = bmap->ineq[pos];
1408 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1409 bmap->ineq[bmap->n_ineq - 1] = t;
1410 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1412 bmap->n_ineq--;
1413 return 0;
1416 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1418 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1421 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1422 isl_int *eq)
1424 int k;
1426 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1427 if (!bmap)
1428 return NULL;
1429 k = isl_basic_map_alloc_equality(bmap);
1430 if (k < 0)
1431 goto error;
1432 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1433 return bmap;
1434 error:
1435 isl_basic_map_free(bmap);
1436 return NULL;
1439 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1440 isl_int *eq)
1442 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1445 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1446 isl_int *ineq)
1448 int k;
1450 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1451 if (!bmap)
1452 return NULL;
1453 k = isl_basic_map_alloc_inequality(bmap);
1454 if (k < 0)
1455 goto error;
1456 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1457 return bmap;
1458 error:
1459 isl_basic_map_free(bmap);
1460 return NULL;
1463 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1464 isl_int *ineq)
1466 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1469 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1471 if (!bmap)
1472 return -1;
1473 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1474 isl_seq_clr(bmap->div[bmap->n_div] +
1475 1 + 1 + isl_basic_map_total_dim(bmap),
1476 bmap->extra - bmap->n_div);
1477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1478 return bmap->n_div++;
1481 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1483 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1486 /* Check that there are "n" dimensions of type "type" starting at "first"
1487 * in "bmap".
1489 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1490 enum isl_dim_type type, unsigned first, unsigned n)
1492 unsigned dim;
1494 if (!bmap)
1495 return isl_stat_error;
1496 dim = isl_basic_map_dim(bmap, type);
1497 if (first + n > dim || first + n < first)
1498 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1499 "position or range out of bounds",
1500 return isl_stat_error);
1501 return isl_stat_ok;
1504 /* Insert an extra integer division, prescribed by "div", to "bmap"
1505 * at (integer division) position "pos".
1507 * The integer division is first added at the end and then moved
1508 * into the right position.
1510 __isl_give isl_basic_map *isl_basic_map_insert_div(
1511 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1513 int i, k;
1515 bmap = isl_basic_map_cow(bmap);
1516 if (!bmap || !div)
1517 return isl_basic_map_free(bmap);
1519 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1520 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1521 "unexpected size", return isl_basic_map_free(bmap));
1522 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1523 return isl_basic_map_free(bmap);
1525 bmap = isl_basic_map_extend_space(bmap,
1526 isl_basic_map_get_space(bmap), 1, 0, 2);
1527 k = isl_basic_map_alloc_div(bmap);
1528 if (k < 0)
1529 return isl_basic_map_free(bmap);
1530 isl_seq_cpy(bmap->div[k], div->el, div->size);
1531 isl_int_set_si(bmap->div[k][div->size], 0);
1533 for (i = k; i > pos; --i)
1534 isl_basic_map_swap_div(bmap, i, i - 1);
1536 return bmap;
1539 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1541 if (!bmap)
1542 return isl_stat_error;
1543 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1544 bmap->n_div -= n;
1545 return isl_stat_ok;
1548 isl_stat isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1550 return isl_basic_map_free_div(bset_to_bmap(bset), n);
1553 /* Copy constraint from src to dst, putting the vars of src at offset
1554 * dim_off in dst and the divs of src at offset div_off in dst.
1555 * If both sets are actually map, then dim_off applies to the input
1556 * variables.
1558 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1559 struct isl_basic_map *src_map, isl_int *src,
1560 unsigned in_off, unsigned out_off, unsigned div_off)
1562 unsigned src_nparam = isl_basic_map_n_param(src_map);
1563 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1564 unsigned src_in = isl_basic_map_n_in(src_map);
1565 unsigned dst_in = isl_basic_map_n_in(dst_map);
1566 unsigned src_out = isl_basic_map_n_out(src_map);
1567 unsigned dst_out = isl_basic_map_n_out(dst_map);
1568 isl_int_set(dst[0], src[0]);
1569 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1570 if (dst_nparam > src_nparam)
1571 isl_seq_clr(dst+1+src_nparam,
1572 dst_nparam - src_nparam);
1573 isl_seq_clr(dst+1+dst_nparam, in_off);
1574 isl_seq_cpy(dst+1+dst_nparam+in_off,
1575 src+1+src_nparam,
1576 isl_min(dst_in-in_off, src_in));
1577 if (dst_in-in_off > src_in)
1578 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1579 dst_in - in_off - src_in);
1580 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1581 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1582 src+1+src_nparam+src_in,
1583 isl_min(dst_out-out_off, src_out));
1584 if (dst_out-out_off > src_out)
1585 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1586 dst_out - out_off - src_out);
1587 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1588 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1589 src+1+src_nparam+src_in+src_out,
1590 isl_min(dst_map->extra-div_off, src_map->n_div));
1591 if (dst_map->n_div-div_off > src_map->n_div)
1592 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1593 div_off+src_map->n_div,
1594 dst_map->n_div - div_off - src_map->n_div);
1597 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1598 struct isl_basic_map *src_map, isl_int *src,
1599 unsigned in_off, unsigned out_off, unsigned div_off)
1601 isl_int_set(dst[0], src[0]);
1602 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1605 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1606 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1608 int i;
1609 unsigned div_off;
1611 if (!bmap1 || !bmap2)
1612 goto error;
1614 div_off = bmap1->n_div;
1616 for (i = 0; i < bmap2->n_eq; ++i) {
1617 int i1 = isl_basic_map_alloc_equality(bmap1);
1618 if (i1 < 0)
1619 goto error;
1620 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1621 i_pos, o_pos, div_off);
1624 for (i = 0; i < bmap2->n_ineq; ++i) {
1625 int i1 = isl_basic_map_alloc_inequality(bmap1);
1626 if (i1 < 0)
1627 goto error;
1628 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1629 i_pos, o_pos, div_off);
1632 for (i = 0; i < bmap2->n_div; ++i) {
1633 int i1 = isl_basic_map_alloc_div(bmap1);
1634 if (i1 < 0)
1635 goto error;
1636 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1637 i_pos, o_pos, div_off);
1640 isl_basic_map_free(bmap2);
1642 return bmap1;
1644 error:
1645 isl_basic_map_free(bmap1);
1646 isl_basic_map_free(bmap2);
1647 return NULL;
1650 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1651 struct isl_basic_set *bset2, unsigned pos)
1653 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1654 bset_to_bmap(bset2), 0, pos));
1657 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1658 __isl_take isl_space *dim, unsigned extra,
1659 unsigned n_eq, unsigned n_ineq)
1661 struct isl_basic_map *ext;
1662 unsigned flags;
1663 int dims_ok;
1665 if (!dim)
1666 goto error;
1668 if (!base)
1669 goto error;
1671 dims_ok = isl_space_is_equal(base->dim, dim) &&
1672 base->extra >= base->n_div + extra;
1674 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1675 room_for_ineq(base, n_ineq)) {
1676 isl_space_free(dim);
1677 return base;
1680 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1681 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1682 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1683 extra += base->extra;
1684 n_eq += base->n_eq;
1685 n_ineq += base->n_ineq;
1687 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1688 dim = NULL;
1689 if (!ext)
1690 goto error;
1692 if (dims_ok)
1693 ext->sample = isl_vec_copy(base->sample);
1694 flags = base->flags;
1695 ext = add_constraints(ext, base, 0, 0);
1696 if (ext) {
1697 ext->flags = flags;
1698 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1701 return ext;
1703 error:
1704 isl_space_free(dim);
1705 isl_basic_map_free(base);
1706 return NULL;
1709 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1710 __isl_take isl_space *dim, unsigned extra,
1711 unsigned n_eq, unsigned n_ineq)
1713 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1714 dim, extra, n_eq, n_ineq));
1717 struct isl_basic_map *isl_basic_map_extend_constraints(
1718 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1720 if (!base)
1721 return NULL;
1722 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1723 0, n_eq, n_ineq);
1726 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1727 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1728 unsigned n_eq, unsigned n_ineq)
1730 struct isl_basic_map *bmap;
1731 isl_space *dim;
1733 if (!base)
1734 return NULL;
1735 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1736 if (!dim)
1737 goto error;
1739 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1740 return bmap;
1741 error:
1742 isl_basic_map_free(base);
1743 return NULL;
1746 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1747 unsigned nparam, unsigned dim, unsigned extra,
1748 unsigned n_eq, unsigned n_ineq)
1750 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1751 nparam, 0, dim, extra, n_eq, n_ineq));
1754 struct isl_basic_set *isl_basic_set_extend_constraints(
1755 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1757 isl_basic_map *bmap = bset_to_bmap(base);
1758 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1759 return bset_from_bmap(bmap);
1762 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1764 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1767 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1769 if (!bmap)
1770 return NULL;
1772 if (bmap->ref > 1) {
1773 bmap->ref--;
1774 bmap = isl_basic_map_dup(bmap);
1776 if (bmap) {
1777 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1778 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1780 return bmap;
1783 /* Clear all cached information in "map", either because it is about
1784 * to be modified or because it is being freed.
1785 * Always return the same pointer that is passed in.
1786 * This is needed for the use in isl_map_free.
1788 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1790 isl_basic_map_free(map->cached_simple_hull[0]);
1791 isl_basic_map_free(map->cached_simple_hull[1]);
1792 map->cached_simple_hull[0] = NULL;
1793 map->cached_simple_hull[1] = NULL;
1794 return map;
1797 struct isl_set *isl_set_cow(struct isl_set *set)
1799 return isl_map_cow(set);
1802 /* Return an isl_map that is equal to "map" and that has only
1803 * a single reference.
1805 * If the original input already has only one reference, then
1806 * simply return it, but clear all cached information, since
1807 * it may be rendered invalid by the operations that will be
1808 * performed on the result.
1810 * Otherwise, create a duplicate (without any cached information).
1812 struct isl_map *isl_map_cow(struct isl_map *map)
1814 if (!map)
1815 return NULL;
1817 if (map->ref == 1)
1818 return clear_caches(map);
1819 map->ref--;
1820 return isl_map_dup(map);
1823 static void swap_vars(struct isl_blk blk, isl_int *a,
1824 unsigned a_len, unsigned b_len)
1826 isl_seq_cpy(blk.data, a+a_len, b_len);
1827 isl_seq_cpy(blk.data+b_len, a, a_len);
1828 isl_seq_cpy(a, blk.data, b_len+a_len);
1831 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1832 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1834 int i;
1835 struct isl_blk blk;
1837 if (!bmap)
1838 goto error;
1840 isl_assert(bmap->ctx,
1841 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1843 if (n1 == 0 || n2 == 0)
1844 return bmap;
1846 bmap = isl_basic_map_cow(bmap);
1847 if (!bmap)
1848 return NULL;
1850 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1851 if (isl_blk_is_error(blk))
1852 goto error;
1854 for (i = 0; i < bmap->n_eq; ++i)
1855 swap_vars(blk,
1856 bmap->eq[i] + pos, n1, n2);
1858 for (i = 0; i < bmap->n_ineq; ++i)
1859 swap_vars(blk,
1860 bmap->ineq[i] + pos, n1, n2);
1862 for (i = 0; i < bmap->n_div; ++i)
1863 swap_vars(blk,
1864 bmap->div[i]+1 + pos, n1, n2);
1866 isl_blk_free(bmap->ctx, blk);
1868 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1869 bmap = isl_basic_map_gauss(bmap, NULL);
1870 return isl_basic_map_finalize(bmap);
1871 error:
1872 isl_basic_map_free(bmap);
1873 return NULL;
1876 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1878 int i = 0;
1879 unsigned total;
1880 if (!bmap)
1881 goto error;
1882 total = isl_basic_map_total_dim(bmap);
1883 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1884 return isl_basic_map_free(bmap);
1885 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1886 if (bmap->n_eq > 0)
1887 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1888 else {
1889 i = isl_basic_map_alloc_equality(bmap);
1890 if (i < 0)
1891 goto error;
1893 isl_int_set_si(bmap->eq[i][0], 1);
1894 isl_seq_clr(bmap->eq[i]+1, total);
1895 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1896 isl_vec_free(bmap->sample);
1897 bmap->sample = NULL;
1898 return isl_basic_map_finalize(bmap);
1899 error:
1900 isl_basic_map_free(bmap);
1901 return NULL;
1904 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1906 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1909 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1910 * of "bmap").
1912 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1914 isl_int *t = bmap->div[a];
1915 bmap->div[a] = bmap->div[b];
1916 bmap->div[b] = t;
1919 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1920 * div definitions accordingly.
1922 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1924 int i;
1925 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1927 swap_div(bmap, a, b);
1929 for (i = 0; i < bmap->n_eq; ++i)
1930 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1932 for (i = 0; i < bmap->n_ineq; ++i)
1933 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1935 for (i = 0; i < bmap->n_div; ++i)
1936 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1937 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1940 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1941 * div definitions accordingly.
1943 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1945 isl_basic_map_swap_div(bset, a, b);
1948 /* Eliminate the specified n dimensions starting at first from the
1949 * constraints, without removing the dimensions from the space.
1950 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1952 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1953 enum isl_dim_type type, unsigned first, unsigned n)
1955 int i;
1957 if (!map)
1958 return NULL;
1959 if (n == 0)
1960 return map;
1962 if (first + n > isl_map_dim(map, type) || first + n < first)
1963 isl_die(map->ctx, isl_error_invalid,
1964 "index out of bounds", goto error);
1966 map = isl_map_cow(map);
1967 if (!map)
1968 return NULL;
1970 for (i = 0; i < map->n; ++i) {
1971 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1972 if (!map->p[i])
1973 goto error;
1975 return map;
1976 error:
1977 isl_map_free(map);
1978 return NULL;
1981 /* Eliminate the specified n dimensions starting at first from the
1982 * constraints, without removing the dimensions from the space.
1983 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1985 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1986 enum isl_dim_type type, unsigned first, unsigned n)
1988 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
1991 /* Eliminate the specified n dimensions starting at first from the
1992 * constraints, without removing the dimensions from the space.
1993 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1995 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1996 unsigned first, unsigned n)
1998 return isl_set_eliminate(set, isl_dim_set, first, n);
2001 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2002 __isl_take isl_basic_map *bmap)
2004 if (!bmap)
2005 return NULL;
2006 bmap = isl_basic_map_eliminate_vars(bmap,
2007 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2008 if (!bmap)
2009 return NULL;
2010 bmap->n_div = 0;
2011 return isl_basic_map_finalize(bmap);
2014 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2015 __isl_take isl_basic_set *bset)
2017 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2020 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2022 int i;
2024 if (!map)
2025 return NULL;
2026 if (map->n == 0)
2027 return map;
2029 map = isl_map_cow(map);
2030 if (!map)
2031 return NULL;
2033 for (i = 0; i < map->n; ++i) {
2034 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2035 if (!map->p[i])
2036 goto error;
2038 return map;
2039 error:
2040 isl_map_free(map);
2041 return NULL;
2044 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2046 return isl_map_remove_divs(set);
2049 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2050 enum isl_dim_type type, unsigned first, unsigned n)
2052 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2053 return isl_basic_map_free(bmap);
2054 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2055 return bmap;
2056 bmap = isl_basic_map_eliminate_vars(bmap,
2057 isl_basic_map_offset(bmap, type) - 1 + first, n);
2058 if (!bmap)
2059 return bmap;
2060 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2061 return bmap;
2062 bmap = isl_basic_map_drop(bmap, type, first, n);
2063 return bmap;
2066 /* Return true if the definition of the given div (recursively) involves
2067 * any of the given variables.
2069 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2070 unsigned first, unsigned n)
2072 int i;
2073 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2075 if (isl_int_is_zero(bmap->div[div][0]))
2076 return isl_bool_false;
2077 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2078 return isl_bool_true;
2080 for (i = bmap->n_div - 1; i >= 0; --i) {
2081 isl_bool involves;
2083 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2084 continue;
2085 involves = div_involves_vars(bmap, i, first, n);
2086 if (involves < 0 || involves)
2087 return involves;
2090 return isl_bool_false;
2093 /* Try and add a lower and/or upper bound on "div" to "bmap"
2094 * based on inequality "i".
2095 * "total" is the total number of variables (excluding the divs).
2096 * "v" is a temporary object that can be used during the calculations.
2097 * If "lb" is set, then a lower bound should be constructed.
2098 * If "ub" is set, then an upper bound should be constructed.
2100 * The calling function has already checked that the inequality does not
2101 * reference "div", but we still need to check that the inequality is
2102 * of the right form. We'll consider the case where we want to construct
2103 * a lower bound. The construction of upper bounds is similar.
2105 * Let "div" be of the form
2107 * q = floor((a + f(x))/d)
2109 * We essentially check if constraint "i" is of the form
2111 * b + f(x) >= 0
2113 * so that we can use it to derive a lower bound on "div".
2114 * However, we allow a slightly more general form
2116 * b + g(x) >= 0
2118 * with the condition that the coefficients of g(x) - f(x) are all
2119 * divisible by d.
2120 * Rewriting this constraint as
2122 * 0 >= -b - g(x)
2124 * adding a + f(x) to both sides and dividing by d, we obtain
2126 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2128 * Taking the floor on both sides, we obtain
2130 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2132 * or
2134 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2136 * In the case of an upper bound, we construct the constraint
2138 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2141 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2142 __isl_take isl_basic_map *bmap, int div, int i,
2143 unsigned total, isl_int v, int lb, int ub)
2145 int j;
2147 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2148 if (lb) {
2149 isl_int_sub(v, bmap->ineq[i][1 + j],
2150 bmap->div[div][1 + 1 + j]);
2151 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2153 if (ub) {
2154 isl_int_add(v, bmap->ineq[i][1 + j],
2155 bmap->div[div][1 + 1 + j]);
2156 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2159 if (!lb && !ub)
2160 return bmap;
2162 bmap = isl_basic_map_cow(bmap);
2163 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2164 if (lb) {
2165 int k = isl_basic_map_alloc_inequality(bmap);
2166 if (k < 0)
2167 goto error;
2168 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2169 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2170 bmap->div[div][1 + j]);
2171 isl_int_cdiv_q(bmap->ineq[k][j],
2172 bmap->ineq[k][j], bmap->div[div][0]);
2174 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2176 if (ub) {
2177 int k = isl_basic_map_alloc_inequality(bmap);
2178 if (k < 0)
2179 goto error;
2180 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2181 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2182 bmap->div[div][1 + j]);
2183 isl_int_fdiv_q(bmap->ineq[k][j],
2184 bmap->ineq[k][j], bmap->div[div][0]);
2186 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2189 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2190 return bmap;
2191 error:
2192 isl_basic_map_free(bmap);
2193 return NULL;
2196 /* This function is called right before "div" is eliminated from "bmap"
2197 * using Fourier-Motzkin.
2198 * Look through the constraints of "bmap" for constraints on the argument
2199 * of the integer division and use them to construct constraints on the
2200 * integer division itself. These constraints can then be combined
2201 * during the Fourier-Motzkin elimination.
2202 * Note that it is only useful to introduce lower bounds on "div"
2203 * if "bmap" already contains upper bounds on "div" as the newly
2204 * introduce lower bounds can then be combined with the pre-existing
2205 * upper bounds. Similarly for upper bounds.
2206 * We therefore first check if "bmap" contains any lower and/or upper bounds
2207 * on "div".
2209 * It is interesting to note that the introduction of these constraints
2210 * can indeed lead to more accurate results, even when compared to
2211 * deriving constraints on the argument of "div" from constraints on "div".
2212 * Consider, for example, the set
2214 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2216 * The second constraint can be rewritten as
2218 * 2 * [(-i-2j+3)/4] + k >= 0
2220 * from which we can derive
2222 * -i - 2j + 3 >= -2k
2224 * or
2226 * i + 2j <= 3 + 2k
2228 * Combined with the first constraint, we obtain
2230 * -3 <= 3 + 2k or k >= -3
2232 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2233 * the first constraint, we obtain
2235 * [(i + 2j)/4] >= [-3/4] = -1
2237 * Combining this constraint with the second constraint, we obtain
2239 * k >= -2
2241 static __isl_give isl_basic_map *insert_bounds_on_div(
2242 __isl_take isl_basic_map *bmap, int div)
2244 int i;
2245 int check_lb, check_ub;
2246 isl_int v;
2247 unsigned total;
2249 if (!bmap)
2250 return NULL;
2252 if (isl_int_is_zero(bmap->div[div][0]))
2253 return bmap;
2255 total = isl_space_dim(bmap->dim, isl_dim_all);
2257 check_lb = 0;
2258 check_ub = 0;
2259 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2260 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2261 if (s > 0)
2262 check_ub = 1;
2263 if (s < 0)
2264 check_lb = 1;
2267 if (!check_lb && !check_ub)
2268 return bmap;
2270 isl_int_init(v);
2272 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2273 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2274 continue;
2276 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2277 check_lb, check_ub);
2280 isl_int_clear(v);
2282 return bmap;
2285 /* Remove all divs (recursively) involving any of the given dimensions
2286 * in their definitions.
2288 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2289 __isl_take isl_basic_map *bmap,
2290 enum isl_dim_type type, unsigned first, unsigned n)
2292 int i;
2294 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2295 return isl_basic_map_free(bmap);
2296 first += isl_basic_map_offset(bmap, type);
2298 for (i = bmap->n_div - 1; i >= 0; --i) {
2299 isl_bool involves;
2301 involves = div_involves_vars(bmap, i, first, n);
2302 if (involves < 0)
2303 return isl_basic_map_free(bmap);
2304 if (!involves)
2305 continue;
2306 bmap = insert_bounds_on_div(bmap, i);
2307 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2308 if (!bmap)
2309 return NULL;
2310 i = bmap->n_div;
2313 return bmap;
2316 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2317 __isl_take isl_basic_set *bset,
2318 enum isl_dim_type type, unsigned first, unsigned n)
2320 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2323 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2324 enum isl_dim_type type, unsigned first, unsigned n)
2326 int i;
2328 if (!map)
2329 return NULL;
2330 if (map->n == 0)
2331 return map;
2333 map = isl_map_cow(map);
2334 if (!map)
2335 return NULL;
2337 for (i = 0; i < map->n; ++i) {
2338 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2339 type, first, n);
2340 if (!map->p[i])
2341 goto error;
2343 return map;
2344 error:
2345 isl_map_free(map);
2346 return NULL;
2349 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2350 enum isl_dim_type type, unsigned first, unsigned n)
2352 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2353 type, first, n));
2356 /* Does the description of "bmap" depend on the specified dimensions?
2357 * We also check whether the dimensions appear in any of the div definitions.
2358 * In principle there is no need for this check. If the dimensions appear
2359 * in a div definition, they also appear in the defining constraints of that
2360 * div.
2362 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2363 enum isl_dim_type type, unsigned first, unsigned n)
2365 int i;
2367 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2368 return isl_bool_error;
2370 first += isl_basic_map_offset(bmap, type);
2371 for (i = 0; i < bmap->n_eq; ++i)
2372 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2373 return isl_bool_true;
2374 for (i = 0; i < bmap->n_ineq; ++i)
2375 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2376 return isl_bool_true;
2377 for (i = 0; i < bmap->n_div; ++i) {
2378 if (isl_int_is_zero(bmap->div[i][0]))
2379 continue;
2380 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2381 return isl_bool_true;
2384 return isl_bool_false;
2387 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2388 enum isl_dim_type type, unsigned first, unsigned n)
2390 int i;
2392 if (!map)
2393 return isl_bool_error;
2395 if (first + n > isl_map_dim(map, type))
2396 isl_die(map->ctx, isl_error_invalid,
2397 "index out of bounds", return isl_bool_error);
2399 for (i = 0; i < map->n; ++i) {
2400 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2401 type, first, n);
2402 if (involves < 0 || involves)
2403 return involves;
2406 return isl_bool_false;
2409 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2410 enum isl_dim_type type, unsigned first, unsigned n)
2412 return isl_basic_map_involves_dims(bset, type, first, n);
2415 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2416 enum isl_dim_type type, unsigned first, unsigned n)
2418 return isl_map_involves_dims(set, type, first, n);
2421 /* Drop all constraints in bmap that involve any of the dimensions
2422 * first to first+n-1.
2424 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2425 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2427 int i;
2429 if (n == 0)
2430 return bmap;
2432 bmap = isl_basic_map_cow(bmap);
2434 if (!bmap)
2435 return NULL;
2437 for (i = bmap->n_eq - 1; i >= 0; --i) {
2438 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2439 continue;
2440 isl_basic_map_drop_equality(bmap, i);
2443 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2444 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2445 continue;
2446 isl_basic_map_drop_inequality(bmap, i);
2449 bmap = isl_basic_map_add_known_div_constraints(bmap);
2450 return bmap;
2453 /* Drop all constraints in bset that involve any of the dimensions
2454 * first to first+n-1.
2456 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2457 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2459 return isl_basic_map_drop_constraints_involving(bset, first, n);
2462 /* Drop all constraints in bmap that do not involve any of the dimensions
2463 * first to first + n - 1 of the given type.
2465 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2466 __isl_take isl_basic_map *bmap,
2467 enum isl_dim_type type, unsigned first, unsigned n)
2469 int i;
2471 if (n == 0) {
2472 isl_space *space = isl_basic_map_get_space(bmap);
2473 isl_basic_map_free(bmap);
2474 return isl_basic_map_universe(space);
2476 bmap = isl_basic_map_cow(bmap);
2477 if (!bmap)
2478 return NULL;
2480 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2481 return isl_basic_map_free(bmap);
2483 first += isl_basic_map_offset(bmap, type) - 1;
2485 for (i = bmap->n_eq - 1; i >= 0; --i) {
2486 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2487 continue;
2488 isl_basic_map_drop_equality(bmap, i);
2491 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2492 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2493 continue;
2494 isl_basic_map_drop_inequality(bmap, i);
2497 bmap = isl_basic_map_add_known_div_constraints(bmap);
2498 return bmap;
2501 /* Drop all constraints in bset that do not involve any of the dimensions
2502 * first to first + n - 1 of the given type.
2504 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2505 __isl_take isl_basic_set *bset,
2506 enum isl_dim_type type, unsigned first, unsigned n)
2508 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2509 type, first, n);
2512 /* Drop all constraints in bmap that involve any of the dimensions
2513 * first to first + n - 1 of the given type.
2515 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2516 __isl_take isl_basic_map *bmap,
2517 enum isl_dim_type type, unsigned first, unsigned n)
2519 if (!bmap)
2520 return NULL;
2521 if (n == 0)
2522 return bmap;
2524 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2525 return isl_basic_map_free(bmap);
2527 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2528 first += isl_basic_map_offset(bmap, type) - 1;
2529 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2532 /* Drop all constraints in bset that involve any of the dimensions
2533 * first to first + n - 1 of the given type.
2535 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2536 __isl_take isl_basic_set *bset,
2537 enum isl_dim_type type, unsigned first, unsigned n)
2539 return isl_basic_map_drop_constraints_involving_dims(bset,
2540 type, first, n);
2543 /* Drop constraints from "map" by applying "drop" to each basic map.
2545 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2546 enum isl_dim_type type, unsigned first, unsigned n,
2547 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2548 enum isl_dim_type type, unsigned first, unsigned n))
2550 int i;
2551 unsigned dim;
2553 if (!map)
2554 return NULL;
2556 dim = isl_map_dim(map, type);
2557 if (first + n > dim || first + n < first)
2558 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2559 "index out of bounds", return isl_map_free(map));
2561 map = isl_map_cow(map);
2562 if (!map)
2563 return NULL;
2565 for (i = 0; i < map->n; ++i) {
2566 map->p[i] = drop(map->p[i], type, first, n);
2567 if (!map->p[i])
2568 return isl_map_free(map);
2571 if (map->n > 1)
2572 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2574 return map;
2577 /* Drop all constraints in map that involve any of the dimensions
2578 * first to first + n - 1 of the given type.
2580 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2581 __isl_take isl_map *map,
2582 enum isl_dim_type type, unsigned first, unsigned n)
2584 if (n == 0)
2585 return map;
2586 return drop_constraints(map, type, first, n,
2587 &isl_basic_map_drop_constraints_involving_dims);
2590 /* Drop all constraints in "map" that do not involve any of the dimensions
2591 * first to first + n - 1 of the given type.
2593 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2594 __isl_take isl_map *map,
2595 enum isl_dim_type type, unsigned first, unsigned n)
2597 if (n == 0) {
2598 isl_space *space = isl_map_get_space(map);
2599 isl_map_free(map);
2600 return isl_map_universe(space);
2602 return drop_constraints(map, type, first, n,
2603 &isl_basic_map_drop_constraints_not_involving_dims);
2606 /* Drop all constraints in set that involve any of the dimensions
2607 * first to first + n - 1 of the given type.
2609 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2610 __isl_take isl_set *set,
2611 enum isl_dim_type type, unsigned first, unsigned n)
2613 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2616 /* Drop all constraints in "set" that do not involve any of the dimensions
2617 * first to first + n - 1 of the given type.
2619 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2620 __isl_take isl_set *set,
2621 enum isl_dim_type type, unsigned first, unsigned n)
2623 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2626 /* Does local variable "div" of "bmap" have a complete explicit representation?
2627 * Having a complete explicit representation requires not only
2628 * an explicit representation, but also that all local variables
2629 * that appear in this explicit representation in turn have
2630 * a complete explicit representation.
2632 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2634 int i;
2635 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2636 isl_bool marked;
2638 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2639 if (marked < 0 || marked)
2640 return isl_bool_not(marked);
2642 for (i = bmap->n_div - 1; i >= 0; --i) {
2643 isl_bool known;
2645 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2646 continue;
2647 known = isl_basic_map_div_is_known(bmap, i);
2648 if (known < 0 || !known)
2649 return known;
2652 return isl_bool_true;
2655 /* Does local variable "div" of "bset" have a complete explicit representation?
2657 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2659 return isl_basic_map_div_is_known(bset, div);
2662 /* Remove all divs that are unknown or defined in terms of unknown divs.
2664 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2665 __isl_take isl_basic_map *bmap)
2667 int i;
2669 if (!bmap)
2670 return NULL;
2672 for (i = bmap->n_div - 1; i >= 0; --i) {
2673 if (isl_basic_map_div_is_known(bmap, i))
2674 continue;
2675 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2676 if (!bmap)
2677 return NULL;
2678 i = bmap->n_div;
2681 return bmap;
2684 /* Remove all divs that are unknown or defined in terms of unknown divs.
2686 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2687 __isl_take isl_basic_set *bset)
2689 return isl_basic_map_remove_unknown_divs(bset);
2692 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2694 int i;
2696 if (!map)
2697 return NULL;
2698 if (map->n == 0)
2699 return map;
2701 map = isl_map_cow(map);
2702 if (!map)
2703 return NULL;
2705 for (i = 0; i < map->n; ++i) {
2706 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2707 if (!map->p[i])
2708 goto error;
2710 return map;
2711 error:
2712 isl_map_free(map);
2713 return NULL;
2716 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2718 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2721 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2722 __isl_take isl_basic_set *bset,
2723 enum isl_dim_type type, unsigned first, unsigned n)
2725 isl_basic_map *bmap = bset_to_bmap(bset);
2726 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2727 return bset_from_bmap(bmap);
2730 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2731 enum isl_dim_type type, unsigned first, unsigned n)
2733 int i;
2735 if (n == 0)
2736 return map;
2738 map = isl_map_cow(map);
2739 if (!map)
2740 return NULL;
2741 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2743 for (i = 0; i < map->n; ++i) {
2744 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2745 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2746 if (!map->p[i])
2747 goto error;
2749 map = isl_map_drop(map, type, first, n);
2750 return map;
2751 error:
2752 isl_map_free(map);
2753 return NULL;
2756 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2757 enum isl_dim_type type, unsigned first, unsigned n)
2759 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2760 type, first, n));
2763 /* Project out n inputs starting at first using Fourier-Motzkin */
2764 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2765 unsigned first, unsigned n)
2767 return isl_map_remove_dims(map, isl_dim_in, first, n);
2770 static void dump_term(struct isl_basic_map *bmap,
2771 isl_int c, int pos, FILE *out)
2773 const char *name;
2774 unsigned in = isl_basic_map_n_in(bmap);
2775 unsigned dim = in + isl_basic_map_n_out(bmap);
2776 unsigned nparam = isl_basic_map_n_param(bmap);
2777 if (!pos)
2778 isl_int_print(out, c, 0);
2779 else {
2780 if (!isl_int_is_one(c))
2781 isl_int_print(out, c, 0);
2782 if (pos < 1 + nparam) {
2783 name = isl_space_get_dim_name(bmap->dim,
2784 isl_dim_param, pos - 1);
2785 if (name)
2786 fprintf(out, "%s", name);
2787 else
2788 fprintf(out, "p%d", pos - 1);
2789 } else if (pos < 1 + nparam + in)
2790 fprintf(out, "i%d", pos - 1 - nparam);
2791 else if (pos < 1 + nparam + dim)
2792 fprintf(out, "o%d", pos - 1 - nparam - in);
2793 else
2794 fprintf(out, "e%d", pos - 1 - nparam - dim);
2798 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2799 int sign, FILE *out)
2801 int i;
2802 int first;
2803 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2804 isl_int v;
2806 isl_int_init(v);
2807 for (i = 0, first = 1; i < len; ++i) {
2808 if (isl_int_sgn(c[i]) * sign <= 0)
2809 continue;
2810 if (!first)
2811 fprintf(out, " + ");
2812 first = 0;
2813 isl_int_abs(v, c[i]);
2814 dump_term(bmap, v, i, out);
2816 isl_int_clear(v);
2817 if (first)
2818 fprintf(out, "0");
2821 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2822 const char *op, FILE *out, int indent)
2824 int i;
2826 fprintf(out, "%*s", indent, "");
2828 dump_constraint_sign(bmap, c, 1, out);
2829 fprintf(out, " %s ", op);
2830 dump_constraint_sign(bmap, c, -1, out);
2832 fprintf(out, "\n");
2834 for (i = bmap->n_div; i < bmap->extra; ++i) {
2835 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2836 continue;
2837 fprintf(out, "%*s", indent, "");
2838 fprintf(out, "ERROR: unused div coefficient not zero\n");
2839 abort();
2843 static void dump_constraints(struct isl_basic_map *bmap,
2844 isl_int **c, unsigned n,
2845 const char *op, FILE *out, int indent)
2847 int i;
2849 for (i = 0; i < n; ++i)
2850 dump_constraint(bmap, c[i], op, out, indent);
2853 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2855 int j;
2856 int first = 1;
2857 unsigned total = isl_basic_map_total_dim(bmap);
2859 for (j = 0; j < 1 + total; ++j) {
2860 if (isl_int_is_zero(exp[j]))
2861 continue;
2862 if (!first && isl_int_is_pos(exp[j]))
2863 fprintf(out, "+");
2864 dump_term(bmap, exp[j], j, out);
2865 first = 0;
2869 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2871 int i;
2873 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2874 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2876 for (i = 0; i < bmap->n_div; ++i) {
2877 fprintf(out, "%*s", indent, "");
2878 fprintf(out, "e%d = [(", i);
2879 dump_affine(bmap, bmap->div[i]+1, out);
2880 fprintf(out, ")/");
2881 isl_int_print(out, bmap->div[i][0], 0);
2882 fprintf(out, "]\n");
2886 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2887 FILE *out, int indent)
2889 if (!bset) {
2890 fprintf(out, "null basic set\n");
2891 return;
2894 fprintf(out, "%*s", indent, "");
2895 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2896 bset->ref, bset->dim->nparam, bset->dim->n_out,
2897 bset->extra, bset->flags);
2898 dump(bset_to_bmap(bset), out, indent);
2901 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2902 FILE *out, int indent)
2904 if (!bmap) {
2905 fprintf(out, "null basic map\n");
2906 return;
2909 fprintf(out, "%*s", indent, "");
2910 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2911 "flags: %x, n_name: %d\n",
2912 bmap->ref,
2913 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2914 bmap->extra, bmap->flags, bmap->dim->n_id);
2915 dump(bmap, out, indent);
2918 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2920 unsigned total;
2921 if (!bmap)
2922 return -1;
2923 total = isl_basic_map_total_dim(bmap);
2924 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2925 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2926 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2927 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2928 return 0;
2931 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2932 unsigned flags)
2934 if (!space)
2935 return NULL;
2936 if (isl_space_dim(space, isl_dim_in) != 0)
2937 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2938 "set cannot have input dimensions", goto error);
2939 return isl_map_alloc_space(space, n, flags);
2940 error:
2941 isl_space_free(space);
2942 return NULL;
2945 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2946 unsigned nparam, unsigned dim, int n, unsigned flags)
2948 struct isl_set *set;
2949 isl_space *dims;
2951 dims = isl_space_alloc(ctx, nparam, 0, dim);
2952 if (!dims)
2953 return NULL;
2955 set = isl_set_alloc_space(dims, n, flags);
2956 return set;
2959 /* Make sure "map" has room for at least "n" more basic maps.
2961 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2963 int i;
2964 struct isl_map *grown = NULL;
2966 if (!map)
2967 return NULL;
2968 isl_assert(map->ctx, n >= 0, goto error);
2969 if (map->n + n <= map->size)
2970 return map;
2971 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2972 if (!grown)
2973 goto error;
2974 for (i = 0; i < map->n; ++i) {
2975 grown->p[i] = isl_basic_map_copy(map->p[i]);
2976 if (!grown->p[i])
2977 goto error;
2978 grown->n++;
2980 isl_map_free(map);
2981 return grown;
2982 error:
2983 isl_map_free(grown);
2984 isl_map_free(map);
2985 return NULL;
2988 /* Make sure "set" has room for at least "n" more basic sets.
2990 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2992 return set_from_map(isl_map_grow(set_to_map(set), n));
2995 struct isl_set *isl_set_dup(struct isl_set *set)
2997 int i;
2998 struct isl_set *dup;
3000 if (!set)
3001 return NULL;
3003 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
3004 if (!dup)
3005 return NULL;
3006 for (i = 0; i < set->n; ++i)
3007 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
3008 return dup;
3011 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
3013 return isl_map_from_basic_map(bset);
3016 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
3018 struct isl_map *map;
3020 if (!bmap)
3021 return NULL;
3023 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3024 return isl_map_add_basic_map(map, bmap);
3027 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3028 __isl_take isl_basic_set *bset)
3030 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3031 bset_to_bmap(bset)));
3034 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3036 return isl_map_free(set);
3039 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3041 int i;
3043 if (!set) {
3044 fprintf(out, "null set\n");
3045 return;
3048 fprintf(out, "%*s", indent, "");
3049 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3050 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3051 set->flags);
3052 for (i = 0; i < set->n; ++i) {
3053 fprintf(out, "%*s", indent, "");
3054 fprintf(out, "basic set %d:\n", i);
3055 isl_basic_set_print_internal(set->p[i], out, indent+4);
3059 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3061 int i;
3063 if (!map) {
3064 fprintf(out, "null map\n");
3065 return;
3068 fprintf(out, "%*s", indent, "");
3069 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3070 "flags: %x, n_name: %d\n",
3071 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3072 map->dim->n_out, map->flags, map->dim->n_id);
3073 for (i = 0; i < map->n; ++i) {
3074 fprintf(out, "%*s", indent, "");
3075 fprintf(out, "basic map %d:\n", i);
3076 isl_basic_map_print_internal(map->p[i], out, indent+4);
3080 struct isl_basic_map *isl_basic_map_intersect_domain(
3081 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3083 struct isl_basic_map *bmap_domain;
3085 if (!bmap || !bset)
3086 goto error;
3088 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3089 bset->dim, isl_dim_param), goto error);
3091 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3092 isl_assert(bset->ctx,
3093 isl_basic_map_compatible_domain(bmap, bset), goto error);
3095 bmap = isl_basic_map_cow(bmap);
3096 if (!bmap)
3097 goto error;
3098 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3099 bset->n_div, bset->n_eq, bset->n_ineq);
3100 bmap_domain = isl_basic_map_from_domain(bset);
3101 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3103 bmap = isl_basic_map_simplify(bmap);
3104 return isl_basic_map_finalize(bmap);
3105 error:
3106 isl_basic_map_free(bmap);
3107 isl_basic_set_free(bset);
3108 return NULL;
3111 /* Check that the space of "bset" is the same as that of the range of "bmap".
3113 static isl_stat isl_basic_map_check_compatible_range(
3114 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3116 isl_bool ok;
3118 ok = isl_basic_map_compatible_range(bmap, bset);
3119 if (ok < 0)
3120 return isl_stat_error;
3121 if (!ok)
3122 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3123 "incompatible spaces", return isl_stat_error);
3125 return isl_stat_ok;
3128 struct isl_basic_map *isl_basic_map_intersect_range(
3129 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3131 struct isl_basic_map *bmap_range;
3133 if (!bmap || !bset)
3134 goto error;
3136 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3137 bset->dim, isl_dim_param), goto error);
3139 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3140 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3141 goto error;
3143 if (isl_basic_set_plain_is_universe(bset)) {
3144 isl_basic_set_free(bset);
3145 return bmap;
3148 bmap = isl_basic_map_cow(bmap);
3149 if (!bmap)
3150 goto error;
3151 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3152 bset->n_div, bset->n_eq, bset->n_ineq);
3153 bmap_range = bset_to_bmap(bset);
3154 bmap = add_constraints(bmap, bmap_range, 0, 0);
3156 bmap = isl_basic_map_simplify(bmap);
3157 return isl_basic_map_finalize(bmap);
3158 error:
3159 isl_basic_map_free(bmap);
3160 isl_basic_set_free(bset);
3161 return NULL;
3164 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3165 __isl_keep isl_vec *vec)
3167 int i;
3168 unsigned total;
3169 isl_int s;
3171 if (!bmap || !vec)
3172 return isl_bool_error;
3174 total = 1 + isl_basic_map_total_dim(bmap);
3175 if (total != vec->size)
3176 return isl_bool_false;
3178 isl_int_init(s);
3180 for (i = 0; i < bmap->n_eq; ++i) {
3181 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3182 if (!isl_int_is_zero(s)) {
3183 isl_int_clear(s);
3184 return isl_bool_false;
3188 for (i = 0; i < bmap->n_ineq; ++i) {
3189 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3190 if (isl_int_is_neg(s)) {
3191 isl_int_clear(s);
3192 return isl_bool_false;
3196 isl_int_clear(s);
3198 return isl_bool_true;
3201 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3202 __isl_keep isl_vec *vec)
3204 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3207 struct isl_basic_map *isl_basic_map_intersect(
3208 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3210 struct isl_vec *sample = NULL;
3212 if (!bmap1 || !bmap2)
3213 goto error;
3215 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
3216 bmap2->dim, isl_dim_param), goto error);
3217 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3218 isl_space_dim(bmap1->dim, isl_dim_param) &&
3219 isl_space_dim(bmap2->dim, isl_dim_all) !=
3220 isl_space_dim(bmap2->dim, isl_dim_param))
3221 return isl_basic_map_intersect(bmap2, bmap1);
3223 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3224 isl_space_dim(bmap2->dim, isl_dim_param))
3225 isl_assert(bmap1->ctx,
3226 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3228 if (isl_basic_map_plain_is_empty(bmap1)) {
3229 isl_basic_map_free(bmap2);
3230 return bmap1;
3232 if (isl_basic_map_plain_is_empty(bmap2)) {
3233 isl_basic_map_free(bmap1);
3234 return bmap2;
3237 if (bmap1->sample &&
3238 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3239 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3240 sample = isl_vec_copy(bmap1->sample);
3241 else if (bmap2->sample &&
3242 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3243 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3244 sample = isl_vec_copy(bmap2->sample);
3246 bmap1 = isl_basic_map_cow(bmap1);
3247 if (!bmap1)
3248 goto error;
3249 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3250 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3251 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3253 if (!bmap1)
3254 isl_vec_free(sample);
3255 else if (sample) {
3256 isl_vec_free(bmap1->sample);
3257 bmap1->sample = sample;
3260 bmap1 = isl_basic_map_simplify(bmap1);
3261 return isl_basic_map_finalize(bmap1);
3262 error:
3263 if (sample)
3264 isl_vec_free(sample);
3265 isl_basic_map_free(bmap1);
3266 isl_basic_map_free(bmap2);
3267 return NULL;
3270 struct isl_basic_set *isl_basic_set_intersect(
3271 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3273 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3274 bset_to_bmap(bset2)));
3277 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3278 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3280 return isl_basic_set_intersect(bset1, bset2);
3283 /* Special case of isl_map_intersect, where both map1 and map2
3284 * are convex, without any divs and such that either map1 or map2
3285 * contains a single constraint. This constraint is then simply
3286 * added to the other map.
3288 static __isl_give isl_map *map_intersect_add_constraint(
3289 __isl_take isl_map *map1, __isl_take isl_map *map2)
3291 isl_assert(map1->ctx, map1->n == 1, goto error);
3292 isl_assert(map2->ctx, map1->n == 1, goto error);
3293 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3294 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3296 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3297 return isl_map_intersect(map2, map1);
3299 isl_assert(map2->ctx,
3300 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3302 map1 = isl_map_cow(map1);
3303 if (!map1)
3304 goto error;
3305 if (isl_map_plain_is_empty(map1)) {
3306 isl_map_free(map2);
3307 return map1;
3309 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3310 if (map2->p[0]->n_eq == 1)
3311 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3312 else
3313 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3314 map2->p[0]->ineq[0]);
3316 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3317 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3318 if (!map1->p[0])
3319 goto error;
3321 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3322 isl_basic_map_free(map1->p[0]);
3323 map1->n = 0;
3326 isl_map_free(map2);
3328 return map1;
3329 error:
3330 isl_map_free(map1);
3331 isl_map_free(map2);
3332 return NULL;
3335 /* map2 may be either a parameter domain or a map living in the same
3336 * space as map1.
3338 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3339 __isl_take isl_map *map2)
3341 unsigned flags = 0;
3342 isl_map *result;
3343 int i, j;
3345 if (!map1 || !map2)
3346 goto error;
3348 if ((isl_map_plain_is_empty(map1) ||
3349 isl_map_plain_is_universe(map2)) &&
3350 isl_space_is_equal(map1->dim, map2->dim)) {
3351 isl_map_free(map2);
3352 return map1;
3354 if ((isl_map_plain_is_empty(map2) ||
3355 isl_map_plain_is_universe(map1)) &&
3356 isl_space_is_equal(map1->dim, map2->dim)) {
3357 isl_map_free(map1);
3358 return map2;
3361 if (map1->n == 1 && map2->n == 1 &&
3362 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3363 isl_space_is_equal(map1->dim, map2->dim) &&
3364 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3365 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3366 return map_intersect_add_constraint(map1, map2);
3368 if (isl_space_dim(map2->dim, isl_dim_all) !=
3369 isl_space_dim(map2->dim, isl_dim_param))
3370 isl_assert(map1->ctx,
3371 isl_space_is_equal(map1->dim, map2->dim), goto error);
3373 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3374 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3375 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3377 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3378 map1->n * map2->n, flags);
3379 if (!result)
3380 goto error;
3381 for (i = 0; i < map1->n; ++i)
3382 for (j = 0; j < map2->n; ++j) {
3383 struct isl_basic_map *part;
3384 part = isl_basic_map_intersect(
3385 isl_basic_map_copy(map1->p[i]),
3386 isl_basic_map_copy(map2->p[j]));
3387 if (isl_basic_map_is_empty(part) < 0)
3388 part = isl_basic_map_free(part);
3389 result = isl_map_add_basic_map(result, part);
3390 if (!result)
3391 goto error;
3393 isl_map_free(map1);
3394 isl_map_free(map2);
3395 return result;
3396 error:
3397 isl_map_free(map1);
3398 isl_map_free(map2);
3399 return NULL;
3402 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3403 __isl_take isl_map *map2)
3405 if (!map1 || !map2)
3406 goto error;
3407 if (!isl_space_is_equal(map1->dim, map2->dim))
3408 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3409 "spaces don't match", goto error);
3410 return map_intersect_internal(map1, map2);
3411 error:
3412 isl_map_free(map1);
3413 isl_map_free(map2);
3414 return NULL;
3417 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3418 __isl_take isl_map *map2)
3420 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3423 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3425 return set_from_map(isl_map_intersect(set_to_map(set1),
3426 set_to_map(set2)));
3429 /* map_intersect_internal accepts intersections
3430 * with parameter domains, so we can just call that function.
3432 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3433 __isl_take isl_set *params)
3435 return map_intersect_internal(map, params);
3438 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3439 __isl_take isl_map *map2)
3441 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3444 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3445 __isl_take isl_set *params)
3447 return isl_map_intersect_params(set, params);
3450 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3452 isl_space *space;
3453 unsigned pos, n1, n2;
3455 if (!bmap)
3456 return NULL;
3457 bmap = isl_basic_map_cow(bmap);
3458 if (!bmap)
3459 return NULL;
3460 space = isl_space_reverse(isl_space_copy(bmap->dim));
3461 pos = isl_basic_map_offset(bmap, isl_dim_in);
3462 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3463 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3464 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3465 return isl_basic_map_reset_space(bmap, space);
3468 static __isl_give isl_basic_map *basic_map_space_reset(
3469 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3471 isl_space *space;
3473 if (!bmap)
3474 return NULL;
3475 if (!isl_space_is_named_or_nested(bmap->dim, type))
3476 return bmap;
3478 space = isl_basic_map_get_space(bmap);
3479 space = isl_space_reset(space, type);
3480 bmap = isl_basic_map_reset_space(bmap, space);
3481 return bmap;
3484 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3485 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3486 unsigned pos, unsigned n)
3488 isl_bool rational;
3489 isl_space *res_dim;
3490 struct isl_basic_map *res;
3491 struct isl_dim_map *dim_map;
3492 unsigned total, off;
3493 enum isl_dim_type t;
3495 if (n == 0)
3496 return basic_map_space_reset(bmap, type);
3498 if (!bmap)
3499 return NULL;
3501 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3503 total = isl_basic_map_total_dim(bmap) + n;
3504 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3505 off = 0;
3506 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3507 if (t != type) {
3508 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3509 } else {
3510 unsigned size = isl_basic_map_dim(bmap, t);
3511 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3512 0, pos, off);
3513 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3514 pos, size - pos, off + pos + n);
3516 off += isl_space_dim(res_dim, t);
3518 isl_dim_map_div(dim_map, bmap, off);
3520 res = isl_basic_map_alloc_space(res_dim,
3521 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3522 rational = isl_basic_map_is_rational(bmap);
3523 if (rational < 0)
3524 res = isl_basic_map_free(res);
3525 if (rational)
3526 res = isl_basic_map_set_rational(res);
3527 if (isl_basic_map_plain_is_empty(bmap)) {
3528 isl_basic_map_free(bmap);
3529 free(dim_map);
3530 return isl_basic_map_set_to_empty(res);
3532 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3533 return isl_basic_map_finalize(res);
3536 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3537 __isl_take isl_basic_set *bset,
3538 enum isl_dim_type type, unsigned pos, unsigned n)
3540 return isl_basic_map_insert_dims(bset, type, pos, n);
3543 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3544 enum isl_dim_type type, unsigned n)
3546 if (!bmap)
3547 return NULL;
3548 return isl_basic_map_insert_dims(bmap, type,
3549 isl_basic_map_dim(bmap, type), n);
3552 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3553 enum isl_dim_type type, unsigned n)
3555 if (!bset)
3556 return NULL;
3557 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3558 return isl_basic_map_add_dims(bset, type, n);
3559 error:
3560 isl_basic_set_free(bset);
3561 return NULL;
3564 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3565 enum isl_dim_type type)
3567 isl_space *space;
3569 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3570 return map;
3572 space = isl_map_get_space(map);
3573 space = isl_space_reset(space, type);
3574 map = isl_map_reset_space(map, space);
3575 return map;
3578 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3579 enum isl_dim_type type, unsigned pos, unsigned n)
3581 int i;
3583 if (n == 0)
3584 return map_space_reset(map, type);
3586 map = isl_map_cow(map);
3587 if (!map)
3588 return NULL;
3590 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3591 if (!map->dim)
3592 goto error;
3594 for (i = 0; i < map->n; ++i) {
3595 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3596 if (!map->p[i])
3597 goto error;
3600 return map;
3601 error:
3602 isl_map_free(map);
3603 return NULL;
3606 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3607 enum isl_dim_type type, unsigned pos, unsigned n)
3609 return isl_map_insert_dims(set, type, pos, n);
3612 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3613 enum isl_dim_type type, unsigned n)
3615 if (!map)
3616 return NULL;
3617 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3620 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3621 enum isl_dim_type type, unsigned n)
3623 if (!set)
3624 return NULL;
3625 isl_assert(set->ctx, type != isl_dim_in, goto error);
3626 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3627 error:
3628 isl_set_free(set);
3629 return NULL;
3632 __isl_give isl_basic_map *isl_basic_map_move_dims(
3633 __isl_take isl_basic_map *bmap,
3634 enum isl_dim_type dst_type, unsigned dst_pos,
3635 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3637 struct isl_dim_map *dim_map;
3638 struct isl_basic_map *res;
3639 enum isl_dim_type t;
3640 unsigned total, off;
3642 if (!bmap)
3643 return NULL;
3644 if (n == 0)
3645 return bmap;
3647 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3648 return isl_basic_map_free(bmap);
3650 if (dst_type == src_type && dst_pos == src_pos)
3651 return bmap;
3653 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3655 if (pos(bmap->dim, dst_type) + dst_pos ==
3656 pos(bmap->dim, src_type) + src_pos +
3657 ((src_type < dst_type) ? n : 0)) {
3658 bmap = isl_basic_map_cow(bmap);
3659 if (!bmap)
3660 return NULL;
3662 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3663 src_type, src_pos, n);
3664 if (!bmap->dim)
3665 goto error;
3667 bmap = isl_basic_map_finalize(bmap);
3669 return bmap;
3672 total = isl_basic_map_total_dim(bmap);
3673 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3675 off = 0;
3676 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3677 unsigned size = isl_space_dim(bmap->dim, t);
3678 if (t == dst_type) {
3679 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3680 0, dst_pos, off);
3681 off += dst_pos;
3682 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3683 src_pos, n, off);
3684 off += n;
3685 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3686 dst_pos, size - dst_pos, off);
3687 off += size - dst_pos;
3688 } else if (t == src_type) {
3689 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3690 0, src_pos, off);
3691 off += src_pos;
3692 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3693 src_pos + n, size - src_pos - n, off);
3694 off += size - src_pos - n;
3695 } else {
3696 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3697 off += size;
3700 isl_dim_map_div(dim_map, bmap, off);
3702 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3703 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3704 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3705 if (!bmap)
3706 goto error;
3708 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3709 src_type, src_pos, n);
3710 if (!bmap->dim)
3711 goto error;
3713 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3714 bmap = isl_basic_map_gauss(bmap, NULL);
3715 bmap = isl_basic_map_finalize(bmap);
3717 return bmap;
3718 error:
3719 isl_basic_map_free(bmap);
3720 return NULL;
3723 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3724 enum isl_dim_type dst_type, unsigned dst_pos,
3725 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3727 isl_basic_map *bmap = bset_to_bmap(bset);
3728 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3729 src_type, src_pos, n);
3730 return bset_from_bmap(bmap);
3733 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3734 enum isl_dim_type dst_type, unsigned dst_pos,
3735 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3737 if (!set)
3738 return NULL;
3739 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3740 return set_from_map(isl_map_move_dims(set_to_map(set),
3741 dst_type, dst_pos, src_type, src_pos, n));
3742 error:
3743 isl_set_free(set);
3744 return NULL;
3747 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3748 enum isl_dim_type dst_type, unsigned dst_pos,
3749 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3751 int i;
3753 if (!map)
3754 return NULL;
3755 if (n == 0)
3756 return map;
3758 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3759 goto error);
3761 if (dst_type == src_type && dst_pos == src_pos)
3762 return map;
3764 isl_assert(map->ctx, dst_type != src_type, goto error);
3766 map = isl_map_cow(map);
3767 if (!map)
3768 return NULL;
3770 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3771 if (!map->dim)
3772 goto error;
3774 for (i = 0; i < map->n; ++i) {
3775 map->p[i] = isl_basic_map_move_dims(map->p[i],
3776 dst_type, dst_pos,
3777 src_type, src_pos, n);
3778 if (!map->p[i])
3779 goto error;
3782 return map;
3783 error:
3784 isl_map_free(map);
3785 return NULL;
3788 /* Move the specified dimensions to the last columns right before
3789 * the divs. Don't change the dimension specification of bmap.
3790 * That's the responsibility of the caller.
3792 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3793 enum isl_dim_type type, unsigned first, unsigned n)
3795 struct isl_dim_map *dim_map;
3796 struct isl_basic_map *res;
3797 enum isl_dim_type t;
3798 unsigned total, off;
3800 if (!bmap)
3801 return NULL;
3802 if (pos(bmap->dim, type) + first + n ==
3803 1 + isl_space_dim(bmap->dim, isl_dim_all))
3804 return bmap;
3806 total = isl_basic_map_total_dim(bmap);
3807 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3809 off = 0;
3810 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3811 unsigned size = isl_space_dim(bmap->dim, t);
3812 if (t == type) {
3813 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3814 0, first, off);
3815 off += first;
3816 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3817 first, n, total - bmap->n_div - n);
3818 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3819 first + n, size - (first + n), off);
3820 off += size - (first + n);
3821 } else {
3822 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3823 off += size;
3826 isl_dim_map_div(dim_map, bmap, off + n);
3828 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3829 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3830 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3831 return res;
3834 /* Insert "n" rows in the divs of "bmap".
3836 * The number of columns is not changed, which means that the last
3837 * dimensions of "bmap" are being reintepreted as the new divs.
3838 * The space of "bmap" is not adjusted, however, which means
3839 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3840 * from the space of "bmap" is the responsibility of the caller.
3842 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3843 int n)
3845 int i;
3846 size_t row_size;
3847 isl_int **new_div;
3848 isl_int *old;
3850 bmap = isl_basic_map_cow(bmap);
3851 if (!bmap)
3852 return NULL;
3854 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3855 old = bmap->block2.data;
3856 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3857 (bmap->extra + n) * (1 + row_size));
3858 if (!bmap->block2.data)
3859 return isl_basic_map_free(bmap);
3860 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3861 if (!new_div)
3862 return isl_basic_map_free(bmap);
3863 for (i = 0; i < n; ++i) {
3864 new_div[i] = bmap->block2.data +
3865 (bmap->extra + i) * (1 + row_size);
3866 isl_seq_clr(new_div[i], 1 + row_size);
3868 for (i = 0; i < bmap->extra; ++i)
3869 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3870 free(bmap->div);
3871 bmap->div = new_div;
3872 bmap->n_div += n;
3873 bmap->extra += n;
3875 return bmap;
3878 /* Drop constraints from "bmap" that only involve the variables
3879 * of "type" in the range [first, first + n] that are not related
3880 * to any of the variables outside that interval.
3881 * These constraints cannot influence the values for the variables
3882 * outside the interval, except in case they cause "bmap" to be empty.
3883 * Only drop the constraints if "bmap" is known to be non-empty.
3885 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3886 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3887 unsigned first, unsigned n)
3889 int i;
3890 int *groups;
3891 unsigned dim, n_div;
3892 isl_bool non_empty;
3894 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3895 if (non_empty < 0)
3896 return isl_basic_map_free(bmap);
3897 if (!non_empty)
3898 return bmap;
3900 dim = isl_basic_map_dim(bmap, isl_dim_all);
3901 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3902 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3903 if (!groups)
3904 return isl_basic_map_free(bmap);
3905 first += isl_basic_map_offset(bmap, type) - 1;
3906 for (i = 0; i < first; ++i)
3907 groups[i] = -1;
3908 for (i = first + n; i < dim - n_div; ++i)
3909 groups[i] = -1;
3911 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3913 return bmap;
3916 /* Turn the n dimensions of type type, starting at first
3917 * into existentially quantified variables.
3919 * If a subset of the projected out variables are unrelated
3920 * to any of the variables that remain, then the constraints
3921 * involving this subset are simply dropped first.
3923 __isl_give isl_basic_map *isl_basic_map_project_out(
3924 __isl_take isl_basic_map *bmap,
3925 enum isl_dim_type type, unsigned first, unsigned n)
3927 if (n == 0)
3928 return basic_map_space_reset(bmap, type);
3929 if (type == isl_dim_div)
3930 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3931 "cannot project out existentially quantified variables",
3932 return isl_basic_map_free(bmap));
3934 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3935 if (!bmap)
3936 return NULL;
3938 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3939 return isl_basic_map_remove_dims(bmap, type, first, n);
3941 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3942 return isl_basic_map_free(bmap);
3944 bmap = move_last(bmap, type, first, n);
3945 bmap = isl_basic_map_cow(bmap);
3946 bmap = insert_div_rows(bmap, n);
3947 if (!bmap)
3948 return NULL;
3950 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3951 if (!bmap->dim)
3952 goto error;
3953 bmap = isl_basic_map_simplify(bmap);
3954 bmap = isl_basic_map_drop_redundant_divs(bmap);
3955 return isl_basic_map_finalize(bmap);
3956 error:
3957 isl_basic_map_free(bmap);
3958 return NULL;
3961 /* Turn the n dimensions of type type, starting at first
3962 * into existentially quantified variables.
3964 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3965 enum isl_dim_type type, unsigned first, unsigned n)
3967 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3968 type, first, n));
3971 /* Turn the n dimensions of type type, starting at first
3972 * into existentially quantified variables.
3974 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3975 enum isl_dim_type type, unsigned first, unsigned n)
3977 int i;
3979 if (!map)
3980 return NULL;
3982 if (n == 0)
3983 return map_space_reset(map, type);
3985 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3987 map = isl_map_cow(map);
3988 if (!map)
3989 return NULL;
3991 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3992 if (!map->dim)
3993 goto error;
3995 for (i = 0; i < map->n; ++i) {
3996 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3997 if (!map->p[i])
3998 goto error;
4001 return map;
4002 error:
4003 isl_map_free(map);
4004 return NULL;
4007 /* Turn the n dimensions of type type, starting at first
4008 * into existentially quantified variables.
4010 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4011 enum isl_dim_type type, unsigned first, unsigned n)
4013 return set_from_map(isl_map_project_out(set_to_map(set),
4014 type, first, n));
4017 /* Return a map that projects the elements in "set" onto their
4018 * "n" set dimensions starting at "first".
4019 * "type" should be equal to isl_dim_set.
4021 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4022 enum isl_dim_type type, unsigned first, unsigned n)
4024 int i;
4025 int dim;
4026 isl_map *map;
4028 if (!set)
4029 return NULL;
4030 if (type != isl_dim_set)
4031 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4032 "only set dimensions can be projected out", goto error);
4033 dim = isl_set_dim(set, isl_dim_set);
4034 if (first + n > dim || first + n < first)
4035 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4036 "index out of bounds", goto error);
4038 map = isl_map_from_domain(set);
4039 map = isl_map_add_dims(map, isl_dim_out, n);
4040 for (i = 0; i < n; ++i)
4041 map = isl_map_equate(map, isl_dim_in, first + i,
4042 isl_dim_out, i);
4043 return map;
4044 error:
4045 isl_set_free(set);
4046 return NULL;
4049 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4051 int i, j;
4053 for (i = 0; i < n; ++i) {
4054 j = isl_basic_map_alloc_div(bmap);
4055 if (j < 0)
4056 goto error;
4057 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4059 return bmap;
4060 error:
4061 isl_basic_map_free(bmap);
4062 return NULL;
4065 struct isl_basic_map *isl_basic_map_apply_range(
4066 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4068 isl_space *dim_result = NULL;
4069 struct isl_basic_map *bmap;
4070 unsigned n_in, n_out, n, nparam, total, pos;
4071 struct isl_dim_map *dim_map1, *dim_map2;
4073 if (!bmap1 || !bmap2)
4074 goto error;
4075 if (!isl_space_match(bmap1->dim, isl_dim_param,
4076 bmap2->dim, isl_dim_param))
4077 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4078 "parameters don't match", goto error);
4079 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4080 bmap2->dim, isl_dim_in))
4081 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4082 "spaces don't match", goto error);
4084 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4085 isl_space_copy(bmap2->dim));
4087 n_in = isl_basic_map_n_in(bmap1);
4088 n_out = isl_basic_map_n_out(bmap2);
4089 n = isl_basic_map_n_out(bmap1);
4090 nparam = isl_basic_map_n_param(bmap1);
4092 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4093 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4094 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4095 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4096 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4097 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4098 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4099 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4100 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4101 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4102 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4104 bmap = isl_basic_map_alloc_space(dim_result,
4105 bmap1->n_div + bmap2->n_div + n,
4106 bmap1->n_eq + bmap2->n_eq,
4107 bmap1->n_ineq + bmap2->n_ineq);
4108 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4109 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4110 bmap = add_divs(bmap, n);
4111 bmap = isl_basic_map_simplify(bmap);
4112 bmap = isl_basic_map_drop_redundant_divs(bmap);
4113 return isl_basic_map_finalize(bmap);
4114 error:
4115 isl_basic_map_free(bmap1);
4116 isl_basic_map_free(bmap2);
4117 return NULL;
4120 struct isl_basic_set *isl_basic_set_apply(
4121 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4123 if (!bset || !bmap)
4124 goto error;
4126 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4127 goto error);
4129 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4130 bmap));
4131 error:
4132 isl_basic_set_free(bset);
4133 isl_basic_map_free(bmap);
4134 return NULL;
4137 struct isl_basic_map *isl_basic_map_apply_domain(
4138 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4140 if (!bmap1 || !bmap2)
4141 goto error;
4143 isl_assert(bmap1->ctx,
4144 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
4145 isl_assert(bmap1->ctx,
4146 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
4147 goto error);
4149 bmap1 = isl_basic_map_reverse(bmap1);
4150 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4151 return isl_basic_map_reverse(bmap1);
4152 error:
4153 isl_basic_map_free(bmap1);
4154 isl_basic_map_free(bmap2);
4155 return NULL;
4158 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4159 * A \cap B -> f(A) + f(B)
4161 struct isl_basic_map *isl_basic_map_sum(
4162 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4164 unsigned n_in, n_out, nparam, total, pos;
4165 struct isl_basic_map *bmap = NULL;
4166 struct isl_dim_map *dim_map1, *dim_map2;
4167 int i;
4169 if (!bmap1 || !bmap2)
4170 goto error;
4172 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4173 goto error);
4175 nparam = isl_basic_map_n_param(bmap1);
4176 n_in = isl_basic_map_n_in(bmap1);
4177 n_out = isl_basic_map_n_out(bmap1);
4179 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4180 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4181 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4182 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4183 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4184 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4185 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4186 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4187 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4188 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4189 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4191 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4192 bmap1->n_div + bmap2->n_div + 2 * n_out,
4193 bmap1->n_eq + bmap2->n_eq + n_out,
4194 bmap1->n_ineq + bmap2->n_ineq);
4195 for (i = 0; i < n_out; ++i) {
4196 int j = isl_basic_map_alloc_equality(bmap);
4197 if (j < 0)
4198 goto error;
4199 isl_seq_clr(bmap->eq[j], 1+total);
4200 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4201 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4202 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4204 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4205 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4206 bmap = add_divs(bmap, 2 * n_out);
4208 bmap = isl_basic_map_simplify(bmap);
4209 return isl_basic_map_finalize(bmap);
4210 error:
4211 isl_basic_map_free(bmap);
4212 isl_basic_map_free(bmap1);
4213 isl_basic_map_free(bmap2);
4214 return NULL;
4217 /* Given two maps A -> f(A) and B -> g(B), construct a map
4218 * A \cap B -> f(A) + f(B)
4220 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4222 struct isl_map *result;
4223 int i, j;
4225 if (!map1 || !map2)
4226 goto error;
4228 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4230 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4231 map1->n * map2->n, 0);
4232 if (!result)
4233 goto error;
4234 for (i = 0; i < map1->n; ++i)
4235 for (j = 0; j < map2->n; ++j) {
4236 struct isl_basic_map *part;
4237 part = isl_basic_map_sum(
4238 isl_basic_map_copy(map1->p[i]),
4239 isl_basic_map_copy(map2->p[j]));
4240 if (isl_basic_map_is_empty(part))
4241 isl_basic_map_free(part);
4242 else
4243 result = isl_map_add_basic_map(result, part);
4244 if (!result)
4245 goto error;
4247 isl_map_free(map1);
4248 isl_map_free(map2);
4249 return result;
4250 error:
4251 isl_map_free(map1);
4252 isl_map_free(map2);
4253 return NULL;
4256 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4257 __isl_take isl_set *set2)
4259 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4262 /* Given a basic map A -> f(A), construct A -> -f(A).
4264 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4266 int i, j;
4267 unsigned off, n;
4269 bmap = isl_basic_map_cow(bmap);
4270 if (!bmap)
4271 return NULL;
4273 n = isl_basic_map_dim(bmap, isl_dim_out);
4274 off = isl_basic_map_offset(bmap, isl_dim_out);
4275 for (i = 0; i < bmap->n_eq; ++i)
4276 for (j = 0; j < n; ++j)
4277 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4278 for (i = 0; i < bmap->n_ineq; ++i)
4279 for (j = 0; j < n; ++j)
4280 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4281 for (i = 0; i < bmap->n_div; ++i)
4282 for (j = 0; j < n; ++j)
4283 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4284 bmap = isl_basic_map_gauss(bmap, NULL);
4285 return isl_basic_map_finalize(bmap);
4288 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4290 return isl_basic_map_neg(bset);
4293 /* Given a map A -> f(A), construct A -> -f(A).
4295 struct isl_map *isl_map_neg(struct isl_map *map)
4297 int i;
4299 map = isl_map_cow(map);
4300 if (!map)
4301 return NULL;
4303 for (i = 0; i < map->n; ++i) {
4304 map->p[i] = isl_basic_map_neg(map->p[i]);
4305 if (!map->p[i])
4306 goto error;
4309 return map;
4310 error:
4311 isl_map_free(map);
4312 return NULL;
4315 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4317 return set_from_map(isl_map_neg(set_to_map(set)));
4320 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4321 * A -> floor(f(A)/d).
4323 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4324 isl_int d)
4326 unsigned n_in, n_out, nparam, total, pos;
4327 struct isl_basic_map *result = NULL;
4328 struct isl_dim_map *dim_map;
4329 int i;
4331 if (!bmap)
4332 return NULL;
4334 nparam = isl_basic_map_n_param(bmap);
4335 n_in = isl_basic_map_n_in(bmap);
4336 n_out = isl_basic_map_n_out(bmap);
4338 total = nparam + n_in + n_out + bmap->n_div + n_out;
4339 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4340 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4341 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4342 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4343 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4345 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4346 bmap->n_div + n_out,
4347 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4348 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4349 result = add_divs(result, n_out);
4350 for (i = 0; i < n_out; ++i) {
4351 int j;
4352 j = isl_basic_map_alloc_inequality(result);
4353 if (j < 0)
4354 goto error;
4355 isl_seq_clr(result->ineq[j], 1+total);
4356 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4357 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4358 j = isl_basic_map_alloc_inequality(result);
4359 if (j < 0)
4360 goto error;
4361 isl_seq_clr(result->ineq[j], 1+total);
4362 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4363 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4364 isl_int_sub_ui(result->ineq[j][0], d, 1);
4367 result = isl_basic_map_simplify(result);
4368 return isl_basic_map_finalize(result);
4369 error:
4370 isl_basic_map_free(result);
4371 return NULL;
4374 /* Given a map A -> f(A) and an integer d, construct a map
4375 * A -> floor(f(A)/d).
4377 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4379 int i;
4381 map = isl_map_cow(map);
4382 if (!map)
4383 return NULL;
4385 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4386 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4387 for (i = 0; i < map->n; ++i) {
4388 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4389 if (!map->p[i])
4390 goto error;
4393 return map;
4394 error:
4395 isl_map_free(map);
4396 return NULL;
4399 /* Given a map A -> f(A) and an integer d, construct a map
4400 * A -> floor(f(A)/d).
4402 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4403 __isl_take isl_val *d)
4405 if (!map || !d)
4406 goto error;
4407 if (!isl_val_is_int(d))
4408 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4409 "expecting integer denominator", goto error);
4410 map = isl_map_floordiv(map, d->n);
4411 isl_val_free(d);
4412 return map;
4413 error:
4414 isl_map_free(map);
4415 isl_val_free(d);
4416 return NULL;
4419 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4421 int i;
4422 unsigned nparam;
4423 unsigned n_in;
4425 i = isl_basic_map_alloc_equality(bmap);
4426 if (i < 0)
4427 goto error;
4428 nparam = isl_basic_map_n_param(bmap);
4429 n_in = isl_basic_map_n_in(bmap);
4430 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4431 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4432 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4433 return isl_basic_map_finalize(bmap);
4434 error:
4435 isl_basic_map_free(bmap);
4436 return NULL;
4439 /* Add a constraint to "bmap" expressing i_pos < o_pos
4441 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4443 int i;
4444 unsigned nparam;
4445 unsigned n_in;
4447 i = isl_basic_map_alloc_inequality(bmap);
4448 if (i < 0)
4449 goto error;
4450 nparam = isl_basic_map_n_param(bmap);
4451 n_in = isl_basic_map_n_in(bmap);
4452 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4453 isl_int_set_si(bmap->ineq[i][0], -1);
4454 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4455 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4456 return isl_basic_map_finalize(bmap);
4457 error:
4458 isl_basic_map_free(bmap);
4459 return NULL;
4462 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4464 static __isl_give isl_basic_map *var_less_or_equal(
4465 __isl_take isl_basic_map *bmap, unsigned pos)
4467 int i;
4468 unsigned nparam;
4469 unsigned n_in;
4471 i = isl_basic_map_alloc_inequality(bmap);
4472 if (i < 0)
4473 goto error;
4474 nparam = isl_basic_map_n_param(bmap);
4475 n_in = isl_basic_map_n_in(bmap);
4476 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4477 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4478 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4479 return isl_basic_map_finalize(bmap);
4480 error:
4481 isl_basic_map_free(bmap);
4482 return NULL;
4485 /* Add a constraint to "bmap" expressing i_pos > o_pos
4487 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4489 int i;
4490 unsigned nparam;
4491 unsigned n_in;
4493 i = isl_basic_map_alloc_inequality(bmap);
4494 if (i < 0)
4495 goto error;
4496 nparam = isl_basic_map_n_param(bmap);
4497 n_in = isl_basic_map_n_in(bmap);
4498 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4499 isl_int_set_si(bmap->ineq[i][0], -1);
4500 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4501 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4502 return isl_basic_map_finalize(bmap);
4503 error:
4504 isl_basic_map_free(bmap);
4505 return NULL;
4508 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4510 static __isl_give isl_basic_map *var_more_or_equal(
4511 __isl_take isl_basic_map *bmap, unsigned pos)
4513 int i;
4514 unsigned nparam;
4515 unsigned n_in;
4517 i = isl_basic_map_alloc_inequality(bmap);
4518 if (i < 0)
4519 goto error;
4520 nparam = isl_basic_map_n_param(bmap);
4521 n_in = isl_basic_map_n_in(bmap);
4522 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4523 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4524 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4525 return isl_basic_map_finalize(bmap);
4526 error:
4527 isl_basic_map_free(bmap);
4528 return NULL;
4531 __isl_give isl_basic_map *isl_basic_map_equal(
4532 __isl_take isl_space *dim, unsigned n_equal)
4534 int i;
4535 struct isl_basic_map *bmap;
4536 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4537 if (!bmap)
4538 return NULL;
4539 for (i = 0; i < n_equal && bmap; ++i)
4540 bmap = var_equal(bmap, i);
4541 return isl_basic_map_finalize(bmap);
4544 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4546 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4547 unsigned pos)
4549 int i;
4550 struct isl_basic_map *bmap;
4551 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4552 if (!bmap)
4553 return NULL;
4554 for (i = 0; i < pos && bmap; ++i)
4555 bmap = var_equal(bmap, i);
4556 if (bmap)
4557 bmap = var_less(bmap, pos);
4558 return isl_basic_map_finalize(bmap);
4561 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4563 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4564 __isl_take isl_space *dim, unsigned pos)
4566 int i;
4567 isl_basic_map *bmap;
4569 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4570 for (i = 0; i < pos; ++i)
4571 bmap = var_equal(bmap, i);
4572 bmap = var_less_or_equal(bmap, pos);
4573 return isl_basic_map_finalize(bmap);
4576 /* Return a relation on "dim" expressing i_pos > o_pos
4578 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4579 unsigned pos)
4581 int i;
4582 struct isl_basic_map *bmap;
4583 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4584 if (!bmap)
4585 return NULL;
4586 for (i = 0; i < pos && bmap; ++i)
4587 bmap = var_equal(bmap, i);
4588 if (bmap)
4589 bmap = var_more(bmap, pos);
4590 return isl_basic_map_finalize(bmap);
4593 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4595 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4596 __isl_take isl_space *dim, unsigned pos)
4598 int i;
4599 isl_basic_map *bmap;
4601 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4602 for (i = 0; i < pos; ++i)
4603 bmap = var_equal(bmap, i);
4604 bmap = var_more_or_equal(bmap, pos);
4605 return isl_basic_map_finalize(bmap);
4608 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4609 unsigned n, int equal)
4611 struct isl_map *map;
4612 int i;
4614 if (n == 0 && equal)
4615 return isl_map_universe(dims);
4617 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4619 for (i = 0; i + 1 < n; ++i)
4620 map = isl_map_add_basic_map(map,
4621 isl_basic_map_less_at(isl_space_copy(dims), i));
4622 if (n > 0) {
4623 if (equal)
4624 map = isl_map_add_basic_map(map,
4625 isl_basic_map_less_or_equal_at(dims, n - 1));
4626 else
4627 map = isl_map_add_basic_map(map,
4628 isl_basic_map_less_at(dims, n - 1));
4629 } else
4630 isl_space_free(dims);
4632 return map;
4635 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4637 if (!dims)
4638 return NULL;
4639 return map_lex_lte_first(dims, dims->n_out, equal);
4642 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4644 return map_lex_lte_first(dim, n, 0);
4647 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4649 return map_lex_lte_first(dim, n, 1);
4652 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4654 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4657 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4659 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4662 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4663 unsigned n, int equal)
4665 struct isl_map *map;
4666 int i;
4668 if (n == 0 && equal)
4669 return isl_map_universe(dims);
4671 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4673 for (i = 0; i + 1 < n; ++i)
4674 map = isl_map_add_basic_map(map,
4675 isl_basic_map_more_at(isl_space_copy(dims), i));
4676 if (n > 0) {
4677 if (equal)
4678 map = isl_map_add_basic_map(map,
4679 isl_basic_map_more_or_equal_at(dims, n - 1));
4680 else
4681 map = isl_map_add_basic_map(map,
4682 isl_basic_map_more_at(dims, n - 1));
4683 } else
4684 isl_space_free(dims);
4686 return map;
4689 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4691 if (!dims)
4692 return NULL;
4693 return map_lex_gte_first(dims, dims->n_out, equal);
4696 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4698 return map_lex_gte_first(dim, n, 0);
4701 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4703 return map_lex_gte_first(dim, n, 1);
4706 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4708 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4711 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4713 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4716 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4717 __isl_take isl_set *set2)
4719 isl_map *map;
4720 map = isl_map_lex_le(isl_set_get_space(set1));
4721 map = isl_map_intersect_domain(map, set1);
4722 map = isl_map_intersect_range(map, set2);
4723 return map;
4726 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4727 __isl_take isl_set *set2)
4729 isl_map *map;
4730 map = isl_map_lex_lt(isl_set_get_space(set1));
4731 map = isl_map_intersect_domain(map, set1);
4732 map = isl_map_intersect_range(map, set2);
4733 return map;
4736 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4737 __isl_take isl_set *set2)
4739 isl_map *map;
4740 map = isl_map_lex_ge(isl_set_get_space(set1));
4741 map = isl_map_intersect_domain(map, set1);
4742 map = isl_map_intersect_range(map, set2);
4743 return map;
4746 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4747 __isl_take isl_set *set2)
4749 isl_map *map;
4750 map = isl_map_lex_gt(isl_set_get_space(set1));
4751 map = isl_map_intersect_domain(map, set1);
4752 map = isl_map_intersect_range(map, set2);
4753 return map;
4756 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4757 __isl_take isl_map *map2)
4759 isl_map *map;
4760 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4761 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4762 map = isl_map_apply_range(map, isl_map_reverse(map2));
4763 return map;
4766 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4767 __isl_take isl_map *map2)
4769 isl_map *map;
4770 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4771 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4772 map = isl_map_apply_range(map, isl_map_reverse(map2));
4773 return map;
4776 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4777 __isl_take isl_map *map2)
4779 isl_map *map;
4780 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4781 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4782 map = isl_map_apply_range(map, isl_map_reverse(map2));
4783 return map;
4786 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4787 __isl_take isl_map *map2)
4789 isl_map *map;
4790 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4791 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4792 map = isl_map_apply_range(map, isl_map_reverse(map2));
4793 return map;
4796 static __isl_give isl_basic_map *basic_map_from_basic_set(
4797 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4799 struct isl_basic_map *bmap;
4801 bset = isl_basic_set_cow(bset);
4802 if (!bset || !dim)
4803 goto error;
4805 isl_assert(bset->ctx, isl_space_compatible_internal(bset->dim, dim),
4806 goto error);
4807 isl_space_free(bset->dim);
4808 bmap = bset_to_bmap(bset);
4809 bmap->dim = dim;
4810 return isl_basic_map_finalize(bmap);
4811 error:
4812 isl_basic_set_free(bset);
4813 isl_space_free(dim);
4814 return NULL;
4817 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4818 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
4820 return basic_map_from_basic_set(bset, space);
4823 /* For a div d = floor(f/m), add the constraint
4825 * f - m d >= 0
4827 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4828 unsigned pos, isl_int *div)
4830 int i;
4831 unsigned total = isl_basic_map_total_dim(bmap);
4833 i = isl_basic_map_alloc_inequality(bmap);
4834 if (i < 0)
4835 return isl_stat_error;
4836 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4837 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4839 return isl_stat_ok;
4842 /* For a div d = floor(f/m), add the constraint
4844 * -(f-(m-1)) + m d >= 0
4846 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4847 unsigned pos, isl_int *div)
4849 int i;
4850 unsigned total = isl_basic_map_total_dim(bmap);
4852 i = isl_basic_map_alloc_inequality(bmap);
4853 if (i < 0)
4854 return isl_stat_error;
4855 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4856 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4857 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4858 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4860 return isl_stat_ok;
4863 /* For a div d = floor(f/m), add the constraints
4865 * f - m d >= 0
4866 * -(f-(m-1)) + m d >= 0
4868 * Note that the second constraint is the negation of
4870 * f - m d >= m
4872 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4873 unsigned pos, isl_int *div)
4875 if (add_upper_div_constraint(bmap, pos, div) < 0)
4876 return -1;
4877 if (add_lower_div_constraint(bmap, pos, div) < 0)
4878 return -1;
4879 return 0;
4882 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4883 unsigned pos, isl_int *div)
4885 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4886 pos, div);
4889 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4891 unsigned total = isl_basic_map_total_dim(bmap);
4892 unsigned div_pos = total - bmap->n_div + div;
4894 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4895 bmap->div[div]);
4898 /* For each known div d = floor(f/m), add the constraints
4900 * f - m d >= 0
4901 * -(f-(m-1)) + m d >= 0
4903 * Remove duplicate constraints in case of some these div constraints
4904 * already appear in "bmap".
4906 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4907 __isl_take isl_basic_map *bmap)
4909 unsigned n_div;
4911 if (!bmap)
4912 return NULL;
4913 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4914 if (n_div == 0)
4915 return bmap;
4917 bmap = add_known_div_constraints(bmap);
4918 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4919 bmap = isl_basic_map_finalize(bmap);
4920 return bmap;
4923 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4925 * In particular, if this div is of the form d = floor(f/m),
4926 * then add the constraint
4928 * f - m d >= 0
4930 * if sign < 0 or the constraint
4932 * -(f-(m-1)) + m d >= 0
4934 * if sign > 0.
4936 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4937 unsigned div, int sign)
4939 unsigned total;
4940 unsigned div_pos;
4942 if (!bmap)
4943 return -1;
4945 total = isl_basic_map_total_dim(bmap);
4946 div_pos = total - bmap->n_div + div;
4948 if (sign < 0)
4949 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4950 else
4951 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4954 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4956 return isl_basic_map_add_div_constraints(bset, div);
4959 struct isl_basic_set *isl_basic_map_underlying_set(
4960 struct isl_basic_map *bmap)
4962 if (!bmap)
4963 goto error;
4964 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4965 bmap->n_div == 0 &&
4966 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4967 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4968 return bset_from_bmap(bmap);
4969 bmap = isl_basic_map_cow(bmap);
4970 if (!bmap)
4971 goto error;
4972 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4973 if (!bmap->dim)
4974 goto error;
4975 bmap->extra -= bmap->n_div;
4976 bmap->n_div = 0;
4977 bmap = isl_basic_map_finalize(bmap);
4978 return bset_from_bmap(bmap);
4979 error:
4980 isl_basic_map_free(bmap);
4981 return NULL;
4984 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4985 __isl_take isl_basic_set *bset)
4987 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4990 /* Replace each element in "list" by the result of applying
4991 * isl_basic_map_underlying_set to the element.
4993 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4994 __isl_take isl_basic_map_list *list)
4996 int i, n;
4998 if (!list)
4999 return NULL;
5001 n = isl_basic_map_list_n_basic_map(list);
5002 for (i = 0; i < n; ++i) {
5003 isl_basic_map *bmap;
5004 isl_basic_set *bset;
5006 bmap = isl_basic_map_list_get_basic_map(list, i);
5007 bset = isl_basic_set_underlying_set(bmap);
5008 list = isl_basic_set_list_set_basic_set(list, i, bset);
5011 return list;
5014 struct isl_basic_map *isl_basic_map_overlying_set(
5015 struct isl_basic_set *bset, struct isl_basic_map *like)
5017 struct isl_basic_map *bmap;
5018 struct isl_ctx *ctx;
5019 unsigned total;
5020 int i;
5022 if (!bset || !like)
5023 goto error;
5024 ctx = bset->ctx;
5025 isl_assert(ctx, bset->n_div == 0, goto error);
5026 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5027 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5028 goto error);
5029 if (like->n_div == 0) {
5030 isl_space *space = isl_basic_map_get_space(like);
5031 isl_basic_map_free(like);
5032 return isl_basic_map_reset_space(bset, space);
5034 bset = isl_basic_set_cow(bset);
5035 if (!bset)
5036 goto error;
5037 total = bset->dim->n_out + bset->extra;
5038 bmap = bset_to_bmap(bset);
5039 isl_space_free(bmap->dim);
5040 bmap->dim = isl_space_copy(like->dim);
5041 if (!bmap->dim)
5042 goto error;
5043 bmap->n_div = like->n_div;
5044 bmap->extra += like->n_div;
5045 if (bmap->extra) {
5046 unsigned ltotal;
5047 isl_int **div;
5048 ltotal = total - bmap->extra + like->extra;
5049 if (ltotal > total)
5050 ltotal = total;
5051 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5052 bmap->extra * (1 + 1 + total));
5053 if (isl_blk_is_error(bmap->block2))
5054 goto error;
5055 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5056 if (!div)
5057 goto error;
5058 bmap->div = div;
5059 for (i = 0; i < bmap->extra; ++i)
5060 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5061 for (i = 0; i < like->n_div; ++i) {
5062 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5063 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5065 bmap = isl_basic_map_add_known_div_constraints(bmap);
5067 isl_basic_map_free(like);
5068 bmap = isl_basic_map_simplify(bmap);
5069 bmap = isl_basic_map_finalize(bmap);
5070 return bmap;
5071 error:
5072 isl_basic_map_free(like);
5073 isl_basic_set_free(bset);
5074 return NULL;
5077 struct isl_basic_set *isl_basic_set_from_underlying_set(
5078 struct isl_basic_set *bset, struct isl_basic_set *like)
5080 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5081 bset_to_bmap(like)));
5084 struct isl_set *isl_set_from_underlying_set(
5085 struct isl_set *set, struct isl_basic_set *like)
5087 int i;
5089 if (!set || !like)
5090 goto error;
5091 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
5092 goto error);
5093 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
5094 isl_basic_set_free(like);
5095 return set;
5097 set = isl_set_cow(set);
5098 if (!set)
5099 goto error;
5100 for (i = 0; i < set->n; ++i) {
5101 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
5102 isl_basic_set_copy(like));
5103 if (!set->p[i])
5104 goto error;
5106 isl_space_free(set->dim);
5107 set->dim = isl_space_copy(like->dim);
5108 if (!set->dim)
5109 goto error;
5110 isl_basic_set_free(like);
5111 return set;
5112 error:
5113 isl_basic_set_free(like);
5114 isl_set_free(set);
5115 return NULL;
5118 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5120 int i;
5122 map = isl_map_cow(map);
5123 if (!map)
5124 return NULL;
5125 map->dim = isl_space_cow(map->dim);
5126 if (!map->dim)
5127 goto error;
5129 for (i = 1; i < map->n; ++i)
5130 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5131 goto error);
5132 for (i = 0; i < map->n; ++i) {
5133 map->p[i] = bset_to_bmap(
5134 isl_basic_map_underlying_set(map->p[i]));
5135 if (!map->p[i])
5136 goto error;
5138 if (map->n == 0)
5139 map->dim = isl_space_underlying(map->dim, 0);
5140 else {
5141 isl_space_free(map->dim);
5142 map->dim = isl_space_copy(map->p[0]->dim);
5144 if (!map->dim)
5145 goto error;
5146 return set_from_map(map);
5147 error:
5148 isl_map_free(map);
5149 return NULL;
5152 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
5154 return set_from_map(isl_map_underlying_set(set_to_map(set)));
5157 /* Replace the space of "bmap" by "space".
5159 * If the space of "bmap" is identical to "space" (including the identifiers
5160 * of the input and output dimensions), then simply return the original input.
5162 __isl_give isl_basic_map *isl_basic_map_reset_space(
5163 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5165 isl_bool equal;
5167 if (!bmap)
5168 goto error;
5169 equal = isl_space_is_equal(bmap->dim, space);
5170 if (equal >= 0 && equal)
5171 equal = isl_space_match(bmap->dim, isl_dim_in,
5172 space, isl_dim_in);
5173 if (equal >= 0 && equal)
5174 equal = isl_space_match(bmap->dim, isl_dim_out,
5175 space, isl_dim_out);
5176 if (equal < 0)
5177 goto error;
5178 if (equal) {
5179 isl_space_free(space);
5180 return bmap;
5182 bmap = isl_basic_map_cow(bmap);
5183 if (!bmap || !space)
5184 goto error;
5186 isl_space_free(bmap->dim);
5187 bmap->dim = space;
5189 bmap = isl_basic_map_finalize(bmap);
5191 return bmap;
5192 error:
5193 isl_basic_map_free(bmap);
5194 isl_space_free(space);
5195 return NULL;
5198 __isl_give isl_basic_set *isl_basic_set_reset_space(
5199 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5201 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5202 dim));
5205 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5206 __isl_take isl_space *dim)
5208 int i;
5210 map = isl_map_cow(map);
5211 if (!map || !dim)
5212 goto error;
5214 for (i = 0; i < map->n; ++i) {
5215 map->p[i] = isl_basic_map_reset_space(map->p[i],
5216 isl_space_copy(dim));
5217 if (!map->p[i])
5218 goto error;
5220 isl_space_free(map->dim);
5221 map->dim = dim;
5223 return map;
5224 error:
5225 isl_map_free(map);
5226 isl_space_free(dim);
5227 return NULL;
5230 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5231 __isl_take isl_space *dim)
5233 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5236 /* Compute the parameter domain of the given basic set.
5238 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5240 isl_bool is_params;
5241 isl_space *space;
5242 unsigned n;
5244 is_params = isl_basic_set_is_params(bset);
5245 if (is_params < 0)
5246 return isl_basic_set_free(bset);
5247 if (is_params)
5248 return bset;
5250 n = isl_basic_set_dim(bset, isl_dim_set);
5251 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5252 space = isl_basic_set_get_space(bset);
5253 space = isl_space_params(space);
5254 bset = isl_basic_set_reset_space(bset, space);
5255 return bset;
5258 /* Construct a zero-dimensional basic set with the given parameter domain.
5260 __isl_give isl_basic_set *isl_basic_set_from_params(
5261 __isl_take isl_basic_set *bset)
5263 isl_space *space;
5264 space = isl_basic_set_get_space(bset);
5265 space = isl_space_set_from_params(space);
5266 bset = isl_basic_set_reset_space(bset, space);
5267 return bset;
5270 /* Compute the parameter domain of the given set.
5272 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5274 isl_space *space;
5275 unsigned n;
5277 if (isl_set_is_params(set))
5278 return set;
5280 n = isl_set_dim(set, isl_dim_set);
5281 set = isl_set_project_out(set, isl_dim_set, 0, n);
5282 space = isl_set_get_space(set);
5283 space = isl_space_params(space);
5284 set = isl_set_reset_space(set, space);
5285 return set;
5288 /* Construct a zero-dimensional set with the given parameter domain.
5290 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5292 isl_space *space;
5293 space = isl_set_get_space(set);
5294 space = isl_space_set_from_params(space);
5295 set = isl_set_reset_space(set, space);
5296 return set;
5299 /* Compute the parameter domain of the given map.
5301 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5303 isl_space *space;
5304 unsigned n;
5306 n = isl_map_dim(map, isl_dim_in);
5307 map = isl_map_project_out(map, isl_dim_in, 0, n);
5308 n = isl_map_dim(map, isl_dim_out);
5309 map = isl_map_project_out(map, isl_dim_out, 0, n);
5310 space = isl_map_get_space(map);
5311 space = isl_space_params(space);
5312 map = isl_map_reset_space(map, space);
5313 return map;
5316 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5318 isl_space *space;
5319 unsigned n_out;
5321 if (!bmap)
5322 return NULL;
5323 space = isl_space_domain(isl_basic_map_get_space(bmap));
5325 n_out = isl_basic_map_n_out(bmap);
5326 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5328 return isl_basic_map_reset_space(bmap, space);
5331 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5333 if (!bmap)
5334 return isl_bool_error;
5335 return isl_space_may_be_set(bmap->dim);
5338 /* Is this basic map actually a set?
5339 * Users should never call this function. Outside of isl,
5340 * the type should indicate whether something is a set or a map.
5342 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5344 if (!bmap)
5345 return isl_bool_error;
5346 return isl_space_is_set(bmap->dim);
5349 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5351 isl_bool is_set;
5353 is_set = isl_basic_map_is_set(bmap);
5354 if (is_set < 0)
5355 goto error;
5356 if (is_set)
5357 return bmap;
5358 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5359 error:
5360 isl_basic_map_free(bmap);
5361 return NULL;
5364 __isl_give isl_basic_map *isl_basic_map_domain_map(
5365 __isl_take isl_basic_map *bmap)
5367 int i;
5368 isl_space *dim;
5369 isl_basic_map *domain;
5370 int nparam, n_in, n_out;
5372 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5373 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5374 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5376 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5377 domain = isl_basic_map_universe(dim);
5379 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5380 bmap = isl_basic_map_apply_range(bmap, domain);
5381 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5383 for (i = 0; i < n_in; ++i)
5384 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5385 isl_dim_out, i);
5387 bmap = isl_basic_map_gauss(bmap, NULL);
5388 return isl_basic_map_finalize(bmap);
5391 __isl_give isl_basic_map *isl_basic_map_range_map(
5392 __isl_take isl_basic_map *bmap)
5394 int i;
5395 isl_space *dim;
5396 isl_basic_map *range;
5397 int nparam, n_in, n_out;
5399 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5400 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5401 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5403 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5404 range = isl_basic_map_universe(dim);
5406 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5407 bmap = isl_basic_map_apply_range(bmap, range);
5408 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5410 for (i = 0; i < n_out; ++i)
5411 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5412 isl_dim_out, i);
5414 bmap = isl_basic_map_gauss(bmap, NULL);
5415 return isl_basic_map_finalize(bmap);
5418 int isl_map_may_be_set(__isl_keep isl_map *map)
5420 if (!map)
5421 return -1;
5422 return isl_space_may_be_set(map->dim);
5425 /* Is this map actually a set?
5426 * Users should never call this function. Outside of isl,
5427 * the type should indicate whether something is a set or a map.
5429 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5431 if (!map)
5432 return isl_bool_error;
5433 return isl_space_is_set(map->dim);
5436 struct isl_set *isl_map_range(struct isl_map *map)
5438 int i;
5439 isl_bool is_set;
5440 struct isl_set *set;
5442 is_set = isl_map_is_set(map);
5443 if (is_set < 0)
5444 goto error;
5445 if (is_set)
5446 return set_from_map(map);
5448 map = isl_map_cow(map);
5449 if (!map)
5450 goto error;
5452 set = set_from_map(map);
5453 set->dim = isl_space_range(set->dim);
5454 if (!set->dim)
5455 goto error;
5456 for (i = 0; i < map->n; ++i) {
5457 set->p[i] = isl_basic_map_range(map->p[i]);
5458 if (!set->p[i])
5459 goto error;
5461 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5462 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5463 return set;
5464 error:
5465 isl_map_free(map);
5466 return NULL;
5469 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5471 int i;
5473 map = isl_map_cow(map);
5474 if (!map)
5475 return NULL;
5477 map->dim = isl_space_domain_map(map->dim);
5478 if (!map->dim)
5479 goto error;
5480 for (i = 0; i < map->n; ++i) {
5481 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5482 if (!map->p[i])
5483 goto error;
5485 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5486 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5487 return map;
5488 error:
5489 isl_map_free(map);
5490 return NULL;
5493 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5495 int i;
5496 isl_space *range_dim;
5498 map = isl_map_cow(map);
5499 if (!map)
5500 return NULL;
5502 range_dim = isl_space_range(isl_map_get_space(map));
5503 range_dim = isl_space_from_range(range_dim);
5504 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5505 map->dim = isl_space_join(map->dim, range_dim);
5506 if (!map->dim)
5507 goto error;
5508 for (i = 0; i < map->n; ++i) {
5509 map->p[i] = isl_basic_map_range_map(map->p[i]);
5510 if (!map->p[i])
5511 goto error;
5513 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5514 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5515 return map;
5516 error:
5517 isl_map_free(map);
5518 return NULL;
5521 /* Given a wrapped map of the form A[B -> C],
5522 * return the map A[B -> C] -> B.
5524 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5526 isl_id *id;
5527 isl_map *map;
5529 if (!set)
5530 return NULL;
5531 if (!isl_set_has_tuple_id(set))
5532 return isl_map_domain_map(isl_set_unwrap(set));
5534 id = isl_set_get_tuple_id(set);
5535 map = isl_map_domain_map(isl_set_unwrap(set));
5536 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5538 return map;
5541 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5542 __isl_take isl_space *dim)
5544 int i;
5545 struct isl_map *map = NULL;
5547 set = isl_set_cow(set);
5548 if (!set || !dim)
5549 goto error;
5550 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5551 goto error);
5552 map = set_to_map(set);
5553 for (i = 0; i < set->n; ++i) {
5554 map->p[i] = basic_map_from_basic_set(
5555 set->p[i], isl_space_copy(dim));
5556 if (!map->p[i])
5557 goto error;
5559 isl_space_free(map->dim);
5560 map->dim = dim;
5561 return map;
5562 error:
5563 isl_space_free(dim);
5564 isl_set_free(set);
5565 return NULL;
5568 __isl_give isl_basic_map *isl_basic_map_from_domain(
5569 __isl_take isl_basic_set *bset)
5571 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5574 __isl_give isl_basic_map *isl_basic_map_from_range(
5575 __isl_take isl_basic_set *bset)
5577 isl_space *space;
5578 space = isl_basic_set_get_space(bset);
5579 space = isl_space_from_range(space);
5580 bset = isl_basic_set_reset_space(bset, space);
5581 return bset_to_bmap(bset);
5584 /* Create a relation with the given set as range.
5585 * The domain of the created relation is a zero-dimensional
5586 * flat anonymous space.
5588 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5590 isl_space *space;
5591 space = isl_set_get_space(set);
5592 space = isl_space_from_range(space);
5593 set = isl_set_reset_space(set, space);
5594 return set_to_map(set);
5597 /* Create a relation with the given set as domain.
5598 * The range of the created relation is a zero-dimensional
5599 * flat anonymous space.
5601 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5603 return isl_map_reverse(isl_map_from_range(set));
5606 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5607 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5609 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5612 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5613 __isl_take isl_set *range)
5615 return isl_map_apply_range(isl_map_reverse(domain), range);
5618 /* Return a newly allocated isl_map with given space and flags and
5619 * room for "n" basic maps.
5620 * Make sure that all cached information is cleared.
5622 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5623 unsigned flags)
5625 struct isl_map *map;
5627 if (!space)
5628 return NULL;
5629 if (n < 0)
5630 isl_die(space->ctx, isl_error_internal,
5631 "negative number of basic maps", goto error);
5632 map = isl_calloc(space->ctx, struct isl_map,
5633 sizeof(struct isl_map) +
5634 (n - 1) * sizeof(struct isl_basic_map *));
5635 if (!map)
5636 goto error;
5638 map->ctx = space->ctx;
5639 isl_ctx_ref(map->ctx);
5640 map->ref = 1;
5641 map->size = n;
5642 map->n = 0;
5643 map->dim = space;
5644 map->flags = flags;
5645 return map;
5646 error:
5647 isl_space_free(space);
5648 return NULL;
5651 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5652 unsigned nparam, unsigned in, unsigned out, int n,
5653 unsigned flags)
5655 struct isl_map *map;
5656 isl_space *dims;
5658 dims = isl_space_alloc(ctx, nparam, in, out);
5659 if (!dims)
5660 return NULL;
5662 map = isl_map_alloc_space(dims, n, flags);
5663 return map;
5666 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5668 struct isl_basic_map *bmap;
5669 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5670 bmap = isl_basic_map_set_to_empty(bmap);
5671 return bmap;
5674 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5676 struct isl_basic_set *bset;
5677 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5678 bset = isl_basic_set_set_to_empty(bset);
5679 return bset;
5682 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5684 struct isl_basic_map *bmap;
5685 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5686 bmap = isl_basic_map_finalize(bmap);
5687 return bmap;
5690 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5692 struct isl_basic_set *bset;
5693 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5694 bset = isl_basic_set_finalize(bset);
5695 return bset;
5698 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5700 int i;
5701 unsigned total = isl_space_dim(dim, isl_dim_all);
5702 isl_basic_map *bmap;
5704 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5705 for (i = 0; i < total; ++i) {
5706 int k = isl_basic_map_alloc_inequality(bmap);
5707 if (k < 0)
5708 goto error;
5709 isl_seq_clr(bmap->ineq[k], 1 + total);
5710 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5712 return bmap;
5713 error:
5714 isl_basic_map_free(bmap);
5715 return NULL;
5718 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5720 return isl_basic_map_nat_universe(dim);
5723 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5725 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5728 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5730 return isl_map_nat_universe(dim);
5733 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5735 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5738 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5740 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5743 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5745 struct isl_map *map;
5746 if (!dim)
5747 return NULL;
5748 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5749 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5750 return map;
5753 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5755 struct isl_set *set;
5756 if (!dim)
5757 return NULL;
5758 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5759 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5760 return set;
5763 struct isl_map *isl_map_dup(struct isl_map *map)
5765 int i;
5766 struct isl_map *dup;
5768 if (!map)
5769 return NULL;
5770 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5771 for (i = 0; i < map->n; ++i)
5772 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5773 return dup;
5776 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5777 __isl_take isl_basic_map *bmap)
5779 if (!bmap || !map)
5780 goto error;
5781 if (isl_basic_map_plain_is_empty(bmap)) {
5782 isl_basic_map_free(bmap);
5783 return map;
5785 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5786 isl_assert(map->ctx, map->n < map->size, goto error);
5787 map->p[map->n] = bmap;
5788 map->n++;
5789 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5790 return map;
5791 error:
5792 if (map)
5793 isl_map_free(map);
5794 if (bmap)
5795 isl_basic_map_free(bmap);
5796 return NULL;
5799 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5801 int i;
5803 if (!map)
5804 return NULL;
5806 if (--map->ref > 0)
5807 return NULL;
5809 clear_caches(map);
5810 isl_ctx_deref(map->ctx);
5811 for (i = 0; i < map->n; ++i)
5812 isl_basic_map_free(map->p[i]);
5813 isl_space_free(map->dim);
5814 free(map);
5816 return NULL;
5819 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5820 struct isl_basic_map *bmap, unsigned pos, int value)
5822 int j;
5824 bmap = isl_basic_map_cow(bmap);
5825 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5826 j = isl_basic_map_alloc_equality(bmap);
5827 if (j < 0)
5828 goto error;
5829 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5830 isl_int_set_si(bmap->eq[j][pos], -1);
5831 isl_int_set_si(bmap->eq[j][0], value);
5832 bmap = isl_basic_map_simplify(bmap);
5833 return isl_basic_map_finalize(bmap);
5834 error:
5835 isl_basic_map_free(bmap);
5836 return NULL;
5839 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5840 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5842 int j;
5844 bmap = isl_basic_map_cow(bmap);
5845 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5846 j = isl_basic_map_alloc_equality(bmap);
5847 if (j < 0)
5848 goto error;
5849 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5850 isl_int_set_si(bmap->eq[j][pos], -1);
5851 isl_int_set(bmap->eq[j][0], value);
5852 bmap = isl_basic_map_simplify(bmap);
5853 return isl_basic_map_finalize(bmap);
5854 error:
5855 isl_basic_map_free(bmap);
5856 return NULL;
5859 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5860 enum isl_dim_type type, unsigned pos, int value)
5862 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5863 return isl_basic_map_free(bmap);
5864 return isl_basic_map_fix_pos_si(bmap,
5865 isl_basic_map_offset(bmap, type) + pos, value);
5868 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5869 enum isl_dim_type type, unsigned pos, isl_int value)
5871 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5872 return isl_basic_map_free(bmap);
5873 return isl_basic_map_fix_pos(bmap,
5874 isl_basic_map_offset(bmap, type) + pos, value);
5877 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5878 * to be equal to "v".
5880 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5881 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5883 if (!bmap || !v)
5884 goto error;
5885 if (!isl_val_is_int(v))
5886 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5887 "expecting integer value", goto error);
5888 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5889 goto error;
5890 pos += isl_basic_map_offset(bmap, type);
5891 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5892 isl_val_free(v);
5893 return bmap;
5894 error:
5895 isl_basic_map_free(bmap);
5896 isl_val_free(v);
5897 return NULL;
5900 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5901 * to be equal to "v".
5903 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5904 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5906 return isl_basic_map_fix_val(bset, type, pos, v);
5909 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5910 enum isl_dim_type type, unsigned pos, int value)
5912 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5913 type, pos, value));
5916 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5917 enum isl_dim_type type, unsigned pos, isl_int value)
5919 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5920 type, pos, value));
5923 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5924 unsigned input, int value)
5926 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5929 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5930 unsigned dim, int value)
5932 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5933 isl_dim_set, dim, value));
5936 static int remove_if_empty(__isl_keep isl_map *map, int i)
5938 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5940 if (empty < 0)
5941 return -1;
5942 if (!empty)
5943 return 0;
5945 isl_basic_map_free(map->p[i]);
5946 if (i != map->n - 1) {
5947 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5948 map->p[i] = map->p[map->n - 1];
5950 map->n--;
5952 return 0;
5955 /* Perform "fn" on each basic map of "map", where we may not be holding
5956 * the only reference to "map".
5957 * In particular, "fn" should be a semantics preserving operation
5958 * that we want to apply to all copies of "map". We therefore need
5959 * to be careful not to modify "map" in a way that breaks "map"
5960 * in case anything goes wrong.
5962 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5963 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5965 struct isl_basic_map *bmap;
5966 int i;
5968 if (!map)
5969 return NULL;
5971 for (i = map->n - 1; i >= 0; --i) {
5972 bmap = isl_basic_map_copy(map->p[i]);
5973 bmap = fn(bmap);
5974 if (!bmap)
5975 goto error;
5976 isl_basic_map_free(map->p[i]);
5977 map->p[i] = bmap;
5978 if (remove_if_empty(map, i) < 0)
5979 goto error;
5982 return map;
5983 error:
5984 isl_map_free(map);
5985 return NULL;
5988 struct isl_map *isl_map_fix_si(struct isl_map *map,
5989 enum isl_dim_type type, unsigned pos, int value)
5991 int i;
5993 map = isl_map_cow(map);
5994 if (!map)
5995 return NULL;
5997 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5998 for (i = map->n - 1; i >= 0; --i) {
5999 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6000 if (remove_if_empty(map, i) < 0)
6001 goto error;
6003 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6004 return map;
6005 error:
6006 isl_map_free(map);
6007 return NULL;
6010 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6011 enum isl_dim_type type, unsigned pos, int value)
6013 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6016 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6017 enum isl_dim_type type, unsigned pos, isl_int value)
6019 int i;
6021 map = isl_map_cow(map);
6022 if (!map)
6023 return NULL;
6025 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6026 for (i = 0; i < map->n; ++i) {
6027 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6028 if (!map->p[i])
6029 goto error;
6031 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6032 return map;
6033 error:
6034 isl_map_free(map);
6035 return NULL;
6038 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6039 enum isl_dim_type type, unsigned pos, isl_int value)
6041 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6044 /* Fix the value of the variable at position "pos" of type "type" of "map"
6045 * to be equal to "v".
6047 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6048 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6050 int i;
6052 map = isl_map_cow(map);
6053 if (!map || !v)
6054 goto error;
6056 if (!isl_val_is_int(v))
6057 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6058 "expecting integer value", goto error);
6059 if (pos >= isl_map_dim(map, type))
6060 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6061 "index out of bounds", goto error);
6062 for (i = map->n - 1; i >= 0; --i) {
6063 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6064 isl_val_copy(v));
6065 if (remove_if_empty(map, i) < 0)
6066 goto error;
6068 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6069 isl_val_free(v);
6070 return map;
6071 error:
6072 isl_map_free(map);
6073 isl_val_free(v);
6074 return NULL;
6077 /* Fix the value of the variable at position "pos" of type "type" of "set"
6078 * to be equal to "v".
6080 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6081 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6083 return isl_map_fix_val(set, type, pos, v);
6086 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6087 unsigned input, int value)
6089 return isl_map_fix_si(map, isl_dim_in, input, value);
6092 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6094 return set_from_map(isl_map_fix_si(set_to_map(set),
6095 isl_dim_set, dim, value));
6098 static __isl_give isl_basic_map *basic_map_bound_si(
6099 __isl_take isl_basic_map *bmap,
6100 enum isl_dim_type type, unsigned pos, int value, int upper)
6102 int j;
6104 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6105 return isl_basic_map_free(bmap);
6106 pos += isl_basic_map_offset(bmap, type);
6107 bmap = isl_basic_map_cow(bmap);
6108 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6109 j = isl_basic_map_alloc_inequality(bmap);
6110 if (j < 0)
6111 goto error;
6112 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6113 if (upper) {
6114 isl_int_set_si(bmap->ineq[j][pos], -1);
6115 isl_int_set_si(bmap->ineq[j][0], value);
6116 } else {
6117 isl_int_set_si(bmap->ineq[j][pos], 1);
6118 isl_int_set_si(bmap->ineq[j][0], -value);
6120 bmap = isl_basic_map_simplify(bmap);
6121 return isl_basic_map_finalize(bmap);
6122 error:
6123 isl_basic_map_free(bmap);
6124 return NULL;
6127 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6128 __isl_take isl_basic_map *bmap,
6129 enum isl_dim_type type, unsigned pos, int value)
6131 return basic_map_bound_si(bmap, type, pos, value, 0);
6134 /* Constrain the values of the given dimension to be no greater than "value".
6136 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6137 __isl_take isl_basic_map *bmap,
6138 enum isl_dim_type type, unsigned pos, int value)
6140 return basic_map_bound_si(bmap, type, pos, value, 1);
6143 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
6144 unsigned dim, isl_int value)
6146 int j;
6148 bset = isl_basic_set_cow(bset);
6149 bset = isl_basic_set_extend_constraints(bset, 0, 1);
6150 j = isl_basic_set_alloc_inequality(bset);
6151 if (j < 0)
6152 goto error;
6153 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
6154 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
6155 isl_int_neg(bset->ineq[j][0], value);
6156 bset = isl_basic_set_simplify(bset);
6157 return isl_basic_set_finalize(bset);
6158 error:
6159 isl_basic_set_free(bset);
6160 return NULL;
6163 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6164 enum isl_dim_type type, unsigned pos, int value, int upper)
6166 int i;
6168 map = isl_map_cow(map);
6169 if (!map)
6170 return NULL;
6172 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6173 for (i = 0; i < map->n; ++i) {
6174 map->p[i] = basic_map_bound_si(map->p[i],
6175 type, pos, value, upper);
6176 if (!map->p[i])
6177 goto error;
6179 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6180 return map;
6181 error:
6182 isl_map_free(map);
6183 return NULL;
6186 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6187 enum isl_dim_type type, unsigned pos, int value)
6189 return map_bound_si(map, type, pos, value, 0);
6192 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6193 enum isl_dim_type type, unsigned pos, int value)
6195 return map_bound_si(map, type, pos, value, 1);
6198 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6199 enum isl_dim_type type, unsigned pos, int value)
6201 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6202 type, pos, value));
6205 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6206 enum isl_dim_type type, unsigned pos, int value)
6208 return isl_map_upper_bound_si(set, type, pos, value);
6211 /* Bound the given variable of "bmap" from below (or above is "upper"
6212 * is set) to "value".
6214 static __isl_give isl_basic_map *basic_map_bound(
6215 __isl_take isl_basic_map *bmap,
6216 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6218 int j;
6220 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6221 return isl_basic_map_free(bmap);
6222 pos += isl_basic_map_offset(bmap, type);
6223 bmap = isl_basic_map_cow(bmap);
6224 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6225 j = isl_basic_map_alloc_inequality(bmap);
6226 if (j < 0)
6227 goto error;
6228 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6229 if (upper) {
6230 isl_int_set_si(bmap->ineq[j][pos], -1);
6231 isl_int_set(bmap->ineq[j][0], value);
6232 } else {
6233 isl_int_set_si(bmap->ineq[j][pos], 1);
6234 isl_int_neg(bmap->ineq[j][0], value);
6236 bmap = isl_basic_map_simplify(bmap);
6237 return isl_basic_map_finalize(bmap);
6238 error:
6239 isl_basic_map_free(bmap);
6240 return NULL;
6243 /* Bound the given variable of "map" from below (or above is "upper"
6244 * is set) to "value".
6246 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6247 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6249 int i;
6251 map = isl_map_cow(map);
6252 if (!map)
6253 return NULL;
6255 if (pos >= isl_map_dim(map, type))
6256 isl_die(map->ctx, isl_error_invalid,
6257 "index out of bounds", goto error);
6258 for (i = map->n - 1; i >= 0; --i) {
6259 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6260 if (remove_if_empty(map, i) < 0)
6261 goto error;
6263 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6264 return map;
6265 error:
6266 isl_map_free(map);
6267 return NULL;
6270 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6271 enum isl_dim_type type, unsigned pos, isl_int value)
6273 return map_bound(map, type, pos, value, 0);
6276 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6277 enum isl_dim_type type, unsigned pos, isl_int value)
6279 return map_bound(map, type, pos, value, 1);
6282 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6283 enum isl_dim_type type, unsigned pos, isl_int value)
6285 return isl_map_lower_bound(set, type, pos, value);
6288 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6289 enum isl_dim_type type, unsigned pos, isl_int value)
6291 return isl_map_upper_bound(set, type, pos, value);
6294 /* Force the values of the variable at position "pos" of type "type" of "set"
6295 * to be no smaller than "value".
6297 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6298 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6300 if (!value)
6301 goto error;
6302 if (!isl_val_is_int(value))
6303 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6304 "expecting integer value", goto error);
6305 set = isl_set_lower_bound(set, type, pos, value->n);
6306 isl_val_free(value);
6307 return set;
6308 error:
6309 isl_val_free(value);
6310 isl_set_free(set);
6311 return NULL;
6314 /* Force the values of the variable at position "pos" of type "type" of "set"
6315 * to be no greater than "value".
6317 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6318 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6320 if (!value)
6321 goto error;
6322 if (!isl_val_is_int(value))
6323 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6324 "expecting integer value", goto error);
6325 set = isl_set_upper_bound(set, type, pos, value->n);
6326 isl_val_free(value);
6327 return set;
6328 error:
6329 isl_val_free(value);
6330 isl_set_free(set);
6331 return NULL;
6334 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6335 isl_int value)
6337 int i;
6339 set = isl_set_cow(set);
6340 if (!set)
6341 return NULL;
6343 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6344 for (i = 0; i < set->n; ++i) {
6345 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6346 if (!set->p[i])
6347 goto error;
6349 return set;
6350 error:
6351 isl_set_free(set);
6352 return NULL;
6355 struct isl_map *isl_map_reverse(struct isl_map *map)
6357 int i;
6359 map = isl_map_cow(map);
6360 if (!map)
6361 return NULL;
6363 map->dim = isl_space_reverse(map->dim);
6364 if (!map->dim)
6365 goto error;
6366 for (i = 0; i < map->n; ++i) {
6367 map->p[i] = isl_basic_map_reverse(map->p[i]);
6368 if (!map->p[i])
6369 goto error;
6371 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6372 return map;
6373 error:
6374 isl_map_free(map);
6375 return NULL;
6378 #undef TYPE
6379 #define TYPE isl_pw_multi_aff
6380 #undef SUFFIX
6381 #define SUFFIX _pw_multi_aff
6382 #undef EMPTY
6383 #define EMPTY isl_pw_multi_aff_empty
6384 #undef ADD
6385 #define ADD isl_pw_multi_aff_union_add
6386 #include "isl_map_lexopt_templ.c"
6388 /* Given a map "map", compute the lexicographically minimal
6389 * (or maximal) image element for each domain element in dom,
6390 * in the form of an isl_pw_multi_aff.
6391 * If "empty" is not NULL, then set *empty to those elements in dom that
6392 * do not have an image element.
6393 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6394 * should be computed over the domain of "map". "empty" is also NULL
6395 * in this case.
6397 * We first compute the lexicographically minimal or maximal element
6398 * in the first basic map. This results in a partial solution "res"
6399 * and a subset "todo" of dom that still need to be handled.
6400 * We then consider each of the remaining maps in "map" and successively
6401 * update both "res" and "todo".
6402 * If "empty" is NULL, then the todo sets are not needed and therefore
6403 * also not computed.
6405 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6406 __isl_take isl_map *map, __isl_take isl_set *dom,
6407 __isl_give isl_set **empty, unsigned flags)
6409 int i;
6410 int full;
6411 isl_pw_multi_aff *res;
6412 isl_set *todo;
6414 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6415 if (!map || (!full && !dom))
6416 goto error;
6418 if (isl_map_plain_is_empty(map)) {
6419 if (empty)
6420 *empty = dom;
6421 else
6422 isl_set_free(dom);
6423 return isl_pw_multi_aff_from_map(map);
6426 res = basic_map_partial_lexopt_pw_multi_aff(
6427 isl_basic_map_copy(map->p[0]),
6428 isl_set_copy(dom), empty, flags);
6430 if (empty)
6431 todo = *empty;
6432 for (i = 1; i < map->n; ++i) {
6433 isl_pw_multi_aff *res_i;
6435 res_i = basic_map_partial_lexopt_pw_multi_aff(
6436 isl_basic_map_copy(map->p[i]),
6437 isl_set_copy(dom), empty, flags);
6439 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6440 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6441 else
6442 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6444 if (empty)
6445 todo = isl_set_intersect(todo, *empty);
6448 isl_set_free(dom);
6449 isl_map_free(map);
6451 if (empty)
6452 *empty = todo;
6454 return res;
6455 error:
6456 if (empty)
6457 *empty = NULL;
6458 isl_set_free(dom);
6459 isl_map_free(map);
6460 return NULL;
6463 #undef TYPE
6464 #define TYPE isl_map
6465 #undef SUFFIX
6466 #define SUFFIX
6467 #undef EMPTY
6468 #define EMPTY isl_map_empty
6469 #undef ADD
6470 #define ADD isl_map_union_disjoint
6471 #include "isl_map_lexopt_templ.c"
6473 /* Given a map "map", compute the lexicographically minimal
6474 * (or maximal) image element for each domain element in "dom",
6475 * in the form of an isl_map.
6476 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6477 * do not have an image element.
6478 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6479 * should be computed over the domain of "map". "empty" is also NULL
6480 * in this case.
6482 * If the input consists of more than one disjunct, then first
6483 * compute the desired result in the form of an isl_pw_multi_aff and
6484 * then convert that into an isl_map.
6486 * This function used to have an explicit implementation in terms
6487 * of isl_maps, but it would continually intersect the domains of
6488 * partial results with the complement of the domain of the next
6489 * partial solution, potentially leading to an explosion in the number
6490 * of disjuncts if there are several disjuncts in the input.
6491 * An even earlier implementation of this function would look for
6492 * better results in the domain of the partial result and for extra
6493 * results in the complement of this domain, which would lead to
6494 * even more splintering.
6496 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6497 __isl_take isl_map *map, __isl_take isl_set *dom,
6498 __isl_give isl_set **empty, unsigned flags)
6500 int full;
6501 struct isl_map *res;
6502 isl_pw_multi_aff *pma;
6504 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6505 if (!map || (!full && !dom))
6506 goto error;
6508 if (isl_map_plain_is_empty(map)) {
6509 if (empty)
6510 *empty = dom;
6511 else
6512 isl_set_free(dom);
6513 return map;
6516 if (map->n == 1) {
6517 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6518 dom, empty, flags);
6519 isl_map_free(map);
6520 return res;
6523 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6524 flags);
6525 return isl_map_from_pw_multi_aff(pma);
6526 error:
6527 if (empty)
6528 *empty = NULL;
6529 isl_set_free(dom);
6530 isl_map_free(map);
6531 return NULL;
6534 __isl_give isl_map *isl_map_partial_lexmax(
6535 __isl_take isl_map *map, __isl_take isl_set *dom,
6536 __isl_give isl_set **empty)
6538 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6541 __isl_give isl_map *isl_map_partial_lexmin(
6542 __isl_take isl_map *map, __isl_take isl_set *dom,
6543 __isl_give isl_set **empty)
6545 return isl_map_partial_lexopt(map, dom, empty, 0);
6548 __isl_give isl_set *isl_set_partial_lexmin(
6549 __isl_take isl_set *set, __isl_take isl_set *dom,
6550 __isl_give isl_set **empty)
6552 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6553 dom, empty));
6556 __isl_give isl_set *isl_set_partial_lexmax(
6557 __isl_take isl_set *set, __isl_take isl_set *dom,
6558 __isl_give isl_set **empty)
6560 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6561 dom, empty));
6564 /* Compute the lexicographic minimum (or maximum if "flags" includes
6565 * ISL_OPT_MAX) of "bset" over its parametric domain.
6567 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6568 unsigned flags)
6570 return isl_basic_map_lexopt(bset, flags);
6573 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6575 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6578 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6580 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6583 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6585 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6588 /* Compute the lexicographic minimum of "bset" over its parametric domain
6589 * for the purpose of quantifier elimination.
6590 * That is, find an explicit representation for all the existentially
6591 * quantified variables in "bset" by computing their lexicographic
6592 * minimum.
6594 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6595 __isl_take isl_basic_set *bset)
6597 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6600 /* Extract the first and only affine expression from list
6601 * and then add it to *pwaff with the given dom.
6602 * This domain is known to be disjoint from other domains
6603 * because of the way isl_basic_map_foreach_lexmax works.
6605 static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
6606 __isl_take isl_aff_list *list, void *user)
6608 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6609 isl_aff *aff;
6610 isl_pw_aff **pwaff = user;
6611 isl_pw_aff *pwaff_i;
6613 if (!list)
6614 goto error;
6615 if (isl_aff_list_n_aff(list) != 1)
6616 isl_die(ctx, isl_error_internal,
6617 "expecting single element list", goto error);
6619 aff = isl_aff_list_get_aff(list, 0);
6620 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6622 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6624 isl_aff_list_free(list);
6626 return isl_stat_ok;
6627 error:
6628 isl_basic_set_free(dom);
6629 isl_aff_list_free(list);
6630 return isl_stat_error;
6633 /* Given a basic map with one output dimension, compute the minimum or
6634 * maximum of that dimension as an isl_pw_aff.
6636 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6637 * call update_dim_opt on each leaf of the result.
6639 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6640 int max)
6642 isl_space *dim = isl_basic_map_get_space(bmap);
6643 isl_pw_aff *pwaff;
6644 isl_stat r;
6646 dim = isl_space_from_domain(isl_space_domain(dim));
6647 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6648 pwaff = isl_pw_aff_empty(dim);
6650 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6651 if (r < 0)
6652 return isl_pw_aff_free(pwaff);
6654 return pwaff;
6657 /* Compute the minimum or maximum of the given output dimension
6658 * as a function of the parameters and the input dimensions,
6659 * but independently of the other output dimensions.
6661 * We first project out the other output dimension and then compute
6662 * the "lexicographic" maximum in each basic map, combining the results
6663 * using isl_pw_aff_union_max.
6665 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6666 int max)
6668 int i;
6669 isl_pw_aff *pwaff;
6670 unsigned n_out;
6672 n_out = isl_map_dim(map, isl_dim_out);
6673 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6674 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6675 if (!map)
6676 return NULL;
6678 if (map->n == 0) {
6679 isl_space *dim = isl_map_get_space(map);
6680 isl_map_free(map);
6681 return isl_pw_aff_empty(dim);
6684 pwaff = basic_map_dim_opt(map->p[0], max);
6685 for (i = 1; i < map->n; ++i) {
6686 isl_pw_aff *pwaff_i;
6688 pwaff_i = basic_map_dim_opt(map->p[i], max);
6689 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6692 isl_map_free(map);
6694 return pwaff;
6697 /* Compute the minimum of the given output dimension as a function of the
6698 * parameters and input dimensions, but independently of
6699 * the other output dimensions.
6701 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6703 return map_dim_opt(map, pos, 0);
6706 /* Compute the maximum of the given output dimension as a function of the
6707 * parameters and input dimensions, but independently of
6708 * the other output dimensions.
6710 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6712 return map_dim_opt(map, pos, 1);
6715 /* Compute the minimum or maximum of the given set dimension
6716 * as a function of the parameters,
6717 * but independently of the other set dimensions.
6719 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6720 int max)
6722 return map_dim_opt(set, pos, max);
6725 /* Compute the maximum of the given set dimension as a function of the
6726 * parameters, but independently of the other set dimensions.
6728 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6730 return set_dim_opt(set, pos, 1);
6733 /* Compute the minimum of the given set dimension as a function of the
6734 * parameters, but independently of the other set dimensions.
6736 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6738 return set_dim_opt(set, pos, 0);
6741 /* Apply a preimage specified by "mat" on the parameters of "bset".
6742 * bset is assumed to have only parameters and divs.
6744 static struct isl_basic_set *basic_set_parameter_preimage(
6745 struct isl_basic_set *bset, struct isl_mat *mat)
6747 unsigned nparam;
6749 if (!bset || !mat)
6750 goto error;
6752 bset->dim = isl_space_cow(bset->dim);
6753 if (!bset->dim)
6754 goto error;
6756 nparam = isl_basic_set_dim(bset, isl_dim_param);
6758 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6760 bset->dim->nparam = 0;
6761 bset->dim->n_out = nparam;
6762 bset = isl_basic_set_preimage(bset, mat);
6763 if (bset) {
6764 bset->dim->nparam = bset->dim->n_out;
6765 bset->dim->n_out = 0;
6767 return bset;
6768 error:
6769 isl_mat_free(mat);
6770 isl_basic_set_free(bset);
6771 return NULL;
6774 /* Apply a preimage specified by "mat" on the parameters of "set".
6775 * set is assumed to have only parameters and divs.
6777 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6778 __isl_take isl_mat *mat)
6780 isl_space *space;
6781 unsigned nparam;
6783 if (!set || !mat)
6784 goto error;
6786 nparam = isl_set_dim(set, isl_dim_param);
6788 if (mat->n_row != 1 + nparam)
6789 isl_die(isl_set_get_ctx(set), isl_error_internal,
6790 "unexpected number of rows", goto error);
6792 space = isl_set_get_space(set);
6793 space = isl_space_move_dims(space, isl_dim_set, 0,
6794 isl_dim_param, 0, nparam);
6795 set = isl_set_reset_space(set, space);
6796 set = isl_set_preimage(set, mat);
6797 nparam = isl_set_dim(set, isl_dim_out);
6798 space = isl_set_get_space(set);
6799 space = isl_space_move_dims(space, isl_dim_param, 0,
6800 isl_dim_out, 0, nparam);
6801 set = isl_set_reset_space(set, space);
6802 return set;
6803 error:
6804 isl_mat_free(mat);
6805 isl_set_free(set);
6806 return NULL;
6809 /* Intersect the basic set "bset" with the affine space specified by the
6810 * equalities in "eq".
6812 static struct isl_basic_set *basic_set_append_equalities(
6813 struct isl_basic_set *bset, struct isl_mat *eq)
6815 int i, k;
6816 unsigned len;
6818 if (!bset || !eq)
6819 goto error;
6821 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6822 eq->n_row, 0);
6823 if (!bset)
6824 goto error;
6826 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6827 for (i = 0; i < eq->n_row; ++i) {
6828 k = isl_basic_set_alloc_equality(bset);
6829 if (k < 0)
6830 goto error;
6831 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6832 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6834 isl_mat_free(eq);
6836 bset = isl_basic_set_gauss(bset, NULL);
6837 bset = isl_basic_set_finalize(bset);
6839 return bset;
6840 error:
6841 isl_mat_free(eq);
6842 isl_basic_set_free(bset);
6843 return NULL;
6846 /* Intersect the set "set" with the affine space specified by the
6847 * equalities in "eq".
6849 static struct isl_set *set_append_equalities(struct isl_set *set,
6850 struct isl_mat *eq)
6852 int i;
6854 if (!set || !eq)
6855 goto error;
6857 for (i = 0; i < set->n; ++i) {
6858 set->p[i] = basic_set_append_equalities(set->p[i],
6859 isl_mat_copy(eq));
6860 if (!set->p[i])
6861 goto error;
6863 isl_mat_free(eq);
6864 return set;
6865 error:
6866 isl_mat_free(eq);
6867 isl_set_free(set);
6868 return NULL;
6871 /* Given a basic set "bset" that only involves parameters and existentially
6872 * quantified variables, return the index of the first equality
6873 * that only involves parameters. If there is no such equality then
6874 * return bset->n_eq.
6876 * This function assumes that isl_basic_set_gauss has been called on "bset".
6878 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6880 int i, j;
6881 unsigned nparam, n_div;
6883 if (!bset)
6884 return -1;
6886 nparam = isl_basic_set_dim(bset, isl_dim_param);
6887 n_div = isl_basic_set_dim(bset, isl_dim_div);
6889 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6890 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6891 ++i;
6894 return i;
6897 /* Compute an explicit representation for the existentially quantified
6898 * variables in "bset" by computing the "minimal value" of the set
6899 * variables. Since there are no set variables, the computation of
6900 * the minimal value essentially computes an explicit representation
6901 * of the non-empty part(s) of "bset".
6903 * The input only involves parameters and existentially quantified variables.
6904 * All equalities among parameters have been removed.
6906 * Since the existentially quantified variables in the result are in general
6907 * going to be different from those in the input, we first replace
6908 * them by the minimal number of variables based on their equalities.
6909 * This should simplify the parametric integer programming.
6911 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6913 isl_morph *morph1, *morph2;
6914 isl_set *set;
6915 unsigned n;
6917 if (!bset)
6918 return NULL;
6919 if (bset->n_eq == 0)
6920 return isl_basic_set_lexmin_compute_divs(bset);
6922 morph1 = isl_basic_set_parameter_compression(bset);
6923 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6924 bset = isl_basic_set_lift(bset);
6925 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6926 bset = isl_morph_basic_set(morph2, bset);
6927 n = isl_basic_set_dim(bset, isl_dim_set);
6928 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6930 set = isl_basic_set_lexmin_compute_divs(bset);
6932 set = isl_morph_set(isl_morph_inverse(morph1), set);
6934 return set;
6937 /* Project the given basic set onto its parameter domain, possibly introducing
6938 * new, explicit, existential variables in the constraints.
6939 * The input has parameters and (possibly implicit) existential variables.
6940 * The output has the same parameters, but only
6941 * explicit existentially quantified variables.
6943 * The actual projection is performed by pip, but pip doesn't seem
6944 * to like equalities very much, so we first remove the equalities
6945 * among the parameters by performing a variable compression on
6946 * the parameters. Afterward, an inverse transformation is performed
6947 * and the equalities among the parameters are inserted back in.
6949 * The variable compression on the parameters may uncover additional
6950 * equalities that were only implicit before. We therefore check
6951 * if there are any new parameter equalities in the result and
6952 * if so recurse. The removal of parameter equalities is required
6953 * for the parameter compression performed by base_compute_divs.
6955 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6957 int i;
6958 struct isl_mat *eq;
6959 struct isl_mat *T, *T2;
6960 struct isl_set *set;
6961 unsigned nparam;
6963 bset = isl_basic_set_cow(bset);
6964 if (!bset)
6965 return NULL;
6967 if (bset->n_eq == 0)
6968 return base_compute_divs(bset);
6970 bset = isl_basic_set_gauss(bset, NULL);
6971 if (!bset)
6972 return NULL;
6973 if (isl_basic_set_plain_is_empty(bset))
6974 return isl_set_from_basic_set(bset);
6976 i = first_parameter_equality(bset);
6977 if (i == bset->n_eq)
6978 return base_compute_divs(bset);
6980 nparam = isl_basic_set_dim(bset, isl_dim_param);
6981 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6982 0, 1 + nparam);
6983 eq = isl_mat_cow(eq);
6984 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6985 if (T && T->n_col == 0) {
6986 isl_mat_free(T);
6987 isl_mat_free(T2);
6988 isl_mat_free(eq);
6989 bset = isl_basic_set_set_to_empty(bset);
6990 return isl_set_from_basic_set(bset);
6992 bset = basic_set_parameter_preimage(bset, T);
6994 i = first_parameter_equality(bset);
6995 if (!bset)
6996 set = NULL;
6997 else if (i == bset->n_eq)
6998 set = base_compute_divs(bset);
6999 else
7000 set = parameter_compute_divs(bset);
7001 set = set_parameter_preimage(set, T2);
7002 set = set_append_equalities(set, eq);
7003 return set;
7006 /* Insert the divs from "ls" before those of "bmap".
7008 * The number of columns is not changed, which means that the last
7009 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7010 * The caller is responsible for removing the same number of dimensions
7011 * from the space of "bmap".
7013 static __isl_give isl_basic_map *insert_divs_from_local_space(
7014 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7016 int i;
7017 int n_div;
7018 int old_n_div;
7020 n_div = isl_local_space_dim(ls, isl_dim_div);
7021 if (n_div == 0)
7022 return bmap;
7024 old_n_div = bmap->n_div;
7025 bmap = insert_div_rows(bmap, n_div);
7026 if (!bmap)
7027 return NULL;
7029 for (i = 0; i < n_div; ++i) {
7030 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7031 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7034 return bmap;
7037 /* Replace the space of "bmap" by the space and divs of "ls".
7039 * If "ls" has any divs, then we simplify the result since we may
7040 * have discovered some additional equalities that could simplify
7041 * the div expressions.
7043 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7044 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7046 int n_div;
7048 bmap = isl_basic_map_cow(bmap);
7049 if (!bmap || !ls)
7050 goto error;
7052 n_div = isl_local_space_dim(ls, isl_dim_div);
7053 bmap = insert_divs_from_local_space(bmap, ls);
7054 if (!bmap)
7055 goto error;
7057 isl_space_free(bmap->dim);
7058 bmap->dim = isl_local_space_get_space(ls);
7059 if (!bmap->dim)
7060 goto error;
7062 isl_local_space_free(ls);
7063 if (n_div > 0)
7064 bmap = isl_basic_map_simplify(bmap);
7065 bmap = isl_basic_map_finalize(bmap);
7066 return bmap;
7067 error:
7068 isl_basic_map_free(bmap);
7069 isl_local_space_free(ls);
7070 return NULL;
7073 /* Replace the space of "map" by the space and divs of "ls".
7075 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7076 __isl_take isl_local_space *ls)
7078 int i;
7080 map = isl_map_cow(map);
7081 if (!map || !ls)
7082 goto error;
7084 for (i = 0; i < map->n; ++i) {
7085 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7086 isl_local_space_copy(ls));
7087 if (!map->p[i])
7088 goto error;
7090 isl_space_free(map->dim);
7091 map->dim = isl_local_space_get_space(ls);
7092 if (!map->dim)
7093 goto error;
7095 isl_local_space_free(ls);
7096 return map;
7097 error:
7098 isl_local_space_free(ls);
7099 isl_map_free(map);
7100 return NULL;
7103 /* Compute an explicit representation for the existentially
7104 * quantified variables for which do not know any explicit representation yet.
7106 * We first sort the existentially quantified variables so that the
7107 * existentially quantified variables for which we already have an explicit
7108 * representation are placed before those for which we do not.
7109 * The input dimensions, the output dimensions and the existentially
7110 * quantified variables for which we already have an explicit
7111 * representation are then turned into parameters.
7112 * compute_divs returns a map with the same parameters and
7113 * no input or output dimensions and the dimension specification
7114 * is reset to that of the input, including the existentially quantified
7115 * variables for which we already had an explicit representation.
7117 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7119 struct isl_basic_set *bset;
7120 struct isl_set *set;
7121 struct isl_map *map;
7122 isl_space *dim;
7123 isl_local_space *ls;
7124 unsigned nparam;
7125 unsigned n_in;
7126 unsigned n_out;
7127 int n_known;
7128 int i;
7130 bmap = isl_basic_map_sort_divs(bmap);
7131 bmap = isl_basic_map_cow(bmap);
7132 if (!bmap)
7133 return NULL;
7135 n_known = isl_basic_map_first_unknown_div(bmap);
7136 if (n_known < 0)
7137 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7139 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7140 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7141 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7142 dim = isl_space_set_alloc(bmap->ctx,
7143 nparam + n_in + n_out + n_known, 0);
7144 if (!dim)
7145 goto error;
7147 ls = isl_basic_map_get_local_space(bmap);
7148 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7149 n_known, bmap->n_div - n_known);
7150 if (n_known > 0) {
7151 for (i = n_known; i < bmap->n_div; ++i)
7152 swap_div(bmap, i - n_known, i);
7153 bmap->n_div -= n_known;
7154 bmap->extra -= n_known;
7156 bmap = isl_basic_map_reset_space(bmap, dim);
7157 bset = bset_from_bmap(bmap);
7159 set = parameter_compute_divs(bset);
7160 map = set_to_map(set);
7161 map = replace_space_by_local_space(map, ls);
7163 return map;
7164 error:
7165 isl_basic_map_free(bmap);
7166 return NULL;
7169 /* Remove the explicit representation of local variable "div",
7170 * if there is any.
7172 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7173 __isl_take isl_basic_map *bmap, int div)
7175 isl_bool unknown;
7177 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7178 if (unknown < 0)
7179 return isl_basic_map_free(bmap);
7180 if (unknown)
7181 return bmap;
7183 bmap = isl_basic_map_cow(bmap);
7184 if (!bmap)
7185 return NULL;
7186 isl_int_set_si(bmap->div[div][0], 0);
7187 return bmap;
7190 /* Is local variable "div" of "bmap" marked as not having an explicit
7191 * representation?
7192 * Note that even if "div" is not marked in this way and therefore
7193 * has an explicit representation, this representation may still
7194 * depend (indirectly) on other local variables that do not
7195 * have an explicit representation.
7197 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7198 int div)
7200 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7201 return isl_bool_error;
7202 return isl_int_is_zero(bmap->div[div][0]);
7205 /* Return the position of the first local variable that does not
7206 * have an explicit representation.
7207 * Return the total number of local variables if they all have
7208 * an explicit representation.
7209 * Return -1 on error.
7211 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7213 int i;
7215 if (!bmap)
7216 return -1;
7218 for (i = 0; i < bmap->n_div; ++i) {
7219 if (!isl_basic_map_div_is_known(bmap, i))
7220 return i;
7222 return bmap->n_div;
7225 /* Return the position of the first local variable that does not
7226 * have an explicit representation.
7227 * Return the total number of local variables if they all have
7228 * an explicit representation.
7229 * Return -1 on error.
7231 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7233 return isl_basic_map_first_unknown_div(bset);
7236 /* Does "bmap" have an explicit representation for all local variables?
7238 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7240 int first, n;
7242 n = isl_basic_map_dim(bmap, isl_dim_div);
7243 first = isl_basic_map_first_unknown_div(bmap);
7244 if (first < 0)
7245 return isl_bool_error;
7246 return first == n;
7249 /* Do all basic maps in "map" have an explicit representation
7250 * for all local variables?
7252 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7254 int i;
7256 if (!map)
7257 return isl_bool_error;
7259 for (i = 0; i < map->n; ++i) {
7260 int known = isl_basic_map_divs_known(map->p[i]);
7261 if (known <= 0)
7262 return known;
7265 return isl_bool_true;
7268 /* If bmap contains any unknown divs, then compute explicit
7269 * expressions for them. However, this computation may be
7270 * quite expensive, so first try to remove divs that aren't
7271 * strictly needed.
7273 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7275 int known;
7276 struct isl_map *map;
7278 known = isl_basic_map_divs_known(bmap);
7279 if (known < 0)
7280 goto error;
7281 if (known)
7282 return isl_map_from_basic_map(bmap);
7284 bmap = isl_basic_map_drop_redundant_divs(bmap);
7286 known = isl_basic_map_divs_known(bmap);
7287 if (known < 0)
7288 goto error;
7289 if (known)
7290 return isl_map_from_basic_map(bmap);
7292 map = compute_divs(bmap);
7293 return map;
7294 error:
7295 isl_basic_map_free(bmap);
7296 return NULL;
7299 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7301 int i;
7302 int known;
7303 struct isl_map *res;
7305 if (!map)
7306 return NULL;
7307 if (map->n == 0)
7308 return map;
7310 known = isl_map_divs_known(map);
7311 if (known < 0) {
7312 isl_map_free(map);
7313 return NULL;
7315 if (known)
7316 return map;
7318 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7319 for (i = 1 ; i < map->n; ++i) {
7320 struct isl_map *r2;
7321 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7322 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7323 res = isl_map_union_disjoint(res, r2);
7324 else
7325 res = isl_map_union(res, r2);
7327 isl_map_free(map);
7329 return res;
7332 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7334 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7337 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7339 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7342 struct isl_set *isl_map_domain(struct isl_map *map)
7344 int i;
7345 struct isl_set *set;
7347 if (!map)
7348 goto error;
7350 map = isl_map_cow(map);
7351 if (!map)
7352 return NULL;
7354 set = set_from_map(map);
7355 set->dim = isl_space_domain(set->dim);
7356 if (!set->dim)
7357 goto error;
7358 for (i = 0; i < map->n; ++i) {
7359 set->p[i] = isl_basic_map_domain(map->p[i]);
7360 if (!set->p[i])
7361 goto error;
7363 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7364 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7365 return set;
7366 error:
7367 isl_map_free(map);
7368 return NULL;
7371 /* Return the union of "map1" and "map2", where we assume for now that
7372 * "map1" and "map2" are disjoint. Note that the basic maps inside
7373 * "map1" or "map2" may not be disjoint from each other.
7374 * Also note that this function is also called from isl_map_union,
7375 * which takes care of handling the situation where "map1" and "map2"
7376 * may not be disjoint.
7378 * If one of the inputs is empty, we can simply return the other input.
7379 * Similarly, if one of the inputs is universal, then it is equal to the union.
7381 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7382 __isl_take isl_map *map2)
7384 int i;
7385 unsigned flags = 0;
7386 struct isl_map *map = NULL;
7387 int is_universe;
7389 if (!map1 || !map2)
7390 goto error;
7392 if (!isl_space_is_equal(map1->dim, map2->dim))
7393 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7394 "spaces don't match", goto error);
7396 if (map1->n == 0) {
7397 isl_map_free(map1);
7398 return map2;
7400 if (map2->n == 0) {
7401 isl_map_free(map2);
7402 return map1;
7405 is_universe = isl_map_plain_is_universe(map1);
7406 if (is_universe < 0)
7407 goto error;
7408 if (is_universe) {
7409 isl_map_free(map2);
7410 return map1;
7413 is_universe = isl_map_plain_is_universe(map2);
7414 if (is_universe < 0)
7415 goto error;
7416 if (is_universe) {
7417 isl_map_free(map1);
7418 return map2;
7421 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7422 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7423 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7425 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7426 map1->n + map2->n, flags);
7427 if (!map)
7428 goto error;
7429 for (i = 0; i < map1->n; ++i) {
7430 map = isl_map_add_basic_map(map,
7431 isl_basic_map_copy(map1->p[i]));
7432 if (!map)
7433 goto error;
7435 for (i = 0; i < map2->n; ++i) {
7436 map = isl_map_add_basic_map(map,
7437 isl_basic_map_copy(map2->p[i]));
7438 if (!map)
7439 goto error;
7441 isl_map_free(map1);
7442 isl_map_free(map2);
7443 return map;
7444 error:
7445 isl_map_free(map);
7446 isl_map_free(map1);
7447 isl_map_free(map2);
7448 return NULL;
7451 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7452 * guaranteed to be disjoint by the caller.
7454 * Note that this functions is called from within isl_map_make_disjoint,
7455 * so we have to be careful not to touch the constraints of the inputs
7456 * in any way.
7458 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7459 __isl_take isl_map *map2)
7461 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7464 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7465 * not be disjoint. The parameters are assumed to have been aligned.
7467 * We currently simply call map_union_disjoint, the internal operation
7468 * of which does not really depend on the inputs being disjoint.
7469 * If the result contains more than one basic map, then we clear
7470 * the disjoint flag since the result may contain basic maps from
7471 * both inputs and these are not guaranteed to be disjoint.
7473 * As a special case, if "map1" and "map2" are obviously equal,
7474 * then we simply return "map1".
7476 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7477 __isl_take isl_map *map2)
7479 int equal;
7481 if (!map1 || !map2)
7482 goto error;
7484 equal = isl_map_plain_is_equal(map1, map2);
7485 if (equal < 0)
7486 goto error;
7487 if (equal) {
7488 isl_map_free(map2);
7489 return map1;
7492 map1 = map_union_disjoint(map1, map2);
7493 if (!map1)
7494 return NULL;
7495 if (map1->n > 1)
7496 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7497 return map1;
7498 error:
7499 isl_map_free(map1);
7500 isl_map_free(map2);
7501 return NULL;
7504 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7505 * not be disjoint.
7507 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7508 __isl_take isl_map *map2)
7510 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7513 struct isl_set *isl_set_union_disjoint(
7514 struct isl_set *set1, struct isl_set *set2)
7516 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7517 set_to_map(set2)));
7520 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7522 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7525 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7526 * the results.
7528 * "map" and "set" are assumed to be compatible and non-NULL.
7530 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7531 __isl_take isl_set *set,
7532 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7533 __isl_take isl_basic_set *bset))
7535 unsigned flags = 0;
7536 struct isl_map *result;
7537 int i, j;
7539 if (isl_set_plain_is_universe(set)) {
7540 isl_set_free(set);
7541 return map;
7544 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7545 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7546 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7548 result = isl_map_alloc_space(isl_space_copy(map->dim),
7549 map->n * set->n, flags);
7550 for (i = 0; result && i < map->n; ++i)
7551 for (j = 0; j < set->n; ++j) {
7552 result = isl_map_add_basic_map(result,
7553 fn(isl_basic_map_copy(map->p[i]),
7554 isl_basic_set_copy(set->p[j])));
7555 if (!result)
7556 break;
7559 isl_map_free(map);
7560 isl_set_free(set);
7561 return result;
7564 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7565 __isl_take isl_set *set)
7567 isl_bool ok;
7569 ok = isl_map_compatible_range(map, set);
7570 if (ok < 0)
7571 goto error;
7572 if (!ok)
7573 isl_die(set->ctx, isl_error_invalid,
7574 "incompatible spaces", goto error);
7576 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7577 error:
7578 isl_map_free(map);
7579 isl_set_free(set);
7580 return NULL;
7583 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7584 __isl_take isl_set *set)
7586 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7589 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7590 __isl_take isl_set *set)
7592 isl_bool ok;
7594 ok = isl_map_compatible_domain(map, set);
7595 if (ok < 0)
7596 goto error;
7597 if (!ok)
7598 isl_die(set->ctx, isl_error_invalid,
7599 "incompatible spaces", goto error);
7601 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7602 error:
7603 isl_map_free(map);
7604 isl_set_free(set);
7605 return NULL;
7608 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7609 __isl_take isl_set *set)
7611 return isl_map_align_params_map_map_and(map, set,
7612 &map_intersect_domain);
7615 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7616 __isl_take isl_map *map2)
7618 if (!map1 || !map2)
7619 goto error;
7620 map1 = isl_map_reverse(map1);
7621 map1 = isl_map_apply_range(map1, map2);
7622 return isl_map_reverse(map1);
7623 error:
7624 isl_map_free(map1);
7625 isl_map_free(map2);
7626 return NULL;
7629 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7630 __isl_take isl_map *map2)
7632 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7635 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7636 __isl_take isl_map *map2)
7638 isl_space *dim_result;
7639 struct isl_map *result;
7640 int i, j;
7642 if (!map1 || !map2)
7643 goto error;
7645 dim_result = isl_space_join(isl_space_copy(map1->dim),
7646 isl_space_copy(map2->dim));
7648 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7649 if (!result)
7650 goto error;
7651 for (i = 0; i < map1->n; ++i)
7652 for (j = 0; j < map2->n; ++j) {
7653 result = isl_map_add_basic_map(result,
7654 isl_basic_map_apply_range(
7655 isl_basic_map_copy(map1->p[i]),
7656 isl_basic_map_copy(map2->p[j])));
7657 if (!result)
7658 goto error;
7660 isl_map_free(map1);
7661 isl_map_free(map2);
7662 if (result && result->n <= 1)
7663 ISL_F_SET(result, ISL_MAP_DISJOINT);
7664 return result;
7665 error:
7666 isl_map_free(map1);
7667 isl_map_free(map2);
7668 return NULL;
7671 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7672 __isl_take isl_map *map2)
7674 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7678 * returns range - domain
7680 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7682 isl_space *target_space;
7683 struct isl_basic_set *bset;
7684 unsigned dim;
7685 unsigned nparam;
7686 int i;
7688 if (!bmap)
7689 goto error;
7690 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7691 bmap->dim, isl_dim_out),
7692 goto error);
7693 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7694 dim = isl_basic_map_n_in(bmap);
7695 nparam = isl_basic_map_n_param(bmap);
7696 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7697 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7698 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7699 for (i = 0; i < dim; ++i) {
7700 int j = isl_basic_map_alloc_equality(bmap);
7701 if (j < 0) {
7702 bmap = isl_basic_map_free(bmap);
7703 break;
7705 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7706 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7707 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7708 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7710 bset = isl_basic_map_domain(bmap);
7711 bset = isl_basic_set_reset_space(bset, target_space);
7712 return bset;
7713 error:
7714 isl_basic_map_free(bmap);
7715 return NULL;
7719 * returns range - domain
7721 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7723 int i;
7724 isl_space *dim;
7725 struct isl_set *result;
7727 if (!map)
7728 return NULL;
7730 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7731 map->dim, isl_dim_out),
7732 goto error);
7733 dim = isl_map_get_space(map);
7734 dim = isl_space_domain(dim);
7735 result = isl_set_alloc_space(dim, map->n, 0);
7736 if (!result)
7737 goto error;
7738 for (i = 0; i < map->n; ++i)
7739 result = isl_set_add_basic_set(result,
7740 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7741 isl_map_free(map);
7742 return result;
7743 error:
7744 isl_map_free(map);
7745 return NULL;
7749 * returns [domain -> range] -> range - domain
7751 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7752 __isl_take isl_basic_map *bmap)
7754 int i, k;
7755 isl_space *dim;
7756 isl_basic_map *domain;
7757 int nparam, n;
7758 unsigned total;
7760 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7761 bmap->dim, isl_dim_out))
7762 isl_die(bmap->ctx, isl_error_invalid,
7763 "domain and range don't match", goto error);
7765 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7766 n = isl_basic_map_dim(bmap, isl_dim_in);
7768 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7769 domain = isl_basic_map_universe(dim);
7771 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7772 bmap = isl_basic_map_apply_range(bmap, domain);
7773 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7775 total = isl_basic_map_total_dim(bmap);
7777 for (i = 0; i < n; ++i) {
7778 k = isl_basic_map_alloc_equality(bmap);
7779 if (k < 0)
7780 goto error;
7781 isl_seq_clr(bmap->eq[k], 1 + total);
7782 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7783 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7784 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7787 bmap = isl_basic_map_gauss(bmap, NULL);
7788 return isl_basic_map_finalize(bmap);
7789 error:
7790 isl_basic_map_free(bmap);
7791 return NULL;
7795 * returns [domain -> range] -> range - domain
7797 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7799 int i;
7800 isl_space *domain_dim;
7802 if (!map)
7803 return NULL;
7805 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7806 map->dim, isl_dim_out))
7807 isl_die(map->ctx, isl_error_invalid,
7808 "domain and range don't match", goto error);
7810 map = isl_map_cow(map);
7811 if (!map)
7812 return NULL;
7814 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7815 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7816 map->dim = isl_space_join(map->dim, domain_dim);
7817 if (!map->dim)
7818 goto error;
7819 for (i = 0; i < map->n; ++i) {
7820 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7821 if (!map->p[i])
7822 goto error;
7824 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7825 return map;
7826 error:
7827 isl_map_free(map);
7828 return NULL;
7831 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7833 struct isl_basic_map *bmap;
7834 unsigned nparam;
7835 unsigned dim;
7836 int i;
7838 if (!dims)
7839 return NULL;
7841 nparam = dims->nparam;
7842 dim = dims->n_out;
7843 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7844 if (!bmap)
7845 goto error;
7847 for (i = 0; i < dim; ++i) {
7848 int j = isl_basic_map_alloc_equality(bmap);
7849 if (j < 0)
7850 goto error;
7851 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7852 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7853 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7855 return isl_basic_map_finalize(bmap);
7856 error:
7857 isl_basic_map_free(bmap);
7858 return NULL;
7861 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7863 if (!dim)
7864 return NULL;
7865 if (dim->n_in != dim->n_out)
7866 isl_die(dim->ctx, isl_error_invalid,
7867 "number of input and output dimensions needs to be "
7868 "the same", goto error);
7869 return basic_map_identity(dim);
7870 error:
7871 isl_space_free(dim);
7872 return NULL;
7875 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7877 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7880 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7882 isl_space *dim = isl_set_get_space(set);
7883 isl_map *id;
7884 id = isl_map_identity(isl_space_map_from_set(dim));
7885 return isl_map_intersect_range(id, set);
7888 /* Construct a basic set with all set dimensions having only non-negative
7889 * values.
7891 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7892 __isl_take isl_space *space)
7894 int i;
7895 unsigned nparam;
7896 unsigned dim;
7897 struct isl_basic_set *bset;
7899 if (!space)
7900 return NULL;
7901 nparam = space->nparam;
7902 dim = space->n_out;
7903 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7904 if (!bset)
7905 return NULL;
7906 for (i = 0; i < dim; ++i) {
7907 int k = isl_basic_set_alloc_inequality(bset);
7908 if (k < 0)
7909 goto error;
7910 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7911 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7913 return bset;
7914 error:
7915 isl_basic_set_free(bset);
7916 return NULL;
7919 /* Construct the half-space x_pos >= 0.
7921 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7922 int pos)
7924 int k;
7925 isl_basic_set *nonneg;
7927 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7928 k = isl_basic_set_alloc_inequality(nonneg);
7929 if (k < 0)
7930 goto error;
7931 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7932 isl_int_set_si(nonneg->ineq[k][pos], 1);
7934 return isl_basic_set_finalize(nonneg);
7935 error:
7936 isl_basic_set_free(nonneg);
7937 return NULL;
7940 /* Construct the half-space x_pos <= -1.
7942 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7944 int k;
7945 isl_basic_set *neg;
7947 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7948 k = isl_basic_set_alloc_inequality(neg);
7949 if (k < 0)
7950 goto error;
7951 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7952 isl_int_set_si(neg->ineq[k][0], -1);
7953 isl_int_set_si(neg->ineq[k][pos], -1);
7955 return isl_basic_set_finalize(neg);
7956 error:
7957 isl_basic_set_free(neg);
7958 return NULL;
7961 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7962 enum isl_dim_type type, unsigned first, unsigned n)
7964 int i;
7965 unsigned offset;
7966 isl_basic_set *nonneg;
7967 isl_basic_set *neg;
7969 if (!set)
7970 return NULL;
7971 if (n == 0)
7972 return set;
7974 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7976 offset = pos(set->dim, type);
7977 for (i = 0; i < n; ++i) {
7978 nonneg = nonneg_halfspace(isl_set_get_space(set),
7979 offset + first + i);
7980 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7982 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7985 return set;
7986 error:
7987 isl_set_free(set);
7988 return NULL;
7991 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7992 int len,
7993 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7994 void *user)
7996 isl_set *half;
7998 if (!set)
7999 return isl_stat_error;
8000 if (isl_set_plain_is_empty(set)) {
8001 isl_set_free(set);
8002 return isl_stat_ok;
8004 if (first == len)
8005 return fn(set, signs, user);
8007 signs[first] = 1;
8008 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8009 1 + first));
8010 half = isl_set_intersect(half, isl_set_copy(set));
8011 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8012 goto error;
8014 signs[first] = -1;
8015 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8016 1 + first));
8017 half = isl_set_intersect(half, set);
8018 return foreach_orthant(half, signs, first + 1, len, fn, user);
8019 error:
8020 isl_set_free(set);
8021 return isl_stat_error;
8024 /* Call "fn" on the intersections of "set" with each of the orthants
8025 * (except for obviously empty intersections). The orthant is identified
8026 * by the signs array, with each entry having value 1 or -1 according
8027 * to the sign of the corresponding variable.
8029 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8030 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8031 void *user)
8033 unsigned nparam;
8034 unsigned nvar;
8035 int *signs;
8036 isl_stat r;
8038 if (!set)
8039 return isl_stat_error;
8040 if (isl_set_plain_is_empty(set))
8041 return isl_stat_ok;
8043 nparam = isl_set_dim(set, isl_dim_param);
8044 nvar = isl_set_dim(set, isl_dim_set);
8046 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8048 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8049 fn, user);
8051 free(signs);
8053 return r;
8056 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8058 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8061 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8062 __isl_keep isl_basic_map *bmap2)
8064 int is_subset;
8065 struct isl_map *map1;
8066 struct isl_map *map2;
8068 if (!bmap1 || !bmap2)
8069 return isl_bool_error;
8071 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8072 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8074 is_subset = isl_map_is_subset(map1, map2);
8076 isl_map_free(map1);
8077 isl_map_free(map2);
8079 return is_subset;
8082 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8083 __isl_keep isl_basic_set *bset2)
8085 return isl_basic_map_is_subset(bset1, bset2);
8088 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8089 __isl_keep isl_basic_map *bmap2)
8091 isl_bool is_subset;
8093 if (!bmap1 || !bmap2)
8094 return isl_bool_error;
8095 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8096 if (is_subset != isl_bool_true)
8097 return is_subset;
8098 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8099 return is_subset;
8102 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8103 __isl_keep isl_basic_set *bset2)
8105 return isl_basic_map_is_equal(
8106 bset_to_bmap(bset1), bset_to_bmap(bset2));
8109 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8111 int i;
8112 int is_empty;
8114 if (!map)
8115 return isl_bool_error;
8116 for (i = 0; i < map->n; ++i) {
8117 is_empty = isl_basic_map_is_empty(map->p[i]);
8118 if (is_empty < 0)
8119 return isl_bool_error;
8120 if (!is_empty)
8121 return isl_bool_false;
8123 return isl_bool_true;
8126 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8128 return map ? map->n == 0 : isl_bool_error;
8131 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8133 return set ? set->n == 0 : isl_bool_error;
8136 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8138 return isl_map_is_empty(set_to_map(set));
8141 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8142 __isl_keep isl_map *map2)
8144 if (!map1 || !map2)
8145 return isl_bool_error;
8147 return isl_space_is_equal(map1->dim, map2->dim);
8150 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8151 __isl_keep isl_set *set2)
8153 if (!set1 || !set2)
8154 return isl_bool_error;
8156 return isl_space_is_equal(set1->dim, set2->dim);
8159 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8161 isl_bool is_subset;
8163 if (!map1 || !map2)
8164 return isl_bool_error;
8165 is_subset = isl_map_is_subset(map1, map2);
8166 if (is_subset != isl_bool_true)
8167 return is_subset;
8168 is_subset = isl_map_is_subset(map2, map1);
8169 return is_subset;
8172 /* Is "map1" equal to "map2"?
8174 * First check if they are obviously equal.
8175 * If not, then perform a more detailed analysis.
8177 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8179 isl_bool equal;
8181 equal = isl_map_plain_is_equal(map1, map2);
8182 if (equal < 0 || equal)
8183 return equal;
8184 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8187 isl_bool isl_basic_map_is_strict_subset(
8188 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8190 isl_bool is_subset;
8192 if (!bmap1 || !bmap2)
8193 return isl_bool_error;
8194 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8195 if (is_subset != isl_bool_true)
8196 return is_subset;
8197 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8198 if (is_subset == isl_bool_error)
8199 return is_subset;
8200 return !is_subset;
8203 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8204 __isl_keep isl_map *map2)
8206 isl_bool is_subset;
8208 if (!map1 || !map2)
8209 return isl_bool_error;
8210 is_subset = isl_map_is_subset(map1, map2);
8211 if (is_subset != isl_bool_true)
8212 return is_subset;
8213 is_subset = isl_map_is_subset(map2, map1);
8214 if (is_subset == isl_bool_error)
8215 return is_subset;
8216 return !is_subset;
8219 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8220 __isl_keep isl_set *set2)
8222 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8225 /* Is "bmap" obviously equal to the universe with the same space?
8227 * That is, does it not have any constraints?
8229 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8231 if (!bmap)
8232 return isl_bool_error;
8233 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8236 /* Is "bset" obviously equal to the universe with the same space?
8238 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8240 return isl_basic_map_plain_is_universe(bset);
8243 /* If "c" does not involve any existentially quantified variables,
8244 * then set *univ to false and abort
8246 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8248 isl_bool *univ = user;
8249 unsigned n;
8251 n = isl_constraint_dim(c, isl_dim_div);
8252 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8253 isl_constraint_free(c);
8254 if (*univ < 0 || !*univ)
8255 return isl_stat_error;
8256 return isl_stat_ok;
8259 /* Is "bmap" equal to the universe with the same space?
8261 * First check if it is obviously equal to the universe.
8262 * If not and if there are any constraints not involving
8263 * existentially quantified variables, then it is certainly
8264 * not equal to the universe.
8265 * Otherwise, check if the universe is a subset of "bmap".
8267 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8269 isl_bool univ;
8270 isl_basic_map *test;
8272 univ = isl_basic_map_plain_is_universe(bmap);
8273 if (univ < 0 || univ)
8274 return univ;
8275 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8276 return isl_bool_false;
8277 univ = isl_bool_true;
8278 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8279 univ)
8280 return isl_bool_error;
8281 if (univ < 0 || !univ)
8282 return univ;
8283 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8284 univ = isl_basic_map_is_subset(test, bmap);
8285 isl_basic_map_free(test);
8286 return univ;
8289 /* Is "bset" equal to the universe with the same space?
8291 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8293 return isl_basic_map_is_universe(bset);
8296 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8298 int i;
8300 if (!map)
8301 return isl_bool_error;
8303 for (i = 0; i < map->n; ++i) {
8304 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8305 if (r < 0 || r)
8306 return r;
8309 return isl_bool_false;
8312 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8314 return isl_map_plain_is_universe(set_to_map(set));
8317 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8319 struct isl_basic_set *bset = NULL;
8320 struct isl_vec *sample = NULL;
8321 isl_bool empty, non_empty;
8323 if (!bmap)
8324 return isl_bool_error;
8326 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8327 return isl_bool_true;
8329 if (isl_basic_map_plain_is_universe(bmap))
8330 return isl_bool_false;
8332 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8333 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8334 copy = isl_basic_map_remove_redundancies(copy);
8335 empty = isl_basic_map_plain_is_empty(copy);
8336 isl_basic_map_free(copy);
8337 return empty;
8340 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8341 if (non_empty < 0)
8342 return isl_bool_error;
8343 if (non_empty)
8344 return isl_bool_false;
8345 isl_vec_free(bmap->sample);
8346 bmap->sample = NULL;
8347 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8348 if (!bset)
8349 return isl_bool_error;
8350 sample = isl_basic_set_sample_vec(bset);
8351 if (!sample)
8352 return isl_bool_error;
8353 empty = sample->size == 0;
8354 isl_vec_free(bmap->sample);
8355 bmap->sample = sample;
8356 if (empty)
8357 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8359 return empty;
8362 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8364 if (!bmap)
8365 return isl_bool_error;
8366 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8369 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8371 if (!bset)
8372 return isl_bool_error;
8373 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8376 /* Is "bmap" known to be non-empty?
8378 * That is, is the cached sample still valid?
8380 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8382 unsigned total;
8384 if (!bmap)
8385 return isl_bool_error;
8386 if (!bmap->sample)
8387 return isl_bool_false;
8388 total = 1 + isl_basic_map_total_dim(bmap);
8389 if (bmap->sample->size != total)
8390 return isl_bool_false;
8391 return isl_basic_map_contains(bmap, bmap->sample);
8394 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8396 return isl_basic_map_is_empty(bset_to_bmap(bset));
8399 struct isl_map *isl_basic_map_union(
8400 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8402 struct isl_map *map;
8403 if (!bmap1 || !bmap2)
8404 goto error;
8406 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8408 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8409 if (!map)
8410 goto error;
8411 map = isl_map_add_basic_map(map, bmap1);
8412 map = isl_map_add_basic_map(map, bmap2);
8413 return map;
8414 error:
8415 isl_basic_map_free(bmap1);
8416 isl_basic_map_free(bmap2);
8417 return NULL;
8420 struct isl_set *isl_basic_set_union(
8421 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8423 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8424 bset_to_bmap(bset2)));
8427 /* Order divs such that any div only depends on previous divs */
8428 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8430 int i;
8431 unsigned off;
8433 if (!bmap)
8434 return NULL;
8436 off = isl_space_dim(bmap->dim, isl_dim_all);
8438 for (i = 0; i < bmap->n_div; ++i) {
8439 int pos;
8440 if (isl_int_is_zero(bmap->div[i][0]))
8441 continue;
8442 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8443 bmap->n_div-i);
8444 if (pos == -1)
8445 continue;
8446 if (pos == 0)
8447 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8448 "integer division depends on itself",
8449 return isl_basic_map_free(bmap));
8450 isl_basic_map_swap_div(bmap, i, i + pos);
8451 --i;
8453 return bmap;
8456 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8458 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8461 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8463 int i;
8465 if (!map)
8466 return 0;
8468 for (i = 0; i < map->n; ++i) {
8469 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8470 if (!map->p[i])
8471 goto error;
8474 return map;
8475 error:
8476 isl_map_free(map);
8477 return NULL;
8480 /* Sort the local variables of "bset".
8482 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8483 __isl_take isl_basic_set *bset)
8485 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8488 /* Apply the expansion computed by isl_merge_divs.
8489 * The expansion itself is given by "exp" while the resulting
8490 * list of divs is given by "div".
8492 * Move the integer divisions of "bmap" into the right position
8493 * according to "exp" and then introduce the additional integer
8494 * divisions, adding div constraints.
8495 * The moving should be done first to avoid moving coefficients
8496 * in the definitions of the extra integer divisions.
8498 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8499 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8501 int i, j;
8502 int n_div;
8504 bmap = isl_basic_map_cow(bmap);
8505 if (!bmap || !div)
8506 goto error;
8508 if (div->n_row < bmap->n_div)
8509 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8510 "not an expansion", goto error);
8512 n_div = bmap->n_div;
8513 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8514 div->n_row - n_div, 0,
8515 2 * (div->n_row - n_div));
8517 for (i = n_div; i < div->n_row; ++i)
8518 if (isl_basic_map_alloc_div(bmap) < 0)
8519 goto error;
8521 for (j = n_div - 1; j >= 0; --j) {
8522 if (exp[j] == j)
8523 break;
8524 isl_basic_map_swap_div(bmap, j, exp[j]);
8526 j = 0;
8527 for (i = 0; i < div->n_row; ++i) {
8528 if (j < n_div && exp[j] == i) {
8529 j++;
8530 } else {
8531 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8532 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8533 continue;
8534 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8535 goto error;
8539 isl_mat_free(div);
8540 return bmap;
8541 error:
8542 isl_basic_map_free(bmap);
8543 isl_mat_free(div);
8544 return NULL;
8547 /* Apply the expansion computed by isl_merge_divs.
8548 * The expansion itself is given by "exp" while the resulting
8549 * list of divs is given by "div".
8551 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8552 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8554 return isl_basic_map_expand_divs(bset, div, exp);
8557 /* Look for a div in dst that corresponds to the div "div" in src.
8558 * The divs before "div" in src and dst are assumed to be the same.
8560 * Returns -1 if no corresponding div was found and the position
8561 * of the corresponding div in dst otherwise.
8563 static int find_div(struct isl_basic_map *dst,
8564 struct isl_basic_map *src, unsigned div)
8566 int i;
8568 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8570 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8571 for (i = div; i < dst->n_div; ++i)
8572 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8573 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8574 dst->n_div - div) == -1)
8575 return i;
8576 return -1;
8579 /* Align the divs of "dst" to those of "src", adding divs from "src"
8580 * if needed. That is, make sure that the first src->n_div divs
8581 * of the result are equal to those of src.
8583 * The result is not finalized as by design it will have redundant
8584 * divs if any divs from "src" were copied.
8586 __isl_give isl_basic_map *isl_basic_map_align_divs(
8587 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8589 int i;
8590 int known, extended;
8591 unsigned total;
8593 if (!dst || !src)
8594 return isl_basic_map_free(dst);
8596 if (src->n_div == 0)
8597 return dst;
8599 known = isl_basic_map_divs_known(src);
8600 if (known < 0)
8601 return isl_basic_map_free(dst);
8602 if (!known)
8603 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8604 "some src divs are unknown",
8605 return isl_basic_map_free(dst));
8607 src = isl_basic_map_order_divs(src);
8609 extended = 0;
8610 total = isl_space_dim(src->dim, isl_dim_all);
8611 for (i = 0; i < src->n_div; ++i) {
8612 int j = find_div(dst, src, i);
8613 if (j < 0) {
8614 if (!extended) {
8615 int extra = src->n_div - i;
8616 dst = isl_basic_map_cow(dst);
8617 if (!dst)
8618 return NULL;
8619 dst = isl_basic_map_extend_space(dst,
8620 isl_space_copy(dst->dim),
8621 extra, 0, 2 * extra);
8622 extended = 1;
8624 j = isl_basic_map_alloc_div(dst);
8625 if (j < 0)
8626 return isl_basic_map_free(dst);
8627 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8628 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8629 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8630 return isl_basic_map_free(dst);
8632 if (j != i)
8633 isl_basic_map_swap_div(dst, i, j);
8635 return dst;
8638 struct isl_basic_set *isl_basic_set_align_divs(
8639 struct isl_basic_set *dst, struct isl_basic_set *src)
8641 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8642 bset_to_bmap(src)));
8645 struct isl_map *isl_map_align_divs(struct isl_map *map)
8647 int i;
8649 if (!map)
8650 return NULL;
8651 if (map->n == 0)
8652 return map;
8653 map = isl_map_compute_divs(map);
8654 map = isl_map_cow(map);
8655 if (!map)
8656 return NULL;
8658 for (i = 1; i < map->n; ++i)
8659 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8660 for (i = 1; i < map->n; ++i) {
8661 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8662 if (!map->p[i])
8663 return isl_map_free(map);
8666 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8667 return map;
8670 struct isl_set *isl_set_align_divs(struct isl_set *set)
8672 return set_from_map(isl_map_align_divs(set_to_map(set)));
8675 /* Align the divs of the basic maps in "map" to those
8676 * of the basic maps in "list", as well as to the other basic maps in "map".
8677 * The elements in "list" are assumed to have known divs.
8679 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8680 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8682 int i, n;
8684 map = isl_map_compute_divs(map);
8685 map = isl_map_cow(map);
8686 if (!map || !list)
8687 return isl_map_free(map);
8688 if (map->n == 0)
8689 return map;
8691 n = isl_basic_map_list_n_basic_map(list);
8692 for (i = 0; i < n; ++i) {
8693 isl_basic_map *bmap;
8695 bmap = isl_basic_map_list_get_basic_map(list, i);
8696 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8697 isl_basic_map_free(bmap);
8699 if (!map->p[0])
8700 return isl_map_free(map);
8702 return isl_map_align_divs(map);
8705 /* Align the divs of each element of "list" to those of "bmap".
8706 * Both "bmap" and the elements of "list" are assumed to have known divs.
8708 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8709 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8711 int i, n;
8713 if (!list || !bmap)
8714 return isl_basic_map_list_free(list);
8716 n = isl_basic_map_list_n_basic_map(list);
8717 for (i = 0; i < n; ++i) {
8718 isl_basic_map *bmap_i;
8720 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8721 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8722 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8725 return list;
8728 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8729 __isl_take isl_map *map)
8731 isl_bool ok;
8733 ok = isl_map_compatible_domain(map, set);
8734 if (ok < 0)
8735 goto error;
8736 if (!ok)
8737 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8738 "incompatible spaces", goto error);
8739 map = isl_map_intersect_domain(map, set);
8740 set = isl_map_range(map);
8741 return set;
8742 error:
8743 isl_set_free(set);
8744 isl_map_free(map);
8745 return NULL;
8748 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8749 __isl_take isl_map *map)
8751 return isl_map_align_params_map_map_and(set, map, &set_apply);
8754 /* There is no need to cow as removing empty parts doesn't change
8755 * the meaning of the set.
8757 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8759 int i;
8761 if (!map)
8762 return NULL;
8764 for (i = map->n - 1; i >= 0; --i)
8765 remove_if_empty(map, i);
8767 return map;
8770 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8772 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8775 static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
8777 struct isl_basic_map *bmap;
8778 if (!map || map->n == 0)
8779 return NULL;
8780 bmap = map->p[map->n-1];
8781 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8782 return isl_basic_map_copy(bmap);
8785 __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
8787 return map_copy_basic_map(map);
8790 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8792 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8795 static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
8796 __isl_keep isl_basic_map *bmap)
8798 int i;
8800 if (!map || !bmap)
8801 goto error;
8802 for (i = map->n-1; i >= 0; --i) {
8803 if (map->p[i] != bmap)
8804 continue;
8805 map = isl_map_cow(map);
8806 if (!map)
8807 goto error;
8808 isl_basic_map_free(map->p[i]);
8809 if (i != map->n-1) {
8810 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8811 map->p[i] = map->p[map->n-1];
8813 map->n--;
8814 return map;
8816 return map;
8817 error:
8818 isl_map_free(map);
8819 return NULL;
8822 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8823 __isl_keep isl_basic_map *bmap)
8825 return map_drop_basic_map(map, bmap);
8828 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8829 struct isl_basic_set *bset)
8831 return set_from_map(map_drop_basic_map(set_to_map(set),
8832 bset_to_bmap(bset)));
8835 /* Given two basic sets bset1 and bset2, compute the maximal difference
8836 * between the values of dimension pos in bset1 and those in bset2
8837 * for any common value of the parameters and dimensions preceding pos.
8839 static enum isl_lp_result basic_set_maximal_difference_at(
8840 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8841 int pos, isl_int *opt)
8843 isl_basic_map *bmap1;
8844 isl_basic_map *bmap2;
8845 struct isl_ctx *ctx;
8846 struct isl_vec *obj;
8847 unsigned total;
8848 unsigned nparam;
8849 unsigned dim1;
8850 enum isl_lp_result res;
8852 if (!bset1 || !bset2)
8853 return isl_lp_error;
8855 nparam = isl_basic_set_n_param(bset1);
8856 dim1 = isl_basic_set_n_dim(bset1);
8858 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8859 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8860 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8861 isl_dim_out, 0, pos);
8862 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8863 isl_dim_out, 0, pos);
8864 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8865 if (!bmap1)
8866 return isl_lp_error;
8868 total = isl_basic_map_total_dim(bmap1);
8869 ctx = bmap1->ctx;
8870 obj = isl_vec_alloc(ctx, 1 + total);
8871 if (!obj)
8872 goto error;
8873 isl_seq_clr(obj->block.data, 1 + total);
8874 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8875 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8876 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8877 opt, NULL, NULL);
8878 isl_basic_map_free(bmap1);
8879 isl_vec_free(obj);
8880 return res;
8881 error:
8882 isl_basic_map_free(bmap1);
8883 return isl_lp_error;
8886 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8887 * for any common value of the parameters and dimensions preceding pos
8888 * in both basic sets, the values of dimension pos in bset1 are
8889 * smaller or larger than those in bset2.
8891 * Returns
8892 * 1 if bset1 follows bset2
8893 * -1 if bset1 precedes bset2
8894 * 0 if bset1 and bset2 are incomparable
8895 * -2 if some error occurred.
8897 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8898 struct isl_basic_set *bset2, int pos)
8900 isl_int opt;
8901 enum isl_lp_result res;
8902 int cmp;
8904 isl_int_init(opt);
8906 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8908 if (res == isl_lp_empty)
8909 cmp = 0;
8910 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8911 res == isl_lp_unbounded)
8912 cmp = 1;
8913 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8914 cmp = -1;
8915 else
8916 cmp = -2;
8918 isl_int_clear(opt);
8919 return cmp;
8922 /* Given two basic sets bset1 and bset2, check whether
8923 * for any common value of the parameters and dimensions preceding pos
8924 * there is a value of dimension pos in bset1 that is larger
8925 * than a value of the same dimension in bset2.
8927 * Return
8928 * 1 if there exists such a pair
8929 * 0 if there is no such pair, but there is a pair of equal values
8930 * -1 otherwise
8931 * -2 if some error occurred.
8933 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8934 __isl_keep isl_basic_set *bset2, int pos)
8936 isl_int opt;
8937 enum isl_lp_result res;
8938 int cmp;
8940 isl_int_init(opt);
8942 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8944 if (res == isl_lp_empty)
8945 cmp = -1;
8946 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8947 res == isl_lp_unbounded)
8948 cmp = 1;
8949 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8950 cmp = -1;
8951 else if (res == isl_lp_ok)
8952 cmp = 0;
8953 else
8954 cmp = -2;
8956 isl_int_clear(opt);
8957 return cmp;
8960 /* Given two sets set1 and set2, check whether
8961 * for any common value of the parameters and dimensions preceding pos
8962 * there is a value of dimension pos in set1 that is larger
8963 * than a value of the same dimension in set2.
8965 * Return
8966 * 1 if there exists such a pair
8967 * 0 if there is no such pair, but there is a pair of equal values
8968 * -1 otherwise
8969 * -2 if some error occurred.
8971 int isl_set_follows_at(__isl_keep isl_set *set1,
8972 __isl_keep isl_set *set2, int pos)
8974 int i, j;
8975 int follows = -1;
8977 if (!set1 || !set2)
8978 return -2;
8980 for (i = 0; i < set1->n; ++i)
8981 for (j = 0; j < set2->n; ++j) {
8982 int f;
8983 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8984 if (f == 1 || f == -2)
8985 return f;
8986 if (f > follows)
8987 follows = f;
8990 return follows;
8993 static isl_bool isl_basic_map_plain_has_fixed_var(
8994 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
8996 int i;
8997 int d;
8998 unsigned total;
9000 if (!bmap)
9001 return isl_bool_error;
9002 total = isl_basic_map_total_dim(bmap);
9003 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9004 for (; d+1 > pos; --d)
9005 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9006 break;
9007 if (d != pos)
9008 continue;
9009 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9010 return isl_bool_false;
9011 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9012 return isl_bool_false;
9013 if (!isl_int_is_one(bmap->eq[i][1+d]))
9014 return isl_bool_false;
9015 if (val)
9016 isl_int_neg(*val, bmap->eq[i][0]);
9017 return isl_bool_true;
9019 return isl_bool_false;
9022 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9023 unsigned pos, isl_int *val)
9025 int i;
9026 isl_int v;
9027 isl_int tmp;
9028 isl_bool fixed;
9030 if (!map)
9031 return isl_bool_error;
9032 if (map->n == 0)
9033 return isl_bool_false;
9034 if (map->n == 1)
9035 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9036 isl_int_init(v);
9037 isl_int_init(tmp);
9038 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9039 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9040 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9041 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9042 fixed = isl_bool_false;
9044 if (val)
9045 isl_int_set(*val, v);
9046 isl_int_clear(tmp);
9047 isl_int_clear(v);
9048 return fixed;
9051 static isl_bool isl_basic_set_plain_has_fixed_var(
9052 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9054 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9055 pos, val);
9058 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
9059 isl_int *val)
9061 return isl_map_plain_has_fixed_var(set_to_map(set), pos, val);
9064 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9065 enum isl_dim_type type, unsigned pos, isl_int *val)
9067 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9068 return isl_bool_error;
9069 return isl_basic_map_plain_has_fixed_var(bmap,
9070 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9073 /* If "bmap" obviously lies on a hyperplane where the given dimension
9074 * has a fixed value, then return that value.
9075 * Otherwise return NaN.
9077 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9078 __isl_keep isl_basic_map *bmap,
9079 enum isl_dim_type type, unsigned pos)
9081 isl_ctx *ctx;
9082 isl_val *v;
9083 isl_bool fixed;
9085 if (!bmap)
9086 return NULL;
9087 ctx = isl_basic_map_get_ctx(bmap);
9088 v = isl_val_alloc(ctx);
9089 if (!v)
9090 return NULL;
9091 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9092 if (fixed < 0)
9093 return isl_val_free(v);
9094 if (fixed) {
9095 isl_int_set_si(v->d, 1);
9096 return v;
9098 isl_val_free(v);
9099 return isl_val_nan(ctx);
9102 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9103 enum isl_dim_type type, unsigned pos, isl_int *val)
9105 if (pos >= isl_map_dim(map, type))
9106 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9107 "position out of bounds", return isl_bool_error);
9108 return isl_map_plain_has_fixed_var(map,
9109 map_offset(map, type) - 1 + pos, val);
9112 /* If "map" obviously lies on a hyperplane where the given dimension
9113 * has a fixed value, then return that value.
9114 * Otherwise return NaN.
9116 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9117 enum isl_dim_type type, unsigned pos)
9119 isl_ctx *ctx;
9120 isl_val *v;
9121 isl_bool fixed;
9123 if (!map)
9124 return NULL;
9125 ctx = isl_map_get_ctx(map);
9126 v = isl_val_alloc(ctx);
9127 if (!v)
9128 return NULL;
9129 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9130 if (fixed < 0)
9131 return isl_val_free(v);
9132 if (fixed) {
9133 isl_int_set_si(v->d, 1);
9134 return v;
9136 isl_val_free(v);
9137 return isl_val_nan(ctx);
9140 /* If "set" obviously lies on a hyperplane where the given dimension
9141 * has a fixed value, then return that value.
9142 * Otherwise return NaN.
9144 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9145 enum isl_dim_type type, unsigned pos)
9147 return isl_map_plain_get_val_if_fixed(set, type, pos);
9150 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
9151 enum isl_dim_type type, unsigned pos, isl_int *val)
9153 return isl_map_plain_is_fixed(set, type, pos, val);
9156 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9157 * then return this fixed value in *val.
9159 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9160 unsigned dim, isl_int *val)
9162 return isl_basic_set_plain_has_fixed_var(bset,
9163 isl_basic_set_n_param(bset) + dim, val);
9166 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9167 * then return this fixed value in *val.
9169 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
9170 unsigned dim, isl_int *val)
9172 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
9175 /* Check if input variable in has fixed value and if so and if val is not NULL,
9176 * then return this fixed value in *val.
9178 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
9179 unsigned in, isl_int *val)
9181 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
9184 /* Check if dimension dim has an (obvious) fixed lower bound and if so
9185 * and if val is not NULL, then return this lower bound in *val.
9187 int isl_basic_set_plain_dim_has_fixed_lower_bound(
9188 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
9190 int i, i_eq = -1, i_ineq = -1;
9191 isl_int *c;
9192 unsigned total;
9193 unsigned nparam;
9195 if (!bset)
9196 return -1;
9197 total = isl_basic_set_total_dim(bset);
9198 nparam = isl_basic_set_n_param(bset);
9199 for (i = 0; i < bset->n_eq; ++i) {
9200 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
9201 continue;
9202 if (i_eq != -1)
9203 return 0;
9204 i_eq = i;
9206 for (i = 0; i < bset->n_ineq; ++i) {
9207 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
9208 continue;
9209 if (i_eq != -1 || i_ineq != -1)
9210 return 0;
9211 i_ineq = i;
9213 if (i_eq == -1 && i_ineq == -1)
9214 return 0;
9215 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
9216 /* The coefficient should always be one due to normalization. */
9217 if (!isl_int_is_one(c[1+nparam+dim]))
9218 return 0;
9219 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
9220 return 0;
9221 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
9222 total - nparam - dim - 1) != -1)
9223 return 0;
9224 if (val)
9225 isl_int_neg(*val, c[0]);
9226 return 1;
9229 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
9230 unsigned dim, isl_int *val)
9232 int i;
9233 isl_int v;
9234 isl_int tmp;
9235 int fixed;
9237 if (!set)
9238 return -1;
9239 if (set->n == 0)
9240 return 0;
9241 if (set->n == 1)
9242 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9243 dim, val);
9244 isl_int_init(v);
9245 isl_int_init(tmp);
9246 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9247 dim, &v);
9248 for (i = 1; fixed == 1 && i < set->n; ++i) {
9249 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9250 dim, &tmp);
9251 if (fixed == 1 && isl_int_ne(tmp, v))
9252 fixed = 0;
9254 if (val)
9255 isl_int_set(*val, v);
9256 isl_int_clear(tmp);
9257 isl_int_clear(v);
9258 return fixed;
9261 /* Return -1 if the constraint "c1" should be sorted before "c2"
9262 * and 1 if it should be sorted after "c2".
9263 * Return 0 if the two constraints are the same (up to the constant term).
9265 * In particular, if a constraint involves later variables than another
9266 * then it is sorted after this other constraint.
9267 * uset_gist depends on constraints without existentially quantified
9268 * variables sorting first.
9270 * For constraints that have the same latest variable, those
9271 * with the same coefficient for this latest variable (first in absolute value
9272 * and then in actual value) are grouped together.
9273 * This is useful for detecting pairs of constraints that can
9274 * be chained in their printed representation.
9276 * Finally, within a group, constraints are sorted according to
9277 * their coefficients (excluding the constant term).
9279 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9281 isl_int **c1 = (isl_int **) p1;
9282 isl_int **c2 = (isl_int **) p2;
9283 int l1, l2;
9284 unsigned size = *(unsigned *) arg;
9285 int cmp;
9287 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9288 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9290 if (l1 != l2)
9291 return l1 - l2;
9293 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9294 if (cmp != 0)
9295 return cmp;
9296 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9297 if (cmp != 0)
9298 return -cmp;
9300 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9303 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9304 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9305 * and 0 if the two constraints are the same (up to the constant term).
9307 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9308 isl_int *c1, isl_int *c2)
9310 unsigned total;
9312 if (!bmap)
9313 return -2;
9314 total = isl_basic_map_total_dim(bmap);
9315 return sort_constraint_cmp(&c1, &c2, &total);
9318 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9319 __isl_take isl_basic_map *bmap)
9321 unsigned total;
9323 if (!bmap)
9324 return NULL;
9325 if (bmap->n_ineq == 0)
9326 return bmap;
9327 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9328 return bmap;
9329 total = isl_basic_map_total_dim(bmap);
9330 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9331 &sort_constraint_cmp, &total) < 0)
9332 return isl_basic_map_free(bmap);
9333 return bmap;
9336 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9337 __isl_take isl_basic_set *bset)
9339 isl_basic_map *bmap = bset_to_bmap(bset);
9340 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9343 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9345 if (!bmap)
9346 return NULL;
9347 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9348 return bmap;
9349 bmap = isl_basic_map_remove_redundancies(bmap);
9350 bmap = isl_basic_map_sort_constraints(bmap);
9351 if (bmap)
9352 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9353 return bmap;
9356 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9358 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
9361 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9362 const __isl_keep isl_basic_map *bmap2)
9364 int i, cmp;
9365 unsigned total;
9367 if (!bmap1 || !bmap2)
9368 return -1;
9370 if (bmap1 == bmap2)
9371 return 0;
9372 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9373 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9374 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9375 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9376 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9377 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9378 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9379 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9380 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9381 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9382 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9383 return 0;
9384 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9385 return 1;
9386 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9387 return -1;
9388 if (bmap1->n_eq != bmap2->n_eq)
9389 return bmap1->n_eq - bmap2->n_eq;
9390 if (bmap1->n_ineq != bmap2->n_ineq)
9391 return bmap1->n_ineq - bmap2->n_ineq;
9392 if (bmap1->n_div != bmap2->n_div)
9393 return bmap1->n_div - bmap2->n_div;
9394 total = isl_basic_map_total_dim(bmap1);
9395 for (i = 0; i < bmap1->n_eq; ++i) {
9396 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9397 if (cmp)
9398 return cmp;
9400 for (i = 0; i < bmap1->n_ineq; ++i) {
9401 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9402 if (cmp)
9403 return cmp;
9405 for (i = 0; i < bmap1->n_div; ++i) {
9406 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9407 if (cmp)
9408 return cmp;
9410 return 0;
9413 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9414 const __isl_keep isl_basic_set *bset2)
9416 return isl_basic_map_plain_cmp(bset1, bset2);
9419 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9421 int i, cmp;
9423 if (set1 == set2)
9424 return 0;
9425 if (set1->n != set2->n)
9426 return set1->n - set2->n;
9428 for (i = 0; i < set1->n; ++i) {
9429 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9430 if (cmp)
9431 return cmp;
9434 return 0;
9437 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9438 __isl_keep isl_basic_map *bmap2)
9440 if (!bmap1 || !bmap2)
9441 return isl_bool_error;
9442 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9445 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9446 __isl_keep isl_basic_set *bset2)
9448 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9449 bset_to_bmap(bset2));
9452 static int qsort_bmap_cmp(const void *p1, const void *p2)
9454 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9455 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9457 return isl_basic_map_plain_cmp(bmap1, bmap2);
9460 /* Sort the basic maps of "map" and remove duplicate basic maps.
9462 * While removing basic maps, we make sure that the basic maps remain
9463 * sorted because isl_map_normalize expects the basic maps of the result
9464 * to be sorted.
9466 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9468 int i, j;
9470 map = isl_map_remove_empty_parts(map);
9471 if (!map)
9472 return NULL;
9473 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9474 for (i = map->n - 1; i >= 1; --i) {
9475 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9476 continue;
9477 isl_basic_map_free(map->p[i-1]);
9478 for (j = i; j < map->n; ++j)
9479 map->p[j - 1] = map->p[j];
9480 map->n--;
9483 return map;
9486 /* Remove obvious duplicates among the basic maps of "map".
9488 * Unlike isl_map_normalize, this function does not remove redundant
9489 * constraints and only removes duplicates that have exactly the same
9490 * constraints in the input. It does sort the constraints and
9491 * the basic maps to ease the detection of duplicates.
9493 * If "map" has already been normalized or if the basic maps are
9494 * disjoint, then there can be no duplicates.
9496 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9498 int i;
9499 isl_basic_map *bmap;
9501 if (!map)
9502 return NULL;
9503 if (map->n <= 1)
9504 return map;
9505 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9506 return map;
9507 for (i = 0; i < map->n; ++i) {
9508 bmap = isl_basic_map_copy(map->p[i]);
9509 bmap = isl_basic_map_sort_constraints(bmap);
9510 if (!bmap)
9511 return isl_map_free(map);
9512 isl_basic_map_free(map->p[i]);
9513 map->p[i] = bmap;
9516 map = sort_and_remove_duplicates(map);
9517 return map;
9520 /* We normalize in place, but if anything goes wrong we need
9521 * to return NULL, so we need to make sure we don't change the
9522 * meaning of any possible other copies of map.
9524 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9526 int i;
9527 struct isl_basic_map *bmap;
9529 if (!map)
9530 return NULL;
9531 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9532 return map;
9533 for (i = 0; i < map->n; ++i) {
9534 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9535 if (!bmap)
9536 goto error;
9537 isl_basic_map_free(map->p[i]);
9538 map->p[i] = bmap;
9541 map = sort_and_remove_duplicates(map);
9542 if (map)
9543 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9544 return map;
9545 error:
9546 isl_map_free(map);
9547 return NULL;
9550 struct isl_set *isl_set_normalize(struct isl_set *set)
9552 return set_from_map(isl_map_normalize(set_to_map(set)));
9555 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9556 __isl_keep isl_map *map2)
9558 int i;
9559 isl_bool equal;
9561 if (!map1 || !map2)
9562 return isl_bool_error;
9564 if (map1 == map2)
9565 return isl_bool_true;
9566 if (!isl_space_is_equal(map1->dim, map2->dim))
9567 return isl_bool_false;
9569 map1 = isl_map_copy(map1);
9570 map2 = isl_map_copy(map2);
9571 map1 = isl_map_normalize(map1);
9572 map2 = isl_map_normalize(map2);
9573 if (!map1 || !map2)
9574 goto error;
9575 equal = map1->n == map2->n;
9576 for (i = 0; equal && i < map1->n; ++i) {
9577 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9578 if (equal < 0)
9579 goto error;
9581 isl_map_free(map1);
9582 isl_map_free(map2);
9583 return equal;
9584 error:
9585 isl_map_free(map1);
9586 isl_map_free(map2);
9587 return isl_bool_error;
9590 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9591 __isl_keep isl_set *set2)
9593 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9596 /* Return an interval that ranges from min to max (inclusive)
9598 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9599 isl_int min, isl_int max)
9601 int k;
9602 struct isl_basic_set *bset = NULL;
9604 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9605 if (!bset)
9606 goto error;
9608 k = isl_basic_set_alloc_inequality(bset);
9609 if (k < 0)
9610 goto error;
9611 isl_int_set_si(bset->ineq[k][1], 1);
9612 isl_int_neg(bset->ineq[k][0], min);
9614 k = isl_basic_set_alloc_inequality(bset);
9615 if (k < 0)
9616 goto error;
9617 isl_int_set_si(bset->ineq[k][1], -1);
9618 isl_int_set(bset->ineq[k][0], max);
9620 return bset;
9621 error:
9622 isl_basic_set_free(bset);
9623 return NULL;
9626 /* Return the basic maps in "map" as a list.
9628 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9629 __isl_keep isl_map *map)
9631 int i;
9632 isl_ctx *ctx;
9633 isl_basic_map_list *list;
9635 if (!map)
9636 return NULL;
9637 ctx = isl_map_get_ctx(map);
9638 list = isl_basic_map_list_alloc(ctx, map->n);
9640 for (i = 0; i < map->n; ++i) {
9641 isl_basic_map *bmap;
9643 bmap = isl_basic_map_copy(map->p[i]);
9644 list = isl_basic_map_list_add(list, bmap);
9647 return list;
9650 /* Return the intersection of the elements in the non-empty list "list".
9651 * All elements are assumed to live in the same space.
9653 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9654 __isl_take isl_basic_map_list *list)
9656 int i, n;
9657 isl_basic_map *bmap;
9659 if (!list)
9660 return NULL;
9661 n = isl_basic_map_list_n_basic_map(list);
9662 if (n < 1)
9663 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9664 "expecting non-empty list", goto error);
9666 bmap = isl_basic_map_list_get_basic_map(list, 0);
9667 for (i = 1; i < n; ++i) {
9668 isl_basic_map *bmap_i;
9670 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9671 bmap = isl_basic_map_intersect(bmap, bmap_i);
9674 isl_basic_map_list_free(list);
9675 return bmap;
9676 error:
9677 isl_basic_map_list_free(list);
9678 return NULL;
9681 /* Return the intersection of the elements in the non-empty list "list".
9682 * All elements are assumed to live in the same space.
9684 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9685 __isl_take isl_basic_set_list *list)
9687 return isl_basic_map_list_intersect(list);
9690 /* Return the union of the elements of "list".
9691 * The list is required to have at least one element.
9693 __isl_give isl_set *isl_basic_set_list_union(
9694 __isl_take isl_basic_set_list *list)
9696 int i, n;
9697 isl_space *space;
9698 isl_basic_set *bset;
9699 isl_set *set;
9701 if (!list)
9702 return NULL;
9703 n = isl_basic_set_list_n_basic_set(list);
9704 if (n < 1)
9705 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9706 "expecting non-empty list", goto error);
9708 bset = isl_basic_set_list_get_basic_set(list, 0);
9709 space = isl_basic_set_get_space(bset);
9710 isl_basic_set_free(bset);
9712 set = isl_set_alloc_space(space, n, 0);
9713 for (i = 0; i < n; ++i) {
9714 bset = isl_basic_set_list_get_basic_set(list, i);
9715 set = isl_set_add_basic_set(set, bset);
9718 isl_basic_set_list_free(list);
9719 return set;
9720 error:
9721 isl_basic_set_list_free(list);
9722 return NULL;
9725 /* Return the union of the elements in the non-empty list "list".
9726 * All elements are assumed to live in the same space.
9728 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9730 int i, n;
9731 isl_set *set;
9733 if (!list)
9734 return NULL;
9735 n = isl_set_list_n_set(list);
9736 if (n < 1)
9737 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9738 "expecting non-empty list", goto error);
9740 set = isl_set_list_get_set(list, 0);
9741 for (i = 1; i < n; ++i) {
9742 isl_set *set_i;
9744 set_i = isl_set_list_get_set(list, i);
9745 set = isl_set_union(set, set_i);
9748 isl_set_list_free(list);
9749 return set;
9750 error:
9751 isl_set_list_free(list);
9752 return NULL;
9755 /* Return the Cartesian product of the basic sets in list (in the given order).
9757 __isl_give isl_basic_set *isl_basic_set_list_product(
9758 __isl_take struct isl_basic_set_list *list)
9760 int i;
9761 unsigned dim;
9762 unsigned nparam;
9763 unsigned extra;
9764 unsigned n_eq;
9765 unsigned n_ineq;
9766 struct isl_basic_set *product = NULL;
9768 if (!list)
9769 goto error;
9770 isl_assert(list->ctx, list->n > 0, goto error);
9771 isl_assert(list->ctx, list->p[0], goto error);
9772 nparam = isl_basic_set_n_param(list->p[0]);
9773 dim = isl_basic_set_n_dim(list->p[0]);
9774 extra = list->p[0]->n_div;
9775 n_eq = list->p[0]->n_eq;
9776 n_ineq = list->p[0]->n_ineq;
9777 for (i = 1; i < list->n; ++i) {
9778 isl_assert(list->ctx, list->p[i], goto error);
9779 isl_assert(list->ctx,
9780 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9781 dim += isl_basic_set_n_dim(list->p[i]);
9782 extra += list->p[i]->n_div;
9783 n_eq += list->p[i]->n_eq;
9784 n_ineq += list->p[i]->n_ineq;
9786 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9787 n_eq, n_ineq);
9788 if (!product)
9789 goto error;
9790 dim = 0;
9791 for (i = 0; i < list->n; ++i) {
9792 isl_basic_set_add_constraints(product,
9793 isl_basic_set_copy(list->p[i]), dim);
9794 dim += isl_basic_set_n_dim(list->p[i]);
9796 isl_basic_set_list_free(list);
9797 return product;
9798 error:
9799 isl_basic_set_free(product);
9800 isl_basic_set_list_free(list);
9801 return NULL;
9804 struct isl_basic_map *isl_basic_map_product(
9805 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9807 isl_space *dim_result = NULL;
9808 struct isl_basic_map *bmap;
9809 unsigned in1, in2, out1, out2, nparam, total, pos;
9810 struct isl_dim_map *dim_map1, *dim_map2;
9812 if (!bmap1 || !bmap2)
9813 goto error;
9815 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9816 bmap2->dim, isl_dim_param), goto error);
9817 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9818 isl_space_copy(bmap2->dim));
9820 in1 = isl_basic_map_n_in(bmap1);
9821 in2 = isl_basic_map_n_in(bmap2);
9822 out1 = isl_basic_map_n_out(bmap1);
9823 out2 = isl_basic_map_n_out(bmap2);
9824 nparam = isl_basic_map_n_param(bmap1);
9826 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9827 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9828 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9829 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9830 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9831 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9832 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9833 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9834 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9835 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9836 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9838 bmap = isl_basic_map_alloc_space(dim_result,
9839 bmap1->n_div + bmap2->n_div,
9840 bmap1->n_eq + bmap2->n_eq,
9841 bmap1->n_ineq + bmap2->n_ineq);
9842 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9843 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9844 bmap = isl_basic_map_simplify(bmap);
9845 return isl_basic_map_finalize(bmap);
9846 error:
9847 isl_basic_map_free(bmap1);
9848 isl_basic_map_free(bmap2);
9849 return NULL;
9852 __isl_give isl_basic_map *isl_basic_map_flat_product(
9853 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9855 isl_basic_map *prod;
9857 prod = isl_basic_map_product(bmap1, bmap2);
9858 prod = isl_basic_map_flatten(prod);
9859 return prod;
9862 __isl_give isl_basic_set *isl_basic_set_flat_product(
9863 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9865 return isl_basic_map_flat_range_product(bset1, bset2);
9868 __isl_give isl_basic_map *isl_basic_map_domain_product(
9869 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9871 isl_space *space_result = NULL;
9872 isl_basic_map *bmap;
9873 unsigned in1, in2, out, nparam, total, pos;
9874 struct isl_dim_map *dim_map1, *dim_map2;
9876 if (!bmap1 || !bmap2)
9877 goto error;
9879 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9880 isl_space_copy(bmap2->dim));
9882 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9883 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9884 out = isl_basic_map_dim(bmap1, isl_dim_out);
9885 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9887 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9888 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9889 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9890 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9891 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9892 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9893 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9894 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9895 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9896 isl_dim_map_div(dim_map1, bmap1, pos += out);
9897 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9899 bmap = isl_basic_map_alloc_space(space_result,
9900 bmap1->n_div + bmap2->n_div,
9901 bmap1->n_eq + bmap2->n_eq,
9902 bmap1->n_ineq + bmap2->n_ineq);
9903 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9904 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9905 bmap = isl_basic_map_simplify(bmap);
9906 return isl_basic_map_finalize(bmap);
9907 error:
9908 isl_basic_map_free(bmap1);
9909 isl_basic_map_free(bmap2);
9910 return NULL;
9913 __isl_give isl_basic_map *isl_basic_map_range_product(
9914 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9916 isl_bool rational;
9917 isl_space *dim_result = NULL;
9918 isl_basic_map *bmap;
9919 unsigned in, out1, out2, nparam, total, pos;
9920 struct isl_dim_map *dim_map1, *dim_map2;
9922 rational = isl_basic_map_is_rational(bmap1);
9923 if (rational >= 0 && rational)
9924 rational = isl_basic_map_is_rational(bmap2);
9925 if (!bmap1 || !bmap2 || rational < 0)
9926 goto error;
9928 if (!isl_space_match(bmap1->dim, isl_dim_param,
9929 bmap2->dim, isl_dim_param))
9930 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9931 "parameters don't match", goto error);
9933 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9934 isl_space_copy(bmap2->dim));
9936 in = isl_basic_map_dim(bmap1, isl_dim_in);
9937 out1 = isl_basic_map_n_out(bmap1);
9938 out2 = isl_basic_map_n_out(bmap2);
9939 nparam = isl_basic_map_n_param(bmap1);
9941 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9942 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9943 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9944 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9945 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9946 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9947 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9948 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9949 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9950 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9951 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9953 bmap = isl_basic_map_alloc_space(dim_result,
9954 bmap1->n_div + bmap2->n_div,
9955 bmap1->n_eq + bmap2->n_eq,
9956 bmap1->n_ineq + bmap2->n_ineq);
9957 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9958 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9959 if (rational)
9960 bmap = isl_basic_map_set_rational(bmap);
9961 bmap = isl_basic_map_simplify(bmap);
9962 return isl_basic_map_finalize(bmap);
9963 error:
9964 isl_basic_map_free(bmap1);
9965 isl_basic_map_free(bmap2);
9966 return NULL;
9969 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9970 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9972 isl_basic_map *prod;
9974 prod = isl_basic_map_range_product(bmap1, bmap2);
9975 prod = isl_basic_map_flatten_range(prod);
9976 return prod;
9979 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9980 * and collect the results.
9981 * The result live in the space obtained by calling "space_product"
9982 * on the spaces of "map1" and "map2".
9983 * If "remove_duplicates" is set then the result may contain duplicates
9984 * (even if the inputs do not) and so we try and remove the obvious
9985 * duplicates.
9987 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9988 __isl_take isl_map *map2,
9989 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9990 __isl_take isl_space *right),
9991 __isl_give isl_basic_map *(*basic_map_product)(
9992 __isl_take isl_basic_map *left,
9993 __isl_take isl_basic_map *right),
9994 int remove_duplicates)
9996 unsigned flags = 0;
9997 struct isl_map *result;
9998 int i, j;
10000 if (!map1 || !map2)
10001 goto error;
10003 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
10004 map2->dim, isl_dim_param), goto error);
10006 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10007 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10008 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10010 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10011 isl_space_copy(map2->dim)),
10012 map1->n * map2->n, flags);
10013 if (!result)
10014 goto error;
10015 for (i = 0; i < map1->n; ++i)
10016 for (j = 0; j < map2->n; ++j) {
10017 struct isl_basic_map *part;
10018 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10019 isl_basic_map_copy(map2->p[j]));
10020 if (isl_basic_map_is_empty(part))
10021 isl_basic_map_free(part);
10022 else
10023 result = isl_map_add_basic_map(result, part);
10024 if (!result)
10025 goto error;
10027 if (remove_duplicates)
10028 result = isl_map_remove_obvious_duplicates(result);
10029 isl_map_free(map1);
10030 isl_map_free(map2);
10031 return result;
10032 error:
10033 isl_map_free(map1);
10034 isl_map_free(map2);
10035 return NULL;
10038 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10040 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10041 __isl_take isl_map *map2)
10043 return map_product(map1, map2, &isl_space_product,
10044 &isl_basic_map_product, 0);
10047 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10048 __isl_take isl_map *map2)
10050 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10053 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10055 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10056 __isl_take isl_map *map2)
10058 isl_map *prod;
10060 prod = isl_map_product(map1, map2);
10061 prod = isl_map_flatten(prod);
10062 return prod;
10065 /* Given two set A and B, construct its Cartesian product A x B.
10067 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10069 return isl_map_range_product(set1, set2);
10072 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10073 __isl_take isl_set *set2)
10075 return isl_map_flat_range_product(set1, set2);
10078 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10080 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10081 __isl_take isl_map *map2)
10083 return map_product(map1, map2, &isl_space_domain_product,
10084 &isl_basic_map_domain_product, 1);
10087 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10089 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10090 __isl_take isl_map *map2)
10092 return map_product(map1, map2, &isl_space_range_product,
10093 &isl_basic_map_range_product, 1);
10096 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10097 __isl_take isl_map *map2)
10099 return isl_map_align_params_map_map_and(map1, map2,
10100 &map_domain_product_aligned);
10103 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10104 __isl_take isl_map *map2)
10106 return isl_map_align_params_map_map_and(map1, map2,
10107 &map_range_product_aligned);
10110 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10112 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10114 isl_space *space;
10115 int total1, keep1, total2, keep2;
10117 if (!map)
10118 return NULL;
10119 if (!isl_space_domain_is_wrapping(map->dim) ||
10120 !isl_space_range_is_wrapping(map->dim))
10121 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10122 "not a product", return isl_map_free(map));
10124 space = isl_map_get_space(map);
10125 total1 = isl_space_dim(space, isl_dim_in);
10126 total2 = isl_space_dim(space, isl_dim_out);
10127 space = isl_space_factor_domain(space);
10128 keep1 = isl_space_dim(space, isl_dim_in);
10129 keep2 = isl_space_dim(space, isl_dim_out);
10130 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10131 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10132 map = isl_map_reset_space(map, space);
10134 return map;
10137 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10139 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10141 isl_space *space;
10142 int total1, keep1, total2, keep2;
10144 if (!map)
10145 return NULL;
10146 if (!isl_space_domain_is_wrapping(map->dim) ||
10147 !isl_space_range_is_wrapping(map->dim))
10148 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10149 "not a product", return isl_map_free(map));
10151 space = isl_map_get_space(map);
10152 total1 = isl_space_dim(space, isl_dim_in);
10153 total2 = isl_space_dim(space, isl_dim_out);
10154 space = isl_space_factor_range(space);
10155 keep1 = isl_space_dim(space, isl_dim_in);
10156 keep2 = isl_space_dim(space, isl_dim_out);
10157 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10158 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10159 map = isl_map_reset_space(map, space);
10161 return map;
10164 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10166 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10168 isl_space *space;
10169 int total, keep;
10171 if (!map)
10172 return NULL;
10173 if (!isl_space_domain_is_wrapping(map->dim))
10174 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10175 "domain is not a product", return isl_map_free(map));
10177 space = isl_map_get_space(map);
10178 total = isl_space_dim(space, isl_dim_in);
10179 space = isl_space_domain_factor_domain(space);
10180 keep = isl_space_dim(space, isl_dim_in);
10181 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10182 map = isl_map_reset_space(map, space);
10184 return map;
10187 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10189 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10191 isl_space *space;
10192 int total, keep;
10194 if (!map)
10195 return NULL;
10196 if (!isl_space_domain_is_wrapping(map->dim))
10197 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10198 "domain is not a product", return isl_map_free(map));
10200 space = isl_map_get_space(map);
10201 total = isl_space_dim(space, isl_dim_in);
10202 space = isl_space_domain_factor_range(space);
10203 keep = isl_space_dim(space, isl_dim_in);
10204 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10205 map = isl_map_reset_space(map, space);
10207 return map;
10210 /* Given a map A -> [B -> C], extract the map A -> B.
10212 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10214 isl_space *space;
10215 int total, keep;
10217 if (!map)
10218 return NULL;
10219 if (!isl_space_range_is_wrapping(map->dim))
10220 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10221 "range is not a product", return isl_map_free(map));
10223 space = isl_map_get_space(map);
10224 total = isl_space_dim(space, isl_dim_out);
10225 space = isl_space_range_factor_domain(space);
10226 keep = isl_space_dim(space, isl_dim_out);
10227 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10228 map = isl_map_reset_space(map, space);
10230 return map;
10233 /* Given a map A -> [B -> C], extract the map A -> C.
10235 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10237 isl_space *space;
10238 int total, keep;
10240 if (!map)
10241 return NULL;
10242 if (!isl_space_range_is_wrapping(map->dim))
10243 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10244 "range is not a product", return isl_map_free(map));
10246 space = isl_map_get_space(map);
10247 total = isl_space_dim(space, isl_dim_out);
10248 space = isl_space_range_factor_range(space);
10249 keep = isl_space_dim(space, isl_dim_out);
10250 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10251 map = isl_map_reset_space(map, space);
10253 return map;
10256 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10258 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10259 __isl_take isl_map *map2)
10261 isl_map *prod;
10263 prod = isl_map_domain_product(map1, map2);
10264 prod = isl_map_flatten_domain(prod);
10265 return prod;
10268 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10270 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10271 __isl_take isl_map *map2)
10273 isl_map *prod;
10275 prod = isl_map_range_product(map1, map2);
10276 prod = isl_map_flatten_range(prod);
10277 return prod;
10280 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10282 int i;
10283 uint32_t hash = isl_hash_init();
10284 unsigned total;
10286 if (!bmap)
10287 return 0;
10288 bmap = isl_basic_map_copy(bmap);
10289 bmap = isl_basic_map_normalize(bmap);
10290 if (!bmap)
10291 return 0;
10292 total = isl_basic_map_total_dim(bmap);
10293 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10294 for (i = 0; i < bmap->n_eq; ++i) {
10295 uint32_t c_hash;
10296 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10297 isl_hash_hash(hash, c_hash);
10299 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10300 for (i = 0; i < bmap->n_ineq; ++i) {
10301 uint32_t c_hash;
10302 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10303 isl_hash_hash(hash, c_hash);
10305 isl_hash_byte(hash, bmap->n_div & 0xFF);
10306 for (i = 0; i < bmap->n_div; ++i) {
10307 uint32_t c_hash;
10308 if (isl_int_is_zero(bmap->div[i][0]))
10309 continue;
10310 isl_hash_byte(hash, i & 0xFF);
10311 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10312 isl_hash_hash(hash, c_hash);
10314 isl_basic_map_free(bmap);
10315 return hash;
10318 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10320 return isl_basic_map_get_hash(bset_to_bmap(bset));
10323 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10325 int i;
10326 uint32_t hash;
10328 if (!map)
10329 return 0;
10330 map = isl_map_copy(map);
10331 map = isl_map_normalize(map);
10332 if (!map)
10333 return 0;
10335 hash = isl_hash_init();
10336 for (i = 0; i < map->n; ++i) {
10337 uint32_t bmap_hash;
10338 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10339 isl_hash_hash(hash, bmap_hash);
10342 isl_map_free(map);
10344 return hash;
10347 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10349 return isl_map_get_hash(set_to_map(set));
10352 /* Check if the value for dimension dim is completely determined
10353 * by the values of the other parameters and variables.
10354 * That is, check if dimension dim is involved in an equality.
10356 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10358 int i;
10359 unsigned nparam;
10361 if (!bset)
10362 return -1;
10363 nparam = isl_basic_set_n_param(bset);
10364 for (i = 0; i < bset->n_eq; ++i)
10365 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10366 return 1;
10367 return 0;
10370 /* Check if the value for dimension dim is completely determined
10371 * by the values of the other parameters and variables.
10372 * That is, check if dimension dim is involved in an equality
10373 * for each of the subsets.
10375 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10377 int i;
10379 if (!set)
10380 return -1;
10381 for (i = 0; i < set->n; ++i) {
10382 int unique;
10383 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10384 if (unique != 1)
10385 return unique;
10387 return 1;
10390 /* Return the number of basic maps in the (current) representation of "map".
10392 int isl_map_n_basic_map(__isl_keep isl_map *map)
10394 return map ? map->n : 0;
10397 int isl_set_n_basic_set(__isl_keep isl_set *set)
10399 return set ? set->n : 0;
10402 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10403 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10405 int i;
10407 if (!map)
10408 return isl_stat_error;
10410 for (i = 0; i < map->n; ++i)
10411 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10412 return isl_stat_error;
10414 return isl_stat_ok;
10417 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10418 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10420 int i;
10422 if (!set)
10423 return isl_stat_error;
10425 for (i = 0; i < set->n; ++i)
10426 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10427 return isl_stat_error;
10429 return isl_stat_ok;
10432 /* Return a list of basic sets, the union of which is equal to "set".
10434 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10435 __isl_keep isl_set *set)
10437 int i;
10438 isl_basic_set_list *list;
10440 if (!set)
10441 return NULL;
10443 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10444 for (i = 0; i < set->n; ++i) {
10445 isl_basic_set *bset;
10447 bset = isl_basic_set_copy(set->p[i]);
10448 list = isl_basic_set_list_add(list, bset);
10451 return list;
10454 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10456 isl_space *dim;
10458 if (!bset)
10459 return NULL;
10461 bset = isl_basic_set_cow(bset);
10462 if (!bset)
10463 return NULL;
10465 dim = isl_basic_set_get_space(bset);
10466 dim = isl_space_lift(dim, bset->n_div);
10467 if (!dim)
10468 goto error;
10469 isl_space_free(bset->dim);
10470 bset->dim = dim;
10471 bset->extra -= bset->n_div;
10472 bset->n_div = 0;
10474 bset = isl_basic_set_finalize(bset);
10476 return bset;
10477 error:
10478 isl_basic_set_free(bset);
10479 return NULL;
10482 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10484 int i;
10485 isl_space *dim;
10486 unsigned n_div;
10488 set = isl_set_align_divs(set);
10490 if (!set)
10491 return NULL;
10493 set = isl_set_cow(set);
10494 if (!set)
10495 return NULL;
10497 n_div = set->p[0]->n_div;
10498 dim = isl_set_get_space(set);
10499 dim = isl_space_lift(dim, n_div);
10500 if (!dim)
10501 goto error;
10502 isl_space_free(set->dim);
10503 set->dim = dim;
10505 for (i = 0; i < set->n; ++i) {
10506 set->p[i] = isl_basic_set_lift(set->p[i]);
10507 if (!set->p[i])
10508 goto error;
10511 return set;
10512 error:
10513 isl_set_free(set);
10514 return NULL;
10517 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10519 isl_space *dim;
10520 struct isl_basic_map *bmap;
10521 unsigned n_set;
10522 unsigned n_div;
10523 unsigned n_param;
10524 unsigned total;
10525 int i, k, l;
10527 set = isl_set_align_divs(set);
10529 if (!set)
10530 return NULL;
10532 dim = isl_set_get_space(set);
10533 if (set->n == 0 || set->p[0]->n_div == 0) {
10534 isl_set_free(set);
10535 return isl_map_identity(isl_space_map_from_set(dim));
10538 n_div = set->p[0]->n_div;
10539 dim = isl_space_map_from_set(dim);
10540 n_param = isl_space_dim(dim, isl_dim_param);
10541 n_set = isl_space_dim(dim, isl_dim_in);
10542 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10543 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10544 for (i = 0; i < n_set; ++i)
10545 bmap = var_equal(bmap, i);
10547 total = n_param + n_set + n_set + n_div;
10548 for (i = 0; i < n_div; ++i) {
10549 k = isl_basic_map_alloc_inequality(bmap);
10550 if (k < 0)
10551 goto error;
10552 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10553 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10554 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10555 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10556 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10557 set->p[0]->div[i][0]);
10559 l = isl_basic_map_alloc_inequality(bmap);
10560 if (l < 0)
10561 goto error;
10562 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10563 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10564 set->p[0]->div[i][0]);
10565 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10568 isl_set_free(set);
10569 bmap = isl_basic_map_simplify(bmap);
10570 bmap = isl_basic_map_finalize(bmap);
10571 return isl_map_from_basic_map(bmap);
10572 error:
10573 isl_set_free(set);
10574 isl_basic_map_free(bmap);
10575 return NULL;
10578 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10580 unsigned dim;
10581 int size = 0;
10583 if (!bset)
10584 return -1;
10586 dim = isl_basic_set_total_dim(bset);
10587 size += bset->n_eq * (1 + dim);
10588 size += bset->n_ineq * (1 + dim);
10589 size += bset->n_div * (2 + dim);
10591 return size;
10594 int isl_set_size(__isl_keep isl_set *set)
10596 int i;
10597 int size = 0;
10599 if (!set)
10600 return -1;
10602 for (i = 0; i < set->n; ++i)
10603 size += isl_basic_set_size(set->p[i]);
10605 return size;
10608 /* Check if there is any lower bound (if lower == 0) and/or upper
10609 * bound (if upper == 0) on the specified dim.
10611 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10612 enum isl_dim_type type, unsigned pos, int lower, int upper)
10614 int i;
10616 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10617 return isl_bool_error;
10619 pos += isl_basic_map_offset(bmap, type);
10621 for (i = 0; i < bmap->n_div; ++i) {
10622 if (isl_int_is_zero(bmap->div[i][0]))
10623 continue;
10624 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10625 return isl_bool_true;
10628 for (i = 0; i < bmap->n_eq; ++i)
10629 if (!isl_int_is_zero(bmap->eq[i][pos]))
10630 return isl_bool_true;
10632 for (i = 0; i < bmap->n_ineq; ++i) {
10633 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10634 if (sgn > 0)
10635 lower = 1;
10636 if (sgn < 0)
10637 upper = 1;
10640 return lower && upper;
10643 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10644 enum isl_dim_type type, unsigned pos)
10646 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10649 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10650 enum isl_dim_type type, unsigned pos)
10652 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10655 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10656 enum isl_dim_type type, unsigned pos)
10658 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10661 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10662 enum isl_dim_type type, unsigned pos)
10664 int i;
10666 if (!map)
10667 return isl_bool_error;
10669 for (i = 0; i < map->n; ++i) {
10670 isl_bool bounded;
10671 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10672 if (bounded < 0 || !bounded)
10673 return bounded;
10676 return isl_bool_true;
10679 /* Return true if the specified dim is involved in both an upper bound
10680 * and a lower bound.
10682 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10683 enum isl_dim_type type, unsigned pos)
10685 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10688 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10690 static isl_bool has_any_bound(__isl_keep isl_map *map,
10691 enum isl_dim_type type, unsigned pos,
10692 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10693 enum isl_dim_type type, unsigned pos))
10695 int i;
10697 if (!map)
10698 return isl_bool_error;
10700 for (i = 0; i < map->n; ++i) {
10701 isl_bool bounded;
10702 bounded = fn(map->p[i], type, pos);
10703 if (bounded < 0 || bounded)
10704 return bounded;
10707 return isl_bool_false;
10710 /* Return 1 if the specified dim is involved in any lower bound.
10712 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10713 enum isl_dim_type type, unsigned pos)
10715 return has_any_bound(set, type, pos,
10716 &isl_basic_map_dim_has_lower_bound);
10719 /* Return 1 if the specified dim is involved in any upper bound.
10721 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10722 enum isl_dim_type type, unsigned pos)
10724 return has_any_bound(set, type, pos,
10725 &isl_basic_map_dim_has_upper_bound);
10728 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10730 static isl_bool has_bound(__isl_keep isl_map *map,
10731 enum isl_dim_type type, unsigned pos,
10732 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10733 enum isl_dim_type type, unsigned pos))
10735 int i;
10737 if (!map)
10738 return isl_bool_error;
10740 for (i = 0; i < map->n; ++i) {
10741 isl_bool bounded;
10742 bounded = fn(map->p[i], type, pos);
10743 if (bounded < 0 || !bounded)
10744 return bounded;
10747 return isl_bool_true;
10750 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10752 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10753 enum isl_dim_type type, unsigned pos)
10755 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10758 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10760 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10761 enum isl_dim_type type, unsigned pos)
10763 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10766 /* For each of the "n" variables starting at "first", determine
10767 * the sign of the variable and put the results in the first "n"
10768 * elements of the array "signs".
10769 * Sign
10770 * 1 means that the variable is non-negative
10771 * -1 means that the variable is non-positive
10772 * 0 means the variable attains both positive and negative values.
10774 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10775 unsigned first, unsigned n, int *signs)
10777 isl_vec *bound = NULL;
10778 struct isl_tab *tab = NULL;
10779 struct isl_tab_undo *snap;
10780 int i;
10782 if (!bset || !signs)
10783 return isl_stat_error;
10785 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10786 tab = isl_tab_from_basic_set(bset, 0);
10787 if (!bound || !tab)
10788 goto error;
10790 isl_seq_clr(bound->el, bound->size);
10791 isl_int_set_si(bound->el[0], -1);
10793 snap = isl_tab_snap(tab);
10794 for (i = 0; i < n; ++i) {
10795 int empty;
10797 isl_int_set_si(bound->el[1 + first + i], -1);
10798 if (isl_tab_add_ineq(tab, bound->el) < 0)
10799 goto error;
10800 empty = tab->empty;
10801 isl_int_set_si(bound->el[1 + first + i], 0);
10802 if (isl_tab_rollback(tab, snap) < 0)
10803 goto error;
10805 if (empty) {
10806 signs[i] = 1;
10807 continue;
10810 isl_int_set_si(bound->el[1 + first + i], 1);
10811 if (isl_tab_add_ineq(tab, bound->el) < 0)
10812 goto error;
10813 empty = tab->empty;
10814 isl_int_set_si(bound->el[1 + first + i], 0);
10815 if (isl_tab_rollback(tab, snap) < 0)
10816 goto error;
10818 signs[i] = empty ? -1 : 0;
10821 isl_tab_free(tab);
10822 isl_vec_free(bound);
10823 return isl_stat_ok;
10824 error:
10825 isl_tab_free(tab);
10826 isl_vec_free(bound);
10827 return isl_stat_error;
10830 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10831 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10833 if (!bset || !signs)
10834 return isl_stat_error;
10835 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10836 return isl_stat_error);
10838 first += pos(bset->dim, type) - 1;
10839 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10842 /* Is it possible for the integer division "div" to depend (possibly
10843 * indirectly) on any output dimensions?
10845 * If the div is undefined, then we conservatively assume that it
10846 * may depend on them.
10847 * Otherwise, we check if it actually depends on them or on any integer
10848 * divisions that may depend on them.
10850 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10852 int i;
10853 unsigned n_out, o_out;
10854 unsigned n_div, o_div;
10856 if (isl_int_is_zero(bmap->div[div][0]))
10857 return isl_bool_true;
10859 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10860 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10862 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10863 return isl_bool_true;
10865 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10866 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10868 for (i = 0; i < n_div; ++i) {
10869 isl_bool may_involve;
10871 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10872 continue;
10873 may_involve = div_may_involve_output(bmap, i);
10874 if (may_involve < 0 || may_involve)
10875 return may_involve;
10878 return isl_bool_false;
10881 /* Return the first integer division of "bmap" in the range
10882 * [first, first + n[ that may depend on any output dimensions and
10883 * that has a non-zero coefficient in "c" (where the first coefficient
10884 * in "c" corresponds to integer division "first").
10886 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10887 isl_int *c, int first, int n)
10889 int k;
10891 if (!bmap)
10892 return -1;
10894 for (k = first; k < first + n; ++k) {
10895 isl_bool may_involve;
10897 if (isl_int_is_zero(c[k]))
10898 continue;
10899 may_involve = div_may_involve_output(bmap, k);
10900 if (may_involve < 0)
10901 return -1;
10902 if (may_involve)
10903 return k;
10906 return first + n;
10909 /* Look for a pair of inequality constraints in "bmap" of the form
10911 * -l + i >= 0 or i >= l
10912 * and
10913 * n + l - i >= 0 or i <= l + n
10915 * with n < "m" and i the output dimension at position "pos".
10916 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10917 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10918 * and earlier output dimensions, as well as integer divisions that do
10919 * not involve any of the output dimensions.
10921 * Return the index of the first inequality constraint or bmap->n_ineq
10922 * if no such pair can be found.
10924 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10925 int pos, isl_int m)
10927 int i, j;
10928 isl_ctx *ctx;
10929 unsigned total;
10930 unsigned n_div, o_div;
10931 unsigned n_out, o_out;
10932 int less;
10934 if (!bmap)
10935 return -1;
10937 ctx = isl_basic_map_get_ctx(bmap);
10938 total = isl_basic_map_total_dim(bmap);
10939 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10940 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10941 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10942 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10943 for (i = 0; i < bmap->n_ineq; ++i) {
10944 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10945 continue;
10946 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10947 n_out - (pos + 1)) != -1)
10948 continue;
10949 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10950 0, n_div) < n_div)
10951 continue;
10952 for (j = i + 1; j < bmap->n_ineq; ++j) {
10953 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10954 ctx->one))
10955 continue;
10956 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10957 bmap->ineq[j] + 1, total))
10958 continue;
10959 break;
10961 if (j >= bmap->n_ineq)
10962 continue;
10963 isl_int_add(bmap->ineq[i][0],
10964 bmap->ineq[i][0], bmap->ineq[j][0]);
10965 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10966 isl_int_sub(bmap->ineq[i][0],
10967 bmap->ineq[i][0], bmap->ineq[j][0]);
10968 if (!less)
10969 continue;
10970 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10971 return i;
10972 else
10973 return j;
10976 return bmap->n_ineq;
10979 /* Return the index of the equality of "bmap" that defines
10980 * the output dimension "pos" in terms of earlier dimensions.
10981 * The equality may also involve integer divisions, as long
10982 * as those integer divisions are defined in terms of
10983 * parameters or input dimensions.
10984 * In this case, *div is set to the number of integer divisions and
10985 * *ineq is set to the number of inequality constraints (provided
10986 * div and ineq are not NULL).
10988 * The equality may also involve a single integer division involving
10989 * the output dimensions (typically only output dimension "pos") as
10990 * long as the coefficient of output dimension "pos" is 1 or -1 and
10991 * there is a pair of constraints i >= l and i <= l + n, with i referring
10992 * to output dimension "pos", l an expression involving only earlier
10993 * dimensions and n smaller than the coefficient of the integer division
10994 * in the equality. In this case, the output dimension can be defined
10995 * in terms of a modulo expression that does not involve the integer division.
10996 * *div is then set to this single integer division and
10997 * *ineq is set to the index of constraint i >= l.
10999 * Return bmap->n_eq if there is no such equality.
11000 * Return -1 on error.
11002 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11003 int pos, int *div, int *ineq)
11005 int j, k, l;
11006 unsigned n_out, o_out;
11007 unsigned n_div, o_div;
11009 if (!bmap)
11010 return -1;
11012 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11013 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11014 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11015 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11017 if (ineq)
11018 *ineq = bmap->n_ineq;
11019 if (div)
11020 *div = n_div;
11021 for (j = 0; j < bmap->n_eq; ++j) {
11022 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11023 continue;
11024 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11025 n_out - (pos + 1)) != -1)
11026 continue;
11027 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11028 0, n_div);
11029 if (k >= n_div)
11030 return j;
11031 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11032 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11033 continue;
11034 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11035 k + 1, n_div - (k+1)) < n_div)
11036 continue;
11037 l = find_modulo_constraint_pair(bmap, pos,
11038 bmap->eq[j][o_div + k]);
11039 if (l < 0)
11040 return -1;
11041 if (l >= bmap->n_ineq)
11042 continue;
11043 if (div)
11044 *div = k;
11045 if (ineq)
11046 *ineq = l;
11047 return j;
11050 return bmap->n_eq;
11053 /* Check if the given basic map is obviously single-valued.
11054 * In particular, for each output dimension, check that there is
11055 * an equality that defines the output dimension in terms of
11056 * earlier dimensions.
11058 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11060 int i;
11061 unsigned n_out;
11063 if (!bmap)
11064 return isl_bool_error;
11066 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11068 for (i = 0; i < n_out; ++i) {
11069 int eq;
11071 eq = isl_basic_map_output_defining_equality(bmap, i,
11072 NULL, NULL);
11073 if (eq < 0)
11074 return isl_bool_error;
11075 if (eq >= bmap->n_eq)
11076 return isl_bool_false;
11079 return isl_bool_true;
11082 /* Check if the given basic map is single-valued.
11083 * We simply compute
11085 * M \circ M^-1
11087 * and check if the result is a subset of the identity mapping.
11089 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11091 isl_space *space;
11092 isl_basic_map *test;
11093 isl_basic_map *id;
11094 isl_bool sv;
11096 sv = isl_basic_map_plain_is_single_valued(bmap);
11097 if (sv < 0 || sv)
11098 return sv;
11100 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11101 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11103 space = isl_basic_map_get_space(bmap);
11104 space = isl_space_map_from_set(isl_space_range(space));
11105 id = isl_basic_map_identity(space);
11107 sv = isl_basic_map_is_subset(test, id);
11109 isl_basic_map_free(test);
11110 isl_basic_map_free(id);
11112 return sv;
11115 /* Check if the given map is obviously single-valued.
11117 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11119 if (!map)
11120 return isl_bool_error;
11121 if (map->n == 0)
11122 return isl_bool_true;
11123 if (map->n >= 2)
11124 return isl_bool_false;
11126 return isl_basic_map_plain_is_single_valued(map->p[0]);
11129 /* Check if the given map is single-valued.
11130 * We simply compute
11132 * M \circ M^-1
11134 * and check if the result is a subset of the identity mapping.
11136 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11138 isl_space *dim;
11139 isl_map *test;
11140 isl_map *id;
11141 isl_bool sv;
11143 sv = isl_map_plain_is_single_valued(map);
11144 if (sv < 0 || sv)
11145 return sv;
11147 test = isl_map_reverse(isl_map_copy(map));
11148 test = isl_map_apply_range(test, isl_map_copy(map));
11150 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11151 id = isl_map_identity(dim);
11153 sv = isl_map_is_subset(test, id);
11155 isl_map_free(test);
11156 isl_map_free(id);
11158 return sv;
11161 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11163 isl_bool in;
11165 map = isl_map_copy(map);
11166 map = isl_map_reverse(map);
11167 in = isl_map_is_single_valued(map);
11168 isl_map_free(map);
11170 return in;
11173 /* Check if the given map is obviously injective.
11175 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11177 isl_bool in;
11179 map = isl_map_copy(map);
11180 map = isl_map_reverse(map);
11181 in = isl_map_plain_is_single_valued(map);
11182 isl_map_free(map);
11184 return in;
11187 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11189 isl_bool sv;
11191 sv = isl_map_is_single_valued(map);
11192 if (sv < 0 || !sv)
11193 return sv;
11195 return isl_map_is_injective(map);
11198 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11200 return isl_map_is_single_valued(set_to_map(set));
11203 /* Does "map" only map elements to themselves?
11205 * If the domain and range spaces are different, then "map"
11206 * is considered not to be an identity relation, even if it is empty.
11207 * Otherwise, construct the maximal identity relation and
11208 * check whether "map" is a subset of this relation.
11210 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11212 isl_space *space;
11213 isl_map *id;
11214 isl_bool equal, is_identity;
11216 space = isl_map_get_space(map);
11217 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11218 isl_space_free(space);
11219 if (equal < 0 || !equal)
11220 return equal;
11222 id = isl_map_identity(isl_map_get_space(map));
11223 is_identity = isl_map_is_subset(map, id);
11224 isl_map_free(id);
11226 return is_identity;
11229 int isl_map_is_translation(__isl_keep isl_map *map)
11231 int ok;
11232 isl_set *delta;
11234 delta = isl_map_deltas(isl_map_copy(map));
11235 ok = isl_set_is_singleton(delta);
11236 isl_set_free(delta);
11238 return ok;
11241 static int unique(isl_int *p, unsigned pos, unsigned len)
11243 if (isl_seq_first_non_zero(p, pos) != -1)
11244 return 0;
11245 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11246 return 0;
11247 return 1;
11250 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11252 int i, j;
11253 unsigned nvar;
11254 unsigned ovar;
11256 if (!bset)
11257 return isl_bool_error;
11259 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11260 return isl_bool_false;
11262 nvar = isl_basic_set_dim(bset, isl_dim_set);
11263 ovar = isl_space_offset(bset->dim, isl_dim_set);
11264 for (j = 0; j < nvar; ++j) {
11265 int lower = 0, upper = 0;
11266 for (i = 0; i < bset->n_eq; ++i) {
11267 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11268 continue;
11269 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11270 return isl_bool_false;
11271 break;
11273 if (i < bset->n_eq)
11274 continue;
11275 for (i = 0; i < bset->n_ineq; ++i) {
11276 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11277 continue;
11278 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11279 return isl_bool_false;
11280 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11281 lower = 1;
11282 else
11283 upper = 1;
11285 if (!lower || !upper)
11286 return isl_bool_false;
11289 return isl_bool_true;
11292 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11294 if (!set)
11295 return isl_bool_error;
11296 if (set->n != 1)
11297 return isl_bool_false;
11299 return isl_basic_set_is_box(set->p[0]);
11302 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11304 if (!bset)
11305 return isl_bool_error;
11307 return isl_space_is_wrapping(bset->dim);
11310 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11312 if (!set)
11313 return isl_bool_error;
11315 return isl_space_is_wrapping(set->dim);
11318 /* Modify the space of "map" through a call to "change".
11319 * If "can_change" is set (not NULL), then first call it to check
11320 * if the modification is allowed, printing the error message "cannot_change"
11321 * if it is not.
11323 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11324 isl_bool (*can_change)(__isl_keep isl_map *map),
11325 const char *cannot_change,
11326 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11328 isl_bool ok;
11329 isl_space *space;
11331 if (!map)
11332 return NULL;
11334 ok = can_change ? can_change(map) : isl_bool_true;
11335 if (ok < 0)
11336 return isl_map_free(map);
11337 if (!ok)
11338 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11339 return isl_map_free(map));
11341 space = change(isl_map_get_space(map));
11342 map = isl_map_reset_space(map, space);
11344 return map;
11347 /* Is the domain of "map" a wrapped relation?
11349 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11351 if (!map)
11352 return isl_bool_error;
11354 return isl_space_domain_is_wrapping(map->dim);
11357 /* Is the range of "map" a wrapped relation?
11359 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11361 if (!map)
11362 return isl_bool_error;
11364 return isl_space_range_is_wrapping(map->dim);
11367 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11369 bmap = isl_basic_map_cow(bmap);
11370 if (!bmap)
11371 return NULL;
11373 bmap->dim = isl_space_wrap(bmap->dim);
11374 if (!bmap->dim)
11375 goto error;
11377 bmap = isl_basic_map_finalize(bmap);
11379 return bset_from_bmap(bmap);
11380 error:
11381 isl_basic_map_free(bmap);
11382 return NULL;
11385 /* Given a map A -> B, return the set (A -> B).
11387 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11389 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11392 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11394 bset = isl_basic_set_cow(bset);
11395 if (!bset)
11396 return NULL;
11398 bset->dim = isl_space_unwrap(bset->dim);
11399 if (!bset->dim)
11400 goto error;
11402 bset = isl_basic_set_finalize(bset);
11404 return bset_to_bmap(bset);
11405 error:
11406 isl_basic_set_free(bset);
11407 return NULL;
11410 /* Given a set (A -> B), return the map A -> B.
11411 * Error out if "set" is not of the form (A -> B).
11413 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11415 return isl_map_change_space(set, &isl_set_is_wrapping,
11416 "not a wrapping set", &isl_space_unwrap);
11419 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11420 enum isl_dim_type type)
11422 if (!bmap)
11423 return NULL;
11425 if (!isl_space_is_named_or_nested(bmap->dim, type))
11426 return bmap;
11428 bmap = isl_basic_map_cow(bmap);
11429 if (!bmap)
11430 return NULL;
11432 bmap->dim = isl_space_reset(bmap->dim, type);
11433 if (!bmap->dim)
11434 goto error;
11436 bmap = isl_basic_map_finalize(bmap);
11438 return bmap;
11439 error:
11440 isl_basic_map_free(bmap);
11441 return NULL;
11444 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11445 enum isl_dim_type type)
11447 int i;
11449 if (!map)
11450 return NULL;
11452 if (!isl_space_is_named_or_nested(map->dim, type))
11453 return map;
11455 map = isl_map_cow(map);
11456 if (!map)
11457 return NULL;
11459 for (i = 0; i < map->n; ++i) {
11460 map->p[i] = isl_basic_map_reset(map->p[i], type);
11461 if (!map->p[i])
11462 goto error;
11464 map->dim = isl_space_reset(map->dim, type);
11465 if (!map->dim)
11466 goto error;
11468 return map;
11469 error:
11470 isl_map_free(map);
11471 return NULL;
11474 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11476 if (!bmap)
11477 return NULL;
11479 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11480 return bmap;
11482 bmap = isl_basic_map_cow(bmap);
11483 if (!bmap)
11484 return NULL;
11486 bmap->dim = isl_space_flatten(bmap->dim);
11487 if (!bmap->dim)
11488 goto error;
11490 bmap = isl_basic_map_finalize(bmap);
11492 return bmap;
11493 error:
11494 isl_basic_map_free(bmap);
11495 return NULL;
11498 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11500 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11503 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11504 __isl_take isl_basic_map *bmap)
11506 if (!bmap)
11507 return NULL;
11509 if (!bmap->dim->nested[0])
11510 return bmap;
11512 bmap = isl_basic_map_cow(bmap);
11513 if (!bmap)
11514 return NULL;
11516 bmap->dim = isl_space_flatten_domain(bmap->dim);
11517 if (!bmap->dim)
11518 goto error;
11520 bmap = isl_basic_map_finalize(bmap);
11522 return bmap;
11523 error:
11524 isl_basic_map_free(bmap);
11525 return NULL;
11528 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11529 __isl_take isl_basic_map *bmap)
11531 if (!bmap)
11532 return NULL;
11534 if (!bmap->dim->nested[1])
11535 return bmap;
11537 bmap = isl_basic_map_cow(bmap);
11538 if (!bmap)
11539 return NULL;
11541 bmap->dim = isl_space_flatten_range(bmap->dim);
11542 if (!bmap->dim)
11543 goto error;
11545 bmap = isl_basic_map_finalize(bmap);
11547 return bmap;
11548 error:
11549 isl_basic_map_free(bmap);
11550 return NULL;
11553 /* Remove any internal structure from the spaces of domain and range of "map".
11555 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11557 if (!map)
11558 return NULL;
11560 if (!map->dim->nested[0] && !map->dim->nested[1])
11561 return map;
11563 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11566 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11568 return set_from_map(isl_map_flatten(set_to_map(set)));
11571 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11573 isl_space *dim, *flat_dim;
11574 isl_map *map;
11576 dim = isl_set_get_space(set);
11577 flat_dim = isl_space_flatten(isl_space_copy(dim));
11578 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11579 map = isl_map_intersect_domain(map, set);
11581 return map;
11584 /* Remove any internal structure from the space of the domain of "map".
11586 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11588 if (!map)
11589 return NULL;
11591 if (!map->dim->nested[0])
11592 return map;
11594 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11597 /* Remove any internal structure from the space of the range of "map".
11599 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11601 if (!map)
11602 return NULL;
11604 if (!map->dim->nested[1])
11605 return map;
11607 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11610 /* Reorder the dimensions of "bmap" according to the given dim_map
11611 * and set the dimension specification to "dim" and
11612 * perform Gaussian elimination on the result.
11614 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11615 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11617 isl_basic_map *res;
11618 unsigned flags;
11620 bmap = isl_basic_map_cow(bmap);
11621 if (!bmap || !dim || !dim_map)
11622 goto error;
11624 flags = bmap->flags;
11625 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11626 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11627 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11628 res = isl_basic_map_alloc_space(dim,
11629 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11630 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11631 if (res)
11632 res->flags = flags;
11633 res = isl_basic_map_gauss(res, NULL);
11634 res = isl_basic_map_finalize(res);
11635 return res;
11636 error:
11637 free(dim_map);
11638 isl_basic_map_free(bmap);
11639 isl_space_free(dim);
11640 return NULL;
11643 /* Reorder the dimensions of "map" according to given reordering.
11645 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11646 __isl_take isl_reordering *r)
11648 int i;
11649 struct isl_dim_map *dim_map;
11651 map = isl_map_cow(map);
11652 dim_map = isl_dim_map_from_reordering(r);
11653 if (!map || !r || !dim_map)
11654 goto error;
11656 for (i = 0; i < map->n; ++i) {
11657 struct isl_dim_map *dim_map_i;
11659 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11661 map->p[i] = isl_basic_map_realign(map->p[i],
11662 isl_space_copy(r->dim), dim_map_i);
11664 if (!map->p[i])
11665 goto error;
11668 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11670 isl_reordering_free(r);
11671 free(dim_map);
11672 return map;
11673 error:
11674 free(dim_map);
11675 isl_map_free(map);
11676 isl_reordering_free(r);
11677 return NULL;
11680 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11681 __isl_take isl_reordering *r)
11683 return set_from_map(isl_map_realign(set_to_map(set), r));
11686 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11687 __isl_take isl_space *model)
11689 isl_ctx *ctx;
11691 if (!map || !model)
11692 goto error;
11694 ctx = isl_space_get_ctx(model);
11695 if (!isl_space_has_named_params(model))
11696 isl_die(ctx, isl_error_invalid,
11697 "model has unnamed parameters", goto error);
11698 if (!isl_space_has_named_params(map->dim))
11699 isl_die(ctx, isl_error_invalid,
11700 "relation has unnamed parameters", goto error);
11701 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11702 isl_reordering *exp;
11704 model = isl_space_drop_dims(model, isl_dim_in,
11705 0, isl_space_dim(model, isl_dim_in));
11706 model = isl_space_drop_dims(model, isl_dim_out,
11707 0, isl_space_dim(model, isl_dim_out));
11708 exp = isl_parameter_alignment_reordering(map->dim, model);
11709 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11710 map = isl_map_realign(map, exp);
11713 isl_space_free(model);
11714 return map;
11715 error:
11716 isl_space_free(model);
11717 isl_map_free(map);
11718 return NULL;
11721 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11722 __isl_take isl_space *model)
11724 return isl_map_align_params(set, model);
11727 /* Align the parameters of "bmap" to those of "model", introducing
11728 * additional parameters if needed.
11730 __isl_give isl_basic_map *isl_basic_map_align_params(
11731 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11733 isl_ctx *ctx;
11735 if (!bmap || !model)
11736 goto error;
11738 ctx = isl_space_get_ctx(model);
11739 if (!isl_space_has_named_params(model))
11740 isl_die(ctx, isl_error_invalid,
11741 "model has unnamed parameters", goto error);
11742 if (!isl_space_has_named_params(bmap->dim))
11743 isl_die(ctx, isl_error_invalid,
11744 "relation has unnamed parameters", goto error);
11745 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11746 isl_reordering *exp;
11747 struct isl_dim_map *dim_map;
11749 model = isl_space_drop_dims(model, isl_dim_in,
11750 0, isl_space_dim(model, isl_dim_in));
11751 model = isl_space_drop_dims(model, isl_dim_out,
11752 0, isl_space_dim(model, isl_dim_out));
11753 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11754 exp = isl_reordering_extend_space(exp,
11755 isl_basic_map_get_space(bmap));
11756 dim_map = isl_dim_map_from_reordering(exp);
11757 bmap = isl_basic_map_realign(bmap,
11758 exp ? isl_space_copy(exp->dim) : NULL,
11759 isl_dim_map_extend(dim_map, bmap));
11760 isl_reordering_free(exp);
11761 free(dim_map);
11764 isl_space_free(model);
11765 return bmap;
11766 error:
11767 isl_space_free(model);
11768 isl_basic_map_free(bmap);
11769 return NULL;
11772 /* Align the parameters of "bset" to those of "model", introducing
11773 * additional parameters if needed.
11775 __isl_give isl_basic_set *isl_basic_set_align_params(
11776 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11778 return isl_basic_map_align_params(bset, model);
11781 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11782 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11783 enum isl_dim_type c2, enum isl_dim_type c3,
11784 enum isl_dim_type c4, enum isl_dim_type c5)
11786 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11787 struct isl_mat *mat;
11788 int i, j, k;
11789 int pos;
11791 if (!bmap)
11792 return NULL;
11793 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11794 isl_basic_map_total_dim(bmap) + 1);
11795 if (!mat)
11796 return NULL;
11797 for (i = 0; i < bmap->n_eq; ++i)
11798 for (j = 0, pos = 0; j < 5; ++j) {
11799 int off = isl_basic_map_offset(bmap, c[j]);
11800 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11801 isl_int_set(mat->row[i][pos],
11802 bmap->eq[i][off + k]);
11803 ++pos;
11807 return mat;
11810 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11811 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11812 enum isl_dim_type c2, enum isl_dim_type c3,
11813 enum isl_dim_type c4, enum isl_dim_type c5)
11815 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11816 struct isl_mat *mat;
11817 int i, j, k;
11818 int pos;
11820 if (!bmap)
11821 return NULL;
11822 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11823 isl_basic_map_total_dim(bmap) + 1);
11824 if (!mat)
11825 return NULL;
11826 for (i = 0; i < bmap->n_ineq; ++i)
11827 for (j = 0, pos = 0; j < 5; ++j) {
11828 int off = isl_basic_map_offset(bmap, c[j]);
11829 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11830 isl_int_set(mat->row[i][pos],
11831 bmap->ineq[i][off + k]);
11832 ++pos;
11836 return mat;
11839 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11840 __isl_take isl_space *dim,
11841 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11842 enum isl_dim_type c2, enum isl_dim_type c3,
11843 enum isl_dim_type c4, enum isl_dim_type c5)
11845 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11846 isl_basic_map *bmap;
11847 unsigned total;
11848 unsigned extra;
11849 int i, j, k, l;
11850 int pos;
11852 if (!dim || !eq || !ineq)
11853 goto error;
11855 if (eq->n_col != ineq->n_col)
11856 isl_die(dim->ctx, isl_error_invalid,
11857 "equalities and inequalities matrices should have "
11858 "same number of columns", goto error);
11860 total = 1 + isl_space_dim(dim, isl_dim_all);
11862 if (eq->n_col < total)
11863 isl_die(dim->ctx, isl_error_invalid,
11864 "number of columns too small", goto error);
11866 extra = eq->n_col - total;
11868 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11869 eq->n_row, ineq->n_row);
11870 if (!bmap)
11871 goto error;
11872 for (i = 0; i < extra; ++i) {
11873 k = isl_basic_map_alloc_div(bmap);
11874 if (k < 0)
11875 goto error;
11876 isl_int_set_si(bmap->div[k][0], 0);
11878 for (i = 0; i < eq->n_row; ++i) {
11879 l = isl_basic_map_alloc_equality(bmap);
11880 if (l < 0)
11881 goto error;
11882 for (j = 0, pos = 0; j < 5; ++j) {
11883 int off = isl_basic_map_offset(bmap, c[j]);
11884 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11885 isl_int_set(bmap->eq[l][off + k],
11886 eq->row[i][pos]);
11887 ++pos;
11891 for (i = 0; i < ineq->n_row; ++i) {
11892 l = isl_basic_map_alloc_inequality(bmap);
11893 if (l < 0)
11894 goto error;
11895 for (j = 0, pos = 0; j < 5; ++j) {
11896 int off = isl_basic_map_offset(bmap, c[j]);
11897 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11898 isl_int_set(bmap->ineq[l][off + k],
11899 ineq->row[i][pos]);
11900 ++pos;
11905 isl_space_free(dim);
11906 isl_mat_free(eq);
11907 isl_mat_free(ineq);
11909 bmap = isl_basic_map_simplify(bmap);
11910 return isl_basic_map_finalize(bmap);
11911 error:
11912 isl_space_free(dim);
11913 isl_mat_free(eq);
11914 isl_mat_free(ineq);
11915 return NULL;
11918 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11919 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11920 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11922 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11923 c1, c2, c3, c4, isl_dim_in);
11926 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11927 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11928 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11930 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11931 c1, c2, c3, c4, isl_dim_in);
11934 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11935 __isl_take isl_space *dim,
11936 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11937 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11939 isl_basic_map *bmap;
11940 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11941 c1, c2, c3, c4, isl_dim_in);
11942 return bset_from_bmap(bmap);
11945 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11947 if (!bmap)
11948 return isl_bool_error;
11950 return isl_space_can_zip(bmap->dim);
11953 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11955 if (!map)
11956 return isl_bool_error;
11958 return isl_space_can_zip(map->dim);
11961 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11962 * (A -> C) -> (B -> D).
11964 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11966 unsigned pos;
11967 unsigned n1;
11968 unsigned n2;
11970 if (!bmap)
11971 return NULL;
11973 if (!isl_basic_map_can_zip(bmap))
11974 isl_die(bmap->ctx, isl_error_invalid,
11975 "basic map cannot be zipped", goto error);
11976 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11977 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11978 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11979 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11980 bmap = isl_basic_map_cow(bmap);
11981 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11982 if (!bmap)
11983 return NULL;
11984 bmap->dim = isl_space_zip(bmap->dim);
11985 if (!bmap->dim)
11986 goto error;
11987 bmap = isl_basic_map_mark_final(bmap);
11988 return bmap;
11989 error:
11990 isl_basic_map_free(bmap);
11991 return NULL;
11994 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11995 * (A -> C) -> (B -> D).
11997 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11999 int i;
12001 if (!map)
12002 return NULL;
12004 if (!isl_map_can_zip(map))
12005 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12006 goto error);
12008 map = isl_map_cow(map);
12009 if (!map)
12010 return NULL;
12012 for (i = 0; i < map->n; ++i) {
12013 map->p[i] = isl_basic_map_zip(map->p[i]);
12014 if (!map->p[i])
12015 goto error;
12018 map->dim = isl_space_zip(map->dim);
12019 if (!map->dim)
12020 goto error;
12022 return map;
12023 error:
12024 isl_map_free(map);
12025 return NULL;
12028 /* Can we apply isl_basic_map_curry to "bmap"?
12029 * That is, does it have a nested relation in its domain?
12031 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12033 if (!bmap)
12034 return isl_bool_error;
12036 return isl_space_can_curry(bmap->dim);
12039 /* Can we apply isl_map_curry to "map"?
12040 * That is, does it have a nested relation in its domain?
12042 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12044 if (!map)
12045 return isl_bool_error;
12047 return isl_space_can_curry(map->dim);
12050 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12051 * A -> (B -> C).
12053 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12056 if (!bmap)
12057 return NULL;
12059 if (!isl_basic_map_can_curry(bmap))
12060 isl_die(bmap->ctx, isl_error_invalid,
12061 "basic map cannot be curried", goto error);
12062 bmap = isl_basic_map_cow(bmap);
12063 if (!bmap)
12064 return NULL;
12065 bmap->dim = isl_space_curry(bmap->dim);
12066 if (!bmap->dim)
12067 goto error;
12068 bmap = isl_basic_map_mark_final(bmap);
12069 return bmap;
12070 error:
12071 isl_basic_map_free(bmap);
12072 return NULL;
12075 /* Given a map (A -> B) -> C, return the corresponding map
12076 * A -> (B -> C).
12078 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12080 return isl_map_change_space(map, &isl_map_can_curry,
12081 "map cannot be curried", &isl_space_curry);
12084 /* Can isl_map_range_curry be applied to "map"?
12085 * That is, does it have a nested relation in its range,
12086 * the domain of which is itself a nested relation?
12088 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12090 if (!map)
12091 return isl_bool_error;
12093 return isl_space_can_range_curry(map->dim);
12096 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12097 * A -> (B -> (C -> D)).
12099 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12101 return isl_map_change_space(map, &isl_map_can_range_curry,
12102 "map range cannot be curried",
12103 &isl_space_range_curry);
12106 /* Can we apply isl_basic_map_uncurry to "bmap"?
12107 * That is, does it have a nested relation in its domain?
12109 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12111 if (!bmap)
12112 return isl_bool_error;
12114 return isl_space_can_uncurry(bmap->dim);
12117 /* Can we apply isl_map_uncurry to "map"?
12118 * That is, does it have a nested relation in its domain?
12120 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12122 if (!map)
12123 return isl_bool_error;
12125 return isl_space_can_uncurry(map->dim);
12128 /* Given a basic map A -> (B -> C), return the corresponding basic map
12129 * (A -> B) -> C.
12131 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12134 if (!bmap)
12135 return NULL;
12137 if (!isl_basic_map_can_uncurry(bmap))
12138 isl_die(bmap->ctx, isl_error_invalid,
12139 "basic map cannot be uncurried",
12140 return isl_basic_map_free(bmap));
12141 bmap = isl_basic_map_cow(bmap);
12142 if (!bmap)
12143 return NULL;
12144 bmap->dim = isl_space_uncurry(bmap->dim);
12145 if (!bmap->dim)
12146 return isl_basic_map_free(bmap);
12147 bmap = isl_basic_map_mark_final(bmap);
12148 return bmap;
12151 /* Given a map A -> (B -> C), return the corresponding map
12152 * (A -> B) -> C.
12154 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12156 return isl_map_change_space(map, &isl_map_can_uncurry,
12157 "map cannot be uncurried", &isl_space_uncurry);
12160 /* Construct a basic map mapping the domain of the affine expression
12161 * to a one-dimensional range prescribed by the affine expression.
12162 * If "rational" is set, then construct a rational basic map.
12164 * A NaN affine expression cannot be converted to a basic map.
12166 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12167 __isl_take isl_aff *aff, int rational)
12169 int k;
12170 int pos;
12171 isl_bool is_nan;
12172 isl_local_space *ls;
12173 isl_basic_map *bmap = NULL;
12175 if (!aff)
12176 return NULL;
12177 is_nan = isl_aff_is_nan(aff);
12178 if (is_nan < 0)
12179 goto error;
12180 if (is_nan)
12181 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12182 "cannot convert NaN", goto error);
12184 ls = isl_aff_get_local_space(aff);
12185 bmap = isl_basic_map_from_local_space(ls);
12186 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12187 k = isl_basic_map_alloc_equality(bmap);
12188 if (k < 0)
12189 goto error;
12191 pos = isl_basic_map_offset(bmap, isl_dim_out);
12192 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12193 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12194 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12195 aff->v->size - (pos + 1));
12197 isl_aff_free(aff);
12198 if (rational)
12199 bmap = isl_basic_map_set_rational(bmap);
12200 bmap = isl_basic_map_finalize(bmap);
12201 return bmap;
12202 error:
12203 isl_aff_free(aff);
12204 isl_basic_map_free(bmap);
12205 return NULL;
12208 /* Construct a basic map mapping the domain of the affine expression
12209 * to a one-dimensional range prescribed by the affine expression.
12211 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12213 return isl_basic_map_from_aff2(aff, 0);
12216 /* Construct a map mapping the domain of the affine expression
12217 * to a one-dimensional range prescribed by the affine expression.
12219 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12221 isl_basic_map *bmap;
12223 bmap = isl_basic_map_from_aff(aff);
12224 return isl_map_from_basic_map(bmap);
12227 /* Construct a basic map mapping the domain the multi-affine expression
12228 * to its range, with each dimension in the range equated to the
12229 * corresponding affine expression.
12230 * If "rational" is set, then construct a rational basic map.
12232 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12233 __isl_take isl_multi_aff *maff, int rational)
12235 int i;
12236 isl_space *space;
12237 isl_basic_map *bmap;
12239 if (!maff)
12240 return NULL;
12242 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12243 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12244 "invalid space", goto error);
12246 space = isl_space_domain(isl_multi_aff_get_space(maff));
12247 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12248 if (rational)
12249 bmap = isl_basic_map_set_rational(bmap);
12251 for (i = 0; i < maff->n; ++i) {
12252 isl_aff *aff;
12253 isl_basic_map *bmap_i;
12255 aff = isl_aff_copy(maff->p[i]);
12256 bmap_i = isl_basic_map_from_aff2(aff, rational);
12258 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12261 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12263 isl_multi_aff_free(maff);
12264 return bmap;
12265 error:
12266 isl_multi_aff_free(maff);
12267 return NULL;
12270 /* Construct a basic map mapping the domain the multi-affine expression
12271 * to its range, with each dimension in the range equated to the
12272 * corresponding affine expression.
12274 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12275 __isl_take isl_multi_aff *ma)
12277 return isl_basic_map_from_multi_aff2(ma, 0);
12280 /* Construct a map mapping the domain the multi-affine expression
12281 * to its range, with each dimension in the range equated to the
12282 * corresponding affine expression.
12284 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12286 isl_basic_map *bmap;
12288 bmap = isl_basic_map_from_multi_aff(maff);
12289 return isl_map_from_basic_map(bmap);
12292 /* Construct a basic map mapping a domain in the given space to
12293 * to an n-dimensional range, with n the number of elements in the list,
12294 * where each coordinate in the range is prescribed by the
12295 * corresponding affine expression.
12296 * The domains of all affine expressions in the list are assumed to match
12297 * domain_dim.
12299 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12300 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12302 int i;
12303 isl_space *dim;
12304 isl_basic_map *bmap;
12306 if (!list)
12307 return NULL;
12309 dim = isl_space_from_domain(domain_dim);
12310 bmap = isl_basic_map_universe(dim);
12312 for (i = 0; i < list->n; ++i) {
12313 isl_aff *aff;
12314 isl_basic_map *bmap_i;
12316 aff = isl_aff_copy(list->p[i]);
12317 bmap_i = isl_basic_map_from_aff(aff);
12319 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12322 isl_aff_list_free(list);
12323 return bmap;
12326 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12327 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12329 return isl_map_equate(set, type1, pos1, type2, pos2);
12332 /* Construct a basic map where the given dimensions are equal to each other.
12334 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12335 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12337 isl_basic_map *bmap = NULL;
12338 int i;
12340 if (!space)
12341 return NULL;
12343 if (pos1 >= isl_space_dim(space, type1))
12344 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12345 "index out of bounds", goto error);
12346 if (pos2 >= isl_space_dim(space, type2))
12347 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12348 "index out of bounds", goto error);
12350 if (type1 == type2 && pos1 == pos2)
12351 return isl_basic_map_universe(space);
12353 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12354 i = isl_basic_map_alloc_equality(bmap);
12355 if (i < 0)
12356 goto error;
12357 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12358 pos1 += isl_basic_map_offset(bmap, type1);
12359 pos2 += isl_basic_map_offset(bmap, type2);
12360 isl_int_set_si(bmap->eq[i][pos1], -1);
12361 isl_int_set_si(bmap->eq[i][pos2], 1);
12362 bmap = isl_basic_map_finalize(bmap);
12363 isl_space_free(space);
12364 return bmap;
12365 error:
12366 isl_space_free(space);
12367 isl_basic_map_free(bmap);
12368 return NULL;
12371 /* Add a constraint imposing that the given two dimensions are equal.
12373 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12374 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12376 isl_basic_map *eq;
12378 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12380 bmap = isl_basic_map_intersect(bmap, eq);
12382 return bmap;
12385 /* Add a constraint imposing that the given two dimensions are equal.
12387 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12388 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12390 isl_basic_map *bmap;
12392 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12394 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12396 return map;
12399 /* Add a constraint imposing that the given two dimensions have opposite values.
12401 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12402 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12404 isl_basic_map *bmap = NULL;
12405 int i;
12407 if (!map)
12408 return NULL;
12410 if (pos1 >= isl_map_dim(map, type1))
12411 isl_die(map->ctx, isl_error_invalid,
12412 "index out of bounds", goto error);
12413 if (pos2 >= isl_map_dim(map, type2))
12414 isl_die(map->ctx, isl_error_invalid,
12415 "index out of bounds", goto error);
12417 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12418 i = isl_basic_map_alloc_equality(bmap);
12419 if (i < 0)
12420 goto error;
12421 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12422 pos1 += isl_basic_map_offset(bmap, type1);
12423 pos2 += isl_basic_map_offset(bmap, type2);
12424 isl_int_set_si(bmap->eq[i][pos1], 1);
12425 isl_int_set_si(bmap->eq[i][pos2], 1);
12426 bmap = isl_basic_map_finalize(bmap);
12428 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12430 return map;
12431 error:
12432 isl_basic_map_free(bmap);
12433 isl_map_free(map);
12434 return NULL;
12437 /* Construct a constraint imposing that the value of the first dimension is
12438 * greater than or equal to that of the second.
12440 static __isl_give isl_constraint *constraint_order_ge(
12441 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12442 enum isl_dim_type type2, int pos2)
12444 isl_constraint *c;
12446 if (!space)
12447 return NULL;
12449 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12451 if (pos1 >= isl_constraint_dim(c, type1))
12452 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12453 "index out of bounds", return isl_constraint_free(c));
12454 if (pos2 >= isl_constraint_dim(c, type2))
12455 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12456 "index out of bounds", return isl_constraint_free(c));
12458 if (type1 == type2 && pos1 == pos2)
12459 return c;
12461 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12462 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12464 return c;
12467 /* Add a constraint imposing that the value of the first dimension is
12468 * greater than or equal to that of the second.
12470 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12471 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12473 isl_constraint *c;
12474 isl_space *space;
12476 if (type1 == type2 && pos1 == pos2)
12477 return bmap;
12478 space = isl_basic_map_get_space(bmap);
12479 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12480 bmap = isl_basic_map_add_constraint(bmap, c);
12482 return bmap;
12485 /* Add a constraint imposing that the value of the first dimension is
12486 * greater than or equal to that of the second.
12488 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12489 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12491 isl_constraint *c;
12492 isl_space *space;
12494 if (type1 == type2 && pos1 == pos2)
12495 return map;
12496 space = isl_map_get_space(map);
12497 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12498 map = isl_map_add_constraint(map, c);
12500 return map;
12503 /* Add a constraint imposing that the value of the first dimension is
12504 * less than or equal to that of the second.
12506 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12507 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12509 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12512 /* Construct a basic map where the value of the first dimension is
12513 * greater than that of the second.
12515 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12516 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12518 isl_basic_map *bmap = NULL;
12519 int i;
12521 if (!space)
12522 return NULL;
12524 if (pos1 >= isl_space_dim(space, type1))
12525 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12526 "index out of bounds", goto error);
12527 if (pos2 >= isl_space_dim(space, type2))
12528 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12529 "index out of bounds", goto error);
12531 if (type1 == type2 && pos1 == pos2)
12532 return isl_basic_map_empty(space);
12534 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12535 i = isl_basic_map_alloc_inequality(bmap);
12536 if (i < 0)
12537 return isl_basic_map_free(bmap);
12538 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12539 pos1 += isl_basic_map_offset(bmap, type1);
12540 pos2 += isl_basic_map_offset(bmap, type2);
12541 isl_int_set_si(bmap->ineq[i][pos1], 1);
12542 isl_int_set_si(bmap->ineq[i][pos2], -1);
12543 isl_int_set_si(bmap->ineq[i][0], -1);
12544 bmap = isl_basic_map_finalize(bmap);
12546 return bmap;
12547 error:
12548 isl_space_free(space);
12549 isl_basic_map_free(bmap);
12550 return NULL;
12553 /* Add a constraint imposing that the value of the first dimension is
12554 * greater than that of the second.
12556 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12557 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12559 isl_basic_map *gt;
12561 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12563 bmap = isl_basic_map_intersect(bmap, gt);
12565 return bmap;
12568 /* Add a constraint imposing that the value of the first dimension is
12569 * greater than that of the second.
12571 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12572 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12574 isl_basic_map *bmap;
12576 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12578 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12580 return map;
12583 /* Add a constraint imposing that the value of the first dimension is
12584 * smaller than that of the second.
12586 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12587 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12589 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12592 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12593 int pos)
12595 isl_aff *div;
12596 isl_local_space *ls;
12598 if (!bmap)
12599 return NULL;
12601 if (!isl_basic_map_divs_known(bmap))
12602 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12603 "some divs are unknown", return NULL);
12605 ls = isl_basic_map_get_local_space(bmap);
12606 div = isl_local_space_get_div(ls, pos);
12607 isl_local_space_free(ls);
12609 return div;
12612 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12613 int pos)
12615 return isl_basic_map_get_div(bset, pos);
12618 /* Plug in "subs" for dimension "type", "pos" of "bset".
12620 * Let i be the dimension to replace and let "subs" be of the form
12622 * f/d
12624 * Any integer division with a non-zero coefficient for i,
12626 * floor((a i + g)/m)
12628 * is replaced by
12630 * floor((a f + d g)/(m d))
12632 * Constraints of the form
12634 * a i + g
12636 * are replaced by
12638 * a f + d g
12640 * We currently require that "subs" is an integral expression.
12641 * Handling rational expressions may require us to add stride constraints
12642 * as we do in isl_basic_set_preimage_multi_aff.
12644 __isl_give isl_basic_set *isl_basic_set_substitute(
12645 __isl_take isl_basic_set *bset,
12646 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12648 int i;
12649 isl_int v;
12650 isl_ctx *ctx;
12652 if (bset && isl_basic_set_plain_is_empty(bset))
12653 return bset;
12655 bset = isl_basic_set_cow(bset);
12656 if (!bset || !subs)
12657 goto error;
12659 ctx = isl_basic_set_get_ctx(bset);
12660 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12661 isl_die(ctx, isl_error_invalid,
12662 "spaces don't match", goto error);
12663 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12664 isl_die(ctx, isl_error_unsupported,
12665 "cannot handle divs yet", goto error);
12666 if (!isl_int_is_one(subs->v->el[0]))
12667 isl_die(ctx, isl_error_invalid,
12668 "can only substitute integer expressions", goto error);
12670 pos += isl_basic_set_offset(bset, type);
12672 isl_int_init(v);
12674 for (i = 0; i < bset->n_eq; ++i) {
12675 if (isl_int_is_zero(bset->eq[i][pos]))
12676 continue;
12677 isl_int_set(v, bset->eq[i][pos]);
12678 isl_int_set_si(bset->eq[i][pos], 0);
12679 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12680 v, subs->v->el + 1, subs->v->size - 1);
12683 for (i = 0; i < bset->n_ineq; ++i) {
12684 if (isl_int_is_zero(bset->ineq[i][pos]))
12685 continue;
12686 isl_int_set(v, bset->ineq[i][pos]);
12687 isl_int_set_si(bset->ineq[i][pos], 0);
12688 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12689 v, subs->v->el + 1, subs->v->size - 1);
12692 for (i = 0; i < bset->n_div; ++i) {
12693 if (isl_int_is_zero(bset->div[i][1 + pos]))
12694 continue;
12695 isl_int_set(v, bset->div[i][1 + pos]);
12696 isl_int_set_si(bset->div[i][1 + pos], 0);
12697 isl_seq_combine(bset->div[i] + 1,
12698 subs->v->el[0], bset->div[i] + 1,
12699 v, subs->v->el + 1, subs->v->size - 1);
12700 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12703 isl_int_clear(v);
12705 bset = isl_basic_set_simplify(bset);
12706 return isl_basic_set_finalize(bset);
12707 error:
12708 isl_basic_set_free(bset);
12709 return NULL;
12712 /* Plug in "subs" for dimension "type", "pos" of "set".
12714 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12715 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12717 int i;
12719 if (set && isl_set_plain_is_empty(set))
12720 return set;
12722 set = isl_set_cow(set);
12723 if (!set || !subs)
12724 goto error;
12726 for (i = set->n - 1; i >= 0; --i) {
12727 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12728 if (remove_if_empty(set, i) < 0)
12729 goto error;
12732 return set;
12733 error:
12734 isl_set_free(set);
12735 return NULL;
12738 /* Check if the range of "ma" is compatible with the domain or range
12739 * (depending on "type") of "bmap".
12741 static isl_stat check_basic_map_compatible_range_multi_aff(
12742 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12743 __isl_keep isl_multi_aff *ma)
12745 isl_bool m;
12746 isl_space *ma_space;
12748 ma_space = isl_multi_aff_get_space(ma);
12750 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12751 if (m < 0)
12752 goto error;
12753 if (!m)
12754 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12755 "parameters don't match", goto error);
12756 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12757 if (m < 0)
12758 goto error;
12759 if (!m)
12760 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12761 "spaces don't match", goto error);
12763 isl_space_free(ma_space);
12764 return isl_stat_ok;
12765 error:
12766 isl_space_free(ma_space);
12767 return isl_stat_error;
12770 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12771 * coefficients before the transformed range of dimensions,
12772 * the "n_after" coefficients after the transformed range of dimensions
12773 * and the coefficients of the other divs in "bmap".
12775 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12776 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12778 int i;
12779 int n_param;
12780 int n_set;
12781 isl_local_space *ls;
12783 if (n_div == 0)
12784 return 0;
12786 ls = isl_aff_get_domain_local_space(ma->p[0]);
12787 if (!ls)
12788 return -1;
12790 n_param = isl_local_space_dim(ls, isl_dim_param);
12791 n_set = isl_local_space_dim(ls, isl_dim_set);
12792 for (i = 0; i < n_div; ++i) {
12793 int o_bmap = 0, o_ls = 0;
12795 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12796 o_bmap += 1 + 1 + n_param;
12797 o_ls += 1 + 1 + n_param;
12798 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12799 o_bmap += n_before;
12800 isl_seq_cpy(bmap->div[i] + o_bmap,
12801 ls->div->row[i] + o_ls, n_set);
12802 o_bmap += n_set;
12803 o_ls += n_set;
12804 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12805 o_bmap += n_after;
12806 isl_seq_cpy(bmap->div[i] + o_bmap,
12807 ls->div->row[i] + o_ls, n_div);
12808 o_bmap += n_div;
12809 o_ls += n_div;
12810 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12811 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12812 goto error;
12815 isl_local_space_free(ls);
12816 return 0;
12817 error:
12818 isl_local_space_free(ls);
12819 return -1;
12822 /* How many stride constraints does "ma" enforce?
12823 * That is, how many of the affine expressions have a denominator
12824 * different from one?
12826 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12828 int i;
12829 int strides = 0;
12831 for (i = 0; i < ma->n; ++i)
12832 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12833 strides++;
12835 return strides;
12838 /* For each affine expression in ma of the form
12840 * x_i = (f_i y + h_i)/m_i
12842 * with m_i different from one, add a constraint to "bmap"
12843 * of the form
12845 * f_i y + h_i = m_i alpha_i
12847 * with alpha_i an additional existentially quantified variable.
12849 * The input variables of "ma" correspond to a subset of the variables
12850 * of "bmap". There are "n_before" variables in "bmap" before this
12851 * subset and "n_after" variables after this subset.
12852 * The integer divisions of the affine expressions in "ma" are assumed
12853 * to have been aligned. There are "n_div_ma" of them and
12854 * they appear first in "bmap", straight after the "n_after" variables.
12856 static __isl_give isl_basic_map *add_ma_strides(
12857 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12858 int n_before, int n_after, int n_div_ma)
12860 int i, k;
12861 int div;
12862 int total;
12863 int n_param;
12864 int n_in;
12866 total = isl_basic_map_total_dim(bmap);
12867 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12868 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12869 for (i = 0; i < ma->n; ++i) {
12870 int o_bmap = 0, o_ma = 1;
12872 if (isl_int_is_one(ma->p[i]->v->el[0]))
12873 continue;
12874 div = isl_basic_map_alloc_div(bmap);
12875 k = isl_basic_map_alloc_equality(bmap);
12876 if (div < 0 || k < 0)
12877 goto error;
12878 isl_int_set_si(bmap->div[div][0], 0);
12879 isl_seq_cpy(bmap->eq[k] + o_bmap,
12880 ma->p[i]->v->el + o_ma, 1 + n_param);
12881 o_bmap += 1 + n_param;
12882 o_ma += 1 + n_param;
12883 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12884 o_bmap += n_before;
12885 isl_seq_cpy(bmap->eq[k] + o_bmap,
12886 ma->p[i]->v->el + o_ma, n_in);
12887 o_bmap += n_in;
12888 o_ma += n_in;
12889 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12890 o_bmap += n_after;
12891 isl_seq_cpy(bmap->eq[k] + o_bmap,
12892 ma->p[i]->v->el + o_ma, n_div_ma);
12893 o_bmap += n_div_ma;
12894 o_ma += n_div_ma;
12895 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12896 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12897 total++;
12900 return bmap;
12901 error:
12902 isl_basic_map_free(bmap);
12903 return NULL;
12906 /* Replace the domain or range space (depending on "type) of "space" by "set".
12908 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12909 enum isl_dim_type type, __isl_take isl_space *set)
12911 if (type == isl_dim_in) {
12912 space = isl_space_range(space);
12913 space = isl_space_map_from_domain_and_range(set, space);
12914 } else {
12915 space = isl_space_domain(space);
12916 space = isl_space_map_from_domain_and_range(space, set);
12919 return space;
12922 /* Compute the preimage of the domain or range (depending on "type")
12923 * of "bmap" under the function represented by "ma".
12924 * In other words, plug in "ma" in the domain or range of "bmap".
12925 * The result is a basic map that lives in the same space as "bmap"
12926 * except that the domain or range has been replaced by
12927 * the domain space of "ma".
12929 * If bmap is represented by
12931 * A(p) + S u + B x + T v + C(divs) >= 0,
12933 * where u and x are input and output dimensions if type == isl_dim_out
12934 * while x and v are input and output dimensions if type == isl_dim_in,
12935 * and ma is represented by
12937 * x = D(p) + F(y) + G(divs')
12939 * then the result is
12941 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12943 * The divs in the input set are similarly adjusted.
12944 * In particular
12946 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12948 * becomes
12950 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12951 * B_i G(divs') + c_i(divs))/n_i)
12953 * If bmap is not a rational map and if F(y) involves any denominators
12955 * x_i = (f_i y + h_i)/m_i
12957 * then additional constraints are added to ensure that we only
12958 * map back integer points. That is we enforce
12960 * f_i y + h_i = m_i alpha_i
12962 * with alpha_i an additional existentially quantified variable.
12964 * We first copy over the divs from "ma".
12965 * Then we add the modified constraints and divs from "bmap".
12966 * Finally, we add the stride constraints, if needed.
12968 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12969 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12970 __isl_take isl_multi_aff *ma)
12972 int i, k;
12973 isl_space *space;
12974 isl_basic_map *res = NULL;
12975 int n_before, n_after, n_div_bmap, n_div_ma;
12976 isl_int f, c1, c2, g;
12977 isl_bool rational;
12978 int strides;
12980 isl_int_init(f);
12981 isl_int_init(c1);
12982 isl_int_init(c2);
12983 isl_int_init(g);
12985 ma = isl_multi_aff_align_divs(ma);
12986 if (!bmap || !ma)
12987 goto error;
12988 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12989 goto error;
12991 if (type == isl_dim_in) {
12992 n_before = 0;
12993 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12994 } else {
12995 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12996 n_after = 0;
12998 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12999 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
13001 space = isl_multi_aff_get_domain_space(ma);
13002 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13003 rational = isl_basic_map_is_rational(bmap);
13004 strides = rational ? 0 : multi_aff_strides(ma);
13005 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13006 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13007 if (rational)
13008 res = isl_basic_map_set_rational(res);
13010 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13011 if (isl_basic_map_alloc_div(res) < 0)
13012 goto error;
13014 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
13015 goto error;
13017 for (i = 0; i < bmap->n_eq; ++i) {
13018 k = isl_basic_map_alloc_equality(res);
13019 if (k < 0)
13020 goto error;
13021 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13022 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13025 for (i = 0; i < bmap->n_ineq; ++i) {
13026 k = isl_basic_map_alloc_inequality(res);
13027 if (k < 0)
13028 goto error;
13029 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13030 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13033 for (i = 0; i < bmap->n_div; ++i) {
13034 if (isl_int_is_zero(bmap->div[i][0])) {
13035 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13036 continue;
13038 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13039 n_before, n_after, n_div_ma, n_div_bmap,
13040 f, c1, c2, g, 1);
13043 if (strides)
13044 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13046 isl_int_clear(f);
13047 isl_int_clear(c1);
13048 isl_int_clear(c2);
13049 isl_int_clear(g);
13050 isl_basic_map_free(bmap);
13051 isl_multi_aff_free(ma);
13052 res = isl_basic_set_simplify(res);
13053 return isl_basic_map_finalize(res);
13054 error:
13055 isl_int_clear(f);
13056 isl_int_clear(c1);
13057 isl_int_clear(c2);
13058 isl_int_clear(g);
13059 isl_basic_map_free(bmap);
13060 isl_multi_aff_free(ma);
13061 isl_basic_map_free(res);
13062 return NULL;
13065 /* Compute the preimage of "bset" under the function represented by "ma".
13066 * In other words, plug in "ma" in "bset". The result is a basic set
13067 * that lives in the domain space of "ma".
13069 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13070 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13072 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13075 /* Compute the preimage of the domain of "bmap" under the function
13076 * represented by "ma".
13077 * In other words, plug in "ma" in the domain of "bmap".
13078 * The result is a basic map that lives in the same space as "bmap"
13079 * except that the domain has been replaced by the domain space of "ma".
13081 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13082 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13084 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13087 /* Compute the preimage of the range of "bmap" under the function
13088 * represented by "ma".
13089 * In other words, plug in "ma" in the range of "bmap".
13090 * The result is a basic map that lives in the same space as "bmap"
13091 * except that the range has been replaced by the domain space of "ma".
13093 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13094 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13096 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13099 /* Check if the range of "ma" is compatible with the domain or range
13100 * (depending on "type") of "map".
13101 * Return isl_stat_error if anything is wrong.
13103 static isl_stat check_map_compatible_range_multi_aff(
13104 __isl_keep isl_map *map, enum isl_dim_type type,
13105 __isl_keep isl_multi_aff *ma)
13107 isl_bool m;
13108 isl_space *ma_space;
13110 ma_space = isl_multi_aff_get_space(ma);
13111 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13112 isl_space_free(ma_space);
13113 if (m < 0)
13114 return isl_stat_error;
13115 if (!m)
13116 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13117 "spaces don't match", return isl_stat_error);
13118 return isl_stat_ok;
13121 /* Compute the preimage of the domain or range (depending on "type")
13122 * of "map" under the function represented by "ma".
13123 * In other words, plug in "ma" in the domain or range of "map".
13124 * The result is a map that lives in the same space as "map"
13125 * except that the domain or range has been replaced by
13126 * the domain space of "ma".
13128 * The parameters are assumed to have been aligned.
13130 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13131 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13133 int i;
13134 isl_space *space;
13136 map = isl_map_cow(map);
13137 ma = isl_multi_aff_align_divs(ma);
13138 if (!map || !ma)
13139 goto error;
13140 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13141 goto error;
13143 for (i = 0; i < map->n; ++i) {
13144 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13145 isl_multi_aff_copy(ma));
13146 if (!map->p[i])
13147 goto error;
13150 space = isl_multi_aff_get_domain_space(ma);
13151 space = isl_space_set(isl_map_get_space(map), type, space);
13153 isl_space_free(map->dim);
13154 map->dim = space;
13155 if (!map->dim)
13156 goto error;
13158 isl_multi_aff_free(ma);
13159 if (map->n > 1)
13160 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13161 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13162 return map;
13163 error:
13164 isl_multi_aff_free(ma);
13165 isl_map_free(map);
13166 return NULL;
13169 /* Compute the preimage of the domain or range (depending on "type")
13170 * of "map" under the function represented by "ma".
13171 * In other words, plug in "ma" in the domain or range of "map".
13172 * The result is a map that lives in the same space as "map"
13173 * except that the domain or range has been replaced by
13174 * the domain space of "ma".
13176 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13177 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13179 if (!map || !ma)
13180 goto error;
13182 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
13183 return map_preimage_multi_aff(map, type, ma);
13185 if (!isl_space_has_named_params(map->dim) ||
13186 !isl_space_has_named_params(ma->space))
13187 isl_die(map->ctx, isl_error_invalid,
13188 "unaligned unnamed parameters", goto error);
13189 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13190 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13192 return map_preimage_multi_aff(map, type, ma);
13193 error:
13194 isl_multi_aff_free(ma);
13195 return isl_map_free(map);
13198 /* Compute the preimage of "set" under the function represented by "ma".
13199 * In other words, plug in "ma" in "set". The result is a set
13200 * that lives in the domain space of "ma".
13202 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13203 __isl_take isl_multi_aff *ma)
13205 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13208 /* Compute the preimage of the domain of "map" under the function
13209 * represented by "ma".
13210 * In other words, plug in "ma" in the domain of "map".
13211 * The result is a map that lives in the same space as "map"
13212 * except that the domain has been replaced by the domain space of "ma".
13214 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13215 __isl_take isl_multi_aff *ma)
13217 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13220 /* Compute the preimage of the range of "map" under the function
13221 * represented by "ma".
13222 * In other words, plug in "ma" in the range of "map".
13223 * The result is a map that lives in the same space as "map"
13224 * except that the range has been replaced by the domain space of "ma".
13226 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13227 __isl_take isl_multi_aff *ma)
13229 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13232 /* Compute the preimage of "map" under the function represented by "pma".
13233 * In other words, plug in "pma" in the domain or range of "map".
13234 * The result is a map that lives in the same space as "map",
13235 * except that the space of type "type" has been replaced by
13236 * the domain space of "pma".
13238 * The parameters of "map" and "pma" are assumed to have been aligned.
13240 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13241 __isl_take isl_map *map, enum isl_dim_type type,
13242 __isl_take isl_pw_multi_aff *pma)
13244 int i;
13245 isl_map *res;
13247 if (!pma)
13248 goto error;
13250 if (pma->n == 0) {
13251 isl_pw_multi_aff_free(pma);
13252 res = isl_map_empty(isl_map_get_space(map));
13253 isl_map_free(map);
13254 return res;
13257 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13258 isl_multi_aff_copy(pma->p[0].maff));
13259 if (type == isl_dim_in)
13260 res = isl_map_intersect_domain(res,
13261 isl_map_copy(pma->p[0].set));
13262 else
13263 res = isl_map_intersect_range(res,
13264 isl_map_copy(pma->p[0].set));
13266 for (i = 1; i < pma->n; ++i) {
13267 isl_map *res_i;
13269 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13270 isl_multi_aff_copy(pma->p[i].maff));
13271 if (type == isl_dim_in)
13272 res_i = isl_map_intersect_domain(res_i,
13273 isl_map_copy(pma->p[i].set));
13274 else
13275 res_i = isl_map_intersect_range(res_i,
13276 isl_map_copy(pma->p[i].set));
13277 res = isl_map_union(res, res_i);
13280 isl_pw_multi_aff_free(pma);
13281 isl_map_free(map);
13282 return res;
13283 error:
13284 isl_pw_multi_aff_free(pma);
13285 isl_map_free(map);
13286 return NULL;
13289 /* Compute the preimage of "map" under the function represented by "pma".
13290 * In other words, plug in "pma" in the domain or range of "map".
13291 * The result is a map that lives in the same space as "map",
13292 * except that the space of type "type" has been replaced by
13293 * the domain space of "pma".
13295 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13296 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13298 if (!map || !pma)
13299 goto error;
13301 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13302 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13304 if (!isl_space_has_named_params(map->dim) ||
13305 !isl_space_has_named_params(pma->dim))
13306 isl_die(map->ctx, isl_error_invalid,
13307 "unaligned unnamed parameters", goto error);
13308 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13309 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13311 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13312 error:
13313 isl_pw_multi_aff_free(pma);
13314 return isl_map_free(map);
13317 /* Compute the preimage of "set" under the function represented by "pma".
13318 * In other words, plug in "pma" in "set". The result is a set
13319 * that lives in the domain space of "pma".
13321 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13322 __isl_take isl_pw_multi_aff *pma)
13324 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13327 /* Compute the preimage of the domain of "map" under the function
13328 * represented by "pma".
13329 * In other words, plug in "pma" in the domain of "map".
13330 * The result is a map that lives in the same space as "map",
13331 * except that domain space has been replaced by the domain space of "pma".
13333 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13334 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13336 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13339 /* Compute the preimage of the range of "map" under the function
13340 * represented by "pma".
13341 * In other words, plug in "pma" in the range of "map".
13342 * The result is a map that lives in the same space as "map",
13343 * except that range space has been replaced by the domain space of "pma".
13345 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13346 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13348 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13351 /* Compute the preimage of "map" under the function represented by "mpa".
13352 * In other words, plug in "mpa" in the domain or range of "map".
13353 * The result is a map that lives in the same space as "map",
13354 * except that the space of type "type" has been replaced by
13355 * the domain space of "mpa".
13357 * If the map does not involve any constraints that refer to the
13358 * dimensions of the substituted space, then the only possible
13359 * effect of "mpa" on the map is to map the space to a different space.
13360 * We create a separate isl_multi_aff to effectuate this change
13361 * in order to avoid spurious splitting of the map along the pieces
13362 * of "mpa".
13364 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13365 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13367 int n;
13368 isl_pw_multi_aff *pma;
13370 if (!map || !mpa)
13371 goto error;
13373 n = isl_map_dim(map, type);
13374 if (!isl_map_involves_dims(map, type, 0, n)) {
13375 isl_space *space;
13376 isl_multi_aff *ma;
13378 space = isl_multi_pw_aff_get_space(mpa);
13379 isl_multi_pw_aff_free(mpa);
13380 ma = isl_multi_aff_zero(space);
13381 return isl_map_preimage_multi_aff(map, type, ma);
13384 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13385 return isl_map_preimage_pw_multi_aff(map, type, pma);
13386 error:
13387 isl_map_free(map);
13388 isl_multi_pw_aff_free(mpa);
13389 return NULL;
13392 /* Compute the preimage of "map" under the function represented by "mpa".
13393 * In other words, plug in "mpa" in the domain "map".
13394 * The result is a map that lives in the same space as "map",
13395 * except that domain space has been replaced by the domain space of "mpa".
13397 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13398 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13400 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13403 /* Compute the preimage of "set" by the function represented by "mpa".
13404 * In other words, plug in "mpa" in "set".
13406 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13407 __isl_take isl_multi_pw_aff *mpa)
13409 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13412 /* Are the "n" "coefficients" starting at "first" of the integer division
13413 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13414 * to each other?
13415 * The "coefficient" at position 0 is the denominator.
13416 * The "coefficient" at position 1 is the constant term.
13418 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13419 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13420 unsigned first, unsigned n)
13422 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13423 return isl_bool_error;
13424 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13425 return isl_bool_error;
13426 return isl_seq_eq(bmap1->div[pos1] + first,
13427 bmap2->div[pos2] + first, n);
13430 /* Are the integer division expressions at position "pos1" in "bmap1" and
13431 * "pos2" in "bmap2" equal to each other, except that the constant terms
13432 * are different?
13434 isl_bool isl_basic_map_equal_div_expr_except_constant(
13435 __isl_keep isl_basic_map *bmap1, int pos1,
13436 __isl_keep isl_basic_map *bmap2, int pos2)
13438 isl_bool equal;
13439 unsigned total;
13441 if (!bmap1 || !bmap2)
13442 return isl_bool_error;
13443 total = isl_basic_map_total_dim(bmap1);
13444 if (total != isl_basic_map_total_dim(bmap2))
13445 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13446 "incomparable div expressions", return isl_bool_error);
13447 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13448 0, 1);
13449 if (equal < 0 || !equal)
13450 return equal;
13451 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13452 1, 1);
13453 if (equal < 0 || equal)
13454 return isl_bool_not(equal);
13455 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13456 2, total);
13459 /* Replace the numerator of the constant term of the integer division
13460 * expression at position "div" in "bmap" by "value".
13461 * The caller guarantees that this does not change the meaning
13462 * of the input.
13464 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13465 __isl_take isl_basic_map *bmap, int div, int value)
13467 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13468 return isl_basic_map_free(bmap);
13470 isl_int_set_si(bmap->div[div][1], value);
13472 return bmap;
13475 /* Is the point "inner" internal to inequality constraint "ineq"
13476 * of "bset"?
13477 * The point is considered to be internal to the inequality constraint,
13478 * if it strictly lies on the positive side of the inequality constraint,
13479 * or if it lies on the constraint and the constraint is lexico-positive.
13481 static isl_bool is_internal(__isl_keep isl_vec *inner,
13482 __isl_keep isl_basic_set *bset, int ineq)
13484 isl_ctx *ctx;
13485 int pos;
13486 unsigned total;
13488 if (!inner || !bset)
13489 return isl_bool_error;
13491 ctx = isl_basic_set_get_ctx(bset);
13492 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13493 &ctx->normalize_gcd);
13494 if (!isl_int_is_zero(ctx->normalize_gcd))
13495 return isl_int_is_nonneg(ctx->normalize_gcd);
13497 total = isl_basic_set_dim(bset, isl_dim_all);
13498 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13499 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13502 /* Tighten the inequality constraints of "bset" that are outward with respect
13503 * to the point "vec".
13504 * That is, tighten the constraints that are not satisfied by "vec".
13506 * "vec" is a point internal to some superset S of "bset" that is used
13507 * to make the subsets of S disjoint, by tightening one half of the constraints
13508 * that separate two subsets. In particular, the constraints of S
13509 * are all satisfied by "vec" and should not be tightened.
13510 * Of the internal constraints, those that have "vec" on the outside
13511 * are tightened. The shared facet is included in the adjacent subset
13512 * with the opposite constraint.
13513 * For constraints that saturate "vec", this criterion cannot be used
13514 * to determine which of the two sides should be tightened.
13515 * Instead, the sign of the first non-zero coefficient is used
13516 * to make this choice. Note that this second criterion is never used
13517 * on the constraints of S since "vec" is interior to "S".
13519 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13520 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13522 int j;
13524 bset = isl_basic_set_cow(bset);
13525 if (!bset)
13526 return NULL;
13527 for (j = 0; j < bset->n_ineq; ++j) {
13528 isl_bool internal;
13530 internal = is_internal(vec, bset, j);
13531 if (internal < 0)
13532 return isl_basic_set_free(bset);
13533 if (internal)
13534 continue;
13535 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13538 return bset;