remove possible use of piplib completely
[isl.git] / isl_map.c
blobf3c97e82bc9a6ca5af1e24176e66787acc4fb476
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <string.h>
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include <isl_blk.h>
19 #include "isl_space_private.h"
20 #include "isl_equalities.h"
21 #include <isl_lp_private.h>
22 #include <isl_seq.h>
23 #include <isl/set.h>
24 #include <isl/map.h>
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
27 #include "isl_tab.h"
28 #include <isl/vec.h>
29 #include <isl_mat_private.h>
30 #include <isl_vec_private.h>
31 #include <isl_dim_map.h>
32 #include <isl_local_space_private.h>
33 #include <isl_aff_private.h>
34 #include <isl_options_private.h>
35 #include <isl_morph.h>
36 #include <isl_val_private.h>
37 #include <isl/deprecated/map_int.h>
38 #include <isl/deprecated/set_int.h>
40 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
42 switch (type) {
43 case isl_dim_param: return dim->nparam;
44 case isl_dim_in: return dim->n_in;
45 case isl_dim_out: return dim->n_out;
46 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
47 default: return 0;
51 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
53 switch (type) {
54 case isl_dim_param: return 1;
55 case isl_dim_in: return 1 + dim->nparam;
56 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
57 default: return 0;
61 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
62 enum isl_dim_type type)
64 if (!bmap)
65 return 0;
66 switch (type) {
67 case isl_dim_cst: return 1;
68 case isl_dim_param:
69 case isl_dim_in:
70 case isl_dim_out: return isl_space_dim(bmap->dim, type);
71 case isl_dim_div: return bmap->n_div;
72 case isl_dim_all: return isl_basic_map_total_dim(bmap);
73 default: return 0;
77 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
79 return map ? n(map->dim, type) : 0;
82 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
84 return set ? n(set->dim, type) : 0;
87 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
88 enum isl_dim_type type)
90 isl_space *dim = bmap->dim;
91 switch (type) {
92 case isl_dim_cst: return 0;
93 case isl_dim_param: return 1;
94 case isl_dim_in: return 1 + dim->nparam;
95 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
96 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
97 default: return 0;
101 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
102 enum isl_dim_type type)
104 return isl_basic_map_offset(bset, type);
107 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
109 return pos(map->dim, type);
112 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
113 enum isl_dim_type type)
115 return isl_basic_map_dim(bset, type);
118 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
120 return isl_basic_set_dim(bset, isl_dim_set);
123 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
125 return isl_basic_set_dim(bset, isl_dim_param);
128 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
130 if (!bset)
131 return 0;
132 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
135 unsigned isl_set_n_dim(__isl_keep isl_set *set)
137 return isl_set_dim(set, isl_dim_set);
140 unsigned isl_set_n_param(__isl_keep isl_set *set)
142 return isl_set_dim(set, isl_dim_param);
145 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
147 return bmap ? bmap->dim->n_in : 0;
150 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
152 return bmap ? bmap->dim->n_out : 0;
155 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
157 return bmap ? bmap->dim->nparam : 0;
160 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
162 return bmap ? bmap->n_div : 0;
165 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
167 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
170 unsigned isl_map_n_in(const struct isl_map *map)
172 return map ? map->dim->n_in : 0;
175 unsigned isl_map_n_out(const struct isl_map *map)
177 return map ? map->dim->n_out : 0;
180 unsigned isl_map_n_param(const struct isl_map *map)
182 return map ? map->dim->nparam : 0;
185 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
187 int m;
188 if (!map || !set)
189 return -1;
190 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
191 if (m < 0 || !m)
192 return m;
193 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
196 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
197 struct isl_basic_set *bset)
199 int m;
200 if (!bmap || !bset)
201 return -1;
202 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
203 if (m < 0 || !m)
204 return m;
205 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
208 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
210 int m;
211 if (!map || !set)
212 return -1;
213 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
214 if (m < 0 || !m)
215 return m;
216 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
219 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
220 struct isl_basic_set *bset)
222 int m;
223 if (!bmap || !bset)
224 return -1;
225 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
226 if (m < 0 || !m)
227 return m;
228 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
231 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
233 return bmap ? bmap->ctx : NULL;
236 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
238 return bset ? bset->ctx : NULL;
241 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
243 return map ? map->ctx : NULL;
246 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
248 return set ? set->ctx : NULL;
251 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
253 if (!bmap)
254 return NULL;
255 return isl_space_copy(bmap->dim);
258 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
260 if (!bset)
261 return NULL;
262 return isl_space_copy(bset->dim);
265 /* Extract the divs in "bmap" as a matrix.
267 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
269 int i;
270 isl_ctx *ctx;
271 isl_mat *div;
272 unsigned total;
273 unsigned cols;
275 if (!bmap)
276 return NULL;
278 ctx = isl_basic_map_get_ctx(bmap);
279 total = isl_space_dim(bmap->dim, isl_dim_all);
280 cols = 1 + 1 + total + bmap->n_div;
281 div = isl_mat_alloc(ctx, bmap->n_div, cols);
282 if (!div)
283 return NULL;
285 for (i = 0; i < bmap->n_div; ++i)
286 isl_seq_cpy(div->row[i], bmap->div[i], cols);
288 return div;
291 /* Extract the divs in "bset" as a matrix.
293 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
295 return isl_basic_map_get_divs(bset);
298 __isl_give isl_local_space *isl_basic_map_get_local_space(
299 __isl_keep isl_basic_map *bmap)
301 isl_mat *div;
303 if (!bmap)
304 return NULL;
306 div = isl_basic_map_get_divs(bmap);
307 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
310 __isl_give isl_local_space *isl_basic_set_get_local_space(
311 __isl_keep isl_basic_set *bset)
313 return isl_basic_map_get_local_space(bset);
316 __isl_give isl_basic_map *isl_basic_map_from_local_space(
317 __isl_take isl_local_space *ls)
319 int i;
320 int n_div;
321 isl_basic_map *bmap;
323 if (!ls)
324 return NULL;
326 n_div = isl_local_space_dim(ls, isl_dim_div);
327 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
328 n_div, 0, 2 * n_div);
330 for (i = 0; i < n_div; ++i)
331 if (isl_basic_map_alloc_div(bmap) < 0)
332 goto error;
334 for (i = 0; i < n_div; ++i) {
335 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
336 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
337 goto error;
340 isl_local_space_free(ls);
341 return bmap;
342 error:
343 isl_local_space_free(ls);
344 isl_basic_map_free(bmap);
345 return NULL;
348 __isl_give isl_basic_set *isl_basic_set_from_local_space(
349 __isl_take isl_local_space *ls)
351 return isl_basic_map_from_local_space(ls);
354 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
356 if (!map)
357 return NULL;
358 return isl_space_copy(map->dim);
361 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
363 if (!set)
364 return NULL;
365 return isl_space_copy(set->dim);
368 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
369 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
371 bmap = isl_basic_map_cow(bmap);
372 if (!bmap)
373 return NULL;
374 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
375 if (!bmap->dim)
376 goto error;
377 bmap = isl_basic_map_finalize(bmap);
378 return bmap;
379 error:
380 isl_basic_map_free(bmap);
381 return NULL;
384 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
385 __isl_take isl_basic_set *bset, const char *s)
387 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
390 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
391 enum isl_dim_type type)
393 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
396 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
397 enum isl_dim_type type, const char *s)
399 int i;
401 map = isl_map_cow(map);
402 if (!map)
403 return NULL;
405 map->dim = isl_space_set_tuple_name(map->dim, type, s);
406 if (!map->dim)
407 goto error;
409 for (i = 0; i < map->n; ++i) {
410 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
411 if (!map->p[i])
412 goto error;
415 return map;
416 error:
417 isl_map_free(map);
418 return NULL;
421 /* Does the input or output tuple have a name?
423 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
425 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
428 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
429 enum isl_dim_type type)
431 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
434 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
435 const char *s)
437 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
440 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
441 enum isl_dim_type type, __isl_take isl_id *id)
443 map = isl_map_cow(map);
444 if (!map)
445 return isl_id_free(id);
447 map->dim = isl_space_set_tuple_id(map->dim, type, id);
449 return isl_map_reset_space(map, isl_space_copy(map->dim));
452 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
453 __isl_take isl_id *id)
455 return isl_map_set_tuple_id(set, isl_dim_set, id);
458 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
459 enum isl_dim_type type)
461 map = isl_map_cow(map);
462 if (!map)
463 return NULL;
465 map->dim = isl_space_reset_tuple_id(map->dim, type);
467 return isl_map_reset_space(map, isl_space_copy(map->dim));
470 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
472 return isl_map_reset_tuple_id(set, isl_dim_set);
475 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
477 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
480 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
481 enum isl_dim_type type)
483 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
486 int isl_set_has_tuple_id(__isl_keep isl_set *set)
488 return isl_map_has_tuple_id(set, isl_dim_set);
491 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
493 return isl_map_get_tuple_id(set, isl_dim_set);
496 /* Does the set tuple have a name?
498 int isl_set_has_tuple_name(__isl_keep isl_set *set)
500 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
504 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
506 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
509 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
511 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
514 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
515 enum isl_dim_type type, unsigned pos)
517 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
520 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
521 enum isl_dim_type type, unsigned pos)
523 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
526 /* Does the given dimension have a name?
528 int isl_map_has_dim_name(__isl_keep isl_map *map,
529 enum isl_dim_type type, unsigned pos)
531 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
534 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
535 enum isl_dim_type type, unsigned pos)
537 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
540 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
541 enum isl_dim_type type, unsigned pos)
543 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
546 /* Does the given dimension have a name?
548 int isl_set_has_dim_name(__isl_keep isl_set *set,
549 enum isl_dim_type type, unsigned pos)
551 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
554 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
555 __isl_take isl_basic_map *bmap,
556 enum isl_dim_type type, unsigned pos, const char *s)
558 bmap = isl_basic_map_cow(bmap);
559 if (!bmap)
560 return NULL;
561 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
562 if (!bmap->dim)
563 goto error;
564 return isl_basic_map_finalize(bmap);
565 error:
566 isl_basic_map_free(bmap);
567 return NULL;
570 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
571 enum isl_dim_type type, unsigned pos, const char *s)
573 int i;
575 map = isl_map_cow(map);
576 if (!map)
577 return NULL;
579 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
580 if (!map->dim)
581 goto error;
583 for (i = 0; i < map->n; ++i) {
584 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
585 if (!map->p[i])
586 goto error;
589 return map;
590 error:
591 isl_map_free(map);
592 return NULL;
595 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
596 __isl_take isl_basic_set *bset,
597 enum isl_dim_type type, unsigned pos, const char *s)
599 return (isl_basic_set *)isl_basic_map_set_dim_name(
600 (isl_basic_map *)bset, type, pos, s);
603 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
604 enum isl_dim_type type, unsigned pos, const char *s)
606 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
609 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
610 enum isl_dim_type type, unsigned pos)
612 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
615 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
616 enum isl_dim_type type, unsigned pos)
618 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
621 int isl_map_has_dim_id(__isl_keep isl_map *map,
622 enum isl_dim_type type, unsigned pos)
624 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
627 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
628 enum isl_dim_type type, unsigned pos)
630 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
633 int isl_set_has_dim_id(__isl_keep isl_set *set,
634 enum isl_dim_type type, unsigned pos)
636 return isl_map_has_dim_id(set, type, pos);
639 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
640 enum isl_dim_type type, unsigned pos)
642 return isl_map_get_dim_id(set, type, pos);
645 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
646 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
648 map = isl_map_cow(map);
649 if (!map)
650 return isl_id_free(id);
652 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
654 return isl_map_reset_space(map, isl_space_copy(map->dim));
657 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
658 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
660 return isl_map_set_dim_id(set, type, pos, id);
663 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
664 __isl_keep isl_id *id)
666 if (!map)
667 return -1;
668 return isl_space_find_dim_by_id(map->dim, type, id);
671 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
672 __isl_keep isl_id *id)
674 return isl_map_find_dim_by_id(set, type, id);
677 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
678 const char *name)
680 if (!map)
681 return -1;
682 return isl_space_find_dim_by_name(map->dim, type, name);
685 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
686 const char *name)
688 return isl_map_find_dim_by_name(set, type, name);
691 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
693 if (!bmap)
694 return -1;
695 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
698 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
700 return isl_basic_map_is_rational(bset);
703 /* Does "bmap" contain any rational points?
705 * If "bmap" has an equality for each dimension, equating the dimension
706 * to an integer constant, then it has no rational points, even if it
707 * is marked as rational.
709 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
711 int has_rational = 1;
712 unsigned total;
714 if (!bmap)
715 return -1;
716 if (isl_basic_map_plain_is_empty(bmap))
717 return 0;
718 if (!isl_basic_map_is_rational(bmap))
719 return 0;
720 bmap = isl_basic_map_copy(bmap);
721 bmap = isl_basic_map_implicit_equalities(bmap);
722 if (!bmap)
723 return -1;
724 total = isl_basic_map_total_dim(bmap);
725 if (bmap->n_eq == total) {
726 int i, j;
727 for (i = 0; i < bmap->n_eq; ++i) {
728 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
729 if (j < 0)
730 break;
731 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
732 !isl_int_is_negone(bmap->eq[i][1 + j]))
733 break;
734 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
735 total - j - 1);
736 if (j >= 0)
737 break;
739 if (i == bmap->n_eq)
740 has_rational = 0;
742 isl_basic_map_free(bmap);
744 return has_rational;
747 /* Does "map" contain any rational points?
749 int isl_map_has_rational(__isl_keep isl_map *map)
751 int i;
752 int has_rational;
754 if (!map)
755 return -1;
756 for (i = 0; i < map->n; ++i) {
757 has_rational = isl_basic_map_has_rational(map->p[i]);
758 if (has_rational < 0)
759 return -1;
760 if (has_rational)
761 return 1;
763 return 0;
766 /* Does "set" contain any rational points?
768 int isl_set_has_rational(__isl_keep isl_set *set)
770 return isl_map_has_rational(set);
773 /* Is this basic set a parameter domain?
775 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
777 if (!bset)
778 return -1;
779 return isl_space_is_params(bset->dim);
782 /* Is this set a parameter domain?
784 int isl_set_is_params(__isl_keep isl_set *set)
786 if (!set)
787 return -1;
788 return isl_space_is_params(set->dim);
791 /* Is this map actually a parameter domain?
792 * Users should never call this function. Outside of isl,
793 * a map can never be a parameter domain.
795 int isl_map_is_params(__isl_keep isl_map *map)
797 if (!map)
798 return -1;
799 return isl_space_is_params(map->dim);
802 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
803 struct isl_basic_map *bmap, unsigned extra,
804 unsigned n_eq, unsigned n_ineq)
806 int i;
807 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
809 bmap->ctx = ctx;
810 isl_ctx_ref(ctx);
812 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
813 if (isl_blk_is_error(bmap->block))
814 goto error;
816 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
817 if (!bmap->ineq)
818 goto error;
820 if (extra == 0) {
821 bmap->block2 = isl_blk_empty();
822 bmap->div = NULL;
823 } else {
824 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
825 if (isl_blk_is_error(bmap->block2))
826 goto error;
828 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
829 if (!bmap->div)
830 goto error;
833 for (i = 0; i < n_ineq + n_eq; ++i)
834 bmap->ineq[i] = bmap->block.data + i * row_size;
836 for (i = 0; i < extra; ++i)
837 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
839 bmap->ref = 1;
840 bmap->flags = 0;
841 bmap->c_size = n_eq + n_ineq;
842 bmap->eq = bmap->ineq + n_ineq;
843 bmap->extra = extra;
844 bmap->n_eq = 0;
845 bmap->n_ineq = 0;
846 bmap->n_div = 0;
847 bmap->sample = NULL;
849 return bmap;
850 error:
851 isl_basic_map_free(bmap);
852 return NULL;
855 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
856 unsigned nparam, unsigned dim, unsigned extra,
857 unsigned n_eq, unsigned n_ineq)
859 struct isl_basic_map *bmap;
860 isl_space *space;
862 space = isl_space_set_alloc(ctx, nparam, dim);
863 if (!space)
864 return NULL;
866 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
867 return (struct isl_basic_set *)bmap;
870 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
871 unsigned extra, unsigned n_eq, unsigned n_ineq)
873 struct isl_basic_map *bmap;
874 if (!dim)
875 return NULL;
876 isl_assert(dim->ctx, dim->n_in == 0, goto error);
877 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
878 return (struct isl_basic_set *)bmap;
879 error:
880 isl_space_free(dim);
881 return NULL;
884 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
885 unsigned extra, unsigned n_eq, unsigned n_ineq)
887 struct isl_basic_map *bmap;
889 if (!dim)
890 return NULL;
891 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
892 if (!bmap)
893 goto error;
894 bmap->dim = dim;
896 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
897 error:
898 isl_space_free(dim);
899 return NULL;
902 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
903 unsigned nparam, unsigned in, unsigned out, unsigned extra,
904 unsigned n_eq, unsigned n_ineq)
906 struct isl_basic_map *bmap;
907 isl_space *dim;
909 dim = isl_space_alloc(ctx, nparam, in, out);
910 if (!dim)
911 return NULL;
913 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
914 return bmap;
917 static void dup_constraints(
918 struct isl_basic_map *dst, struct isl_basic_map *src)
920 int i;
921 unsigned total = isl_basic_map_total_dim(src);
923 for (i = 0; i < src->n_eq; ++i) {
924 int j = isl_basic_map_alloc_equality(dst);
925 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
928 for (i = 0; i < src->n_ineq; ++i) {
929 int j = isl_basic_map_alloc_inequality(dst);
930 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
933 for (i = 0; i < src->n_div; ++i) {
934 int j = isl_basic_map_alloc_div(dst);
935 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
937 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
940 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
942 struct isl_basic_map *dup;
944 if (!bmap)
945 return NULL;
946 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
947 bmap->n_div, bmap->n_eq, bmap->n_ineq);
948 if (!dup)
949 return NULL;
950 dup_constraints(dup, bmap);
951 dup->flags = bmap->flags;
952 dup->sample = isl_vec_copy(bmap->sample);
953 return dup;
956 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
958 struct isl_basic_map *dup;
960 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
961 return (struct isl_basic_set *)dup;
964 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
966 if (!bset)
967 return NULL;
969 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
970 bset->ref++;
971 return bset;
973 return isl_basic_set_dup(bset);
976 struct isl_set *isl_set_copy(struct isl_set *set)
978 if (!set)
979 return NULL;
981 set->ref++;
982 return set;
985 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
987 if (!bmap)
988 return NULL;
990 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
991 bmap->ref++;
992 return bmap;
994 bmap = isl_basic_map_dup(bmap);
995 if (bmap)
996 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
997 return bmap;
1000 struct isl_map *isl_map_copy(struct isl_map *map)
1002 if (!map)
1003 return NULL;
1005 map->ref++;
1006 return map;
1009 void *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1011 if (!bmap)
1012 return NULL;
1014 if (--bmap->ref > 0)
1015 return NULL;
1017 isl_ctx_deref(bmap->ctx);
1018 free(bmap->div);
1019 isl_blk_free(bmap->ctx, bmap->block2);
1020 free(bmap->ineq);
1021 isl_blk_free(bmap->ctx, bmap->block);
1022 isl_vec_free(bmap->sample);
1023 isl_space_free(bmap->dim);
1024 free(bmap);
1026 return NULL;
1029 void *isl_basic_set_free(struct isl_basic_set *bset)
1031 return isl_basic_map_free((struct isl_basic_map *)bset);
1034 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1036 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1039 __isl_give isl_map *isl_map_align_params_map_map_and(
1040 __isl_take isl_map *map1, __isl_take isl_map *map2,
1041 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1042 __isl_take isl_map *map2))
1044 if (!map1 || !map2)
1045 goto error;
1046 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1047 return fn(map1, map2);
1048 if (!isl_space_has_named_params(map1->dim) ||
1049 !isl_space_has_named_params(map2->dim))
1050 isl_die(map1->ctx, isl_error_invalid,
1051 "unaligned unnamed parameters", goto error);
1052 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1053 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1054 return fn(map1, map2);
1055 error:
1056 isl_map_free(map1);
1057 isl_map_free(map2);
1058 return NULL;
1061 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1062 __isl_keep isl_map *map2,
1063 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1065 int r;
1067 if (!map1 || !map2)
1068 return -1;
1069 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1070 return fn(map1, map2);
1071 if (!isl_space_has_named_params(map1->dim) ||
1072 !isl_space_has_named_params(map2->dim))
1073 isl_die(map1->ctx, isl_error_invalid,
1074 "unaligned unnamed parameters", return -1);
1075 map1 = isl_map_copy(map1);
1076 map2 = isl_map_copy(map2);
1077 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1078 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1079 r = fn(map1, map2);
1080 isl_map_free(map1);
1081 isl_map_free(map2);
1082 return r;
1085 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1087 struct isl_ctx *ctx;
1088 if (!bmap)
1089 return -1;
1090 ctx = bmap->ctx;
1091 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1092 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1093 return -1);
1094 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1095 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1096 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1097 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1098 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1099 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1100 isl_int *t;
1101 int j = isl_basic_map_alloc_inequality(bmap);
1102 if (j < 0)
1103 return -1;
1104 t = bmap->ineq[j];
1105 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1106 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1107 bmap->eq[-1] = t;
1108 bmap->n_eq++;
1109 bmap->n_ineq--;
1110 bmap->eq--;
1111 return 0;
1113 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1114 bmap->extra - bmap->n_div);
1115 return bmap->n_eq++;
1118 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1120 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1123 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1125 if (!bmap)
1126 return -1;
1127 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1128 bmap->n_eq -= n;
1129 return 0;
1132 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1134 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1137 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1139 isl_int *t;
1140 if (!bmap)
1141 return -1;
1142 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1144 if (pos != bmap->n_eq - 1) {
1145 t = bmap->eq[pos];
1146 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1147 bmap->eq[bmap->n_eq - 1] = t;
1149 bmap->n_eq--;
1150 return 0;
1153 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1155 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1158 /* Turn inequality "pos" of "bmap" into an equality.
1160 * In particular, we move the inequality in front of the equalities
1161 * and move the last inequality in the position of the moved inequality.
1162 * Note that isl_tab_make_equalities_explicit depends on this particular
1163 * change in the ordering of the constraints.
1165 void isl_basic_map_inequality_to_equality(
1166 struct isl_basic_map *bmap, unsigned pos)
1168 isl_int *t;
1170 t = bmap->ineq[pos];
1171 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1172 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1173 bmap->eq[-1] = t;
1174 bmap->n_eq++;
1175 bmap->n_ineq--;
1176 bmap->eq--;
1177 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1178 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1179 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1180 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1183 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1185 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1188 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1190 struct isl_ctx *ctx;
1191 if (!bmap)
1192 return -1;
1193 ctx = bmap->ctx;
1194 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1195 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1196 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1197 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1198 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1199 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1200 1 + isl_basic_map_total_dim(bmap),
1201 bmap->extra - bmap->n_div);
1202 return bmap->n_ineq++;
1205 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1207 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1210 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1212 if (!bmap)
1213 return -1;
1214 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1215 bmap->n_ineq -= n;
1216 return 0;
1219 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1221 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1224 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1226 isl_int *t;
1227 if (!bmap)
1228 return -1;
1229 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1231 if (pos != bmap->n_ineq - 1) {
1232 t = bmap->ineq[pos];
1233 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1234 bmap->ineq[bmap->n_ineq - 1] = t;
1235 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1237 bmap->n_ineq--;
1238 return 0;
1241 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1243 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1246 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1247 isl_int *eq)
1249 int k;
1251 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1252 if (!bmap)
1253 return NULL;
1254 k = isl_basic_map_alloc_equality(bmap);
1255 if (k < 0)
1256 goto error;
1257 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1258 return bmap;
1259 error:
1260 isl_basic_map_free(bmap);
1261 return NULL;
1264 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1265 isl_int *eq)
1267 return (isl_basic_set *)
1268 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1271 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1272 isl_int *ineq)
1274 int k;
1276 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1277 if (!bmap)
1278 return NULL;
1279 k = isl_basic_map_alloc_inequality(bmap);
1280 if (k < 0)
1281 goto error;
1282 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1283 return bmap;
1284 error:
1285 isl_basic_map_free(bmap);
1286 return NULL;
1289 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1290 isl_int *ineq)
1292 return (isl_basic_set *)
1293 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1296 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1298 if (!bmap)
1299 return -1;
1300 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1301 isl_seq_clr(bmap->div[bmap->n_div] +
1302 1 + 1 + isl_basic_map_total_dim(bmap),
1303 bmap->extra - bmap->n_div);
1304 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1305 return bmap->n_div++;
1308 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1310 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1313 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1315 if (!bmap)
1316 return -1;
1317 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1318 bmap->n_div -= n;
1319 return 0;
1322 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1324 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1327 /* Copy constraint from src to dst, putting the vars of src at offset
1328 * dim_off in dst and the divs of src at offset div_off in dst.
1329 * If both sets are actually map, then dim_off applies to the input
1330 * variables.
1332 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1333 struct isl_basic_map *src_map, isl_int *src,
1334 unsigned in_off, unsigned out_off, unsigned div_off)
1336 unsigned src_nparam = isl_basic_map_n_param(src_map);
1337 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1338 unsigned src_in = isl_basic_map_n_in(src_map);
1339 unsigned dst_in = isl_basic_map_n_in(dst_map);
1340 unsigned src_out = isl_basic_map_n_out(src_map);
1341 unsigned dst_out = isl_basic_map_n_out(dst_map);
1342 isl_int_set(dst[0], src[0]);
1343 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1344 if (dst_nparam > src_nparam)
1345 isl_seq_clr(dst+1+src_nparam,
1346 dst_nparam - src_nparam);
1347 isl_seq_clr(dst+1+dst_nparam, in_off);
1348 isl_seq_cpy(dst+1+dst_nparam+in_off,
1349 src+1+src_nparam,
1350 isl_min(dst_in-in_off, src_in));
1351 if (dst_in-in_off > src_in)
1352 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1353 dst_in - in_off - src_in);
1354 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1355 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1356 src+1+src_nparam+src_in,
1357 isl_min(dst_out-out_off, src_out));
1358 if (dst_out-out_off > src_out)
1359 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1360 dst_out - out_off - src_out);
1361 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1362 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1363 src+1+src_nparam+src_in+src_out,
1364 isl_min(dst_map->extra-div_off, src_map->n_div));
1365 if (dst_map->n_div-div_off > src_map->n_div)
1366 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1367 div_off+src_map->n_div,
1368 dst_map->n_div - div_off - src_map->n_div);
1371 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1372 struct isl_basic_map *src_map, isl_int *src,
1373 unsigned in_off, unsigned out_off, unsigned div_off)
1375 isl_int_set(dst[0], src[0]);
1376 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1379 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1380 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1382 int i;
1383 unsigned div_off;
1385 if (!bmap1 || !bmap2)
1386 goto error;
1388 div_off = bmap1->n_div;
1390 for (i = 0; i < bmap2->n_eq; ++i) {
1391 int i1 = isl_basic_map_alloc_equality(bmap1);
1392 if (i1 < 0)
1393 goto error;
1394 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1395 i_pos, o_pos, div_off);
1398 for (i = 0; i < bmap2->n_ineq; ++i) {
1399 int i1 = isl_basic_map_alloc_inequality(bmap1);
1400 if (i1 < 0)
1401 goto error;
1402 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1403 i_pos, o_pos, div_off);
1406 for (i = 0; i < bmap2->n_div; ++i) {
1407 int i1 = isl_basic_map_alloc_div(bmap1);
1408 if (i1 < 0)
1409 goto error;
1410 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1411 i_pos, o_pos, div_off);
1414 isl_basic_map_free(bmap2);
1416 return bmap1;
1418 error:
1419 isl_basic_map_free(bmap1);
1420 isl_basic_map_free(bmap2);
1421 return NULL;
1424 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1425 struct isl_basic_set *bset2, unsigned pos)
1427 return (struct isl_basic_set *)
1428 add_constraints((struct isl_basic_map *)bset1,
1429 (struct isl_basic_map *)bset2, 0, pos);
1432 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1433 __isl_take isl_space *dim, unsigned extra,
1434 unsigned n_eq, unsigned n_ineq)
1436 struct isl_basic_map *ext;
1437 unsigned flags;
1438 int dims_ok;
1440 if (!dim)
1441 goto error;
1443 if (!base)
1444 goto error;
1446 dims_ok = isl_space_is_equal(base->dim, dim) &&
1447 base->extra >= base->n_div + extra;
1449 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1450 room_for_ineq(base, n_ineq)) {
1451 isl_space_free(dim);
1452 return base;
1455 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1456 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1457 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1458 extra += base->extra;
1459 n_eq += base->n_eq;
1460 n_ineq += base->n_ineq;
1462 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1463 dim = NULL;
1464 if (!ext)
1465 goto error;
1467 if (dims_ok)
1468 ext->sample = isl_vec_copy(base->sample);
1469 flags = base->flags;
1470 ext = add_constraints(ext, base, 0, 0);
1471 if (ext) {
1472 ext->flags = flags;
1473 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1476 return ext;
1478 error:
1479 isl_space_free(dim);
1480 isl_basic_map_free(base);
1481 return NULL;
1484 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1485 __isl_take isl_space *dim, unsigned extra,
1486 unsigned n_eq, unsigned n_ineq)
1488 return (struct isl_basic_set *)
1489 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1490 extra, n_eq, n_ineq);
1493 struct isl_basic_map *isl_basic_map_extend_constraints(
1494 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1496 if (!base)
1497 return NULL;
1498 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1499 0, n_eq, n_ineq);
1502 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1503 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1504 unsigned n_eq, unsigned n_ineq)
1506 struct isl_basic_map *bmap;
1507 isl_space *dim;
1509 if (!base)
1510 return NULL;
1511 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1512 if (!dim)
1513 goto error;
1515 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1516 return bmap;
1517 error:
1518 isl_basic_map_free(base);
1519 return NULL;
1522 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1523 unsigned nparam, unsigned dim, unsigned extra,
1524 unsigned n_eq, unsigned n_ineq)
1526 return (struct isl_basic_set *)
1527 isl_basic_map_extend((struct isl_basic_map *)base,
1528 nparam, 0, dim, extra, n_eq, n_ineq);
1531 struct isl_basic_set *isl_basic_set_extend_constraints(
1532 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1534 return (struct isl_basic_set *)
1535 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1536 n_eq, n_ineq);
1539 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1541 return (struct isl_basic_set *)
1542 isl_basic_map_cow((struct isl_basic_map *)bset);
1545 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1547 if (!bmap)
1548 return NULL;
1550 if (bmap->ref > 1) {
1551 bmap->ref--;
1552 bmap = isl_basic_map_dup(bmap);
1554 if (bmap)
1555 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1556 return bmap;
1559 struct isl_set *isl_set_cow(struct isl_set *set)
1561 if (!set)
1562 return NULL;
1564 if (set->ref == 1)
1565 return set;
1566 set->ref--;
1567 return isl_set_dup(set);
1570 struct isl_map *isl_map_cow(struct isl_map *map)
1572 if (!map)
1573 return NULL;
1575 if (map->ref == 1)
1576 return map;
1577 map->ref--;
1578 return isl_map_dup(map);
1581 static void swap_vars(struct isl_blk blk, isl_int *a,
1582 unsigned a_len, unsigned b_len)
1584 isl_seq_cpy(blk.data, a+a_len, b_len);
1585 isl_seq_cpy(blk.data+b_len, a, a_len);
1586 isl_seq_cpy(a, blk.data, b_len+a_len);
1589 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1590 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1592 int i;
1593 struct isl_blk blk;
1595 if (!bmap)
1596 goto error;
1598 isl_assert(bmap->ctx,
1599 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1601 if (n1 == 0 || n2 == 0)
1602 return bmap;
1604 bmap = isl_basic_map_cow(bmap);
1605 if (!bmap)
1606 return NULL;
1608 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1609 if (isl_blk_is_error(blk))
1610 goto error;
1612 for (i = 0; i < bmap->n_eq; ++i)
1613 swap_vars(blk,
1614 bmap->eq[i] + pos, n1, n2);
1616 for (i = 0; i < bmap->n_ineq; ++i)
1617 swap_vars(blk,
1618 bmap->ineq[i] + pos, n1, n2);
1620 for (i = 0; i < bmap->n_div; ++i)
1621 swap_vars(blk,
1622 bmap->div[i]+1 + pos, n1, n2);
1624 isl_blk_free(bmap->ctx, blk);
1626 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1627 bmap = isl_basic_map_gauss(bmap, NULL);
1628 return isl_basic_map_finalize(bmap);
1629 error:
1630 isl_basic_map_free(bmap);
1631 return NULL;
1634 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1635 __isl_take isl_basic_set *bset, unsigned n)
1637 unsigned dim;
1638 unsigned nparam;
1640 if (!bset)
1641 return NULL;
1643 nparam = isl_basic_set_n_param(bset);
1644 dim = isl_basic_set_n_dim(bset);
1645 isl_assert(bset->ctx, n <= dim, goto error);
1647 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1648 error:
1649 isl_basic_set_free(bset);
1650 return NULL;
1653 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1655 int i = 0;
1656 unsigned total;
1657 if (!bmap)
1658 goto error;
1659 total = isl_basic_map_total_dim(bmap);
1660 isl_basic_map_free_div(bmap, bmap->n_div);
1661 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1662 if (bmap->n_eq > 0)
1663 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1664 else {
1665 i = isl_basic_map_alloc_equality(bmap);
1666 if (i < 0)
1667 goto error;
1669 isl_int_set_si(bmap->eq[i][0], 1);
1670 isl_seq_clr(bmap->eq[i]+1, total);
1671 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1672 isl_vec_free(bmap->sample);
1673 bmap->sample = NULL;
1674 return isl_basic_map_finalize(bmap);
1675 error:
1676 isl_basic_map_free(bmap);
1677 return NULL;
1680 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1682 return (struct isl_basic_set *)
1683 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1686 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1687 * of "bmap").
1689 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1691 isl_int *t = bmap->div[a];
1692 bmap->div[a] = bmap->div[b];
1693 bmap->div[b] = t;
1696 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1697 * div definitions accordingly.
1699 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1701 int i;
1702 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1704 swap_div(bmap, a, b);
1706 for (i = 0; i < bmap->n_eq; ++i)
1707 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1709 for (i = 0; i < bmap->n_ineq; ++i)
1710 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1712 for (i = 0; i < bmap->n_div; ++i)
1713 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1714 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1717 /* Eliminate the specified n dimensions starting at first from the
1718 * constraints, without removing the dimensions from the space.
1719 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1721 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1722 enum isl_dim_type type, unsigned first, unsigned n)
1724 int i;
1726 if (!map)
1727 return NULL;
1728 if (n == 0)
1729 return map;
1731 if (first + n > isl_map_dim(map, type) || first + n < first)
1732 isl_die(map->ctx, isl_error_invalid,
1733 "index out of bounds", goto error);
1735 map = isl_map_cow(map);
1736 if (!map)
1737 return NULL;
1739 for (i = 0; i < map->n; ++i) {
1740 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1741 if (!map->p[i])
1742 goto error;
1744 return map;
1745 error:
1746 isl_map_free(map);
1747 return NULL;
1750 /* Eliminate the specified n dimensions starting at first from the
1751 * constraints, without removing the dimensions from the space.
1752 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1754 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1755 enum isl_dim_type type, unsigned first, unsigned n)
1757 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1760 /* Eliminate the specified n dimensions starting at first from the
1761 * constraints, without removing the dimensions from the space.
1762 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1764 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1765 unsigned first, unsigned n)
1767 return isl_set_eliminate(set, isl_dim_set, first, n);
1770 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1771 __isl_take isl_basic_map *bmap)
1773 if (!bmap)
1774 return NULL;
1775 bmap = isl_basic_map_eliminate_vars(bmap,
1776 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1777 if (!bmap)
1778 return NULL;
1779 bmap->n_div = 0;
1780 return isl_basic_map_finalize(bmap);
1783 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1784 __isl_take isl_basic_set *bset)
1786 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1787 (struct isl_basic_map *)bset);
1790 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1792 int i;
1794 if (!map)
1795 return NULL;
1796 if (map->n == 0)
1797 return map;
1799 map = isl_map_cow(map);
1800 if (!map)
1801 return NULL;
1803 for (i = 0; i < map->n; ++i) {
1804 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1805 if (!map->p[i])
1806 goto error;
1808 return map;
1809 error:
1810 isl_map_free(map);
1811 return NULL;
1814 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1816 return isl_map_remove_divs(set);
1819 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1820 enum isl_dim_type type, unsigned first, unsigned n)
1822 if (!bmap)
1823 return NULL;
1824 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1825 goto error);
1826 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1827 return bmap;
1828 bmap = isl_basic_map_eliminate_vars(bmap,
1829 isl_basic_map_offset(bmap, type) - 1 + first, n);
1830 if (!bmap)
1831 return bmap;
1832 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1833 return bmap;
1834 bmap = isl_basic_map_drop(bmap, type, first, n);
1835 return bmap;
1836 error:
1837 isl_basic_map_free(bmap);
1838 return NULL;
1841 /* Return true if the definition of the given div (recursively) involves
1842 * any of the given variables.
1844 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1845 unsigned first, unsigned n)
1847 int i;
1848 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1850 if (isl_int_is_zero(bmap->div[div][0]))
1851 return 0;
1852 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1853 return 1;
1855 for (i = bmap->n_div - 1; i >= 0; --i) {
1856 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1857 continue;
1858 if (div_involves_vars(bmap, i, first, n))
1859 return 1;
1862 return 0;
1865 /* Try and add a lower and/or upper bound on "div" to "bmap"
1866 * based on inequality "i".
1867 * "total" is the total number of variables (excluding the divs).
1868 * "v" is a temporary object that can be used during the calculations.
1869 * If "lb" is set, then a lower bound should be constructed.
1870 * If "ub" is set, then an upper bound should be constructed.
1872 * The calling function has already checked that the inequality does not
1873 * reference "div", but we still need to check that the inequality is
1874 * of the right form. We'll consider the case where we want to construct
1875 * a lower bound. The construction of upper bounds is similar.
1877 * Let "div" be of the form
1879 * q = floor((a + f(x))/d)
1881 * We essentially check if constraint "i" is of the form
1883 * b + f(x) >= 0
1885 * so that we can use it to derive a lower bound on "div".
1886 * However, we allow a slightly more general form
1888 * b + g(x) >= 0
1890 * with the condition that the coefficients of g(x) - f(x) are all
1891 * divisible by d.
1892 * Rewriting this constraint as
1894 * 0 >= -b - g(x)
1896 * adding a + f(x) to both sides and dividing by d, we obtain
1898 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1900 * Taking the floor on both sides, we obtain
1902 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1904 * or
1906 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1908 * In the case of an upper bound, we construct the constraint
1910 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1913 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1914 __isl_take isl_basic_map *bmap, int div, int i,
1915 unsigned total, isl_int v, int lb, int ub)
1917 int j;
1919 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1920 if (lb) {
1921 isl_int_sub(v, bmap->ineq[i][1 + j],
1922 bmap->div[div][1 + 1 + j]);
1923 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1925 if (ub) {
1926 isl_int_add(v, bmap->ineq[i][1 + j],
1927 bmap->div[div][1 + 1 + j]);
1928 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1931 if (!lb && !ub)
1932 return bmap;
1934 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1935 if (lb) {
1936 int k = isl_basic_map_alloc_inequality(bmap);
1937 if (k < 0)
1938 goto error;
1939 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1940 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1941 bmap->div[div][1 + j]);
1942 isl_int_cdiv_q(bmap->ineq[k][j],
1943 bmap->ineq[k][j], bmap->div[div][0]);
1945 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1947 if (ub) {
1948 int k = isl_basic_map_alloc_inequality(bmap);
1949 if (k < 0)
1950 goto error;
1951 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1952 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1953 bmap->div[div][1 + j]);
1954 isl_int_fdiv_q(bmap->ineq[k][j],
1955 bmap->ineq[k][j], bmap->div[div][0]);
1957 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
1960 return bmap;
1961 error:
1962 isl_basic_map_free(bmap);
1963 return NULL;
1966 /* This function is called right before "div" is eliminated from "bmap"
1967 * using Fourier-Motzkin.
1968 * Look through the constraints of "bmap" for constraints on the argument
1969 * of the integer division and use them to construct constraints on the
1970 * integer division itself. These constraints can then be combined
1971 * during the Fourier-Motzkin elimination.
1972 * Note that it is only useful to introduce lower bounds on "div"
1973 * if "bmap" already contains upper bounds on "div" as the newly
1974 * introduce lower bounds can then be combined with the pre-existing
1975 * upper bounds. Similarly for upper bounds.
1976 * We therefore first check if "bmap" contains any lower and/or upper bounds
1977 * on "div".
1979 * It is interesting to note that the introduction of these constraints
1980 * can indeed lead to more accurate results, even when compared to
1981 * deriving constraints on the argument of "div" from constraints on "div".
1982 * Consider, for example, the set
1984 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1986 * The second constraint can be rewritten as
1988 * 2 * [(-i-2j+3)/4] + k >= 0
1990 * from which we can derive
1992 * -i - 2j + 3 >= -2k
1994 * or
1996 * i + 2j <= 3 + 2k
1998 * Combined with the first constraint, we obtain
2000 * -3 <= 3 + 2k or k >= -3
2002 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2003 * the first constraint, we obtain
2005 * [(i + 2j)/4] >= [-3/4] = -1
2007 * Combining this constraint with the second constraint, we obtain
2009 * k >= -2
2011 static __isl_give isl_basic_map *insert_bounds_on_div(
2012 __isl_take isl_basic_map *bmap, int div)
2014 int i;
2015 int check_lb, check_ub;
2016 isl_int v;
2017 unsigned total;
2019 if (!bmap)
2020 return NULL;
2022 if (isl_int_is_zero(bmap->div[div][0]))
2023 return bmap;
2025 total = isl_space_dim(bmap->dim, isl_dim_all);
2027 check_lb = 0;
2028 check_ub = 0;
2029 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2030 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2031 if (s > 0)
2032 check_ub = 1;
2033 if (s < 0)
2034 check_lb = 1;
2037 if (!check_lb && !check_ub)
2038 return bmap;
2040 isl_int_init(v);
2042 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2043 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2044 continue;
2046 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2047 check_lb, check_ub);
2050 isl_int_clear(v);
2052 return bmap;
2055 /* Remove all divs (recursively) involving any of the given dimensions
2056 * in their definitions.
2058 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2059 __isl_take isl_basic_map *bmap,
2060 enum isl_dim_type type, unsigned first, unsigned n)
2062 int i;
2064 if (!bmap)
2065 return NULL;
2066 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2067 goto error);
2068 first += isl_basic_map_offset(bmap, type);
2070 for (i = bmap->n_div - 1; i >= 0; --i) {
2071 if (!div_involves_vars(bmap, i, first, n))
2072 continue;
2073 bmap = insert_bounds_on_div(bmap, i);
2074 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2075 if (!bmap)
2076 return NULL;
2077 i = bmap->n_div;
2080 return bmap;
2081 error:
2082 isl_basic_map_free(bmap);
2083 return NULL;
2086 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2087 __isl_take isl_basic_set *bset,
2088 enum isl_dim_type type, unsigned first, unsigned n)
2090 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2093 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2094 enum isl_dim_type type, unsigned first, unsigned n)
2096 int i;
2098 if (!map)
2099 return NULL;
2100 if (map->n == 0)
2101 return map;
2103 map = isl_map_cow(map);
2104 if (!map)
2105 return NULL;
2107 for (i = 0; i < map->n; ++i) {
2108 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2109 type, first, n);
2110 if (!map->p[i])
2111 goto error;
2113 return map;
2114 error:
2115 isl_map_free(map);
2116 return NULL;
2119 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2120 enum isl_dim_type type, unsigned first, unsigned n)
2122 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2123 type, first, n);
2126 /* Does the desciption of "bmap" depend on the specified dimensions?
2127 * We also check whether the dimensions appear in any of the div definitions.
2128 * In principle there is no need for this check. If the dimensions appear
2129 * in a div definition, they also appear in the defining constraints of that
2130 * div.
2132 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2133 enum isl_dim_type type, unsigned first, unsigned n)
2135 int i;
2137 if (!bmap)
2138 return -1;
2140 if (first + n > isl_basic_map_dim(bmap, type))
2141 isl_die(bmap->ctx, isl_error_invalid,
2142 "index out of bounds", return -1);
2144 first += isl_basic_map_offset(bmap, type);
2145 for (i = 0; i < bmap->n_eq; ++i)
2146 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2147 return 1;
2148 for (i = 0; i < bmap->n_ineq; ++i)
2149 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2150 return 1;
2151 for (i = 0; i < bmap->n_div; ++i) {
2152 if (isl_int_is_zero(bmap->div[i][0]))
2153 continue;
2154 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2155 return 1;
2158 return 0;
2161 int isl_map_involves_dims(__isl_keep isl_map *map,
2162 enum isl_dim_type type, unsigned first, unsigned n)
2164 int i;
2166 if (!map)
2167 return -1;
2169 if (first + n > isl_map_dim(map, type))
2170 isl_die(map->ctx, isl_error_invalid,
2171 "index out of bounds", return -1);
2173 for (i = 0; i < map->n; ++i) {
2174 int involves = isl_basic_map_involves_dims(map->p[i],
2175 type, first, n);
2176 if (involves < 0 || involves)
2177 return involves;
2180 return 0;
2183 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2184 enum isl_dim_type type, unsigned first, unsigned n)
2186 return isl_basic_map_involves_dims(bset, type, first, n);
2189 int isl_set_involves_dims(__isl_keep isl_set *set,
2190 enum isl_dim_type type, unsigned first, unsigned n)
2192 return isl_map_involves_dims(set, type, first, n);
2195 /* Return true if the definition of the given div is unknown or depends
2196 * on unknown divs.
2198 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2200 int i;
2201 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2203 if (isl_int_is_zero(bmap->div[div][0]))
2204 return 1;
2206 for (i = bmap->n_div - 1; i >= 0; --i) {
2207 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2208 continue;
2209 if (div_is_unknown(bmap, i))
2210 return 1;
2213 return 0;
2216 /* Remove all divs that are unknown or defined in terms of unknown divs.
2218 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2219 __isl_take isl_basic_map *bmap)
2221 int i;
2223 if (!bmap)
2224 return NULL;
2226 for (i = bmap->n_div - 1; i >= 0; --i) {
2227 if (!div_is_unknown(bmap, i))
2228 continue;
2229 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2230 if (!bmap)
2231 return NULL;
2232 i = bmap->n_div;
2235 return bmap;
2238 /* Remove all divs that are unknown or defined in terms of unknown divs.
2240 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2241 __isl_take isl_basic_set *bset)
2243 return isl_basic_map_remove_unknown_divs(bset);
2246 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2248 int i;
2250 if (!map)
2251 return NULL;
2252 if (map->n == 0)
2253 return map;
2255 map = isl_map_cow(map);
2256 if (!map)
2257 return NULL;
2259 for (i = 0; i < map->n; ++i) {
2260 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2261 if (!map->p[i])
2262 goto error;
2264 return map;
2265 error:
2266 isl_map_free(map);
2267 return NULL;
2270 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2272 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2275 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2276 __isl_take isl_basic_set *bset,
2277 enum isl_dim_type type, unsigned first, unsigned n)
2279 return (isl_basic_set *)
2280 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2283 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2284 enum isl_dim_type type, unsigned first, unsigned n)
2286 int i;
2288 if (n == 0)
2289 return map;
2291 map = isl_map_cow(map);
2292 if (!map)
2293 return NULL;
2294 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2296 for (i = 0; i < map->n; ++i) {
2297 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2298 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2299 if (!map->p[i])
2300 goto error;
2302 map = isl_map_drop(map, type, first, n);
2303 return map;
2304 error:
2305 isl_map_free(map);
2306 return NULL;
2309 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2310 enum isl_dim_type type, unsigned first, unsigned n)
2312 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2315 /* Project out n inputs starting at first using Fourier-Motzkin */
2316 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2317 unsigned first, unsigned n)
2319 return isl_map_remove_dims(map, isl_dim_in, first, n);
2322 static void dump_term(struct isl_basic_map *bmap,
2323 isl_int c, int pos, FILE *out)
2325 const char *name;
2326 unsigned in = isl_basic_map_n_in(bmap);
2327 unsigned dim = in + isl_basic_map_n_out(bmap);
2328 unsigned nparam = isl_basic_map_n_param(bmap);
2329 if (!pos)
2330 isl_int_print(out, c, 0);
2331 else {
2332 if (!isl_int_is_one(c))
2333 isl_int_print(out, c, 0);
2334 if (pos < 1 + nparam) {
2335 name = isl_space_get_dim_name(bmap->dim,
2336 isl_dim_param, pos - 1);
2337 if (name)
2338 fprintf(out, "%s", name);
2339 else
2340 fprintf(out, "p%d", pos - 1);
2341 } else if (pos < 1 + nparam + in)
2342 fprintf(out, "i%d", pos - 1 - nparam);
2343 else if (pos < 1 + nparam + dim)
2344 fprintf(out, "o%d", pos - 1 - nparam - in);
2345 else
2346 fprintf(out, "e%d", pos - 1 - nparam - dim);
2350 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2351 int sign, FILE *out)
2353 int i;
2354 int first;
2355 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2356 isl_int v;
2358 isl_int_init(v);
2359 for (i = 0, first = 1; i < len; ++i) {
2360 if (isl_int_sgn(c[i]) * sign <= 0)
2361 continue;
2362 if (!first)
2363 fprintf(out, " + ");
2364 first = 0;
2365 isl_int_abs(v, c[i]);
2366 dump_term(bmap, v, i, out);
2368 isl_int_clear(v);
2369 if (first)
2370 fprintf(out, "0");
2373 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2374 const char *op, FILE *out, int indent)
2376 int i;
2378 fprintf(out, "%*s", indent, "");
2380 dump_constraint_sign(bmap, c, 1, out);
2381 fprintf(out, " %s ", op);
2382 dump_constraint_sign(bmap, c, -1, out);
2384 fprintf(out, "\n");
2386 for (i = bmap->n_div; i < bmap->extra; ++i) {
2387 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2388 continue;
2389 fprintf(out, "%*s", indent, "");
2390 fprintf(out, "ERROR: unused div coefficient not zero\n");
2391 abort();
2395 static void dump_constraints(struct isl_basic_map *bmap,
2396 isl_int **c, unsigned n,
2397 const char *op, FILE *out, int indent)
2399 int i;
2401 for (i = 0; i < n; ++i)
2402 dump_constraint(bmap, c[i], op, out, indent);
2405 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2407 int j;
2408 int first = 1;
2409 unsigned total = isl_basic_map_total_dim(bmap);
2411 for (j = 0; j < 1 + total; ++j) {
2412 if (isl_int_is_zero(exp[j]))
2413 continue;
2414 if (!first && isl_int_is_pos(exp[j]))
2415 fprintf(out, "+");
2416 dump_term(bmap, exp[j], j, out);
2417 first = 0;
2421 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2423 int i;
2425 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2426 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2428 for (i = 0; i < bmap->n_div; ++i) {
2429 fprintf(out, "%*s", indent, "");
2430 fprintf(out, "e%d = [(", i);
2431 dump_affine(bmap, bmap->div[i]+1, out);
2432 fprintf(out, ")/");
2433 isl_int_print(out, bmap->div[i][0], 0);
2434 fprintf(out, "]\n");
2438 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2439 FILE *out, int indent)
2441 if (!bset) {
2442 fprintf(out, "null basic set\n");
2443 return;
2446 fprintf(out, "%*s", indent, "");
2447 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2448 bset->ref, bset->dim->nparam, bset->dim->n_out,
2449 bset->extra, bset->flags);
2450 dump((struct isl_basic_map *)bset, out, indent);
2453 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2454 FILE *out, int indent)
2456 if (!bmap) {
2457 fprintf(out, "null basic map\n");
2458 return;
2461 fprintf(out, "%*s", indent, "");
2462 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2463 "flags: %x, n_name: %d\n",
2464 bmap->ref,
2465 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2466 bmap->extra, bmap->flags, bmap->dim->n_id);
2467 dump(bmap, out, indent);
2470 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2472 unsigned total;
2473 if (!bmap)
2474 return -1;
2475 total = isl_basic_map_total_dim(bmap);
2476 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2477 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2478 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2479 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2480 return 0;
2483 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2484 unsigned flags)
2486 struct isl_set *set;
2488 if (!dim)
2489 return NULL;
2490 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2491 isl_assert(dim->ctx, n >= 0, goto error);
2492 set = isl_alloc(dim->ctx, struct isl_set,
2493 sizeof(struct isl_set) +
2494 (n - 1) * sizeof(struct isl_basic_set *));
2495 if (!set)
2496 goto error;
2498 set->ctx = dim->ctx;
2499 isl_ctx_ref(set->ctx);
2500 set->ref = 1;
2501 set->size = n;
2502 set->n = 0;
2503 set->dim = dim;
2504 set->flags = flags;
2505 return set;
2506 error:
2507 isl_space_free(dim);
2508 return NULL;
2511 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2512 unsigned nparam, unsigned dim, int n, unsigned flags)
2514 struct isl_set *set;
2515 isl_space *dims;
2517 dims = isl_space_alloc(ctx, nparam, 0, dim);
2518 if (!dims)
2519 return NULL;
2521 set = isl_set_alloc_space(dims, n, flags);
2522 return set;
2525 /* Make sure "map" has room for at least "n" more basic maps.
2527 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2529 int i;
2530 struct isl_map *grown = NULL;
2532 if (!map)
2533 return NULL;
2534 isl_assert(map->ctx, n >= 0, goto error);
2535 if (map->n + n <= map->size)
2536 return map;
2537 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2538 if (!grown)
2539 goto error;
2540 for (i = 0; i < map->n; ++i) {
2541 grown->p[i] = isl_basic_map_copy(map->p[i]);
2542 if (!grown->p[i])
2543 goto error;
2544 grown->n++;
2546 isl_map_free(map);
2547 return grown;
2548 error:
2549 isl_map_free(grown);
2550 isl_map_free(map);
2551 return NULL;
2554 /* Make sure "set" has room for at least "n" more basic sets.
2556 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2558 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2561 struct isl_set *isl_set_dup(struct isl_set *set)
2563 int i;
2564 struct isl_set *dup;
2566 if (!set)
2567 return NULL;
2569 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2570 if (!dup)
2571 return NULL;
2572 for (i = 0; i < set->n; ++i)
2573 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2574 return dup;
2577 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2579 return isl_map_from_basic_map(bset);
2582 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2584 struct isl_map *map;
2586 if (!bmap)
2587 return NULL;
2589 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2590 return isl_map_add_basic_map(map, bmap);
2593 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2594 __isl_take isl_basic_set *bset)
2596 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2597 (struct isl_basic_map *)bset);
2600 void *isl_set_free(__isl_take isl_set *set)
2602 int i;
2604 if (!set)
2605 return NULL;
2607 if (--set->ref > 0)
2608 return NULL;
2610 isl_ctx_deref(set->ctx);
2611 for (i = 0; i < set->n; ++i)
2612 isl_basic_set_free(set->p[i]);
2613 isl_space_free(set->dim);
2614 free(set);
2616 return NULL;
2619 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2621 int i;
2623 if (!set) {
2624 fprintf(out, "null set\n");
2625 return;
2628 fprintf(out, "%*s", indent, "");
2629 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2630 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2631 set->flags);
2632 for (i = 0; i < set->n; ++i) {
2633 fprintf(out, "%*s", indent, "");
2634 fprintf(out, "basic set %d:\n", i);
2635 isl_basic_set_print_internal(set->p[i], out, indent+4);
2639 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2641 int i;
2643 if (!map) {
2644 fprintf(out, "null map\n");
2645 return;
2648 fprintf(out, "%*s", indent, "");
2649 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2650 "flags: %x, n_name: %d\n",
2651 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2652 map->dim->n_out, map->flags, map->dim->n_id);
2653 for (i = 0; i < map->n; ++i) {
2654 fprintf(out, "%*s", indent, "");
2655 fprintf(out, "basic map %d:\n", i);
2656 isl_basic_map_print_internal(map->p[i], out, indent+4);
2660 struct isl_basic_map *isl_basic_map_intersect_domain(
2661 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2663 struct isl_basic_map *bmap_domain;
2665 if (!bmap || !bset)
2666 goto error;
2668 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2669 bset->dim, isl_dim_param), goto error);
2671 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2672 isl_assert(bset->ctx,
2673 isl_basic_map_compatible_domain(bmap, bset), goto error);
2675 bmap = isl_basic_map_cow(bmap);
2676 if (!bmap)
2677 goto error;
2678 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2679 bset->n_div, bset->n_eq, bset->n_ineq);
2680 bmap_domain = isl_basic_map_from_domain(bset);
2681 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2683 bmap = isl_basic_map_simplify(bmap);
2684 return isl_basic_map_finalize(bmap);
2685 error:
2686 isl_basic_map_free(bmap);
2687 isl_basic_set_free(bset);
2688 return NULL;
2691 struct isl_basic_map *isl_basic_map_intersect_range(
2692 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2694 struct isl_basic_map *bmap_range;
2696 if (!bmap || !bset)
2697 goto error;
2699 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2700 bset->dim, isl_dim_param), goto error);
2702 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2703 isl_assert(bset->ctx,
2704 isl_basic_map_compatible_range(bmap, bset), goto error);
2706 if (isl_basic_set_is_universe(bset)) {
2707 isl_basic_set_free(bset);
2708 return bmap;
2711 bmap = isl_basic_map_cow(bmap);
2712 if (!bmap)
2713 goto error;
2714 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2715 bset->n_div, bset->n_eq, bset->n_ineq);
2716 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2717 bmap = add_constraints(bmap, bmap_range, 0, 0);
2719 bmap = isl_basic_map_simplify(bmap);
2720 return isl_basic_map_finalize(bmap);
2721 error:
2722 isl_basic_map_free(bmap);
2723 isl_basic_set_free(bset);
2724 return NULL;
2727 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2729 int i;
2730 unsigned total;
2731 isl_int s;
2733 if (!bmap || !vec)
2734 return -1;
2736 total = 1 + isl_basic_map_total_dim(bmap);
2737 if (total != vec->size)
2738 return -1;
2740 isl_int_init(s);
2742 for (i = 0; i < bmap->n_eq; ++i) {
2743 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2744 if (!isl_int_is_zero(s)) {
2745 isl_int_clear(s);
2746 return 0;
2750 for (i = 0; i < bmap->n_ineq; ++i) {
2751 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2752 if (isl_int_is_neg(s)) {
2753 isl_int_clear(s);
2754 return 0;
2758 isl_int_clear(s);
2760 return 1;
2763 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2765 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2768 struct isl_basic_map *isl_basic_map_intersect(
2769 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2771 struct isl_vec *sample = NULL;
2773 if (!bmap1 || !bmap2)
2774 goto error;
2776 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2777 bmap2->dim, isl_dim_param), goto error);
2778 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2779 isl_space_dim(bmap1->dim, isl_dim_param) &&
2780 isl_space_dim(bmap2->dim, isl_dim_all) !=
2781 isl_space_dim(bmap2->dim, isl_dim_param))
2782 return isl_basic_map_intersect(bmap2, bmap1);
2784 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2785 isl_space_dim(bmap2->dim, isl_dim_param))
2786 isl_assert(bmap1->ctx,
2787 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2789 if (bmap1->sample &&
2790 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2791 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2792 sample = isl_vec_copy(bmap1->sample);
2793 else if (bmap2->sample &&
2794 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2795 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2796 sample = isl_vec_copy(bmap2->sample);
2798 bmap1 = isl_basic_map_cow(bmap1);
2799 if (!bmap1)
2800 goto error;
2801 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2802 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2803 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2805 if (!bmap1)
2806 isl_vec_free(sample);
2807 else if (sample) {
2808 isl_vec_free(bmap1->sample);
2809 bmap1->sample = sample;
2812 bmap1 = isl_basic_map_simplify(bmap1);
2813 return isl_basic_map_finalize(bmap1);
2814 error:
2815 if (sample)
2816 isl_vec_free(sample);
2817 isl_basic_map_free(bmap1);
2818 isl_basic_map_free(bmap2);
2819 return NULL;
2822 struct isl_basic_set *isl_basic_set_intersect(
2823 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2825 return (struct isl_basic_set *)
2826 isl_basic_map_intersect(
2827 (struct isl_basic_map *)bset1,
2828 (struct isl_basic_map *)bset2);
2831 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2832 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2834 return isl_basic_set_intersect(bset1, bset2);
2837 /* Special case of isl_map_intersect, where both map1 and map2
2838 * are convex, without any divs and such that either map1 or map2
2839 * contains a single constraint. This constraint is then simply
2840 * added to the other map.
2842 static __isl_give isl_map *map_intersect_add_constraint(
2843 __isl_take isl_map *map1, __isl_take isl_map *map2)
2845 isl_assert(map1->ctx, map1->n == 1, goto error);
2846 isl_assert(map2->ctx, map1->n == 1, goto error);
2847 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2848 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2850 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2851 return isl_map_intersect(map2, map1);
2853 isl_assert(map2->ctx,
2854 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2856 map1 = isl_map_cow(map1);
2857 if (!map1)
2858 goto error;
2859 if (isl_map_plain_is_empty(map1)) {
2860 isl_map_free(map2);
2861 return map1;
2863 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2864 if (map2->p[0]->n_eq == 1)
2865 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2866 else
2867 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2868 map2->p[0]->ineq[0]);
2870 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2871 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2872 if (!map1->p[0])
2873 goto error;
2875 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2876 isl_basic_map_free(map1->p[0]);
2877 map1->n = 0;
2880 isl_map_free(map2);
2882 return map1;
2883 error:
2884 isl_map_free(map1);
2885 isl_map_free(map2);
2886 return NULL;
2889 /* map2 may be either a parameter domain or a map living in the same
2890 * space as map1.
2892 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2893 __isl_take isl_map *map2)
2895 unsigned flags = 0;
2896 isl_map *result;
2897 int i, j;
2899 if (!map1 || !map2)
2900 goto error;
2902 if ((isl_map_plain_is_empty(map1) ||
2903 isl_map_plain_is_universe(map2)) &&
2904 isl_space_is_equal(map1->dim, map2->dim)) {
2905 isl_map_free(map2);
2906 return map1;
2908 if ((isl_map_plain_is_empty(map2) ||
2909 isl_map_plain_is_universe(map1)) &&
2910 isl_space_is_equal(map1->dim, map2->dim)) {
2911 isl_map_free(map1);
2912 return map2;
2915 if (map1->n == 1 && map2->n == 1 &&
2916 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2917 isl_space_is_equal(map1->dim, map2->dim) &&
2918 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2919 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2920 return map_intersect_add_constraint(map1, map2);
2922 if (isl_space_dim(map2->dim, isl_dim_all) !=
2923 isl_space_dim(map2->dim, isl_dim_param))
2924 isl_assert(map1->ctx,
2925 isl_space_is_equal(map1->dim, map2->dim), goto error);
2927 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2928 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2929 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2931 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2932 map1->n * map2->n, flags);
2933 if (!result)
2934 goto error;
2935 for (i = 0; i < map1->n; ++i)
2936 for (j = 0; j < map2->n; ++j) {
2937 struct isl_basic_map *part;
2938 part = isl_basic_map_intersect(
2939 isl_basic_map_copy(map1->p[i]),
2940 isl_basic_map_copy(map2->p[j]));
2941 if (isl_basic_map_is_empty(part) < 0)
2942 part = isl_basic_map_free(part);
2943 result = isl_map_add_basic_map(result, part);
2944 if (!result)
2945 goto error;
2947 isl_map_free(map1);
2948 isl_map_free(map2);
2949 return result;
2950 error:
2951 isl_map_free(map1);
2952 isl_map_free(map2);
2953 return NULL;
2956 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2957 __isl_take isl_map *map2)
2959 if (!map1 || !map2)
2960 goto error;
2961 if (!isl_space_is_equal(map1->dim, map2->dim))
2962 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2963 "spaces don't match", goto error);
2964 return map_intersect_internal(map1, map2);
2965 error:
2966 isl_map_free(map1);
2967 isl_map_free(map2);
2968 return NULL;
2971 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2972 __isl_take isl_map *map2)
2974 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2977 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2979 return (struct isl_set *)
2980 isl_map_intersect((struct isl_map *)set1,
2981 (struct isl_map *)set2);
2984 /* map_intersect_internal accepts intersections
2985 * with parameter domains, so we can just call that function.
2987 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2988 __isl_take isl_set *params)
2990 return map_intersect_internal(map, params);
2993 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2994 __isl_take isl_map *map2)
2996 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2999 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3000 __isl_take isl_set *params)
3002 return isl_map_intersect_params(set, params);
3005 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3007 isl_space *dim;
3008 struct isl_basic_set *bset;
3009 unsigned in;
3011 if (!bmap)
3012 return NULL;
3013 bmap = isl_basic_map_cow(bmap);
3014 if (!bmap)
3015 return NULL;
3016 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3017 in = isl_basic_map_n_in(bmap);
3018 bset = isl_basic_set_from_basic_map(bmap);
3019 bset = isl_basic_set_swap_vars(bset, in);
3020 return isl_basic_map_from_basic_set(bset, dim);
3023 static __isl_give isl_basic_map *basic_map_space_reset(
3024 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3026 isl_space *space;
3028 if (!bmap)
3029 return NULL;
3030 if (!isl_space_is_named_or_nested(bmap->dim, type))
3031 return bmap;
3033 space = isl_basic_map_get_space(bmap);
3034 space = isl_space_reset(space, type);
3035 bmap = isl_basic_map_reset_space(bmap, space);
3036 return bmap;
3039 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3040 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3041 unsigned pos, unsigned n)
3043 isl_space *res_dim;
3044 struct isl_basic_map *res;
3045 struct isl_dim_map *dim_map;
3046 unsigned total, off;
3047 enum isl_dim_type t;
3049 if (n == 0)
3050 return basic_map_space_reset(bmap, type);
3052 if (!bmap)
3053 return NULL;
3055 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3057 total = isl_basic_map_total_dim(bmap) + n;
3058 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3059 off = 0;
3060 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3061 if (t != type) {
3062 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3063 } else {
3064 unsigned size = isl_basic_map_dim(bmap, t);
3065 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3066 0, pos, off);
3067 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3068 pos, size - pos, off + pos + n);
3070 off += isl_space_dim(res_dim, t);
3072 isl_dim_map_div(dim_map, bmap, off);
3074 res = isl_basic_map_alloc_space(res_dim,
3075 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3076 if (isl_basic_map_is_rational(bmap))
3077 res = isl_basic_map_set_rational(res);
3078 if (isl_basic_map_plain_is_empty(bmap)) {
3079 isl_basic_map_free(bmap);
3080 free(dim_map);
3081 return isl_basic_map_set_to_empty(res);
3083 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3084 return isl_basic_map_finalize(res);
3087 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3088 __isl_take isl_basic_set *bset,
3089 enum isl_dim_type type, unsigned pos, unsigned n)
3091 return isl_basic_map_insert_dims(bset, type, pos, n);
3094 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3095 enum isl_dim_type type, unsigned n)
3097 if (!bmap)
3098 return NULL;
3099 return isl_basic_map_insert_dims(bmap, type,
3100 isl_basic_map_dim(bmap, type), n);
3103 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3104 enum isl_dim_type type, unsigned n)
3106 if (!bset)
3107 return NULL;
3108 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3109 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3110 error:
3111 isl_basic_set_free(bset);
3112 return NULL;
3115 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3116 enum isl_dim_type type)
3118 isl_space *space;
3120 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3121 return map;
3123 space = isl_map_get_space(map);
3124 space = isl_space_reset(space, type);
3125 map = isl_map_reset_space(map, space);
3126 return map;
3129 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3130 enum isl_dim_type type, unsigned pos, unsigned n)
3132 int i;
3134 if (n == 0)
3135 return map_space_reset(map, type);
3137 map = isl_map_cow(map);
3138 if (!map)
3139 return NULL;
3141 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3142 if (!map->dim)
3143 goto error;
3145 for (i = 0; i < map->n; ++i) {
3146 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3147 if (!map->p[i])
3148 goto error;
3151 return map;
3152 error:
3153 isl_map_free(map);
3154 return NULL;
3157 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3158 enum isl_dim_type type, unsigned pos, unsigned n)
3160 return isl_map_insert_dims(set, type, pos, n);
3163 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3164 enum isl_dim_type type, unsigned n)
3166 if (!map)
3167 return NULL;
3168 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3171 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3172 enum isl_dim_type type, unsigned n)
3174 if (!set)
3175 return NULL;
3176 isl_assert(set->ctx, type != isl_dim_in, goto error);
3177 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3178 error:
3179 isl_set_free(set);
3180 return NULL;
3183 __isl_give isl_basic_map *isl_basic_map_move_dims(
3184 __isl_take isl_basic_map *bmap,
3185 enum isl_dim_type dst_type, unsigned dst_pos,
3186 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3188 struct isl_dim_map *dim_map;
3189 struct isl_basic_map *res;
3190 enum isl_dim_type t;
3191 unsigned total, off;
3193 if (!bmap)
3194 return NULL;
3195 if (n == 0)
3196 return bmap;
3198 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3199 goto error);
3201 if (dst_type == src_type && dst_pos == src_pos)
3202 return bmap;
3204 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3206 if (pos(bmap->dim, dst_type) + dst_pos ==
3207 pos(bmap->dim, src_type) + src_pos +
3208 ((src_type < dst_type) ? n : 0)) {
3209 bmap = isl_basic_map_cow(bmap);
3210 if (!bmap)
3211 return NULL;
3213 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3214 src_type, src_pos, n);
3215 if (!bmap->dim)
3216 goto error;
3218 bmap = isl_basic_map_finalize(bmap);
3220 return bmap;
3223 total = isl_basic_map_total_dim(bmap);
3224 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3226 off = 0;
3227 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3228 unsigned size = isl_space_dim(bmap->dim, t);
3229 if (t == dst_type) {
3230 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3231 0, dst_pos, off);
3232 off += dst_pos;
3233 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3234 src_pos, n, off);
3235 off += n;
3236 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3237 dst_pos, size - dst_pos, off);
3238 off += size - dst_pos;
3239 } else if (t == src_type) {
3240 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3241 0, src_pos, off);
3242 off += src_pos;
3243 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3244 src_pos + n, size - src_pos - n, off);
3245 off += size - src_pos - n;
3246 } else {
3247 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3248 off += size;
3251 isl_dim_map_div(dim_map, bmap, off);
3253 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3254 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3255 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3256 if (!bmap)
3257 goto error;
3259 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3260 src_type, src_pos, n);
3261 if (!bmap->dim)
3262 goto error;
3264 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3265 bmap = isl_basic_map_gauss(bmap, NULL);
3266 bmap = isl_basic_map_finalize(bmap);
3268 return bmap;
3269 error:
3270 isl_basic_map_free(bmap);
3271 return NULL;
3274 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3275 enum isl_dim_type dst_type, unsigned dst_pos,
3276 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3278 return (isl_basic_set *)isl_basic_map_move_dims(
3279 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3282 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3283 enum isl_dim_type dst_type, unsigned dst_pos,
3284 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3286 if (!set)
3287 return NULL;
3288 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3289 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3290 src_type, src_pos, n);
3291 error:
3292 isl_set_free(set);
3293 return NULL;
3296 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3297 enum isl_dim_type dst_type, unsigned dst_pos,
3298 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3300 int i;
3302 if (!map)
3303 return NULL;
3304 if (n == 0)
3305 return map;
3307 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3308 goto error);
3310 if (dst_type == src_type && dst_pos == src_pos)
3311 return map;
3313 isl_assert(map->ctx, dst_type != src_type, goto error);
3315 map = isl_map_cow(map);
3316 if (!map)
3317 return NULL;
3319 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3320 if (!map->dim)
3321 goto error;
3323 for (i = 0; i < map->n; ++i) {
3324 map->p[i] = isl_basic_map_move_dims(map->p[i],
3325 dst_type, dst_pos,
3326 src_type, src_pos, n);
3327 if (!map->p[i])
3328 goto error;
3331 return map;
3332 error:
3333 isl_map_free(map);
3334 return NULL;
3337 /* Move the specified dimensions to the last columns right before
3338 * the divs. Don't change the dimension specification of bmap.
3339 * That's the responsibility of the caller.
3341 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3342 enum isl_dim_type type, unsigned first, unsigned n)
3344 struct isl_dim_map *dim_map;
3345 struct isl_basic_map *res;
3346 enum isl_dim_type t;
3347 unsigned total, off;
3349 if (!bmap)
3350 return NULL;
3351 if (pos(bmap->dim, type) + first + n ==
3352 1 + isl_space_dim(bmap->dim, isl_dim_all))
3353 return bmap;
3355 total = isl_basic_map_total_dim(bmap);
3356 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3358 off = 0;
3359 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3360 unsigned size = isl_space_dim(bmap->dim, t);
3361 if (t == type) {
3362 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3363 0, first, off);
3364 off += first;
3365 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3366 first, n, total - bmap->n_div - n);
3367 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3368 first + n, size - (first + n), off);
3369 off += size - (first + n);
3370 } else {
3371 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3372 off += size;
3375 isl_dim_map_div(dim_map, bmap, off + n);
3377 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3378 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3379 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3380 return res;
3383 /* Insert "n" rows in the divs of "bmap".
3385 * The number of columns is not changed, which means that the last
3386 * dimensions of "bmap" are being reintepreted as the new divs.
3387 * The space of "bmap" is not adjusted, however, which means
3388 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3389 * from the space of "bmap" is the responsibility of the caller.
3391 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3392 int n)
3394 int i;
3395 size_t row_size;
3396 isl_int **new_div;
3397 isl_int *old;
3399 bmap = isl_basic_map_cow(bmap);
3400 if (!bmap)
3401 return NULL;
3403 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3404 old = bmap->block2.data;
3405 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3406 (bmap->extra + n) * (1 + row_size));
3407 if (!bmap->block2.data)
3408 return isl_basic_map_free(bmap);
3409 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3410 if (!new_div)
3411 return isl_basic_map_free(bmap);
3412 for (i = 0; i < n; ++i) {
3413 new_div[i] = bmap->block2.data +
3414 (bmap->extra + i) * (1 + row_size);
3415 isl_seq_clr(new_div[i], 1 + row_size);
3417 for (i = 0; i < bmap->extra; ++i)
3418 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3419 free(bmap->div);
3420 bmap->div = new_div;
3421 bmap->n_div += n;
3422 bmap->extra += n;
3424 return bmap;
3427 /* Turn the n dimensions of type type, starting at first
3428 * into existentially quantified variables.
3430 __isl_give isl_basic_map *isl_basic_map_project_out(
3431 __isl_take isl_basic_map *bmap,
3432 enum isl_dim_type type, unsigned first, unsigned n)
3434 if (n == 0)
3435 return basic_map_space_reset(bmap, type);
3437 if (!bmap)
3438 return NULL;
3440 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3441 return isl_basic_map_remove_dims(bmap, type, first, n);
3443 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3444 goto error);
3446 bmap = move_last(bmap, type, first, n);
3447 bmap = isl_basic_map_cow(bmap);
3448 bmap = insert_div_rows(bmap, n);
3449 if (!bmap)
3450 return NULL;
3452 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3453 if (!bmap->dim)
3454 goto error;
3455 bmap = isl_basic_map_simplify(bmap);
3456 bmap = isl_basic_map_drop_redundant_divs(bmap);
3457 return isl_basic_map_finalize(bmap);
3458 error:
3459 isl_basic_map_free(bmap);
3460 return NULL;
3463 /* Turn the n dimensions of type type, starting at first
3464 * into existentially quantified variables.
3466 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3467 enum isl_dim_type type, unsigned first, unsigned n)
3469 return (isl_basic_set *)isl_basic_map_project_out(
3470 (isl_basic_map *)bset, type, first, n);
3473 /* Turn the n dimensions of type type, starting at first
3474 * into existentially quantified variables.
3476 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3477 enum isl_dim_type type, unsigned first, unsigned n)
3479 int i;
3481 if (!map)
3482 return NULL;
3484 if (n == 0)
3485 return map_space_reset(map, type);
3487 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3489 map = isl_map_cow(map);
3490 if (!map)
3491 return NULL;
3493 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3494 if (!map->dim)
3495 goto error;
3497 for (i = 0; i < map->n; ++i) {
3498 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3499 if (!map->p[i])
3500 goto error;
3503 return map;
3504 error:
3505 isl_map_free(map);
3506 return NULL;
3509 /* Turn the n dimensions of type type, starting at first
3510 * into existentially quantified variables.
3512 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3513 enum isl_dim_type type, unsigned first, unsigned n)
3515 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3518 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3520 int i, j;
3522 for (i = 0; i < n; ++i) {
3523 j = isl_basic_map_alloc_div(bmap);
3524 if (j < 0)
3525 goto error;
3526 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3528 return bmap;
3529 error:
3530 isl_basic_map_free(bmap);
3531 return NULL;
3534 struct isl_basic_map *isl_basic_map_apply_range(
3535 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3537 isl_space *dim_result = NULL;
3538 struct isl_basic_map *bmap;
3539 unsigned n_in, n_out, n, nparam, total, pos;
3540 struct isl_dim_map *dim_map1, *dim_map2;
3542 if (!bmap1 || !bmap2)
3543 goto error;
3545 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3546 isl_space_copy(bmap2->dim));
3548 n_in = isl_basic_map_n_in(bmap1);
3549 n_out = isl_basic_map_n_out(bmap2);
3550 n = isl_basic_map_n_out(bmap1);
3551 nparam = isl_basic_map_n_param(bmap1);
3553 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3554 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3555 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3556 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3557 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3558 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3559 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3560 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3561 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3562 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3563 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3565 bmap = isl_basic_map_alloc_space(dim_result,
3566 bmap1->n_div + bmap2->n_div + n,
3567 bmap1->n_eq + bmap2->n_eq,
3568 bmap1->n_ineq + bmap2->n_ineq);
3569 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3570 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3571 bmap = add_divs(bmap, n);
3572 bmap = isl_basic_map_simplify(bmap);
3573 bmap = isl_basic_map_drop_redundant_divs(bmap);
3574 return isl_basic_map_finalize(bmap);
3575 error:
3576 isl_basic_map_free(bmap1);
3577 isl_basic_map_free(bmap2);
3578 return NULL;
3581 struct isl_basic_set *isl_basic_set_apply(
3582 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3584 if (!bset || !bmap)
3585 goto error;
3587 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3588 goto error);
3590 return (struct isl_basic_set *)
3591 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3592 error:
3593 isl_basic_set_free(bset);
3594 isl_basic_map_free(bmap);
3595 return NULL;
3598 struct isl_basic_map *isl_basic_map_apply_domain(
3599 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3601 if (!bmap1 || !bmap2)
3602 goto error;
3604 isl_assert(bmap1->ctx,
3605 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3606 isl_assert(bmap1->ctx,
3607 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3608 goto error);
3610 bmap1 = isl_basic_map_reverse(bmap1);
3611 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3612 return isl_basic_map_reverse(bmap1);
3613 error:
3614 isl_basic_map_free(bmap1);
3615 isl_basic_map_free(bmap2);
3616 return NULL;
3619 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3620 * A \cap B -> f(A) + f(B)
3622 struct isl_basic_map *isl_basic_map_sum(
3623 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3625 unsigned n_in, n_out, nparam, total, pos;
3626 struct isl_basic_map *bmap = NULL;
3627 struct isl_dim_map *dim_map1, *dim_map2;
3628 int i;
3630 if (!bmap1 || !bmap2)
3631 goto error;
3633 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3634 goto error);
3636 nparam = isl_basic_map_n_param(bmap1);
3637 n_in = isl_basic_map_n_in(bmap1);
3638 n_out = isl_basic_map_n_out(bmap1);
3640 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3641 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3642 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3643 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3644 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3645 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3646 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3647 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3648 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3649 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3650 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3652 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3653 bmap1->n_div + bmap2->n_div + 2 * n_out,
3654 bmap1->n_eq + bmap2->n_eq + n_out,
3655 bmap1->n_ineq + bmap2->n_ineq);
3656 for (i = 0; i < n_out; ++i) {
3657 int j = isl_basic_map_alloc_equality(bmap);
3658 if (j < 0)
3659 goto error;
3660 isl_seq_clr(bmap->eq[j], 1+total);
3661 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3662 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3663 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3665 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3666 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3667 bmap = add_divs(bmap, 2 * n_out);
3669 bmap = isl_basic_map_simplify(bmap);
3670 return isl_basic_map_finalize(bmap);
3671 error:
3672 isl_basic_map_free(bmap);
3673 isl_basic_map_free(bmap1);
3674 isl_basic_map_free(bmap2);
3675 return NULL;
3678 /* Given two maps A -> f(A) and B -> g(B), construct a map
3679 * A \cap B -> f(A) + f(B)
3681 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3683 struct isl_map *result;
3684 int i, j;
3686 if (!map1 || !map2)
3687 goto error;
3689 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3691 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3692 map1->n * map2->n, 0);
3693 if (!result)
3694 goto error;
3695 for (i = 0; i < map1->n; ++i)
3696 for (j = 0; j < map2->n; ++j) {
3697 struct isl_basic_map *part;
3698 part = isl_basic_map_sum(
3699 isl_basic_map_copy(map1->p[i]),
3700 isl_basic_map_copy(map2->p[j]));
3701 if (isl_basic_map_is_empty(part))
3702 isl_basic_map_free(part);
3703 else
3704 result = isl_map_add_basic_map(result, part);
3705 if (!result)
3706 goto error;
3708 isl_map_free(map1);
3709 isl_map_free(map2);
3710 return result;
3711 error:
3712 isl_map_free(map1);
3713 isl_map_free(map2);
3714 return NULL;
3717 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3718 __isl_take isl_set *set2)
3720 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3723 /* Given a basic map A -> f(A), construct A -> -f(A).
3725 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3727 int i, j;
3728 unsigned off, n;
3730 bmap = isl_basic_map_cow(bmap);
3731 if (!bmap)
3732 return NULL;
3734 n = isl_basic_map_dim(bmap, isl_dim_out);
3735 off = isl_basic_map_offset(bmap, isl_dim_out);
3736 for (i = 0; i < bmap->n_eq; ++i)
3737 for (j = 0; j < n; ++j)
3738 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3739 for (i = 0; i < bmap->n_ineq; ++i)
3740 for (j = 0; j < n; ++j)
3741 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3742 for (i = 0; i < bmap->n_div; ++i)
3743 for (j = 0; j < n; ++j)
3744 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3745 bmap = isl_basic_map_gauss(bmap, NULL);
3746 return isl_basic_map_finalize(bmap);
3749 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3751 return isl_basic_map_neg(bset);
3754 /* Given a map A -> f(A), construct A -> -f(A).
3756 struct isl_map *isl_map_neg(struct isl_map *map)
3758 int i;
3760 map = isl_map_cow(map);
3761 if (!map)
3762 return NULL;
3764 for (i = 0; i < map->n; ++i) {
3765 map->p[i] = isl_basic_map_neg(map->p[i]);
3766 if (!map->p[i])
3767 goto error;
3770 return map;
3771 error:
3772 isl_map_free(map);
3773 return NULL;
3776 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3778 return (isl_set *)isl_map_neg((isl_map *)set);
3781 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3782 * A -> floor(f(A)/d).
3784 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3785 isl_int d)
3787 unsigned n_in, n_out, nparam, total, pos;
3788 struct isl_basic_map *result = NULL;
3789 struct isl_dim_map *dim_map;
3790 int i;
3792 if (!bmap)
3793 return NULL;
3795 nparam = isl_basic_map_n_param(bmap);
3796 n_in = isl_basic_map_n_in(bmap);
3797 n_out = isl_basic_map_n_out(bmap);
3799 total = nparam + n_in + n_out + bmap->n_div + n_out;
3800 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3801 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3802 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3803 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3804 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3806 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3807 bmap->n_div + n_out,
3808 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3809 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3810 result = add_divs(result, n_out);
3811 for (i = 0; i < n_out; ++i) {
3812 int j;
3813 j = isl_basic_map_alloc_inequality(result);
3814 if (j < 0)
3815 goto error;
3816 isl_seq_clr(result->ineq[j], 1+total);
3817 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3818 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3819 j = isl_basic_map_alloc_inequality(result);
3820 if (j < 0)
3821 goto error;
3822 isl_seq_clr(result->ineq[j], 1+total);
3823 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3824 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3825 isl_int_sub_ui(result->ineq[j][0], d, 1);
3828 result = isl_basic_map_simplify(result);
3829 return isl_basic_map_finalize(result);
3830 error:
3831 isl_basic_map_free(result);
3832 return NULL;
3835 /* Given a map A -> f(A) and an integer d, construct a map
3836 * A -> floor(f(A)/d).
3838 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3840 int i;
3842 map = isl_map_cow(map);
3843 if (!map)
3844 return NULL;
3846 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3847 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3848 for (i = 0; i < map->n; ++i) {
3849 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3850 if (!map->p[i])
3851 goto error;
3854 return map;
3855 error:
3856 isl_map_free(map);
3857 return NULL;
3860 /* Given a map A -> f(A) and an integer d, construct a map
3861 * A -> floor(f(A)/d).
3863 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3864 __isl_take isl_val *d)
3866 if (!map || !d)
3867 goto error;
3868 if (!isl_val_is_int(d))
3869 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3870 "expecting integer denominator", goto error);
3871 map = isl_map_floordiv(map, d->n);
3872 isl_val_free(d);
3873 return map;
3874 error:
3875 isl_map_free(map);
3876 isl_val_free(d);
3877 return NULL;
3880 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3882 int i;
3883 unsigned nparam;
3884 unsigned n_in;
3886 i = isl_basic_map_alloc_equality(bmap);
3887 if (i < 0)
3888 goto error;
3889 nparam = isl_basic_map_n_param(bmap);
3890 n_in = isl_basic_map_n_in(bmap);
3891 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3892 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3893 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3894 return isl_basic_map_finalize(bmap);
3895 error:
3896 isl_basic_map_free(bmap);
3897 return NULL;
3900 /* Add a constraints to "bmap" expressing i_pos < o_pos
3902 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3904 int i;
3905 unsigned nparam;
3906 unsigned n_in;
3908 i = isl_basic_map_alloc_inequality(bmap);
3909 if (i < 0)
3910 goto error;
3911 nparam = isl_basic_map_n_param(bmap);
3912 n_in = isl_basic_map_n_in(bmap);
3913 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3914 isl_int_set_si(bmap->ineq[i][0], -1);
3915 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3916 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3917 return isl_basic_map_finalize(bmap);
3918 error:
3919 isl_basic_map_free(bmap);
3920 return NULL;
3923 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3925 static __isl_give isl_basic_map *var_less_or_equal(
3926 __isl_take isl_basic_map *bmap, unsigned pos)
3928 int i;
3929 unsigned nparam;
3930 unsigned n_in;
3932 i = isl_basic_map_alloc_inequality(bmap);
3933 if (i < 0)
3934 goto error;
3935 nparam = isl_basic_map_n_param(bmap);
3936 n_in = isl_basic_map_n_in(bmap);
3937 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3938 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3939 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3940 return isl_basic_map_finalize(bmap);
3941 error:
3942 isl_basic_map_free(bmap);
3943 return NULL;
3946 /* Add a constraints to "bmap" expressing i_pos > o_pos
3948 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3950 int i;
3951 unsigned nparam;
3952 unsigned n_in;
3954 i = isl_basic_map_alloc_inequality(bmap);
3955 if (i < 0)
3956 goto error;
3957 nparam = isl_basic_map_n_param(bmap);
3958 n_in = isl_basic_map_n_in(bmap);
3959 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3960 isl_int_set_si(bmap->ineq[i][0], -1);
3961 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3962 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3963 return isl_basic_map_finalize(bmap);
3964 error:
3965 isl_basic_map_free(bmap);
3966 return NULL;
3969 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3971 static __isl_give isl_basic_map *var_more_or_equal(
3972 __isl_take isl_basic_map *bmap, unsigned pos)
3974 int i;
3975 unsigned nparam;
3976 unsigned n_in;
3978 i = isl_basic_map_alloc_inequality(bmap);
3979 if (i < 0)
3980 goto error;
3981 nparam = isl_basic_map_n_param(bmap);
3982 n_in = isl_basic_map_n_in(bmap);
3983 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3984 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3985 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3986 return isl_basic_map_finalize(bmap);
3987 error:
3988 isl_basic_map_free(bmap);
3989 return NULL;
3992 __isl_give isl_basic_map *isl_basic_map_equal(
3993 __isl_take isl_space *dim, unsigned n_equal)
3995 int i;
3996 struct isl_basic_map *bmap;
3997 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3998 if (!bmap)
3999 return NULL;
4000 for (i = 0; i < n_equal && bmap; ++i)
4001 bmap = var_equal(bmap, i);
4002 return isl_basic_map_finalize(bmap);
4005 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4007 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4008 unsigned pos)
4010 int i;
4011 struct isl_basic_map *bmap;
4012 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4013 if (!bmap)
4014 return NULL;
4015 for (i = 0; i < pos && bmap; ++i)
4016 bmap = var_equal(bmap, i);
4017 if (bmap)
4018 bmap = var_less(bmap, pos);
4019 return isl_basic_map_finalize(bmap);
4022 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4024 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4025 __isl_take isl_space *dim, unsigned pos)
4027 int i;
4028 isl_basic_map *bmap;
4030 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4031 for (i = 0; i < pos; ++i)
4032 bmap = var_equal(bmap, i);
4033 bmap = var_less_or_equal(bmap, pos);
4034 return isl_basic_map_finalize(bmap);
4037 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4039 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4040 unsigned pos)
4042 int i;
4043 struct isl_basic_map *bmap;
4044 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4045 if (!bmap)
4046 return NULL;
4047 for (i = 0; i < pos && bmap; ++i)
4048 bmap = var_equal(bmap, i);
4049 if (bmap)
4050 bmap = var_more(bmap, pos);
4051 return isl_basic_map_finalize(bmap);
4054 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4056 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4057 __isl_take isl_space *dim, unsigned pos)
4059 int i;
4060 isl_basic_map *bmap;
4062 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4063 for (i = 0; i < pos; ++i)
4064 bmap = var_equal(bmap, i);
4065 bmap = var_more_or_equal(bmap, pos);
4066 return isl_basic_map_finalize(bmap);
4069 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4070 unsigned n, int equal)
4072 struct isl_map *map;
4073 int i;
4075 if (n == 0 && equal)
4076 return isl_map_universe(dims);
4078 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4080 for (i = 0; i + 1 < n; ++i)
4081 map = isl_map_add_basic_map(map,
4082 isl_basic_map_less_at(isl_space_copy(dims), i));
4083 if (n > 0) {
4084 if (equal)
4085 map = isl_map_add_basic_map(map,
4086 isl_basic_map_less_or_equal_at(dims, n - 1));
4087 else
4088 map = isl_map_add_basic_map(map,
4089 isl_basic_map_less_at(dims, n - 1));
4090 } else
4091 isl_space_free(dims);
4093 return map;
4096 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4098 if (!dims)
4099 return NULL;
4100 return map_lex_lte_first(dims, dims->n_out, equal);
4103 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4105 return map_lex_lte_first(dim, n, 0);
4108 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4110 return map_lex_lte_first(dim, n, 1);
4113 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4115 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4118 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4120 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4123 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4124 unsigned n, int equal)
4126 struct isl_map *map;
4127 int i;
4129 if (n == 0 && equal)
4130 return isl_map_universe(dims);
4132 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4134 for (i = 0; i + 1 < n; ++i)
4135 map = isl_map_add_basic_map(map,
4136 isl_basic_map_more_at(isl_space_copy(dims), i));
4137 if (n > 0) {
4138 if (equal)
4139 map = isl_map_add_basic_map(map,
4140 isl_basic_map_more_or_equal_at(dims, n - 1));
4141 else
4142 map = isl_map_add_basic_map(map,
4143 isl_basic_map_more_at(dims, n - 1));
4144 } else
4145 isl_space_free(dims);
4147 return map;
4150 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4152 if (!dims)
4153 return NULL;
4154 return map_lex_gte_first(dims, dims->n_out, equal);
4157 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4159 return map_lex_gte_first(dim, n, 0);
4162 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4164 return map_lex_gte_first(dim, n, 1);
4167 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4169 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4172 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4174 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4177 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4178 __isl_take isl_set *set2)
4180 isl_map *map;
4181 map = isl_map_lex_le(isl_set_get_space(set1));
4182 map = isl_map_intersect_domain(map, set1);
4183 map = isl_map_intersect_range(map, set2);
4184 return map;
4187 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4188 __isl_take isl_set *set2)
4190 isl_map *map;
4191 map = isl_map_lex_lt(isl_set_get_space(set1));
4192 map = isl_map_intersect_domain(map, set1);
4193 map = isl_map_intersect_range(map, set2);
4194 return map;
4197 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4198 __isl_take isl_set *set2)
4200 isl_map *map;
4201 map = isl_map_lex_ge(isl_set_get_space(set1));
4202 map = isl_map_intersect_domain(map, set1);
4203 map = isl_map_intersect_range(map, set2);
4204 return map;
4207 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4208 __isl_take isl_set *set2)
4210 isl_map *map;
4211 map = isl_map_lex_gt(isl_set_get_space(set1));
4212 map = isl_map_intersect_domain(map, set1);
4213 map = isl_map_intersect_range(map, set2);
4214 return map;
4217 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4218 __isl_take isl_map *map2)
4220 isl_map *map;
4221 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4222 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4223 map = isl_map_apply_range(map, isl_map_reverse(map2));
4224 return map;
4227 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4228 __isl_take isl_map *map2)
4230 isl_map *map;
4231 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4232 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4233 map = isl_map_apply_range(map, isl_map_reverse(map2));
4234 return map;
4237 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4238 __isl_take isl_map *map2)
4240 isl_map *map;
4241 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4242 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4243 map = isl_map_apply_range(map, isl_map_reverse(map2));
4244 return map;
4247 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4248 __isl_take isl_map *map2)
4250 isl_map *map;
4251 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4252 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4253 map = isl_map_apply_range(map, isl_map_reverse(map2));
4254 return map;
4257 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4258 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4260 struct isl_basic_map *bmap;
4262 bset = isl_basic_set_cow(bset);
4263 if (!bset || !dim)
4264 goto error;
4266 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4267 isl_space_free(bset->dim);
4268 bmap = (struct isl_basic_map *) bset;
4269 bmap->dim = dim;
4270 return isl_basic_map_finalize(bmap);
4271 error:
4272 isl_basic_set_free(bset);
4273 isl_space_free(dim);
4274 return NULL;
4277 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4279 if (!bmap)
4280 goto error;
4281 if (bmap->dim->n_in == 0)
4282 return (struct isl_basic_set *)bmap;
4283 bmap = isl_basic_map_cow(bmap);
4284 if (!bmap)
4285 goto error;
4286 bmap->dim = isl_space_as_set_space(bmap->dim);
4287 if (!bmap->dim)
4288 goto error;
4289 bmap = isl_basic_map_finalize(bmap);
4290 return (struct isl_basic_set *)bmap;
4291 error:
4292 isl_basic_map_free(bmap);
4293 return NULL;
4296 /* For a div d = floor(f/m), add the constraints
4298 * f - m d >= 0
4299 * -(f-(n-1)) + m d >= 0
4301 * Note that the second constraint is the negation of
4303 * f - m d >= n
4305 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4306 unsigned pos, isl_int *div)
4308 int i, j;
4309 unsigned total = isl_basic_map_total_dim(bmap);
4311 i = isl_basic_map_alloc_inequality(bmap);
4312 if (i < 0)
4313 return -1;
4314 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4315 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4317 j = isl_basic_map_alloc_inequality(bmap);
4318 if (j < 0)
4319 return -1;
4320 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4321 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4322 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4323 return j;
4326 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4327 unsigned pos, isl_int *div)
4329 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4330 pos, div);
4333 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4335 unsigned total = isl_basic_map_total_dim(bmap);
4336 unsigned div_pos = total - bmap->n_div + div;
4338 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4339 bmap->div[div]);
4342 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4344 return isl_basic_map_add_div_constraints(bset, div);
4347 struct isl_basic_set *isl_basic_map_underlying_set(
4348 struct isl_basic_map *bmap)
4350 if (!bmap)
4351 goto error;
4352 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4353 bmap->n_div == 0 &&
4354 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4355 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4356 return (struct isl_basic_set *)bmap;
4357 bmap = isl_basic_map_cow(bmap);
4358 if (!bmap)
4359 goto error;
4360 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4361 if (!bmap->dim)
4362 goto error;
4363 bmap->extra -= bmap->n_div;
4364 bmap->n_div = 0;
4365 bmap = isl_basic_map_finalize(bmap);
4366 return (struct isl_basic_set *)bmap;
4367 error:
4368 isl_basic_map_free(bmap);
4369 return NULL;
4372 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4373 __isl_take isl_basic_set *bset)
4375 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4378 struct isl_basic_map *isl_basic_map_overlying_set(
4379 struct isl_basic_set *bset, struct isl_basic_map *like)
4381 struct isl_basic_map *bmap;
4382 struct isl_ctx *ctx;
4383 unsigned total;
4384 int i;
4386 if (!bset || !like)
4387 goto error;
4388 ctx = bset->ctx;
4389 isl_assert(ctx, bset->n_div == 0, goto error);
4390 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4391 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4392 goto error);
4393 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4394 isl_basic_map_free(like);
4395 return (struct isl_basic_map *)bset;
4397 bset = isl_basic_set_cow(bset);
4398 if (!bset)
4399 goto error;
4400 total = bset->dim->n_out + bset->extra;
4401 bmap = (struct isl_basic_map *)bset;
4402 isl_space_free(bmap->dim);
4403 bmap->dim = isl_space_copy(like->dim);
4404 if (!bmap->dim)
4405 goto error;
4406 bmap->n_div = like->n_div;
4407 bmap->extra += like->n_div;
4408 if (bmap->extra) {
4409 unsigned ltotal;
4410 isl_int **div;
4411 ltotal = total - bmap->extra + like->extra;
4412 if (ltotal > total)
4413 ltotal = total;
4414 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4415 bmap->extra * (1 + 1 + total));
4416 if (isl_blk_is_error(bmap->block2))
4417 goto error;
4418 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4419 if (!div)
4420 goto error;
4421 bmap->div = div;
4422 for (i = 0; i < bmap->extra; ++i)
4423 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4424 for (i = 0; i < like->n_div; ++i) {
4425 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4426 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4428 bmap = isl_basic_map_extend_constraints(bmap,
4429 0, 2 * like->n_div);
4430 for (i = 0; i < like->n_div; ++i) {
4431 if (!bmap)
4432 break;
4433 if (isl_int_is_zero(bmap->div[i][0]))
4434 continue;
4435 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4436 bmap = isl_basic_map_free(bmap);
4439 isl_basic_map_free(like);
4440 bmap = isl_basic_map_simplify(bmap);
4441 bmap = isl_basic_map_finalize(bmap);
4442 return bmap;
4443 error:
4444 isl_basic_map_free(like);
4445 isl_basic_set_free(bset);
4446 return NULL;
4449 struct isl_basic_set *isl_basic_set_from_underlying_set(
4450 struct isl_basic_set *bset, struct isl_basic_set *like)
4452 return (struct isl_basic_set *)
4453 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4456 struct isl_set *isl_set_from_underlying_set(
4457 struct isl_set *set, struct isl_basic_set *like)
4459 int i;
4461 if (!set || !like)
4462 goto error;
4463 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4464 goto error);
4465 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4466 isl_basic_set_free(like);
4467 return set;
4469 set = isl_set_cow(set);
4470 if (!set)
4471 goto error;
4472 for (i = 0; i < set->n; ++i) {
4473 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4474 isl_basic_set_copy(like));
4475 if (!set->p[i])
4476 goto error;
4478 isl_space_free(set->dim);
4479 set->dim = isl_space_copy(like->dim);
4480 if (!set->dim)
4481 goto error;
4482 isl_basic_set_free(like);
4483 return set;
4484 error:
4485 isl_basic_set_free(like);
4486 isl_set_free(set);
4487 return NULL;
4490 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4492 int i;
4494 map = isl_map_cow(map);
4495 if (!map)
4496 return NULL;
4497 map->dim = isl_space_cow(map->dim);
4498 if (!map->dim)
4499 goto error;
4501 for (i = 1; i < map->n; ++i)
4502 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4503 goto error);
4504 for (i = 0; i < map->n; ++i) {
4505 map->p[i] = (struct isl_basic_map *)
4506 isl_basic_map_underlying_set(map->p[i]);
4507 if (!map->p[i])
4508 goto error;
4510 if (map->n == 0)
4511 map->dim = isl_space_underlying(map->dim, 0);
4512 else {
4513 isl_space_free(map->dim);
4514 map->dim = isl_space_copy(map->p[0]->dim);
4516 if (!map->dim)
4517 goto error;
4518 return (struct isl_set *)map;
4519 error:
4520 isl_map_free(map);
4521 return NULL;
4524 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4526 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4529 __isl_give isl_basic_map *isl_basic_map_reset_space(
4530 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4532 bmap = isl_basic_map_cow(bmap);
4533 if (!bmap || !dim)
4534 goto error;
4536 isl_space_free(bmap->dim);
4537 bmap->dim = dim;
4539 bmap = isl_basic_map_finalize(bmap);
4541 return bmap;
4542 error:
4543 isl_basic_map_free(bmap);
4544 isl_space_free(dim);
4545 return NULL;
4548 __isl_give isl_basic_set *isl_basic_set_reset_space(
4549 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4551 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4552 dim);
4555 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4556 __isl_take isl_space *dim)
4558 int i;
4560 map = isl_map_cow(map);
4561 if (!map || !dim)
4562 goto error;
4564 for (i = 0; i < map->n; ++i) {
4565 map->p[i] = isl_basic_map_reset_space(map->p[i],
4566 isl_space_copy(dim));
4567 if (!map->p[i])
4568 goto error;
4570 isl_space_free(map->dim);
4571 map->dim = dim;
4573 return map;
4574 error:
4575 isl_map_free(map);
4576 isl_space_free(dim);
4577 return NULL;
4580 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4581 __isl_take isl_space *dim)
4583 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4586 /* Compute the parameter domain of the given basic set.
4588 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4590 isl_space *space;
4591 unsigned n;
4593 if (isl_basic_set_is_params(bset))
4594 return bset;
4596 n = isl_basic_set_dim(bset, isl_dim_set);
4597 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4598 space = isl_basic_set_get_space(bset);
4599 space = isl_space_params(space);
4600 bset = isl_basic_set_reset_space(bset, space);
4601 return bset;
4604 /* Construct a zero-dimensional basic set with the given parameter domain.
4606 __isl_give isl_basic_set *isl_basic_set_from_params(
4607 __isl_take isl_basic_set *bset)
4609 isl_space *space;
4610 space = isl_basic_set_get_space(bset);
4611 space = isl_space_set_from_params(space);
4612 bset = isl_basic_set_reset_space(bset, space);
4613 return bset;
4616 /* Compute the parameter domain of the given set.
4618 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4620 isl_space *space;
4621 unsigned n;
4623 if (isl_set_is_params(set))
4624 return set;
4626 n = isl_set_dim(set, isl_dim_set);
4627 set = isl_set_project_out(set, isl_dim_set, 0, n);
4628 space = isl_set_get_space(set);
4629 space = isl_space_params(space);
4630 set = isl_set_reset_space(set, space);
4631 return set;
4634 /* Construct a zero-dimensional set with the given parameter domain.
4636 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4638 isl_space *space;
4639 space = isl_set_get_space(set);
4640 space = isl_space_set_from_params(space);
4641 set = isl_set_reset_space(set, space);
4642 return set;
4645 /* Compute the parameter domain of the given map.
4647 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4649 isl_space *space;
4650 unsigned n;
4652 n = isl_map_dim(map, isl_dim_in);
4653 map = isl_map_project_out(map, isl_dim_in, 0, n);
4654 n = isl_map_dim(map, isl_dim_out);
4655 map = isl_map_project_out(map, isl_dim_out, 0, n);
4656 space = isl_map_get_space(map);
4657 space = isl_space_params(space);
4658 map = isl_map_reset_space(map, space);
4659 return map;
4662 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4664 isl_space *dim;
4665 struct isl_basic_set *domain;
4666 unsigned n_in;
4667 unsigned n_out;
4669 if (!bmap)
4670 return NULL;
4671 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4673 n_in = isl_basic_map_n_in(bmap);
4674 n_out = isl_basic_map_n_out(bmap);
4675 domain = isl_basic_set_from_basic_map(bmap);
4676 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4678 domain = isl_basic_set_reset_space(domain, dim);
4680 return domain;
4683 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4685 if (!bmap)
4686 return -1;
4687 return isl_space_may_be_set(bmap->dim);
4690 /* Is this basic map actually a set?
4691 * Users should never call this function. Outside of isl,
4692 * the type should indicate whether something is a set or a map.
4694 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4696 if (!bmap)
4697 return -1;
4698 return isl_space_is_set(bmap->dim);
4701 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4703 if (!bmap)
4704 return NULL;
4705 if (isl_basic_map_is_set(bmap))
4706 return bmap;
4707 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4710 __isl_give isl_basic_map *isl_basic_map_domain_map(
4711 __isl_take isl_basic_map *bmap)
4713 int i, k;
4714 isl_space *dim;
4715 isl_basic_map *domain;
4716 int nparam, n_in, n_out;
4717 unsigned total;
4719 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4720 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4721 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4723 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4724 domain = isl_basic_map_universe(dim);
4726 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4727 bmap = isl_basic_map_apply_range(bmap, domain);
4728 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4730 total = isl_basic_map_total_dim(bmap);
4732 for (i = 0; i < n_in; ++i) {
4733 k = isl_basic_map_alloc_equality(bmap);
4734 if (k < 0)
4735 goto error;
4736 isl_seq_clr(bmap->eq[k], 1 + total);
4737 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4738 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4741 bmap = isl_basic_map_gauss(bmap, NULL);
4742 return isl_basic_map_finalize(bmap);
4743 error:
4744 isl_basic_map_free(bmap);
4745 return NULL;
4748 __isl_give isl_basic_map *isl_basic_map_range_map(
4749 __isl_take isl_basic_map *bmap)
4751 int i, k;
4752 isl_space *dim;
4753 isl_basic_map *range;
4754 int nparam, n_in, n_out;
4755 unsigned total;
4757 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4758 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4759 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4761 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4762 range = isl_basic_map_universe(dim);
4764 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4765 bmap = isl_basic_map_apply_range(bmap, range);
4766 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4768 total = isl_basic_map_total_dim(bmap);
4770 for (i = 0; i < n_out; ++i) {
4771 k = isl_basic_map_alloc_equality(bmap);
4772 if (k < 0)
4773 goto error;
4774 isl_seq_clr(bmap->eq[k], 1 + total);
4775 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4776 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4779 bmap = isl_basic_map_gauss(bmap, NULL);
4780 return isl_basic_map_finalize(bmap);
4781 error:
4782 isl_basic_map_free(bmap);
4783 return NULL;
4786 int isl_map_may_be_set(__isl_keep isl_map *map)
4788 if (!map)
4789 return -1;
4790 return isl_space_may_be_set(map->dim);
4793 /* Is this map actually a set?
4794 * Users should never call this function. Outside of isl,
4795 * the type should indicate whether something is a set or a map.
4797 int isl_map_is_set(__isl_keep isl_map *map)
4799 if (!map)
4800 return -1;
4801 return isl_space_is_set(map->dim);
4804 struct isl_set *isl_map_range(struct isl_map *map)
4806 int i;
4807 struct isl_set *set;
4809 if (!map)
4810 goto error;
4811 if (isl_map_is_set(map))
4812 return (isl_set *)map;
4814 map = isl_map_cow(map);
4815 if (!map)
4816 goto error;
4818 set = (struct isl_set *) map;
4819 set->dim = isl_space_range(set->dim);
4820 if (!set->dim)
4821 goto error;
4822 for (i = 0; i < map->n; ++i) {
4823 set->p[i] = isl_basic_map_range(map->p[i]);
4824 if (!set->p[i])
4825 goto error;
4827 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4828 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4829 return set;
4830 error:
4831 isl_map_free(map);
4832 return NULL;
4835 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4837 int i;
4838 isl_space *domain_dim;
4840 map = isl_map_cow(map);
4841 if (!map)
4842 return NULL;
4844 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4845 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4846 map->dim = isl_space_join(map->dim, domain_dim);
4847 if (!map->dim)
4848 goto error;
4849 for (i = 0; i < map->n; ++i) {
4850 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4851 if (!map->p[i])
4852 goto error;
4854 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4855 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4856 return map;
4857 error:
4858 isl_map_free(map);
4859 return NULL;
4862 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4864 int i;
4865 isl_space *range_dim;
4867 map = isl_map_cow(map);
4868 if (!map)
4869 return NULL;
4871 range_dim = isl_space_range(isl_map_get_space(map));
4872 range_dim = isl_space_from_range(range_dim);
4873 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4874 map->dim = isl_space_join(map->dim, range_dim);
4875 if (!map->dim)
4876 goto error;
4877 for (i = 0; i < map->n; ++i) {
4878 map->p[i] = isl_basic_map_range_map(map->p[i]);
4879 if (!map->p[i])
4880 goto error;
4882 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4883 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4884 return map;
4885 error:
4886 isl_map_free(map);
4887 return NULL;
4890 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4891 __isl_take isl_space *dim)
4893 int i;
4894 struct isl_map *map = NULL;
4896 set = isl_set_cow(set);
4897 if (!set || !dim)
4898 goto error;
4899 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4900 map = (struct isl_map *)set;
4901 for (i = 0; i < set->n; ++i) {
4902 map->p[i] = isl_basic_map_from_basic_set(
4903 set->p[i], isl_space_copy(dim));
4904 if (!map->p[i])
4905 goto error;
4907 isl_space_free(map->dim);
4908 map->dim = dim;
4909 return map;
4910 error:
4911 isl_space_free(dim);
4912 isl_set_free(set);
4913 return NULL;
4916 __isl_give isl_basic_map *isl_basic_map_from_domain(
4917 __isl_take isl_basic_set *bset)
4919 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4922 __isl_give isl_basic_map *isl_basic_map_from_range(
4923 __isl_take isl_basic_set *bset)
4925 isl_space *space;
4926 space = isl_basic_set_get_space(bset);
4927 space = isl_space_from_range(space);
4928 bset = isl_basic_set_reset_space(bset, space);
4929 return (isl_basic_map *)bset;
4932 struct isl_map *isl_map_from_range(struct isl_set *set)
4934 isl_space *space;
4935 space = isl_set_get_space(set);
4936 space = isl_space_from_range(space);
4937 set = isl_set_reset_space(set, space);
4938 return (struct isl_map *)set;
4941 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4943 return isl_map_reverse(isl_map_from_range(set));
4946 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4947 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4949 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4952 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4953 __isl_take isl_set *range)
4955 return isl_map_apply_range(isl_map_reverse(domain), range);
4958 struct isl_set *isl_set_from_map(struct isl_map *map)
4960 int i;
4961 struct isl_set *set = NULL;
4963 if (!map)
4964 return NULL;
4965 map = isl_map_cow(map);
4966 if (!map)
4967 return NULL;
4968 map->dim = isl_space_as_set_space(map->dim);
4969 if (!map->dim)
4970 goto error;
4971 set = (struct isl_set *)map;
4972 for (i = 0; i < map->n; ++i) {
4973 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4974 if (!set->p[i])
4975 goto error;
4977 return set;
4978 error:
4979 isl_map_free(map);
4980 return NULL;
4983 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4984 unsigned flags)
4986 struct isl_map *map;
4988 if (!dim)
4989 return NULL;
4990 if (n < 0)
4991 isl_die(dim->ctx, isl_error_internal,
4992 "negative number of basic maps", goto error);
4993 map = isl_alloc(dim->ctx, struct isl_map,
4994 sizeof(struct isl_map) +
4995 (n - 1) * sizeof(struct isl_basic_map *));
4996 if (!map)
4997 goto error;
4999 map->ctx = dim->ctx;
5000 isl_ctx_ref(map->ctx);
5001 map->ref = 1;
5002 map->size = n;
5003 map->n = 0;
5004 map->dim = dim;
5005 map->flags = flags;
5006 return map;
5007 error:
5008 isl_space_free(dim);
5009 return NULL;
5012 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5013 unsigned nparam, unsigned in, unsigned out, int n,
5014 unsigned flags)
5016 struct isl_map *map;
5017 isl_space *dims;
5019 dims = isl_space_alloc(ctx, nparam, in, out);
5020 if (!dims)
5021 return NULL;
5023 map = isl_map_alloc_space(dims, n, flags);
5024 return map;
5027 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5029 struct isl_basic_map *bmap;
5030 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5031 bmap = isl_basic_map_set_to_empty(bmap);
5032 return bmap;
5035 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5037 struct isl_basic_set *bset;
5038 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5039 bset = isl_basic_set_set_to_empty(bset);
5040 return bset;
5043 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5045 struct isl_basic_map *bmap;
5046 if (!model)
5047 return NULL;
5048 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5049 bmap = isl_basic_map_set_to_empty(bmap);
5050 return bmap;
5053 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5055 struct isl_basic_map *bmap;
5056 if (!model)
5057 return NULL;
5058 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5059 bmap = isl_basic_map_set_to_empty(bmap);
5060 return bmap;
5063 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5065 struct isl_basic_set *bset;
5066 if (!model)
5067 return NULL;
5068 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5069 bset = isl_basic_set_set_to_empty(bset);
5070 return bset;
5073 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5075 struct isl_basic_map *bmap;
5076 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5077 bmap = isl_basic_map_finalize(bmap);
5078 return bmap;
5081 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5083 struct isl_basic_set *bset;
5084 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5085 bset = isl_basic_set_finalize(bset);
5086 return bset;
5089 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5091 int i;
5092 unsigned total = isl_space_dim(dim, isl_dim_all);
5093 isl_basic_map *bmap;
5095 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5096 for (i = 0; i < total; ++i) {
5097 int k = isl_basic_map_alloc_inequality(bmap);
5098 if (k < 0)
5099 goto error;
5100 isl_seq_clr(bmap->ineq[k], 1 + total);
5101 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5103 return bmap;
5104 error:
5105 isl_basic_map_free(bmap);
5106 return NULL;
5109 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5111 return isl_basic_map_nat_universe(dim);
5114 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5116 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5119 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5121 return isl_map_nat_universe(dim);
5124 __isl_give isl_basic_map *isl_basic_map_universe_like(
5125 __isl_keep isl_basic_map *model)
5127 if (!model)
5128 return NULL;
5129 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5132 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5134 if (!model)
5135 return NULL;
5136 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5139 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5140 __isl_keep isl_set *model)
5142 if (!model)
5143 return NULL;
5144 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5147 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5149 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5152 struct isl_map *isl_map_empty_like(struct isl_map *model)
5154 if (!model)
5155 return NULL;
5156 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5159 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5161 if (!model)
5162 return NULL;
5163 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5166 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5168 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5171 struct isl_set *isl_set_empty_like(struct isl_set *model)
5173 if (!model)
5174 return NULL;
5175 return isl_set_empty(isl_space_copy(model->dim));
5178 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5180 struct isl_map *map;
5181 if (!dim)
5182 return NULL;
5183 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5184 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5185 return map;
5188 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5190 struct isl_set *set;
5191 if (!dim)
5192 return NULL;
5193 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5194 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5195 return set;
5198 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5200 if (!model)
5201 return NULL;
5202 return isl_set_universe(isl_space_copy(model->dim));
5205 struct isl_map *isl_map_dup(struct isl_map *map)
5207 int i;
5208 struct isl_map *dup;
5210 if (!map)
5211 return NULL;
5212 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5213 for (i = 0; i < map->n; ++i)
5214 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5215 return dup;
5218 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5219 __isl_take isl_basic_map *bmap)
5221 if (!bmap || !map)
5222 goto error;
5223 if (isl_basic_map_plain_is_empty(bmap)) {
5224 isl_basic_map_free(bmap);
5225 return map;
5227 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5228 isl_assert(map->ctx, map->n < map->size, goto error);
5229 map->p[map->n] = bmap;
5230 map->n++;
5231 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5232 return map;
5233 error:
5234 if (map)
5235 isl_map_free(map);
5236 if (bmap)
5237 isl_basic_map_free(bmap);
5238 return NULL;
5241 void *isl_map_free(struct isl_map *map)
5243 int i;
5245 if (!map)
5246 return NULL;
5248 if (--map->ref > 0)
5249 return NULL;
5251 isl_ctx_deref(map->ctx);
5252 for (i = 0; i < map->n; ++i)
5253 isl_basic_map_free(map->p[i]);
5254 isl_space_free(map->dim);
5255 free(map);
5257 return NULL;
5260 struct isl_map *isl_map_extend(struct isl_map *base,
5261 unsigned nparam, unsigned n_in, unsigned n_out)
5263 int i;
5265 base = isl_map_cow(base);
5266 if (!base)
5267 return NULL;
5269 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5270 if (!base->dim)
5271 goto error;
5272 for (i = 0; i < base->n; ++i) {
5273 base->p[i] = isl_basic_map_extend_space(base->p[i],
5274 isl_space_copy(base->dim), 0, 0, 0);
5275 if (!base->p[i])
5276 goto error;
5278 return base;
5279 error:
5280 isl_map_free(base);
5281 return NULL;
5284 struct isl_set *isl_set_extend(struct isl_set *base,
5285 unsigned nparam, unsigned dim)
5287 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5288 nparam, 0, dim);
5291 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5292 struct isl_basic_map *bmap, unsigned pos, int value)
5294 int j;
5296 bmap = isl_basic_map_cow(bmap);
5297 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5298 j = isl_basic_map_alloc_equality(bmap);
5299 if (j < 0)
5300 goto error;
5301 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5302 isl_int_set_si(bmap->eq[j][pos], -1);
5303 isl_int_set_si(bmap->eq[j][0], value);
5304 bmap = isl_basic_map_simplify(bmap);
5305 return isl_basic_map_finalize(bmap);
5306 error:
5307 isl_basic_map_free(bmap);
5308 return NULL;
5311 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5312 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5314 int j;
5316 bmap = isl_basic_map_cow(bmap);
5317 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5318 j = isl_basic_map_alloc_equality(bmap);
5319 if (j < 0)
5320 goto error;
5321 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5322 isl_int_set_si(bmap->eq[j][pos], -1);
5323 isl_int_set(bmap->eq[j][0], value);
5324 bmap = isl_basic_map_simplify(bmap);
5325 return isl_basic_map_finalize(bmap);
5326 error:
5327 isl_basic_map_free(bmap);
5328 return NULL;
5331 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5332 enum isl_dim_type type, unsigned pos, int value)
5334 if (!bmap)
5335 return NULL;
5336 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5337 return isl_basic_map_fix_pos_si(bmap,
5338 isl_basic_map_offset(bmap, type) + pos, value);
5339 error:
5340 isl_basic_map_free(bmap);
5341 return NULL;
5344 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5345 enum isl_dim_type type, unsigned pos, isl_int value)
5347 if (!bmap)
5348 return NULL;
5349 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5350 return isl_basic_map_fix_pos(bmap,
5351 isl_basic_map_offset(bmap, type) + pos, value);
5352 error:
5353 isl_basic_map_free(bmap);
5354 return NULL;
5357 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5358 * to be equal to "v".
5360 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5361 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5363 if (!bmap || !v)
5364 goto error;
5365 if (!isl_val_is_int(v))
5366 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5367 "expecting integer value", goto error);
5368 if (pos >= isl_basic_map_dim(bmap, type))
5369 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5370 "index out of bounds", goto error);
5371 pos += isl_basic_map_offset(bmap, type);
5372 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5373 isl_val_free(v);
5374 return bmap;
5375 error:
5376 isl_basic_map_free(bmap);
5377 isl_val_free(v);
5378 return NULL;
5381 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5382 * to be equal to "v".
5384 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5385 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5387 return isl_basic_map_fix_val(bset, type, pos, v);
5390 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5391 enum isl_dim_type type, unsigned pos, int value)
5393 return (struct isl_basic_set *)
5394 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5395 type, pos, value);
5398 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5399 enum isl_dim_type type, unsigned pos, isl_int value)
5401 return (struct isl_basic_set *)
5402 isl_basic_map_fix((struct isl_basic_map *)bset,
5403 type, pos, value);
5406 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5407 unsigned input, int value)
5409 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5412 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5413 unsigned dim, int value)
5415 return (struct isl_basic_set *)
5416 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5417 isl_dim_set, dim, value);
5420 static int remove_if_empty(__isl_keep isl_map *map, int i)
5422 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5424 if (empty < 0)
5425 return -1;
5426 if (!empty)
5427 return 0;
5429 isl_basic_map_free(map->p[i]);
5430 if (i != map->n - 1) {
5431 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5432 map->p[i] = map->p[map->n - 1];
5434 map->n--;
5436 return 0;
5439 /* Perform "fn" on each basic map of "map", where we may not be holding
5440 * the only reference to "map".
5441 * In particular, "fn" should be a semantics preserving operation
5442 * that we want to apply to all copies of "map". We therefore need
5443 * to be careful not to modify "map" in a way that breaks "map"
5444 * in case anything goes wrong.
5446 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5447 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5449 struct isl_basic_map *bmap;
5450 int i;
5452 if (!map)
5453 return NULL;
5455 for (i = map->n - 1; i >= 0; --i) {
5456 bmap = isl_basic_map_copy(map->p[i]);
5457 bmap = fn(bmap);
5458 if (!bmap)
5459 goto error;
5460 isl_basic_map_free(map->p[i]);
5461 map->p[i] = bmap;
5462 if (remove_if_empty(map, i) < 0)
5463 goto error;
5466 return map;
5467 error:
5468 isl_map_free(map);
5469 return NULL;
5472 struct isl_map *isl_map_fix_si(struct isl_map *map,
5473 enum isl_dim_type type, unsigned pos, int value)
5475 int i;
5477 map = isl_map_cow(map);
5478 if (!map)
5479 return NULL;
5481 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5482 for (i = map->n - 1; i >= 0; --i) {
5483 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5484 if (remove_if_empty(map, i) < 0)
5485 goto error;
5487 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5488 return map;
5489 error:
5490 isl_map_free(map);
5491 return NULL;
5494 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5495 enum isl_dim_type type, unsigned pos, int value)
5497 return (struct isl_set *)
5498 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5501 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5502 enum isl_dim_type type, unsigned pos, isl_int value)
5504 int i;
5506 map = isl_map_cow(map);
5507 if (!map)
5508 return NULL;
5510 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5511 for (i = 0; i < map->n; ++i) {
5512 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5513 if (!map->p[i])
5514 goto error;
5516 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5517 return map;
5518 error:
5519 isl_map_free(map);
5520 return NULL;
5523 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5524 enum isl_dim_type type, unsigned pos, isl_int value)
5526 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5529 /* Fix the value of the variable at position "pos" of type "type" of "map"
5530 * to be equal to "v".
5532 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5533 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5535 int i;
5537 map = isl_map_cow(map);
5538 if (!map || !v)
5539 goto error;
5541 if (!isl_val_is_int(v))
5542 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5543 "expecting integer value", goto error);
5544 if (pos >= isl_map_dim(map, type))
5545 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5546 "index out of bounds", goto error);
5547 for (i = map->n - 1; i >= 0; --i) {
5548 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5549 isl_val_copy(v));
5550 if (remove_if_empty(map, i) < 0)
5551 goto error;
5553 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5554 isl_val_free(v);
5555 return map;
5556 error:
5557 isl_map_free(map);
5558 isl_val_free(v);
5559 return NULL;
5562 /* Fix the value of the variable at position "pos" of type "type" of "set"
5563 * to be equal to "v".
5565 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5566 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5568 return isl_map_fix_val(set, type, pos, v);
5571 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5572 unsigned input, int value)
5574 return isl_map_fix_si(map, isl_dim_in, input, value);
5577 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5579 return (struct isl_set *)
5580 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5583 static __isl_give isl_basic_map *basic_map_bound_si(
5584 __isl_take isl_basic_map *bmap,
5585 enum isl_dim_type type, unsigned pos, int value, int upper)
5587 int j;
5589 if (!bmap)
5590 return NULL;
5591 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5592 pos += isl_basic_map_offset(bmap, type);
5593 bmap = isl_basic_map_cow(bmap);
5594 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5595 j = isl_basic_map_alloc_inequality(bmap);
5596 if (j < 0)
5597 goto error;
5598 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5599 if (upper) {
5600 isl_int_set_si(bmap->ineq[j][pos], -1);
5601 isl_int_set_si(bmap->ineq[j][0], value);
5602 } else {
5603 isl_int_set_si(bmap->ineq[j][pos], 1);
5604 isl_int_set_si(bmap->ineq[j][0], -value);
5606 bmap = isl_basic_map_simplify(bmap);
5607 return isl_basic_map_finalize(bmap);
5608 error:
5609 isl_basic_map_free(bmap);
5610 return NULL;
5613 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5614 __isl_take isl_basic_map *bmap,
5615 enum isl_dim_type type, unsigned pos, int value)
5617 return basic_map_bound_si(bmap, type, pos, value, 0);
5620 /* Constrain the values of the given dimension to be no greater than "value".
5622 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5623 __isl_take isl_basic_map *bmap,
5624 enum isl_dim_type type, unsigned pos, int value)
5626 return basic_map_bound_si(bmap, type, pos, value, 1);
5629 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5630 unsigned dim, isl_int value)
5632 int j;
5634 bset = isl_basic_set_cow(bset);
5635 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5636 j = isl_basic_set_alloc_inequality(bset);
5637 if (j < 0)
5638 goto error;
5639 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5640 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5641 isl_int_neg(bset->ineq[j][0], value);
5642 bset = isl_basic_set_simplify(bset);
5643 return isl_basic_set_finalize(bset);
5644 error:
5645 isl_basic_set_free(bset);
5646 return NULL;
5649 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5650 enum isl_dim_type type, unsigned pos, int value, int upper)
5652 int i;
5654 map = isl_map_cow(map);
5655 if (!map)
5656 return NULL;
5658 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5659 for (i = 0; i < map->n; ++i) {
5660 map->p[i] = basic_map_bound_si(map->p[i],
5661 type, pos, value, upper);
5662 if (!map->p[i])
5663 goto error;
5665 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5666 return map;
5667 error:
5668 isl_map_free(map);
5669 return NULL;
5672 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5673 enum isl_dim_type type, unsigned pos, int value)
5675 return map_bound_si(map, type, pos, value, 0);
5678 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5679 enum isl_dim_type type, unsigned pos, int value)
5681 return map_bound_si(map, type, pos, value, 1);
5684 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5685 enum isl_dim_type type, unsigned pos, int value)
5687 return (struct isl_set *)
5688 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5691 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5692 enum isl_dim_type type, unsigned pos, int value)
5694 return isl_map_upper_bound_si(set, type, pos, value);
5697 /* Bound the given variable of "bmap" from below (or above is "upper"
5698 * is set) to "value".
5700 static __isl_give isl_basic_map *basic_map_bound(
5701 __isl_take isl_basic_map *bmap,
5702 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5704 int j;
5706 if (!bmap)
5707 return NULL;
5708 if (pos >= isl_basic_map_dim(bmap, type))
5709 isl_die(bmap->ctx, isl_error_invalid,
5710 "index out of bounds", goto error);
5711 pos += isl_basic_map_offset(bmap, type);
5712 bmap = isl_basic_map_cow(bmap);
5713 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5714 j = isl_basic_map_alloc_inequality(bmap);
5715 if (j < 0)
5716 goto error;
5717 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5718 if (upper) {
5719 isl_int_set_si(bmap->ineq[j][pos], -1);
5720 isl_int_set(bmap->ineq[j][0], value);
5721 } else {
5722 isl_int_set_si(bmap->ineq[j][pos], 1);
5723 isl_int_neg(bmap->ineq[j][0], value);
5725 bmap = isl_basic_map_simplify(bmap);
5726 return isl_basic_map_finalize(bmap);
5727 error:
5728 isl_basic_map_free(bmap);
5729 return NULL;
5732 /* Bound the given variable of "map" from below (or above is "upper"
5733 * is set) to "value".
5735 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5736 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5738 int i;
5740 map = isl_map_cow(map);
5741 if (!map)
5742 return NULL;
5744 if (pos >= isl_map_dim(map, type))
5745 isl_die(map->ctx, isl_error_invalid,
5746 "index out of bounds", goto error);
5747 for (i = map->n - 1; i >= 0; --i) {
5748 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5749 if (remove_if_empty(map, i) < 0)
5750 goto error;
5752 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5753 return map;
5754 error:
5755 isl_map_free(map);
5756 return NULL;
5759 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5760 enum isl_dim_type type, unsigned pos, isl_int value)
5762 return map_bound(map, type, pos, value, 0);
5765 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5766 enum isl_dim_type type, unsigned pos, isl_int value)
5768 return map_bound(map, type, pos, value, 1);
5771 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5772 enum isl_dim_type type, unsigned pos, isl_int value)
5774 return isl_map_lower_bound(set, type, pos, value);
5777 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5778 enum isl_dim_type type, unsigned pos, isl_int value)
5780 return isl_map_upper_bound(set, type, pos, value);
5783 /* Force the values of the variable at position "pos" of type "type" of "set"
5784 * to be no smaller than "value".
5786 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5787 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5789 if (!value)
5790 goto error;
5791 if (!isl_val_is_int(value))
5792 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5793 "expecting integer value", goto error);
5794 set = isl_set_lower_bound(set, type, pos, value->n);
5795 isl_val_free(value);
5796 return set;
5797 error:
5798 isl_val_free(value);
5799 isl_set_free(set);
5800 return NULL;
5803 /* Force the values of the variable at position "pos" of type "type" of "set"
5804 * to be no greater than "value".
5806 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5807 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5809 if (!value)
5810 goto error;
5811 if (!isl_val_is_int(value))
5812 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5813 "expecting integer value", goto error);
5814 set = isl_set_upper_bound(set, type, pos, value->n);
5815 isl_val_free(value);
5816 return set;
5817 error:
5818 isl_val_free(value);
5819 isl_set_free(set);
5820 return NULL;
5823 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5824 isl_int value)
5826 int i;
5828 set = isl_set_cow(set);
5829 if (!set)
5830 return NULL;
5832 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5833 for (i = 0; i < set->n; ++i) {
5834 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5835 if (!set->p[i])
5836 goto error;
5838 return set;
5839 error:
5840 isl_set_free(set);
5841 return NULL;
5844 struct isl_map *isl_map_reverse(struct isl_map *map)
5846 int i;
5848 map = isl_map_cow(map);
5849 if (!map)
5850 return NULL;
5852 map->dim = isl_space_reverse(map->dim);
5853 if (!map->dim)
5854 goto error;
5855 for (i = 0; i < map->n; ++i) {
5856 map->p[i] = isl_basic_map_reverse(map->p[i]);
5857 if (!map->p[i])
5858 goto error;
5860 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5861 return map;
5862 error:
5863 isl_map_free(map);
5864 return NULL;
5867 static struct isl_map *isl_basic_map_partial_lexopt(
5868 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5869 struct isl_set **empty, int max)
5871 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5874 struct isl_map *isl_basic_map_partial_lexmax(
5875 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5876 struct isl_set **empty)
5878 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5881 struct isl_map *isl_basic_map_partial_lexmin(
5882 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5883 struct isl_set **empty)
5885 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5888 struct isl_set *isl_basic_set_partial_lexmin(
5889 struct isl_basic_set *bset, struct isl_basic_set *dom,
5890 struct isl_set **empty)
5892 return (struct isl_set *)
5893 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5894 dom, empty);
5897 struct isl_set *isl_basic_set_partial_lexmax(
5898 struct isl_basic_set *bset, struct isl_basic_set *dom,
5899 struct isl_set **empty)
5901 return (struct isl_set *)
5902 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5903 dom, empty);
5906 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5907 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5908 __isl_give isl_set **empty)
5910 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5913 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5914 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5915 __isl_give isl_set **empty)
5917 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5920 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5921 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5922 __isl_give isl_set **empty)
5924 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5927 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5928 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5929 __isl_give isl_set **empty)
5931 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5934 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5935 __isl_take isl_basic_map *bmap, int max)
5937 isl_basic_set *dom = NULL;
5938 isl_space *dom_space;
5940 if (!bmap)
5941 goto error;
5942 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5943 dom = isl_basic_set_universe(dom_space);
5944 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5945 error:
5946 isl_basic_map_free(bmap);
5947 return NULL;
5950 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5951 __isl_take isl_basic_map *bmap)
5953 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5956 #undef TYPE
5957 #define TYPE isl_pw_multi_aff
5958 #undef SUFFIX
5959 #define SUFFIX _pw_multi_aff
5960 #undef EMPTY
5961 #define EMPTY isl_pw_multi_aff_empty
5962 #undef ADD
5963 #define ADD isl_pw_multi_aff_union_add
5964 #include "isl_map_lexopt_templ.c"
5966 /* Given a map "map", compute the lexicographically minimal
5967 * (or maximal) image element for each domain element in dom,
5968 * in the form of an isl_pw_multi_aff.
5969 * Set *empty to those elements in dom that do not have an image element.
5971 * We first compute the lexicographically minimal or maximal element
5972 * in the first basic map. This results in a partial solution "res"
5973 * and a subset "todo" of dom that still need to be handled.
5974 * We then consider each of the remaining maps in "map" and successively
5975 * update both "res" and "todo".
5977 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
5978 __isl_take isl_map *map, __isl_take isl_set *dom,
5979 __isl_give isl_set **empty, int max)
5981 int i;
5982 isl_pw_multi_aff *res;
5983 isl_set *todo;
5985 if (!map || !dom)
5986 goto error;
5988 if (isl_map_plain_is_empty(map)) {
5989 if (empty)
5990 *empty = dom;
5991 else
5992 isl_set_free(dom);
5993 return isl_pw_multi_aff_from_map(map);
5996 res = basic_map_partial_lexopt_pw_multi_aff(
5997 isl_basic_map_copy(map->p[0]),
5998 isl_set_copy(dom), &todo, max);
6000 for (i = 1; i < map->n; ++i) {
6001 isl_pw_multi_aff *res_i;
6002 isl_set *todo_i;
6004 res_i = basic_map_partial_lexopt_pw_multi_aff(
6005 isl_basic_map_copy(map->p[i]),
6006 isl_set_copy(dom), &todo_i, max);
6008 if (max)
6009 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6010 else
6011 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6013 todo = isl_set_intersect(todo, todo_i);
6016 isl_set_free(dom);
6017 isl_map_free(map);
6019 if (empty)
6020 *empty = todo;
6021 else
6022 isl_set_free(todo);
6024 return res;
6025 error:
6026 if (empty)
6027 *empty = NULL;
6028 isl_set_free(dom);
6029 isl_map_free(map);
6030 return NULL;
6033 #undef TYPE
6034 #define TYPE isl_map
6035 #undef SUFFIX
6036 #define SUFFIX
6037 #undef EMPTY
6038 #define EMPTY isl_map_empty
6039 #undef ADD
6040 #define ADD isl_map_union_disjoint
6041 #include "isl_map_lexopt_templ.c"
6043 /* Given a map "map", compute the lexicographically minimal
6044 * (or maximal) image element for each domain element in dom.
6045 * Set *empty to those elements in dom that do not have an image element.
6047 * We first compute the lexicographically minimal or maximal element
6048 * in the first basic map. This results in a partial solution "res"
6049 * and a subset "todo" of dom that still need to be handled.
6050 * We then consider each of the remaining maps in "map" and successively
6051 * update both "res" and "todo".
6053 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6054 * Assume we are computing the lexicographical maximum.
6055 * We first compute the lexicographically maximal element in basic map i.
6056 * This results in a partial solution res_i and a subset todo_i.
6057 * Then we combine these results with those obtain for the first k basic maps
6058 * to obtain a result that is valid for the first k+1 basic maps.
6059 * In particular, the set where there is no solution is the set where
6060 * there is no solution for the first k basic maps and also no solution
6061 * for the ith basic map, i.e.,
6063 * todo^i = todo^k * todo_i
6065 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6066 * solutions, arbitrarily breaking ties in favor of res^k.
6067 * That is, when res^k(a) >= res_i(a), we pick res^k and
6068 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6069 * the lexicographic order.)
6070 * In practice, we compute
6072 * res^k * (res_i . "<=")
6074 * and
6076 * res_i * (res^k . "<")
6078 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6079 * where only one of res^k and res_i provides a solution and we simply pick
6080 * that one, i.e.,
6082 * res^k * todo_i
6083 * and
6084 * res_i * todo^k
6086 * Note that we only compute these intersections when dom(res^k) intersects
6087 * dom(res_i). Otherwise, the only effect of these intersections is to
6088 * potentially break up res^k and res_i into smaller pieces.
6089 * We want to avoid such splintering as much as possible.
6090 * In fact, an earlier implementation of this function would look for
6091 * better results in the domain of res^k and for extra results in todo^k,
6092 * but this would always result in a splintering according to todo^k,
6093 * even when the domain of basic map i is disjoint from the domains of
6094 * the previous basic maps.
6096 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6097 __isl_take isl_map *map, __isl_take isl_set *dom,
6098 __isl_give isl_set **empty, int max)
6100 int i;
6101 struct isl_map *res;
6102 struct isl_set *todo;
6104 if (!map || !dom)
6105 goto error;
6107 if (isl_map_plain_is_empty(map)) {
6108 if (empty)
6109 *empty = dom;
6110 else
6111 isl_set_free(dom);
6112 return map;
6115 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6116 isl_set_copy(dom), &todo, max);
6118 for (i = 1; i < map->n; ++i) {
6119 isl_map *lt, *le;
6120 isl_map *res_i;
6121 isl_set *todo_i;
6122 isl_space *dim = isl_space_range(isl_map_get_space(res));
6124 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6125 isl_set_copy(dom), &todo_i, max);
6127 if (max) {
6128 lt = isl_map_lex_lt(isl_space_copy(dim));
6129 le = isl_map_lex_le(dim);
6130 } else {
6131 lt = isl_map_lex_gt(isl_space_copy(dim));
6132 le = isl_map_lex_ge(dim);
6134 lt = isl_map_apply_range(isl_map_copy(res), lt);
6135 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6136 le = isl_map_apply_range(isl_map_copy(res_i), le);
6137 le = isl_map_intersect(le, isl_map_copy(res));
6139 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6140 res = isl_map_intersect_domain(res,
6141 isl_set_copy(todo_i));
6142 res_i = isl_map_intersect_domain(res_i,
6143 isl_set_copy(todo));
6146 res = isl_map_union_disjoint(res, res_i);
6147 res = isl_map_union_disjoint(res, lt);
6148 res = isl_map_union_disjoint(res, le);
6150 todo = isl_set_intersect(todo, todo_i);
6153 isl_set_free(dom);
6154 isl_map_free(map);
6156 if (empty)
6157 *empty = todo;
6158 else
6159 isl_set_free(todo);
6161 return res;
6162 error:
6163 if (empty)
6164 *empty = NULL;
6165 isl_set_free(dom);
6166 isl_map_free(map);
6167 return NULL;
6170 __isl_give isl_map *isl_map_partial_lexmax(
6171 __isl_take isl_map *map, __isl_take isl_set *dom,
6172 __isl_give isl_set **empty)
6174 return isl_map_partial_lexopt(map, dom, empty, 1);
6177 __isl_give isl_map *isl_map_partial_lexmin(
6178 __isl_take isl_map *map, __isl_take isl_set *dom,
6179 __isl_give isl_set **empty)
6181 return isl_map_partial_lexopt(map, dom, empty, 0);
6184 __isl_give isl_set *isl_set_partial_lexmin(
6185 __isl_take isl_set *set, __isl_take isl_set *dom,
6186 __isl_give isl_set **empty)
6188 return (struct isl_set *)
6189 isl_map_partial_lexmin((struct isl_map *)set,
6190 dom, empty);
6193 __isl_give isl_set *isl_set_partial_lexmax(
6194 __isl_take isl_set *set, __isl_take isl_set *dom,
6195 __isl_give isl_set **empty)
6197 return (struct isl_set *)
6198 isl_map_partial_lexmax((struct isl_map *)set,
6199 dom, empty);
6202 /* Compute the lexicographic minimum (or maximum if "max" is set)
6203 * of "bmap" over its domain.
6205 * Since we are not interested in the part of the domain space where
6206 * there is no solution, we initialize the domain to those constraints
6207 * of "bmap" that only involve the parameters and the input dimensions.
6208 * This relieves the parametric programming engine from detecting those
6209 * inequalities and transferring them to the context. More importantly,
6210 * it ensures that those inequalities are transferred first and not
6211 * intermixed with inequalities that actually split the domain.
6213 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6215 int n_div;
6216 int n_out;
6217 isl_basic_map *copy;
6218 isl_basic_set *dom;
6220 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6221 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6222 copy = isl_basic_map_copy(bmap);
6223 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6224 isl_dim_div, 0, n_div);
6225 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6226 isl_dim_out, 0, n_out);
6227 dom = isl_basic_map_domain(copy);
6228 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6231 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6233 return isl_basic_map_lexopt(bmap, 0);
6236 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6238 return isl_basic_map_lexopt(bmap, 1);
6241 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6243 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6246 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6248 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6251 /* Extract the first and only affine expression from list
6252 * and then add it to *pwaff with the given dom.
6253 * This domain is known to be disjoint from other domains
6254 * because of the way isl_basic_map_foreach_lexmax works.
6256 static int update_dim_opt(__isl_take isl_basic_set *dom,
6257 __isl_take isl_aff_list *list, void *user)
6259 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6260 isl_aff *aff;
6261 isl_pw_aff **pwaff = user;
6262 isl_pw_aff *pwaff_i;
6264 if (!list)
6265 goto error;
6266 if (isl_aff_list_n_aff(list) != 1)
6267 isl_die(ctx, isl_error_internal,
6268 "expecting single element list", goto error);
6270 aff = isl_aff_list_get_aff(list, 0);
6271 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6273 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6275 isl_aff_list_free(list);
6277 return 0;
6278 error:
6279 isl_basic_set_free(dom);
6280 isl_aff_list_free(list);
6281 return -1;
6284 /* Given a basic map with one output dimension, compute the minimum or
6285 * maximum of that dimension as an isl_pw_aff.
6287 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6288 * call update_dim_opt on each leaf of the result.
6290 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6291 int max)
6293 isl_space *dim = isl_basic_map_get_space(bmap);
6294 isl_pw_aff *pwaff;
6295 int r;
6297 dim = isl_space_from_domain(isl_space_domain(dim));
6298 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6299 pwaff = isl_pw_aff_empty(dim);
6301 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6302 if (r < 0)
6303 return isl_pw_aff_free(pwaff);
6305 return pwaff;
6308 /* Compute the minimum or maximum of the given output dimension
6309 * as a function of the parameters and the input dimensions,
6310 * but independently of the other output dimensions.
6312 * We first project out the other output dimension and then compute
6313 * the "lexicographic" maximum in each basic map, combining the results
6314 * using isl_pw_aff_union_max.
6316 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6317 int max)
6319 int i;
6320 isl_pw_aff *pwaff;
6321 unsigned n_out;
6323 n_out = isl_map_dim(map, isl_dim_out);
6324 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6325 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6326 if (!map)
6327 return NULL;
6329 if (map->n == 0) {
6330 isl_space *dim = isl_map_get_space(map);
6331 dim = isl_space_domain(isl_space_from_range(dim));
6332 isl_map_free(map);
6333 return isl_pw_aff_empty(dim);
6336 pwaff = basic_map_dim_opt(map->p[0], max);
6337 for (i = 1; i < map->n; ++i) {
6338 isl_pw_aff *pwaff_i;
6340 pwaff_i = basic_map_dim_opt(map->p[i], max);
6341 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6344 isl_map_free(map);
6346 return pwaff;
6349 /* Compute the maximum of the given output dimension as a function of the
6350 * parameters and input dimensions, but independently of
6351 * the other output dimensions.
6353 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6355 return map_dim_opt(map, pos, 1);
6358 /* Compute the minimum or maximum of the given set dimension
6359 * as a function of the parameters,
6360 * but independently of the other set dimensions.
6362 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6363 int max)
6365 return map_dim_opt(set, pos, max);
6368 /* Compute the maximum of the given set dimension as a function of the
6369 * parameters, but independently of the other set dimensions.
6371 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6373 return set_dim_opt(set, pos, 1);
6376 /* Compute the minimum of the given set dimension as a function of the
6377 * parameters, but independently of the other set dimensions.
6379 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6381 return set_dim_opt(set, pos, 0);
6384 /* Apply a preimage specified by "mat" on the parameters of "bset".
6385 * bset is assumed to have only parameters and divs.
6387 static struct isl_basic_set *basic_set_parameter_preimage(
6388 struct isl_basic_set *bset, struct isl_mat *mat)
6390 unsigned nparam;
6392 if (!bset || !mat)
6393 goto error;
6395 bset->dim = isl_space_cow(bset->dim);
6396 if (!bset->dim)
6397 goto error;
6399 nparam = isl_basic_set_dim(bset, isl_dim_param);
6401 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6403 bset->dim->nparam = 0;
6404 bset->dim->n_out = nparam;
6405 bset = isl_basic_set_preimage(bset, mat);
6406 if (bset) {
6407 bset->dim->nparam = bset->dim->n_out;
6408 bset->dim->n_out = 0;
6410 return bset;
6411 error:
6412 isl_mat_free(mat);
6413 isl_basic_set_free(bset);
6414 return NULL;
6417 /* Apply a preimage specified by "mat" on the parameters of "set".
6418 * set is assumed to have only parameters and divs.
6420 static struct isl_set *set_parameter_preimage(
6421 struct isl_set *set, struct isl_mat *mat)
6423 isl_space *dim = NULL;
6424 unsigned nparam;
6426 if (!set || !mat)
6427 goto error;
6429 dim = isl_space_copy(set->dim);
6430 dim = isl_space_cow(dim);
6431 if (!dim)
6432 goto error;
6434 nparam = isl_set_dim(set, isl_dim_param);
6436 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6438 dim->nparam = 0;
6439 dim->n_out = nparam;
6440 isl_set_reset_space(set, dim);
6441 set = isl_set_preimage(set, mat);
6442 if (!set)
6443 goto error2;
6444 dim = isl_space_copy(set->dim);
6445 dim = isl_space_cow(dim);
6446 if (!dim)
6447 goto error2;
6448 dim->nparam = dim->n_out;
6449 dim->n_out = 0;
6450 isl_set_reset_space(set, dim);
6451 return set;
6452 error:
6453 isl_space_free(dim);
6454 isl_mat_free(mat);
6455 error2:
6456 isl_set_free(set);
6457 return NULL;
6460 /* Intersect the basic set "bset" with the affine space specified by the
6461 * equalities in "eq".
6463 static struct isl_basic_set *basic_set_append_equalities(
6464 struct isl_basic_set *bset, struct isl_mat *eq)
6466 int i, k;
6467 unsigned len;
6469 if (!bset || !eq)
6470 goto error;
6472 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6473 eq->n_row, 0);
6474 if (!bset)
6475 goto error;
6477 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6478 for (i = 0; i < eq->n_row; ++i) {
6479 k = isl_basic_set_alloc_equality(bset);
6480 if (k < 0)
6481 goto error;
6482 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6483 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6485 isl_mat_free(eq);
6487 bset = isl_basic_set_gauss(bset, NULL);
6488 bset = isl_basic_set_finalize(bset);
6490 return bset;
6491 error:
6492 isl_mat_free(eq);
6493 isl_basic_set_free(bset);
6494 return NULL;
6497 /* Intersect the set "set" with the affine space specified by the
6498 * equalities in "eq".
6500 static struct isl_set *set_append_equalities(struct isl_set *set,
6501 struct isl_mat *eq)
6503 int i;
6505 if (!set || !eq)
6506 goto error;
6508 for (i = 0; i < set->n; ++i) {
6509 set->p[i] = basic_set_append_equalities(set->p[i],
6510 isl_mat_copy(eq));
6511 if (!set->p[i])
6512 goto error;
6514 isl_mat_free(eq);
6515 return set;
6516 error:
6517 isl_mat_free(eq);
6518 isl_set_free(set);
6519 return NULL;
6522 /* Given a basic set "bset" that only involves parameters and existentially
6523 * quantified variables, return the index of the first equality
6524 * that only involves parameters. If there is no such equality then
6525 * return bset->n_eq.
6527 * This function assumes that isl_basic_set_gauss has been called on "bset".
6529 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6531 int i, j;
6532 unsigned nparam, n_div;
6534 if (!bset)
6535 return -1;
6537 nparam = isl_basic_set_dim(bset, isl_dim_param);
6538 n_div = isl_basic_set_dim(bset, isl_dim_div);
6540 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6541 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6542 ++i;
6545 return i;
6548 /* Compute an explicit representation for the existentially quantified
6549 * variables in "bset" by computing the "minimal value" of the set
6550 * variables. Since there are no set variables, the computation of
6551 * the minimal value essentially computes an explicit representation
6552 * of the non-empty part(s) of "bset".
6554 * The input only involves parameters and existentially quantified variables.
6555 * All equalities among parameters have been removed.
6557 * Since the existentially quantified variables in the result are in general
6558 * going to be different from those in the input, we first replace
6559 * them by the minimal number of variables based on their equalities.
6560 * This should simplify the parametric integer programming.
6562 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6564 isl_morph *morph1, *morph2;
6565 isl_set *set;
6566 unsigned n;
6568 if (!bset)
6569 return NULL;
6570 if (bset->n_eq == 0)
6571 return isl_basic_set_lexmin(bset);
6573 morph1 = isl_basic_set_parameter_compression(bset);
6574 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6575 bset = isl_basic_set_lift(bset);
6576 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6577 bset = isl_morph_basic_set(morph2, bset);
6578 n = isl_basic_set_dim(bset, isl_dim_set);
6579 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6581 set = isl_basic_set_lexmin(bset);
6583 set = isl_morph_set(isl_morph_inverse(morph1), set);
6585 return set;
6588 /* Project the given basic set onto its parameter domain, possibly introducing
6589 * new, explicit, existential variables in the constraints.
6590 * The input has parameters and (possibly implicit) existential variables.
6591 * The output has the same parameters, but only
6592 * explicit existentially quantified variables.
6594 * The actual projection is performed by pip, but pip doesn't seem
6595 * to like equalities very much, so we first remove the equalities
6596 * among the parameters by performing a variable compression on
6597 * the parameters. Afterward, an inverse transformation is performed
6598 * and the equalities among the parameters are inserted back in.
6600 * The variable compression on the parameters may uncover additional
6601 * equalities that were only implicit before. We therefore check
6602 * if there are any new parameter equalities in the result and
6603 * if so recurse. The removal of parameter equalities is required
6604 * for the parameter compression performed by base_compute_divs.
6606 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6608 int i;
6609 struct isl_mat *eq;
6610 struct isl_mat *T, *T2;
6611 struct isl_set *set;
6612 unsigned nparam;
6614 bset = isl_basic_set_cow(bset);
6615 if (!bset)
6616 return NULL;
6618 if (bset->n_eq == 0)
6619 return base_compute_divs(bset);
6621 bset = isl_basic_set_gauss(bset, NULL);
6622 if (!bset)
6623 return NULL;
6624 if (isl_basic_set_plain_is_empty(bset))
6625 return isl_set_from_basic_set(bset);
6627 i = first_parameter_equality(bset);
6628 if (i == bset->n_eq)
6629 return base_compute_divs(bset);
6631 nparam = isl_basic_set_dim(bset, isl_dim_param);
6632 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6633 0, 1 + nparam);
6634 eq = isl_mat_cow(eq);
6635 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6636 if (T && T->n_col == 0) {
6637 isl_mat_free(T);
6638 isl_mat_free(T2);
6639 isl_mat_free(eq);
6640 bset = isl_basic_set_set_to_empty(bset);
6641 return isl_set_from_basic_set(bset);
6643 bset = basic_set_parameter_preimage(bset, T);
6645 i = first_parameter_equality(bset);
6646 if (!bset)
6647 set = NULL;
6648 else if (i == bset->n_eq)
6649 set = base_compute_divs(bset);
6650 else
6651 set = parameter_compute_divs(bset);
6652 set = set_parameter_preimage(set, T2);
6653 set = set_append_equalities(set, eq);
6654 return set;
6657 /* Insert the divs from "ls" before those of "bmap".
6659 * The number of columns is not changed, which means that the last
6660 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6661 * The caller is responsible for removing the same number of dimensions
6662 * from the space of "bmap".
6664 static __isl_give isl_basic_map *insert_divs_from_local_space(
6665 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6667 int i;
6668 int n_div;
6669 int old_n_div;
6671 n_div = isl_local_space_dim(ls, isl_dim_div);
6672 if (n_div == 0)
6673 return bmap;
6675 old_n_div = bmap->n_div;
6676 bmap = insert_div_rows(bmap, n_div);
6677 if (!bmap)
6678 return NULL;
6680 for (i = 0; i < n_div; ++i) {
6681 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6682 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6685 return bmap;
6688 /* Replace the space of "bmap" by the space and divs of "ls".
6690 * If "ls" has any divs, then we simplify the result since we may
6691 * have discovered some additional equalities that could simplify
6692 * the div expressions.
6694 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6695 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6697 int n_div;
6699 bmap = isl_basic_map_cow(bmap);
6700 if (!bmap || !ls)
6701 goto error;
6703 n_div = isl_local_space_dim(ls, isl_dim_div);
6704 bmap = insert_divs_from_local_space(bmap, ls);
6705 if (!bmap)
6706 goto error;
6708 isl_space_free(bmap->dim);
6709 bmap->dim = isl_local_space_get_space(ls);
6710 if (!bmap->dim)
6711 goto error;
6713 isl_local_space_free(ls);
6714 if (n_div > 0)
6715 bmap = isl_basic_map_simplify(bmap);
6716 bmap = isl_basic_map_finalize(bmap);
6717 return bmap;
6718 error:
6719 isl_basic_map_free(bmap);
6720 isl_local_space_free(ls);
6721 return NULL;
6724 /* Replace the space of "map" by the space and divs of "ls".
6726 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6727 __isl_take isl_local_space *ls)
6729 int i;
6731 map = isl_map_cow(map);
6732 if (!map || !ls)
6733 goto error;
6735 for (i = 0; i < map->n; ++i) {
6736 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6737 isl_local_space_copy(ls));
6738 if (!map->p[i])
6739 goto error;
6741 isl_space_free(map->dim);
6742 map->dim = isl_local_space_get_space(ls);
6743 if (!map->dim)
6744 goto error;
6746 isl_local_space_free(ls);
6747 return map;
6748 error:
6749 isl_local_space_free(ls);
6750 isl_map_free(map);
6751 return NULL;
6754 /* Compute an explicit representation for the existentially
6755 * quantified variables for which do not know any explicit representation yet.
6757 * We first sort the existentially quantified variables so that the
6758 * existentially quantified variables for which we already have an explicit
6759 * representation are placed before those for which we do not.
6760 * The input dimensions, the output dimensions and the existentially
6761 * quantified variables for which we already have an explicit
6762 * representation are then turned into parameters.
6763 * compute_divs returns a map with the same parameters and
6764 * no input or output dimensions and the dimension specification
6765 * is reset to that of the input, including the existentially quantified
6766 * variables for which we already had an explicit representation.
6768 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6770 struct isl_basic_set *bset;
6771 struct isl_set *set;
6772 struct isl_map *map;
6773 isl_space *dim;
6774 isl_local_space *ls;
6775 unsigned nparam;
6776 unsigned n_in;
6777 unsigned n_out;
6778 unsigned n_known;
6779 int i;
6781 bmap = isl_basic_map_sort_divs(bmap);
6782 bmap = isl_basic_map_cow(bmap);
6783 if (!bmap)
6784 return NULL;
6786 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6787 if (isl_int_is_zero(bmap->div[n_known][0]))
6788 break;
6790 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6791 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6792 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6793 dim = isl_space_set_alloc(bmap->ctx,
6794 nparam + n_in + n_out + n_known, 0);
6795 if (!dim)
6796 goto error;
6798 ls = isl_basic_map_get_local_space(bmap);
6799 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6800 n_known, bmap->n_div - n_known);
6801 if (n_known > 0) {
6802 for (i = n_known; i < bmap->n_div; ++i)
6803 swap_div(bmap, i - n_known, i);
6804 bmap->n_div -= n_known;
6805 bmap->extra -= n_known;
6807 bmap = isl_basic_map_reset_space(bmap, dim);
6808 bset = (struct isl_basic_set *)bmap;
6810 set = parameter_compute_divs(bset);
6811 map = (struct isl_map *)set;
6812 map = replace_space_by_local_space(map, ls);
6814 return map;
6815 error:
6816 isl_basic_map_free(bmap);
6817 return NULL;
6820 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6822 int i;
6823 unsigned off;
6825 if (!bmap)
6826 return -1;
6828 off = isl_space_dim(bmap->dim, isl_dim_all);
6829 for (i = 0; i < bmap->n_div; ++i) {
6830 if (isl_int_is_zero(bmap->div[i][0]))
6831 return 0;
6832 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6833 return -1);
6835 return 1;
6838 static int map_divs_known(__isl_keep isl_map *map)
6840 int i;
6842 if (!map)
6843 return -1;
6845 for (i = 0; i < map->n; ++i) {
6846 int known = isl_basic_map_divs_known(map->p[i]);
6847 if (known <= 0)
6848 return known;
6851 return 1;
6854 /* If bmap contains any unknown divs, then compute explicit
6855 * expressions for them. However, this computation may be
6856 * quite expensive, so first try to remove divs that aren't
6857 * strictly needed.
6859 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6861 int known;
6862 struct isl_map *map;
6864 known = isl_basic_map_divs_known(bmap);
6865 if (known < 0)
6866 goto error;
6867 if (known)
6868 return isl_map_from_basic_map(bmap);
6870 bmap = isl_basic_map_drop_redundant_divs(bmap);
6872 known = isl_basic_map_divs_known(bmap);
6873 if (known < 0)
6874 goto error;
6875 if (known)
6876 return isl_map_from_basic_map(bmap);
6878 map = compute_divs(bmap);
6879 return map;
6880 error:
6881 isl_basic_map_free(bmap);
6882 return NULL;
6885 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6887 int i;
6888 int known;
6889 struct isl_map *res;
6891 if (!map)
6892 return NULL;
6893 if (map->n == 0)
6894 return map;
6896 known = map_divs_known(map);
6897 if (known < 0) {
6898 isl_map_free(map);
6899 return NULL;
6901 if (known)
6902 return map;
6904 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6905 for (i = 1 ; i < map->n; ++i) {
6906 struct isl_map *r2;
6907 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6908 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6909 res = isl_map_union_disjoint(res, r2);
6910 else
6911 res = isl_map_union(res, r2);
6913 isl_map_free(map);
6915 return res;
6918 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6920 return (struct isl_set *)
6921 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6924 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6926 return (struct isl_set *)
6927 isl_map_compute_divs((struct isl_map *)set);
6930 struct isl_set *isl_map_domain(struct isl_map *map)
6932 int i;
6933 struct isl_set *set;
6935 if (!map)
6936 goto error;
6938 map = isl_map_cow(map);
6939 if (!map)
6940 return NULL;
6942 set = (struct isl_set *)map;
6943 set->dim = isl_space_domain(set->dim);
6944 if (!set->dim)
6945 goto error;
6946 for (i = 0; i < map->n; ++i) {
6947 set->p[i] = isl_basic_map_domain(map->p[i]);
6948 if (!set->p[i])
6949 goto error;
6951 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6952 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6953 return set;
6954 error:
6955 isl_map_free(map);
6956 return NULL;
6959 /* Return the union of "map1" and "map2", where we assume for now that
6960 * "map1" and "map2" are disjoint. Note that the basic maps inside
6961 * "map1" or "map2" may not be disjoint from each other.
6962 * Also note that this function is also called from isl_map_union,
6963 * which takes care of handling the situation where "map1" and "map2"
6964 * may not be disjoint.
6966 * If one of the inputs is empty, we can simply return the other input.
6967 * Similarly, if one of the inputs is universal, then it is equal to the union.
6969 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6970 __isl_take isl_map *map2)
6972 int i;
6973 unsigned flags = 0;
6974 struct isl_map *map = NULL;
6975 int is_universe;
6977 if (!map1 || !map2)
6978 goto error;
6980 if (map1->n == 0) {
6981 isl_map_free(map1);
6982 return map2;
6984 if (map2->n == 0) {
6985 isl_map_free(map2);
6986 return map1;
6989 is_universe = isl_map_plain_is_universe(map1);
6990 if (is_universe < 0)
6991 goto error;
6992 if (is_universe) {
6993 isl_map_free(map2);
6994 return map1;
6997 is_universe = isl_map_plain_is_universe(map2);
6998 if (is_universe < 0)
6999 goto error;
7000 if (is_universe) {
7001 isl_map_free(map1);
7002 return map2;
7005 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
7007 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7008 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7009 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7011 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7012 map1->n + map2->n, flags);
7013 if (!map)
7014 goto error;
7015 for (i = 0; i < map1->n; ++i) {
7016 map = isl_map_add_basic_map(map,
7017 isl_basic_map_copy(map1->p[i]));
7018 if (!map)
7019 goto error;
7021 for (i = 0; i < map2->n; ++i) {
7022 map = isl_map_add_basic_map(map,
7023 isl_basic_map_copy(map2->p[i]));
7024 if (!map)
7025 goto error;
7027 isl_map_free(map1);
7028 isl_map_free(map2);
7029 return map;
7030 error:
7031 isl_map_free(map);
7032 isl_map_free(map1);
7033 isl_map_free(map2);
7034 return NULL;
7037 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7038 __isl_take isl_map *map2)
7040 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7043 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
7045 map1 = isl_map_union_disjoint(map1, map2);
7046 if (!map1)
7047 return NULL;
7048 if (map1->n > 1)
7049 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7050 return map1;
7053 struct isl_set *isl_set_union_disjoint(
7054 struct isl_set *set1, struct isl_set *set2)
7056 return (struct isl_set *)
7057 isl_map_union_disjoint(
7058 (struct isl_map *)set1, (struct isl_map *)set2);
7061 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7063 return (struct isl_set *)
7064 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7067 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7068 * the results.
7070 * "map" and "set" are assumed to be compatible and non-NULL.
7072 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7073 __isl_take isl_set *set,
7074 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7075 __isl_take isl_basic_set *bset))
7077 unsigned flags = 0;
7078 struct isl_map *result;
7079 int i, j;
7081 if (isl_set_plain_is_universe(set)) {
7082 isl_set_free(set);
7083 return map;
7086 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7087 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7088 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7090 result = isl_map_alloc_space(isl_space_copy(map->dim),
7091 map->n * set->n, flags);
7092 for (i = 0; result && i < map->n; ++i)
7093 for (j = 0; j < set->n; ++j) {
7094 result = isl_map_add_basic_map(result,
7095 fn(isl_basic_map_copy(map->p[i]),
7096 isl_basic_set_copy(set->p[j])));
7097 if (!result)
7098 break;
7101 isl_map_free(map);
7102 isl_set_free(set);
7103 return result;
7106 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7107 __isl_take isl_set *set)
7109 if (!map || !set)
7110 goto error;
7112 if (!isl_map_compatible_range(map, set))
7113 isl_die(set->ctx, isl_error_invalid,
7114 "incompatible spaces", goto error);
7116 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7117 error:
7118 isl_map_free(map);
7119 isl_set_free(set);
7120 return NULL;
7123 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7124 __isl_take isl_set *set)
7126 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7129 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7130 __isl_take isl_set *set)
7132 if (!map || !set)
7133 goto error;
7135 if (!isl_map_compatible_domain(map, set))
7136 isl_die(set->ctx, isl_error_invalid,
7137 "incompatible spaces", goto error);
7139 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7140 error:
7141 isl_map_free(map);
7142 isl_set_free(set);
7143 return NULL;
7146 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7147 __isl_take isl_set *set)
7149 return isl_map_align_params_map_map_and(map, set,
7150 &map_intersect_domain);
7153 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7154 __isl_take isl_map *map2)
7156 if (!map1 || !map2)
7157 goto error;
7158 map1 = isl_map_reverse(map1);
7159 map1 = isl_map_apply_range(map1, map2);
7160 return isl_map_reverse(map1);
7161 error:
7162 isl_map_free(map1);
7163 isl_map_free(map2);
7164 return NULL;
7167 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7168 __isl_take isl_map *map2)
7170 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7173 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7174 __isl_take isl_map *map2)
7176 isl_space *dim_result;
7177 struct isl_map *result;
7178 int i, j;
7180 if (!map1 || !map2)
7181 goto error;
7183 dim_result = isl_space_join(isl_space_copy(map1->dim),
7184 isl_space_copy(map2->dim));
7186 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7187 if (!result)
7188 goto error;
7189 for (i = 0; i < map1->n; ++i)
7190 for (j = 0; j < map2->n; ++j) {
7191 result = isl_map_add_basic_map(result,
7192 isl_basic_map_apply_range(
7193 isl_basic_map_copy(map1->p[i]),
7194 isl_basic_map_copy(map2->p[j])));
7195 if (!result)
7196 goto error;
7198 isl_map_free(map1);
7199 isl_map_free(map2);
7200 if (result && result->n <= 1)
7201 ISL_F_SET(result, ISL_MAP_DISJOINT);
7202 return result;
7203 error:
7204 isl_map_free(map1);
7205 isl_map_free(map2);
7206 return NULL;
7209 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7210 __isl_take isl_map *map2)
7212 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7216 * returns range - domain
7218 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7220 isl_space *dims, *target_dim;
7221 struct isl_basic_set *bset;
7222 unsigned dim;
7223 unsigned nparam;
7224 int i;
7226 if (!bmap)
7227 goto error;
7228 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7229 bmap->dim, isl_dim_out),
7230 goto error);
7231 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7232 dim = isl_basic_map_n_in(bmap);
7233 nparam = isl_basic_map_n_param(bmap);
7234 bset = isl_basic_set_from_basic_map(bmap);
7235 bset = isl_basic_set_cow(bset);
7236 dims = isl_basic_set_get_space(bset);
7237 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7238 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7239 bset = isl_basic_set_swap_vars(bset, 2*dim);
7240 for (i = 0; i < dim; ++i) {
7241 int j = isl_basic_map_alloc_equality(
7242 (struct isl_basic_map *)bset);
7243 if (j < 0) {
7244 bset = isl_basic_set_free(bset);
7245 break;
7247 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7248 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7249 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7250 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7252 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7253 bset = isl_basic_set_reset_space(bset, target_dim);
7254 return bset;
7255 error:
7256 isl_basic_map_free(bmap);
7257 return NULL;
7261 * returns range - domain
7263 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7265 int i;
7266 isl_space *dim;
7267 struct isl_set *result;
7269 if (!map)
7270 return NULL;
7272 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7273 map->dim, isl_dim_out),
7274 goto error);
7275 dim = isl_map_get_space(map);
7276 dim = isl_space_domain(dim);
7277 result = isl_set_alloc_space(dim, map->n, 0);
7278 if (!result)
7279 goto error;
7280 for (i = 0; i < map->n; ++i)
7281 result = isl_set_add_basic_set(result,
7282 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7283 isl_map_free(map);
7284 return result;
7285 error:
7286 isl_map_free(map);
7287 return NULL;
7291 * returns [domain -> range] -> range - domain
7293 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7294 __isl_take isl_basic_map *bmap)
7296 int i, k;
7297 isl_space *dim;
7298 isl_basic_map *domain;
7299 int nparam, n;
7300 unsigned total;
7302 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7303 isl_die(bmap->ctx, isl_error_invalid,
7304 "domain and range don't match", goto error);
7306 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7307 n = isl_basic_map_dim(bmap, isl_dim_in);
7309 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7310 domain = isl_basic_map_universe(dim);
7312 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7313 bmap = isl_basic_map_apply_range(bmap, domain);
7314 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7316 total = isl_basic_map_total_dim(bmap);
7318 for (i = 0; i < n; ++i) {
7319 k = isl_basic_map_alloc_equality(bmap);
7320 if (k < 0)
7321 goto error;
7322 isl_seq_clr(bmap->eq[k], 1 + total);
7323 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7324 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7325 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7328 bmap = isl_basic_map_gauss(bmap, NULL);
7329 return isl_basic_map_finalize(bmap);
7330 error:
7331 isl_basic_map_free(bmap);
7332 return NULL;
7336 * returns [domain -> range] -> range - domain
7338 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7340 int i;
7341 isl_space *domain_dim;
7343 if (!map)
7344 return NULL;
7346 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7347 isl_die(map->ctx, isl_error_invalid,
7348 "domain and range don't match", goto error);
7350 map = isl_map_cow(map);
7351 if (!map)
7352 return NULL;
7354 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7355 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7356 map->dim = isl_space_join(map->dim, domain_dim);
7357 if (!map->dim)
7358 goto error;
7359 for (i = 0; i < map->n; ++i) {
7360 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7361 if (!map->p[i])
7362 goto error;
7364 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7365 return map;
7366 error:
7367 isl_map_free(map);
7368 return NULL;
7371 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7373 struct isl_basic_map *bmap;
7374 unsigned nparam;
7375 unsigned dim;
7376 int i;
7378 if (!dims)
7379 return NULL;
7381 nparam = dims->nparam;
7382 dim = dims->n_out;
7383 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7384 if (!bmap)
7385 goto error;
7387 for (i = 0; i < dim; ++i) {
7388 int j = isl_basic_map_alloc_equality(bmap);
7389 if (j < 0)
7390 goto error;
7391 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7392 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7393 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7395 return isl_basic_map_finalize(bmap);
7396 error:
7397 isl_basic_map_free(bmap);
7398 return NULL;
7401 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7403 if (!dim)
7404 return NULL;
7405 if (dim->n_in != dim->n_out)
7406 isl_die(dim->ctx, isl_error_invalid,
7407 "number of input and output dimensions needs to be "
7408 "the same", goto error);
7409 return basic_map_identity(dim);
7410 error:
7411 isl_space_free(dim);
7412 return NULL;
7415 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7417 if (!model || !model->dim)
7418 return NULL;
7419 return isl_basic_map_identity(isl_space_copy(model->dim));
7422 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7424 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7427 struct isl_map *isl_map_identity_like(struct isl_map *model)
7429 if (!model || !model->dim)
7430 return NULL;
7431 return isl_map_identity(isl_space_copy(model->dim));
7434 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7436 if (!model || !model->dim)
7437 return NULL;
7438 return isl_map_identity(isl_space_copy(model->dim));
7441 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7443 isl_space *dim = isl_set_get_space(set);
7444 isl_map *id;
7445 id = isl_map_identity(isl_space_map_from_set(dim));
7446 return isl_map_intersect_range(id, set);
7449 /* Construct a basic set with all set dimensions having only non-negative
7450 * values.
7452 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7453 __isl_take isl_space *space)
7455 int i;
7456 unsigned nparam;
7457 unsigned dim;
7458 struct isl_basic_set *bset;
7460 if (!space)
7461 return NULL;
7462 nparam = space->nparam;
7463 dim = space->n_out;
7464 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7465 if (!bset)
7466 return NULL;
7467 for (i = 0; i < dim; ++i) {
7468 int k = isl_basic_set_alloc_inequality(bset);
7469 if (k < 0)
7470 goto error;
7471 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7472 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7474 return bset;
7475 error:
7476 isl_basic_set_free(bset);
7477 return NULL;
7480 /* Construct the half-space x_pos >= 0.
7482 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7483 int pos)
7485 int k;
7486 isl_basic_set *nonneg;
7488 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7489 k = isl_basic_set_alloc_inequality(nonneg);
7490 if (k < 0)
7491 goto error;
7492 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7493 isl_int_set_si(nonneg->ineq[k][pos], 1);
7495 return isl_basic_set_finalize(nonneg);
7496 error:
7497 isl_basic_set_free(nonneg);
7498 return NULL;
7501 /* Construct the half-space x_pos <= -1.
7503 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7505 int k;
7506 isl_basic_set *neg;
7508 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7509 k = isl_basic_set_alloc_inequality(neg);
7510 if (k < 0)
7511 goto error;
7512 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7513 isl_int_set_si(neg->ineq[k][0], -1);
7514 isl_int_set_si(neg->ineq[k][pos], -1);
7516 return isl_basic_set_finalize(neg);
7517 error:
7518 isl_basic_set_free(neg);
7519 return NULL;
7522 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7523 enum isl_dim_type type, unsigned first, unsigned n)
7525 int i;
7526 isl_basic_set *nonneg;
7527 isl_basic_set *neg;
7529 if (!set)
7530 return NULL;
7531 if (n == 0)
7532 return set;
7534 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7536 for (i = 0; i < n; ++i) {
7537 nonneg = nonneg_halfspace(isl_set_get_space(set),
7538 pos(set->dim, type) + first + i);
7539 neg = neg_halfspace(isl_set_get_space(set),
7540 pos(set->dim, type) + first + i);
7542 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7545 return set;
7546 error:
7547 isl_set_free(set);
7548 return NULL;
7551 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7552 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7553 void *user)
7555 isl_set *half;
7557 if (!set)
7558 return -1;
7559 if (isl_set_plain_is_empty(set)) {
7560 isl_set_free(set);
7561 return 0;
7563 if (first == len)
7564 return fn(set, signs, user);
7566 signs[first] = 1;
7567 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7568 1 + first));
7569 half = isl_set_intersect(half, isl_set_copy(set));
7570 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7571 goto error;
7573 signs[first] = -1;
7574 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7575 1 + first));
7576 half = isl_set_intersect(half, set);
7577 return foreach_orthant(half, signs, first + 1, len, fn, user);
7578 error:
7579 isl_set_free(set);
7580 return -1;
7583 /* Call "fn" on the intersections of "set" with each of the orthants
7584 * (except for obviously empty intersections). The orthant is identified
7585 * by the signs array, with each entry having value 1 or -1 according
7586 * to the sign of the corresponding variable.
7588 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7589 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7590 void *user)
7592 unsigned nparam;
7593 unsigned nvar;
7594 int *signs;
7595 int r;
7597 if (!set)
7598 return -1;
7599 if (isl_set_plain_is_empty(set))
7600 return 0;
7602 nparam = isl_set_dim(set, isl_dim_param);
7603 nvar = isl_set_dim(set, isl_dim_set);
7605 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7607 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7608 fn, user);
7610 free(signs);
7612 return r;
7615 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7617 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7620 int isl_basic_map_is_subset(
7621 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7623 int is_subset;
7624 struct isl_map *map1;
7625 struct isl_map *map2;
7627 if (!bmap1 || !bmap2)
7628 return -1;
7630 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7631 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7633 is_subset = isl_map_is_subset(map1, map2);
7635 isl_map_free(map1);
7636 isl_map_free(map2);
7638 return is_subset;
7641 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7642 __isl_keep isl_basic_set *bset2)
7644 return isl_basic_map_is_subset(bset1, bset2);
7647 int isl_basic_map_is_equal(
7648 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7650 int is_subset;
7652 if (!bmap1 || !bmap2)
7653 return -1;
7654 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7655 if (is_subset != 1)
7656 return is_subset;
7657 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7658 return is_subset;
7661 int isl_basic_set_is_equal(
7662 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7664 return isl_basic_map_is_equal(
7665 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7668 int isl_map_is_empty(struct isl_map *map)
7670 int i;
7671 int is_empty;
7673 if (!map)
7674 return -1;
7675 for (i = 0; i < map->n; ++i) {
7676 is_empty = isl_basic_map_is_empty(map->p[i]);
7677 if (is_empty < 0)
7678 return -1;
7679 if (!is_empty)
7680 return 0;
7682 return 1;
7685 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7687 return map ? map->n == 0 : -1;
7690 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7692 return isl_map_plain_is_empty(map);
7695 int isl_set_plain_is_empty(struct isl_set *set)
7697 return set ? set->n == 0 : -1;
7700 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7702 return isl_set_plain_is_empty(set);
7705 int isl_set_is_empty(struct isl_set *set)
7707 return isl_map_is_empty((struct isl_map *)set);
7710 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7712 if (!map1 || !map2)
7713 return -1;
7715 return isl_space_is_equal(map1->dim, map2->dim);
7718 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7720 if (!set1 || !set2)
7721 return -1;
7723 return isl_space_is_equal(set1->dim, set2->dim);
7726 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7728 int is_subset;
7730 if (!map1 || !map2)
7731 return -1;
7732 is_subset = isl_map_is_subset(map1, map2);
7733 if (is_subset != 1)
7734 return is_subset;
7735 is_subset = isl_map_is_subset(map2, map1);
7736 return is_subset;
7739 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7741 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7744 int isl_basic_map_is_strict_subset(
7745 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7747 int is_subset;
7749 if (!bmap1 || !bmap2)
7750 return -1;
7751 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7752 if (is_subset != 1)
7753 return is_subset;
7754 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7755 if (is_subset == -1)
7756 return is_subset;
7757 return !is_subset;
7760 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7762 int is_subset;
7764 if (!map1 || !map2)
7765 return -1;
7766 is_subset = isl_map_is_subset(map1, map2);
7767 if (is_subset != 1)
7768 return is_subset;
7769 is_subset = isl_map_is_subset(map2, map1);
7770 if (is_subset == -1)
7771 return is_subset;
7772 return !is_subset;
7775 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7777 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7780 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7782 if (!bmap)
7783 return -1;
7784 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7787 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7789 if (!bset)
7790 return -1;
7791 return bset->n_eq == 0 && bset->n_ineq == 0;
7794 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7796 int i;
7798 if (!map)
7799 return -1;
7801 for (i = 0; i < map->n; ++i) {
7802 int r = isl_basic_map_is_universe(map->p[i]);
7803 if (r < 0 || r)
7804 return r;
7807 return 0;
7810 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7812 return isl_map_plain_is_universe((isl_map *) set);
7815 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7817 return isl_set_plain_is_universe(set);
7820 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7822 struct isl_basic_set *bset = NULL;
7823 struct isl_vec *sample = NULL;
7824 int empty;
7825 unsigned total;
7827 if (!bmap)
7828 return -1;
7830 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7831 return 1;
7833 if (isl_basic_map_is_universe(bmap))
7834 return 0;
7836 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7837 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7838 copy = isl_basic_map_remove_redundancies(copy);
7839 empty = isl_basic_map_plain_is_empty(copy);
7840 isl_basic_map_free(copy);
7841 return empty;
7844 total = 1 + isl_basic_map_total_dim(bmap);
7845 if (bmap->sample && bmap->sample->size == total) {
7846 int contains = isl_basic_map_contains(bmap, bmap->sample);
7847 if (contains < 0)
7848 return -1;
7849 if (contains)
7850 return 0;
7852 isl_vec_free(bmap->sample);
7853 bmap->sample = NULL;
7854 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7855 if (!bset)
7856 return -1;
7857 sample = isl_basic_set_sample_vec(bset);
7858 if (!sample)
7859 return -1;
7860 empty = sample->size == 0;
7861 isl_vec_free(bmap->sample);
7862 bmap->sample = sample;
7863 if (empty)
7864 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7866 return empty;
7869 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7871 if (!bmap)
7872 return -1;
7873 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7876 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7878 return isl_basic_map_plain_is_empty(bmap);
7881 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7883 if (!bset)
7884 return -1;
7885 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7888 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7890 return isl_basic_set_plain_is_empty(bset);
7893 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7895 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7898 struct isl_map *isl_basic_map_union(
7899 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7901 struct isl_map *map;
7902 if (!bmap1 || !bmap2)
7903 goto error;
7905 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7907 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7908 if (!map)
7909 goto error;
7910 map = isl_map_add_basic_map(map, bmap1);
7911 map = isl_map_add_basic_map(map, bmap2);
7912 return map;
7913 error:
7914 isl_basic_map_free(bmap1);
7915 isl_basic_map_free(bmap2);
7916 return NULL;
7919 struct isl_set *isl_basic_set_union(
7920 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7922 return (struct isl_set *)isl_basic_map_union(
7923 (struct isl_basic_map *)bset1,
7924 (struct isl_basic_map *)bset2);
7927 /* Order divs such that any div only depends on previous divs */
7928 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7930 int i;
7931 unsigned off;
7933 if (!bmap)
7934 return NULL;
7936 off = isl_space_dim(bmap->dim, isl_dim_all);
7938 for (i = 0; i < bmap->n_div; ++i) {
7939 int pos;
7940 if (isl_int_is_zero(bmap->div[i][0]))
7941 continue;
7942 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7943 bmap->n_div-i);
7944 if (pos == -1)
7945 continue;
7946 isl_basic_map_swap_div(bmap, i, i + pos);
7947 --i;
7949 return bmap;
7952 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7954 return (struct isl_basic_set *)
7955 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7958 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7960 int i;
7962 if (!map)
7963 return 0;
7965 for (i = 0; i < map->n; ++i) {
7966 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7967 if (!map->p[i])
7968 goto error;
7971 return map;
7972 error:
7973 isl_map_free(map);
7974 return NULL;
7977 /* Apply the expansion computed by isl_merge_divs.
7978 * The expansion itself is given by "exp" while the resulting
7979 * list of divs is given by "div".
7981 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7982 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7984 int i, j;
7985 int n_div;
7987 bset = isl_basic_set_cow(bset);
7988 if (!bset || !div)
7989 goto error;
7991 if (div->n_row < bset->n_div)
7992 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7993 "not an expansion", goto error);
7995 n_div = bset->n_div;
7996 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7997 div->n_row - n_div, 0,
7998 2 * (div->n_row - n_div));
8000 for (i = n_div; i < div->n_row; ++i)
8001 if (isl_basic_set_alloc_div(bset) < 0)
8002 goto error;
8004 j = n_div - 1;
8005 for (i = div->n_row - 1; i >= 0; --i) {
8006 if (j >= 0 && exp[j] == i) {
8007 if (i != j)
8008 isl_basic_map_swap_div(bset, i, j);
8009 j--;
8010 } else {
8011 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8012 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8013 goto error;
8017 isl_mat_free(div);
8018 return bset;
8019 error:
8020 isl_basic_set_free(bset);
8021 isl_mat_free(div);
8022 return NULL;
8025 /* Look for a div in dst that corresponds to the div "div" in src.
8026 * The divs before "div" in src and dst are assumed to be the same.
8028 * Returns -1 if no corresponding div was found and the position
8029 * of the corresponding div in dst otherwise.
8031 static int find_div(struct isl_basic_map *dst,
8032 struct isl_basic_map *src, unsigned div)
8034 int i;
8036 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8038 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8039 for (i = div; i < dst->n_div; ++i)
8040 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8041 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8042 dst->n_div - div) == -1)
8043 return i;
8044 return -1;
8047 struct isl_basic_map *isl_basic_map_align_divs(
8048 struct isl_basic_map *dst, struct isl_basic_map *src)
8050 int i;
8051 unsigned total;
8053 if (!dst || !src)
8054 goto error;
8056 if (src->n_div == 0)
8057 return dst;
8059 for (i = 0; i < src->n_div; ++i)
8060 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
8062 src = isl_basic_map_order_divs(src);
8063 dst = isl_basic_map_cow(dst);
8064 if (!dst)
8065 return NULL;
8066 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
8067 src->n_div, 0, 2 * src->n_div);
8068 if (!dst)
8069 return NULL;
8070 total = isl_space_dim(src->dim, isl_dim_all);
8071 for (i = 0; i < src->n_div; ++i) {
8072 int j = find_div(dst, src, i);
8073 if (j < 0) {
8074 j = isl_basic_map_alloc_div(dst);
8075 if (j < 0)
8076 goto error;
8077 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8078 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8079 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8080 goto error;
8082 if (j != i)
8083 isl_basic_map_swap_div(dst, i, j);
8085 return dst;
8086 error:
8087 isl_basic_map_free(dst);
8088 return NULL;
8091 struct isl_basic_set *isl_basic_set_align_divs(
8092 struct isl_basic_set *dst, struct isl_basic_set *src)
8094 return (struct isl_basic_set *)isl_basic_map_align_divs(
8095 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8098 struct isl_map *isl_map_align_divs(struct isl_map *map)
8100 int i;
8102 if (!map)
8103 return NULL;
8104 if (map->n == 0)
8105 return map;
8106 map = isl_map_compute_divs(map);
8107 map = isl_map_cow(map);
8108 if (!map)
8109 return NULL;
8111 for (i = 1; i < map->n; ++i)
8112 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8113 for (i = 1; i < map->n; ++i) {
8114 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8115 if (!map->p[i])
8116 return isl_map_free(map);
8119 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8120 return map;
8123 struct isl_set *isl_set_align_divs(struct isl_set *set)
8125 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8128 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8129 __isl_take isl_map *map)
8131 if (!set || !map)
8132 goto error;
8133 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8134 map = isl_map_intersect_domain(map, set);
8135 set = isl_map_range(map);
8136 return set;
8137 error:
8138 isl_set_free(set);
8139 isl_map_free(map);
8140 return NULL;
8143 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8144 __isl_take isl_map *map)
8146 return isl_map_align_params_map_map_and(set, map, &set_apply);
8149 /* There is no need to cow as removing empty parts doesn't change
8150 * the meaning of the set.
8152 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8154 int i;
8156 if (!map)
8157 return NULL;
8159 for (i = map->n - 1; i >= 0; --i)
8160 remove_if_empty(map, i);
8162 return map;
8165 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8167 return (struct isl_set *)
8168 isl_map_remove_empty_parts((struct isl_map *)set);
8171 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8173 struct isl_basic_map *bmap;
8174 if (!map || map->n == 0)
8175 return NULL;
8176 bmap = map->p[map->n-1];
8177 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8178 return isl_basic_map_copy(bmap);
8181 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8183 return (struct isl_basic_set *)
8184 isl_map_copy_basic_map((struct isl_map *)set);
8187 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8188 __isl_keep isl_basic_map *bmap)
8190 int i;
8192 if (!map || !bmap)
8193 goto error;
8194 for (i = map->n-1; i >= 0; --i) {
8195 if (map->p[i] != bmap)
8196 continue;
8197 map = isl_map_cow(map);
8198 if (!map)
8199 goto error;
8200 isl_basic_map_free(map->p[i]);
8201 if (i != map->n-1) {
8202 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8203 map->p[i] = map->p[map->n-1];
8205 map->n--;
8206 return map;
8208 return map;
8209 error:
8210 isl_map_free(map);
8211 return NULL;
8214 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8215 struct isl_basic_set *bset)
8217 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8218 (struct isl_basic_map *)bset);
8221 /* Given two basic sets bset1 and bset2, compute the maximal difference
8222 * between the values of dimension pos in bset1 and those in bset2
8223 * for any common value of the parameters and dimensions preceding pos.
8225 static enum isl_lp_result basic_set_maximal_difference_at(
8226 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8227 int pos, isl_int *opt)
8229 isl_space *dims;
8230 struct isl_basic_map *bmap1 = NULL;
8231 struct isl_basic_map *bmap2 = NULL;
8232 struct isl_ctx *ctx;
8233 struct isl_vec *obj;
8234 unsigned total;
8235 unsigned nparam;
8236 unsigned dim1, dim2;
8237 enum isl_lp_result res;
8239 if (!bset1 || !bset2)
8240 return isl_lp_error;
8242 nparam = isl_basic_set_n_param(bset1);
8243 dim1 = isl_basic_set_n_dim(bset1);
8244 dim2 = isl_basic_set_n_dim(bset2);
8245 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8246 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8247 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8248 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8249 if (!bmap1 || !bmap2)
8250 goto error;
8251 bmap1 = isl_basic_map_cow(bmap1);
8252 bmap1 = isl_basic_map_extend(bmap1, nparam,
8253 pos, (dim1 - pos) + (dim2 - pos),
8254 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8255 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8256 if (!bmap1)
8257 goto error;
8258 total = isl_basic_map_total_dim(bmap1);
8259 ctx = bmap1->ctx;
8260 obj = isl_vec_alloc(ctx, 1 + total);
8261 if (!obj)
8262 goto error2;
8263 isl_seq_clr(obj->block.data, 1 + total);
8264 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8265 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8266 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8267 opt, NULL, NULL);
8268 isl_basic_map_free(bmap1);
8269 isl_vec_free(obj);
8270 return res;
8271 error:
8272 isl_basic_map_free(bmap2);
8273 error2:
8274 isl_basic_map_free(bmap1);
8275 return isl_lp_error;
8278 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8279 * for any common value of the parameters and dimensions preceding pos
8280 * in both basic sets, the values of dimension pos in bset1 are
8281 * smaller or larger than those in bset2.
8283 * Returns
8284 * 1 if bset1 follows bset2
8285 * -1 if bset1 precedes bset2
8286 * 0 if bset1 and bset2 are incomparable
8287 * -2 if some error occurred.
8289 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8290 struct isl_basic_set *bset2, int pos)
8292 isl_int opt;
8293 enum isl_lp_result res;
8294 int cmp;
8296 isl_int_init(opt);
8298 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8300 if (res == isl_lp_empty)
8301 cmp = 0;
8302 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8303 res == isl_lp_unbounded)
8304 cmp = 1;
8305 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8306 cmp = -1;
8307 else
8308 cmp = -2;
8310 isl_int_clear(opt);
8311 return cmp;
8314 /* Given two basic sets bset1 and bset2, check whether
8315 * for any common value of the parameters and dimensions preceding pos
8316 * there is a value of dimension pos in bset1 that is larger
8317 * than a value of the same dimension in bset2.
8319 * Return
8320 * 1 if there exists such a pair
8321 * 0 if there is no such pair, but there is a pair of equal values
8322 * -1 otherwise
8323 * -2 if some error occurred.
8325 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8326 __isl_keep isl_basic_set *bset2, int pos)
8328 isl_int opt;
8329 enum isl_lp_result res;
8330 int cmp;
8332 isl_int_init(opt);
8334 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8336 if (res == isl_lp_empty)
8337 cmp = -1;
8338 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8339 res == isl_lp_unbounded)
8340 cmp = 1;
8341 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8342 cmp = -1;
8343 else if (res == isl_lp_ok)
8344 cmp = 0;
8345 else
8346 cmp = -2;
8348 isl_int_clear(opt);
8349 return cmp;
8352 /* Given two sets set1 and set2, check whether
8353 * for any common value of the parameters and dimensions preceding pos
8354 * there is a value of dimension pos in set1 that is larger
8355 * than a value of the same dimension in set2.
8357 * Return
8358 * 1 if there exists such a pair
8359 * 0 if there is no such pair, but there is a pair of equal values
8360 * -1 otherwise
8361 * -2 if some error occurred.
8363 int isl_set_follows_at(__isl_keep isl_set *set1,
8364 __isl_keep isl_set *set2, int pos)
8366 int i, j;
8367 int follows = -1;
8369 if (!set1 || !set2)
8370 return -2;
8372 for (i = 0; i < set1->n; ++i)
8373 for (j = 0; j < set2->n; ++j) {
8374 int f;
8375 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8376 if (f == 1 || f == -2)
8377 return f;
8378 if (f > follows)
8379 follows = f;
8382 return follows;
8385 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8386 unsigned pos, isl_int *val)
8388 int i;
8389 int d;
8390 unsigned total;
8392 if (!bmap)
8393 return -1;
8394 total = isl_basic_map_total_dim(bmap);
8395 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8396 for (; d+1 > pos; --d)
8397 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8398 break;
8399 if (d != pos)
8400 continue;
8401 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8402 return 0;
8403 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8404 return 0;
8405 if (!isl_int_is_one(bmap->eq[i][1+d]))
8406 return 0;
8407 if (val)
8408 isl_int_neg(*val, bmap->eq[i][0]);
8409 return 1;
8411 return 0;
8414 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8415 unsigned pos, isl_int *val)
8417 int i;
8418 isl_int v;
8419 isl_int tmp;
8420 int fixed;
8422 if (!map)
8423 return -1;
8424 if (map->n == 0)
8425 return 0;
8426 if (map->n == 1)
8427 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8428 isl_int_init(v);
8429 isl_int_init(tmp);
8430 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8431 for (i = 1; fixed == 1 && i < map->n; ++i) {
8432 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8433 if (fixed == 1 && isl_int_ne(tmp, v))
8434 fixed = 0;
8436 if (val)
8437 isl_int_set(*val, v);
8438 isl_int_clear(tmp);
8439 isl_int_clear(v);
8440 return fixed;
8443 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8444 unsigned pos, isl_int *val)
8446 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8447 pos, val);
8450 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8451 isl_int *val)
8453 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8456 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8457 enum isl_dim_type type, unsigned pos, isl_int *val)
8459 if (pos >= isl_basic_map_dim(bmap, type))
8460 return -1;
8461 return isl_basic_map_plain_has_fixed_var(bmap,
8462 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8465 /* If "bmap" obviously lies on a hyperplane where the given dimension
8466 * has a fixed value, then return that value.
8467 * Otherwise return NaN.
8469 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8470 __isl_keep isl_basic_map *bmap,
8471 enum isl_dim_type type, unsigned pos)
8473 isl_ctx *ctx;
8474 isl_val *v;
8475 int fixed;
8477 if (!bmap)
8478 return NULL;
8479 ctx = isl_basic_map_get_ctx(bmap);
8480 v = isl_val_alloc(ctx);
8481 if (!v)
8482 return NULL;
8483 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8484 if (fixed < 0)
8485 return isl_val_free(v);
8486 if (fixed) {
8487 isl_int_set_si(v->d, 1);
8488 return v;
8490 isl_val_free(v);
8491 return isl_val_nan(ctx);
8494 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8495 enum isl_dim_type type, unsigned pos, isl_int *val)
8497 if (pos >= isl_map_dim(map, type))
8498 return -1;
8499 return isl_map_plain_has_fixed_var(map,
8500 map_offset(map, type) - 1 + pos, val);
8503 /* If "map" obviously lies on a hyperplane where the given dimension
8504 * has a fixed value, then return that value.
8505 * Otherwise return NaN.
8507 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8508 enum isl_dim_type type, unsigned pos)
8510 isl_ctx *ctx;
8511 isl_val *v;
8512 int fixed;
8514 if (!map)
8515 return NULL;
8516 ctx = isl_map_get_ctx(map);
8517 v = isl_val_alloc(ctx);
8518 if (!v)
8519 return NULL;
8520 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8521 if (fixed < 0)
8522 return isl_val_free(v);
8523 if (fixed) {
8524 isl_int_set_si(v->d, 1);
8525 return v;
8527 isl_val_free(v);
8528 return isl_val_nan(ctx);
8531 /* If "set" obviously lies on a hyperplane where the given dimension
8532 * has a fixed value, then return that value.
8533 * Otherwise return NaN.
8535 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8536 enum isl_dim_type type, unsigned pos)
8538 return isl_map_plain_get_val_if_fixed(set, type, pos);
8541 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8542 enum isl_dim_type type, unsigned pos, isl_int *val)
8544 return isl_map_plain_is_fixed(set, type, pos, val);
8547 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8548 enum isl_dim_type type, unsigned pos, isl_int *val)
8550 return isl_map_plain_is_fixed(map, type, pos, val);
8553 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8554 * then return this fixed value in *val.
8556 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8557 unsigned dim, isl_int *val)
8559 return isl_basic_set_plain_has_fixed_var(bset,
8560 isl_basic_set_n_param(bset) + dim, val);
8563 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8564 * then return this fixed value in *val.
8566 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8567 unsigned dim, isl_int *val)
8569 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8572 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8573 unsigned dim, isl_int *val)
8575 return isl_set_plain_dim_is_fixed(set, dim, val);
8578 /* Check if input variable in has fixed value and if so and if val is not NULL,
8579 * then return this fixed value in *val.
8581 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8582 unsigned in, isl_int *val)
8584 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8587 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8588 * and if val is not NULL, then return this lower bound in *val.
8590 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8591 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8593 int i, i_eq = -1, i_ineq = -1;
8594 isl_int *c;
8595 unsigned total;
8596 unsigned nparam;
8598 if (!bset)
8599 return -1;
8600 total = isl_basic_set_total_dim(bset);
8601 nparam = isl_basic_set_n_param(bset);
8602 for (i = 0; i < bset->n_eq; ++i) {
8603 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8604 continue;
8605 if (i_eq != -1)
8606 return 0;
8607 i_eq = i;
8609 for (i = 0; i < bset->n_ineq; ++i) {
8610 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8611 continue;
8612 if (i_eq != -1 || i_ineq != -1)
8613 return 0;
8614 i_ineq = i;
8616 if (i_eq == -1 && i_ineq == -1)
8617 return 0;
8618 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8619 /* The coefficient should always be one due to normalization. */
8620 if (!isl_int_is_one(c[1+nparam+dim]))
8621 return 0;
8622 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8623 return 0;
8624 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8625 total - nparam - dim - 1) != -1)
8626 return 0;
8627 if (val)
8628 isl_int_neg(*val, c[0]);
8629 return 1;
8632 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8633 unsigned dim, isl_int *val)
8635 int i;
8636 isl_int v;
8637 isl_int tmp;
8638 int fixed;
8640 if (!set)
8641 return -1;
8642 if (set->n == 0)
8643 return 0;
8644 if (set->n == 1)
8645 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8646 dim, val);
8647 isl_int_init(v);
8648 isl_int_init(tmp);
8649 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8650 dim, &v);
8651 for (i = 1; fixed == 1 && i < set->n; ++i) {
8652 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8653 dim, &tmp);
8654 if (fixed == 1 && isl_int_ne(tmp, v))
8655 fixed = 0;
8657 if (val)
8658 isl_int_set(*val, v);
8659 isl_int_clear(tmp);
8660 isl_int_clear(v);
8661 return fixed;
8664 struct constraint {
8665 unsigned size;
8666 isl_int *c;
8669 /* uset_gist depends on constraints without existentially quantified
8670 * variables sorting first.
8672 static int qsort_constraint_cmp(const void *p1, const void *p2)
8674 const struct constraint *c1 = (const struct constraint *)p1;
8675 const struct constraint *c2 = (const struct constraint *)p2;
8676 int l1, l2;
8677 unsigned size = isl_min(c1->size, c2->size);
8679 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8680 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8682 if (l1 != l2)
8683 return l1 - l2;
8685 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8688 static struct isl_basic_map *isl_basic_map_sort_constraints(
8689 struct isl_basic_map *bmap)
8691 int i;
8692 struct constraint *c;
8693 unsigned total;
8695 if (!bmap)
8696 return NULL;
8697 total = isl_basic_map_total_dim(bmap);
8698 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8699 if (!c)
8700 goto error;
8701 for (i = 0; i < bmap->n_ineq; ++i) {
8702 c[i].size = total;
8703 c[i].c = bmap->ineq[i];
8705 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8706 for (i = 0; i < bmap->n_ineq; ++i)
8707 bmap->ineq[i] = c[i].c;
8708 free(c);
8709 return bmap;
8710 error:
8711 isl_basic_map_free(bmap);
8712 return NULL;
8715 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8716 __isl_take isl_basic_set *bset)
8718 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8719 (struct isl_basic_map *)bset);
8722 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8724 if (!bmap)
8725 return NULL;
8726 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8727 return bmap;
8728 bmap = isl_basic_map_remove_redundancies(bmap);
8729 bmap = isl_basic_map_sort_constraints(bmap);
8730 if (bmap)
8731 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8732 return bmap;
8735 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8737 return (struct isl_basic_set *)isl_basic_map_normalize(
8738 (struct isl_basic_map *)bset);
8741 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8742 const __isl_keep isl_basic_map *bmap2)
8744 int i, cmp;
8745 unsigned total;
8747 if (bmap1 == bmap2)
8748 return 0;
8749 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8750 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8751 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8752 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8753 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8754 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8755 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8756 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8757 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8758 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8759 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8760 return 0;
8761 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8762 return 1;
8763 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8764 return -1;
8765 if (bmap1->n_eq != bmap2->n_eq)
8766 return bmap1->n_eq - bmap2->n_eq;
8767 if (bmap1->n_ineq != bmap2->n_ineq)
8768 return bmap1->n_ineq - bmap2->n_ineq;
8769 if (bmap1->n_div != bmap2->n_div)
8770 return bmap1->n_div - bmap2->n_div;
8771 total = isl_basic_map_total_dim(bmap1);
8772 for (i = 0; i < bmap1->n_eq; ++i) {
8773 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8774 if (cmp)
8775 return cmp;
8777 for (i = 0; i < bmap1->n_ineq; ++i) {
8778 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8779 if (cmp)
8780 return cmp;
8782 for (i = 0; i < bmap1->n_div; ++i) {
8783 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8784 if (cmp)
8785 return cmp;
8787 return 0;
8790 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8791 const __isl_keep isl_basic_set *bset2)
8793 return isl_basic_map_plain_cmp(bset1, bset2);
8796 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8798 int i, cmp;
8800 if (set1 == set2)
8801 return 0;
8802 if (set1->n != set2->n)
8803 return set1->n - set2->n;
8805 for (i = 0; i < set1->n; ++i) {
8806 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8807 if (cmp)
8808 return cmp;
8811 return 0;
8814 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8815 __isl_keep isl_basic_map *bmap2)
8817 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8820 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8821 __isl_keep isl_basic_set *bset2)
8823 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8824 (isl_basic_map *)bset2);
8827 static int qsort_bmap_cmp(const void *p1, const void *p2)
8829 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8830 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8832 return isl_basic_map_plain_cmp(bmap1, bmap2);
8835 /* We normalize in place, but if anything goes wrong we need
8836 * to return NULL, so we need to make sure we don't change the
8837 * meaning of any possible other copies of map.
8839 struct isl_map *isl_map_normalize(struct isl_map *map)
8841 int i, j;
8842 struct isl_basic_map *bmap;
8844 if (!map)
8845 return NULL;
8846 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8847 return map;
8848 for (i = 0; i < map->n; ++i) {
8849 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8850 if (!bmap)
8851 goto error;
8852 isl_basic_map_free(map->p[i]);
8853 map->p[i] = bmap;
8855 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8856 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8857 map = isl_map_remove_empty_parts(map);
8858 if (!map)
8859 return NULL;
8860 for (i = map->n - 1; i >= 1; --i) {
8861 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8862 continue;
8863 isl_basic_map_free(map->p[i-1]);
8864 for (j = i; j < map->n; ++j)
8865 map->p[j-1] = map->p[j];
8866 map->n--;
8868 return map;
8869 error:
8870 isl_map_free(map);
8871 return NULL;
8875 struct isl_set *isl_set_normalize(struct isl_set *set)
8877 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8880 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8882 int i;
8883 int equal;
8885 if (!map1 || !map2)
8886 return -1;
8888 if (map1 == map2)
8889 return 1;
8890 if (!isl_space_is_equal(map1->dim, map2->dim))
8891 return 0;
8893 map1 = isl_map_copy(map1);
8894 map2 = isl_map_copy(map2);
8895 map1 = isl_map_normalize(map1);
8896 map2 = isl_map_normalize(map2);
8897 if (!map1 || !map2)
8898 goto error;
8899 equal = map1->n == map2->n;
8900 for (i = 0; equal && i < map1->n; ++i) {
8901 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8902 if (equal < 0)
8903 goto error;
8905 isl_map_free(map1);
8906 isl_map_free(map2);
8907 return equal;
8908 error:
8909 isl_map_free(map1);
8910 isl_map_free(map2);
8911 return -1;
8914 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8916 return isl_map_plain_is_equal(map1, map2);
8919 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8921 return isl_map_plain_is_equal((struct isl_map *)set1,
8922 (struct isl_map *)set2);
8925 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8927 return isl_set_plain_is_equal(set1, set2);
8930 /* Return an interval that ranges from min to max (inclusive)
8932 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8933 isl_int min, isl_int max)
8935 int k;
8936 struct isl_basic_set *bset = NULL;
8938 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8939 if (!bset)
8940 goto error;
8942 k = isl_basic_set_alloc_inequality(bset);
8943 if (k < 0)
8944 goto error;
8945 isl_int_set_si(bset->ineq[k][1], 1);
8946 isl_int_neg(bset->ineq[k][0], min);
8948 k = isl_basic_set_alloc_inequality(bset);
8949 if (k < 0)
8950 goto error;
8951 isl_int_set_si(bset->ineq[k][1], -1);
8952 isl_int_set(bset->ineq[k][0], max);
8954 return bset;
8955 error:
8956 isl_basic_set_free(bset);
8957 return NULL;
8960 /* Return the Cartesian product of the basic sets in list (in the given order).
8962 __isl_give isl_basic_set *isl_basic_set_list_product(
8963 __isl_take struct isl_basic_set_list *list)
8965 int i;
8966 unsigned dim;
8967 unsigned nparam;
8968 unsigned extra;
8969 unsigned n_eq;
8970 unsigned n_ineq;
8971 struct isl_basic_set *product = NULL;
8973 if (!list)
8974 goto error;
8975 isl_assert(list->ctx, list->n > 0, goto error);
8976 isl_assert(list->ctx, list->p[0], goto error);
8977 nparam = isl_basic_set_n_param(list->p[0]);
8978 dim = isl_basic_set_n_dim(list->p[0]);
8979 extra = list->p[0]->n_div;
8980 n_eq = list->p[0]->n_eq;
8981 n_ineq = list->p[0]->n_ineq;
8982 for (i = 1; i < list->n; ++i) {
8983 isl_assert(list->ctx, list->p[i], goto error);
8984 isl_assert(list->ctx,
8985 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8986 dim += isl_basic_set_n_dim(list->p[i]);
8987 extra += list->p[i]->n_div;
8988 n_eq += list->p[i]->n_eq;
8989 n_ineq += list->p[i]->n_ineq;
8991 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8992 n_eq, n_ineq);
8993 if (!product)
8994 goto error;
8995 dim = 0;
8996 for (i = 0; i < list->n; ++i) {
8997 isl_basic_set_add_constraints(product,
8998 isl_basic_set_copy(list->p[i]), dim);
8999 dim += isl_basic_set_n_dim(list->p[i]);
9001 isl_basic_set_list_free(list);
9002 return product;
9003 error:
9004 isl_basic_set_free(product);
9005 isl_basic_set_list_free(list);
9006 return NULL;
9009 struct isl_basic_map *isl_basic_map_product(
9010 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9012 isl_space *dim_result = NULL;
9013 struct isl_basic_map *bmap;
9014 unsigned in1, in2, out1, out2, nparam, total, pos;
9015 struct isl_dim_map *dim_map1, *dim_map2;
9017 if (!bmap1 || !bmap2)
9018 goto error;
9020 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9021 bmap2->dim, isl_dim_param), goto error);
9022 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9023 isl_space_copy(bmap2->dim));
9025 in1 = isl_basic_map_n_in(bmap1);
9026 in2 = isl_basic_map_n_in(bmap2);
9027 out1 = isl_basic_map_n_out(bmap1);
9028 out2 = isl_basic_map_n_out(bmap2);
9029 nparam = isl_basic_map_n_param(bmap1);
9031 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9032 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9033 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9034 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9035 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9036 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9037 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9038 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9039 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9040 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9041 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9043 bmap = isl_basic_map_alloc_space(dim_result,
9044 bmap1->n_div + bmap2->n_div,
9045 bmap1->n_eq + bmap2->n_eq,
9046 bmap1->n_ineq + bmap2->n_ineq);
9047 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9048 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9049 bmap = isl_basic_map_simplify(bmap);
9050 return isl_basic_map_finalize(bmap);
9051 error:
9052 isl_basic_map_free(bmap1);
9053 isl_basic_map_free(bmap2);
9054 return NULL;
9057 __isl_give isl_basic_map *isl_basic_map_flat_product(
9058 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9060 isl_basic_map *prod;
9062 prod = isl_basic_map_product(bmap1, bmap2);
9063 prod = isl_basic_map_flatten(prod);
9064 return prod;
9067 __isl_give isl_basic_set *isl_basic_set_flat_product(
9068 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9070 return isl_basic_map_flat_range_product(bset1, bset2);
9073 __isl_give isl_basic_map *isl_basic_map_domain_product(
9074 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9076 isl_space *space_result = NULL;
9077 isl_basic_map *bmap;
9078 unsigned in1, in2, out, nparam, total, pos;
9079 struct isl_dim_map *dim_map1, *dim_map2;
9081 if (!bmap1 || !bmap2)
9082 goto error;
9084 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9085 isl_space_copy(bmap2->dim));
9087 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9088 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9089 out = isl_basic_map_dim(bmap1, isl_dim_out);
9090 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9092 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9093 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9094 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9095 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9096 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9097 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9098 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9099 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9100 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9101 isl_dim_map_div(dim_map1, bmap1, pos += out);
9102 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9104 bmap = isl_basic_map_alloc_space(space_result,
9105 bmap1->n_div + bmap2->n_div,
9106 bmap1->n_eq + bmap2->n_eq,
9107 bmap1->n_ineq + bmap2->n_ineq);
9108 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9109 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9110 bmap = isl_basic_map_simplify(bmap);
9111 return isl_basic_map_finalize(bmap);
9112 error:
9113 isl_basic_map_free(bmap1);
9114 isl_basic_map_free(bmap2);
9115 return NULL;
9118 __isl_give isl_basic_map *isl_basic_map_range_product(
9119 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9121 isl_space *dim_result = NULL;
9122 isl_basic_map *bmap;
9123 unsigned in, out1, out2, nparam, total, pos;
9124 struct isl_dim_map *dim_map1, *dim_map2;
9126 if (!bmap1 || !bmap2)
9127 goto error;
9129 if (!isl_space_match(bmap1->dim, isl_dim_param,
9130 bmap2->dim, isl_dim_param))
9131 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9132 "parameters don't match", goto error);
9134 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9135 isl_space_copy(bmap2->dim));
9137 in = isl_basic_map_dim(bmap1, isl_dim_in);
9138 out1 = isl_basic_map_n_out(bmap1);
9139 out2 = isl_basic_map_n_out(bmap2);
9140 nparam = isl_basic_map_n_param(bmap1);
9142 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9143 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9144 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9145 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9146 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9147 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9148 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9149 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9150 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9151 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9152 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9154 bmap = isl_basic_map_alloc_space(dim_result,
9155 bmap1->n_div + bmap2->n_div,
9156 bmap1->n_eq + bmap2->n_eq,
9157 bmap1->n_ineq + bmap2->n_ineq);
9158 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9159 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9160 bmap = isl_basic_map_simplify(bmap);
9161 return isl_basic_map_finalize(bmap);
9162 error:
9163 isl_basic_map_free(bmap1);
9164 isl_basic_map_free(bmap2);
9165 return NULL;
9168 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9169 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9171 isl_basic_map *prod;
9173 prod = isl_basic_map_range_product(bmap1, bmap2);
9174 prod = isl_basic_map_flatten_range(prod);
9175 return prod;
9178 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9179 __isl_take isl_map *map2,
9180 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9181 __isl_take isl_space *right),
9182 __isl_give isl_basic_map *(*basic_map_product)(
9183 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9185 unsigned flags = 0;
9186 struct isl_map *result;
9187 int i, j;
9189 if (!map1 || !map2)
9190 goto error;
9192 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9193 map2->dim, isl_dim_param), goto error);
9195 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9196 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9197 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9199 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9200 isl_space_copy(map2->dim)),
9201 map1->n * map2->n, flags);
9202 if (!result)
9203 goto error;
9204 for (i = 0; i < map1->n; ++i)
9205 for (j = 0; j < map2->n; ++j) {
9206 struct isl_basic_map *part;
9207 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9208 isl_basic_map_copy(map2->p[j]));
9209 if (isl_basic_map_is_empty(part))
9210 isl_basic_map_free(part);
9211 else
9212 result = isl_map_add_basic_map(result, part);
9213 if (!result)
9214 goto error;
9216 isl_map_free(map1);
9217 isl_map_free(map2);
9218 return result;
9219 error:
9220 isl_map_free(map1);
9221 isl_map_free(map2);
9222 return NULL;
9225 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9227 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9228 __isl_take isl_map *map2)
9230 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9233 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9234 __isl_take isl_map *map2)
9236 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9239 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9241 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9242 __isl_take isl_map *map2)
9244 isl_map *prod;
9246 prod = isl_map_product(map1, map2);
9247 prod = isl_map_flatten(prod);
9248 return prod;
9251 /* Given two set A and B, construct its Cartesian product A x B.
9253 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9255 return isl_map_range_product(set1, set2);
9258 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9259 __isl_take isl_set *set2)
9261 return isl_map_flat_range_product(set1, set2);
9264 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9266 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9267 __isl_take isl_map *map2)
9269 return map_product(map1, map2, &isl_space_domain_product,
9270 &isl_basic_map_domain_product);
9273 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9275 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9276 __isl_take isl_map *map2)
9278 return map_product(map1, map2, &isl_space_range_product,
9279 &isl_basic_map_range_product);
9282 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9283 __isl_take isl_map *map2)
9285 return isl_map_align_params_map_map_and(map1, map2,
9286 &map_domain_product_aligned);
9289 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9290 __isl_take isl_map *map2)
9292 return isl_map_align_params_map_map_and(map1, map2,
9293 &map_range_product_aligned);
9296 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9298 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9299 __isl_take isl_map *map2)
9301 isl_map *prod;
9303 prod = isl_map_domain_product(map1, map2);
9304 prod = isl_map_flatten_domain(prod);
9305 return prod;
9308 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9310 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9311 __isl_take isl_map *map2)
9313 isl_map *prod;
9315 prod = isl_map_range_product(map1, map2);
9316 prod = isl_map_flatten_range(prod);
9317 return prod;
9320 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9322 int i;
9323 uint32_t hash = isl_hash_init();
9324 unsigned total;
9326 if (!bmap)
9327 return 0;
9328 bmap = isl_basic_map_copy(bmap);
9329 bmap = isl_basic_map_normalize(bmap);
9330 if (!bmap)
9331 return 0;
9332 total = isl_basic_map_total_dim(bmap);
9333 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9334 for (i = 0; i < bmap->n_eq; ++i) {
9335 uint32_t c_hash;
9336 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9337 isl_hash_hash(hash, c_hash);
9339 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9340 for (i = 0; i < bmap->n_ineq; ++i) {
9341 uint32_t c_hash;
9342 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9343 isl_hash_hash(hash, c_hash);
9345 isl_hash_byte(hash, bmap->n_div & 0xFF);
9346 for (i = 0; i < bmap->n_div; ++i) {
9347 uint32_t c_hash;
9348 if (isl_int_is_zero(bmap->div[i][0]))
9349 continue;
9350 isl_hash_byte(hash, i & 0xFF);
9351 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9352 isl_hash_hash(hash, c_hash);
9354 isl_basic_map_free(bmap);
9355 return hash;
9358 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9360 return isl_basic_map_get_hash((isl_basic_map *)bset);
9363 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9365 int i;
9366 uint32_t hash;
9368 if (!map)
9369 return 0;
9370 map = isl_map_copy(map);
9371 map = isl_map_normalize(map);
9372 if (!map)
9373 return 0;
9375 hash = isl_hash_init();
9376 for (i = 0; i < map->n; ++i) {
9377 uint32_t bmap_hash;
9378 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9379 isl_hash_hash(hash, bmap_hash);
9382 isl_map_free(map);
9384 return hash;
9387 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9389 return isl_map_get_hash((isl_map *)set);
9392 /* Check if the value for dimension dim is completely determined
9393 * by the values of the other parameters and variables.
9394 * That is, check if dimension dim is involved in an equality.
9396 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9398 int i;
9399 unsigned nparam;
9401 if (!bset)
9402 return -1;
9403 nparam = isl_basic_set_n_param(bset);
9404 for (i = 0; i < bset->n_eq; ++i)
9405 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9406 return 1;
9407 return 0;
9410 /* Check if the value for dimension dim is completely determined
9411 * by the values of the other parameters and variables.
9412 * That is, check if dimension dim is involved in an equality
9413 * for each of the subsets.
9415 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9417 int i;
9419 if (!set)
9420 return -1;
9421 for (i = 0; i < set->n; ++i) {
9422 int unique;
9423 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9424 if (unique != 1)
9425 return unique;
9427 return 1;
9430 int isl_set_n_basic_set(__isl_keep isl_set *set)
9432 return set ? set->n : 0;
9435 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9436 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9438 int i;
9440 if (!map)
9441 return -1;
9443 for (i = 0; i < map->n; ++i)
9444 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9445 return -1;
9447 return 0;
9450 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9451 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9453 int i;
9455 if (!set)
9456 return -1;
9458 for (i = 0; i < set->n; ++i)
9459 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9460 return -1;
9462 return 0;
9465 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9467 isl_space *dim;
9469 if (!bset)
9470 return NULL;
9472 bset = isl_basic_set_cow(bset);
9473 if (!bset)
9474 return NULL;
9476 dim = isl_basic_set_get_space(bset);
9477 dim = isl_space_lift(dim, bset->n_div);
9478 if (!dim)
9479 goto error;
9480 isl_space_free(bset->dim);
9481 bset->dim = dim;
9482 bset->extra -= bset->n_div;
9483 bset->n_div = 0;
9485 bset = isl_basic_set_finalize(bset);
9487 return bset;
9488 error:
9489 isl_basic_set_free(bset);
9490 return NULL;
9493 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9495 int i;
9496 isl_space *dim;
9497 unsigned n_div;
9499 set = isl_set_align_divs(set);
9501 if (!set)
9502 return NULL;
9504 set = isl_set_cow(set);
9505 if (!set)
9506 return NULL;
9508 n_div = set->p[0]->n_div;
9509 dim = isl_set_get_space(set);
9510 dim = isl_space_lift(dim, n_div);
9511 if (!dim)
9512 goto error;
9513 isl_space_free(set->dim);
9514 set->dim = dim;
9516 for (i = 0; i < set->n; ++i) {
9517 set->p[i] = isl_basic_set_lift(set->p[i]);
9518 if (!set->p[i])
9519 goto error;
9522 return set;
9523 error:
9524 isl_set_free(set);
9525 return NULL;
9528 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9530 isl_space *dim;
9531 struct isl_basic_map *bmap;
9532 unsigned n_set;
9533 unsigned n_div;
9534 unsigned n_param;
9535 unsigned total;
9536 int i, k, l;
9538 set = isl_set_align_divs(set);
9540 if (!set)
9541 return NULL;
9543 dim = isl_set_get_space(set);
9544 if (set->n == 0 || set->p[0]->n_div == 0) {
9545 isl_set_free(set);
9546 return isl_map_identity(isl_space_map_from_set(dim));
9549 n_div = set->p[0]->n_div;
9550 dim = isl_space_map_from_set(dim);
9551 n_param = isl_space_dim(dim, isl_dim_param);
9552 n_set = isl_space_dim(dim, isl_dim_in);
9553 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9554 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9555 for (i = 0; i < n_set; ++i)
9556 bmap = var_equal(bmap, i);
9558 total = n_param + n_set + n_set + n_div;
9559 for (i = 0; i < n_div; ++i) {
9560 k = isl_basic_map_alloc_inequality(bmap);
9561 if (k < 0)
9562 goto error;
9563 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9564 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9565 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9566 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9567 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9568 set->p[0]->div[i][0]);
9570 l = isl_basic_map_alloc_inequality(bmap);
9571 if (l < 0)
9572 goto error;
9573 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9574 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9575 set->p[0]->div[i][0]);
9576 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9579 isl_set_free(set);
9580 bmap = isl_basic_map_simplify(bmap);
9581 bmap = isl_basic_map_finalize(bmap);
9582 return isl_map_from_basic_map(bmap);
9583 error:
9584 isl_set_free(set);
9585 isl_basic_map_free(bmap);
9586 return NULL;
9589 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9591 unsigned dim;
9592 int size = 0;
9594 if (!bset)
9595 return -1;
9597 dim = isl_basic_set_total_dim(bset);
9598 size += bset->n_eq * (1 + dim);
9599 size += bset->n_ineq * (1 + dim);
9600 size += bset->n_div * (2 + dim);
9602 return size;
9605 int isl_set_size(__isl_keep isl_set *set)
9607 int i;
9608 int size = 0;
9610 if (!set)
9611 return -1;
9613 for (i = 0; i < set->n; ++i)
9614 size += isl_basic_set_size(set->p[i]);
9616 return size;
9619 /* Check if there is any lower bound (if lower == 0) and/or upper
9620 * bound (if upper == 0) on the specified dim.
9622 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9623 enum isl_dim_type type, unsigned pos, int lower, int upper)
9625 int i;
9627 if (!bmap)
9628 return -1;
9630 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9632 pos += isl_basic_map_offset(bmap, type);
9634 for (i = 0; i < bmap->n_div; ++i) {
9635 if (isl_int_is_zero(bmap->div[i][0]))
9636 continue;
9637 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9638 return 1;
9641 for (i = 0; i < bmap->n_eq; ++i)
9642 if (!isl_int_is_zero(bmap->eq[i][pos]))
9643 return 1;
9645 for (i = 0; i < bmap->n_ineq; ++i) {
9646 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9647 if (sgn > 0)
9648 lower = 1;
9649 if (sgn < 0)
9650 upper = 1;
9653 return lower && upper;
9656 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9657 enum isl_dim_type type, unsigned pos)
9659 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9662 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9663 enum isl_dim_type type, unsigned pos)
9665 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9668 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9669 enum isl_dim_type type, unsigned pos)
9671 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9674 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9675 enum isl_dim_type type, unsigned pos)
9677 int i;
9679 if (!map)
9680 return -1;
9682 for (i = 0; i < map->n; ++i) {
9683 int bounded;
9684 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9685 if (bounded < 0 || !bounded)
9686 return bounded;
9689 return 1;
9692 /* Return 1 if the specified dim is involved in both an upper bound
9693 * and a lower bound.
9695 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9696 enum isl_dim_type type, unsigned pos)
9698 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9701 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9703 static int has_any_bound(__isl_keep isl_map *map,
9704 enum isl_dim_type type, unsigned pos,
9705 int (*fn)(__isl_keep isl_basic_map *bmap,
9706 enum isl_dim_type type, unsigned pos))
9708 int i;
9710 if (!map)
9711 return -1;
9713 for (i = 0; i < map->n; ++i) {
9714 int bounded;
9715 bounded = fn(map->p[i], type, pos);
9716 if (bounded < 0 || bounded)
9717 return bounded;
9720 return 0;
9723 /* Return 1 if the specified dim is involved in any lower bound.
9725 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9726 enum isl_dim_type type, unsigned pos)
9728 return has_any_bound(set, type, pos,
9729 &isl_basic_map_dim_has_lower_bound);
9732 /* Return 1 if the specified dim is involved in any upper bound.
9734 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9735 enum isl_dim_type type, unsigned pos)
9737 return has_any_bound(set, type, pos,
9738 &isl_basic_map_dim_has_upper_bound);
9741 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9743 static int has_bound(__isl_keep isl_map *map,
9744 enum isl_dim_type type, unsigned pos,
9745 int (*fn)(__isl_keep isl_basic_map *bmap,
9746 enum isl_dim_type type, unsigned pos))
9748 int i;
9750 if (!map)
9751 return -1;
9753 for (i = 0; i < map->n; ++i) {
9754 int bounded;
9755 bounded = fn(map->p[i], type, pos);
9756 if (bounded < 0 || !bounded)
9757 return bounded;
9760 return 1;
9763 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9765 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9766 enum isl_dim_type type, unsigned pos)
9768 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9771 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9773 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9774 enum isl_dim_type type, unsigned pos)
9776 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9779 /* For each of the "n" variables starting at "first", determine
9780 * the sign of the variable and put the results in the first "n"
9781 * elements of the array "signs".
9782 * Sign
9783 * 1 means that the variable is non-negative
9784 * -1 means that the variable is non-positive
9785 * 0 means the variable attains both positive and negative values.
9787 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9788 unsigned first, unsigned n, int *signs)
9790 isl_vec *bound = NULL;
9791 struct isl_tab *tab = NULL;
9792 struct isl_tab_undo *snap;
9793 int i;
9795 if (!bset || !signs)
9796 return -1;
9798 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9799 tab = isl_tab_from_basic_set(bset, 0);
9800 if (!bound || !tab)
9801 goto error;
9803 isl_seq_clr(bound->el, bound->size);
9804 isl_int_set_si(bound->el[0], -1);
9806 snap = isl_tab_snap(tab);
9807 for (i = 0; i < n; ++i) {
9808 int empty;
9810 isl_int_set_si(bound->el[1 + first + i], -1);
9811 if (isl_tab_add_ineq(tab, bound->el) < 0)
9812 goto error;
9813 empty = tab->empty;
9814 isl_int_set_si(bound->el[1 + first + i], 0);
9815 if (isl_tab_rollback(tab, snap) < 0)
9816 goto error;
9818 if (empty) {
9819 signs[i] = 1;
9820 continue;
9823 isl_int_set_si(bound->el[1 + first + i], 1);
9824 if (isl_tab_add_ineq(tab, bound->el) < 0)
9825 goto error;
9826 empty = tab->empty;
9827 isl_int_set_si(bound->el[1 + first + i], 0);
9828 if (isl_tab_rollback(tab, snap) < 0)
9829 goto error;
9831 signs[i] = empty ? -1 : 0;
9834 isl_tab_free(tab);
9835 isl_vec_free(bound);
9836 return 0;
9837 error:
9838 isl_tab_free(tab);
9839 isl_vec_free(bound);
9840 return -1;
9843 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9844 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9846 if (!bset || !signs)
9847 return -1;
9848 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9849 return -1);
9851 first += pos(bset->dim, type) - 1;
9852 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9855 /* Check if the given basic map is obviously single-valued.
9856 * In particular, for each output dimension, check that there is
9857 * an equality that defines the output dimension in terms of
9858 * earlier dimensions.
9860 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9862 int i, j;
9863 unsigned total;
9864 unsigned n_out;
9865 unsigned o_out;
9867 if (!bmap)
9868 return -1;
9870 total = 1 + isl_basic_map_total_dim(bmap);
9871 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9872 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9874 for (i = 0; i < n_out; ++i) {
9875 for (j = 0; j < bmap->n_eq; ++j) {
9876 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9877 continue;
9878 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9879 total - (o_out + i + 1)) == -1)
9880 break;
9882 if (j >= bmap->n_eq)
9883 return 0;
9886 return 1;
9889 /* Check if the given basic map is single-valued.
9890 * We simply compute
9892 * M \circ M^-1
9894 * and check if the result is a subset of the identity mapping.
9896 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9898 isl_space *space;
9899 isl_basic_map *test;
9900 isl_basic_map *id;
9901 int sv;
9903 sv = isl_basic_map_plain_is_single_valued(bmap);
9904 if (sv < 0 || sv)
9905 return sv;
9907 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9908 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9910 space = isl_basic_map_get_space(bmap);
9911 space = isl_space_map_from_set(isl_space_range(space));
9912 id = isl_basic_map_identity(space);
9914 sv = isl_basic_map_is_subset(test, id);
9916 isl_basic_map_free(test);
9917 isl_basic_map_free(id);
9919 return sv;
9922 /* Check if the given map is obviously single-valued.
9924 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9926 if (!map)
9927 return -1;
9928 if (map->n == 0)
9929 return 1;
9930 if (map->n >= 2)
9931 return 0;
9933 return isl_basic_map_plain_is_single_valued(map->p[0]);
9936 /* Check if the given map is single-valued.
9937 * We simply compute
9939 * M \circ M^-1
9941 * and check if the result is a subset of the identity mapping.
9943 int isl_map_is_single_valued(__isl_keep isl_map *map)
9945 isl_space *dim;
9946 isl_map *test;
9947 isl_map *id;
9948 int sv;
9950 sv = isl_map_plain_is_single_valued(map);
9951 if (sv < 0 || sv)
9952 return sv;
9954 test = isl_map_reverse(isl_map_copy(map));
9955 test = isl_map_apply_range(test, isl_map_copy(map));
9957 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9958 id = isl_map_identity(dim);
9960 sv = isl_map_is_subset(test, id);
9962 isl_map_free(test);
9963 isl_map_free(id);
9965 return sv;
9968 int isl_map_is_injective(__isl_keep isl_map *map)
9970 int in;
9972 map = isl_map_copy(map);
9973 map = isl_map_reverse(map);
9974 in = isl_map_is_single_valued(map);
9975 isl_map_free(map);
9977 return in;
9980 /* Check if the given map is obviously injective.
9982 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9984 int in;
9986 map = isl_map_copy(map);
9987 map = isl_map_reverse(map);
9988 in = isl_map_plain_is_single_valued(map);
9989 isl_map_free(map);
9991 return in;
9994 int isl_map_is_bijective(__isl_keep isl_map *map)
9996 int sv;
9998 sv = isl_map_is_single_valued(map);
9999 if (sv < 0 || !sv)
10000 return sv;
10002 return isl_map_is_injective(map);
10005 int isl_set_is_singleton(__isl_keep isl_set *set)
10007 return isl_map_is_single_valued((isl_map *)set);
10010 int isl_map_is_translation(__isl_keep isl_map *map)
10012 int ok;
10013 isl_set *delta;
10015 delta = isl_map_deltas(isl_map_copy(map));
10016 ok = isl_set_is_singleton(delta);
10017 isl_set_free(delta);
10019 return ok;
10022 static int unique(isl_int *p, unsigned pos, unsigned len)
10024 if (isl_seq_first_non_zero(p, pos) != -1)
10025 return 0;
10026 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10027 return 0;
10028 return 1;
10031 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10033 int i, j;
10034 unsigned nvar;
10035 unsigned ovar;
10037 if (!bset)
10038 return -1;
10040 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10041 return 0;
10043 nvar = isl_basic_set_dim(bset, isl_dim_set);
10044 ovar = isl_space_offset(bset->dim, isl_dim_set);
10045 for (j = 0; j < nvar; ++j) {
10046 int lower = 0, upper = 0;
10047 for (i = 0; i < bset->n_eq; ++i) {
10048 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10049 continue;
10050 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10051 return 0;
10052 break;
10054 if (i < bset->n_eq)
10055 continue;
10056 for (i = 0; i < bset->n_ineq; ++i) {
10057 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10058 continue;
10059 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10060 return 0;
10061 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10062 lower = 1;
10063 else
10064 upper = 1;
10066 if (!lower || !upper)
10067 return 0;
10070 return 1;
10073 int isl_set_is_box(__isl_keep isl_set *set)
10075 if (!set)
10076 return -1;
10077 if (set->n != 1)
10078 return 0;
10080 return isl_basic_set_is_box(set->p[0]);
10083 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10085 if (!bset)
10086 return -1;
10088 return isl_space_is_wrapping(bset->dim);
10091 int isl_set_is_wrapping(__isl_keep isl_set *set)
10093 if (!set)
10094 return -1;
10096 return isl_space_is_wrapping(set->dim);
10099 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10101 bmap = isl_basic_map_cow(bmap);
10102 if (!bmap)
10103 return NULL;
10105 bmap->dim = isl_space_wrap(bmap->dim);
10106 if (!bmap->dim)
10107 goto error;
10109 bmap = isl_basic_map_finalize(bmap);
10111 return (isl_basic_set *)bmap;
10112 error:
10113 isl_basic_map_free(bmap);
10114 return NULL;
10117 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10119 int i;
10121 map = isl_map_cow(map);
10122 if (!map)
10123 return NULL;
10125 for (i = 0; i < map->n; ++i) {
10126 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10127 if (!map->p[i])
10128 goto error;
10130 map->dim = isl_space_wrap(map->dim);
10131 if (!map->dim)
10132 goto error;
10134 return (isl_set *)map;
10135 error:
10136 isl_map_free(map);
10137 return NULL;
10140 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10142 bset = isl_basic_set_cow(bset);
10143 if (!bset)
10144 return NULL;
10146 bset->dim = isl_space_unwrap(bset->dim);
10147 if (!bset->dim)
10148 goto error;
10150 bset = isl_basic_set_finalize(bset);
10152 return (isl_basic_map *)bset;
10153 error:
10154 isl_basic_set_free(bset);
10155 return NULL;
10158 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10160 int i;
10162 if (!set)
10163 return NULL;
10165 if (!isl_set_is_wrapping(set))
10166 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10167 goto error);
10169 set = isl_set_cow(set);
10170 if (!set)
10171 return NULL;
10173 for (i = 0; i < set->n; ++i) {
10174 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10175 if (!set->p[i])
10176 goto error;
10179 set->dim = isl_space_unwrap(set->dim);
10180 if (!set->dim)
10181 goto error;
10183 return (isl_map *)set;
10184 error:
10185 isl_set_free(set);
10186 return NULL;
10189 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10190 enum isl_dim_type type)
10192 if (!bmap)
10193 return NULL;
10195 if (!isl_space_is_named_or_nested(bmap->dim, type))
10196 return bmap;
10198 bmap = isl_basic_map_cow(bmap);
10199 if (!bmap)
10200 return NULL;
10202 bmap->dim = isl_space_reset(bmap->dim, type);
10203 if (!bmap->dim)
10204 goto error;
10206 bmap = isl_basic_map_finalize(bmap);
10208 return bmap;
10209 error:
10210 isl_basic_map_free(bmap);
10211 return NULL;
10214 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10215 enum isl_dim_type type)
10217 int i;
10219 if (!map)
10220 return NULL;
10222 if (!isl_space_is_named_or_nested(map->dim, type))
10223 return map;
10225 map = isl_map_cow(map);
10226 if (!map)
10227 return NULL;
10229 for (i = 0; i < map->n; ++i) {
10230 map->p[i] = isl_basic_map_reset(map->p[i], type);
10231 if (!map->p[i])
10232 goto error;
10234 map->dim = isl_space_reset(map->dim, type);
10235 if (!map->dim)
10236 goto error;
10238 return map;
10239 error:
10240 isl_map_free(map);
10241 return NULL;
10244 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10246 if (!bmap)
10247 return NULL;
10249 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10250 return bmap;
10252 bmap = isl_basic_map_cow(bmap);
10253 if (!bmap)
10254 return NULL;
10256 bmap->dim = isl_space_flatten(bmap->dim);
10257 if (!bmap->dim)
10258 goto error;
10260 bmap = isl_basic_map_finalize(bmap);
10262 return bmap;
10263 error:
10264 isl_basic_map_free(bmap);
10265 return NULL;
10268 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10270 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10273 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10274 __isl_take isl_basic_map *bmap)
10276 if (!bmap)
10277 return NULL;
10279 if (!bmap->dim->nested[0])
10280 return bmap;
10282 bmap = isl_basic_map_cow(bmap);
10283 if (!bmap)
10284 return NULL;
10286 bmap->dim = isl_space_flatten_domain(bmap->dim);
10287 if (!bmap->dim)
10288 goto error;
10290 bmap = isl_basic_map_finalize(bmap);
10292 return bmap;
10293 error:
10294 isl_basic_map_free(bmap);
10295 return NULL;
10298 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10299 __isl_take isl_basic_map *bmap)
10301 if (!bmap)
10302 return NULL;
10304 if (!bmap->dim->nested[1])
10305 return bmap;
10307 bmap = isl_basic_map_cow(bmap);
10308 if (!bmap)
10309 return NULL;
10311 bmap->dim = isl_space_flatten_range(bmap->dim);
10312 if (!bmap->dim)
10313 goto error;
10315 bmap = isl_basic_map_finalize(bmap);
10317 return bmap;
10318 error:
10319 isl_basic_map_free(bmap);
10320 return NULL;
10323 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10325 int i;
10327 if (!map)
10328 return NULL;
10330 if (!map->dim->nested[0] && !map->dim->nested[1])
10331 return map;
10333 map = isl_map_cow(map);
10334 if (!map)
10335 return NULL;
10337 for (i = 0; i < map->n; ++i) {
10338 map->p[i] = isl_basic_map_flatten(map->p[i]);
10339 if (!map->p[i])
10340 goto error;
10342 map->dim = isl_space_flatten(map->dim);
10343 if (!map->dim)
10344 goto error;
10346 return map;
10347 error:
10348 isl_map_free(map);
10349 return NULL;
10352 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10354 return (isl_set *)isl_map_flatten((isl_map *)set);
10357 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10359 isl_space *dim, *flat_dim;
10360 isl_map *map;
10362 dim = isl_set_get_space(set);
10363 flat_dim = isl_space_flatten(isl_space_copy(dim));
10364 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10365 map = isl_map_intersect_domain(map, set);
10367 return map;
10370 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10372 int i;
10374 if (!map)
10375 return NULL;
10377 if (!map->dim->nested[0])
10378 return map;
10380 map = isl_map_cow(map);
10381 if (!map)
10382 return NULL;
10384 for (i = 0; i < map->n; ++i) {
10385 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10386 if (!map->p[i])
10387 goto error;
10389 map->dim = isl_space_flatten_domain(map->dim);
10390 if (!map->dim)
10391 goto error;
10393 return map;
10394 error:
10395 isl_map_free(map);
10396 return NULL;
10399 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10401 int i;
10403 if (!map)
10404 return NULL;
10406 if (!map->dim->nested[1])
10407 return map;
10409 map = isl_map_cow(map);
10410 if (!map)
10411 return NULL;
10413 for (i = 0; i < map->n; ++i) {
10414 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10415 if (!map->p[i])
10416 goto error;
10418 map->dim = isl_space_flatten_range(map->dim);
10419 if (!map->dim)
10420 goto error;
10422 return map;
10423 error:
10424 isl_map_free(map);
10425 return NULL;
10428 /* Reorder the dimensions of "bmap" according to the given dim_map
10429 * and set the dimension specification to "dim".
10431 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10432 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10434 isl_basic_map *res;
10435 unsigned flags;
10437 bmap = isl_basic_map_cow(bmap);
10438 if (!bmap || !dim || !dim_map)
10439 goto error;
10441 flags = bmap->flags;
10442 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10443 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10444 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10445 res = isl_basic_map_alloc_space(dim,
10446 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10447 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10448 if (res)
10449 res->flags = flags;
10450 res = isl_basic_map_finalize(res);
10451 return res;
10452 error:
10453 free(dim_map);
10454 isl_basic_map_free(bmap);
10455 isl_space_free(dim);
10456 return NULL;
10459 /* Reorder the dimensions of "map" according to given reordering.
10461 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10462 __isl_take isl_reordering *r)
10464 int i;
10465 struct isl_dim_map *dim_map;
10467 map = isl_map_cow(map);
10468 dim_map = isl_dim_map_from_reordering(r);
10469 if (!map || !r || !dim_map)
10470 goto error;
10472 for (i = 0; i < map->n; ++i) {
10473 struct isl_dim_map *dim_map_i;
10475 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10477 map->p[i] = isl_basic_map_realign(map->p[i],
10478 isl_space_copy(r->dim), dim_map_i);
10480 if (!map->p[i])
10481 goto error;
10484 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10486 isl_reordering_free(r);
10487 free(dim_map);
10488 return map;
10489 error:
10490 free(dim_map);
10491 isl_map_free(map);
10492 isl_reordering_free(r);
10493 return NULL;
10496 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10497 __isl_take isl_reordering *r)
10499 return (isl_set *)isl_map_realign((isl_map *)set, r);
10502 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10503 __isl_take isl_space *model)
10505 isl_ctx *ctx;
10507 if (!map || !model)
10508 goto error;
10510 ctx = isl_space_get_ctx(model);
10511 if (!isl_space_has_named_params(model))
10512 isl_die(ctx, isl_error_invalid,
10513 "model has unnamed parameters", goto error);
10514 if (!isl_space_has_named_params(map->dim))
10515 isl_die(ctx, isl_error_invalid,
10516 "relation has unnamed parameters", goto error);
10517 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10518 isl_reordering *exp;
10520 model = isl_space_drop_dims(model, isl_dim_in,
10521 0, isl_space_dim(model, isl_dim_in));
10522 model = isl_space_drop_dims(model, isl_dim_out,
10523 0, isl_space_dim(model, isl_dim_out));
10524 exp = isl_parameter_alignment_reordering(map->dim, model);
10525 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10526 map = isl_map_realign(map, exp);
10529 isl_space_free(model);
10530 return map;
10531 error:
10532 isl_space_free(model);
10533 isl_map_free(map);
10534 return NULL;
10537 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10538 __isl_take isl_space *model)
10540 return isl_map_align_params(set, model);
10543 /* Align the parameters of "bmap" to those of "model", introducing
10544 * additional parameters if needed.
10546 __isl_give isl_basic_map *isl_basic_map_align_params(
10547 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10549 isl_ctx *ctx;
10551 if (!bmap || !model)
10552 goto error;
10554 ctx = isl_space_get_ctx(model);
10555 if (!isl_space_has_named_params(model))
10556 isl_die(ctx, isl_error_invalid,
10557 "model has unnamed parameters", goto error);
10558 if (!isl_space_has_named_params(bmap->dim))
10559 isl_die(ctx, isl_error_invalid,
10560 "relation has unnamed parameters", goto error);
10561 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10562 isl_reordering *exp;
10563 struct isl_dim_map *dim_map;
10565 model = isl_space_drop_dims(model, isl_dim_in,
10566 0, isl_space_dim(model, isl_dim_in));
10567 model = isl_space_drop_dims(model, isl_dim_out,
10568 0, isl_space_dim(model, isl_dim_out));
10569 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10570 exp = isl_reordering_extend_space(exp,
10571 isl_basic_map_get_space(bmap));
10572 dim_map = isl_dim_map_from_reordering(exp);
10573 bmap = isl_basic_map_realign(bmap,
10574 exp ? isl_space_copy(exp->dim) : NULL,
10575 isl_dim_map_extend(dim_map, bmap));
10576 isl_reordering_free(exp);
10577 free(dim_map);
10580 isl_space_free(model);
10581 return bmap;
10582 error:
10583 isl_space_free(model);
10584 isl_basic_map_free(bmap);
10585 return NULL;
10588 /* Align the parameters of "bset" to those of "model", introducing
10589 * additional parameters if needed.
10591 __isl_give isl_basic_set *isl_basic_set_align_params(
10592 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10594 return isl_basic_map_align_params(bset, model);
10597 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10598 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10599 enum isl_dim_type c2, enum isl_dim_type c3,
10600 enum isl_dim_type c4, enum isl_dim_type c5)
10602 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10603 struct isl_mat *mat;
10604 int i, j, k;
10605 int pos;
10607 if (!bmap)
10608 return NULL;
10609 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10610 isl_basic_map_total_dim(bmap) + 1);
10611 if (!mat)
10612 return NULL;
10613 for (i = 0; i < bmap->n_eq; ++i)
10614 for (j = 0, pos = 0; j < 5; ++j) {
10615 int off = isl_basic_map_offset(bmap, c[j]);
10616 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10617 isl_int_set(mat->row[i][pos],
10618 bmap->eq[i][off + k]);
10619 ++pos;
10623 return mat;
10626 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10627 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10628 enum isl_dim_type c2, enum isl_dim_type c3,
10629 enum isl_dim_type c4, enum isl_dim_type c5)
10631 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10632 struct isl_mat *mat;
10633 int i, j, k;
10634 int pos;
10636 if (!bmap)
10637 return NULL;
10638 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10639 isl_basic_map_total_dim(bmap) + 1);
10640 if (!mat)
10641 return NULL;
10642 for (i = 0; i < bmap->n_ineq; ++i)
10643 for (j = 0, pos = 0; j < 5; ++j) {
10644 int off = isl_basic_map_offset(bmap, c[j]);
10645 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10646 isl_int_set(mat->row[i][pos],
10647 bmap->ineq[i][off + k]);
10648 ++pos;
10652 return mat;
10655 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10656 __isl_take isl_space *dim,
10657 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10658 enum isl_dim_type c2, enum isl_dim_type c3,
10659 enum isl_dim_type c4, enum isl_dim_type c5)
10661 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10662 isl_basic_map *bmap;
10663 unsigned total;
10664 unsigned extra;
10665 int i, j, k, l;
10666 int pos;
10668 if (!dim || !eq || !ineq)
10669 goto error;
10671 if (eq->n_col != ineq->n_col)
10672 isl_die(dim->ctx, isl_error_invalid,
10673 "equalities and inequalities matrices should have "
10674 "same number of columns", goto error);
10676 total = 1 + isl_space_dim(dim, isl_dim_all);
10678 if (eq->n_col < total)
10679 isl_die(dim->ctx, isl_error_invalid,
10680 "number of columns too small", goto error);
10682 extra = eq->n_col - total;
10684 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10685 eq->n_row, ineq->n_row);
10686 if (!bmap)
10687 goto error;
10688 for (i = 0; i < extra; ++i) {
10689 k = isl_basic_map_alloc_div(bmap);
10690 if (k < 0)
10691 goto error;
10692 isl_int_set_si(bmap->div[k][0], 0);
10694 for (i = 0; i < eq->n_row; ++i) {
10695 l = isl_basic_map_alloc_equality(bmap);
10696 if (l < 0)
10697 goto error;
10698 for (j = 0, pos = 0; j < 5; ++j) {
10699 int off = isl_basic_map_offset(bmap, c[j]);
10700 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10701 isl_int_set(bmap->eq[l][off + k],
10702 eq->row[i][pos]);
10703 ++pos;
10707 for (i = 0; i < ineq->n_row; ++i) {
10708 l = isl_basic_map_alloc_inequality(bmap);
10709 if (l < 0)
10710 goto error;
10711 for (j = 0, pos = 0; j < 5; ++j) {
10712 int off = isl_basic_map_offset(bmap, c[j]);
10713 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10714 isl_int_set(bmap->ineq[l][off + k],
10715 ineq->row[i][pos]);
10716 ++pos;
10721 isl_space_free(dim);
10722 isl_mat_free(eq);
10723 isl_mat_free(ineq);
10725 bmap = isl_basic_map_simplify(bmap);
10726 return isl_basic_map_finalize(bmap);
10727 error:
10728 isl_space_free(dim);
10729 isl_mat_free(eq);
10730 isl_mat_free(ineq);
10731 return NULL;
10734 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10735 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10736 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10738 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10739 c1, c2, c3, c4, isl_dim_in);
10742 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10743 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10744 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10746 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10747 c1, c2, c3, c4, isl_dim_in);
10750 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10751 __isl_take isl_space *dim,
10752 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10753 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10755 return (isl_basic_set*)
10756 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10757 c1, c2, c3, c4, isl_dim_in);
10760 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10762 if (!bmap)
10763 return -1;
10765 return isl_space_can_zip(bmap->dim);
10768 int isl_map_can_zip(__isl_keep isl_map *map)
10770 if (!map)
10771 return -1;
10773 return isl_space_can_zip(map->dim);
10776 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10777 * (A -> C) -> (B -> D).
10779 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10781 unsigned pos;
10782 unsigned n1;
10783 unsigned n2;
10785 if (!bmap)
10786 return NULL;
10788 if (!isl_basic_map_can_zip(bmap))
10789 isl_die(bmap->ctx, isl_error_invalid,
10790 "basic map cannot be zipped", goto error);
10791 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10792 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10793 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10794 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10795 bmap = isl_basic_map_cow(bmap);
10796 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10797 if (!bmap)
10798 return NULL;
10799 bmap->dim = isl_space_zip(bmap->dim);
10800 if (!bmap->dim)
10801 goto error;
10802 return bmap;
10803 error:
10804 isl_basic_map_free(bmap);
10805 return NULL;
10808 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10809 * (A -> C) -> (B -> D).
10811 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10813 int i;
10815 if (!map)
10816 return NULL;
10818 if (!isl_map_can_zip(map))
10819 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10820 goto error);
10822 map = isl_map_cow(map);
10823 if (!map)
10824 return NULL;
10826 for (i = 0; i < map->n; ++i) {
10827 map->p[i] = isl_basic_map_zip(map->p[i]);
10828 if (!map->p[i])
10829 goto error;
10832 map->dim = isl_space_zip(map->dim);
10833 if (!map->dim)
10834 goto error;
10836 return map;
10837 error:
10838 isl_map_free(map);
10839 return NULL;
10842 /* Can we apply isl_basic_map_curry to "bmap"?
10843 * That is, does it have a nested relation in its domain?
10845 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10847 if (!bmap)
10848 return -1;
10850 return isl_space_can_curry(bmap->dim);
10853 /* Can we apply isl_map_curry to "map"?
10854 * That is, does it have a nested relation in its domain?
10856 int isl_map_can_curry(__isl_keep isl_map *map)
10858 if (!map)
10859 return -1;
10861 return isl_space_can_curry(map->dim);
10864 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10865 * A -> (B -> C).
10867 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10870 if (!bmap)
10871 return NULL;
10873 if (!isl_basic_map_can_curry(bmap))
10874 isl_die(bmap->ctx, isl_error_invalid,
10875 "basic map cannot be curried", goto error);
10876 bmap = isl_basic_map_cow(bmap);
10877 if (!bmap)
10878 return NULL;
10879 bmap->dim = isl_space_curry(bmap->dim);
10880 if (!bmap->dim)
10881 goto error;
10882 return bmap;
10883 error:
10884 isl_basic_map_free(bmap);
10885 return NULL;
10888 /* Given a map (A -> B) -> C, return the corresponding map
10889 * A -> (B -> C).
10891 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10893 int i;
10895 if (!map)
10896 return NULL;
10898 if (!isl_map_can_curry(map))
10899 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10900 goto error);
10902 map = isl_map_cow(map);
10903 if (!map)
10904 return NULL;
10906 for (i = 0; i < map->n; ++i) {
10907 map->p[i] = isl_basic_map_curry(map->p[i]);
10908 if (!map->p[i])
10909 goto error;
10912 map->dim = isl_space_curry(map->dim);
10913 if (!map->dim)
10914 goto error;
10916 return map;
10917 error:
10918 isl_map_free(map);
10919 return NULL;
10922 /* Can we apply isl_basic_map_uncurry to "bmap"?
10923 * That is, does it have a nested relation in its domain?
10925 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
10927 if (!bmap)
10928 return -1;
10930 return isl_space_can_uncurry(bmap->dim);
10933 /* Can we apply isl_map_uncurry to "map"?
10934 * That is, does it have a nested relation in its domain?
10936 int isl_map_can_uncurry(__isl_keep isl_map *map)
10938 if (!map)
10939 return -1;
10941 return isl_space_can_uncurry(map->dim);
10944 /* Given a basic map A -> (B -> C), return the corresponding basic map
10945 * (A -> B) -> C.
10947 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
10950 if (!bmap)
10951 return NULL;
10953 if (!isl_basic_map_can_uncurry(bmap))
10954 isl_die(bmap->ctx, isl_error_invalid,
10955 "basic map cannot be uncurried",
10956 return isl_basic_map_free(bmap));
10957 bmap = isl_basic_map_cow(bmap);
10958 if (!bmap)
10959 return NULL;
10960 bmap->dim = isl_space_uncurry(bmap->dim);
10961 if (!bmap->dim)
10962 return isl_basic_map_free(bmap);
10963 return bmap;
10966 /* Given a map A -> (B -> C), return the corresponding map
10967 * (A -> B) -> C.
10969 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
10971 int i;
10973 if (!map)
10974 return NULL;
10976 if (!isl_map_can_uncurry(map))
10977 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
10978 return isl_map_free(map));
10980 map = isl_map_cow(map);
10981 if (!map)
10982 return NULL;
10984 for (i = 0; i < map->n; ++i) {
10985 map->p[i] = isl_basic_map_uncurry(map->p[i]);
10986 if (!map->p[i])
10987 return isl_map_free(map);
10990 map->dim = isl_space_uncurry(map->dim);
10991 if (!map->dim)
10992 return isl_map_free(map);
10994 return map;
10997 /* Construct a basic map mapping the domain of the affine expression
10998 * to a one-dimensional range prescribed by the affine expression.
11000 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11002 int k;
11003 int pos;
11004 isl_local_space *ls;
11005 isl_basic_map *bmap;
11007 if (!aff)
11008 return NULL;
11010 ls = isl_aff_get_local_space(aff);
11011 bmap = isl_basic_map_from_local_space(ls);
11012 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11013 k = isl_basic_map_alloc_equality(bmap);
11014 if (k < 0)
11015 goto error;
11017 pos = isl_basic_map_offset(bmap, isl_dim_out);
11018 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11019 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11020 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11021 aff->v->size - (pos + 1));
11023 isl_aff_free(aff);
11024 bmap = isl_basic_map_finalize(bmap);
11025 return bmap;
11026 error:
11027 isl_aff_free(aff);
11028 isl_basic_map_free(bmap);
11029 return NULL;
11032 /* Construct a map mapping the domain of the affine expression
11033 * to a one-dimensional range prescribed by the affine expression.
11035 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11037 isl_basic_map *bmap;
11039 bmap = isl_basic_map_from_aff(aff);
11040 return isl_map_from_basic_map(bmap);
11043 /* Construct a basic map mapping the domain the multi-affine expression
11044 * to its range, with each dimension in the range equated to the
11045 * corresponding affine expression.
11047 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11048 __isl_take isl_multi_aff *maff)
11050 int i;
11051 isl_space *space;
11052 isl_basic_map *bmap;
11054 if (!maff)
11055 return NULL;
11057 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11058 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11059 "invalid space", return isl_multi_aff_free(maff));
11061 space = isl_space_domain(isl_multi_aff_get_space(maff));
11062 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11064 for (i = 0; i < maff->n; ++i) {
11065 isl_aff *aff;
11066 isl_basic_map *bmap_i;
11068 aff = isl_aff_copy(maff->p[i]);
11069 bmap_i = isl_basic_map_from_aff(aff);
11071 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11074 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11076 isl_multi_aff_free(maff);
11077 return bmap;
11080 /* Construct a map mapping the domain the multi-affine expression
11081 * to its range, with each dimension in the range equated to the
11082 * corresponding affine expression.
11084 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11086 isl_basic_map *bmap;
11088 bmap = isl_basic_map_from_multi_aff(maff);
11089 return isl_map_from_basic_map(bmap);
11092 /* Construct a basic map mapping a domain in the given space to
11093 * to an n-dimensional range, with n the number of elements in the list,
11094 * where each coordinate in the range is prescribed by the
11095 * corresponding affine expression.
11096 * The domains of all affine expressions in the list are assumed to match
11097 * domain_dim.
11099 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11100 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11102 int i;
11103 isl_space *dim;
11104 isl_basic_map *bmap;
11106 if (!list)
11107 return NULL;
11109 dim = isl_space_from_domain(domain_dim);
11110 bmap = isl_basic_map_universe(dim);
11112 for (i = 0; i < list->n; ++i) {
11113 isl_aff *aff;
11114 isl_basic_map *bmap_i;
11116 aff = isl_aff_copy(list->p[i]);
11117 bmap_i = isl_basic_map_from_aff(aff);
11119 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11122 isl_aff_list_free(list);
11123 return bmap;
11126 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11127 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11129 return isl_map_equate(set, type1, pos1, type2, pos2);
11132 /* Construct a basic map where the given dimensions are equal to each other.
11134 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11135 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11137 isl_basic_map *bmap = NULL;
11138 int i;
11140 if (!space)
11141 return NULL;
11143 if (pos1 >= isl_space_dim(space, type1))
11144 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11145 "index out of bounds", goto error);
11146 if (pos2 >= isl_space_dim(space, type2))
11147 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11148 "index out of bounds", goto error);
11150 if (type1 == type2 && pos1 == pos2)
11151 return isl_basic_map_universe(space);
11153 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11154 i = isl_basic_map_alloc_equality(bmap);
11155 if (i < 0)
11156 goto error;
11157 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11158 pos1 += isl_basic_map_offset(bmap, type1);
11159 pos2 += isl_basic_map_offset(bmap, type2);
11160 isl_int_set_si(bmap->eq[i][pos1], -1);
11161 isl_int_set_si(bmap->eq[i][pos2], 1);
11162 bmap = isl_basic_map_finalize(bmap);
11163 isl_space_free(space);
11164 return bmap;
11165 error:
11166 isl_space_free(space);
11167 isl_basic_map_free(bmap);
11168 return NULL;
11171 /* Add a constraint imposing that the given two dimensions are equal.
11173 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11174 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11176 isl_basic_map *eq;
11178 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11180 bmap = isl_basic_map_intersect(bmap, eq);
11182 return bmap;
11185 /* Add a constraint imposing that the given two dimensions are equal.
11187 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11188 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11190 isl_basic_map *bmap;
11192 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11194 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11196 return map;
11199 /* Add a constraint imposing that the given two dimensions have opposite values.
11201 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11202 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11204 isl_basic_map *bmap = NULL;
11205 int i;
11207 if (!map)
11208 return NULL;
11210 if (pos1 >= isl_map_dim(map, type1))
11211 isl_die(map->ctx, isl_error_invalid,
11212 "index out of bounds", goto error);
11213 if (pos2 >= isl_map_dim(map, type2))
11214 isl_die(map->ctx, isl_error_invalid,
11215 "index out of bounds", goto error);
11217 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11218 i = isl_basic_map_alloc_equality(bmap);
11219 if (i < 0)
11220 goto error;
11221 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11222 pos1 += isl_basic_map_offset(bmap, type1);
11223 pos2 += isl_basic_map_offset(bmap, type2);
11224 isl_int_set_si(bmap->eq[i][pos1], 1);
11225 isl_int_set_si(bmap->eq[i][pos2], 1);
11226 bmap = isl_basic_map_finalize(bmap);
11228 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11230 return map;
11231 error:
11232 isl_basic_map_free(bmap);
11233 isl_map_free(map);
11234 return NULL;
11237 /* Add a constraint imposing that the value of the first dimension is
11238 * greater than or equal to that of the second.
11240 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11241 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11243 isl_constraint *c;
11244 isl_local_space *ls;
11246 if (!bmap)
11247 return NULL;
11249 if (pos1 >= isl_basic_map_dim(bmap, type1))
11250 isl_die(bmap->ctx, isl_error_invalid,
11251 "index out of bounds", return isl_basic_map_free(bmap));
11252 if (pos2 >= isl_basic_map_dim(bmap, type2))
11253 isl_die(bmap->ctx, isl_error_invalid,
11254 "index out of bounds", return isl_basic_map_free(bmap));
11256 if (type1 == type2 && pos1 == pos2)
11257 return bmap;
11259 ls = isl_local_space_from_space(isl_basic_map_get_space(bmap));
11260 c = isl_inequality_alloc(ls);
11261 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11262 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11263 bmap = isl_basic_map_add_constraint(bmap, c);
11265 return bmap;
11268 /* Construct a basic map where the value of the first dimension is
11269 * greater than that of the second.
11271 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11272 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11274 isl_basic_map *bmap = NULL;
11275 int i;
11277 if (!space)
11278 return NULL;
11280 if (pos1 >= isl_space_dim(space, type1))
11281 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11282 "index out of bounds", goto error);
11283 if (pos2 >= isl_space_dim(space, type2))
11284 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11285 "index out of bounds", goto error);
11287 if (type1 == type2 && pos1 == pos2)
11288 return isl_basic_map_empty(space);
11290 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11291 i = isl_basic_map_alloc_inequality(bmap);
11292 if (i < 0)
11293 goto error;
11294 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11295 pos1 += isl_basic_map_offset(bmap, type1);
11296 pos2 += isl_basic_map_offset(bmap, type2);
11297 isl_int_set_si(bmap->ineq[i][pos1], 1);
11298 isl_int_set_si(bmap->ineq[i][pos2], -1);
11299 isl_int_set_si(bmap->ineq[i][0], -1);
11300 bmap = isl_basic_map_finalize(bmap);
11302 return bmap;
11303 error:
11304 isl_space_free(space);
11305 isl_basic_map_free(bmap);
11306 return NULL;
11309 /* Add a constraint imposing that the value of the first dimension is
11310 * greater than that of the second.
11312 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11313 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11315 isl_basic_map *gt;
11317 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11319 bmap = isl_basic_map_intersect(bmap, gt);
11321 return bmap;
11324 /* Add a constraint imposing that the value of the first dimension is
11325 * greater than that of the second.
11327 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11328 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11330 isl_basic_map *bmap;
11332 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11334 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11336 return map;
11339 /* Add a constraint imposing that the value of the first dimension is
11340 * smaller than that of the second.
11342 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11343 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11345 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11348 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11349 int pos)
11351 isl_aff *div;
11352 isl_local_space *ls;
11354 if (!bmap)
11355 return NULL;
11357 if (!isl_basic_map_divs_known(bmap))
11358 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11359 "some divs are unknown", return NULL);
11361 ls = isl_basic_map_get_local_space(bmap);
11362 div = isl_local_space_get_div(ls, pos);
11363 isl_local_space_free(ls);
11365 return div;
11368 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11369 int pos)
11371 return isl_basic_map_get_div(bset, pos);
11374 /* Plug in "subs" for dimension "type", "pos" of "bset".
11376 * Let i be the dimension to replace and let "subs" be of the form
11378 * f/d
11380 * Any integer division with a non-zero coefficient for i,
11382 * floor((a i + g)/m)
11384 * is replaced by
11386 * floor((a f + d g)/(m d))
11388 * Constraints of the form
11390 * a i + g
11392 * are replaced by
11394 * a f + d g
11396 * We currently require that "subs" is an integral expression.
11397 * Handling rational expressions may require us to add stride constraints
11398 * as we do in isl_basic_set_preimage_multi_aff.
11400 __isl_give isl_basic_set *isl_basic_set_substitute(
11401 __isl_take isl_basic_set *bset,
11402 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11404 int i;
11405 isl_int v;
11406 isl_ctx *ctx;
11408 if (bset && isl_basic_set_plain_is_empty(bset))
11409 return bset;
11411 bset = isl_basic_set_cow(bset);
11412 if (!bset || !subs)
11413 goto error;
11415 ctx = isl_basic_set_get_ctx(bset);
11416 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11417 isl_die(ctx, isl_error_invalid,
11418 "spaces don't match", goto error);
11419 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11420 isl_die(ctx, isl_error_unsupported,
11421 "cannot handle divs yet", goto error);
11422 if (!isl_int_is_one(subs->v->el[0]))
11423 isl_die(ctx, isl_error_invalid,
11424 "can only substitute integer expressions", goto error);
11426 pos += isl_basic_set_offset(bset, type);
11428 isl_int_init(v);
11430 for (i = 0; i < bset->n_eq; ++i) {
11431 if (isl_int_is_zero(bset->eq[i][pos]))
11432 continue;
11433 isl_int_set(v, bset->eq[i][pos]);
11434 isl_int_set_si(bset->eq[i][pos], 0);
11435 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11436 v, subs->v->el + 1, subs->v->size - 1);
11439 for (i = 0; i < bset->n_ineq; ++i) {
11440 if (isl_int_is_zero(bset->ineq[i][pos]))
11441 continue;
11442 isl_int_set(v, bset->ineq[i][pos]);
11443 isl_int_set_si(bset->ineq[i][pos], 0);
11444 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11445 v, subs->v->el + 1, subs->v->size - 1);
11448 for (i = 0; i < bset->n_div; ++i) {
11449 if (isl_int_is_zero(bset->div[i][1 + pos]))
11450 continue;
11451 isl_int_set(v, bset->div[i][1 + pos]);
11452 isl_int_set_si(bset->div[i][1 + pos], 0);
11453 isl_seq_combine(bset->div[i] + 1,
11454 subs->v->el[0], bset->div[i] + 1,
11455 v, subs->v->el + 1, subs->v->size - 1);
11456 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11459 isl_int_clear(v);
11461 bset = isl_basic_set_simplify(bset);
11462 return isl_basic_set_finalize(bset);
11463 error:
11464 isl_basic_set_free(bset);
11465 return NULL;
11468 /* Plug in "subs" for dimension "type", "pos" of "set".
11470 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11471 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11473 int i;
11475 if (set && isl_set_plain_is_empty(set))
11476 return set;
11478 set = isl_set_cow(set);
11479 if (!set || !subs)
11480 goto error;
11482 for (i = set->n - 1; i >= 0; --i) {
11483 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11484 if (remove_if_empty(set, i) < 0)
11485 goto error;
11488 return set;
11489 error:
11490 isl_set_free(set);
11491 return NULL;
11494 /* Check if the range of "ma" is compatible with the domain or range
11495 * (depending on "type") of "bmap".
11496 * Return -1 if anything is wrong.
11498 static int check_basic_map_compatible_range_multi_aff(
11499 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11500 __isl_keep isl_multi_aff *ma)
11502 int m;
11503 isl_space *ma_space;
11505 ma_space = isl_multi_aff_get_space(ma);
11506 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11507 isl_space_free(ma_space);
11508 if (m >= 0 && !m)
11509 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11510 "spaces don't match", return -1);
11511 return m;
11514 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11515 * coefficients before the transformed range of dimensions,
11516 * the "n_after" coefficients after the transformed range of dimensions
11517 * and the coefficients of the other divs in "bmap".
11519 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11520 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11522 int i;
11523 int n_param;
11524 int n_set;
11525 isl_local_space *ls;
11527 if (n_div == 0)
11528 return 0;
11530 ls = isl_aff_get_domain_local_space(ma->p[0]);
11531 if (!ls)
11532 return -1;
11534 n_param = isl_local_space_dim(ls, isl_dim_param);
11535 n_set = isl_local_space_dim(ls, isl_dim_set);
11536 for (i = 0; i < n_div; ++i) {
11537 int o_bmap = 0, o_ls = 0;
11539 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
11540 o_bmap += 1 + 1 + n_param;
11541 o_ls += 1 + 1 + n_param;
11542 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
11543 o_bmap += n_before;
11544 isl_seq_cpy(bmap->div[i] + o_bmap,
11545 ls->div->row[i] + o_ls, n_set);
11546 o_bmap += n_set;
11547 o_ls += n_set;
11548 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
11549 o_bmap += n_after;
11550 isl_seq_cpy(bmap->div[i] + o_bmap,
11551 ls->div->row[i] + o_ls, n_div);
11552 o_bmap += n_div;
11553 o_ls += n_div;
11554 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
11555 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
11556 goto error;
11559 isl_local_space_free(ls);
11560 return 0;
11561 error:
11562 isl_local_space_free(ls);
11563 return -1;
11566 /* How many stride constraints does "ma" enforce?
11567 * That is, how many of the affine expressions have a denominator
11568 * different from one?
11570 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11572 int i;
11573 int strides = 0;
11575 for (i = 0; i < ma->n; ++i)
11576 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11577 strides++;
11579 return strides;
11582 /* For each affine expression in ma of the form
11584 * x_i = (f_i y + h_i)/m_i
11586 * with m_i different from one, add a constraint to "bmap"
11587 * of the form
11589 * f_i y + h_i = m_i alpha_i
11591 * with alpha_i an additional existentially quantified variable.
11593 static __isl_give isl_basic_map *add_ma_strides(
11594 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
11595 int n_before, int n_after)
11597 int i, k;
11598 int div;
11599 int total;
11600 int n_param;
11601 int n_in;
11602 int n_div;
11604 total = isl_basic_map_total_dim(bmap);
11605 n_param = isl_multi_aff_dim(ma, isl_dim_param);
11606 n_in = isl_multi_aff_dim(ma, isl_dim_in);
11607 n_div = isl_multi_aff_dim(ma, isl_dim_div);
11608 for (i = 0; i < ma->n; ++i) {
11609 int o_bmap = 0, o_ma = 1;
11611 if (isl_int_is_one(ma->p[i]->v->el[0]))
11612 continue;
11613 div = isl_basic_map_alloc_div(bmap);
11614 k = isl_basic_map_alloc_equality(bmap);
11615 if (div < 0 || k < 0)
11616 goto error;
11617 isl_int_set_si(bmap->div[div][0], 0);
11618 isl_seq_cpy(bmap->eq[k] + o_bmap,
11619 ma->p[i]->v->el + o_ma, 1 + n_param);
11620 o_bmap += 1 + n_param;
11621 o_ma += 1 + n_param;
11622 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
11623 o_bmap += n_before;
11624 isl_seq_cpy(bmap->eq[k] + o_bmap,
11625 ma->p[i]->v->el + o_ma, n_in);
11626 o_bmap += n_in;
11627 o_ma += n_in;
11628 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
11629 o_bmap += n_after;
11630 isl_seq_cpy(bmap->eq[k] + o_bmap,
11631 ma->p[i]->v->el + o_ma, n_div);
11632 o_bmap += n_div;
11633 o_ma += n_div;
11634 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
11635 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
11636 total++;
11639 return bmap;
11640 error:
11641 isl_basic_map_free(bmap);
11642 return NULL;
11645 /* Replace the domain or range space (depending on "type) of "space" by "set".
11647 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
11648 enum isl_dim_type type, __isl_take isl_space *set)
11650 if (type == isl_dim_in) {
11651 space = isl_space_range(space);
11652 space = isl_space_map_from_domain_and_range(set, space);
11653 } else {
11654 space = isl_space_domain(space);
11655 space = isl_space_map_from_domain_and_range(space, set);
11658 return space;
11661 /* Compute the preimage of the domain or range (depending on "type")
11662 * of "bmap" under the function represented by "ma".
11663 * In other words, plug in "ma" in the domain or range of "bmap".
11664 * The result is a basic map that lives in the same space as "bmap"
11665 * except that the domain or range has been replaced by
11666 * the domain space of "ma".
11668 * If bmap is represented by
11670 * A(p) + S u + B x + T v + C(divs) >= 0,
11672 * where u and x are input and output dimensions if type == isl_dim_out
11673 * while x and v are input and output dimensions if type == isl_dim_in,
11674 * and ma is represented by
11676 * x = D(p) + F(y) + G(divs')
11678 * then the result is
11680 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11682 * The divs in the input set are similarly adjusted.
11683 * In particular
11685 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11687 * becomes
11689 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11690 * B_i G(divs') + c_i(divs))/n_i)
11692 * If bmap is not a rational map and if F(y) involves any denominators
11694 * x_i = (f_i y + h_i)/m_i
11696 * then additional constraints are added to ensure that we only
11697 * map back integer points. That is we enforce
11699 * f_i y + h_i = m_i alpha_i
11701 * with alpha_i an additional existentially quantified variable.
11703 * We first copy over the divs from "ma".
11704 * Then we add the modified constraints and divs from "bmap".
11705 * Finally, we add the stride constraints, if needed.
11707 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
11708 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
11709 __isl_take isl_multi_aff *ma)
11711 int i, k;
11712 isl_space *space;
11713 isl_basic_map *res = NULL;
11714 int n_before, n_after, n_div_bmap, n_div_ma;
11715 isl_int f, c1, c2, g;
11716 int rational, strides;
11718 isl_int_init(f);
11719 isl_int_init(c1);
11720 isl_int_init(c2);
11721 isl_int_init(g);
11723 ma = isl_multi_aff_align_divs(ma);
11724 if (!bmap || !ma)
11725 goto error;
11726 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
11727 goto error;
11729 if (type == isl_dim_in) {
11730 n_before = 0;
11731 n_after = isl_basic_map_dim(bmap, isl_dim_out);
11732 } else {
11733 n_before = isl_basic_map_dim(bmap, isl_dim_in);
11734 n_after = 0;
11736 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
11737 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11739 space = isl_multi_aff_get_domain_space(ma);
11740 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
11741 rational = isl_basic_map_is_rational(bmap);
11742 strides = rational ? 0 : multi_aff_strides(ma);
11743 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
11744 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
11745 if (rational)
11746 res = isl_basic_map_set_rational(res);
11748 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
11749 if (isl_basic_map_alloc_div(res) < 0)
11750 goto error;
11752 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
11753 goto error;
11755 for (i = 0; i < bmap->n_eq; ++i) {
11756 k = isl_basic_map_alloc_equality(res);
11757 if (k < 0)
11758 goto error;
11759 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
11760 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11763 for (i = 0; i < bmap->n_ineq; ++i) {
11764 k = isl_basic_map_alloc_inequality(res);
11765 if (k < 0)
11766 goto error;
11767 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
11768 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11771 for (i = 0; i < bmap->n_div; ++i) {
11772 if (isl_int_is_zero(bmap->div[i][0])) {
11773 isl_int_set_si(res->div[n_div_ma + i][0], 0);
11774 continue;
11776 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
11777 n_before, n_after, n_div_ma, n_div_bmap,
11778 f, c1, c2, g, 1);
11781 if (strides)
11782 res = add_ma_strides(res, ma, n_before, n_after);
11784 isl_int_clear(f);
11785 isl_int_clear(c1);
11786 isl_int_clear(c2);
11787 isl_int_clear(g);
11788 isl_basic_map_free(bmap);
11789 isl_multi_aff_free(ma);
11790 res = isl_basic_set_simplify(res);
11791 return isl_basic_map_finalize(res);
11792 error:
11793 isl_int_clear(f);
11794 isl_int_clear(c1);
11795 isl_int_clear(c2);
11796 isl_int_clear(g);
11797 isl_basic_map_free(bmap);
11798 isl_multi_aff_free(ma);
11799 isl_basic_map_free(res);
11800 return NULL;
11803 /* Compute the preimage of "bset" under the function represented by "ma".
11804 * In other words, plug in "ma" in "bset". The result is a basic set
11805 * that lives in the domain space of "ma".
11807 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
11808 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
11810 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
11813 /* Check if the range of "ma" is compatible with the domain or range
11814 * (depending on "type") of "map".
11815 * Return -1 if anything is wrong.
11817 static int check_map_compatible_range_multi_aff(
11818 __isl_keep isl_map *map, enum isl_dim_type type,
11819 __isl_keep isl_multi_aff *ma)
11821 int m;
11822 isl_space *ma_space;
11824 ma_space = isl_multi_aff_get_space(ma);
11825 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
11826 isl_space_free(ma_space);
11827 if (m >= 0 && !m)
11828 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11829 "spaces don't match", return -1);
11830 return m;
11833 /* Compute the preimage of the domain or range (depending on "type")
11834 * of "map" under the function represented by "ma".
11835 * In other words, plug in "ma" in the domain or range of "map".
11836 * The result is a map that lives in the same space as "map"
11837 * except that the domain or range has been replaced by
11838 * the domain space of "ma".
11840 * The parameters are assumed to have been aligned.
11842 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
11843 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11845 int i;
11846 isl_space *space;
11848 map = isl_map_cow(map);
11849 ma = isl_multi_aff_align_divs(ma);
11850 if (!map || !ma)
11851 goto error;
11852 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
11853 goto error;
11855 for (i = 0; i < map->n; ++i) {
11856 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
11857 isl_multi_aff_copy(ma));
11858 if (!map->p[i])
11859 goto error;
11862 space = isl_multi_aff_get_domain_space(ma);
11863 space = isl_space_set(isl_map_get_space(map), type, space);
11865 isl_space_free(map->dim);
11866 map->dim = space;
11867 if (!map->dim)
11868 goto error;
11870 isl_multi_aff_free(ma);
11871 if (map->n > 1)
11872 ISL_F_CLR(map, ISL_MAP_DISJOINT);
11873 ISL_F_CLR(map, ISL_SET_NORMALIZED);
11874 return map;
11875 error:
11876 isl_multi_aff_free(ma);
11877 isl_map_free(map);
11878 return NULL;
11881 /* Compute the preimage of the domain or range (depending on "type")
11882 * of "map" under the function represented by "ma".
11883 * In other words, plug in "ma" in the domain or range of "map".
11884 * The result is a map that lives in the same space as "map"
11885 * except that the domain or range has been replaced by
11886 * the domain space of "ma".
11888 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
11889 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11891 if (!map || !ma)
11892 goto error;
11894 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
11895 return map_preimage_multi_aff(map, type, ma);
11897 if (!isl_space_has_named_params(map->dim) ||
11898 !isl_space_has_named_params(ma->space))
11899 isl_die(map->ctx, isl_error_invalid,
11900 "unaligned unnamed parameters", goto error);
11901 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
11902 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
11904 return map_preimage_multi_aff(map, type, ma);
11905 error:
11906 isl_multi_aff_free(ma);
11907 return isl_map_free(map);
11910 /* Compute the preimage of "set" under the function represented by "ma".
11911 * In other words, plug in "ma" "set". The result is a set
11912 * that lives in the domain space of "ma".
11914 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
11915 __isl_take isl_multi_aff *ma)
11917 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
11920 /* Compute the preimage of the domain of "map" under the function
11921 * represented by "ma".
11922 * In other words, plug in "ma" in the domain of "map".
11923 * The result is a map that lives in the same space as "map"
11924 * except that the domain has been replaced by the domain space of "ma".
11926 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
11927 __isl_take isl_multi_aff *ma)
11929 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
11932 /* Compute the preimage of "set" under the function represented by "pma".
11933 * In other words, plug in "pma" in "set. The result is a set
11934 * that lives in the domain space of "pma".
11936 static __isl_give isl_set *set_preimage_pw_multi_aff(__isl_take isl_set *set,
11937 __isl_take isl_pw_multi_aff *pma)
11939 int i;
11940 isl_set *res;
11942 if (!pma)
11943 goto error;
11945 if (pma->n == 0) {
11946 isl_pw_multi_aff_free(pma);
11947 res = isl_set_empty(isl_set_get_space(set));
11948 isl_set_free(set);
11949 return res;
11952 res = isl_set_preimage_multi_aff(isl_set_copy(set),
11953 isl_multi_aff_copy(pma->p[0].maff));
11954 res = isl_set_intersect(res, isl_set_copy(pma->p[0].set));
11956 for (i = 1; i < pma->n; ++i) {
11957 isl_set *res_i;
11959 res_i = isl_set_preimage_multi_aff(isl_set_copy(set),
11960 isl_multi_aff_copy(pma->p[i].maff));
11961 res_i = isl_set_intersect(res_i, isl_set_copy(pma->p[i].set));
11962 res = isl_set_union(res, res_i);
11965 isl_pw_multi_aff_free(pma);
11966 isl_set_free(set);
11967 return res;
11968 error:
11969 isl_pw_multi_aff_free(pma);
11970 isl_set_free(set);
11971 return NULL;
11974 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
11975 __isl_take isl_pw_multi_aff *pma)
11977 if (!set || !pma)
11978 goto error;
11980 if (isl_space_match(set->dim, isl_dim_param, pma->dim, isl_dim_param))
11981 return set_preimage_pw_multi_aff(set, pma);
11983 if (!isl_space_has_named_params(set->dim) ||
11984 !isl_space_has_named_params(pma->dim))
11985 isl_die(set->ctx, isl_error_invalid,
11986 "unaligned unnamed parameters", goto error);
11987 set = isl_set_align_params(set, isl_pw_multi_aff_get_space(pma));
11988 pma = isl_pw_multi_aff_align_params(pma, isl_set_get_space(set));
11990 return set_preimage_pw_multi_aff(set, pma);
11991 error:
11992 isl_pw_multi_aff_free(pma);
11993 return isl_set_free(set);