drop isl_basic_map_drop_inputs
[isl.git] / isl_map.c
blobb2f3b77ea2a54698b885088e8b6eaa9a16f9ea87
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 map1 = isl_map_cow(map1);
3300 if (!map1)
3301 goto error;
3302 if (isl_map_plain_is_empty(map1)) {
3303 isl_map_free(map2);
3304 return map1;
3306 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3307 if (map2->p[0]->n_eq == 1)
3308 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3309 else
3310 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3311 map2->p[0]->ineq[0]);
3313 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3314 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3315 if (!map1->p[0])
3316 goto error;
3318 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3319 isl_basic_map_free(map1->p[0]);
3320 map1->n = 0;
3323 isl_map_free(map2);
3325 return map1;
3326 error:
3327 isl_map_free(map1);
3328 isl_map_free(map2);
3329 return NULL;
3332 /* map2 may be either a parameter domain or a map living in the same
3333 * space as map1.
3335 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3336 __isl_take isl_map *map2)
3338 unsigned flags = 0;
3339 isl_map *result;
3340 int i, j;
3342 if (!map1 || !map2)
3343 goto error;
3345 if ((isl_map_plain_is_empty(map1) ||
3346 isl_map_plain_is_universe(map2)) &&
3347 isl_space_is_equal(map1->dim, map2->dim)) {
3348 isl_map_free(map2);
3349 return map1;
3351 if ((isl_map_plain_is_empty(map2) ||
3352 isl_map_plain_is_universe(map1)) &&
3353 isl_space_is_equal(map1->dim, map2->dim)) {
3354 isl_map_free(map1);
3355 return map2;
3358 if (map1->n == 1 && map2->n == 1 &&
3359 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3360 isl_space_is_equal(map1->dim, map2->dim) &&
3361 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3362 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3363 return map_intersect_add_constraint(map1, map2);
3365 if (isl_space_dim(map2->dim, isl_dim_all) !=
3366 isl_space_dim(map2->dim, isl_dim_param))
3367 isl_assert(map1->ctx,
3368 isl_space_is_equal(map1->dim, map2->dim), goto error);
3370 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3371 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3372 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3374 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3375 map1->n * map2->n, flags);
3376 if (!result)
3377 goto error;
3378 for (i = 0; i < map1->n; ++i)
3379 for (j = 0; j < map2->n; ++j) {
3380 struct isl_basic_map *part;
3381 part = isl_basic_map_intersect(
3382 isl_basic_map_copy(map1->p[i]),
3383 isl_basic_map_copy(map2->p[j]));
3384 if (isl_basic_map_is_empty(part) < 0)
3385 part = isl_basic_map_free(part);
3386 result = isl_map_add_basic_map(result, part);
3387 if (!result)
3388 goto error;
3390 isl_map_free(map1);
3391 isl_map_free(map2);
3392 return result;
3393 error:
3394 isl_map_free(map1);
3395 isl_map_free(map2);
3396 return NULL;
3399 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3400 __isl_take isl_map *map2)
3402 if (!map1 || !map2)
3403 goto error;
3404 if (!isl_space_is_equal(map1->dim, map2->dim))
3405 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3406 "spaces don't match", goto error);
3407 return map_intersect_internal(map1, map2);
3408 error:
3409 isl_map_free(map1);
3410 isl_map_free(map2);
3411 return NULL;
3414 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3415 __isl_take isl_map *map2)
3417 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3420 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3422 return set_from_map(isl_map_intersect(set_to_map(set1),
3423 set_to_map(set2)));
3426 /* map_intersect_internal accepts intersections
3427 * with parameter domains, so we can just call that function.
3429 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3430 __isl_take isl_set *params)
3432 return map_intersect_internal(map, params);
3435 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3436 __isl_take isl_map *map2)
3438 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3441 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3442 __isl_take isl_set *params)
3444 return isl_map_intersect_params(set, params);
3447 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3449 isl_space *space;
3450 unsigned pos, n1, n2;
3452 if (!bmap)
3453 return NULL;
3454 bmap = isl_basic_map_cow(bmap);
3455 if (!bmap)
3456 return NULL;
3457 space = isl_space_reverse(isl_space_copy(bmap->dim));
3458 pos = isl_basic_map_offset(bmap, isl_dim_in);
3459 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3460 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3461 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3462 return isl_basic_map_reset_space(bmap, space);
3465 static __isl_give isl_basic_map *basic_map_space_reset(
3466 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3468 isl_space *space;
3470 if (!bmap)
3471 return NULL;
3472 if (!isl_space_is_named_or_nested(bmap->dim, type))
3473 return bmap;
3475 space = isl_basic_map_get_space(bmap);
3476 space = isl_space_reset(space, type);
3477 bmap = isl_basic_map_reset_space(bmap, space);
3478 return bmap;
3481 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3482 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3483 unsigned pos, unsigned n)
3485 isl_bool rational;
3486 isl_space *res_dim;
3487 struct isl_basic_map *res;
3488 struct isl_dim_map *dim_map;
3489 unsigned total, off;
3490 enum isl_dim_type t;
3492 if (n == 0)
3493 return basic_map_space_reset(bmap, type);
3495 if (!bmap)
3496 return NULL;
3498 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3500 total = isl_basic_map_total_dim(bmap) + n;
3501 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3502 off = 0;
3503 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3504 if (t != type) {
3505 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3506 } else {
3507 unsigned size = isl_basic_map_dim(bmap, t);
3508 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3509 0, pos, off);
3510 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3511 pos, size - pos, off + pos + n);
3513 off += isl_space_dim(res_dim, t);
3515 isl_dim_map_div(dim_map, bmap, off);
3517 res = isl_basic_map_alloc_space(res_dim,
3518 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3519 rational = isl_basic_map_is_rational(bmap);
3520 if (rational < 0)
3521 res = isl_basic_map_free(res);
3522 if (rational)
3523 res = isl_basic_map_set_rational(res);
3524 if (isl_basic_map_plain_is_empty(bmap)) {
3525 isl_basic_map_free(bmap);
3526 free(dim_map);
3527 return isl_basic_map_set_to_empty(res);
3529 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3530 return isl_basic_map_finalize(res);
3533 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3534 __isl_take isl_basic_set *bset,
3535 enum isl_dim_type type, unsigned pos, unsigned n)
3537 return isl_basic_map_insert_dims(bset, type, pos, n);
3540 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3541 enum isl_dim_type type, unsigned n)
3543 if (!bmap)
3544 return NULL;
3545 return isl_basic_map_insert_dims(bmap, type,
3546 isl_basic_map_dim(bmap, type), n);
3549 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3550 enum isl_dim_type type, unsigned n)
3552 if (!bset)
3553 return NULL;
3554 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3555 return isl_basic_map_add_dims(bset, type, n);
3556 error:
3557 isl_basic_set_free(bset);
3558 return NULL;
3561 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3562 enum isl_dim_type type)
3564 isl_space *space;
3566 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3567 return map;
3569 space = isl_map_get_space(map);
3570 space = isl_space_reset(space, type);
3571 map = isl_map_reset_space(map, space);
3572 return map;
3575 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3576 enum isl_dim_type type, unsigned pos, unsigned n)
3578 int i;
3580 if (n == 0)
3581 return map_space_reset(map, type);
3583 map = isl_map_cow(map);
3584 if (!map)
3585 return NULL;
3587 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3588 if (!map->dim)
3589 goto error;
3591 for (i = 0; i < map->n; ++i) {
3592 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3593 if (!map->p[i])
3594 goto error;
3597 return map;
3598 error:
3599 isl_map_free(map);
3600 return NULL;
3603 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3604 enum isl_dim_type type, unsigned pos, unsigned n)
3606 return isl_map_insert_dims(set, type, pos, n);
3609 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3610 enum isl_dim_type type, unsigned n)
3612 if (!map)
3613 return NULL;
3614 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3617 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3618 enum isl_dim_type type, unsigned n)
3620 if (!set)
3621 return NULL;
3622 isl_assert(set->ctx, type != isl_dim_in, goto error);
3623 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3624 error:
3625 isl_set_free(set);
3626 return NULL;
3629 __isl_give isl_basic_map *isl_basic_map_move_dims(
3630 __isl_take isl_basic_map *bmap,
3631 enum isl_dim_type dst_type, unsigned dst_pos,
3632 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3634 struct isl_dim_map *dim_map;
3635 struct isl_basic_map *res;
3636 enum isl_dim_type t;
3637 unsigned total, off;
3639 if (!bmap)
3640 return NULL;
3641 if (n == 0)
3642 return bmap;
3644 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3645 return isl_basic_map_free(bmap);
3647 if (dst_type == src_type && dst_pos == src_pos)
3648 return bmap;
3650 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3652 if (pos(bmap->dim, dst_type) + dst_pos ==
3653 pos(bmap->dim, src_type) + src_pos +
3654 ((src_type < dst_type) ? n : 0)) {
3655 bmap = isl_basic_map_cow(bmap);
3656 if (!bmap)
3657 return NULL;
3659 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3660 src_type, src_pos, n);
3661 if (!bmap->dim)
3662 goto error;
3664 bmap = isl_basic_map_finalize(bmap);
3666 return bmap;
3669 total = isl_basic_map_total_dim(bmap);
3670 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3672 off = 0;
3673 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3674 unsigned size = isl_space_dim(bmap->dim, t);
3675 if (t == dst_type) {
3676 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3677 0, dst_pos, off);
3678 off += dst_pos;
3679 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3680 src_pos, n, off);
3681 off += n;
3682 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3683 dst_pos, size - dst_pos, off);
3684 off += size - dst_pos;
3685 } else if (t == src_type) {
3686 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3687 0, src_pos, off);
3688 off += src_pos;
3689 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3690 src_pos + n, size - src_pos - n, off);
3691 off += size - src_pos - n;
3692 } else {
3693 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3694 off += size;
3697 isl_dim_map_div(dim_map, bmap, off);
3699 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3700 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3701 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3702 if (!bmap)
3703 goto error;
3705 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3706 src_type, src_pos, n);
3707 if (!bmap->dim)
3708 goto error;
3710 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3711 bmap = isl_basic_map_gauss(bmap, NULL);
3712 bmap = isl_basic_map_finalize(bmap);
3714 return bmap;
3715 error:
3716 isl_basic_map_free(bmap);
3717 return NULL;
3720 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3721 enum isl_dim_type dst_type, unsigned dst_pos,
3722 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3724 isl_basic_map *bmap = bset_to_bmap(bset);
3725 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3726 src_type, src_pos, n);
3727 return bset_from_bmap(bmap);
3730 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3731 enum isl_dim_type dst_type, unsigned dst_pos,
3732 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3734 if (!set)
3735 return NULL;
3736 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3737 return set_from_map(isl_map_move_dims(set_to_map(set),
3738 dst_type, dst_pos, src_type, src_pos, n));
3739 error:
3740 isl_set_free(set);
3741 return NULL;
3744 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3745 enum isl_dim_type dst_type, unsigned dst_pos,
3746 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3748 int i;
3750 if (!map)
3751 return NULL;
3752 if (n == 0)
3753 return map;
3755 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3756 goto error);
3758 if (dst_type == src_type && dst_pos == src_pos)
3759 return map;
3761 isl_assert(map->ctx, dst_type != src_type, goto error);
3763 map = isl_map_cow(map);
3764 if (!map)
3765 return NULL;
3767 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3768 if (!map->dim)
3769 goto error;
3771 for (i = 0; i < map->n; ++i) {
3772 map->p[i] = isl_basic_map_move_dims(map->p[i],
3773 dst_type, dst_pos,
3774 src_type, src_pos, n);
3775 if (!map->p[i])
3776 goto error;
3779 return map;
3780 error:
3781 isl_map_free(map);
3782 return NULL;
3785 /* Move the specified dimensions to the last columns right before
3786 * the divs. Don't change the dimension specification of bmap.
3787 * That's the responsibility of the caller.
3789 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3790 enum isl_dim_type type, unsigned first, unsigned n)
3792 struct isl_dim_map *dim_map;
3793 struct isl_basic_map *res;
3794 enum isl_dim_type t;
3795 unsigned total, off;
3797 if (!bmap)
3798 return NULL;
3799 if (pos(bmap->dim, type) + first + n ==
3800 1 + isl_space_dim(bmap->dim, isl_dim_all))
3801 return bmap;
3803 total = isl_basic_map_total_dim(bmap);
3804 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3806 off = 0;
3807 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3808 unsigned size = isl_space_dim(bmap->dim, t);
3809 if (t == type) {
3810 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3811 0, first, off);
3812 off += first;
3813 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3814 first, n, total - bmap->n_div - n);
3815 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3816 first + n, size - (first + n), off);
3817 off += size - (first + n);
3818 } else {
3819 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3820 off += size;
3823 isl_dim_map_div(dim_map, bmap, off + n);
3825 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3826 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3827 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3828 return res;
3831 /* Insert "n" rows in the divs of "bmap".
3833 * The number of columns is not changed, which means that the last
3834 * dimensions of "bmap" are being reintepreted as the new divs.
3835 * The space of "bmap" is not adjusted, however, which means
3836 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3837 * from the space of "bmap" is the responsibility of the caller.
3839 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3840 int n)
3842 int i;
3843 size_t row_size;
3844 isl_int **new_div;
3845 isl_int *old;
3847 bmap = isl_basic_map_cow(bmap);
3848 if (!bmap)
3849 return NULL;
3851 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3852 old = bmap->block2.data;
3853 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3854 (bmap->extra + n) * (1 + row_size));
3855 if (!bmap->block2.data)
3856 return isl_basic_map_free(bmap);
3857 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3858 if (!new_div)
3859 return isl_basic_map_free(bmap);
3860 for (i = 0; i < n; ++i) {
3861 new_div[i] = bmap->block2.data +
3862 (bmap->extra + i) * (1 + row_size);
3863 isl_seq_clr(new_div[i], 1 + row_size);
3865 for (i = 0; i < bmap->extra; ++i)
3866 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3867 free(bmap->div);
3868 bmap->div = new_div;
3869 bmap->n_div += n;
3870 bmap->extra += n;
3872 return bmap;
3875 /* Drop constraints from "bmap" that only involve the variables
3876 * of "type" in the range [first, first + n] that are not related
3877 * to any of the variables outside that interval.
3878 * These constraints cannot influence the values for the variables
3879 * outside the interval, except in case they cause "bmap" to be empty.
3880 * Only drop the constraints if "bmap" is known to be non-empty.
3882 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3883 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3884 unsigned first, unsigned n)
3886 int i;
3887 int *groups;
3888 unsigned dim, n_div;
3889 isl_bool non_empty;
3891 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3892 if (non_empty < 0)
3893 return isl_basic_map_free(bmap);
3894 if (!non_empty)
3895 return bmap;
3897 dim = isl_basic_map_dim(bmap, isl_dim_all);
3898 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3899 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3900 if (!groups)
3901 return isl_basic_map_free(bmap);
3902 first += isl_basic_map_offset(bmap, type) - 1;
3903 for (i = 0; i < first; ++i)
3904 groups[i] = -1;
3905 for (i = first + n; i < dim - n_div; ++i)
3906 groups[i] = -1;
3908 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3910 return bmap;
3913 /* Turn the n dimensions of type type, starting at first
3914 * into existentially quantified variables.
3916 * If a subset of the projected out variables are unrelated
3917 * to any of the variables that remain, then the constraints
3918 * involving this subset are simply dropped first.
3920 __isl_give isl_basic_map *isl_basic_map_project_out(
3921 __isl_take isl_basic_map *bmap,
3922 enum isl_dim_type type, unsigned first, unsigned n)
3924 if (n == 0)
3925 return basic_map_space_reset(bmap, type);
3926 if (type == isl_dim_div)
3927 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3928 "cannot project out existentially quantified variables",
3929 return isl_basic_map_free(bmap));
3931 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3932 if (!bmap)
3933 return NULL;
3935 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3936 return isl_basic_map_remove_dims(bmap, type, first, n);
3938 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3939 return isl_basic_map_free(bmap);
3941 bmap = move_last(bmap, type, first, n);
3942 bmap = isl_basic_map_cow(bmap);
3943 bmap = insert_div_rows(bmap, n);
3944 if (!bmap)
3945 return NULL;
3947 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3948 if (!bmap->dim)
3949 goto error;
3950 bmap = isl_basic_map_simplify(bmap);
3951 bmap = isl_basic_map_drop_redundant_divs(bmap);
3952 return isl_basic_map_finalize(bmap);
3953 error:
3954 isl_basic_map_free(bmap);
3955 return NULL;
3958 /* Turn the n dimensions of type type, starting at first
3959 * into existentially quantified variables.
3961 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3962 enum isl_dim_type type, unsigned first, unsigned n)
3964 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3965 type, first, n));
3968 /* Turn the n dimensions of type type, starting at first
3969 * into existentially quantified variables.
3971 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3972 enum isl_dim_type type, unsigned first, unsigned n)
3974 int i;
3976 if (!map)
3977 return NULL;
3979 if (n == 0)
3980 return map_space_reset(map, type);
3982 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3984 map = isl_map_cow(map);
3985 if (!map)
3986 return NULL;
3988 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3989 if (!map->dim)
3990 goto error;
3992 for (i = 0; i < map->n; ++i) {
3993 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3994 if (!map->p[i])
3995 goto error;
3998 return map;
3999 error:
4000 isl_map_free(map);
4001 return NULL;
4004 /* Turn the n dimensions of type type, starting at first
4005 * into existentially quantified variables.
4007 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4008 enum isl_dim_type type, unsigned first, unsigned n)
4010 return set_from_map(isl_map_project_out(set_to_map(set),
4011 type, first, n));
4014 /* Return a map that projects the elements in "set" onto their
4015 * "n" set dimensions starting at "first".
4016 * "type" should be equal to isl_dim_set.
4018 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4019 enum isl_dim_type type, unsigned first, unsigned n)
4021 int i;
4022 int dim;
4023 isl_map *map;
4025 if (!set)
4026 return NULL;
4027 if (type != isl_dim_set)
4028 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4029 "only set dimensions can be projected out", goto error);
4030 dim = isl_set_dim(set, isl_dim_set);
4031 if (first + n > dim || first + n < first)
4032 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4033 "index out of bounds", goto error);
4035 map = isl_map_from_domain(set);
4036 map = isl_map_add_dims(map, isl_dim_out, n);
4037 for (i = 0; i < n; ++i)
4038 map = isl_map_equate(map, isl_dim_in, first + i,
4039 isl_dim_out, i);
4040 return map;
4041 error:
4042 isl_set_free(set);
4043 return NULL;
4046 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4048 int i, j;
4050 for (i = 0; i < n; ++i) {
4051 j = isl_basic_map_alloc_div(bmap);
4052 if (j < 0)
4053 goto error;
4054 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4056 return bmap;
4057 error:
4058 isl_basic_map_free(bmap);
4059 return NULL;
4062 struct isl_basic_map *isl_basic_map_apply_range(
4063 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4065 isl_space *dim_result = NULL;
4066 struct isl_basic_map *bmap;
4067 unsigned n_in, n_out, n, nparam, total, pos;
4068 struct isl_dim_map *dim_map1, *dim_map2;
4070 if (!bmap1 || !bmap2)
4071 goto error;
4072 if (!isl_space_match(bmap1->dim, isl_dim_param,
4073 bmap2->dim, isl_dim_param))
4074 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4075 "parameters don't match", goto error);
4076 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4077 bmap2->dim, isl_dim_in))
4078 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4079 "spaces don't match", goto error);
4081 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4082 isl_space_copy(bmap2->dim));
4084 n_in = isl_basic_map_n_in(bmap1);
4085 n_out = isl_basic_map_n_out(bmap2);
4086 n = isl_basic_map_n_out(bmap1);
4087 nparam = isl_basic_map_n_param(bmap1);
4089 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4090 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4091 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4092 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4093 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4094 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4095 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4096 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4097 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4098 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4099 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4101 bmap = isl_basic_map_alloc_space(dim_result,
4102 bmap1->n_div + bmap2->n_div + n,
4103 bmap1->n_eq + bmap2->n_eq,
4104 bmap1->n_ineq + bmap2->n_ineq);
4105 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4106 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4107 bmap = add_divs(bmap, n);
4108 bmap = isl_basic_map_simplify(bmap);
4109 bmap = isl_basic_map_drop_redundant_divs(bmap);
4110 return isl_basic_map_finalize(bmap);
4111 error:
4112 isl_basic_map_free(bmap1);
4113 isl_basic_map_free(bmap2);
4114 return NULL;
4117 struct isl_basic_set *isl_basic_set_apply(
4118 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4120 if (!bset || !bmap)
4121 goto error;
4123 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4124 goto error);
4126 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4127 bmap));
4128 error:
4129 isl_basic_set_free(bset);
4130 isl_basic_map_free(bmap);
4131 return NULL;
4134 struct isl_basic_map *isl_basic_map_apply_domain(
4135 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4137 if (!bmap1 || !bmap2)
4138 goto error;
4140 if (!isl_space_match(bmap1->dim, isl_dim_param,
4141 bmap2->dim, isl_dim_param))
4142 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4143 "parameters don't match", goto error);
4144 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4145 bmap2->dim, isl_dim_in))
4146 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4147 "spaces don't match", 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 struct isl_basic_set *isl_basic_map_underlying_set(
4955 struct isl_basic_map *bmap)
4957 if (!bmap)
4958 goto error;
4959 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4960 bmap->n_div == 0 &&
4961 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4962 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4963 return bset_from_bmap(bmap);
4964 bmap = isl_basic_map_cow(bmap);
4965 if (!bmap)
4966 goto error;
4967 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4968 if (!bmap->dim)
4969 goto error;
4970 bmap->extra -= bmap->n_div;
4971 bmap->n_div = 0;
4972 bmap = isl_basic_map_finalize(bmap);
4973 return bset_from_bmap(bmap);
4974 error:
4975 isl_basic_map_free(bmap);
4976 return NULL;
4979 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4980 __isl_take isl_basic_set *bset)
4982 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4985 /* Replace each element in "list" by the result of applying
4986 * isl_basic_map_underlying_set to the element.
4988 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4989 __isl_take isl_basic_map_list *list)
4991 int i, n;
4993 if (!list)
4994 return NULL;
4996 n = isl_basic_map_list_n_basic_map(list);
4997 for (i = 0; i < n; ++i) {
4998 isl_basic_map *bmap;
4999 isl_basic_set *bset;
5001 bmap = isl_basic_map_list_get_basic_map(list, i);
5002 bset = isl_basic_set_underlying_set(bmap);
5003 list = isl_basic_set_list_set_basic_set(list, i, bset);
5006 return list;
5009 struct isl_basic_map *isl_basic_map_overlying_set(
5010 struct isl_basic_set *bset, struct isl_basic_map *like)
5012 struct isl_basic_map *bmap;
5013 struct isl_ctx *ctx;
5014 unsigned total;
5015 int i;
5017 if (!bset || !like)
5018 goto error;
5019 ctx = bset->ctx;
5020 isl_assert(ctx, bset->n_div == 0, goto error);
5021 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5022 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5023 goto error);
5024 if (like->n_div == 0) {
5025 isl_space *space = isl_basic_map_get_space(like);
5026 isl_basic_map_free(like);
5027 return isl_basic_map_reset_space(bset, space);
5029 bset = isl_basic_set_cow(bset);
5030 if (!bset)
5031 goto error;
5032 total = bset->dim->n_out + bset->extra;
5033 bmap = bset_to_bmap(bset);
5034 isl_space_free(bmap->dim);
5035 bmap->dim = isl_space_copy(like->dim);
5036 if (!bmap->dim)
5037 goto error;
5038 bmap->n_div = like->n_div;
5039 bmap->extra += like->n_div;
5040 if (bmap->extra) {
5041 unsigned ltotal;
5042 isl_int **div;
5043 ltotal = total - bmap->extra + like->extra;
5044 if (ltotal > total)
5045 ltotal = total;
5046 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5047 bmap->extra * (1 + 1 + total));
5048 if (isl_blk_is_error(bmap->block2))
5049 goto error;
5050 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5051 if (!div)
5052 goto error;
5053 bmap->div = div;
5054 for (i = 0; i < bmap->extra; ++i)
5055 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5056 for (i = 0; i < like->n_div; ++i) {
5057 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5058 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5060 bmap = isl_basic_map_add_known_div_constraints(bmap);
5062 isl_basic_map_free(like);
5063 bmap = isl_basic_map_simplify(bmap);
5064 bmap = isl_basic_map_finalize(bmap);
5065 return bmap;
5066 error:
5067 isl_basic_map_free(like);
5068 isl_basic_set_free(bset);
5069 return NULL;
5072 struct isl_basic_set *isl_basic_set_from_underlying_set(
5073 struct isl_basic_set *bset, struct isl_basic_set *like)
5075 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5076 bset_to_bmap(like)));
5079 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5081 int i;
5083 map = isl_map_cow(map);
5084 if (!map)
5085 return NULL;
5086 map->dim = isl_space_cow(map->dim);
5087 if (!map->dim)
5088 goto error;
5090 for (i = 1; i < map->n; ++i)
5091 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5092 goto error);
5093 for (i = 0; i < map->n; ++i) {
5094 map->p[i] = bset_to_bmap(
5095 isl_basic_map_underlying_set(map->p[i]));
5096 if (!map->p[i])
5097 goto error;
5099 if (map->n == 0)
5100 map->dim = isl_space_underlying(map->dim, 0);
5101 else {
5102 isl_space_free(map->dim);
5103 map->dim = isl_space_copy(map->p[0]->dim);
5105 if (!map->dim)
5106 goto error;
5107 return set_from_map(map);
5108 error:
5109 isl_map_free(map);
5110 return NULL;
5113 /* Replace the space of "bmap" by "space".
5115 * If the space of "bmap" is identical to "space" (including the identifiers
5116 * of the input and output dimensions), then simply return the original input.
5118 __isl_give isl_basic_map *isl_basic_map_reset_space(
5119 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5121 isl_bool equal;
5123 if (!bmap)
5124 goto error;
5125 equal = isl_space_is_equal(bmap->dim, space);
5126 if (equal >= 0 && equal)
5127 equal = isl_space_match(bmap->dim, isl_dim_in,
5128 space, isl_dim_in);
5129 if (equal >= 0 && equal)
5130 equal = isl_space_match(bmap->dim, isl_dim_out,
5131 space, isl_dim_out);
5132 if (equal < 0)
5133 goto error;
5134 if (equal) {
5135 isl_space_free(space);
5136 return bmap;
5138 bmap = isl_basic_map_cow(bmap);
5139 if (!bmap || !space)
5140 goto error;
5142 isl_space_free(bmap->dim);
5143 bmap->dim = space;
5145 bmap = isl_basic_map_finalize(bmap);
5147 return bmap;
5148 error:
5149 isl_basic_map_free(bmap);
5150 isl_space_free(space);
5151 return NULL;
5154 __isl_give isl_basic_set *isl_basic_set_reset_space(
5155 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5157 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5158 dim));
5161 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5162 __isl_take isl_space *dim)
5164 int i;
5166 map = isl_map_cow(map);
5167 if (!map || !dim)
5168 goto error;
5170 for (i = 0; i < map->n; ++i) {
5171 map->p[i] = isl_basic_map_reset_space(map->p[i],
5172 isl_space_copy(dim));
5173 if (!map->p[i])
5174 goto error;
5176 isl_space_free(map->dim);
5177 map->dim = dim;
5179 return map;
5180 error:
5181 isl_map_free(map);
5182 isl_space_free(dim);
5183 return NULL;
5186 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5187 __isl_take isl_space *dim)
5189 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5192 /* Compute the parameter domain of the given basic set.
5194 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5196 isl_bool is_params;
5197 isl_space *space;
5198 unsigned n;
5200 is_params = isl_basic_set_is_params(bset);
5201 if (is_params < 0)
5202 return isl_basic_set_free(bset);
5203 if (is_params)
5204 return bset;
5206 n = isl_basic_set_dim(bset, isl_dim_set);
5207 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5208 space = isl_basic_set_get_space(bset);
5209 space = isl_space_params(space);
5210 bset = isl_basic_set_reset_space(bset, space);
5211 return bset;
5214 /* Construct a zero-dimensional basic set with the given parameter domain.
5216 __isl_give isl_basic_set *isl_basic_set_from_params(
5217 __isl_take isl_basic_set *bset)
5219 isl_space *space;
5220 space = isl_basic_set_get_space(bset);
5221 space = isl_space_set_from_params(space);
5222 bset = isl_basic_set_reset_space(bset, space);
5223 return bset;
5226 /* Compute the parameter domain of the given set.
5228 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5230 isl_space *space;
5231 unsigned n;
5233 if (isl_set_is_params(set))
5234 return set;
5236 n = isl_set_dim(set, isl_dim_set);
5237 set = isl_set_project_out(set, isl_dim_set, 0, n);
5238 space = isl_set_get_space(set);
5239 space = isl_space_params(space);
5240 set = isl_set_reset_space(set, space);
5241 return set;
5244 /* Construct a zero-dimensional set with the given parameter domain.
5246 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5248 isl_space *space;
5249 space = isl_set_get_space(set);
5250 space = isl_space_set_from_params(space);
5251 set = isl_set_reset_space(set, space);
5252 return set;
5255 /* Compute the parameter domain of the given map.
5257 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5259 isl_space *space;
5260 unsigned n;
5262 n = isl_map_dim(map, isl_dim_in);
5263 map = isl_map_project_out(map, isl_dim_in, 0, n);
5264 n = isl_map_dim(map, isl_dim_out);
5265 map = isl_map_project_out(map, isl_dim_out, 0, n);
5266 space = isl_map_get_space(map);
5267 space = isl_space_params(space);
5268 map = isl_map_reset_space(map, space);
5269 return map;
5272 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5274 isl_space *space;
5275 unsigned n_out;
5277 if (!bmap)
5278 return NULL;
5279 space = isl_space_domain(isl_basic_map_get_space(bmap));
5281 n_out = isl_basic_map_n_out(bmap);
5282 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5284 return isl_basic_map_reset_space(bmap, space);
5287 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5289 if (!bmap)
5290 return isl_bool_error;
5291 return isl_space_may_be_set(bmap->dim);
5294 /* Is this basic map actually a set?
5295 * Users should never call this function. Outside of isl,
5296 * the type should indicate whether something is a set or a map.
5298 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5300 if (!bmap)
5301 return isl_bool_error;
5302 return isl_space_is_set(bmap->dim);
5305 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5307 isl_bool is_set;
5309 is_set = isl_basic_map_is_set(bmap);
5310 if (is_set < 0)
5311 goto error;
5312 if (is_set)
5313 return bmap;
5314 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5315 error:
5316 isl_basic_map_free(bmap);
5317 return NULL;
5320 __isl_give isl_basic_map *isl_basic_map_domain_map(
5321 __isl_take isl_basic_map *bmap)
5323 int i;
5324 isl_space *dim;
5325 isl_basic_map *domain;
5326 int nparam, n_in, n_out;
5328 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5329 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5330 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5332 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5333 domain = isl_basic_map_universe(dim);
5335 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5336 bmap = isl_basic_map_apply_range(bmap, domain);
5337 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5339 for (i = 0; i < n_in; ++i)
5340 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5341 isl_dim_out, i);
5343 bmap = isl_basic_map_gauss(bmap, NULL);
5344 return isl_basic_map_finalize(bmap);
5347 __isl_give isl_basic_map *isl_basic_map_range_map(
5348 __isl_take isl_basic_map *bmap)
5350 int i;
5351 isl_space *dim;
5352 isl_basic_map *range;
5353 int nparam, n_in, n_out;
5355 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5356 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5357 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5359 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5360 range = isl_basic_map_universe(dim);
5362 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5363 bmap = isl_basic_map_apply_range(bmap, range);
5364 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5366 for (i = 0; i < n_out; ++i)
5367 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5368 isl_dim_out, i);
5370 bmap = isl_basic_map_gauss(bmap, NULL);
5371 return isl_basic_map_finalize(bmap);
5374 int isl_map_may_be_set(__isl_keep isl_map *map)
5376 if (!map)
5377 return -1;
5378 return isl_space_may_be_set(map->dim);
5381 /* Is this map actually a set?
5382 * Users should never call this function. Outside of isl,
5383 * the type should indicate whether something is a set or a map.
5385 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5387 if (!map)
5388 return isl_bool_error;
5389 return isl_space_is_set(map->dim);
5392 struct isl_set *isl_map_range(struct isl_map *map)
5394 int i;
5395 isl_bool is_set;
5396 struct isl_set *set;
5398 is_set = isl_map_is_set(map);
5399 if (is_set < 0)
5400 goto error;
5401 if (is_set)
5402 return set_from_map(map);
5404 map = isl_map_cow(map);
5405 if (!map)
5406 goto error;
5408 set = set_from_map(map);
5409 set->dim = isl_space_range(set->dim);
5410 if (!set->dim)
5411 goto error;
5412 for (i = 0; i < map->n; ++i) {
5413 set->p[i] = isl_basic_map_range(map->p[i]);
5414 if (!set->p[i])
5415 goto error;
5417 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5418 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5419 return set;
5420 error:
5421 isl_map_free(map);
5422 return NULL;
5425 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5427 int i;
5429 map = isl_map_cow(map);
5430 if (!map)
5431 return NULL;
5433 map->dim = isl_space_domain_map(map->dim);
5434 if (!map->dim)
5435 goto error;
5436 for (i = 0; i < map->n; ++i) {
5437 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5438 if (!map->p[i])
5439 goto error;
5441 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5442 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5443 return map;
5444 error:
5445 isl_map_free(map);
5446 return NULL;
5449 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5451 int i;
5452 isl_space *range_dim;
5454 map = isl_map_cow(map);
5455 if (!map)
5456 return NULL;
5458 range_dim = isl_space_range(isl_map_get_space(map));
5459 range_dim = isl_space_from_range(range_dim);
5460 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5461 map->dim = isl_space_join(map->dim, range_dim);
5462 if (!map->dim)
5463 goto error;
5464 for (i = 0; i < map->n; ++i) {
5465 map->p[i] = isl_basic_map_range_map(map->p[i]);
5466 if (!map->p[i])
5467 goto error;
5469 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5470 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5471 return map;
5472 error:
5473 isl_map_free(map);
5474 return NULL;
5477 /* Given a wrapped map of the form A[B -> C],
5478 * return the map A[B -> C] -> B.
5480 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5482 isl_id *id;
5483 isl_map *map;
5485 if (!set)
5486 return NULL;
5487 if (!isl_set_has_tuple_id(set))
5488 return isl_map_domain_map(isl_set_unwrap(set));
5490 id = isl_set_get_tuple_id(set);
5491 map = isl_map_domain_map(isl_set_unwrap(set));
5492 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5494 return map;
5497 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5498 __isl_take isl_space *dim)
5500 int i;
5501 struct isl_map *map = NULL;
5503 set = isl_set_cow(set);
5504 if (!set || !dim)
5505 goto error;
5506 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5507 goto error);
5508 map = set_to_map(set);
5509 for (i = 0; i < set->n; ++i) {
5510 map->p[i] = basic_map_from_basic_set(
5511 set->p[i], isl_space_copy(dim));
5512 if (!map->p[i])
5513 goto error;
5515 isl_space_free(map->dim);
5516 map->dim = dim;
5517 return map;
5518 error:
5519 isl_space_free(dim);
5520 isl_set_free(set);
5521 return NULL;
5524 __isl_give isl_basic_map *isl_basic_map_from_domain(
5525 __isl_take isl_basic_set *bset)
5527 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5530 __isl_give isl_basic_map *isl_basic_map_from_range(
5531 __isl_take isl_basic_set *bset)
5533 isl_space *space;
5534 space = isl_basic_set_get_space(bset);
5535 space = isl_space_from_range(space);
5536 bset = isl_basic_set_reset_space(bset, space);
5537 return bset_to_bmap(bset);
5540 /* Create a relation with the given set as range.
5541 * The domain of the created relation is a zero-dimensional
5542 * flat anonymous space.
5544 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5546 isl_space *space;
5547 space = isl_set_get_space(set);
5548 space = isl_space_from_range(space);
5549 set = isl_set_reset_space(set, space);
5550 return set_to_map(set);
5553 /* Create a relation with the given set as domain.
5554 * The range of the created relation is a zero-dimensional
5555 * flat anonymous space.
5557 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5559 return isl_map_reverse(isl_map_from_range(set));
5562 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5563 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5565 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5568 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5569 __isl_take isl_set *range)
5571 return isl_map_apply_range(isl_map_reverse(domain), range);
5574 /* Return a newly allocated isl_map with given space and flags and
5575 * room for "n" basic maps.
5576 * Make sure that all cached information is cleared.
5578 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5579 unsigned flags)
5581 struct isl_map *map;
5583 if (!space)
5584 return NULL;
5585 if (n < 0)
5586 isl_die(space->ctx, isl_error_internal,
5587 "negative number of basic maps", goto error);
5588 map = isl_calloc(space->ctx, struct isl_map,
5589 sizeof(struct isl_map) +
5590 (n - 1) * sizeof(struct isl_basic_map *));
5591 if (!map)
5592 goto error;
5594 map->ctx = space->ctx;
5595 isl_ctx_ref(map->ctx);
5596 map->ref = 1;
5597 map->size = n;
5598 map->n = 0;
5599 map->dim = space;
5600 map->flags = flags;
5601 return map;
5602 error:
5603 isl_space_free(space);
5604 return NULL;
5607 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5608 unsigned nparam, unsigned in, unsigned out, int n,
5609 unsigned flags)
5611 struct isl_map *map;
5612 isl_space *dims;
5614 dims = isl_space_alloc(ctx, nparam, in, out);
5615 if (!dims)
5616 return NULL;
5618 map = isl_map_alloc_space(dims, n, flags);
5619 return map;
5622 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5624 struct isl_basic_map *bmap;
5625 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5626 bmap = isl_basic_map_set_to_empty(bmap);
5627 return bmap;
5630 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5632 struct isl_basic_set *bset;
5633 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5634 bset = isl_basic_set_set_to_empty(bset);
5635 return bset;
5638 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5640 struct isl_basic_map *bmap;
5641 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5642 bmap = isl_basic_map_finalize(bmap);
5643 return bmap;
5646 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5648 struct isl_basic_set *bset;
5649 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5650 bset = isl_basic_set_finalize(bset);
5651 return bset;
5654 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5656 int i;
5657 unsigned total = isl_space_dim(dim, isl_dim_all);
5658 isl_basic_map *bmap;
5660 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5661 for (i = 0; i < total; ++i) {
5662 int k = isl_basic_map_alloc_inequality(bmap);
5663 if (k < 0)
5664 goto error;
5665 isl_seq_clr(bmap->ineq[k], 1 + total);
5666 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5668 return bmap;
5669 error:
5670 isl_basic_map_free(bmap);
5671 return NULL;
5674 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5676 return isl_basic_map_nat_universe(dim);
5679 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5681 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5684 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5686 return isl_map_nat_universe(dim);
5689 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5691 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5694 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5696 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5699 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5701 struct isl_map *map;
5702 if (!dim)
5703 return NULL;
5704 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5705 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5706 return map;
5709 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5711 struct isl_set *set;
5712 if (!dim)
5713 return NULL;
5714 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5715 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5716 return set;
5719 struct isl_map *isl_map_dup(struct isl_map *map)
5721 int i;
5722 struct isl_map *dup;
5724 if (!map)
5725 return NULL;
5726 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5727 for (i = 0; i < map->n; ++i)
5728 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5729 return dup;
5732 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5733 __isl_take isl_basic_map *bmap)
5735 if (!bmap || !map)
5736 goto error;
5737 if (isl_basic_map_plain_is_empty(bmap)) {
5738 isl_basic_map_free(bmap);
5739 return map;
5741 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5742 isl_assert(map->ctx, map->n < map->size, goto error);
5743 map->p[map->n] = bmap;
5744 map->n++;
5745 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5746 return map;
5747 error:
5748 if (map)
5749 isl_map_free(map);
5750 if (bmap)
5751 isl_basic_map_free(bmap);
5752 return NULL;
5755 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5757 int i;
5759 if (!map)
5760 return NULL;
5762 if (--map->ref > 0)
5763 return NULL;
5765 clear_caches(map);
5766 isl_ctx_deref(map->ctx);
5767 for (i = 0; i < map->n; ++i)
5768 isl_basic_map_free(map->p[i]);
5769 isl_space_free(map->dim);
5770 free(map);
5772 return NULL;
5775 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5776 struct isl_basic_map *bmap, unsigned pos, int value)
5778 int j;
5780 bmap = isl_basic_map_cow(bmap);
5781 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5782 j = isl_basic_map_alloc_equality(bmap);
5783 if (j < 0)
5784 goto error;
5785 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5786 isl_int_set_si(bmap->eq[j][pos], -1);
5787 isl_int_set_si(bmap->eq[j][0], value);
5788 bmap = isl_basic_map_simplify(bmap);
5789 return isl_basic_map_finalize(bmap);
5790 error:
5791 isl_basic_map_free(bmap);
5792 return NULL;
5795 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5796 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5798 int j;
5800 bmap = isl_basic_map_cow(bmap);
5801 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5802 j = isl_basic_map_alloc_equality(bmap);
5803 if (j < 0)
5804 goto error;
5805 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5806 isl_int_set_si(bmap->eq[j][pos], -1);
5807 isl_int_set(bmap->eq[j][0], value);
5808 bmap = isl_basic_map_simplify(bmap);
5809 return isl_basic_map_finalize(bmap);
5810 error:
5811 isl_basic_map_free(bmap);
5812 return NULL;
5815 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5816 enum isl_dim_type type, unsigned pos, int value)
5818 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5819 return isl_basic_map_free(bmap);
5820 return isl_basic_map_fix_pos_si(bmap,
5821 isl_basic_map_offset(bmap, type) + pos, value);
5824 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5825 enum isl_dim_type type, unsigned pos, isl_int value)
5827 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5828 return isl_basic_map_free(bmap);
5829 return isl_basic_map_fix_pos(bmap,
5830 isl_basic_map_offset(bmap, type) + pos, value);
5833 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5834 * to be equal to "v".
5836 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5837 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5839 if (!bmap || !v)
5840 goto error;
5841 if (!isl_val_is_int(v))
5842 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5843 "expecting integer value", goto error);
5844 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5845 goto error;
5846 pos += isl_basic_map_offset(bmap, type);
5847 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5848 isl_val_free(v);
5849 return bmap;
5850 error:
5851 isl_basic_map_free(bmap);
5852 isl_val_free(v);
5853 return NULL;
5856 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5857 * to be equal to "v".
5859 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5860 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5862 return isl_basic_map_fix_val(bset, type, pos, v);
5865 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5866 enum isl_dim_type type, unsigned pos, int value)
5868 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5869 type, pos, value));
5872 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5873 enum isl_dim_type type, unsigned pos, isl_int value)
5875 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5876 type, pos, value));
5879 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5880 unsigned input, int value)
5882 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5885 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5886 unsigned dim, int value)
5888 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5889 isl_dim_set, dim, value));
5892 static int remove_if_empty(__isl_keep isl_map *map, int i)
5894 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5896 if (empty < 0)
5897 return -1;
5898 if (!empty)
5899 return 0;
5901 isl_basic_map_free(map->p[i]);
5902 if (i != map->n - 1) {
5903 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5904 map->p[i] = map->p[map->n - 1];
5906 map->n--;
5908 return 0;
5911 /* Perform "fn" on each basic map of "map", where we may not be holding
5912 * the only reference to "map".
5913 * In particular, "fn" should be a semantics preserving operation
5914 * that we want to apply to all copies of "map". We therefore need
5915 * to be careful not to modify "map" in a way that breaks "map"
5916 * in case anything goes wrong.
5918 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5919 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5921 struct isl_basic_map *bmap;
5922 int i;
5924 if (!map)
5925 return NULL;
5927 for (i = map->n - 1; i >= 0; --i) {
5928 bmap = isl_basic_map_copy(map->p[i]);
5929 bmap = fn(bmap);
5930 if (!bmap)
5931 goto error;
5932 isl_basic_map_free(map->p[i]);
5933 map->p[i] = bmap;
5934 if (remove_if_empty(map, i) < 0)
5935 goto error;
5938 return map;
5939 error:
5940 isl_map_free(map);
5941 return NULL;
5944 struct isl_map *isl_map_fix_si(struct isl_map *map,
5945 enum isl_dim_type type, unsigned pos, int value)
5947 int i;
5949 map = isl_map_cow(map);
5950 if (!map)
5951 return NULL;
5953 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5954 for (i = map->n - 1; i >= 0; --i) {
5955 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5956 if (remove_if_empty(map, i) < 0)
5957 goto error;
5959 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5960 return map;
5961 error:
5962 isl_map_free(map);
5963 return NULL;
5966 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5967 enum isl_dim_type type, unsigned pos, int value)
5969 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5972 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5973 enum isl_dim_type type, unsigned pos, isl_int value)
5975 int i;
5977 map = isl_map_cow(map);
5978 if (!map)
5979 return NULL;
5981 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5982 for (i = 0; i < map->n; ++i) {
5983 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5984 if (!map->p[i])
5985 goto error;
5987 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5988 return map;
5989 error:
5990 isl_map_free(map);
5991 return NULL;
5994 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5995 enum isl_dim_type type, unsigned pos, isl_int value)
5997 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6000 /* Fix the value of the variable at position "pos" of type "type" of "map"
6001 * to be equal to "v".
6003 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6004 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6006 int i;
6008 map = isl_map_cow(map);
6009 if (!map || !v)
6010 goto error;
6012 if (!isl_val_is_int(v))
6013 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6014 "expecting integer value", goto error);
6015 if (pos >= isl_map_dim(map, type))
6016 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6017 "index out of bounds", goto error);
6018 for (i = map->n - 1; i >= 0; --i) {
6019 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6020 isl_val_copy(v));
6021 if (remove_if_empty(map, i) < 0)
6022 goto error;
6024 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6025 isl_val_free(v);
6026 return map;
6027 error:
6028 isl_map_free(map);
6029 isl_val_free(v);
6030 return NULL;
6033 /* Fix the value of the variable at position "pos" of type "type" of "set"
6034 * to be equal to "v".
6036 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6037 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6039 return isl_map_fix_val(set, type, pos, v);
6042 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6043 unsigned input, int value)
6045 return isl_map_fix_si(map, isl_dim_in, input, value);
6048 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6050 return set_from_map(isl_map_fix_si(set_to_map(set),
6051 isl_dim_set, dim, value));
6054 static __isl_give isl_basic_map *basic_map_bound_si(
6055 __isl_take isl_basic_map *bmap,
6056 enum isl_dim_type type, unsigned pos, int value, int upper)
6058 int j;
6060 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6061 return isl_basic_map_free(bmap);
6062 pos += isl_basic_map_offset(bmap, type);
6063 bmap = isl_basic_map_cow(bmap);
6064 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6065 j = isl_basic_map_alloc_inequality(bmap);
6066 if (j < 0)
6067 goto error;
6068 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6069 if (upper) {
6070 isl_int_set_si(bmap->ineq[j][pos], -1);
6071 isl_int_set_si(bmap->ineq[j][0], value);
6072 } else {
6073 isl_int_set_si(bmap->ineq[j][pos], 1);
6074 isl_int_set_si(bmap->ineq[j][0], -value);
6076 bmap = isl_basic_map_simplify(bmap);
6077 return isl_basic_map_finalize(bmap);
6078 error:
6079 isl_basic_map_free(bmap);
6080 return NULL;
6083 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6084 __isl_take isl_basic_map *bmap,
6085 enum isl_dim_type type, unsigned pos, int value)
6087 return basic_map_bound_si(bmap, type, pos, value, 0);
6090 /* Constrain the values of the given dimension to be no greater than "value".
6092 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6093 __isl_take isl_basic_map *bmap,
6094 enum isl_dim_type type, unsigned pos, int value)
6096 return basic_map_bound_si(bmap, type, pos, value, 1);
6099 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6100 enum isl_dim_type type, unsigned pos, int value, int upper)
6102 int i;
6104 map = isl_map_cow(map);
6105 if (!map)
6106 return NULL;
6108 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6109 for (i = 0; i < map->n; ++i) {
6110 map->p[i] = basic_map_bound_si(map->p[i],
6111 type, pos, value, upper);
6112 if (!map->p[i])
6113 goto error;
6115 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6116 return map;
6117 error:
6118 isl_map_free(map);
6119 return NULL;
6122 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6123 enum isl_dim_type type, unsigned pos, int value)
6125 return map_bound_si(map, type, pos, value, 0);
6128 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6129 enum isl_dim_type type, unsigned pos, int value)
6131 return map_bound_si(map, type, pos, value, 1);
6134 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6135 enum isl_dim_type type, unsigned pos, int value)
6137 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6138 type, pos, value));
6141 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6142 enum isl_dim_type type, unsigned pos, int value)
6144 return isl_map_upper_bound_si(set, type, pos, value);
6147 /* Bound the given variable of "bmap" from below (or above is "upper"
6148 * is set) to "value".
6150 static __isl_give isl_basic_map *basic_map_bound(
6151 __isl_take isl_basic_map *bmap,
6152 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6154 int j;
6156 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6157 return isl_basic_map_free(bmap);
6158 pos += isl_basic_map_offset(bmap, type);
6159 bmap = isl_basic_map_cow(bmap);
6160 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6161 j = isl_basic_map_alloc_inequality(bmap);
6162 if (j < 0)
6163 goto error;
6164 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6165 if (upper) {
6166 isl_int_set_si(bmap->ineq[j][pos], -1);
6167 isl_int_set(bmap->ineq[j][0], value);
6168 } else {
6169 isl_int_set_si(bmap->ineq[j][pos], 1);
6170 isl_int_neg(bmap->ineq[j][0], value);
6172 bmap = isl_basic_map_simplify(bmap);
6173 return isl_basic_map_finalize(bmap);
6174 error:
6175 isl_basic_map_free(bmap);
6176 return NULL;
6179 /* Bound the given variable of "map" from below (or above is "upper"
6180 * is set) to "value".
6182 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6183 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6185 int i;
6187 map = isl_map_cow(map);
6188 if (!map)
6189 return NULL;
6191 if (pos >= isl_map_dim(map, type))
6192 isl_die(map->ctx, isl_error_invalid,
6193 "index out of bounds", goto error);
6194 for (i = map->n - 1; i >= 0; --i) {
6195 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6196 if (remove_if_empty(map, i) < 0)
6197 goto error;
6199 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6200 return map;
6201 error:
6202 isl_map_free(map);
6203 return NULL;
6206 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6207 enum isl_dim_type type, unsigned pos, isl_int value)
6209 return map_bound(map, type, pos, value, 0);
6212 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6213 enum isl_dim_type type, unsigned pos, isl_int value)
6215 return map_bound(map, type, pos, value, 1);
6218 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6219 enum isl_dim_type type, unsigned pos, isl_int value)
6221 return isl_map_lower_bound(set, type, pos, value);
6224 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6225 enum isl_dim_type type, unsigned pos, isl_int value)
6227 return isl_map_upper_bound(set, type, pos, value);
6230 /* Force the values of the variable at position "pos" of type "type" of "set"
6231 * to be no smaller than "value".
6233 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6234 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6236 if (!value)
6237 goto error;
6238 if (!isl_val_is_int(value))
6239 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6240 "expecting integer value", goto error);
6241 set = isl_set_lower_bound(set, type, pos, value->n);
6242 isl_val_free(value);
6243 return set;
6244 error:
6245 isl_val_free(value);
6246 isl_set_free(set);
6247 return NULL;
6250 /* Force the values of the variable at position "pos" of type "type" of "set"
6251 * to be no greater than "value".
6253 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6254 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6256 if (!value)
6257 goto error;
6258 if (!isl_val_is_int(value))
6259 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6260 "expecting integer value", goto error);
6261 set = isl_set_upper_bound(set, type, pos, value->n);
6262 isl_val_free(value);
6263 return set;
6264 error:
6265 isl_val_free(value);
6266 isl_set_free(set);
6267 return NULL;
6270 struct isl_map *isl_map_reverse(struct isl_map *map)
6272 int i;
6274 map = isl_map_cow(map);
6275 if (!map)
6276 return NULL;
6278 map->dim = isl_space_reverse(map->dim);
6279 if (!map->dim)
6280 goto error;
6281 for (i = 0; i < map->n; ++i) {
6282 map->p[i] = isl_basic_map_reverse(map->p[i]);
6283 if (!map->p[i])
6284 goto error;
6286 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6287 return map;
6288 error:
6289 isl_map_free(map);
6290 return NULL;
6293 #undef TYPE
6294 #define TYPE isl_pw_multi_aff
6295 #undef SUFFIX
6296 #define SUFFIX _pw_multi_aff
6297 #undef EMPTY
6298 #define EMPTY isl_pw_multi_aff_empty
6299 #undef ADD
6300 #define ADD isl_pw_multi_aff_union_add
6301 #include "isl_map_lexopt_templ.c"
6303 /* Given a map "map", compute the lexicographically minimal
6304 * (or maximal) image element for each domain element in dom,
6305 * in the form of an isl_pw_multi_aff.
6306 * If "empty" is not NULL, then set *empty to those elements in dom that
6307 * do not have an image element.
6308 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6309 * should be computed over the domain of "map". "empty" is also NULL
6310 * in this case.
6312 * We first compute the lexicographically minimal or maximal element
6313 * in the first basic map. This results in a partial solution "res"
6314 * and a subset "todo" of dom that still need to be handled.
6315 * We then consider each of the remaining maps in "map" and successively
6316 * update both "res" and "todo".
6317 * If "empty" is NULL, then the todo sets are not needed and therefore
6318 * also not computed.
6320 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6321 __isl_take isl_map *map, __isl_take isl_set *dom,
6322 __isl_give isl_set **empty, unsigned flags)
6324 int i;
6325 int full;
6326 isl_pw_multi_aff *res;
6327 isl_set *todo;
6329 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6330 if (!map || (!full && !dom))
6331 goto error;
6333 if (isl_map_plain_is_empty(map)) {
6334 if (empty)
6335 *empty = dom;
6336 else
6337 isl_set_free(dom);
6338 return isl_pw_multi_aff_from_map(map);
6341 res = basic_map_partial_lexopt_pw_multi_aff(
6342 isl_basic_map_copy(map->p[0]),
6343 isl_set_copy(dom), empty, flags);
6345 if (empty)
6346 todo = *empty;
6347 for (i = 1; i < map->n; ++i) {
6348 isl_pw_multi_aff *res_i;
6350 res_i = basic_map_partial_lexopt_pw_multi_aff(
6351 isl_basic_map_copy(map->p[i]),
6352 isl_set_copy(dom), empty, flags);
6354 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6355 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6356 else
6357 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6359 if (empty)
6360 todo = isl_set_intersect(todo, *empty);
6363 isl_set_free(dom);
6364 isl_map_free(map);
6366 if (empty)
6367 *empty = todo;
6369 return res;
6370 error:
6371 if (empty)
6372 *empty = NULL;
6373 isl_set_free(dom);
6374 isl_map_free(map);
6375 return NULL;
6378 #undef TYPE
6379 #define TYPE isl_map
6380 #undef SUFFIX
6381 #define SUFFIX
6382 #undef EMPTY
6383 #define EMPTY isl_map_empty
6384 #undef ADD
6385 #define ADD isl_map_union_disjoint
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_map.
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 * If the input consists of more than one disjunct, then first
6398 * compute the desired result in the form of an isl_pw_multi_aff and
6399 * then convert that into an isl_map.
6401 * This function used to have an explicit implementation in terms
6402 * of isl_maps, but it would continually intersect the domains of
6403 * partial results with the complement of the domain of the next
6404 * partial solution, potentially leading to an explosion in the number
6405 * of disjuncts if there are several disjuncts in the input.
6406 * An even earlier implementation of this function would look for
6407 * better results in the domain of the partial result and for extra
6408 * results in the complement of this domain, which would lead to
6409 * even more splintering.
6411 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6412 __isl_take isl_map *map, __isl_take isl_set *dom,
6413 __isl_give isl_set **empty, unsigned flags)
6415 int full;
6416 struct isl_map *res;
6417 isl_pw_multi_aff *pma;
6419 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6420 if (!map || (!full && !dom))
6421 goto error;
6423 if (isl_map_plain_is_empty(map)) {
6424 if (empty)
6425 *empty = dom;
6426 else
6427 isl_set_free(dom);
6428 return map;
6431 if (map->n == 1) {
6432 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6433 dom, empty, flags);
6434 isl_map_free(map);
6435 return res;
6438 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6439 flags);
6440 return isl_map_from_pw_multi_aff(pma);
6441 error:
6442 if (empty)
6443 *empty = NULL;
6444 isl_set_free(dom);
6445 isl_map_free(map);
6446 return NULL;
6449 __isl_give isl_map *isl_map_partial_lexmax(
6450 __isl_take isl_map *map, __isl_take isl_set *dom,
6451 __isl_give isl_set **empty)
6453 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6456 __isl_give isl_map *isl_map_partial_lexmin(
6457 __isl_take isl_map *map, __isl_take isl_set *dom,
6458 __isl_give isl_set **empty)
6460 return isl_map_partial_lexopt(map, dom, empty, 0);
6463 __isl_give isl_set *isl_set_partial_lexmin(
6464 __isl_take isl_set *set, __isl_take isl_set *dom,
6465 __isl_give isl_set **empty)
6467 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6468 dom, empty));
6471 __isl_give isl_set *isl_set_partial_lexmax(
6472 __isl_take isl_set *set, __isl_take isl_set *dom,
6473 __isl_give isl_set **empty)
6475 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6476 dom, empty));
6479 /* Compute the lexicographic minimum (or maximum if "flags" includes
6480 * ISL_OPT_MAX) of "bset" over its parametric domain.
6482 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6483 unsigned flags)
6485 return isl_basic_map_lexopt(bset, flags);
6488 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6490 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6493 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6495 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6498 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6500 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6503 /* Compute the lexicographic minimum of "bset" over its parametric domain
6504 * for the purpose of quantifier elimination.
6505 * That is, find an explicit representation for all the existentially
6506 * quantified variables in "bset" by computing their lexicographic
6507 * minimum.
6509 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6510 __isl_take isl_basic_set *bset)
6512 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6515 /* Extract the first and only affine expression from list
6516 * and then add it to *pwaff with the given dom.
6517 * This domain is known to be disjoint from other domains
6518 * because of the way isl_basic_map_foreach_lexmax works.
6520 static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
6521 __isl_take isl_aff_list *list, void *user)
6523 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6524 isl_aff *aff;
6525 isl_pw_aff **pwaff = user;
6526 isl_pw_aff *pwaff_i;
6528 if (!list)
6529 goto error;
6530 if (isl_aff_list_n_aff(list) != 1)
6531 isl_die(ctx, isl_error_internal,
6532 "expecting single element list", goto error);
6534 aff = isl_aff_list_get_aff(list, 0);
6535 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6537 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6539 isl_aff_list_free(list);
6541 return isl_stat_ok;
6542 error:
6543 isl_basic_set_free(dom);
6544 isl_aff_list_free(list);
6545 return isl_stat_error;
6548 /* Given a basic map with one output dimension, compute the minimum or
6549 * maximum of that dimension as an isl_pw_aff.
6551 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6552 * call update_dim_opt on each leaf of the result.
6554 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6555 int max)
6557 isl_space *dim = isl_basic_map_get_space(bmap);
6558 isl_pw_aff *pwaff;
6559 isl_stat r;
6561 dim = isl_space_from_domain(isl_space_domain(dim));
6562 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6563 pwaff = isl_pw_aff_empty(dim);
6565 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6566 if (r < 0)
6567 return isl_pw_aff_free(pwaff);
6569 return pwaff;
6572 /* Compute the minimum or maximum of the given output dimension
6573 * as a function of the parameters and the input dimensions,
6574 * but independently of the other output dimensions.
6576 * We first project out the other output dimension and then compute
6577 * the "lexicographic" maximum in each basic map, combining the results
6578 * using isl_pw_aff_union_max.
6580 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6581 int max)
6583 int i;
6584 isl_pw_aff *pwaff;
6585 unsigned n_out;
6587 n_out = isl_map_dim(map, isl_dim_out);
6588 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6589 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6590 if (!map)
6591 return NULL;
6593 if (map->n == 0) {
6594 isl_space *dim = isl_map_get_space(map);
6595 isl_map_free(map);
6596 return isl_pw_aff_empty(dim);
6599 pwaff = basic_map_dim_opt(map->p[0], max);
6600 for (i = 1; i < map->n; ++i) {
6601 isl_pw_aff *pwaff_i;
6603 pwaff_i = basic_map_dim_opt(map->p[i], max);
6604 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6607 isl_map_free(map);
6609 return pwaff;
6612 /* Compute the minimum of the given output dimension as a function of the
6613 * parameters and input dimensions, but independently of
6614 * the other output dimensions.
6616 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6618 return map_dim_opt(map, pos, 0);
6621 /* Compute the maximum of the given output dimension as a function of the
6622 * parameters and input dimensions, but independently of
6623 * the other output dimensions.
6625 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6627 return map_dim_opt(map, pos, 1);
6630 /* Compute the minimum or maximum of the given set dimension
6631 * as a function of the parameters,
6632 * but independently of the other set dimensions.
6634 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6635 int max)
6637 return map_dim_opt(set, pos, max);
6640 /* Compute the maximum of the given set dimension as a function of the
6641 * parameters, but independently of the other set dimensions.
6643 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6645 return set_dim_opt(set, pos, 1);
6648 /* Compute the minimum of the given set dimension as a function of the
6649 * parameters, but independently of the other set dimensions.
6651 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6653 return set_dim_opt(set, pos, 0);
6656 /* Apply a preimage specified by "mat" on the parameters of "bset".
6657 * bset is assumed to have only parameters and divs.
6659 static struct isl_basic_set *basic_set_parameter_preimage(
6660 struct isl_basic_set *bset, struct isl_mat *mat)
6662 unsigned nparam;
6664 if (!bset || !mat)
6665 goto error;
6667 bset->dim = isl_space_cow(bset->dim);
6668 if (!bset->dim)
6669 goto error;
6671 nparam = isl_basic_set_dim(bset, isl_dim_param);
6673 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6675 bset->dim->nparam = 0;
6676 bset->dim->n_out = nparam;
6677 bset = isl_basic_set_preimage(bset, mat);
6678 if (bset) {
6679 bset->dim->nparam = bset->dim->n_out;
6680 bset->dim->n_out = 0;
6682 return bset;
6683 error:
6684 isl_mat_free(mat);
6685 isl_basic_set_free(bset);
6686 return NULL;
6689 /* Apply a preimage specified by "mat" on the parameters of "set".
6690 * set is assumed to have only parameters and divs.
6692 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6693 __isl_take isl_mat *mat)
6695 isl_space *space;
6696 unsigned nparam;
6698 if (!set || !mat)
6699 goto error;
6701 nparam = isl_set_dim(set, isl_dim_param);
6703 if (mat->n_row != 1 + nparam)
6704 isl_die(isl_set_get_ctx(set), isl_error_internal,
6705 "unexpected number of rows", goto error);
6707 space = isl_set_get_space(set);
6708 space = isl_space_move_dims(space, isl_dim_set, 0,
6709 isl_dim_param, 0, nparam);
6710 set = isl_set_reset_space(set, space);
6711 set = isl_set_preimage(set, mat);
6712 nparam = isl_set_dim(set, isl_dim_out);
6713 space = isl_set_get_space(set);
6714 space = isl_space_move_dims(space, isl_dim_param, 0,
6715 isl_dim_out, 0, nparam);
6716 set = isl_set_reset_space(set, space);
6717 return set;
6718 error:
6719 isl_mat_free(mat);
6720 isl_set_free(set);
6721 return NULL;
6724 /* Intersect the basic set "bset" with the affine space specified by the
6725 * equalities in "eq".
6727 static struct isl_basic_set *basic_set_append_equalities(
6728 struct isl_basic_set *bset, struct isl_mat *eq)
6730 int i, k;
6731 unsigned len;
6733 if (!bset || !eq)
6734 goto error;
6736 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6737 eq->n_row, 0);
6738 if (!bset)
6739 goto error;
6741 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6742 for (i = 0; i < eq->n_row; ++i) {
6743 k = isl_basic_set_alloc_equality(bset);
6744 if (k < 0)
6745 goto error;
6746 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6747 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6749 isl_mat_free(eq);
6751 bset = isl_basic_set_gauss(bset, NULL);
6752 bset = isl_basic_set_finalize(bset);
6754 return bset;
6755 error:
6756 isl_mat_free(eq);
6757 isl_basic_set_free(bset);
6758 return NULL;
6761 /* Intersect the set "set" with the affine space specified by the
6762 * equalities in "eq".
6764 static struct isl_set *set_append_equalities(struct isl_set *set,
6765 struct isl_mat *eq)
6767 int i;
6769 if (!set || !eq)
6770 goto error;
6772 for (i = 0; i < set->n; ++i) {
6773 set->p[i] = basic_set_append_equalities(set->p[i],
6774 isl_mat_copy(eq));
6775 if (!set->p[i])
6776 goto error;
6778 isl_mat_free(eq);
6779 return set;
6780 error:
6781 isl_mat_free(eq);
6782 isl_set_free(set);
6783 return NULL;
6786 /* Given a basic set "bset" that only involves parameters and existentially
6787 * quantified variables, return the index of the first equality
6788 * that only involves parameters. If there is no such equality then
6789 * return bset->n_eq.
6791 * This function assumes that isl_basic_set_gauss has been called on "bset".
6793 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6795 int i, j;
6796 unsigned nparam, n_div;
6798 if (!bset)
6799 return -1;
6801 nparam = isl_basic_set_dim(bset, isl_dim_param);
6802 n_div = isl_basic_set_dim(bset, isl_dim_div);
6804 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6805 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6806 ++i;
6809 return i;
6812 /* Compute an explicit representation for the existentially quantified
6813 * variables in "bset" by computing the "minimal value" of the set
6814 * variables. Since there are no set variables, the computation of
6815 * the minimal value essentially computes an explicit representation
6816 * of the non-empty part(s) of "bset".
6818 * The input only involves parameters and existentially quantified variables.
6819 * All equalities among parameters have been removed.
6821 * Since the existentially quantified variables in the result are in general
6822 * going to be different from those in the input, we first replace
6823 * them by the minimal number of variables based on their equalities.
6824 * This should simplify the parametric integer programming.
6826 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6828 isl_morph *morph1, *morph2;
6829 isl_set *set;
6830 unsigned n;
6832 if (!bset)
6833 return NULL;
6834 if (bset->n_eq == 0)
6835 return isl_basic_set_lexmin_compute_divs(bset);
6837 morph1 = isl_basic_set_parameter_compression(bset);
6838 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6839 bset = isl_basic_set_lift(bset);
6840 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6841 bset = isl_morph_basic_set(morph2, bset);
6842 n = isl_basic_set_dim(bset, isl_dim_set);
6843 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6845 set = isl_basic_set_lexmin_compute_divs(bset);
6847 set = isl_morph_set(isl_morph_inverse(morph1), set);
6849 return set;
6852 /* Project the given basic set onto its parameter domain, possibly introducing
6853 * new, explicit, existential variables in the constraints.
6854 * The input has parameters and (possibly implicit) existential variables.
6855 * The output has the same parameters, but only
6856 * explicit existentially quantified variables.
6858 * The actual projection is performed by pip, but pip doesn't seem
6859 * to like equalities very much, so we first remove the equalities
6860 * among the parameters by performing a variable compression on
6861 * the parameters. Afterward, an inverse transformation is performed
6862 * and the equalities among the parameters are inserted back in.
6864 * The variable compression on the parameters may uncover additional
6865 * equalities that were only implicit before. We therefore check
6866 * if there are any new parameter equalities in the result and
6867 * if so recurse. The removal of parameter equalities is required
6868 * for the parameter compression performed by base_compute_divs.
6870 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6872 int i;
6873 struct isl_mat *eq;
6874 struct isl_mat *T, *T2;
6875 struct isl_set *set;
6876 unsigned nparam;
6878 bset = isl_basic_set_cow(bset);
6879 if (!bset)
6880 return NULL;
6882 if (bset->n_eq == 0)
6883 return base_compute_divs(bset);
6885 bset = isl_basic_set_gauss(bset, NULL);
6886 if (!bset)
6887 return NULL;
6888 if (isl_basic_set_plain_is_empty(bset))
6889 return isl_set_from_basic_set(bset);
6891 i = first_parameter_equality(bset);
6892 if (i == bset->n_eq)
6893 return base_compute_divs(bset);
6895 nparam = isl_basic_set_dim(bset, isl_dim_param);
6896 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6897 0, 1 + nparam);
6898 eq = isl_mat_cow(eq);
6899 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6900 if (T && T->n_col == 0) {
6901 isl_mat_free(T);
6902 isl_mat_free(T2);
6903 isl_mat_free(eq);
6904 bset = isl_basic_set_set_to_empty(bset);
6905 return isl_set_from_basic_set(bset);
6907 bset = basic_set_parameter_preimage(bset, T);
6909 i = first_parameter_equality(bset);
6910 if (!bset)
6911 set = NULL;
6912 else if (i == bset->n_eq)
6913 set = base_compute_divs(bset);
6914 else
6915 set = parameter_compute_divs(bset);
6916 set = set_parameter_preimage(set, T2);
6917 set = set_append_equalities(set, eq);
6918 return set;
6921 /* Insert the divs from "ls" before those of "bmap".
6923 * The number of columns is not changed, which means that the last
6924 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6925 * The caller is responsible for removing the same number of dimensions
6926 * from the space of "bmap".
6928 static __isl_give isl_basic_map *insert_divs_from_local_space(
6929 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6931 int i;
6932 int n_div;
6933 int old_n_div;
6935 n_div = isl_local_space_dim(ls, isl_dim_div);
6936 if (n_div == 0)
6937 return bmap;
6939 old_n_div = bmap->n_div;
6940 bmap = insert_div_rows(bmap, n_div);
6941 if (!bmap)
6942 return NULL;
6944 for (i = 0; i < n_div; ++i) {
6945 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6946 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6949 return bmap;
6952 /* Replace the space of "bmap" by the space and divs of "ls".
6954 * If "ls" has any divs, then we simplify the result since we may
6955 * have discovered some additional equalities that could simplify
6956 * the div expressions.
6958 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6959 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6961 int n_div;
6963 bmap = isl_basic_map_cow(bmap);
6964 if (!bmap || !ls)
6965 goto error;
6967 n_div = isl_local_space_dim(ls, isl_dim_div);
6968 bmap = insert_divs_from_local_space(bmap, ls);
6969 if (!bmap)
6970 goto error;
6972 isl_space_free(bmap->dim);
6973 bmap->dim = isl_local_space_get_space(ls);
6974 if (!bmap->dim)
6975 goto error;
6977 isl_local_space_free(ls);
6978 if (n_div > 0)
6979 bmap = isl_basic_map_simplify(bmap);
6980 bmap = isl_basic_map_finalize(bmap);
6981 return bmap;
6982 error:
6983 isl_basic_map_free(bmap);
6984 isl_local_space_free(ls);
6985 return NULL;
6988 /* Replace the space of "map" by the space and divs of "ls".
6990 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6991 __isl_take isl_local_space *ls)
6993 int i;
6995 map = isl_map_cow(map);
6996 if (!map || !ls)
6997 goto error;
6999 for (i = 0; i < map->n; ++i) {
7000 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7001 isl_local_space_copy(ls));
7002 if (!map->p[i])
7003 goto error;
7005 isl_space_free(map->dim);
7006 map->dim = isl_local_space_get_space(ls);
7007 if (!map->dim)
7008 goto error;
7010 isl_local_space_free(ls);
7011 return map;
7012 error:
7013 isl_local_space_free(ls);
7014 isl_map_free(map);
7015 return NULL;
7018 /* Compute an explicit representation for the existentially
7019 * quantified variables for which do not know any explicit representation yet.
7021 * We first sort the existentially quantified variables so that the
7022 * existentially quantified variables for which we already have an explicit
7023 * representation are placed before those for which we do not.
7024 * The input dimensions, the output dimensions and the existentially
7025 * quantified variables for which we already have an explicit
7026 * representation are then turned into parameters.
7027 * compute_divs returns a map with the same parameters and
7028 * no input or output dimensions and the dimension specification
7029 * is reset to that of the input, including the existentially quantified
7030 * variables for which we already had an explicit representation.
7032 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7034 struct isl_basic_set *bset;
7035 struct isl_set *set;
7036 struct isl_map *map;
7037 isl_space *dim;
7038 isl_local_space *ls;
7039 unsigned nparam;
7040 unsigned n_in;
7041 unsigned n_out;
7042 int n_known;
7043 int i;
7045 bmap = isl_basic_map_sort_divs(bmap);
7046 bmap = isl_basic_map_cow(bmap);
7047 if (!bmap)
7048 return NULL;
7050 n_known = isl_basic_map_first_unknown_div(bmap);
7051 if (n_known < 0)
7052 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7054 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7055 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7056 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7057 dim = isl_space_set_alloc(bmap->ctx,
7058 nparam + n_in + n_out + n_known, 0);
7059 if (!dim)
7060 goto error;
7062 ls = isl_basic_map_get_local_space(bmap);
7063 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7064 n_known, bmap->n_div - n_known);
7065 if (n_known > 0) {
7066 for (i = n_known; i < bmap->n_div; ++i)
7067 swap_div(bmap, i - n_known, i);
7068 bmap->n_div -= n_known;
7069 bmap->extra -= n_known;
7071 bmap = isl_basic_map_reset_space(bmap, dim);
7072 bset = bset_from_bmap(bmap);
7074 set = parameter_compute_divs(bset);
7075 map = set_to_map(set);
7076 map = replace_space_by_local_space(map, ls);
7078 return map;
7079 error:
7080 isl_basic_map_free(bmap);
7081 return NULL;
7084 /* Remove the explicit representation of local variable "div",
7085 * if there is any.
7087 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7088 __isl_take isl_basic_map *bmap, int div)
7090 isl_bool unknown;
7092 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7093 if (unknown < 0)
7094 return isl_basic_map_free(bmap);
7095 if (unknown)
7096 return bmap;
7098 bmap = isl_basic_map_cow(bmap);
7099 if (!bmap)
7100 return NULL;
7101 isl_int_set_si(bmap->div[div][0], 0);
7102 return bmap;
7105 /* Is local variable "div" of "bmap" marked as not having an explicit
7106 * representation?
7107 * Note that even if "div" is not marked in this way and therefore
7108 * has an explicit representation, this representation may still
7109 * depend (indirectly) on other local variables that do not
7110 * have an explicit representation.
7112 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7113 int div)
7115 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7116 return isl_bool_error;
7117 return isl_int_is_zero(bmap->div[div][0]);
7120 /* Return the position of the first local variable that does not
7121 * have an explicit representation.
7122 * Return the total number of local variables if they all have
7123 * an explicit representation.
7124 * Return -1 on error.
7126 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7128 int i;
7130 if (!bmap)
7131 return -1;
7133 for (i = 0; i < bmap->n_div; ++i) {
7134 if (!isl_basic_map_div_is_known(bmap, i))
7135 return i;
7137 return bmap->n_div;
7140 /* Return the position of the first local variable that does not
7141 * have an explicit representation.
7142 * Return the total number of local variables if they all have
7143 * an explicit representation.
7144 * Return -1 on error.
7146 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7148 return isl_basic_map_first_unknown_div(bset);
7151 /* Does "bmap" have an explicit representation for all local variables?
7153 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7155 int first, n;
7157 n = isl_basic_map_dim(bmap, isl_dim_div);
7158 first = isl_basic_map_first_unknown_div(bmap);
7159 if (first < 0)
7160 return isl_bool_error;
7161 return first == n;
7164 /* Do all basic maps in "map" have an explicit representation
7165 * for all local variables?
7167 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7169 int i;
7171 if (!map)
7172 return isl_bool_error;
7174 for (i = 0; i < map->n; ++i) {
7175 int known = isl_basic_map_divs_known(map->p[i]);
7176 if (known <= 0)
7177 return known;
7180 return isl_bool_true;
7183 /* If bmap contains any unknown divs, then compute explicit
7184 * expressions for them. However, this computation may be
7185 * quite expensive, so first try to remove divs that aren't
7186 * strictly needed.
7188 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7190 int known;
7191 struct isl_map *map;
7193 known = isl_basic_map_divs_known(bmap);
7194 if (known < 0)
7195 goto error;
7196 if (known)
7197 return isl_map_from_basic_map(bmap);
7199 bmap = isl_basic_map_drop_redundant_divs(bmap);
7201 known = isl_basic_map_divs_known(bmap);
7202 if (known < 0)
7203 goto error;
7204 if (known)
7205 return isl_map_from_basic_map(bmap);
7207 map = compute_divs(bmap);
7208 return map;
7209 error:
7210 isl_basic_map_free(bmap);
7211 return NULL;
7214 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7216 int i;
7217 int known;
7218 struct isl_map *res;
7220 if (!map)
7221 return NULL;
7222 if (map->n == 0)
7223 return map;
7225 known = isl_map_divs_known(map);
7226 if (known < 0) {
7227 isl_map_free(map);
7228 return NULL;
7230 if (known)
7231 return map;
7233 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7234 for (i = 1 ; i < map->n; ++i) {
7235 struct isl_map *r2;
7236 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7237 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7238 res = isl_map_union_disjoint(res, r2);
7239 else
7240 res = isl_map_union(res, r2);
7242 isl_map_free(map);
7244 return res;
7247 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7249 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7252 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7254 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7257 struct isl_set *isl_map_domain(struct isl_map *map)
7259 int i;
7260 struct isl_set *set;
7262 if (!map)
7263 goto error;
7265 map = isl_map_cow(map);
7266 if (!map)
7267 return NULL;
7269 set = set_from_map(map);
7270 set->dim = isl_space_domain(set->dim);
7271 if (!set->dim)
7272 goto error;
7273 for (i = 0; i < map->n; ++i) {
7274 set->p[i] = isl_basic_map_domain(map->p[i]);
7275 if (!set->p[i])
7276 goto error;
7278 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7279 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7280 return set;
7281 error:
7282 isl_map_free(map);
7283 return NULL;
7286 /* Return the union of "map1" and "map2", where we assume for now that
7287 * "map1" and "map2" are disjoint. Note that the basic maps inside
7288 * "map1" or "map2" may not be disjoint from each other.
7289 * Also note that this function is also called from isl_map_union,
7290 * which takes care of handling the situation where "map1" and "map2"
7291 * may not be disjoint.
7293 * If one of the inputs is empty, we can simply return the other input.
7294 * Similarly, if one of the inputs is universal, then it is equal to the union.
7296 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7297 __isl_take isl_map *map2)
7299 int i;
7300 unsigned flags = 0;
7301 struct isl_map *map = NULL;
7302 int is_universe;
7304 if (!map1 || !map2)
7305 goto error;
7307 if (!isl_space_is_equal(map1->dim, map2->dim))
7308 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7309 "spaces don't match", goto error);
7311 if (map1->n == 0) {
7312 isl_map_free(map1);
7313 return map2;
7315 if (map2->n == 0) {
7316 isl_map_free(map2);
7317 return map1;
7320 is_universe = isl_map_plain_is_universe(map1);
7321 if (is_universe < 0)
7322 goto error;
7323 if (is_universe) {
7324 isl_map_free(map2);
7325 return map1;
7328 is_universe = isl_map_plain_is_universe(map2);
7329 if (is_universe < 0)
7330 goto error;
7331 if (is_universe) {
7332 isl_map_free(map1);
7333 return map2;
7336 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7337 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7338 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7340 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7341 map1->n + map2->n, flags);
7342 if (!map)
7343 goto error;
7344 for (i = 0; i < map1->n; ++i) {
7345 map = isl_map_add_basic_map(map,
7346 isl_basic_map_copy(map1->p[i]));
7347 if (!map)
7348 goto error;
7350 for (i = 0; i < map2->n; ++i) {
7351 map = isl_map_add_basic_map(map,
7352 isl_basic_map_copy(map2->p[i]));
7353 if (!map)
7354 goto error;
7356 isl_map_free(map1);
7357 isl_map_free(map2);
7358 return map;
7359 error:
7360 isl_map_free(map);
7361 isl_map_free(map1);
7362 isl_map_free(map2);
7363 return NULL;
7366 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7367 * guaranteed to be disjoint by the caller.
7369 * Note that this functions is called from within isl_map_make_disjoint,
7370 * so we have to be careful not to touch the constraints of the inputs
7371 * in any way.
7373 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7374 __isl_take isl_map *map2)
7376 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7379 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7380 * not be disjoint. The parameters are assumed to have been aligned.
7382 * We currently simply call map_union_disjoint, the internal operation
7383 * of which does not really depend on the inputs being disjoint.
7384 * If the result contains more than one basic map, then we clear
7385 * the disjoint flag since the result may contain basic maps from
7386 * both inputs and these are not guaranteed to be disjoint.
7388 * As a special case, if "map1" and "map2" are obviously equal,
7389 * then we simply return "map1".
7391 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7392 __isl_take isl_map *map2)
7394 int equal;
7396 if (!map1 || !map2)
7397 goto error;
7399 equal = isl_map_plain_is_equal(map1, map2);
7400 if (equal < 0)
7401 goto error;
7402 if (equal) {
7403 isl_map_free(map2);
7404 return map1;
7407 map1 = map_union_disjoint(map1, map2);
7408 if (!map1)
7409 return NULL;
7410 if (map1->n > 1)
7411 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7412 return map1;
7413 error:
7414 isl_map_free(map1);
7415 isl_map_free(map2);
7416 return NULL;
7419 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7420 * not be disjoint.
7422 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7423 __isl_take isl_map *map2)
7425 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7428 struct isl_set *isl_set_union_disjoint(
7429 struct isl_set *set1, struct isl_set *set2)
7431 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7432 set_to_map(set2)));
7435 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7437 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7440 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7441 * the results.
7443 * "map" and "set" are assumed to be compatible and non-NULL.
7445 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7446 __isl_take isl_set *set,
7447 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7448 __isl_take isl_basic_set *bset))
7450 unsigned flags = 0;
7451 struct isl_map *result;
7452 int i, j;
7454 if (isl_set_plain_is_universe(set)) {
7455 isl_set_free(set);
7456 return map;
7459 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7460 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7461 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7463 result = isl_map_alloc_space(isl_space_copy(map->dim),
7464 map->n * set->n, flags);
7465 for (i = 0; result && i < map->n; ++i)
7466 for (j = 0; j < set->n; ++j) {
7467 result = isl_map_add_basic_map(result,
7468 fn(isl_basic_map_copy(map->p[i]),
7469 isl_basic_set_copy(set->p[j])));
7470 if (!result)
7471 break;
7474 isl_map_free(map);
7475 isl_set_free(set);
7476 return result;
7479 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7480 __isl_take isl_set *set)
7482 isl_bool ok;
7484 ok = isl_map_compatible_range(map, set);
7485 if (ok < 0)
7486 goto error;
7487 if (!ok)
7488 isl_die(set->ctx, isl_error_invalid,
7489 "incompatible spaces", goto error);
7491 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7492 error:
7493 isl_map_free(map);
7494 isl_set_free(set);
7495 return NULL;
7498 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7499 __isl_take isl_set *set)
7501 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7504 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7505 __isl_take isl_set *set)
7507 isl_bool ok;
7509 ok = isl_map_compatible_domain(map, set);
7510 if (ok < 0)
7511 goto error;
7512 if (!ok)
7513 isl_die(set->ctx, isl_error_invalid,
7514 "incompatible spaces", goto error);
7516 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7517 error:
7518 isl_map_free(map);
7519 isl_set_free(set);
7520 return NULL;
7523 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7524 __isl_take isl_set *set)
7526 return isl_map_align_params_map_map_and(map, set,
7527 &map_intersect_domain);
7530 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7531 __isl_take isl_map *map2)
7533 if (!map1 || !map2)
7534 goto error;
7535 map1 = isl_map_reverse(map1);
7536 map1 = isl_map_apply_range(map1, map2);
7537 return isl_map_reverse(map1);
7538 error:
7539 isl_map_free(map1);
7540 isl_map_free(map2);
7541 return NULL;
7544 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7545 __isl_take isl_map *map2)
7547 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7550 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7551 __isl_take isl_map *map2)
7553 isl_space *dim_result;
7554 struct isl_map *result;
7555 int i, j;
7557 if (!map1 || !map2)
7558 goto error;
7560 dim_result = isl_space_join(isl_space_copy(map1->dim),
7561 isl_space_copy(map2->dim));
7563 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7564 if (!result)
7565 goto error;
7566 for (i = 0; i < map1->n; ++i)
7567 for (j = 0; j < map2->n; ++j) {
7568 result = isl_map_add_basic_map(result,
7569 isl_basic_map_apply_range(
7570 isl_basic_map_copy(map1->p[i]),
7571 isl_basic_map_copy(map2->p[j])));
7572 if (!result)
7573 goto error;
7575 isl_map_free(map1);
7576 isl_map_free(map2);
7577 if (result && result->n <= 1)
7578 ISL_F_SET(result, ISL_MAP_DISJOINT);
7579 return result;
7580 error:
7581 isl_map_free(map1);
7582 isl_map_free(map2);
7583 return NULL;
7586 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7587 __isl_take isl_map *map2)
7589 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7593 * returns range - domain
7595 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7597 isl_space *target_space;
7598 struct isl_basic_set *bset;
7599 unsigned dim;
7600 unsigned nparam;
7601 int i;
7603 if (!bmap)
7604 goto error;
7605 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7606 bmap->dim, isl_dim_out),
7607 goto error);
7608 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7609 dim = isl_basic_map_n_in(bmap);
7610 nparam = isl_basic_map_n_param(bmap);
7611 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7612 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7613 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7614 for (i = 0; i < dim; ++i) {
7615 int j = isl_basic_map_alloc_equality(bmap);
7616 if (j < 0) {
7617 bmap = isl_basic_map_free(bmap);
7618 break;
7620 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7621 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7622 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7623 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7625 bset = isl_basic_map_domain(bmap);
7626 bset = isl_basic_set_reset_space(bset, target_space);
7627 return bset;
7628 error:
7629 isl_basic_map_free(bmap);
7630 return NULL;
7634 * returns range - domain
7636 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7638 int i;
7639 isl_space *dim;
7640 struct isl_set *result;
7642 if (!map)
7643 return NULL;
7645 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7646 map->dim, isl_dim_out),
7647 goto error);
7648 dim = isl_map_get_space(map);
7649 dim = isl_space_domain(dim);
7650 result = isl_set_alloc_space(dim, map->n, 0);
7651 if (!result)
7652 goto error;
7653 for (i = 0; i < map->n; ++i)
7654 result = isl_set_add_basic_set(result,
7655 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7656 isl_map_free(map);
7657 return result;
7658 error:
7659 isl_map_free(map);
7660 return NULL;
7664 * returns [domain -> range] -> range - domain
7666 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7667 __isl_take isl_basic_map *bmap)
7669 int i, k;
7670 isl_space *dim;
7671 isl_basic_map *domain;
7672 int nparam, n;
7673 unsigned total;
7675 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7676 bmap->dim, isl_dim_out))
7677 isl_die(bmap->ctx, isl_error_invalid,
7678 "domain and range don't match", goto error);
7680 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7681 n = isl_basic_map_dim(bmap, isl_dim_in);
7683 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7684 domain = isl_basic_map_universe(dim);
7686 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7687 bmap = isl_basic_map_apply_range(bmap, domain);
7688 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7690 total = isl_basic_map_total_dim(bmap);
7692 for (i = 0; i < n; ++i) {
7693 k = isl_basic_map_alloc_equality(bmap);
7694 if (k < 0)
7695 goto error;
7696 isl_seq_clr(bmap->eq[k], 1 + total);
7697 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7698 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7699 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7702 bmap = isl_basic_map_gauss(bmap, NULL);
7703 return isl_basic_map_finalize(bmap);
7704 error:
7705 isl_basic_map_free(bmap);
7706 return NULL;
7710 * returns [domain -> range] -> range - domain
7712 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7714 int i;
7715 isl_space *domain_dim;
7717 if (!map)
7718 return NULL;
7720 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7721 map->dim, isl_dim_out))
7722 isl_die(map->ctx, isl_error_invalid,
7723 "domain and range don't match", goto error);
7725 map = isl_map_cow(map);
7726 if (!map)
7727 return NULL;
7729 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7730 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7731 map->dim = isl_space_join(map->dim, domain_dim);
7732 if (!map->dim)
7733 goto error;
7734 for (i = 0; i < map->n; ++i) {
7735 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7736 if (!map->p[i])
7737 goto error;
7739 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7740 return map;
7741 error:
7742 isl_map_free(map);
7743 return NULL;
7746 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7748 struct isl_basic_map *bmap;
7749 unsigned nparam;
7750 unsigned dim;
7751 int i;
7753 if (!dims)
7754 return NULL;
7756 nparam = dims->nparam;
7757 dim = dims->n_out;
7758 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7759 if (!bmap)
7760 goto error;
7762 for (i = 0; i < dim; ++i) {
7763 int j = isl_basic_map_alloc_equality(bmap);
7764 if (j < 0)
7765 goto error;
7766 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7767 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7768 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7770 return isl_basic_map_finalize(bmap);
7771 error:
7772 isl_basic_map_free(bmap);
7773 return NULL;
7776 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7778 if (!dim)
7779 return NULL;
7780 if (dim->n_in != dim->n_out)
7781 isl_die(dim->ctx, isl_error_invalid,
7782 "number of input and output dimensions needs to be "
7783 "the same", goto error);
7784 return basic_map_identity(dim);
7785 error:
7786 isl_space_free(dim);
7787 return NULL;
7790 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7792 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7795 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7797 isl_space *dim = isl_set_get_space(set);
7798 isl_map *id;
7799 id = isl_map_identity(isl_space_map_from_set(dim));
7800 return isl_map_intersect_range(id, set);
7803 /* Construct a basic set with all set dimensions having only non-negative
7804 * values.
7806 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7807 __isl_take isl_space *space)
7809 int i;
7810 unsigned nparam;
7811 unsigned dim;
7812 struct isl_basic_set *bset;
7814 if (!space)
7815 return NULL;
7816 nparam = space->nparam;
7817 dim = space->n_out;
7818 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7819 if (!bset)
7820 return NULL;
7821 for (i = 0; i < dim; ++i) {
7822 int k = isl_basic_set_alloc_inequality(bset);
7823 if (k < 0)
7824 goto error;
7825 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7826 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7828 return bset;
7829 error:
7830 isl_basic_set_free(bset);
7831 return NULL;
7834 /* Construct the half-space x_pos >= 0.
7836 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7837 int pos)
7839 int k;
7840 isl_basic_set *nonneg;
7842 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7843 k = isl_basic_set_alloc_inequality(nonneg);
7844 if (k < 0)
7845 goto error;
7846 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7847 isl_int_set_si(nonneg->ineq[k][pos], 1);
7849 return isl_basic_set_finalize(nonneg);
7850 error:
7851 isl_basic_set_free(nonneg);
7852 return NULL;
7855 /* Construct the half-space x_pos <= -1.
7857 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7859 int k;
7860 isl_basic_set *neg;
7862 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7863 k = isl_basic_set_alloc_inequality(neg);
7864 if (k < 0)
7865 goto error;
7866 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7867 isl_int_set_si(neg->ineq[k][0], -1);
7868 isl_int_set_si(neg->ineq[k][pos], -1);
7870 return isl_basic_set_finalize(neg);
7871 error:
7872 isl_basic_set_free(neg);
7873 return NULL;
7876 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7877 enum isl_dim_type type, unsigned first, unsigned n)
7879 int i;
7880 unsigned offset;
7881 isl_basic_set *nonneg;
7882 isl_basic_set *neg;
7884 if (!set)
7885 return NULL;
7886 if (n == 0)
7887 return set;
7889 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7891 offset = pos(set->dim, type);
7892 for (i = 0; i < n; ++i) {
7893 nonneg = nonneg_halfspace(isl_set_get_space(set),
7894 offset + first + i);
7895 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7897 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7900 return set;
7901 error:
7902 isl_set_free(set);
7903 return NULL;
7906 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7907 int len,
7908 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7909 void *user)
7911 isl_set *half;
7913 if (!set)
7914 return isl_stat_error;
7915 if (isl_set_plain_is_empty(set)) {
7916 isl_set_free(set);
7917 return isl_stat_ok;
7919 if (first == len)
7920 return fn(set, signs, user);
7922 signs[first] = 1;
7923 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7924 1 + first));
7925 half = isl_set_intersect(half, isl_set_copy(set));
7926 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7927 goto error;
7929 signs[first] = -1;
7930 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7931 1 + first));
7932 half = isl_set_intersect(half, set);
7933 return foreach_orthant(half, signs, first + 1, len, fn, user);
7934 error:
7935 isl_set_free(set);
7936 return isl_stat_error;
7939 /* Call "fn" on the intersections of "set" with each of the orthants
7940 * (except for obviously empty intersections). The orthant is identified
7941 * by the signs array, with each entry having value 1 or -1 according
7942 * to the sign of the corresponding variable.
7944 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
7945 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7946 void *user)
7948 unsigned nparam;
7949 unsigned nvar;
7950 int *signs;
7951 isl_stat r;
7953 if (!set)
7954 return isl_stat_error;
7955 if (isl_set_plain_is_empty(set))
7956 return isl_stat_ok;
7958 nparam = isl_set_dim(set, isl_dim_param);
7959 nvar = isl_set_dim(set, isl_dim_set);
7961 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7963 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7964 fn, user);
7966 free(signs);
7968 return r;
7971 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7973 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7976 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7977 __isl_keep isl_basic_map *bmap2)
7979 int is_subset;
7980 struct isl_map *map1;
7981 struct isl_map *map2;
7983 if (!bmap1 || !bmap2)
7984 return isl_bool_error;
7986 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7987 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7989 is_subset = isl_map_is_subset(map1, map2);
7991 isl_map_free(map1);
7992 isl_map_free(map2);
7994 return is_subset;
7997 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7998 __isl_keep isl_basic_set *bset2)
8000 return isl_basic_map_is_subset(bset1, bset2);
8003 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8004 __isl_keep isl_basic_map *bmap2)
8006 isl_bool is_subset;
8008 if (!bmap1 || !bmap2)
8009 return isl_bool_error;
8010 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8011 if (is_subset != isl_bool_true)
8012 return is_subset;
8013 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8014 return is_subset;
8017 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8018 __isl_keep isl_basic_set *bset2)
8020 return isl_basic_map_is_equal(
8021 bset_to_bmap(bset1), bset_to_bmap(bset2));
8024 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8026 int i;
8027 int is_empty;
8029 if (!map)
8030 return isl_bool_error;
8031 for (i = 0; i < map->n; ++i) {
8032 is_empty = isl_basic_map_is_empty(map->p[i]);
8033 if (is_empty < 0)
8034 return isl_bool_error;
8035 if (!is_empty)
8036 return isl_bool_false;
8038 return isl_bool_true;
8041 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8043 return map ? map->n == 0 : isl_bool_error;
8046 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8048 return set ? set->n == 0 : isl_bool_error;
8051 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8053 return isl_map_is_empty(set_to_map(set));
8056 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8057 __isl_keep isl_map *map2)
8059 if (!map1 || !map2)
8060 return isl_bool_error;
8062 return isl_space_is_equal(map1->dim, map2->dim);
8065 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8066 __isl_keep isl_set *set2)
8068 if (!set1 || !set2)
8069 return isl_bool_error;
8071 return isl_space_is_equal(set1->dim, set2->dim);
8074 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8076 isl_bool is_subset;
8078 if (!map1 || !map2)
8079 return isl_bool_error;
8080 is_subset = isl_map_is_subset(map1, map2);
8081 if (is_subset != isl_bool_true)
8082 return is_subset;
8083 is_subset = isl_map_is_subset(map2, map1);
8084 return is_subset;
8087 /* Is "map1" equal to "map2"?
8089 * First check if they are obviously equal.
8090 * If not, then perform a more detailed analysis.
8092 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8094 isl_bool equal;
8096 equal = isl_map_plain_is_equal(map1, map2);
8097 if (equal < 0 || equal)
8098 return equal;
8099 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8102 isl_bool isl_basic_map_is_strict_subset(
8103 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8105 isl_bool is_subset;
8107 if (!bmap1 || !bmap2)
8108 return isl_bool_error;
8109 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8110 if (is_subset != isl_bool_true)
8111 return is_subset;
8112 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8113 if (is_subset == isl_bool_error)
8114 return is_subset;
8115 return !is_subset;
8118 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8119 __isl_keep isl_map *map2)
8121 isl_bool is_subset;
8123 if (!map1 || !map2)
8124 return isl_bool_error;
8125 is_subset = isl_map_is_subset(map1, map2);
8126 if (is_subset != isl_bool_true)
8127 return is_subset;
8128 is_subset = isl_map_is_subset(map2, map1);
8129 if (is_subset == isl_bool_error)
8130 return is_subset;
8131 return !is_subset;
8134 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8135 __isl_keep isl_set *set2)
8137 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8140 /* Is "bmap" obviously equal to the universe with the same space?
8142 * That is, does it not have any constraints?
8144 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8146 if (!bmap)
8147 return isl_bool_error;
8148 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8151 /* Is "bset" obviously equal to the universe with the same space?
8153 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8155 return isl_basic_map_plain_is_universe(bset);
8158 /* If "c" does not involve any existentially quantified variables,
8159 * then set *univ to false and abort
8161 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8163 isl_bool *univ = user;
8164 unsigned n;
8166 n = isl_constraint_dim(c, isl_dim_div);
8167 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8168 isl_constraint_free(c);
8169 if (*univ < 0 || !*univ)
8170 return isl_stat_error;
8171 return isl_stat_ok;
8174 /* Is "bmap" equal to the universe with the same space?
8176 * First check if it is obviously equal to the universe.
8177 * If not and if there are any constraints not involving
8178 * existentially quantified variables, then it is certainly
8179 * not equal to the universe.
8180 * Otherwise, check if the universe is a subset of "bmap".
8182 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8184 isl_bool univ;
8185 isl_basic_map *test;
8187 univ = isl_basic_map_plain_is_universe(bmap);
8188 if (univ < 0 || univ)
8189 return univ;
8190 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8191 return isl_bool_false;
8192 univ = isl_bool_true;
8193 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8194 univ)
8195 return isl_bool_error;
8196 if (univ < 0 || !univ)
8197 return univ;
8198 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8199 univ = isl_basic_map_is_subset(test, bmap);
8200 isl_basic_map_free(test);
8201 return univ;
8204 /* Is "bset" equal to the universe with the same space?
8206 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8208 return isl_basic_map_is_universe(bset);
8211 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8213 int i;
8215 if (!map)
8216 return isl_bool_error;
8218 for (i = 0; i < map->n; ++i) {
8219 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8220 if (r < 0 || r)
8221 return r;
8224 return isl_bool_false;
8227 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8229 return isl_map_plain_is_universe(set_to_map(set));
8232 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8234 struct isl_basic_set *bset = NULL;
8235 struct isl_vec *sample = NULL;
8236 isl_bool empty, non_empty;
8238 if (!bmap)
8239 return isl_bool_error;
8241 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8242 return isl_bool_true;
8244 if (isl_basic_map_plain_is_universe(bmap))
8245 return isl_bool_false;
8247 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8248 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8249 copy = isl_basic_map_remove_redundancies(copy);
8250 empty = isl_basic_map_plain_is_empty(copy);
8251 isl_basic_map_free(copy);
8252 return empty;
8255 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8256 if (non_empty < 0)
8257 return isl_bool_error;
8258 if (non_empty)
8259 return isl_bool_false;
8260 isl_vec_free(bmap->sample);
8261 bmap->sample = NULL;
8262 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8263 if (!bset)
8264 return isl_bool_error;
8265 sample = isl_basic_set_sample_vec(bset);
8266 if (!sample)
8267 return isl_bool_error;
8268 empty = sample->size == 0;
8269 isl_vec_free(bmap->sample);
8270 bmap->sample = sample;
8271 if (empty)
8272 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8274 return empty;
8277 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8279 if (!bmap)
8280 return isl_bool_error;
8281 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8284 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8286 if (!bset)
8287 return isl_bool_error;
8288 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8291 /* Is "bmap" known to be non-empty?
8293 * That is, is the cached sample still valid?
8295 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8297 unsigned total;
8299 if (!bmap)
8300 return isl_bool_error;
8301 if (!bmap->sample)
8302 return isl_bool_false;
8303 total = 1 + isl_basic_map_total_dim(bmap);
8304 if (bmap->sample->size != total)
8305 return isl_bool_false;
8306 return isl_basic_map_contains(bmap, bmap->sample);
8309 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8311 return isl_basic_map_is_empty(bset_to_bmap(bset));
8314 struct isl_map *isl_basic_map_union(
8315 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8317 struct isl_map *map;
8318 if (!bmap1 || !bmap2)
8319 goto error;
8321 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8323 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8324 if (!map)
8325 goto error;
8326 map = isl_map_add_basic_map(map, bmap1);
8327 map = isl_map_add_basic_map(map, bmap2);
8328 return map;
8329 error:
8330 isl_basic_map_free(bmap1);
8331 isl_basic_map_free(bmap2);
8332 return NULL;
8335 struct isl_set *isl_basic_set_union(
8336 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8338 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8339 bset_to_bmap(bset2)));
8342 /* Order divs such that any div only depends on previous divs */
8343 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8345 int i;
8346 unsigned off;
8348 if (!bmap)
8349 return NULL;
8351 off = isl_space_dim(bmap->dim, isl_dim_all);
8353 for (i = 0; i < bmap->n_div; ++i) {
8354 int pos;
8355 if (isl_int_is_zero(bmap->div[i][0]))
8356 continue;
8357 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8358 bmap->n_div-i);
8359 if (pos == -1)
8360 continue;
8361 if (pos == 0)
8362 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8363 "integer division depends on itself",
8364 return isl_basic_map_free(bmap));
8365 isl_basic_map_swap_div(bmap, i, i + pos);
8366 --i;
8368 return bmap;
8371 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8373 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8376 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8378 int i;
8380 if (!map)
8381 return 0;
8383 for (i = 0; i < map->n; ++i) {
8384 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8385 if (!map->p[i])
8386 goto error;
8389 return map;
8390 error:
8391 isl_map_free(map);
8392 return NULL;
8395 /* Sort the local variables of "bset".
8397 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8398 __isl_take isl_basic_set *bset)
8400 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8403 /* Apply the expansion computed by isl_merge_divs.
8404 * The expansion itself is given by "exp" while the resulting
8405 * list of divs is given by "div".
8407 * Move the integer divisions of "bmap" into the right position
8408 * according to "exp" and then introduce the additional integer
8409 * divisions, adding div constraints.
8410 * The moving should be done first to avoid moving coefficients
8411 * in the definitions of the extra integer divisions.
8413 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8414 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8416 int i, j;
8417 int n_div;
8419 bmap = isl_basic_map_cow(bmap);
8420 if (!bmap || !div)
8421 goto error;
8423 if (div->n_row < bmap->n_div)
8424 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8425 "not an expansion", goto error);
8427 n_div = bmap->n_div;
8428 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8429 div->n_row - n_div, 0,
8430 2 * (div->n_row - n_div));
8432 for (i = n_div; i < div->n_row; ++i)
8433 if (isl_basic_map_alloc_div(bmap) < 0)
8434 goto error;
8436 for (j = n_div - 1; j >= 0; --j) {
8437 if (exp[j] == j)
8438 break;
8439 isl_basic_map_swap_div(bmap, j, exp[j]);
8441 j = 0;
8442 for (i = 0; i < div->n_row; ++i) {
8443 if (j < n_div && exp[j] == i) {
8444 j++;
8445 } else {
8446 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8447 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8448 continue;
8449 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8450 goto error;
8454 isl_mat_free(div);
8455 return bmap;
8456 error:
8457 isl_basic_map_free(bmap);
8458 isl_mat_free(div);
8459 return NULL;
8462 /* Apply the expansion computed by isl_merge_divs.
8463 * The expansion itself is given by "exp" while the resulting
8464 * list of divs is given by "div".
8466 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8467 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8469 return isl_basic_map_expand_divs(bset, div, exp);
8472 /* Look for a div in dst that corresponds to the div "div" in src.
8473 * The divs before "div" in src and dst are assumed to be the same.
8475 * Returns -1 if no corresponding div was found and the position
8476 * of the corresponding div in dst otherwise.
8478 static int find_div(struct isl_basic_map *dst,
8479 struct isl_basic_map *src, unsigned div)
8481 int i;
8483 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8485 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8486 for (i = div; i < dst->n_div; ++i)
8487 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8488 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8489 dst->n_div - div) == -1)
8490 return i;
8491 return -1;
8494 /* Align the divs of "dst" to those of "src", adding divs from "src"
8495 * if needed. That is, make sure that the first src->n_div divs
8496 * of the result are equal to those of src.
8498 * The result is not finalized as by design it will have redundant
8499 * divs if any divs from "src" were copied.
8501 __isl_give isl_basic_map *isl_basic_map_align_divs(
8502 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8504 int i;
8505 int known, extended;
8506 unsigned total;
8508 if (!dst || !src)
8509 return isl_basic_map_free(dst);
8511 if (src->n_div == 0)
8512 return dst;
8514 known = isl_basic_map_divs_known(src);
8515 if (known < 0)
8516 return isl_basic_map_free(dst);
8517 if (!known)
8518 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8519 "some src divs are unknown",
8520 return isl_basic_map_free(dst));
8522 src = isl_basic_map_order_divs(src);
8524 extended = 0;
8525 total = isl_space_dim(src->dim, isl_dim_all);
8526 for (i = 0; i < src->n_div; ++i) {
8527 int j = find_div(dst, src, i);
8528 if (j < 0) {
8529 if (!extended) {
8530 int extra = src->n_div - i;
8531 dst = isl_basic_map_cow(dst);
8532 if (!dst)
8533 return NULL;
8534 dst = isl_basic_map_extend_space(dst,
8535 isl_space_copy(dst->dim),
8536 extra, 0, 2 * extra);
8537 extended = 1;
8539 j = isl_basic_map_alloc_div(dst);
8540 if (j < 0)
8541 return isl_basic_map_free(dst);
8542 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8543 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8544 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8545 return isl_basic_map_free(dst);
8547 if (j != i)
8548 isl_basic_map_swap_div(dst, i, j);
8550 return dst;
8553 struct isl_basic_set *isl_basic_set_align_divs(
8554 struct isl_basic_set *dst, struct isl_basic_set *src)
8556 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8557 bset_to_bmap(src)));
8560 struct isl_map *isl_map_align_divs(struct isl_map *map)
8562 int i;
8564 if (!map)
8565 return NULL;
8566 if (map->n == 0)
8567 return map;
8568 map = isl_map_compute_divs(map);
8569 map = isl_map_cow(map);
8570 if (!map)
8571 return NULL;
8573 for (i = 1; i < map->n; ++i)
8574 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8575 for (i = 1; i < map->n; ++i) {
8576 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8577 if (!map->p[i])
8578 return isl_map_free(map);
8581 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8582 return map;
8585 struct isl_set *isl_set_align_divs(struct isl_set *set)
8587 return set_from_map(isl_map_align_divs(set_to_map(set)));
8590 /* Align the divs of the basic maps in "map" to those
8591 * of the basic maps in "list", as well as to the other basic maps in "map".
8592 * The elements in "list" are assumed to have known divs.
8594 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8595 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8597 int i, n;
8599 map = isl_map_compute_divs(map);
8600 map = isl_map_cow(map);
8601 if (!map || !list)
8602 return isl_map_free(map);
8603 if (map->n == 0)
8604 return map;
8606 n = isl_basic_map_list_n_basic_map(list);
8607 for (i = 0; i < n; ++i) {
8608 isl_basic_map *bmap;
8610 bmap = isl_basic_map_list_get_basic_map(list, i);
8611 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8612 isl_basic_map_free(bmap);
8614 if (!map->p[0])
8615 return isl_map_free(map);
8617 return isl_map_align_divs(map);
8620 /* Align the divs of each element of "list" to those of "bmap".
8621 * Both "bmap" and the elements of "list" are assumed to have known divs.
8623 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8624 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8626 int i, n;
8628 if (!list || !bmap)
8629 return isl_basic_map_list_free(list);
8631 n = isl_basic_map_list_n_basic_map(list);
8632 for (i = 0; i < n; ++i) {
8633 isl_basic_map *bmap_i;
8635 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8636 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8637 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8640 return list;
8643 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8644 __isl_take isl_map *map)
8646 isl_bool ok;
8648 ok = isl_map_compatible_domain(map, set);
8649 if (ok < 0)
8650 goto error;
8651 if (!ok)
8652 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8653 "incompatible spaces", goto error);
8654 map = isl_map_intersect_domain(map, set);
8655 set = isl_map_range(map);
8656 return set;
8657 error:
8658 isl_set_free(set);
8659 isl_map_free(map);
8660 return NULL;
8663 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8664 __isl_take isl_map *map)
8666 return isl_map_align_params_map_map_and(set, map, &set_apply);
8669 /* There is no need to cow as removing empty parts doesn't change
8670 * the meaning of the set.
8672 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8674 int i;
8676 if (!map)
8677 return NULL;
8679 for (i = map->n - 1; i >= 0; --i)
8680 remove_if_empty(map, i);
8682 return map;
8685 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8687 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8690 static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
8692 struct isl_basic_map *bmap;
8693 if (!map || map->n == 0)
8694 return NULL;
8695 bmap = map->p[map->n-1];
8696 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8697 return isl_basic_map_copy(bmap);
8700 __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
8702 return map_copy_basic_map(map);
8705 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8707 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8710 static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
8711 __isl_keep isl_basic_map *bmap)
8713 int i;
8715 if (!map || !bmap)
8716 goto error;
8717 for (i = map->n-1; i >= 0; --i) {
8718 if (map->p[i] != bmap)
8719 continue;
8720 map = isl_map_cow(map);
8721 if (!map)
8722 goto error;
8723 isl_basic_map_free(map->p[i]);
8724 if (i != map->n-1) {
8725 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8726 map->p[i] = map->p[map->n-1];
8728 map->n--;
8729 return map;
8731 return map;
8732 error:
8733 isl_map_free(map);
8734 return NULL;
8737 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8738 __isl_keep isl_basic_map *bmap)
8740 return map_drop_basic_map(map, bmap);
8743 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8744 struct isl_basic_set *bset)
8746 return set_from_map(map_drop_basic_map(set_to_map(set),
8747 bset_to_bmap(bset)));
8750 /* Given two basic sets bset1 and bset2, compute the maximal difference
8751 * between the values of dimension pos in bset1 and those in bset2
8752 * for any common value of the parameters and dimensions preceding pos.
8754 static enum isl_lp_result basic_set_maximal_difference_at(
8755 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8756 int pos, isl_int *opt)
8758 isl_basic_map *bmap1;
8759 isl_basic_map *bmap2;
8760 struct isl_ctx *ctx;
8761 struct isl_vec *obj;
8762 unsigned total;
8763 unsigned nparam;
8764 unsigned dim1;
8765 enum isl_lp_result res;
8767 if (!bset1 || !bset2)
8768 return isl_lp_error;
8770 nparam = isl_basic_set_n_param(bset1);
8771 dim1 = isl_basic_set_n_dim(bset1);
8773 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8774 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8775 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8776 isl_dim_out, 0, pos);
8777 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8778 isl_dim_out, 0, pos);
8779 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8780 if (!bmap1)
8781 return isl_lp_error;
8783 total = isl_basic_map_total_dim(bmap1);
8784 ctx = bmap1->ctx;
8785 obj = isl_vec_alloc(ctx, 1 + total);
8786 if (!obj)
8787 goto error;
8788 isl_seq_clr(obj->block.data, 1 + total);
8789 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8790 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8791 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8792 opt, NULL, NULL);
8793 isl_basic_map_free(bmap1);
8794 isl_vec_free(obj);
8795 return res;
8796 error:
8797 isl_basic_map_free(bmap1);
8798 return isl_lp_error;
8801 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8802 * for any common value of the parameters and dimensions preceding pos
8803 * in both basic sets, the values of dimension pos in bset1 are
8804 * smaller or larger than those in bset2.
8806 * Returns
8807 * 1 if bset1 follows bset2
8808 * -1 if bset1 precedes bset2
8809 * 0 if bset1 and bset2 are incomparable
8810 * -2 if some error occurred.
8812 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8813 struct isl_basic_set *bset2, int pos)
8815 isl_int opt;
8816 enum isl_lp_result res;
8817 int cmp;
8819 isl_int_init(opt);
8821 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8823 if (res == isl_lp_empty)
8824 cmp = 0;
8825 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8826 res == isl_lp_unbounded)
8827 cmp = 1;
8828 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8829 cmp = -1;
8830 else
8831 cmp = -2;
8833 isl_int_clear(opt);
8834 return cmp;
8837 /* Given two basic sets bset1 and bset2, check whether
8838 * for any common value of the parameters and dimensions preceding pos
8839 * there is a value of dimension pos in bset1 that is larger
8840 * than a value of the same dimension in bset2.
8842 * Return
8843 * 1 if there exists such a pair
8844 * 0 if there is no such pair, but there is a pair of equal values
8845 * -1 otherwise
8846 * -2 if some error occurred.
8848 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8849 __isl_keep isl_basic_set *bset2, int pos)
8851 isl_int opt;
8852 enum isl_lp_result res;
8853 int cmp;
8855 isl_int_init(opt);
8857 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8859 if (res == isl_lp_empty)
8860 cmp = -1;
8861 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8862 res == isl_lp_unbounded)
8863 cmp = 1;
8864 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8865 cmp = -1;
8866 else if (res == isl_lp_ok)
8867 cmp = 0;
8868 else
8869 cmp = -2;
8871 isl_int_clear(opt);
8872 return cmp;
8875 /* Given two sets set1 and set2, check whether
8876 * for any common value of the parameters and dimensions preceding pos
8877 * there is a value of dimension pos in set1 that is larger
8878 * than a value of the same dimension in set2.
8880 * Return
8881 * 1 if there exists such a pair
8882 * 0 if there is no such pair, but there is a pair of equal values
8883 * -1 otherwise
8884 * -2 if some error occurred.
8886 int isl_set_follows_at(__isl_keep isl_set *set1,
8887 __isl_keep isl_set *set2, int pos)
8889 int i, j;
8890 int follows = -1;
8892 if (!set1 || !set2)
8893 return -2;
8895 for (i = 0; i < set1->n; ++i)
8896 for (j = 0; j < set2->n; ++j) {
8897 int f;
8898 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8899 if (f == 1 || f == -2)
8900 return f;
8901 if (f > follows)
8902 follows = f;
8905 return follows;
8908 static isl_bool isl_basic_map_plain_has_fixed_var(
8909 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
8911 int i;
8912 int d;
8913 unsigned total;
8915 if (!bmap)
8916 return isl_bool_error;
8917 total = isl_basic_map_total_dim(bmap);
8918 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8919 for (; d+1 > pos; --d)
8920 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8921 break;
8922 if (d != pos)
8923 continue;
8924 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8925 return isl_bool_false;
8926 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8927 return isl_bool_false;
8928 if (!isl_int_is_one(bmap->eq[i][1+d]))
8929 return isl_bool_false;
8930 if (val)
8931 isl_int_neg(*val, bmap->eq[i][0]);
8932 return isl_bool_true;
8934 return isl_bool_false;
8937 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8938 unsigned pos, isl_int *val)
8940 int i;
8941 isl_int v;
8942 isl_int tmp;
8943 isl_bool fixed;
8945 if (!map)
8946 return isl_bool_error;
8947 if (map->n == 0)
8948 return isl_bool_false;
8949 if (map->n == 1)
8950 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8951 isl_int_init(v);
8952 isl_int_init(tmp);
8953 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8954 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
8955 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8956 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
8957 fixed = isl_bool_false;
8959 if (val)
8960 isl_int_set(*val, v);
8961 isl_int_clear(tmp);
8962 isl_int_clear(v);
8963 return fixed;
8966 static isl_bool isl_basic_set_plain_has_fixed_var(
8967 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
8969 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8970 pos, val);
8973 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8974 enum isl_dim_type type, unsigned pos, isl_int *val)
8976 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
8977 return isl_bool_error;
8978 return isl_basic_map_plain_has_fixed_var(bmap,
8979 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8982 /* If "bmap" obviously lies on a hyperplane where the given dimension
8983 * has a fixed value, then return that value.
8984 * Otherwise return NaN.
8986 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8987 __isl_keep isl_basic_map *bmap,
8988 enum isl_dim_type type, unsigned pos)
8990 isl_ctx *ctx;
8991 isl_val *v;
8992 isl_bool fixed;
8994 if (!bmap)
8995 return NULL;
8996 ctx = isl_basic_map_get_ctx(bmap);
8997 v = isl_val_alloc(ctx);
8998 if (!v)
8999 return NULL;
9000 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9001 if (fixed < 0)
9002 return isl_val_free(v);
9003 if (fixed) {
9004 isl_int_set_si(v->d, 1);
9005 return v;
9007 isl_val_free(v);
9008 return isl_val_nan(ctx);
9011 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9012 enum isl_dim_type type, unsigned pos, isl_int *val)
9014 if (pos >= isl_map_dim(map, type))
9015 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9016 "position out of bounds", return isl_bool_error);
9017 return isl_map_plain_has_fixed_var(map,
9018 map_offset(map, type) - 1 + pos, val);
9021 /* If "map" obviously lies on a hyperplane where the given dimension
9022 * has a fixed value, then return that value.
9023 * Otherwise return NaN.
9025 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9026 enum isl_dim_type type, unsigned pos)
9028 isl_ctx *ctx;
9029 isl_val *v;
9030 isl_bool fixed;
9032 if (!map)
9033 return NULL;
9034 ctx = isl_map_get_ctx(map);
9035 v = isl_val_alloc(ctx);
9036 if (!v)
9037 return NULL;
9038 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9039 if (fixed < 0)
9040 return isl_val_free(v);
9041 if (fixed) {
9042 isl_int_set_si(v->d, 1);
9043 return v;
9045 isl_val_free(v);
9046 return isl_val_nan(ctx);
9049 /* If "set" obviously lies on a hyperplane where the given dimension
9050 * has a fixed value, then return that value.
9051 * Otherwise return NaN.
9053 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9054 enum isl_dim_type type, unsigned pos)
9056 return isl_map_plain_get_val_if_fixed(set, type, pos);
9059 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
9060 enum isl_dim_type type, unsigned pos, isl_int *val)
9062 return isl_map_plain_is_fixed(set, type, pos, val);
9065 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9066 * then return this fixed value in *val.
9068 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9069 unsigned dim, isl_int *val)
9071 return isl_basic_set_plain_has_fixed_var(bset,
9072 isl_basic_set_n_param(bset) + dim, val);
9075 /* Return -1 if the constraint "c1" should be sorted before "c2"
9076 * and 1 if it should be sorted after "c2".
9077 * Return 0 if the two constraints are the same (up to the constant term).
9079 * In particular, if a constraint involves later variables than another
9080 * then it is sorted after this other constraint.
9081 * uset_gist depends on constraints without existentially quantified
9082 * variables sorting first.
9084 * For constraints that have the same latest variable, those
9085 * with the same coefficient for this latest variable (first in absolute value
9086 * and then in actual value) are grouped together.
9087 * This is useful for detecting pairs of constraints that can
9088 * be chained in their printed representation.
9090 * Finally, within a group, constraints are sorted according to
9091 * their coefficients (excluding the constant term).
9093 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9095 isl_int **c1 = (isl_int **) p1;
9096 isl_int **c2 = (isl_int **) p2;
9097 int l1, l2;
9098 unsigned size = *(unsigned *) arg;
9099 int cmp;
9101 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9102 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9104 if (l1 != l2)
9105 return l1 - l2;
9107 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9108 if (cmp != 0)
9109 return cmp;
9110 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9111 if (cmp != 0)
9112 return -cmp;
9114 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9117 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9118 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9119 * and 0 if the two constraints are the same (up to the constant term).
9121 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9122 isl_int *c1, isl_int *c2)
9124 unsigned total;
9126 if (!bmap)
9127 return -2;
9128 total = isl_basic_map_total_dim(bmap);
9129 return sort_constraint_cmp(&c1, &c2, &total);
9132 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9133 __isl_take isl_basic_map *bmap)
9135 unsigned total;
9137 if (!bmap)
9138 return NULL;
9139 if (bmap->n_ineq == 0)
9140 return bmap;
9141 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9142 return bmap;
9143 total = isl_basic_map_total_dim(bmap);
9144 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9145 &sort_constraint_cmp, &total) < 0)
9146 return isl_basic_map_free(bmap);
9147 return bmap;
9150 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9151 __isl_take isl_basic_set *bset)
9153 isl_basic_map *bmap = bset_to_bmap(bset);
9154 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9157 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9159 if (!bmap)
9160 return NULL;
9161 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9162 return bmap;
9163 bmap = isl_basic_map_remove_redundancies(bmap);
9164 bmap = isl_basic_map_sort_constraints(bmap);
9165 if (bmap)
9166 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9167 return bmap;
9170 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9172 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
9175 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9176 __isl_keep isl_basic_map *bmap2)
9178 int i, cmp;
9179 unsigned total;
9181 if (!bmap1 || !bmap2)
9182 return -1;
9184 if (bmap1 == bmap2)
9185 return 0;
9186 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9187 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9188 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9189 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9190 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9191 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9192 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9193 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9194 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9195 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9196 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9197 return 0;
9198 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9199 return 1;
9200 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9201 return -1;
9202 if (bmap1->n_eq != bmap2->n_eq)
9203 return bmap1->n_eq - bmap2->n_eq;
9204 if (bmap1->n_ineq != bmap2->n_ineq)
9205 return bmap1->n_ineq - bmap2->n_ineq;
9206 if (bmap1->n_div != bmap2->n_div)
9207 return bmap1->n_div - bmap2->n_div;
9208 total = isl_basic_map_total_dim(bmap1);
9209 for (i = 0; i < bmap1->n_eq; ++i) {
9210 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9211 if (cmp)
9212 return cmp;
9214 for (i = 0; i < bmap1->n_ineq; ++i) {
9215 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9216 if (cmp)
9217 return cmp;
9219 for (i = 0; i < bmap1->n_div; ++i) {
9220 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9221 if (cmp)
9222 return cmp;
9224 return 0;
9227 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9228 __isl_keep isl_basic_set *bset2)
9230 return isl_basic_map_plain_cmp(bset1, bset2);
9233 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9235 int i, cmp;
9237 if (set1 == set2)
9238 return 0;
9239 if (set1->n != set2->n)
9240 return set1->n - set2->n;
9242 for (i = 0; i < set1->n; ++i) {
9243 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9244 if (cmp)
9245 return cmp;
9248 return 0;
9251 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9252 __isl_keep isl_basic_map *bmap2)
9254 if (!bmap1 || !bmap2)
9255 return isl_bool_error;
9256 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9259 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9260 __isl_keep isl_basic_set *bset2)
9262 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9263 bset_to_bmap(bset2));
9266 static int qsort_bmap_cmp(const void *p1, const void *p2)
9268 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9269 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9271 return isl_basic_map_plain_cmp(bmap1, bmap2);
9274 /* Sort the basic maps of "map" and remove duplicate basic maps.
9276 * While removing basic maps, we make sure that the basic maps remain
9277 * sorted because isl_map_normalize expects the basic maps of the result
9278 * to be sorted.
9280 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9282 int i, j;
9284 map = isl_map_remove_empty_parts(map);
9285 if (!map)
9286 return NULL;
9287 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9288 for (i = map->n - 1; i >= 1; --i) {
9289 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9290 continue;
9291 isl_basic_map_free(map->p[i-1]);
9292 for (j = i; j < map->n; ++j)
9293 map->p[j - 1] = map->p[j];
9294 map->n--;
9297 return map;
9300 /* Remove obvious duplicates among the basic maps of "map".
9302 * Unlike isl_map_normalize, this function does not remove redundant
9303 * constraints and only removes duplicates that have exactly the same
9304 * constraints in the input. It does sort the constraints and
9305 * the basic maps to ease the detection of duplicates.
9307 * If "map" has already been normalized or if the basic maps are
9308 * disjoint, then there can be no duplicates.
9310 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9312 int i;
9313 isl_basic_map *bmap;
9315 if (!map)
9316 return NULL;
9317 if (map->n <= 1)
9318 return map;
9319 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9320 return map;
9321 for (i = 0; i < map->n; ++i) {
9322 bmap = isl_basic_map_copy(map->p[i]);
9323 bmap = isl_basic_map_sort_constraints(bmap);
9324 if (!bmap)
9325 return isl_map_free(map);
9326 isl_basic_map_free(map->p[i]);
9327 map->p[i] = bmap;
9330 map = sort_and_remove_duplicates(map);
9331 return map;
9334 /* We normalize in place, but if anything goes wrong we need
9335 * to return NULL, so we need to make sure we don't change the
9336 * meaning of any possible other copies of map.
9338 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9340 int i;
9341 struct isl_basic_map *bmap;
9343 if (!map)
9344 return NULL;
9345 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9346 return map;
9347 for (i = 0; i < map->n; ++i) {
9348 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9349 if (!bmap)
9350 goto error;
9351 isl_basic_map_free(map->p[i]);
9352 map->p[i] = bmap;
9355 map = sort_and_remove_duplicates(map);
9356 if (map)
9357 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9358 return map;
9359 error:
9360 isl_map_free(map);
9361 return NULL;
9364 struct isl_set *isl_set_normalize(struct isl_set *set)
9366 return set_from_map(isl_map_normalize(set_to_map(set)));
9369 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9370 __isl_keep isl_map *map2)
9372 int i;
9373 isl_bool equal;
9375 if (!map1 || !map2)
9376 return isl_bool_error;
9378 if (map1 == map2)
9379 return isl_bool_true;
9380 if (!isl_space_is_equal(map1->dim, map2->dim))
9381 return isl_bool_false;
9383 map1 = isl_map_copy(map1);
9384 map2 = isl_map_copy(map2);
9385 map1 = isl_map_normalize(map1);
9386 map2 = isl_map_normalize(map2);
9387 if (!map1 || !map2)
9388 goto error;
9389 equal = map1->n == map2->n;
9390 for (i = 0; equal && i < map1->n; ++i) {
9391 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9392 if (equal < 0)
9393 goto error;
9395 isl_map_free(map1);
9396 isl_map_free(map2);
9397 return equal;
9398 error:
9399 isl_map_free(map1);
9400 isl_map_free(map2);
9401 return isl_bool_error;
9404 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9405 __isl_keep isl_set *set2)
9407 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9410 /* Return the basic maps in "map" as a list.
9412 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9413 __isl_keep isl_map *map)
9415 int i;
9416 isl_ctx *ctx;
9417 isl_basic_map_list *list;
9419 if (!map)
9420 return NULL;
9421 ctx = isl_map_get_ctx(map);
9422 list = isl_basic_map_list_alloc(ctx, map->n);
9424 for (i = 0; i < map->n; ++i) {
9425 isl_basic_map *bmap;
9427 bmap = isl_basic_map_copy(map->p[i]);
9428 list = isl_basic_map_list_add(list, bmap);
9431 return list;
9434 /* Return the intersection of the elements in the non-empty list "list".
9435 * All elements are assumed to live in the same space.
9437 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9438 __isl_take isl_basic_map_list *list)
9440 int i, n;
9441 isl_basic_map *bmap;
9443 if (!list)
9444 return NULL;
9445 n = isl_basic_map_list_n_basic_map(list);
9446 if (n < 1)
9447 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9448 "expecting non-empty list", goto error);
9450 bmap = isl_basic_map_list_get_basic_map(list, 0);
9451 for (i = 1; i < n; ++i) {
9452 isl_basic_map *bmap_i;
9454 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9455 bmap = isl_basic_map_intersect(bmap, bmap_i);
9458 isl_basic_map_list_free(list);
9459 return bmap;
9460 error:
9461 isl_basic_map_list_free(list);
9462 return NULL;
9465 /* Return the intersection of the elements in the non-empty list "list".
9466 * All elements are assumed to live in the same space.
9468 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9469 __isl_take isl_basic_set_list *list)
9471 return isl_basic_map_list_intersect(list);
9474 /* Return the union of the elements of "list".
9475 * The list is required to have at least one element.
9477 __isl_give isl_set *isl_basic_set_list_union(
9478 __isl_take isl_basic_set_list *list)
9480 int i, n;
9481 isl_space *space;
9482 isl_basic_set *bset;
9483 isl_set *set;
9485 if (!list)
9486 return NULL;
9487 n = isl_basic_set_list_n_basic_set(list);
9488 if (n < 1)
9489 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9490 "expecting non-empty list", goto error);
9492 bset = isl_basic_set_list_get_basic_set(list, 0);
9493 space = isl_basic_set_get_space(bset);
9494 isl_basic_set_free(bset);
9496 set = isl_set_alloc_space(space, n, 0);
9497 for (i = 0; i < n; ++i) {
9498 bset = isl_basic_set_list_get_basic_set(list, i);
9499 set = isl_set_add_basic_set(set, bset);
9502 isl_basic_set_list_free(list);
9503 return set;
9504 error:
9505 isl_basic_set_list_free(list);
9506 return NULL;
9509 /* Return the union of the elements in the non-empty list "list".
9510 * All elements are assumed to live in the same space.
9512 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9514 int i, n;
9515 isl_set *set;
9517 if (!list)
9518 return NULL;
9519 n = isl_set_list_n_set(list);
9520 if (n < 1)
9521 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9522 "expecting non-empty list", goto error);
9524 set = isl_set_list_get_set(list, 0);
9525 for (i = 1; i < n; ++i) {
9526 isl_set *set_i;
9528 set_i = isl_set_list_get_set(list, i);
9529 set = isl_set_union(set, set_i);
9532 isl_set_list_free(list);
9533 return set;
9534 error:
9535 isl_set_list_free(list);
9536 return NULL;
9539 /* Return the Cartesian product of the basic sets in list (in the given order).
9541 __isl_give isl_basic_set *isl_basic_set_list_product(
9542 __isl_take struct isl_basic_set_list *list)
9544 int i;
9545 unsigned dim;
9546 unsigned nparam;
9547 unsigned extra;
9548 unsigned n_eq;
9549 unsigned n_ineq;
9550 struct isl_basic_set *product = NULL;
9552 if (!list)
9553 goto error;
9554 isl_assert(list->ctx, list->n > 0, goto error);
9555 isl_assert(list->ctx, list->p[0], goto error);
9556 nparam = isl_basic_set_n_param(list->p[0]);
9557 dim = isl_basic_set_n_dim(list->p[0]);
9558 extra = list->p[0]->n_div;
9559 n_eq = list->p[0]->n_eq;
9560 n_ineq = list->p[0]->n_ineq;
9561 for (i = 1; i < list->n; ++i) {
9562 isl_assert(list->ctx, list->p[i], goto error);
9563 isl_assert(list->ctx,
9564 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9565 dim += isl_basic_set_n_dim(list->p[i]);
9566 extra += list->p[i]->n_div;
9567 n_eq += list->p[i]->n_eq;
9568 n_ineq += list->p[i]->n_ineq;
9570 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9571 n_eq, n_ineq);
9572 if (!product)
9573 goto error;
9574 dim = 0;
9575 for (i = 0; i < list->n; ++i) {
9576 isl_basic_set_add_constraints(product,
9577 isl_basic_set_copy(list->p[i]), dim);
9578 dim += isl_basic_set_n_dim(list->p[i]);
9580 isl_basic_set_list_free(list);
9581 return product;
9582 error:
9583 isl_basic_set_free(product);
9584 isl_basic_set_list_free(list);
9585 return NULL;
9588 struct isl_basic_map *isl_basic_map_product(
9589 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9591 isl_space *dim_result = NULL;
9592 struct isl_basic_map *bmap;
9593 unsigned in1, in2, out1, out2, nparam, total, pos;
9594 struct isl_dim_map *dim_map1, *dim_map2;
9596 if (!bmap1 || !bmap2)
9597 goto error;
9599 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9600 bmap2->dim, isl_dim_param), goto error);
9601 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9602 isl_space_copy(bmap2->dim));
9604 in1 = isl_basic_map_n_in(bmap1);
9605 in2 = isl_basic_map_n_in(bmap2);
9606 out1 = isl_basic_map_n_out(bmap1);
9607 out2 = isl_basic_map_n_out(bmap2);
9608 nparam = isl_basic_map_n_param(bmap1);
9610 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9611 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9612 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9613 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9614 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9615 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9616 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9617 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9618 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9619 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9620 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9622 bmap = isl_basic_map_alloc_space(dim_result,
9623 bmap1->n_div + bmap2->n_div,
9624 bmap1->n_eq + bmap2->n_eq,
9625 bmap1->n_ineq + bmap2->n_ineq);
9626 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9627 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9628 bmap = isl_basic_map_simplify(bmap);
9629 return isl_basic_map_finalize(bmap);
9630 error:
9631 isl_basic_map_free(bmap1);
9632 isl_basic_map_free(bmap2);
9633 return NULL;
9636 __isl_give isl_basic_map *isl_basic_map_flat_product(
9637 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9639 isl_basic_map *prod;
9641 prod = isl_basic_map_product(bmap1, bmap2);
9642 prod = isl_basic_map_flatten(prod);
9643 return prod;
9646 __isl_give isl_basic_set *isl_basic_set_flat_product(
9647 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9649 return isl_basic_map_flat_range_product(bset1, bset2);
9652 __isl_give isl_basic_map *isl_basic_map_domain_product(
9653 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9655 isl_space *space_result = NULL;
9656 isl_basic_map *bmap;
9657 unsigned in1, in2, out, nparam, total, pos;
9658 struct isl_dim_map *dim_map1, *dim_map2;
9660 if (!bmap1 || !bmap2)
9661 goto error;
9663 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9664 isl_space_copy(bmap2->dim));
9666 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9667 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9668 out = isl_basic_map_dim(bmap1, isl_dim_out);
9669 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9671 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9672 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9673 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9674 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9675 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9676 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9677 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9678 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9679 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9680 isl_dim_map_div(dim_map1, bmap1, pos += out);
9681 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9683 bmap = isl_basic_map_alloc_space(space_result,
9684 bmap1->n_div + bmap2->n_div,
9685 bmap1->n_eq + bmap2->n_eq,
9686 bmap1->n_ineq + bmap2->n_ineq);
9687 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9688 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9689 bmap = isl_basic_map_simplify(bmap);
9690 return isl_basic_map_finalize(bmap);
9691 error:
9692 isl_basic_map_free(bmap1);
9693 isl_basic_map_free(bmap2);
9694 return NULL;
9697 __isl_give isl_basic_map *isl_basic_map_range_product(
9698 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9700 isl_bool rational;
9701 isl_space *dim_result = NULL;
9702 isl_basic_map *bmap;
9703 unsigned in, out1, out2, nparam, total, pos;
9704 struct isl_dim_map *dim_map1, *dim_map2;
9706 rational = isl_basic_map_is_rational(bmap1);
9707 if (rational >= 0 && rational)
9708 rational = isl_basic_map_is_rational(bmap2);
9709 if (!bmap1 || !bmap2 || rational < 0)
9710 goto error;
9712 if (!isl_space_match(bmap1->dim, isl_dim_param,
9713 bmap2->dim, isl_dim_param))
9714 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9715 "parameters don't match", goto error);
9717 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9718 isl_space_copy(bmap2->dim));
9720 in = isl_basic_map_dim(bmap1, isl_dim_in);
9721 out1 = isl_basic_map_n_out(bmap1);
9722 out2 = isl_basic_map_n_out(bmap2);
9723 nparam = isl_basic_map_n_param(bmap1);
9725 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9726 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9727 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9728 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9729 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9730 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9731 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9732 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9733 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9734 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9735 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9737 bmap = isl_basic_map_alloc_space(dim_result,
9738 bmap1->n_div + bmap2->n_div,
9739 bmap1->n_eq + bmap2->n_eq,
9740 bmap1->n_ineq + bmap2->n_ineq);
9741 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9742 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9743 if (rational)
9744 bmap = isl_basic_map_set_rational(bmap);
9745 bmap = isl_basic_map_simplify(bmap);
9746 return isl_basic_map_finalize(bmap);
9747 error:
9748 isl_basic_map_free(bmap1);
9749 isl_basic_map_free(bmap2);
9750 return NULL;
9753 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9754 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9756 isl_basic_map *prod;
9758 prod = isl_basic_map_range_product(bmap1, bmap2);
9759 prod = isl_basic_map_flatten_range(prod);
9760 return prod;
9763 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9764 * and collect the results.
9765 * The result live in the space obtained by calling "space_product"
9766 * on the spaces of "map1" and "map2".
9767 * If "remove_duplicates" is set then the result may contain duplicates
9768 * (even if the inputs do not) and so we try and remove the obvious
9769 * duplicates.
9771 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9772 __isl_take isl_map *map2,
9773 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9774 __isl_take isl_space *right),
9775 __isl_give isl_basic_map *(*basic_map_product)(
9776 __isl_take isl_basic_map *left,
9777 __isl_take isl_basic_map *right),
9778 int remove_duplicates)
9780 unsigned flags = 0;
9781 struct isl_map *result;
9782 int i, j;
9784 if (!map1 || !map2)
9785 goto error;
9787 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9788 map2->dim, isl_dim_param), goto error);
9790 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9791 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9792 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9794 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9795 isl_space_copy(map2->dim)),
9796 map1->n * map2->n, flags);
9797 if (!result)
9798 goto error;
9799 for (i = 0; i < map1->n; ++i)
9800 for (j = 0; j < map2->n; ++j) {
9801 struct isl_basic_map *part;
9802 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9803 isl_basic_map_copy(map2->p[j]));
9804 if (isl_basic_map_is_empty(part))
9805 isl_basic_map_free(part);
9806 else
9807 result = isl_map_add_basic_map(result, part);
9808 if (!result)
9809 goto error;
9811 if (remove_duplicates)
9812 result = isl_map_remove_obvious_duplicates(result);
9813 isl_map_free(map1);
9814 isl_map_free(map2);
9815 return result;
9816 error:
9817 isl_map_free(map1);
9818 isl_map_free(map2);
9819 return NULL;
9822 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9824 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9825 __isl_take isl_map *map2)
9827 return map_product(map1, map2, &isl_space_product,
9828 &isl_basic_map_product, 0);
9831 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9832 __isl_take isl_map *map2)
9834 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9837 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9839 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9840 __isl_take isl_map *map2)
9842 isl_map *prod;
9844 prod = isl_map_product(map1, map2);
9845 prod = isl_map_flatten(prod);
9846 return prod;
9849 /* Given two set A and B, construct its Cartesian product A x B.
9851 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9853 return isl_map_range_product(set1, set2);
9856 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9857 __isl_take isl_set *set2)
9859 return isl_map_flat_range_product(set1, set2);
9862 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9864 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9865 __isl_take isl_map *map2)
9867 return map_product(map1, map2, &isl_space_domain_product,
9868 &isl_basic_map_domain_product, 1);
9871 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9873 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9874 __isl_take isl_map *map2)
9876 return map_product(map1, map2, &isl_space_range_product,
9877 &isl_basic_map_range_product, 1);
9880 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9881 __isl_take isl_map *map2)
9883 return isl_map_align_params_map_map_and(map1, map2,
9884 &map_domain_product_aligned);
9887 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9888 __isl_take isl_map *map2)
9890 return isl_map_align_params_map_map_and(map1, map2,
9891 &map_range_product_aligned);
9894 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9896 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9898 isl_space *space;
9899 int total1, keep1, total2, keep2;
9901 if (!map)
9902 return NULL;
9903 if (!isl_space_domain_is_wrapping(map->dim) ||
9904 !isl_space_range_is_wrapping(map->dim))
9905 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9906 "not a product", return isl_map_free(map));
9908 space = isl_map_get_space(map);
9909 total1 = isl_space_dim(space, isl_dim_in);
9910 total2 = isl_space_dim(space, isl_dim_out);
9911 space = isl_space_factor_domain(space);
9912 keep1 = isl_space_dim(space, isl_dim_in);
9913 keep2 = isl_space_dim(space, isl_dim_out);
9914 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9915 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9916 map = isl_map_reset_space(map, space);
9918 return map;
9921 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9923 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9925 isl_space *space;
9926 int total1, keep1, total2, keep2;
9928 if (!map)
9929 return NULL;
9930 if (!isl_space_domain_is_wrapping(map->dim) ||
9931 !isl_space_range_is_wrapping(map->dim))
9932 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9933 "not a product", return isl_map_free(map));
9935 space = isl_map_get_space(map);
9936 total1 = isl_space_dim(space, isl_dim_in);
9937 total2 = isl_space_dim(space, isl_dim_out);
9938 space = isl_space_factor_range(space);
9939 keep1 = isl_space_dim(space, isl_dim_in);
9940 keep2 = isl_space_dim(space, isl_dim_out);
9941 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9942 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9943 map = isl_map_reset_space(map, space);
9945 return map;
9948 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9950 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9952 isl_space *space;
9953 int total, keep;
9955 if (!map)
9956 return NULL;
9957 if (!isl_space_domain_is_wrapping(map->dim))
9958 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9959 "domain is not a product", return isl_map_free(map));
9961 space = isl_map_get_space(map);
9962 total = isl_space_dim(space, isl_dim_in);
9963 space = isl_space_domain_factor_domain(space);
9964 keep = isl_space_dim(space, isl_dim_in);
9965 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9966 map = isl_map_reset_space(map, space);
9968 return map;
9971 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9973 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9975 isl_space *space;
9976 int total, keep;
9978 if (!map)
9979 return NULL;
9980 if (!isl_space_domain_is_wrapping(map->dim))
9981 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9982 "domain is not a product", return isl_map_free(map));
9984 space = isl_map_get_space(map);
9985 total = isl_space_dim(space, isl_dim_in);
9986 space = isl_space_domain_factor_range(space);
9987 keep = isl_space_dim(space, isl_dim_in);
9988 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9989 map = isl_map_reset_space(map, space);
9991 return map;
9994 /* Given a map A -> [B -> C], extract the map A -> B.
9996 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9998 isl_space *space;
9999 int total, keep;
10001 if (!map)
10002 return NULL;
10003 if (!isl_space_range_is_wrapping(map->dim))
10004 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10005 "range is not a product", return isl_map_free(map));
10007 space = isl_map_get_space(map);
10008 total = isl_space_dim(space, isl_dim_out);
10009 space = isl_space_range_factor_domain(space);
10010 keep = isl_space_dim(space, isl_dim_out);
10011 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10012 map = isl_map_reset_space(map, space);
10014 return map;
10017 /* Given a map A -> [B -> C], extract the map A -> C.
10019 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10021 isl_space *space;
10022 int total, keep;
10024 if (!map)
10025 return NULL;
10026 if (!isl_space_range_is_wrapping(map->dim))
10027 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10028 "range is not a product", return isl_map_free(map));
10030 space = isl_map_get_space(map);
10031 total = isl_space_dim(space, isl_dim_out);
10032 space = isl_space_range_factor_range(space);
10033 keep = isl_space_dim(space, isl_dim_out);
10034 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10035 map = isl_map_reset_space(map, space);
10037 return map;
10040 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10042 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10043 __isl_take isl_map *map2)
10045 isl_map *prod;
10047 prod = isl_map_domain_product(map1, map2);
10048 prod = isl_map_flatten_domain(prod);
10049 return prod;
10052 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10054 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10055 __isl_take isl_map *map2)
10057 isl_map *prod;
10059 prod = isl_map_range_product(map1, map2);
10060 prod = isl_map_flatten_range(prod);
10061 return prod;
10064 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10066 int i;
10067 uint32_t hash = isl_hash_init();
10068 unsigned total;
10070 if (!bmap)
10071 return 0;
10072 bmap = isl_basic_map_copy(bmap);
10073 bmap = isl_basic_map_normalize(bmap);
10074 if (!bmap)
10075 return 0;
10076 total = isl_basic_map_total_dim(bmap);
10077 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10078 for (i = 0; i < bmap->n_eq; ++i) {
10079 uint32_t c_hash;
10080 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10081 isl_hash_hash(hash, c_hash);
10083 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10084 for (i = 0; i < bmap->n_ineq; ++i) {
10085 uint32_t c_hash;
10086 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10087 isl_hash_hash(hash, c_hash);
10089 isl_hash_byte(hash, bmap->n_div & 0xFF);
10090 for (i = 0; i < bmap->n_div; ++i) {
10091 uint32_t c_hash;
10092 if (isl_int_is_zero(bmap->div[i][0]))
10093 continue;
10094 isl_hash_byte(hash, i & 0xFF);
10095 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10096 isl_hash_hash(hash, c_hash);
10098 isl_basic_map_free(bmap);
10099 return hash;
10102 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10104 return isl_basic_map_get_hash(bset_to_bmap(bset));
10107 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10109 int i;
10110 uint32_t hash;
10112 if (!map)
10113 return 0;
10114 map = isl_map_copy(map);
10115 map = isl_map_normalize(map);
10116 if (!map)
10117 return 0;
10119 hash = isl_hash_init();
10120 for (i = 0; i < map->n; ++i) {
10121 uint32_t bmap_hash;
10122 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10123 isl_hash_hash(hash, bmap_hash);
10126 isl_map_free(map);
10128 return hash;
10131 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10133 return isl_map_get_hash(set_to_map(set));
10136 /* Check if the value for dimension dim is completely determined
10137 * by the values of the other parameters and variables.
10138 * That is, check if dimension dim is involved in an equality.
10140 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10142 int i;
10143 unsigned nparam;
10145 if (!bset)
10146 return -1;
10147 nparam = isl_basic_set_n_param(bset);
10148 for (i = 0; i < bset->n_eq; ++i)
10149 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10150 return 1;
10151 return 0;
10154 /* Check if the value for dimension dim is completely determined
10155 * by the values of the other parameters and variables.
10156 * That is, check if dimension dim is involved in an equality
10157 * for each of the subsets.
10159 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10161 int i;
10163 if (!set)
10164 return -1;
10165 for (i = 0; i < set->n; ++i) {
10166 int unique;
10167 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10168 if (unique != 1)
10169 return unique;
10171 return 1;
10174 /* Return the number of basic maps in the (current) representation of "map".
10176 int isl_map_n_basic_map(__isl_keep isl_map *map)
10178 return map ? map->n : 0;
10181 int isl_set_n_basic_set(__isl_keep isl_set *set)
10183 return set ? set->n : 0;
10186 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10187 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10189 int i;
10191 if (!map)
10192 return isl_stat_error;
10194 for (i = 0; i < map->n; ++i)
10195 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10196 return isl_stat_error;
10198 return isl_stat_ok;
10201 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10202 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10204 int i;
10206 if (!set)
10207 return isl_stat_error;
10209 for (i = 0; i < set->n; ++i)
10210 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10211 return isl_stat_error;
10213 return isl_stat_ok;
10216 /* Return a list of basic sets, the union of which is equal to "set".
10218 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10219 __isl_keep isl_set *set)
10221 int i;
10222 isl_basic_set_list *list;
10224 if (!set)
10225 return NULL;
10227 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10228 for (i = 0; i < set->n; ++i) {
10229 isl_basic_set *bset;
10231 bset = isl_basic_set_copy(set->p[i]);
10232 list = isl_basic_set_list_add(list, bset);
10235 return list;
10238 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10240 isl_space *dim;
10242 if (!bset)
10243 return NULL;
10245 bset = isl_basic_set_cow(bset);
10246 if (!bset)
10247 return NULL;
10249 dim = isl_basic_set_get_space(bset);
10250 dim = isl_space_lift(dim, bset->n_div);
10251 if (!dim)
10252 goto error;
10253 isl_space_free(bset->dim);
10254 bset->dim = dim;
10255 bset->extra -= bset->n_div;
10256 bset->n_div = 0;
10258 bset = isl_basic_set_finalize(bset);
10260 return bset;
10261 error:
10262 isl_basic_set_free(bset);
10263 return NULL;
10266 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10268 int i;
10269 isl_space *dim;
10270 unsigned n_div;
10272 set = isl_set_align_divs(set);
10274 if (!set)
10275 return NULL;
10277 set = isl_set_cow(set);
10278 if (!set)
10279 return NULL;
10281 n_div = set->p[0]->n_div;
10282 dim = isl_set_get_space(set);
10283 dim = isl_space_lift(dim, n_div);
10284 if (!dim)
10285 goto error;
10286 isl_space_free(set->dim);
10287 set->dim = dim;
10289 for (i = 0; i < set->n; ++i) {
10290 set->p[i] = isl_basic_set_lift(set->p[i]);
10291 if (!set->p[i])
10292 goto error;
10295 return set;
10296 error:
10297 isl_set_free(set);
10298 return NULL;
10301 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10303 isl_space *dim;
10304 struct isl_basic_map *bmap;
10305 unsigned n_set;
10306 unsigned n_div;
10307 unsigned n_param;
10308 unsigned total;
10309 int i, k, l;
10311 set = isl_set_align_divs(set);
10313 if (!set)
10314 return NULL;
10316 dim = isl_set_get_space(set);
10317 if (set->n == 0 || set->p[0]->n_div == 0) {
10318 isl_set_free(set);
10319 return isl_map_identity(isl_space_map_from_set(dim));
10322 n_div = set->p[0]->n_div;
10323 dim = isl_space_map_from_set(dim);
10324 n_param = isl_space_dim(dim, isl_dim_param);
10325 n_set = isl_space_dim(dim, isl_dim_in);
10326 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10327 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10328 for (i = 0; i < n_set; ++i)
10329 bmap = var_equal(bmap, i);
10331 total = n_param + n_set + n_set + n_div;
10332 for (i = 0; i < n_div; ++i) {
10333 k = isl_basic_map_alloc_inequality(bmap);
10334 if (k < 0)
10335 goto error;
10336 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10337 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10338 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10339 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10340 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10341 set->p[0]->div[i][0]);
10343 l = isl_basic_map_alloc_inequality(bmap);
10344 if (l < 0)
10345 goto error;
10346 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10347 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10348 set->p[0]->div[i][0]);
10349 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10352 isl_set_free(set);
10353 bmap = isl_basic_map_simplify(bmap);
10354 bmap = isl_basic_map_finalize(bmap);
10355 return isl_map_from_basic_map(bmap);
10356 error:
10357 isl_set_free(set);
10358 isl_basic_map_free(bmap);
10359 return NULL;
10362 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10364 unsigned dim;
10365 int size = 0;
10367 if (!bset)
10368 return -1;
10370 dim = isl_basic_set_total_dim(bset);
10371 size += bset->n_eq * (1 + dim);
10372 size += bset->n_ineq * (1 + dim);
10373 size += bset->n_div * (2 + dim);
10375 return size;
10378 int isl_set_size(__isl_keep isl_set *set)
10380 int i;
10381 int size = 0;
10383 if (!set)
10384 return -1;
10386 for (i = 0; i < set->n; ++i)
10387 size += isl_basic_set_size(set->p[i]);
10389 return size;
10392 /* Check if there is any lower bound (if lower == 0) and/or upper
10393 * bound (if upper == 0) on the specified dim.
10395 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10396 enum isl_dim_type type, unsigned pos, int lower, int upper)
10398 int i;
10400 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10401 return isl_bool_error;
10403 pos += isl_basic_map_offset(bmap, type);
10405 for (i = 0; i < bmap->n_div; ++i) {
10406 if (isl_int_is_zero(bmap->div[i][0]))
10407 continue;
10408 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10409 return isl_bool_true;
10412 for (i = 0; i < bmap->n_eq; ++i)
10413 if (!isl_int_is_zero(bmap->eq[i][pos]))
10414 return isl_bool_true;
10416 for (i = 0; i < bmap->n_ineq; ++i) {
10417 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10418 if (sgn > 0)
10419 lower = 1;
10420 if (sgn < 0)
10421 upper = 1;
10424 return lower && upper;
10427 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10428 enum isl_dim_type type, unsigned pos)
10430 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10433 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10434 enum isl_dim_type type, unsigned pos)
10436 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10439 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10440 enum isl_dim_type type, unsigned pos)
10442 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10445 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10446 enum isl_dim_type type, unsigned pos)
10448 int i;
10450 if (!map)
10451 return isl_bool_error;
10453 for (i = 0; i < map->n; ++i) {
10454 isl_bool bounded;
10455 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10456 if (bounded < 0 || !bounded)
10457 return bounded;
10460 return isl_bool_true;
10463 /* Return true if the specified dim is involved in both an upper bound
10464 * and a lower bound.
10466 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10467 enum isl_dim_type type, unsigned pos)
10469 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10472 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10474 static isl_bool has_any_bound(__isl_keep isl_map *map,
10475 enum isl_dim_type type, unsigned pos,
10476 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10477 enum isl_dim_type type, unsigned pos))
10479 int i;
10481 if (!map)
10482 return isl_bool_error;
10484 for (i = 0; i < map->n; ++i) {
10485 isl_bool bounded;
10486 bounded = fn(map->p[i], type, pos);
10487 if (bounded < 0 || bounded)
10488 return bounded;
10491 return isl_bool_false;
10494 /* Return 1 if the specified dim is involved in any lower bound.
10496 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10497 enum isl_dim_type type, unsigned pos)
10499 return has_any_bound(set, type, pos,
10500 &isl_basic_map_dim_has_lower_bound);
10503 /* Return 1 if the specified dim is involved in any upper bound.
10505 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10506 enum isl_dim_type type, unsigned pos)
10508 return has_any_bound(set, type, pos,
10509 &isl_basic_map_dim_has_upper_bound);
10512 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10514 static isl_bool has_bound(__isl_keep isl_map *map,
10515 enum isl_dim_type type, unsigned pos,
10516 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10517 enum isl_dim_type type, unsigned pos))
10519 int i;
10521 if (!map)
10522 return isl_bool_error;
10524 for (i = 0; i < map->n; ++i) {
10525 isl_bool bounded;
10526 bounded = fn(map->p[i], type, pos);
10527 if (bounded < 0 || !bounded)
10528 return bounded;
10531 return isl_bool_true;
10534 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10536 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10537 enum isl_dim_type type, unsigned pos)
10539 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10542 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10544 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10545 enum isl_dim_type type, unsigned pos)
10547 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10550 /* For each of the "n" variables starting at "first", determine
10551 * the sign of the variable and put the results in the first "n"
10552 * elements of the array "signs".
10553 * Sign
10554 * 1 means that the variable is non-negative
10555 * -1 means that the variable is non-positive
10556 * 0 means the variable attains both positive and negative values.
10558 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10559 unsigned first, unsigned n, int *signs)
10561 isl_vec *bound = NULL;
10562 struct isl_tab *tab = NULL;
10563 struct isl_tab_undo *snap;
10564 int i;
10566 if (!bset || !signs)
10567 return isl_stat_error;
10569 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10570 tab = isl_tab_from_basic_set(bset, 0);
10571 if (!bound || !tab)
10572 goto error;
10574 isl_seq_clr(bound->el, bound->size);
10575 isl_int_set_si(bound->el[0], -1);
10577 snap = isl_tab_snap(tab);
10578 for (i = 0; i < n; ++i) {
10579 int empty;
10581 isl_int_set_si(bound->el[1 + first + i], -1);
10582 if (isl_tab_add_ineq(tab, bound->el) < 0)
10583 goto error;
10584 empty = tab->empty;
10585 isl_int_set_si(bound->el[1 + first + i], 0);
10586 if (isl_tab_rollback(tab, snap) < 0)
10587 goto error;
10589 if (empty) {
10590 signs[i] = 1;
10591 continue;
10594 isl_int_set_si(bound->el[1 + first + i], 1);
10595 if (isl_tab_add_ineq(tab, bound->el) < 0)
10596 goto error;
10597 empty = tab->empty;
10598 isl_int_set_si(bound->el[1 + first + i], 0);
10599 if (isl_tab_rollback(tab, snap) < 0)
10600 goto error;
10602 signs[i] = empty ? -1 : 0;
10605 isl_tab_free(tab);
10606 isl_vec_free(bound);
10607 return isl_stat_ok;
10608 error:
10609 isl_tab_free(tab);
10610 isl_vec_free(bound);
10611 return isl_stat_error;
10614 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10615 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10617 if (!bset || !signs)
10618 return isl_stat_error;
10619 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10620 return isl_stat_error);
10622 first += pos(bset->dim, type) - 1;
10623 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10626 /* Is it possible for the integer division "div" to depend (possibly
10627 * indirectly) on any output dimensions?
10629 * If the div is undefined, then we conservatively assume that it
10630 * may depend on them.
10631 * Otherwise, we check if it actually depends on them or on any integer
10632 * divisions that may depend on them.
10634 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10636 int i;
10637 unsigned n_out, o_out;
10638 unsigned n_div, o_div;
10640 if (isl_int_is_zero(bmap->div[div][0]))
10641 return isl_bool_true;
10643 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10644 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10646 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10647 return isl_bool_true;
10649 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10650 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10652 for (i = 0; i < n_div; ++i) {
10653 isl_bool may_involve;
10655 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10656 continue;
10657 may_involve = div_may_involve_output(bmap, i);
10658 if (may_involve < 0 || may_involve)
10659 return may_involve;
10662 return isl_bool_false;
10665 /* Return the first integer division of "bmap" in the range
10666 * [first, first + n[ that may depend on any output dimensions and
10667 * that has a non-zero coefficient in "c" (where the first coefficient
10668 * in "c" corresponds to integer division "first").
10670 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10671 isl_int *c, int first, int n)
10673 int k;
10675 if (!bmap)
10676 return -1;
10678 for (k = first; k < first + n; ++k) {
10679 isl_bool may_involve;
10681 if (isl_int_is_zero(c[k]))
10682 continue;
10683 may_involve = div_may_involve_output(bmap, k);
10684 if (may_involve < 0)
10685 return -1;
10686 if (may_involve)
10687 return k;
10690 return first + n;
10693 /* Look for a pair of inequality constraints in "bmap" of the form
10695 * -l + i >= 0 or i >= l
10696 * and
10697 * n + l - i >= 0 or i <= l + n
10699 * with n < "m" and i the output dimension at position "pos".
10700 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10701 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10702 * and earlier output dimensions, as well as integer divisions that do
10703 * not involve any of the output dimensions.
10705 * Return the index of the first inequality constraint or bmap->n_ineq
10706 * if no such pair can be found.
10708 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10709 int pos, isl_int m)
10711 int i, j;
10712 isl_ctx *ctx;
10713 unsigned total;
10714 unsigned n_div, o_div;
10715 unsigned n_out, o_out;
10716 int less;
10718 if (!bmap)
10719 return -1;
10721 ctx = isl_basic_map_get_ctx(bmap);
10722 total = isl_basic_map_total_dim(bmap);
10723 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10724 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10725 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10726 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10727 for (i = 0; i < bmap->n_ineq; ++i) {
10728 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10729 continue;
10730 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10731 n_out - (pos + 1)) != -1)
10732 continue;
10733 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10734 0, n_div) < n_div)
10735 continue;
10736 for (j = i + 1; j < bmap->n_ineq; ++j) {
10737 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10738 ctx->one))
10739 continue;
10740 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10741 bmap->ineq[j] + 1, total))
10742 continue;
10743 break;
10745 if (j >= bmap->n_ineq)
10746 continue;
10747 isl_int_add(bmap->ineq[i][0],
10748 bmap->ineq[i][0], bmap->ineq[j][0]);
10749 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10750 isl_int_sub(bmap->ineq[i][0],
10751 bmap->ineq[i][0], bmap->ineq[j][0]);
10752 if (!less)
10753 continue;
10754 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10755 return i;
10756 else
10757 return j;
10760 return bmap->n_ineq;
10763 /* Return the index of the equality of "bmap" that defines
10764 * the output dimension "pos" in terms of earlier dimensions.
10765 * The equality may also involve integer divisions, as long
10766 * as those integer divisions are defined in terms of
10767 * parameters or input dimensions.
10768 * In this case, *div is set to the number of integer divisions and
10769 * *ineq is set to the number of inequality constraints (provided
10770 * div and ineq are not NULL).
10772 * The equality may also involve a single integer division involving
10773 * the output dimensions (typically only output dimension "pos") as
10774 * long as the coefficient of output dimension "pos" is 1 or -1 and
10775 * there is a pair of constraints i >= l and i <= l + n, with i referring
10776 * to output dimension "pos", l an expression involving only earlier
10777 * dimensions and n smaller than the coefficient of the integer division
10778 * in the equality. In this case, the output dimension can be defined
10779 * in terms of a modulo expression that does not involve the integer division.
10780 * *div is then set to this single integer division and
10781 * *ineq is set to the index of constraint i >= l.
10783 * Return bmap->n_eq if there is no such equality.
10784 * Return -1 on error.
10786 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10787 int pos, int *div, int *ineq)
10789 int j, k, l;
10790 unsigned n_out, o_out;
10791 unsigned n_div, o_div;
10793 if (!bmap)
10794 return -1;
10796 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10797 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10798 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10799 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10801 if (ineq)
10802 *ineq = bmap->n_ineq;
10803 if (div)
10804 *div = n_div;
10805 for (j = 0; j < bmap->n_eq; ++j) {
10806 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10807 continue;
10808 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10809 n_out - (pos + 1)) != -1)
10810 continue;
10811 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10812 0, n_div);
10813 if (k >= n_div)
10814 return j;
10815 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10816 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10817 continue;
10818 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10819 k + 1, n_div - (k+1)) < n_div)
10820 continue;
10821 l = find_modulo_constraint_pair(bmap, pos,
10822 bmap->eq[j][o_div + k]);
10823 if (l < 0)
10824 return -1;
10825 if (l >= bmap->n_ineq)
10826 continue;
10827 if (div)
10828 *div = k;
10829 if (ineq)
10830 *ineq = l;
10831 return j;
10834 return bmap->n_eq;
10837 /* Check if the given basic map is obviously single-valued.
10838 * In particular, for each output dimension, check that there is
10839 * an equality that defines the output dimension in terms of
10840 * earlier dimensions.
10842 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10844 int i;
10845 unsigned n_out;
10847 if (!bmap)
10848 return isl_bool_error;
10850 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10852 for (i = 0; i < n_out; ++i) {
10853 int eq;
10855 eq = isl_basic_map_output_defining_equality(bmap, i,
10856 NULL, NULL);
10857 if (eq < 0)
10858 return isl_bool_error;
10859 if (eq >= bmap->n_eq)
10860 return isl_bool_false;
10863 return isl_bool_true;
10866 /* Check if the given basic map is single-valued.
10867 * We simply compute
10869 * M \circ M^-1
10871 * and check if the result is a subset of the identity mapping.
10873 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10875 isl_space *space;
10876 isl_basic_map *test;
10877 isl_basic_map *id;
10878 isl_bool sv;
10880 sv = isl_basic_map_plain_is_single_valued(bmap);
10881 if (sv < 0 || sv)
10882 return sv;
10884 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10885 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10887 space = isl_basic_map_get_space(bmap);
10888 space = isl_space_map_from_set(isl_space_range(space));
10889 id = isl_basic_map_identity(space);
10891 sv = isl_basic_map_is_subset(test, id);
10893 isl_basic_map_free(test);
10894 isl_basic_map_free(id);
10896 return sv;
10899 /* Check if the given map is obviously single-valued.
10901 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10903 if (!map)
10904 return isl_bool_error;
10905 if (map->n == 0)
10906 return isl_bool_true;
10907 if (map->n >= 2)
10908 return isl_bool_false;
10910 return isl_basic_map_plain_is_single_valued(map->p[0]);
10913 /* Check if the given map is single-valued.
10914 * We simply compute
10916 * M \circ M^-1
10918 * and check if the result is a subset of the identity mapping.
10920 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10922 isl_space *dim;
10923 isl_map *test;
10924 isl_map *id;
10925 isl_bool sv;
10927 sv = isl_map_plain_is_single_valued(map);
10928 if (sv < 0 || sv)
10929 return sv;
10931 test = isl_map_reverse(isl_map_copy(map));
10932 test = isl_map_apply_range(test, isl_map_copy(map));
10934 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10935 id = isl_map_identity(dim);
10937 sv = isl_map_is_subset(test, id);
10939 isl_map_free(test);
10940 isl_map_free(id);
10942 return sv;
10945 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10947 isl_bool in;
10949 map = isl_map_copy(map);
10950 map = isl_map_reverse(map);
10951 in = isl_map_is_single_valued(map);
10952 isl_map_free(map);
10954 return in;
10957 /* Check if the given map is obviously injective.
10959 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10961 isl_bool in;
10963 map = isl_map_copy(map);
10964 map = isl_map_reverse(map);
10965 in = isl_map_plain_is_single_valued(map);
10966 isl_map_free(map);
10968 return in;
10971 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10973 isl_bool sv;
10975 sv = isl_map_is_single_valued(map);
10976 if (sv < 0 || !sv)
10977 return sv;
10979 return isl_map_is_injective(map);
10982 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10984 return isl_map_is_single_valued(set_to_map(set));
10987 /* Does "map" only map elements to themselves?
10989 * If the domain and range spaces are different, then "map"
10990 * is considered not to be an identity relation, even if it is empty.
10991 * Otherwise, construct the maximal identity relation and
10992 * check whether "map" is a subset of this relation.
10994 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10996 isl_space *space;
10997 isl_map *id;
10998 isl_bool equal, is_identity;
11000 space = isl_map_get_space(map);
11001 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11002 isl_space_free(space);
11003 if (equal < 0 || !equal)
11004 return equal;
11006 id = isl_map_identity(isl_map_get_space(map));
11007 is_identity = isl_map_is_subset(map, id);
11008 isl_map_free(id);
11010 return is_identity;
11013 int isl_map_is_translation(__isl_keep isl_map *map)
11015 int ok;
11016 isl_set *delta;
11018 delta = isl_map_deltas(isl_map_copy(map));
11019 ok = isl_set_is_singleton(delta);
11020 isl_set_free(delta);
11022 return ok;
11025 static int unique(isl_int *p, unsigned pos, unsigned len)
11027 if (isl_seq_first_non_zero(p, pos) != -1)
11028 return 0;
11029 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11030 return 0;
11031 return 1;
11034 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11036 int i, j;
11037 unsigned nvar;
11038 unsigned ovar;
11040 if (!bset)
11041 return isl_bool_error;
11043 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11044 return isl_bool_false;
11046 nvar = isl_basic_set_dim(bset, isl_dim_set);
11047 ovar = isl_space_offset(bset->dim, isl_dim_set);
11048 for (j = 0; j < nvar; ++j) {
11049 int lower = 0, upper = 0;
11050 for (i = 0; i < bset->n_eq; ++i) {
11051 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11052 continue;
11053 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11054 return isl_bool_false;
11055 break;
11057 if (i < bset->n_eq)
11058 continue;
11059 for (i = 0; i < bset->n_ineq; ++i) {
11060 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11061 continue;
11062 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11063 return isl_bool_false;
11064 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11065 lower = 1;
11066 else
11067 upper = 1;
11069 if (!lower || !upper)
11070 return isl_bool_false;
11073 return isl_bool_true;
11076 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11078 if (!set)
11079 return isl_bool_error;
11080 if (set->n != 1)
11081 return isl_bool_false;
11083 return isl_basic_set_is_box(set->p[0]);
11086 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11088 if (!bset)
11089 return isl_bool_error;
11091 return isl_space_is_wrapping(bset->dim);
11094 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11096 if (!set)
11097 return isl_bool_error;
11099 return isl_space_is_wrapping(set->dim);
11102 /* Modify the space of "map" through a call to "change".
11103 * If "can_change" is set (not NULL), then first call it to check
11104 * if the modification is allowed, printing the error message "cannot_change"
11105 * if it is not.
11107 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11108 isl_bool (*can_change)(__isl_keep isl_map *map),
11109 const char *cannot_change,
11110 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11112 isl_bool ok;
11113 isl_space *space;
11115 if (!map)
11116 return NULL;
11118 ok = can_change ? can_change(map) : isl_bool_true;
11119 if (ok < 0)
11120 return isl_map_free(map);
11121 if (!ok)
11122 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11123 return isl_map_free(map));
11125 space = change(isl_map_get_space(map));
11126 map = isl_map_reset_space(map, space);
11128 return map;
11131 /* Is the domain of "map" a wrapped relation?
11133 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11135 if (!map)
11136 return isl_bool_error;
11138 return isl_space_domain_is_wrapping(map->dim);
11141 /* Is the range of "map" a wrapped relation?
11143 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11145 if (!map)
11146 return isl_bool_error;
11148 return isl_space_range_is_wrapping(map->dim);
11151 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11153 bmap = isl_basic_map_cow(bmap);
11154 if (!bmap)
11155 return NULL;
11157 bmap->dim = isl_space_wrap(bmap->dim);
11158 if (!bmap->dim)
11159 goto error;
11161 bmap = isl_basic_map_finalize(bmap);
11163 return bset_from_bmap(bmap);
11164 error:
11165 isl_basic_map_free(bmap);
11166 return NULL;
11169 /* Given a map A -> B, return the set (A -> B).
11171 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11173 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11176 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11178 bset = isl_basic_set_cow(bset);
11179 if (!bset)
11180 return NULL;
11182 bset->dim = isl_space_unwrap(bset->dim);
11183 if (!bset->dim)
11184 goto error;
11186 bset = isl_basic_set_finalize(bset);
11188 return bset_to_bmap(bset);
11189 error:
11190 isl_basic_set_free(bset);
11191 return NULL;
11194 /* Given a set (A -> B), return the map A -> B.
11195 * Error out if "set" is not of the form (A -> B).
11197 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11199 return isl_map_change_space(set, &isl_set_is_wrapping,
11200 "not a wrapping set", &isl_space_unwrap);
11203 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11204 enum isl_dim_type type)
11206 if (!bmap)
11207 return NULL;
11209 if (!isl_space_is_named_or_nested(bmap->dim, type))
11210 return bmap;
11212 bmap = isl_basic_map_cow(bmap);
11213 if (!bmap)
11214 return NULL;
11216 bmap->dim = isl_space_reset(bmap->dim, type);
11217 if (!bmap->dim)
11218 goto error;
11220 bmap = isl_basic_map_finalize(bmap);
11222 return bmap;
11223 error:
11224 isl_basic_map_free(bmap);
11225 return NULL;
11228 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11229 enum isl_dim_type type)
11231 int i;
11233 if (!map)
11234 return NULL;
11236 if (!isl_space_is_named_or_nested(map->dim, type))
11237 return map;
11239 map = isl_map_cow(map);
11240 if (!map)
11241 return NULL;
11243 for (i = 0; i < map->n; ++i) {
11244 map->p[i] = isl_basic_map_reset(map->p[i], type);
11245 if (!map->p[i])
11246 goto error;
11248 map->dim = isl_space_reset(map->dim, type);
11249 if (!map->dim)
11250 goto error;
11252 return map;
11253 error:
11254 isl_map_free(map);
11255 return NULL;
11258 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11260 if (!bmap)
11261 return NULL;
11263 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11264 return bmap;
11266 bmap = isl_basic_map_cow(bmap);
11267 if (!bmap)
11268 return NULL;
11270 bmap->dim = isl_space_flatten(bmap->dim);
11271 if (!bmap->dim)
11272 goto error;
11274 bmap = isl_basic_map_finalize(bmap);
11276 return bmap;
11277 error:
11278 isl_basic_map_free(bmap);
11279 return NULL;
11282 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11284 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11287 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11288 __isl_take isl_basic_map *bmap)
11290 if (!bmap)
11291 return NULL;
11293 if (!bmap->dim->nested[0])
11294 return bmap;
11296 bmap = isl_basic_map_cow(bmap);
11297 if (!bmap)
11298 return NULL;
11300 bmap->dim = isl_space_flatten_domain(bmap->dim);
11301 if (!bmap->dim)
11302 goto error;
11304 bmap = isl_basic_map_finalize(bmap);
11306 return bmap;
11307 error:
11308 isl_basic_map_free(bmap);
11309 return NULL;
11312 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11313 __isl_take isl_basic_map *bmap)
11315 if (!bmap)
11316 return NULL;
11318 if (!bmap->dim->nested[1])
11319 return bmap;
11321 bmap = isl_basic_map_cow(bmap);
11322 if (!bmap)
11323 return NULL;
11325 bmap->dim = isl_space_flatten_range(bmap->dim);
11326 if (!bmap->dim)
11327 goto error;
11329 bmap = isl_basic_map_finalize(bmap);
11331 return bmap;
11332 error:
11333 isl_basic_map_free(bmap);
11334 return NULL;
11337 /* Remove any internal structure from the spaces of domain and range of "map".
11339 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11341 if (!map)
11342 return NULL;
11344 if (!map->dim->nested[0] && !map->dim->nested[1])
11345 return map;
11347 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11350 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11352 return set_from_map(isl_map_flatten(set_to_map(set)));
11355 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11357 isl_space *dim, *flat_dim;
11358 isl_map *map;
11360 dim = isl_set_get_space(set);
11361 flat_dim = isl_space_flatten(isl_space_copy(dim));
11362 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11363 map = isl_map_intersect_domain(map, set);
11365 return map;
11368 /* Remove any internal structure from the space of the domain of "map".
11370 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11372 if (!map)
11373 return NULL;
11375 if (!map->dim->nested[0])
11376 return map;
11378 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11381 /* Remove any internal structure from the space of the range of "map".
11383 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11385 if (!map)
11386 return NULL;
11388 if (!map->dim->nested[1])
11389 return map;
11391 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11394 /* Reorder the dimensions of "bmap" according to the given dim_map
11395 * and set the dimension specification to "dim" and
11396 * perform Gaussian elimination on the result.
11398 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11399 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11401 isl_basic_map *res;
11402 unsigned flags;
11404 bmap = isl_basic_map_cow(bmap);
11405 if (!bmap || !dim || !dim_map)
11406 goto error;
11408 flags = bmap->flags;
11409 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11410 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11411 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11412 res = isl_basic_map_alloc_space(dim,
11413 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11414 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11415 if (res)
11416 res->flags = flags;
11417 res = isl_basic_map_gauss(res, NULL);
11418 res = isl_basic_map_finalize(res);
11419 return res;
11420 error:
11421 free(dim_map);
11422 isl_basic_map_free(bmap);
11423 isl_space_free(dim);
11424 return NULL;
11427 /* Reorder the dimensions of "map" according to given reordering.
11429 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11430 __isl_take isl_reordering *r)
11432 int i;
11433 struct isl_dim_map *dim_map;
11435 map = isl_map_cow(map);
11436 dim_map = isl_dim_map_from_reordering(r);
11437 if (!map || !r || !dim_map)
11438 goto error;
11440 for (i = 0; i < map->n; ++i) {
11441 struct isl_dim_map *dim_map_i;
11443 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11445 map->p[i] = isl_basic_map_realign(map->p[i],
11446 isl_space_copy(r->dim), dim_map_i);
11448 if (!map->p[i])
11449 goto error;
11452 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11454 isl_reordering_free(r);
11455 free(dim_map);
11456 return map;
11457 error:
11458 free(dim_map);
11459 isl_map_free(map);
11460 isl_reordering_free(r);
11461 return NULL;
11464 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11465 __isl_take isl_reordering *r)
11467 return set_from_map(isl_map_realign(set_to_map(set), r));
11470 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11471 __isl_take isl_space *model)
11473 isl_ctx *ctx;
11475 if (!map || !model)
11476 goto error;
11478 ctx = isl_space_get_ctx(model);
11479 if (!isl_space_has_named_params(model))
11480 isl_die(ctx, isl_error_invalid,
11481 "model has unnamed parameters", goto error);
11482 if (!isl_space_has_named_params(map->dim))
11483 isl_die(ctx, isl_error_invalid,
11484 "relation has unnamed parameters", goto error);
11485 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11486 isl_reordering *exp;
11488 model = isl_space_drop_dims(model, isl_dim_in,
11489 0, isl_space_dim(model, isl_dim_in));
11490 model = isl_space_drop_dims(model, isl_dim_out,
11491 0, isl_space_dim(model, isl_dim_out));
11492 exp = isl_parameter_alignment_reordering(map->dim, model);
11493 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11494 map = isl_map_realign(map, exp);
11497 isl_space_free(model);
11498 return map;
11499 error:
11500 isl_space_free(model);
11501 isl_map_free(map);
11502 return NULL;
11505 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11506 __isl_take isl_space *model)
11508 return isl_map_align_params(set, model);
11511 /* Align the parameters of "bmap" to those of "model", introducing
11512 * additional parameters if needed.
11514 __isl_give isl_basic_map *isl_basic_map_align_params(
11515 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11517 isl_ctx *ctx;
11519 if (!bmap || !model)
11520 goto error;
11522 ctx = isl_space_get_ctx(model);
11523 if (!isl_space_has_named_params(model))
11524 isl_die(ctx, isl_error_invalid,
11525 "model has unnamed parameters", goto error);
11526 if (!isl_space_has_named_params(bmap->dim))
11527 isl_die(ctx, isl_error_invalid,
11528 "relation has unnamed parameters", goto error);
11529 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11530 isl_reordering *exp;
11531 struct isl_dim_map *dim_map;
11533 model = isl_space_drop_dims(model, isl_dim_in,
11534 0, isl_space_dim(model, isl_dim_in));
11535 model = isl_space_drop_dims(model, isl_dim_out,
11536 0, isl_space_dim(model, isl_dim_out));
11537 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11538 exp = isl_reordering_extend_space(exp,
11539 isl_basic_map_get_space(bmap));
11540 dim_map = isl_dim_map_from_reordering(exp);
11541 bmap = isl_basic_map_realign(bmap,
11542 exp ? isl_space_copy(exp->dim) : NULL,
11543 isl_dim_map_extend(dim_map, bmap));
11544 isl_reordering_free(exp);
11545 free(dim_map);
11548 isl_space_free(model);
11549 return bmap;
11550 error:
11551 isl_space_free(model);
11552 isl_basic_map_free(bmap);
11553 return NULL;
11556 /* Align the parameters of "bset" to those of "model", introducing
11557 * additional parameters if needed.
11559 __isl_give isl_basic_set *isl_basic_set_align_params(
11560 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11562 return isl_basic_map_align_params(bset, model);
11565 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11566 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11567 enum isl_dim_type c2, enum isl_dim_type c3,
11568 enum isl_dim_type c4, enum isl_dim_type c5)
11570 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11571 struct isl_mat *mat;
11572 int i, j, k;
11573 int pos;
11575 if (!bmap)
11576 return NULL;
11577 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11578 isl_basic_map_total_dim(bmap) + 1);
11579 if (!mat)
11580 return NULL;
11581 for (i = 0; i < bmap->n_eq; ++i)
11582 for (j = 0, pos = 0; j < 5; ++j) {
11583 int off = isl_basic_map_offset(bmap, c[j]);
11584 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11585 isl_int_set(mat->row[i][pos],
11586 bmap->eq[i][off + k]);
11587 ++pos;
11591 return mat;
11594 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11595 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11596 enum isl_dim_type c2, enum isl_dim_type c3,
11597 enum isl_dim_type c4, enum isl_dim_type c5)
11599 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11600 struct isl_mat *mat;
11601 int i, j, k;
11602 int pos;
11604 if (!bmap)
11605 return NULL;
11606 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11607 isl_basic_map_total_dim(bmap) + 1);
11608 if (!mat)
11609 return NULL;
11610 for (i = 0; i < bmap->n_ineq; ++i)
11611 for (j = 0, pos = 0; j < 5; ++j) {
11612 int off = isl_basic_map_offset(bmap, c[j]);
11613 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11614 isl_int_set(mat->row[i][pos],
11615 bmap->ineq[i][off + k]);
11616 ++pos;
11620 return mat;
11623 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11624 __isl_take isl_space *dim,
11625 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11626 enum isl_dim_type c2, enum isl_dim_type c3,
11627 enum isl_dim_type c4, enum isl_dim_type c5)
11629 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11630 isl_basic_map *bmap;
11631 unsigned total;
11632 unsigned extra;
11633 int i, j, k, l;
11634 int pos;
11636 if (!dim || !eq || !ineq)
11637 goto error;
11639 if (eq->n_col != ineq->n_col)
11640 isl_die(dim->ctx, isl_error_invalid,
11641 "equalities and inequalities matrices should have "
11642 "same number of columns", goto error);
11644 total = 1 + isl_space_dim(dim, isl_dim_all);
11646 if (eq->n_col < total)
11647 isl_die(dim->ctx, isl_error_invalid,
11648 "number of columns too small", goto error);
11650 extra = eq->n_col - total;
11652 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11653 eq->n_row, ineq->n_row);
11654 if (!bmap)
11655 goto error;
11656 for (i = 0; i < extra; ++i) {
11657 k = isl_basic_map_alloc_div(bmap);
11658 if (k < 0)
11659 goto error;
11660 isl_int_set_si(bmap->div[k][0], 0);
11662 for (i = 0; i < eq->n_row; ++i) {
11663 l = isl_basic_map_alloc_equality(bmap);
11664 if (l < 0)
11665 goto error;
11666 for (j = 0, pos = 0; j < 5; ++j) {
11667 int off = isl_basic_map_offset(bmap, c[j]);
11668 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11669 isl_int_set(bmap->eq[l][off + k],
11670 eq->row[i][pos]);
11671 ++pos;
11675 for (i = 0; i < ineq->n_row; ++i) {
11676 l = isl_basic_map_alloc_inequality(bmap);
11677 if (l < 0)
11678 goto error;
11679 for (j = 0, pos = 0; j < 5; ++j) {
11680 int off = isl_basic_map_offset(bmap, c[j]);
11681 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11682 isl_int_set(bmap->ineq[l][off + k],
11683 ineq->row[i][pos]);
11684 ++pos;
11689 isl_space_free(dim);
11690 isl_mat_free(eq);
11691 isl_mat_free(ineq);
11693 bmap = isl_basic_map_simplify(bmap);
11694 return isl_basic_map_finalize(bmap);
11695 error:
11696 isl_space_free(dim);
11697 isl_mat_free(eq);
11698 isl_mat_free(ineq);
11699 return NULL;
11702 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11703 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11704 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11706 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11707 c1, c2, c3, c4, isl_dim_in);
11710 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11711 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11712 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11714 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11715 c1, c2, c3, c4, isl_dim_in);
11718 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11719 __isl_take isl_space *dim,
11720 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11721 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11723 isl_basic_map *bmap;
11724 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11725 c1, c2, c3, c4, isl_dim_in);
11726 return bset_from_bmap(bmap);
11729 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11731 if (!bmap)
11732 return isl_bool_error;
11734 return isl_space_can_zip(bmap->dim);
11737 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11739 if (!map)
11740 return isl_bool_error;
11742 return isl_space_can_zip(map->dim);
11745 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11746 * (A -> C) -> (B -> D).
11748 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11750 unsigned pos;
11751 unsigned n1;
11752 unsigned n2;
11754 if (!bmap)
11755 return NULL;
11757 if (!isl_basic_map_can_zip(bmap))
11758 isl_die(bmap->ctx, isl_error_invalid,
11759 "basic map cannot be zipped", goto error);
11760 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11761 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11762 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11763 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11764 bmap = isl_basic_map_cow(bmap);
11765 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11766 if (!bmap)
11767 return NULL;
11768 bmap->dim = isl_space_zip(bmap->dim);
11769 if (!bmap->dim)
11770 goto error;
11771 bmap = isl_basic_map_mark_final(bmap);
11772 return bmap;
11773 error:
11774 isl_basic_map_free(bmap);
11775 return NULL;
11778 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11779 * (A -> C) -> (B -> D).
11781 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11783 int i;
11785 if (!map)
11786 return NULL;
11788 if (!isl_map_can_zip(map))
11789 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11790 goto error);
11792 map = isl_map_cow(map);
11793 if (!map)
11794 return NULL;
11796 for (i = 0; i < map->n; ++i) {
11797 map->p[i] = isl_basic_map_zip(map->p[i]);
11798 if (!map->p[i])
11799 goto error;
11802 map->dim = isl_space_zip(map->dim);
11803 if (!map->dim)
11804 goto error;
11806 return map;
11807 error:
11808 isl_map_free(map);
11809 return NULL;
11812 /* Can we apply isl_basic_map_curry to "bmap"?
11813 * That is, does it have a nested relation in its domain?
11815 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11817 if (!bmap)
11818 return isl_bool_error;
11820 return isl_space_can_curry(bmap->dim);
11823 /* Can we apply isl_map_curry to "map"?
11824 * That is, does it have a nested relation in its domain?
11826 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11828 if (!map)
11829 return isl_bool_error;
11831 return isl_space_can_curry(map->dim);
11834 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11835 * A -> (B -> C).
11837 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11840 if (!bmap)
11841 return NULL;
11843 if (!isl_basic_map_can_curry(bmap))
11844 isl_die(bmap->ctx, isl_error_invalid,
11845 "basic map cannot be curried", goto error);
11846 bmap = isl_basic_map_cow(bmap);
11847 if (!bmap)
11848 return NULL;
11849 bmap->dim = isl_space_curry(bmap->dim);
11850 if (!bmap->dim)
11851 goto error;
11852 bmap = isl_basic_map_mark_final(bmap);
11853 return bmap;
11854 error:
11855 isl_basic_map_free(bmap);
11856 return NULL;
11859 /* Given a map (A -> B) -> C, return the corresponding map
11860 * A -> (B -> C).
11862 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11864 return isl_map_change_space(map, &isl_map_can_curry,
11865 "map cannot be curried", &isl_space_curry);
11868 /* Can isl_map_range_curry be applied to "map"?
11869 * That is, does it have a nested relation in its range,
11870 * the domain of which is itself a nested relation?
11872 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11874 if (!map)
11875 return isl_bool_error;
11877 return isl_space_can_range_curry(map->dim);
11880 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11881 * A -> (B -> (C -> D)).
11883 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11885 return isl_map_change_space(map, &isl_map_can_range_curry,
11886 "map range cannot be curried",
11887 &isl_space_range_curry);
11890 /* Can we apply isl_basic_map_uncurry to "bmap"?
11891 * That is, does it have a nested relation in its domain?
11893 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11895 if (!bmap)
11896 return isl_bool_error;
11898 return isl_space_can_uncurry(bmap->dim);
11901 /* Can we apply isl_map_uncurry to "map"?
11902 * That is, does it have a nested relation in its domain?
11904 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11906 if (!map)
11907 return isl_bool_error;
11909 return isl_space_can_uncurry(map->dim);
11912 /* Given a basic map A -> (B -> C), return the corresponding basic map
11913 * (A -> B) -> C.
11915 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11918 if (!bmap)
11919 return NULL;
11921 if (!isl_basic_map_can_uncurry(bmap))
11922 isl_die(bmap->ctx, isl_error_invalid,
11923 "basic map cannot be uncurried",
11924 return isl_basic_map_free(bmap));
11925 bmap = isl_basic_map_cow(bmap);
11926 if (!bmap)
11927 return NULL;
11928 bmap->dim = isl_space_uncurry(bmap->dim);
11929 if (!bmap->dim)
11930 return isl_basic_map_free(bmap);
11931 bmap = isl_basic_map_mark_final(bmap);
11932 return bmap;
11935 /* Given a map A -> (B -> C), return the corresponding map
11936 * (A -> B) -> C.
11938 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11940 return isl_map_change_space(map, &isl_map_can_uncurry,
11941 "map cannot be uncurried", &isl_space_uncurry);
11944 /* Construct a basic map mapping the domain of the affine expression
11945 * to a one-dimensional range prescribed by the affine expression.
11946 * If "rational" is set, then construct a rational basic map.
11948 * A NaN affine expression cannot be converted to a basic map.
11950 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11951 __isl_take isl_aff *aff, int rational)
11953 int k;
11954 int pos;
11955 isl_bool is_nan;
11956 isl_local_space *ls;
11957 isl_basic_map *bmap = NULL;
11959 if (!aff)
11960 return NULL;
11961 is_nan = isl_aff_is_nan(aff);
11962 if (is_nan < 0)
11963 goto error;
11964 if (is_nan)
11965 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11966 "cannot convert NaN", goto error);
11968 ls = isl_aff_get_local_space(aff);
11969 bmap = isl_basic_map_from_local_space(ls);
11970 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11971 k = isl_basic_map_alloc_equality(bmap);
11972 if (k < 0)
11973 goto error;
11975 pos = isl_basic_map_offset(bmap, isl_dim_out);
11976 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11977 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11978 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11979 aff->v->size - (pos + 1));
11981 isl_aff_free(aff);
11982 if (rational)
11983 bmap = isl_basic_map_set_rational(bmap);
11984 bmap = isl_basic_map_finalize(bmap);
11985 return bmap;
11986 error:
11987 isl_aff_free(aff);
11988 isl_basic_map_free(bmap);
11989 return NULL;
11992 /* Construct a basic map mapping the domain of the affine expression
11993 * to a one-dimensional range prescribed by the affine expression.
11995 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11997 return isl_basic_map_from_aff2(aff, 0);
12000 /* Construct a map mapping the domain of the affine expression
12001 * to a one-dimensional range prescribed by the affine expression.
12003 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12005 isl_basic_map *bmap;
12007 bmap = isl_basic_map_from_aff(aff);
12008 return isl_map_from_basic_map(bmap);
12011 /* Construct a basic map mapping the domain the multi-affine expression
12012 * to its range, with each dimension in the range equated to the
12013 * corresponding affine expression.
12014 * If "rational" is set, then construct a rational basic map.
12016 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12017 __isl_take isl_multi_aff *maff, int rational)
12019 int i;
12020 isl_space *space;
12021 isl_basic_map *bmap;
12023 if (!maff)
12024 return NULL;
12026 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12027 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12028 "invalid space", goto error);
12030 space = isl_space_domain(isl_multi_aff_get_space(maff));
12031 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12032 if (rational)
12033 bmap = isl_basic_map_set_rational(bmap);
12035 for (i = 0; i < maff->n; ++i) {
12036 isl_aff *aff;
12037 isl_basic_map *bmap_i;
12039 aff = isl_aff_copy(maff->p[i]);
12040 bmap_i = isl_basic_map_from_aff2(aff, rational);
12042 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12045 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12047 isl_multi_aff_free(maff);
12048 return bmap;
12049 error:
12050 isl_multi_aff_free(maff);
12051 return NULL;
12054 /* Construct a basic map mapping the domain the multi-affine expression
12055 * to its range, with each dimension in the range equated to the
12056 * corresponding affine expression.
12058 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12059 __isl_take isl_multi_aff *ma)
12061 return isl_basic_map_from_multi_aff2(ma, 0);
12064 /* Construct a map mapping the domain the multi-affine expression
12065 * to its range, with each dimension in the range equated to the
12066 * corresponding affine expression.
12068 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12070 isl_basic_map *bmap;
12072 bmap = isl_basic_map_from_multi_aff(maff);
12073 return isl_map_from_basic_map(bmap);
12076 /* Construct a basic map mapping a domain in the given space to
12077 * to an n-dimensional range, with n the number of elements in the list,
12078 * where each coordinate in the range is prescribed by the
12079 * corresponding affine expression.
12080 * The domains of all affine expressions in the list are assumed to match
12081 * domain_dim.
12083 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12084 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12086 int i;
12087 isl_space *dim;
12088 isl_basic_map *bmap;
12090 if (!list)
12091 return NULL;
12093 dim = isl_space_from_domain(domain_dim);
12094 bmap = isl_basic_map_universe(dim);
12096 for (i = 0; i < list->n; ++i) {
12097 isl_aff *aff;
12098 isl_basic_map *bmap_i;
12100 aff = isl_aff_copy(list->p[i]);
12101 bmap_i = isl_basic_map_from_aff(aff);
12103 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12106 isl_aff_list_free(list);
12107 return bmap;
12110 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12111 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12113 return isl_map_equate(set, type1, pos1, type2, pos2);
12116 /* Construct a basic map where the given dimensions are equal to each other.
12118 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12119 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12121 isl_basic_map *bmap = NULL;
12122 int i;
12124 if (!space)
12125 return NULL;
12127 if (pos1 >= isl_space_dim(space, type1))
12128 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12129 "index out of bounds", goto error);
12130 if (pos2 >= isl_space_dim(space, type2))
12131 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12132 "index out of bounds", goto error);
12134 if (type1 == type2 && pos1 == pos2)
12135 return isl_basic_map_universe(space);
12137 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12138 i = isl_basic_map_alloc_equality(bmap);
12139 if (i < 0)
12140 goto error;
12141 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12142 pos1 += isl_basic_map_offset(bmap, type1);
12143 pos2 += isl_basic_map_offset(bmap, type2);
12144 isl_int_set_si(bmap->eq[i][pos1], -1);
12145 isl_int_set_si(bmap->eq[i][pos2], 1);
12146 bmap = isl_basic_map_finalize(bmap);
12147 isl_space_free(space);
12148 return bmap;
12149 error:
12150 isl_space_free(space);
12151 isl_basic_map_free(bmap);
12152 return NULL;
12155 /* Add a constraint imposing that the given two dimensions are equal.
12157 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12158 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12160 isl_basic_map *eq;
12162 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12164 bmap = isl_basic_map_intersect(bmap, eq);
12166 return bmap;
12169 /* Add a constraint imposing that the given two dimensions are equal.
12171 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12172 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12174 isl_basic_map *bmap;
12176 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12178 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12180 return map;
12183 /* Add a constraint imposing that the given two dimensions have opposite values.
12185 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12186 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12188 isl_basic_map *bmap = NULL;
12189 int i;
12191 if (!map)
12192 return NULL;
12194 if (pos1 >= isl_map_dim(map, type1))
12195 isl_die(map->ctx, isl_error_invalid,
12196 "index out of bounds", goto error);
12197 if (pos2 >= isl_map_dim(map, type2))
12198 isl_die(map->ctx, isl_error_invalid,
12199 "index out of bounds", goto error);
12201 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12202 i = isl_basic_map_alloc_equality(bmap);
12203 if (i < 0)
12204 goto error;
12205 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12206 pos1 += isl_basic_map_offset(bmap, type1);
12207 pos2 += isl_basic_map_offset(bmap, type2);
12208 isl_int_set_si(bmap->eq[i][pos1], 1);
12209 isl_int_set_si(bmap->eq[i][pos2], 1);
12210 bmap = isl_basic_map_finalize(bmap);
12212 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12214 return map;
12215 error:
12216 isl_basic_map_free(bmap);
12217 isl_map_free(map);
12218 return NULL;
12221 /* Construct a constraint imposing that the value of the first dimension is
12222 * greater than or equal to that of the second.
12224 static __isl_give isl_constraint *constraint_order_ge(
12225 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12226 enum isl_dim_type type2, int pos2)
12228 isl_constraint *c;
12230 if (!space)
12231 return NULL;
12233 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12235 if (pos1 >= isl_constraint_dim(c, type1))
12236 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12237 "index out of bounds", return isl_constraint_free(c));
12238 if (pos2 >= isl_constraint_dim(c, type2))
12239 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12240 "index out of bounds", return isl_constraint_free(c));
12242 if (type1 == type2 && pos1 == pos2)
12243 return c;
12245 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12246 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12248 return c;
12251 /* Add a constraint imposing that the value of the first dimension is
12252 * greater than or equal to that of the second.
12254 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12255 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12257 isl_constraint *c;
12258 isl_space *space;
12260 if (type1 == type2 && pos1 == pos2)
12261 return bmap;
12262 space = isl_basic_map_get_space(bmap);
12263 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12264 bmap = isl_basic_map_add_constraint(bmap, c);
12266 return bmap;
12269 /* Add a constraint imposing that the value of the first dimension is
12270 * greater than or equal to that of the second.
12272 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12273 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12275 isl_constraint *c;
12276 isl_space *space;
12278 if (type1 == type2 && pos1 == pos2)
12279 return map;
12280 space = isl_map_get_space(map);
12281 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12282 map = isl_map_add_constraint(map, c);
12284 return map;
12287 /* Add a constraint imposing that the value of the first dimension is
12288 * less than or equal to that of the second.
12290 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12291 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12293 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12296 /* Construct a basic map where the value of the first dimension is
12297 * greater than that of the second.
12299 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12300 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12302 isl_basic_map *bmap = NULL;
12303 int i;
12305 if (!space)
12306 return NULL;
12308 if (pos1 >= isl_space_dim(space, type1))
12309 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12310 "index out of bounds", goto error);
12311 if (pos2 >= isl_space_dim(space, type2))
12312 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12313 "index out of bounds", goto error);
12315 if (type1 == type2 && pos1 == pos2)
12316 return isl_basic_map_empty(space);
12318 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12319 i = isl_basic_map_alloc_inequality(bmap);
12320 if (i < 0)
12321 return isl_basic_map_free(bmap);
12322 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12323 pos1 += isl_basic_map_offset(bmap, type1);
12324 pos2 += isl_basic_map_offset(bmap, type2);
12325 isl_int_set_si(bmap->ineq[i][pos1], 1);
12326 isl_int_set_si(bmap->ineq[i][pos2], -1);
12327 isl_int_set_si(bmap->ineq[i][0], -1);
12328 bmap = isl_basic_map_finalize(bmap);
12330 return bmap;
12331 error:
12332 isl_space_free(space);
12333 isl_basic_map_free(bmap);
12334 return NULL;
12337 /* Add a constraint imposing that the value of the first dimension is
12338 * greater than that of the second.
12340 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12341 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12343 isl_basic_map *gt;
12345 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12347 bmap = isl_basic_map_intersect(bmap, gt);
12349 return bmap;
12352 /* Add a constraint imposing that the value of the first dimension is
12353 * greater than that of the second.
12355 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12356 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12358 isl_basic_map *bmap;
12360 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12362 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12364 return map;
12367 /* Add a constraint imposing that the value of the first dimension is
12368 * smaller than that of the second.
12370 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12371 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12373 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12376 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12377 int pos)
12379 isl_aff *div;
12380 isl_local_space *ls;
12382 if (!bmap)
12383 return NULL;
12385 if (!isl_basic_map_divs_known(bmap))
12386 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12387 "some divs are unknown", return NULL);
12389 ls = isl_basic_map_get_local_space(bmap);
12390 div = isl_local_space_get_div(ls, pos);
12391 isl_local_space_free(ls);
12393 return div;
12396 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12397 int pos)
12399 return isl_basic_map_get_div(bset, pos);
12402 /* Plug in "subs" for dimension "type", "pos" of "bset".
12404 * Let i be the dimension to replace and let "subs" be of the form
12406 * f/d
12408 * Any integer division with a non-zero coefficient for i,
12410 * floor((a i + g)/m)
12412 * is replaced by
12414 * floor((a f + d g)/(m d))
12416 * Constraints of the form
12418 * a i + g
12420 * are replaced by
12422 * a f + d g
12424 * We currently require that "subs" is an integral expression.
12425 * Handling rational expressions may require us to add stride constraints
12426 * as we do in isl_basic_set_preimage_multi_aff.
12428 __isl_give isl_basic_set *isl_basic_set_substitute(
12429 __isl_take isl_basic_set *bset,
12430 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12432 int i;
12433 isl_int v;
12434 isl_ctx *ctx;
12436 if (bset && isl_basic_set_plain_is_empty(bset))
12437 return bset;
12439 bset = isl_basic_set_cow(bset);
12440 if (!bset || !subs)
12441 goto error;
12443 ctx = isl_basic_set_get_ctx(bset);
12444 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12445 isl_die(ctx, isl_error_invalid,
12446 "spaces don't match", goto error);
12447 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12448 isl_die(ctx, isl_error_unsupported,
12449 "cannot handle divs yet", goto error);
12450 if (!isl_int_is_one(subs->v->el[0]))
12451 isl_die(ctx, isl_error_invalid,
12452 "can only substitute integer expressions", goto error);
12454 pos += isl_basic_set_offset(bset, type);
12456 isl_int_init(v);
12458 for (i = 0; i < bset->n_eq; ++i) {
12459 if (isl_int_is_zero(bset->eq[i][pos]))
12460 continue;
12461 isl_int_set(v, bset->eq[i][pos]);
12462 isl_int_set_si(bset->eq[i][pos], 0);
12463 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12464 v, subs->v->el + 1, subs->v->size - 1);
12467 for (i = 0; i < bset->n_ineq; ++i) {
12468 if (isl_int_is_zero(bset->ineq[i][pos]))
12469 continue;
12470 isl_int_set(v, bset->ineq[i][pos]);
12471 isl_int_set_si(bset->ineq[i][pos], 0);
12472 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12473 v, subs->v->el + 1, subs->v->size - 1);
12476 for (i = 0; i < bset->n_div; ++i) {
12477 if (isl_int_is_zero(bset->div[i][1 + pos]))
12478 continue;
12479 isl_int_set(v, bset->div[i][1 + pos]);
12480 isl_int_set_si(bset->div[i][1 + pos], 0);
12481 isl_seq_combine(bset->div[i] + 1,
12482 subs->v->el[0], bset->div[i] + 1,
12483 v, subs->v->el + 1, subs->v->size - 1);
12484 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12487 isl_int_clear(v);
12489 bset = isl_basic_set_simplify(bset);
12490 return isl_basic_set_finalize(bset);
12491 error:
12492 isl_basic_set_free(bset);
12493 return NULL;
12496 /* Plug in "subs" for dimension "type", "pos" of "set".
12498 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12499 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12501 int i;
12503 if (set && isl_set_plain_is_empty(set))
12504 return set;
12506 set = isl_set_cow(set);
12507 if (!set || !subs)
12508 goto error;
12510 for (i = set->n - 1; i >= 0; --i) {
12511 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12512 if (remove_if_empty(set, i) < 0)
12513 goto error;
12516 return set;
12517 error:
12518 isl_set_free(set);
12519 return NULL;
12522 /* Check if the range of "ma" is compatible with the domain or range
12523 * (depending on "type") of "bmap".
12525 static isl_stat check_basic_map_compatible_range_multi_aff(
12526 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12527 __isl_keep isl_multi_aff *ma)
12529 isl_bool m;
12530 isl_space *ma_space;
12532 ma_space = isl_multi_aff_get_space(ma);
12534 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12535 if (m < 0)
12536 goto error;
12537 if (!m)
12538 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12539 "parameters don't match", goto error);
12540 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12541 if (m < 0)
12542 goto error;
12543 if (!m)
12544 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12545 "spaces don't match", goto error);
12547 isl_space_free(ma_space);
12548 return isl_stat_ok;
12549 error:
12550 isl_space_free(ma_space);
12551 return isl_stat_error;
12554 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12555 * coefficients before the transformed range of dimensions,
12556 * the "n_after" coefficients after the transformed range of dimensions
12557 * and the coefficients of the other divs in "bmap".
12559 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12560 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12562 int i;
12563 int n_param;
12564 int n_set;
12565 isl_local_space *ls;
12567 if (n_div == 0)
12568 return 0;
12570 ls = isl_aff_get_domain_local_space(ma->p[0]);
12571 if (!ls)
12572 return -1;
12574 n_param = isl_local_space_dim(ls, isl_dim_param);
12575 n_set = isl_local_space_dim(ls, isl_dim_set);
12576 for (i = 0; i < n_div; ++i) {
12577 int o_bmap = 0, o_ls = 0;
12579 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12580 o_bmap += 1 + 1 + n_param;
12581 o_ls += 1 + 1 + n_param;
12582 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12583 o_bmap += n_before;
12584 isl_seq_cpy(bmap->div[i] + o_bmap,
12585 ls->div->row[i] + o_ls, n_set);
12586 o_bmap += n_set;
12587 o_ls += n_set;
12588 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12589 o_bmap += n_after;
12590 isl_seq_cpy(bmap->div[i] + o_bmap,
12591 ls->div->row[i] + o_ls, n_div);
12592 o_bmap += n_div;
12593 o_ls += n_div;
12594 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12595 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12596 goto error;
12599 isl_local_space_free(ls);
12600 return 0;
12601 error:
12602 isl_local_space_free(ls);
12603 return -1;
12606 /* How many stride constraints does "ma" enforce?
12607 * That is, how many of the affine expressions have a denominator
12608 * different from one?
12610 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12612 int i;
12613 int strides = 0;
12615 for (i = 0; i < ma->n; ++i)
12616 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12617 strides++;
12619 return strides;
12622 /* For each affine expression in ma of the form
12624 * x_i = (f_i y + h_i)/m_i
12626 * with m_i different from one, add a constraint to "bmap"
12627 * of the form
12629 * f_i y + h_i = m_i alpha_i
12631 * with alpha_i an additional existentially quantified variable.
12633 * The input variables of "ma" correspond to a subset of the variables
12634 * of "bmap". There are "n_before" variables in "bmap" before this
12635 * subset and "n_after" variables after this subset.
12636 * The integer divisions of the affine expressions in "ma" are assumed
12637 * to have been aligned. There are "n_div_ma" of them and
12638 * they appear first in "bmap", straight after the "n_after" variables.
12640 static __isl_give isl_basic_map *add_ma_strides(
12641 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12642 int n_before, int n_after, int n_div_ma)
12644 int i, k;
12645 int div;
12646 int total;
12647 int n_param;
12648 int n_in;
12650 total = isl_basic_map_total_dim(bmap);
12651 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12652 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12653 for (i = 0; i < ma->n; ++i) {
12654 int o_bmap = 0, o_ma = 1;
12656 if (isl_int_is_one(ma->p[i]->v->el[0]))
12657 continue;
12658 div = isl_basic_map_alloc_div(bmap);
12659 k = isl_basic_map_alloc_equality(bmap);
12660 if (div < 0 || k < 0)
12661 goto error;
12662 isl_int_set_si(bmap->div[div][0], 0);
12663 isl_seq_cpy(bmap->eq[k] + o_bmap,
12664 ma->p[i]->v->el + o_ma, 1 + n_param);
12665 o_bmap += 1 + n_param;
12666 o_ma += 1 + n_param;
12667 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12668 o_bmap += n_before;
12669 isl_seq_cpy(bmap->eq[k] + o_bmap,
12670 ma->p[i]->v->el + o_ma, n_in);
12671 o_bmap += n_in;
12672 o_ma += n_in;
12673 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12674 o_bmap += n_after;
12675 isl_seq_cpy(bmap->eq[k] + o_bmap,
12676 ma->p[i]->v->el + o_ma, n_div_ma);
12677 o_bmap += n_div_ma;
12678 o_ma += n_div_ma;
12679 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12680 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12681 total++;
12684 return bmap;
12685 error:
12686 isl_basic_map_free(bmap);
12687 return NULL;
12690 /* Replace the domain or range space (depending on "type) of "space" by "set".
12692 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12693 enum isl_dim_type type, __isl_take isl_space *set)
12695 if (type == isl_dim_in) {
12696 space = isl_space_range(space);
12697 space = isl_space_map_from_domain_and_range(set, space);
12698 } else {
12699 space = isl_space_domain(space);
12700 space = isl_space_map_from_domain_and_range(space, set);
12703 return space;
12706 /* Compute the preimage of the domain or range (depending on "type")
12707 * of "bmap" under the function represented by "ma".
12708 * In other words, plug in "ma" in the domain or range of "bmap".
12709 * The result is a basic map that lives in the same space as "bmap"
12710 * except that the domain or range has been replaced by
12711 * the domain space of "ma".
12713 * If bmap is represented by
12715 * A(p) + S u + B x + T v + C(divs) >= 0,
12717 * where u and x are input and output dimensions if type == isl_dim_out
12718 * while x and v are input and output dimensions if type == isl_dim_in,
12719 * and ma is represented by
12721 * x = D(p) + F(y) + G(divs')
12723 * then the result is
12725 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12727 * The divs in the input set are similarly adjusted.
12728 * In particular
12730 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12732 * becomes
12734 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12735 * B_i G(divs') + c_i(divs))/n_i)
12737 * If bmap is not a rational map and if F(y) involves any denominators
12739 * x_i = (f_i y + h_i)/m_i
12741 * then additional constraints are added to ensure that we only
12742 * map back integer points. That is we enforce
12744 * f_i y + h_i = m_i alpha_i
12746 * with alpha_i an additional existentially quantified variable.
12748 * We first copy over the divs from "ma".
12749 * Then we add the modified constraints and divs from "bmap".
12750 * Finally, we add the stride constraints, if needed.
12752 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12753 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12754 __isl_take isl_multi_aff *ma)
12756 int i, k;
12757 isl_space *space;
12758 isl_basic_map *res = NULL;
12759 int n_before, n_after, n_div_bmap, n_div_ma;
12760 isl_int f, c1, c2, g;
12761 isl_bool rational;
12762 int strides;
12764 isl_int_init(f);
12765 isl_int_init(c1);
12766 isl_int_init(c2);
12767 isl_int_init(g);
12769 ma = isl_multi_aff_align_divs(ma);
12770 if (!bmap || !ma)
12771 goto error;
12772 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12773 goto error;
12775 if (type == isl_dim_in) {
12776 n_before = 0;
12777 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12778 } else {
12779 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12780 n_after = 0;
12782 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12783 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12785 space = isl_multi_aff_get_domain_space(ma);
12786 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12787 rational = isl_basic_map_is_rational(bmap);
12788 strides = rational ? 0 : multi_aff_strides(ma);
12789 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12790 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12791 if (rational)
12792 res = isl_basic_map_set_rational(res);
12794 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12795 if (isl_basic_map_alloc_div(res) < 0)
12796 goto error;
12798 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12799 goto error;
12801 for (i = 0; i < bmap->n_eq; ++i) {
12802 k = isl_basic_map_alloc_equality(res);
12803 if (k < 0)
12804 goto error;
12805 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12806 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12809 for (i = 0; i < bmap->n_ineq; ++i) {
12810 k = isl_basic_map_alloc_inequality(res);
12811 if (k < 0)
12812 goto error;
12813 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12814 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12817 for (i = 0; i < bmap->n_div; ++i) {
12818 if (isl_int_is_zero(bmap->div[i][0])) {
12819 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12820 continue;
12822 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12823 n_before, n_after, n_div_ma, n_div_bmap,
12824 f, c1, c2, g, 1);
12827 if (strides)
12828 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12830 isl_int_clear(f);
12831 isl_int_clear(c1);
12832 isl_int_clear(c2);
12833 isl_int_clear(g);
12834 isl_basic_map_free(bmap);
12835 isl_multi_aff_free(ma);
12836 res = isl_basic_map_simplify(res);
12837 return isl_basic_map_finalize(res);
12838 error:
12839 isl_int_clear(f);
12840 isl_int_clear(c1);
12841 isl_int_clear(c2);
12842 isl_int_clear(g);
12843 isl_basic_map_free(bmap);
12844 isl_multi_aff_free(ma);
12845 isl_basic_map_free(res);
12846 return NULL;
12849 /* Compute the preimage of "bset" under the function represented by "ma".
12850 * In other words, plug in "ma" in "bset". The result is a basic set
12851 * that lives in the domain space of "ma".
12853 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12854 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12856 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12859 /* Compute the preimage of the domain of "bmap" under the function
12860 * represented by "ma".
12861 * In other words, plug in "ma" in the domain of "bmap".
12862 * The result is a basic map that lives in the same space as "bmap"
12863 * except that the domain has been replaced by the domain space of "ma".
12865 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12866 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12868 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12871 /* Compute the preimage of the range of "bmap" under the function
12872 * represented by "ma".
12873 * In other words, plug in "ma" in the range of "bmap".
12874 * The result is a basic map that lives in the same space as "bmap"
12875 * except that the range has been replaced by the domain space of "ma".
12877 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12878 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12880 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12883 /* Check if the range of "ma" is compatible with the domain or range
12884 * (depending on "type") of "map".
12885 * Return isl_stat_error if anything is wrong.
12887 static isl_stat check_map_compatible_range_multi_aff(
12888 __isl_keep isl_map *map, enum isl_dim_type type,
12889 __isl_keep isl_multi_aff *ma)
12891 isl_bool m;
12892 isl_space *ma_space;
12894 ma_space = isl_multi_aff_get_space(ma);
12895 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12896 isl_space_free(ma_space);
12897 if (m < 0)
12898 return isl_stat_error;
12899 if (!m)
12900 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12901 "spaces don't match", return isl_stat_error);
12902 return isl_stat_ok;
12905 /* Compute the preimage of the domain or range (depending on "type")
12906 * of "map" under the function represented by "ma".
12907 * In other words, plug in "ma" in the domain or range of "map".
12908 * The result is a map that lives in the same space as "map"
12909 * except that the domain or range has been replaced by
12910 * the domain space of "ma".
12912 * The parameters are assumed to have been aligned.
12914 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12915 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12917 int i;
12918 isl_space *space;
12920 map = isl_map_cow(map);
12921 ma = isl_multi_aff_align_divs(ma);
12922 if (!map || !ma)
12923 goto error;
12924 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12925 goto error;
12927 for (i = 0; i < map->n; ++i) {
12928 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12929 isl_multi_aff_copy(ma));
12930 if (!map->p[i])
12931 goto error;
12934 space = isl_multi_aff_get_domain_space(ma);
12935 space = isl_space_set(isl_map_get_space(map), type, space);
12937 isl_space_free(map->dim);
12938 map->dim = space;
12939 if (!map->dim)
12940 goto error;
12942 isl_multi_aff_free(ma);
12943 if (map->n > 1)
12944 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12945 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12946 return map;
12947 error:
12948 isl_multi_aff_free(ma);
12949 isl_map_free(map);
12950 return NULL;
12953 /* Compute the preimage of the domain or range (depending on "type")
12954 * of "map" under the function represented by "ma".
12955 * In other words, plug in "ma" in the domain or range of "map".
12956 * The result is a map that lives in the same space as "map"
12957 * except that the domain or range has been replaced by
12958 * the domain space of "ma".
12960 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12961 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12963 if (!map || !ma)
12964 goto error;
12966 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12967 return map_preimage_multi_aff(map, type, ma);
12969 if (!isl_space_has_named_params(map->dim) ||
12970 !isl_space_has_named_params(ma->space))
12971 isl_die(map->ctx, isl_error_invalid,
12972 "unaligned unnamed parameters", goto error);
12973 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12974 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12976 return map_preimage_multi_aff(map, type, ma);
12977 error:
12978 isl_multi_aff_free(ma);
12979 return isl_map_free(map);
12982 /* Compute the preimage of "set" under the function represented by "ma".
12983 * In other words, plug in "ma" in "set". The result is a set
12984 * that lives in the domain space of "ma".
12986 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12987 __isl_take isl_multi_aff *ma)
12989 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12992 /* Compute the preimage of the domain of "map" under the function
12993 * represented by "ma".
12994 * In other words, plug in "ma" in the domain of "map".
12995 * The result is a map that lives in the same space as "map"
12996 * except that the domain has been replaced by the domain space of "ma".
12998 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12999 __isl_take isl_multi_aff *ma)
13001 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13004 /* Compute the preimage of the range of "map" under the function
13005 * represented by "ma".
13006 * In other words, plug in "ma" in the range of "map".
13007 * The result is a map that lives in the same space as "map"
13008 * except that the range has been replaced by the domain space of "ma".
13010 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13011 __isl_take isl_multi_aff *ma)
13013 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13016 /* Compute the preimage of "map" under the function represented by "pma".
13017 * In other words, plug in "pma" in the domain or range of "map".
13018 * The result is a map that lives in the same space as "map",
13019 * except that the space of type "type" has been replaced by
13020 * the domain space of "pma".
13022 * The parameters of "map" and "pma" are assumed to have been aligned.
13024 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13025 __isl_take isl_map *map, enum isl_dim_type type,
13026 __isl_take isl_pw_multi_aff *pma)
13028 int i;
13029 isl_map *res;
13031 if (!pma)
13032 goto error;
13034 if (pma->n == 0) {
13035 isl_pw_multi_aff_free(pma);
13036 res = isl_map_empty(isl_map_get_space(map));
13037 isl_map_free(map);
13038 return res;
13041 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13042 isl_multi_aff_copy(pma->p[0].maff));
13043 if (type == isl_dim_in)
13044 res = isl_map_intersect_domain(res,
13045 isl_map_copy(pma->p[0].set));
13046 else
13047 res = isl_map_intersect_range(res,
13048 isl_map_copy(pma->p[0].set));
13050 for (i = 1; i < pma->n; ++i) {
13051 isl_map *res_i;
13053 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13054 isl_multi_aff_copy(pma->p[i].maff));
13055 if (type == isl_dim_in)
13056 res_i = isl_map_intersect_domain(res_i,
13057 isl_map_copy(pma->p[i].set));
13058 else
13059 res_i = isl_map_intersect_range(res_i,
13060 isl_map_copy(pma->p[i].set));
13061 res = isl_map_union(res, res_i);
13064 isl_pw_multi_aff_free(pma);
13065 isl_map_free(map);
13066 return res;
13067 error:
13068 isl_pw_multi_aff_free(pma);
13069 isl_map_free(map);
13070 return NULL;
13073 /* Compute the preimage of "map" under the function represented by "pma".
13074 * In other words, plug in "pma" in the domain or range of "map".
13075 * The result is a map that lives in the same space as "map",
13076 * except that the space of type "type" has been replaced by
13077 * the domain space of "pma".
13079 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13080 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13082 if (!map || !pma)
13083 goto error;
13085 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13086 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13088 if (!isl_space_has_named_params(map->dim) ||
13089 !isl_space_has_named_params(pma->dim))
13090 isl_die(map->ctx, isl_error_invalid,
13091 "unaligned unnamed parameters", goto error);
13092 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13093 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13095 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13096 error:
13097 isl_pw_multi_aff_free(pma);
13098 return isl_map_free(map);
13101 /* Compute the preimage of "set" under the function represented by "pma".
13102 * In other words, plug in "pma" in "set". The result is a set
13103 * that lives in the domain space of "pma".
13105 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13106 __isl_take isl_pw_multi_aff *pma)
13108 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13111 /* Compute the preimage of the domain of "map" under the function
13112 * represented by "pma".
13113 * In other words, plug in "pma" in the domain of "map".
13114 * The result is a map that lives in the same space as "map",
13115 * except that domain space has been replaced by the domain space of "pma".
13117 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13118 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13120 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13123 /* Compute the preimage of the range of "map" under the function
13124 * represented by "pma".
13125 * In other words, plug in "pma" in the range of "map".
13126 * The result is a map that lives in the same space as "map",
13127 * except that range space has been replaced by the domain space of "pma".
13129 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13130 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13132 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13135 /* Compute the preimage of "map" under the function represented by "mpa".
13136 * In other words, plug in "mpa" in the domain or range of "map".
13137 * The result is a map that lives in the same space as "map",
13138 * except that the space of type "type" has been replaced by
13139 * the domain space of "mpa".
13141 * If the map does not involve any constraints that refer to the
13142 * dimensions of the substituted space, then the only possible
13143 * effect of "mpa" on the map is to map the space to a different space.
13144 * We create a separate isl_multi_aff to effectuate this change
13145 * in order to avoid spurious splitting of the map along the pieces
13146 * of "mpa".
13148 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13149 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13151 int n;
13152 isl_pw_multi_aff *pma;
13154 if (!map || !mpa)
13155 goto error;
13157 n = isl_map_dim(map, type);
13158 if (!isl_map_involves_dims(map, type, 0, n)) {
13159 isl_space *space;
13160 isl_multi_aff *ma;
13162 space = isl_multi_pw_aff_get_space(mpa);
13163 isl_multi_pw_aff_free(mpa);
13164 ma = isl_multi_aff_zero(space);
13165 return isl_map_preimage_multi_aff(map, type, ma);
13168 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13169 return isl_map_preimage_pw_multi_aff(map, type, pma);
13170 error:
13171 isl_map_free(map);
13172 isl_multi_pw_aff_free(mpa);
13173 return NULL;
13176 /* Compute the preimage of "map" under the function represented by "mpa".
13177 * In other words, plug in "mpa" in the domain "map".
13178 * The result is a map that lives in the same space as "map",
13179 * except that domain space has been replaced by the domain space of "mpa".
13181 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13182 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13184 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13187 /* Compute the preimage of "set" by the function represented by "mpa".
13188 * In other words, plug in "mpa" in "set".
13190 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13191 __isl_take isl_multi_pw_aff *mpa)
13193 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13196 /* Are the "n" "coefficients" starting at "first" of the integer division
13197 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13198 * to each other?
13199 * The "coefficient" at position 0 is the denominator.
13200 * The "coefficient" at position 1 is the constant term.
13202 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13203 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13204 unsigned first, unsigned n)
13206 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13207 return isl_bool_error;
13208 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13209 return isl_bool_error;
13210 return isl_seq_eq(bmap1->div[pos1] + first,
13211 bmap2->div[pos2] + first, n);
13214 /* Are the integer division expressions at position "pos1" in "bmap1" and
13215 * "pos2" in "bmap2" equal to each other, except that the constant terms
13216 * are different?
13218 isl_bool isl_basic_map_equal_div_expr_except_constant(
13219 __isl_keep isl_basic_map *bmap1, int pos1,
13220 __isl_keep isl_basic_map *bmap2, int pos2)
13222 isl_bool equal;
13223 unsigned total;
13225 if (!bmap1 || !bmap2)
13226 return isl_bool_error;
13227 total = isl_basic_map_total_dim(bmap1);
13228 if (total != isl_basic_map_total_dim(bmap2))
13229 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13230 "incomparable div expressions", return isl_bool_error);
13231 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13232 0, 1);
13233 if (equal < 0 || !equal)
13234 return equal;
13235 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13236 1, 1);
13237 if (equal < 0 || equal)
13238 return isl_bool_not(equal);
13239 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13240 2, total);
13243 /* Replace the numerator of the constant term of the integer division
13244 * expression at position "div" in "bmap" by "value".
13245 * The caller guarantees that this does not change the meaning
13246 * of the input.
13248 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13249 __isl_take isl_basic_map *bmap, int div, int value)
13251 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13252 return isl_basic_map_free(bmap);
13254 isl_int_set_si(bmap->div[div][1], value);
13256 return bmap;
13259 /* Is the point "inner" internal to inequality constraint "ineq"
13260 * of "bset"?
13261 * The point is considered to be internal to the inequality constraint,
13262 * if it strictly lies on the positive side of the inequality constraint,
13263 * or if it lies on the constraint and the constraint is lexico-positive.
13265 static isl_bool is_internal(__isl_keep isl_vec *inner,
13266 __isl_keep isl_basic_set *bset, int ineq)
13268 isl_ctx *ctx;
13269 int pos;
13270 unsigned total;
13272 if (!inner || !bset)
13273 return isl_bool_error;
13275 ctx = isl_basic_set_get_ctx(bset);
13276 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13277 &ctx->normalize_gcd);
13278 if (!isl_int_is_zero(ctx->normalize_gcd))
13279 return isl_int_is_nonneg(ctx->normalize_gcd);
13281 total = isl_basic_set_dim(bset, isl_dim_all);
13282 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13283 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13286 /* Tighten the inequality constraints of "bset" that are outward with respect
13287 * to the point "vec".
13288 * That is, tighten the constraints that are not satisfied by "vec".
13290 * "vec" is a point internal to some superset S of "bset" that is used
13291 * to make the subsets of S disjoint, by tightening one half of the constraints
13292 * that separate two subsets. In particular, the constraints of S
13293 * are all satisfied by "vec" and should not be tightened.
13294 * Of the internal constraints, those that have "vec" on the outside
13295 * are tightened. The shared facet is included in the adjacent subset
13296 * with the opposite constraint.
13297 * For constraints that saturate "vec", this criterion cannot be used
13298 * to determine which of the two sides should be tightened.
13299 * Instead, the sign of the first non-zero coefficient is used
13300 * to make this choice. Note that this second criterion is never used
13301 * on the constraints of S since "vec" is interior to "S".
13303 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13304 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13306 int j;
13308 bset = isl_basic_set_cow(bset);
13309 if (!bset)
13310 return NULL;
13311 for (j = 0; j < bset->n_ineq; ++j) {
13312 isl_bool internal;
13314 internal = is_internal(vec, bset, j);
13315 if (internal < 0)
13316 return isl_basic_set_free(bset);
13317 if (internal)
13318 continue;
13319 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13322 return bset;