isl_multi_aff_{add,sub}: align parameters
[isl.git] / isl_map.c
blob2697f23281fe9ff604064e18e50a8786b6290b24
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 ((n_ineq + n_eq) && !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;
3544 if (!isl_space_match(bmap1->dim, isl_dim_param,
3545 bmap2->dim, isl_dim_param))
3546 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3547 "parameters don't match", goto error);
3548 if (!isl_space_tuple_match(bmap1->dim, isl_dim_out,
3549 bmap2->dim, isl_dim_in))
3550 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3551 "spaces don't match", goto error);
3553 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3554 isl_space_copy(bmap2->dim));
3556 n_in = isl_basic_map_n_in(bmap1);
3557 n_out = isl_basic_map_n_out(bmap2);
3558 n = isl_basic_map_n_out(bmap1);
3559 nparam = isl_basic_map_n_param(bmap1);
3561 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3562 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3563 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3564 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3565 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3566 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3567 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3568 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3569 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3570 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3571 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3573 bmap = isl_basic_map_alloc_space(dim_result,
3574 bmap1->n_div + bmap2->n_div + n,
3575 bmap1->n_eq + bmap2->n_eq,
3576 bmap1->n_ineq + bmap2->n_ineq);
3577 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3578 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3579 bmap = add_divs(bmap, n);
3580 bmap = isl_basic_map_simplify(bmap);
3581 bmap = isl_basic_map_drop_redundant_divs(bmap);
3582 return isl_basic_map_finalize(bmap);
3583 error:
3584 isl_basic_map_free(bmap1);
3585 isl_basic_map_free(bmap2);
3586 return NULL;
3589 struct isl_basic_set *isl_basic_set_apply(
3590 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3592 if (!bset || !bmap)
3593 goto error;
3595 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3596 goto error);
3598 return (struct isl_basic_set *)
3599 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3600 error:
3601 isl_basic_set_free(bset);
3602 isl_basic_map_free(bmap);
3603 return NULL;
3606 struct isl_basic_map *isl_basic_map_apply_domain(
3607 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3609 if (!bmap1 || !bmap2)
3610 goto error;
3612 isl_assert(bmap1->ctx,
3613 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3614 isl_assert(bmap1->ctx,
3615 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3616 goto error);
3618 bmap1 = isl_basic_map_reverse(bmap1);
3619 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3620 return isl_basic_map_reverse(bmap1);
3621 error:
3622 isl_basic_map_free(bmap1);
3623 isl_basic_map_free(bmap2);
3624 return NULL;
3627 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3628 * A \cap B -> f(A) + f(B)
3630 struct isl_basic_map *isl_basic_map_sum(
3631 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3633 unsigned n_in, n_out, nparam, total, pos;
3634 struct isl_basic_map *bmap = NULL;
3635 struct isl_dim_map *dim_map1, *dim_map2;
3636 int i;
3638 if (!bmap1 || !bmap2)
3639 goto error;
3641 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3642 goto error);
3644 nparam = isl_basic_map_n_param(bmap1);
3645 n_in = isl_basic_map_n_in(bmap1);
3646 n_out = isl_basic_map_n_out(bmap1);
3648 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3649 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3650 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3651 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3652 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3653 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3654 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3655 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3656 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3657 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3658 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3660 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3661 bmap1->n_div + bmap2->n_div + 2 * n_out,
3662 bmap1->n_eq + bmap2->n_eq + n_out,
3663 bmap1->n_ineq + bmap2->n_ineq);
3664 for (i = 0; i < n_out; ++i) {
3665 int j = isl_basic_map_alloc_equality(bmap);
3666 if (j < 0)
3667 goto error;
3668 isl_seq_clr(bmap->eq[j], 1+total);
3669 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3670 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3671 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3673 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3674 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3675 bmap = add_divs(bmap, 2 * n_out);
3677 bmap = isl_basic_map_simplify(bmap);
3678 return isl_basic_map_finalize(bmap);
3679 error:
3680 isl_basic_map_free(bmap);
3681 isl_basic_map_free(bmap1);
3682 isl_basic_map_free(bmap2);
3683 return NULL;
3686 /* Given two maps A -> f(A) and B -> g(B), construct a map
3687 * A \cap B -> f(A) + f(B)
3689 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3691 struct isl_map *result;
3692 int i, j;
3694 if (!map1 || !map2)
3695 goto error;
3697 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3699 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3700 map1->n * map2->n, 0);
3701 if (!result)
3702 goto error;
3703 for (i = 0; i < map1->n; ++i)
3704 for (j = 0; j < map2->n; ++j) {
3705 struct isl_basic_map *part;
3706 part = isl_basic_map_sum(
3707 isl_basic_map_copy(map1->p[i]),
3708 isl_basic_map_copy(map2->p[j]));
3709 if (isl_basic_map_is_empty(part))
3710 isl_basic_map_free(part);
3711 else
3712 result = isl_map_add_basic_map(result, part);
3713 if (!result)
3714 goto error;
3716 isl_map_free(map1);
3717 isl_map_free(map2);
3718 return result;
3719 error:
3720 isl_map_free(map1);
3721 isl_map_free(map2);
3722 return NULL;
3725 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3726 __isl_take isl_set *set2)
3728 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3731 /* Given a basic map A -> f(A), construct A -> -f(A).
3733 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3735 int i, j;
3736 unsigned off, n;
3738 bmap = isl_basic_map_cow(bmap);
3739 if (!bmap)
3740 return NULL;
3742 n = isl_basic_map_dim(bmap, isl_dim_out);
3743 off = isl_basic_map_offset(bmap, isl_dim_out);
3744 for (i = 0; i < bmap->n_eq; ++i)
3745 for (j = 0; j < n; ++j)
3746 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3747 for (i = 0; i < bmap->n_ineq; ++i)
3748 for (j = 0; j < n; ++j)
3749 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3750 for (i = 0; i < bmap->n_div; ++i)
3751 for (j = 0; j < n; ++j)
3752 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3753 bmap = isl_basic_map_gauss(bmap, NULL);
3754 return isl_basic_map_finalize(bmap);
3757 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3759 return isl_basic_map_neg(bset);
3762 /* Given a map A -> f(A), construct A -> -f(A).
3764 struct isl_map *isl_map_neg(struct isl_map *map)
3766 int i;
3768 map = isl_map_cow(map);
3769 if (!map)
3770 return NULL;
3772 for (i = 0; i < map->n; ++i) {
3773 map->p[i] = isl_basic_map_neg(map->p[i]);
3774 if (!map->p[i])
3775 goto error;
3778 return map;
3779 error:
3780 isl_map_free(map);
3781 return NULL;
3784 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3786 return (isl_set *)isl_map_neg((isl_map *)set);
3789 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3790 * A -> floor(f(A)/d).
3792 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3793 isl_int d)
3795 unsigned n_in, n_out, nparam, total, pos;
3796 struct isl_basic_map *result = NULL;
3797 struct isl_dim_map *dim_map;
3798 int i;
3800 if (!bmap)
3801 return NULL;
3803 nparam = isl_basic_map_n_param(bmap);
3804 n_in = isl_basic_map_n_in(bmap);
3805 n_out = isl_basic_map_n_out(bmap);
3807 total = nparam + n_in + n_out + bmap->n_div + n_out;
3808 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3809 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3810 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3811 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3812 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3814 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3815 bmap->n_div + n_out,
3816 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3817 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3818 result = add_divs(result, n_out);
3819 for (i = 0; i < n_out; ++i) {
3820 int j;
3821 j = isl_basic_map_alloc_inequality(result);
3822 if (j < 0)
3823 goto error;
3824 isl_seq_clr(result->ineq[j], 1+total);
3825 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3826 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3827 j = isl_basic_map_alloc_inequality(result);
3828 if (j < 0)
3829 goto error;
3830 isl_seq_clr(result->ineq[j], 1+total);
3831 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3832 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3833 isl_int_sub_ui(result->ineq[j][0], d, 1);
3836 result = isl_basic_map_simplify(result);
3837 return isl_basic_map_finalize(result);
3838 error:
3839 isl_basic_map_free(result);
3840 return NULL;
3843 /* Given a map A -> f(A) and an integer d, construct a map
3844 * A -> floor(f(A)/d).
3846 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3848 int i;
3850 map = isl_map_cow(map);
3851 if (!map)
3852 return NULL;
3854 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3855 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3856 for (i = 0; i < map->n; ++i) {
3857 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3858 if (!map->p[i])
3859 goto error;
3862 return map;
3863 error:
3864 isl_map_free(map);
3865 return NULL;
3868 /* Given a map A -> f(A) and an integer d, construct a map
3869 * A -> floor(f(A)/d).
3871 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3872 __isl_take isl_val *d)
3874 if (!map || !d)
3875 goto error;
3876 if (!isl_val_is_int(d))
3877 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3878 "expecting integer denominator", goto error);
3879 map = isl_map_floordiv(map, d->n);
3880 isl_val_free(d);
3881 return map;
3882 error:
3883 isl_map_free(map);
3884 isl_val_free(d);
3885 return NULL;
3888 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3890 int i;
3891 unsigned nparam;
3892 unsigned n_in;
3894 i = isl_basic_map_alloc_equality(bmap);
3895 if (i < 0)
3896 goto error;
3897 nparam = isl_basic_map_n_param(bmap);
3898 n_in = isl_basic_map_n_in(bmap);
3899 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3900 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3901 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3902 return isl_basic_map_finalize(bmap);
3903 error:
3904 isl_basic_map_free(bmap);
3905 return NULL;
3908 /* Add a constraints to "bmap" expressing i_pos < o_pos
3910 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3912 int i;
3913 unsigned nparam;
3914 unsigned n_in;
3916 i = isl_basic_map_alloc_inequality(bmap);
3917 if (i < 0)
3918 goto error;
3919 nparam = isl_basic_map_n_param(bmap);
3920 n_in = isl_basic_map_n_in(bmap);
3921 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3922 isl_int_set_si(bmap->ineq[i][0], -1);
3923 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3924 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3925 return isl_basic_map_finalize(bmap);
3926 error:
3927 isl_basic_map_free(bmap);
3928 return NULL;
3931 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3933 static __isl_give isl_basic_map *var_less_or_equal(
3934 __isl_take isl_basic_map *bmap, unsigned pos)
3936 int i;
3937 unsigned nparam;
3938 unsigned n_in;
3940 i = isl_basic_map_alloc_inequality(bmap);
3941 if (i < 0)
3942 goto error;
3943 nparam = isl_basic_map_n_param(bmap);
3944 n_in = isl_basic_map_n_in(bmap);
3945 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3946 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3947 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3948 return isl_basic_map_finalize(bmap);
3949 error:
3950 isl_basic_map_free(bmap);
3951 return NULL;
3954 /* Add a constraints to "bmap" expressing i_pos > o_pos
3956 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3958 int i;
3959 unsigned nparam;
3960 unsigned n_in;
3962 i = isl_basic_map_alloc_inequality(bmap);
3963 if (i < 0)
3964 goto error;
3965 nparam = isl_basic_map_n_param(bmap);
3966 n_in = isl_basic_map_n_in(bmap);
3967 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3968 isl_int_set_si(bmap->ineq[i][0], -1);
3969 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3970 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3971 return isl_basic_map_finalize(bmap);
3972 error:
3973 isl_basic_map_free(bmap);
3974 return NULL;
3977 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3979 static __isl_give isl_basic_map *var_more_or_equal(
3980 __isl_take isl_basic_map *bmap, unsigned pos)
3982 int i;
3983 unsigned nparam;
3984 unsigned n_in;
3986 i = isl_basic_map_alloc_inequality(bmap);
3987 if (i < 0)
3988 goto error;
3989 nparam = isl_basic_map_n_param(bmap);
3990 n_in = isl_basic_map_n_in(bmap);
3991 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3992 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3993 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3994 return isl_basic_map_finalize(bmap);
3995 error:
3996 isl_basic_map_free(bmap);
3997 return NULL;
4000 __isl_give isl_basic_map *isl_basic_map_equal(
4001 __isl_take isl_space *dim, unsigned n_equal)
4003 int i;
4004 struct isl_basic_map *bmap;
4005 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4006 if (!bmap)
4007 return NULL;
4008 for (i = 0; i < n_equal && bmap; ++i)
4009 bmap = var_equal(bmap, i);
4010 return isl_basic_map_finalize(bmap);
4013 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4015 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4016 unsigned pos)
4018 int i;
4019 struct isl_basic_map *bmap;
4020 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4021 if (!bmap)
4022 return NULL;
4023 for (i = 0; i < pos && bmap; ++i)
4024 bmap = var_equal(bmap, i);
4025 if (bmap)
4026 bmap = var_less(bmap, pos);
4027 return isl_basic_map_finalize(bmap);
4030 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4032 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4033 __isl_take isl_space *dim, unsigned pos)
4035 int i;
4036 isl_basic_map *bmap;
4038 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4039 for (i = 0; i < pos; ++i)
4040 bmap = var_equal(bmap, i);
4041 bmap = var_less_or_equal(bmap, pos);
4042 return isl_basic_map_finalize(bmap);
4045 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4047 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4048 unsigned pos)
4050 int i;
4051 struct isl_basic_map *bmap;
4052 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4053 if (!bmap)
4054 return NULL;
4055 for (i = 0; i < pos && bmap; ++i)
4056 bmap = var_equal(bmap, i);
4057 if (bmap)
4058 bmap = var_more(bmap, pos);
4059 return isl_basic_map_finalize(bmap);
4062 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4064 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4065 __isl_take isl_space *dim, unsigned pos)
4067 int i;
4068 isl_basic_map *bmap;
4070 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4071 for (i = 0; i < pos; ++i)
4072 bmap = var_equal(bmap, i);
4073 bmap = var_more_or_equal(bmap, pos);
4074 return isl_basic_map_finalize(bmap);
4077 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4078 unsigned n, int equal)
4080 struct isl_map *map;
4081 int i;
4083 if (n == 0 && equal)
4084 return isl_map_universe(dims);
4086 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4088 for (i = 0; i + 1 < n; ++i)
4089 map = isl_map_add_basic_map(map,
4090 isl_basic_map_less_at(isl_space_copy(dims), i));
4091 if (n > 0) {
4092 if (equal)
4093 map = isl_map_add_basic_map(map,
4094 isl_basic_map_less_or_equal_at(dims, n - 1));
4095 else
4096 map = isl_map_add_basic_map(map,
4097 isl_basic_map_less_at(dims, n - 1));
4098 } else
4099 isl_space_free(dims);
4101 return map;
4104 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4106 if (!dims)
4107 return NULL;
4108 return map_lex_lte_first(dims, dims->n_out, equal);
4111 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4113 return map_lex_lte_first(dim, n, 0);
4116 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4118 return map_lex_lte_first(dim, n, 1);
4121 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4123 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4126 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4128 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4131 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4132 unsigned n, int equal)
4134 struct isl_map *map;
4135 int i;
4137 if (n == 0 && equal)
4138 return isl_map_universe(dims);
4140 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4142 for (i = 0; i + 1 < n; ++i)
4143 map = isl_map_add_basic_map(map,
4144 isl_basic_map_more_at(isl_space_copy(dims), i));
4145 if (n > 0) {
4146 if (equal)
4147 map = isl_map_add_basic_map(map,
4148 isl_basic_map_more_or_equal_at(dims, n - 1));
4149 else
4150 map = isl_map_add_basic_map(map,
4151 isl_basic_map_more_at(dims, n - 1));
4152 } else
4153 isl_space_free(dims);
4155 return map;
4158 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4160 if (!dims)
4161 return NULL;
4162 return map_lex_gte_first(dims, dims->n_out, equal);
4165 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4167 return map_lex_gte_first(dim, n, 0);
4170 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4172 return map_lex_gte_first(dim, n, 1);
4175 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4177 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4180 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4182 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4185 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4186 __isl_take isl_set *set2)
4188 isl_map *map;
4189 map = isl_map_lex_le(isl_set_get_space(set1));
4190 map = isl_map_intersect_domain(map, set1);
4191 map = isl_map_intersect_range(map, set2);
4192 return map;
4195 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4196 __isl_take isl_set *set2)
4198 isl_map *map;
4199 map = isl_map_lex_lt(isl_set_get_space(set1));
4200 map = isl_map_intersect_domain(map, set1);
4201 map = isl_map_intersect_range(map, set2);
4202 return map;
4205 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4206 __isl_take isl_set *set2)
4208 isl_map *map;
4209 map = isl_map_lex_ge(isl_set_get_space(set1));
4210 map = isl_map_intersect_domain(map, set1);
4211 map = isl_map_intersect_range(map, set2);
4212 return map;
4215 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4216 __isl_take isl_set *set2)
4218 isl_map *map;
4219 map = isl_map_lex_gt(isl_set_get_space(set1));
4220 map = isl_map_intersect_domain(map, set1);
4221 map = isl_map_intersect_range(map, set2);
4222 return map;
4225 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4226 __isl_take isl_map *map2)
4228 isl_map *map;
4229 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4230 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4231 map = isl_map_apply_range(map, isl_map_reverse(map2));
4232 return map;
4235 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4236 __isl_take isl_map *map2)
4238 isl_map *map;
4239 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4240 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4241 map = isl_map_apply_range(map, isl_map_reverse(map2));
4242 return map;
4245 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4246 __isl_take isl_map *map2)
4248 isl_map *map;
4249 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4250 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4251 map = isl_map_apply_range(map, isl_map_reverse(map2));
4252 return map;
4255 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4256 __isl_take isl_map *map2)
4258 isl_map *map;
4259 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4260 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4261 map = isl_map_apply_range(map, isl_map_reverse(map2));
4262 return map;
4265 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4266 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4268 struct isl_basic_map *bmap;
4270 bset = isl_basic_set_cow(bset);
4271 if (!bset || !dim)
4272 goto error;
4274 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4275 isl_space_free(bset->dim);
4276 bmap = (struct isl_basic_map *) bset;
4277 bmap->dim = dim;
4278 return isl_basic_map_finalize(bmap);
4279 error:
4280 isl_basic_set_free(bset);
4281 isl_space_free(dim);
4282 return NULL;
4285 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4287 if (!bmap)
4288 goto error;
4289 if (bmap->dim->n_in == 0)
4290 return (struct isl_basic_set *)bmap;
4291 bmap = isl_basic_map_cow(bmap);
4292 if (!bmap)
4293 goto error;
4294 bmap->dim = isl_space_as_set_space(bmap->dim);
4295 if (!bmap->dim)
4296 goto error;
4297 bmap = isl_basic_map_finalize(bmap);
4298 return (struct isl_basic_set *)bmap;
4299 error:
4300 isl_basic_map_free(bmap);
4301 return NULL;
4304 /* For a div d = floor(f/m), add the constraints
4306 * f - m d >= 0
4307 * -(f-(n-1)) + m d >= 0
4309 * Note that the second constraint is the negation of
4311 * f - m d >= n
4313 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4314 unsigned pos, isl_int *div)
4316 int i, j;
4317 unsigned total = isl_basic_map_total_dim(bmap);
4319 i = isl_basic_map_alloc_inequality(bmap);
4320 if (i < 0)
4321 return -1;
4322 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4323 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4325 j = isl_basic_map_alloc_inequality(bmap);
4326 if (j < 0)
4327 return -1;
4328 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4329 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4330 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4331 return j;
4334 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4335 unsigned pos, isl_int *div)
4337 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4338 pos, div);
4341 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4343 unsigned total = isl_basic_map_total_dim(bmap);
4344 unsigned div_pos = total - bmap->n_div + div;
4346 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4347 bmap->div[div]);
4350 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4352 return isl_basic_map_add_div_constraints(bset, div);
4355 struct isl_basic_set *isl_basic_map_underlying_set(
4356 struct isl_basic_map *bmap)
4358 if (!bmap)
4359 goto error;
4360 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4361 bmap->n_div == 0 &&
4362 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4363 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4364 return (struct isl_basic_set *)bmap;
4365 bmap = isl_basic_map_cow(bmap);
4366 if (!bmap)
4367 goto error;
4368 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4369 if (!bmap->dim)
4370 goto error;
4371 bmap->extra -= bmap->n_div;
4372 bmap->n_div = 0;
4373 bmap = isl_basic_map_finalize(bmap);
4374 return (struct isl_basic_set *)bmap;
4375 error:
4376 isl_basic_map_free(bmap);
4377 return NULL;
4380 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4381 __isl_take isl_basic_set *bset)
4383 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4386 struct isl_basic_map *isl_basic_map_overlying_set(
4387 struct isl_basic_set *bset, struct isl_basic_map *like)
4389 struct isl_basic_map *bmap;
4390 struct isl_ctx *ctx;
4391 unsigned total;
4392 int i;
4394 if (!bset || !like)
4395 goto error;
4396 ctx = bset->ctx;
4397 isl_assert(ctx, bset->n_div == 0, goto error);
4398 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4399 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4400 goto error);
4401 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4402 isl_basic_map_free(like);
4403 return (struct isl_basic_map *)bset;
4405 bset = isl_basic_set_cow(bset);
4406 if (!bset)
4407 goto error;
4408 total = bset->dim->n_out + bset->extra;
4409 bmap = (struct isl_basic_map *)bset;
4410 isl_space_free(bmap->dim);
4411 bmap->dim = isl_space_copy(like->dim);
4412 if (!bmap->dim)
4413 goto error;
4414 bmap->n_div = like->n_div;
4415 bmap->extra += like->n_div;
4416 if (bmap->extra) {
4417 unsigned ltotal;
4418 isl_int **div;
4419 ltotal = total - bmap->extra + like->extra;
4420 if (ltotal > total)
4421 ltotal = total;
4422 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4423 bmap->extra * (1 + 1 + total));
4424 if (isl_blk_is_error(bmap->block2))
4425 goto error;
4426 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4427 if (!div)
4428 goto error;
4429 bmap->div = div;
4430 for (i = 0; i < bmap->extra; ++i)
4431 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4432 for (i = 0; i < like->n_div; ++i) {
4433 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4434 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4436 bmap = isl_basic_map_extend_constraints(bmap,
4437 0, 2 * like->n_div);
4438 for (i = 0; i < like->n_div; ++i) {
4439 if (!bmap)
4440 break;
4441 if (isl_int_is_zero(bmap->div[i][0]))
4442 continue;
4443 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4444 bmap = isl_basic_map_free(bmap);
4447 isl_basic_map_free(like);
4448 bmap = isl_basic_map_simplify(bmap);
4449 bmap = isl_basic_map_finalize(bmap);
4450 return bmap;
4451 error:
4452 isl_basic_map_free(like);
4453 isl_basic_set_free(bset);
4454 return NULL;
4457 struct isl_basic_set *isl_basic_set_from_underlying_set(
4458 struct isl_basic_set *bset, struct isl_basic_set *like)
4460 return (struct isl_basic_set *)
4461 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4464 struct isl_set *isl_set_from_underlying_set(
4465 struct isl_set *set, struct isl_basic_set *like)
4467 int i;
4469 if (!set || !like)
4470 goto error;
4471 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4472 goto error);
4473 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4474 isl_basic_set_free(like);
4475 return set;
4477 set = isl_set_cow(set);
4478 if (!set)
4479 goto error;
4480 for (i = 0; i < set->n; ++i) {
4481 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4482 isl_basic_set_copy(like));
4483 if (!set->p[i])
4484 goto error;
4486 isl_space_free(set->dim);
4487 set->dim = isl_space_copy(like->dim);
4488 if (!set->dim)
4489 goto error;
4490 isl_basic_set_free(like);
4491 return set;
4492 error:
4493 isl_basic_set_free(like);
4494 isl_set_free(set);
4495 return NULL;
4498 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4500 int i;
4502 map = isl_map_cow(map);
4503 if (!map)
4504 return NULL;
4505 map->dim = isl_space_cow(map->dim);
4506 if (!map->dim)
4507 goto error;
4509 for (i = 1; i < map->n; ++i)
4510 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4511 goto error);
4512 for (i = 0; i < map->n; ++i) {
4513 map->p[i] = (struct isl_basic_map *)
4514 isl_basic_map_underlying_set(map->p[i]);
4515 if (!map->p[i])
4516 goto error;
4518 if (map->n == 0)
4519 map->dim = isl_space_underlying(map->dim, 0);
4520 else {
4521 isl_space_free(map->dim);
4522 map->dim = isl_space_copy(map->p[0]->dim);
4524 if (!map->dim)
4525 goto error;
4526 return (struct isl_set *)map;
4527 error:
4528 isl_map_free(map);
4529 return NULL;
4532 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4534 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4537 __isl_give isl_basic_map *isl_basic_map_reset_space(
4538 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4540 bmap = isl_basic_map_cow(bmap);
4541 if (!bmap || !dim)
4542 goto error;
4544 isl_space_free(bmap->dim);
4545 bmap->dim = dim;
4547 bmap = isl_basic_map_finalize(bmap);
4549 return bmap;
4550 error:
4551 isl_basic_map_free(bmap);
4552 isl_space_free(dim);
4553 return NULL;
4556 __isl_give isl_basic_set *isl_basic_set_reset_space(
4557 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4559 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4560 dim);
4563 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4564 __isl_take isl_space *dim)
4566 int i;
4568 map = isl_map_cow(map);
4569 if (!map || !dim)
4570 goto error;
4572 for (i = 0; i < map->n; ++i) {
4573 map->p[i] = isl_basic_map_reset_space(map->p[i],
4574 isl_space_copy(dim));
4575 if (!map->p[i])
4576 goto error;
4578 isl_space_free(map->dim);
4579 map->dim = dim;
4581 return map;
4582 error:
4583 isl_map_free(map);
4584 isl_space_free(dim);
4585 return NULL;
4588 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4589 __isl_take isl_space *dim)
4591 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4594 /* Compute the parameter domain of the given basic set.
4596 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4598 isl_space *space;
4599 unsigned n;
4601 if (isl_basic_set_is_params(bset))
4602 return bset;
4604 n = isl_basic_set_dim(bset, isl_dim_set);
4605 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4606 space = isl_basic_set_get_space(bset);
4607 space = isl_space_params(space);
4608 bset = isl_basic_set_reset_space(bset, space);
4609 return bset;
4612 /* Construct a zero-dimensional basic set with the given parameter domain.
4614 __isl_give isl_basic_set *isl_basic_set_from_params(
4615 __isl_take isl_basic_set *bset)
4617 isl_space *space;
4618 space = isl_basic_set_get_space(bset);
4619 space = isl_space_set_from_params(space);
4620 bset = isl_basic_set_reset_space(bset, space);
4621 return bset;
4624 /* Compute the parameter domain of the given set.
4626 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4628 isl_space *space;
4629 unsigned n;
4631 if (isl_set_is_params(set))
4632 return set;
4634 n = isl_set_dim(set, isl_dim_set);
4635 set = isl_set_project_out(set, isl_dim_set, 0, n);
4636 space = isl_set_get_space(set);
4637 space = isl_space_params(space);
4638 set = isl_set_reset_space(set, space);
4639 return set;
4642 /* Construct a zero-dimensional set with the given parameter domain.
4644 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4646 isl_space *space;
4647 space = isl_set_get_space(set);
4648 space = isl_space_set_from_params(space);
4649 set = isl_set_reset_space(set, space);
4650 return set;
4653 /* Compute the parameter domain of the given map.
4655 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4657 isl_space *space;
4658 unsigned n;
4660 n = isl_map_dim(map, isl_dim_in);
4661 map = isl_map_project_out(map, isl_dim_in, 0, n);
4662 n = isl_map_dim(map, isl_dim_out);
4663 map = isl_map_project_out(map, isl_dim_out, 0, n);
4664 space = isl_map_get_space(map);
4665 space = isl_space_params(space);
4666 map = isl_map_reset_space(map, space);
4667 return map;
4670 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4672 isl_space *dim;
4673 struct isl_basic_set *domain;
4674 unsigned n_in;
4675 unsigned n_out;
4677 if (!bmap)
4678 return NULL;
4679 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4681 n_in = isl_basic_map_n_in(bmap);
4682 n_out = isl_basic_map_n_out(bmap);
4683 domain = isl_basic_set_from_basic_map(bmap);
4684 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4686 domain = isl_basic_set_reset_space(domain, dim);
4688 return domain;
4691 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4693 if (!bmap)
4694 return -1;
4695 return isl_space_may_be_set(bmap->dim);
4698 /* Is this basic map actually a set?
4699 * Users should never call this function. Outside of isl,
4700 * the type should indicate whether something is a set or a map.
4702 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4704 if (!bmap)
4705 return -1;
4706 return isl_space_is_set(bmap->dim);
4709 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4711 if (!bmap)
4712 return NULL;
4713 if (isl_basic_map_is_set(bmap))
4714 return bmap;
4715 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4718 __isl_give isl_basic_map *isl_basic_map_domain_map(
4719 __isl_take isl_basic_map *bmap)
4721 int i, k;
4722 isl_space *dim;
4723 isl_basic_map *domain;
4724 int nparam, n_in, n_out;
4725 unsigned total;
4727 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4728 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4729 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4731 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4732 domain = isl_basic_map_universe(dim);
4734 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4735 bmap = isl_basic_map_apply_range(bmap, domain);
4736 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4738 total = isl_basic_map_total_dim(bmap);
4740 for (i = 0; i < n_in; ++i) {
4741 k = isl_basic_map_alloc_equality(bmap);
4742 if (k < 0)
4743 goto error;
4744 isl_seq_clr(bmap->eq[k], 1 + total);
4745 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4746 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4749 bmap = isl_basic_map_gauss(bmap, NULL);
4750 return isl_basic_map_finalize(bmap);
4751 error:
4752 isl_basic_map_free(bmap);
4753 return NULL;
4756 __isl_give isl_basic_map *isl_basic_map_range_map(
4757 __isl_take isl_basic_map *bmap)
4759 int i, k;
4760 isl_space *dim;
4761 isl_basic_map *range;
4762 int nparam, n_in, n_out;
4763 unsigned total;
4765 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4766 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4767 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4769 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4770 range = isl_basic_map_universe(dim);
4772 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4773 bmap = isl_basic_map_apply_range(bmap, range);
4774 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4776 total = isl_basic_map_total_dim(bmap);
4778 for (i = 0; i < n_out; ++i) {
4779 k = isl_basic_map_alloc_equality(bmap);
4780 if (k < 0)
4781 goto error;
4782 isl_seq_clr(bmap->eq[k], 1 + total);
4783 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4784 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4787 bmap = isl_basic_map_gauss(bmap, NULL);
4788 return isl_basic_map_finalize(bmap);
4789 error:
4790 isl_basic_map_free(bmap);
4791 return NULL;
4794 int isl_map_may_be_set(__isl_keep isl_map *map)
4796 if (!map)
4797 return -1;
4798 return isl_space_may_be_set(map->dim);
4801 /* Is this map actually a set?
4802 * Users should never call this function. Outside of isl,
4803 * the type should indicate whether something is a set or a map.
4805 int isl_map_is_set(__isl_keep isl_map *map)
4807 if (!map)
4808 return -1;
4809 return isl_space_is_set(map->dim);
4812 struct isl_set *isl_map_range(struct isl_map *map)
4814 int i;
4815 struct isl_set *set;
4817 if (!map)
4818 goto error;
4819 if (isl_map_is_set(map))
4820 return (isl_set *)map;
4822 map = isl_map_cow(map);
4823 if (!map)
4824 goto error;
4826 set = (struct isl_set *) map;
4827 set->dim = isl_space_range(set->dim);
4828 if (!set->dim)
4829 goto error;
4830 for (i = 0; i < map->n; ++i) {
4831 set->p[i] = isl_basic_map_range(map->p[i]);
4832 if (!set->p[i])
4833 goto error;
4835 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4836 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4837 return set;
4838 error:
4839 isl_map_free(map);
4840 return NULL;
4843 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4845 int i;
4846 isl_space *domain_dim;
4848 map = isl_map_cow(map);
4849 if (!map)
4850 return NULL;
4852 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4853 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4854 map->dim = isl_space_join(map->dim, domain_dim);
4855 if (!map->dim)
4856 goto error;
4857 for (i = 0; i < map->n; ++i) {
4858 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4859 if (!map->p[i])
4860 goto error;
4862 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4863 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4864 return map;
4865 error:
4866 isl_map_free(map);
4867 return NULL;
4870 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4872 int i;
4873 isl_space *range_dim;
4875 map = isl_map_cow(map);
4876 if (!map)
4877 return NULL;
4879 range_dim = isl_space_range(isl_map_get_space(map));
4880 range_dim = isl_space_from_range(range_dim);
4881 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4882 map->dim = isl_space_join(map->dim, range_dim);
4883 if (!map->dim)
4884 goto error;
4885 for (i = 0; i < map->n; ++i) {
4886 map->p[i] = isl_basic_map_range_map(map->p[i]);
4887 if (!map->p[i])
4888 goto error;
4890 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4891 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4892 return map;
4893 error:
4894 isl_map_free(map);
4895 return NULL;
4898 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4899 __isl_take isl_space *dim)
4901 int i;
4902 struct isl_map *map = NULL;
4904 set = isl_set_cow(set);
4905 if (!set || !dim)
4906 goto error;
4907 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4908 map = (struct isl_map *)set;
4909 for (i = 0; i < set->n; ++i) {
4910 map->p[i] = isl_basic_map_from_basic_set(
4911 set->p[i], isl_space_copy(dim));
4912 if (!map->p[i])
4913 goto error;
4915 isl_space_free(map->dim);
4916 map->dim = dim;
4917 return map;
4918 error:
4919 isl_space_free(dim);
4920 isl_set_free(set);
4921 return NULL;
4924 __isl_give isl_basic_map *isl_basic_map_from_domain(
4925 __isl_take isl_basic_set *bset)
4927 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4930 __isl_give isl_basic_map *isl_basic_map_from_range(
4931 __isl_take isl_basic_set *bset)
4933 isl_space *space;
4934 space = isl_basic_set_get_space(bset);
4935 space = isl_space_from_range(space);
4936 bset = isl_basic_set_reset_space(bset, space);
4937 return (isl_basic_map *)bset;
4940 struct isl_map *isl_map_from_range(struct isl_set *set)
4942 isl_space *space;
4943 space = isl_set_get_space(set);
4944 space = isl_space_from_range(space);
4945 set = isl_set_reset_space(set, space);
4946 return (struct isl_map *)set;
4949 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4951 return isl_map_reverse(isl_map_from_range(set));
4954 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4955 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4957 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4960 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4961 __isl_take isl_set *range)
4963 return isl_map_apply_range(isl_map_reverse(domain), range);
4966 struct isl_set *isl_set_from_map(struct isl_map *map)
4968 int i;
4969 struct isl_set *set = NULL;
4971 if (!map)
4972 return NULL;
4973 map = isl_map_cow(map);
4974 if (!map)
4975 return NULL;
4976 map->dim = isl_space_as_set_space(map->dim);
4977 if (!map->dim)
4978 goto error;
4979 set = (struct isl_set *)map;
4980 for (i = 0; i < map->n; ++i) {
4981 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4982 if (!set->p[i])
4983 goto error;
4985 return set;
4986 error:
4987 isl_map_free(map);
4988 return NULL;
4991 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4992 unsigned flags)
4994 struct isl_map *map;
4996 if (!dim)
4997 return NULL;
4998 if (n < 0)
4999 isl_die(dim->ctx, isl_error_internal,
5000 "negative number of basic maps", goto error);
5001 map = isl_alloc(dim->ctx, struct isl_map,
5002 sizeof(struct isl_map) +
5003 (n - 1) * sizeof(struct isl_basic_map *));
5004 if (!map)
5005 goto error;
5007 map->ctx = dim->ctx;
5008 isl_ctx_ref(map->ctx);
5009 map->ref = 1;
5010 map->size = n;
5011 map->n = 0;
5012 map->dim = dim;
5013 map->flags = flags;
5014 return map;
5015 error:
5016 isl_space_free(dim);
5017 return NULL;
5020 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5021 unsigned nparam, unsigned in, unsigned out, int n,
5022 unsigned flags)
5024 struct isl_map *map;
5025 isl_space *dims;
5027 dims = isl_space_alloc(ctx, nparam, in, out);
5028 if (!dims)
5029 return NULL;
5031 map = isl_map_alloc_space(dims, n, flags);
5032 return map;
5035 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5037 struct isl_basic_map *bmap;
5038 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5039 bmap = isl_basic_map_set_to_empty(bmap);
5040 return bmap;
5043 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5045 struct isl_basic_set *bset;
5046 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5047 bset = isl_basic_set_set_to_empty(bset);
5048 return bset;
5051 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5053 struct isl_basic_map *bmap;
5054 if (!model)
5055 return NULL;
5056 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5057 bmap = isl_basic_map_set_to_empty(bmap);
5058 return bmap;
5061 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5063 struct isl_basic_map *bmap;
5064 if (!model)
5065 return NULL;
5066 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5067 bmap = isl_basic_map_set_to_empty(bmap);
5068 return bmap;
5071 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5073 struct isl_basic_set *bset;
5074 if (!model)
5075 return NULL;
5076 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5077 bset = isl_basic_set_set_to_empty(bset);
5078 return bset;
5081 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5083 struct isl_basic_map *bmap;
5084 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5085 bmap = isl_basic_map_finalize(bmap);
5086 return bmap;
5089 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5091 struct isl_basic_set *bset;
5092 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5093 bset = isl_basic_set_finalize(bset);
5094 return bset;
5097 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5099 int i;
5100 unsigned total = isl_space_dim(dim, isl_dim_all);
5101 isl_basic_map *bmap;
5103 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5104 for (i = 0; i < total; ++i) {
5105 int k = isl_basic_map_alloc_inequality(bmap);
5106 if (k < 0)
5107 goto error;
5108 isl_seq_clr(bmap->ineq[k], 1 + total);
5109 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5111 return bmap;
5112 error:
5113 isl_basic_map_free(bmap);
5114 return NULL;
5117 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5119 return isl_basic_map_nat_universe(dim);
5122 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5124 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5127 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5129 return isl_map_nat_universe(dim);
5132 __isl_give isl_basic_map *isl_basic_map_universe_like(
5133 __isl_keep isl_basic_map *model)
5135 if (!model)
5136 return NULL;
5137 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5140 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_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_basic_set *isl_basic_set_universe_like_set(
5148 __isl_keep isl_set *model)
5150 if (!model)
5151 return NULL;
5152 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5155 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5157 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5160 struct isl_map *isl_map_empty_like(struct isl_map *model)
5162 if (!model)
5163 return NULL;
5164 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5167 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5169 if (!model)
5170 return NULL;
5171 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5174 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5176 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5179 struct isl_set *isl_set_empty_like(struct isl_set *model)
5181 if (!model)
5182 return NULL;
5183 return isl_set_empty(isl_space_copy(model->dim));
5186 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5188 struct isl_map *map;
5189 if (!dim)
5190 return NULL;
5191 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5192 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5193 return map;
5196 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5198 struct isl_set *set;
5199 if (!dim)
5200 return NULL;
5201 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5202 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5203 return set;
5206 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5208 if (!model)
5209 return NULL;
5210 return isl_set_universe(isl_space_copy(model->dim));
5213 struct isl_map *isl_map_dup(struct isl_map *map)
5215 int i;
5216 struct isl_map *dup;
5218 if (!map)
5219 return NULL;
5220 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5221 for (i = 0; i < map->n; ++i)
5222 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5223 return dup;
5226 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5227 __isl_take isl_basic_map *bmap)
5229 if (!bmap || !map)
5230 goto error;
5231 if (isl_basic_map_plain_is_empty(bmap)) {
5232 isl_basic_map_free(bmap);
5233 return map;
5235 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5236 isl_assert(map->ctx, map->n < map->size, goto error);
5237 map->p[map->n] = bmap;
5238 map->n++;
5239 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5240 return map;
5241 error:
5242 if (map)
5243 isl_map_free(map);
5244 if (bmap)
5245 isl_basic_map_free(bmap);
5246 return NULL;
5249 void *isl_map_free(struct isl_map *map)
5251 int i;
5253 if (!map)
5254 return NULL;
5256 if (--map->ref > 0)
5257 return NULL;
5259 isl_ctx_deref(map->ctx);
5260 for (i = 0; i < map->n; ++i)
5261 isl_basic_map_free(map->p[i]);
5262 isl_space_free(map->dim);
5263 free(map);
5265 return NULL;
5268 struct isl_map *isl_map_extend(struct isl_map *base,
5269 unsigned nparam, unsigned n_in, unsigned n_out)
5271 int i;
5273 base = isl_map_cow(base);
5274 if (!base)
5275 return NULL;
5277 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5278 if (!base->dim)
5279 goto error;
5280 for (i = 0; i < base->n; ++i) {
5281 base->p[i] = isl_basic_map_extend_space(base->p[i],
5282 isl_space_copy(base->dim), 0, 0, 0);
5283 if (!base->p[i])
5284 goto error;
5286 return base;
5287 error:
5288 isl_map_free(base);
5289 return NULL;
5292 struct isl_set *isl_set_extend(struct isl_set *base,
5293 unsigned nparam, unsigned dim)
5295 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5296 nparam, 0, dim);
5299 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5300 struct isl_basic_map *bmap, unsigned pos, int value)
5302 int j;
5304 bmap = isl_basic_map_cow(bmap);
5305 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5306 j = isl_basic_map_alloc_equality(bmap);
5307 if (j < 0)
5308 goto error;
5309 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5310 isl_int_set_si(bmap->eq[j][pos], -1);
5311 isl_int_set_si(bmap->eq[j][0], value);
5312 bmap = isl_basic_map_simplify(bmap);
5313 return isl_basic_map_finalize(bmap);
5314 error:
5315 isl_basic_map_free(bmap);
5316 return NULL;
5319 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5320 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5322 int j;
5324 bmap = isl_basic_map_cow(bmap);
5325 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5326 j = isl_basic_map_alloc_equality(bmap);
5327 if (j < 0)
5328 goto error;
5329 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5330 isl_int_set_si(bmap->eq[j][pos], -1);
5331 isl_int_set(bmap->eq[j][0], value);
5332 bmap = isl_basic_map_simplify(bmap);
5333 return isl_basic_map_finalize(bmap);
5334 error:
5335 isl_basic_map_free(bmap);
5336 return NULL;
5339 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5340 enum isl_dim_type type, unsigned pos, int value)
5342 if (!bmap)
5343 return NULL;
5344 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5345 return isl_basic_map_fix_pos_si(bmap,
5346 isl_basic_map_offset(bmap, type) + pos, value);
5347 error:
5348 isl_basic_map_free(bmap);
5349 return NULL;
5352 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5353 enum isl_dim_type type, unsigned pos, isl_int value)
5355 if (!bmap)
5356 return NULL;
5357 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5358 return isl_basic_map_fix_pos(bmap,
5359 isl_basic_map_offset(bmap, type) + pos, value);
5360 error:
5361 isl_basic_map_free(bmap);
5362 return NULL;
5365 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5366 * to be equal to "v".
5368 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5369 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5371 if (!bmap || !v)
5372 goto error;
5373 if (!isl_val_is_int(v))
5374 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5375 "expecting integer value", goto error);
5376 if (pos >= isl_basic_map_dim(bmap, type))
5377 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5378 "index out of bounds", goto error);
5379 pos += isl_basic_map_offset(bmap, type);
5380 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5381 isl_val_free(v);
5382 return bmap;
5383 error:
5384 isl_basic_map_free(bmap);
5385 isl_val_free(v);
5386 return NULL;
5389 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5390 * to be equal to "v".
5392 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5393 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5395 return isl_basic_map_fix_val(bset, type, pos, v);
5398 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5399 enum isl_dim_type type, unsigned pos, int value)
5401 return (struct isl_basic_set *)
5402 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5403 type, pos, value);
5406 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5407 enum isl_dim_type type, unsigned pos, isl_int value)
5409 return (struct isl_basic_set *)
5410 isl_basic_map_fix((struct isl_basic_map *)bset,
5411 type, pos, value);
5414 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5415 unsigned input, int value)
5417 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5420 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5421 unsigned dim, int value)
5423 return (struct isl_basic_set *)
5424 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5425 isl_dim_set, dim, value);
5428 static int remove_if_empty(__isl_keep isl_map *map, int i)
5430 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5432 if (empty < 0)
5433 return -1;
5434 if (!empty)
5435 return 0;
5437 isl_basic_map_free(map->p[i]);
5438 if (i != map->n - 1) {
5439 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5440 map->p[i] = map->p[map->n - 1];
5442 map->n--;
5444 return 0;
5447 /* Perform "fn" on each basic map of "map", where we may not be holding
5448 * the only reference to "map".
5449 * In particular, "fn" should be a semantics preserving operation
5450 * that we want to apply to all copies of "map". We therefore need
5451 * to be careful not to modify "map" in a way that breaks "map"
5452 * in case anything goes wrong.
5454 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5455 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5457 struct isl_basic_map *bmap;
5458 int i;
5460 if (!map)
5461 return NULL;
5463 for (i = map->n - 1; i >= 0; --i) {
5464 bmap = isl_basic_map_copy(map->p[i]);
5465 bmap = fn(bmap);
5466 if (!bmap)
5467 goto error;
5468 isl_basic_map_free(map->p[i]);
5469 map->p[i] = bmap;
5470 if (remove_if_empty(map, i) < 0)
5471 goto error;
5474 return map;
5475 error:
5476 isl_map_free(map);
5477 return NULL;
5480 struct isl_map *isl_map_fix_si(struct isl_map *map,
5481 enum isl_dim_type type, unsigned pos, int value)
5483 int i;
5485 map = isl_map_cow(map);
5486 if (!map)
5487 return NULL;
5489 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5490 for (i = map->n - 1; i >= 0; --i) {
5491 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5492 if (remove_if_empty(map, i) < 0)
5493 goto error;
5495 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5496 return map;
5497 error:
5498 isl_map_free(map);
5499 return NULL;
5502 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5503 enum isl_dim_type type, unsigned pos, int value)
5505 return (struct isl_set *)
5506 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5509 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5510 enum isl_dim_type type, unsigned pos, isl_int value)
5512 int i;
5514 map = isl_map_cow(map);
5515 if (!map)
5516 return NULL;
5518 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5519 for (i = 0; i < map->n; ++i) {
5520 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5521 if (!map->p[i])
5522 goto error;
5524 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5525 return map;
5526 error:
5527 isl_map_free(map);
5528 return NULL;
5531 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5532 enum isl_dim_type type, unsigned pos, isl_int value)
5534 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5537 /* Fix the value of the variable at position "pos" of type "type" of "map"
5538 * to be equal to "v".
5540 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5541 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5543 int i;
5545 map = isl_map_cow(map);
5546 if (!map || !v)
5547 goto error;
5549 if (!isl_val_is_int(v))
5550 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5551 "expecting integer value", goto error);
5552 if (pos >= isl_map_dim(map, type))
5553 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5554 "index out of bounds", goto error);
5555 for (i = map->n - 1; i >= 0; --i) {
5556 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5557 isl_val_copy(v));
5558 if (remove_if_empty(map, i) < 0)
5559 goto error;
5561 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5562 isl_val_free(v);
5563 return map;
5564 error:
5565 isl_map_free(map);
5566 isl_val_free(v);
5567 return NULL;
5570 /* Fix the value of the variable at position "pos" of type "type" of "set"
5571 * to be equal to "v".
5573 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5574 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5576 return isl_map_fix_val(set, type, pos, v);
5579 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5580 unsigned input, int value)
5582 return isl_map_fix_si(map, isl_dim_in, input, value);
5585 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5587 return (struct isl_set *)
5588 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5591 static __isl_give isl_basic_map *basic_map_bound_si(
5592 __isl_take isl_basic_map *bmap,
5593 enum isl_dim_type type, unsigned pos, int value, int upper)
5595 int j;
5597 if (!bmap)
5598 return NULL;
5599 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5600 pos += isl_basic_map_offset(bmap, type);
5601 bmap = isl_basic_map_cow(bmap);
5602 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5603 j = isl_basic_map_alloc_inequality(bmap);
5604 if (j < 0)
5605 goto error;
5606 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5607 if (upper) {
5608 isl_int_set_si(bmap->ineq[j][pos], -1);
5609 isl_int_set_si(bmap->ineq[j][0], value);
5610 } else {
5611 isl_int_set_si(bmap->ineq[j][pos], 1);
5612 isl_int_set_si(bmap->ineq[j][0], -value);
5614 bmap = isl_basic_map_simplify(bmap);
5615 return isl_basic_map_finalize(bmap);
5616 error:
5617 isl_basic_map_free(bmap);
5618 return NULL;
5621 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5622 __isl_take isl_basic_map *bmap,
5623 enum isl_dim_type type, unsigned pos, int value)
5625 return basic_map_bound_si(bmap, type, pos, value, 0);
5628 /* Constrain the values of the given dimension to be no greater than "value".
5630 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5631 __isl_take isl_basic_map *bmap,
5632 enum isl_dim_type type, unsigned pos, int value)
5634 return basic_map_bound_si(bmap, type, pos, value, 1);
5637 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5638 unsigned dim, isl_int value)
5640 int j;
5642 bset = isl_basic_set_cow(bset);
5643 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5644 j = isl_basic_set_alloc_inequality(bset);
5645 if (j < 0)
5646 goto error;
5647 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5648 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5649 isl_int_neg(bset->ineq[j][0], value);
5650 bset = isl_basic_set_simplify(bset);
5651 return isl_basic_set_finalize(bset);
5652 error:
5653 isl_basic_set_free(bset);
5654 return NULL;
5657 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5658 enum isl_dim_type type, unsigned pos, int value, int upper)
5660 int i;
5662 map = isl_map_cow(map);
5663 if (!map)
5664 return NULL;
5666 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5667 for (i = 0; i < map->n; ++i) {
5668 map->p[i] = basic_map_bound_si(map->p[i],
5669 type, pos, value, upper);
5670 if (!map->p[i])
5671 goto error;
5673 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5674 return map;
5675 error:
5676 isl_map_free(map);
5677 return NULL;
5680 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5681 enum isl_dim_type type, unsigned pos, int value)
5683 return map_bound_si(map, type, pos, value, 0);
5686 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5687 enum isl_dim_type type, unsigned pos, int value)
5689 return map_bound_si(map, type, pos, value, 1);
5692 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5693 enum isl_dim_type type, unsigned pos, int value)
5695 return (struct isl_set *)
5696 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5699 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5700 enum isl_dim_type type, unsigned pos, int value)
5702 return isl_map_upper_bound_si(set, type, pos, value);
5705 /* Bound the given variable of "bmap" from below (or above is "upper"
5706 * is set) to "value".
5708 static __isl_give isl_basic_map *basic_map_bound(
5709 __isl_take isl_basic_map *bmap,
5710 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5712 int j;
5714 if (!bmap)
5715 return NULL;
5716 if (pos >= isl_basic_map_dim(bmap, type))
5717 isl_die(bmap->ctx, isl_error_invalid,
5718 "index out of bounds", goto error);
5719 pos += isl_basic_map_offset(bmap, type);
5720 bmap = isl_basic_map_cow(bmap);
5721 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5722 j = isl_basic_map_alloc_inequality(bmap);
5723 if (j < 0)
5724 goto error;
5725 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5726 if (upper) {
5727 isl_int_set_si(bmap->ineq[j][pos], -1);
5728 isl_int_set(bmap->ineq[j][0], value);
5729 } else {
5730 isl_int_set_si(bmap->ineq[j][pos], 1);
5731 isl_int_neg(bmap->ineq[j][0], value);
5733 bmap = isl_basic_map_simplify(bmap);
5734 return isl_basic_map_finalize(bmap);
5735 error:
5736 isl_basic_map_free(bmap);
5737 return NULL;
5740 /* Bound the given variable of "map" from below (or above is "upper"
5741 * is set) to "value".
5743 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5744 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5746 int i;
5748 map = isl_map_cow(map);
5749 if (!map)
5750 return NULL;
5752 if (pos >= isl_map_dim(map, type))
5753 isl_die(map->ctx, isl_error_invalid,
5754 "index out of bounds", goto error);
5755 for (i = map->n - 1; i >= 0; --i) {
5756 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5757 if (remove_if_empty(map, i) < 0)
5758 goto error;
5760 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5761 return map;
5762 error:
5763 isl_map_free(map);
5764 return NULL;
5767 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5768 enum isl_dim_type type, unsigned pos, isl_int value)
5770 return map_bound(map, type, pos, value, 0);
5773 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5774 enum isl_dim_type type, unsigned pos, isl_int value)
5776 return map_bound(map, type, pos, value, 1);
5779 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5780 enum isl_dim_type type, unsigned pos, isl_int value)
5782 return isl_map_lower_bound(set, type, pos, value);
5785 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5786 enum isl_dim_type type, unsigned pos, isl_int value)
5788 return isl_map_upper_bound(set, type, pos, value);
5791 /* Force the values of the variable at position "pos" of type "type" of "set"
5792 * to be no smaller than "value".
5794 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5795 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5797 if (!value)
5798 goto error;
5799 if (!isl_val_is_int(value))
5800 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5801 "expecting integer value", goto error);
5802 set = isl_set_lower_bound(set, type, pos, value->n);
5803 isl_val_free(value);
5804 return set;
5805 error:
5806 isl_val_free(value);
5807 isl_set_free(set);
5808 return NULL;
5811 /* Force the values of the variable at position "pos" of type "type" of "set"
5812 * to be no greater than "value".
5814 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5815 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5817 if (!value)
5818 goto error;
5819 if (!isl_val_is_int(value))
5820 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5821 "expecting integer value", goto error);
5822 set = isl_set_upper_bound(set, type, pos, value->n);
5823 isl_val_free(value);
5824 return set;
5825 error:
5826 isl_val_free(value);
5827 isl_set_free(set);
5828 return NULL;
5831 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5832 isl_int value)
5834 int i;
5836 set = isl_set_cow(set);
5837 if (!set)
5838 return NULL;
5840 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5841 for (i = 0; i < set->n; ++i) {
5842 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5843 if (!set->p[i])
5844 goto error;
5846 return set;
5847 error:
5848 isl_set_free(set);
5849 return NULL;
5852 struct isl_map *isl_map_reverse(struct isl_map *map)
5854 int i;
5856 map = isl_map_cow(map);
5857 if (!map)
5858 return NULL;
5860 map->dim = isl_space_reverse(map->dim);
5861 if (!map->dim)
5862 goto error;
5863 for (i = 0; i < map->n; ++i) {
5864 map->p[i] = isl_basic_map_reverse(map->p[i]);
5865 if (!map->p[i])
5866 goto error;
5868 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5869 return map;
5870 error:
5871 isl_map_free(map);
5872 return NULL;
5875 static struct isl_map *isl_basic_map_partial_lexopt(
5876 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5877 struct isl_set **empty, int max)
5879 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5882 struct isl_map *isl_basic_map_partial_lexmax(
5883 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5884 struct isl_set **empty)
5886 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5889 struct isl_map *isl_basic_map_partial_lexmin(
5890 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5891 struct isl_set **empty)
5893 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5896 struct isl_set *isl_basic_set_partial_lexmin(
5897 struct isl_basic_set *bset, struct isl_basic_set *dom,
5898 struct isl_set **empty)
5900 return (struct isl_set *)
5901 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5902 dom, empty);
5905 struct isl_set *isl_basic_set_partial_lexmax(
5906 struct isl_basic_set *bset, struct isl_basic_set *dom,
5907 struct isl_set **empty)
5909 return (struct isl_set *)
5910 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5911 dom, empty);
5914 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5915 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5916 __isl_give isl_set **empty)
5918 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5921 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5922 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5923 __isl_give isl_set **empty)
5925 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5928 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5929 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5930 __isl_give isl_set **empty)
5932 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5935 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5936 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5937 __isl_give isl_set **empty)
5939 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5942 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5943 __isl_take isl_basic_map *bmap, int max)
5945 isl_basic_set *dom = NULL;
5946 isl_space *dom_space;
5948 if (!bmap)
5949 goto error;
5950 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5951 dom = isl_basic_set_universe(dom_space);
5952 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5953 error:
5954 isl_basic_map_free(bmap);
5955 return NULL;
5958 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5959 __isl_take isl_basic_map *bmap)
5961 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5964 #undef TYPE
5965 #define TYPE isl_pw_multi_aff
5966 #undef SUFFIX
5967 #define SUFFIX _pw_multi_aff
5968 #undef EMPTY
5969 #define EMPTY isl_pw_multi_aff_empty
5970 #undef ADD
5971 #define ADD isl_pw_multi_aff_union_add
5972 #include "isl_map_lexopt_templ.c"
5974 /* Given a map "map", compute the lexicographically minimal
5975 * (or maximal) image element for each domain element in dom,
5976 * in the form of an isl_pw_multi_aff.
5977 * Set *empty to those elements in dom that do not have an image element.
5979 * We first compute the lexicographically minimal or maximal element
5980 * in the first basic map. This results in a partial solution "res"
5981 * and a subset "todo" of dom that still need to be handled.
5982 * We then consider each of the remaining maps in "map" and successively
5983 * update both "res" and "todo".
5985 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
5986 __isl_take isl_map *map, __isl_take isl_set *dom,
5987 __isl_give isl_set **empty, int max)
5989 int i;
5990 isl_pw_multi_aff *res;
5991 isl_set *todo;
5993 if (!map || !dom)
5994 goto error;
5996 if (isl_map_plain_is_empty(map)) {
5997 if (empty)
5998 *empty = dom;
5999 else
6000 isl_set_free(dom);
6001 return isl_pw_multi_aff_from_map(map);
6004 res = basic_map_partial_lexopt_pw_multi_aff(
6005 isl_basic_map_copy(map->p[0]),
6006 isl_set_copy(dom), &todo, max);
6008 for (i = 1; i < map->n; ++i) {
6009 isl_pw_multi_aff *res_i;
6010 isl_set *todo_i;
6012 res_i = basic_map_partial_lexopt_pw_multi_aff(
6013 isl_basic_map_copy(map->p[i]),
6014 isl_set_copy(dom), &todo_i, max);
6016 if (max)
6017 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6018 else
6019 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6021 todo = isl_set_intersect(todo, todo_i);
6024 isl_set_free(dom);
6025 isl_map_free(map);
6027 if (empty)
6028 *empty = todo;
6029 else
6030 isl_set_free(todo);
6032 return res;
6033 error:
6034 if (empty)
6035 *empty = NULL;
6036 isl_set_free(dom);
6037 isl_map_free(map);
6038 return NULL;
6041 #undef TYPE
6042 #define TYPE isl_map
6043 #undef SUFFIX
6044 #define SUFFIX
6045 #undef EMPTY
6046 #define EMPTY isl_map_empty
6047 #undef ADD
6048 #define ADD isl_map_union_disjoint
6049 #include "isl_map_lexopt_templ.c"
6051 /* Given a map "map", compute the lexicographically minimal
6052 * (or maximal) image element for each domain element in dom.
6053 * Set *empty to those elements in dom that do not have an image element.
6055 * We first compute the lexicographically minimal or maximal element
6056 * in the first basic map. This results in a partial solution "res"
6057 * and a subset "todo" of dom that still need to be handled.
6058 * We then consider each of the remaining maps in "map" and successively
6059 * update both "res" and "todo".
6061 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6062 * Assume we are computing the lexicographical maximum.
6063 * We first compute the lexicographically maximal element in basic map i.
6064 * This results in a partial solution res_i and a subset todo_i.
6065 * Then we combine these results with those obtain for the first k basic maps
6066 * to obtain a result that is valid for the first k+1 basic maps.
6067 * In particular, the set where there is no solution is the set where
6068 * there is no solution for the first k basic maps and also no solution
6069 * for the ith basic map, i.e.,
6071 * todo^i = todo^k * todo_i
6073 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6074 * solutions, arbitrarily breaking ties in favor of res^k.
6075 * That is, when res^k(a) >= res_i(a), we pick res^k and
6076 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6077 * the lexicographic order.)
6078 * In practice, we compute
6080 * res^k * (res_i . "<=")
6082 * and
6084 * res_i * (res^k . "<")
6086 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6087 * where only one of res^k and res_i provides a solution and we simply pick
6088 * that one, i.e.,
6090 * res^k * todo_i
6091 * and
6092 * res_i * todo^k
6094 * Note that we only compute these intersections when dom(res^k) intersects
6095 * dom(res_i). Otherwise, the only effect of these intersections is to
6096 * potentially break up res^k and res_i into smaller pieces.
6097 * We want to avoid such splintering as much as possible.
6098 * In fact, an earlier implementation of this function would look for
6099 * better results in the domain of res^k and for extra results in todo^k,
6100 * but this would always result in a splintering according to todo^k,
6101 * even when the domain of basic map i is disjoint from the domains of
6102 * the previous basic maps.
6104 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6105 __isl_take isl_map *map, __isl_take isl_set *dom,
6106 __isl_give isl_set **empty, int max)
6108 int i;
6109 struct isl_map *res;
6110 struct isl_set *todo;
6112 if (!map || !dom)
6113 goto error;
6115 if (isl_map_plain_is_empty(map)) {
6116 if (empty)
6117 *empty = dom;
6118 else
6119 isl_set_free(dom);
6120 return map;
6123 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6124 isl_set_copy(dom), &todo, max);
6126 for (i = 1; i < map->n; ++i) {
6127 isl_map *lt, *le;
6128 isl_map *res_i;
6129 isl_set *todo_i;
6130 isl_space *dim = isl_space_range(isl_map_get_space(res));
6132 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6133 isl_set_copy(dom), &todo_i, max);
6135 if (max) {
6136 lt = isl_map_lex_lt(isl_space_copy(dim));
6137 le = isl_map_lex_le(dim);
6138 } else {
6139 lt = isl_map_lex_gt(isl_space_copy(dim));
6140 le = isl_map_lex_ge(dim);
6142 lt = isl_map_apply_range(isl_map_copy(res), lt);
6143 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6144 le = isl_map_apply_range(isl_map_copy(res_i), le);
6145 le = isl_map_intersect(le, isl_map_copy(res));
6147 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6148 res = isl_map_intersect_domain(res,
6149 isl_set_copy(todo_i));
6150 res_i = isl_map_intersect_domain(res_i,
6151 isl_set_copy(todo));
6154 res = isl_map_union_disjoint(res, res_i);
6155 res = isl_map_union_disjoint(res, lt);
6156 res = isl_map_union_disjoint(res, le);
6158 todo = isl_set_intersect(todo, todo_i);
6161 isl_set_free(dom);
6162 isl_map_free(map);
6164 if (empty)
6165 *empty = todo;
6166 else
6167 isl_set_free(todo);
6169 return res;
6170 error:
6171 if (empty)
6172 *empty = NULL;
6173 isl_set_free(dom);
6174 isl_map_free(map);
6175 return NULL;
6178 __isl_give isl_map *isl_map_partial_lexmax(
6179 __isl_take isl_map *map, __isl_take isl_set *dom,
6180 __isl_give isl_set **empty)
6182 return isl_map_partial_lexopt(map, dom, empty, 1);
6185 __isl_give isl_map *isl_map_partial_lexmin(
6186 __isl_take isl_map *map, __isl_take isl_set *dom,
6187 __isl_give isl_set **empty)
6189 return isl_map_partial_lexopt(map, dom, empty, 0);
6192 __isl_give isl_set *isl_set_partial_lexmin(
6193 __isl_take isl_set *set, __isl_take isl_set *dom,
6194 __isl_give isl_set **empty)
6196 return (struct isl_set *)
6197 isl_map_partial_lexmin((struct isl_map *)set,
6198 dom, empty);
6201 __isl_give isl_set *isl_set_partial_lexmax(
6202 __isl_take isl_set *set, __isl_take isl_set *dom,
6203 __isl_give isl_set **empty)
6205 return (struct isl_set *)
6206 isl_map_partial_lexmax((struct isl_map *)set,
6207 dom, empty);
6210 /* Compute the lexicographic minimum (or maximum if "max" is set)
6211 * of "bmap" over its domain.
6213 * Since we are not interested in the part of the domain space where
6214 * there is no solution, we initialize the domain to those constraints
6215 * of "bmap" that only involve the parameters and the input dimensions.
6216 * This relieves the parametric programming engine from detecting those
6217 * inequalities and transferring them to the context. More importantly,
6218 * it ensures that those inequalities are transferred first and not
6219 * intermixed with inequalities that actually split the domain.
6221 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6223 int n_div;
6224 int n_out;
6225 isl_basic_map *copy;
6226 isl_basic_set *dom;
6228 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6229 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6230 copy = isl_basic_map_copy(bmap);
6231 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6232 isl_dim_div, 0, n_div);
6233 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6234 isl_dim_out, 0, n_out);
6235 dom = isl_basic_map_domain(copy);
6236 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6239 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6241 return isl_basic_map_lexopt(bmap, 0);
6244 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6246 return isl_basic_map_lexopt(bmap, 1);
6249 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6251 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6254 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6256 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6259 /* Extract the first and only affine expression from list
6260 * and then add it to *pwaff with the given dom.
6261 * This domain is known to be disjoint from other domains
6262 * because of the way isl_basic_map_foreach_lexmax works.
6264 static int update_dim_opt(__isl_take isl_basic_set *dom,
6265 __isl_take isl_aff_list *list, void *user)
6267 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6268 isl_aff *aff;
6269 isl_pw_aff **pwaff = user;
6270 isl_pw_aff *pwaff_i;
6272 if (!list)
6273 goto error;
6274 if (isl_aff_list_n_aff(list) != 1)
6275 isl_die(ctx, isl_error_internal,
6276 "expecting single element list", goto error);
6278 aff = isl_aff_list_get_aff(list, 0);
6279 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6281 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6283 isl_aff_list_free(list);
6285 return 0;
6286 error:
6287 isl_basic_set_free(dom);
6288 isl_aff_list_free(list);
6289 return -1;
6292 /* Given a basic map with one output dimension, compute the minimum or
6293 * maximum of that dimension as an isl_pw_aff.
6295 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6296 * call update_dim_opt on each leaf of the result.
6298 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6299 int max)
6301 isl_space *dim = isl_basic_map_get_space(bmap);
6302 isl_pw_aff *pwaff;
6303 int r;
6305 dim = isl_space_from_domain(isl_space_domain(dim));
6306 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6307 pwaff = isl_pw_aff_empty(dim);
6309 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6310 if (r < 0)
6311 return isl_pw_aff_free(pwaff);
6313 return pwaff;
6316 /* Compute the minimum or maximum of the given output dimension
6317 * as a function of the parameters and the input dimensions,
6318 * but independently of the other output dimensions.
6320 * We first project out the other output dimension and then compute
6321 * the "lexicographic" maximum in each basic map, combining the results
6322 * using isl_pw_aff_union_max.
6324 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6325 int max)
6327 int i;
6328 isl_pw_aff *pwaff;
6329 unsigned n_out;
6331 n_out = isl_map_dim(map, isl_dim_out);
6332 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6333 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6334 if (!map)
6335 return NULL;
6337 if (map->n == 0) {
6338 isl_space *dim = isl_map_get_space(map);
6339 dim = isl_space_domain(isl_space_from_range(dim));
6340 isl_map_free(map);
6341 return isl_pw_aff_empty(dim);
6344 pwaff = basic_map_dim_opt(map->p[0], max);
6345 for (i = 1; i < map->n; ++i) {
6346 isl_pw_aff *pwaff_i;
6348 pwaff_i = basic_map_dim_opt(map->p[i], max);
6349 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6352 isl_map_free(map);
6354 return pwaff;
6357 /* Compute the maximum of the given output dimension as a function of the
6358 * parameters and input dimensions, but independently of
6359 * the other output dimensions.
6361 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6363 return map_dim_opt(map, pos, 1);
6366 /* Compute the minimum or maximum of the given set dimension
6367 * as a function of the parameters,
6368 * but independently of the other set dimensions.
6370 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6371 int max)
6373 return map_dim_opt(set, pos, max);
6376 /* Compute the maximum 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_max(__isl_take isl_set *set, int pos)
6381 return set_dim_opt(set, pos, 1);
6384 /* Compute the minimum of the given set dimension as a function of the
6385 * parameters, but independently of the other set dimensions.
6387 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6389 return set_dim_opt(set, pos, 0);
6392 /* Apply a preimage specified by "mat" on the parameters of "bset".
6393 * bset is assumed to have only parameters and divs.
6395 static struct isl_basic_set *basic_set_parameter_preimage(
6396 struct isl_basic_set *bset, struct isl_mat *mat)
6398 unsigned nparam;
6400 if (!bset || !mat)
6401 goto error;
6403 bset->dim = isl_space_cow(bset->dim);
6404 if (!bset->dim)
6405 goto error;
6407 nparam = isl_basic_set_dim(bset, isl_dim_param);
6409 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6411 bset->dim->nparam = 0;
6412 bset->dim->n_out = nparam;
6413 bset = isl_basic_set_preimage(bset, mat);
6414 if (bset) {
6415 bset->dim->nparam = bset->dim->n_out;
6416 bset->dim->n_out = 0;
6418 return bset;
6419 error:
6420 isl_mat_free(mat);
6421 isl_basic_set_free(bset);
6422 return NULL;
6425 /* Apply a preimage specified by "mat" on the parameters of "set".
6426 * set is assumed to have only parameters and divs.
6428 static struct isl_set *set_parameter_preimage(
6429 struct isl_set *set, struct isl_mat *mat)
6431 isl_space *dim = NULL;
6432 unsigned nparam;
6434 if (!set || !mat)
6435 goto error;
6437 dim = isl_space_copy(set->dim);
6438 dim = isl_space_cow(dim);
6439 if (!dim)
6440 goto error;
6442 nparam = isl_set_dim(set, isl_dim_param);
6444 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6446 dim->nparam = 0;
6447 dim->n_out = nparam;
6448 isl_set_reset_space(set, dim);
6449 set = isl_set_preimage(set, mat);
6450 if (!set)
6451 goto error2;
6452 dim = isl_space_copy(set->dim);
6453 dim = isl_space_cow(dim);
6454 if (!dim)
6455 goto error2;
6456 dim->nparam = dim->n_out;
6457 dim->n_out = 0;
6458 isl_set_reset_space(set, dim);
6459 return set;
6460 error:
6461 isl_space_free(dim);
6462 isl_mat_free(mat);
6463 error2:
6464 isl_set_free(set);
6465 return NULL;
6468 /* Intersect the basic set "bset" with the affine space specified by the
6469 * equalities in "eq".
6471 static struct isl_basic_set *basic_set_append_equalities(
6472 struct isl_basic_set *bset, struct isl_mat *eq)
6474 int i, k;
6475 unsigned len;
6477 if (!bset || !eq)
6478 goto error;
6480 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6481 eq->n_row, 0);
6482 if (!bset)
6483 goto error;
6485 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6486 for (i = 0; i < eq->n_row; ++i) {
6487 k = isl_basic_set_alloc_equality(bset);
6488 if (k < 0)
6489 goto error;
6490 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6491 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6493 isl_mat_free(eq);
6495 bset = isl_basic_set_gauss(bset, NULL);
6496 bset = isl_basic_set_finalize(bset);
6498 return bset;
6499 error:
6500 isl_mat_free(eq);
6501 isl_basic_set_free(bset);
6502 return NULL;
6505 /* Intersect the set "set" with the affine space specified by the
6506 * equalities in "eq".
6508 static struct isl_set *set_append_equalities(struct isl_set *set,
6509 struct isl_mat *eq)
6511 int i;
6513 if (!set || !eq)
6514 goto error;
6516 for (i = 0; i < set->n; ++i) {
6517 set->p[i] = basic_set_append_equalities(set->p[i],
6518 isl_mat_copy(eq));
6519 if (!set->p[i])
6520 goto error;
6522 isl_mat_free(eq);
6523 return set;
6524 error:
6525 isl_mat_free(eq);
6526 isl_set_free(set);
6527 return NULL;
6530 /* Given a basic set "bset" that only involves parameters and existentially
6531 * quantified variables, return the index of the first equality
6532 * that only involves parameters. If there is no such equality then
6533 * return bset->n_eq.
6535 * This function assumes that isl_basic_set_gauss has been called on "bset".
6537 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6539 int i, j;
6540 unsigned nparam, n_div;
6542 if (!bset)
6543 return -1;
6545 nparam = isl_basic_set_dim(bset, isl_dim_param);
6546 n_div = isl_basic_set_dim(bset, isl_dim_div);
6548 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6549 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6550 ++i;
6553 return i;
6556 /* Compute an explicit representation for the existentially quantified
6557 * variables in "bset" by computing the "minimal value" of the set
6558 * variables. Since there are no set variables, the computation of
6559 * the minimal value essentially computes an explicit representation
6560 * of the non-empty part(s) of "bset".
6562 * The input only involves parameters and existentially quantified variables.
6563 * All equalities among parameters have been removed.
6565 * Since the existentially quantified variables in the result are in general
6566 * going to be different from those in the input, we first replace
6567 * them by the minimal number of variables based on their equalities.
6568 * This should simplify the parametric integer programming.
6570 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6572 isl_morph *morph1, *morph2;
6573 isl_set *set;
6574 unsigned n;
6576 if (!bset)
6577 return NULL;
6578 if (bset->n_eq == 0)
6579 return isl_basic_set_lexmin(bset);
6581 morph1 = isl_basic_set_parameter_compression(bset);
6582 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6583 bset = isl_basic_set_lift(bset);
6584 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6585 bset = isl_morph_basic_set(morph2, bset);
6586 n = isl_basic_set_dim(bset, isl_dim_set);
6587 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6589 set = isl_basic_set_lexmin(bset);
6591 set = isl_morph_set(isl_morph_inverse(morph1), set);
6593 return set;
6596 /* Project the given basic set onto its parameter domain, possibly introducing
6597 * new, explicit, existential variables in the constraints.
6598 * The input has parameters and (possibly implicit) existential variables.
6599 * The output has the same parameters, but only
6600 * explicit existentially quantified variables.
6602 * The actual projection is performed by pip, but pip doesn't seem
6603 * to like equalities very much, so we first remove the equalities
6604 * among the parameters by performing a variable compression on
6605 * the parameters. Afterward, an inverse transformation is performed
6606 * and the equalities among the parameters are inserted back in.
6608 * The variable compression on the parameters may uncover additional
6609 * equalities that were only implicit before. We therefore check
6610 * if there are any new parameter equalities in the result and
6611 * if so recurse. The removal of parameter equalities is required
6612 * for the parameter compression performed by base_compute_divs.
6614 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6616 int i;
6617 struct isl_mat *eq;
6618 struct isl_mat *T, *T2;
6619 struct isl_set *set;
6620 unsigned nparam;
6622 bset = isl_basic_set_cow(bset);
6623 if (!bset)
6624 return NULL;
6626 if (bset->n_eq == 0)
6627 return base_compute_divs(bset);
6629 bset = isl_basic_set_gauss(bset, NULL);
6630 if (!bset)
6631 return NULL;
6632 if (isl_basic_set_plain_is_empty(bset))
6633 return isl_set_from_basic_set(bset);
6635 i = first_parameter_equality(bset);
6636 if (i == bset->n_eq)
6637 return base_compute_divs(bset);
6639 nparam = isl_basic_set_dim(bset, isl_dim_param);
6640 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6641 0, 1 + nparam);
6642 eq = isl_mat_cow(eq);
6643 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6644 if (T && T->n_col == 0) {
6645 isl_mat_free(T);
6646 isl_mat_free(T2);
6647 isl_mat_free(eq);
6648 bset = isl_basic_set_set_to_empty(bset);
6649 return isl_set_from_basic_set(bset);
6651 bset = basic_set_parameter_preimage(bset, T);
6653 i = first_parameter_equality(bset);
6654 if (!bset)
6655 set = NULL;
6656 else if (i == bset->n_eq)
6657 set = base_compute_divs(bset);
6658 else
6659 set = parameter_compute_divs(bset);
6660 set = set_parameter_preimage(set, T2);
6661 set = set_append_equalities(set, eq);
6662 return set;
6665 /* Insert the divs from "ls" before those of "bmap".
6667 * The number of columns is not changed, which means that the last
6668 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6669 * The caller is responsible for removing the same number of dimensions
6670 * from the space of "bmap".
6672 static __isl_give isl_basic_map *insert_divs_from_local_space(
6673 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6675 int i;
6676 int n_div;
6677 int old_n_div;
6679 n_div = isl_local_space_dim(ls, isl_dim_div);
6680 if (n_div == 0)
6681 return bmap;
6683 old_n_div = bmap->n_div;
6684 bmap = insert_div_rows(bmap, n_div);
6685 if (!bmap)
6686 return NULL;
6688 for (i = 0; i < n_div; ++i) {
6689 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6690 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6693 return bmap;
6696 /* Replace the space of "bmap" by the space and divs of "ls".
6698 * If "ls" has any divs, then we simplify the result since we may
6699 * have discovered some additional equalities that could simplify
6700 * the div expressions.
6702 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6703 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6705 int n_div;
6707 bmap = isl_basic_map_cow(bmap);
6708 if (!bmap || !ls)
6709 goto error;
6711 n_div = isl_local_space_dim(ls, isl_dim_div);
6712 bmap = insert_divs_from_local_space(bmap, ls);
6713 if (!bmap)
6714 goto error;
6716 isl_space_free(bmap->dim);
6717 bmap->dim = isl_local_space_get_space(ls);
6718 if (!bmap->dim)
6719 goto error;
6721 isl_local_space_free(ls);
6722 if (n_div > 0)
6723 bmap = isl_basic_map_simplify(bmap);
6724 bmap = isl_basic_map_finalize(bmap);
6725 return bmap;
6726 error:
6727 isl_basic_map_free(bmap);
6728 isl_local_space_free(ls);
6729 return NULL;
6732 /* Replace the space of "map" by the space and divs of "ls".
6734 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6735 __isl_take isl_local_space *ls)
6737 int i;
6739 map = isl_map_cow(map);
6740 if (!map || !ls)
6741 goto error;
6743 for (i = 0; i < map->n; ++i) {
6744 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6745 isl_local_space_copy(ls));
6746 if (!map->p[i])
6747 goto error;
6749 isl_space_free(map->dim);
6750 map->dim = isl_local_space_get_space(ls);
6751 if (!map->dim)
6752 goto error;
6754 isl_local_space_free(ls);
6755 return map;
6756 error:
6757 isl_local_space_free(ls);
6758 isl_map_free(map);
6759 return NULL;
6762 /* Compute an explicit representation for the existentially
6763 * quantified variables for which do not know any explicit representation yet.
6765 * We first sort the existentially quantified variables so that the
6766 * existentially quantified variables for which we already have an explicit
6767 * representation are placed before those for which we do not.
6768 * The input dimensions, the output dimensions and the existentially
6769 * quantified variables for which we already have an explicit
6770 * representation are then turned into parameters.
6771 * compute_divs returns a map with the same parameters and
6772 * no input or output dimensions and the dimension specification
6773 * is reset to that of the input, including the existentially quantified
6774 * variables for which we already had an explicit representation.
6776 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6778 struct isl_basic_set *bset;
6779 struct isl_set *set;
6780 struct isl_map *map;
6781 isl_space *dim;
6782 isl_local_space *ls;
6783 unsigned nparam;
6784 unsigned n_in;
6785 unsigned n_out;
6786 unsigned n_known;
6787 int i;
6789 bmap = isl_basic_map_sort_divs(bmap);
6790 bmap = isl_basic_map_cow(bmap);
6791 if (!bmap)
6792 return NULL;
6794 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6795 if (isl_int_is_zero(bmap->div[n_known][0]))
6796 break;
6798 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6799 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6800 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6801 dim = isl_space_set_alloc(bmap->ctx,
6802 nparam + n_in + n_out + n_known, 0);
6803 if (!dim)
6804 goto error;
6806 ls = isl_basic_map_get_local_space(bmap);
6807 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6808 n_known, bmap->n_div - n_known);
6809 if (n_known > 0) {
6810 for (i = n_known; i < bmap->n_div; ++i)
6811 swap_div(bmap, i - n_known, i);
6812 bmap->n_div -= n_known;
6813 bmap->extra -= n_known;
6815 bmap = isl_basic_map_reset_space(bmap, dim);
6816 bset = (struct isl_basic_set *)bmap;
6818 set = parameter_compute_divs(bset);
6819 map = (struct isl_map *)set;
6820 map = replace_space_by_local_space(map, ls);
6822 return map;
6823 error:
6824 isl_basic_map_free(bmap);
6825 return NULL;
6828 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6830 int i;
6831 unsigned off;
6833 if (!bmap)
6834 return -1;
6836 off = isl_space_dim(bmap->dim, isl_dim_all);
6837 for (i = 0; i < bmap->n_div; ++i) {
6838 if (isl_int_is_zero(bmap->div[i][0]))
6839 return 0;
6840 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6841 return -1);
6843 return 1;
6846 static int map_divs_known(__isl_keep isl_map *map)
6848 int i;
6850 if (!map)
6851 return -1;
6853 for (i = 0; i < map->n; ++i) {
6854 int known = isl_basic_map_divs_known(map->p[i]);
6855 if (known <= 0)
6856 return known;
6859 return 1;
6862 /* If bmap contains any unknown divs, then compute explicit
6863 * expressions for them. However, this computation may be
6864 * quite expensive, so first try to remove divs that aren't
6865 * strictly needed.
6867 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6869 int known;
6870 struct isl_map *map;
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 bmap = isl_basic_map_drop_redundant_divs(bmap);
6880 known = isl_basic_map_divs_known(bmap);
6881 if (known < 0)
6882 goto error;
6883 if (known)
6884 return isl_map_from_basic_map(bmap);
6886 map = compute_divs(bmap);
6887 return map;
6888 error:
6889 isl_basic_map_free(bmap);
6890 return NULL;
6893 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6895 int i;
6896 int known;
6897 struct isl_map *res;
6899 if (!map)
6900 return NULL;
6901 if (map->n == 0)
6902 return map;
6904 known = map_divs_known(map);
6905 if (known < 0) {
6906 isl_map_free(map);
6907 return NULL;
6909 if (known)
6910 return map;
6912 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6913 for (i = 1 ; i < map->n; ++i) {
6914 struct isl_map *r2;
6915 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6916 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6917 res = isl_map_union_disjoint(res, r2);
6918 else
6919 res = isl_map_union(res, r2);
6921 isl_map_free(map);
6923 return res;
6926 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6928 return (struct isl_set *)
6929 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6932 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6934 return (struct isl_set *)
6935 isl_map_compute_divs((struct isl_map *)set);
6938 struct isl_set *isl_map_domain(struct isl_map *map)
6940 int i;
6941 struct isl_set *set;
6943 if (!map)
6944 goto error;
6946 map = isl_map_cow(map);
6947 if (!map)
6948 return NULL;
6950 set = (struct isl_set *)map;
6951 set->dim = isl_space_domain(set->dim);
6952 if (!set->dim)
6953 goto error;
6954 for (i = 0; i < map->n; ++i) {
6955 set->p[i] = isl_basic_map_domain(map->p[i]);
6956 if (!set->p[i])
6957 goto error;
6959 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6960 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6961 return set;
6962 error:
6963 isl_map_free(map);
6964 return NULL;
6967 /* Return the union of "map1" and "map2", where we assume for now that
6968 * "map1" and "map2" are disjoint. Note that the basic maps inside
6969 * "map1" or "map2" may not be disjoint from each other.
6970 * Also note that this function is also called from isl_map_union,
6971 * which takes care of handling the situation where "map1" and "map2"
6972 * may not be disjoint.
6974 * If one of the inputs is empty, we can simply return the other input.
6975 * Similarly, if one of the inputs is universal, then it is equal to the union.
6977 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6978 __isl_take isl_map *map2)
6980 int i;
6981 unsigned flags = 0;
6982 struct isl_map *map = NULL;
6983 int is_universe;
6985 if (!map1 || !map2)
6986 goto error;
6988 if (map1->n == 0) {
6989 isl_map_free(map1);
6990 return map2;
6992 if (map2->n == 0) {
6993 isl_map_free(map2);
6994 return map1;
6997 is_universe = isl_map_plain_is_universe(map1);
6998 if (is_universe < 0)
6999 goto error;
7000 if (is_universe) {
7001 isl_map_free(map2);
7002 return map1;
7005 is_universe = isl_map_plain_is_universe(map2);
7006 if (is_universe < 0)
7007 goto error;
7008 if (is_universe) {
7009 isl_map_free(map1);
7010 return map2;
7013 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
7015 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7016 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7017 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7019 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7020 map1->n + map2->n, flags);
7021 if (!map)
7022 goto error;
7023 for (i = 0; i < map1->n; ++i) {
7024 map = isl_map_add_basic_map(map,
7025 isl_basic_map_copy(map1->p[i]));
7026 if (!map)
7027 goto error;
7029 for (i = 0; i < map2->n; ++i) {
7030 map = isl_map_add_basic_map(map,
7031 isl_basic_map_copy(map2->p[i]));
7032 if (!map)
7033 goto error;
7035 isl_map_free(map1);
7036 isl_map_free(map2);
7037 return map;
7038 error:
7039 isl_map_free(map);
7040 isl_map_free(map1);
7041 isl_map_free(map2);
7042 return NULL;
7045 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7046 __isl_take isl_map *map2)
7048 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7051 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
7053 map1 = isl_map_union_disjoint(map1, map2);
7054 if (!map1)
7055 return NULL;
7056 if (map1->n > 1)
7057 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7058 return map1;
7061 struct isl_set *isl_set_union_disjoint(
7062 struct isl_set *set1, struct isl_set *set2)
7064 return (struct isl_set *)
7065 isl_map_union_disjoint(
7066 (struct isl_map *)set1, (struct isl_map *)set2);
7069 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7071 return (struct isl_set *)
7072 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7075 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7076 * the results.
7078 * "map" and "set" are assumed to be compatible and non-NULL.
7080 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7081 __isl_take isl_set *set,
7082 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7083 __isl_take isl_basic_set *bset))
7085 unsigned flags = 0;
7086 struct isl_map *result;
7087 int i, j;
7089 if (isl_set_plain_is_universe(set)) {
7090 isl_set_free(set);
7091 return map;
7094 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7095 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7096 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7098 result = isl_map_alloc_space(isl_space_copy(map->dim),
7099 map->n * set->n, flags);
7100 for (i = 0; result && i < map->n; ++i)
7101 for (j = 0; j < set->n; ++j) {
7102 result = isl_map_add_basic_map(result,
7103 fn(isl_basic_map_copy(map->p[i]),
7104 isl_basic_set_copy(set->p[j])));
7105 if (!result)
7106 break;
7109 isl_map_free(map);
7110 isl_set_free(set);
7111 return result;
7114 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7115 __isl_take isl_set *set)
7117 if (!map || !set)
7118 goto error;
7120 if (!isl_map_compatible_range(map, set))
7121 isl_die(set->ctx, isl_error_invalid,
7122 "incompatible spaces", goto error);
7124 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7125 error:
7126 isl_map_free(map);
7127 isl_set_free(set);
7128 return NULL;
7131 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7132 __isl_take isl_set *set)
7134 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7137 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7138 __isl_take isl_set *set)
7140 if (!map || !set)
7141 goto error;
7143 if (!isl_map_compatible_domain(map, set))
7144 isl_die(set->ctx, isl_error_invalid,
7145 "incompatible spaces", goto error);
7147 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7148 error:
7149 isl_map_free(map);
7150 isl_set_free(set);
7151 return NULL;
7154 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7155 __isl_take isl_set *set)
7157 return isl_map_align_params_map_map_and(map, set,
7158 &map_intersect_domain);
7161 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7162 __isl_take isl_map *map2)
7164 if (!map1 || !map2)
7165 goto error;
7166 map1 = isl_map_reverse(map1);
7167 map1 = isl_map_apply_range(map1, map2);
7168 return isl_map_reverse(map1);
7169 error:
7170 isl_map_free(map1);
7171 isl_map_free(map2);
7172 return NULL;
7175 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7176 __isl_take isl_map *map2)
7178 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7181 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7182 __isl_take isl_map *map2)
7184 isl_space *dim_result;
7185 struct isl_map *result;
7186 int i, j;
7188 if (!map1 || !map2)
7189 goto error;
7191 dim_result = isl_space_join(isl_space_copy(map1->dim),
7192 isl_space_copy(map2->dim));
7194 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7195 if (!result)
7196 goto error;
7197 for (i = 0; i < map1->n; ++i)
7198 for (j = 0; j < map2->n; ++j) {
7199 result = isl_map_add_basic_map(result,
7200 isl_basic_map_apply_range(
7201 isl_basic_map_copy(map1->p[i]),
7202 isl_basic_map_copy(map2->p[j])));
7203 if (!result)
7204 goto error;
7206 isl_map_free(map1);
7207 isl_map_free(map2);
7208 if (result && result->n <= 1)
7209 ISL_F_SET(result, ISL_MAP_DISJOINT);
7210 return result;
7211 error:
7212 isl_map_free(map1);
7213 isl_map_free(map2);
7214 return NULL;
7217 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7218 __isl_take isl_map *map2)
7220 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7224 * returns range - domain
7226 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7228 isl_space *dims, *target_dim;
7229 struct isl_basic_set *bset;
7230 unsigned dim;
7231 unsigned nparam;
7232 int i;
7234 if (!bmap)
7235 goto error;
7236 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7237 bmap->dim, isl_dim_out),
7238 goto error);
7239 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7240 dim = isl_basic_map_n_in(bmap);
7241 nparam = isl_basic_map_n_param(bmap);
7242 bset = isl_basic_set_from_basic_map(bmap);
7243 bset = isl_basic_set_cow(bset);
7244 dims = isl_basic_set_get_space(bset);
7245 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7246 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7247 bset = isl_basic_set_swap_vars(bset, 2*dim);
7248 for (i = 0; i < dim; ++i) {
7249 int j = isl_basic_map_alloc_equality(
7250 (struct isl_basic_map *)bset);
7251 if (j < 0) {
7252 bset = isl_basic_set_free(bset);
7253 break;
7255 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7256 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7257 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7258 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7260 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7261 bset = isl_basic_set_reset_space(bset, target_dim);
7262 return bset;
7263 error:
7264 isl_basic_map_free(bmap);
7265 return NULL;
7269 * returns range - domain
7271 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7273 int i;
7274 isl_space *dim;
7275 struct isl_set *result;
7277 if (!map)
7278 return NULL;
7280 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7281 map->dim, isl_dim_out),
7282 goto error);
7283 dim = isl_map_get_space(map);
7284 dim = isl_space_domain(dim);
7285 result = isl_set_alloc_space(dim, map->n, 0);
7286 if (!result)
7287 goto error;
7288 for (i = 0; i < map->n; ++i)
7289 result = isl_set_add_basic_set(result,
7290 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7291 isl_map_free(map);
7292 return result;
7293 error:
7294 isl_map_free(map);
7295 return NULL;
7299 * returns [domain -> range] -> range - domain
7301 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7302 __isl_take isl_basic_map *bmap)
7304 int i, k;
7305 isl_space *dim;
7306 isl_basic_map *domain;
7307 int nparam, n;
7308 unsigned total;
7310 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7311 isl_die(bmap->ctx, isl_error_invalid,
7312 "domain and range don't match", goto error);
7314 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7315 n = isl_basic_map_dim(bmap, isl_dim_in);
7317 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7318 domain = isl_basic_map_universe(dim);
7320 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7321 bmap = isl_basic_map_apply_range(bmap, domain);
7322 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7324 total = isl_basic_map_total_dim(bmap);
7326 for (i = 0; i < n; ++i) {
7327 k = isl_basic_map_alloc_equality(bmap);
7328 if (k < 0)
7329 goto error;
7330 isl_seq_clr(bmap->eq[k], 1 + total);
7331 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7332 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7333 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7336 bmap = isl_basic_map_gauss(bmap, NULL);
7337 return isl_basic_map_finalize(bmap);
7338 error:
7339 isl_basic_map_free(bmap);
7340 return NULL;
7344 * returns [domain -> range] -> range - domain
7346 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7348 int i;
7349 isl_space *domain_dim;
7351 if (!map)
7352 return NULL;
7354 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7355 isl_die(map->ctx, isl_error_invalid,
7356 "domain and range don't match", goto error);
7358 map = isl_map_cow(map);
7359 if (!map)
7360 return NULL;
7362 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7363 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7364 map->dim = isl_space_join(map->dim, domain_dim);
7365 if (!map->dim)
7366 goto error;
7367 for (i = 0; i < map->n; ++i) {
7368 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7369 if (!map->p[i])
7370 goto error;
7372 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7373 return map;
7374 error:
7375 isl_map_free(map);
7376 return NULL;
7379 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7381 struct isl_basic_map *bmap;
7382 unsigned nparam;
7383 unsigned dim;
7384 int i;
7386 if (!dims)
7387 return NULL;
7389 nparam = dims->nparam;
7390 dim = dims->n_out;
7391 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7392 if (!bmap)
7393 goto error;
7395 for (i = 0; i < dim; ++i) {
7396 int j = isl_basic_map_alloc_equality(bmap);
7397 if (j < 0)
7398 goto error;
7399 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7400 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7401 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7403 return isl_basic_map_finalize(bmap);
7404 error:
7405 isl_basic_map_free(bmap);
7406 return NULL;
7409 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7411 if (!dim)
7412 return NULL;
7413 if (dim->n_in != dim->n_out)
7414 isl_die(dim->ctx, isl_error_invalid,
7415 "number of input and output dimensions needs to be "
7416 "the same", goto error);
7417 return basic_map_identity(dim);
7418 error:
7419 isl_space_free(dim);
7420 return NULL;
7423 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7425 if (!model || !model->dim)
7426 return NULL;
7427 return isl_basic_map_identity(isl_space_copy(model->dim));
7430 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7432 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7435 struct isl_map *isl_map_identity_like(struct isl_map *model)
7437 if (!model || !model->dim)
7438 return NULL;
7439 return isl_map_identity(isl_space_copy(model->dim));
7442 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7444 if (!model || !model->dim)
7445 return NULL;
7446 return isl_map_identity(isl_space_copy(model->dim));
7449 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7451 isl_space *dim = isl_set_get_space(set);
7452 isl_map *id;
7453 id = isl_map_identity(isl_space_map_from_set(dim));
7454 return isl_map_intersect_range(id, set);
7457 /* Construct a basic set with all set dimensions having only non-negative
7458 * values.
7460 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7461 __isl_take isl_space *space)
7463 int i;
7464 unsigned nparam;
7465 unsigned dim;
7466 struct isl_basic_set *bset;
7468 if (!space)
7469 return NULL;
7470 nparam = space->nparam;
7471 dim = space->n_out;
7472 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7473 if (!bset)
7474 return NULL;
7475 for (i = 0; i < dim; ++i) {
7476 int k = isl_basic_set_alloc_inequality(bset);
7477 if (k < 0)
7478 goto error;
7479 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7480 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7482 return bset;
7483 error:
7484 isl_basic_set_free(bset);
7485 return NULL;
7488 /* Construct the half-space x_pos >= 0.
7490 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7491 int pos)
7493 int k;
7494 isl_basic_set *nonneg;
7496 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7497 k = isl_basic_set_alloc_inequality(nonneg);
7498 if (k < 0)
7499 goto error;
7500 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7501 isl_int_set_si(nonneg->ineq[k][pos], 1);
7503 return isl_basic_set_finalize(nonneg);
7504 error:
7505 isl_basic_set_free(nonneg);
7506 return NULL;
7509 /* Construct the half-space x_pos <= -1.
7511 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7513 int k;
7514 isl_basic_set *neg;
7516 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7517 k = isl_basic_set_alloc_inequality(neg);
7518 if (k < 0)
7519 goto error;
7520 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7521 isl_int_set_si(neg->ineq[k][0], -1);
7522 isl_int_set_si(neg->ineq[k][pos], -1);
7524 return isl_basic_set_finalize(neg);
7525 error:
7526 isl_basic_set_free(neg);
7527 return NULL;
7530 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7531 enum isl_dim_type type, unsigned first, unsigned n)
7533 int i;
7534 isl_basic_set *nonneg;
7535 isl_basic_set *neg;
7537 if (!set)
7538 return NULL;
7539 if (n == 0)
7540 return set;
7542 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7544 for (i = 0; i < n; ++i) {
7545 nonneg = nonneg_halfspace(isl_set_get_space(set),
7546 pos(set->dim, type) + first + i);
7547 neg = neg_halfspace(isl_set_get_space(set),
7548 pos(set->dim, type) + first + i);
7550 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7553 return set;
7554 error:
7555 isl_set_free(set);
7556 return NULL;
7559 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7560 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7561 void *user)
7563 isl_set *half;
7565 if (!set)
7566 return -1;
7567 if (isl_set_plain_is_empty(set)) {
7568 isl_set_free(set);
7569 return 0;
7571 if (first == len)
7572 return fn(set, signs, user);
7574 signs[first] = 1;
7575 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7576 1 + first));
7577 half = isl_set_intersect(half, isl_set_copy(set));
7578 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7579 goto error;
7581 signs[first] = -1;
7582 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7583 1 + first));
7584 half = isl_set_intersect(half, set);
7585 return foreach_orthant(half, signs, first + 1, len, fn, user);
7586 error:
7587 isl_set_free(set);
7588 return -1;
7591 /* Call "fn" on the intersections of "set" with each of the orthants
7592 * (except for obviously empty intersections). The orthant is identified
7593 * by the signs array, with each entry having value 1 or -1 according
7594 * to the sign of the corresponding variable.
7596 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7597 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7598 void *user)
7600 unsigned nparam;
7601 unsigned nvar;
7602 int *signs;
7603 int r;
7605 if (!set)
7606 return -1;
7607 if (isl_set_plain_is_empty(set))
7608 return 0;
7610 nparam = isl_set_dim(set, isl_dim_param);
7611 nvar = isl_set_dim(set, isl_dim_set);
7613 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7615 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7616 fn, user);
7618 free(signs);
7620 return r;
7623 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7625 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7628 int isl_basic_map_is_subset(
7629 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7631 int is_subset;
7632 struct isl_map *map1;
7633 struct isl_map *map2;
7635 if (!bmap1 || !bmap2)
7636 return -1;
7638 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7639 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7641 is_subset = isl_map_is_subset(map1, map2);
7643 isl_map_free(map1);
7644 isl_map_free(map2);
7646 return is_subset;
7649 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7650 __isl_keep isl_basic_set *bset2)
7652 return isl_basic_map_is_subset(bset1, bset2);
7655 int isl_basic_map_is_equal(
7656 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7658 int is_subset;
7660 if (!bmap1 || !bmap2)
7661 return -1;
7662 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7663 if (is_subset != 1)
7664 return is_subset;
7665 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7666 return is_subset;
7669 int isl_basic_set_is_equal(
7670 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7672 return isl_basic_map_is_equal(
7673 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7676 int isl_map_is_empty(struct isl_map *map)
7678 int i;
7679 int is_empty;
7681 if (!map)
7682 return -1;
7683 for (i = 0; i < map->n; ++i) {
7684 is_empty = isl_basic_map_is_empty(map->p[i]);
7685 if (is_empty < 0)
7686 return -1;
7687 if (!is_empty)
7688 return 0;
7690 return 1;
7693 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7695 return map ? map->n == 0 : -1;
7698 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7700 return isl_map_plain_is_empty(map);
7703 int isl_set_plain_is_empty(struct isl_set *set)
7705 return set ? set->n == 0 : -1;
7708 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7710 return isl_set_plain_is_empty(set);
7713 int isl_set_is_empty(struct isl_set *set)
7715 return isl_map_is_empty((struct isl_map *)set);
7718 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7720 if (!map1 || !map2)
7721 return -1;
7723 return isl_space_is_equal(map1->dim, map2->dim);
7726 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7728 if (!set1 || !set2)
7729 return -1;
7731 return isl_space_is_equal(set1->dim, set2->dim);
7734 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7736 int is_subset;
7738 if (!map1 || !map2)
7739 return -1;
7740 is_subset = isl_map_is_subset(map1, map2);
7741 if (is_subset != 1)
7742 return is_subset;
7743 is_subset = isl_map_is_subset(map2, map1);
7744 return is_subset;
7747 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7749 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7752 int isl_basic_map_is_strict_subset(
7753 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7755 int is_subset;
7757 if (!bmap1 || !bmap2)
7758 return -1;
7759 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7760 if (is_subset != 1)
7761 return is_subset;
7762 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7763 if (is_subset == -1)
7764 return is_subset;
7765 return !is_subset;
7768 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7770 int is_subset;
7772 if (!map1 || !map2)
7773 return -1;
7774 is_subset = isl_map_is_subset(map1, map2);
7775 if (is_subset != 1)
7776 return is_subset;
7777 is_subset = isl_map_is_subset(map2, map1);
7778 if (is_subset == -1)
7779 return is_subset;
7780 return !is_subset;
7783 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7785 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7788 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7790 if (!bmap)
7791 return -1;
7792 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7795 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7797 if (!bset)
7798 return -1;
7799 return bset->n_eq == 0 && bset->n_ineq == 0;
7802 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7804 int i;
7806 if (!map)
7807 return -1;
7809 for (i = 0; i < map->n; ++i) {
7810 int r = isl_basic_map_is_universe(map->p[i]);
7811 if (r < 0 || r)
7812 return r;
7815 return 0;
7818 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7820 return isl_map_plain_is_universe((isl_map *) set);
7823 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7825 return isl_set_plain_is_universe(set);
7828 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7830 struct isl_basic_set *bset = NULL;
7831 struct isl_vec *sample = NULL;
7832 int empty;
7833 unsigned total;
7835 if (!bmap)
7836 return -1;
7838 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7839 return 1;
7841 if (isl_basic_map_is_universe(bmap))
7842 return 0;
7844 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7845 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7846 copy = isl_basic_map_remove_redundancies(copy);
7847 empty = isl_basic_map_plain_is_empty(copy);
7848 isl_basic_map_free(copy);
7849 return empty;
7852 total = 1 + isl_basic_map_total_dim(bmap);
7853 if (bmap->sample && bmap->sample->size == total) {
7854 int contains = isl_basic_map_contains(bmap, bmap->sample);
7855 if (contains < 0)
7856 return -1;
7857 if (contains)
7858 return 0;
7860 isl_vec_free(bmap->sample);
7861 bmap->sample = NULL;
7862 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7863 if (!bset)
7864 return -1;
7865 sample = isl_basic_set_sample_vec(bset);
7866 if (!sample)
7867 return -1;
7868 empty = sample->size == 0;
7869 isl_vec_free(bmap->sample);
7870 bmap->sample = sample;
7871 if (empty)
7872 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7874 return empty;
7877 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7879 if (!bmap)
7880 return -1;
7881 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7884 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7886 return isl_basic_map_plain_is_empty(bmap);
7889 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7891 if (!bset)
7892 return -1;
7893 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7896 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7898 return isl_basic_set_plain_is_empty(bset);
7901 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7903 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7906 struct isl_map *isl_basic_map_union(
7907 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7909 struct isl_map *map;
7910 if (!bmap1 || !bmap2)
7911 goto error;
7913 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7915 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7916 if (!map)
7917 goto error;
7918 map = isl_map_add_basic_map(map, bmap1);
7919 map = isl_map_add_basic_map(map, bmap2);
7920 return map;
7921 error:
7922 isl_basic_map_free(bmap1);
7923 isl_basic_map_free(bmap2);
7924 return NULL;
7927 struct isl_set *isl_basic_set_union(
7928 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7930 return (struct isl_set *)isl_basic_map_union(
7931 (struct isl_basic_map *)bset1,
7932 (struct isl_basic_map *)bset2);
7935 /* Order divs such that any div only depends on previous divs */
7936 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7938 int i;
7939 unsigned off;
7941 if (!bmap)
7942 return NULL;
7944 off = isl_space_dim(bmap->dim, isl_dim_all);
7946 for (i = 0; i < bmap->n_div; ++i) {
7947 int pos;
7948 if (isl_int_is_zero(bmap->div[i][0]))
7949 continue;
7950 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7951 bmap->n_div-i);
7952 if (pos == -1)
7953 continue;
7954 isl_basic_map_swap_div(bmap, i, i + pos);
7955 --i;
7957 return bmap;
7960 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7962 return (struct isl_basic_set *)
7963 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7966 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7968 int i;
7970 if (!map)
7971 return 0;
7973 for (i = 0; i < map->n; ++i) {
7974 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7975 if (!map->p[i])
7976 goto error;
7979 return map;
7980 error:
7981 isl_map_free(map);
7982 return NULL;
7985 /* Apply the expansion computed by isl_merge_divs.
7986 * The expansion itself is given by "exp" while the resulting
7987 * list of divs is given by "div".
7989 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7990 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7992 int i, j;
7993 int n_div;
7995 bset = isl_basic_set_cow(bset);
7996 if (!bset || !div)
7997 goto error;
7999 if (div->n_row < bset->n_div)
8000 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8001 "not an expansion", goto error);
8003 n_div = bset->n_div;
8004 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8005 div->n_row - n_div, 0,
8006 2 * (div->n_row - n_div));
8008 for (i = n_div; i < div->n_row; ++i)
8009 if (isl_basic_set_alloc_div(bset) < 0)
8010 goto error;
8012 j = n_div - 1;
8013 for (i = div->n_row - 1; i >= 0; --i) {
8014 if (j >= 0 && exp[j] == i) {
8015 if (i != j)
8016 isl_basic_map_swap_div(bset, i, j);
8017 j--;
8018 } else {
8019 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8020 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8021 goto error;
8025 isl_mat_free(div);
8026 return bset;
8027 error:
8028 isl_basic_set_free(bset);
8029 isl_mat_free(div);
8030 return NULL;
8033 /* Look for a div in dst that corresponds to the div "div" in src.
8034 * The divs before "div" in src and dst are assumed to be the same.
8036 * Returns -1 if no corresponding div was found and the position
8037 * of the corresponding div in dst otherwise.
8039 static int find_div(struct isl_basic_map *dst,
8040 struct isl_basic_map *src, unsigned div)
8042 int i;
8044 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8046 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8047 for (i = div; i < dst->n_div; ++i)
8048 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8049 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8050 dst->n_div - div) == -1)
8051 return i;
8052 return -1;
8055 struct isl_basic_map *isl_basic_map_align_divs(
8056 struct isl_basic_map *dst, struct isl_basic_map *src)
8058 int i;
8059 unsigned total;
8061 if (!dst || !src)
8062 goto error;
8064 if (src->n_div == 0)
8065 return dst;
8067 for (i = 0; i < src->n_div; ++i)
8068 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
8070 src = isl_basic_map_order_divs(src);
8071 dst = isl_basic_map_cow(dst);
8072 if (!dst)
8073 return NULL;
8074 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
8075 src->n_div, 0, 2 * src->n_div);
8076 if (!dst)
8077 return NULL;
8078 total = isl_space_dim(src->dim, isl_dim_all);
8079 for (i = 0; i < src->n_div; ++i) {
8080 int j = find_div(dst, src, i);
8081 if (j < 0) {
8082 j = isl_basic_map_alloc_div(dst);
8083 if (j < 0)
8084 goto error;
8085 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8086 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8087 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8088 goto error;
8090 if (j != i)
8091 isl_basic_map_swap_div(dst, i, j);
8093 return dst;
8094 error:
8095 isl_basic_map_free(dst);
8096 return NULL;
8099 struct isl_basic_set *isl_basic_set_align_divs(
8100 struct isl_basic_set *dst, struct isl_basic_set *src)
8102 return (struct isl_basic_set *)isl_basic_map_align_divs(
8103 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8106 struct isl_map *isl_map_align_divs(struct isl_map *map)
8108 int i;
8110 if (!map)
8111 return NULL;
8112 if (map->n == 0)
8113 return map;
8114 map = isl_map_compute_divs(map);
8115 map = isl_map_cow(map);
8116 if (!map)
8117 return NULL;
8119 for (i = 1; i < map->n; ++i)
8120 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8121 for (i = 1; i < map->n; ++i) {
8122 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8123 if (!map->p[i])
8124 return isl_map_free(map);
8127 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8128 return map;
8131 struct isl_set *isl_set_align_divs(struct isl_set *set)
8133 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8136 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8137 __isl_take isl_map *map)
8139 if (!set || !map)
8140 goto error;
8141 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8142 map = isl_map_intersect_domain(map, set);
8143 set = isl_map_range(map);
8144 return set;
8145 error:
8146 isl_set_free(set);
8147 isl_map_free(map);
8148 return NULL;
8151 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8152 __isl_take isl_map *map)
8154 return isl_map_align_params_map_map_and(set, map, &set_apply);
8157 /* There is no need to cow as removing empty parts doesn't change
8158 * the meaning of the set.
8160 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8162 int i;
8164 if (!map)
8165 return NULL;
8167 for (i = map->n - 1; i >= 0; --i)
8168 remove_if_empty(map, i);
8170 return map;
8173 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8175 return (struct isl_set *)
8176 isl_map_remove_empty_parts((struct isl_map *)set);
8179 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8181 struct isl_basic_map *bmap;
8182 if (!map || map->n == 0)
8183 return NULL;
8184 bmap = map->p[map->n-1];
8185 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8186 return isl_basic_map_copy(bmap);
8189 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8191 return (struct isl_basic_set *)
8192 isl_map_copy_basic_map((struct isl_map *)set);
8195 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8196 __isl_keep isl_basic_map *bmap)
8198 int i;
8200 if (!map || !bmap)
8201 goto error;
8202 for (i = map->n-1; i >= 0; --i) {
8203 if (map->p[i] != bmap)
8204 continue;
8205 map = isl_map_cow(map);
8206 if (!map)
8207 goto error;
8208 isl_basic_map_free(map->p[i]);
8209 if (i != map->n-1) {
8210 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8211 map->p[i] = map->p[map->n-1];
8213 map->n--;
8214 return map;
8216 return map;
8217 error:
8218 isl_map_free(map);
8219 return NULL;
8222 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8223 struct isl_basic_set *bset)
8225 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8226 (struct isl_basic_map *)bset);
8229 /* Given two basic sets bset1 and bset2, compute the maximal difference
8230 * between the values of dimension pos in bset1 and those in bset2
8231 * for any common value of the parameters and dimensions preceding pos.
8233 static enum isl_lp_result basic_set_maximal_difference_at(
8234 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8235 int pos, isl_int *opt)
8237 isl_space *dims;
8238 struct isl_basic_map *bmap1 = NULL;
8239 struct isl_basic_map *bmap2 = NULL;
8240 struct isl_ctx *ctx;
8241 struct isl_vec *obj;
8242 unsigned total;
8243 unsigned nparam;
8244 unsigned dim1, dim2;
8245 enum isl_lp_result res;
8247 if (!bset1 || !bset2)
8248 return isl_lp_error;
8250 nparam = isl_basic_set_n_param(bset1);
8251 dim1 = isl_basic_set_n_dim(bset1);
8252 dim2 = isl_basic_set_n_dim(bset2);
8253 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8254 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8255 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8256 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8257 if (!bmap1 || !bmap2)
8258 goto error;
8259 bmap1 = isl_basic_map_cow(bmap1);
8260 bmap1 = isl_basic_map_extend(bmap1, nparam,
8261 pos, (dim1 - pos) + (dim2 - pos),
8262 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8263 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8264 if (!bmap1)
8265 goto error;
8266 total = isl_basic_map_total_dim(bmap1);
8267 ctx = bmap1->ctx;
8268 obj = isl_vec_alloc(ctx, 1 + total);
8269 if (!obj)
8270 goto error2;
8271 isl_seq_clr(obj->block.data, 1 + total);
8272 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8273 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8274 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8275 opt, NULL, NULL);
8276 isl_basic_map_free(bmap1);
8277 isl_vec_free(obj);
8278 return res;
8279 error:
8280 isl_basic_map_free(bmap2);
8281 error2:
8282 isl_basic_map_free(bmap1);
8283 return isl_lp_error;
8286 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8287 * for any common value of the parameters and dimensions preceding pos
8288 * in both basic sets, the values of dimension pos in bset1 are
8289 * smaller or larger than those in bset2.
8291 * Returns
8292 * 1 if bset1 follows bset2
8293 * -1 if bset1 precedes bset2
8294 * 0 if bset1 and bset2 are incomparable
8295 * -2 if some error occurred.
8297 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8298 struct isl_basic_set *bset2, int pos)
8300 isl_int opt;
8301 enum isl_lp_result res;
8302 int cmp;
8304 isl_int_init(opt);
8306 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8308 if (res == isl_lp_empty)
8309 cmp = 0;
8310 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8311 res == isl_lp_unbounded)
8312 cmp = 1;
8313 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8314 cmp = -1;
8315 else
8316 cmp = -2;
8318 isl_int_clear(opt);
8319 return cmp;
8322 /* Given two basic sets bset1 and bset2, check whether
8323 * for any common value of the parameters and dimensions preceding pos
8324 * there is a value of dimension pos in bset1 that is larger
8325 * than a value of the same dimension in bset2.
8327 * Return
8328 * 1 if there exists such a pair
8329 * 0 if there is no such pair, but there is a pair of equal values
8330 * -1 otherwise
8331 * -2 if some error occurred.
8333 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8334 __isl_keep isl_basic_set *bset2, int pos)
8336 isl_int opt;
8337 enum isl_lp_result res;
8338 int cmp;
8340 isl_int_init(opt);
8342 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8344 if (res == isl_lp_empty)
8345 cmp = -1;
8346 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8347 res == isl_lp_unbounded)
8348 cmp = 1;
8349 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8350 cmp = -1;
8351 else if (res == isl_lp_ok)
8352 cmp = 0;
8353 else
8354 cmp = -2;
8356 isl_int_clear(opt);
8357 return cmp;
8360 /* Given two sets set1 and set2, check whether
8361 * for any common value of the parameters and dimensions preceding pos
8362 * there is a value of dimension pos in set1 that is larger
8363 * than a value of the same dimension in set2.
8365 * Return
8366 * 1 if there exists such a pair
8367 * 0 if there is no such pair, but there is a pair of equal values
8368 * -1 otherwise
8369 * -2 if some error occurred.
8371 int isl_set_follows_at(__isl_keep isl_set *set1,
8372 __isl_keep isl_set *set2, int pos)
8374 int i, j;
8375 int follows = -1;
8377 if (!set1 || !set2)
8378 return -2;
8380 for (i = 0; i < set1->n; ++i)
8381 for (j = 0; j < set2->n; ++j) {
8382 int f;
8383 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8384 if (f == 1 || f == -2)
8385 return f;
8386 if (f > follows)
8387 follows = f;
8390 return follows;
8393 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8394 unsigned pos, isl_int *val)
8396 int i;
8397 int d;
8398 unsigned total;
8400 if (!bmap)
8401 return -1;
8402 total = isl_basic_map_total_dim(bmap);
8403 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8404 for (; d+1 > pos; --d)
8405 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8406 break;
8407 if (d != pos)
8408 continue;
8409 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8410 return 0;
8411 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8412 return 0;
8413 if (!isl_int_is_one(bmap->eq[i][1+d]))
8414 return 0;
8415 if (val)
8416 isl_int_neg(*val, bmap->eq[i][0]);
8417 return 1;
8419 return 0;
8422 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8423 unsigned pos, isl_int *val)
8425 int i;
8426 isl_int v;
8427 isl_int tmp;
8428 int fixed;
8430 if (!map)
8431 return -1;
8432 if (map->n == 0)
8433 return 0;
8434 if (map->n == 1)
8435 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8436 isl_int_init(v);
8437 isl_int_init(tmp);
8438 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8439 for (i = 1; fixed == 1 && i < map->n; ++i) {
8440 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8441 if (fixed == 1 && isl_int_ne(tmp, v))
8442 fixed = 0;
8444 if (val)
8445 isl_int_set(*val, v);
8446 isl_int_clear(tmp);
8447 isl_int_clear(v);
8448 return fixed;
8451 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8452 unsigned pos, isl_int *val)
8454 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8455 pos, val);
8458 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8459 isl_int *val)
8461 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8464 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8465 enum isl_dim_type type, unsigned pos, isl_int *val)
8467 if (pos >= isl_basic_map_dim(bmap, type))
8468 return -1;
8469 return isl_basic_map_plain_has_fixed_var(bmap,
8470 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8473 /* If "bmap" obviously lies on a hyperplane where the given dimension
8474 * has a fixed value, then return that value.
8475 * Otherwise return NaN.
8477 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8478 __isl_keep isl_basic_map *bmap,
8479 enum isl_dim_type type, unsigned pos)
8481 isl_ctx *ctx;
8482 isl_val *v;
8483 int fixed;
8485 if (!bmap)
8486 return NULL;
8487 ctx = isl_basic_map_get_ctx(bmap);
8488 v = isl_val_alloc(ctx);
8489 if (!v)
8490 return NULL;
8491 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8492 if (fixed < 0)
8493 return isl_val_free(v);
8494 if (fixed) {
8495 isl_int_set_si(v->d, 1);
8496 return v;
8498 isl_val_free(v);
8499 return isl_val_nan(ctx);
8502 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8503 enum isl_dim_type type, unsigned pos, isl_int *val)
8505 if (pos >= isl_map_dim(map, type))
8506 return -1;
8507 return isl_map_plain_has_fixed_var(map,
8508 map_offset(map, type) - 1 + pos, val);
8511 /* If "map" obviously lies on a hyperplane where the given dimension
8512 * has a fixed value, then return that value.
8513 * Otherwise return NaN.
8515 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8516 enum isl_dim_type type, unsigned pos)
8518 isl_ctx *ctx;
8519 isl_val *v;
8520 int fixed;
8522 if (!map)
8523 return NULL;
8524 ctx = isl_map_get_ctx(map);
8525 v = isl_val_alloc(ctx);
8526 if (!v)
8527 return NULL;
8528 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8529 if (fixed < 0)
8530 return isl_val_free(v);
8531 if (fixed) {
8532 isl_int_set_si(v->d, 1);
8533 return v;
8535 isl_val_free(v);
8536 return isl_val_nan(ctx);
8539 /* If "set" obviously lies on a hyperplane where the given dimension
8540 * has a fixed value, then return that value.
8541 * Otherwise return NaN.
8543 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8544 enum isl_dim_type type, unsigned pos)
8546 return isl_map_plain_get_val_if_fixed(set, type, pos);
8549 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8550 enum isl_dim_type type, unsigned pos, isl_int *val)
8552 return isl_map_plain_is_fixed(set, type, pos, val);
8555 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8556 enum isl_dim_type type, unsigned pos, isl_int *val)
8558 return isl_map_plain_is_fixed(map, type, pos, val);
8561 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8562 * then return this fixed value in *val.
8564 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8565 unsigned dim, isl_int *val)
8567 return isl_basic_set_plain_has_fixed_var(bset,
8568 isl_basic_set_n_param(bset) + dim, val);
8571 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8572 * then return this fixed value in *val.
8574 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8575 unsigned dim, isl_int *val)
8577 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8580 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8581 unsigned dim, isl_int *val)
8583 return isl_set_plain_dim_is_fixed(set, dim, val);
8586 /* Check if input variable in has fixed value and if so and if val is not NULL,
8587 * then return this fixed value in *val.
8589 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8590 unsigned in, isl_int *val)
8592 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8595 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8596 * and if val is not NULL, then return this lower bound in *val.
8598 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8599 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8601 int i, i_eq = -1, i_ineq = -1;
8602 isl_int *c;
8603 unsigned total;
8604 unsigned nparam;
8606 if (!bset)
8607 return -1;
8608 total = isl_basic_set_total_dim(bset);
8609 nparam = isl_basic_set_n_param(bset);
8610 for (i = 0; i < bset->n_eq; ++i) {
8611 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8612 continue;
8613 if (i_eq != -1)
8614 return 0;
8615 i_eq = i;
8617 for (i = 0; i < bset->n_ineq; ++i) {
8618 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8619 continue;
8620 if (i_eq != -1 || i_ineq != -1)
8621 return 0;
8622 i_ineq = i;
8624 if (i_eq == -1 && i_ineq == -1)
8625 return 0;
8626 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8627 /* The coefficient should always be one due to normalization. */
8628 if (!isl_int_is_one(c[1+nparam+dim]))
8629 return 0;
8630 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8631 return 0;
8632 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8633 total - nparam - dim - 1) != -1)
8634 return 0;
8635 if (val)
8636 isl_int_neg(*val, c[0]);
8637 return 1;
8640 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8641 unsigned dim, isl_int *val)
8643 int i;
8644 isl_int v;
8645 isl_int tmp;
8646 int fixed;
8648 if (!set)
8649 return -1;
8650 if (set->n == 0)
8651 return 0;
8652 if (set->n == 1)
8653 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8654 dim, val);
8655 isl_int_init(v);
8656 isl_int_init(tmp);
8657 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8658 dim, &v);
8659 for (i = 1; fixed == 1 && i < set->n; ++i) {
8660 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8661 dim, &tmp);
8662 if (fixed == 1 && isl_int_ne(tmp, v))
8663 fixed = 0;
8665 if (val)
8666 isl_int_set(*val, v);
8667 isl_int_clear(tmp);
8668 isl_int_clear(v);
8669 return fixed;
8672 struct constraint {
8673 unsigned size;
8674 isl_int *c;
8677 /* uset_gist depends on constraints without existentially quantified
8678 * variables sorting first.
8680 static int qsort_constraint_cmp(const void *p1, const void *p2)
8682 const struct constraint *c1 = (const struct constraint *)p1;
8683 const struct constraint *c2 = (const struct constraint *)p2;
8684 int l1, l2;
8685 unsigned size = isl_min(c1->size, c2->size);
8687 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8688 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8690 if (l1 != l2)
8691 return l1 - l2;
8693 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8696 static struct isl_basic_map *isl_basic_map_sort_constraints(
8697 struct isl_basic_map *bmap)
8699 int i;
8700 struct constraint *c;
8701 unsigned total;
8703 if (!bmap)
8704 return NULL;
8705 if (bmap->n_ineq == 0)
8706 return bmap;
8707 total = isl_basic_map_total_dim(bmap);
8708 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8709 if (!c)
8710 goto error;
8711 for (i = 0; i < bmap->n_ineq; ++i) {
8712 c[i].size = total;
8713 c[i].c = bmap->ineq[i];
8715 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8716 for (i = 0; i < bmap->n_ineq; ++i)
8717 bmap->ineq[i] = c[i].c;
8718 free(c);
8719 return bmap;
8720 error:
8721 isl_basic_map_free(bmap);
8722 return NULL;
8725 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8726 __isl_take isl_basic_set *bset)
8728 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8729 (struct isl_basic_map *)bset);
8732 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8734 if (!bmap)
8735 return NULL;
8736 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8737 return bmap;
8738 bmap = isl_basic_map_remove_redundancies(bmap);
8739 bmap = isl_basic_map_sort_constraints(bmap);
8740 if (bmap)
8741 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8742 return bmap;
8745 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8747 return (struct isl_basic_set *)isl_basic_map_normalize(
8748 (struct isl_basic_map *)bset);
8751 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8752 const __isl_keep isl_basic_map *bmap2)
8754 int i, cmp;
8755 unsigned total;
8757 if (bmap1 == bmap2)
8758 return 0;
8759 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8760 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8761 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8762 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8763 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8764 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8765 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8766 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8767 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8768 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8769 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8770 return 0;
8771 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8772 return 1;
8773 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8774 return -1;
8775 if (bmap1->n_eq != bmap2->n_eq)
8776 return bmap1->n_eq - bmap2->n_eq;
8777 if (bmap1->n_ineq != bmap2->n_ineq)
8778 return bmap1->n_ineq - bmap2->n_ineq;
8779 if (bmap1->n_div != bmap2->n_div)
8780 return bmap1->n_div - bmap2->n_div;
8781 total = isl_basic_map_total_dim(bmap1);
8782 for (i = 0; i < bmap1->n_eq; ++i) {
8783 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8784 if (cmp)
8785 return cmp;
8787 for (i = 0; i < bmap1->n_ineq; ++i) {
8788 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8789 if (cmp)
8790 return cmp;
8792 for (i = 0; i < bmap1->n_div; ++i) {
8793 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8794 if (cmp)
8795 return cmp;
8797 return 0;
8800 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8801 const __isl_keep isl_basic_set *bset2)
8803 return isl_basic_map_plain_cmp(bset1, bset2);
8806 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8808 int i, cmp;
8810 if (set1 == set2)
8811 return 0;
8812 if (set1->n != set2->n)
8813 return set1->n - set2->n;
8815 for (i = 0; i < set1->n; ++i) {
8816 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8817 if (cmp)
8818 return cmp;
8821 return 0;
8824 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8825 __isl_keep isl_basic_map *bmap2)
8827 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8830 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8831 __isl_keep isl_basic_set *bset2)
8833 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8834 (isl_basic_map *)bset2);
8837 static int qsort_bmap_cmp(const void *p1, const void *p2)
8839 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8840 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8842 return isl_basic_map_plain_cmp(bmap1, bmap2);
8845 /* We normalize in place, but if anything goes wrong we need
8846 * to return NULL, so we need to make sure we don't change the
8847 * meaning of any possible other copies of map.
8849 struct isl_map *isl_map_normalize(struct isl_map *map)
8851 int i, j;
8852 struct isl_basic_map *bmap;
8854 if (!map)
8855 return NULL;
8856 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8857 return map;
8858 for (i = 0; i < map->n; ++i) {
8859 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8860 if (!bmap)
8861 goto error;
8862 isl_basic_map_free(map->p[i]);
8863 map->p[i] = bmap;
8865 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8866 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8867 map = isl_map_remove_empty_parts(map);
8868 if (!map)
8869 return NULL;
8870 for (i = map->n - 1; i >= 1; --i) {
8871 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8872 continue;
8873 isl_basic_map_free(map->p[i-1]);
8874 for (j = i; j < map->n; ++j)
8875 map->p[j-1] = map->p[j];
8876 map->n--;
8878 return map;
8879 error:
8880 isl_map_free(map);
8881 return NULL;
8885 struct isl_set *isl_set_normalize(struct isl_set *set)
8887 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8890 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8892 int i;
8893 int equal;
8895 if (!map1 || !map2)
8896 return -1;
8898 if (map1 == map2)
8899 return 1;
8900 if (!isl_space_is_equal(map1->dim, map2->dim))
8901 return 0;
8903 map1 = isl_map_copy(map1);
8904 map2 = isl_map_copy(map2);
8905 map1 = isl_map_normalize(map1);
8906 map2 = isl_map_normalize(map2);
8907 if (!map1 || !map2)
8908 goto error;
8909 equal = map1->n == map2->n;
8910 for (i = 0; equal && i < map1->n; ++i) {
8911 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8912 if (equal < 0)
8913 goto error;
8915 isl_map_free(map1);
8916 isl_map_free(map2);
8917 return equal;
8918 error:
8919 isl_map_free(map1);
8920 isl_map_free(map2);
8921 return -1;
8924 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8926 return isl_map_plain_is_equal(map1, map2);
8929 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8931 return isl_map_plain_is_equal((struct isl_map *)set1,
8932 (struct isl_map *)set2);
8935 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8937 return isl_set_plain_is_equal(set1, set2);
8940 /* Return an interval that ranges from min to max (inclusive)
8942 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8943 isl_int min, isl_int max)
8945 int k;
8946 struct isl_basic_set *bset = NULL;
8948 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8949 if (!bset)
8950 goto error;
8952 k = isl_basic_set_alloc_inequality(bset);
8953 if (k < 0)
8954 goto error;
8955 isl_int_set_si(bset->ineq[k][1], 1);
8956 isl_int_neg(bset->ineq[k][0], min);
8958 k = isl_basic_set_alloc_inequality(bset);
8959 if (k < 0)
8960 goto error;
8961 isl_int_set_si(bset->ineq[k][1], -1);
8962 isl_int_set(bset->ineq[k][0], max);
8964 return bset;
8965 error:
8966 isl_basic_set_free(bset);
8967 return NULL;
8970 /* Return the Cartesian product of the basic sets in list (in the given order).
8972 __isl_give isl_basic_set *isl_basic_set_list_product(
8973 __isl_take struct isl_basic_set_list *list)
8975 int i;
8976 unsigned dim;
8977 unsigned nparam;
8978 unsigned extra;
8979 unsigned n_eq;
8980 unsigned n_ineq;
8981 struct isl_basic_set *product = NULL;
8983 if (!list)
8984 goto error;
8985 isl_assert(list->ctx, list->n > 0, goto error);
8986 isl_assert(list->ctx, list->p[0], goto error);
8987 nparam = isl_basic_set_n_param(list->p[0]);
8988 dim = isl_basic_set_n_dim(list->p[0]);
8989 extra = list->p[0]->n_div;
8990 n_eq = list->p[0]->n_eq;
8991 n_ineq = list->p[0]->n_ineq;
8992 for (i = 1; i < list->n; ++i) {
8993 isl_assert(list->ctx, list->p[i], goto error);
8994 isl_assert(list->ctx,
8995 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8996 dim += isl_basic_set_n_dim(list->p[i]);
8997 extra += list->p[i]->n_div;
8998 n_eq += list->p[i]->n_eq;
8999 n_ineq += list->p[i]->n_ineq;
9001 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9002 n_eq, n_ineq);
9003 if (!product)
9004 goto error;
9005 dim = 0;
9006 for (i = 0; i < list->n; ++i) {
9007 isl_basic_set_add_constraints(product,
9008 isl_basic_set_copy(list->p[i]), dim);
9009 dim += isl_basic_set_n_dim(list->p[i]);
9011 isl_basic_set_list_free(list);
9012 return product;
9013 error:
9014 isl_basic_set_free(product);
9015 isl_basic_set_list_free(list);
9016 return NULL;
9019 struct isl_basic_map *isl_basic_map_product(
9020 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9022 isl_space *dim_result = NULL;
9023 struct isl_basic_map *bmap;
9024 unsigned in1, in2, out1, out2, nparam, total, pos;
9025 struct isl_dim_map *dim_map1, *dim_map2;
9027 if (!bmap1 || !bmap2)
9028 goto error;
9030 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9031 bmap2->dim, isl_dim_param), goto error);
9032 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9033 isl_space_copy(bmap2->dim));
9035 in1 = isl_basic_map_n_in(bmap1);
9036 in2 = isl_basic_map_n_in(bmap2);
9037 out1 = isl_basic_map_n_out(bmap1);
9038 out2 = isl_basic_map_n_out(bmap2);
9039 nparam = isl_basic_map_n_param(bmap1);
9041 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9042 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9043 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9044 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9045 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9046 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9047 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9048 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9049 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9050 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9051 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9053 bmap = isl_basic_map_alloc_space(dim_result,
9054 bmap1->n_div + bmap2->n_div,
9055 bmap1->n_eq + bmap2->n_eq,
9056 bmap1->n_ineq + bmap2->n_ineq);
9057 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9058 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9059 bmap = isl_basic_map_simplify(bmap);
9060 return isl_basic_map_finalize(bmap);
9061 error:
9062 isl_basic_map_free(bmap1);
9063 isl_basic_map_free(bmap2);
9064 return NULL;
9067 __isl_give isl_basic_map *isl_basic_map_flat_product(
9068 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9070 isl_basic_map *prod;
9072 prod = isl_basic_map_product(bmap1, bmap2);
9073 prod = isl_basic_map_flatten(prod);
9074 return prod;
9077 __isl_give isl_basic_set *isl_basic_set_flat_product(
9078 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9080 return isl_basic_map_flat_range_product(bset1, bset2);
9083 __isl_give isl_basic_map *isl_basic_map_domain_product(
9084 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9086 isl_space *space_result = NULL;
9087 isl_basic_map *bmap;
9088 unsigned in1, in2, out, nparam, total, pos;
9089 struct isl_dim_map *dim_map1, *dim_map2;
9091 if (!bmap1 || !bmap2)
9092 goto error;
9094 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9095 isl_space_copy(bmap2->dim));
9097 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9098 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9099 out = isl_basic_map_dim(bmap1, isl_dim_out);
9100 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9102 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9103 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9104 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9105 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9106 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9107 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9108 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9109 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9110 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9111 isl_dim_map_div(dim_map1, bmap1, pos += out);
9112 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9114 bmap = isl_basic_map_alloc_space(space_result,
9115 bmap1->n_div + bmap2->n_div,
9116 bmap1->n_eq + bmap2->n_eq,
9117 bmap1->n_ineq + bmap2->n_ineq);
9118 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9119 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9120 bmap = isl_basic_map_simplify(bmap);
9121 return isl_basic_map_finalize(bmap);
9122 error:
9123 isl_basic_map_free(bmap1);
9124 isl_basic_map_free(bmap2);
9125 return NULL;
9128 __isl_give isl_basic_map *isl_basic_map_range_product(
9129 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9131 isl_space *dim_result = NULL;
9132 isl_basic_map *bmap;
9133 unsigned in, out1, out2, nparam, total, pos;
9134 struct isl_dim_map *dim_map1, *dim_map2;
9136 if (!bmap1 || !bmap2)
9137 goto error;
9139 if (!isl_space_match(bmap1->dim, isl_dim_param,
9140 bmap2->dim, isl_dim_param))
9141 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9142 "parameters don't match", goto error);
9144 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9145 isl_space_copy(bmap2->dim));
9147 in = isl_basic_map_dim(bmap1, isl_dim_in);
9148 out1 = isl_basic_map_n_out(bmap1);
9149 out2 = isl_basic_map_n_out(bmap2);
9150 nparam = isl_basic_map_n_param(bmap1);
9152 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9153 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9154 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9155 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9156 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9157 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9158 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9159 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9160 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9161 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9162 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9164 bmap = isl_basic_map_alloc_space(dim_result,
9165 bmap1->n_div + bmap2->n_div,
9166 bmap1->n_eq + bmap2->n_eq,
9167 bmap1->n_ineq + bmap2->n_ineq);
9168 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9169 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9170 bmap = isl_basic_map_simplify(bmap);
9171 return isl_basic_map_finalize(bmap);
9172 error:
9173 isl_basic_map_free(bmap1);
9174 isl_basic_map_free(bmap2);
9175 return NULL;
9178 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9179 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9181 isl_basic_map *prod;
9183 prod = isl_basic_map_range_product(bmap1, bmap2);
9184 prod = isl_basic_map_flatten_range(prod);
9185 return prod;
9188 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9189 __isl_take isl_map *map2,
9190 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9191 __isl_take isl_space *right),
9192 __isl_give isl_basic_map *(*basic_map_product)(
9193 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9195 unsigned flags = 0;
9196 struct isl_map *result;
9197 int i, j;
9199 if (!map1 || !map2)
9200 goto error;
9202 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9203 map2->dim, isl_dim_param), goto error);
9205 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9206 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9207 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9209 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9210 isl_space_copy(map2->dim)),
9211 map1->n * map2->n, flags);
9212 if (!result)
9213 goto error;
9214 for (i = 0; i < map1->n; ++i)
9215 for (j = 0; j < map2->n; ++j) {
9216 struct isl_basic_map *part;
9217 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9218 isl_basic_map_copy(map2->p[j]));
9219 if (isl_basic_map_is_empty(part))
9220 isl_basic_map_free(part);
9221 else
9222 result = isl_map_add_basic_map(result, part);
9223 if (!result)
9224 goto error;
9226 isl_map_free(map1);
9227 isl_map_free(map2);
9228 return result;
9229 error:
9230 isl_map_free(map1);
9231 isl_map_free(map2);
9232 return NULL;
9235 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9237 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9238 __isl_take isl_map *map2)
9240 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9243 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9244 __isl_take isl_map *map2)
9246 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9249 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9251 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9252 __isl_take isl_map *map2)
9254 isl_map *prod;
9256 prod = isl_map_product(map1, map2);
9257 prod = isl_map_flatten(prod);
9258 return prod;
9261 /* Given two set A and B, construct its Cartesian product A x B.
9263 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9265 return isl_map_range_product(set1, set2);
9268 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9269 __isl_take isl_set *set2)
9271 return isl_map_flat_range_product(set1, set2);
9274 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9276 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9277 __isl_take isl_map *map2)
9279 return map_product(map1, map2, &isl_space_domain_product,
9280 &isl_basic_map_domain_product);
9283 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9285 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9286 __isl_take isl_map *map2)
9288 return map_product(map1, map2, &isl_space_range_product,
9289 &isl_basic_map_range_product);
9292 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9293 __isl_take isl_map *map2)
9295 return isl_map_align_params_map_map_and(map1, map2,
9296 &map_domain_product_aligned);
9299 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9300 __isl_take isl_map *map2)
9302 return isl_map_align_params_map_map_and(map1, map2,
9303 &map_range_product_aligned);
9306 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9308 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9309 __isl_take isl_map *map2)
9311 isl_map *prod;
9313 prod = isl_map_domain_product(map1, map2);
9314 prod = isl_map_flatten_domain(prod);
9315 return prod;
9318 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9320 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9321 __isl_take isl_map *map2)
9323 isl_map *prod;
9325 prod = isl_map_range_product(map1, map2);
9326 prod = isl_map_flatten_range(prod);
9327 return prod;
9330 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9332 int i;
9333 uint32_t hash = isl_hash_init();
9334 unsigned total;
9336 if (!bmap)
9337 return 0;
9338 bmap = isl_basic_map_copy(bmap);
9339 bmap = isl_basic_map_normalize(bmap);
9340 if (!bmap)
9341 return 0;
9342 total = isl_basic_map_total_dim(bmap);
9343 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9344 for (i = 0; i < bmap->n_eq; ++i) {
9345 uint32_t c_hash;
9346 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9347 isl_hash_hash(hash, c_hash);
9349 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9350 for (i = 0; i < bmap->n_ineq; ++i) {
9351 uint32_t c_hash;
9352 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9353 isl_hash_hash(hash, c_hash);
9355 isl_hash_byte(hash, bmap->n_div & 0xFF);
9356 for (i = 0; i < bmap->n_div; ++i) {
9357 uint32_t c_hash;
9358 if (isl_int_is_zero(bmap->div[i][0]))
9359 continue;
9360 isl_hash_byte(hash, i & 0xFF);
9361 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9362 isl_hash_hash(hash, c_hash);
9364 isl_basic_map_free(bmap);
9365 return hash;
9368 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9370 return isl_basic_map_get_hash((isl_basic_map *)bset);
9373 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9375 int i;
9376 uint32_t hash;
9378 if (!map)
9379 return 0;
9380 map = isl_map_copy(map);
9381 map = isl_map_normalize(map);
9382 if (!map)
9383 return 0;
9385 hash = isl_hash_init();
9386 for (i = 0; i < map->n; ++i) {
9387 uint32_t bmap_hash;
9388 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9389 isl_hash_hash(hash, bmap_hash);
9392 isl_map_free(map);
9394 return hash;
9397 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9399 return isl_map_get_hash((isl_map *)set);
9402 /* Check if the value for dimension dim is completely determined
9403 * by the values of the other parameters and variables.
9404 * That is, check if dimension dim is involved in an equality.
9406 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9408 int i;
9409 unsigned nparam;
9411 if (!bset)
9412 return -1;
9413 nparam = isl_basic_set_n_param(bset);
9414 for (i = 0; i < bset->n_eq; ++i)
9415 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9416 return 1;
9417 return 0;
9420 /* Check if the value for dimension dim is completely determined
9421 * by the values of the other parameters and variables.
9422 * That is, check if dimension dim is involved in an equality
9423 * for each of the subsets.
9425 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9427 int i;
9429 if (!set)
9430 return -1;
9431 for (i = 0; i < set->n; ++i) {
9432 int unique;
9433 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9434 if (unique != 1)
9435 return unique;
9437 return 1;
9440 int isl_set_n_basic_set(__isl_keep isl_set *set)
9442 return set ? set->n : 0;
9445 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9446 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9448 int i;
9450 if (!map)
9451 return -1;
9453 for (i = 0; i < map->n; ++i)
9454 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9455 return -1;
9457 return 0;
9460 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9461 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9463 int i;
9465 if (!set)
9466 return -1;
9468 for (i = 0; i < set->n; ++i)
9469 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9470 return -1;
9472 return 0;
9475 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9477 isl_space *dim;
9479 if (!bset)
9480 return NULL;
9482 bset = isl_basic_set_cow(bset);
9483 if (!bset)
9484 return NULL;
9486 dim = isl_basic_set_get_space(bset);
9487 dim = isl_space_lift(dim, bset->n_div);
9488 if (!dim)
9489 goto error;
9490 isl_space_free(bset->dim);
9491 bset->dim = dim;
9492 bset->extra -= bset->n_div;
9493 bset->n_div = 0;
9495 bset = isl_basic_set_finalize(bset);
9497 return bset;
9498 error:
9499 isl_basic_set_free(bset);
9500 return NULL;
9503 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9505 int i;
9506 isl_space *dim;
9507 unsigned n_div;
9509 set = isl_set_align_divs(set);
9511 if (!set)
9512 return NULL;
9514 set = isl_set_cow(set);
9515 if (!set)
9516 return NULL;
9518 n_div = set->p[0]->n_div;
9519 dim = isl_set_get_space(set);
9520 dim = isl_space_lift(dim, n_div);
9521 if (!dim)
9522 goto error;
9523 isl_space_free(set->dim);
9524 set->dim = dim;
9526 for (i = 0; i < set->n; ++i) {
9527 set->p[i] = isl_basic_set_lift(set->p[i]);
9528 if (!set->p[i])
9529 goto error;
9532 return set;
9533 error:
9534 isl_set_free(set);
9535 return NULL;
9538 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9540 isl_space *dim;
9541 struct isl_basic_map *bmap;
9542 unsigned n_set;
9543 unsigned n_div;
9544 unsigned n_param;
9545 unsigned total;
9546 int i, k, l;
9548 set = isl_set_align_divs(set);
9550 if (!set)
9551 return NULL;
9553 dim = isl_set_get_space(set);
9554 if (set->n == 0 || set->p[0]->n_div == 0) {
9555 isl_set_free(set);
9556 return isl_map_identity(isl_space_map_from_set(dim));
9559 n_div = set->p[0]->n_div;
9560 dim = isl_space_map_from_set(dim);
9561 n_param = isl_space_dim(dim, isl_dim_param);
9562 n_set = isl_space_dim(dim, isl_dim_in);
9563 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9564 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9565 for (i = 0; i < n_set; ++i)
9566 bmap = var_equal(bmap, i);
9568 total = n_param + n_set + n_set + n_div;
9569 for (i = 0; i < n_div; ++i) {
9570 k = isl_basic_map_alloc_inequality(bmap);
9571 if (k < 0)
9572 goto error;
9573 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9574 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9575 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9576 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9577 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9578 set->p[0]->div[i][0]);
9580 l = isl_basic_map_alloc_inequality(bmap);
9581 if (l < 0)
9582 goto error;
9583 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9584 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9585 set->p[0]->div[i][0]);
9586 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9589 isl_set_free(set);
9590 bmap = isl_basic_map_simplify(bmap);
9591 bmap = isl_basic_map_finalize(bmap);
9592 return isl_map_from_basic_map(bmap);
9593 error:
9594 isl_set_free(set);
9595 isl_basic_map_free(bmap);
9596 return NULL;
9599 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9601 unsigned dim;
9602 int size = 0;
9604 if (!bset)
9605 return -1;
9607 dim = isl_basic_set_total_dim(bset);
9608 size += bset->n_eq * (1 + dim);
9609 size += bset->n_ineq * (1 + dim);
9610 size += bset->n_div * (2 + dim);
9612 return size;
9615 int isl_set_size(__isl_keep isl_set *set)
9617 int i;
9618 int size = 0;
9620 if (!set)
9621 return -1;
9623 for (i = 0; i < set->n; ++i)
9624 size += isl_basic_set_size(set->p[i]);
9626 return size;
9629 /* Check if there is any lower bound (if lower == 0) and/or upper
9630 * bound (if upper == 0) on the specified dim.
9632 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9633 enum isl_dim_type type, unsigned pos, int lower, int upper)
9635 int i;
9637 if (!bmap)
9638 return -1;
9640 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9642 pos += isl_basic_map_offset(bmap, type);
9644 for (i = 0; i < bmap->n_div; ++i) {
9645 if (isl_int_is_zero(bmap->div[i][0]))
9646 continue;
9647 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9648 return 1;
9651 for (i = 0; i < bmap->n_eq; ++i)
9652 if (!isl_int_is_zero(bmap->eq[i][pos]))
9653 return 1;
9655 for (i = 0; i < bmap->n_ineq; ++i) {
9656 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9657 if (sgn > 0)
9658 lower = 1;
9659 if (sgn < 0)
9660 upper = 1;
9663 return lower && upper;
9666 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9667 enum isl_dim_type type, unsigned pos)
9669 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9672 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9673 enum isl_dim_type type, unsigned pos)
9675 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9678 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9679 enum isl_dim_type type, unsigned pos)
9681 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9684 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9685 enum isl_dim_type type, unsigned pos)
9687 int i;
9689 if (!map)
9690 return -1;
9692 for (i = 0; i < map->n; ++i) {
9693 int bounded;
9694 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9695 if (bounded < 0 || !bounded)
9696 return bounded;
9699 return 1;
9702 /* Return 1 if the specified dim is involved in both an upper bound
9703 * and a lower bound.
9705 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9706 enum isl_dim_type type, unsigned pos)
9708 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9711 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9713 static int has_any_bound(__isl_keep isl_map *map,
9714 enum isl_dim_type type, unsigned pos,
9715 int (*fn)(__isl_keep isl_basic_map *bmap,
9716 enum isl_dim_type type, unsigned pos))
9718 int i;
9720 if (!map)
9721 return -1;
9723 for (i = 0; i < map->n; ++i) {
9724 int bounded;
9725 bounded = fn(map->p[i], type, pos);
9726 if (bounded < 0 || bounded)
9727 return bounded;
9730 return 0;
9733 /* Return 1 if the specified dim is involved in any lower bound.
9735 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9736 enum isl_dim_type type, unsigned pos)
9738 return has_any_bound(set, type, pos,
9739 &isl_basic_map_dim_has_lower_bound);
9742 /* Return 1 if the specified dim is involved in any upper bound.
9744 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9745 enum isl_dim_type type, unsigned pos)
9747 return has_any_bound(set, type, pos,
9748 &isl_basic_map_dim_has_upper_bound);
9751 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9753 static int has_bound(__isl_keep isl_map *map,
9754 enum isl_dim_type type, unsigned pos,
9755 int (*fn)(__isl_keep isl_basic_map *bmap,
9756 enum isl_dim_type type, unsigned pos))
9758 int i;
9760 if (!map)
9761 return -1;
9763 for (i = 0; i < map->n; ++i) {
9764 int bounded;
9765 bounded = fn(map->p[i], type, pos);
9766 if (bounded < 0 || !bounded)
9767 return bounded;
9770 return 1;
9773 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9775 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9776 enum isl_dim_type type, unsigned pos)
9778 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9781 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9783 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9784 enum isl_dim_type type, unsigned pos)
9786 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9789 /* For each of the "n" variables starting at "first", determine
9790 * the sign of the variable and put the results in the first "n"
9791 * elements of the array "signs".
9792 * Sign
9793 * 1 means that the variable is non-negative
9794 * -1 means that the variable is non-positive
9795 * 0 means the variable attains both positive and negative values.
9797 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9798 unsigned first, unsigned n, int *signs)
9800 isl_vec *bound = NULL;
9801 struct isl_tab *tab = NULL;
9802 struct isl_tab_undo *snap;
9803 int i;
9805 if (!bset || !signs)
9806 return -1;
9808 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9809 tab = isl_tab_from_basic_set(bset, 0);
9810 if (!bound || !tab)
9811 goto error;
9813 isl_seq_clr(bound->el, bound->size);
9814 isl_int_set_si(bound->el[0], -1);
9816 snap = isl_tab_snap(tab);
9817 for (i = 0; i < n; ++i) {
9818 int empty;
9820 isl_int_set_si(bound->el[1 + first + i], -1);
9821 if (isl_tab_add_ineq(tab, bound->el) < 0)
9822 goto error;
9823 empty = tab->empty;
9824 isl_int_set_si(bound->el[1 + first + i], 0);
9825 if (isl_tab_rollback(tab, snap) < 0)
9826 goto error;
9828 if (empty) {
9829 signs[i] = 1;
9830 continue;
9833 isl_int_set_si(bound->el[1 + first + i], 1);
9834 if (isl_tab_add_ineq(tab, bound->el) < 0)
9835 goto error;
9836 empty = tab->empty;
9837 isl_int_set_si(bound->el[1 + first + i], 0);
9838 if (isl_tab_rollback(tab, snap) < 0)
9839 goto error;
9841 signs[i] = empty ? -1 : 0;
9844 isl_tab_free(tab);
9845 isl_vec_free(bound);
9846 return 0;
9847 error:
9848 isl_tab_free(tab);
9849 isl_vec_free(bound);
9850 return -1;
9853 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9854 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9856 if (!bset || !signs)
9857 return -1;
9858 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9859 return -1);
9861 first += pos(bset->dim, type) - 1;
9862 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9865 /* Check if the given basic map is obviously single-valued.
9866 * In particular, for each output dimension, check that there is
9867 * an equality that defines the output dimension in terms of
9868 * earlier dimensions.
9870 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9872 int i, j;
9873 unsigned total;
9874 unsigned n_out;
9875 unsigned o_out;
9877 if (!bmap)
9878 return -1;
9880 total = 1 + isl_basic_map_total_dim(bmap);
9881 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9882 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9884 for (i = 0; i < n_out; ++i) {
9885 for (j = 0; j < bmap->n_eq; ++j) {
9886 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9887 continue;
9888 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9889 total - (o_out + i + 1)) == -1)
9890 break;
9892 if (j >= bmap->n_eq)
9893 return 0;
9896 return 1;
9899 /* Check if the given basic map is single-valued.
9900 * We simply compute
9902 * M \circ M^-1
9904 * and check if the result is a subset of the identity mapping.
9906 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9908 isl_space *space;
9909 isl_basic_map *test;
9910 isl_basic_map *id;
9911 int sv;
9913 sv = isl_basic_map_plain_is_single_valued(bmap);
9914 if (sv < 0 || sv)
9915 return sv;
9917 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9918 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9920 space = isl_basic_map_get_space(bmap);
9921 space = isl_space_map_from_set(isl_space_range(space));
9922 id = isl_basic_map_identity(space);
9924 sv = isl_basic_map_is_subset(test, id);
9926 isl_basic_map_free(test);
9927 isl_basic_map_free(id);
9929 return sv;
9932 /* Check if the given map is obviously single-valued.
9934 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9936 if (!map)
9937 return -1;
9938 if (map->n == 0)
9939 return 1;
9940 if (map->n >= 2)
9941 return 0;
9943 return isl_basic_map_plain_is_single_valued(map->p[0]);
9946 /* Check if the given map is single-valued.
9947 * We simply compute
9949 * M \circ M^-1
9951 * and check if the result is a subset of the identity mapping.
9953 int isl_map_is_single_valued(__isl_keep isl_map *map)
9955 isl_space *dim;
9956 isl_map *test;
9957 isl_map *id;
9958 int sv;
9960 sv = isl_map_plain_is_single_valued(map);
9961 if (sv < 0 || sv)
9962 return sv;
9964 test = isl_map_reverse(isl_map_copy(map));
9965 test = isl_map_apply_range(test, isl_map_copy(map));
9967 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9968 id = isl_map_identity(dim);
9970 sv = isl_map_is_subset(test, id);
9972 isl_map_free(test);
9973 isl_map_free(id);
9975 return sv;
9978 int isl_map_is_injective(__isl_keep isl_map *map)
9980 int in;
9982 map = isl_map_copy(map);
9983 map = isl_map_reverse(map);
9984 in = isl_map_is_single_valued(map);
9985 isl_map_free(map);
9987 return in;
9990 /* Check if the given map is obviously injective.
9992 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9994 int in;
9996 map = isl_map_copy(map);
9997 map = isl_map_reverse(map);
9998 in = isl_map_plain_is_single_valued(map);
9999 isl_map_free(map);
10001 return in;
10004 int isl_map_is_bijective(__isl_keep isl_map *map)
10006 int sv;
10008 sv = isl_map_is_single_valued(map);
10009 if (sv < 0 || !sv)
10010 return sv;
10012 return isl_map_is_injective(map);
10015 int isl_set_is_singleton(__isl_keep isl_set *set)
10017 return isl_map_is_single_valued((isl_map *)set);
10020 int isl_map_is_translation(__isl_keep isl_map *map)
10022 int ok;
10023 isl_set *delta;
10025 delta = isl_map_deltas(isl_map_copy(map));
10026 ok = isl_set_is_singleton(delta);
10027 isl_set_free(delta);
10029 return ok;
10032 static int unique(isl_int *p, unsigned pos, unsigned len)
10034 if (isl_seq_first_non_zero(p, pos) != -1)
10035 return 0;
10036 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10037 return 0;
10038 return 1;
10041 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10043 int i, j;
10044 unsigned nvar;
10045 unsigned ovar;
10047 if (!bset)
10048 return -1;
10050 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10051 return 0;
10053 nvar = isl_basic_set_dim(bset, isl_dim_set);
10054 ovar = isl_space_offset(bset->dim, isl_dim_set);
10055 for (j = 0; j < nvar; ++j) {
10056 int lower = 0, upper = 0;
10057 for (i = 0; i < bset->n_eq; ++i) {
10058 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10059 continue;
10060 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10061 return 0;
10062 break;
10064 if (i < bset->n_eq)
10065 continue;
10066 for (i = 0; i < bset->n_ineq; ++i) {
10067 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10068 continue;
10069 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10070 return 0;
10071 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10072 lower = 1;
10073 else
10074 upper = 1;
10076 if (!lower || !upper)
10077 return 0;
10080 return 1;
10083 int isl_set_is_box(__isl_keep isl_set *set)
10085 if (!set)
10086 return -1;
10087 if (set->n != 1)
10088 return 0;
10090 return isl_basic_set_is_box(set->p[0]);
10093 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10095 if (!bset)
10096 return -1;
10098 return isl_space_is_wrapping(bset->dim);
10101 int isl_set_is_wrapping(__isl_keep isl_set *set)
10103 if (!set)
10104 return -1;
10106 return isl_space_is_wrapping(set->dim);
10109 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10111 bmap = isl_basic_map_cow(bmap);
10112 if (!bmap)
10113 return NULL;
10115 bmap->dim = isl_space_wrap(bmap->dim);
10116 if (!bmap->dim)
10117 goto error;
10119 bmap = isl_basic_map_finalize(bmap);
10121 return (isl_basic_set *)bmap;
10122 error:
10123 isl_basic_map_free(bmap);
10124 return NULL;
10127 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10129 int i;
10131 map = isl_map_cow(map);
10132 if (!map)
10133 return NULL;
10135 for (i = 0; i < map->n; ++i) {
10136 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10137 if (!map->p[i])
10138 goto error;
10140 map->dim = isl_space_wrap(map->dim);
10141 if (!map->dim)
10142 goto error;
10144 return (isl_set *)map;
10145 error:
10146 isl_map_free(map);
10147 return NULL;
10150 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10152 bset = isl_basic_set_cow(bset);
10153 if (!bset)
10154 return NULL;
10156 bset->dim = isl_space_unwrap(bset->dim);
10157 if (!bset->dim)
10158 goto error;
10160 bset = isl_basic_set_finalize(bset);
10162 return (isl_basic_map *)bset;
10163 error:
10164 isl_basic_set_free(bset);
10165 return NULL;
10168 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10170 int i;
10172 if (!set)
10173 return NULL;
10175 if (!isl_set_is_wrapping(set))
10176 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10177 goto error);
10179 set = isl_set_cow(set);
10180 if (!set)
10181 return NULL;
10183 for (i = 0; i < set->n; ++i) {
10184 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10185 if (!set->p[i])
10186 goto error;
10189 set->dim = isl_space_unwrap(set->dim);
10190 if (!set->dim)
10191 goto error;
10193 return (isl_map *)set;
10194 error:
10195 isl_set_free(set);
10196 return NULL;
10199 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10200 enum isl_dim_type type)
10202 if (!bmap)
10203 return NULL;
10205 if (!isl_space_is_named_or_nested(bmap->dim, type))
10206 return bmap;
10208 bmap = isl_basic_map_cow(bmap);
10209 if (!bmap)
10210 return NULL;
10212 bmap->dim = isl_space_reset(bmap->dim, type);
10213 if (!bmap->dim)
10214 goto error;
10216 bmap = isl_basic_map_finalize(bmap);
10218 return bmap;
10219 error:
10220 isl_basic_map_free(bmap);
10221 return NULL;
10224 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10225 enum isl_dim_type type)
10227 int i;
10229 if (!map)
10230 return NULL;
10232 if (!isl_space_is_named_or_nested(map->dim, type))
10233 return map;
10235 map = isl_map_cow(map);
10236 if (!map)
10237 return NULL;
10239 for (i = 0; i < map->n; ++i) {
10240 map->p[i] = isl_basic_map_reset(map->p[i], type);
10241 if (!map->p[i])
10242 goto error;
10244 map->dim = isl_space_reset(map->dim, type);
10245 if (!map->dim)
10246 goto error;
10248 return map;
10249 error:
10250 isl_map_free(map);
10251 return NULL;
10254 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10256 if (!bmap)
10257 return NULL;
10259 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10260 return bmap;
10262 bmap = isl_basic_map_cow(bmap);
10263 if (!bmap)
10264 return NULL;
10266 bmap->dim = isl_space_flatten(bmap->dim);
10267 if (!bmap->dim)
10268 goto error;
10270 bmap = isl_basic_map_finalize(bmap);
10272 return bmap;
10273 error:
10274 isl_basic_map_free(bmap);
10275 return NULL;
10278 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10280 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10283 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10284 __isl_take isl_basic_map *bmap)
10286 if (!bmap)
10287 return NULL;
10289 if (!bmap->dim->nested[0])
10290 return bmap;
10292 bmap = isl_basic_map_cow(bmap);
10293 if (!bmap)
10294 return NULL;
10296 bmap->dim = isl_space_flatten_domain(bmap->dim);
10297 if (!bmap->dim)
10298 goto error;
10300 bmap = isl_basic_map_finalize(bmap);
10302 return bmap;
10303 error:
10304 isl_basic_map_free(bmap);
10305 return NULL;
10308 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10309 __isl_take isl_basic_map *bmap)
10311 if (!bmap)
10312 return NULL;
10314 if (!bmap->dim->nested[1])
10315 return bmap;
10317 bmap = isl_basic_map_cow(bmap);
10318 if (!bmap)
10319 return NULL;
10321 bmap->dim = isl_space_flatten_range(bmap->dim);
10322 if (!bmap->dim)
10323 goto error;
10325 bmap = isl_basic_map_finalize(bmap);
10327 return bmap;
10328 error:
10329 isl_basic_map_free(bmap);
10330 return NULL;
10333 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10335 int i;
10337 if (!map)
10338 return NULL;
10340 if (!map->dim->nested[0] && !map->dim->nested[1])
10341 return map;
10343 map = isl_map_cow(map);
10344 if (!map)
10345 return NULL;
10347 for (i = 0; i < map->n; ++i) {
10348 map->p[i] = isl_basic_map_flatten(map->p[i]);
10349 if (!map->p[i])
10350 goto error;
10352 map->dim = isl_space_flatten(map->dim);
10353 if (!map->dim)
10354 goto error;
10356 return map;
10357 error:
10358 isl_map_free(map);
10359 return NULL;
10362 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10364 return (isl_set *)isl_map_flatten((isl_map *)set);
10367 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10369 isl_space *dim, *flat_dim;
10370 isl_map *map;
10372 dim = isl_set_get_space(set);
10373 flat_dim = isl_space_flatten(isl_space_copy(dim));
10374 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10375 map = isl_map_intersect_domain(map, set);
10377 return map;
10380 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10382 int i;
10384 if (!map)
10385 return NULL;
10387 if (!map->dim->nested[0])
10388 return map;
10390 map = isl_map_cow(map);
10391 if (!map)
10392 return NULL;
10394 for (i = 0; i < map->n; ++i) {
10395 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10396 if (!map->p[i])
10397 goto error;
10399 map->dim = isl_space_flatten_domain(map->dim);
10400 if (!map->dim)
10401 goto error;
10403 return map;
10404 error:
10405 isl_map_free(map);
10406 return NULL;
10409 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10411 int i;
10413 if (!map)
10414 return NULL;
10416 if (!map->dim->nested[1])
10417 return map;
10419 map = isl_map_cow(map);
10420 if (!map)
10421 return NULL;
10423 for (i = 0; i < map->n; ++i) {
10424 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10425 if (!map->p[i])
10426 goto error;
10428 map->dim = isl_space_flatten_range(map->dim);
10429 if (!map->dim)
10430 goto error;
10432 return map;
10433 error:
10434 isl_map_free(map);
10435 return NULL;
10438 /* Reorder the dimensions of "bmap" according to the given dim_map
10439 * and set the dimension specification to "dim".
10441 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10442 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10444 isl_basic_map *res;
10445 unsigned flags;
10447 bmap = isl_basic_map_cow(bmap);
10448 if (!bmap || !dim || !dim_map)
10449 goto error;
10451 flags = bmap->flags;
10452 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10453 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10454 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10455 res = isl_basic_map_alloc_space(dim,
10456 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10457 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10458 if (res)
10459 res->flags = flags;
10460 res = isl_basic_map_finalize(res);
10461 return res;
10462 error:
10463 free(dim_map);
10464 isl_basic_map_free(bmap);
10465 isl_space_free(dim);
10466 return NULL;
10469 /* Reorder the dimensions of "map" according to given reordering.
10471 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10472 __isl_take isl_reordering *r)
10474 int i;
10475 struct isl_dim_map *dim_map;
10477 map = isl_map_cow(map);
10478 dim_map = isl_dim_map_from_reordering(r);
10479 if (!map || !r || !dim_map)
10480 goto error;
10482 for (i = 0; i < map->n; ++i) {
10483 struct isl_dim_map *dim_map_i;
10485 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10487 map->p[i] = isl_basic_map_realign(map->p[i],
10488 isl_space_copy(r->dim), dim_map_i);
10490 if (!map->p[i])
10491 goto error;
10494 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10496 isl_reordering_free(r);
10497 free(dim_map);
10498 return map;
10499 error:
10500 free(dim_map);
10501 isl_map_free(map);
10502 isl_reordering_free(r);
10503 return NULL;
10506 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10507 __isl_take isl_reordering *r)
10509 return (isl_set *)isl_map_realign((isl_map *)set, r);
10512 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10513 __isl_take isl_space *model)
10515 isl_ctx *ctx;
10517 if (!map || !model)
10518 goto error;
10520 ctx = isl_space_get_ctx(model);
10521 if (!isl_space_has_named_params(model))
10522 isl_die(ctx, isl_error_invalid,
10523 "model has unnamed parameters", goto error);
10524 if (!isl_space_has_named_params(map->dim))
10525 isl_die(ctx, isl_error_invalid,
10526 "relation has unnamed parameters", goto error);
10527 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10528 isl_reordering *exp;
10530 model = isl_space_drop_dims(model, isl_dim_in,
10531 0, isl_space_dim(model, isl_dim_in));
10532 model = isl_space_drop_dims(model, isl_dim_out,
10533 0, isl_space_dim(model, isl_dim_out));
10534 exp = isl_parameter_alignment_reordering(map->dim, model);
10535 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10536 map = isl_map_realign(map, exp);
10539 isl_space_free(model);
10540 return map;
10541 error:
10542 isl_space_free(model);
10543 isl_map_free(map);
10544 return NULL;
10547 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10548 __isl_take isl_space *model)
10550 return isl_map_align_params(set, model);
10553 /* Align the parameters of "bmap" to those of "model", introducing
10554 * additional parameters if needed.
10556 __isl_give isl_basic_map *isl_basic_map_align_params(
10557 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10559 isl_ctx *ctx;
10561 if (!bmap || !model)
10562 goto error;
10564 ctx = isl_space_get_ctx(model);
10565 if (!isl_space_has_named_params(model))
10566 isl_die(ctx, isl_error_invalid,
10567 "model has unnamed parameters", goto error);
10568 if (!isl_space_has_named_params(bmap->dim))
10569 isl_die(ctx, isl_error_invalid,
10570 "relation has unnamed parameters", goto error);
10571 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10572 isl_reordering *exp;
10573 struct isl_dim_map *dim_map;
10575 model = isl_space_drop_dims(model, isl_dim_in,
10576 0, isl_space_dim(model, isl_dim_in));
10577 model = isl_space_drop_dims(model, isl_dim_out,
10578 0, isl_space_dim(model, isl_dim_out));
10579 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10580 exp = isl_reordering_extend_space(exp,
10581 isl_basic_map_get_space(bmap));
10582 dim_map = isl_dim_map_from_reordering(exp);
10583 bmap = isl_basic_map_realign(bmap,
10584 exp ? isl_space_copy(exp->dim) : NULL,
10585 isl_dim_map_extend(dim_map, bmap));
10586 isl_reordering_free(exp);
10587 free(dim_map);
10590 isl_space_free(model);
10591 return bmap;
10592 error:
10593 isl_space_free(model);
10594 isl_basic_map_free(bmap);
10595 return NULL;
10598 /* Align the parameters of "bset" to those of "model", introducing
10599 * additional parameters if needed.
10601 __isl_give isl_basic_set *isl_basic_set_align_params(
10602 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10604 return isl_basic_map_align_params(bset, model);
10607 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10608 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10609 enum isl_dim_type c2, enum isl_dim_type c3,
10610 enum isl_dim_type c4, enum isl_dim_type c5)
10612 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10613 struct isl_mat *mat;
10614 int i, j, k;
10615 int pos;
10617 if (!bmap)
10618 return NULL;
10619 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10620 isl_basic_map_total_dim(bmap) + 1);
10621 if (!mat)
10622 return NULL;
10623 for (i = 0; i < bmap->n_eq; ++i)
10624 for (j = 0, pos = 0; j < 5; ++j) {
10625 int off = isl_basic_map_offset(bmap, c[j]);
10626 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10627 isl_int_set(mat->row[i][pos],
10628 bmap->eq[i][off + k]);
10629 ++pos;
10633 return mat;
10636 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10637 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10638 enum isl_dim_type c2, enum isl_dim_type c3,
10639 enum isl_dim_type c4, enum isl_dim_type c5)
10641 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10642 struct isl_mat *mat;
10643 int i, j, k;
10644 int pos;
10646 if (!bmap)
10647 return NULL;
10648 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10649 isl_basic_map_total_dim(bmap) + 1);
10650 if (!mat)
10651 return NULL;
10652 for (i = 0; i < bmap->n_ineq; ++i)
10653 for (j = 0, pos = 0; j < 5; ++j) {
10654 int off = isl_basic_map_offset(bmap, c[j]);
10655 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10656 isl_int_set(mat->row[i][pos],
10657 bmap->ineq[i][off + k]);
10658 ++pos;
10662 return mat;
10665 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10666 __isl_take isl_space *dim,
10667 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10668 enum isl_dim_type c2, enum isl_dim_type c3,
10669 enum isl_dim_type c4, enum isl_dim_type c5)
10671 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10672 isl_basic_map *bmap;
10673 unsigned total;
10674 unsigned extra;
10675 int i, j, k, l;
10676 int pos;
10678 if (!dim || !eq || !ineq)
10679 goto error;
10681 if (eq->n_col != ineq->n_col)
10682 isl_die(dim->ctx, isl_error_invalid,
10683 "equalities and inequalities matrices should have "
10684 "same number of columns", goto error);
10686 total = 1 + isl_space_dim(dim, isl_dim_all);
10688 if (eq->n_col < total)
10689 isl_die(dim->ctx, isl_error_invalid,
10690 "number of columns too small", goto error);
10692 extra = eq->n_col - total;
10694 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10695 eq->n_row, ineq->n_row);
10696 if (!bmap)
10697 goto error;
10698 for (i = 0; i < extra; ++i) {
10699 k = isl_basic_map_alloc_div(bmap);
10700 if (k < 0)
10701 goto error;
10702 isl_int_set_si(bmap->div[k][0], 0);
10704 for (i = 0; i < eq->n_row; ++i) {
10705 l = isl_basic_map_alloc_equality(bmap);
10706 if (l < 0)
10707 goto error;
10708 for (j = 0, pos = 0; j < 5; ++j) {
10709 int off = isl_basic_map_offset(bmap, c[j]);
10710 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10711 isl_int_set(bmap->eq[l][off + k],
10712 eq->row[i][pos]);
10713 ++pos;
10717 for (i = 0; i < ineq->n_row; ++i) {
10718 l = isl_basic_map_alloc_inequality(bmap);
10719 if (l < 0)
10720 goto error;
10721 for (j = 0, pos = 0; j < 5; ++j) {
10722 int off = isl_basic_map_offset(bmap, c[j]);
10723 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10724 isl_int_set(bmap->ineq[l][off + k],
10725 ineq->row[i][pos]);
10726 ++pos;
10731 isl_space_free(dim);
10732 isl_mat_free(eq);
10733 isl_mat_free(ineq);
10735 bmap = isl_basic_map_simplify(bmap);
10736 return isl_basic_map_finalize(bmap);
10737 error:
10738 isl_space_free(dim);
10739 isl_mat_free(eq);
10740 isl_mat_free(ineq);
10741 return NULL;
10744 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10745 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10746 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10748 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10749 c1, c2, c3, c4, isl_dim_in);
10752 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10753 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10754 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10756 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10757 c1, c2, c3, c4, isl_dim_in);
10760 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10761 __isl_take isl_space *dim,
10762 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10763 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10765 return (isl_basic_set*)
10766 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10767 c1, c2, c3, c4, isl_dim_in);
10770 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10772 if (!bmap)
10773 return -1;
10775 return isl_space_can_zip(bmap->dim);
10778 int isl_map_can_zip(__isl_keep isl_map *map)
10780 if (!map)
10781 return -1;
10783 return isl_space_can_zip(map->dim);
10786 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10787 * (A -> C) -> (B -> D).
10789 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10791 unsigned pos;
10792 unsigned n1;
10793 unsigned n2;
10795 if (!bmap)
10796 return NULL;
10798 if (!isl_basic_map_can_zip(bmap))
10799 isl_die(bmap->ctx, isl_error_invalid,
10800 "basic map cannot be zipped", goto error);
10801 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10802 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10803 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10804 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10805 bmap = isl_basic_map_cow(bmap);
10806 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10807 if (!bmap)
10808 return NULL;
10809 bmap->dim = isl_space_zip(bmap->dim);
10810 if (!bmap->dim)
10811 goto error;
10812 return bmap;
10813 error:
10814 isl_basic_map_free(bmap);
10815 return NULL;
10818 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10819 * (A -> C) -> (B -> D).
10821 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10823 int i;
10825 if (!map)
10826 return NULL;
10828 if (!isl_map_can_zip(map))
10829 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10830 goto error);
10832 map = isl_map_cow(map);
10833 if (!map)
10834 return NULL;
10836 for (i = 0; i < map->n; ++i) {
10837 map->p[i] = isl_basic_map_zip(map->p[i]);
10838 if (!map->p[i])
10839 goto error;
10842 map->dim = isl_space_zip(map->dim);
10843 if (!map->dim)
10844 goto error;
10846 return map;
10847 error:
10848 isl_map_free(map);
10849 return NULL;
10852 /* Can we apply isl_basic_map_curry to "bmap"?
10853 * That is, does it have a nested relation in its domain?
10855 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10857 if (!bmap)
10858 return -1;
10860 return isl_space_can_curry(bmap->dim);
10863 /* Can we apply isl_map_curry to "map"?
10864 * That is, does it have a nested relation in its domain?
10866 int isl_map_can_curry(__isl_keep isl_map *map)
10868 if (!map)
10869 return -1;
10871 return isl_space_can_curry(map->dim);
10874 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10875 * A -> (B -> C).
10877 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10880 if (!bmap)
10881 return NULL;
10883 if (!isl_basic_map_can_curry(bmap))
10884 isl_die(bmap->ctx, isl_error_invalid,
10885 "basic map cannot be curried", goto error);
10886 bmap = isl_basic_map_cow(bmap);
10887 if (!bmap)
10888 return NULL;
10889 bmap->dim = isl_space_curry(bmap->dim);
10890 if (!bmap->dim)
10891 goto error;
10892 return bmap;
10893 error:
10894 isl_basic_map_free(bmap);
10895 return NULL;
10898 /* Given a map (A -> B) -> C, return the corresponding map
10899 * A -> (B -> C).
10901 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10903 int i;
10905 if (!map)
10906 return NULL;
10908 if (!isl_map_can_curry(map))
10909 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10910 goto error);
10912 map = isl_map_cow(map);
10913 if (!map)
10914 return NULL;
10916 for (i = 0; i < map->n; ++i) {
10917 map->p[i] = isl_basic_map_curry(map->p[i]);
10918 if (!map->p[i])
10919 goto error;
10922 map->dim = isl_space_curry(map->dim);
10923 if (!map->dim)
10924 goto error;
10926 return map;
10927 error:
10928 isl_map_free(map);
10929 return NULL;
10932 /* Can we apply isl_basic_map_uncurry to "bmap"?
10933 * That is, does it have a nested relation in its domain?
10935 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
10937 if (!bmap)
10938 return -1;
10940 return isl_space_can_uncurry(bmap->dim);
10943 /* Can we apply isl_map_uncurry to "map"?
10944 * That is, does it have a nested relation in its domain?
10946 int isl_map_can_uncurry(__isl_keep isl_map *map)
10948 if (!map)
10949 return -1;
10951 return isl_space_can_uncurry(map->dim);
10954 /* Given a basic map A -> (B -> C), return the corresponding basic map
10955 * (A -> B) -> C.
10957 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
10960 if (!bmap)
10961 return NULL;
10963 if (!isl_basic_map_can_uncurry(bmap))
10964 isl_die(bmap->ctx, isl_error_invalid,
10965 "basic map cannot be uncurried",
10966 return isl_basic_map_free(bmap));
10967 bmap = isl_basic_map_cow(bmap);
10968 if (!bmap)
10969 return NULL;
10970 bmap->dim = isl_space_uncurry(bmap->dim);
10971 if (!bmap->dim)
10972 return isl_basic_map_free(bmap);
10973 return bmap;
10976 /* Given a map A -> (B -> C), return the corresponding map
10977 * (A -> B) -> C.
10979 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
10981 int i;
10983 if (!map)
10984 return NULL;
10986 if (!isl_map_can_uncurry(map))
10987 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
10988 return isl_map_free(map));
10990 map = isl_map_cow(map);
10991 if (!map)
10992 return NULL;
10994 for (i = 0; i < map->n; ++i) {
10995 map->p[i] = isl_basic_map_uncurry(map->p[i]);
10996 if (!map->p[i])
10997 return isl_map_free(map);
11000 map->dim = isl_space_uncurry(map->dim);
11001 if (!map->dim)
11002 return isl_map_free(map);
11004 return map;
11007 /* Construct a basic map mapping the domain of the affine expression
11008 * to a one-dimensional range prescribed by the affine expression.
11010 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11012 int k;
11013 int pos;
11014 isl_local_space *ls;
11015 isl_basic_map *bmap;
11017 if (!aff)
11018 return NULL;
11020 ls = isl_aff_get_local_space(aff);
11021 bmap = isl_basic_map_from_local_space(ls);
11022 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11023 k = isl_basic_map_alloc_equality(bmap);
11024 if (k < 0)
11025 goto error;
11027 pos = isl_basic_map_offset(bmap, isl_dim_out);
11028 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11029 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11030 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11031 aff->v->size - (pos + 1));
11033 isl_aff_free(aff);
11034 bmap = isl_basic_map_finalize(bmap);
11035 return bmap;
11036 error:
11037 isl_aff_free(aff);
11038 isl_basic_map_free(bmap);
11039 return NULL;
11042 /* Construct a map mapping the domain of the affine expression
11043 * to a one-dimensional range prescribed by the affine expression.
11045 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11047 isl_basic_map *bmap;
11049 bmap = isl_basic_map_from_aff(aff);
11050 return isl_map_from_basic_map(bmap);
11053 /* Construct a basic map mapping the domain the multi-affine expression
11054 * to its range, with each dimension in the range equated to the
11055 * corresponding affine expression.
11057 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11058 __isl_take isl_multi_aff *maff)
11060 int i;
11061 isl_space *space;
11062 isl_basic_map *bmap;
11064 if (!maff)
11065 return NULL;
11067 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11068 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11069 "invalid space", return isl_multi_aff_free(maff));
11071 space = isl_space_domain(isl_multi_aff_get_space(maff));
11072 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11074 for (i = 0; i < maff->n; ++i) {
11075 isl_aff *aff;
11076 isl_basic_map *bmap_i;
11078 aff = isl_aff_copy(maff->p[i]);
11079 bmap_i = isl_basic_map_from_aff(aff);
11081 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11084 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11086 isl_multi_aff_free(maff);
11087 return bmap;
11090 /* Construct a map mapping the domain the multi-affine expression
11091 * to its range, with each dimension in the range equated to the
11092 * corresponding affine expression.
11094 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11096 isl_basic_map *bmap;
11098 bmap = isl_basic_map_from_multi_aff(maff);
11099 return isl_map_from_basic_map(bmap);
11102 /* Construct a basic map mapping a domain in the given space to
11103 * to an n-dimensional range, with n the number of elements in the list,
11104 * where each coordinate in the range is prescribed by the
11105 * corresponding affine expression.
11106 * The domains of all affine expressions in the list are assumed to match
11107 * domain_dim.
11109 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11110 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11112 int i;
11113 isl_space *dim;
11114 isl_basic_map *bmap;
11116 if (!list)
11117 return NULL;
11119 dim = isl_space_from_domain(domain_dim);
11120 bmap = isl_basic_map_universe(dim);
11122 for (i = 0; i < list->n; ++i) {
11123 isl_aff *aff;
11124 isl_basic_map *bmap_i;
11126 aff = isl_aff_copy(list->p[i]);
11127 bmap_i = isl_basic_map_from_aff(aff);
11129 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11132 isl_aff_list_free(list);
11133 return bmap;
11136 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11137 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11139 return isl_map_equate(set, type1, pos1, type2, pos2);
11142 /* Construct a basic map where the given dimensions are equal to each other.
11144 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11145 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11147 isl_basic_map *bmap = NULL;
11148 int i;
11150 if (!space)
11151 return NULL;
11153 if (pos1 >= isl_space_dim(space, type1))
11154 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11155 "index out of bounds", goto error);
11156 if (pos2 >= isl_space_dim(space, type2))
11157 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11158 "index out of bounds", goto error);
11160 if (type1 == type2 && pos1 == pos2)
11161 return isl_basic_map_universe(space);
11163 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11164 i = isl_basic_map_alloc_equality(bmap);
11165 if (i < 0)
11166 goto error;
11167 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11168 pos1 += isl_basic_map_offset(bmap, type1);
11169 pos2 += isl_basic_map_offset(bmap, type2);
11170 isl_int_set_si(bmap->eq[i][pos1], -1);
11171 isl_int_set_si(bmap->eq[i][pos2], 1);
11172 bmap = isl_basic_map_finalize(bmap);
11173 isl_space_free(space);
11174 return bmap;
11175 error:
11176 isl_space_free(space);
11177 isl_basic_map_free(bmap);
11178 return NULL;
11181 /* Add a constraint imposing that the given two dimensions are equal.
11183 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11184 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11186 isl_basic_map *eq;
11188 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11190 bmap = isl_basic_map_intersect(bmap, eq);
11192 return bmap;
11195 /* Add a constraint imposing that the given two dimensions are equal.
11197 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11198 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11200 isl_basic_map *bmap;
11202 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11204 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11206 return map;
11209 /* Add a constraint imposing that the given two dimensions have opposite values.
11211 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11212 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11214 isl_basic_map *bmap = NULL;
11215 int i;
11217 if (!map)
11218 return NULL;
11220 if (pos1 >= isl_map_dim(map, type1))
11221 isl_die(map->ctx, isl_error_invalid,
11222 "index out of bounds", goto error);
11223 if (pos2 >= isl_map_dim(map, type2))
11224 isl_die(map->ctx, isl_error_invalid,
11225 "index out of bounds", goto error);
11227 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11228 i = isl_basic_map_alloc_equality(bmap);
11229 if (i < 0)
11230 goto error;
11231 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11232 pos1 += isl_basic_map_offset(bmap, type1);
11233 pos2 += isl_basic_map_offset(bmap, type2);
11234 isl_int_set_si(bmap->eq[i][pos1], 1);
11235 isl_int_set_si(bmap->eq[i][pos2], 1);
11236 bmap = isl_basic_map_finalize(bmap);
11238 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11240 return map;
11241 error:
11242 isl_basic_map_free(bmap);
11243 isl_map_free(map);
11244 return NULL;
11247 /* Add a constraint imposing that the value of the first dimension is
11248 * greater than or equal to that of the second.
11250 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11251 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11253 isl_constraint *c;
11254 isl_local_space *ls;
11256 if (!bmap)
11257 return NULL;
11259 if (pos1 >= isl_basic_map_dim(bmap, type1))
11260 isl_die(bmap->ctx, isl_error_invalid,
11261 "index out of bounds", return isl_basic_map_free(bmap));
11262 if (pos2 >= isl_basic_map_dim(bmap, type2))
11263 isl_die(bmap->ctx, isl_error_invalid,
11264 "index out of bounds", return isl_basic_map_free(bmap));
11266 if (type1 == type2 && pos1 == pos2)
11267 return bmap;
11269 ls = isl_local_space_from_space(isl_basic_map_get_space(bmap));
11270 c = isl_inequality_alloc(ls);
11271 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11272 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11273 bmap = isl_basic_map_add_constraint(bmap, c);
11275 return bmap;
11278 /* Construct a basic map where the value of the first dimension is
11279 * greater than that of the second.
11281 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11282 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11284 isl_basic_map *bmap = NULL;
11285 int i;
11287 if (!space)
11288 return NULL;
11290 if (pos1 >= isl_space_dim(space, type1))
11291 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11292 "index out of bounds", goto error);
11293 if (pos2 >= isl_space_dim(space, type2))
11294 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11295 "index out of bounds", goto error);
11297 if (type1 == type2 && pos1 == pos2)
11298 return isl_basic_map_empty(space);
11300 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11301 i = isl_basic_map_alloc_inequality(bmap);
11302 if (i < 0)
11303 goto error;
11304 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11305 pos1 += isl_basic_map_offset(bmap, type1);
11306 pos2 += isl_basic_map_offset(bmap, type2);
11307 isl_int_set_si(bmap->ineq[i][pos1], 1);
11308 isl_int_set_si(bmap->ineq[i][pos2], -1);
11309 isl_int_set_si(bmap->ineq[i][0], -1);
11310 bmap = isl_basic_map_finalize(bmap);
11312 return bmap;
11313 error:
11314 isl_space_free(space);
11315 isl_basic_map_free(bmap);
11316 return NULL;
11319 /* Add a constraint imposing that the value of the first dimension is
11320 * greater than that of the second.
11322 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11323 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11325 isl_basic_map *gt;
11327 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11329 bmap = isl_basic_map_intersect(bmap, gt);
11331 return bmap;
11334 /* Add a constraint imposing that the value of the first dimension is
11335 * greater than that of the second.
11337 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11338 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11340 isl_basic_map *bmap;
11342 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11344 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11346 return map;
11349 /* Add a constraint imposing that the value of the first dimension is
11350 * smaller than that of the second.
11352 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11353 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11355 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11358 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11359 int pos)
11361 isl_aff *div;
11362 isl_local_space *ls;
11364 if (!bmap)
11365 return NULL;
11367 if (!isl_basic_map_divs_known(bmap))
11368 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11369 "some divs are unknown", return NULL);
11371 ls = isl_basic_map_get_local_space(bmap);
11372 div = isl_local_space_get_div(ls, pos);
11373 isl_local_space_free(ls);
11375 return div;
11378 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11379 int pos)
11381 return isl_basic_map_get_div(bset, pos);
11384 /* Plug in "subs" for dimension "type", "pos" of "bset".
11386 * Let i be the dimension to replace and let "subs" be of the form
11388 * f/d
11390 * Any integer division with a non-zero coefficient for i,
11392 * floor((a i + g)/m)
11394 * is replaced by
11396 * floor((a f + d g)/(m d))
11398 * Constraints of the form
11400 * a i + g
11402 * are replaced by
11404 * a f + d g
11406 * We currently require that "subs" is an integral expression.
11407 * Handling rational expressions may require us to add stride constraints
11408 * as we do in isl_basic_set_preimage_multi_aff.
11410 __isl_give isl_basic_set *isl_basic_set_substitute(
11411 __isl_take isl_basic_set *bset,
11412 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11414 int i;
11415 isl_int v;
11416 isl_ctx *ctx;
11418 if (bset && isl_basic_set_plain_is_empty(bset))
11419 return bset;
11421 bset = isl_basic_set_cow(bset);
11422 if (!bset || !subs)
11423 goto error;
11425 ctx = isl_basic_set_get_ctx(bset);
11426 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11427 isl_die(ctx, isl_error_invalid,
11428 "spaces don't match", goto error);
11429 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11430 isl_die(ctx, isl_error_unsupported,
11431 "cannot handle divs yet", goto error);
11432 if (!isl_int_is_one(subs->v->el[0]))
11433 isl_die(ctx, isl_error_invalid,
11434 "can only substitute integer expressions", goto error);
11436 pos += isl_basic_set_offset(bset, type);
11438 isl_int_init(v);
11440 for (i = 0; i < bset->n_eq; ++i) {
11441 if (isl_int_is_zero(bset->eq[i][pos]))
11442 continue;
11443 isl_int_set(v, bset->eq[i][pos]);
11444 isl_int_set_si(bset->eq[i][pos], 0);
11445 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11446 v, subs->v->el + 1, subs->v->size - 1);
11449 for (i = 0; i < bset->n_ineq; ++i) {
11450 if (isl_int_is_zero(bset->ineq[i][pos]))
11451 continue;
11452 isl_int_set(v, bset->ineq[i][pos]);
11453 isl_int_set_si(bset->ineq[i][pos], 0);
11454 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11455 v, subs->v->el + 1, subs->v->size - 1);
11458 for (i = 0; i < bset->n_div; ++i) {
11459 if (isl_int_is_zero(bset->div[i][1 + pos]))
11460 continue;
11461 isl_int_set(v, bset->div[i][1 + pos]);
11462 isl_int_set_si(bset->div[i][1 + pos], 0);
11463 isl_seq_combine(bset->div[i] + 1,
11464 subs->v->el[0], bset->div[i] + 1,
11465 v, subs->v->el + 1, subs->v->size - 1);
11466 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11469 isl_int_clear(v);
11471 bset = isl_basic_set_simplify(bset);
11472 return isl_basic_set_finalize(bset);
11473 error:
11474 isl_basic_set_free(bset);
11475 return NULL;
11478 /* Plug in "subs" for dimension "type", "pos" of "set".
11480 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11481 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11483 int i;
11485 if (set && isl_set_plain_is_empty(set))
11486 return set;
11488 set = isl_set_cow(set);
11489 if (!set || !subs)
11490 goto error;
11492 for (i = set->n - 1; i >= 0; --i) {
11493 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11494 if (remove_if_empty(set, i) < 0)
11495 goto error;
11498 return set;
11499 error:
11500 isl_set_free(set);
11501 return NULL;
11504 /* Check if the range of "ma" is compatible with the domain or range
11505 * (depending on "type") of "bmap".
11506 * Return -1 if anything is wrong.
11508 static int check_basic_map_compatible_range_multi_aff(
11509 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11510 __isl_keep isl_multi_aff *ma)
11512 int m;
11513 isl_space *ma_space;
11515 ma_space = isl_multi_aff_get_space(ma);
11516 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11517 isl_space_free(ma_space);
11518 if (m >= 0 && !m)
11519 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11520 "spaces don't match", return -1);
11521 return m;
11524 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11525 * coefficients before the transformed range of dimensions,
11526 * the "n_after" coefficients after the transformed range of dimensions
11527 * and the coefficients of the other divs in "bmap".
11529 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11530 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11532 int i;
11533 int n_param;
11534 int n_set;
11535 isl_local_space *ls;
11537 if (n_div == 0)
11538 return 0;
11540 ls = isl_aff_get_domain_local_space(ma->p[0]);
11541 if (!ls)
11542 return -1;
11544 n_param = isl_local_space_dim(ls, isl_dim_param);
11545 n_set = isl_local_space_dim(ls, isl_dim_set);
11546 for (i = 0; i < n_div; ++i) {
11547 int o_bmap = 0, o_ls = 0;
11549 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
11550 o_bmap += 1 + 1 + n_param;
11551 o_ls += 1 + 1 + n_param;
11552 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
11553 o_bmap += n_before;
11554 isl_seq_cpy(bmap->div[i] + o_bmap,
11555 ls->div->row[i] + o_ls, n_set);
11556 o_bmap += n_set;
11557 o_ls += n_set;
11558 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
11559 o_bmap += n_after;
11560 isl_seq_cpy(bmap->div[i] + o_bmap,
11561 ls->div->row[i] + o_ls, n_div);
11562 o_bmap += n_div;
11563 o_ls += n_div;
11564 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
11565 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
11566 goto error;
11569 isl_local_space_free(ls);
11570 return 0;
11571 error:
11572 isl_local_space_free(ls);
11573 return -1;
11576 /* How many stride constraints does "ma" enforce?
11577 * That is, how many of the affine expressions have a denominator
11578 * different from one?
11580 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11582 int i;
11583 int strides = 0;
11585 for (i = 0; i < ma->n; ++i)
11586 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11587 strides++;
11589 return strides;
11592 /* For each affine expression in ma of the form
11594 * x_i = (f_i y + h_i)/m_i
11596 * with m_i different from one, add a constraint to "bmap"
11597 * of the form
11599 * f_i y + h_i = m_i alpha_i
11601 * with alpha_i an additional existentially quantified variable.
11603 static __isl_give isl_basic_map *add_ma_strides(
11604 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
11605 int n_before, int n_after)
11607 int i, k;
11608 int div;
11609 int total;
11610 int n_param;
11611 int n_in;
11612 int n_div;
11614 total = isl_basic_map_total_dim(bmap);
11615 n_param = isl_multi_aff_dim(ma, isl_dim_param);
11616 n_in = isl_multi_aff_dim(ma, isl_dim_in);
11617 n_div = isl_multi_aff_dim(ma, isl_dim_div);
11618 for (i = 0; i < ma->n; ++i) {
11619 int o_bmap = 0, o_ma = 1;
11621 if (isl_int_is_one(ma->p[i]->v->el[0]))
11622 continue;
11623 div = isl_basic_map_alloc_div(bmap);
11624 k = isl_basic_map_alloc_equality(bmap);
11625 if (div < 0 || k < 0)
11626 goto error;
11627 isl_int_set_si(bmap->div[div][0], 0);
11628 isl_seq_cpy(bmap->eq[k] + o_bmap,
11629 ma->p[i]->v->el + o_ma, 1 + n_param);
11630 o_bmap += 1 + n_param;
11631 o_ma += 1 + n_param;
11632 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
11633 o_bmap += n_before;
11634 isl_seq_cpy(bmap->eq[k] + o_bmap,
11635 ma->p[i]->v->el + o_ma, n_in);
11636 o_bmap += n_in;
11637 o_ma += n_in;
11638 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
11639 o_bmap += n_after;
11640 isl_seq_cpy(bmap->eq[k] + o_bmap,
11641 ma->p[i]->v->el + o_ma, n_div);
11642 o_bmap += n_div;
11643 o_ma += n_div;
11644 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
11645 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
11646 total++;
11649 return bmap;
11650 error:
11651 isl_basic_map_free(bmap);
11652 return NULL;
11655 /* Replace the domain or range space (depending on "type) of "space" by "set".
11657 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
11658 enum isl_dim_type type, __isl_take isl_space *set)
11660 if (type == isl_dim_in) {
11661 space = isl_space_range(space);
11662 space = isl_space_map_from_domain_and_range(set, space);
11663 } else {
11664 space = isl_space_domain(space);
11665 space = isl_space_map_from_domain_and_range(space, set);
11668 return space;
11671 /* Compute the preimage of the domain or range (depending on "type")
11672 * of "bmap" under the function represented by "ma".
11673 * In other words, plug in "ma" in the domain or range of "bmap".
11674 * The result is a basic map that lives in the same space as "bmap"
11675 * except that the domain or range has been replaced by
11676 * the domain space of "ma".
11678 * If bmap is represented by
11680 * A(p) + S u + B x + T v + C(divs) >= 0,
11682 * where u and x are input and output dimensions if type == isl_dim_out
11683 * while x and v are input and output dimensions if type == isl_dim_in,
11684 * and ma is represented by
11686 * x = D(p) + F(y) + G(divs')
11688 * then the result is
11690 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11692 * The divs in the input set are similarly adjusted.
11693 * In particular
11695 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11697 * becomes
11699 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11700 * B_i G(divs') + c_i(divs))/n_i)
11702 * If bmap is not a rational map and if F(y) involves any denominators
11704 * x_i = (f_i y + h_i)/m_i
11706 * then additional constraints are added to ensure that we only
11707 * map back integer points. That is we enforce
11709 * f_i y + h_i = m_i alpha_i
11711 * with alpha_i an additional existentially quantified variable.
11713 * We first copy over the divs from "ma".
11714 * Then we add the modified constraints and divs from "bmap".
11715 * Finally, we add the stride constraints, if needed.
11717 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
11718 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
11719 __isl_take isl_multi_aff *ma)
11721 int i, k;
11722 isl_space *space;
11723 isl_basic_map *res = NULL;
11724 int n_before, n_after, n_div_bmap, n_div_ma;
11725 isl_int f, c1, c2, g;
11726 int rational, strides;
11728 isl_int_init(f);
11729 isl_int_init(c1);
11730 isl_int_init(c2);
11731 isl_int_init(g);
11733 ma = isl_multi_aff_align_divs(ma);
11734 if (!bmap || !ma)
11735 goto error;
11736 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
11737 goto error;
11739 if (type == isl_dim_in) {
11740 n_before = 0;
11741 n_after = isl_basic_map_dim(bmap, isl_dim_out);
11742 } else {
11743 n_before = isl_basic_map_dim(bmap, isl_dim_in);
11744 n_after = 0;
11746 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
11747 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11749 space = isl_multi_aff_get_domain_space(ma);
11750 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
11751 rational = isl_basic_map_is_rational(bmap);
11752 strides = rational ? 0 : multi_aff_strides(ma);
11753 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
11754 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
11755 if (rational)
11756 res = isl_basic_map_set_rational(res);
11758 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
11759 if (isl_basic_map_alloc_div(res) < 0)
11760 goto error;
11762 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
11763 goto error;
11765 for (i = 0; i < bmap->n_eq; ++i) {
11766 k = isl_basic_map_alloc_equality(res);
11767 if (k < 0)
11768 goto error;
11769 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
11770 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11773 for (i = 0; i < bmap->n_ineq; ++i) {
11774 k = isl_basic_map_alloc_inequality(res);
11775 if (k < 0)
11776 goto error;
11777 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
11778 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11781 for (i = 0; i < bmap->n_div; ++i) {
11782 if (isl_int_is_zero(bmap->div[i][0])) {
11783 isl_int_set_si(res->div[n_div_ma + i][0], 0);
11784 continue;
11786 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
11787 n_before, n_after, n_div_ma, n_div_bmap,
11788 f, c1, c2, g, 1);
11791 if (strides)
11792 res = add_ma_strides(res, ma, n_before, n_after);
11794 isl_int_clear(f);
11795 isl_int_clear(c1);
11796 isl_int_clear(c2);
11797 isl_int_clear(g);
11798 isl_basic_map_free(bmap);
11799 isl_multi_aff_free(ma);
11800 res = isl_basic_set_simplify(res);
11801 return isl_basic_map_finalize(res);
11802 error:
11803 isl_int_clear(f);
11804 isl_int_clear(c1);
11805 isl_int_clear(c2);
11806 isl_int_clear(g);
11807 isl_basic_map_free(bmap);
11808 isl_multi_aff_free(ma);
11809 isl_basic_map_free(res);
11810 return NULL;
11813 /* Compute the preimage of "bset" under the function represented by "ma".
11814 * In other words, plug in "ma" in "bset". The result is a basic set
11815 * that lives in the domain space of "ma".
11817 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
11818 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
11820 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
11823 /* Compute the preimage of the domain of "bmap" under the function
11824 * represented by "ma".
11825 * In other words, plug in "ma" in the domain of "bmap".
11826 * The result is a basic map that lives in the same space as "bmap"
11827 * except that the domain has been replaced by the domain space of "ma".
11829 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
11830 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
11832 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
11835 /* Compute the preimage of the range of "bmap" under the function
11836 * represented by "ma".
11837 * In other words, plug in "ma" in the range of "bmap".
11838 * The result is a basic map that lives in the same space as "bmap"
11839 * except that the range has been replaced by the domain space of "ma".
11841 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
11842 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
11844 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
11847 /* Check if the range of "ma" is compatible with the domain or range
11848 * (depending on "type") of "map".
11849 * Return -1 if anything is wrong.
11851 static int check_map_compatible_range_multi_aff(
11852 __isl_keep isl_map *map, enum isl_dim_type type,
11853 __isl_keep isl_multi_aff *ma)
11855 int m;
11856 isl_space *ma_space;
11858 ma_space = isl_multi_aff_get_space(ma);
11859 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
11860 isl_space_free(ma_space);
11861 if (m >= 0 && !m)
11862 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11863 "spaces don't match", return -1);
11864 return m;
11867 /* Compute the preimage of the domain or range (depending on "type")
11868 * of "map" under the function represented by "ma".
11869 * In other words, plug in "ma" in the domain or range of "map".
11870 * The result is a map that lives in the same space as "map"
11871 * except that the domain or range has been replaced by
11872 * the domain space of "ma".
11874 * The parameters are assumed to have been aligned.
11876 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
11877 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11879 int i;
11880 isl_space *space;
11882 map = isl_map_cow(map);
11883 ma = isl_multi_aff_align_divs(ma);
11884 if (!map || !ma)
11885 goto error;
11886 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
11887 goto error;
11889 for (i = 0; i < map->n; ++i) {
11890 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
11891 isl_multi_aff_copy(ma));
11892 if (!map->p[i])
11893 goto error;
11896 space = isl_multi_aff_get_domain_space(ma);
11897 space = isl_space_set(isl_map_get_space(map), type, space);
11899 isl_space_free(map->dim);
11900 map->dim = space;
11901 if (!map->dim)
11902 goto error;
11904 isl_multi_aff_free(ma);
11905 if (map->n > 1)
11906 ISL_F_CLR(map, ISL_MAP_DISJOINT);
11907 ISL_F_CLR(map, ISL_SET_NORMALIZED);
11908 return map;
11909 error:
11910 isl_multi_aff_free(ma);
11911 isl_map_free(map);
11912 return NULL;
11915 /* Compute the preimage of the domain or range (depending on "type")
11916 * of "map" under the function represented by "ma".
11917 * In other words, plug in "ma" in the domain or range of "map".
11918 * The result is a map that lives in the same space as "map"
11919 * except that the domain or range has been replaced by
11920 * the domain space of "ma".
11922 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
11923 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11925 if (!map || !ma)
11926 goto error;
11928 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
11929 return map_preimage_multi_aff(map, type, ma);
11931 if (!isl_space_has_named_params(map->dim) ||
11932 !isl_space_has_named_params(ma->space))
11933 isl_die(map->ctx, isl_error_invalid,
11934 "unaligned unnamed parameters", goto error);
11935 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
11936 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
11938 return map_preimage_multi_aff(map, type, ma);
11939 error:
11940 isl_multi_aff_free(ma);
11941 return isl_map_free(map);
11944 /* Compute the preimage of "set" under the function represented by "ma".
11945 * In other words, plug in "ma" "set". The result is a set
11946 * that lives in the domain space of "ma".
11948 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
11949 __isl_take isl_multi_aff *ma)
11951 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
11954 /* Compute the preimage of the domain of "map" under the function
11955 * represented by "ma".
11956 * In other words, plug in "ma" in the domain of "map".
11957 * The result is a map that lives in the same space as "map"
11958 * except that the domain has been replaced by the domain space of "ma".
11960 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
11961 __isl_take isl_multi_aff *ma)
11963 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
11966 /* Compute the preimage of "set" under the function represented by "pma".
11967 * In other words, plug in "pma" in "set. The result is a set
11968 * that lives in the domain space of "pma".
11970 static __isl_give isl_set *set_preimage_pw_multi_aff(__isl_take isl_set *set,
11971 __isl_take isl_pw_multi_aff *pma)
11973 int i;
11974 isl_set *res;
11976 if (!pma)
11977 goto error;
11979 if (pma->n == 0) {
11980 isl_pw_multi_aff_free(pma);
11981 res = isl_set_empty(isl_set_get_space(set));
11982 isl_set_free(set);
11983 return res;
11986 res = isl_set_preimage_multi_aff(isl_set_copy(set),
11987 isl_multi_aff_copy(pma->p[0].maff));
11988 res = isl_set_intersect(res, isl_set_copy(pma->p[0].set));
11990 for (i = 1; i < pma->n; ++i) {
11991 isl_set *res_i;
11993 res_i = isl_set_preimage_multi_aff(isl_set_copy(set),
11994 isl_multi_aff_copy(pma->p[i].maff));
11995 res_i = isl_set_intersect(res_i, isl_set_copy(pma->p[i].set));
11996 res = isl_set_union(res, res_i);
11999 isl_pw_multi_aff_free(pma);
12000 isl_set_free(set);
12001 return res;
12002 error:
12003 isl_pw_multi_aff_free(pma);
12004 isl_set_free(set);
12005 return NULL;
12008 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12009 __isl_take isl_pw_multi_aff *pma)
12011 if (!set || !pma)
12012 goto error;
12014 if (isl_space_match(set->dim, isl_dim_param, pma->dim, isl_dim_param))
12015 return set_preimage_pw_multi_aff(set, pma);
12017 if (!isl_space_has_named_params(set->dim) ||
12018 !isl_space_has_named_params(pma->dim))
12019 isl_die(set->ctx, isl_error_invalid,
12020 "unaligned unnamed parameters", goto error);
12021 set = isl_set_align_params(set, isl_pw_multi_aff_get_space(pma));
12022 pma = isl_pw_multi_aff_align_params(pma, isl_set_get_space(set));
12024 return set_preimage_pw_multi_aff(set, pma);
12025 error:
12026 isl_pw_multi_aff_free(pma);
12027 return isl_set_free(set);