isl_schedule_constraints: add support for conditional validity constraints
[isl.git] / isl_map.c
blob6bebe6d4b20805537ab38569c4a94f546de53a8a
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 /* Reset the user pointer on all identifiers of parameters and tuples
692 * of the space of "map".
694 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
696 isl_space *space;
698 space = isl_map_get_space(map);
699 space = isl_space_reset_user(space);
700 map = isl_map_reset_space(map, space);
702 return map;
705 /* Reset the user pointer on all identifiers of parameters and tuples
706 * of the space of "set".
708 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
710 return isl_map_reset_user(set);
713 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
715 if (!bmap)
716 return -1;
717 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
720 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
722 return isl_basic_map_is_rational(bset);
725 /* Does "bmap" contain any rational points?
727 * If "bmap" has an equality for each dimension, equating the dimension
728 * to an integer constant, then it has no rational points, even if it
729 * is marked as rational.
731 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
733 int has_rational = 1;
734 unsigned total;
736 if (!bmap)
737 return -1;
738 if (isl_basic_map_plain_is_empty(bmap))
739 return 0;
740 if (!isl_basic_map_is_rational(bmap))
741 return 0;
742 bmap = isl_basic_map_copy(bmap);
743 bmap = isl_basic_map_implicit_equalities(bmap);
744 if (!bmap)
745 return -1;
746 total = isl_basic_map_total_dim(bmap);
747 if (bmap->n_eq == total) {
748 int i, j;
749 for (i = 0; i < bmap->n_eq; ++i) {
750 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
751 if (j < 0)
752 break;
753 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
754 !isl_int_is_negone(bmap->eq[i][1 + j]))
755 break;
756 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
757 total - j - 1);
758 if (j >= 0)
759 break;
761 if (i == bmap->n_eq)
762 has_rational = 0;
764 isl_basic_map_free(bmap);
766 return has_rational;
769 /* Does "map" contain any rational points?
771 int isl_map_has_rational(__isl_keep isl_map *map)
773 int i;
774 int has_rational;
776 if (!map)
777 return -1;
778 for (i = 0; i < map->n; ++i) {
779 has_rational = isl_basic_map_has_rational(map->p[i]);
780 if (has_rational < 0)
781 return -1;
782 if (has_rational)
783 return 1;
785 return 0;
788 /* Does "set" contain any rational points?
790 int isl_set_has_rational(__isl_keep isl_set *set)
792 return isl_map_has_rational(set);
795 /* Is this basic set a parameter domain?
797 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
799 if (!bset)
800 return -1;
801 return isl_space_is_params(bset->dim);
804 /* Is this set a parameter domain?
806 int isl_set_is_params(__isl_keep isl_set *set)
808 if (!set)
809 return -1;
810 return isl_space_is_params(set->dim);
813 /* Is this map actually a parameter domain?
814 * Users should never call this function. Outside of isl,
815 * a map can never be a parameter domain.
817 int isl_map_is_params(__isl_keep isl_map *map)
819 if (!map)
820 return -1;
821 return isl_space_is_params(map->dim);
824 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
825 struct isl_basic_map *bmap, unsigned extra,
826 unsigned n_eq, unsigned n_ineq)
828 int i;
829 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
831 bmap->ctx = ctx;
832 isl_ctx_ref(ctx);
834 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
835 if (isl_blk_is_error(bmap->block))
836 goto error;
838 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
839 if ((n_ineq + n_eq) && !bmap->ineq)
840 goto error;
842 if (extra == 0) {
843 bmap->block2 = isl_blk_empty();
844 bmap->div = NULL;
845 } else {
846 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
847 if (isl_blk_is_error(bmap->block2))
848 goto error;
850 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
851 if (!bmap->div)
852 goto error;
855 for (i = 0; i < n_ineq + n_eq; ++i)
856 bmap->ineq[i] = bmap->block.data + i * row_size;
858 for (i = 0; i < extra; ++i)
859 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
861 bmap->ref = 1;
862 bmap->flags = 0;
863 bmap->c_size = n_eq + n_ineq;
864 bmap->eq = bmap->ineq + n_ineq;
865 bmap->extra = extra;
866 bmap->n_eq = 0;
867 bmap->n_ineq = 0;
868 bmap->n_div = 0;
869 bmap->sample = NULL;
871 return bmap;
872 error:
873 isl_basic_map_free(bmap);
874 return NULL;
877 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
878 unsigned nparam, unsigned dim, unsigned extra,
879 unsigned n_eq, unsigned n_ineq)
881 struct isl_basic_map *bmap;
882 isl_space *space;
884 space = isl_space_set_alloc(ctx, nparam, dim);
885 if (!space)
886 return NULL;
888 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
889 return (struct isl_basic_set *)bmap;
892 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
893 unsigned extra, unsigned n_eq, unsigned n_ineq)
895 struct isl_basic_map *bmap;
896 if (!dim)
897 return NULL;
898 isl_assert(dim->ctx, dim->n_in == 0, goto error);
899 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
900 return (struct isl_basic_set *)bmap;
901 error:
902 isl_space_free(dim);
903 return NULL;
906 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
907 unsigned extra, unsigned n_eq, unsigned n_ineq)
909 struct isl_basic_map *bmap;
911 if (!dim)
912 return NULL;
913 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
914 if (!bmap)
915 goto error;
916 bmap->dim = dim;
918 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
919 error:
920 isl_space_free(dim);
921 return NULL;
924 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
925 unsigned nparam, unsigned in, unsigned out, unsigned extra,
926 unsigned n_eq, unsigned n_ineq)
928 struct isl_basic_map *bmap;
929 isl_space *dim;
931 dim = isl_space_alloc(ctx, nparam, in, out);
932 if (!dim)
933 return NULL;
935 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
936 return bmap;
939 static void dup_constraints(
940 struct isl_basic_map *dst, struct isl_basic_map *src)
942 int i;
943 unsigned total = isl_basic_map_total_dim(src);
945 for (i = 0; i < src->n_eq; ++i) {
946 int j = isl_basic_map_alloc_equality(dst);
947 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
950 for (i = 0; i < src->n_ineq; ++i) {
951 int j = isl_basic_map_alloc_inequality(dst);
952 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
955 for (i = 0; i < src->n_div; ++i) {
956 int j = isl_basic_map_alloc_div(dst);
957 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
959 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
962 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
964 struct isl_basic_map *dup;
966 if (!bmap)
967 return NULL;
968 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
969 bmap->n_div, bmap->n_eq, bmap->n_ineq);
970 if (!dup)
971 return NULL;
972 dup_constraints(dup, bmap);
973 dup->flags = bmap->flags;
974 dup->sample = isl_vec_copy(bmap->sample);
975 return dup;
978 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
980 struct isl_basic_map *dup;
982 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
983 return (struct isl_basic_set *)dup;
986 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
988 if (!bset)
989 return NULL;
991 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
992 bset->ref++;
993 return bset;
995 return isl_basic_set_dup(bset);
998 struct isl_set *isl_set_copy(struct isl_set *set)
1000 if (!set)
1001 return NULL;
1003 set->ref++;
1004 return set;
1007 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1009 if (!bmap)
1010 return NULL;
1012 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1013 bmap->ref++;
1014 return bmap;
1016 bmap = isl_basic_map_dup(bmap);
1017 if (bmap)
1018 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1019 return bmap;
1022 struct isl_map *isl_map_copy(struct isl_map *map)
1024 if (!map)
1025 return NULL;
1027 map->ref++;
1028 return map;
1031 void *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1033 if (!bmap)
1034 return NULL;
1036 if (--bmap->ref > 0)
1037 return NULL;
1039 isl_ctx_deref(bmap->ctx);
1040 free(bmap->div);
1041 isl_blk_free(bmap->ctx, bmap->block2);
1042 free(bmap->ineq);
1043 isl_blk_free(bmap->ctx, bmap->block);
1044 isl_vec_free(bmap->sample);
1045 isl_space_free(bmap->dim);
1046 free(bmap);
1048 return NULL;
1051 void *isl_basic_set_free(struct isl_basic_set *bset)
1053 return isl_basic_map_free((struct isl_basic_map *)bset);
1056 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1058 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1061 __isl_give isl_map *isl_map_align_params_map_map_and(
1062 __isl_take isl_map *map1, __isl_take isl_map *map2,
1063 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1064 __isl_take isl_map *map2))
1066 if (!map1 || !map2)
1067 goto error;
1068 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1069 return fn(map1, map2);
1070 if (!isl_space_has_named_params(map1->dim) ||
1071 !isl_space_has_named_params(map2->dim))
1072 isl_die(map1->ctx, isl_error_invalid,
1073 "unaligned unnamed parameters", goto error);
1074 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1075 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1076 return fn(map1, map2);
1077 error:
1078 isl_map_free(map1);
1079 isl_map_free(map2);
1080 return NULL;
1083 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1084 __isl_keep isl_map *map2,
1085 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1087 int r;
1089 if (!map1 || !map2)
1090 return -1;
1091 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1092 return fn(map1, map2);
1093 if (!isl_space_has_named_params(map1->dim) ||
1094 !isl_space_has_named_params(map2->dim))
1095 isl_die(map1->ctx, isl_error_invalid,
1096 "unaligned unnamed parameters", return -1);
1097 map1 = isl_map_copy(map1);
1098 map2 = isl_map_copy(map2);
1099 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1100 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1101 r = fn(map1, map2);
1102 isl_map_free(map1);
1103 isl_map_free(map2);
1104 return r;
1107 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1109 struct isl_ctx *ctx;
1110 if (!bmap)
1111 return -1;
1112 ctx = bmap->ctx;
1113 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1114 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1115 return -1);
1116 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1117 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1118 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1119 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1120 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1121 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1122 isl_int *t;
1123 int j = isl_basic_map_alloc_inequality(bmap);
1124 if (j < 0)
1125 return -1;
1126 t = bmap->ineq[j];
1127 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1128 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1129 bmap->eq[-1] = t;
1130 bmap->n_eq++;
1131 bmap->n_ineq--;
1132 bmap->eq--;
1133 return 0;
1135 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1136 bmap->extra - bmap->n_div);
1137 return bmap->n_eq++;
1140 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1142 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1145 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1147 if (!bmap)
1148 return -1;
1149 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1150 bmap->n_eq -= n;
1151 return 0;
1154 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1156 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1159 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1161 isl_int *t;
1162 if (!bmap)
1163 return -1;
1164 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1166 if (pos != bmap->n_eq - 1) {
1167 t = bmap->eq[pos];
1168 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1169 bmap->eq[bmap->n_eq - 1] = t;
1171 bmap->n_eq--;
1172 return 0;
1175 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1177 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1180 /* Turn inequality "pos" of "bmap" into an equality.
1182 * In particular, we move the inequality in front of the equalities
1183 * and move the last inequality in the position of the moved inequality.
1184 * Note that isl_tab_make_equalities_explicit depends on this particular
1185 * change in the ordering of the constraints.
1187 void isl_basic_map_inequality_to_equality(
1188 struct isl_basic_map *bmap, unsigned pos)
1190 isl_int *t;
1192 t = bmap->ineq[pos];
1193 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1194 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1195 bmap->eq[-1] = t;
1196 bmap->n_eq++;
1197 bmap->n_ineq--;
1198 bmap->eq--;
1199 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1200 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1201 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1202 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1205 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1207 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1210 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1212 struct isl_ctx *ctx;
1213 if (!bmap)
1214 return -1;
1215 ctx = bmap->ctx;
1216 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1217 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1218 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1219 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1220 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1221 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1222 1 + isl_basic_map_total_dim(bmap),
1223 bmap->extra - bmap->n_div);
1224 return bmap->n_ineq++;
1227 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1229 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1232 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1234 if (!bmap)
1235 return -1;
1236 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1237 bmap->n_ineq -= n;
1238 return 0;
1241 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1243 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1246 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1248 isl_int *t;
1249 if (!bmap)
1250 return -1;
1251 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1253 if (pos != bmap->n_ineq - 1) {
1254 t = bmap->ineq[pos];
1255 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1256 bmap->ineq[bmap->n_ineq - 1] = t;
1257 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1259 bmap->n_ineq--;
1260 return 0;
1263 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1265 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1268 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1269 isl_int *eq)
1271 int k;
1273 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1274 if (!bmap)
1275 return NULL;
1276 k = isl_basic_map_alloc_equality(bmap);
1277 if (k < 0)
1278 goto error;
1279 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1280 return bmap;
1281 error:
1282 isl_basic_map_free(bmap);
1283 return NULL;
1286 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1287 isl_int *eq)
1289 return (isl_basic_set *)
1290 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1293 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1294 isl_int *ineq)
1296 int k;
1298 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1299 if (!bmap)
1300 return NULL;
1301 k = isl_basic_map_alloc_inequality(bmap);
1302 if (k < 0)
1303 goto error;
1304 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1305 return bmap;
1306 error:
1307 isl_basic_map_free(bmap);
1308 return NULL;
1311 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1312 isl_int *ineq)
1314 return (isl_basic_set *)
1315 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1318 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1320 if (!bmap)
1321 return -1;
1322 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1323 isl_seq_clr(bmap->div[bmap->n_div] +
1324 1 + 1 + isl_basic_map_total_dim(bmap),
1325 bmap->extra - bmap->n_div);
1326 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1327 return bmap->n_div++;
1330 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1332 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1335 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1337 if (!bmap)
1338 return -1;
1339 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1340 bmap->n_div -= n;
1341 return 0;
1344 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1346 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1349 /* Copy constraint from src to dst, putting the vars of src at offset
1350 * dim_off in dst and the divs of src at offset div_off in dst.
1351 * If both sets are actually map, then dim_off applies to the input
1352 * variables.
1354 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1355 struct isl_basic_map *src_map, isl_int *src,
1356 unsigned in_off, unsigned out_off, unsigned div_off)
1358 unsigned src_nparam = isl_basic_map_n_param(src_map);
1359 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1360 unsigned src_in = isl_basic_map_n_in(src_map);
1361 unsigned dst_in = isl_basic_map_n_in(dst_map);
1362 unsigned src_out = isl_basic_map_n_out(src_map);
1363 unsigned dst_out = isl_basic_map_n_out(dst_map);
1364 isl_int_set(dst[0], src[0]);
1365 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1366 if (dst_nparam > src_nparam)
1367 isl_seq_clr(dst+1+src_nparam,
1368 dst_nparam - src_nparam);
1369 isl_seq_clr(dst+1+dst_nparam, in_off);
1370 isl_seq_cpy(dst+1+dst_nparam+in_off,
1371 src+1+src_nparam,
1372 isl_min(dst_in-in_off, src_in));
1373 if (dst_in-in_off > src_in)
1374 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1375 dst_in - in_off - src_in);
1376 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1377 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1378 src+1+src_nparam+src_in,
1379 isl_min(dst_out-out_off, src_out));
1380 if (dst_out-out_off > src_out)
1381 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1382 dst_out - out_off - src_out);
1383 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1384 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1385 src+1+src_nparam+src_in+src_out,
1386 isl_min(dst_map->extra-div_off, src_map->n_div));
1387 if (dst_map->n_div-div_off > src_map->n_div)
1388 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1389 div_off+src_map->n_div,
1390 dst_map->n_div - div_off - src_map->n_div);
1393 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1394 struct isl_basic_map *src_map, isl_int *src,
1395 unsigned in_off, unsigned out_off, unsigned div_off)
1397 isl_int_set(dst[0], src[0]);
1398 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1401 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1402 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1404 int i;
1405 unsigned div_off;
1407 if (!bmap1 || !bmap2)
1408 goto error;
1410 div_off = bmap1->n_div;
1412 for (i = 0; i < bmap2->n_eq; ++i) {
1413 int i1 = isl_basic_map_alloc_equality(bmap1);
1414 if (i1 < 0)
1415 goto error;
1416 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1417 i_pos, o_pos, div_off);
1420 for (i = 0; i < bmap2->n_ineq; ++i) {
1421 int i1 = isl_basic_map_alloc_inequality(bmap1);
1422 if (i1 < 0)
1423 goto error;
1424 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1425 i_pos, o_pos, div_off);
1428 for (i = 0; i < bmap2->n_div; ++i) {
1429 int i1 = isl_basic_map_alloc_div(bmap1);
1430 if (i1 < 0)
1431 goto error;
1432 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1433 i_pos, o_pos, div_off);
1436 isl_basic_map_free(bmap2);
1438 return bmap1;
1440 error:
1441 isl_basic_map_free(bmap1);
1442 isl_basic_map_free(bmap2);
1443 return NULL;
1446 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1447 struct isl_basic_set *bset2, unsigned pos)
1449 return (struct isl_basic_set *)
1450 add_constraints((struct isl_basic_map *)bset1,
1451 (struct isl_basic_map *)bset2, 0, pos);
1454 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1455 __isl_take isl_space *dim, unsigned extra,
1456 unsigned n_eq, unsigned n_ineq)
1458 struct isl_basic_map *ext;
1459 unsigned flags;
1460 int dims_ok;
1462 if (!dim)
1463 goto error;
1465 if (!base)
1466 goto error;
1468 dims_ok = isl_space_is_equal(base->dim, dim) &&
1469 base->extra >= base->n_div + extra;
1471 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1472 room_for_ineq(base, n_ineq)) {
1473 isl_space_free(dim);
1474 return base;
1477 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1478 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1479 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1480 extra += base->extra;
1481 n_eq += base->n_eq;
1482 n_ineq += base->n_ineq;
1484 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1485 dim = NULL;
1486 if (!ext)
1487 goto error;
1489 if (dims_ok)
1490 ext->sample = isl_vec_copy(base->sample);
1491 flags = base->flags;
1492 ext = add_constraints(ext, base, 0, 0);
1493 if (ext) {
1494 ext->flags = flags;
1495 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1498 return ext;
1500 error:
1501 isl_space_free(dim);
1502 isl_basic_map_free(base);
1503 return NULL;
1506 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1507 __isl_take isl_space *dim, unsigned extra,
1508 unsigned n_eq, unsigned n_ineq)
1510 return (struct isl_basic_set *)
1511 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1512 extra, n_eq, n_ineq);
1515 struct isl_basic_map *isl_basic_map_extend_constraints(
1516 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1518 if (!base)
1519 return NULL;
1520 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1521 0, n_eq, n_ineq);
1524 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1525 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1526 unsigned n_eq, unsigned n_ineq)
1528 struct isl_basic_map *bmap;
1529 isl_space *dim;
1531 if (!base)
1532 return NULL;
1533 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1534 if (!dim)
1535 goto error;
1537 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1538 return bmap;
1539 error:
1540 isl_basic_map_free(base);
1541 return NULL;
1544 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1545 unsigned nparam, unsigned dim, unsigned extra,
1546 unsigned n_eq, unsigned n_ineq)
1548 return (struct isl_basic_set *)
1549 isl_basic_map_extend((struct isl_basic_map *)base,
1550 nparam, 0, dim, extra, n_eq, n_ineq);
1553 struct isl_basic_set *isl_basic_set_extend_constraints(
1554 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1556 return (struct isl_basic_set *)
1557 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1558 n_eq, n_ineq);
1561 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1563 return (struct isl_basic_set *)
1564 isl_basic_map_cow((struct isl_basic_map *)bset);
1567 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1569 if (!bmap)
1570 return NULL;
1572 if (bmap->ref > 1) {
1573 bmap->ref--;
1574 bmap = isl_basic_map_dup(bmap);
1576 if (bmap)
1577 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1578 return bmap;
1581 struct isl_set *isl_set_cow(struct isl_set *set)
1583 if (!set)
1584 return NULL;
1586 if (set->ref == 1)
1587 return set;
1588 set->ref--;
1589 return isl_set_dup(set);
1592 struct isl_map *isl_map_cow(struct isl_map *map)
1594 if (!map)
1595 return NULL;
1597 if (map->ref == 1)
1598 return map;
1599 map->ref--;
1600 return isl_map_dup(map);
1603 static void swap_vars(struct isl_blk blk, isl_int *a,
1604 unsigned a_len, unsigned b_len)
1606 isl_seq_cpy(blk.data, a+a_len, b_len);
1607 isl_seq_cpy(blk.data+b_len, a, a_len);
1608 isl_seq_cpy(a, blk.data, b_len+a_len);
1611 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1612 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1614 int i;
1615 struct isl_blk blk;
1617 if (!bmap)
1618 goto error;
1620 isl_assert(bmap->ctx,
1621 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1623 if (n1 == 0 || n2 == 0)
1624 return bmap;
1626 bmap = isl_basic_map_cow(bmap);
1627 if (!bmap)
1628 return NULL;
1630 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1631 if (isl_blk_is_error(blk))
1632 goto error;
1634 for (i = 0; i < bmap->n_eq; ++i)
1635 swap_vars(blk,
1636 bmap->eq[i] + pos, n1, n2);
1638 for (i = 0; i < bmap->n_ineq; ++i)
1639 swap_vars(blk,
1640 bmap->ineq[i] + pos, n1, n2);
1642 for (i = 0; i < bmap->n_div; ++i)
1643 swap_vars(blk,
1644 bmap->div[i]+1 + pos, n1, n2);
1646 isl_blk_free(bmap->ctx, blk);
1648 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1649 bmap = isl_basic_map_gauss(bmap, NULL);
1650 return isl_basic_map_finalize(bmap);
1651 error:
1652 isl_basic_map_free(bmap);
1653 return NULL;
1656 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1657 __isl_take isl_basic_set *bset, unsigned n)
1659 unsigned dim;
1660 unsigned nparam;
1662 if (!bset)
1663 return NULL;
1665 nparam = isl_basic_set_n_param(bset);
1666 dim = isl_basic_set_n_dim(bset);
1667 isl_assert(bset->ctx, n <= dim, goto error);
1669 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1670 error:
1671 isl_basic_set_free(bset);
1672 return NULL;
1675 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1677 int i = 0;
1678 unsigned total;
1679 if (!bmap)
1680 goto error;
1681 total = isl_basic_map_total_dim(bmap);
1682 isl_basic_map_free_div(bmap, bmap->n_div);
1683 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1684 if (bmap->n_eq > 0)
1685 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1686 else {
1687 i = isl_basic_map_alloc_equality(bmap);
1688 if (i < 0)
1689 goto error;
1691 isl_int_set_si(bmap->eq[i][0], 1);
1692 isl_seq_clr(bmap->eq[i]+1, total);
1693 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1694 isl_vec_free(bmap->sample);
1695 bmap->sample = NULL;
1696 return isl_basic_map_finalize(bmap);
1697 error:
1698 isl_basic_map_free(bmap);
1699 return NULL;
1702 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1704 return (struct isl_basic_set *)
1705 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1708 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1709 * of "bmap").
1711 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1713 isl_int *t = bmap->div[a];
1714 bmap->div[a] = bmap->div[b];
1715 bmap->div[b] = t;
1718 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1719 * div definitions accordingly.
1721 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1723 int i;
1724 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1726 swap_div(bmap, a, b);
1728 for (i = 0; i < bmap->n_eq; ++i)
1729 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1731 for (i = 0; i < bmap->n_ineq; ++i)
1732 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1734 for (i = 0; i < bmap->n_div; ++i)
1735 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1736 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1739 /* Eliminate the specified n dimensions starting at first from the
1740 * constraints, without removing the dimensions from the space.
1741 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1743 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1744 enum isl_dim_type type, unsigned first, unsigned n)
1746 int i;
1748 if (!map)
1749 return NULL;
1750 if (n == 0)
1751 return map;
1753 if (first + n > isl_map_dim(map, type) || first + n < first)
1754 isl_die(map->ctx, isl_error_invalid,
1755 "index out of bounds", goto error);
1757 map = isl_map_cow(map);
1758 if (!map)
1759 return NULL;
1761 for (i = 0; i < map->n; ++i) {
1762 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1763 if (!map->p[i])
1764 goto error;
1766 return map;
1767 error:
1768 isl_map_free(map);
1769 return NULL;
1772 /* Eliminate the specified n dimensions starting at first from the
1773 * constraints, without removing the dimensions from the space.
1774 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1776 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1777 enum isl_dim_type type, unsigned first, unsigned n)
1779 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1782 /* Eliminate the specified n dimensions starting at first from the
1783 * constraints, without removing the dimensions from the space.
1784 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1786 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1787 unsigned first, unsigned n)
1789 return isl_set_eliminate(set, isl_dim_set, first, n);
1792 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1793 __isl_take isl_basic_map *bmap)
1795 if (!bmap)
1796 return NULL;
1797 bmap = isl_basic_map_eliminate_vars(bmap,
1798 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1799 if (!bmap)
1800 return NULL;
1801 bmap->n_div = 0;
1802 return isl_basic_map_finalize(bmap);
1805 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1806 __isl_take isl_basic_set *bset)
1808 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1809 (struct isl_basic_map *)bset);
1812 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1814 int i;
1816 if (!map)
1817 return NULL;
1818 if (map->n == 0)
1819 return map;
1821 map = isl_map_cow(map);
1822 if (!map)
1823 return NULL;
1825 for (i = 0; i < map->n; ++i) {
1826 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1827 if (!map->p[i])
1828 goto error;
1830 return map;
1831 error:
1832 isl_map_free(map);
1833 return NULL;
1836 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1838 return isl_map_remove_divs(set);
1841 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1842 enum isl_dim_type type, unsigned first, unsigned n)
1844 if (!bmap)
1845 return NULL;
1846 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1847 goto error);
1848 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1849 return bmap;
1850 bmap = isl_basic_map_eliminate_vars(bmap,
1851 isl_basic_map_offset(bmap, type) - 1 + first, n);
1852 if (!bmap)
1853 return bmap;
1854 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1855 return bmap;
1856 bmap = isl_basic_map_drop(bmap, type, first, n);
1857 return bmap;
1858 error:
1859 isl_basic_map_free(bmap);
1860 return NULL;
1863 /* Return true if the definition of the given div (recursively) involves
1864 * any of the given variables.
1866 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1867 unsigned first, unsigned n)
1869 int i;
1870 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1872 if (isl_int_is_zero(bmap->div[div][0]))
1873 return 0;
1874 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1875 return 1;
1877 for (i = bmap->n_div - 1; i >= 0; --i) {
1878 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1879 continue;
1880 if (div_involves_vars(bmap, i, first, n))
1881 return 1;
1884 return 0;
1887 /* Try and add a lower and/or upper bound on "div" to "bmap"
1888 * based on inequality "i".
1889 * "total" is the total number of variables (excluding the divs).
1890 * "v" is a temporary object that can be used during the calculations.
1891 * If "lb" is set, then a lower bound should be constructed.
1892 * If "ub" is set, then an upper bound should be constructed.
1894 * The calling function has already checked that the inequality does not
1895 * reference "div", but we still need to check that the inequality is
1896 * of the right form. We'll consider the case where we want to construct
1897 * a lower bound. The construction of upper bounds is similar.
1899 * Let "div" be of the form
1901 * q = floor((a + f(x))/d)
1903 * We essentially check if constraint "i" is of the form
1905 * b + f(x) >= 0
1907 * so that we can use it to derive a lower bound on "div".
1908 * However, we allow a slightly more general form
1910 * b + g(x) >= 0
1912 * with the condition that the coefficients of g(x) - f(x) are all
1913 * divisible by d.
1914 * Rewriting this constraint as
1916 * 0 >= -b - g(x)
1918 * adding a + f(x) to both sides and dividing by d, we obtain
1920 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1922 * Taking the floor on both sides, we obtain
1924 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1926 * or
1928 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1930 * In the case of an upper bound, we construct the constraint
1932 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1935 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1936 __isl_take isl_basic_map *bmap, int div, int i,
1937 unsigned total, isl_int v, int lb, int ub)
1939 int j;
1941 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1942 if (lb) {
1943 isl_int_sub(v, bmap->ineq[i][1 + j],
1944 bmap->div[div][1 + 1 + j]);
1945 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1947 if (ub) {
1948 isl_int_add(v, bmap->ineq[i][1 + j],
1949 bmap->div[div][1 + 1 + j]);
1950 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1953 if (!lb && !ub)
1954 return bmap;
1956 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1957 if (lb) {
1958 int k = isl_basic_map_alloc_inequality(bmap);
1959 if (k < 0)
1960 goto error;
1961 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1962 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1963 bmap->div[div][1 + j]);
1964 isl_int_cdiv_q(bmap->ineq[k][j],
1965 bmap->ineq[k][j], bmap->div[div][0]);
1967 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1969 if (ub) {
1970 int k = isl_basic_map_alloc_inequality(bmap);
1971 if (k < 0)
1972 goto error;
1973 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1974 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1975 bmap->div[div][1 + j]);
1976 isl_int_fdiv_q(bmap->ineq[k][j],
1977 bmap->ineq[k][j], bmap->div[div][0]);
1979 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
1982 return bmap;
1983 error:
1984 isl_basic_map_free(bmap);
1985 return NULL;
1988 /* This function is called right before "div" is eliminated from "bmap"
1989 * using Fourier-Motzkin.
1990 * Look through the constraints of "bmap" for constraints on the argument
1991 * of the integer division and use them to construct constraints on the
1992 * integer division itself. These constraints can then be combined
1993 * during the Fourier-Motzkin elimination.
1994 * Note that it is only useful to introduce lower bounds on "div"
1995 * if "bmap" already contains upper bounds on "div" as the newly
1996 * introduce lower bounds can then be combined with the pre-existing
1997 * upper bounds. Similarly for upper bounds.
1998 * We therefore first check if "bmap" contains any lower and/or upper bounds
1999 * on "div".
2001 * It is interesting to note that the introduction of these constraints
2002 * can indeed lead to more accurate results, even when compared to
2003 * deriving constraints on the argument of "div" from constraints on "div".
2004 * Consider, for example, the set
2006 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2008 * The second constraint can be rewritten as
2010 * 2 * [(-i-2j+3)/4] + k >= 0
2012 * from which we can derive
2014 * -i - 2j + 3 >= -2k
2016 * or
2018 * i + 2j <= 3 + 2k
2020 * Combined with the first constraint, we obtain
2022 * -3 <= 3 + 2k or k >= -3
2024 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2025 * the first constraint, we obtain
2027 * [(i + 2j)/4] >= [-3/4] = -1
2029 * Combining this constraint with the second constraint, we obtain
2031 * k >= -2
2033 static __isl_give isl_basic_map *insert_bounds_on_div(
2034 __isl_take isl_basic_map *bmap, int div)
2036 int i;
2037 int check_lb, check_ub;
2038 isl_int v;
2039 unsigned total;
2041 if (!bmap)
2042 return NULL;
2044 if (isl_int_is_zero(bmap->div[div][0]))
2045 return bmap;
2047 total = isl_space_dim(bmap->dim, isl_dim_all);
2049 check_lb = 0;
2050 check_ub = 0;
2051 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2052 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2053 if (s > 0)
2054 check_ub = 1;
2055 if (s < 0)
2056 check_lb = 1;
2059 if (!check_lb && !check_ub)
2060 return bmap;
2062 isl_int_init(v);
2064 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2065 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2066 continue;
2068 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2069 check_lb, check_ub);
2072 isl_int_clear(v);
2074 return bmap;
2077 /* Remove all divs (recursively) involving any of the given dimensions
2078 * in their definitions.
2080 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2081 __isl_take isl_basic_map *bmap,
2082 enum isl_dim_type type, unsigned first, unsigned n)
2084 int i;
2086 if (!bmap)
2087 return NULL;
2088 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2089 goto error);
2090 first += isl_basic_map_offset(bmap, type);
2092 for (i = bmap->n_div - 1; i >= 0; --i) {
2093 if (!div_involves_vars(bmap, i, first, n))
2094 continue;
2095 bmap = insert_bounds_on_div(bmap, i);
2096 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2097 if (!bmap)
2098 return NULL;
2099 i = bmap->n_div;
2102 return bmap;
2103 error:
2104 isl_basic_map_free(bmap);
2105 return NULL;
2108 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2109 __isl_take isl_basic_set *bset,
2110 enum isl_dim_type type, unsigned first, unsigned n)
2112 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2115 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2116 enum isl_dim_type type, unsigned first, unsigned n)
2118 int i;
2120 if (!map)
2121 return NULL;
2122 if (map->n == 0)
2123 return map;
2125 map = isl_map_cow(map);
2126 if (!map)
2127 return NULL;
2129 for (i = 0; i < map->n; ++i) {
2130 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2131 type, first, n);
2132 if (!map->p[i])
2133 goto error;
2135 return map;
2136 error:
2137 isl_map_free(map);
2138 return NULL;
2141 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2142 enum isl_dim_type type, unsigned first, unsigned n)
2144 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2145 type, first, n);
2148 /* Does the desciption of "bmap" depend on the specified dimensions?
2149 * We also check whether the dimensions appear in any of the div definitions.
2150 * In principle there is no need for this check. If the dimensions appear
2151 * in a div definition, they also appear in the defining constraints of that
2152 * div.
2154 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2155 enum isl_dim_type type, unsigned first, unsigned n)
2157 int i;
2159 if (!bmap)
2160 return -1;
2162 if (first + n > isl_basic_map_dim(bmap, type))
2163 isl_die(bmap->ctx, isl_error_invalid,
2164 "index out of bounds", return -1);
2166 first += isl_basic_map_offset(bmap, type);
2167 for (i = 0; i < bmap->n_eq; ++i)
2168 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2169 return 1;
2170 for (i = 0; i < bmap->n_ineq; ++i)
2171 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2172 return 1;
2173 for (i = 0; i < bmap->n_div; ++i) {
2174 if (isl_int_is_zero(bmap->div[i][0]))
2175 continue;
2176 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2177 return 1;
2180 return 0;
2183 int isl_map_involves_dims(__isl_keep isl_map *map,
2184 enum isl_dim_type type, unsigned first, unsigned n)
2186 int i;
2188 if (!map)
2189 return -1;
2191 if (first + n > isl_map_dim(map, type))
2192 isl_die(map->ctx, isl_error_invalid,
2193 "index out of bounds", return -1);
2195 for (i = 0; i < map->n; ++i) {
2196 int involves = isl_basic_map_involves_dims(map->p[i],
2197 type, first, n);
2198 if (involves < 0 || involves)
2199 return involves;
2202 return 0;
2205 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2206 enum isl_dim_type type, unsigned first, unsigned n)
2208 return isl_basic_map_involves_dims(bset, type, first, n);
2211 int isl_set_involves_dims(__isl_keep isl_set *set,
2212 enum isl_dim_type type, unsigned first, unsigned n)
2214 return isl_map_involves_dims(set, type, first, n);
2217 /* Return true if the definition of the given div is unknown or depends
2218 * on unknown divs.
2220 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2222 int i;
2223 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2225 if (isl_int_is_zero(bmap->div[div][0]))
2226 return 1;
2228 for (i = bmap->n_div - 1; i >= 0; --i) {
2229 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2230 continue;
2231 if (div_is_unknown(bmap, i))
2232 return 1;
2235 return 0;
2238 /* Remove all divs that are unknown or defined in terms of unknown divs.
2240 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2241 __isl_take isl_basic_map *bmap)
2243 int i;
2245 if (!bmap)
2246 return NULL;
2248 for (i = bmap->n_div - 1; i >= 0; --i) {
2249 if (!div_is_unknown(bmap, i))
2250 continue;
2251 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2252 if (!bmap)
2253 return NULL;
2254 i = bmap->n_div;
2257 return bmap;
2260 /* Remove all divs that are unknown or defined in terms of unknown divs.
2262 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2263 __isl_take isl_basic_set *bset)
2265 return isl_basic_map_remove_unknown_divs(bset);
2268 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2270 int i;
2272 if (!map)
2273 return NULL;
2274 if (map->n == 0)
2275 return map;
2277 map = isl_map_cow(map);
2278 if (!map)
2279 return NULL;
2281 for (i = 0; i < map->n; ++i) {
2282 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2283 if (!map->p[i])
2284 goto error;
2286 return map;
2287 error:
2288 isl_map_free(map);
2289 return NULL;
2292 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2294 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2297 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2298 __isl_take isl_basic_set *bset,
2299 enum isl_dim_type type, unsigned first, unsigned n)
2301 return (isl_basic_set *)
2302 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2305 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2306 enum isl_dim_type type, unsigned first, unsigned n)
2308 int i;
2310 if (n == 0)
2311 return map;
2313 map = isl_map_cow(map);
2314 if (!map)
2315 return NULL;
2316 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2318 for (i = 0; i < map->n; ++i) {
2319 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2320 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2321 if (!map->p[i])
2322 goto error;
2324 map = isl_map_drop(map, type, first, n);
2325 return map;
2326 error:
2327 isl_map_free(map);
2328 return NULL;
2331 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2332 enum isl_dim_type type, unsigned first, unsigned n)
2334 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2337 /* Project out n inputs starting at first using Fourier-Motzkin */
2338 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2339 unsigned first, unsigned n)
2341 return isl_map_remove_dims(map, isl_dim_in, first, n);
2344 static void dump_term(struct isl_basic_map *bmap,
2345 isl_int c, int pos, FILE *out)
2347 const char *name;
2348 unsigned in = isl_basic_map_n_in(bmap);
2349 unsigned dim = in + isl_basic_map_n_out(bmap);
2350 unsigned nparam = isl_basic_map_n_param(bmap);
2351 if (!pos)
2352 isl_int_print(out, c, 0);
2353 else {
2354 if (!isl_int_is_one(c))
2355 isl_int_print(out, c, 0);
2356 if (pos < 1 + nparam) {
2357 name = isl_space_get_dim_name(bmap->dim,
2358 isl_dim_param, pos - 1);
2359 if (name)
2360 fprintf(out, "%s", name);
2361 else
2362 fprintf(out, "p%d", pos - 1);
2363 } else if (pos < 1 + nparam + in)
2364 fprintf(out, "i%d", pos - 1 - nparam);
2365 else if (pos < 1 + nparam + dim)
2366 fprintf(out, "o%d", pos - 1 - nparam - in);
2367 else
2368 fprintf(out, "e%d", pos - 1 - nparam - dim);
2372 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2373 int sign, FILE *out)
2375 int i;
2376 int first;
2377 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2378 isl_int v;
2380 isl_int_init(v);
2381 for (i = 0, first = 1; i < len; ++i) {
2382 if (isl_int_sgn(c[i]) * sign <= 0)
2383 continue;
2384 if (!first)
2385 fprintf(out, " + ");
2386 first = 0;
2387 isl_int_abs(v, c[i]);
2388 dump_term(bmap, v, i, out);
2390 isl_int_clear(v);
2391 if (first)
2392 fprintf(out, "0");
2395 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2396 const char *op, FILE *out, int indent)
2398 int i;
2400 fprintf(out, "%*s", indent, "");
2402 dump_constraint_sign(bmap, c, 1, out);
2403 fprintf(out, " %s ", op);
2404 dump_constraint_sign(bmap, c, -1, out);
2406 fprintf(out, "\n");
2408 for (i = bmap->n_div; i < bmap->extra; ++i) {
2409 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2410 continue;
2411 fprintf(out, "%*s", indent, "");
2412 fprintf(out, "ERROR: unused div coefficient not zero\n");
2413 abort();
2417 static void dump_constraints(struct isl_basic_map *bmap,
2418 isl_int **c, unsigned n,
2419 const char *op, FILE *out, int indent)
2421 int i;
2423 for (i = 0; i < n; ++i)
2424 dump_constraint(bmap, c[i], op, out, indent);
2427 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2429 int j;
2430 int first = 1;
2431 unsigned total = isl_basic_map_total_dim(bmap);
2433 for (j = 0; j < 1 + total; ++j) {
2434 if (isl_int_is_zero(exp[j]))
2435 continue;
2436 if (!first && isl_int_is_pos(exp[j]))
2437 fprintf(out, "+");
2438 dump_term(bmap, exp[j], j, out);
2439 first = 0;
2443 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2445 int i;
2447 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2448 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2450 for (i = 0; i < bmap->n_div; ++i) {
2451 fprintf(out, "%*s", indent, "");
2452 fprintf(out, "e%d = [(", i);
2453 dump_affine(bmap, bmap->div[i]+1, out);
2454 fprintf(out, ")/");
2455 isl_int_print(out, bmap->div[i][0], 0);
2456 fprintf(out, "]\n");
2460 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2461 FILE *out, int indent)
2463 if (!bset) {
2464 fprintf(out, "null basic set\n");
2465 return;
2468 fprintf(out, "%*s", indent, "");
2469 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2470 bset->ref, bset->dim->nparam, bset->dim->n_out,
2471 bset->extra, bset->flags);
2472 dump((struct isl_basic_map *)bset, out, indent);
2475 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2476 FILE *out, int indent)
2478 if (!bmap) {
2479 fprintf(out, "null basic map\n");
2480 return;
2483 fprintf(out, "%*s", indent, "");
2484 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2485 "flags: %x, n_name: %d\n",
2486 bmap->ref,
2487 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2488 bmap->extra, bmap->flags, bmap->dim->n_id);
2489 dump(bmap, out, indent);
2492 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2494 unsigned total;
2495 if (!bmap)
2496 return -1;
2497 total = isl_basic_map_total_dim(bmap);
2498 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2499 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2500 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2501 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2502 return 0;
2505 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2506 unsigned flags)
2508 struct isl_set *set;
2510 if (!dim)
2511 return NULL;
2512 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2513 isl_assert(dim->ctx, n >= 0, goto error);
2514 set = isl_alloc(dim->ctx, struct isl_set,
2515 sizeof(struct isl_set) +
2516 (n - 1) * sizeof(struct isl_basic_set *));
2517 if (!set)
2518 goto error;
2520 set->ctx = dim->ctx;
2521 isl_ctx_ref(set->ctx);
2522 set->ref = 1;
2523 set->size = n;
2524 set->n = 0;
2525 set->dim = dim;
2526 set->flags = flags;
2527 return set;
2528 error:
2529 isl_space_free(dim);
2530 return NULL;
2533 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2534 unsigned nparam, unsigned dim, int n, unsigned flags)
2536 struct isl_set *set;
2537 isl_space *dims;
2539 dims = isl_space_alloc(ctx, nparam, 0, dim);
2540 if (!dims)
2541 return NULL;
2543 set = isl_set_alloc_space(dims, n, flags);
2544 return set;
2547 /* Make sure "map" has room for at least "n" more basic maps.
2549 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2551 int i;
2552 struct isl_map *grown = NULL;
2554 if (!map)
2555 return NULL;
2556 isl_assert(map->ctx, n >= 0, goto error);
2557 if (map->n + n <= map->size)
2558 return map;
2559 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2560 if (!grown)
2561 goto error;
2562 for (i = 0; i < map->n; ++i) {
2563 grown->p[i] = isl_basic_map_copy(map->p[i]);
2564 if (!grown->p[i])
2565 goto error;
2566 grown->n++;
2568 isl_map_free(map);
2569 return grown;
2570 error:
2571 isl_map_free(grown);
2572 isl_map_free(map);
2573 return NULL;
2576 /* Make sure "set" has room for at least "n" more basic sets.
2578 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2580 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2583 struct isl_set *isl_set_dup(struct isl_set *set)
2585 int i;
2586 struct isl_set *dup;
2588 if (!set)
2589 return NULL;
2591 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2592 if (!dup)
2593 return NULL;
2594 for (i = 0; i < set->n; ++i)
2595 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2596 return dup;
2599 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2601 return isl_map_from_basic_map(bset);
2604 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2606 struct isl_map *map;
2608 if (!bmap)
2609 return NULL;
2611 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2612 return isl_map_add_basic_map(map, bmap);
2615 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2616 __isl_take isl_basic_set *bset)
2618 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2619 (struct isl_basic_map *)bset);
2622 void *isl_set_free(__isl_take isl_set *set)
2624 int i;
2626 if (!set)
2627 return NULL;
2629 if (--set->ref > 0)
2630 return NULL;
2632 isl_ctx_deref(set->ctx);
2633 for (i = 0; i < set->n; ++i)
2634 isl_basic_set_free(set->p[i]);
2635 isl_space_free(set->dim);
2636 free(set);
2638 return NULL;
2641 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2643 int i;
2645 if (!set) {
2646 fprintf(out, "null set\n");
2647 return;
2650 fprintf(out, "%*s", indent, "");
2651 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2652 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2653 set->flags);
2654 for (i = 0; i < set->n; ++i) {
2655 fprintf(out, "%*s", indent, "");
2656 fprintf(out, "basic set %d:\n", i);
2657 isl_basic_set_print_internal(set->p[i], out, indent+4);
2661 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2663 int i;
2665 if (!map) {
2666 fprintf(out, "null map\n");
2667 return;
2670 fprintf(out, "%*s", indent, "");
2671 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2672 "flags: %x, n_name: %d\n",
2673 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2674 map->dim->n_out, map->flags, map->dim->n_id);
2675 for (i = 0; i < map->n; ++i) {
2676 fprintf(out, "%*s", indent, "");
2677 fprintf(out, "basic map %d:\n", i);
2678 isl_basic_map_print_internal(map->p[i], out, indent+4);
2682 struct isl_basic_map *isl_basic_map_intersect_domain(
2683 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2685 struct isl_basic_map *bmap_domain;
2687 if (!bmap || !bset)
2688 goto error;
2690 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2691 bset->dim, isl_dim_param), goto error);
2693 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2694 isl_assert(bset->ctx,
2695 isl_basic_map_compatible_domain(bmap, bset), goto error);
2697 bmap = isl_basic_map_cow(bmap);
2698 if (!bmap)
2699 goto error;
2700 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2701 bset->n_div, bset->n_eq, bset->n_ineq);
2702 bmap_domain = isl_basic_map_from_domain(bset);
2703 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2705 bmap = isl_basic_map_simplify(bmap);
2706 return isl_basic_map_finalize(bmap);
2707 error:
2708 isl_basic_map_free(bmap);
2709 isl_basic_set_free(bset);
2710 return NULL;
2713 struct isl_basic_map *isl_basic_map_intersect_range(
2714 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2716 struct isl_basic_map *bmap_range;
2718 if (!bmap || !bset)
2719 goto error;
2721 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2722 bset->dim, isl_dim_param), goto error);
2724 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2725 isl_assert(bset->ctx,
2726 isl_basic_map_compatible_range(bmap, bset), goto error);
2728 if (isl_basic_set_is_universe(bset)) {
2729 isl_basic_set_free(bset);
2730 return bmap;
2733 bmap = isl_basic_map_cow(bmap);
2734 if (!bmap)
2735 goto error;
2736 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2737 bset->n_div, bset->n_eq, bset->n_ineq);
2738 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2739 bmap = add_constraints(bmap, bmap_range, 0, 0);
2741 bmap = isl_basic_map_simplify(bmap);
2742 return isl_basic_map_finalize(bmap);
2743 error:
2744 isl_basic_map_free(bmap);
2745 isl_basic_set_free(bset);
2746 return NULL;
2749 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2751 int i;
2752 unsigned total;
2753 isl_int s;
2755 if (!bmap || !vec)
2756 return -1;
2758 total = 1 + isl_basic_map_total_dim(bmap);
2759 if (total != vec->size)
2760 return -1;
2762 isl_int_init(s);
2764 for (i = 0; i < bmap->n_eq; ++i) {
2765 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2766 if (!isl_int_is_zero(s)) {
2767 isl_int_clear(s);
2768 return 0;
2772 for (i = 0; i < bmap->n_ineq; ++i) {
2773 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2774 if (isl_int_is_neg(s)) {
2775 isl_int_clear(s);
2776 return 0;
2780 isl_int_clear(s);
2782 return 1;
2785 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2787 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2790 struct isl_basic_map *isl_basic_map_intersect(
2791 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2793 struct isl_vec *sample = NULL;
2795 if (!bmap1 || !bmap2)
2796 goto error;
2798 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2799 bmap2->dim, isl_dim_param), goto error);
2800 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2801 isl_space_dim(bmap1->dim, isl_dim_param) &&
2802 isl_space_dim(bmap2->dim, isl_dim_all) !=
2803 isl_space_dim(bmap2->dim, isl_dim_param))
2804 return isl_basic_map_intersect(bmap2, bmap1);
2806 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2807 isl_space_dim(bmap2->dim, isl_dim_param))
2808 isl_assert(bmap1->ctx,
2809 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2811 if (bmap1->sample &&
2812 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2813 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2814 sample = isl_vec_copy(bmap1->sample);
2815 else if (bmap2->sample &&
2816 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2817 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2818 sample = isl_vec_copy(bmap2->sample);
2820 bmap1 = isl_basic_map_cow(bmap1);
2821 if (!bmap1)
2822 goto error;
2823 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2824 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2825 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2827 if (!bmap1)
2828 isl_vec_free(sample);
2829 else if (sample) {
2830 isl_vec_free(bmap1->sample);
2831 bmap1->sample = sample;
2834 bmap1 = isl_basic_map_simplify(bmap1);
2835 return isl_basic_map_finalize(bmap1);
2836 error:
2837 if (sample)
2838 isl_vec_free(sample);
2839 isl_basic_map_free(bmap1);
2840 isl_basic_map_free(bmap2);
2841 return NULL;
2844 struct isl_basic_set *isl_basic_set_intersect(
2845 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2847 return (struct isl_basic_set *)
2848 isl_basic_map_intersect(
2849 (struct isl_basic_map *)bset1,
2850 (struct isl_basic_map *)bset2);
2853 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2854 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2856 return isl_basic_set_intersect(bset1, bset2);
2859 /* Special case of isl_map_intersect, where both map1 and map2
2860 * are convex, without any divs and such that either map1 or map2
2861 * contains a single constraint. This constraint is then simply
2862 * added to the other map.
2864 static __isl_give isl_map *map_intersect_add_constraint(
2865 __isl_take isl_map *map1, __isl_take isl_map *map2)
2867 isl_assert(map1->ctx, map1->n == 1, goto error);
2868 isl_assert(map2->ctx, map1->n == 1, goto error);
2869 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2870 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2872 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2873 return isl_map_intersect(map2, map1);
2875 isl_assert(map2->ctx,
2876 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2878 map1 = isl_map_cow(map1);
2879 if (!map1)
2880 goto error;
2881 if (isl_map_plain_is_empty(map1)) {
2882 isl_map_free(map2);
2883 return map1;
2885 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2886 if (map2->p[0]->n_eq == 1)
2887 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2888 else
2889 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2890 map2->p[0]->ineq[0]);
2892 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2893 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2894 if (!map1->p[0])
2895 goto error;
2897 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2898 isl_basic_map_free(map1->p[0]);
2899 map1->n = 0;
2902 isl_map_free(map2);
2904 return map1;
2905 error:
2906 isl_map_free(map1);
2907 isl_map_free(map2);
2908 return NULL;
2911 /* map2 may be either a parameter domain or a map living in the same
2912 * space as map1.
2914 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2915 __isl_take isl_map *map2)
2917 unsigned flags = 0;
2918 isl_map *result;
2919 int i, j;
2921 if (!map1 || !map2)
2922 goto error;
2924 if ((isl_map_plain_is_empty(map1) ||
2925 isl_map_plain_is_universe(map2)) &&
2926 isl_space_is_equal(map1->dim, map2->dim)) {
2927 isl_map_free(map2);
2928 return map1;
2930 if ((isl_map_plain_is_empty(map2) ||
2931 isl_map_plain_is_universe(map1)) &&
2932 isl_space_is_equal(map1->dim, map2->dim)) {
2933 isl_map_free(map1);
2934 return map2;
2937 if (map1->n == 1 && map2->n == 1 &&
2938 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2939 isl_space_is_equal(map1->dim, map2->dim) &&
2940 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2941 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2942 return map_intersect_add_constraint(map1, map2);
2944 if (isl_space_dim(map2->dim, isl_dim_all) !=
2945 isl_space_dim(map2->dim, isl_dim_param))
2946 isl_assert(map1->ctx,
2947 isl_space_is_equal(map1->dim, map2->dim), goto error);
2949 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2950 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2951 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2953 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2954 map1->n * map2->n, flags);
2955 if (!result)
2956 goto error;
2957 for (i = 0; i < map1->n; ++i)
2958 for (j = 0; j < map2->n; ++j) {
2959 struct isl_basic_map *part;
2960 part = isl_basic_map_intersect(
2961 isl_basic_map_copy(map1->p[i]),
2962 isl_basic_map_copy(map2->p[j]));
2963 if (isl_basic_map_is_empty(part) < 0)
2964 part = isl_basic_map_free(part);
2965 result = isl_map_add_basic_map(result, part);
2966 if (!result)
2967 goto error;
2969 isl_map_free(map1);
2970 isl_map_free(map2);
2971 return result;
2972 error:
2973 isl_map_free(map1);
2974 isl_map_free(map2);
2975 return NULL;
2978 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2979 __isl_take isl_map *map2)
2981 if (!map1 || !map2)
2982 goto error;
2983 if (!isl_space_is_equal(map1->dim, map2->dim))
2984 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2985 "spaces don't match", goto error);
2986 return map_intersect_internal(map1, map2);
2987 error:
2988 isl_map_free(map1);
2989 isl_map_free(map2);
2990 return NULL;
2993 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2994 __isl_take isl_map *map2)
2996 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2999 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3001 return (struct isl_set *)
3002 isl_map_intersect((struct isl_map *)set1,
3003 (struct isl_map *)set2);
3006 /* map_intersect_internal accepts intersections
3007 * with parameter domains, so we can just call that function.
3009 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3010 __isl_take isl_set *params)
3012 return map_intersect_internal(map, params);
3015 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3016 __isl_take isl_map *map2)
3018 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3021 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3022 __isl_take isl_set *params)
3024 return isl_map_intersect_params(set, params);
3027 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3029 isl_space *dim;
3030 struct isl_basic_set *bset;
3031 unsigned in;
3033 if (!bmap)
3034 return NULL;
3035 bmap = isl_basic_map_cow(bmap);
3036 if (!bmap)
3037 return NULL;
3038 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3039 in = isl_basic_map_n_in(bmap);
3040 bset = isl_basic_set_from_basic_map(bmap);
3041 bset = isl_basic_set_swap_vars(bset, in);
3042 return isl_basic_map_from_basic_set(bset, dim);
3045 static __isl_give isl_basic_map *basic_map_space_reset(
3046 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3048 isl_space *space;
3050 if (!bmap)
3051 return NULL;
3052 if (!isl_space_is_named_or_nested(bmap->dim, type))
3053 return bmap;
3055 space = isl_basic_map_get_space(bmap);
3056 space = isl_space_reset(space, type);
3057 bmap = isl_basic_map_reset_space(bmap, space);
3058 return bmap;
3061 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3062 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3063 unsigned pos, unsigned n)
3065 isl_space *res_dim;
3066 struct isl_basic_map *res;
3067 struct isl_dim_map *dim_map;
3068 unsigned total, off;
3069 enum isl_dim_type t;
3071 if (n == 0)
3072 return basic_map_space_reset(bmap, type);
3074 if (!bmap)
3075 return NULL;
3077 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3079 total = isl_basic_map_total_dim(bmap) + n;
3080 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3081 off = 0;
3082 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3083 if (t != type) {
3084 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3085 } else {
3086 unsigned size = isl_basic_map_dim(bmap, t);
3087 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3088 0, pos, off);
3089 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3090 pos, size - pos, off + pos + n);
3092 off += isl_space_dim(res_dim, t);
3094 isl_dim_map_div(dim_map, bmap, off);
3096 res = isl_basic_map_alloc_space(res_dim,
3097 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3098 if (isl_basic_map_is_rational(bmap))
3099 res = isl_basic_map_set_rational(res);
3100 if (isl_basic_map_plain_is_empty(bmap)) {
3101 isl_basic_map_free(bmap);
3102 free(dim_map);
3103 return isl_basic_map_set_to_empty(res);
3105 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3106 return isl_basic_map_finalize(res);
3109 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3110 __isl_take isl_basic_set *bset,
3111 enum isl_dim_type type, unsigned pos, unsigned n)
3113 return isl_basic_map_insert_dims(bset, type, pos, n);
3116 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3117 enum isl_dim_type type, unsigned n)
3119 if (!bmap)
3120 return NULL;
3121 return isl_basic_map_insert_dims(bmap, type,
3122 isl_basic_map_dim(bmap, type), n);
3125 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3126 enum isl_dim_type type, unsigned n)
3128 if (!bset)
3129 return NULL;
3130 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3131 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3132 error:
3133 isl_basic_set_free(bset);
3134 return NULL;
3137 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3138 enum isl_dim_type type)
3140 isl_space *space;
3142 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3143 return map;
3145 space = isl_map_get_space(map);
3146 space = isl_space_reset(space, type);
3147 map = isl_map_reset_space(map, space);
3148 return map;
3151 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3152 enum isl_dim_type type, unsigned pos, unsigned n)
3154 int i;
3156 if (n == 0)
3157 return map_space_reset(map, type);
3159 map = isl_map_cow(map);
3160 if (!map)
3161 return NULL;
3163 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3164 if (!map->dim)
3165 goto error;
3167 for (i = 0; i < map->n; ++i) {
3168 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3169 if (!map->p[i])
3170 goto error;
3173 return map;
3174 error:
3175 isl_map_free(map);
3176 return NULL;
3179 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3180 enum isl_dim_type type, unsigned pos, unsigned n)
3182 return isl_map_insert_dims(set, type, pos, n);
3185 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3186 enum isl_dim_type type, unsigned n)
3188 if (!map)
3189 return NULL;
3190 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3193 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3194 enum isl_dim_type type, unsigned n)
3196 if (!set)
3197 return NULL;
3198 isl_assert(set->ctx, type != isl_dim_in, goto error);
3199 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3200 error:
3201 isl_set_free(set);
3202 return NULL;
3205 __isl_give isl_basic_map *isl_basic_map_move_dims(
3206 __isl_take isl_basic_map *bmap,
3207 enum isl_dim_type dst_type, unsigned dst_pos,
3208 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3210 struct isl_dim_map *dim_map;
3211 struct isl_basic_map *res;
3212 enum isl_dim_type t;
3213 unsigned total, off;
3215 if (!bmap)
3216 return NULL;
3217 if (n == 0)
3218 return bmap;
3220 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3221 goto error);
3223 if (dst_type == src_type && dst_pos == src_pos)
3224 return bmap;
3226 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3228 if (pos(bmap->dim, dst_type) + dst_pos ==
3229 pos(bmap->dim, src_type) + src_pos +
3230 ((src_type < dst_type) ? n : 0)) {
3231 bmap = isl_basic_map_cow(bmap);
3232 if (!bmap)
3233 return NULL;
3235 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3236 src_type, src_pos, n);
3237 if (!bmap->dim)
3238 goto error;
3240 bmap = isl_basic_map_finalize(bmap);
3242 return bmap;
3245 total = isl_basic_map_total_dim(bmap);
3246 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3248 off = 0;
3249 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3250 unsigned size = isl_space_dim(bmap->dim, t);
3251 if (t == dst_type) {
3252 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3253 0, dst_pos, off);
3254 off += dst_pos;
3255 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3256 src_pos, n, off);
3257 off += n;
3258 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3259 dst_pos, size - dst_pos, off);
3260 off += size - dst_pos;
3261 } else if (t == src_type) {
3262 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3263 0, src_pos, off);
3264 off += src_pos;
3265 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3266 src_pos + n, size - src_pos - n, off);
3267 off += size - src_pos - n;
3268 } else {
3269 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3270 off += size;
3273 isl_dim_map_div(dim_map, bmap, off);
3275 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3276 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3277 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3278 if (!bmap)
3279 goto error;
3281 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3282 src_type, src_pos, n);
3283 if (!bmap->dim)
3284 goto error;
3286 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3287 bmap = isl_basic_map_gauss(bmap, NULL);
3288 bmap = isl_basic_map_finalize(bmap);
3290 return bmap;
3291 error:
3292 isl_basic_map_free(bmap);
3293 return NULL;
3296 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3297 enum isl_dim_type dst_type, unsigned dst_pos,
3298 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3300 return (isl_basic_set *)isl_basic_map_move_dims(
3301 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3304 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3305 enum isl_dim_type dst_type, unsigned dst_pos,
3306 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3308 if (!set)
3309 return NULL;
3310 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3311 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3312 src_type, src_pos, n);
3313 error:
3314 isl_set_free(set);
3315 return NULL;
3318 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3319 enum isl_dim_type dst_type, unsigned dst_pos,
3320 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3322 int i;
3324 if (!map)
3325 return NULL;
3326 if (n == 0)
3327 return map;
3329 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3330 goto error);
3332 if (dst_type == src_type && dst_pos == src_pos)
3333 return map;
3335 isl_assert(map->ctx, dst_type != src_type, goto error);
3337 map = isl_map_cow(map);
3338 if (!map)
3339 return NULL;
3341 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3342 if (!map->dim)
3343 goto error;
3345 for (i = 0; i < map->n; ++i) {
3346 map->p[i] = isl_basic_map_move_dims(map->p[i],
3347 dst_type, dst_pos,
3348 src_type, src_pos, n);
3349 if (!map->p[i])
3350 goto error;
3353 return map;
3354 error:
3355 isl_map_free(map);
3356 return NULL;
3359 /* Move the specified dimensions to the last columns right before
3360 * the divs. Don't change the dimension specification of bmap.
3361 * That's the responsibility of the caller.
3363 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3364 enum isl_dim_type type, unsigned first, unsigned n)
3366 struct isl_dim_map *dim_map;
3367 struct isl_basic_map *res;
3368 enum isl_dim_type t;
3369 unsigned total, off;
3371 if (!bmap)
3372 return NULL;
3373 if (pos(bmap->dim, type) + first + n ==
3374 1 + isl_space_dim(bmap->dim, isl_dim_all))
3375 return bmap;
3377 total = isl_basic_map_total_dim(bmap);
3378 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3380 off = 0;
3381 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3382 unsigned size = isl_space_dim(bmap->dim, t);
3383 if (t == type) {
3384 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3385 0, first, off);
3386 off += first;
3387 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3388 first, n, total - bmap->n_div - n);
3389 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3390 first + n, size - (first + n), off);
3391 off += size - (first + n);
3392 } else {
3393 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3394 off += size;
3397 isl_dim_map_div(dim_map, bmap, off + n);
3399 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3400 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3401 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3402 return res;
3405 /* Insert "n" rows in the divs of "bmap".
3407 * The number of columns is not changed, which means that the last
3408 * dimensions of "bmap" are being reintepreted as the new divs.
3409 * The space of "bmap" is not adjusted, however, which means
3410 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3411 * from the space of "bmap" is the responsibility of the caller.
3413 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3414 int n)
3416 int i;
3417 size_t row_size;
3418 isl_int **new_div;
3419 isl_int *old;
3421 bmap = isl_basic_map_cow(bmap);
3422 if (!bmap)
3423 return NULL;
3425 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3426 old = bmap->block2.data;
3427 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3428 (bmap->extra + n) * (1 + row_size));
3429 if (!bmap->block2.data)
3430 return isl_basic_map_free(bmap);
3431 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3432 if (!new_div)
3433 return isl_basic_map_free(bmap);
3434 for (i = 0; i < n; ++i) {
3435 new_div[i] = bmap->block2.data +
3436 (bmap->extra + i) * (1 + row_size);
3437 isl_seq_clr(new_div[i], 1 + row_size);
3439 for (i = 0; i < bmap->extra; ++i)
3440 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3441 free(bmap->div);
3442 bmap->div = new_div;
3443 bmap->n_div += n;
3444 bmap->extra += n;
3446 return bmap;
3449 /* Turn the n dimensions of type type, starting at first
3450 * into existentially quantified variables.
3452 __isl_give isl_basic_map *isl_basic_map_project_out(
3453 __isl_take isl_basic_map *bmap,
3454 enum isl_dim_type type, unsigned first, unsigned n)
3456 if (n == 0)
3457 return basic_map_space_reset(bmap, type);
3459 if (!bmap)
3460 return NULL;
3462 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3463 return isl_basic_map_remove_dims(bmap, type, first, n);
3465 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3466 goto error);
3468 bmap = move_last(bmap, type, first, n);
3469 bmap = isl_basic_map_cow(bmap);
3470 bmap = insert_div_rows(bmap, n);
3471 if (!bmap)
3472 return NULL;
3474 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3475 if (!bmap->dim)
3476 goto error;
3477 bmap = isl_basic_map_simplify(bmap);
3478 bmap = isl_basic_map_drop_redundant_divs(bmap);
3479 return isl_basic_map_finalize(bmap);
3480 error:
3481 isl_basic_map_free(bmap);
3482 return NULL;
3485 /* Turn the n dimensions of type type, starting at first
3486 * into existentially quantified variables.
3488 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3489 enum isl_dim_type type, unsigned first, unsigned n)
3491 return (isl_basic_set *)isl_basic_map_project_out(
3492 (isl_basic_map *)bset, type, first, n);
3495 /* Turn the n dimensions of type type, starting at first
3496 * into existentially quantified variables.
3498 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3499 enum isl_dim_type type, unsigned first, unsigned n)
3501 int i;
3503 if (!map)
3504 return NULL;
3506 if (n == 0)
3507 return map_space_reset(map, type);
3509 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3511 map = isl_map_cow(map);
3512 if (!map)
3513 return NULL;
3515 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3516 if (!map->dim)
3517 goto error;
3519 for (i = 0; i < map->n; ++i) {
3520 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3521 if (!map->p[i])
3522 goto error;
3525 return map;
3526 error:
3527 isl_map_free(map);
3528 return NULL;
3531 /* Turn the n dimensions of type type, starting at first
3532 * into existentially quantified variables.
3534 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3535 enum isl_dim_type type, unsigned first, unsigned n)
3537 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3540 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3542 int i, j;
3544 for (i = 0; i < n; ++i) {
3545 j = isl_basic_map_alloc_div(bmap);
3546 if (j < 0)
3547 goto error;
3548 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3550 return bmap;
3551 error:
3552 isl_basic_map_free(bmap);
3553 return NULL;
3556 struct isl_basic_map *isl_basic_map_apply_range(
3557 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3559 isl_space *dim_result = NULL;
3560 struct isl_basic_map *bmap;
3561 unsigned n_in, n_out, n, nparam, total, pos;
3562 struct isl_dim_map *dim_map1, *dim_map2;
3564 if (!bmap1 || !bmap2)
3565 goto error;
3566 if (!isl_space_match(bmap1->dim, isl_dim_param,
3567 bmap2->dim, isl_dim_param))
3568 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3569 "parameters don't match", goto error);
3570 if (!isl_space_tuple_match(bmap1->dim, isl_dim_out,
3571 bmap2->dim, isl_dim_in))
3572 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3573 "spaces don't match", goto error);
3575 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3576 isl_space_copy(bmap2->dim));
3578 n_in = isl_basic_map_n_in(bmap1);
3579 n_out = isl_basic_map_n_out(bmap2);
3580 n = isl_basic_map_n_out(bmap1);
3581 nparam = isl_basic_map_n_param(bmap1);
3583 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3584 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3585 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3586 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3587 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3588 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3589 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3590 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3591 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3592 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3593 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3595 bmap = isl_basic_map_alloc_space(dim_result,
3596 bmap1->n_div + bmap2->n_div + n,
3597 bmap1->n_eq + bmap2->n_eq,
3598 bmap1->n_ineq + bmap2->n_ineq);
3599 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3600 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3601 bmap = add_divs(bmap, n);
3602 bmap = isl_basic_map_simplify(bmap);
3603 bmap = isl_basic_map_drop_redundant_divs(bmap);
3604 return isl_basic_map_finalize(bmap);
3605 error:
3606 isl_basic_map_free(bmap1);
3607 isl_basic_map_free(bmap2);
3608 return NULL;
3611 struct isl_basic_set *isl_basic_set_apply(
3612 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3614 if (!bset || !bmap)
3615 goto error;
3617 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3618 goto error);
3620 return (struct isl_basic_set *)
3621 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3622 error:
3623 isl_basic_set_free(bset);
3624 isl_basic_map_free(bmap);
3625 return NULL;
3628 struct isl_basic_map *isl_basic_map_apply_domain(
3629 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3631 if (!bmap1 || !bmap2)
3632 goto error;
3634 isl_assert(bmap1->ctx,
3635 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3636 isl_assert(bmap1->ctx,
3637 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3638 goto error);
3640 bmap1 = isl_basic_map_reverse(bmap1);
3641 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3642 return isl_basic_map_reverse(bmap1);
3643 error:
3644 isl_basic_map_free(bmap1);
3645 isl_basic_map_free(bmap2);
3646 return NULL;
3649 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3650 * A \cap B -> f(A) + f(B)
3652 struct isl_basic_map *isl_basic_map_sum(
3653 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3655 unsigned n_in, n_out, nparam, total, pos;
3656 struct isl_basic_map *bmap = NULL;
3657 struct isl_dim_map *dim_map1, *dim_map2;
3658 int i;
3660 if (!bmap1 || !bmap2)
3661 goto error;
3663 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3664 goto error);
3666 nparam = isl_basic_map_n_param(bmap1);
3667 n_in = isl_basic_map_n_in(bmap1);
3668 n_out = isl_basic_map_n_out(bmap1);
3670 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3671 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3672 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3673 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3674 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3675 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3676 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3677 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3678 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3679 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3680 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3682 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3683 bmap1->n_div + bmap2->n_div + 2 * n_out,
3684 bmap1->n_eq + bmap2->n_eq + n_out,
3685 bmap1->n_ineq + bmap2->n_ineq);
3686 for (i = 0; i < n_out; ++i) {
3687 int j = isl_basic_map_alloc_equality(bmap);
3688 if (j < 0)
3689 goto error;
3690 isl_seq_clr(bmap->eq[j], 1+total);
3691 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3692 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3693 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3695 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3696 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3697 bmap = add_divs(bmap, 2 * n_out);
3699 bmap = isl_basic_map_simplify(bmap);
3700 return isl_basic_map_finalize(bmap);
3701 error:
3702 isl_basic_map_free(bmap);
3703 isl_basic_map_free(bmap1);
3704 isl_basic_map_free(bmap2);
3705 return NULL;
3708 /* Given two maps A -> f(A) and B -> g(B), construct a map
3709 * A \cap B -> f(A) + f(B)
3711 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3713 struct isl_map *result;
3714 int i, j;
3716 if (!map1 || !map2)
3717 goto error;
3719 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3721 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3722 map1->n * map2->n, 0);
3723 if (!result)
3724 goto error;
3725 for (i = 0; i < map1->n; ++i)
3726 for (j = 0; j < map2->n; ++j) {
3727 struct isl_basic_map *part;
3728 part = isl_basic_map_sum(
3729 isl_basic_map_copy(map1->p[i]),
3730 isl_basic_map_copy(map2->p[j]));
3731 if (isl_basic_map_is_empty(part))
3732 isl_basic_map_free(part);
3733 else
3734 result = isl_map_add_basic_map(result, part);
3735 if (!result)
3736 goto error;
3738 isl_map_free(map1);
3739 isl_map_free(map2);
3740 return result;
3741 error:
3742 isl_map_free(map1);
3743 isl_map_free(map2);
3744 return NULL;
3747 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3748 __isl_take isl_set *set2)
3750 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3753 /* Given a basic map A -> f(A), construct A -> -f(A).
3755 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3757 int i, j;
3758 unsigned off, n;
3760 bmap = isl_basic_map_cow(bmap);
3761 if (!bmap)
3762 return NULL;
3764 n = isl_basic_map_dim(bmap, isl_dim_out);
3765 off = isl_basic_map_offset(bmap, isl_dim_out);
3766 for (i = 0; i < bmap->n_eq; ++i)
3767 for (j = 0; j < n; ++j)
3768 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3769 for (i = 0; i < bmap->n_ineq; ++i)
3770 for (j = 0; j < n; ++j)
3771 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3772 for (i = 0; i < bmap->n_div; ++i)
3773 for (j = 0; j < n; ++j)
3774 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3775 bmap = isl_basic_map_gauss(bmap, NULL);
3776 return isl_basic_map_finalize(bmap);
3779 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3781 return isl_basic_map_neg(bset);
3784 /* Given a map A -> f(A), construct A -> -f(A).
3786 struct isl_map *isl_map_neg(struct isl_map *map)
3788 int i;
3790 map = isl_map_cow(map);
3791 if (!map)
3792 return NULL;
3794 for (i = 0; i < map->n; ++i) {
3795 map->p[i] = isl_basic_map_neg(map->p[i]);
3796 if (!map->p[i])
3797 goto error;
3800 return map;
3801 error:
3802 isl_map_free(map);
3803 return NULL;
3806 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3808 return (isl_set *)isl_map_neg((isl_map *)set);
3811 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3812 * A -> floor(f(A)/d).
3814 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3815 isl_int d)
3817 unsigned n_in, n_out, nparam, total, pos;
3818 struct isl_basic_map *result = NULL;
3819 struct isl_dim_map *dim_map;
3820 int i;
3822 if (!bmap)
3823 return NULL;
3825 nparam = isl_basic_map_n_param(bmap);
3826 n_in = isl_basic_map_n_in(bmap);
3827 n_out = isl_basic_map_n_out(bmap);
3829 total = nparam + n_in + n_out + bmap->n_div + n_out;
3830 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3831 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3832 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3833 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3834 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3836 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3837 bmap->n_div + n_out,
3838 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3839 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3840 result = add_divs(result, n_out);
3841 for (i = 0; i < n_out; ++i) {
3842 int j;
3843 j = isl_basic_map_alloc_inequality(result);
3844 if (j < 0)
3845 goto error;
3846 isl_seq_clr(result->ineq[j], 1+total);
3847 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3848 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3849 j = isl_basic_map_alloc_inequality(result);
3850 if (j < 0)
3851 goto error;
3852 isl_seq_clr(result->ineq[j], 1+total);
3853 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3854 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3855 isl_int_sub_ui(result->ineq[j][0], d, 1);
3858 result = isl_basic_map_simplify(result);
3859 return isl_basic_map_finalize(result);
3860 error:
3861 isl_basic_map_free(result);
3862 return NULL;
3865 /* Given a map A -> f(A) and an integer d, construct a map
3866 * A -> floor(f(A)/d).
3868 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3870 int i;
3872 map = isl_map_cow(map);
3873 if (!map)
3874 return NULL;
3876 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3877 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3878 for (i = 0; i < map->n; ++i) {
3879 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3880 if (!map->p[i])
3881 goto error;
3884 return map;
3885 error:
3886 isl_map_free(map);
3887 return NULL;
3890 /* Given a map A -> f(A) and an integer d, construct a map
3891 * A -> floor(f(A)/d).
3893 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3894 __isl_take isl_val *d)
3896 if (!map || !d)
3897 goto error;
3898 if (!isl_val_is_int(d))
3899 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3900 "expecting integer denominator", goto error);
3901 map = isl_map_floordiv(map, d->n);
3902 isl_val_free(d);
3903 return map;
3904 error:
3905 isl_map_free(map);
3906 isl_val_free(d);
3907 return NULL;
3910 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3912 int i;
3913 unsigned nparam;
3914 unsigned n_in;
3916 i = isl_basic_map_alloc_equality(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->eq[i], 1 + isl_basic_map_total_dim(bmap));
3922 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3923 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3924 return isl_basic_map_finalize(bmap);
3925 error:
3926 isl_basic_map_free(bmap);
3927 return NULL;
3930 /* Add a constraints to "bmap" expressing i_pos < o_pos
3932 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3934 int i;
3935 unsigned nparam;
3936 unsigned n_in;
3938 i = isl_basic_map_alloc_inequality(bmap);
3939 if (i < 0)
3940 goto error;
3941 nparam = isl_basic_map_n_param(bmap);
3942 n_in = isl_basic_map_n_in(bmap);
3943 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3944 isl_int_set_si(bmap->ineq[i][0], -1);
3945 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3946 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3947 return isl_basic_map_finalize(bmap);
3948 error:
3949 isl_basic_map_free(bmap);
3950 return NULL;
3953 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3955 static __isl_give isl_basic_map *var_less_or_equal(
3956 __isl_take 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][1+nparam+pos], -1);
3969 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3970 return isl_basic_map_finalize(bmap);
3971 error:
3972 isl_basic_map_free(bmap);
3973 return NULL;
3976 /* Add a constraints to "bmap" expressing i_pos > o_pos
3978 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3980 int i;
3981 unsigned nparam;
3982 unsigned n_in;
3984 i = isl_basic_map_alloc_inequality(bmap);
3985 if (i < 0)
3986 goto error;
3987 nparam = isl_basic_map_n_param(bmap);
3988 n_in = isl_basic_map_n_in(bmap);
3989 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3990 isl_int_set_si(bmap->ineq[i][0], -1);
3991 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3992 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3993 return isl_basic_map_finalize(bmap);
3994 error:
3995 isl_basic_map_free(bmap);
3996 return NULL;
3999 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4001 static __isl_give isl_basic_map *var_more_or_equal(
4002 __isl_take isl_basic_map *bmap, unsigned pos)
4004 int i;
4005 unsigned nparam;
4006 unsigned n_in;
4008 i = isl_basic_map_alloc_inequality(bmap);
4009 if (i < 0)
4010 goto error;
4011 nparam = isl_basic_map_n_param(bmap);
4012 n_in = isl_basic_map_n_in(bmap);
4013 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4014 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4015 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4016 return isl_basic_map_finalize(bmap);
4017 error:
4018 isl_basic_map_free(bmap);
4019 return NULL;
4022 __isl_give isl_basic_map *isl_basic_map_equal(
4023 __isl_take isl_space *dim, unsigned n_equal)
4025 int i;
4026 struct isl_basic_map *bmap;
4027 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4028 if (!bmap)
4029 return NULL;
4030 for (i = 0; i < n_equal && bmap; ++i)
4031 bmap = var_equal(bmap, i);
4032 return isl_basic_map_finalize(bmap);
4035 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4037 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4038 unsigned pos)
4040 int i;
4041 struct isl_basic_map *bmap;
4042 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4043 if (!bmap)
4044 return NULL;
4045 for (i = 0; i < pos && bmap; ++i)
4046 bmap = var_equal(bmap, i);
4047 if (bmap)
4048 bmap = var_less(bmap, pos);
4049 return isl_basic_map_finalize(bmap);
4052 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4054 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4055 __isl_take isl_space *dim, unsigned pos)
4057 int i;
4058 isl_basic_map *bmap;
4060 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4061 for (i = 0; i < pos; ++i)
4062 bmap = var_equal(bmap, i);
4063 bmap = var_less_or_equal(bmap, pos);
4064 return isl_basic_map_finalize(bmap);
4067 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4069 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4070 unsigned pos)
4072 int i;
4073 struct isl_basic_map *bmap;
4074 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4075 if (!bmap)
4076 return NULL;
4077 for (i = 0; i < pos && bmap; ++i)
4078 bmap = var_equal(bmap, i);
4079 if (bmap)
4080 bmap = var_more(bmap, pos);
4081 return isl_basic_map_finalize(bmap);
4084 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4086 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4087 __isl_take isl_space *dim, unsigned pos)
4089 int i;
4090 isl_basic_map *bmap;
4092 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4093 for (i = 0; i < pos; ++i)
4094 bmap = var_equal(bmap, i);
4095 bmap = var_more_or_equal(bmap, pos);
4096 return isl_basic_map_finalize(bmap);
4099 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4100 unsigned n, int equal)
4102 struct isl_map *map;
4103 int i;
4105 if (n == 0 && equal)
4106 return isl_map_universe(dims);
4108 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4110 for (i = 0; i + 1 < n; ++i)
4111 map = isl_map_add_basic_map(map,
4112 isl_basic_map_less_at(isl_space_copy(dims), i));
4113 if (n > 0) {
4114 if (equal)
4115 map = isl_map_add_basic_map(map,
4116 isl_basic_map_less_or_equal_at(dims, n - 1));
4117 else
4118 map = isl_map_add_basic_map(map,
4119 isl_basic_map_less_at(dims, n - 1));
4120 } else
4121 isl_space_free(dims);
4123 return map;
4126 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4128 if (!dims)
4129 return NULL;
4130 return map_lex_lte_first(dims, dims->n_out, equal);
4133 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4135 return map_lex_lte_first(dim, n, 0);
4138 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4140 return map_lex_lte_first(dim, n, 1);
4143 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4145 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4148 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4150 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4153 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4154 unsigned n, int equal)
4156 struct isl_map *map;
4157 int i;
4159 if (n == 0 && equal)
4160 return isl_map_universe(dims);
4162 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4164 for (i = 0; i + 1 < n; ++i)
4165 map = isl_map_add_basic_map(map,
4166 isl_basic_map_more_at(isl_space_copy(dims), i));
4167 if (n > 0) {
4168 if (equal)
4169 map = isl_map_add_basic_map(map,
4170 isl_basic_map_more_or_equal_at(dims, n - 1));
4171 else
4172 map = isl_map_add_basic_map(map,
4173 isl_basic_map_more_at(dims, n - 1));
4174 } else
4175 isl_space_free(dims);
4177 return map;
4180 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4182 if (!dims)
4183 return NULL;
4184 return map_lex_gte_first(dims, dims->n_out, equal);
4187 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4189 return map_lex_gte_first(dim, n, 0);
4192 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4194 return map_lex_gte_first(dim, n, 1);
4197 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4199 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4202 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4204 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4207 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4208 __isl_take isl_set *set2)
4210 isl_map *map;
4211 map = isl_map_lex_le(isl_set_get_space(set1));
4212 map = isl_map_intersect_domain(map, set1);
4213 map = isl_map_intersect_range(map, set2);
4214 return map;
4217 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4218 __isl_take isl_set *set2)
4220 isl_map *map;
4221 map = isl_map_lex_lt(isl_set_get_space(set1));
4222 map = isl_map_intersect_domain(map, set1);
4223 map = isl_map_intersect_range(map, set2);
4224 return map;
4227 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4228 __isl_take isl_set *set2)
4230 isl_map *map;
4231 map = isl_map_lex_ge(isl_set_get_space(set1));
4232 map = isl_map_intersect_domain(map, set1);
4233 map = isl_map_intersect_range(map, set2);
4234 return map;
4237 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4238 __isl_take isl_set *set2)
4240 isl_map *map;
4241 map = isl_map_lex_gt(isl_set_get_space(set1));
4242 map = isl_map_intersect_domain(map, set1);
4243 map = isl_map_intersect_range(map, set2);
4244 return map;
4247 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4248 __isl_take isl_map *map2)
4250 isl_map *map;
4251 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4252 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4253 map = isl_map_apply_range(map, isl_map_reverse(map2));
4254 return map;
4257 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4258 __isl_take isl_map *map2)
4260 isl_map *map;
4261 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4262 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4263 map = isl_map_apply_range(map, isl_map_reverse(map2));
4264 return map;
4267 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4268 __isl_take isl_map *map2)
4270 isl_map *map;
4271 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4272 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4273 map = isl_map_apply_range(map, isl_map_reverse(map2));
4274 return map;
4277 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4278 __isl_take isl_map *map2)
4280 isl_map *map;
4281 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4282 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4283 map = isl_map_apply_range(map, isl_map_reverse(map2));
4284 return map;
4287 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4288 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4290 struct isl_basic_map *bmap;
4292 bset = isl_basic_set_cow(bset);
4293 if (!bset || !dim)
4294 goto error;
4296 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4297 isl_space_free(bset->dim);
4298 bmap = (struct isl_basic_map *) bset;
4299 bmap->dim = dim;
4300 return isl_basic_map_finalize(bmap);
4301 error:
4302 isl_basic_set_free(bset);
4303 isl_space_free(dim);
4304 return NULL;
4307 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4309 if (!bmap)
4310 goto error;
4311 if (bmap->dim->n_in == 0)
4312 return (struct isl_basic_set *)bmap;
4313 bmap = isl_basic_map_cow(bmap);
4314 if (!bmap)
4315 goto error;
4316 bmap->dim = isl_space_as_set_space(bmap->dim);
4317 if (!bmap->dim)
4318 goto error;
4319 bmap = isl_basic_map_finalize(bmap);
4320 return (struct isl_basic_set *)bmap;
4321 error:
4322 isl_basic_map_free(bmap);
4323 return NULL;
4326 /* For a div d = floor(f/m), add the constraints
4328 * f - m d >= 0
4329 * -(f-(n-1)) + m d >= 0
4331 * Note that the second constraint is the negation of
4333 * f - m d >= n
4335 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4336 unsigned pos, isl_int *div)
4338 int i, j;
4339 unsigned total = isl_basic_map_total_dim(bmap);
4341 i = isl_basic_map_alloc_inequality(bmap);
4342 if (i < 0)
4343 return -1;
4344 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4345 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4347 j = isl_basic_map_alloc_inequality(bmap);
4348 if (j < 0)
4349 return -1;
4350 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4351 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4352 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4353 return j;
4356 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4357 unsigned pos, isl_int *div)
4359 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4360 pos, div);
4363 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4365 unsigned total = isl_basic_map_total_dim(bmap);
4366 unsigned div_pos = total - bmap->n_div + div;
4368 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4369 bmap->div[div]);
4372 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4374 return isl_basic_map_add_div_constraints(bset, div);
4377 struct isl_basic_set *isl_basic_map_underlying_set(
4378 struct isl_basic_map *bmap)
4380 if (!bmap)
4381 goto error;
4382 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4383 bmap->n_div == 0 &&
4384 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4385 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4386 return (struct isl_basic_set *)bmap;
4387 bmap = isl_basic_map_cow(bmap);
4388 if (!bmap)
4389 goto error;
4390 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4391 if (!bmap->dim)
4392 goto error;
4393 bmap->extra -= bmap->n_div;
4394 bmap->n_div = 0;
4395 bmap = isl_basic_map_finalize(bmap);
4396 return (struct isl_basic_set *)bmap;
4397 error:
4398 isl_basic_map_free(bmap);
4399 return NULL;
4402 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4403 __isl_take isl_basic_set *bset)
4405 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4408 struct isl_basic_map *isl_basic_map_overlying_set(
4409 struct isl_basic_set *bset, struct isl_basic_map *like)
4411 struct isl_basic_map *bmap;
4412 struct isl_ctx *ctx;
4413 unsigned total;
4414 int i;
4416 if (!bset || !like)
4417 goto error;
4418 ctx = bset->ctx;
4419 isl_assert(ctx, bset->n_div == 0, goto error);
4420 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4421 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4422 goto error);
4423 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4424 isl_basic_map_free(like);
4425 return (struct isl_basic_map *)bset;
4427 bset = isl_basic_set_cow(bset);
4428 if (!bset)
4429 goto error;
4430 total = bset->dim->n_out + bset->extra;
4431 bmap = (struct isl_basic_map *)bset;
4432 isl_space_free(bmap->dim);
4433 bmap->dim = isl_space_copy(like->dim);
4434 if (!bmap->dim)
4435 goto error;
4436 bmap->n_div = like->n_div;
4437 bmap->extra += like->n_div;
4438 if (bmap->extra) {
4439 unsigned ltotal;
4440 isl_int **div;
4441 ltotal = total - bmap->extra + like->extra;
4442 if (ltotal > total)
4443 ltotal = total;
4444 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4445 bmap->extra * (1 + 1 + total));
4446 if (isl_blk_is_error(bmap->block2))
4447 goto error;
4448 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4449 if (!div)
4450 goto error;
4451 bmap->div = div;
4452 for (i = 0; i < bmap->extra; ++i)
4453 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4454 for (i = 0; i < like->n_div; ++i) {
4455 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4456 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4458 bmap = isl_basic_map_extend_constraints(bmap,
4459 0, 2 * like->n_div);
4460 for (i = 0; i < like->n_div; ++i) {
4461 if (!bmap)
4462 break;
4463 if (isl_int_is_zero(bmap->div[i][0]))
4464 continue;
4465 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4466 bmap = isl_basic_map_free(bmap);
4469 isl_basic_map_free(like);
4470 bmap = isl_basic_map_simplify(bmap);
4471 bmap = isl_basic_map_finalize(bmap);
4472 return bmap;
4473 error:
4474 isl_basic_map_free(like);
4475 isl_basic_set_free(bset);
4476 return NULL;
4479 struct isl_basic_set *isl_basic_set_from_underlying_set(
4480 struct isl_basic_set *bset, struct isl_basic_set *like)
4482 return (struct isl_basic_set *)
4483 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4486 struct isl_set *isl_set_from_underlying_set(
4487 struct isl_set *set, struct isl_basic_set *like)
4489 int i;
4491 if (!set || !like)
4492 goto error;
4493 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4494 goto error);
4495 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4496 isl_basic_set_free(like);
4497 return set;
4499 set = isl_set_cow(set);
4500 if (!set)
4501 goto error;
4502 for (i = 0; i < set->n; ++i) {
4503 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4504 isl_basic_set_copy(like));
4505 if (!set->p[i])
4506 goto error;
4508 isl_space_free(set->dim);
4509 set->dim = isl_space_copy(like->dim);
4510 if (!set->dim)
4511 goto error;
4512 isl_basic_set_free(like);
4513 return set;
4514 error:
4515 isl_basic_set_free(like);
4516 isl_set_free(set);
4517 return NULL;
4520 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4522 int i;
4524 map = isl_map_cow(map);
4525 if (!map)
4526 return NULL;
4527 map->dim = isl_space_cow(map->dim);
4528 if (!map->dim)
4529 goto error;
4531 for (i = 1; i < map->n; ++i)
4532 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4533 goto error);
4534 for (i = 0; i < map->n; ++i) {
4535 map->p[i] = (struct isl_basic_map *)
4536 isl_basic_map_underlying_set(map->p[i]);
4537 if (!map->p[i])
4538 goto error;
4540 if (map->n == 0)
4541 map->dim = isl_space_underlying(map->dim, 0);
4542 else {
4543 isl_space_free(map->dim);
4544 map->dim = isl_space_copy(map->p[0]->dim);
4546 if (!map->dim)
4547 goto error;
4548 return (struct isl_set *)map;
4549 error:
4550 isl_map_free(map);
4551 return NULL;
4554 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4556 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4559 __isl_give isl_basic_map *isl_basic_map_reset_space(
4560 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4562 bmap = isl_basic_map_cow(bmap);
4563 if (!bmap || !dim)
4564 goto error;
4566 isl_space_free(bmap->dim);
4567 bmap->dim = dim;
4569 bmap = isl_basic_map_finalize(bmap);
4571 return bmap;
4572 error:
4573 isl_basic_map_free(bmap);
4574 isl_space_free(dim);
4575 return NULL;
4578 __isl_give isl_basic_set *isl_basic_set_reset_space(
4579 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4581 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4582 dim);
4585 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4586 __isl_take isl_space *dim)
4588 int i;
4590 map = isl_map_cow(map);
4591 if (!map || !dim)
4592 goto error;
4594 for (i = 0; i < map->n; ++i) {
4595 map->p[i] = isl_basic_map_reset_space(map->p[i],
4596 isl_space_copy(dim));
4597 if (!map->p[i])
4598 goto error;
4600 isl_space_free(map->dim);
4601 map->dim = dim;
4603 return map;
4604 error:
4605 isl_map_free(map);
4606 isl_space_free(dim);
4607 return NULL;
4610 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4611 __isl_take isl_space *dim)
4613 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4616 /* Compute the parameter domain of the given basic set.
4618 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4620 isl_space *space;
4621 unsigned n;
4623 if (isl_basic_set_is_params(bset))
4624 return bset;
4626 n = isl_basic_set_dim(bset, isl_dim_set);
4627 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4628 space = isl_basic_set_get_space(bset);
4629 space = isl_space_params(space);
4630 bset = isl_basic_set_reset_space(bset, space);
4631 return bset;
4634 /* Construct a zero-dimensional basic set with the given parameter domain.
4636 __isl_give isl_basic_set *isl_basic_set_from_params(
4637 __isl_take isl_basic_set *bset)
4639 isl_space *space;
4640 space = isl_basic_set_get_space(bset);
4641 space = isl_space_set_from_params(space);
4642 bset = isl_basic_set_reset_space(bset, space);
4643 return bset;
4646 /* Compute the parameter domain of the given set.
4648 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4650 isl_space *space;
4651 unsigned n;
4653 if (isl_set_is_params(set))
4654 return set;
4656 n = isl_set_dim(set, isl_dim_set);
4657 set = isl_set_project_out(set, isl_dim_set, 0, n);
4658 space = isl_set_get_space(set);
4659 space = isl_space_params(space);
4660 set = isl_set_reset_space(set, space);
4661 return set;
4664 /* Construct a zero-dimensional set with the given parameter domain.
4666 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4668 isl_space *space;
4669 space = isl_set_get_space(set);
4670 space = isl_space_set_from_params(space);
4671 set = isl_set_reset_space(set, space);
4672 return set;
4675 /* Compute the parameter domain of the given map.
4677 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4679 isl_space *space;
4680 unsigned n;
4682 n = isl_map_dim(map, isl_dim_in);
4683 map = isl_map_project_out(map, isl_dim_in, 0, n);
4684 n = isl_map_dim(map, isl_dim_out);
4685 map = isl_map_project_out(map, isl_dim_out, 0, n);
4686 space = isl_map_get_space(map);
4687 space = isl_space_params(space);
4688 map = isl_map_reset_space(map, space);
4689 return map;
4692 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4694 isl_space *dim;
4695 struct isl_basic_set *domain;
4696 unsigned n_in;
4697 unsigned n_out;
4699 if (!bmap)
4700 return NULL;
4701 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4703 n_in = isl_basic_map_n_in(bmap);
4704 n_out = isl_basic_map_n_out(bmap);
4705 domain = isl_basic_set_from_basic_map(bmap);
4706 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4708 domain = isl_basic_set_reset_space(domain, dim);
4710 return domain;
4713 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4715 if (!bmap)
4716 return -1;
4717 return isl_space_may_be_set(bmap->dim);
4720 /* Is this basic map actually a set?
4721 * Users should never call this function. Outside of isl,
4722 * the type should indicate whether something is a set or a map.
4724 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4726 if (!bmap)
4727 return -1;
4728 return isl_space_is_set(bmap->dim);
4731 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4733 if (!bmap)
4734 return NULL;
4735 if (isl_basic_map_is_set(bmap))
4736 return bmap;
4737 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4740 __isl_give isl_basic_map *isl_basic_map_domain_map(
4741 __isl_take isl_basic_map *bmap)
4743 int i, k;
4744 isl_space *dim;
4745 isl_basic_map *domain;
4746 int nparam, n_in, n_out;
4747 unsigned total;
4749 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4750 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4751 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4753 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4754 domain = isl_basic_map_universe(dim);
4756 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4757 bmap = isl_basic_map_apply_range(bmap, domain);
4758 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4760 total = isl_basic_map_total_dim(bmap);
4762 for (i = 0; i < n_in; ++i) {
4763 k = isl_basic_map_alloc_equality(bmap);
4764 if (k < 0)
4765 goto error;
4766 isl_seq_clr(bmap->eq[k], 1 + total);
4767 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4768 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4771 bmap = isl_basic_map_gauss(bmap, NULL);
4772 return isl_basic_map_finalize(bmap);
4773 error:
4774 isl_basic_map_free(bmap);
4775 return NULL;
4778 __isl_give isl_basic_map *isl_basic_map_range_map(
4779 __isl_take isl_basic_map *bmap)
4781 int i, k;
4782 isl_space *dim;
4783 isl_basic_map *range;
4784 int nparam, n_in, n_out;
4785 unsigned total;
4787 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4788 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4789 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4791 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4792 range = isl_basic_map_universe(dim);
4794 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4795 bmap = isl_basic_map_apply_range(bmap, range);
4796 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4798 total = isl_basic_map_total_dim(bmap);
4800 for (i = 0; i < n_out; ++i) {
4801 k = isl_basic_map_alloc_equality(bmap);
4802 if (k < 0)
4803 goto error;
4804 isl_seq_clr(bmap->eq[k], 1 + total);
4805 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4806 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4809 bmap = isl_basic_map_gauss(bmap, NULL);
4810 return isl_basic_map_finalize(bmap);
4811 error:
4812 isl_basic_map_free(bmap);
4813 return NULL;
4816 int isl_map_may_be_set(__isl_keep isl_map *map)
4818 if (!map)
4819 return -1;
4820 return isl_space_may_be_set(map->dim);
4823 /* Is this map actually a set?
4824 * Users should never call this function. Outside of isl,
4825 * the type should indicate whether something is a set or a map.
4827 int isl_map_is_set(__isl_keep isl_map *map)
4829 if (!map)
4830 return -1;
4831 return isl_space_is_set(map->dim);
4834 struct isl_set *isl_map_range(struct isl_map *map)
4836 int i;
4837 struct isl_set *set;
4839 if (!map)
4840 goto error;
4841 if (isl_map_is_set(map))
4842 return (isl_set *)map;
4844 map = isl_map_cow(map);
4845 if (!map)
4846 goto error;
4848 set = (struct isl_set *) map;
4849 set->dim = isl_space_range(set->dim);
4850 if (!set->dim)
4851 goto error;
4852 for (i = 0; i < map->n; ++i) {
4853 set->p[i] = isl_basic_map_range(map->p[i]);
4854 if (!set->p[i])
4855 goto error;
4857 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4858 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4859 return set;
4860 error:
4861 isl_map_free(map);
4862 return NULL;
4865 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4867 int i;
4869 map = isl_map_cow(map);
4870 if (!map)
4871 return NULL;
4873 map->dim = isl_space_domain_map(map->dim);
4874 if (!map->dim)
4875 goto error;
4876 for (i = 0; i < map->n; ++i) {
4877 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4878 if (!map->p[i])
4879 goto error;
4881 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4882 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4883 return map;
4884 error:
4885 isl_map_free(map);
4886 return NULL;
4889 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4891 int i;
4892 isl_space *range_dim;
4894 map = isl_map_cow(map);
4895 if (!map)
4896 return NULL;
4898 range_dim = isl_space_range(isl_map_get_space(map));
4899 range_dim = isl_space_from_range(range_dim);
4900 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4901 map->dim = isl_space_join(map->dim, range_dim);
4902 if (!map->dim)
4903 goto error;
4904 for (i = 0; i < map->n; ++i) {
4905 map->p[i] = isl_basic_map_range_map(map->p[i]);
4906 if (!map->p[i])
4907 goto error;
4909 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4910 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4911 return map;
4912 error:
4913 isl_map_free(map);
4914 return NULL;
4917 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4918 __isl_take isl_space *dim)
4920 int i;
4921 struct isl_map *map = NULL;
4923 set = isl_set_cow(set);
4924 if (!set || !dim)
4925 goto error;
4926 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4927 map = (struct isl_map *)set;
4928 for (i = 0; i < set->n; ++i) {
4929 map->p[i] = isl_basic_map_from_basic_set(
4930 set->p[i], isl_space_copy(dim));
4931 if (!map->p[i])
4932 goto error;
4934 isl_space_free(map->dim);
4935 map->dim = dim;
4936 return map;
4937 error:
4938 isl_space_free(dim);
4939 isl_set_free(set);
4940 return NULL;
4943 __isl_give isl_basic_map *isl_basic_map_from_domain(
4944 __isl_take isl_basic_set *bset)
4946 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4949 __isl_give isl_basic_map *isl_basic_map_from_range(
4950 __isl_take isl_basic_set *bset)
4952 isl_space *space;
4953 space = isl_basic_set_get_space(bset);
4954 space = isl_space_from_range(space);
4955 bset = isl_basic_set_reset_space(bset, space);
4956 return (isl_basic_map *)bset;
4959 struct isl_map *isl_map_from_range(struct isl_set *set)
4961 isl_space *space;
4962 space = isl_set_get_space(set);
4963 space = isl_space_from_range(space);
4964 set = isl_set_reset_space(set, space);
4965 return (struct isl_map *)set;
4968 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4970 return isl_map_reverse(isl_map_from_range(set));
4973 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4974 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4976 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4979 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4980 __isl_take isl_set *range)
4982 return isl_map_apply_range(isl_map_reverse(domain), range);
4985 struct isl_set *isl_set_from_map(struct isl_map *map)
4987 int i;
4988 struct isl_set *set = NULL;
4990 if (!map)
4991 return NULL;
4992 map = isl_map_cow(map);
4993 if (!map)
4994 return NULL;
4995 map->dim = isl_space_as_set_space(map->dim);
4996 if (!map->dim)
4997 goto error;
4998 set = (struct isl_set *)map;
4999 for (i = 0; i < map->n; ++i) {
5000 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5001 if (!set->p[i])
5002 goto error;
5004 return set;
5005 error:
5006 isl_map_free(map);
5007 return NULL;
5010 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5011 unsigned flags)
5013 struct isl_map *map;
5015 if (!dim)
5016 return NULL;
5017 if (n < 0)
5018 isl_die(dim->ctx, isl_error_internal,
5019 "negative number of basic maps", goto error);
5020 map = isl_alloc(dim->ctx, struct isl_map,
5021 sizeof(struct isl_map) +
5022 (n - 1) * sizeof(struct isl_basic_map *));
5023 if (!map)
5024 goto error;
5026 map->ctx = dim->ctx;
5027 isl_ctx_ref(map->ctx);
5028 map->ref = 1;
5029 map->size = n;
5030 map->n = 0;
5031 map->dim = dim;
5032 map->flags = flags;
5033 return map;
5034 error:
5035 isl_space_free(dim);
5036 return NULL;
5039 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5040 unsigned nparam, unsigned in, unsigned out, int n,
5041 unsigned flags)
5043 struct isl_map *map;
5044 isl_space *dims;
5046 dims = isl_space_alloc(ctx, nparam, in, out);
5047 if (!dims)
5048 return NULL;
5050 map = isl_map_alloc_space(dims, n, flags);
5051 return map;
5054 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5056 struct isl_basic_map *bmap;
5057 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5058 bmap = isl_basic_map_set_to_empty(bmap);
5059 return bmap;
5062 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5064 struct isl_basic_set *bset;
5065 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5066 bset = isl_basic_set_set_to_empty(bset);
5067 return bset;
5070 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5072 struct isl_basic_map *bmap;
5073 if (!model)
5074 return NULL;
5075 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5076 bmap = isl_basic_map_set_to_empty(bmap);
5077 return bmap;
5080 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5082 struct isl_basic_map *bmap;
5083 if (!model)
5084 return NULL;
5085 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5086 bmap = isl_basic_map_set_to_empty(bmap);
5087 return bmap;
5090 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5092 struct isl_basic_set *bset;
5093 if (!model)
5094 return NULL;
5095 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5096 bset = isl_basic_set_set_to_empty(bset);
5097 return bset;
5100 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5102 struct isl_basic_map *bmap;
5103 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5104 bmap = isl_basic_map_finalize(bmap);
5105 return bmap;
5108 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5110 struct isl_basic_set *bset;
5111 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5112 bset = isl_basic_set_finalize(bset);
5113 return bset;
5116 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5118 int i;
5119 unsigned total = isl_space_dim(dim, isl_dim_all);
5120 isl_basic_map *bmap;
5122 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5123 for (i = 0; i < total; ++i) {
5124 int k = isl_basic_map_alloc_inequality(bmap);
5125 if (k < 0)
5126 goto error;
5127 isl_seq_clr(bmap->ineq[k], 1 + total);
5128 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5130 return bmap;
5131 error:
5132 isl_basic_map_free(bmap);
5133 return NULL;
5136 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5138 return isl_basic_map_nat_universe(dim);
5141 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5143 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5146 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5148 return isl_map_nat_universe(dim);
5151 __isl_give isl_basic_map *isl_basic_map_universe_like(
5152 __isl_keep isl_basic_map *model)
5154 if (!model)
5155 return NULL;
5156 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5159 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5161 if (!model)
5162 return NULL;
5163 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5166 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5167 __isl_keep isl_set *model)
5169 if (!model)
5170 return NULL;
5171 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5174 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5176 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5179 struct isl_map *isl_map_empty_like(struct isl_map *model)
5181 if (!model)
5182 return NULL;
5183 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5186 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5188 if (!model)
5189 return NULL;
5190 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5193 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5195 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5198 struct isl_set *isl_set_empty_like(struct isl_set *model)
5200 if (!model)
5201 return NULL;
5202 return isl_set_empty(isl_space_copy(model->dim));
5205 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5207 struct isl_map *map;
5208 if (!dim)
5209 return NULL;
5210 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5211 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5212 return map;
5215 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5217 struct isl_set *set;
5218 if (!dim)
5219 return NULL;
5220 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5221 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5222 return set;
5225 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5227 if (!model)
5228 return NULL;
5229 return isl_set_universe(isl_space_copy(model->dim));
5232 struct isl_map *isl_map_dup(struct isl_map *map)
5234 int i;
5235 struct isl_map *dup;
5237 if (!map)
5238 return NULL;
5239 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5240 for (i = 0; i < map->n; ++i)
5241 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5242 return dup;
5245 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5246 __isl_take isl_basic_map *bmap)
5248 if (!bmap || !map)
5249 goto error;
5250 if (isl_basic_map_plain_is_empty(bmap)) {
5251 isl_basic_map_free(bmap);
5252 return map;
5254 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5255 isl_assert(map->ctx, map->n < map->size, goto error);
5256 map->p[map->n] = bmap;
5257 map->n++;
5258 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5259 return map;
5260 error:
5261 if (map)
5262 isl_map_free(map);
5263 if (bmap)
5264 isl_basic_map_free(bmap);
5265 return NULL;
5268 void *isl_map_free(struct isl_map *map)
5270 int i;
5272 if (!map)
5273 return NULL;
5275 if (--map->ref > 0)
5276 return NULL;
5278 isl_ctx_deref(map->ctx);
5279 for (i = 0; i < map->n; ++i)
5280 isl_basic_map_free(map->p[i]);
5281 isl_space_free(map->dim);
5282 free(map);
5284 return NULL;
5287 struct isl_map *isl_map_extend(struct isl_map *base,
5288 unsigned nparam, unsigned n_in, unsigned n_out)
5290 int i;
5292 base = isl_map_cow(base);
5293 if (!base)
5294 return NULL;
5296 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5297 if (!base->dim)
5298 goto error;
5299 for (i = 0; i < base->n; ++i) {
5300 base->p[i] = isl_basic_map_extend_space(base->p[i],
5301 isl_space_copy(base->dim), 0, 0, 0);
5302 if (!base->p[i])
5303 goto error;
5305 return base;
5306 error:
5307 isl_map_free(base);
5308 return NULL;
5311 struct isl_set *isl_set_extend(struct isl_set *base,
5312 unsigned nparam, unsigned dim)
5314 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5315 nparam, 0, dim);
5318 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5319 struct isl_basic_map *bmap, unsigned pos, int value)
5321 int j;
5323 bmap = isl_basic_map_cow(bmap);
5324 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5325 j = isl_basic_map_alloc_equality(bmap);
5326 if (j < 0)
5327 goto error;
5328 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5329 isl_int_set_si(bmap->eq[j][pos], -1);
5330 isl_int_set_si(bmap->eq[j][0], value);
5331 bmap = isl_basic_map_simplify(bmap);
5332 return isl_basic_map_finalize(bmap);
5333 error:
5334 isl_basic_map_free(bmap);
5335 return NULL;
5338 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5339 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5341 int j;
5343 bmap = isl_basic_map_cow(bmap);
5344 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5345 j = isl_basic_map_alloc_equality(bmap);
5346 if (j < 0)
5347 goto error;
5348 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5349 isl_int_set_si(bmap->eq[j][pos], -1);
5350 isl_int_set(bmap->eq[j][0], value);
5351 bmap = isl_basic_map_simplify(bmap);
5352 return isl_basic_map_finalize(bmap);
5353 error:
5354 isl_basic_map_free(bmap);
5355 return NULL;
5358 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5359 enum isl_dim_type type, unsigned pos, int value)
5361 if (!bmap)
5362 return NULL;
5363 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5364 return isl_basic_map_fix_pos_si(bmap,
5365 isl_basic_map_offset(bmap, type) + pos, value);
5366 error:
5367 isl_basic_map_free(bmap);
5368 return NULL;
5371 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5372 enum isl_dim_type type, unsigned pos, isl_int value)
5374 if (!bmap)
5375 return NULL;
5376 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5377 return isl_basic_map_fix_pos(bmap,
5378 isl_basic_map_offset(bmap, type) + pos, value);
5379 error:
5380 isl_basic_map_free(bmap);
5381 return NULL;
5384 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5385 * to be equal to "v".
5387 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5388 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5390 if (!bmap || !v)
5391 goto error;
5392 if (!isl_val_is_int(v))
5393 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5394 "expecting integer value", goto error);
5395 if (pos >= isl_basic_map_dim(bmap, type))
5396 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5397 "index out of bounds", goto error);
5398 pos += isl_basic_map_offset(bmap, type);
5399 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5400 isl_val_free(v);
5401 return bmap;
5402 error:
5403 isl_basic_map_free(bmap);
5404 isl_val_free(v);
5405 return NULL;
5408 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5409 * to be equal to "v".
5411 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5412 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5414 return isl_basic_map_fix_val(bset, type, pos, v);
5417 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5418 enum isl_dim_type type, unsigned pos, int value)
5420 return (struct isl_basic_set *)
5421 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5422 type, pos, value);
5425 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5426 enum isl_dim_type type, unsigned pos, isl_int value)
5428 return (struct isl_basic_set *)
5429 isl_basic_map_fix((struct isl_basic_map *)bset,
5430 type, pos, value);
5433 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5434 unsigned input, int value)
5436 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5439 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5440 unsigned dim, int value)
5442 return (struct isl_basic_set *)
5443 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5444 isl_dim_set, dim, value);
5447 static int remove_if_empty(__isl_keep isl_map *map, int i)
5449 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5451 if (empty < 0)
5452 return -1;
5453 if (!empty)
5454 return 0;
5456 isl_basic_map_free(map->p[i]);
5457 if (i != map->n - 1) {
5458 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5459 map->p[i] = map->p[map->n - 1];
5461 map->n--;
5463 return 0;
5466 /* Perform "fn" on each basic map of "map", where we may not be holding
5467 * the only reference to "map".
5468 * In particular, "fn" should be a semantics preserving operation
5469 * that we want to apply to all copies of "map". We therefore need
5470 * to be careful not to modify "map" in a way that breaks "map"
5471 * in case anything goes wrong.
5473 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5474 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5476 struct isl_basic_map *bmap;
5477 int i;
5479 if (!map)
5480 return NULL;
5482 for (i = map->n - 1; i >= 0; --i) {
5483 bmap = isl_basic_map_copy(map->p[i]);
5484 bmap = fn(bmap);
5485 if (!bmap)
5486 goto error;
5487 isl_basic_map_free(map->p[i]);
5488 map->p[i] = bmap;
5489 if (remove_if_empty(map, i) < 0)
5490 goto error;
5493 return map;
5494 error:
5495 isl_map_free(map);
5496 return NULL;
5499 struct isl_map *isl_map_fix_si(struct isl_map *map,
5500 enum isl_dim_type type, unsigned pos, int value)
5502 int i;
5504 map = isl_map_cow(map);
5505 if (!map)
5506 return NULL;
5508 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5509 for (i = map->n - 1; i >= 0; --i) {
5510 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5511 if (remove_if_empty(map, i) < 0)
5512 goto error;
5514 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5515 return map;
5516 error:
5517 isl_map_free(map);
5518 return NULL;
5521 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5522 enum isl_dim_type type, unsigned pos, int value)
5524 return (struct isl_set *)
5525 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5528 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5529 enum isl_dim_type type, unsigned pos, isl_int value)
5531 int i;
5533 map = isl_map_cow(map);
5534 if (!map)
5535 return NULL;
5537 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5538 for (i = 0; i < map->n; ++i) {
5539 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5540 if (!map->p[i])
5541 goto error;
5543 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5544 return map;
5545 error:
5546 isl_map_free(map);
5547 return NULL;
5550 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5551 enum isl_dim_type type, unsigned pos, isl_int value)
5553 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5556 /* Fix the value of the variable at position "pos" of type "type" of "map"
5557 * to be equal to "v".
5559 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5560 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5562 int i;
5564 map = isl_map_cow(map);
5565 if (!map || !v)
5566 goto error;
5568 if (!isl_val_is_int(v))
5569 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5570 "expecting integer value", goto error);
5571 if (pos >= isl_map_dim(map, type))
5572 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5573 "index out of bounds", goto error);
5574 for (i = map->n - 1; i >= 0; --i) {
5575 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5576 isl_val_copy(v));
5577 if (remove_if_empty(map, i) < 0)
5578 goto error;
5580 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5581 isl_val_free(v);
5582 return map;
5583 error:
5584 isl_map_free(map);
5585 isl_val_free(v);
5586 return NULL;
5589 /* Fix the value of the variable at position "pos" of type "type" of "set"
5590 * to be equal to "v".
5592 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5593 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5595 return isl_map_fix_val(set, type, pos, v);
5598 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5599 unsigned input, int value)
5601 return isl_map_fix_si(map, isl_dim_in, input, value);
5604 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5606 return (struct isl_set *)
5607 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5610 static __isl_give isl_basic_map *basic_map_bound_si(
5611 __isl_take isl_basic_map *bmap,
5612 enum isl_dim_type type, unsigned pos, int value, int upper)
5614 int j;
5616 if (!bmap)
5617 return NULL;
5618 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5619 pos += isl_basic_map_offset(bmap, type);
5620 bmap = isl_basic_map_cow(bmap);
5621 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5622 j = isl_basic_map_alloc_inequality(bmap);
5623 if (j < 0)
5624 goto error;
5625 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5626 if (upper) {
5627 isl_int_set_si(bmap->ineq[j][pos], -1);
5628 isl_int_set_si(bmap->ineq[j][0], value);
5629 } else {
5630 isl_int_set_si(bmap->ineq[j][pos], 1);
5631 isl_int_set_si(bmap->ineq[j][0], -value);
5633 bmap = isl_basic_map_simplify(bmap);
5634 return isl_basic_map_finalize(bmap);
5635 error:
5636 isl_basic_map_free(bmap);
5637 return NULL;
5640 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5641 __isl_take isl_basic_map *bmap,
5642 enum isl_dim_type type, unsigned pos, int value)
5644 return basic_map_bound_si(bmap, type, pos, value, 0);
5647 /* Constrain the values of the given dimension to be no greater than "value".
5649 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5650 __isl_take isl_basic_map *bmap,
5651 enum isl_dim_type type, unsigned pos, int value)
5653 return basic_map_bound_si(bmap, type, pos, value, 1);
5656 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5657 unsigned dim, isl_int value)
5659 int j;
5661 bset = isl_basic_set_cow(bset);
5662 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5663 j = isl_basic_set_alloc_inequality(bset);
5664 if (j < 0)
5665 goto error;
5666 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5667 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5668 isl_int_neg(bset->ineq[j][0], value);
5669 bset = isl_basic_set_simplify(bset);
5670 return isl_basic_set_finalize(bset);
5671 error:
5672 isl_basic_set_free(bset);
5673 return NULL;
5676 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5677 enum isl_dim_type type, unsigned pos, int value, int upper)
5679 int i;
5681 map = isl_map_cow(map);
5682 if (!map)
5683 return NULL;
5685 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5686 for (i = 0; i < map->n; ++i) {
5687 map->p[i] = basic_map_bound_si(map->p[i],
5688 type, pos, value, upper);
5689 if (!map->p[i])
5690 goto error;
5692 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5693 return map;
5694 error:
5695 isl_map_free(map);
5696 return NULL;
5699 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5700 enum isl_dim_type type, unsigned pos, int value)
5702 return map_bound_si(map, type, pos, value, 0);
5705 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5706 enum isl_dim_type type, unsigned pos, int value)
5708 return map_bound_si(map, type, pos, value, 1);
5711 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5712 enum isl_dim_type type, unsigned pos, int value)
5714 return (struct isl_set *)
5715 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5718 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5719 enum isl_dim_type type, unsigned pos, int value)
5721 return isl_map_upper_bound_si(set, type, pos, value);
5724 /* Bound the given variable of "bmap" from below (or above is "upper"
5725 * is set) to "value".
5727 static __isl_give isl_basic_map *basic_map_bound(
5728 __isl_take isl_basic_map *bmap,
5729 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5731 int j;
5733 if (!bmap)
5734 return NULL;
5735 if (pos >= isl_basic_map_dim(bmap, type))
5736 isl_die(bmap->ctx, isl_error_invalid,
5737 "index out of bounds", goto error);
5738 pos += isl_basic_map_offset(bmap, type);
5739 bmap = isl_basic_map_cow(bmap);
5740 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5741 j = isl_basic_map_alloc_inequality(bmap);
5742 if (j < 0)
5743 goto error;
5744 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5745 if (upper) {
5746 isl_int_set_si(bmap->ineq[j][pos], -1);
5747 isl_int_set(bmap->ineq[j][0], value);
5748 } else {
5749 isl_int_set_si(bmap->ineq[j][pos], 1);
5750 isl_int_neg(bmap->ineq[j][0], value);
5752 bmap = isl_basic_map_simplify(bmap);
5753 return isl_basic_map_finalize(bmap);
5754 error:
5755 isl_basic_map_free(bmap);
5756 return NULL;
5759 /* Bound the given variable of "map" from below (or above is "upper"
5760 * is set) to "value".
5762 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5763 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5765 int i;
5767 map = isl_map_cow(map);
5768 if (!map)
5769 return NULL;
5771 if (pos >= isl_map_dim(map, type))
5772 isl_die(map->ctx, isl_error_invalid,
5773 "index out of bounds", goto error);
5774 for (i = map->n - 1; i >= 0; --i) {
5775 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5776 if (remove_if_empty(map, i) < 0)
5777 goto error;
5779 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5780 return map;
5781 error:
5782 isl_map_free(map);
5783 return NULL;
5786 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5787 enum isl_dim_type type, unsigned pos, isl_int value)
5789 return map_bound(map, type, pos, value, 0);
5792 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5793 enum isl_dim_type type, unsigned pos, isl_int value)
5795 return map_bound(map, type, pos, value, 1);
5798 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5799 enum isl_dim_type type, unsigned pos, isl_int value)
5801 return isl_map_lower_bound(set, type, pos, value);
5804 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5805 enum isl_dim_type type, unsigned pos, isl_int value)
5807 return isl_map_upper_bound(set, type, pos, value);
5810 /* Force the values of the variable at position "pos" of type "type" of "set"
5811 * to be no smaller than "value".
5813 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5814 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5816 if (!value)
5817 goto error;
5818 if (!isl_val_is_int(value))
5819 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5820 "expecting integer value", goto error);
5821 set = isl_set_lower_bound(set, type, pos, value->n);
5822 isl_val_free(value);
5823 return set;
5824 error:
5825 isl_val_free(value);
5826 isl_set_free(set);
5827 return NULL;
5830 /* Force the values of the variable at position "pos" of type "type" of "set"
5831 * to be no greater than "value".
5833 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5834 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5836 if (!value)
5837 goto error;
5838 if (!isl_val_is_int(value))
5839 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5840 "expecting integer value", goto error);
5841 set = isl_set_upper_bound(set, type, pos, value->n);
5842 isl_val_free(value);
5843 return set;
5844 error:
5845 isl_val_free(value);
5846 isl_set_free(set);
5847 return NULL;
5850 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5851 isl_int value)
5853 int i;
5855 set = isl_set_cow(set);
5856 if (!set)
5857 return NULL;
5859 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5860 for (i = 0; i < set->n; ++i) {
5861 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5862 if (!set->p[i])
5863 goto error;
5865 return set;
5866 error:
5867 isl_set_free(set);
5868 return NULL;
5871 struct isl_map *isl_map_reverse(struct isl_map *map)
5873 int i;
5875 map = isl_map_cow(map);
5876 if (!map)
5877 return NULL;
5879 map->dim = isl_space_reverse(map->dim);
5880 if (!map->dim)
5881 goto error;
5882 for (i = 0; i < map->n; ++i) {
5883 map->p[i] = isl_basic_map_reverse(map->p[i]);
5884 if (!map->p[i])
5885 goto error;
5887 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5888 return map;
5889 error:
5890 isl_map_free(map);
5891 return NULL;
5894 static struct isl_map *isl_basic_map_partial_lexopt(
5895 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5896 struct isl_set **empty, int max)
5898 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5901 struct isl_map *isl_basic_map_partial_lexmax(
5902 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5903 struct isl_set **empty)
5905 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5908 struct isl_map *isl_basic_map_partial_lexmin(
5909 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5910 struct isl_set **empty)
5912 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5915 struct isl_set *isl_basic_set_partial_lexmin(
5916 struct isl_basic_set *bset, struct isl_basic_set *dom,
5917 struct isl_set **empty)
5919 return (struct isl_set *)
5920 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5921 dom, empty);
5924 struct isl_set *isl_basic_set_partial_lexmax(
5925 struct isl_basic_set *bset, struct isl_basic_set *dom,
5926 struct isl_set **empty)
5928 return (struct isl_set *)
5929 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5930 dom, empty);
5933 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5934 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5935 __isl_give isl_set **empty)
5937 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5940 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5941 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5942 __isl_give isl_set **empty)
5944 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5947 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5948 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5949 __isl_give isl_set **empty)
5951 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5954 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5955 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5956 __isl_give isl_set **empty)
5958 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5961 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5962 __isl_take isl_basic_map *bmap, int max)
5964 isl_basic_set *dom = NULL;
5965 isl_space *dom_space;
5967 if (!bmap)
5968 goto error;
5969 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5970 dom = isl_basic_set_universe(dom_space);
5971 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5972 error:
5973 isl_basic_map_free(bmap);
5974 return NULL;
5977 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5978 __isl_take isl_basic_map *bmap)
5980 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5983 #undef TYPE
5984 #define TYPE isl_pw_multi_aff
5985 #undef SUFFIX
5986 #define SUFFIX _pw_multi_aff
5987 #undef EMPTY
5988 #define EMPTY isl_pw_multi_aff_empty
5989 #undef ADD
5990 #define ADD isl_pw_multi_aff_union_add
5991 #include "isl_map_lexopt_templ.c"
5993 /* Given a map "map", compute the lexicographically minimal
5994 * (or maximal) image element for each domain element in dom,
5995 * in the form of an isl_pw_multi_aff.
5996 * Set *empty to those elements in dom that do not have an image element.
5998 * We first compute the lexicographically minimal or maximal element
5999 * in the first basic map. This results in a partial solution "res"
6000 * and a subset "todo" of dom that still need to be handled.
6001 * We then consider each of the remaining maps in "map" and successively
6002 * update both "res" and "todo".
6004 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6005 __isl_take isl_map *map, __isl_take isl_set *dom,
6006 __isl_give isl_set **empty, int max)
6008 int i;
6009 isl_pw_multi_aff *res;
6010 isl_set *todo;
6012 if (!map || !dom)
6013 goto error;
6015 if (isl_map_plain_is_empty(map)) {
6016 if (empty)
6017 *empty = dom;
6018 else
6019 isl_set_free(dom);
6020 return isl_pw_multi_aff_from_map(map);
6023 res = basic_map_partial_lexopt_pw_multi_aff(
6024 isl_basic_map_copy(map->p[0]),
6025 isl_set_copy(dom), &todo, max);
6027 for (i = 1; i < map->n; ++i) {
6028 isl_pw_multi_aff *res_i;
6029 isl_set *todo_i;
6031 res_i = basic_map_partial_lexopt_pw_multi_aff(
6032 isl_basic_map_copy(map->p[i]),
6033 isl_set_copy(dom), &todo_i, max);
6035 if (max)
6036 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6037 else
6038 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6040 todo = isl_set_intersect(todo, todo_i);
6043 isl_set_free(dom);
6044 isl_map_free(map);
6046 if (empty)
6047 *empty = todo;
6048 else
6049 isl_set_free(todo);
6051 return res;
6052 error:
6053 if (empty)
6054 *empty = NULL;
6055 isl_set_free(dom);
6056 isl_map_free(map);
6057 return NULL;
6060 #undef TYPE
6061 #define TYPE isl_map
6062 #undef SUFFIX
6063 #define SUFFIX
6064 #undef EMPTY
6065 #define EMPTY isl_map_empty
6066 #undef ADD
6067 #define ADD isl_map_union_disjoint
6068 #include "isl_map_lexopt_templ.c"
6070 /* Given a map "map", compute the lexicographically minimal
6071 * (or maximal) image element for each domain element in dom.
6072 * Set *empty to those elements in dom that do not have an image element.
6074 * We first compute the lexicographically minimal or maximal element
6075 * in the first basic map. This results in a partial solution "res"
6076 * and a subset "todo" of dom that still need to be handled.
6077 * We then consider each of the remaining maps in "map" and successively
6078 * update both "res" and "todo".
6080 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6081 * Assume we are computing the lexicographical maximum.
6082 * We first compute the lexicographically maximal element in basic map i.
6083 * This results in a partial solution res_i and a subset todo_i.
6084 * Then we combine these results with those obtain for the first k basic maps
6085 * to obtain a result that is valid for the first k+1 basic maps.
6086 * In particular, the set where there is no solution is the set where
6087 * there is no solution for the first k basic maps and also no solution
6088 * for the ith basic map, i.e.,
6090 * todo^i = todo^k * todo_i
6092 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6093 * solutions, arbitrarily breaking ties in favor of res^k.
6094 * That is, when res^k(a) >= res_i(a), we pick res^k and
6095 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6096 * the lexicographic order.)
6097 * In practice, we compute
6099 * res^k * (res_i . "<=")
6101 * and
6103 * res_i * (res^k . "<")
6105 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6106 * where only one of res^k and res_i provides a solution and we simply pick
6107 * that one, i.e.,
6109 * res^k * todo_i
6110 * and
6111 * res_i * todo^k
6113 * Note that we only compute these intersections when dom(res^k) intersects
6114 * dom(res_i). Otherwise, the only effect of these intersections is to
6115 * potentially break up res^k and res_i into smaller pieces.
6116 * We want to avoid such splintering as much as possible.
6117 * In fact, an earlier implementation of this function would look for
6118 * better results in the domain of res^k and for extra results in todo^k,
6119 * but this would always result in a splintering according to todo^k,
6120 * even when the domain of basic map i is disjoint from the domains of
6121 * the previous basic maps.
6123 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6124 __isl_take isl_map *map, __isl_take isl_set *dom,
6125 __isl_give isl_set **empty, int max)
6127 int i;
6128 struct isl_map *res;
6129 struct isl_set *todo;
6131 if (!map || !dom)
6132 goto error;
6134 if (isl_map_plain_is_empty(map)) {
6135 if (empty)
6136 *empty = dom;
6137 else
6138 isl_set_free(dom);
6139 return map;
6142 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6143 isl_set_copy(dom), &todo, max);
6145 for (i = 1; i < map->n; ++i) {
6146 isl_map *lt, *le;
6147 isl_map *res_i;
6148 isl_set *todo_i;
6149 isl_space *dim = isl_space_range(isl_map_get_space(res));
6151 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6152 isl_set_copy(dom), &todo_i, max);
6154 if (max) {
6155 lt = isl_map_lex_lt(isl_space_copy(dim));
6156 le = isl_map_lex_le(dim);
6157 } else {
6158 lt = isl_map_lex_gt(isl_space_copy(dim));
6159 le = isl_map_lex_ge(dim);
6161 lt = isl_map_apply_range(isl_map_copy(res), lt);
6162 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6163 le = isl_map_apply_range(isl_map_copy(res_i), le);
6164 le = isl_map_intersect(le, isl_map_copy(res));
6166 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6167 res = isl_map_intersect_domain(res,
6168 isl_set_copy(todo_i));
6169 res_i = isl_map_intersect_domain(res_i,
6170 isl_set_copy(todo));
6173 res = isl_map_union_disjoint(res, res_i);
6174 res = isl_map_union_disjoint(res, lt);
6175 res = isl_map_union_disjoint(res, le);
6177 todo = isl_set_intersect(todo, todo_i);
6180 isl_set_free(dom);
6181 isl_map_free(map);
6183 if (empty)
6184 *empty = todo;
6185 else
6186 isl_set_free(todo);
6188 return res;
6189 error:
6190 if (empty)
6191 *empty = NULL;
6192 isl_set_free(dom);
6193 isl_map_free(map);
6194 return NULL;
6197 __isl_give isl_map *isl_map_partial_lexmax(
6198 __isl_take isl_map *map, __isl_take isl_set *dom,
6199 __isl_give isl_set **empty)
6201 return isl_map_partial_lexopt(map, dom, empty, 1);
6204 __isl_give isl_map *isl_map_partial_lexmin(
6205 __isl_take isl_map *map, __isl_take isl_set *dom,
6206 __isl_give isl_set **empty)
6208 return isl_map_partial_lexopt(map, dom, empty, 0);
6211 __isl_give isl_set *isl_set_partial_lexmin(
6212 __isl_take isl_set *set, __isl_take isl_set *dom,
6213 __isl_give isl_set **empty)
6215 return (struct isl_set *)
6216 isl_map_partial_lexmin((struct isl_map *)set,
6217 dom, empty);
6220 __isl_give isl_set *isl_set_partial_lexmax(
6221 __isl_take isl_set *set, __isl_take isl_set *dom,
6222 __isl_give isl_set **empty)
6224 return (struct isl_set *)
6225 isl_map_partial_lexmax((struct isl_map *)set,
6226 dom, empty);
6229 /* Compute the lexicographic minimum (or maximum if "max" is set)
6230 * of "bmap" over its domain.
6232 * Since we are not interested in the part of the domain space where
6233 * there is no solution, we initialize the domain to those constraints
6234 * of "bmap" that only involve the parameters and the input dimensions.
6235 * This relieves the parametric programming engine from detecting those
6236 * inequalities and transferring them to the context. More importantly,
6237 * it ensures that those inequalities are transferred first and not
6238 * intermixed with inequalities that actually split the domain.
6240 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6242 int n_div;
6243 int n_out;
6244 isl_basic_map *copy;
6245 isl_basic_set *dom;
6247 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6248 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6249 copy = isl_basic_map_copy(bmap);
6250 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6251 isl_dim_div, 0, n_div);
6252 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6253 isl_dim_out, 0, n_out);
6254 dom = isl_basic_map_domain(copy);
6255 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6258 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6260 return isl_basic_map_lexopt(bmap, 0);
6263 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6265 return isl_basic_map_lexopt(bmap, 1);
6268 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6270 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6273 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6275 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6278 /* Extract the first and only affine expression from list
6279 * and then add it to *pwaff with the given dom.
6280 * This domain is known to be disjoint from other domains
6281 * because of the way isl_basic_map_foreach_lexmax works.
6283 static int update_dim_opt(__isl_take isl_basic_set *dom,
6284 __isl_take isl_aff_list *list, void *user)
6286 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6287 isl_aff *aff;
6288 isl_pw_aff **pwaff = user;
6289 isl_pw_aff *pwaff_i;
6291 if (!list)
6292 goto error;
6293 if (isl_aff_list_n_aff(list) != 1)
6294 isl_die(ctx, isl_error_internal,
6295 "expecting single element list", goto error);
6297 aff = isl_aff_list_get_aff(list, 0);
6298 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6300 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6302 isl_aff_list_free(list);
6304 return 0;
6305 error:
6306 isl_basic_set_free(dom);
6307 isl_aff_list_free(list);
6308 return -1;
6311 /* Given a basic map with one output dimension, compute the minimum or
6312 * maximum of that dimension as an isl_pw_aff.
6314 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6315 * call update_dim_opt on each leaf of the result.
6317 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6318 int max)
6320 isl_space *dim = isl_basic_map_get_space(bmap);
6321 isl_pw_aff *pwaff;
6322 int r;
6324 dim = isl_space_from_domain(isl_space_domain(dim));
6325 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6326 pwaff = isl_pw_aff_empty(dim);
6328 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6329 if (r < 0)
6330 return isl_pw_aff_free(pwaff);
6332 return pwaff;
6335 /* Compute the minimum or maximum of the given output dimension
6336 * as a function of the parameters and the input dimensions,
6337 * but independently of the other output dimensions.
6339 * We first project out the other output dimension and then compute
6340 * the "lexicographic" maximum in each basic map, combining the results
6341 * using isl_pw_aff_union_max.
6343 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6344 int max)
6346 int i;
6347 isl_pw_aff *pwaff;
6348 unsigned n_out;
6350 n_out = isl_map_dim(map, isl_dim_out);
6351 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6352 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6353 if (!map)
6354 return NULL;
6356 if (map->n == 0) {
6357 isl_space *dim = isl_map_get_space(map);
6358 dim = isl_space_domain(isl_space_from_range(dim));
6359 isl_map_free(map);
6360 return isl_pw_aff_empty(dim);
6363 pwaff = basic_map_dim_opt(map->p[0], max);
6364 for (i = 1; i < map->n; ++i) {
6365 isl_pw_aff *pwaff_i;
6367 pwaff_i = basic_map_dim_opt(map->p[i], max);
6368 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6371 isl_map_free(map);
6373 return pwaff;
6376 /* Compute the maximum of the given output dimension as a function of the
6377 * parameters and input dimensions, but independently of
6378 * the other output dimensions.
6380 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6382 return map_dim_opt(map, pos, 1);
6385 /* Compute the minimum or maximum of the given set dimension
6386 * as a function of the parameters,
6387 * but independently of the other set dimensions.
6389 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6390 int max)
6392 return map_dim_opt(set, pos, max);
6395 /* Compute the maximum of the given set dimension as a function of the
6396 * parameters, but independently of the other set dimensions.
6398 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6400 return set_dim_opt(set, pos, 1);
6403 /* Compute the minimum of the given set dimension as a function of the
6404 * parameters, but independently of the other set dimensions.
6406 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6408 return set_dim_opt(set, pos, 0);
6411 /* Apply a preimage specified by "mat" on the parameters of "bset".
6412 * bset is assumed to have only parameters and divs.
6414 static struct isl_basic_set *basic_set_parameter_preimage(
6415 struct isl_basic_set *bset, struct isl_mat *mat)
6417 unsigned nparam;
6419 if (!bset || !mat)
6420 goto error;
6422 bset->dim = isl_space_cow(bset->dim);
6423 if (!bset->dim)
6424 goto error;
6426 nparam = isl_basic_set_dim(bset, isl_dim_param);
6428 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6430 bset->dim->nparam = 0;
6431 bset->dim->n_out = nparam;
6432 bset = isl_basic_set_preimage(bset, mat);
6433 if (bset) {
6434 bset->dim->nparam = bset->dim->n_out;
6435 bset->dim->n_out = 0;
6437 return bset;
6438 error:
6439 isl_mat_free(mat);
6440 isl_basic_set_free(bset);
6441 return NULL;
6444 /* Apply a preimage specified by "mat" on the parameters of "set".
6445 * set is assumed to have only parameters and divs.
6447 static struct isl_set *set_parameter_preimage(
6448 struct isl_set *set, struct isl_mat *mat)
6450 isl_space *dim = NULL;
6451 unsigned nparam;
6453 if (!set || !mat)
6454 goto error;
6456 dim = isl_space_copy(set->dim);
6457 dim = isl_space_cow(dim);
6458 if (!dim)
6459 goto error;
6461 nparam = isl_set_dim(set, isl_dim_param);
6463 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6465 dim->nparam = 0;
6466 dim->n_out = nparam;
6467 isl_set_reset_space(set, dim);
6468 set = isl_set_preimage(set, mat);
6469 if (!set)
6470 goto error2;
6471 dim = isl_space_copy(set->dim);
6472 dim = isl_space_cow(dim);
6473 if (!dim)
6474 goto error2;
6475 dim->nparam = dim->n_out;
6476 dim->n_out = 0;
6477 isl_set_reset_space(set, dim);
6478 return set;
6479 error:
6480 isl_space_free(dim);
6481 isl_mat_free(mat);
6482 error2:
6483 isl_set_free(set);
6484 return NULL;
6487 /* Intersect the basic set "bset" with the affine space specified by the
6488 * equalities in "eq".
6490 static struct isl_basic_set *basic_set_append_equalities(
6491 struct isl_basic_set *bset, struct isl_mat *eq)
6493 int i, k;
6494 unsigned len;
6496 if (!bset || !eq)
6497 goto error;
6499 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6500 eq->n_row, 0);
6501 if (!bset)
6502 goto error;
6504 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6505 for (i = 0; i < eq->n_row; ++i) {
6506 k = isl_basic_set_alloc_equality(bset);
6507 if (k < 0)
6508 goto error;
6509 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6510 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6512 isl_mat_free(eq);
6514 bset = isl_basic_set_gauss(bset, NULL);
6515 bset = isl_basic_set_finalize(bset);
6517 return bset;
6518 error:
6519 isl_mat_free(eq);
6520 isl_basic_set_free(bset);
6521 return NULL;
6524 /* Intersect the set "set" with the affine space specified by the
6525 * equalities in "eq".
6527 static struct isl_set *set_append_equalities(struct isl_set *set,
6528 struct isl_mat *eq)
6530 int i;
6532 if (!set || !eq)
6533 goto error;
6535 for (i = 0; i < set->n; ++i) {
6536 set->p[i] = basic_set_append_equalities(set->p[i],
6537 isl_mat_copy(eq));
6538 if (!set->p[i])
6539 goto error;
6541 isl_mat_free(eq);
6542 return set;
6543 error:
6544 isl_mat_free(eq);
6545 isl_set_free(set);
6546 return NULL;
6549 /* Given a basic set "bset" that only involves parameters and existentially
6550 * quantified variables, return the index of the first equality
6551 * that only involves parameters. If there is no such equality then
6552 * return bset->n_eq.
6554 * This function assumes that isl_basic_set_gauss has been called on "bset".
6556 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6558 int i, j;
6559 unsigned nparam, n_div;
6561 if (!bset)
6562 return -1;
6564 nparam = isl_basic_set_dim(bset, isl_dim_param);
6565 n_div = isl_basic_set_dim(bset, isl_dim_div);
6567 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6568 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6569 ++i;
6572 return i;
6575 /* Compute an explicit representation for the existentially quantified
6576 * variables in "bset" by computing the "minimal value" of the set
6577 * variables. Since there are no set variables, the computation of
6578 * the minimal value essentially computes an explicit representation
6579 * of the non-empty part(s) of "bset".
6581 * The input only involves parameters and existentially quantified variables.
6582 * All equalities among parameters have been removed.
6584 * Since the existentially quantified variables in the result are in general
6585 * going to be different from those in the input, we first replace
6586 * them by the minimal number of variables based on their equalities.
6587 * This should simplify the parametric integer programming.
6589 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6591 isl_morph *morph1, *morph2;
6592 isl_set *set;
6593 unsigned n;
6595 if (!bset)
6596 return NULL;
6597 if (bset->n_eq == 0)
6598 return isl_basic_set_lexmin(bset);
6600 morph1 = isl_basic_set_parameter_compression(bset);
6601 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6602 bset = isl_basic_set_lift(bset);
6603 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6604 bset = isl_morph_basic_set(morph2, bset);
6605 n = isl_basic_set_dim(bset, isl_dim_set);
6606 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6608 set = isl_basic_set_lexmin(bset);
6610 set = isl_morph_set(isl_morph_inverse(morph1), set);
6612 return set;
6615 /* Project the given basic set onto its parameter domain, possibly introducing
6616 * new, explicit, existential variables in the constraints.
6617 * The input has parameters and (possibly implicit) existential variables.
6618 * The output has the same parameters, but only
6619 * explicit existentially quantified variables.
6621 * The actual projection is performed by pip, but pip doesn't seem
6622 * to like equalities very much, so we first remove the equalities
6623 * among the parameters by performing a variable compression on
6624 * the parameters. Afterward, an inverse transformation is performed
6625 * and the equalities among the parameters are inserted back in.
6627 * The variable compression on the parameters may uncover additional
6628 * equalities that were only implicit before. We therefore check
6629 * if there are any new parameter equalities in the result and
6630 * if so recurse. The removal of parameter equalities is required
6631 * for the parameter compression performed by base_compute_divs.
6633 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6635 int i;
6636 struct isl_mat *eq;
6637 struct isl_mat *T, *T2;
6638 struct isl_set *set;
6639 unsigned nparam;
6641 bset = isl_basic_set_cow(bset);
6642 if (!bset)
6643 return NULL;
6645 if (bset->n_eq == 0)
6646 return base_compute_divs(bset);
6648 bset = isl_basic_set_gauss(bset, NULL);
6649 if (!bset)
6650 return NULL;
6651 if (isl_basic_set_plain_is_empty(bset))
6652 return isl_set_from_basic_set(bset);
6654 i = first_parameter_equality(bset);
6655 if (i == bset->n_eq)
6656 return base_compute_divs(bset);
6658 nparam = isl_basic_set_dim(bset, isl_dim_param);
6659 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6660 0, 1 + nparam);
6661 eq = isl_mat_cow(eq);
6662 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6663 if (T && T->n_col == 0) {
6664 isl_mat_free(T);
6665 isl_mat_free(T2);
6666 isl_mat_free(eq);
6667 bset = isl_basic_set_set_to_empty(bset);
6668 return isl_set_from_basic_set(bset);
6670 bset = basic_set_parameter_preimage(bset, T);
6672 i = first_parameter_equality(bset);
6673 if (!bset)
6674 set = NULL;
6675 else if (i == bset->n_eq)
6676 set = base_compute_divs(bset);
6677 else
6678 set = parameter_compute_divs(bset);
6679 set = set_parameter_preimage(set, T2);
6680 set = set_append_equalities(set, eq);
6681 return set;
6684 /* Insert the divs from "ls" before those of "bmap".
6686 * The number of columns is not changed, which means that the last
6687 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6688 * The caller is responsible for removing the same number of dimensions
6689 * from the space of "bmap".
6691 static __isl_give isl_basic_map *insert_divs_from_local_space(
6692 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6694 int i;
6695 int n_div;
6696 int old_n_div;
6698 n_div = isl_local_space_dim(ls, isl_dim_div);
6699 if (n_div == 0)
6700 return bmap;
6702 old_n_div = bmap->n_div;
6703 bmap = insert_div_rows(bmap, n_div);
6704 if (!bmap)
6705 return NULL;
6707 for (i = 0; i < n_div; ++i) {
6708 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6709 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6712 return bmap;
6715 /* Replace the space of "bmap" by the space and divs of "ls".
6717 * If "ls" has any divs, then we simplify the result since we may
6718 * have discovered some additional equalities that could simplify
6719 * the div expressions.
6721 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6722 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6724 int n_div;
6726 bmap = isl_basic_map_cow(bmap);
6727 if (!bmap || !ls)
6728 goto error;
6730 n_div = isl_local_space_dim(ls, isl_dim_div);
6731 bmap = insert_divs_from_local_space(bmap, ls);
6732 if (!bmap)
6733 goto error;
6735 isl_space_free(bmap->dim);
6736 bmap->dim = isl_local_space_get_space(ls);
6737 if (!bmap->dim)
6738 goto error;
6740 isl_local_space_free(ls);
6741 if (n_div > 0)
6742 bmap = isl_basic_map_simplify(bmap);
6743 bmap = isl_basic_map_finalize(bmap);
6744 return bmap;
6745 error:
6746 isl_basic_map_free(bmap);
6747 isl_local_space_free(ls);
6748 return NULL;
6751 /* Replace the space of "map" by the space and divs of "ls".
6753 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6754 __isl_take isl_local_space *ls)
6756 int i;
6758 map = isl_map_cow(map);
6759 if (!map || !ls)
6760 goto error;
6762 for (i = 0; i < map->n; ++i) {
6763 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6764 isl_local_space_copy(ls));
6765 if (!map->p[i])
6766 goto error;
6768 isl_space_free(map->dim);
6769 map->dim = isl_local_space_get_space(ls);
6770 if (!map->dim)
6771 goto error;
6773 isl_local_space_free(ls);
6774 return map;
6775 error:
6776 isl_local_space_free(ls);
6777 isl_map_free(map);
6778 return NULL;
6781 /* Compute an explicit representation for the existentially
6782 * quantified variables for which do not know any explicit representation yet.
6784 * We first sort the existentially quantified variables so that the
6785 * existentially quantified variables for which we already have an explicit
6786 * representation are placed before those for which we do not.
6787 * The input dimensions, the output dimensions and the existentially
6788 * quantified variables for which we already have an explicit
6789 * representation are then turned into parameters.
6790 * compute_divs returns a map with the same parameters and
6791 * no input or output dimensions and the dimension specification
6792 * is reset to that of the input, including the existentially quantified
6793 * variables for which we already had an explicit representation.
6795 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6797 struct isl_basic_set *bset;
6798 struct isl_set *set;
6799 struct isl_map *map;
6800 isl_space *dim;
6801 isl_local_space *ls;
6802 unsigned nparam;
6803 unsigned n_in;
6804 unsigned n_out;
6805 unsigned n_known;
6806 int i;
6808 bmap = isl_basic_map_sort_divs(bmap);
6809 bmap = isl_basic_map_cow(bmap);
6810 if (!bmap)
6811 return NULL;
6813 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6814 if (isl_int_is_zero(bmap->div[n_known][0]))
6815 break;
6817 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6818 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6819 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6820 dim = isl_space_set_alloc(bmap->ctx,
6821 nparam + n_in + n_out + n_known, 0);
6822 if (!dim)
6823 goto error;
6825 ls = isl_basic_map_get_local_space(bmap);
6826 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6827 n_known, bmap->n_div - n_known);
6828 if (n_known > 0) {
6829 for (i = n_known; i < bmap->n_div; ++i)
6830 swap_div(bmap, i - n_known, i);
6831 bmap->n_div -= n_known;
6832 bmap->extra -= n_known;
6834 bmap = isl_basic_map_reset_space(bmap, dim);
6835 bset = (struct isl_basic_set *)bmap;
6837 set = parameter_compute_divs(bset);
6838 map = (struct isl_map *)set;
6839 map = replace_space_by_local_space(map, ls);
6841 return map;
6842 error:
6843 isl_basic_map_free(bmap);
6844 return NULL;
6847 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6849 int i;
6850 unsigned off;
6852 if (!bmap)
6853 return -1;
6855 off = isl_space_dim(bmap->dim, isl_dim_all);
6856 for (i = 0; i < bmap->n_div; ++i) {
6857 if (isl_int_is_zero(bmap->div[i][0]))
6858 return 0;
6859 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6860 return -1);
6862 return 1;
6865 static int map_divs_known(__isl_keep isl_map *map)
6867 int i;
6869 if (!map)
6870 return -1;
6872 for (i = 0; i < map->n; ++i) {
6873 int known = isl_basic_map_divs_known(map->p[i]);
6874 if (known <= 0)
6875 return known;
6878 return 1;
6881 /* If bmap contains any unknown divs, then compute explicit
6882 * expressions for them. However, this computation may be
6883 * quite expensive, so first try to remove divs that aren't
6884 * strictly needed.
6886 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6888 int known;
6889 struct isl_map *map;
6891 known = isl_basic_map_divs_known(bmap);
6892 if (known < 0)
6893 goto error;
6894 if (known)
6895 return isl_map_from_basic_map(bmap);
6897 bmap = isl_basic_map_drop_redundant_divs(bmap);
6899 known = isl_basic_map_divs_known(bmap);
6900 if (known < 0)
6901 goto error;
6902 if (known)
6903 return isl_map_from_basic_map(bmap);
6905 map = compute_divs(bmap);
6906 return map;
6907 error:
6908 isl_basic_map_free(bmap);
6909 return NULL;
6912 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6914 int i;
6915 int known;
6916 struct isl_map *res;
6918 if (!map)
6919 return NULL;
6920 if (map->n == 0)
6921 return map;
6923 known = map_divs_known(map);
6924 if (known < 0) {
6925 isl_map_free(map);
6926 return NULL;
6928 if (known)
6929 return map;
6931 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6932 for (i = 1 ; i < map->n; ++i) {
6933 struct isl_map *r2;
6934 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6935 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6936 res = isl_map_union_disjoint(res, r2);
6937 else
6938 res = isl_map_union(res, r2);
6940 isl_map_free(map);
6942 return res;
6945 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6947 return (struct isl_set *)
6948 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6951 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6953 return (struct isl_set *)
6954 isl_map_compute_divs((struct isl_map *)set);
6957 struct isl_set *isl_map_domain(struct isl_map *map)
6959 int i;
6960 struct isl_set *set;
6962 if (!map)
6963 goto error;
6965 map = isl_map_cow(map);
6966 if (!map)
6967 return NULL;
6969 set = (struct isl_set *)map;
6970 set->dim = isl_space_domain(set->dim);
6971 if (!set->dim)
6972 goto error;
6973 for (i = 0; i < map->n; ++i) {
6974 set->p[i] = isl_basic_map_domain(map->p[i]);
6975 if (!set->p[i])
6976 goto error;
6978 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6979 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6980 return set;
6981 error:
6982 isl_map_free(map);
6983 return NULL;
6986 /* Return the union of "map1" and "map2", where we assume for now that
6987 * "map1" and "map2" are disjoint. Note that the basic maps inside
6988 * "map1" or "map2" may not be disjoint from each other.
6989 * Also note that this function is also called from isl_map_union,
6990 * which takes care of handling the situation where "map1" and "map2"
6991 * may not be disjoint.
6993 * If one of the inputs is empty, we can simply return the other input.
6994 * Similarly, if one of the inputs is universal, then it is equal to the union.
6996 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6997 __isl_take isl_map *map2)
6999 int i;
7000 unsigned flags = 0;
7001 struct isl_map *map = NULL;
7002 int is_universe;
7004 if (!map1 || !map2)
7005 goto error;
7007 if (map1->n == 0) {
7008 isl_map_free(map1);
7009 return map2;
7011 if (map2->n == 0) {
7012 isl_map_free(map2);
7013 return map1;
7016 is_universe = isl_map_plain_is_universe(map1);
7017 if (is_universe < 0)
7018 goto error;
7019 if (is_universe) {
7020 isl_map_free(map2);
7021 return map1;
7024 is_universe = isl_map_plain_is_universe(map2);
7025 if (is_universe < 0)
7026 goto error;
7027 if (is_universe) {
7028 isl_map_free(map1);
7029 return map2;
7032 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
7034 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7035 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7036 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7038 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7039 map1->n + map2->n, flags);
7040 if (!map)
7041 goto error;
7042 for (i = 0; i < map1->n; ++i) {
7043 map = isl_map_add_basic_map(map,
7044 isl_basic_map_copy(map1->p[i]));
7045 if (!map)
7046 goto error;
7048 for (i = 0; i < map2->n; ++i) {
7049 map = isl_map_add_basic_map(map,
7050 isl_basic_map_copy(map2->p[i]));
7051 if (!map)
7052 goto error;
7054 isl_map_free(map1);
7055 isl_map_free(map2);
7056 return map;
7057 error:
7058 isl_map_free(map);
7059 isl_map_free(map1);
7060 isl_map_free(map2);
7061 return NULL;
7064 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7065 __isl_take isl_map *map2)
7067 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7070 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
7072 map1 = isl_map_union_disjoint(map1, map2);
7073 if (!map1)
7074 return NULL;
7075 if (map1->n > 1)
7076 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7077 return map1;
7080 struct isl_set *isl_set_union_disjoint(
7081 struct isl_set *set1, struct isl_set *set2)
7083 return (struct isl_set *)
7084 isl_map_union_disjoint(
7085 (struct isl_map *)set1, (struct isl_map *)set2);
7088 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7090 return (struct isl_set *)
7091 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7094 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7095 * the results.
7097 * "map" and "set" are assumed to be compatible and non-NULL.
7099 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7100 __isl_take isl_set *set,
7101 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7102 __isl_take isl_basic_set *bset))
7104 unsigned flags = 0;
7105 struct isl_map *result;
7106 int i, j;
7108 if (isl_set_plain_is_universe(set)) {
7109 isl_set_free(set);
7110 return map;
7113 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7114 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7115 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7117 result = isl_map_alloc_space(isl_space_copy(map->dim),
7118 map->n * set->n, flags);
7119 for (i = 0; result && i < map->n; ++i)
7120 for (j = 0; j < set->n; ++j) {
7121 result = isl_map_add_basic_map(result,
7122 fn(isl_basic_map_copy(map->p[i]),
7123 isl_basic_set_copy(set->p[j])));
7124 if (!result)
7125 break;
7128 isl_map_free(map);
7129 isl_set_free(set);
7130 return result;
7133 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7134 __isl_take isl_set *set)
7136 if (!map || !set)
7137 goto error;
7139 if (!isl_map_compatible_range(map, set))
7140 isl_die(set->ctx, isl_error_invalid,
7141 "incompatible spaces", goto error);
7143 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7144 error:
7145 isl_map_free(map);
7146 isl_set_free(set);
7147 return NULL;
7150 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7151 __isl_take isl_set *set)
7153 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7156 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7157 __isl_take isl_set *set)
7159 if (!map || !set)
7160 goto error;
7162 if (!isl_map_compatible_domain(map, set))
7163 isl_die(set->ctx, isl_error_invalid,
7164 "incompatible spaces", goto error);
7166 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7167 error:
7168 isl_map_free(map);
7169 isl_set_free(set);
7170 return NULL;
7173 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7174 __isl_take isl_set *set)
7176 return isl_map_align_params_map_map_and(map, set,
7177 &map_intersect_domain);
7180 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7181 __isl_take isl_map *map2)
7183 if (!map1 || !map2)
7184 goto error;
7185 map1 = isl_map_reverse(map1);
7186 map1 = isl_map_apply_range(map1, map2);
7187 return isl_map_reverse(map1);
7188 error:
7189 isl_map_free(map1);
7190 isl_map_free(map2);
7191 return NULL;
7194 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7195 __isl_take isl_map *map2)
7197 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7200 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7201 __isl_take isl_map *map2)
7203 isl_space *dim_result;
7204 struct isl_map *result;
7205 int i, j;
7207 if (!map1 || !map2)
7208 goto error;
7210 dim_result = isl_space_join(isl_space_copy(map1->dim),
7211 isl_space_copy(map2->dim));
7213 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7214 if (!result)
7215 goto error;
7216 for (i = 0; i < map1->n; ++i)
7217 for (j = 0; j < map2->n; ++j) {
7218 result = isl_map_add_basic_map(result,
7219 isl_basic_map_apply_range(
7220 isl_basic_map_copy(map1->p[i]),
7221 isl_basic_map_copy(map2->p[j])));
7222 if (!result)
7223 goto error;
7225 isl_map_free(map1);
7226 isl_map_free(map2);
7227 if (result && result->n <= 1)
7228 ISL_F_SET(result, ISL_MAP_DISJOINT);
7229 return result;
7230 error:
7231 isl_map_free(map1);
7232 isl_map_free(map2);
7233 return NULL;
7236 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7237 __isl_take isl_map *map2)
7239 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7243 * returns range - domain
7245 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7247 isl_space *dims, *target_dim;
7248 struct isl_basic_set *bset;
7249 unsigned dim;
7250 unsigned nparam;
7251 int i;
7253 if (!bmap)
7254 goto error;
7255 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7256 bmap->dim, isl_dim_out),
7257 goto error);
7258 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7259 dim = isl_basic_map_n_in(bmap);
7260 nparam = isl_basic_map_n_param(bmap);
7261 bset = isl_basic_set_from_basic_map(bmap);
7262 bset = isl_basic_set_cow(bset);
7263 dims = isl_basic_set_get_space(bset);
7264 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7265 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7266 bset = isl_basic_set_swap_vars(bset, 2*dim);
7267 for (i = 0; i < dim; ++i) {
7268 int j = isl_basic_map_alloc_equality(
7269 (struct isl_basic_map *)bset);
7270 if (j < 0) {
7271 bset = isl_basic_set_free(bset);
7272 break;
7274 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7275 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7276 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7277 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7279 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7280 bset = isl_basic_set_reset_space(bset, target_dim);
7281 return bset;
7282 error:
7283 isl_basic_map_free(bmap);
7284 return NULL;
7288 * returns range - domain
7290 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7292 int i;
7293 isl_space *dim;
7294 struct isl_set *result;
7296 if (!map)
7297 return NULL;
7299 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7300 map->dim, isl_dim_out),
7301 goto error);
7302 dim = isl_map_get_space(map);
7303 dim = isl_space_domain(dim);
7304 result = isl_set_alloc_space(dim, map->n, 0);
7305 if (!result)
7306 goto error;
7307 for (i = 0; i < map->n; ++i)
7308 result = isl_set_add_basic_set(result,
7309 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7310 isl_map_free(map);
7311 return result;
7312 error:
7313 isl_map_free(map);
7314 return NULL;
7318 * returns [domain -> range] -> range - domain
7320 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7321 __isl_take isl_basic_map *bmap)
7323 int i, k;
7324 isl_space *dim;
7325 isl_basic_map *domain;
7326 int nparam, n;
7327 unsigned total;
7329 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7330 isl_die(bmap->ctx, isl_error_invalid,
7331 "domain and range don't match", goto error);
7333 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7334 n = isl_basic_map_dim(bmap, isl_dim_in);
7336 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7337 domain = isl_basic_map_universe(dim);
7339 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7340 bmap = isl_basic_map_apply_range(bmap, domain);
7341 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7343 total = isl_basic_map_total_dim(bmap);
7345 for (i = 0; i < n; ++i) {
7346 k = isl_basic_map_alloc_equality(bmap);
7347 if (k < 0)
7348 goto error;
7349 isl_seq_clr(bmap->eq[k], 1 + total);
7350 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7351 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7352 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7355 bmap = isl_basic_map_gauss(bmap, NULL);
7356 return isl_basic_map_finalize(bmap);
7357 error:
7358 isl_basic_map_free(bmap);
7359 return NULL;
7363 * returns [domain -> range] -> range - domain
7365 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7367 int i;
7368 isl_space *domain_dim;
7370 if (!map)
7371 return NULL;
7373 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7374 isl_die(map->ctx, isl_error_invalid,
7375 "domain and range don't match", goto error);
7377 map = isl_map_cow(map);
7378 if (!map)
7379 return NULL;
7381 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7382 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7383 map->dim = isl_space_join(map->dim, domain_dim);
7384 if (!map->dim)
7385 goto error;
7386 for (i = 0; i < map->n; ++i) {
7387 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7388 if (!map->p[i])
7389 goto error;
7391 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7392 return map;
7393 error:
7394 isl_map_free(map);
7395 return NULL;
7398 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7400 struct isl_basic_map *bmap;
7401 unsigned nparam;
7402 unsigned dim;
7403 int i;
7405 if (!dims)
7406 return NULL;
7408 nparam = dims->nparam;
7409 dim = dims->n_out;
7410 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7411 if (!bmap)
7412 goto error;
7414 for (i = 0; i < dim; ++i) {
7415 int j = isl_basic_map_alloc_equality(bmap);
7416 if (j < 0)
7417 goto error;
7418 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7419 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7420 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7422 return isl_basic_map_finalize(bmap);
7423 error:
7424 isl_basic_map_free(bmap);
7425 return NULL;
7428 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7430 if (!dim)
7431 return NULL;
7432 if (dim->n_in != dim->n_out)
7433 isl_die(dim->ctx, isl_error_invalid,
7434 "number of input and output dimensions needs to be "
7435 "the same", goto error);
7436 return basic_map_identity(dim);
7437 error:
7438 isl_space_free(dim);
7439 return NULL;
7442 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7444 if (!model || !model->dim)
7445 return NULL;
7446 return isl_basic_map_identity(isl_space_copy(model->dim));
7449 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7451 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7454 struct isl_map *isl_map_identity_like(struct isl_map *model)
7456 if (!model || !model->dim)
7457 return NULL;
7458 return isl_map_identity(isl_space_copy(model->dim));
7461 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7463 if (!model || !model->dim)
7464 return NULL;
7465 return isl_map_identity(isl_space_copy(model->dim));
7468 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7470 isl_space *dim = isl_set_get_space(set);
7471 isl_map *id;
7472 id = isl_map_identity(isl_space_map_from_set(dim));
7473 return isl_map_intersect_range(id, set);
7476 /* Construct a basic set with all set dimensions having only non-negative
7477 * values.
7479 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7480 __isl_take isl_space *space)
7482 int i;
7483 unsigned nparam;
7484 unsigned dim;
7485 struct isl_basic_set *bset;
7487 if (!space)
7488 return NULL;
7489 nparam = space->nparam;
7490 dim = space->n_out;
7491 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7492 if (!bset)
7493 return NULL;
7494 for (i = 0; i < dim; ++i) {
7495 int k = isl_basic_set_alloc_inequality(bset);
7496 if (k < 0)
7497 goto error;
7498 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7499 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7501 return bset;
7502 error:
7503 isl_basic_set_free(bset);
7504 return NULL;
7507 /* Construct the half-space x_pos >= 0.
7509 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7510 int pos)
7512 int k;
7513 isl_basic_set *nonneg;
7515 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7516 k = isl_basic_set_alloc_inequality(nonneg);
7517 if (k < 0)
7518 goto error;
7519 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7520 isl_int_set_si(nonneg->ineq[k][pos], 1);
7522 return isl_basic_set_finalize(nonneg);
7523 error:
7524 isl_basic_set_free(nonneg);
7525 return NULL;
7528 /* Construct the half-space x_pos <= -1.
7530 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7532 int k;
7533 isl_basic_set *neg;
7535 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7536 k = isl_basic_set_alloc_inequality(neg);
7537 if (k < 0)
7538 goto error;
7539 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7540 isl_int_set_si(neg->ineq[k][0], -1);
7541 isl_int_set_si(neg->ineq[k][pos], -1);
7543 return isl_basic_set_finalize(neg);
7544 error:
7545 isl_basic_set_free(neg);
7546 return NULL;
7549 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7550 enum isl_dim_type type, unsigned first, unsigned n)
7552 int i;
7553 isl_basic_set *nonneg;
7554 isl_basic_set *neg;
7556 if (!set)
7557 return NULL;
7558 if (n == 0)
7559 return set;
7561 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7563 for (i = 0; i < n; ++i) {
7564 nonneg = nonneg_halfspace(isl_set_get_space(set),
7565 pos(set->dim, type) + first + i);
7566 neg = neg_halfspace(isl_set_get_space(set),
7567 pos(set->dim, type) + first + i);
7569 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7572 return set;
7573 error:
7574 isl_set_free(set);
7575 return NULL;
7578 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7579 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7580 void *user)
7582 isl_set *half;
7584 if (!set)
7585 return -1;
7586 if (isl_set_plain_is_empty(set)) {
7587 isl_set_free(set);
7588 return 0;
7590 if (first == len)
7591 return fn(set, signs, user);
7593 signs[first] = 1;
7594 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7595 1 + first));
7596 half = isl_set_intersect(half, isl_set_copy(set));
7597 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7598 goto error;
7600 signs[first] = -1;
7601 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7602 1 + first));
7603 half = isl_set_intersect(half, set);
7604 return foreach_orthant(half, signs, first + 1, len, fn, user);
7605 error:
7606 isl_set_free(set);
7607 return -1;
7610 /* Call "fn" on the intersections of "set" with each of the orthants
7611 * (except for obviously empty intersections). The orthant is identified
7612 * by the signs array, with each entry having value 1 or -1 according
7613 * to the sign of the corresponding variable.
7615 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7616 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7617 void *user)
7619 unsigned nparam;
7620 unsigned nvar;
7621 int *signs;
7622 int r;
7624 if (!set)
7625 return -1;
7626 if (isl_set_plain_is_empty(set))
7627 return 0;
7629 nparam = isl_set_dim(set, isl_dim_param);
7630 nvar = isl_set_dim(set, isl_dim_set);
7632 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7634 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7635 fn, user);
7637 free(signs);
7639 return r;
7642 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7644 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7647 int isl_basic_map_is_subset(
7648 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7650 int is_subset;
7651 struct isl_map *map1;
7652 struct isl_map *map2;
7654 if (!bmap1 || !bmap2)
7655 return -1;
7657 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7658 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7660 is_subset = isl_map_is_subset(map1, map2);
7662 isl_map_free(map1);
7663 isl_map_free(map2);
7665 return is_subset;
7668 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7669 __isl_keep isl_basic_set *bset2)
7671 return isl_basic_map_is_subset(bset1, bset2);
7674 int isl_basic_map_is_equal(
7675 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7677 int is_subset;
7679 if (!bmap1 || !bmap2)
7680 return -1;
7681 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7682 if (is_subset != 1)
7683 return is_subset;
7684 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7685 return is_subset;
7688 int isl_basic_set_is_equal(
7689 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7691 return isl_basic_map_is_equal(
7692 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7695 int isl_map_is_empty(struct isl_map *map)
7697 int i;
7698 int is_empty;
7700 if (!map)
7701 return -1;
7702 for (i = 0; i < map->n; ++i) {
7703 is_empty = isl_basic_map_is_empty(map->p[i]);
7704 if (is_empty < 0)
7705 return -1;
7706 if (!is_empty)
7707 return 0;
7709 return 1;
7712 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7714 return map ? map->n == 0 : -1;
7717 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7719 return isl_map_plain_is_empty(map);
7722 int isl_set_plain_is_empty(struct isl_set *set)
7724 return set ? set->n == 0 : -1;
7727 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7729 return isl_set_plain_is_empty(set);
7732 int isl_set_is_empty(struct isl_set *set)
7734 return isl_map_is_empty((struct isl_map *)set);
7737 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7739 if (!map1 || !map2)
7740 return -1;
7742 return isl_space_is_equal(map1->dim, map2->dim);
7745 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7747 if (!set1 || !set2)
7748 return -1;
7750 return isl_space_is_equal(set1->dim, set2->dim);
7753 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7755 int is_subset;
7757 if (!map1 || !map2)
7758 return -1;
7759 is_subset = isl_map_is_subset(map1, map2);
7760 if (is_subset != 1)
7761 return is_subset;
7762 is_subset = isl_map_is_subset(map2, map1);
7763 return is_subset;
7766 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7768 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7771 int isl_basic_map_is_strict_subset(
7772 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7774 int is_subset;
7776 if (!bmap1 || !bmap2)
7777 return -1;
7778 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7779 if (is_subset != 1)
7780 return is_subset;
7781 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7782 if (is_subset == -1)
7783 return is_subset;
7784 return !is_subset;
7787 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7789 int is_subset;
7791 if (!map1 || !map2)
7792 return -1;
7793 is_subset = isl_map_is_subset(map1, map2);
7794 if (is_subset != 1)
7795 return is_subset;
7796 is_subset = isl_map_is_subset(map2, map1);
7797 if (is_subset == -1)
7798 return is_subset;
7799 return !is_subset;
7802 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7804 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7807 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7809 if (!bmap)
7810 return -1;
7811 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7814 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7816 if (!bset)
7817 return -1;
7818 return bset->n_eq == 0 && bset->n_ineq == 0;
7821 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7823 int i;
7825 if (!map)
7826 return -1;
7828 for (i = 0; i < map->n; ++i) {
7829 int r = isl_basic_map_is_universe(map->p[i]);
7830 if (r < 0 || r)
7831 return r;
7834 return 0;
7837 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7839 return isl_map_plain_is_universe((isl_map *) set);
7842 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7844 return isl_set_plain_is_universe(set);
7847 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7849 struct isl_basic_set *bset = NULL;
7850 struct isl_vec *sample = NULL;
7851 int empty;
7852 unsigned total;
7854 if (!bmap)
7855 return -1;
7857 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7858 return 1;
7860 if (isl_basic_map_is_universe(bmap))
7861 return 0;
7863 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7864 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7865 copy = isl_basic_map_remove_redundancies(copy);
7866 empty = isl_basic_map_plain_is_empty(copy);
7867 isl_basic_map_free(copy);
7868 return empty;
7871 total = 1 + isl_basic_map_total_dim(bmap);
7872 if (bmap->sample && bmap->sample->size == total) {
7873 int contains = isl_basic_map_contains(bmap, bmap->sample);
7874 if (contains < 0)
7875 return -1;
7876 if (contains)
7877 return 0;
7879 isl_vec_free(bmap->sample);
7880 bmap->sample = NULL;
7881 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7882 if (!bset)
7883 return -1;
7884 sample = isl_basic_set_sample_vec(bset);
7885 if (!sample)
7886 return -1;
7887 empty = sample->size == 0;
7888 isl_vec_free(bmap->sample);
7889 bmap->sample = sample;
7890 if (empty)
7891 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7893 return empty;
7896 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7898 if (!bmap)
7899 return -1;
7900 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7903 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7905 return isl_basic_map_plain_is_empty(bmap);
7908 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7910 if (!bset)
7911 return -1;
7912 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7915 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7917 return isl_basic_set_plain_is_empty(bset);
7920 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7922 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7925 struct isl_map *isl_basic_map_union(
7926 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7928 struct isl_map *map;
7929 if (!bmap1 || !bmap2)
7930 goto error;
7932 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7934 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7935 if (!map)
7936 goto error;
7937 map = isl_map_add_basic_map(map, bmap1);
7938 map = isl_map_add_basic_map(map, bmap2);
7939 return map;
7940 error:
7941 isl_basic_map_free(bmap1);
7942 isl_basic_map_free(bmap2);
7943 return NULL;
7946 struct isl_set *isl_basic_set_union(
7947 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7949 return (struct isl_set *)isl_basic_map_union(
7950 (struct isl_basic_map *)bset1,
7951 (struct isl_basic_map *)bset2);
7954 /* Order divs such that any div only depends on previous divs */
7955 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7957 int i;
7958 unsigned off;
7960 if (!bmap)
7961 return NULL;
7963 off = isl_space_dim(bmap->dim, isl_dim_all);
7965 for (i = 0; i < bmap->n_div; ++i) {
7966 int pos;
7967 if (isl_int_is_zero(bmap->div[i][0]))
7968 continue;
7969 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7970 bmap->n_div-i);
7971 if (pos == -1)
7972 continue;
7973 isl_basic_map_swap_div(bmap, i, i + pos);
7974 --i;
7976 return bmap;
7979 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7981 return (struct isl_basic_set *)
7982 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7985 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7987 int i;
7989 if (!map)
7990 return 0;
7992 for (i = 0; i < map->n; ++i) {
7993 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7994 if (!map->p[i])
7995 goto error;
7998 return map;
7999 error:
8000 isl_map_free(map);
8001 return NULL;
8004 /* Apply the expansion computed by isl_merge_divs.
8005 * The expansion itself is given by "exp" while the resulting
8006 * list of divs is given by "div".
8008 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8009 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8011 int i, j;
8012 int n_div;
8014 bset = isl_basic_set_cow(bset);
8015 if (!bset || !div)
8016 goto error;
8018 if (div->n_row < bset->n_div)
8019 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8020 "not an expansion", goto error);
8022 n_div = bset->n_div;
8023 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8024 div->n_row - n_div, 0,
8025 2 * (div->n_row - n_div));
8027 for (i = n_div; i < div->n_row; ++i)
8028 if (isl_basic_set_alloc_div(bset) < 0)
8029 goto error;
8031 j = n_div - 1;
8032 for (i = div->n_row - 1; i >= 0; --i) {
8033 if (j >= 0 && exp[j] == i) {
8034 if (i != j)
8035 isl_basic_map_swap_div(bset, i, j);
8036 j--;
8037 } else {
8038 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8039 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8040 goto error;
8044 isl_mat_free(div);
8045 return bset;
8046 error:
8047 isl_basic_set_free(bset);
8048 isl_mat_free(div);
8049 return NULL;
8052 /* Look for a div in dst that corresponds to the div "div" in src.
8053 * The divs before "div" in src and dst are assumed to be the same.
8055 * Returns -1 if no corresponding div was found and the position
8056 * of the corresponding div in dst otherwise.
8058 static int find_div(struct isl_basic_map *dst,
8059 struct isl_basic_map *src, unsigned div)
8061 int i;
8063 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8065 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8066 for (i = div; i < dst->n_div; ++i)
8067 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8068 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8069 dst->n_div - div) == -1)
8070 return i;
8071 return -1;
8074 struct isl_basic_map *isl_basic_map_align_divs(
8075 struct isl_basic_map *dst, struct isl_basic_map *src)
8077 int i;
8078 unsigned total;
8080 if (!dst || !src)
8081 goto error;
8083 if (src->n_div == 0)
8084 return dst;
8086 for (i = 0; i < src->n_div; ++i)
8087 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
8089 src = isl_basic_map_order_divs(src);
8090 dst = isl_basic_map_cow(dst);
8091 if (!dst)
8092 return NULL;
8093 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
8094 src->n_div, 0, 2 * src->n_div);
8095 if (!dst)
8096 return NULL;
8097 total = isl_space_dim(src->dim, isl_dim_all);
8098 for (i = 0; i < src->n_div; ++i) {
8099 int j = find_div(dst, src, i);
8100 if (j < 0) {
8101 j = isl_basic_map_alloc_div(dst);
8102 if (j < 0)
8103 goto error;
8104 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8105 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8106 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8107 goto error;
8109 if (j != i)
8110 isl_basic_map_swap_div(dst, i, j);
8112 return dst;
8113 error:
8114 isl_basic_map_free(dst);
8115 return NULL;
8118 struct isl_basic_set *isl_basic_set_align_divs(
8119 struct isl_basic_set *dst, struct isl_basic_set *src)
8121 return (struct isl_basic_set *)isl_basic_map_align_divs(
8122 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8125 struct isl_map *isl_map_align_divs(struct isl_map *map)
8127 int i;
8129 if (!map)
8130 return NULL;
8131 if (map->n == 0)
8132 return map;
8133 map = isl_map_compute_divs(map);
8134 map = isl_map_cow(map);
8135 if (!map)
8136 return NULL;
8138 for (i = 1; i < map->n; ++i)
8139 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8140 for (i = 1; i < map->n; ++i) {
8141 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8142 if (!map->p[i])
8143 return isl_map_free(map);
8146 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8147 return map;
8150 struct isl_set *isl_set_align_divs(struct isl_set *set)
8152 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8155 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8156 __isl_take isl_map *map)
8158 if (!set || !map)
8159 goto error;
8160 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8161 map = isl_map_intersect_domain(map, set);
8162 set = isl_map_range(map);
8163 return set;
8164 error:
8165 isl_set_free(set);
8166 isl_map_free(map);
8167 return NULL;
8170 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8171 __isl_take isl_map *map)
8173 return isl_map_align_params_map_map_and(set, map, &set_apply);
8176 /* There is no need to cow as removing empty parts doesn't change
8177 * the meaning of the set.
8179 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8181 int i;
8183 if (!map)
8184 return NULL;
8186 for (i = map->n - 1; i >= 0; --i)
8187 remove_if_empty(map, i);
8189 return map;
8192 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8194 return (struct isl_set *)
8195 isl_map_remove_empty_parts((struct isl_map *)set);
8198 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8200 struct isl_basic_map *bmap;
8201 if (!map || map->n == 0)
8202 return NULL;
8203 bmap = map->p[map->n-1];
8204 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8205 return isl_basic_map_copy(bmap);
8208 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8210 return (struct isl_basic_set *)
8211 isl_map_copy_basic_map((struct isl_map *)set);
8214 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8215 __isl_keep isl_basic_map *bmap)
8217 int i;
8219 if (!map || !bmap)
8220 goto error;
8221 for (i = map->n-1; i >= 0; --i) {
8222 if (map->p[i] != bmap)
8223 continue;
8224 map = isl_map_cow(map);
8225 if (!map)
8226 goto error;
8227 isl_basic_map_free(map->p[i]);
8228 if (i != map->n-1) {
8229 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8230 map->p[i] = map->p[map->n-1];
8232 map->n--;
8233 return map;
8235 return map;
8236 error:
8237 isl_map_free(map);
8238 return NULL;
8241 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8242 struct isl_basic_set *bset)
8244 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8245 (struct isl_basic_map *)bset);
8248 /* Given two basic sets bset1 and bset2, compute the maximal difference
8249 * between the values of dimension pos in bset1 and those in bset2
8250 * for any common value of the parameters and dimensions preceding pos.
8252 static enum isl_lp_result basic_set_maximal_difference_at(
8253 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8254 int pos, isl_int *opt)
8256 isl_space *dims;
8257 struct isl_basic_map *bmap1 = NULL;
8258 struct isl_basic_map *bmap2 = NULL;
8259 struct isl_ctx *ctx;
8260 struct isl_vec *obj;
8261 unsigned total;
8262 unsigned nparam;
8263 unsigned dim1, dim2;
8264 enum isl_lp_result res;
8266 if (!bset1 || !bset2)
8267 return isl_lp_error;
8269 nparam = isl_basic_set_n_param(bset1);
8270 dim1 = isl_basic_set_n_dim(bset1);
8271 dim2 = isl_basic_set_n_dim(bset2);
8272 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8273 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8274 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8275 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8276 if (!bmap1 || !bmap2)
8277 goto error;
8278 bmap1 = isl_basic_map_cow(bmap1);
8279 bmap1 = isl_basic_map_extend(bmap1, nparam,
8280 pos, (dim1 - pos) + (dim2 - pos),
8281 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8282 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8283 if (!bmap1)
8284 goto error;
8285 total = isl_basic_map_total_dim(bmap1);
8286 ctx = bmap1->ctx;
8287 obj = isl_vec_alloc(ctx, 1 + total);
8288 if (!obj)
8289 goto error2;
8290 isl_seq_clr(obj->block.data, 1 + total);
8291 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8292 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8293 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8294 opt, NULL, NULL);
8295 isl_basic_map_free(bmap1);
8296 isl_vec_free(obj);
8297 return res;
8298 error:
8299 isl_basic_map_free(bmap2);
8300 error2:
8301 isl_basic_map_free(bmap1);
8302 return isl_lp_error;
8305 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8306 * for any common value of the parameters and dimensions preceding pos
8307 * in both basic sets, the values of dimension pos in bset1 are
8308 * smaller or larger than those in bset2.
8310 * Returns
8311 * 1 if bset1 follows bset2
8312 * -1 if bset1 precedes bset2
8313 * 0 if bset1 and bset2 are incomparable
8314 * -2 if some error occurred.
8316 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8317 struct isl_basic_set *bset2, int pos)
8319 isl_int opt;
8320 enum isl_lp_result res;
8321 int cmp;
8323 isl_int_init(opt);
8325 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8327 if (res == isl_lp_empty)
8328 cmp = 0;
8329 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8330 res == isl_lp_unbounded)
8331 cmp = 1;
8332 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8333 cmp = -1;
8334 else
8335 cmp = -2;
8337 isl_int_clear(opt);
8338 return cmp;
8341 /* Given two basic sets bset1 and bset2, check whether
8342 * for any common value of the parameters and dimensions preceding pos
8343 * there is a value of dimension pos in bset1 that is larger
8344 * than a value of the same dimension in bset2.
8346 * Return
8347 * 1 if there exists such a pair
8348 * 0 if there is no such pair, but there is a pair of equal values
8349 * -1 otherwise
8350 * -2 if some error occurred.
8352 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8353 __isl_keep isl_basic_set *bset2, int pos)
8355 isl_int opt;
8356 enum isl_lp_result res;
8357 int cmp;
8359 isl_int_init(opt);
8361 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8363 if (res == isl_lp_empty)
8364 cmp = -1;
8365 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8366 res == isl_lp_unbounded)
8367 cmp = 1;
8368 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8369 cmp = -1;
8370 else if (res == isl_lp_ok)
8371 cmp = 0;
8372 else
8373 cmp = -2;
8375 isl_int_clear(opt);
8376 return cmp;
8379 /* Given two sets set1 and set2, check whether
8380 * for any common value of the parameters and dimensions preceding pos
8381 * there is a value of dimension pos in set1 that is larger
8382 * than a value of the same dimension in set2.
8384 * Return
8385 * 1 if there exists such a pair
8386 * 0 if there is no such pair, but there is a pair of equal values
8387 * -1 otherwise
8388 * -2 if some error occurred.
8390 int isl_set_follows_at(__isl_keep isl_set *set1,
8391 __isl_keep isl_set *set2, int pos)
8393 int i, j;
8394 int follows = -1;
8396 if (!set1 || !set2)
8397 return -2;
8399 for (i = 0; i < set1->n; ++i)
8400 for (j = 0; j < set2->n; ++j) {
8401 int f;
8402 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8403 if (f == 1 || f == -2)
8404 return f;
8405 if (f > follows)
8406 follows = f;
8409 return follows;
8412 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8413 unsigned pos, isl_int *val)
8415 int i;
8416 int d;
8417 unsigned total;
8419 if (!bmap)
8420 return -1;
8421 total = isl_basic_map_total_dim(bmap);
8422 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8423 for (; d+1 > pos; --d)
8424 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8425 break;
8426 if (d != pos)
8427 continue;
8428 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8429 return 0;
8430 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8431 return 0;
8432 if (!isl_int_is_one(bmap->eq[i][1+d]))
8433 return 0;
8434 if (val)
8435 isl_int_neg(*val, bmap->eq[i][0]);
8436 return 1;
8438 return 0;
8441 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8442 unsigned pos, isl_int *val)
8444 int i;
8445 isl_int v;
8446 isl_int tmp;
8447 int fixed;
8449 if (!map)
8450 return -1;
8451 if (map->n == 0)
8452 return 0;
8453 if (map->n == 1)
8454 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8455 isl_int_init(v);
8456 isl_int_init(tmp);
8457 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8458 for (i = 1; fixed == 1 && i < map->n; ++i) {
8459 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8460 if (fixed == 1 && isl_int_ne(tmp, v))
8461 fixed = 0;
8463 if (val)
8464 isl_int_set(*val, v);
8465 isl_int_clear(tmp);
8466 isl_int_clear(v);
8467 return fixed;
8470 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8471 unsigned pos, isl_int *val)
8473 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8474 pos, val);
8477 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8478 isl_int *val)
8480 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8483 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8484 enum isl_dim_type type, unsigned pos, isl_int *val)
8486 if (pos >= isl_basic_map_dim(bmap, type))
8487 return -1;
8488 return isl_basic_map_plain_has_fixed_var(bmap,
8489 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8492 /* If "bmap" obviously lies on a hyperplane where the given dimension
8493 * has a fixed value, then return that value.
8494 * Otherwise return NaN.
8496 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8497 __isl_keep isl_basic_map *bmap,
8498 enum isl_dim_type type, unsigned pos)
8500 isl_ctx *ctx;
8501 isl_val *v;
8502 int fixed;
8504 if (!bmap)
8505 return NULL;
8506 ctx = isl_basic_map_get_ctx(bmap);
8507 v = isl_val_alloc(ctx);
8508 if (!v)
8509 return NULL;
8510 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8511 if (fixed < 0)
8512 return isl_val_free(v);
8513 if (fixed) {
8514 isl_int_set_si(v->d, 1);
8515 return v;
8517 isl_val_free(v);
8518 return isl_val_nan(ctx);
8521 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8522 enum isl_dim_type type, unsigned pos, isl_int *val)
8524 if (pos >= isl_map_dim(map, type))
8525 return -1;
8526 return isl_map_plain_has_fixed_var(map,
8527 map_offset(map, type) - 1 + pos, val);
8530 /* If "map" obviously lies on a hyperplane where the given dimension
8531 * has a fixed value, then return that value.
8532 * Otherwise return NaN.
8534 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8535 enum isl_dim_type type, unsigned pos)
8537 isl_ctx *ctx;
8538 isl_val *v;
8539 int fixed;
8541 if (!map)
8542 return NULL;
8543 ctx = isl_map_get_ctx(map);
8544 v = isl_val_alloc(ctx);
8545 if (!v)
8546 return NULL;
8547 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8548 if (fixed < 0)
8549 return isl_val_free(v);
8550 if (fixed) {
8551 isl_int_set_si(v->d, 1);
8552 return v;
8554 isl_val_free(v);
8555 return isl_val_nan(ctx);
8558 /* If "set" obviously lies on a hyperplane where the given dimension
8559 * has a fixed value, then return that value.
8560 * Otherwise return NaN.
8562 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8563 enum isl_dim_type type, unsigned pos)
8565 return isl_map_plain_get_val_if_fixed(set, type, pos);
8568 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8569 enum isl_dim_type type, unsigned pos, isl_int *val)
8571 return isl_map_plain_is_fixed(set, type, pos, val);
8574 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8575 enum isl_dim_type type, unsigned pos, isl_int *val)
8577 return isl_map_plain_is_fixed(map, type, pos, val);
8580 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8581 * then return this fixed value in *val.
8583 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8584 unsigned dim, isl_int *val)
8586 return isl_basic_set_plain_has_fixed_var(bset,
8587 isl_basic_set_n_param(bset) + dim, val);
8590 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8591 * then return this fixed value in *val.
8593 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8594 unsigned dim, isl_int *val)
8596 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8599 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8600 unsigned dim, isl_int *val)
8602 return isl_set_plain_dim_is_fixed(set, dim, val);
8605 /* Check if input variable in has fixed value and if so and if val is not NULL,
8606 * then return this fixed value in *val.
8608 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8609 unsigned in, isl_int *val)
8611 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8614 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8615 * and if val is not NULL, then return this lower bound in *val.
8617 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8618 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8620 int i, i_eq = -1, i_ineq = -1;
8621 isl_int *c;
8622 unsigned total;
8623 unsigned nparam;
8625 if (!bset)
8626 return -1;
8627 total = isl_basic_set_total_dim(bset);
8628 nparam = isl_basic_set_n_param(bset);
8629 for (i = 0; i < bset->n_eq; ++i) {
8630 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8631 continue;
8632 if (i_eq != -1)
8633 return 0;
8634 i_eq = i;
8636 for (i = 0; i < bset->n_ineq; ++i) {
8637 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8638 continue;
8639 if (i_eq != -1 || i_ineq != -1)
8640 return 0;
8641 i_ineq = i;
8643 if (i_eq == -1 && i_ineq == -1)
8644 return 0;
8645 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8646 /* The coefficient should always be one due to normalization. */
8647 if (!isl_int_is_one(c[1+nparam+dim]))
8648 return 0;
8649 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8650 return 0;
8651 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8652 total - nparam - dim - 1) != -1)
8653 return 0;
8654 if (val)
8655 isl_int_neg(*val, c[0]);
8656 return 1;
8659 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8660 unsigned dim, isl_int *val)
8662 int i;
8663 isl_int v;
8664 isl_int tmp;
8665 int fixed;
8667 if (!set)
8668 return -1;
8669 if (set->n == 0)
8670 return 0;
8671 if (set->n == 1)
8672 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8673 dim, val);
8674 isl_int_init(v);
8675 isl_int_init(tmp);
8676 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8677 dim, &v);
8678 for (i = 1; fixed == 1 && i < set->n; ++i) {
8679 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8680 dim, &tmp);
8681 if (fixed == 1 && isl_int_ne(tmp, v))
8682 fixed = 0;
8684 if (val)
8685 isl_int_set(*val, v);
8686 isl_int_clear(tmp);
8687 isl_int_clear(v);
8688 return fixed;
8691 struct constraint {
8692 unsigned size;
8693 isl_int *c;
8696 /* uset_gist depends on constraints without existentially quantified
8697 * variables sorting first.
8699 static int qsort_constraint_cmp(const void *p1, const void *p2)
8701 const struct constraint *c1 = (const struct constraint *)p1;
8702 const struct constraint *c2 = (const struct constraint *)p2;
8703 int l1, l2;
8704 unsigned size = isl_min(c1->size, c2->size);
8706 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8707 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8709 if (l1 != l2)
8710 return l1 - l2;
8712 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8715 static struct isl_basic_map *isl_basic_map_sort_constraints(
8716 struct isl_basic_map *bmap)
8718 int i;
8719 struct constraint *c;
8720 unsigned total;
8722 if (!bmap)
8723 return NULL;
8724 if (bmap->n_ineq == 0)
8725 return bmap;
8726 total = isl_basic_map_total_dim(bmap);
8727 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8728 if (!c)
8729 goto error;
8730 for (i = 0; i < bmap->n_ineq; ++i) {
8731 c[i].size = total;
8732 c[i].c = bmap->ineq[i];
8734 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8735 for (i = 0; i < bmap->n_ineq; ++i)
8736 bmap->ineq[i] = c[i].c;
8737 free(c);
8738 return bmap;
8739 error:
8740 isl_basic_map_free(bmap);
8741 return NULL;
8744 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8745 __isl_take isl_basic_set *bset)
8747 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8748 (struct isl_basic_map *)bset);
8751 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8753 if (!bmap)
8754 return NULL;
8755 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8756 return bmap;
8757 bmap = isl_basic_map_remove_redundancies(bmap);
8758 bmap = isl_basic_map_sort_constraints(bmap);
8759 if (bmap)
8760 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8761 return bmap;
8764 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8766 return (struct isl_basic_set *)isl_basic_map_normalize(
8767 (struct isl_basic_map *)bset);
8770 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8771 const __isl_keep isl_basic_map *bmap2)
8773 int i, cmp;
8774 unsigned total;
8776 if (bmap1 == bmap2)
8777 return 0;
8778 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8779 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8780 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8781 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8782 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8783 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8784 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8785 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8786 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8787 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8788 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8789 return 0;
8790 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8791 return 1;
8792 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8793 return -1;
8794 if (bmap1->n_eq != bmap2->n_eq)
8795 return bmap1->n_eq - bmap2->n_eq;
8796 if (bmap1->n_ineq != bmap2->n_ineq)
8797 return bmap1->n_ineq - bmap2->n_ineq;
8798 if (bmap1->n_div != bmap2->n_div)
8799 return bmap1->n_div - bmap2->n_div;
8800 total = isl_basic_map_total_dim(bmap1);
8801 for (i = 0; i < bmap1->n_eq; ++i) {
8802 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8803 if (cmp)
8804 return cmp;
8806 for (i = 0; i < bmap1->n_ineq; ++i) {
8807 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8808 if (cmp)
8809 return cmp;
8811 for (i = 0; i < bmap1->n_div; ++i) {
8812 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8813 if (cmp)
8814 return cmp;
8816 return 0;
8819 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8820 const __isl_keep isl_basic_set *bset2)
8822 return isl_basic_map_plain_cmp(bset1, bset2);
8825 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8827 int i, cmp;
8829 if (set1 == set2)
8830 return 0;
8831 if (set1->n != set2->n)
8832 return set1->n - set2->n;
8834 for (i = 0; i < set1->n; ++i) {
8835 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8836 if (cmp)
8837 return cmp;
8840 return 0;
8843 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8844 __isl_keep isl_basic_map *bmap2)
8846 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8849 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8850 __isl_keep isl_basic_set *bset2)
8852 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8853 (isl_basic_map *)bset2);
8856 static int qsort_bmap_cmp(const void *p1, const void *p2)
8858 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8859 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8861 return isl_basic_map_plain_cmp(bmap1, bmap2);
8864 /* We normalize in place, but if anything goes wrong we need
8865 * to return NULL, so we need to make sure we don't change the
8866 * meaning of any possible other copies of map.
8868 struct isl_map *isl_map_normalize(struct isl_map *map)
8870 int i, j;
8871 struct isl_basic_map *bmap;
8873 if (!map)
8874 return NULL;
8875 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8876 return map;
8877 for (i = 0; i < map->n; ++i) {
8878 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8879 if (!bmap)
8880 goto error;
8881 isl_basic_map_free(map->p[i]);
8882 map->p[i] = bmap;
8884 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8885 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8886 map = isl_map_remove_empty_parts(map);
8887 if (!map)
8888 return NULL;
8889 for (i = map->n - 1; i >= 1; --i) {
8890 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8891 continue;
8892 isl_basic_map_free(map->p[i-1]);
8893 for (j = i; j < map->n; ++j)
8894 map->p[j-1] = map->p[j];
8895 map->n--;
8897 return map;
8898 error:
8899 isl_map_free(map);
8900 return NULL;
8904 struct isl_set *isl_set_normalize(struct isl_set *set)
8906 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8909 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8911 int i;
8912 int equal;
8914 if (!map1 || !map2)
8915 return -1;
8917 if (map1 == map2)
8918 return 1;
8919 if (!isl_space_is_equal(map1->dim, map2->dim))
8920 return 0;
8922 map1 = isl_map_copy(map1);
8923 map2 = isl_map_copy(map2);
8924 map1 = isl_map_normalize(map1);
8925 map2 = isl_map_normalize(map2);
8926 if (!map1 || !map2)
8927 goto error;
8928 equal = map1->n == map2->n;
8929 for (i = 0; equal && i < map1->n; ++i) {
8930 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8931 if (equal < 0)
8932 goto error;
8934 isl_map_free(map1);
8935 isl_map_free(map2);
8936 return equal;
8937 error:
8938 isl_map_free(map1);
8939 isl_map_free(map2);
8940 return -1;
8943 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8945 return isl_map_plain_is_equal(map1, map2);
8948 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8950 return isl_map_plain_is_equal((struct isl_map *)set1,
8951 (struct isl_map *)set2);
8954 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8956 return isl_set_plain_is_equal(set1, set2);
8959 /* Return an interval that ranges from min to max (inclusive)
8961 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8962 isl_int min, isl_int max)
8964 int k;
8965 struct isl_basic_set *bset = NULL;
8967 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8968 if (!bset)
8969 goto error;
8971 k = isl_basic_set_alloc_inequality(bset);
8972 if (k < 0)
8973 goto error;
8974 isl_int_set_si(bset->ineq[k][1], 1);
8975 isl_int_neg(bset->ineq[k][0], min);
8977 k = isl_basic_set_alloc_inequality(bset);
8978 if (k < 0)
8979 goto error;
8980 isl_int_set_si(bset->ineq[k][1], -1);
8981 isl_int_set(bset->ineq[k][0], max);
8983 return bset;
8984 error:
8985 isl_basic_set_free(bset);
8986 return NULL;
8989 /* Return the Cartesian product of the basic sets in list (in the given order).
8991 __isl_give isl_basic_set *isl_basic_set_list_product(
8992 __isl_take struct isl_basic_set_list *list)
8994 int i;
8995 unsigned dim;
8996 unsigned nparam;
8997 unsigned extra;
8998 unsigned n_eq;
8999 unsigned n_ineq;
9000 struct isl_basic_set *product = NULL;
9002 if (!list)
9003 goto error;
9004 isl_assert(list->ctx, list->n > 0, goto error);
9005 isl_assert(list->ctx, list->p[0], goto error);
9006 nparam = isl_basic_set_n_param(list->p[0]);
9007 dim = isl_basic_set_n_dim(list->p[0]);
9008 extra = list->p[0]->n_div;
9009 n_eq = list->p[0]->n_eq;
9010 n_ineq = list->p[0]->n_ineq;
9011 for (i = 1; i < list->n; ++i) {
9012 isl_assert(list->ctx, list->p[i], goto error);
9013 isl_assert(list->ctx,
9014 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9015 dim += isl_basic_set_n_dim(list->p[i]);
9016 extra += list->p[i]->n_div;
9017 n_eq += list->p[i]->n_eq;
9018 n_ineq += list->p[i]->n_ineq;
9020 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9021 n_eq, n_ineq);
9022 if (!product)
9023 goto error;
9024 dim = 0;
9025 for (i = 0; i < list->n; ++i) {
9026 isl_basic_set_add_constraints(product,
9027 isl_basic_set_copy(list->p[i]), dim);
9028 dim += isl_basic_set_n_dim(list->p[i]);
9030 isl_basic_set_list_free(list);
9031 return product;
9032 error:
9033 isl_basic_set_free(product);
9034 isl_basic_set_list_free(list);
9035 return NULL;
9038 struct isl_basic_map *isl_basic_map_product(
9039 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9041 isl_space *dim_result = NULL;
9042 struct isl_basic_map *bmap;
9043 unsigned in1, in2, out1, out2, nparam, total, pos;
9044 struct isl_dim_map *dim_map1, *dim_map2;
9046 if (!bmap1 || !bmap2)
9047 goto error;
9049 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9050 bmap2->dim, isl_dim_param), goto error);
9051 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9052 isl_space_copy(bmap2->dim));
9054 in1 = isl_basic_map_n_in(bmap1);
9055 in2 = isl_basic_map_n_in(bmap2);
9056 out1 = isl_basic_map_n_out(bmap1);
9057 out2 = isl_basic_map_n_out(bmap2);
9058 nparam = isl_basic_map_n_param(bmap1);
9060 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9061 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9062 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9063 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9064 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9065 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9066 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9067 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9068 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9069 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9070 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9072 bmap = isl_basic_map_alloc_space(dim_result,
9073 bmap1->n_div + bmap2->n_div,
9074 bmap1->n_eq + bmap2->n_eq,
9075 bmap1->n_ineq + bmap2->n_ineq);
9076 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9077 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9078 bmap = isl_basic_map_simplify(bmap);
9079 return isl_basic_map_finalize(bmap);
9080 error:
9081 isl_basic_map_free(bmap1);
9082 isl_basic_map_free(bmap2);
9083 return NULL;
9086 __isl_give isl_basic_map *isl_basic_map_flat_product(
9087 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9089 isl_basic_map *prod;
9091 prod = isl_basic_map_product(bmap1, bmap2);
9092 prod = isl_basic_map_flatten(prod);
9093 return prod;
9096 __isl_give isl_basic_set *isl_basic_set_flat_product(
9097 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9099 return isl_basic_map_flat_range_product(bset1, bset2);
9102 __isl_give isl_basic_map *isl_basic_map_domain_product(
9103 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9105 isl_space *space_result = NULL;
9106 isl_basic_map *bmap;
9107 unsigned in1, in2, out, nparam, total, pos;
9108 struct isl_dim_map *dim_map1, *dim_map2;
9110 if (!bmap1 || !bmap2)
9111 goto error;
9113 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9114 isl_space_copy(bmap2->dim));
9116 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9117 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9118 out = isl_basic_map_dim(bmap1, isl_dim_out);
9119 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9121 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9122 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9123 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9124 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9125 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9126 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9127 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9128 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9129 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9130 isl_dim_map_div(dim_map1, bmap1, pos += out);
9131 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9133 bmap = isl_basic_map_alloc_space(space_result,
9134 bmap1->n_div + bmap2->n_div,
9135 bmap1->n_eq + bmap2->n_eq,
9136 bmap1->n_ineq + bmap2->n_ineq);
9137 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9138 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9139 bmap = isl_basic_map_simplify(bmap);
9140 return isl_basic_map_finalize(bmap);
9141 error:
9142 isl_basic_map_free(bmap1);
9143 isl_basic_map_free(bmap2);
9144 return NULL;
9147 __isl_give isl_basic_map *isl_basic_map_range_product(
9148 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9150 isl_space *dim_result = NULL;
9151 isl_basic_map *bmap;
9152 unsigned in, out1, out2, nparam, total, pos;
9153 struct isl_dim_map *dim_map1, *dim_map2;
9155 if (!bmap1 || !bmap2)
9156 goto error;
9158 if (!isl_space_match(bmap1->dim, isl_dim_param,
9159 bmap2->dim, isl_dim_param))
9160 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9161 "parameters don't match", goto error);
9163 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9164 isl_space_copy(bmap2->dim));
9166 in = isl_basic_map_dim(bmap1, isl_dim_in);
9167 out1 = isl_basic_map_n_out(bmap1);
9168 out2 = isl_basic_map_n_out(bmap2);
9169 nparam = isl_basic_map_n_param(bmap1);
9171 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9172 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9173 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9174 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9175 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9176 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9177 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9178 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9179 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9180 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9181 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9183 bmap = isl_basic_map_alloc_space(dim_result,
9184 bmap1->n_div + bmap2->n_div,
9185 bmap1->n_eq + bmap2->n_eq,
9186 bmap1->n_ineq + bmap2->n_ineq);
9187 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9188 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9189 bmap = isl_basic_map_simplify(bmap);
9190 return isl_basic_map_finalize(bmap);
9191 error:
9192 isl_basic_map_free(bmap1);
9193 isl_basic_map_free(bmap2);
9194 return NULL;
9197 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9198 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9200 isl_basic_map *prod;
9202 prod = isl_basic_map_range_product(bmap1, bmap2);
9203 prod = isl_basic_map_flatten_range(prod);
9204 return prod;
9207 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9208 __isl_take isl_map *map2,
9209 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9210 __isl_take isl_space *right),
9211 __isl_give isl_basic_map *(*basic_map_product)(
9212 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9214 unsigned flags = 0;
9215 struct isl_map *result;
9216 int i, j;
9218 if (!map1 || !map2)
9219 goto error;
9221 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9222 map2->dim, isl_dim_param), goto error);
9224 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9225 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9226 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9228 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9229 isl_space_copy(map2->dim)),
9230 map1->n * map2->n, flags);
9231 if (!result)
9232 goto error;
9233 for (i = 0; i < map1->n; ++i)
9234 for (j = 0; j < map2->n; ++j) {
9235 struct isl_basic_map *part;
9236 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9237 isl_basic_map_copy(map2->p[j]));
9238 if (isl_basic_map_is_empty(part))
9239 isl_basic_map_free(part);
9240 else
9241 result = isl_map_add_basic_map(result, part);
9242 if (!result)
9243 goto error;
9245 isl_map_free(map1);
9246 isl_map_free(map2);
9247 return result;
9248 error:
9249 isl_map_free(map1);
9250 isl_map_free(map2);
9251 return NULL;
9254 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9256 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9257 __isl_take isl_map *map2)
9259 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9262 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9263 __isl_take isl_map *map2)
9265 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9268 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9270 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9271 __isl_take isl_map *map2)
9273 isl_map *prod;
9275 prod = isl_map_product(map1, map2);
9276 prod = isl_map_flatten(prod);
9277 return prod;
9280 /* Given two set A and B, construct its Cartesian product A x B.
9282 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9284 return isl_map_range_product(set1, set2);
9287 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9288 __isl_take isl_set *set2)
9290 return isl_map_flat_range_product(set1, set2);
9293 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9295 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9296 __isl_take isl_map *map2)
9298 return map_product(map1, map2, &isl_space_domain_product,
9299 &isl_basic_map_domain_product);
9302 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9304 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9305 __isl_take isl_map *map2)
9307 return map_product(map1, map2, &isl_space_range_product,
9308 &isl_basic_map_range_product);
9311 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9312 __isl_take isl_map *map2)
9314 return isl_map_align_params_map_map_and(map1, map2,
9315 &map_domain_product_aligned);
9318 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9319 __isl_take isl_map *map2)
9321 return isl_map_align_params_map_map_and(map1, map2,
9322 &map_range_product_aligned);
9325 /* Given a map A -> [B -> C], extract the map A -> B.
9327 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9329 isl_space *space;
9330 int total, keep;
9332 if (!map)
9333 return NULL;
9334 if (!isl_space_range_is_wrapping(map->dim))
9335 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9336 "range is not a product", return isl_map_free(map));
9338 space = isl_map_get_space(map);
9339 total = isl_space_dim(space, isl_dim_out);
9340 space = isl_space_range_factor_domain(space);
9341 keep = isl_space_dim(space, isl_dim_out);
9342 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9343 map = isl_map_reset_space(map, space);
9345 return map;
9348 /* Given a map A -> [B -> C], extract the map A -> C.
9350 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9352 isl_space *space;
9353 int total, keep;
9355 if (!map)
9356 return NULL;
9357 if (!isl_space_range_is_wrapping(map->dim))
9358 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9359 "range is not a product", return isl_map_free(map));
9361 space = isl_map_get_space(map);
9362 total = isl_space_dim(space, isl_dim_out);
9363 space = isl_space_range_factor_range(space);
9364 keep = isl_space_dim(space, isl_dim_out);
9365 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9366 map = isl_map_reset_space(map, space);
9368 return map;
9371 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9373 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9374 __isl_take isl_map *map2)
9376 isl_map *prod;
9378 prod = isl_map_domain_product(map1, map2);
9379 prod = isl_map_flatten_domain(prod);
9380 return prod;
9383 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9385 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9386 __isl_take isl_map *map2)
9388 isl_map *prod;
9390 prod = isl_map_range_product(map1, map2);
9391 prod = isl_map_flatten_range(prod);
9392 return prod;
9395 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9397 int i;
9398 uint32_t hash = isl_hash_init();
9399 unsigned total;
9401 if (!bmap)
9402 return 0;
9403 bmap = isl_basic_map_copy(bmap);
9404 bmap = isl_basic_map_normalize(bmap);
9405 if (!bmap)
9406 return 0;
9407 total = isl_basic_map_total_dim(bmap);
9408 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9409 for (i = 0; i < bmap->n_eq; ++i) {
9410 uint32_t c_hash;
9411 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9412 isl_hash_hash(hash, c_hash);
9414 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9415 for (i = 0; i < bmap->n_ineq; ++i) {
9416 uint32_t c_hash;
9417 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9418 isl_hash_hash(hash, c_hash);
9420 isl_hash_byte(hash, bmap->n_div & 0xFF);
9421 for (i = 0; i < bmap->n_div; ++i) {
9422 uint32_t c_hash;
9423 if (isl_int_is_zero(bmap->div[i][0]))
9424 continue;
9425 isl_hash_byte(hash, i & 0xFF);
9426 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9427 isl_hash_hash(hash, c_hash);
9429 isl_basic_map_free(bmap);
9430 return hash;
9433 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9435 return isl_basic_map_get_hash((isl_basic_map *)bset);
9438 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9440 int i;
9441 uint32_t hash;
9443 if (!map)
9444 return 0;
9445 map = isl_map_copy(map);
9446 map = isl_map_normalize(map);
9447 if (!map)
9448 return 0;
9450 hash = isl_hash_init();
9451 for (i = 0; i < map->n; ++i) {
9452 uint32_t bmap_hash;
9453 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9454 isl_hash_hash(hash, bmap_hash);
9457 isl_map_free(map);
9459 return hash;
9462 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9464 return isl_map_get_hash((isl_map *)set);
9467 /* Check if the value for dimension dim is completely determined
9468 * by the values of the other parameters and variables.
9469 * That is, check if dimension dim is involved in an equality.
9471 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9473 int i;
9474 unsigned nparam;
9476 if (!bset)
9477 return -1;
9478 nparam = isl_basic_set_n_param(bset);
9479 for (i = 0; i < bset->n_eq; ++i)
9480 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9481 return 1;
9482 return 0;
9485 /* Check if the value for dimension dim is completely determined
9486 * by the values of the other parameters and variables.
9487 * That is, check if dimension dim is involved in an equality
9488 * for each of the subsets.
9490 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9492 int i;
9494 if (!set)
9495 return -1;
9496 for (i = 0; i < set->n; ++i) {
9497 int unique;
9498 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9499 if (unique != 1)
9500 return unique;
9502 return 1;
9505 int isl_set_n_basic_set(__isl_keep isl_set *set)
9507 return set ? set->n : 0;
9510 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9511 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9513 int i;
9515 if (!map)
9516 return -1;
9518 for (i = 0; i < map->n; ++i)
9519 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9520 return -1;
9522 return 0;
9525 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9526 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9528 int i;
9530 if (!set)
9531 return -1;
9533 for (i = 0; i < set->n; ++i)
9534 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9535 return -1;
9537 return 0;
9540 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9542 isl_space *dim;
9544 if (!bset)
9545 return NULL;
9547 bset = isl_basic_set_cow(bset);
9548 if (!bset)
9549 return NULL;
9551 dim = isl_basic_set_get_space(bset);
9552 dim = isl_space_lift(dim, bset->n_div);
9553 if (!dim)
9554 goto error;
9555 isl_space_free(bset->dim);
9556 bset->dim = dim;
9557 bset->extra -= bset->n_div;
9558 bset->n_div = 0;
9560 bset = isl_basic_set_finalize(bset);
9562 return bset;
9563 error:
9564 isl_basic_set_free(bset);
9565 return NULL;
9568 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9570 int i;
9571 isl_space *dim;
9572 unsigned n_div;
9574 set = isl_set_align_divs(set);
9576 if (!set)
9577 return NULL;
9579 set = isl_set_cow(set);
9580 if (!set)
9581 return NULL;
9583 n_div = set->p[0]->n_div;
9584 dim = isl_set_get_space(set);
9585 dim = isl_space_lift(dim, n_div);
9586 if (!dim)
9587 goto error;
9588 isl_space_free(set->dim);
9589 set->dim = dim;
9591 for (i = 0; i < set->n; ++i) {
9592 set->p[i] = isl_basic_set_lift(set->p[i]);
9593 if (!set->p[i])
9594 goto error;
9597 return set;
9598 error:
9599 isl_set_free(set);
9600 return NULL;
9603 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9605 isl_space *dim;
9606 struct isl_basic_map *bmap;
9607 unsigned n_set;
9608 unsigned n_div;
9609 unsigned n_param;
9610 unsigned total;
9611 int i, k, l;
9613 set = isl_set_align_divs(set);
9615 if (!set)
9616 return NULL;
9618 dim = isl_set_get_space(set);
9619 if (set->n == 0 || set->p[0]->n_div == 0) {
9620 isl_set_free(set);
9621 return isl_map_identity(isl_space_map_from_set(dim));
9624 n_div = set->p[0]->n_div;
9625 dim = isl_space_map_from_set(dim);
9626 n_param = isl_space_dim(dim, isl_dim_param);
9627 n_set = isl_space_dim(dim, isl_dim_in);
9628 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9629 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9630 for (i = 0; i < n_set; ++i)
9631 bmap = var_equal(bmap, i);
9633 total = n_param + n_set + n_set + n_div;
9634 for (i = 0; i < n_div; ++i) {
9635 k = isl_basic_map_alloc_inequality(bmap);
9636 if (k < 0)
9637 goto error;
9638 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9639 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9640 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9641 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9642 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9643 set->p[0]->div[i][0]);
9645 l = isl_basic_map_alloc_inequality(bmap);
9646 if (l < 0)
9647 goto error;
9648 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9649 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9650 set->p[0]->div[i][0]);
9651 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9654 isl_set_free(set);
9655 bmap = isl_basic_map_simplify(bmap);
9656 bmap = isl_basic_map_finalize(bmap);
9657 return isl_map_from_basic_map(bmap);
9658 error:
9659 isl_set_free(set);
9660 isl_basic_map_free(bmap);
9661 return NULL;
9664 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9666 unsigned dim;
9667 int size = 0;
9669 if (!bset)
9670 return -1;
9672 dim = isl_basic_set_total_dim(bset);
9673 size += bset->n_eq * (1 + dim);
9674 size += bset->n_ineq * (1 + dim);
9675 size += bset->n_div * (2 + dim);
9677 return size;
9680 int isl_set_size(__isl_keep isl_set *set)
9682 int i;
9683 int size = 0;
9685 if (!set)
9686 return -1;
9688 for (i = 0; i < set->n; ++i)
9689 size += isl_basic_set_size(set->p[i]);
9691 return size;
9694 /* Check if there is any lower bound (if lower == 0) and/or upper
9695 * bound (if upper == 0) on the specified dim.
9697 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9698 enum isl_dim_type type, unsigned pos, int lower, int upper)
9700 int i;
9702 if (!bmap)
9703 return -1;
9705 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9707 pos += isl_basic_map_offset(bmap, type);
9709 for (i = 0; i < bmap->n_div; ++i) {
9710 if (isl_int_is_zero(bmap->div[i][0]))
9711 continue;
9712 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9713 return 1;
9716 for (i = 0; i < bmap->n_eq; ++i)
9717 if (!isl_int_is_zero(bmap->eq[i][pos]))
9718 return 1;
9720 for (i = 0; i < bmap->n_ineq; ++i) {
9721 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9722 if (sgn > 0)
9723 lower = 1;
9724 if (sgn < 0)
9725 upper = 1;
9728 return lower && upper;
9731 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9732 enum isl_dim_type type, unsigned pos)
9734 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9737 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9738 enum isl_dim_type type, unsigned pos)
9740 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9743 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9744 enum isl_dim_type type, unsigned pos)
9746 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9749 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9750 enum isl_dim_type type, unsigned pos)
9752 int i;
9754 if (!map)
9755 return -1;
9757 for (i = 0; i < map->n; ++i) {
9758 int bounded;
9759 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9760 if (bounded < 0 || !bounded)
9761 return bounded;
9764 return 1;
9767 /* Return 1 if the specified dim is involved in both an upper bound
9768 * and a lower bound.
9770 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9771 enum isl_dim_type type, unsigned pos)
9773 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9776 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9778 static int has_any_bound(__isl_keep isl_map *map,
9779 enum isl_dim_type type, unsigned pos,
9780 int (*fn)(__isl_keep isl_basic_map *bmap,
9781 enum isl_dim_type type, unsigned pos))
9783 int i;
9785 if (!map)
9786 return -1;
9788 for (i = 0; i < map->n; ++i) {
9789 int bounded;
9790 bounded = fn(map->p[i], type, pos);
9791 if (bounded < 0 || bounded)
9792 return bounded;
9795 return 0;
9798 /* Return 1 if the specified dim is involved in any lower bound.
9800 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9801 enum isl_dim_type type, unsigned pos)
9803 return has_any_bound(set, type, pos,
9804 &isl_basic_map_dim_has_lower_bound);
9807 /* Return 1 if the specified dim is involved in any upper bound.
9809 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9810 enum isl_dim_type type, unsigned pos)
9812 return has_any_bound(set, type, pos,
9813 &isl_basic_map_dim_has_upper_bound);
9816 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9818 static int has_bound(__isl_keep isl_map *map,
9819 enum isl_dim_type type, unsigned pos,
9820 int (*fn)(__isl_keep isl_basic_map *bmap,
9821 enum isl_dim_type type, unsigned pos))
9823 int i;
9825 if (!map)
9826 return -1;
9828 for (i = 0; i < map->n; ++i) {
9829 int bounded;
9830 bounded = fn(map->p[i], type, pos);
9831 if (bounded < 0 || !bounded)
9832 return bounded;
9835 return 1;
9838 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9840 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9841 enum isl_dim_type type, unsigned pos)
9843 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9846 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9848 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9849 enum isl_dim_type type, unsigned pos)
9851 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9854 /* For each of the "n" variables starting at "first", determine
9855 * the sign of the variable and put the results in the first "n"
9856 * elements of the array "signs".
9857 * Sign
9858 * 1 means that the variable is non-negative
9859 * -1 means that the variable is non-positive
9860 * 0 means the variable attains both positive and negative values.
9862 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9863 unsigned first, unsigned n, int *signs)
9865 isl_vec *bound = NULL;
9866 struct isl_tab *tab = NULL;
9867 struct isl_tab_undo *snap;
9868 int i;
9870 if (!bset || !signs)
9871 return -1;
9873 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9874 tab = isl_tab_from_basic_set(bset, 0);
9875 if (!bound || !tab)
9876 goto error;
9878 isl_seq_clr(bound->el, bound->size);
9879 isl_int_set_si(bound->el[0], -1);
9881 snap = isl_tab_snap(tab);
9882 for (i = 0; i < n; ++i) {
9883 int empty;
9885 isl_int_set_si(bound->el[1 + first + i], -1);
9886 if (isl_tab_add_ineq(tab, bound->el) < 0)
9887 goto error;
9888 empty = tab->empty;
9889 isl_int_set_si(bound->el[1 + first + i], 0);
9890 if (isl_tab_rollback(tab, snap) < 0)
9891 goto error;
9893 if (empty) {
9894 signs[i] = 1;
9895 continue;
9898 isl_int_set_si(bound->el[1 + first + i], 1);
9899 if (isl_tab_add_ineq(tab, bound->el) < 0)
9900 goto error;
9901 empty = tab->empty;
9902 isl_int_set_si(bound->el[1 + first + i], 0);
9903 if (isl_tab_rollback(tab, snap) < 0)
9904 goto error;
9906 signs[i] = empty ? -1 : 0;
9909 isl_tab_free(tab);
9910 isl_vec_free(bound);
9911 return 0;
9912 error:
9913 isl_tab_free(tab);
9914 isl_vec_free(bound);
9915 return -1;
9918 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9919 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9921 if (!bset || !signs)
9922 return -1;
9923 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9924 return -1);
9926 first += pos(bset->dim, type) - 1;
9927 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9930 /* Check if the given basic map is obviously single-valued.
9931 * In particular, for each output dimension, check that there is
9932 * an equality that defines the output dimension in terms of
9933 * earlier dimensions.
9935 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9937 int i, j;
9938 unsigned total;
9939 unsigned n_out;
9940 unsigned o_out;
9942 if (!bmap)
9943 return -1;
9945 total = 1 + isl_basic_map_total_dim(bmap);
9946 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9947 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9949 for (i = 0; i < n_out; ++i) {
9950 for (j = 0; j < bmap->n_eq; ++j) {
9951 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9952 continue;
9953 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9954 total - (o_out + i + 1)) == -1)
9955 break;
9957 if (j >= bmap->n_eq)
9958 return 0;
9961 return 1;
9964 /* Check if the given basic map is single-valued.
9965 * We simply compute
9967 * M \circ M^-1
9969 * and check if the result is a subset of the identity mapping.
9971 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9973 isl_space *space;
9974 isl_basic_map *test;
9975 isl_basic_map *id;
9976 int sv;
9978 sv = isl_basic_map_plain_is_single_valued(bmap);
9979 if (sv < 0 || sv)
9980 return sv;
9982 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9983 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9985 space = isl_basic_map_get_space(bmap);
9986 space = isl_space_map_from_set(isl_space_range(space));
9987 id = isl_basic_map_identity(space);
9989 sv = isl_basic_map_is_subset(test, id);
9991 isl_basic_map_free(test);
9992 isl_basic_map_free(id);
9994 return sv;
9997 /* Check if the given map is obviously single-valued.
9999 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10001 if (!map)
10002 return -1;
10003 if (map->n == 0)
10004 return 1;
10005 if (map->n >= 2)
10006 return 0;
10008 return isl_basic_map_plain_is_single_valued(map->p[0]);
10011 /* Check if the given map is single-valued.
10012 * We simply compute
10014 * M \circ M^-1
10016 * and check if the result is a subset of the identity mapping.
10018 int isl_map_is_single_valued(__isl_keep isl_map *map)
10020 isl_space *dim;
10021 isl_map *test;
10022 isl_map *id;
10023 int sv;
10025 sv = isl_map_plain_is_single_valued(map);
10026 if (sv < 0 || sv)
10027 return sv;
10029 test = isl_map_reverse(isl_map_copy(map));
10030 test = isl_map_apply_range(test, isl_map_copy(map));
10032 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10033 id = isl_map_identity(dim);
10035 sv = isl_map_is_subset(test, id);
10037 isl_map_free(test);
10038 isl_map_free(id);
10040 return sv;
10043 int isl_map_is_injective(__isl_keep isl_map *map)
10045 int in;
10047 map = isl_map_copy(map);
10048 map = isl_map_reverse(map);
10049 in = isl_map_is_single_valued(map);
10050 isl_map_free(map);
10052 return in;
10055 /* Check if the given map is obviously injective.
10057 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10059 int in;
10061 map = isl_map_copy(map);
10062 map = isl_map_reverse(map);
10063 in = isl_map_plain_is_single_valued(map);
10064 isl_map_free(map);
10066 return in;
10069 int isl_map_is_bijective(__isl_keep isl_map *map)
10071 int sv;
10073 sv = isl_map_is_single_valued(map);
10074 if (sv < 0 || !sv)
10075 return sv;
10077 return isl_map_is_injective(map);
10080 int isl_set_is_singleton(__isl_keep isl_set *set)
10082 return isl_map_is_single_valued((isl_map *)set);
10085 int isl_map_is_translation(__isl_keep isl_map *map)
10087 int ok;
10088 isl_set *delta;
10090 delta = isl_map_deltas(isl_map_copy(map));
10091 ok = isl_set_is_singleton(delta);
10092 isl_set_free(delta);
10094 return ok;
10097 static int unique(isl_int *p, unsigned pos, unsigned len)
10099 if (isl_seq_first_non_zero(p, pos) != -1)
10100 return 0;
10101 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10102 return 0;
10103 return 1;
10106 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10108 int i, j;
10109 unsigned nvar;
10110 unsigned ovar;
10112 if (!bset)
10113 return -1;
10115 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10116 return 0;
10118 nvar = isl_basic_set_dim(bset, isl_dim_set);
10119 ovar = isl_space_offset(bset->dim, isl_dim_set);
10120 for (j = 0; j < nvar; ++j) {
10121 int lower = 0, upper = 0;
10122 for (i = 0; i < bset->n_eq; ++i) {
10123 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10124 continue;
10125 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10126 return 0;
10127 break;
10129 if (i < bset->n_eq)
10130 continue;
10131 for (i = 0; i < bset->n_ineq; ++i) {
10132 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10133 continue;
10134 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10135 return 0;
10136 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10137 lower = 1;
10138 else
10139 upper = 1;
10141 if (!lower || !upper)
10142 return 0;
10145 return 1;
10148 int isl_set_is_box(__isl_keep isl_set *set)
10150 if (!set)
10151 return -1;
10152 if (set->n != 1)
10153 return 0;
10155 return isl_basic_set_is_box(set->p[0]);
10158 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10160 if (!bset)
10161 return -1;
10163 return isl_space_is_wrapping(bset->dim);
10166 int isl_set_is_wrapping(__isl_keep isl_set *set)
10168 if (!set)
10169 return -1;
10171 return isl_space_is_wrapping(set->dim);
10174 /* Is the range of "map" a wrapped relation?
10176 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10178 if (!map)
10179 return -1;
10181 return isl_space_range_is_wrapping(map->dim);
10184 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10186 bmap = isl_basic_map_cow(bmap);
10187 if (!bmap)
10188 return NULL;
10190 bmap->dim = isl_space_wrap(bmap->dim);
10191 if (!bmap->dim)
10192 goto error;
10194 bmap = isl_basic_map_finalize(bmap);
10196 return (isl_basic_set *)bmap;
10197 error:
10198 isl_basic_map_free(bmap);
10199 return NULL;
10202 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10204 int i;
10206 map = isl_map_cow(map);
10207 if (!map)
10208 return NULL;
10210 for (i = 0; i < map->n; ++i) {
10211 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10212 if (!map->p[i])
10213 goto error;
10215 map->dim = isl_space_wrap(map->dim);
10216 if (!map->dim)
10217 goto error;
10219 return (isl_set *)map;
10220 error:
10221 isl_map_free(map);
10222 return NULL;
10225 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10227 bset = isl_basic_set_cow(bset);
10228 if (!bset)
10229 return NULL;
10231 bset->dim = isl_space_unwrap(bset->dim);
10232 if (!bset->dim)
10233 goto error;
10235 bset = isl_basic_set_finalize(bset);
10237 return (isl_basic_map *)bset;
10238 error:
10239 isl_basic_set_free(bset);
10240 return NULL;
10243 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10245 int i;
10247 if (!set)
10248 return NULL;
10250 if (!isl_set_is_wrapping(set))
10251 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10252 goto error);
10254 set = isl_set_cow(set);
10255 if (!set)
10256 return NULL;
10258 for (i = 0; i < set->n; ++i) {
10259 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10260 if (!set->p[i])
10261 goto error;
10264 set->dim = isl_space_unwrap(set->dim);
10265 if (!set->dim)
10266 goto error;
10268 return (isl_map *)set;
10269 error:
10270 isl_set_free(set);
10271 return NULL;
10274 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10275 enum isl_dim_type type)
10277 if (!bmap)
10278 return NULL;
10280 if (!isl_space_is_named_or_nested(bmap->dim, type))
10281 return bmap;
10283 bmap = isl_basic_map_cow(bmap);
10284 if (!bmap)
10285 return NULL;
10287 bmap->dim = isl_space_reset(bmap->dim, type);
10288 if (!bmap->dim)
10289 goto error;
10291 bmap = isl_basic_map_finalize(bmap);
10293 return bmap;
10294 error:
10295 isl_basic_map_free(bmap);
10296 return NULL;
10299 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10300 enum isl_dim_type type)
10302 int i;
10304 if (!map)
10305 return NULL;
10307 if (!isl_space_is_named_or_nested(map->dim, type))
10308 return map;
10310 map = isl_map_cow(map);
10311 if (!map)
10312 return NULL;
10314 for (i = 0; i < map->n; ++i) {
10315 map->p[i] = isl_basic_map_reset(map->p[i], type);
10316 if (!map->p[i])
10317 goto error;
10319 map->dim = isl_space_reset(map->dim, type);
10320 if (!map->dim)
10321 goto error;
10323 return map;
10324 error:
10325 isl_map_free(map);
10326 return NULL;
10329 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10331 if (!bmap)
10332 return NULL;
10334 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10335 return bmap;
10337 bmap = isl_basic_map_cow(bmap);
10338 if (!bmap)
10339 return NULL;
10341 bmap->dim = isl_space_flatten(bmap->dim);
10342 if (!bmap->dim)
10343 goto error;
10345 bmap = isl_basic_map_finalize(bmap);
10347 return bmap;
10348 error:
10349 isl_basic_map_free(bmap);
10350 return NULL;
10353 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10355 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10358 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10359 __isl_take isl_basic_map *bmap)
10361 if (!bmap)
10362 return NULL;
10364 if (!bmap->dim->nested[0])
10365 return bmap;
10367 bmap = isl_basic_map_cow(bmap);
10368 if (!bmap)
10369 return NULL;
10371 bmap->dim = isl_space_flatten_domain(bmap->dim);
10372 if (!bmap->dim)
10373 goto error;
10375 bmap = isl_basic_map_finalize(bmap);
10377 return bmap;
10378 error:
10379 isl_basic_map_free(bmap);
10380 return NULL;
10383 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10384 __isl_take isl_basic_map *bmap)
10386 if (!bmap)
10387 return NULL;
10389 if (!bmap->dim->nested[1])
10390 return bmap;
10392 bmap = isl_basic_map_cow(bmap);
10393 if (!bmap)
10394 return NULL;
10396 bmap->dim = isl_space_flatten_range(bmap->dim);
10397 if (!bmap->dim)
10398 goto error;
10400 bmap = isl_basic_map_finalize(bmap);
10402 return bmap;
10403 error:
10404 isl_basic_map_free(bmap);
10405 return NULL;
10408 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10410 int i;
10412 if (!map)
10413 return NULL;
10415 if (!map->dim->nested[0] && !map->dim->nested[1])
10416 return map;
10418 map = isl_map_cow(map);
10419 if (!map)
10420 return NULL;
10422 for (i = 0; i < map->n; ++i) {
10423 map->p[i] = isl_basic_map_flatten(map->p[i]);
10424 if (!map->p[i])
10425 goto error;
10427 map->dim = isl_space_flatten(map->dim);
10428 if (!map->dim)
10429 goto error;
10431 return map;
10432 error:
10433 isl_map_free(map);
10434 return NULL;
10437 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10439 return (isl_set *)isl_map_flatten((isl_map *)set);
10442 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10444 isl_space *dim, *flat_dim;
10445 isl_map *map;
10447 dim = isl_set_get_space(set);
10448 flat_dim = isl_space_flatten(isl_space_copy(dim));
10449 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10450 map = isl_map_intersect_domain(map, set);
10452 return map;
10455 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10457 int i;
10459 if (!map)
10460 return NULL;
10462 if (!map->dim->nested[0])
10463 return map;
10465 map = isl_map_cow(map);
10466 if (!map)
10467 return NULL;
10469 for (i = 0; i < map->n; ++i) {
10470 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10471 if (!map->p[i])
10472 goto error;
10474 map->dim = isl_space_flatten_domain(map->dim);
10475 if (!map->dim)
10476 goto error;
10478 return map;
10479 error:
10480 isl_map_free(map);
10481 return NULL;
10484 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10486 int i;
10488 if (!map)
10489 return NULL;
10491 if (!map->dim->nested[1])
10492 return map;
10494 map = isl_map_cow(map);
10495 if (!map)
10496 return NULL;
10498 for (i = 0; i < map->n; ++i) {
10499 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10500 if (!map->p[i])
10501 goto error;
10503 map->dim = isl_space_flatten_range(map->dim);
10504 if (!map->dim)
10505 goto error;
10507 return map;
10508 error:
10509 isl_map_free(map);
10510 return NULL;
10513 /* Reorder the dimensions of "bmap" according to the given dim_map
10514 * and set the dimension specification to "dim".
10516 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10517 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10519 isl_basic_map *res;
10520 unsigned flags;
10522 bmap = isl_basic_map_cow(bmap);
10523 if (!bmap || !dim || !dim_map)
10524 goto error;
10526 flags = bmap->flags;
10527 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10528 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10529 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10530 res = isl_basic_map_alloc_space(dim,
10531 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10532 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10533 if (res)
10534 res->flags = flags;
10535 res = isl_basic_map_finalize(res);
10536 return res;
10537 error:
10538 free(dim_map);
10539 isl_basic_map_free(bmap);
10540 isl_space_free(dim);
10541 return NULL;
10544 /* Reorder the dimensions of "map" according to given reordering.
10546 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10547 __isl_take isl_reordering *r)
10549 int i;
10550 struct isl_dim_map *dim_map;
10552 map = isl_map_cow(map);
10553 dim_map = isl_dim_map_from_reordering(r);
10554 if (!map || !r || !dim_map)
10555 goto error;
10557 for (i = 0; i < map->n; ++i) {
10558 struct isl_dim_map *dim_map_i;
10560 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10562 map->p[i] = isl_basic_map_realign(map->p[i],
10563 isl_space_copy(r->dim), dim_map_i);
10565 if (!map->p[i])
10566 goto error;
10569 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10571 isl_reordering_free(r);
10572 free(dim_map);
10573 return map;
10574 error:
10575 free(dim_map);
10576 isl_map_free(map);
10577 isl_reordering_free(r);
10578 return NULL;
10581 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10582 __isl_take isl_reordering *r)
10584 return (isl_set *)isl_map_realign((isl_map *)set, r);
10587 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10588 __isl_take isl_space *model)
10590 isl_ctx *ctx;
10592 if (!map || !model)
10593 goto error;
10595 ctx = isl_space_get_ctx(model);
10596 if (!isl_space_has_named_params(model))
10597 isl_die(ctx, isl_error_invalid,
10598 "model has unnamed parameters", goto error);
10599 if (!isl_space_has_named_params(map->dim))
10600 isl_die(ctx, isl_error_invalid,
10601 "relation has unnamed parameters", goto error);
10602 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10603 isl_reordering *exp;
10605 model = isl_space_drop_dims(model, isl_dim_in,
10606 0, isl_space_dim(model, isl_dim_in));
10607 model = isl_space_drop_dims(model, isl_dim_out,
10608 0, isl_space_dim(model, isl_dim_out));
10609 exp = isl_parameter_alignment_reordering(map->dim, model);
10610 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10611 map = isl_map_realign(map, exp);
10614 isl_space_free(model);
10615 return map;
10616 error:
10617 isl_space_free(model);
10618 isl_map_free(map);
10619 return NULL;
10622 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10623 __isl_take isl_space *model)
10625 return isl_map_align_params(set, model);
10628 /* Align the parameters of "bmap" to those of "model", introducing
10629 * additional parameters if needed.
10631 __isl_give isl_basic_map *isl_basic_map_align_params(
10632 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10634 isl_ctx *ctx;
10636 if (!bmap || !model)
10637 goto error;
10639 ctx = isl_space_get_ctx(model);
10640 if (!isl_space_has_named_params(model))
10641 isl_die(ctx, isl_error_invalid,
10642 "model has unnamed parameters", goto error);
10643 if (!isl_space_has_named_params(bmap->dim))
10644 isl_die(ctx, isl_error_invalid,
10645 "relation has unnamed parameters", goto error);
10646 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10647 isl_reordering *exp;
10648 struct isl_dim_map *dim_map;
10650 model = isl_space_drop_dims(model, isl_dim_in,
10651 0, isl_space_dim(model, isl_dim_in));
10652 model = isl_space_drop_dims(model, isl_dim_out,
10653 0, isl_space_dim(model, isl_dim_out));
10654 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10655 exp = isl_reordering_extend_space(exp,
10656 isl_basic_map_get_space(bmap));
10657 dim_map = isl_dim_map_from_reordering(exp);
10658 bmap = isl_basic_map_realign(bmap,
10659 exp ? isl_space_copy(exp->dim) : NULL,
10660 isl_dim_map_extend(dim_map, bmap));
10661 isl_reordering_free(exp);
10662 free(dim_map);
10665 isl_space_free(model);
10666 return bmap;
10667 error:
10668 isl_space_free(model);
10669 isl_basic_map_free(bmap);
10670 return NULL;
10673 /* Align the parameters of "bset" to those of "model", introducing
10674 * additional parameters if needed.
10676 __isl_give isl_basic_set *isl_basic_set_align_params(
10677 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10679 return isl_basic_map_align_params(bset, model);
10682 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10683 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10684 enum isl_dim_type c2, enum isl_dim_type c3,
10685 enum isl_dim_type c4, enum isl_dim_type c5)
10687 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10688 struct isl_mat *mat;
10689 int i, j, k;
10690 int pos;
10692 if (!bmap)
10693 return NULL;
10694 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10695 isl_basic_map_total_dim(bmap) + 1);
10696 if (!mat)
10697 return NULL;
10698 for (i = 0; i < bmap->n_eq; ++i)
10699 for (j = 0, pos = 0; j < 5; ++j) {
10700 int off = isl_basic_map_offset(bmap, c[j]);
10701 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10702 isl_int_set(mat->row[i][pos],
10703 bmap->eq[i][off + k]);
10704 ++pos;
10708 return mat;
10711 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10712 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10713 enum isl_dim_type c2, enum isl_dim_type c3,
10714 enum isl_dim_type c4, enum isl_dim_type c5)
10716 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10717 struct isl_mat *mat;
10718 int i, j, k;
10719 int pos;
10721 if (!bmap)
10722 return NULL;
10723 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10724 isl_basic_map_total_dim(bmap) + 1);
10725 if (!mat)
10726 return NULL;
10727 for (i = 0; i < bmap->n_ineq; ++i)
10728 for (j = 0, pos = 0; j < 5; ++j) {
10729 int off = isl_basic_map_offset(bmap, c[j]);
10730 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10731 isl_int_set(mat->row[i][pos],
10732 bmap->ineq[i][off + k]);
10733 ++pos;
10737 return mat;
10740 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10741 __isl_take isl_space *dim,
10742 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10743 enum isl_dim_type c2, enum isl_dim_type c3,
10744 enum isl_dim_type c4, enum isl_dim_type c5)
10746 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10747 isl_basic_map *bmap;
10748 unsigned total;
10749 unsigned extra;
10750 int i, j, k, l;
10751 int pos;
10753 if (!dim || !eq || !ineq)
10754 goto error;
10756 if (eq->n_col != ineq->n_col)
10757 isl_die(dim->ctx, isl_error_invalid,
10758 "equalities and inequalities matrices should have "
10759 "same number of columns", goto error);
10761 total = 1 + isl_space_dim(dim, isl_dim_all);
10763 if (eq->n_col < total)
10764 isl_die(dim->ctx, isl_error_invalid,
10765 "number of columns too small", goto error);
10767 extra = eq->n_col - total;
10769 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10770 eq->n_row, ineq->n_row);
10771 if (!bmap)
10772 goto error;
10773 for (i = 0; i < extra; ++i) {
10774 k = isl_basic_map_alloc_div(bmap);
10775 if (k < 0)
10776 goto error;
10777 isl_int_set_si(bmap->div[k][0], 0);
10779 for (i = 0; i < eq->n_row; ++i) {
10780 l = isl_basic_map_alloc_equality(bmap);
10781 if (l < 0)
10782 goto error;
10783 for (j = 0, pos = 0; j < 5; ++j) {
10784 int off = isl_basic_map_offset(bmap, c[j]);
10785 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10786 isl_int_set(bmap->eq[l][off + k],
10787 eq->row[i][pos]);
10788 ++pos;
10792 for (i = 0; i < ineq->n_row; ++i) {
10793 l = isl_basic_map_alloc_inequality(bmap);
10794 if (l < 0)
10795 goto error;
10796 for (j = 0, pos = 0; j < 5; ++j) {
10797 int off = isl_basic_map_offset(bmap, c[j]);
10798 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10799 isl_int_set(bmap->ineq[l][off + k],
10800 ineq->row[i][pos]);
10801 ++pos;
10806 isl_space_free(dim);
10807 isl_mat_free(eq);
10808 isl_mat_free(ineq);
10810 bmap = isl_basic_map_simplify(bmap);
10811 return isl_basic_map_finalize(bmap);
10812 error:
10813 isl_space_free(dim);
10814 isl_mat_free(eq);
10815 isl_mat_free(ineq);
10816 return NULL;
10819 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10820 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10821 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10823 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10824 c1, c2, c3, c4, isl_dim_in);
10827 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10828 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10829 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10831 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10832 c1, c2, c3, c4, isl_dim_in);
10835 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10836 __isl_take isl_space *dim,
10837 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10838 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10840 return (isl_basic_set*)
10841 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10842 c1, c2, c3, c4, isl_dim_in);
10845 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10847 if (!bmap)
10848 return -1;
10850 return isl_space_can_zip(bmap->dim);
10853 int isl_map_can_zip(__isl_keep isl_map *map)
10855 if (!map)
10856 return -1;
10858 return isl_space_can_zip(map->dim);
10861 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10862 * (A -> C) -> (B -> D).
10864 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10866 unsigned pos;
10867 unsigned n1;
10868 unsigned n2;
10870 if (!bmap)
10871 return NULL;
10873 if (!isl_basic_map_can_zip(bmap))
10874 isl_die(bmap->ctx, isl_error_invalid,
10875 "basic map cannot be zipped", goto error);
10876 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10877 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10878 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10879 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10880 bmap = isl_basic_map_cow(bmap);
10881 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10882 if (!bmap)
10883 return NULL;
10884 bmap->dim = isl_space_zip(bmap->dim);
10885 if (!bmap->dim)
10886 goto error;
10887 return bmap;
10888 error:
10889 isl_basic_map_free(bmap);
10890 return NULL;
10893 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10894 * (A -> C) -> (B -> D).
10896 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10898 int i;
10900 if (!map)
10901 return NULL;
10903 if (!isl_map_can_zip(map))
10904 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10905 goto error);
10907 map = isl_map_cow(map);
10908 if (!map)
10909 return NULL;
10911 for (i = 0; i < map->n; ++i) {
10912 map->p[i] = isl_basic_map_zip(map->p[i]);
10913 if (!map->p[i])
10914 goto error;
10917 map->dim = isl_space_zip(map->dim);
10918 if (!map->dim)
10919 goto error;
10921 return map;
10922 error:
10923 isl_map_free(map);
10924 return NULL;
10927 /* Can we apply isl_basic_map_curry to "bmap"?
10928 * That is, does it have a nested relation in its domain?
10930 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10932 if (!bmap)
10933 return -1;
10935 return isl_space_can_curry(bmap->dim);
10938 /* Can we apply isl_map_curry to "map"?
10939 * That is, does it have a nested relation in its domain?
10941 int isl_map_can_curry(__isl_keep isl_map *map)
10943 if (!map)
10944 return -1;
10946 return isl_space_can_curry(map->dim);
10949 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10950 * A -> (B -> C).
10952 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10955 if (!bmap)
10956 return NULL;
10958 if (!isl_basic_map_can_curry(bmap))
10959 isl_die(bmap->ctx, isl_error_invalid,
10960 "basic map cannot be curried", goto error);
10961 bmap = isl_basic_map_cow(bmap);
10962 if (!bmap)
10963 return NULL;
10964 bmap->dim = isl_space_curry(bmap->dim);
10965 if (!bmap->dim)
10966 goto error;
10967 return bmap;
10968 error:
10969 isl_basic_map_free(bmap);
10970 return NULL;
10973 /* Given a map (A -> B) -> C, return the corresponding map
10974 * A -> (B -> C).
10976 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10978 int i;
10980 if (!map)
10981 return NULL;
10983 if (!isl_map_can_curry(map))
10984 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10985 goto error);
10987 map = isl_map_cow(map);
10988 if (!map)
10989 return NULL;
10991 for (i = 0; i < map->n; ++i) {
10992 map->p[i] = isl_basic_map_curry(map->p[i]);
10993 if (!map->p[i])
10994 goto error;
10997 map->dim = isl_space_curry(map->dim);
10998 if (!map->dim)
10999 goto error;
11001 return map;
11002 error:
11003 isl_map_free(map);
11004 return NULL;
11007 /* Can we apply isl_basic_map_uncurry to "bmap"?
11008 * That is, does it have a nested relation in its domain?
11010 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11012 if (!bmap)
11013 return -1;
11015 return isl_space_can_uncurry(bmap->dim);
11018 /* Can we apply isl_map_uncurry to "map"?
11019 * That is, does it have a nested relation in its domain?
11021 int isl_map_can_uncurry(__isl_keep isl_map *map)
11023 if (!map)
11024 return -1;
11026 return isl_space_can_uncurry(map->dim);
11029 /* Given a basic map A -> (B -> C), return the corresponding basic map
11030 * (A -> B) -> C.
11032 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11035 if (!bmap)
11036 return NULL;
11038 if (!isl_basic_map_can_uncurry(bmap))
11039 isl_die(bmap->ctx, isl_error_invalid,
11040 "basic map cannot be uncurried",
11041 return isl_basic_map_free(bmap));
11042 bmap = isl_basic_map_cow(bmap);
11043 if (!bmap)
11044 return NULL;
11045 bmap->dim = isl_space_uncurry(bmap->dim);
11046 if (!bmap->dim)
11047 return isl_basic_map_free(bmap);
11048 return bmap;
11051 /* Given a map A -> (B -> C), return the corresponding map
11052 * (A -> B) -> C.
11054 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11056 int i;
11058 if (!map)
11059 return NULL;
11061 if (!isl_map_can_uncurry(map))
11062 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11063 return isl_map_free(map));
11065 map = isl_map_cow(map);
11066 if (!map)
11067 return NULL;
11069 for (i = 0; i < map->n; ++i) {
11070 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11071 if (!map->p[i])
11072 return isl_map_free(map);
11075 map->dim = isl_space_uncurry(map->dim);
11076 if (!map->dim)
11077 return isl_map_free(map);
11079 return map;
11082 /* Construct a basic map mapping the domain of the affine expression
11083 * to a one-dimensional range prescribed by the affine expression.
11085 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11087 int k;
11088 int pos;
11089 isl_local_space *ls;
11090 isl_basic_map *bmap;
11092 if (!aff)
11093 return NULL;
11095 ls = isl_aff_get_local_space(aff);
11096 bmap = isl_basic_map_from_local_space(ls);
11097 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11098 k = isl_basic_map_alloc_equality(bmap);
11099 if (k < 0)
11100 goto error;
11102 pos = isl_basic_map_offset(bmap, isl_dim_out);
11103 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11104 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11105 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11106 aff->v->size - (pos + 1));
11108 isl_aff_free(aff);
11109 bmap = isl_basic_map_finalize(bmap);
11110 return bmap;
11111 error:
11112 isl_aff_free(aff);
11113 isl_basic_map_free(bmap);
11114 return NULL;
11117 /* Construct a map mapping the domain of the affine expression
11118 * to a one-dimensional range prescribed by the affine expression.
11120 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11122 isl_basic_map *bmap;
11124 bmap = isl_basic_map_from_aff(aff);
11125 return isl_map_from_basic_map(bmap);
11128 /* Construct a basic map mapping the domain the multi-affine expression
11129 * to its range, with each dimension in the range equated to the
11130 * corresponding affine expression.
11132 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11133 __isl_take isl_multi_aff *maff)
11135 int i;
11136 isl_space *space;
11137 isl_basic_map *bmap;
11139 if (!maff)
11140 return NULL;
11142 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11143 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11144 "invalid space", return isl_multi_aff_free(maff));
11146 space = isl_space_domain(isl_multi_aff_get_space(maff));
11147 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11149 for (i = 0; i < maff->n; ++i) {
11150 isl_aff *aff;
11151 isl_basic_map *bmap_i;
11153 aff = isl_aff_copy(maff->p[i]);
11154 bmap_i = isl_basic_map_from_aff(aff);
11156 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11159 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11161 isl_multi_aff_free(maff);
11162 return bmap;
11165 /* Construct a map mapping the domain the multi-affine expression
11166 * to its range, with each dimension in the range equated to the
11167 * corresponding affine expression.
11169 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11171 isl_basic_map *bmap;
11173 bmap = isl_basic_map_from_multi_aff(maff);
11174 return isl_map_from_basic_map(bmap);
11177 /* Construct a basic map mapping a domain in the given space to
11178 * to an n-dimensional range, with n the number of elements in the list,
11179 * where each coordinate in the range is prescribed by the
11180 * corresponding affine expression.
11181 * The domains of all affine expressions in the list are assumed to match
11182 * domain_dim.
11184 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11185 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11187 int i;
11188 isl_space *dim;
11189 isl_basic_map *bmap;
11191 if (!list)
11192 return NULL;
11194 dim = isl_space_from_domain(domain_dim);
11195 bmap = isl_basic_map_universe(dim);
11197 for (i = 0; i < list->n; ++i) {
11198 isl_aff *aff;
11199 isl_basic_map *bmap_i;
11201 aff = isl_aff_copy(list->p[i]);
11202 bmap_i = isl_basic_map_from_aff(aff);
11204 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11207 isl_aff_list_free(list);
11208 return bmap;
11211 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11212 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11214 return isl_map_equate(set, type1, pos1, type2, pos2);
11217 /* Construct a basic map where the given dimensions are equal to each other.
11219 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11220 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11222 isl_basic_map *bmap = NULL;
11223 int i;
11225 if (!space)
11226 return NULL;
11228 if (pos1 >= isl_space_dim(space, type1))
11229 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11230 "index out of bounds", goto error);
11231 if (pos2 >= isl_space_dim(space, type2))
11232 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11233 "index out of bounds", goto error);
11235 if (type1 == type2 && pos1 == pos2)
11236 return isl_basic_map_universe(space);
11238 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11239 i = isl_basic_map_alloc_equality(bmap);
11240 if (i < 0)
11241 goto error;
11242 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11243 pos1 += isl_basic_map_offset(bmap, type1);
11244 pos2 += isl_basic_map_offset(bmap, type2);
11245 isl_int_set_si(bmap->eq[i][pos1], -1);
11246 isl_int_set_si(bmap->eq[i][pos2], 1);
11247 bmap = isl_basic_map_finalize(bmap);
11248 isl_space_free(space);
11249 return bmap;
11250 error:
11251 isl_space_free(space);
11252 isl_basic_map_free(bmap);
11253 return NULL;
11256 /* Add a constraint imposing that the given two dimensions are equal.
11258 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11259 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11261 isl_basic_map *eq;
11263 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11265 bmap = isl_basic_map_intersect(bmap, eq);
11267 return bmap;
11270 /* Add a constraint imposing that the given two dimensions are equal.
11272 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11273 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11275 isl_basic_map *bmap;
11277 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11279 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11281 return map;
11284 /* Add a constraint imposing that the given two dimensions have opposite values.
11286 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11287 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11289 isl_basic_map *bmap = NULL;
11290 int i;
11292 if (!map)
11293 return NULL;
11295 if (pos1 >= isl_map_dim(map, type1))
11296 isl_die(map->ctx, isl_error_invalid,
11297 "index out of bounds", goto error);
11298 if (pos2 >= isl_map_dim(map, type2))
11299 isl_die(map->ctx, isl_error_invalid,
11300 "index out of bounds", goto error);
11302 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11303 i = isl_basic_map_alloc_equality(bmap);
11304 if (i < 0)
11305 goto error;
11306 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11307 pos1 += isl_basic_map_offset(bmap, type1);
11308 pos2 += isl_basic_map_offset(bmap, type2);
11309 isl_int_set_si(bmap->eq[i][pos1], 1);
11310 isl_int_set_si(bmap->eq[i][pos2], 1);
11311 bmap = isl_basic_map_finalize(bmap);
11313 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11315 return map;
11316 error:
11317 isl_basic_map_free(bmap);
11318 isl_map_free(map);
11319 return NULL;
11322 /* Add a constraint imposing that the value of the first dimension is
11323 * greater than or equal to that of the second.
11325 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11326 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11328 isl_constraint *c;
11329 isl_local_space *ls;
11331 if (!bmap)
11332 return NULL;
11334 if (pos1 >= isl_basic_map_dim(bmap, type1))
11335 isl_die(bmap->ctx, isl_error_invalid,
11336 "index out of bounds", return isl_basic_map_free(bmap));
11337 if (pos2 >= isl_basic_map_dim(bmap, type2))
11338 isl_die(bmap->ctx, isl_error_invalid,
11339 "index out of bounds", return isl_basic_map_free(bmap));
11341 if (type1 == type2 && pos1 == pos2)
11342 return bmap;
11344 ls = isl_local_space_from_space(isl_basic_map_get_space(bmap));
11345 c = isl_inequality_alloc(ls);
11346 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11347 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11348 bmap = isl_basic_map_add_constraint(bmap, c);
11350 return bmap;
11353 /* Construct a basic map where the value of the first dimension is
11354 * greater than that of the second.
11356 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11357 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11359 isl_basic_map *bmap = NULL;
11360 int i;
11362 if (!space)
11363 return NULL;
11365 if (pos1 >= isl_space_dim(space, type1))
11366 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11367 "index out of bounds", goto error);
11368 if (pos2 >= isl_space_dim(space, type2))
11369 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11370 "index out of bounds", goto error);
11372 if (type1 == type2 && pos1 == pos2)
11373 return isl_basic_map_empty(space);
11375 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11376 i = isl_basic_map_alloc_inequality(bmap);
11377 if (i < 0)
11378 goto error;
11379 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11380 pos1 += isl_basic_map_offset(bmap, type1);
11381 pos2 += isl_basic_map_offset(bmap, type2);
11382 isl_int_set_si(bmap->ineq[i][pos1], 1);
11383 isl_int_set_si(bmap->ineq[i][pos2], -1);
11384 isl_int_set_si(bmap->ineq[i][0], -1);
11385 bmap = isl_basic_map_finalize(bmap);
11387 return bmap;
11388 error:
11389 isl_space_free(space);
11390 isl_basic_map_free(bmap);
11391 return NULL;
11394 /* Add a constraint imposing that the value of the first dimension is
11395 * greater than that of the second.
11397 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11398 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11400 isl_basic_map *gt;
11402 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11404 bmap = isl_basic_map_intersect(bmap, gt);
11406 return bmap;
11409 /* Add a constraint imposing that the value of the first dimension is
11410 * greater than that of the second.
11412 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11413 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11415 isl_basic_map *bmap;
11417 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11419 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11421 return map;
11424 /* Add a constraint imposing that the value of the first dimension is
11425 * smaller than that of the second.
11427 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11428 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11430 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11433 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11434 int pos)
11436 isl_aff *div;
11437 isl_local_space *ls;
11439 if (!bmap)
11440 return NULL;
11442 if (!isl_basic_map_divs_known(bmap))
11443 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11444 "some divs are unknown", return NULL);
11446 ls = isl_basic_map_get_local_space(bmap);
11447 div = isl_local_space_get_div(ls, pos);
11448 isl_local_space_free(ls);
11450 return div;
11453 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11454 int pos)
11456 return isl_basic_map_get_div(bset, pos);
11459 /* Plug in "subs" for dimension "type", "pos" of "bset".
11461 * Let i be the dimension to replace and let "subs" be of the form
11463 * f/d
11465 * Any integer division with a non-zero coefficient for i,
11467 * floor((a i + g)/m)
11469 * is replaced by
11471 * floor((a f + d g)/(m d))
11473 * Constraints of the form
11475 * a i + g
11477 * are replaced by
11479 * a f + d g
11481 * We currently require that "subs" is an integral expression.
11482 * Handling rational expressions may require us to add stride constraints
11483 * as we do in isl_basic_set_preimage_multi_aff.
11485 __isl_give isl_basic_set *isl_basic_set_substitute(
11486 __isl_take isl_basic_set *bset,
11487 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11489 int i;
11490 isl_int v;
11491 isl_ctx *ctx;
11493 if (bset && isl_basic_set_plain_is_empty(bset))
11494 return bset;
11496 bset = isl_basic_set_cow(bset);
11497 if (!bset || !subs)
11498 goto error;
11500 ctx = isl_basic_set_get_ctx(bset);
11501 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11502 isl_die(ctx, isl_error_invalid,
11503 "spaces don't match", goto error);
11504 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11505 isl_die(ctx, isl_error_unsupported,
11506 "cannot handle divs yet", goto error);
11507 if (!isl_int_is_one(subs->v->el[0]))
11508 isl_die(ctx, isl_error_invalid,
11509 "can only substitute integer expressions", goto error);
11511 pos += isl_basic_set_offset(bset, type);
11513 isl_int_init(v);
11515 for (i = 0; i < bset->n_eq; ++i) {
11516 if (isl_int_is_zero(bset->eq[i][pos]))
11517 continue;
11518 isl_int_set(v, bset->eq[i][pos]);
11519 isl_int_set_si(bset->eq[i][pos], 0);
11520 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11521 v, subs->v->el + 1, subs->v->size - 1);
11524 for (i = 0; i < bset->n_ineq; ++i) {
11525 if (isl_int_is_zero(bset->ineq[i][pos]))
11526 continue;
11527 isl_int_set(v, bset->ineq[i][pos]);
11528 isl_int_set_si(bset->ineq[i][pos], 0);
11529 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11530 v, subs->v->el + 1, subs->v->size - 1);
11533 for (i = 0; i < bset->n_div; ++i) {
11534 if (isl_int_is_zero(bset->div[i][1 + pos]))
11535 continue;
11536 isl_int_set(v, bset->div[i][1 + pos]);
11537 isl_int_set_si(bset->div[i][1 + pos], 0);
11538 isl_seq_combine(bset->div[i] + 1,
11539 subs->v->el[0], bset->div[i] + 1,
11540 v, subs->v->el + 1, subs->v->size - 1);
11541 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11544 isl_int_clear(v);
11546 bset = isl_basic_set_simplify(bset);
11547 return isl_basic_set_finalize(bset);
11548 error:
11549 isl_basic_set_free(bset);
11550 return NULL;
11553 /* Plug in "subs" for dimension "type", "pos" of "set".
11555 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11556 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11558 int i;
11560 if (set && isl_set_plain_is_empty(set))
11561 return set;
11563 set = isl_set_cow(set);
11564 if (!set || !subs)
11565 goto error;
11567 for (i = set->n - 1; i >= 0; --i) {
11568 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11569 if (remove_if_empty(set, i) < 0)
11570 goto error;
11573 return set;
11574 error:
11575 isl_set_free(set);
11576 return NULL;
11579 /* Check if the range of "ma" is compatible with the domain or range
11580 * (depending on "type") of "bmap".
11581 * Return -1 if anything is wrong.
11583 static int check_basic_map_compatible_range_multi_aff(
11584 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11585 __isl_keep isl_multi_aff *ma)
11587 int m;
11588 isl_space *ma_space;
11590 ma_space = isl_multi_aff_get_space(ma);
11591 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11592 isl_space_free(ma_space);
11593 if (m >= 0 && !m)
11594 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11595 "spaces don't match", return -1);
11596 return m;
11599 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11600 * coefficients before the transformed range of dimensions,
11601 * the "n_after" coefficients after the transformed range of dimensions
11602 * and the coefficients of the other divs in "bmap".
11604 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11605 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11607 int i;
11608 int n_param;
11609 int n_set;
11610 isl_local_space *ls;
11612 if (n_div == 0)
11613 return 0;
11615 ls = isl_aff_get_domain_local_space(ma->p[0]);
11616 if (!ls)
11617 return -1;
11619 n_param = isl_local_space_dim(ls, isl_dim_param);
11620 n_set = isl_local_space_dim(ls, isl_dim_set);
11621 for (i = 0; i < n_div; ++i) {
11622 int o_bmap = 0, o_ls = 0;
11624 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
11625 o_bmap += 1 + 1 + n_param;
11626 o_ls += 1 + 1 + n_param;
11627 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
11628 o_bmap += n_before;
11629 isl_seq_cpy(bmap->div[i] + o_bmap,
11630 ls->div->row[i] + o_ls, n_set);
11631 o_bmap += n_set;
11632 o_ls += n_set;
11633 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
11634 o_bmap += n_after;
11635 isl_seq_cpy(bmap->div[i] + o_bmap,
11636 ls->div->row[i] + o_ls, n_div);
11637 o_bmap += n_div;
11638 o_ls += n_div;
11639 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
11640 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
11641 goto error;
11644 isl_local_space_free(ls);
11645 return 0;
11646 error:
11647 isl_local_space_free(ls);
11648 return -1;
11651 /* How many stride constraints does "ma" enforce?
11652 * That is, how many of the affine expressions have a denominator
11653 * different from one?
11655 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11657 int i;
11658 int strides = 0;
11660 for (i = 0; i < ma->n; ++i)
11661 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11662 strides++;
11664 return strides;
11667 /* For each affine expression in ma of the form
11669 * x_i = (f_i y + h_i)/m_i
11671 * with m_i different from one, add a constraint to "bmap"
11672 * of the form
11674 * f_i y + h_i = m_i alpha_i
11676 * with alpha_i an additional existentially quantified variable.
11678 static __isl_give isl_basic_map *add_ma_strides(
11679 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
11680 int n_before, int n_after)
11682 int i, k;
11683 int div;
11684 int total;
11685 int n_param;
11686 int n_in;
11687 int n_div;
11689 total = isl_basic_map_total_dim(bmap);
11690 n_param = isl_multi_aff_dim(ma, isl_dim_param);
11691 n_in = isl_multi_aff_dim(ma, isl_dim_in);
11692 n_div = isl_multi_aff_dim(ma, isl_dim_div);
11693 for (i = 0; i < ma->n; ++i) {
11694 int o_bmap = 0, o_ma = 1;
11696 if (isl_int_is_one(ma->p[i]->v->el[0]))
11697 continue;
11698 div = isl_basic_map_alloc_div(bmap);
11699 k = isl_basic_map_alloc_equality(bmap);
11700 if (div < 0 || k < 0)
11701 goto error;
11702 isl_int_set_si(bmap->div[div][0], 0);
11703 isl_seq_cpy(bmap->eq[k] + o_bmap,
11704 ma->p[i]->v->el + o_ma, 1 + n_param);
11705 o_bmap += 1 + n_param;
11706 o_ma += 1 + n_param;
11707 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
11708 o_bmap += n_before;
11709 isl_seq_cpy(bmap->eq[k] + o_bmap,
11710 ma->p[i]->v->el + o_ma, n_in);
11711 o_bmap += n_in;
11712 o_ma += n_in;
11713 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
11714 o_bmap += n_after;
11715 isl_seq_cpy(bmap->eq[k] + o_bmap,
11716 ma->p[i]->v->el + o_ma, n_div);
11717 o_bmap += n_div;
11718 o_ma += n_div;
11719 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
11720 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
11721 total++;
11724 return bmap;
11725 error:
11726 isl_basic_map_free(bmap);
11727 return NULL;
11730 /* Replace the domain or range space (depending on "type) of "space" by "set".
11732 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
11733 enum isl_dim_type type, __isl_take isl_space *set)
11735 if (type == isl_dim_in) {
11736 space = isl_space_range(space);
11737 space = isl_space_map_from_domain_and_range(set, space);
11738 } else {
11739 space = isl_space_domain(space);
11740 space = isl_space_map_from_domain_and_range(space, set);
11743 return space;
11746 /* Compute the preimage of the domain or range (depending on "type")
11747 * of "bmap" under the function represented by "ma".
11748 * In other words, plug in "ma" in the domain or range of "bmap".
11749 * The result is a basic map that lives in the same space as "bmap"
11750 * except that the domain or range has been replaced by
11751 * the domain space of "ma".
11753 * If bmap is represented by
11755 * A(p) + S u + B x + T v + C(divs) >= 0,
11757 * where u and x are input and output dimensions if type == isl_dim_out
11758 * while x and v are input and output dimensions if type == isl_dim_in,
11759 * and ma is represented by
11761 * x = D(p) + F(y) + G(divs')
11763 * then the result is
11765 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11767 * The divs in the input set are similarly adjusted.
11768 * In particular
11770 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11772 * becomes
11774 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11775 * B_i G(divs') + c_i(divs))/n_i)
11777 * If bmap is not a rational map and if F(y) involves any denominators
11779 * x_i = (f_i y + h_i)/m_i
11781 * then additional constraints are added to ensure that we only
11782 * map back integer points. That is we enforce
11784 * f_i y + h_i = m_i alpha_i
11786 * with alpha_i an additional existentially quantified variable.
11788 * We first copy over the divs from "ma".
11789 * Then we add the modified constraints and divs from "bmap".
11790 * Finally, we add the stride constraints, if needed.
11792 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
11793 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
11794 __isl_take isl_multi_aff *ma)
11796 int i, k;
11797 isl_space *space;
11798 isl_basic_map *res = NULL;
11799 int n_before, n_after, n_div_bmap, n_div_ma;
11800 isl_int f, c1, c2, g;
11801 int rational, strides;
11803 isl_int_init(f);
11804 isl_int_init(c1);
11805 isl_int_init(c2);
11806 isl_int_init(g);
11808 ma = isl_multi_aff_align_divs(ma);
11809 if (!bmap || !ma)
11810 goto error;
11811 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
11812 goto error;
11814 if (type == isl_dim_in) {
11815 n_before = 0;
11816 n_after = isl_basic_map_dim(bmap, isl_dim_out);
11817 } else {
11818 n_before = isl_basic_map_dim(bmap, isl_dim_in);
11819 n_after = 0;
11821 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
11822 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11824 space = isl_multi_aff_get_domain_space(ma);
11825 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
11826 rational = isl_basic_map_is_rational(bmap);
11827 strides = rational ? 0 : multi_aff_strides(ma);
11828 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
11829 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
11830 if (rational)
11831 res = isl_basic_map_set_rational(res);
11833 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
11834 if (isl_basic_map_alloc_div(res) < 0)
11835 goto error;
11837 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
11838 goto error;
11840 for (i = 0; i < bmap->n_eq; ++i) {
11841 k = isl_basic_map_alloc_equality(res);
11842 if (k < 0)
11843 goto error;
11844 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
11845 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11848 for (i = 0; i < bmap->n_ineq; ++i) {
11849 k = isl_basic_map_alloc_inequality(res);
11850 if (k < 0)
11851 goto error;
11852 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
11853 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11856 for (i = 0; i < bmap->n_div; ++i) {
11857 if (isl_int_is_zero(bmap->div[i][0])) {
11858 isl_int_set_si(res->div[n_div_ma + i][0], 0);
11859 continue;
11861 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
11862 n_before, n_after, n_div_ma, n_div_bmap,
11863 f, c1, c2, g, 1);
11866 if (strides)
11867 res = add_ma_strides(res, ma, n_before, n_after);
11869 isl_int_clear(f);
11870 isl_int_clear(c1);
11871 isl_int_clear(c2);
11872 isl_int_clear(g);
11873 isl_basic_map_free(bmap);
11874 isl_multi_aff_free(ma);
11875 res = isl_basic_set_simplify(res);
11876 return isl_basic_map_finalize(res);
11877 error:
11878 isl_int_clear(f);
11879 isl_int_clear(c1);
11880 isl_int_clear(c2);
11881 isl_int_clear(g);
11882 isl_basic_map_free(bmap);
11883 isl_multi_aff_free(ma);
11884 isl_basic_map_free(res);
11885 return NULL;
11888 /* Compute the preimage of "bset" under the function represented by "ma".
11889 * In other words, plug in "ma" in "bset". The result is a basic set
11890 * that lives in the domain space of "ma".
11892 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
11893 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
11895 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
11898 /* Compute the preimage of the domain of "bmap" under the function
11899 * represented by "ma".
11900 * In other words, plug in "ma" in the domain of "bmap".
11901 * The result is a basic map that lives in the same space as "bmap"
11902 * except that the domain has been replaced by the domain space of "ma".
11904 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
11905 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
11907 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
11910 /* Compute the preimage of the range of "bmap" under the function
11911 * represented by "ma".
11912 * In other words, plug in "ma" in the range of "bmap".
11913 * The result is a basic map that lives in the same space as "bmap"
11914 * except that the range has been replaced by the domain space of "ma".
11916 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
11917 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
11919 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
11922 /* Check if the range of "ma" is compatible with the domain or range
11923 * (depending on "type") of "map".
11924 * Return -1 if anything is wrong.
11926 static int check_map_compatible_range_multi_aff(
11927 __isl_keep isl_map *map, enum isl_dim_type type,
11928 __isl_keep isl_multi_aff *ma)
11930 int m;
11931 isl_space *ma_space;
11933 ma_space = isl_multi_aff_get_space(ma);
11934 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
11935 isl_space_free(ma_space);
11936 if (m >= 0 && !m)
11937 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11938 "spaces don't match", return -1);
11939 return m;
11942 /* Compute the preimage of the domain or range (depending on "type")
11943 * of "map" under the function represented by "ma".
11944 * In other words, plug in "ma" in the domain or range of "map".
11945 * The result is a map that lives in the same space as "map"
11946 * except that the domain or range has been replaced by
11947 * the domain space of "ma".
11949 * The parameters are assumed to have been aligned.
11951 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
11952 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11954 int i;
11955 isl_space *space;
11957 map = isl_map_cow(map);
11958 ma = isl_multi_aff_align_divs(ma);
11959 if (!map || !ma)
11960 goto error;
11961 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
11962 goto error;
11964 for (i = 0; i < map->n; ++i) {
11965 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
11966 isl_multi_aff_copy(ma));
11967 if (!map->p[i])
11968 goto error;
11971 space = isl_multi_aff_get_domain_space(ma);
11972 space = isl_space_set(isl_map_get_space(map), type, space);
11974 isl_space_free(map->dim);
11975 map->dim = space;
11976 if (!map->dim)
11977 goto error;
11979 isl_multi_aff_free(ma);
11980 if (map->n > 1)
11981 ISL_F_CLR(map, ISL_MAP_DISJOINT);
11982 ISL_F_CLR(map, ISL_SET_NORMALIZED);
11983 return map;
11984 error:
11985 isl_multi_aff_free(ma);
11986 isl_map_free(map);
11987 return NULL;
11990 /* Compute the preimage of the domain or range (depending on "type")
11991 * of "map" under the function represented by "ma".
11992 * In other words, plug in "ma" in the domain or range of "map".
11993 * The result is a map that lives in the same space as "map"
11994 * except that the domain or range has been replaced by
11995 * the domain space of "ma".
11997 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
11998 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12000 if (!map || !ma)
12001 goto error;
12003 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12004 return map_preimage_multi_aff(map, type, ma);
12006 if (!isl_space_has_named_params(map->dim) ||
12007 !isl_space_has_named_params(ma->space))
12008 isl_die(map->ctx, isl_error_invalid,
12009 "unaligned unnamed parameters", goto error);
12010 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12011 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12013 return map_preimage_multi_aff(map, type, ma);
12014 error:
12015 isl_multi_aff_free(ma);
12016 return isl_map_free(map);
12019 /* Compute the preimage of "set" under the function represented by "ma".
12020 * In other words, plug in "ma" "set". The result is a set
12021 * that lives in the domain space of "ma".
12023 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12024 __isl_take isl_multi_aff *ma)
12026 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12029 /* Compute the preimage of the domain of "map" under the function
12030 * represented by "ma".
12031 * In other words, plug in "ma" in the domain of "map".
12032 * The result is a map that lives in the same space as "map"
12033 * except that the domain has been replaced by the domain space of "ma".
12035 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12036 __isl_take isl_multi_aff *ma)
12038 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12041 /* Compute the preimage of "map" under the function represented by "pma".
12042 * In other words, plug in "pma" in the domain or range of "map".
12043 * The result is a map that lives in the same space as "map",
12044 * except that the space of type "type" has been replaced by
12045 * the domain space of "pma".
12047 * The parameters of "map" and "pma" are assumed to have been aligned.
12049 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12050 __isl_take isl_map *map, enum isl_dim_type type,
12051 __isl_take isl_pw_multi_aff *pma)
12053 int i;
12054 isl_map *res;
12056 if (!pma)
12057 goto error;
12059 if (pma->n == 0) {
12060 isl_pw_multi_aff_free(pma);
12061 res = isl_map_empty(isl_map_get_space(map));
12062 isl_map_free(map);
12063 return res;
12066 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12067 isl_multi_aff_copy(pma->p[0].maff));
12068 if (type == isl_dim_in)
12069 res = isl_map_intersect_domain(res,
12070 isl_map_copy(pma->p[0].set));
12071 else
12072 res = isl_map_intersect_range(res,
12073 isl_map_copy(pma->p[0].set));
12075 for (i = 1; i < pma->n; ++i) {
12076 isl_map *res_i;
12078 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12079 isl_multi_aff_copy(pma->p[i].maff));
12080 if (type == isl_dim_in)
12081 res_i = isl_map_intersect_domain(res_i,
12082 isl_map_copy(pma->p[i].set));
12083 else
12084 res_i = isl_map_intersect_range(res_i,
12085 isl_map_copy(pma->p[i].set));
12086 res = isl_map_union(res, res_i);
12089 isl_pw_multi_aff_free(pma);
12090 isl_map_free(map);
12091 return res;
12092 error:
12093 isl_pw_multi_aff_free(pma);
12094 isl_map_free(map);
12095 return NULL;
12098 /* Compute the preimage of "map" under the function represented by "pma".
12099 * In other words, plug in "pma" in the domain or range of "map".
12100 * The result is a map that lives in the same space as "map",
12101 * except that the space of type "type" has been replaced by
12102 * the domain space of "pma".
12104 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12105 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12107 if (!map || !pma)
12108 goto error;
12110 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12111 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12113 if (!isl_space_has_named_params(map->dim) ||
12114 !isl_space_has_named_params(pma->dim))
12115 isl_die(map->ctx, isl_error_invalid,
12116 "unaligned unnamed parameters", goto error);
12117 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12118 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12120 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12121 error:
12122 isl_pw_multi_aff_free(pma);
12123 return isl_map_free(map);
12126 /* Compute the preimage of "set" under the function represented by "pma".
12127 * In other words, plug in "pma" in "set". The result is a set
12128 * that lives in the domain space of "pma".
12130 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12131 __isl_take isl_pw_multi_aff *pma)
12133 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12136 /* Compute the preimage of "map" under the function represented by "pma".
12137 * In other words, plug in "pma" in the domain "map".
12138 * The result is a map that lives in the same space as "map",
12139 * except that domain space has been replaced by the domain space of "pma".
12141 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12142 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12144 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12147 /* Compute the preimage of "map" under the function represented by "mpa".
12148 * In other words, plug in "mpa" in the domain or range of "map".
12149 * The result is a map that lives in the same space as "map",
12150 * except that the space of type "type" has been replaced by
12151 * the domain space of "mpa".
12153 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12154 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12156 isl_pw_multi_aff *pma;
12158 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12159 return isl_map_preimage_pw_multi_aff(map, type, pma);
12162 /* Compute the preimage of "map" under the function represented by "mpa".
12163 * In other words, plug in "mpa" in the domain "map".
12164 * The result is a map that lives in the same space as "map",
12165 * except that domain space has been replaced by the domain space of "mpa".
12167 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12168 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12170 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12173 /* Compute the preimage of "set" by the function represented by "mpa".
12174 * In other words, plug in "mpa" in "set".
12176 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12177 __isl_take isl_multi_pw_aff *mpa)
12179 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);