add isl_map_has_dim_name
[isl.git] / isl_map.c
blob44a2056f9ec94552c932a6bcc7d2161b48c91022
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the GNU LGPLv2.1 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_list_private.h>
22 #include <isl/lp.h>
23 #include <isl/seq.h>
24 #include <isl/set.h>
25 #include <isl/map.h>
26 #include "isl_map_piplib.h"
27 #include <isl_reordering.h>
28 #include "isl_sample.h"
29 #include "isl_tab.h"
30 #include <isl/vec.h>
31 #include <isl_mat_private.h>
32 #include <isl_dim_map.h>
33 #include <isl_local_space_private.h>
34 #include <isl_aff_private.h>
35 #include <isl_options_private.h>
37 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
39 switch (type) {
40 case isl_dim_param: return dim->nparam;
41 case isl_dim_in: return dim->n_in;
42 case isl_dim_out: return dim->n_out;
43 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
44 default: return 0;
48 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
50 switch (type) {
51 case isl_dim_param: return 1;
52 case isl_dim_in: return 1 + dim->nparam;
53 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
54 default: return 0;
58 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
59 enum isl_dim_type type)
61 if (!bmap)
62 return 0;
63 switch (type) {
64 case isl_dim_cst: return 1;
65 case isl_dim_param:
66 case isl_dim_in:
67 case isl_dim_out: return isl_space_dim(bmap->dim, type);
68 case isl_dim_div: return bmap->n_div;
69 case isl_dim_all: return isl_basic_map_total_dim(bmap);
70 default: return 0;
74 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
76 return map ? n(map->dim, type) : 0;
79 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
81 return set ? n(set->dim, type) : 0;
84 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
85 enum isl_dim_type type)
87 isl_space *dim = bmap->dim;
88 switch (type) {
89 case isl_dim_cst: return 0;
90 case isl_dim_param: return 1;
91 case isl_dim_in: return 1 + dim->nparam;
92 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
93 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
94 default: return 0;
98 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
99 enum isl_dim_type type)
101 return isl_basic_map_offset(bset, type);
104 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
106 return pos(map->dim, type);
109 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
110 enum isl_dim_type type)
112 return isl_basic_map_dim(bset, type);
115 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
117 return isl_basic_set_dim(bset, isl_dim_set);
120 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
122 return isl_basic_set_dim(bset, isl_dim_param);
125 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
127 if (!bset)
128 return 0;
129 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
132 unsigned isl_set_n_dim(__isl_keep isl_set *set)
134 return isl_set_dim(set, isl_dim_set);
137 unsigned isl_set_n_param(__isl_keep isl_set *set)
139 return isl_set_dim(set, isl_dim_param);
142 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
144 return bmap ? bmap->dim->n_in : 0;
147 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
149 return bmap ? bmap->dim->n_out : 0;
152 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
154 return bmap ? bmap->dim->nparam : 0;
157 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
159 return bmap ? bmap->n_div : 0;
162 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
164 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
167 unsigned isl_map_n_in(const struct isl_map *map)
169 return map ? map->dim->n_in : 0;
172 unsigned isl_map_n_out(const struct isl_map *map)
174 return map ? map->dim->n_out : 0;
177 unsigned isl_map_n_param(const struct isl_map *map)
179 return map ? map->dim->nparam : 0;
182 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
184 int m;
185 if (!map || !set)
186 return -1;
187 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
188 if (m < 0 || !m)
189 return m;
190 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
193 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
194 struct isl_basic_set *bset)
196 int m;
197 if (!bmap || !bset)
198 return -1;
199 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
200 if (m < 0 || !m)
201 return m;
202 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
205 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
207 int m;
208 if (!map || !set)
209 return -1;
210 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
211 if (m < 0 || !m)
212 return m;
213 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
216 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
217 struct isl_basic_set *bset)
219 int m;
220 if (!bmap || !bset)
221 return -1;
222 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
223 if (m < 0 || !m)
224 return m;
225 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
228 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
230 return bmap ? bmap->ctx : NULL;
233 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
235 return bset ? bset->ctx : NULL;
238 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
240 return map ? map->ctx : NULL;
243 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
245 return set ? set->ctx : NULL;
248 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
250 if (!bmap)
251 return NULL;
252 return isl_space_copy(bmap->dim);
255 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
257 if (!bset)
258 return NULL;
259 return isl_space_copy(bset->dim);
262 /* Extract the divs in "bmap" as a matrix.
264 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
266 int i;
267 isl_ctx *ctx;
268 isl_mat *div;
269 unsigned total;
270 unsigned cols;
272 if (!bmap)
273 return NULL;
275 ctx = isl_basic_map_get_ctx(bmap);
276 total = isl_space_dim(bmap->dim, isl_dim_all);
277 cols = 1 + 1 + total + bmap->n_div;
278 div = isl_mat_alloc(ctx, bmap->n_div, cols);
279 if (!div)
280 return NULL;
282 for (i = 0; i < bmap->n_div; ++i)
283 isl_seq_cpy(div->row[i], bmap->div[i], cols);
285 return div;
288 __isl_give isl_local_space *isl_basic_map_get_local_space(
289 __isl_keep isl_basic_map *bmap)
291 isl_mat *div;
293 if (!bmap)
294 return NULL;
296 div = isl_basic_map_get_divs(bmap);
297 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
300 __isl_give isl_local_space *isl_basic_set_get_local_space(
301 __isl_keep isl_basic_set *bset)
303 return isl_basic_map_get_local_space(bset);
306 __isl_give isl_basic_map *isl_basic_map_from_local_space(
307 __isl_take isl_local_space *ls)
309 int i;
310 int n_div;
311 isl_basic_map *bmap;
313 if (!ls)
314 return NULL;
316 n_div = isl_local_space_dim(ls, isl_dim_div);
317 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
318 n_div, 0, 2 * n_div);
320 for (i = 0; i < n_div; ++i)
321 if (isl_basic_map_alloc_div(bmap) < 0)
322 goto error;
324 for (i = 0; i < n_div; ++i) {
325 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
326 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
327 goto error;
330 isl_local_space_free(ls);
331 return bmap;
332 error:
333 isl_local_space_free(ls);
334 isl_basic_map_free(bmap);
335 return NULL;
338 __isl_give isl_basic_set *isl_basic_set_from_local_space(
339 __isl_take isl_local_space *ls)
341 return isl_basic_map_from_local_space(ls);
344 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
346 if (!map)
347 return NULL;
348 return isl_space_copy(map->dim);
351 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
353 if (!set)
354 return NULL;
355 return isl_space_copy(set->dim);
358 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
359 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
361 bmap = isl_basic_map_cow(bmap);
362 if (!bmap)
363 return NULL;
364 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
365 if (!bmap->dim)
366 goto error;
367 bmap = isl_basic_map_finalize(bmap);
368 return bmap;
369 error:
370 isl_basic_map_free(bmap);
371 return NULL;
374 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
375 __isl_take isl_basic_set *bset, const char *s)
377 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
380 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
381 enum isl_dim_type type)
383 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
386 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
387 enum isl_dim_type type, const char *s)
389 int i;
391 map = isl_map_cow(map);
392 if (!map)
393 return NULL;
395 map->dim = isl_space_set_tuple_name(map->dim, type, s);
396 if (!map->dim)
397 goto error;
399 for (i = 0; i < map->n; ++i) {
400 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
401 if (!map->p[i])
402 goto error;
405 return map;
406 error:
407 isl_map_free(map);
408 return NULL;
411 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
412 enum isl_dim_type type)
414 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
417 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
418 const char *s)
420 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
423 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
424 enum isl_dim_type type, __isl_take isl_id *id)
426 map = isl_map_cow(map);
427 if (!map)
428 return isl_id_free(id);
430 map->dim = isl_space_set_tuple_id(map->dim, type, id);
432 return isl_map_reset_space(map, isl_space_copy(map->dim));
435 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
436 __isl_take isl_id *id)
438 return isl_map_set_tuple_id(set, isl_dim_set, id);
441 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
442 enum isl_dim_type type)
444 map = isl_map_cow(map);
445 if (!map)
446 return NULL;
448 map->dim = isl_space_reset_tuple_id(map->dim, type);
450 return isl_map_reset_space(map, isl_space_copy(map->dim));
453 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
455 return isl_map_reset_tuple_id(set, isl_dim_set);
458 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
460 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
463 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
464 enum isl_dim_type type)
466 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
469 int isl_set_has_tuple_id(__isl_keep isl_set *set)
471 return isl_map_has_tuple_id(set, isl_dim_set);
474 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
476 return isl_map_get_tuple_id(set, isl_dim_set);
479 /* Does the set tuple have a name?
481 int isl_set_has_tuple_name(__isl_keep isl_set *set)
483 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
487 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
489 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
492 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
494 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
497 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
498 enum isl_dim_type type, unsigned pos)
500 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
503 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
504 enum isl_dim_type type, unsigned pos)
506 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
509 /* Does the given dimension have a name?
511 int isl_map_has_dim_name(__isl_keep isl_map *map,
512 enum isl_dim_type type, unsigned pos)
514 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
517 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
518 enum isl_dim_type type, unsigned pos)
520 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
523 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
524 enum isl_dim_type type, unsigned pos)
526 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
529 /* Does the given dimension have a name?
531 int isl_set_has_dim_name(__isl_keep isl_set *set,
532 enum isl_dim_type type, unsigned pos)
534 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
537 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
538 __isl_take isl_basic_map *bmap,
539 enum isl_dim_type type, unsigned pos, const char *s)
541 bmap = isl_basic_map_cow(bmap);
542 if (!bmap)
543 return NULL;
544 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
545 if (!bmap->dim)
546 goto error;
547 return isl_basic_map_finalize(bmap);
548 error:
549 isl_basic_map_free(bmap);
550 return NULL;
553 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
554 enum isl_dim_type type, unsigned pos, const char *s)
556 int i;
558 map = isl_map_cow(map);
559 if (!map)
560 return NULL;
562 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
563 if (!map->dim)
564 goto error;
566 for (i = 0; i < map->n; ++i) {
567 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
568 if (!map->p[i])
569 goto error;
572 return map;
573 error:
574 isl_map_free(map);
575 return NULL;
578 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
579 __isl_take isl_basic_set *bset,
580 enum isl_dim_type type, unsigned pos, const char *s)
582 return (isl_basic_set *)isl_basic_map_set_dim_name(
583 (isl_basic_map *)bset, type, pos, s);
586 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
587 enum isl_dim_type type, unsigned pos, const char *s)
589 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
592 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
593 enum isl_dim_type type, unsigned pos)
595 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
598 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
599 enum isl_dim_type type, unsigned pos)
601 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
604 int isl_map_has_dim_id(__isl_keep isl_map *map,
605 enum isl_dim_type type, unsigned pos)
607 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
610 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
611 enum isl_dim_type type, unsigned pos)
613 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
616 int isl_set_has_dim_id(__isl_keep isl_set *set,
617 enum isl_dim_type type, unsigned pos)
619 return isl_map_has_dim_id(set, type, pos);
622 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
623 enum isl_dim_type type, unsigned pos)
625 return isl_map_get_dim_id(set, type, pos);
628 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
629 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
631 map = isl_map_cow(map);
632 if (!map)
633 return isl_id_free(id);
635 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
637 return isl_map_reset_space(map, isl_space_copy(map->dim));
640 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
641 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
643 return isl_map_set_dim_id(set, type, pos, id);
646 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
647 __isl_keep isl_id *id)
649 if (!map)
650 return -1;
651 return isl_space_find_dim_by_id(map->dim, type, id);
654 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
655 __isl_keep isl_id *id)
657 return isl_map_find_dim_by_id(set, type, id);
660 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
661 const char *name)
663 if (!map)
664 return -1;
665 return isl_space_find_dim_by_name(map->dim, type, name);
668 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
669 const char *name)
671 return isl_map_find_dim_by_name(set, type, name);
674 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
676 if (!bmap)
677 return -1;
678 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
681 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
683 return isl_basic_map_is_rational(bset);
686 /* Is this basic set a parameter domain?
688 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
690 if (!bset)
691 return -1;
692 return isl_space_is_params(bset->dim);
695 /* Is this set a parameter domain?
697 int isl_set_is_params(__isl_keep isl_set *set)
699 if (!set)
700 return -1;
701 return isl_space_is_params(set->dim);
704 /* Is this map actually a parameter domain?
705 * Users should never call this function. Outside of isl,
706 * a map can never be a parameter domain.
708 int isl_map_is_params(__isl_keep isl_map *map)
710 if (!map)
711 return -1;
712 return isl_space_is_params(map->dim);
715 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
716 struct isl_basic_map *bmap, unsigned extra,
717 unsigned n_eq, unsigned n_ineq)
719 int i;
720 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
722 bmap->ctx = ctx;
723 isl_ctx_ref(ctx);
725 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
726 if (isl_blk_is_error(bmap->block))
727 goto error;
729 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
730 if (!bmap->ineq)
731 goto error;
733 if (extra == 0) {
734 bmap->block2 = isl_blk_empty();
735 bmap->div = NULL;
736 } else {
737 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
738 if (isl_blk_is_error(bmap->block2))
739 goto error;
741 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
742 if (!bmap->div)
743 goto error;
746 for (i = 0; i < n_ineq + n_eq; ++i)
747 bmap->ineq[i] = bmap->block.data + i * row_size;
749 for (i = 0; i < extra; ++i)
750 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
752 bmap->ref = 1;
753 bmap->flags = 0;
754 bmap->c_size = n_eq + n_ineq;
755 bmap->eq = bmap->ineq + n_ineq;
756 bmap->extra = extra;
757 bmap->n_eq = 0;
758 bmap->n_ineq = 0;
759 bmap->n_div = 0;
760 bmap->sample = NULL;
762 return bmap;
763 error:
764 isl_basic_map_free(bmap);
765 return NULL;
768 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
769 unsigned nparam, unsigned dim, unsigned extra,
770 unsigned n_eq, unsigned n_ineq)
772 struct isl_basic_map *bmap;
773 isl_space *space;
775 space = isl_space_set_alloc(ctx, nparam, dim);
776 if (!space)
777 return NULL;
779 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
780 return (struct isl_basic_set *)bmap;
783 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
784 unsigned extra, unsigned n_eq, unsigned n_ineq)
786 struct isl_basic_map *bmap;
787 if (!dim)
788 return NULL;
789 isl_assert(dim->ctx, dim->n_in == 0, goto error);
790 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
791 return (struct isl_basic_set *)bmap;
792 error:
793 isl_space_free(dim);
794 return NULL;
797 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
798 unsigned extra, unsigned n_eq, unsigned n_ineq)
800 struct isl_basic_map *bmap;
802 if (!dim)
803 return NULL;
804 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
805 if (!bmap)
806 goto error;
807 bmap->dim = dim;
809 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
810 error:
811 isl_space_free(dim);
812 return NULL;
815 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
816 unsigned nparam, unsigned in, unsigned out, unsigned extra,
817 unsigned n_eq, unsigned n_ineq)
819 struct isl_basic_map *bmap;
820 isl_space *dim;
822 dim = isl_space_alloc(ctx, nparam, in, out);
823 if (!dim)
824 return NULL;
826 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
827 return bmap;
830 static void dup_constraints(
831 struct isl_basic_map *dst, struct isl_basic_map *src)
833 int i;
834 unsigned total = isl_basic_map_total_dim(src);
836 for (i = 0; i < src->n_eq; ++i) {
837 int j = isl_basic_map_alloc_equality(dst);
838 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
841 for (i = 0; i < src->n_ineq; ++i) {
842 int j = isl_basic_map_alloc_inequality(dst);
843 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
846 for (i = 0; i < src->n_div; ++i) {
847 int j = isl_basic_map_alloc_div(dst);
848 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
850 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
853 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
855 struct isl_basic_map *dup;
857 if (!bmap)
858 return NULL;
859 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
860 bmap->n_div, bmap->n_eq, bmap->n_ineq);
861 if (!dup)
862 return NULL;
863 dup_constraints(dup, bmap);
864 dup->flags = bmap->flags;
865 dup->sample = isl_vec_copy(bmap->sample);
866 return dup;
869 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
871 struct isl_basic_map *dup;
873 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
874 return (struct isl_basic_set *)dup;
877 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
879 if (!bset)
880 return NULL;
882 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
883 bset->ref++;
884 return bset;
886 return isl_basic_set_dup(bset);
889 struct isl_set *isl_set_copy(struct isl_set *set)
891 if (!set)
892 return NULL;
894 set->ref++;
895 return set;
898 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
900 if (!bmap)
901 return NULL;
903 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
904 bmap->ref++;
905 return bmap;
907 bmap = isl_basic_map_dup(bmap);
908 if (bmap)
909 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
910 return bmap;
913 struct isl_map *isl_map_copy(struct isl_map *map)
915 if (!map)
916 return NULL;
918 map->ref++;
919 return map;
922 void isl_basic_map_free(struct isl_basic_map *bmap)
924 if (!bmap)
925 return;
927 if (--bmap->ref > 0)
928 return;
930 isl_ctx_deref(bmap->ctx);
931 free(bmap->div);
932 isl_blk_free(bmap->ctx, bmap->block2);
933 free(bmap->ineq);
934 isl_blk_free(bmap->ctx, bmap->block);
935 isl_vec_free(bmap->sample);
936 isl_space_free(bmap->dim);
937 free(bmap);
940 void isl_basic_set_free(struct isl_basic_set *bset)
942 isl_basic_map_free((struct isl_basic_map *)bset);
945 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
947 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
950 __isl_give isl_map *isl_map_align_params_map_map_and(
951 __isl_take isl_map *map1, __isl_take isl_map *map2,
952 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
953 __isl_take isl_map *map2))
955 if (!map1 || !map2)
956 goto error;
957 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
958 return fn(map1, map2);
959 if (!isl_space_has_named_params(map1->dim) ||
960 !isl_space_has_named_params(map2->dim))
961 isl_die(map1->ctx, isl_error_invalid,
962 "unaligned unnamed parameters", goto error);
963 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
964 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
965 return fn(map1, map2);
966 error:
967 isl_map_free(map1);
968 isl_map_free(map2);
969 return NULL;
972 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
973 __isl_keep isl_map *map2,
974 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
976 int r;
978 if (!map1 || !map2)
979 return -1;
980 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
981 return fn(map1, map2);
982 if (!isl_space_has_named_params(map1->dim) ||
983 !isl_space_has_named_params(map2->dim))
984 isl_die(map1->ctx, isl_error_invalid,
985 "unaligned unnamed parameters", return -1);
986 map1 = isl_map_copy(map1);
987 map2 = isl_map_copy(map2);
988 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
989 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
990 r = fn(map1, map2);
991 isl_map_free(map1);
992 isl_map_free(map2);
993 return r;
996 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
998 struct isl_ctx *ctx;
999 if (!bmap)
1000 return -1;
1001 ctx = bmap->ctx;
1002 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1003 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1004 return -1);
1005 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1006 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1007 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1008 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1009 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1010 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1011 isl_int *t;
1012 int j = isl_basic_map_alloc_inequality(bmap);
1013 if (j < 0)
1014 return -1;
1015 t = bmap->ineq[j];
1016 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1017 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1018 bmap->eq[-1] = t;
1019 bmap->n_eq++;
1020 bmap->n_ineq--;
1021 bmap->eq--;
1022 return 0;
1024 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1025 bmap->extra - bmap->n_div);
1026 return bmap->n_eq++;
1029 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1031 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1034 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1036 if (!bmap)
1037 return -1;
1038 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1039 bmap->n_eq -= n;
1040 return 0;
1043 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1045 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1048 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1050 isl_int *t;
1051 if (!bmap)
1052 return -1;
1053 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1055 if (pos != bmap->n_eq - 1) {
1056 t = bmap->eq[pos];
1057 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1058 bmap->eq[bmap->n_eq - 1] = t;
1060 bmap->n_eq--;
1061 return 0;
1064 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1066 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1069 void isl_basic_map_inequality_to_equality(
1070 struct isl_basic_map *bmap, unsigned pos)
1072 isl_int *t;
1074 t = bmap->ineq[pos];
1075 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1076 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1077 bmap->eq[-1] = t;
1078 bmap->n_eq++;
1079 bmap->n_ineq--;
1080 bmap->eq--;
1081 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1082 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1083 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1084 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1087 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1089 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1092 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1094 struct isl_ctx *ctx;
1095 if (!bmap)
1096 return -1;
1097 ctx = bmap->ctx;
1098 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1099 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1100 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1101 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1102 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1103 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1104 1 + isl_basic_map_total_dim(bmap),
1105 bmap->extra - bmap->n_div);
1106 return bmap->n_ineq++;
1109 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1111 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1114 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1116 if (!bmap)
1117 return -1;
1118 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1119 bmap->n_ineq -= n;
1120 return 0;
1123 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1125 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1128 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1130 isl_int *t;
1131 if (!bmap)
1132 return -1;
1133 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1135 if (pos != bmap->n_ineq - 1) {
1136 t = bmap->ineq[pos];
1137 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1138 bmap->ineq[bmap->n_ineq - 1] = t;
1139 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1141 bmap->n_ineq--;
1142 return 0;
1145 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1147 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1150 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1151 isl_int *eq)
1153 int k;
1155 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1156 if (!bmap)
1157 return NULL;
1158 k = isl_basic_map_alloc_equality(bmap);
1159 if (k < 0)
1160 goto error;
1161 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1162 return bmap;
1163 error:
1164 isl_basic_map_free(bmap);
1165 return NULL;
1168 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1169 isl_int *eq)
1171 return (isl_basic_set *)
1172 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1175 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1176 isl_int *ineq)
1178 int k;
1180 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1181 if (!bmap)
1182 return NULL;
1183 k = isl_basic_map_alloc_inequality(bmap);
1184 if (k < 0)
1185 goto error;
1186 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1187 return bmap;
1188 error:
1189 isl_basic_map_free(bmap);
1190 return NULL;
1193 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1194 isl_int *ineq)
1196 return (isl_basic_set *)
1197 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1200 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1202 if (!bmap)
1203 return -1;
1204 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1205 isl_seq_clr(bmap->div[bmap->n_div] +
1206 1 + 1 + isl_basic_map_total_dim(bmap),
1207 bmap->extra - bmap->n_div);
1208 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1209 return bmap->n_div++;
1212 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1214 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1217 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1219 if (!bmap)
1220 return -1;
1221 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1222 bmap->n_div -= n;
1223 return 0;
1226 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1228 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1231 /* Copy constraint from src to dst, putting the vars of src at offset
1232 * dim_off in dst and the divs of src at offset div_off in dst.
1233 * If both sets are actually map, then dim_off applies to the input
1234 * variables.
1236 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1237 struct isl_basic_map *src_map, isl_int *src,
1238 unsigned in_off, unsigned out_off, unsigned div_off)
1240 unsigned src_nparam = isl_basic_map_n_param(src_map);
1241 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1242 unsigned src_in = isl_basic_map_n_in(src_map);
1243 unsigned dst_in = isl_basic_map_n_in(dst_map);
1244 unsigned src_out = isl_basic_map_n_out(src_map);
1245 unsigned dst_out = isl_basic_map_n_out(dst_map);
1246 isl_int_set(dst[0], src[0]);
1247 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1248 if (dst_nparam > src_nparam)
1249 isl_seq_clr(dst+1+src_nparam,
1250 dst_nparam - src_nparam);
1251 isl_seq_clr(dst+1+dst_nparam, in_off);
1252 isl_seq_cpy(dst+1+dst_nparam+in_off,
1253 src+1+src_nparam,
1254 isl_min(dst_in-in_off, src_in));
1255 if (dst_in-in_off > src_in)
1256 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1257 dst_in - in_off - src_in);
1258 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1259 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1260 src+1+src_nparam+src_in,
1261 isl_min(dst_out-out_off, src_out));
1262 if (dst_out-out_off > src_out)
1263 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1264 dst_out - out_off - src_out);
1265 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1266 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1267 src+1+src_nparam+src_in+src_out,
1268 isl_min(dst_map->extra-div_off, src_map->n_div));
1269 if (dst_map->n_div-div_off > src_map->n_div)
1270 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1271 div_off+src_map->n_div,
1272 dst_map->n_div - div_off - src_map->n_div);
1275 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1276 struct isl_basic_map *src_map, isl_int *src,
1277 unsigned in_off, unsigned out_off, unsigned div_off)
1279 isl_int_set(dst[0], src[0]);
1280 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1283 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1284 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1286 int i;
1287 unsigned div_off;
1289 if (!bmap1 || !bmap2)
1290 goto error;
1292 div_off = bmap1->n_div;
1294 for (i = 0; i < bmap2->n_eq; ++i) {
1295 int i1 = isl_basic_map_alloc_equality(bmap1);
1296 if (i1 < 0)
1297 goto error;
1298 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1299 i_pos, o_pos, div_off);
1302 for (i = 0; i < bmap2->n_ineq; ++i) {
1303 int i1 = isl_basic_map_alloc_inequality(bmap1);
1304 if (i1 < 0)
1305 goto error;
1306 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1307 i_pos, o_pos, div_off);
1310 for (i = 0; i < bmap2->n_div; ++i) {
1311 int i1 = isl_basic_map_alloc_div(bmap1);
1312 if (i1 < 0)
1313 goto error;
1314 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1315 i_pos, o_pos, div_off);
1318 isl_basic_map_free(bmap2);
1320 return bmap1;
1322 error:
1323 isl_basic_map_free(bmap1);
1324 isl_basic_map_free(bmap2);
1325 return NULL;
1328 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1329 struct isl_basic_set *bset2, unsigned pos)
1331 return (struct isl_basic_set *)
1332 add_constraints((struct isl_basic_map *)bset1,
1333 (struct isl_basic_map *)bset2, 0, pos);
1336 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1337 __isl_take isl_space *dim, unsigned extra,
1338 unsigned n_eq, unsigned n_ineq)
1340 struct isl_basic_map *ext;
1341 unsigned flags;
1342 int dims_ok;
1344 if (!dim)
1345 goto error;
1347 if (!base)
1348 goto error;
1350 dims_ok = isl_space_is_equal(base->dim, dim) &&
1351 base->extra >= base->n_div + extra;
1353 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1354 room_for_ineq(base, n_ineq)) {
1355 isl_space_free(dim);
1356 return base;
1359 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1360 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1361 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1362 extra += base->extra;
1363 n_eq += base->n_eq;
1364 n_ineq += base->n_ineq;
1366 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1367 dim = NULL;
1368 if (!ext)
1369 goto error;
1371 if (dims_ok)
1372 ext->sample = isl_vec_copy(base->sample);
1373 flags = base->flags;
1374 ext = add_constraints(ext, base, 0, 0);
1375 if (ext) {
1376 ext->flags = flags;
1377 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1380 return ext;
1382 error:
1383 isl_space_free(dim);
1384 isl_basic_map_free(base);
1385 return NULL;
1388 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1389 __isl_take isl_space *dim, unsigned extra,
1390 unsigned n_eq, unsigned n_ineq)
1392 return (struct isl_basic_set *)
1393 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1394 extra, n_eq, n_ineq);
1397 struct isl_basic_map *isl_basic_map_extend_constraints(
1398 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1400 if (!base)
1401 return NULL;
1402 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1403 0, n_eq, n_ineq);
1406 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1407 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1408 unsigned n_eq, unsigned n_ineq)
1410 struct isl_basic_map *bmap;
1411 isl_space *dim;
1413 if (!base)
1414 return NULL;
1415 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1416 if (!dim)
1417 goto error;
1419 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1420 return bmap;
1421 error:
1422 isl_basic_map_free(base);
1423 return NULL;
1426 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1427 unsigned nparam, unsigned dim, unsigned extra,
1428 unsigned n_eq, unsigned n_ineq)
1430 return (struct isl_basic_set *)
1431 isl_basic_map_extend((struct isl_basic_map *)base,
1432 nparam, 0, dim, extra, n_eq, n_ineq);
1435 struct isl_basic_set *isl_basic_set_extend_constraints(
1436 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1438 return (struct isl_basic_set *)
1439 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1440 n_eq, n_ineq);
1443 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1445 return (struct isl_basic_set *)
1446 isl_basic_map_cow((struct isl_basic_map *)bset);
1449 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1451 if (!bmap)
1452 return NULL;
1454 if (bmap->ref > 1) {
1455 bmap->ref--;
1456 bmap = isl_basic_map_dup(bmap);
1458 if (bmap)
1459 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1460 return bmap;
1463 struct isl_set *isl_set_cow(struct isl_set *set)
1465 if (!set)
1466 return NULL;
1468 if (set->ref == 1)
1469 return set;
1470 set->ref--;
1471 return isl_set_dup(set);
1474 struct isl_map *isl_map_cow(struct isl_map *map)
1476 if (!map)
1477 return NULL;
1479 if (map->ref == 1)
1480 return map;
1481 map->ref--;
1482 return isl_map_dup(map);
1485 static void swap_vars(struct isl_blk blk, isl_int *a,
1486 unsigned a_len, unsigned b_len)
1488 isl_seq_cpy(blk.data, a+a_len, b_len);
1489 isl_seq_cpy(blk.data+b_len, a, a_len);
1490 isl_seq_cpy(a, blk.data, b_len+a_len);
1493 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1494 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1496 int i;
1497 struct isl_blk blk;
1499 if (!bmap)
1500 goto error;
1502 isl_assert(bmap->ctx,
1503 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1505 if (n1 == 0 || n2 == 0)
1506 return bmap;
1508 bmap = isl_basic_map_cow(bmap);
1509 if (!bmap)
1510 return NULL;
1512 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1513 if (isl_blk_is_error(blk))
1514 goto error;
1516 for (i = 0; i < bmap->n_eq; ++i)
1517 swap_vars(blk,
1518 bmap->eq[i] + pos, n1, n2);
1520 for (i = 0; i < bmap->n_ineq; ++i)
1521 swap_vars(blk,
1522 bmap->ineq[i] + pos, n1, n2);
1524 for (i = 0; i < bmap->n_div; ++i)
1525 swap_vars(blk,
1526 bmap->div[i]+1 + pos, n1, n2);
1528 isl_blk_free(bmap->ctx, blk);
1530 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1531 bmap = isl_basic_map_gauss(bmap, NULL);
1532 return isl_basic_map_finalize(bmap);
1533 error:
1534 isl_basic_map_free(bmap);
1535 return NULL;
1538 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1539 __isl_take isl_basic_set *bset, unsigned n)
1541 unsigned dim;
1542 unsigned nparam;
1544 nparam = isl_basic_set_n_param(bset);
1545 dim = isl_basic_set_n_dim(bset);
1546 isl_assert(bset->ctx, n <= dim, goto error);
1548 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1549 error:
1550 isl_basic_set_free(bset);
1551 return NULL;
1554 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1556 int i = 0;
1557 unsigned total;
1558 if (!bmap)
1559 goto error;
1560 total = isl_basic_map_total_dim(bmap);
1561 isl_basic_map_free_div(bmap, bmap->n_div);
1562 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1563 if (bmap->n_eq > 0)
1564 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1565 else {
1566 i = isl_basic_map_alloc_equality(bmap);
1567 if (i < 0)
1568 goto error;
1570 isl_int_set_si(bmap->eq[i][0], 1);
1571 isl_seq_clr(bmap->eq[i]+1, total);
1572 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1573 isl_vec_free(bmap->sample);
1574 bmap->sample = NULL;
1575 return isl_basic_map_finalize(bmap);
1576 error:
1577 isl_basic_map_free(bmap);
1578 return NULL;
1581 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1583 return (struct isl_basic_set *)
1584 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1587 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1589 int i;
1590 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1591 isl_int *t = bmap->div[a];
1592 bmap->div[a] = bmap->div[b];
1593 bmap->div[b] = t;
1595 for (i = 0; i < bmap->n_eq; ++i)
1596 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1598 for (i = 0; i < bmap->n_ineq; ++i)
1599 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1601 for (i = 0; i < bmap->n_div; ++i)
1602 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1603 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1606 /* Eliminate the specified n dimensions starting at first from the
1607 * constraints, without removing the dimensions from the space.
1608 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1610 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1611 enum isl_dim_type type, unsigned first, unsigned n)
1613 int i;
1615 if (!map)
1616 return NULL;
1617 if (n == 0)
1618 return map;
1620 if (first + n > isl_map_dim(map, type) || first + n < first)
1621 isl_die(map->ctx, isl_error_invalid,
1622 "index out of bounds", goto error);
1624 map = isl_map_cow(map);
1625 if (!map)
1626 return NULL;
1628 for (i = 0; i < map->n; ++i) {
1629 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1630 if (!map->p[i])
1631 goto error;
1633 return map;
1634 error:
1635 isl_map_free(map);
1636 return NULL;
1639 /* Eliminate the specified n dimensions starting at first from the
1640 * constraints, without removing the dimensions from the space.
1641 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1643 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1644 enum isl_dim_type type, unsigned first, unsigned n)
1646 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1649 /* Eliminate the specified n dimensions starting at first from the
1650 * constraints, without removing the dimensions from the space.
1651 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1653 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1654 unsigned first, unsigned n)
1656 return isl_set_eliminate(set, isl_dim_set, first, n);
1659 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1660 __isl_take isl_basic_map *bmap)
1662 if (!bmap)
1663 return NULL;
1664 bmap = isl_basic_map_eliminate_vars(bmap,
1665 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1666 if (!bmap)
1667 return NULL;
1668 bmap->n_div = 0;
1669 return isl_basic_map_finalize(bmap);
1672 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1673 __isl_take isl_basic_set *bset)
1675 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1676 (struct isl_basic_map *)bset);
1679 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1681 int i;
1683 if (!map)
1684 return NULL;
1685 if (map->n == 0)
1686 return map;
1688 map = isl_map_cow(map);
1689 if (!map)
1690 return NULL;
1692 for (i = 0; i < map->n; ++i) {
1693 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1694 if (!map->p[i])
1695 goto error;
1697 return map;
1698 error:
1699 isl_map_free(map);
1700 return NULL;
1703 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1705 return isl_map_remove_divs(set);
1708 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1709 enum isl_dim_type type, unsigned first, unsigned n)
1711 if (!bmap)
1712 return NULL;
1713 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1714 goto error);
1715 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1716 return bmap;
1717 bmap = isl_basic_map_eliminate_vars(bmap,
1718 isl_basic_map_offset(bmap, type) - 1 + first, n);
1719 if (!bmap)
1720 return bmap;
1721 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1722 return bmap;
1723 bmap = isl_basic_map_drop(bmap, type, first, n);
1724 return bmap;
1725 error:
1726 isl_basic_map_free(bmap);
1727 return NULL;
1730 /* Return true if the definition of the given div (recursively) involves
1731 * any of the given variables.
1733 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1734 unsigned first, unsigned n)
1736 int i;
1737 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1739 if (isl_int_is_zero(bmap->div[div][0]))
1740 return 0;
1741 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1742 return 1;
1744 for (i = bmap->n_div - 1; i >= 0; --i) {
1745 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1746 continue;
1747 if (div_involves_vars(bmap, i, first, n))
1748 return 1;
1751 return 0;
1754 /* Try and add a lower and/or upper bound on "div" to "bmap"
1755 * based on inequality "i".
1756 * "total" is the total number of variables (excluding the divs).
1757 * "v" is a temporary object that can be used during the calculations.
1758 * If "lb" is set, then a lower bound should be constructed.
1759 * If "ub" is set, then an upper bound should be constructed.
1761 * The calling function has already checked that the inequality does not
1762 * reference "div", but we still need to check that the inequality is
1763 * of the right form. We'll consider the case where we want to construct
1764 * a lower bound. The construction of upper bounds is similar.
1766 * Let "div" be of the form
1768 * q = floor((a + f(x))/d)
1770 * We essentially check if constraint "i" is of the form
1772 * b + f(x) >= 0
1774 * so that we can use it to derive a lower bound on "div".
1775 * However, we allow a slightly more general form
1777 * b + g(x) >= 0
1779 * with the condition that the coefficients of g(x) - f(x) are all
1780 * divisible by d.
1781 * Rewriting this constraint as
1783 * 0 >= -b - g(x)
1785 * adding a + f(x) to both sides and dividing by d, we obtain
1787 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1789 * Taking the floor on both sides, we obtain
1791 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1793 * or
1795 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1797 * In the case of an upper bound, we construct the constraint
1799 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1802 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1803 __isl_take isl_basic_map *bmap, int div, int i,
1804 unsigned total, isl_int v, int lb, int ub)
1806 int j;
1808 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1809 if (lb) {
1810 isl_int_sub(v, bmap->ineq[i][1 + j],
1811 bmap->div[div][1 + 1 + j]);
1812 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1814 if (ub) {
1815 isl_int_add(v, bmap->ineq[i][1 + j],
1816 bmap->div[div][1 + 1 + j]);
1817 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1820 if (!lb && !ub)
1821 return bmap;
1823 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1824 if (lb) {
1825 int k = isl_basic_map_alloc_inequality(bmap);
1826 if (k < 0)
1827 goto error;
1828 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1829 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1830 bmap->div[div][1 + j]);
1831 isl_int_cdiv_q(bmap->ineq[k][j],
1832 bmap->ineq[k][j], bmap->div[div][0]);
1834 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1836 if (ub) {
1837 int k = isl_basic_map_alloc_inequality(bmap);
1838 if (k < 0)
1839 goto error;
1840 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1841 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1842 bmap->div[div][1 + j]);
1843 isl_int_fdiv_q(bmap->ineq[k][j],
1844 bmap->ineq[k][j], bmap->div[div][0]);
1846 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
1849 return bmap;
1850 error:
1851 isl_basic_map_free(bmap);
1852 return NULL;
1855 /* This function is called right before "div" is eliminated from "bmap"
1856 * using Fourier-Motzkin.
1857 * Look through the constraints of "bmap" for constraints on the argument
1858 * of the integer division and use them to construct constraints on the
1859 * integer division itself. These constraints can then be combined
1860 * during the Fourier-Motzkin elimination.
1861 * Note that it is only useful to introduce lower bounds on "div"
1862 * if "bmap" already contains upper bounds on "div" as the newly
1863 * introduce lower bounds can then be combined with the pre-existing
1864 * upper bounds. Similarly for upper bounds.
1865 * We therefore first check if "bmap" contains any lower and/or upper bounds
1866 * on "div".
1868 * It is interesting to note that the introduction of these constraints
1869 * can indeed lead to more accurate results, even when compared to
1870 * deriving constraints on the argument of "div" from constraints on "div".
1871 * Consider, for example, the set
1873 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1875 * The second constraint can be rewritten as
1877 * 2 * [(-i-2j+3)/4] + k >= 0
1879 * from which we can derive
1881 * -i - 2j + 3 >= -2k
1883 * or
1885 * i + 2j <= 3 + 2k
1887 * Combined with the first constraint, we obtain
1889 * -3 <= 3 + 2k or k >= -3
1891 * If, on the other hand we derive a constraint on [(i+2j)/4] from
1892 * the first constraint, we obtain
1894 * [(i + 2j)/4] >= [-3/4] = -1
1896 * Combining this constraint with the second constraint, we obtain
1898 * k >= -2
1900 static __isl_give isl_basic_map *insert_bounds_on_div(
1901 __isl_take isl_basic_map *bmap, int div)
1903 int i;
1904 int check_lb, check_ub;
1905 isl_int v;
1906 unsigned total;
1908 if (!bmap)
1909 return NULL;
1911 if (isl_int_is_zero(bmap->div[div][0]))
1912 return bmap;
1914 total = isl_space_dim(bmap->dim, isl_dim_all);
1916 check_lb = 0;
1917 check_ub = 0;
1918 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
1919 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
1920 if (s > 0)
1921 check_ub = 1;
1922 if (s < 0)
1923 check_lb = 1;
1926 if (!check_lb && !check_ub)
1927 return bmap;
1929 isl_int_init(v);
1931 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
1932 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
1933 continue;
1935 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
1936 check_lb, check_ub);
1939 isl_int_clear(v);
1941 return bmap;
1944 /* Remove all divs (recursively) involving any of the given dimensions
1945 * in their definitions.
1947 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1948 __isl_take isl_basic_map *bmap,
1949 enum isl_dim_type type, unsigned first, unsigned n)
1951 int i;
1953 if (!bmap)
1954 return NULL;
1955 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1956 goto error);
1957 first += isl_basic_map_offset(bmap, type);
1959 for (i = bmap->n_div - 1; i >= 0; --i) {
1960 if (!div_involves_vars(bmap, i, first, n))
1961 continue;
1962 bmap = insert_bounds_on_div(bmap, i);
1963 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1964 if (!bmap)
1965 return NULL;
1966 i = bmap->n_div;
1969 return bmap;
1970 error:
1971 isl_basic_map_free(bmap);
1972 return NULL;
1975 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1976 enum isl_dim_type type, unsigned first, unsigned n)
1978 int i;
1980 if (!map)
1981 return NULL;
1982 if (map->n == 0)
1983 return map;
1985 map = isl_map_cow(map);
1986 if (!map)
1987 return NULL;
1989 for (i = 0; i < map->n; ++i) {
1990 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1991 type, first, n);
1992 if (!map->p[i])
1993 goto error;
1995 return map;
1996 error:
1997 isl_map_free(map);
1998 return NULL;
2001 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2002 enum isl_dim_type type, unsigned first, unsigned n)
2004 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2005 type, first, n);
2008 /* Does the desciption of "bmap" depend on the specified dimensions?
2009 * We also check whether the dimensions appear in any of the div definitions.
2010 * In principle there is no need for this check. If the dimensions appear
2011 * in a div definition, they also appear in the defining constraints of that
2012 * div.
2014 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2015 enum isl_dim_type type, unsigned first, unsigned n)
2017 int i;
2019 if (!bmap)
2020 return -1;
2022 if (first + n > isl_basic_map_dim(bmap, type))
2023 isl_die(bmap->ctx, isl_error_invalid,
2024 "index out of bounds", return -1);
2026 first += isl_basic_map_offset(bmap, type);
2027 for (i = 0; i < bmap->n_eq; ++i)
2028 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2029 return 1;
2030 for (i = 0; i < bmap->n_ineq; ++i)
2031 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2032 return 1;
2033 for (i = 0; i < bmap->n_div; ++i) {
2034 if (isl_int_is_zero(bmap->div[i][0]))
2035 continue;
2036 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2037 return 1;
2040 return 0;
2043 int isl_map_involves_dims(__isl_keep isl_map *map,
2044 enum isl_dim_type type, unsigned first, unsigned n)
2046 int i;
2048 if (!map)
2049 return -1;
2051 if (first + n > isl_map_dim(map, type))
2052 isl_die(map->ctx, isl_error_invalid,
2053 "index out of bounds", return -1);
2055 for (i = 0; i < map->n; ++i) {
2056 int involves = isl_basic_map_involves_dims(map->p[i],
2057 type, first, n);
2058 if (involves < 0 || involves)
2059 return involves;
2062 return 0;
2065 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2066 enum isl_dim_type type, unsigned first, unsigned n)
2068 return isl_basic_map_involves_dims(bset, type, first, n);
2071 int isl_set_involves_dims(__isl_keep isl_set *set,
2072 enum isl_dim_type type, unsigned first, unsigned n)
2074 return isl_map_involves_dims(set, type, first, n);
2077 /* Return true if the definition of the given div is unknown or depends
2078 * on unknown divs.
2080 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2082 int i;
2083 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2085 if (isl_int_is_zero(bmap->div[div][0]))
2086 return 1;
2088 for (i = bmap->n_div - 1; i >= 0; --i) {
2089 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2090 continue;
2091 if (div_is_unknown(bmap, i))
2092 return 1;
2095 return 0;
2098 /* Remove all divs that are unknown or defined in terms of unknown divs.
2100 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2101 __isl_take isl_basic_map *bmap)
2103 int i;
2105 if (!bmap)
2106 return NULL;
2108 for (i = bmap->n_div - 1; i >= 0; --i) {
2109 if (!div_is_unknown(bmap, i))
2110 continue;
2111 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2112 if (!bmap)
2113 return NULL;
2114 i = bmap->n_div;
2117 return bmap;
2120 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2122 int i;
2124 if (!map)
2125 return NULL;
2126 if (map->n == 0)
2127 return map;
2129 map = isl_map_cow(map);
2130 if (!map)
2131 return NULL;
2133 for (i = 0; i < map->n; ++i) {
2134 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2135 if (!map->p[i])
2136 goto error;
2138 return map;
2139 error:
2140 isl_map_free(map);
2141 return NULL;
2144 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2146 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2149 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2150 __isl_take isl_basic_set *bset,
2151 enum isl_dim_type type, unsigned first, unsigned n)
2153 return (isl_basic_set *)
2154 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2157 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2158 enum isl_dim_type type, unsigned first, unsigned n)
2160 int i;
2162 if (n == 0)
2163 return map;
2165 map = isl_map_cow(map);
2166 if (!map)
2167 return NULL;
2168 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2170 for (i = 0; i < map->n; ++i) {
2171 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2172 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2173 if (!map->p[i])
2174 goto error;
2176 map = isl_map_drop(map, type, first, n);
2177 return map;
2178 error:
2179 isl_map_free(map);
2180 return NULL;
2183 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2184 enum isl_dim_type type, unsigned first, unsigned n)
2186 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2189 /* Project out n inputs starting at first using Fourier-Motzkin */
2190 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2191 unsigned first, unsigned n)
2193 return isl_map_remove_dims(map, isl_dim_in, first, n);
2196 static void dump_term(struct isl_basic_map *bmap,
2197 isl_int c, int pos, FILE *out)
2199 const char *name;
2200 unsigned in = isl_basic_map_n_in(bmap);
2201 unsigned dim = in + isl_basic_map_n_out(bmap);
2202 unsigned nparam = isl_basic_map_n_param(bmap);
2203 if (!pos)
2204 isl_int_print(out, c, 0);
2205 else {
2206 if (!isl_int_is_one(c))
2207 isl_int_print(out, c, 0);
2208 if (pos < 1 + nparam) {
2209 name = isl_space_get_dim_name(bmap->dim,
2210 isl_dim_param, pos - 1);
2211 if (name)
2212 fprintf(out, "%s", name);
2213 else
2214 fprintf(out, "p%d", pos - 1);
2215 } else if (pos < 1 + nparam + in)
2216 fprintf(out, "i%d", pos - 1 - nparam);
2217 else if (pos < 1 + nparam + dim)
2218 fprintf(out, "o%d", pos - 1 - nparam - in);
2219 else
2220 fprintf(out, "e%d", pos - 1 - nparam - dim);
2224 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2225 int sign, FILE *out)
2227 int i;
2228 int first;
2229 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2230 isl_int v;
2232 isl_int_init(v);
2233 for (i = 0, first = 1; i < len; ++i) {
2234 if (isl_int_sgn(c[i]) * sign <= 0)
2235 continue;
2236 if (!first)
2237 fprintf(out, " + ");
2238 first = 0;
2239 isl_int_abs(v, c[i]);
2240 dump_term(bmap, v, i, out);
2242 isl_int_clear(v);
2243 if (first)
2244 fprintf(out, "0");
2247 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2248 const char *op, FILE *out, int indent)
2250 int i;
2252 fprintf(out, "%*s", indent, "");
2254 dump_constraint_sign(bmap, c, 1, out);
2255 fprintf(out, " %s ", op);
2256 dump_constraint_sign(bmap, c, -1, out);
2258 fprintf(out, "\n");
2260 for (i = bmap->n_div; i < bmap->extra; ++i) {
2261 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2262 continue;
2263 fprintf(out, "%*s", indent, "");
2264 fprintf(out, "ERROR: unused div coefficient not zero\n");
2265 abort();
2269 static void dump_constraints(struct isl_basic_map *bmap,
2270 isl_int **c, unsigned n,
2271 const char *op, FILE *out, int indent)
2273 int i;
2275 for (i = 0; i < n; ++i)
2276 dump_constraint(bmap, c[i], op, out, indent);
2279 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2281 int j;
2282 int first = 1;
2283 unsigned total = isl_basic_map_total_dim(bmap);
2285 for (j = 0; j < 1 + total; ++j) {
2286 if (isl_int_is_zero(exp[j]))
2287 continue;
2288 if (!first && isl_int_is_pos(exp[j]))
2289 fprintf(out, "+");
2290 dump_term(bmap, exp[j], j, out);
2291 first = 0;
2295 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2297 int i;
2299 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2300 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2302 for (i = 0; i < bmap->n_div; ++i) {
2303 fprintf(out, "%*s", indent, "");
2304 fprintf(out, "e%d = [(", i);
2305 dump_affine(bmap, bmap->div[i]+1, out);
2306 fprintf(out, ")/");
2307 isl_int_print(out, bmap->div[i][0], 0);
2308 fprintf(out, "]\n");
2312 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2313 FILE *out, int indent)
2315 if (!bset) {
2316 fprintf(out, "null basic set\n");
2317 return;
2320 fprintf(out, "%*s", indent, "");
2321 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2322 bset->ref, bset->dim->nparam, bset->dim->n_out,
2323 bset->extra, bset->flags);
2324 dump((struct isl_basic_map *)bset, out, indent);
2327 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2328 FILE *out, int indent)
2330 if (!bmap) {
2331 fprintf(out, "null basic map\n");
2332 return;
2335 fprintf(out, "%*s", indent, "");
2336 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2337 "flags: %x, n_name: %d\n",
2338 bmap->ref,
2339 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2340 bmap->extra, bmap->flags, bmap->dim->n_id);
2341 dump(bmap, out, indent);
2344 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2346 unsigned total;
2347 if (!bmap)
2348 return -1;
2349 total = isl_basic_map_total_dim(bmap);
2350 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2351 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2352 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2353 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2354 return 0;
2357 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2358 unsigned flags)
2360 struct isl_set *set;
2362 if (!dim)
2363 return NULL;
2364 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2365 isl_assert(dim->ctx, n >= 0, goto error);
2366 set = isl_alloc(dim->ctx, struct isl_set,
2367 sizeof(struct isl_set) +
2368 (n - 1) * sizeof(struct isl_basic_set *));
2369 if (!set)
2370 goto error;
2372 set->ctx = dim->ctx;
2373 isl_ctx_ref(set->ctx);
2374 set->ref = 1;
2375 set->size = n;
2376 set->n = 0;
2377 set->dim = dim;
2378 set->flags = flags;
2379 return set;
2380 error:
2381 isl_space_free(dim);
2382 return NULL;
2385 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2386 unsigned nparam, unsigned dim, int n, unsigned flags)
2388 struct isl_set *set;
2389 isl_space *dims;
2391 dims = isl_space_alloc(ctx, nparam, 0, dim);
2392 if (!dims)
2393 return NULL;
2395 set = isl_set_alloc_space(dims, n, flags);
2396 return set;
2399 /* Make sure "map" has room for at least "n" more basic maps.
2401 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2403 int i;
2404 struct isl_map *grown = NULL;
2406 if (!map)
2407 return NULL;
2408 isl_assert(map->ctx, n >= 0, goto error);
2409 if (map->n + n <= map->size)
2410 return map;
2411 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2412 if (!grown)
2413 goto error;
2414 for (i = 0; i < map->n; ++i) {
2415 grown->p[i] = isl_basic_map_copy(map->p[i]);
2416 if (!grown->p[i])
2417 goto error;
2418 grown->n++;
2420 isl_map_free(map);
2421 return grown;
2422 error:
2423 isl_map_free(grown);
2424 isl_map_free(map);
2425 return NULL;
2428 /* Make sure "set" has room for at least "n" more basic sets.
2430 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2432 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2435 struct isl_set *isl_set_dup(struct isl_set *set)
2437 int i;
2438 struct isl_set *dup;
2440 if (!set)
2441 return NULL;
2443 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2444 if (!dup)
2445 return NULL;
2446 for (i = 0; i < set->n; ++i)
2447 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2448 return dup;
2451 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2453 return isl_map_from_basic_map(bset);
2456 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2458 struct isl_map *map;
2460 if (!bmap)
2461 return NULL;
2463 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2464 return isl_map_add_basic_map(map, bmap);
2467 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2468 __isl_take isl_basic_set *bset)
2470 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2471 (struct isl_basic_map *)bset);
2474 void isl_set_free(struct isl_set *set)
2476 int i;
2478 if (!set)
2479 return;
2481 if (--set->ref > 0)
2482 return;
2484 isl_ctx_deref(set->ctx);
2485 for (i = 0; i < set->n; ++i)
2486 isl_basic_set_free(set->p[i]);
2487 isl_space_free(set->dim);
2488 free(set);
2491 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2493 int i;
2495 if (!set) {
2496 fprintf(out, "null set\n");
2497 return;
2500 fprintf(out, "%*s", indent, "");
2501 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2502 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2503 set->flags);
2504 for (i = 0; i < set->n; ++i) {
2505 fprintf(out, "%*s", indent, "");
2506 fprintf(out, "basic set %d:\n", i);
2507 isl_basic_set_print_internal(set->p[i], out, indent+4);
2511 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2513 int i;
2515 if (!map) {
2516 fprintf(out, "null map\n");
2517 return;
2520 fprintf(out, "%*s", indent, "");
2521 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2522 "flags: %x, n_name: %d\n",
2523 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2524 map->dim->n_out, map->flags, map->dim->n_id);
2525 for (i = 0; i < map->n; ++i) {
2526 fprintf(out, "%*s", indent, "");
2527 fprintf(out, "basic map %d:\n", i);
2528 isl_basic_map_print_internal(map->p[i], out, indent+4);
2532 struct isl_basic_map *isl_basic_map_intersect_domain(
2533 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2535 struct isl_basic_map *bmap_domain;
2537 if (!bmap || !bset)
2538 goto error;
2540 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2541 bset->dim, isl_dim_param), goto error);
2543 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2544 isl_assert(bset->ctx,
2545 isl_basic_map_compatible_domain(bmap, bset), goto error);
2547 bmap = isl_basic_map_cow(bmap);
2548 if (!bmap)
2549 goto error;
2550 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2551 bset->n_div, bset->n_eq, bset->n_ineq);
2552 bmap_domain = isl_basic_map_from_domain(bset);
2553 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2555 bmap = isl_basic_map_simplify(bmap);
2556 return isl_basic_map_finalize(bmap);
2557 error:
2558 isl_basic_map_free(bmap);
2559 isl_basic_set_free(bset);
2560 return NULL;
2563 struct isl_basic_map *isl_basic_map_intersect_range(
2564 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2566 struct isl_basic_map *bmap_range;
2568 if (!bmap || !bset)
2569 goto error;
2571 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2572 bset->dim, isl_dim_param), goto error);
2574 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2575 isl_assert(bset->ctx,
2576 isl_basic_map_compatible_range(bmap, bset), goto error);
2578 if (isl_basic_set_is_universe(bset)) {
2579 isl_basic_set_free(bset);
2580 return bmap;
2583 bmap = isl_basic_map_cow(bmap);
2584 if (!bmap)
2585 goto error;
2586 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2587 bset->n_div, bset->n_eq, bset->n_ineq);
2588 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2589 bmap = add_constraints(bmap, bmap_range, 0, 0);
2591 bmap = isl_basic_map_simplify(bmap);
2592 return isl_basic_map_finalize(bmap);
2593 error:
2594 isl_basic_map_free(bmap);
2595 isl_basic_set_free(bset);
2596 return NULL;
2599 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2601 int i;
2602 unsigned total;
2603 isl_int s;
2605 total = 1 + isl_basic_map_total_dim(bmap);
2606 if (total != vec->size)
2607 return -1;
2609 isl_int_init(s);
2611 for (i = 0; i < bmap->n_eq; ++i) {
2612 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2613 if (!isl_int_is_zero(s)) {
2614 isl_int_clear(s);
2615 return 0;
2619 for (i = 0; i < bmap->n_ineq; ++i) {
2620 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2621 if (isl_int_is_neg(s)) {
2622 isl_int_clear(s);
2623 return 0;
2627 isl_int_clear(s);
2629 return 1;
2632 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2634 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2637 struct isl_basic_map *isl_basic_map_intersect(
2638 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2640 struct isl_vec *sample = NULL;
2642 if (!bmap1 || !bmap2)
2643 goto error;
2645 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2646 bmap2->dim, isl_dim_param), goto error);
2647 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2648 isl_space_dim(bmap1->dim, isl_dim_param) &&
2649 isl_space_dim(bmap2->dim, isl_dim_all) !=
2650 isl_space_dim(bmap2->dim, isl_dim_param))
2651 return isl_basic_map_intersect(bmap2, bmap1);
2653 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2654 isl_space_dim(bmap2->dim, isl_dim_param))
2655 isl_assert(bmap1->ctx,
2656 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2658 if (bmap1->sample &&
2659 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2660 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2661 sample = isl_vec_copy(bmap1->sample);
2662 else if (bmap2->sample &&
2663 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2664 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2665 sample = isl_vec_copy(bmap2->sample);
2667 bmap1 = isl_basic_map_cow(bmap1);
2668 if (!bmap1)
2669 goto error;
2670 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2671 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2672 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2674 if (!bmap1)
2675 isl_vec_free(sample);
2676 else if (sample) {
2677 isl_vec_free(bmap1->sample);
2678 bmap1->sample = sample;
2681 bmap1 = isl_basic_map_simplify(bmap1);
2682 return isl_basic_map_finalize(bmap1);
2683 error:
2684 if (sample)
2685 isl_vec_free(sample);
2686 isl_basic_map_free(bmap1);
2687 isl_basic_map_free(bmap2);
2688 return NULL;
2691 struct isl_basic_set *isl_basic_set_intersect(
2692 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2694 return (struct isl_basic_set *)
2695 isl_basic_map_intersect(
2696 (struct isl_basic_map *)bset1,
2697 (struct isl_basic_map *)bset2);
2700 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2701 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2703 return isl_basic_set_intersect(bset1, bset2);
2706 /* Special case of isl_map_intersect, where both map1 and map2
2707 * are convex, without any divs and such that either map1 or map2
2708 * contains a single constraint. This constraint is then simply
2709 * added to the other map.
2711 static __isl_give isl_map *map_intersect_add_constraint(
2712 __isl_take isl_map *map1, __isl_take isl_map *map2)
2714 isl_assert(map1->ctx, map1->n == 1, goto error);
2715 isl_assert(map2->ctx, map1->n == 1, goto error);
2716 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2717 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2719 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2720 return isl_map_intersect(map2, map1);
2722 isl_assert(map2->ctx,
2723 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2725 map1 = isl_map_cow(map1);
2726 if (!map1)
2727 goto error;
2728 if (isl_map_plain_is_empty(map1)) {
2729 isl_map_free(map2);
2730 return map1;
2732 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2733 if (map2->p[0]->n_eq == 1)
2734 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2735 else
2736 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2737 map2->p[0]->ineq[0]);
2739 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2740 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2741 if (!map1->p[0])
2742 goto error;
2744 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2745 isl_basic_map_free(map1->p[0]);
2746 map1->n = 0;
2749 isl_map_free(map2);
2751 return map1;
2752 error:
2753 isl_map_free(map1);
2754 isl_map_free(map2);
2755 return NULL;
2758 /* map2 may be either a parameter domain or a map living in the same
2759 * space as map1.
2761 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2762 __isl_take isl_map *map2)
2764 unsigned flags = 0;
2765 struct isl_map *result;
2766 int i, j;
2768 if (!map1 || !map2)
2769 goto error;
2771 if (isl_map_plain_is_empty(map1) &&
2772 isl_space_is_equal(map1->dim, map2->dim)) {
2773 isl_map_free(map2);
2774 return map1;
2776 if (isl_map_plain_is_empty(map2) &&
2777 isl_space_is_equal(map1->dim, map2->dim)) {
2778 isl_map_free(map1);
2779 return map2;
2782 if (map1->n == 1 && map2->n == 1 &&
2783 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2784 isl_space_is_equal(map1->dim, map2->dim) &&
2785 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2786 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2787 return map_intersect_add_constraint(map1, map2);
2789 if (isl_space_dim(map2->dim, isl_dim_all) !=
2790 isl_space_dim(map2->dim, isl_dim_param))
2791 isl_assert(map1->ctx,
2792 isl_space_is_equal(map1->dim, map2->dim), goto error);
2794 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2795 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2796 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2798 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2799 map1->n * map2->n, flags);
2800 if (!result)
2801 goto error;
2802 for (i = 0; i < map1->n; ++i)
2803 for (j = 0; j < map2->n; ++j) {
2804 struct isl_basic_map *part;
2805 part = isl_basic_map_intersect(
2806 isl_basic_map_copy(map1->p[i]),
2807 isl_basic_map_copy(map2->p[j]));
2808 if (isl_basic_map_is_empty(part))
2809 isl_basic_map_free(part);
2810 else
2811 result = isl_map_add_basic_map(result, part);
2812 if (!result)
2813 goto error;
2815 isl_map_free(map1);
2816 isl_map_free(map2);
2817 return result;
2818 error:
2819 isl_map_free(map1);
2820 isl_map_free(map2);
2821 return NULL;
2824 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2825 __isl_take isl_map *map2)
2827 if (!map1 || !map2)
2828 goto error;
2829 if (!isl_space_is_equal(map1->dim, map2->dim))
2830 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2831 "spaces don't match", goto error);
2832 return map_intersect_internal(map1, map2);
2833 error:
2834 isl_map_free(map1);
2835 isl_map_free(map2);
2836 return NULL;
2839 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2840 __isl_take isl_map *map2)
2842 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2845 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2847 return (struct isl_set *)
2848 isl_map_intersect((struct isl_map *)set1,
2849 (struct isl_map *)set2);
2852 /* map_intersect_internal accepts intersections
2853 * with parameter domains, so we can just call that function.
2855 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2856 __isl_take isl_set *params)
2858 return map_intersect_internal(map, params);
2861 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2862 __isl_take isl_map *map2)
2864 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2867 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2868 __isl_take isl_set *params)
2870 return isl_map_intersect_params(set, params);
2873 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2875 isl_space *dim;
2876 struct isl_basic_set *bset;
2877 unsigned in;
2879 if (!bmap)
2880 return NULL;
2881 bmap = isl_basic_map_cow(bmap);
2882 if (!bmap)
2883 return NULL;
2884 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2885 in = isl_basic_map_n_in(bmap);
2886 bset = isl_basic_set_from_basic_map(bmap);
2887 bset = isl_basic_set_swap_vars(bset, in);
2888 return isl_basic_map_from_basic_set(bset, dim);
2891 static __isl_give isl_basic_map *basic_map_space_reset(
2892 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2894 isl_space *space;
2896 if (!isl_space_is_named_or_nested(bmap->dim, type))
2897 return bmap;
2899 space = isl_basic_map_get_space(bmap);
2900 space = isl_space_reset(space, type);
2901 bmap = isl_basic_map_reset_space(bmap, space);
2902 return bmap;
2905 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2906 enum isl_dim_type type, unsigned pos, unsigned n)
2908 isl_space *res_dim;
2909 struct isl_basic_map *res;
2910 struct isl_dim_map *dim_map;
2911 unsigned total, off;
2912 enum isl_dim_type t;
2914 if (n == 0)
2915 return basic_map_space_reset(bmap, type);
2917 if (!bmap)
2918 return NULL;
2920 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2922 total = isl_basic_map_total_dim(bmap) + n;
2923 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2924 off = 0;
2925 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2926 if (t != type) {
2927 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2928 } else {
2929 unsigned size = isl_basic_map_dim(bmap, t);
2930 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2931 0, pos, off);
2932 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2933 pos, size - pos, off + pos + n);
2935 off += isl_space_dim(res_dim, t);
2937 isl_dim_map_div(dim_map, bmap, off);
2939 res = isl_basic_map_alloc_space(res_dim,
2940 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2941 if (isl_basic_map_is_rational(bmap))
2942 res = isl_basic_map_set_rational(res);
2943 if (isl_basic_map_plain_is_empty(bmap)) {
2944 isl_basic_map_free(bmap);
2945 return isl_basic_map_set_to_empty(res);
2947 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2948 return isl_basic_map_finalize(res);
2951 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2952 enum isl_dim_type type, unsigned n)
2954 if (!bmap)
2955 return NULL;
2956 return isl_basic_map_insert(bmap, type,
2957 isl_basic_map_dim(bmap, type), n);
2960 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2961 enum isl_dim_type type, unsigned n)
2963 if (!bset)
2964 return NULL;
2965 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2966 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2967 error:
2968 isl_basic_set_free(bset);
2969 return NULL;
2972 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2973 enum isl_dim_type type)
2975 isl_space *space;
2977 if (!map || !isl_space_is_named_or_nested(map->dim, type))
2978 return map;
2980 space = isl_map_get_space(map);
2981 space = isl_space_reset(space, type);
2982 map = isl_map_reset_space(map, space);
2983 return map;
2986 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2987 enum isl_dim_type type, unsigned pos, unsigned n)
2989 int i;
2991 if (n == 0)
2992 return map_space_reset(map, type);
2994 map = isl_map_cow(map);
2995 if (!map)
2996 return NULL;
2998 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2999 if (!map->dim)
3000 goto error;
3002 for (i = 0; i < map->n; ++i) {
3003 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
3004 if (!map->p[i])
3005 goto error;
3008 return map;
3009 error:
3010 isl_map_free(map);
3011 return NULL;
3014 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3015 enum isl_dim_type type, unsigned pos, unsigned n)
3017 return isl_map_insert_dims(set, type, pos, n);
3020 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3021 enum isl_dim_type type, unsigned n)
3023 if (!map)
3024 return NULL;
3025 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3028 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3029 enum isl_dim_type type, unsigned n)
3031 if (!set)
3032 return NULL;
3033 isl_assert(set->ctx, type != isl_dim_in, goto error);
3034 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3035 error:
3036 isl_set_free(set);
3037 return NULL;
3040 __isl_give isl_basic_map *isl_basic_map_move_dims(
3041 __isl_take isl_basic_map *bmap,
3042 enum isl_dim_type dst_type, unsigned dst_pos,
3043 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3045 struct isl_dim_map *dim_map;
3046 struct isl_basic_map *res;
3047 enum isl_dim_type t;
3048 unsigned total, off;
3050 if (!bmap)
3051 return NULL;
3052 if (n == 0)
3053 return bmap;
3055 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3056 goto error);
3058 if (dst_type == src_type && dst_pos == src_pos)
3059 return bmap;
3061 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3063 if (pos(bmap->dim, dst_type) + dst_pos ==
3064 pos(bmap->dim, src_type) + src_pos +
3065 ((src_type < dst_type) ? n : 0)) {
3066 bmap = isl_basic_map_cow(bmap);
3067 if (!bmap)
3068 return NULL;
3070 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3071 src_type, src_pos, n);
3072 if (!bmap->dim)
3073 goto error;
3075 bmap = isl_basic_map_finalize(bmap);
3077 return bmap;
3080 total = isl_basic_map_total_dim(bmap);
3081 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3083 off = 0;
3084 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3085 unsigned size = isl_space_dim(bmap->dim, t);
3086 if (t == dst_type) {
3087 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3088 0, dst_pos, off);
3089 off += dst_pos;
3090 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3091 src_pos, n, off);
3092 off += n;
3093 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3094 dst_pos, size - dst_pos, off);
3095 off += size - dst_pos;
3096 } else if (t == src_type) {
3097 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3098 0, src_pos, off);
3099 off += src_pos;
3100 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3101 src_pos + n, size - src_pos - n, off);
3102 off += size - src_pos - n;
3103 } else {
3104 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3105 off += size;
3108 isl_dim_map_div(dim_map, bmap, off);
3110 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3111 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3112 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3114 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3115 src_type, src_pos, n);
3116 if (!bmap->dim)
3117 goto error;
3119 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3120 bmap = isl_basic_map_gauss(bmap, NULL);
3121 bmap = isl_basic_map_finalize(bmap);
3123 return bmap;
3124 error:
3125 isl_basic_map_free(bmap);
3126 return NULL;
3129 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3130 enum isl_dim_type dst_type, unsigned dst_pos,
3131 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3133 return (isl_basic_set *)isl_basic_map_move_dims(
3134 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3137 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3138 enum isl_dim_type dst_type, unsigned dst_pos,
3139 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3141 if (!set)
3142 return NULL;
3143 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3144 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3145 src_type, src_pos, n);
3146 error:
3147 isl_set_free(set);
3148 return NULL;
3151 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3152 enum isl_dim_type dst_type, unsigned dst_pos,
3153 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3155 int i;
3157 if (!map)
3158 return NULL;
3159 if (n == 0)
3160 return map;
3162 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3163 goto error);
3165 if (dst_type == src_type && dst_pos == src_pos)
3166 return map;
3168 isl_assert(map->ctx, dst_type != src_type, goto error);
3170 map = isl_map_cow(map);
3171 if (!map)
3172 return NULL;
3174 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3175 if (!map->dim)
3176 goto error;
3178 for (i = 0; i < map->n; ++i) {
3179 map->p[i] = isl_basic_map_move_dims(map->p[i],
3180 dst_type, dst_pos,
3181 src_type, src_pos, n);
3182 if (!map->p[i])
3183 goto error;
3186 return map;
3187 error:
3188 isl_map_free(map);
3189 return NULL;
3192 /* Move the specified dimensions to the last columns right before
3193 * the divs. Don't change the dimension specification of bmap.
3194 * That's the responsibility of the caller.
3196 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3197 enum isl_dim_type type, unsigned first, unsigned n)
3199 struct isl_dim_map *dim_map;
3200 struct isl_basic_map *res;
3201 enum isl_dim_type t;
3202 unsigned total, off;
3204 if (!bmap)
3205 return NULL;
3206 if (pos(bmap->dim, type) + first + n ==
3207 1 + isl_space_dim(bmap->dim, isl_dim_all))
3208 return bmap;
3210 total = isl_basic_map_total_dim(bmap);
3211 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3213 off = 0;
3214 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3215 unsigned size = isl_space_dim(bmap->dim, t);
3216 if (t == type) {
3217 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3218 0, first, off);
3219 off += first;
3220 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3221 first, n, total - bmap->n_div - n);
3222 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3223 first + n, size - (first + n), off);
3224 off += size - (first + n);
3225 } else {
3226 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3227 off += size;
3230 isl_dim_map_div(dim_map, bmap, off + n);
3232 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3233 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3234 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3235 return res;
3238 /* Turn the n dimensions of type type, starting at first
3239 * into existentially quantified variables.
3241 __isl_give isl_basic_map *isl_basic_map_project_out(
3242 __isl_take isl_basic_map *bmap,
3243 enum isl_dim_type type, unsigned first, unsigned n)
3245 int i;
3246 size_t row_size;
3247 isl_int **new_div;
3248 isl_int *old;
3250 if (n == 0)
3251 return basic_map_space_reset(bmap, type);
3253 if (!bmap)
3254 return NULL;
3256 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3257 return isl_basic_map_remove_dims(bmap, type, first, n);
3259 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3260 goto error);
3262 bmap = move_last(bmap, type, first, n);
3263 bmap = isl_basic_map_cow(bmap);
3264 if (!bmap)
3265 return NULL;
3267 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3268 old = bmap->block2.data;
3269 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3270 (bmap->extra + n) * (1 + row_size));
3271 if (!bmap->block2.data)
3272 goto error;
3273 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3274 if (!new_div)
3275 goto error;
3276 for (i = 0; i < n; ++i) {
3277 new_div[i] = bmap->block2.data +
3278 (bmap->extra + i) * (1 + row_size);
3279 isl_seq_clr(new_div[i], 1 + row_size);
3281 for (i = 0; i < bmap->extra; ++i)
3282 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3283 free(bmap->div);
3284 bmap->div = new_div;
3285 bmap->n_div += n;
3286 bmap->extra += n;
3288 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3289 if (!bmap->dim)
3290 goto error;
3291 bmap = isl_basic_map_simplify(bmap);
3292 bmap = isl_basic_map_drop_redundant_divs(bmap);
3293 return isl_basic_map_finalize(bmap);
3294 error:
3295 isl_basic_map_free(bmap);
3296 return NULL;
3299 /* Turn the n dimensions of type type, starting at first
3300 * into existentially quantified variables.
3302 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3303 enum isl_dim_type type, unsigned first, unsigned n)
3305 return (isl_basic_set *)isl_basic_map_project_out(
3306 (isl_basic_map *)bset, type, first, n);
3309 /* Turn the n dimensions of type type, starting at first
3310 * into existentially quantified variables.
3312 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3313 enum isl_dim_type type, unsigned first, unsigned n)
3315 int i;
3317 if (!map)
3318 return NULL;
3320 if (n == 0)
3321 return map_space_reset(map, type);
3323 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3325 map = isl_map_cow(map);
3326 if (!map)
3327 return NULL;
3329 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3330 if (!map->dim)
3331 goto error;
3333 for (i = 0; i < map->n; ++i) {
3334 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3335 if (!map->p[i])
3336 goto error;
3339 return map;
3340 error:
3341 isl_map_free(map);
3342 return NULL;
3345 /* Turn the n dimensions of type type, starting at first
3346 * into existentially quantified variables.
3348 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3349 enum isl_dim_type type, unsigned first, unsigned n)
3351 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3354 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3356 int i, j;
3358 for (i = 0; i < n; ++i) {
3359 j = isl_basic_map_alloc_div(bmap);
3360 if (j < 0)
3361 goto error;
3362 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3364 return bmap;
3365 error:
3366 isl_basic_map_free(bmap);
3367 return NULL;
3370 struct isl_basic_map *isl_basic_map_apply_range(
3371 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3373 isl_space *dim_result = NULL;
3374 struct isl_basic_map *bmap;
3375 unsigned n_in, n_out, n, nparam, total, pos;
3376 struct isl_dim_map *dim_map1, *dim_map2;
3378 if (!bmap1 || !bmap2)
3379 goto error;
3381 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3382 isl_space_copy(bmap2->dim));
3384 n_in = isl_basic_map_n_in(bmap1);
3385 n_out = isl_basic_map_n_out(bmap2);
3386 n = isl_basic_map_n_out(bmap1);
3387 nparam = isl_basic_map_n_param(bmap1);
3389 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3390 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3391 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3392 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3393 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3394 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3395 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3396 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3397 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3398 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3399 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3401 bmap = isl_basic_map_alloc_space(dim_result,
3402 bmap1->n_div + bmap2->n_div + n,
3403 bmap1->n_eq + bmap2->n_eq,
3404 bmap1->n_ineq + bmap2->n_ineq);
3405 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3406 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3407 bmap = add_divs(bmap, n);
3408 bmap = isl_basic_map_simplify(bmap);
3409 bmap = isl_basic_map_drop_redundant_divs(bmap);
3410 return isl_basic_map_finalize(bmap);
3411 error:
3412 isl_basic_map_free(bmap1);
3413 isl_basic_map_free(bmap2);
3414 return NULL;
3417 struct isl_basic_set *isl_basic_set_apply(
3418 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3420 if (!bset || !bmap)
3421 goto error;
3423 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3424 goto error);
3426 return (struct isl_basic_set *)
3427 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3428 error:
3429 isl_basic_set_free(bset);
3430 isl_basic_map_free(bmap);
3431 return NULL;
3434 struct isl_basic_map *isl_basic_map_apply_domain(
3435 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3437 if (!bmap1 || !bmap2)
3438 goto error;
3440 isl_assert(bmap1->ctx,
3441 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3442 isl_assert(bmap1->ctx,
3443 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3444 goto error);
3446 bmap1 = isl_basic_map_reverse(bmap1);
3447 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3448 return isl_basic_map_reverse(bmap1);
3449 error:
3450 isl_basic_map_free(bmap1);
3451 isl_basic_map_free(bmap2);
3452 return NULL;
3455 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3456 * A \cap B -> f(A) + f(B)
3458 struct isl_basic_map *isl_basic_map_sum(
3459 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3461 unsigned n_in, n_out, nparam, total, pos;
3462 struct isl_basic_map *bmap = NULL;
3463 struct isl_dim_map *dim_map1, *dim_map2;
3464 int i;
3466 if (!bmap1 || !bmap2)
3467 goto error;
3469 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3470 goto error);
3472 nparam = isl_basic_map_n_param(bmap1);
3473 n_in = isl_basic_map_n_in(bmap1);
3474 n_out = isl_basic_map_n_out(bmap1);
3476 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3477 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3478 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3479 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3480 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3481 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3482 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3483 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3484 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3485 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3486 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3488 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3489 bmap1->n_div + bmap2->n_div + 2 * n_out,
3490 bmap1->n_eq + bmap2->n_eq + n_out,
3491 bmap1->n_ineq + bmap2->n_ineq);
3492 for (i = 0; i < n_out; ++i) {
3493 int j = isl_basic_map_alloc_equality(bmap);
3494 if (j < 0)
3495 goto error;
3496 isl_seq_clr(bmap->eq[j], 1+total);
3497 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3498 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3499 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3501 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3502 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3503 bmap = add_divs(bmap, 2 * n_out);
3505 bmap = isl_basic_map_simplify(bmap);
3506 return isl_basic_map_finalize(bmap);
3507 error:
3508 isl_basic_map_free(bmap);
3509 isl_basic_map_free(bmap1);
3510 isl_basic_map_free(bmap2);
3511 return NULL;
3514 /* Given two maps A -> f(A) and B -> g(B), construct a map
3515 * A \cap B -> f(A) + f(B)
3517 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3519 struct isl_map *result;
3520 int i, j;
3522 if (!map1 || !map2)
3523 goto error;
3525 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3527 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3528 map1->n * map2->n, 0);
3529 if (!result)
3530 goto error;
3531 for (i = 0; i < map1->n; ++i)
3532 for (j = 0; j < map2->n; ++j) {
3533 struct isl_basic_map *part;
3534 part = isl_basic_map_sum(
3535 isl_basic_map_copy(map1->p[i]),
3536 isl_basic_map_copy(map2->p[j]));
3537 if (isl_basic_map_is_empty(part))
3538 isl_basic_map_free(part);
3539 else
3540 result = isl_map_add_basic_map(result, part);
3541 if (!result)
3542 goto error;
3544 isl_map_free(map1);
3545 isl_map_free(map2);
3546 return result;
3547 error:
3548 isl_map_free(map1);
3549 isl_map_free(map2);
3550 return NULL;
3553 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3554 __isl_take isl_set *set2)
3556 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3559 /* Given a basic map A -> f(A), construct A -> -f(A).
3561 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3563 int i, j;
3564 unsigned off, n;
3566 bmap = isl_basic_map_cow(bmap);
3567 if (!bmap)
3568 return NULL;
3570 n = isl_basic_map_dim(bmap, isl_dim_out);
3571 off = isl_basic_map_offset(bmap, isl_dim_out);
3572 for (i = 0; i < bmap->n_eq; ++i)
3573 for (j = 0; j < n; ++j)
3574 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3575 for (i = 0; i < bmap->n_ineq; ++i)
3576 for (j = 0; j < n; ++j)
3577 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3578 for (i = 0; i < bmap->n_div; ++i)
3579 for (j = 0; j < n; ++j)
3580 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3581 bmap = isl_basic_map_gauss(bmap, NULL);
3582 return isl_basic_map_finalize(bmap);
3585 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3587 return isl_basic_map_neg(bset);
3590 /* Given a map A -> f(A), construct A -> -f(A).
3592 struct isl_map *isl_map_neg(struct isl_map *map)
3594 int i;
3596 map = isl_map_cow(map);
3597 if (!map)
3598 return NULL;
3600 for (i = 0; i < map->n; ++i) {
3601 map->p[i] = isl_basic_map_neg(map->p[i]);
3602 if (!map->p[i])
3603 goto error;
3606 return map;
3607 error:
3608 isl_map_free(map);
3609 return NULL;
3612 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3614 return (isl_set *)isl_map_neg((isl_map *)set);
3617 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3618 * A -> floor(f(A)/d).
3620 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3621 isl_int d)
3623 unsigned n_in, n_out, nparam, total, pos;
3624 struct isl_basic_map *result = NULL;
3625 struct isl_dim_map *dim_map;
3626 int i;
3628 if (!bmap)
3629 return NULL;
3631 nparam = isl_basic_map_n_param(bmap);
3632 n_in = isl_basic_map_n_in(bmap);
3633 n_out = isl_basic_map_n_out(bmap);
3635 total = nparam + n_in + n_out + bmap->n_div + n_out;
3636 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3637 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3638 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3639 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3640 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3642 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3643 bmap->n_div + n_out,
3644 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3645 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3646 result = add_divs(result, n_out);
3647 for (i = 0; i < n_out; ++i) {
3648 int j;
3649 j = isl_basic_map_alloc_inequality(result);
3650 if (j < 0)
3651 goto error;
3652 isl_seq_clr(result->ineq[j], 1+total);
3653 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3654 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3655 j = isl_basic_map_alloc_inequality(result);
3656 if (j < 0)
3657 goto error;
3658 isl_seq_clr(result->ineq[j], 1+total);
3659 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3660 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3661 isl_int_sub_ui(result->ineq[j][0], d, 1);
3664 result = isl_basic_map_simplify(result);
3665 return isl_basic_map_finalize(result);
3666 error:
3667 isl_basic_map_free(result);
3668 return NULL;
3671 /* Given a map A -> f(A) and an integer d, construct a map
3672 * A -> floor(f(A)/d).
3674 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3676 int i;
3678 map = isl_map_cow(map);
3679 if (!map)
3680 return NULL;
3682 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3683 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3684 for (i = 0; i < map->n; ++i) {
3685 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3686 if (!map->p[i])
3687 goto error;
3690 return map;
3691 error:
3692 isl_map_free(map);
3693 return NULL;
3696 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3698 int i;
3699 unsigned nparam;
3700 unsigned n_in;
3702 i = isl_basic_map_alloc_equality(bmap);
3703 if (i < 0)
3704 goto error;
3705 nparam = isl_basic_map_n_param(bmap);
3706 n_in = isl_basic_map_n_in(bmap);
3707 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3708 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3709 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3710 return isl_basic_map_finalize(bmap);
3711 error:
3712 isl_basic_map_free(bmap);
3713 return NULL;
3716 /* Add a constraints to "bmap" expressing i_pos < o_pos
3718 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3720 int i;
3721 unsigned nparam;
3722 unsigned n_in;
3724 i = isl_basic_map_alloc_inequality(bmap);
3725 if (i < 0)
3726 goto error;
3727 nparam = isl_basic_map_n_param(bmap);
3728 n_in = isl_basic_map_n_in(bmap);
3729 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3730 isl_int_set_si(bmap->ineq[i][0], -1);
3731 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3732 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3733 return isl_basic_map_finalize(bmap);
3734 error:
3735 isl_basic_map_free(bmap);
3736 return NULL;
3739 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3741 static __isl_give isl_basic_map *var_less_or_equal(
3742 __isl_take isl_basic_map *bmap, unsigned pos)
3744 int i;
3745 unsigned nparam;
3746 unsigned n_in;
3748 i = isl_basic_map_alloc_inequality(bmap);
3749 if (i < 0)
3750 goto error;
3751 nparam = isl_basic_map_n_param(bmap);
3752 n_in = isl_basic_map_n_in(bmap);
3753 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3754 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3755 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3756 return isl_basic_map_finalize(bmap);
3757 error:
3758 isl_basic_map_free(bmap);
3759 return NULL;
3762 /* Add a constraints to "bmap" expressing i_pos > o_pos
3764 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3766 int i;
3767 unsigned nparam;
3768 unsigned n_in;
3770 i = isl_basic_map_alloc_inequality(bmap);
3771 if (i < 0)
3772 goto error;
3773 nparam = isl_basic_map_n_param(bmap);
3774 n_in = isl_basic_map_n_in(bmap);
3775 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3776 isl_int_set_si(bmap->ineq[i][0], -1);
3777 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3778 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3779 return isl_basic_map_finalize(bmap);
3780 error:
3781 isl_basic_map_free(bmap);
3782 return NULL;
3785 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3787 static __isl_give isl_basic_map *var_more_or_equal(
3788 __isl_take isl_basic_map *bmap, unsigned pos)
3790 int i;
3791 unsigned nparam;
3792 unsigned n_in;
3794 i = isl_basic_map_alloc_inequality(bmap);
3795 if (i < 0)
3796 goto error;
3797 nparam = isl_basic_map_n_param(bmap);
3798 n_in = isl_basic_map_n_in(bmap);
3799 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3800 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3801 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3802 return isl_basic_map_finalize(bmap);
3803 error:
3804 isl_basic_map_free(bmap);
3805 return NULL;
3808 __isl_give isl_basic_map *isl_basic_map_equal(
3809 __isl_take isl_space *dim, unsigned n_equal)
3811 int i;
3812 struct isl_basic_map *bmap;
3813 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3814 if (!bmap)
3815 return NULL;
3816 for (i = 0; i < n_equal && bmap; ++i)
3817 bmap = var_equal(bmap, i);
3818 return isl_basic_map_finalize(bmap);
3821 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3823 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3824 unsigned pos)
3826 int i;
3827 struct isl_basic_map *bmap;
3828 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3829 if (!bmap)
3830 return NULL;
3831 for (i = 0; i < pos && bmap; ++i)
3832 bmap = var_equal(bmap, i);
3833 if (bmap)
3834 bmap = var_less(bmap, pos);
3835 return isl_basic_map_finalize(bmap);
3838 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3840 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3841 __isl_take isl_space *dim, unsigned pos)
3843 int i;
3844 isl_basic_map *bmap;
3846 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3847 for (i = 0; i < pos; ++i)
3848 bmap = var_equal(bmap, i);
3849 bmap = var_less_or_equal(bmap, pos);
3850 return isl_basic_map_finalize(bmap);
3853 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3855 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3856 unsigned pos)
3858 int i;
3859 struct isl_basic_map *bmap;
3860 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3861 if (!bmap)
3862 return NULL;
3863 for (i = 0; i < pos && bmap; ++i)
3864 bmap = var_equal(bmap, i);
3865 if (bmap)
3866 bmap = var_more(bmap, pos);
3867 return isl_basic_map_finalize(bmap);
3870 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3872 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3873 __isl_take isl_space *dim, unsigned pos)
3875 int i;
3876 isl_basic_map *bmap;
3878 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3879 for (i = 0; i < pos; ++i)
3880 bmap = var_equal(bmap, i);
3881 bmap = var_more_or_equal(bmap, pos);
3882 return isl_basic_map_finalize(bmap);
3885 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3886 unsigned n, int equal)
3888 struct isl_map *map;
3889 int i;
3891 if (n == 0 && equal)
3892 return isl_map_universe(dims);
3894 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3896 for (i = 0; i + 1 < n; ++i)
3897 map = isl_map_add_basic_map(map,
3898 isl_basic_map_less_at(isl_space_copy(dims), i));
3899 if (n > 0) {
3900 if (equal)
3901 map = isl_map_add_basic_map(map,
3902 isl_basic_map_less_or_equal_at(dims, n - 1));
3903 else
3904 map = isl_map_add_basic_map(map,
3905 isl_basic_map_less_at(dims, n - 1));
3906 } else
3907 isl_space_free(dims);
3909 return map;
3912 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3914 if (!dims)
3915 return NULL;
3916 return map_lex_lte_first(dims, dims->n_out, equal);
3919 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3921 return map_lex_lte_first(dim, n, 0);
3924 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3926 return map_lex_lte_first(dim, n, 1);
3929 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3931 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3934 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3936 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3939 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3940 unsigned n, int equal)
3942 struct isl_map *map;
3943 int i;
3945 if (n == 0 && equal)
3946 return isl_map_universe(dims);
3948 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3950 for (i = 0; i + 1 < n; ++i)
3951 map = isl_map_add_basic_map(map,
3952 isl_basic_map_more_at(isl_space_copy(dims), i));
3953 if (n > 0) {
3954 if (equal)
3955 map = isl_map_add_basic_map(map,
3956 isl_basic_map_more_or_equal_at(dims, n - 1));
3957 else
3958 map = isl_map_add_basic_map(map,
3959 isl_basic_map_more_at(dims, n - 1));
3960 } else
3961 isl_space_free(dims);
3963 return map;
3966 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3968 if (!dims)
3969 return NULL;
3970 return map_lex_gte_first(dims, dims->n_out, equal);
3973 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3975 return map_lex_gte_first(dim, n, 0);
3978 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3980 return map_lex_gte_first(dim, n, 1);
3983 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3985 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3988 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3990 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3993 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3994 __isl_take isl_set *set2)
3996 isl_map *map;
3997 map = isl_map_lex_le(isl_set_get_space(set1));
3998 map = isl_map_intersect_domain(map, set1);
3999 map = isl_map_intersect_range(map, set2);
4000 return map;
4003 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4004 __isl_take isl_set *set2)
4006 isl_map *map;
4007 map = isl_map_lex_lt(isl_set_get_space(set1));
4008 map = isl_map_intersect_domain(map, set1);
4009 map = isl_map_intersect_range(map, set2);
4010 return map;
4013 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4014 __isl_take isl_set *set2)
4016 isl_map *map;
4017 map = isl_map_lex_ge(isl_set_get_space(set1));
4018 map = isl_map_intersect_domain(map, set1);
4019 map = isl_map_intersect_range(map, set2);
4020 return map;
4023 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4024 __isl_take isl_set *set2)
4026 isl_map *map;
4027 map = isl_map_lex_gt(isl_set_get_space(set1));
4028 map = isl_map_intersect_domain(map, set1);
4029 map = isl_map_intersect_range(map, set2);
4030 return map;
4033 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4034 __isl_take isl_map *map2)
4036 isl_map *map;
4037 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4038 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4039 map = isl_map_apply_range(map, isl_map_reverse(map2));
4040 return map;
4043 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4044 __isl_take isl_map *map2)
4046 isl_map *map;
4047 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4048 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4049 map = isl_map_apply_range(map, isl_map_reverse(map2));
4050 return map;
4053 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4054 __isl_take isl_map *map2)
4056 isl_map *map;
4057 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4058 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4059 map = isl_map_apply_range(map, isl_map_reverse(map2));
4060 return map;
4063 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4064 __isl_take isl_map *map2)
4066 isl_map *map;
4067 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4068 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4069 map = isl_map_apply_range(map, isl_map_reverse(map2));
4070 return map;
4073 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4074 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4076 struct isl_basic_map *bmap;
4078 bset = isl_basic_set_cow(bset);
4079 if (!bset || !dim)
4080 goto error;
4082 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4083 isl_space_free(bset->dim);
4084 bmap = (struct isl_basic_map *) bset;
4085 bmap->dim = dim;
4086 return isl_basic_map_finalize(bmap);
4087 error:
4088 isl_basic_set_free(bset);
4089 isl_space_free(dim);
4090 return NULL;
4093 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4095 if (!bmap)
4096 goto error;
4097 if (bmap->dim->n_in == 0)
4098 return (struct isl_basic_set *)bmap;
4099 bmap = isl_basic_map_cow(bmap);
4100 if (!bmap)
4101 goto error;
4102 bmap->dim = isl_space_as_set_space(bmap->dim);
4103 if (!bmap->dim)
4104 goto error;
4105 bmap = isl_basic_map_finalize(bmap);
4106 return (struct isl_basic_set *)bmap;
4107 error:
4108 isl_basic_map_free(bmap);
4109 return NULL;
4112 /* For a div d = floor(f/m), add the constraints
4114 * f - m d >= 0
4115 * -(f-(n-1)) + m d >= 0
4117 * Note that the second constraint is the negation of
4119 * f - m d >= n
4121 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4122 unsigned pos, isl_int *div)
4124 int i, j;
4125 unsigned total = isl_basic_map_total_dim(bmap);
4127 i = isl_basic_map_alloc_inequality(bmap);
4128 if (i < 0)
4129 return -1;
4130 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4131 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4133 j = isl_basic_map_alloc_inequality(bmap);
4134 if (j < 0)
4135 return -1;
4136 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4137 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4138 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4139 return j;
4142 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4143 unsigned pos, isl_int *div)
4145 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4146 pos, div);
4149 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4151 unsigned total = isl_basic_map_total_dim(bmap);
4152 unsigned div_pos = total - bmap->n_div + div;
4154 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4155 bmap->div[div]);
4158 struct isl_basic_set *isl_basic_map_underlying_set(
4159 struct isl_basic_map *bmap)
4161 if (!bmap)
4162 goto error;
4163 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4164 bmap->n_div == 0 &&
4165 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4166 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4167 return (struct isl_basic_set *)bmap;
4168 bmap = isl_basic_map_cow(bmap);
4169 if (!bmap)
4170 goto error;
4171 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4172 if (!bmap->dim)
4173 goto error;
4174 bmap->extra -= bmap->n_div;
4175 bmap->n_div = 0;
4176 bmap = isl_basic_map_finalize(bmap);
4177 return (struct isl_basic_set *)bmap;
4178 error:
4179 return NULL;
4182 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4183 __isl_take isl_basic_set *bset)
4185 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4188 struct isl_basic_map *isl_basic_map_overlying_set(
4189 struct isl_basic_set *bset, struct isl_basic_map *like)
4191 struct isl_basic_map *bmap;
4192 struct isl_ctx *ctx;
4193 unsigned total;
4194 int i;
4196 if (!bset || !like)
4197 goto error;
4198 ctx = bset->ctx;
4199 isl_assert(ctx, bset->n_div == 0, goto error);
4200 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4201 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4202 goto error);
4203 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4204 isl_basic_map_free(like);
4205 return (struct isl_basic_map *)bset;
4207 bset = isl_basic_set_cow(bset);
4208 if (!bset)
4209 goto error;
4210 total = bset->dim->n_out + bset->extra;
4211 bmap = (struct isl_basic_map *)bset;
4212 isl_space_free(bmap->dim);
4213 bmap->dim = isl_space_copy(like->dim);
4214 if (!bmap->dim)
4215 goto error;
4216 bmap->n_div = like->n_div;
4217 bmap->extra += like->n_div;
4218 if (bmap->extra) {
4219 unsigned ltotal;
4220 isl_int **div;
4221 ltotal = total - bmap->extra + like->extra;
4222 if (ltotal > total)
4223 ltotal = total;
4224 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4225 bmap->extra * (1 + 1 + total));
4226 if (isl_blk_is_error(bmap->block2))
4227 goto error;
4228 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4229 if (!div)
4230 goto error;
4231 bmap->div = div;
4232 for (i = 0; i < bmap->extra; ++i)
4233 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4234 for (i = 0; i < like->n_div; ++i) {
4235 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4236 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4238 bmap = isl_basic_map_extend_constraints(bmap,
4239 0, 2 * like->n_div);
4240 for (i = 0; i < like->n_div; ++i) {
4241 if (isl_int_is_zero(bmap->div[i][0]))
4242 continue;
4243 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4244 goto error;
4247 isl_basic_map_free(like);
4248 bmap = isl_basic_map_simplify(bmap);
4249 bmap = isl_basic_map_finalize(bmap);
4250 return bmap;
4251 error:
4252 isl_basic_map_free(like);
4253 isl_basic_set_free(bset);
4254 return NULL;
4257 struct isl_basic_set *isl_basic_set_from_underlying_set(
4258 struct isl_basic_set *bset, struct isl_basic_set *like)
4260 return (struct isl_basic_set *)
4261 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4264 struct isl_set *isl_set_from_underlying_set(
4265 struct isl_set *set, struct isl_basic_set *like)
4267 int i;
4269 if (!set || !like)
4270 goto error;
4271 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4272 goto error);
4273 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4274 isl_basic_set_free(like);
4275 return set;
4277 set = isl_set_cow(set);
4278 if (!set)
4279 goto error;
4280 for (i = 0; i < set->n; ++i) {
4281 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4282 isl_basic_set_copy(like));
4283 if (!set->p[i])
4284 goto error;
4286 isl_space_free(set->dim);
4287 set->dim = isl_space_copy(like->dim);
4288 if (!set->dim)
4289 goto error;
4290 isl_basic_set_free(like);
4291 return set;
4292 error:
4293 isl_basic_set_free(like);
4294 isl_set_free(set);
4295 return NULL;
4298 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4300 int i;
4302 map = isl_map_cow(map);
4303 if (!map)
4304 return NULL;
4305 map->dim = isl_space_cow(map->dim);
4306 if (!map->dim)
4307 goto error;
4309 for (i = 1; i < map->n; ++i)
4310 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4311 goto error);
4312 for (i = 0; i < map->n; ++i) {
4313 map->p[i] = (struct isl_basic_map *)
4314 isl_basic_map_underlying_set(map->p[i]);
4315 if (!map->p[i])
4316 goto error;
4318 if (map->n == 0)
4319 map->dim = isl_space_underlying(map->dim, 0);
4320 else {
4321 isl_space_free(map->dim);
4322 map->dim = isl_space_copy(map->p[0]->dim);
4324 if (!map->dim)
4325 goto error;
4326 return (struct isl_set *)map;
4327 error:
4328 isl_map_free(map);
4329 return NULL;
4332 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4334 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4337 __isl_give isl_basic_map *isl_basic_map_reset_space(
4338 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4340 bmap = isl_basic_map_cow(bmap);
4341 if (!bmap || !dim)
4342 goto error;
4344 isl_space_free(bmap->dim);
4345 bmap->dim = dim;
4347 bmap = isl_basic_map_finalize(bmap);
4349 return bmap;
4350 error:
4351 isl_basic_map_free(bmap);
4352 isl_space_free(dim);
4353 return NULL;
4356 __isl_give isl_basic_set *isl_basic_set_reset_space(
4357 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4359 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4360 dim);
4363 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4364 __isl_take isl_space *dim)
4366 int i;
4368 map = isl_map_cow(map);
4369 if (!map || !dim)
4370 goto error;
4372 for (i = 0; i < map->n; ++i) {
4373 map->p[i] = isl_basic_map_reset_space(map->p[i],
4374 isl_space_copy(dim));
4375 if (!map->p[i])
4376 goto error;
4378 isl_space_free(map->dim);
4379 map->dim = dim;
4381 return map;
4382 error:
4383 isl_map_free(map);
4384 isl_space_free(dim);
4385 return NULL;
4388 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4389 __isl_take isl_space *dim)
4391 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4394 /* Compute the parameter domain of the given basic set.
4396 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4398 isl_space *space;
4399 unsigned n;
4401 if (isl_basic_set_is_params(bset))
4402 return bset;
4404 n = isl_basic_set_dim(bset, isl_dim_set);
4405 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4406 space = isl_basic_set_get_space(bset);
4407 space = isl_space_params(space);
4408 bset = isl_basic_set_reset_space(bset, space);
4409 return bset;
4412 /* Compute the parameter domain of the given set.
4414 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4416 isl_space *space;
4417 unsigned n;
4419 if (isl_set_is_params(set))
4420 return set;
4422 n = isl_set_dim(set, isl_dim_set);
4423 set = isl_set_project_out(set, isl_dim_set, 0, n);
4424 space = isl_set_get_space(set);
4425 space = isl_space_params(space);
4426 set = isl_set_reset_space(set, space);
4427 return set;
4430 /* Construct a zero-dimensional set with the given parameter domain.
4432 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4434 isl_space *space;
4435 space = isl_set_get_space(set);
4436 space = isl_space_set_from_params(space);
4437 set = isl_set_reset_space(set, space);
4438 return set;
4441 /* Compute the parameter domain of the given map.
4443 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4445 isl_space *space;
4446 unsigned n;
4448 n = isl_map_dim(map, isl_dim_in);
4449 map = isl_map_project_out(map, isl_dim_in, 0, n);
4450 n = isl_map_dim(map, isl_dim_out);
4451 map = isl_map_project_out(map, isl_dim_out, 0, n);
4452 space = isl_map_get_space(map);
4453 space = isl_space_params(space);
4454 map = isl_map_reset_space(map, space);
4455 return map;
4458 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4460 isl_space *dim;
4461 struct isl_basic_set *domain;
4462 unsigned n_in;
4463 unsigned n_out;
4465 if (!bmap)
4466 return NULL;
4467 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4469 n_in = isl_basic_map_n_in(bmap);
4470 n_out = isl_basic_map_n_out(bmap);
4471 domain = isl_basic_set_from_basic_map(bmap);
4472 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4474 domain = isl_basic_set_reset_space(domain, dim);
4476 return domain;
4479 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4481 if (!bmap)
4482 return -1;
4483 return isl_space_may_be_set(bmap->dim);
4486 /* Is this basic map actually a set?
4487 * Users should never call this function. Outside of isl,
4488 * the type should indicate whether something is a set or a map.
4490 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4492 if (!bmap)
4493 return -1;
4494 return isl_space_is_set(bmap->dim);
4497 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4499 if (!bmap)
4500 return NULL;
4501 if (isl_basic_map_is_set(bmap))
4502 return bmap;
4503 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4506 __isl_give isl_basic_map *isl_basic_map_domain_map(
4507 __isl_take isl_basic_map *bmap)
4509 int i, k;
4510 isl_space *dim;
4511 isl_basic_map *domain;
4512 int nparam, n_in, n_out;
4513 unsigned total;
4515 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4516 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4517 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4519 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4520 domain = isl_basic_map_universe(dim);
4522 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4523 bmap = isl_basic_map_apply_range(bmap, domain);
4524 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4526 total = isl_basic_map_total_dim(bmap);
4528 for (i = 0; i < n_in; ++i) {
4529 k = isl_basic_map_alloc_equality(bmap);
4530 if (k < 0)
4531 goto error;
4532 isl_seq_clr(bmap->eq[k], 1 + total);
4533 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4534 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4537 bmap = isl_basic_map_gauss(bmap, NULL);
4538 return isl_basic_map_finalize(bmap);
4539 error:
4540 isl_basic_map_free(bmap);
4541 return NULL;
4544 __isl_give isl_basic_map *isl_basic_map_range_map(
4545 __isl_take isl_basic_map *bmap)
4547 int i, k;
4548 isl_space *dim;
4549 isl_basic_map *range;
4550 int nparam, n_in, n_out;
4551 unsigned total;
4553 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4554 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4555 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4557 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4558 range = isl_basic_map_universe(dim);
4560 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4561 bmap = isl_basic_map_apply_range(bmap, range);
4562 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4564 total = isl_basic_map_total_dim(bmap);
4566 for (i = 0; i < n_out; ++i) {
4567 k = isl_basic_map_alloc_equality(bmap);
4568 if (k < 0)
4569 goto error;
4570 isl_seq_clr(bmap->eq[k], 1 + total);
4571 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4572 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4575 bmap = isl_basic_map_gauss(bmap, NULL);
4576 return isl_basic_map_finalize(bmap);
4577 error:
4578 isl_basic_map_free(bmap);
4579 return NULL;
4582 int isl_map_may_be_set(__isl_keep isl_map *map)
4584 if (!map)
4585 return -1;
4586 return isl_space_may_be_set(map->dim);
4589 /* Is this map actually a set?
4590 * Users should never call this function. Outside of isl,
4591 * the type should indicate whether something is a set or a map.
4593 int isl_map_is_set(__isl_keep isl_map *map)
4595 if (!map)
4596 return -1;
4597 return isl_space_is_set(map->dim);
4600 struct isl_set *isl_map_range(struct isl_map *map)
4602 int i;
4603 struct isl_set *set;
4605 if (!map)
4606 goto error;
4607 if (isl_map_is_set(map))
4608 return (isl_set *)map;
4610 map = isl_map_cow(map);
4611 if (!map)
4612 goto error;
4614 set = (struct isl_set *) map;
4615 set->dim = isl_space_range(set->dim);
4616 if (!set->dim)
4617 goto error;
4618 for (i = 0; i < map->n; ++i) {
4619 set->p[i] = isl_basic_map_range(map->p[i]);
4620 if (!set->p[i])
4621 goto error;
4623 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4624 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4625 return set;
4626 error:
4627 isl_map_free(map);
4628 return NULL;
4631 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4633 int i;
4634 isl_space *domain_dim;
4636 map = isl_map_cow(map);
4637 if (!map)
4638 return NULL;
4640 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4641 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4642 map->dim = isl_space_join(map->dim, domain_dim);
4643 if (!map->dim)
4644 goto error;
4645 for (i = 0; i < map->n; ++i) {
4646 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4647 if (!map->p[i])
4648 goto error;
4650 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4651 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4652 return map;
4653 error:
4654 isl_map_free(map);
4655 return NULL;
4658 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4660 int i;
4661 isl_space *range_dim;
4663 map = isl_map_cow(map);
4664 if (!map)
4665 return NULL;
4667 range_dim = isl_space_range(isl_map_get_space(map));
4668 range_dim = isl_space_from_range(range_dim);
4669 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4670 map->dim = isl_space_join(map->dim, range_dim);
4671 if (!map->dim)
4672 goto error;
4673 for (i = 0; i < map->n; ++i) {
4674 map->p[i] = isl_basic_map_range_map(map->p[i]);
4675 if (!map->p[i])
4676 goto error;
4678 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4679 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4680 return map;
4681 error:
4682 isl_map_free(map);
4683 return NULL;
4686 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4687 __isl_take isl_space *dim)
4689 int i;
4690 struct isl_map *map = NULL;
4692 set = isl_set_cow(set);
4693 if (!set || !dim)
4694 goto error;
4695 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4696 map = (struct isl_map *)set;
4697 for (i = 0; i < set->n; ++i) {
4698 map->p[i] = isl_basic_map_from_basic_set(
4699 set->p[i], isl_space_copy(dim));
4700 if (!map->p[i])
4701 goto error;
4703 isl_space_free(map->dim);
4704 map->dim = dim;
4705 return map;
4706 error:
4707 isl_space_free(dim);
4708 isl_set_free(set);
4709 return NULL;
4712 __isl_give isl_basic_map *isl_basic_map_from_domain(
4713 __isl_take isl_basic_set *bset)
4715 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4718 __isl_give isl_basic_map *isl_basic_map_from_range(
4719 __isl_take isl_basic_set *bset)
4721 isl_space *space;
4722 space = isl_basic_set_get_space(bset);
4723 space = isl_space_from_range(space);
4724 bset = isl_basic_set_reset_space(bset, space);
4725 return (isl_basic_map *)bset;
4728 struct isl_map *isl_map_from_range(struct isl_set *set)
4730 isl_space *space;
4731 space = isl_set_get_space(set);
4732 space = isl_space_from_range(space);
4733 set = isl_set_reset_space(set, space);
4734 return (struct isl_map *)set;
4737 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4739 return isl_map_reverse(isl_map_from_range(set));
4742 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4743 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4745 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4748 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4749 __isl_take isl_set *range)
4751 return isl_map_apply_range(isl_map_reverse(domain), range);
4754 struct isl_set *isl_set_from_map(struct isl_map *map)
4756 int i;
4757 struct isl_set *set = NULL;
4759 if (!map)
4760 return NULL;
4761 map = isl_map_cow(map);
4762 if (!map)
4763 return NULL;
4764 map->dim = isl_space_as_set_space(map->dim);
4765 if (!map->dim)
4766 goto error;
4767 set = (struct isl_set *)map;
4768 for (i = 0; i < map->n; ++i) {
4769 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4770 if (!set->p[i])
4771 goto error;
4773 return set;
4774 error:
4775 isl_map_free(map);
4776 return NULL;
4779 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4780 unsigned flags)
4782 struct isl_map *map;
4784 if (!dim)
4785 return NULL;
4786 if (n < 0)
4787 isl_die(dim->ctx, isl_error_internal,
4788 "negative number of basic maps", goto error);
4789 map = isl_alloc(dim->ctx, struct isl_map,
4790 sizeof(struct isl_map) +
4791 (n - 1) * sizeof(struct isl_basic_map *));
4792 if (!map)
4793 goto error;
4795 map->ctx = dim->ctx;
4796 isl_ctx_ref(map->ctx);
4797 map->ref = 1;
4798 map->size = n;
4799 map->n = 0;
4800 map->dim = dim;
4801 map->flags = flags;
4802 return map;
4803 error:
4804 isl_space_free(dim);
4805 return NULL;
4808 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4809 unsigned nparam, unsigned in, unsigned out, int n,
4810 unsigned flags)
4812 struct isl_map *map;
4813 isl_space *dims;
4815 dims = isl_space_alloc(ctx, nparam, in, out);
4816 if (!dims)
4817 return NULL;
4819 map = isl_map_alloc_space(dims, n, flags);
4820 return map;
4823 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4825 struct isl_basic_map *bmap;
4826 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4827 bmap = isl_basic_map_set_to_empty(bmap);
4828 return bmap;
4831 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4833 struct isl_basic_set *bset;
4834 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4835 bset = isl_basic_set_set_to_empty(bset);
4836 return bset;
4839 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4841 struct isl_basic_map *bmap;
4842 if (!model)
4843 return NULL;
4844 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4845 bmap = isl_basic_map_set_to_empty(bmap);
4846 return bmap;
4849 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4851 struct isl_basic_map *bmap;
4852 if (!model)
4853 return NULL;
4854 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4855 bmap = isl_basic_map_set_to_empty(bmap);
4856 return bmap;
4859 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4861 struct isl_basic_set *bset;
4862 if (!model)
4863 return NULL;
4864 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4865 bset = isl_basic_set_set_to_empty(bset);
4866 return bset;
4869 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4871 struct isl_basic_map *bmap;
4872 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4873 bmap = isl_basic_map_finalize(bmap);
4874 return bmap;
4877 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4879 struct isl_basic_set *bset;
4880 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4881 bset = isl_basic_set_finalize(bset);
4882 return bset;
4885 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4887 int i;
4888 unsigned total = isl_space_dim(dim, isl_dim_all);
4889 isl_basic_map *bmap;
4891 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4892 for (i = 0; i < total; ++i) {
4893 int k = isl_basic_map_alloc_inequality(bmap);
4894 if (k < 0)
4895 goto error;
4896 isl_seq_clr(bmap->ineq[k], 1 + total);
4897 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4899 return bmap;
4900 error:
4901 isl_basic_map_free(bmap);
4902 return NULL;
4905 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4907 return isl_basic_map_nat_universe(dim);
4910 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4912 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4915 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4917 return isl_map_nat_universe(dim);
4920 __isl_give isl_basic_map *isl_basic_map_universe_like(
4921 __isl_keep isl_basic_map *model)
4923 if (!model)
4924 return NULL;
4925 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4928 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4930 if (!model)
4931 return NULL;
4932 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4935 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4936 __isl_keep isl_set *model)
4938 if (!model)
4939 return NULL;
4940 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4943 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4945 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4948 struct isl_map *isl_map_empty_like(struct isl_map *model)
4950 if (!model)
4951 return NULL;
4952 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4955 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4957 if (!model)
4958 return NULL;
4959 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4962 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4964 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4967 struct isl_set *isl_set_empty_like(struct isl_set *model)
4969 if (!model)
4970 return NULL;
4971 return isl_set_empty(isl_space_copy(model->dim));
4974 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4976 struct isl_map *map;
4977 if (!dim)
4978 return NULL;
4979 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4980 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4981 return map;
4984 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4986 struct isl_set *set;
4987 if (!dim)
4988 return NULL;
4989 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4990 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4991 return set;
4994 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4996 if (!model)
4997 return NULL;
4998 return isl_set_universe(isl_space_copy(model->dim));
5001 struct isl_map *isl_map_dup(struct isl_map *map)
5003 int i;
5004 struct isl_map *dup;
5006 if (!map)
5007 return NULL;
5008 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5009 for (i = 0; i < map->n; ++i)
5010 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5011 return dup;
5014 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5015 __isl_take isl_basic_map *bmap)
5017 if (!bmap || !map)
5018 goto error;
5019 if (isl_basic_map_plain_is_empty(bmap)) {
5020 isl_basic_map_free(bmap);
5021 return map;
5023 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5024 isl_assert(map->ctx, map->n < map->size, goto error);
5025 map->p[map->n] = bmap;
5026 map->n++;
5027 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5028 return map;
5029 error:
5030 if (map)
5031 isl_map_free(map);
5032 if (bmap)
5033 isl_basic_map_free(bmap);
5034 return NULL;
5037 void isl_map_free(struct isl_map *map)
5039 int i;
5041 if (!map)
5042 return;
5044 if (--map->ref > 0)
5045 return;
5047 isl_ctx_deref(map->ctx);
5048 for (i = 0; i < map->n; ++i)
5049 isl_basic_map_free(map->p[i]);
5050 isl_space_free(map->dim);
5051 free(map);
5054 struct isl_map *isl_map_extend(struct isl_map *base,
5055 unsigned nparam, unsigned n_in, unsigned n_out)
5057 int i;
5059 base = isl_map_cow(base);
5060 if (!base)
5061 return NULL;
5063 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5064 if (!base->dim)
5065 goto error;
5066 for (i = 0; i < base->n; ++i) {
5067 base->p[i] = isl_basic_map_extend_space(base->p[i],
5068 isl_space_copy(base->dim), 0, 0, 0);
5069 if (!base->p[i])
5070 goto error;
5072 return base;
5073 error:
5074 isl_map_free(base);
5075 return NULL;
5078 struct isl_set *isl_set_extend(struct isl_set *base,
5079 unsigned nparam, unsigned dim)
5081 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5082 nparam, 0, dim);
5085 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5086 struct isl_basic_map *bmap, unsigned pos, int value)
5088 int j;
5090 bmap = isl_basic_map_cow(bmap);
5091 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5092 j = isl_basic_map_alloc_equality(bmap);
5093 if (j < 0)
5094 goto error;
5095 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5096 isl_int_set_si(bmap->eq[j][pos], -1);
5097 isl_int_set_si(bmap->eq[j][0], value);
5098 bmap = isl_basic_map_simplify(bmap);
5099 return isl_basic_map_finalize(bmap);
5100 error:
5101 isl_basic_map_free(bmap);
5102 return NULL;
5105 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5106 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5108 int j;
5110 bmap = isl_basic_map_cow(bmap);
5111 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5112 j = isl_basic_map_alloc_equality(bmap);
5113 if (j < 0)
5114 goto error;
5115 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5116 isl_int_set_si(bmap->eq[j][pos], -1);
5117 isl_int_set(bmap->eq[j][0], value);
5118 bmap = isl_basic_map_simplify(bmap);
5119 return isl_basic_map_finalize(bmap);
5120 error:
5121 isl_basic_map_free(bmap);
5122 return NULL;
5125 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5126 enum isl_dim_type type, unsigned pos, int value)
5128 if (!bmap)
5129 return NULL;
5130 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5131 return isl_basic_map_fix_pos_si(bmap,
5132 isl_basic_map_offset(bmap, type) + pos, value);
5133 error:
5134 isl_basic_map_free(bmap);
5135 return NULL;
5138 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5139 enum isl_dim_type type, unsigned pos, isl_int value)
5141 if (!bmap)
5142 return NULL;
5143 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5144 return isl_basic_map_fix_pos(bmap,
5145 isl_basic_map_offset(bmap, type) + pos, value);
5146 error:
5147 isl_basic_map_free(bmap);
5148 return NULL;
5151 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5152 enum isl_dim_type type, unsigned pos, int value)
5154 return (struct isl_basic_set *)
5155 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5156 type, pos, value);
5159 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5160 enum isl_dim_type type, unsigned pos, isl_int value)
5162 return (struct isl_basic_set *)
5163 isl_basic_map_fix((struct isl_basic_map *)bset,
5164 type, pos, value);
5167 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5168 unsigned input, int value)
5170 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5173 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5174 unsigned dim, int value)
5176 return (struct isl_basic_set *)
5177 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5178 isl_dim_set, dim, value);
5181 static int remove_if_empty(__isl_keep isl_map *map, int i)
5183 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5185 if (empty < 0)
5186 return -1;
5187 if (!empty)
5188 return 0;
5190 isl_basic_map_free(map->p[i]);
5191 if (i != map->n - 1) {
5192 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5193 map->p[i] = map->p[map->n - 1];
5195 map->n--;
5197 return 0;
5200 struct isl_map *isl_map_fix_si(struct isl_map *map,
5201 enum isl_dim_type type, unsigned pos, int value)
5203 int i;
5205 map = isl_map_cow(map);
5206 if (!map)
5207 return NULL;
5209 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5210 for (i = map->n - 1; i >= 0; --i) {
5211 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5212 if (remove_if_empty(map, i) < 0)
5213 goto error;
5215 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5216 return map;
5217 error:
5218 isl_map_free(map);
5219 return NULL;
5222 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5223 enum isl_dim_type type, unsigned pos, int value)
5225 return (struct isl_set *)
5226 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5229 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5230 enum isl_dim_type type, unsigned pos, isl_int value)
5232 int i;
5234 map = isl_map_cow(map);
5235 if (!map)
5236 return NULL;
5238 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5239 for (i = 0; i < map->n; ++i) {
5240 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5241 if (!map->p[i])
5242 goto error;
5244 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5245 return map;
5246 error:
5247 isl_map_free(map);
5248 return NULL;
5251 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5252 enum isl_dim_type type, unsigned pos, isl_int value)
5254 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5257 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5258 unsigned input, int value)
5260 return isl_map_fix_si(map, isl_dim_in, input, value);
5263 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5265 return (struct isl_set *)
5266 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5269 static __isl_give isl_basic_map *basic_map_bound_si(
5270 __isl_take isl_basic_map *bmap,
5271 enum isl_dim_type type, unsigned pos, int value, int upper)
5273 int j;
5275 if (!bmap)
5276 return NULL;
5277 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5278 pos += isl_basic_map_offset(bmap, type);
5279 bmap = isl_basic_map_cow(bmap);
5280 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5281 j = isl_basic_map_alloc_inequality(bmap);
5282 if (j < 0)
5283 goto error;
5284 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5285 if (upper) {
5286 isl_int_set_si(bmap->ineq[j][pos], -1);
5287 isl_int_set_si(bmap->ineq[j][0], value);
5288 } else {
5289 isl_int_set_si(bmap->ineq[j][pos], 1);
5290 isl_int_set_si(bmap->ineq[j][0], -value);
5292 bmap = isl_basic_map_simplify(bmap);
5293 return isl_basic_map_finalize(bmap);
5294 error:
5295 isl_basic_map_free(bmap);
5296 return NULL;
5299 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5300 __isl_take isl_basic_map *bmap,
5301 enum isl_dim_type type, unsigned pos, int value)
5303 return basic_map_bound_si(bmap, type, pos, value, 0);
5306 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5307 unsigned dim, isl_int value)
5309 int j;
5311 bset = isl_basic_set_cow(bset);
5312 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5313 j = isl_basic_set_alloc_inequality(bset);
5314 if (j < 0)
5315 goto error;
5316 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5317 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5318 isl_int_neg(bset->ineq[j][0], value);
5319 bset = isl_basic_set_simplify(bset);
5320 return isl_basic_set_finalize(bset);
5321 error:
5322 isl_basic_set_free(bset);
5323 return NULL;
5326 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5327 enum isl_dim_type type, unsigned pos, int value, int upper)
5329 int i;
5331 map = isl_map_cow(map);
5332 if (!map)
5333 return NULL;
5335 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5336 for (i = 0; i < map->n; ++i) {
5337 map->p[i] = basic_map_bound_si(map->p[i],
5338 type, pos, value, upper);
5339 if (!map->p[i])
5340 goto error;
5342 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5343 return map;
5344 error:
5345 isl_map_free(map);
5346 return NULL;
5349 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5350 enum isl_dim_type type, unsigned pos, int value)
5352 return map_bound_si(map, type, pos, value, 0);
5355 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5356 enum isl_dim_type type, unsigned pos, int value)
5358 return map_bound_si(map, type, pos, value, 1);
5361 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5362 enum isl_dim_type type, unsigned pos, int value)
5364 return (struct isl_set *)
5365 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5368 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5369 enum isl_dim_type type, unsigned pos, int value)
5371 return isl_map_upper_bound_si(set, type, pos, value);
5374 /* Bound the given variable of "bmap" from below (or above is "upper"
5375 * is set) to "value".
5377 static __isl_give isl_basic_map *basic_map_bound(
5378 __isl_take isl_basic_map *bmap,
5379 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5381 int j;
5383 if (!bmap)
5384 return NULL;
5385 if (pos >= isl_basic_map_dim(bmap, type))
5386 isl_die(bmap->ctx, isl_error_invalid,
5387 "index out of bounds", goto error);
5388 pos += isl_basic_map_offset(bmap, type);
5389 bmap = isl_basic_map_cow(bmap);
5390 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5391 j = isl_basic_map_alloc_inequality(bmap);
5392 if (j < 0)
5393 goto error;
5394 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5395 if (upper) {
5396 isl_int_set_si(bmap->ineq[j][pos], -1);
5397 isl_int_set(bmap->ineq[j][0], value);
5398 } else {
5399 isl_int_set_si(bmap->ineq[j][pos], 1);
5400 isl_int_neg(bmap->ineq[j][0], value);
5402 bmap = isl_basic_map_simplify(bmap);
5403 return isl_basic_map_finalize(bmap);
5404 error:
5405 isl_basic_map_free(bmap);
5406 return NULL;
5409 /* Bound the given variable of "map" from below (or above is "upper"
5410 * is set) to "value".
5412 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5413 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5415 int i;
5417 map = isl_map_cow(map);
5418 if (!map)
5419 return NULL;
5421 if (pos >= isl_map_dim(map, type))
5422 isl_die(map->ctx, isl_error_invalid,
5423 "index out of bounds", goto error);
5424 for (i = map->n - 1; i >= 0; --i) {
5425 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5426 if (remove_if_empty(map, i) < 0)
5427 goto error;
5429 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5430 return map;
5431 error:
5432 isl_map_free(map);
5433 return NULL;
5436 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5437 enum isl_dim_type type, unsigned pos, isl_int value)
5439 return map_bound(map, type, pos, value, 0);
5442 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5443 enum isl_dim_type type, unsigned pos, isl_int value)
5445 return map_bound(map, type, pos, value, 1);
5448 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5449 enum isl_dim_type type, unsigned pos, isl_int value)
5451 return isl_map_lower_bound(set, type, pos, value);
5454 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5455 enum isl_dim_type type, unsigned pos, isl_int value)
5457 return isl_map_upper_bound(set, type, pos, value);
5460 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5461 isl_int value)
5463 int i;
5465 set = isl_set_cow(set);
5466 if (!set)
5467 return NULL;
5469 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5470 for (i = 0; i < set->n; ++i) {
5471 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5472 if (!set->p[i])
5473 goto error;
5475 return set;
5476 error:
5477 isl_set_free(set);
5478 return NULL;
5481 struct isl_map *isl_map_reverse(struct isl_map *map)
5483 int i;
5485 map = isl_map_cow(map);
5486 if (!map)
5487 return NULL;
5489 map->dim = isl_space_reverse(map->dim);
5490 if (!map->dim)
5491 goto error;
5492 for (i = 0; i < map->n; ++i) {
5493 map->p[i] = isl_basic_map_reverse(map->p[i]);
5494 if (!map->p[i])
5495 goto error;
5497 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5498 return map;
5499 error:
5500 isl_map_free(map);
5501 return NULL;
5504 static struct isl_map *isl_basic_map_partial_lexopt(
5505 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5506 struct isl_set **empty, int max)
5508 if (!bmap)
5509 goto error;
5510 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5511 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5512 else
5513 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5514 error:
5515 isl_basic_map_free(bmap);
5516 isl_basic_set_free(dom);
5517 if (empty)
5518 *empty = NULL;
5519 return NULL;
5522 struct isl_map *isl_basic_map_partial_lexmax(
5523 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5524 struct isl_set **empty)
5526 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5529 struct isl_map *isl_basic_map_partial_lexmin(
5530 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5531 struct isl_set **empty)
5533 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5536 struct isl_set *isl_basic_set_partial_lexmin(
5537 struct isl_basic_set *bset, struct isl_basic_set *dom,
5538 struct isl_set **empty)
5540 return (struct isl_set *)
5541 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5542 dom, empty);
5545 struct isl_set *isl_basic_set_partial_lexmax(
5546 struct isl_basic_set *bset, struct isl_basic_set *dom,
5547 struct isl_set **empty)
5549 return (struct isl_set *)
5550 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5551 dom, empty);
5554 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5555 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5556 __isl_give isl_set **empty)
5558 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5561 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5562 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5563 __isl_give isl_set **empty)
5565 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5568 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5569 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5570 __isl_give isl_set **empty)
5572 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5575 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5576 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5577 __isl_give isl_set **empty)
5579 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5582 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5583 __isl_take isl_basic_map *bmap, int max)
5585 isl_basic_set *dom = NULL;
5586 isl_space *dom_space;
5588 if (!bmap)
5589 goto error;
5590 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5591 dom = isl_basic_set_universe(dom_space);
5592 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5593 error:
5594 isl_basic_map_free(bmap);
5595 return NULL;
5598 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5599 __isl_take isl_basic_map *bmap)
5601 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5604 /* Given a basic map "bmap", compute the lexicographically minimal
5605 * (or maximal) image element for each domain element in dom.
5606 * Set *empty to those elements in dom that do not have an image element.
5608 * We first make sure the basic sets in dom are disjoint and then
5609 * simply collect the results over each of the basic sets separately.
5610 * We could probably improve the efficiency a bit by moving the union
5611 * domain down into the parametric integer programming.
5613 static __isl_give isl_map *basic_map_partial_lexopt(
5614 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5615 __isl_give isl_set **empty, int max)
5617 int i;
5618 struct isl_map *res;
5620 dom = isl_set_make_disjoint(dom);
5621 if (!dom)
5622 goto error;
5624 if (isl_set_plain_is_empty(dom)) {
5625 res = isl_map_empty_like_basic_map(bmap);
5626 *empty = isl_set_empty_like(dom);
5627 isl_set_free(dom);
5628 isl_basic_map_free(bmap);
5629 return res;
5632 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5633 isl_basic_set_copy(dom->p[0]), empty, max);
5635 for (i = 1; i < dom->n; ++i) {
5636 struct isl_map *res_i;
5637 struct isl_set *empty_i;
5639 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5640 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5642 res = isl_map_union_disjoint(res, res_i);
5643 *empty = isl_set_union_disjoint(*empty, empty_i);
5646 isl_set_free(dom);
5647 isl_basic_map_free(bmap);
5648 return res;
5649 error:
5650 *empty = NULL;
5651 isl_set_free(dom);
5652 isl_basic_map_free(bmap);
5653 return NULL;
5656 /* Given a map "map", compute the lexicographically minimal
5657 * (or maximal) image element for each domain element in dom.
5658 * Set *empty to those elements in dom that do not have an image element.
5660 * We first compute the lexicographically minimal or maximal element
5661 * in the first basic map. This results in a partial solution "res"
5662 * and a subset "todo" of dom that still need to be handled.
5663 * We then consider each of the remaining maps in "map" and successively
5664 * improve both "res" and "todo".
5666 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5667 * Assume we are computing the lexicographical maximum.
5668 * We first compute the lexicographically maximal element in basic map i.
5669 * This results in a partial solution res_i and a subset todo_i.
5670 * Then we combine these results with those obtain for the first k basic maps
5671 * to obtain a result that is valid for the first k+1 basic maps.
5672 * In particular, the set where there is no solution is the set where
5673 * there is no solution for the first k basic maps and also no solution
5674 * for the ith basic map, i.e.,
5676 * todo^i = todo^k * todo_i
5678 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5679 * solutions, arbitrarily breaking ties in favor of res^k.
5680 * That is, when res^k(a) >= res_i(a), we pick res^k and
5681 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5682 * the lexicographic order.)
5683 * In practice, we compute
5685 * res^k * (res_i . "<=")
5687 * and
5689 * res_i * (res^k . "<")
5691 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5692 * where only one of res^k and res_i provides a solution and we simply pick
5693 * that one, i.e.,
5695 * res^k * todo_i
5696 * and
5697 * res_i * todo^k
5699 * Note that we only compute these intersections when dom(res^k) intersects
5700 * dom(res_i). Otherwise, the only effect of these intersections is to
5701 * potentially break up res^k and res_i into smaller pieces.
5702 * We want to avoid such splintering as much as possible.
5703 * In fact, an earlier implementation of this function would look for
5704 * better results in the domain of res^k and for extra results in todo^k,
5705 * but this would always result in a splintering according to todo^k,
5706 * even when the domain of basic map i is disjoint from the domains of
5707 * the previous basic maps.
5709 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5710 __isl_take isl_map *map, __isl_take isl_set *dom,
5711 __isl_give isl_set **empty, int max)
5713 int i;
5714 struct isl_map *res;
5715 struct isl_set *todo;
5717 if (!map || !dom)
5718 goto error;
5720 if (isl_map_plain_is_empty(map)) {
5721 if (empty)
5722 *empty = dom;
5723 else
5724 isl_set_free(dom);
5725 return map;
5728 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5729 isl_set_copy(dom), &todo, max);
5731 for (i = 1; i < map->n; ++i) {
5732 isl_map *lt, *le;
5733 isl_map *res_i;
5734 isl_set *todo_i;
5735 isl_space *dim = isl_space_range(isl_map_get_space(res));
5737 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5738 isl_set_copy(dom), &todo_i, max);
5740 if (max) {
5741 lt = isl_map_lex_lt(isl_space_copy(dim));
5742 le = isl_map_lex_le(dim);
5743 } else {
5744 lt = isl_map_lex_gt(isl_space_copy(dim));
5745 le = isl_map_lex_ge(dim);
5747 lt = isl_map_apply_range(isl_map_copy(res), lt);
5748 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5749 le = isl_map_apply_range(isl_map_copy(res_i), le);
5750 le = isl_map_intersect(le, isl_map_copy(res));
5752 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5753 res = isl_map_intersect_domain(res,
5754 isl_set_copy(todo_i));
5755 res_i = isl_map_intersect_domain(res_i,
5756 isl_set_copy(todo));
5759 res = isl_map_union_disjoint(res, res_i);
5760 res = isl_map_union_disjoint(res, lt);
5761 res = isl_map_union_disjoint(res, le);
5763 todo = isl_set_intersect(todo, todo_i);
5766 isl_set_free(dom);
5767 isl_map_free(map);
5769 if (empty)
5770 *empty = todo;
5771 else
5772 isl_set_free(todo);
5774 return res;
5775 error:
5776 if (empty)
5777 *empty = NULL;
5778 isl_set_free(dom);
5779 isl_map_free(map);
5780 return NULL;
5783 /* Given a map "map", compute the lexicographically minimal
5784 * (or maximal) image element for each domain element in dom.
5785 * Set *empty to those elements in dom that do not have an image element.
5787 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5789 static __isl_give isl_map *isl_map_partial_lexopt(
5790 __isl_take isl_map *map, __isl_take isl_set *dom,
5791 __isl_give isl_set **empty, int max)
5793 if (!map || !dom)
5794 goto error;
5795 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5796 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5797 if (!isl_space_has_named_params(map->dim) ||
5798 !isl_space_has_named_params(dom->dim))
5799 isl_die(map->ctx, isl_error_invalid,
5800 "unaligned unnamed parameters", goto error);
5801 map = isl_map_align_params(map, isl_map_get_space(dom));
5802 dom = isl_map_align_params(dom, isl_map_get_space(map));
5803 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5804 error:
5805 if (empty)
5806 *empty = NULL;
5807 isl_set_free(dom);
5808 isl_map_free(map);
5809 return NULL;
5812 __isl_give isl_map *isl_map_partial_lexmax(
5813 __isl_take isl_map *map, __isl_take isl_set *dom,
5814 __isl_give isl_set **empty)
5816 return isl_map_partial_lexopt(map, dom, empty, 1);
5819 __isl_give isl_map *isl_map_partial_lexmin(
5820 __isl_take isl_map *map, __isl_take isl_set *dom,
5821 __isl_give isl_set **empty)
5823 return isl_map_partial_lexopt(map, dom, empty, 0);
5826 __isl_give isl_set *isl_set_partial_lexmin(
5827 __isl_take isl_set *set, __isl_take isl_set *dom,
5828 __isl_give isl_set **empty)
5830 return (struct isl_set *)
5831 isl_map_partial_lexmin((struct isl_map *)set,
5832 dom, empty);
5835 __isl_give isl_set *isl_set_partial_lexmax(
5836 __isl_take isl_set *set, __isl_take isl_set *dom,
5837 __isl_give isl_set **empty)
5839 return (struct isl_set *)
5840 isl_map_partial_lexmax((struct isl_map *)set,
5841 dom, empty);
5844 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5846 struct isl_basic_set *dom = NULL;
5847 isl_space *dom_dim;
5849 if (!bmap)
5850 goto error;
5851 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5852 dom = isl_basic_set_universe(dom_dim);
5853 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5854 error:
5855 isl_basic_map_free(bmap);
5856 return NULL;
5859 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5861 return isl_basic_map_lexopt(bmap, 0);
5864 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5866 return isl_basic_map_lexopt(bmap, 1);
5869 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5871 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5874 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5876 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5879 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5881 struct isl_set *dom = NULL;
5882 isl_space *dom_dim;
5884 if (!map)
5885 goto error;
5886 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5887 dom = isl_set_universe(dom_dim);
5888 return isl_map_partial_lexopt(map, dom, NULL, max);
5889 error:
5890 isl_map_free(map);
5891 return NULL;
5894 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5896 return isl_map_lexopt(map, 0);
5899 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5901 return isl_map_lexopt(map, 1);
5904 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5906 return (isl_set *)isl_map_lexmin((isl_map *)set);
5909 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5911 return (isl_set *)isl_map_lexmax((isl_map *)set);
5914 /* Extract the first and only affine expression from list
5915 * and then add it to *pwaff with the given dom.
5916 * This domain is known to be disjoint from other domains
5917 * because of the way isl_basic_map_foreach_lexmax works.
5919 static int update_dim_opt(__isl_take isl_basic_set *dom,
5920 __isl_take isl_aff_list *list, void *user)
5922 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5923 isl_aff *aff;
5924 isl_pw_aff **pwaff = user;
5925 isl_pw_aff *pwaff_i;
5927 if (isl_aff_list_n_aff(list) != 1)
5928 isl_die(ctx, isl_error_internal,
5929 "expecting single element list", goto error);
5931 aff = isl_aff_list_get_aff(list, 0);
5932 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5934 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5936 isl_aff_list_free(list);
5938 return 0;
5939 error:
5940 isl_basic_set_free(dom);
5941 isl_aff_list_free(list);
5942 return -1;
5945 /* Given a basic map with one output dimension, compute the minimum or
5946 * maximum of that dimension as an isl_pw_aff.
5948 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5949 * call update_dim_opt on each leaf of the result.
5951 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
5952 int max)
5954 isl_space *dim = isl_basic_map_get_space(bmap);
5955 isl_pw_aff *pwaff;
5956 int r;
5958 dim = isl_space_from_domain(isl_space_domain(dim));
5959 dim = isl_space_add_dims(dim, isl_dim_out, 1);
5960 pwaff = isl_pw_aff_empty(dim);
5962 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
5963 if (r < 0)
5964 return isl_pw_aff_free(pwaff);
5966 return pwaff;
5969 /* Compute the minimum or maximum of the given output dimension
5970 * as a function of the parameters and the input dimensions,
5971 * but independently of the other output dimensions.
5973 * We first project out the other output dimension and then compute
5974 * the "lexicographic" maximum in each basic map, combining the results
5975 * using isl_pw_aff_union_max.
5977 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
5978 int max)
5980 int i;
5981 isl_pw_aff *pwaff;
5982 unsigned n_out;
5984 n_out = isl_map_dim(map, isl_dim_out);
5985 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
5986 map = isl_map_project_out(map, isl_dim_out, 0, pos);
5987 if (!map)
5988 return NULL;
5990 if (map->n == 0) {
5991 isl_space *dim = isl_map_get_space(map);
5992 dim = isl_space_domain(isl_space_from_range(dim));
5993 isl_map_free(map);
5994 return isl_pw_aff_empty(dim);
5997 pwaff = basic_map_dim_opt(map->p[0], max);
5998 for (i = 1; i < map->n; ++i) {
5999 isl_pw_aff *pwaff_i;
6001 pwaff_i = basic_map_dim_opt(map->p[i], max);
6002 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6005 isl_map_free(map);
6007 return pwaff;
6010 /* Compute the maximum of the given output dimension as a function of the
6011 * parameters and input dimensions, but independently of
6012 * the other output dimensions.
6014 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6016 return map_dim_opt(map, pos, 1);
6019 /* Compute the minimum or maximum of the given set dimension
6020 * as a function of the parameters,
6021 * but independently of the other set dimensions.
6023 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6024 int max)
6026 return map_dim_opt(set, pos, max);
6029 /* Compute the maximum of the given set dimension as a function of the
6030 * parameters, but independently of the other set dimensions.
6032 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6034 return set_dim_opt(set, pos, 1);
6037 /* Compute the minimum of the given set dimension as a function of the
6038 * parameters, but independently of the other set dimensions.
6040 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6042 return set_dim_opt(set, pos, 0);
6045 /* Apply a preimage specified by "mat" on the parameters of "bset".
6046 * bset is assumed to have only parameters and divs.
6048 static struct isl_basic_set *basic_set_parameter_preimage(
6049 struct isl_basic_set *bset, struct isl_mat *mat)
6051 unsigned nparam;
6053 if (!bset || !mat)
6054 goto error;
6056 bset->dim = isl_space_cow(bset->dim);
6057 if (!bset->dim)
6058 goto error;
6060 nparam = isl_basic_set_dim(bset, isl_dim_param);
6062 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6064 bset->dim->nparam = 0;
6065 bset->dim->n_out = nparam;
6066 bset = isl_basic_set_preimage(bset, mat);
6067 if (bset) {
6068 bset->dim->nparam = bset->dim->n_out;
6069 bset->dim->n_out = 0;
6071 return bset;
6072 error:
6073 isl_mat_free(mat);
6074 isl_basic_set_free(bset);
6075 return NULL;
6078 /* Apply a preimage specified by "mat" on the parameters of "set".
6079 * set is assumed to have only parameters and divs.
6081 static struct isl_set *set_parameter_preimage(
6082 struct isl_set *set, struct isl_mat *mat)
6084 isl_space *dim = NULL;
6085 unsigned nparam;
6087 if (!set || !mat)
6088 goto error;
6090 dim = isl_space_copy(set->dim);
6091 dim = isl_space_cow(dim);
6092 if (!dim)
6093 goto error;
6095 nparam = isl_set_dim(set, isl_dim_param);
6097 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6099 dim->nparam = 0;
6100 dim->n_out = nparam;
6101 isl_set_reset_space(set, dim);
6102 set = isl_set_preimage(set, mat);
6103 if (!set)
6104 goto error2;
6105 dim = isl_space_copy(set->dim);
6106 dim = isl_space_cow(dim);
6107 if (!dim)
6108 goto error2;
6109 dim->nparam = dim->n_out;
6110 dim->n_out = 0;
6111 isl_set_reset_space(set, dim);
6112 return set;
6113 error:
6114 isl_space_free(dim);
6115 isl_mat_free(mat);
6116 error2:
6117 isl_set_free(set);
6118 return NULL;
6121 /* Intersect the basic set "bset" with the affine space specified by the
6122 * equalities in "eq".
6124 static struct isl_basic_set *basic_set_append_equalities(
6125 struct isl_basic_set *bset, struct isl_mat *eq)
6127 int i, k;
6128 unsigned len;
6130 if (!bset || !eq)
6131 goto error;
6133 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6134 eq->n_row, 0);
6135 if (!bset)
6136 goto error;
6138 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6139 for (i = 0; i < eq->n_row; ++i) {
6140 k = isl_basic_set_alloc_equality(bset);
6141 if (k < 0)
6142 goto error;
6143 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6144 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6146 isl_mat_free(eq);
6148 bset = isl_basic_set_gauss(bset, NULL);
6149 bset = isl_basic_set_finalize(bset);
6151 return bset;
6152 error:
6153 isl_mat_free(eq);
6154 isl_basic_set_free(bset);
6155 return NULL;
6158 /* Intersect the set "set" with the affine space specified by the
6159 * equalities in "eq".
6161 static struct isl_set *set_append_equalities(struct isl_set *set,
6162 struct isl_mat *eq)
6164 int i;
6166 if (!set || !eq)
6167 goto error;
6169 for (i = 0; i < set->n; ++i) {
6170 set->p[i] = basic_set_append_equalities(set->p[i],
6171 isl_mat_copy(eq));
6172 if (!set->p[i])
6173 goto error;
6175 isl_mat_free(eq);
6176 return set;
6177 error:
6178 isl_mat_free(eq);
6179 isl_set_free(set);
6180 return NULL;
6183 /* Project the given basic set onto its parameter domain, possibly introducing
6184 * new, explicit, existential variables in the constraints.
6185 * The input has parameters and (possibly implicit) existential variables.
6186 * The output has the same parameters, but only
6187 * explicit existentially quantified variables.
6189 * The actual projection is performed by pip, but pip doesn't seem
6190 * to like equalities very much, so we first remove the equalities
6191 * among the parameters by performing a variable compression on
6192 * the parameters. Afterward, an inverse transformation is performed
6193 * and the equalities among the parameters are inserted back in.
6195 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6197 int i, j;
6198 struct isl_mat *eq;
6199 struct isl_mat *T, *T2;
6200 struct isl_set *set;
6201 unsigned nparam, n_div;
6203 bset = isl_basic_set_cow(bset);
6204 if (!bset)
6205 return NULL;
6207 if (bset->n_eq == 0)
6208 return isl_basic_set_lexmin(bset);
6210 isl_basic_set_gauss(bset, NULL);
6212 nparam = isl_basic_set_dim(bset, isl_dim_param);
6213 n_div = isl_basic_set_dim(bset, isl_dim_div);
6215 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6216 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6217 ++i;
6219 if (i == bset->n_eq)
6220 return isl_basic_set_lexmin(bset);
6222 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6223 0, 1 + nparam);
6224 eq = isl_mat_cow(eq);
6225 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6226 if (T && T->n_col == 0) {
6227 isl_mat_free(T);
6228 isl_mat_free(T2);
6229 isl_mat_free(eq);
6230 bset = isl_basic_set_set_to_empty(bset);
6231 return isl_set_from_basic_set(bset);
6233 bset = basic_set_parameter_preimage(bset, T);
6235 set = isl_basic_set_lexmin(bset);
6236 set = set_parameter_preimage(set, T2);
6237 set = set_append_equalities(set, eq);
6238 return set;
6241 /* Compute an explicit representation for all the existentially
6242 * quantified variables.
6243 * The input and output dimensions are first turned into parameters.
6244 * compute_divs then returns a map with the same parameters and
6245 * no input or output dimensions and the dimension specification
6246 * is reset to that of the input.
6248 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6250 struct isl_basic_set *bset;
6251 struct isl_set *set;
6252 struct isl_map *map;
6253 isl_space *dim, *orig_dim = NULL;
6254 unsigned nparam;
6255 unsigned n_in;
6256 unsigned n_out;
6258 bmap = isl_basic_map_cow(bmap);
6259 if (!bmap)
6260 return NULL;
6262 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6263 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6264 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6265 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
6266 if (!dim)
6267 goto error;
6269 orig_dim = bmap->dim;
6270 bmap->dim = dim;
6271 bset = (struct isl_basic_set *)bmap;
6273 set = parameter_compute_divs(bset);
6274 map = (struct isl_map *)set;
6275 map = isl_map_reset_space(map, orig_dim);
6277 return map;
6278 error:
6279 isl_basic_map_free(bmap);
6280 return NULL;
6283 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6285 int i;
6286 unsigned off;
6288 if (!bmap)
6289 return -1;
6291 off = isl_space_dim(bmap->dim, isl_dim_all);
6292 for (i = 0; i < bmap->n_div; ++i) {
6293 if (isl_int_is_zero(bmap->div[i][0]))
6294 return 0;
6295 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6296 return -1);
6298 return 1;
6301 static int map_divs_known(__isl_keep isl_map *map)
6303 int i;
6305 if (!map)
6306 return -1;
6308 for (i = 0; i < map->n; ++i) {
6309 int known = isl_basic_map_divs_known(map->p[i]);
6310 if (known <= 0)
6311 return known;
6314 return 1;
6317 /* If bmap contains any unknown divs, then compute explicit
6318 * expressions for them. However, this computation may be
6319 * quite expensive, so first try to remove divs that aren't
6320 * strictly needed.
6322 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6324 int known;
6325 struct isl_map *map;
6327 known = isl_basic_map_divs_known(bmap);
6328 if (known < 0)
6329 goto error;
6330 if (known)
6331 return isl_map_from_basic_map(bmap);
6333 bmap = isl_basic_map_drop_redundant_divs(bmap);
6335 known = isl_basic_map_divs_known(bmap);
6336 if (known < 0)
6337 goto error;
6338 if (known)
6339 return isl_map_from_basic_map(bmap);
6341 map = compute_divs(bmap);
6342 return map;
6343 error:
6344 isl_basic_map_free(bmap);
6345 return NULL;
6348 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6350 int i;
6351 int known;
6352 struct isl_map *res;
6354 if (!map)
6355 return NULL;
6356 if (map->n == 0)
6357 return map;
6359 known = map_divs_known(map);
6360 if (known < 0) {
6361 isl_map_free(map);
6362 return NULL;
6364 if (known)
6365 return map;
6367 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6368 for (i = 1 ; i < map->n; ++i) {
6369 struct isl_map *r2;
6370 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6371 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6372 res = isl_map_union_disjoint(res, r2);
6373 else
6374 res = isl_map_union(res, r2);
6376 isl_map_free(map);
6378 return res;
6381 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6383 return (struct isl_set *)
6384 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6387 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6389 return (struct isl_set *)
6390 isl_map_compute_divs((struct isl_map *)set);
6393 struct isl_set *isl_map_domain(struct isl_map *map)
6395 int i;
6396 struct isl_set *set;
6398 if (!map)
6399 goto error;
6401 map = isl_map_cow(map);
6402 if (!map)
6403 return NULL;
6405 set = (struct isl_set *)map;
6406 set->dim = isl_space_domain(set->dim);
6407 if (!set->dim)
6408 goto error;
6409 for (i = 0; i < map->n; ++i) {
6410 set->p[i] = isl_basic_map_domain(map->p[i]);
6411 if (!set->p[i])
6412 goto error;
6414 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6415 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6416 return set;
6417 error:
6418 isl_map_free(map);
6419 return NULL;
6422 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6423 __isl_take isl_map *map2)
6425 int i;
6426 unsigned flags = 0;
6427 struct isl_map *map = NULL;
6429 if (!map1 || !map2)
6430 goto error;
6432 if (map1->n == 0) {
6433 isl_map_free(map1);
6434 return map2;
6436 if (map2->n == 0) {
6437 isl_map_free(map2);
6438 return map1;
6441 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6443 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6444 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6445 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6447 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6448 map1->n + map2->n, flags);
6449 if (!map)
6450 goto error;
6451 for (i = 0; i < map1->n; ++i) {
6452 map = isl_map_add_basic_map(map,
6453 isl_basic_map_copy(map1->p[i]));
6454 if (!map)
6455 goto error;
6457 for (i = 0; i < map2->n; ++i) {
6458 map = isl_map_add_basic_map(map,
6459 isl_basic_map_copy(map2->p[i]));
6460 if (!map)
6461 goto error;
6463 isl_map_free(map1);
6464 isl_map_free(map2);
6465 return map;
6466 error:
6467 isl_map_free(map);
6468 isl_map_free(map1);
6469 isl_map_free(map2);
6470 return NULL;
6473 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6474 __isl_take isl_map *map2)
6476 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6479 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6481 map1 = isl_map_union_disjoint(map1, map2);
6482 if (!map1)
6483 return NULL;
6484 if (map1->n > 1)
6485 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6486 return map1;
6489 struct isl_set *isl_set_union_disjoint(
6490 struct isl_set *set1, struct isl_set *set2)
6492 return (struct isl_set *)
6493 isl_map_union_disjoint(
6494 (struct isl_map *)set1, (struct isl_map *)set2);
6497 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6499 return (struct isl_set *)
6500 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6503 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6504 __isl_take isl_set *set)
6506 unsigned flags = 0;
6507 struct isl_map *result;
6508 int i, j;
6510 if (!map || !set)
6511 goto error;
6513 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6514 isl_die(set->ctx, isl_error_invalid,
6515 "parameters don't match", goto error);
6517 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6518 !isl_map_compatible_range(map, set))
6519 isl_die(set->ctx, isl_error_invalid,
6520 "incompatible spaces", goto error);
6522 if (isl_set_plain_is_universe(set)) {
6523 isl_set_free(set);
6524 return map;
6527 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6528 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6529 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6531 result = isl_map_alloc_space(isl_space_copy(map->dim),
6532 map->n * set->n, flags);
6533 if (!result)
6534 goto error;
6535 for (i = 0; i < map->n; ++i)
6536 for (j = 0; j < set->n; ++j) {
6537 result = isl_map_add_basic_map(result,
6538 isl_basic_map_intersect_range(
6539 isl_basic_map_copy(map->p[i]),
6540 isl_basic_set_copy(set->p[j])));
6541 if (!result)
6542 goto error;
6544 isl_map_free(map);
6545 isl_set_free(set);
6546 return result;
6547 error:
6548 isl_map_free(map);
6549 isl_set_free(set);
6550 return NULL;
6553 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6554 __isl_take isl_set *set)
6556 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6559 struct isl_map *isl_map_intersect_domain(
6560 struct isl_map *map, struct isl_set *set)
6562 return isl_map_reverse(
6563 isl_map_intersect_range(isl_map_reverse(map), set));
6566 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6567 __isl_take isl_map *map2)
6569 if (!map1 || !map2)
6570 goto error;
6571 map1 = isl_map_reverse(map1);
6572 map1 = isl_map_apply_range(map1, map2);
6573 return isl_map_reverse(map1);
6574 error:
6575 isl_map_free(map1);
6576 isl_map_free(map2);
6577 return NULL;
6580 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6581 __isl_take isl_map *map2)
6583 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6586 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6587 __isl_take isl_map *map2)
6589 isl_space *dim_result;
6590 struct isl_map *result;
6591 int i, j;
6593 if (!map1 || !map2)
6594 goto error;
6596 dim_result = isl_space_join(isl_space_copy(map1->dim),
6597 isl_space_copy(map2->dim));
6599 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6600 if (!result)
6601 goto error;
6602 for (i = 0; i < map1->n; ++i)
6603 for (j = 0; j < map2->n; ++j) {
6604 result = isl_map_add_basic_map(result,
6605 isl_basic_map_apply_range(
6606 isl_basic_map_copy(map1->p[i]),
6607 isl_basic_map_copy(map2->p[j])));
6608 if (!result)
6609 goto error;
6611 isl_map_free(map1);
6612 isl_map_free(map2);
6613 if (result && result->n <= 1)
6614 ISL_F_SET(result, ISL_MAP_DISJOINT);
6615 return result;
6616 error:
6617 isl_map_free(map1);
6618 isl_map_free(map2);
6619 return NULL;
6622 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6623 __isl_take isl_map *map2)
6625 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6629 * returns range - domain
6631 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6633 isl_space *dims, *target_dim;
6634 struct isl_basic_set *bset;
6635 unsigned dim;
6636 unsigned nparam;
6637 int i;
6639 if (!bmap)
6640 goto error;
6641 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6642 bmap->dim, isl_dim_out),
6643 goto error);
6644 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6645 dim = isl_basic_map_n_in(bmap);
6646 nparam = isl_basic_map_n_param(bmap);
6647 bset = isl_basic_set_from_basic_map(bmap);
6648 bset = isl_basic_set_cow(bset);
6649 dims = isl_basic_set_get_space(bset);
6650 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6651 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6652 bset = isl_basic_set_swap_vars(bset, 2*dim);
6653 for (i = 0; i < dim; ++i) {
6654 int j = isl_basic_map_alloc_equality(
6655 (struct isl_basic_map *)bset);
6656 if (j < 0)
6657 goto error;
6658 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6659 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6660 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6661 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6663 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6664 bset = isl_basic_set_reset_space(bset, target_dim);
6665 return bset;
6666 error:
6667 isl_basic_map_free(bmap);
6668 return NULL;
6672 * returns range - domain
6674 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
6676 int i;
6677 isl_space *dim;
6678 struct isl_set *result;
6680 if (!map)
6681 return NULL;
6683 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6684 map->dim, isl_dim_out),
6685 goto error);
6686 dim = isl_map_get_space(map);
6687 dim = isl_space_domain(dim);
6688 result = isl_set_alloc_space(dim, map->n, 0);
6689 if (!result)
6690 goto error;
6691 for (i = 0; i < map->n; ++i)
6692 result = isl_set_add_basic_set(result,
6693 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6694 isl_map_free(map);
6695 return result;
6696 error:
6697 isl_map_free(map);
6698 return NULL;
6702 * returns [domain -> range] -> range - domain
6704 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6705 __isl_take isl_basic_map *bmap)
6707 int i, k;
6708 isl_space *dim;
6709 isl_basic_map *domain;
6710 int nparam, n;
6711 unsigned total;
6713 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6714 isl_die(bmap->ctx, isl_error_invalid,
6715 "domain and range don't match", goto error);
6717 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6718 n = isl_basic_map_dim(bmap, isl_dim_in);
6720 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6721 domain = isl_basic_map_universe(dim);
6723 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6724 bmap = isl_basic_map_apply_range(bmap, domain);
6725 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6727 total = isl_basic_map_total_dim(bmap);
6729 for (i = 0; i < n; ++i) {
6730 k = isl_basic_map_alloc_equality(bmap);
6731 if (k < 0)
6732 goto error;
6733 isl_seq_clr(bmap->eq[k], 1 + total);
6734 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6735 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6736 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6739 bmap = isl_basic_map_gauss(bmap, NULL);
6740 return isl_basic_map_finalize(bmap);
6741 error:
6742 isl_basic_map_free(bmap);
6743 return NULL;
6747 * returns [domain -> range] -> range - domain
6749 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6751 int i;
6752 isl_space *domain_dim;
6754 if (!map)
6755 return NULL;
6757 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6758 isl_die(map->ctx, isl_error_invalid,
6759 "domain and range don't match", goto error);
6761 map = isl_map_cow(map);
6762 if (!map)
6763 return NULL;
6765 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6766 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6767 map->dim = isl_space_join(map->dim, domain_dim);
6768 if (!map->dim)
6769 goto error;
6770 for (i = 0; i < map->n; ++i) {
6771 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6772 if (!map->p[i])
6773 goto error;
6775 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6776 return map;
6777 error:
6778 isl_map_free(map);
6779 return NULL;
6782 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6784 struct isl_basic_map *bmap;
6785 unsigned nparam;
6786 unsigned dim;
6787 int i;
6789 if (!dims)
6790 return NULL;
6792 nparam = dims->nparam;
6793 dim = dims->n_out;
6794 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6795 if (!bmap)
6796 goto error;
6798 for (i = 0; i < dim; ++i) {
6799 int j = isl_basic_map_alloc_equality(bmap);
6800 if (j < 0)
6801 goto error;
6802 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6803 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6804 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6806 return isl_basic_map_finalize(bmap);
6807 error:
6808 isl_basic_map_free(bmap);
6809 return NULL;
6812 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6814 if (!dim)
6815 return NULL;
6816 if (dim->n_in != dim->n_out)
6817 isl_die(dim->ctx, isl_error_invalid,
6818 "number of input and output dimensions needs to be "
6819 "the same", goto error);
6820 return basic_map_identity(dim);
6821 error:
6822 isl_space_free(dim);
6823 return NULL;
6826 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6828 if (!model || !model->dim)
6829 return NULL;
6830 return isl_basic_map_identity(isl_space_copy(model->dim));
6833 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6835 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6838 struct isl_map *isl_map_identity_like(struct isl_map *model)
6840 if (!model || !model->dim)
6841 return NULL;
6842 return isl_map_identity(isl_space_copy(model->dim));
6845 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6847 if (!model || !model->dim)
6848 return NULL;
6849 return isl_map_identity(isl_space_copy(model->dim));
6852 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6854 isl_space *dim = isl_set_get_space(set);
6855 isl_map *id;
6856 id = isl_map_identity(isl_space_map_from_set(dim));
6857 return isl_map_intersect_range(id, set);
6860 /* Construct a basic set with all set dimensions having only non-negative
6861 * values.
6863 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6864 __isl_take isl_space *space)
6866 int i;
6867 unsigned nparam;
6868 unsigned dim;
6869 struct isl_basic_set *bset;
6871 if (!space)
6872 return NULL;
6873 nparam = space->nparam;
6874 dim = space->n_out;
6875 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6876 if (!bset)
6877 return NULL;
6878 for (i = 0; i < dim; ++i) {
6879 int k = isl_basic_set_alloc_inequality(bset);
6880 if (k < 0)
6881 goto error;
6882 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6883 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6885 return bset;
6886 error:
6887 isl_basic_set_free(bset);
6888 return NULL;
6891 /* Construct the half-space x_pos >= 0.
6893 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6894 int pos)
6896 int k;
6897 isl_basic_set *nonneg;
6899 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6900 k = isl_basic_set_alloc_inequality(nonneg);
6901 if (k < 0)
6902 goto error;
6903 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6904 isl_int_set_si(nonneg->ineq[k][pos], 1);
6906 return isl_basic_set_finalize(nonneg);
6907 error:
6908 isl_basic_set_free(nonneg);
6909 return NULL;
6912 /* Construct the half-space x_pos <= -1.
6914 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6916 int k;
6917 isl_basic_set *neg;
6919 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6920 k = isl_basic_set_alloc_inequality(neg);
6921 if (k < 0)
6922 goto error;
6923 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6924 isl_int_set_si(neg->ineq[k][0], -1);
6925 isl_int_set_si(neg->ineq[k][pos], -1);
6927 return isl_basic_set_finalize(neg);
6928 error:
6929 isl_basic_set_free(neg);
6930 return NULL;
6933 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6934 enum isl_dim_type type, unsigned first, unsigned n)
6936 int i;
6937 isl_basic_set *nonneg;
6938 isl_basic_set *neg;
6940 if (!set)
6941 return NULL;
6942 if (n == 0)
6943 return set;
6945 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6947 for (i = 0; i < n; ++i) {
6948 nonneg = nonneg_halfspace(isl_set_get_space(set),
6949 pos(set->dim, type) + first + i);
6950 neg = neg_halfspace(isl_set_get_space(set),
6951 pos(set->dim, type) + first + i);
6953 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6956 return set;
6957 error:
6958 isl_set_free(set);
6959 return NULL;
6962 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6963 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6964 void *user)
6966 isl_set *half;
6968 if (!set)
6969 return -1;
6970 if (isl_set_plain_is_empty(set)) {
6971 isl_set_free(set);
6972 return 0;
6974 if (first == len)
6975 return fn(set, signs, user);
6977 signs[first] = 1;
6978 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6979 1 + first));
6980 half = isl_set_intersect(half, isl_set_copy(set));
6981 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6982 goto error;
6984 signs[first] = -1;
6985 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6986 1 + first));
6987 half = isl_set_intersect(half, set);
6988 return foreach_orthant(half, signs, first + 1, len, fn, user);
6989 error:
6990 isl_set_free(set);
6991 return -1;
6994 /* Call "fn" on the intersections of "set" with each of the orthants
6995 * (except for obviously empty intersections). The orthant is identified
6996 * by the signs array, with each entry having value 1 or -1 according
6997 * to the sign of the corresponding variable.
6999 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7000 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7001 void *user)
7003 unsigned nparam;
7004 unsigned nvar;
7005 int *signs;
7006 int r;
7008 if (!set)
7009 return -1;
7010 if (isl_set_plain_is_empty(set))
7011 return 0;
7013 nparam = isl_set_dim(set, isl_dim_param);
7014 nvar = isl_set_dim(set, isl_dim_set);
7016 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7018 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7019 fn, user);
7021 free(signs);
7023 return r;
7026 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7028 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7031 int isl_basic_map_is_subset(
7032 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7034 int is_subset;
7035 struct isl_map *map1;
7036 struct isl_map *map2;
7038 if (!bmap1 || !bmap2)
7039 return -1;
7041 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7042 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7044 is_subset = isl_map_is_subset(map1, map2);
7046 isl_map_free(map1);
7047 isl_map_free(map2);
7049 return is_subset;
7052 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7053 __isl_keep isl_basic_set *bset2)
7055 return isl_basic_map_is_subset(bset1, bset2);
7058 int isl_basic_map_is_equal(
7059 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7061 int is_subset;
7063 if (!bmap1 || !bmap2)
7064 return -1;
7065 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7066 if (is_subset != 1)
7067 return is_subset;
7068 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7069 return is_subset;
7072 int isl_basic_set_is_equal(
7073 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7075 return isl_basic_map_is_equal(
7076 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7079 int isl_map_is_empty(struct isl_map *map)
7081 int i;
7082 int is_empty;
7084 if (!map)
7085 return -1;
7086 for (i = 0; i < map->n; ++i) {
7087 is_empty = isl_basic_map_is_empty(map->p[i]);
7088 if (is_empty < 0)
7089 return -1;
7090 if (!is_empty)
7091 return 0;
7093 return 1;
7096 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7098 return map ? map->n == 0 : -1;
7101 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7103 return isl_map_plain_is_empty(map);
7106 int isl_set_plain_is_empty(struct isl_set *set)
7108 return set ? set->n == 0 : -1;
7111 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7113 return isl_set_plain_is_empty(set);
7116 int isl_set_is_empty(struct isl_set *set)
7118 return isl_map_is_empty((struct isl_map *)set);
7121 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7123 if (!map1 || !map2)
7124 return -1;
7126 return isl_space_is_equal(map1->dim, map2->dim);
7129 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7131 if (!set1 || !set2)
7132 return -1;
7134 return isl_space_is_equal(set1->dim, set2->dim);
7137 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7139 int is_subset;
7141 if (!map1 || !map2)
7142 return -1;
7143 is_subset = isl_map_is_subset(map1, map2);
7144 if (is_subset != 1)
7145 return is_subset;
7146 is_subset = isl_map_is_subset(map2, map1);
7147 return is_subset;
7150 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7152 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7155 int isl_basic_map_is_strict_subset(
7156 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7158 int is_subset;
7160 if (!bmap1 || !bmap2)
7161 return -1;
7162 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7163 if (is_subset != 1)
7164 return is_subset;
7165 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7166 if (is_subset == -1)
7167 return is_subset;
7168 return !is_subset;
7171 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7173 int is_subset;
7175 if (!map1 || !map2)
7176 return -1;
7177 is_subset = isl_map_is_subset(map1, map2);
7178 if (is_subset != 1)
7179 return is_subset;
7180 is_subset = isl_map_is_subset(map2, map1);
7181 if (is_subset == -1)
7182 return is_subset;
7183 return !is_subset;
7186 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7188 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7191 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7193 if (!bmap)
7194 return -1;
7195 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7198 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7200 if (!bset)
7201 return -1;
7202 return bset->n_eq == 0 && bset->n_ineq == 0;
7205 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7207 int i;
7209 if (!map)
7210 return -1;
7212 for (i = 0; i < map->n; ++i) {
7213 int r = isl_basic_map_is_universe(map->p[i]);
7214 if (r < 0 || r)
7215 return r;
7218 return 0;
7221 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7223 return isl_map_plain_is_universe((isl_map *) set);
7226 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7228 return isl_set_plain_is_universe(set);
7231 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7233 struct isl_basic_set *bset = NULL;
7234 struct isl_vec *sample = NULL;
7235 int empty;
7236 unsigned total;
7238 if (!bmap)
7239 return -1;
7241 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7242 return 1;
7244 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7245 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7246 copy = isl_basic_map_remove_redundancies(copy);
7247 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
7248 isl_basic_map_free(copy);
7249 return empty;
7252 total = 1 + isl_basic_map_total_dim(bmap);
7253 if (bmap->sample && bmap->sample->size == total) {
7254 int contains = isl_basic_map_contains(bmap, bmap->sample);
7255 if (contains < 0)
7256 return -1;
7257 if (contains)
7258 return 0;
7260 isl_vec_free(bmap->sample);
7261 bmap->sample = NULL;
7262 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7263 if (!bset)
7264 return -1;
7265 sample = isl_basic_set_sample_vec(bset);
7266 if (!sample)
7267 return -1;
7268 empty = sample->size == 0;
7269 isl_vec_free(bmap->sample);
7270 bmap->sample = sample;
7271 if (empty)
7272 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7274 return empty;
7277 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7279 if (!bmap)
7280 return -1;
7281 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7284 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7286 return isl_basic_map_plain_is_empty(bmap);
7289 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7291 if (!bset)
7292 return -1;
7293 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7296 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7298 return isl_basic_set_plain_is_empty(bset);
7301 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7303 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7306 struct isl_map *isl_basic_map_union(
7307 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7309 struct isl_map *map;
7310 if (!bmap1 || !bmap2)
7311 goto error;
7313 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7315 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7316 if (!map)
7317 goto error;
7318 map = isl_map_add_basic_map(map, bmap1);
7319 map = isl_map_add_basic_map(map, bmap2);
7320 return map;
7321 error:
7322 isl_basic_map_free(bmap1);
7323 isl_basic_map_free(bmap2);
7324 return NULL;
7327 struct isl_set *isl_basic_set_union(
7328 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7330 return (struct isl_set *)isl_basic_map_union(
7331 (struct isl_basic_map *)bset1,
7332 (struct isl_basic_map *)bset2);
7335 /* Order divs such that any div only depends on previous divs */
7336 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7338 int i;
7339 unsigned off;
7341 if (!bmap)
7342 return NULL;
7344 off = isl_space_dim(bmap->dim, isl_dim_all);
7346 for (i = 0; i < bmap->n_div; ++i) {
7347 int pos;
7348 if (isl_int_is_zero(bmap->div[i][0]))
7349 continue;
7350 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7351 bmap->n_div-i);
7352 if (pos == -1)
7353 continue;
7354 isl_basic_map_swap_div(bmap, i, i + pos);
7355 --i;
7357 return bmap;
7360 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7362 return (struct isl_basic_set *)
7363 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7366 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7368 int i;
7370 if (!map)
7371 return 0;
7373 for (i = 0; i < map->n; ++i) {
7374 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7375 if (!map->p[i])
7376 goto error;
7379 return map;
7380 error:
7381 isl_map_free(map);
7382 return NULL;
7385 /* Apply the expansion computed by isl_merge_divs.
7386 * The expansion itself is given by "exp" while the resulting
7387 * list of divs is given by "div".
7389 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7390 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7392 int i, j;
7393 int n_div;
7395 bset = isl_basic_set_cow(bset);
7396 if (!bset || !div)
7397 goto error;
7399 if (div->n_row < bset->n_div)
7400 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7401 "not an expansion", goto error);
7403 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7404 div->n_row - bset->n_div, 0,
7405 2 * (div->n_row - bset->n_div));
7407 n_div = bset->n_div;
7408 for (i = n_div; i < div->n_row; ++i)
7409 if (isl_basic_set_alloc_div(bset) < 0)
7410 goto error;
7412 j = n_div - 1;
7413 for (i = div->n_row - 1; i >= 0; --i) {
7414 if (j >= 0 && exp[j] == i) {
7415 if (i != j)
7416 isl_basic_map_swap_div(bset, i, j);
7417 j--;
7418 } else {
7419 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7420 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7421 goto error;
7425 isl_mat_free(div);
7426 return bset;
7427 error:
7428 isl_basic_set_free(bset);
7429 isl_mat_free(div);
7430 return NULL;
7433 /* Look for a div in dst that corresponds to the div "div" in src.
7434 * The divs before "div" in src and dst are assumed to be the same.
7436 * Returns -1 if no corresponding div was found and the position
7437 * of the corresponding div in dst otherwise.
7439 static int find_div(struct isl_basic_map *dst,
7440 struct isl_basic_map *src, unsigned div)
7442 int i;
7444 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7446 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7447 for (i = div; i < dst->n_div; ++i)
7448 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7449 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7450 dst->n_div - div) == -1)
7451 return i;
7452 return -1;
7455 struct isl_basic_map *isl_basic_map_align_divs(
7456 struct isl_basic_map *dst, struct isl_basic_map *src)
7458 int i;
7459 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7461 if (!dst || !src)
7462 goto error;
7464 if (src->n_div == 0)
7465 return dst;
7467 for (i = 0; i < src->n_div; ++i)
7468 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7470 src = isl_basic_map_order_divs(src);
7471 dst = isl_basic_map_cow(dst);
7472 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7473 src->n_div, 0, 2 * src->n_div);
7474 if (!dst)
7475 return NULL;
7476 for (i = 0; i < src->n_div; ++i) {
7477 int j = find_div(dst, src, i);
7478 if (j < 0) {
7479 j = isl_basic_map_alloc_div(dst);
7480 if (j < 0)
7481 goto error;
7482 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7483 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7484 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7485 goto error;
7487 if (j != i)
7488 isl_basic_map_swap_div(dst, i, j);
7490 return dst;
7491 error:
7492 isl_basic_map_free(dst);
7493 return NULL;
7496 struct isl_basic_set *isl_basic_set_align_divs(
7497 struct isl_basic_set *dst, struct isl_basic_set *src)
7499 return (struct isl_basic_set *)isl_basic_map_align_divs(
7500 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7503 struct isl_map *isl_map_align_divs(struct isl_map *map)
7505 int i;
7507 if (!map)
7508 return NULL;
7509 if (map->n == 0)
7510 return map;
7511 map = isl_map_compute_divs(map);
7512 map = isl_map_cow(map);
7513 if (!map)
7514 return NULL;
7516 for (i = 1; i < map->n; ++i)
7517 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7518 for (i = 1; i < map->n; ++i)
7519 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7521 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7522 return map;
7525 struct isl_set *isl_set_align_divs(struct isl_set *set)
7527 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7530 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7531 __isl_take isl_map *map)
7533 if (!set || !map)
7534 goto error;
7535 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7536 map = isl_map_intersect_domain(map, set);
7537 set = isl_map_range(map);
7538 return set;
7539 error:
7540 isl_set_free(set);
7541 isl_map_free(map);
7542 return NULL;
7545 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7546 __isl_take isl_map *map)
7548 return isl_map_align_params_map_map_and(set, map, &set_apply);
7551 /* There is no need to cow as removing empty parts doesn't change
7552 * the meaning of the set.
7554 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7556 int i;
7558 if (!map)
7559 return NULL;
7561 for (i = map->n - 1; i >= 0; --i)
7562 remove_if_empty(map, i);
7564 return map;
7567 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7569 return (struct isl_set *)
7570 isl_map_remove_empty_parts((struct isl_map *)set);
7573 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7575 struct isl_basic_map *bmap;
7576 if (!map || map->n == 0)
7577 return NULL;
7578 bmap = map->p[map->n-1];
7579 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7580 return isl_basic_map_copy(bmap);
7583 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7585 return (struct isl_basic_set *)
7586 isl_map_copy_basic_map((struct isl_map *)set);
7589 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7590 __isl_keep isl_basic_map *bmap)
7592 int i;
7594 if (!map || !bmap)
7595 goto error;
7596 for (i = map->n-1; i >= 0; --i) {
7597 if (map->p[i] != bmap)
7598 continue;
7599 map = isl_map_cow(map);
7600 if (!map)
7601 goto error;
7602 isl_basic_map_free(map->p[i]);
7603 if (i != map->n-1) {
7604 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7605 map->p[i] = map->p[map->n-1];
7607 map->n--;
7608 return map;
7610 return map;
7611 error:
7612 isl_map_free(map);
7613 return NULL;
7616 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7617 struct isl_basic_set *bset)
7619 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7620 (struct isl_basic_map *)bset);
7623 /* Given two basic sets bset1 and bset2, compute the maximal difference
7624 * between the values of dimension pos in bset1 and those in bset2
7625 * for any common value of the parameters and dimensions preceding pos.
7627 static enum isl_lp_result basic_set_maximal_difference_at(
7628 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7629 int pos, isl_int *opt)
7631 isl_space *dims;
7632 struct isl_basic_map *bmap1 = NULL;
7633 struct isl_basic_map *bmap2 = NULL;
7634 struct isl_ctx *ctx;
7635 struct isl_vec *obj;
7636 unsigned total;
7637 unsigned nparam;
7638 unsigned dim1, dim2;
7639 enum isl_lp_result res;
7641 if (!bset1 || !bset2)
7642 return isl_lp_error;
7644 nparam = isl_basic_set_n_param(bset1);
7645 dim1 = isl_basic_set_n_dim(bset1);
7646 dim2 = isl_basic_set_n_dim(bset2);
7647 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7648 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7649 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7650 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7651 if (!bmap1 || !bmap2)
7652 goto error;
7653 bmap1 = isl_basic_map_cow(bmap1);
7654 bmap1 = isl_basic_map_extend(bmap1, nparam,
7655 pos, (dim1 - pos) + (dim2 - pos),
7656 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7657 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7658 if (!bmap1)
7659 goto error;
7660 total = isl_basic_map_total_dim(bmap1);
7661 ctx = bmap1->ctx;
7662 obj = isl_vec_alloc(ctx, 1 + total);
7663 isl_seq_clr(obj->block.data, 1 + total);
7664 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7665 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7666 if (!obj)
7667 goto error;
7668 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7669 opt, NULL, NULL);
7670 isl_basic_map_free(bmap1);
7671 isl_vec_free(obj);
7672 return res;
7673 error:
7674 isl_basic_map_free(bmap1);
7675 isl_basic_map_free(bmap2);
7676 return isl_lp_error;
7679 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7680 * for any common value of the parameters and dimensions preceding pos
7681 * in both basic sets, the values of dimension pos in bset1 are
7682 * smaller or larger than those in bset2.
7684 * Returns
7685 * 1 if bset1 follows bset2
7686 * -1 if bset1 precedes bset2
7687 * 0 if bset1 and bset2 are incomparable
7688 * -2 if some error occurred.
7690 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7691 struct isl_basic_set *bset2, int pos)
7693 isl_int opt;
7694 enum isl_lp_result res;
7695 int cmp;
7697 isl_int_init(opt);
7699 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7701 if (res == isl_lp_empty)
7702 cmp = 0;
7703 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7704 res == isl_lp_unbounded)
7705 cmp = 1;
7706 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7707 cmp = -1;
7708 else
7709 cmp = -2;
7711 isl_int_clear(opt);
7712 return cmp;
7715 /* Given two basic sets bset1 and bset2, check whether
7716 * for any common value of the parameters and dimensions preceding pos
7717 * there is a value of dimension pos in bset1 that is larger
7718 * than a value of the same dimension in bset2.
7720 * Return
7721 * 1 if there exists such a pair
7722 * 0 if there is no such pair, but there is a pair of equal values
7723 * -1 otherwise
7724 * -2 if some error occurred.
7726 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7727 __isl_keep isl_basic_set *bset2, int pos)
7729 isl_int opt;
7730 enum isl_lp_result res;
7731 int cmp;
7733 isl_int_init(opt);
7735 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7737 if (res == isl_lp_empty)
7738 cmp = -1;
7739 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7740 res == isl_lp_unbounded)
7741 cmp = 1;
7742 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7743 cmp = -1;
7744 else if (res == isl_lp_ok)
7745 cmp = 0;
7746 else
7747 cmp = -2;
7749 isl_int_clear(opt);
7750 return cmp;
7753 /* Given two sets set1 and set2, check whether
7754 * for any common value of the parameters and dimensions preceding pos
7755 * there is a value of dimension pos in set1 that is larger
7756 * than a value of the same dimension in set2.
7758 * Return
7759 * 1 if there exists such a pair
7760 * 0 if there is no such pair, but there is a pair of equal values
7761 * -1 otherwise
7762 * -2 if some error occurred.
7764 int isl_set_follows_at(__isl_keep isl_set *set1,
7765 __isl_keep isl_set *set2, int pos)
7767 int i, j;
7768 int follows = -1;
7770 if (!set1 || !set2)
7771 return -2;
7773 for (i = 0; i < set1->n; ++i)
7774 for (j = 0; j < set2->n; ++j) {
7775 int f;
7776 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7777 if (f == 1 || f == -2)
7778 return f;
7779 if (f > follows)
7780 follows = f;
7783 return follows;
7786 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7787 unsigned pos, isl_int *val)
7789 int i;
7790 int d;
7791 unsigned total;
7793 if (!bmap)
7794 return -1;
7795 total = isl_basic_map_total_dim(bmap);
7796 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7797 for (; d+1 > pos; --d)
7798 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7799 break;
7800 if (d != pos)
7801 continue;
7802 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7803 return 0;
7804 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7805 return 0;
7806 if (!isl_int_is_one(bmap->eq[i][1+d]))
7807 return 0;
7808 if (val)
7809 isl_int_neg(*val, bmap->eq[i][0]);
7810 return 1;
7812 return 0;
7815 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7816 unsigned pos, isl_int *val)
7818 int i;
7819 isl_int v;
7820 isl_int tmp;
7821 int fixed;
7823 if (!map)
7824 return -1;
7825 if (map->n == 0)
7826 return 0;
7827 if (map->n == 1)
7828 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7829 isl_int_init(v);
7830 isl_int_init(tmp);
7831 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7832 for (i = 1; fixed == 1 && i < map->n; ++i) {
7833 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7834 if (fixed == 1 && isl_int_ne(tmp, v))
7835 fixed = 0;
7837 if (val)
7838 isl_int_set(*val, v);
7839 isl_int_clear(tmp);
7840 isl_int_clear(v);
7841 return fixed;
7844 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7845 unsigned pos, isl_int *val)
7847 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7848 pos, val);
7851 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7852 isl_int *val)
7854 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7857 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7858 enum isl_dim_type type, unsigned pos, isl_int *val)
7860 if (pos >= isl_basic_map_dim(bmap, type))
7861 return -1;
7862 return isl_basic_map_plain_has_fixed_var(bmap,
7863 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7866 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7867 enum isl_dim_type type, unsigned pos, isl_int *val)
7869 if (pos >= isl_map_dim(map, type))
7870 return -1;
7871 return isl_map_plain_has_fixed_var(map,
7872 map_offset(map, type) - 1 + pos, val);
7875 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
7876 enum isl_dim_type type, unsigned pos, isl_int *val)
7878 return isl_map_plain_is_fixed(set, type, pos, val);
7881 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7882 enum isl_dim_type type, unsigned pos, isl_int *val)
7884 return isl_map_plain_is_fixed(map, type, pos, val);
7887 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7888 * then return this fixed value in *val.
7890 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7891 unsigned dim, isl_int *val)
7893 return isl_basic_set_plain_has_fixed_var(bset,
7894 isl_basic_set_n_param(bset) + dim, val);
7897 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7898 * then return this fixed value in *val.
7900 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7901 unsigned dim, isl_int *val)
7903 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7906 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7907 unsigned dim, isl_int *val)
7909 return isl_set_plain_dim_is_fixed(set, dim, val);
7912 /* Check if input variable in has fixed value and if so and if val is not NULL,
7913 * then return this fixed value in *val.
7915 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7916 unsigned in, isl_int *val)
7918 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7921 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7922 * and if val is not NULL, then return this lower bound in *val.
7924 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7925 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7927 int i, i_eq = -1, i_ineq = -1;
7928 isl_int *c;
7929 unsigned total;
7930 unsigned nparam;
7932 if (!bset)
7933 return -1;
7934 total = isl_basic_set_total_dim(bset);
7935 nparam = isl_basic_set_n_param(bset);
7936 for (i = 0; i < bset->n_eq; ++i) {
7937 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7938 continue;
7939 if (i_eq != -1)
7940 return 0;
7941 i_eq = i;
7943 for (i = 0; i < bset->n_ineq; ++i) {
7944 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7945 continue;
7946 if (i_eq != -1 || i_ineq != -1)
7947 return 0;
7948 i_ineq = i;
7950 if (i_eq == -1 && i_ineq == -1)
7951 return 0;
7952 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7953 /* The coefficient should always be one due to normalization. */
7954 if (!isl_int_is_one(c[1+nparam+dim]))
7955 return 0;
7956 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7957 return 0;
7958 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7959 total - nparam - dim - 1) != -1)
7960 return 0;
7961 if (val)
7962 isl_int_neg(*val, c[0]);
7963 return 1;
7966 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7967 unsigned dim, isl_int *val)
7969 int i;
7970 isl_int v;
7971 isl_int tmp;
7972 int fixed;
7974 if (!set)
7975 return -1;
7976 if (set->n == 0)
7977 return 0;
7978 if (set->n == 1)
7979 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7980 dim, val);
7981 isl_int_init(v);
7982 isl_int_init(tmp);
7983 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7984 dim, &v);
7985 for (i = 1; fixed == 1 && i < set->n; ++i) {
7986 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7987 dim, &tmp);
7988 if (fixed == 1 && isl_int_ne(tmp, v))
7989 fixed = 0;
7991 if (val)
7992 isl_int_set(*val, v);
7993 isl_int_clear(tmp);
7994 isl_int_clear(v);
7995 return fixed;
7998 struct constraint {
7999 unsigned size;
8000 isl_int *c;
8003 /* uset_gist depends on constraints without existentially quantified
8004 * variables sorting first.
8006 static int qsort_constraint_cmp(const void *p1, const void *p2)
8008 const struct constraint *c1 = (const struct constraint *)p1;
8009 const struct constraint *c2 = (const struct constraint *)p2;
8010 int l1, l2;
8011 unsigned size = isl_min(c1->size, c2->size);
8013 l1 = isl_seq_last_non_zero(c1->c, size);
8014 l2 = isl_seq_last_non_zero(c2->c, size);
8016 if (l1 != l2)
8017 return l1 - l2;
8019 return isl_seq_cmp(c1->c, c2->c, size);
8022 static struct isl_basic_map *isl_basic_map_sort_constraints(
8023 struct isl_basic_map *bmap)
8025 int i;
8026 struct constraint *c;
8027 unsigned total;
8029 if (!bmap)
8030 return NULL;
8031 total = isl_basic_map_total_dim(bmap);
8032 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8033 if (!c)
8034 goto error;
8035 for (i = 0; i < bmap->n_ineq; ++i) {
8036 c[i].size = total;
8037 c[i].c = bmap->ineq[i];
8039 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8040 for (i = 0; i < bmap->n_ineq; ++i)
8041 bmap->ineq[i] = c[i].c;
8042 free(c);
8043 return bmap;
8044 error:
8045 isl_basic_map_free(bmap);
8046 return NULL;
8049 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8050 __isl_take isl_basic_set *bset)
8052 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8053 (struct isl_basic_map *)bset);
8056 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8058 if (!bmap)
8059 return NULL;
8060 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8061 return bmap;
8062 bmap = isl_basic_map_remove_redundancies(bmap);
8063 bmap = isl_basic_map_sort_constraints(bmap);
8064 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8065 return bmap;
8068 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8070 return (struct isl_basic_set *)isl_basic_map_normalize(
8071 (struct isl_basic_map *)bset);
8074 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8075 const __isl_keep isl_basic_map *bmap2)
8077 int i, cmp;
8078 unsigned total;
8080 if (bmap1 == bmap2)
8081 return 0;
8082 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8083 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8084 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8085 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8086 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8087 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8088 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8089 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8090 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8091 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8092 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8093 return 0;
8094 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8095 return 1;
8096 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8097 return -1;
8098 if (bmap1->n_eq != bmap2->n_eq)
8099 return bmap1->n_eq - bmap2->n_eq;
8100 if (bmap1->n_ineq != bmap2->n_ineq)
8101 return bmap1->n_ineq - bmap2->n_ineq;
8102 if (bmap1->n_div != bmap2->n_div)
8103 return bmap1->n_div - bmap2->n_div;
8104 total = isl_basic_map_total_dim(bmap1);
8105 for (i = 0; i < bmap1->n_eq; ++i) {
8106 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8107 if (cmp)
8108 return cmp;
8110 for (i = 0; i < bmap1->n_ineq; ++i) {
8111 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8112 if (cmp)
8113 return cmp;
8115 for (i = 0; i < bmap1->n_div; ++i) {
8116 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8117 if (cmp)
8118 return cmp;
8120 return 0;
8123 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8124 const __isl_keep isl_basic_set *bset2)
8126 return isl_basic_map_plain_cmp(bset1, bset2);
8129 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
8130 const __isl_keep isl_set *set2)
8132 int i, cmp;
8134 if (set1 == set2)
8135 return 0;
8136 if (set1->n != set2->n)
8137 return set1->n - set2->n;
8139 for (i = 0; i < set1->n; ++i) {
8140 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8141 if (cmp)
8142 return cmp;
8145 return 0;
8148 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8149 __isl_keep isl_basic_map *bmap2)
8151 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8154 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8155 __isl_keep isl_basic_set *bset2)
8157 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8158 (isl_basic_map *)bset2);
8161 static int qsort_bmap_cmp(const void *p1, const void *p2)
8163 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8164 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8166 return isl_basic_map_plain_cmp(bmap1, bmap2);
8169 /* We normalize in place, but if anything goes wrong we need
8170 * to return NULL, so we need to make sure we don't change the
8171 * meaning of any possible other copies of map.
8173 struct isl_map *isl_map_normalize(struct isl_map *map)
8175 int i, j;
8176 struct isl_basic_map *bmap;
8178 if (!map)
8179 return NULL;
8180 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8181 return map;
8182 for (i = 0; i < map->n; ++i) {
8183 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8184 if (!bmap)
8185 goto error;
8186 isl_basic_map_free(map->p[i]);
8187 map->p[i] = bmap;
8189 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8190 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8191 map = isl_map_remove_empty_parts(map);
8192 if (!map)
8193 return NULL;
8194 for (i = map->n - 1; i >= 1; --i) {
8195 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8196 continue;
8197 isl_basic_map_free(map->p[i-1]);
8198 for (j = i; j < map->n; ++j)
8199 map->p[j-1] = map->p[j];
8200 map->n--;
8202 return map;
8203 error:
8204 isl_map_free(map);
8205 return NULL;
8209 struct isl_set *isl_set_normalize(struct isl_set *set)
8211 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8214 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8216 int i;
8217 int equal;
8219 if (!map1 || !map2)
8220 return -1;
8222 if (map1 == map2)
8223 return 1;
8224 if (!isl_space_is_equal(map1->dim, map2->dim))
8225 return 0;
8227 map1 = isl_map_copy(map1);
8228 map2 = isl_map_copy(map2);
8229 map1 = isl_map_normalize(map1);
8230 map2 = isl_map_normalize(map2);
8231 if (!map1 || !map2)
8232 goto error;
8233 equal = map1->n == map2->n;
8234 for (i = 0; equal && i < map1->n; ++i) {
8235 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8236 if (equal < 0)
8237 goto error;
8239 isl_map_free(map1);
8240 isl_map_free(map2);
8241 return equal;
8242 error:
8243 isl_map_free(map1);
8244 isl_map_free(map2);
8245 return -1;
8248 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8250 return isl_map_plain_is_equal(map1, map2);
8253 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8255 return isl_map_plain_is_equal((struct isl_map *)set1,
8256 (struct isl_map *)set2);
8259 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8261 return isl_set_plain_is_equal(set1, set2);
8264 /* Return an interval that ranges from min to max (inclusive)
8266 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8267 isl_int min, isl_int max)
8269 int k;
8270 struct isl_basic_set *bset = NULL;
8272 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8273 if (!bset)
8274 goto error;
8276 k = isl_basic_set_alloc_inequality(bset);
8277 if (k < 0)
8278 goto error;
8279 isl_int_set_si(bset->ineq[k][1], 1);
8280 isl_int_neg(bset->ineq[k][0], min);
8282 k = isl_basic_set_alloc_inequality(bset);
8283 if (k < 0)
8284 goto error;
8285 isl_int_set_si(bset->ineq[k][1], -1);
8286 isl_int_set(bset->ineq[k][0], max);
8288 return bset;
8289 error:
8290 isl_basic_set_free(bset);
8291 return NULL;
8294 /* Return the Cartesian product of the basic sets in list (in the given order).
8296 __isl_give isl_basic_set *isl_basic_set_list_product(
8297 __isl_take struct isl_basic_set_list *list)
8299 int i;
8300 unsigned dim;
8301 unsigned nparam;
8302 unsigned extra;
8303 unsigned n_eq;
8304 unsigned n_ineq;
8305 struct isl_basic_set *product = NULL;
8307 if (!list)
8308 goto error;
8309 isl_assert(list->ctx, list->n > 0, goto error);
8310 isl_assert(list->ctx, list->p[0], goto error);
8311 nparam = isl_basic_set_n_param(list->p[0]);
8312 dim = isl_basic_set_n_dim(list->p[0]);
8313 extra = list->p[0]->n_div;
8314 n_eq = list->p[0]->n_eq;
8315 n_ineq = list->p[0]->n_ineq;
8316 for (i = 1; i < list->n; ++i) {
8317 isl_assert(list->ctx, list->p[i], goto error);
8318 isl_assert(list->ctx,
8319 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8320 dim += isl_basic_set_n_dim(list->p[i]);
8321 extra += list->p[i]->n_div;
8322 n_eq += list->p[i]->n_eq;
8323 n_ineq += list->p[i]->n_ineq;
8325 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8326 n_eq, n_ineq);
8327 if (!product)
8328 goto error;
8329 dim = 0;
8330 for (i = 0; i < list->n; ++i) {
8331 isl_basic_set_add_constraints(product,
8332 isl_basic_set_copy(list->p[i]), dim);
8333 dim += isl_basic_set_n_dim(list->p[i]);
8335 isl_basic_set_list_free(list);
8336 return product;
8337 error:
8338 isl_basic_set_free(product);
8339 isl_basic_set_list_free(list);
8340 return NULL;
8343 struct isl_basic_map *isl_basic_map_product(
8344 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8346 isl_space *dim_result = NULL;
8347 struct isl_basic_map *bmap;
8348 unsigned in1, in2, out1, out2, nparam, total, pos;
8349 struct isl_dim_map *dim_map1, *dim_map2;
8351 if (!bmap1 || !bmap2)
8352 goto error;
8354 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
8355 bmap2->dim, isl_dim_param), goto error);
8356 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
8357 isl_space_copy(bmap2->dim));
8359 in1 = isl_basic_map_n_in(bmap1);
8360 in2 = isl_basic_map_n_in(bmap2);
8361 out1 = isl_basic_map_n_out(bmap1);
8362 out2 = isl_basic_map_n_out(bmap2);
8363 nparam = isl_basic_map_n_param(bmap1);
8365 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8366 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8367 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8368 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8369 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8370 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8371 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8372 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8373 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8374 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8375 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8377 bmap = isl_basic_map_alloc_space(dim_result,
8378 bmap1->n_div + bmap2->n_div,
8379 bmap1->n_eq + bmap2->n_eq,
8380 bmap1->n_ineq + bmap2->n_ineq);
8381 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8382 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8383 bmap = isl_basic_map_simplify(bmap);
8384 return isl_basic_map_finalize(bmap);
8385 error:
8386 isl_basic_map_free(bmap1);
8387 isl_basic_map_free(bmap2);
8388 return NULL;
8391 __isl_give isl_basic_map *isl_basic_map_flat_product(
8392 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8394 isl_basic_map *prod;
8396 prod = isl_basic_map_product(bmap1, bmap2);
8397 prod = isl_basic_map_flatten(prod);
8398 return prod;
8401 __isl_give isl_basic_set *isl_basic_set_flat_product(
8402 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8404 return isl_basic_map_flat_range_product(bset1, bset2);
8407 __isl_give isl_basic_map *isl_basic_map_domain_product(
8408 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8410 isl_space *space_result = NULL;
8411 isl_basic_map *bmap;
8412 unsigned in1, in2, out, nparam, total, pos;
8413 struct isl_dim_map *dim_map1, *dim_map2;
8415 if (!bmap1 || !bmap2)
8416 goto error;
8418 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8419 isl_space_copy(bmap2->dim));
8421 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8422 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8423 out = isl_basic_map_dim(bmap1, isl_dim_out);
8424 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8426 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8427 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8428 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8429 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8430 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8431 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8432 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8433 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8434 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8435 isl_dim_map_div(dim_map1, bmap1, pos += out);
8436 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8438 bmap = isl_basic_map_alloc_space(space_result,
8439 bmap1->n_div + bmap2->n_div,
8440 bmap1->n_eq + bmap2->n_eq,
8441 bmap1->n_ineq + bmap2->n_ineq);
8442 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8443 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8444 bmap = isl_basic_map_simplify(bmap);
8445 return isl_basic_map_finalize(bmap);
8446 error:
8447 isl_basic_map_free(bmap1);
8448 isl_basic_map_free(bmap2);
8449 return NULL;
8452 __isl_give isl_basic_map *isl_basic_map_range_product(
8453 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8455 isl_space *dim_result = NULL;
8456 isl_basic_map *bmap;
8457 unsigned in, out1, out2, nparam, total, pos;
8458 struct isl_dim_map *dim_map1, *dim_map2;
8460 if (!bmap1 || !bmap2)
8461 goto error;
8463 if (!isl_space_match(bmap1->dim, isl_dim_param,
8464 bmap2->dim, isl_dim_param))
8465 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
8466 "parameters don't match", goto error);
8468 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8469 isl_space_copy(bmap2->dim));
8471 in = isl_basic_map_dim(bmap1, isl_dim_in);
8472 out1 = isl_basic_map_n_out(bmap1);
8473 out2 = isl_basic_map_n_out(bmap2);
8474 nparam = isl_basic_map_n_param(bmap1);
8476 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8477 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8478 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8479 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8480 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8481 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8482 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8483 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8484 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8485 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8486 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8488 bmap = isl_basic_map_alloc_space(dim_result,
8489 bmap1->n_div + bmap2->n_div,
8490 bmap1->n_eq + bmap2->n_eq,
8491 bmap1->n_ineq + bmap2->n_ineq);
8492 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8493 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8494 bmap = isl_basic_map_simplify(bmap);
8495 return isl_basic_map_finalize(bmap);
8496 error:
8497 isl_basic_map_free(bmap1);
8498 isl_basic_map_free(bmap2);
8499 return NULL;
8502 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8503 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8505 isl_basic_map *prod;
8507 prod = isl_basic_map_range_product(bmap1, bmap2);
8508 prod = isl_basic_map_flatten_range(prod);
8509 return prod;
8512 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8513 __isl_take isl_map *map2,
8514 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8515 __isl_take isl_space *right),
8516 __isl_give isl_basic_map *(*basic_map_product)(
8517 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8519 unsigned flags = 0;
8520 struct isl_map *result;
8521 int i, j;
8523 if (!map1 || !map2)
8524 goto error;
8526 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8527 map2->dim, isl_dim_param), goto error);
8529 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8530 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8531 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8533 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8534 isl_space_copy(map2->dim)),
8535 map1->n * map2->n, flags);
8536 if (!result)
8537 goto error;
8538 for (i = 0; i < map1->n; ++i)
8539 for (j = 0; j < map2->n; ++j) {
8540 struct isl_basic_map *part;
8541 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8542 isl_basic_map_copy(map2->p[j]));
8543 if (isl_basic_map_is_empty(part))
8544 isl_basic_map_free(part);
8545 else
8546 result = isl_map_add_basic_map(result, part);
8547 if (!result)
8548 goto error;
8550 isl_map_free(map1);
8551 isl_map_free(map2);
8552 return result;
8553 error:
8554 isl_map_free(map1);
8555 isl_map_free(map2);
8556 return NULL;
8559 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8561 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8562 __isl_take isl_map *map2)
8564 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8567 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8568 __isl_take isl_map *map2)
8570 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8573 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8575 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8576 __isl_take isl_map *map2)
8578 isl_map *prod;
8580 prod = isl_map_product(map1, map2);
8581 prod = isl_map_flatten(prod);
8582 return prod;
8585 /* Given two set A and B, construct its Cartesian product A x B.
8587 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8589 return isl_map_range_product(set1, set2);
8592 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8593 __isl_take isl_set *set2)
8595 return isl_map_flat_range_product(set1, set2);
8598 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8600 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8601 __isl_take isl_map *map2)
8603 return map_product(map1, map2, &isl_space_domain_product,
8604 &isl_basic_map_domain_product);
8607 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8609 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8610 __isl_take isl_map *map2)
8612 return map_product(map1, map2, &isl_space_range_product,
8613 &isl_basic_map_range_product);
8616 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8617 __isl_take isl_map *map2)
8619 return isl_map_align_params_map_map_and(map1, map2,
8620 &map_domain_product_aligned);
8623 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8624 __isl_take isl_map *map2)
8626 return isl_map_align_params_map_map_and(map1, map2,
8627 &map_range_product_aligned);
8630 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8632 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8633 __isl_take isl_map *map2)
8635 isl_map *prod;
8637 prod = isl_map_domain_product(map1, map2);
8638 prod = isl_map_flatten_domain(prod);
8639 return prod;
8642 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8644 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8645 __isl_take isl_map *map2)
8647 isl_map *prod;
8649 prod = isl_map_range_product(map1, map2);
8650 prod = isl_map_flatten_range(prod);
8651 return prod;
8654 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8656 int i;
8657 uint32_t hash = isl_hash_init();
8658 unsigned total;
8660 if (!bmap)
8661 return 0;
8662 bmap = isl_basic_map_copy(bmap);
8663 bmap = isl_basic_map_normalize(bmap);
8664 if (!bmap)
8665 return 0;
8666 total = isl_basic_map_total_dim(bmap);
8667 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8668 for (i = 0; i < bmap->n_eq; ++i) {
8669 uint32_t c_hash;
8670 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8671 isl_hash_hash(hash, c_hash);
8673 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8674 for (i = 0; i < bmap->n_ineq; ++i) {
8675 uint32_t c_hash;
8676 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8677 isl_hash_hash(hash, c_hash);
8679 isl_hash_byte(hash, bmap->n_div & 0xFF);
8680 for (i = 0; i < bmap->n_div; ++i) {
8681 uint32_t c_hash;
8682 if (isl_int_is_zero(bmap->div[i][0]))
8683 continue;
8684 isl_hash_byte(hash, i & 0xFF);
8685 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8686 isl_hash_hash(hash, c_hash);
8688 isl_basic_map_free(bmap);
8689 return hash;
8692 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8694 return isl_basic_map_get_hash((isl_basic_map *)bset);
8697 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8699 int i;
8700 uint32_t hash;
8702 if (!map)
8703 return 0;
8704 map = isl_map_copy(map);
8705 map = isl_map_normalize(map);
8706 if (!map)
8707 return 0;
8709 hash = isl_hash_init();
8710 for (i = 0; i < map->n; ++i) {
8711 uint32_t bmap_hash;
8712 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8713 isl_hash_hash(hash, bmap_hash);
8716 isl_map_free(map);
8718 return hash;
8721 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8723 return isl_map_get_hash((isl_map *)set);
8726 /* Check if the value for dimension dim is completely determined
8727 * by the values of the other parameters and variables.
8728 * That is, check if dimension dim is involved in an equality.
8730 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8732 int i;
8733 unsigned nparam;
8735 if (!bset)
8736 return -1;
8737 nparam = isl_basic_set_n_param(bset);
8738 for (i = 0; i < bset->n_eq; ++i)
8739 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8740 return 1;
8741 return 0;
8744 /* Check if the value for dimension dim is completely determined
8745 * by the values of the other parameters and variables.
8746 * That is, check if dimension dim is involved in an equality
8747 * for each of the subsets.
8749 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8751 int i;
8753 if (!set)
8754 return -1;
8755 for (i = 0; i < set->n; ++i) {
8756 int unique;
8757 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8758 if (unique != 1)
8759 return unique;
8761 return 1;
8764 int isl_set_n_basic_set(__isl_keep isl_set *set)
8766 return set ? set->n : 0;
8769 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8770 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8772 int i;
8774 if (!map)
8775 return -1;
8777 for (i = 0; i < map->n; ++i)
8778 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8779 return -1;
8781 return 0;
8784 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8785 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8787 int i;
8789 if (!set)
8790 return -1;
8792 for (i = 0; i < set->n; ++i)
8793 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8794 return -1;
8796 return 0;
8799 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8801 isl_space *dim;
8803 if (!bset)
8804 return NULL;
8806 bset = isl_basic_set_cow(bset);
8807 if (!bset)
8808 return NULL;
8810 dim = isl_basic_set_get_space(bset);
8811 dim = isl_space_lift(dim, bset->n_div);
8812 if (!dim)
8813 goto error;
8814 isl_space_free(bset->dim);
8815 bset->dim = dim;
8816 bset->extra -= bset->n_div;
8817 bset->n_div = 0;
8819 bset = isl_basic_set_finalize(bset);
8821 return bset;
8822 error:
8823 isl_basic_set_free(bset);
8824 return NULL;
8827 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8829 int i;
8830 isl_space *dim;
8831 unsigned n_div;
8833 set = isl_set_align_divs(set);
8835 if (!set)
8836 return NULL;
8838 set = isl_set_cow(set);
8839 if (!set)
8840 return NULL;
8842 n_div = set->p[0]->n_div;
8843 dim = isl_set_get_space(set);
8844 dim = isl_space_lift(dim, n_div);
8845 if (!dim)
8846 goto error;
8847 isl_space_free(set->dim);
8848 set->dim = dim;
8850 for (i = 0; i < set->n; ++i) {
8851 set->p[i] = isl_basic_set_lift(set->p[i]);
8852 if (!set->p[i])
8853 goto error;
8856 return set;
8857 error:
8858 isl_set_free(set);
8859 return NULL;
8862 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8864 isl_space *dim;
8865 struct isl_basic_map *bmap;
8866 unsigned n_set;
8867 unsigned n_div;
8868 unsigned n_param;
8869 unsigned total;
8870 int i, k, l;
8872 set = isl_set_align_divs(set);
8874 if (!set)
8875 return NULL;
8877 dim = isl_set_get_space(set);
8878 if (set->n == 0 || set->p[0]->n_div == 0) {
8879 isl_set_free(set);
8880 return isl_map_identity(isl_space_map_from_set(dim));
8883 n_div = set->p[0]->n_div;
8884 dim = isl_space_map_from_set(dim);
8885 n_param = isl_space_dim(dim, isl_dim_param);
8886 n_set = isl_space_dim(dim, isl_dim_in);
8887 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8888 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8889 for (i = 0; i < n_set; ++i)
8890 bmap = var_equal(bmap, i);
8892 total = n_param + n_set + n_set + n_div;
8893 for (i = 0; i < n_div; ++i) {
8894 k = isl_basic_map_alloc_inequality(bmap);
8895 if (k < 0)
8896 goto error;
8897 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8898 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8899 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8900 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8901 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8902 set->p[0]->div[i][0]);
8904 l = isl_basic_map_alloc_inequality(bmap);
8905 if (l < 0)
8906 goto error;
8907 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8908 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8909 set->p[0]->div[i][0]);
8910 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8913 isl_set_free(set);
8914 bmap = isl_basic_map_simplify(bmap);
8915 bmap = isl_basic_map_finalize(bmap);
8916 return isl_map_from_basic_map(bmap);
8917 error:
8918 isl_set_free(set);
8919 isl_basic_map_free(bmap);
8920 return NULL;
8923 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8925 unsigned dim;
8926 int size = 0;
8928 if (!bset)
8929 return -1;
8931 dim = isl_basic_set_total_dim(bset);
8932 size += bset->n_eq * (1 + dim);
8933 size += bset->n_ineq * (1 + dim);
8934 size += bset->n_div * (2 + dim);
8936 return size;
8939 int isl_set_size(__isl_keep isl_set *set)
8941 int i;
8942 int size = 0;
8944 if (!set)
8945 return -1;
8947 for (i = 0; i < set->n; ++i)
8948 size += isl_basic_set_size(set->p[i]);
8950 return size;
8953 /* Check if there is any lower bound (if lower == 0) and/or upper
8954 * bound (if upper == 0) on the specified dim.
8956 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8957 enum isl_dim_type type, unsigned pos, int lower, int upper)
8959 int i;
8961 if (!bmap)
8962 return -1;
8964 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8966 pos += isl_basic_map_offset(bmap, type);
8968 for (i = 0; i < bmap->n_div; ++i) {
8969 if (isl_int_is_zero(bmap->div[i][0]))
8970 continue;
8971 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8972 return 1;
8975 for (i = 0; i < bmap->n_eq; ++i)
8976 if (!isl_int_is_zero(bmap->eq[i][pos]))
8977 return 1;
8979 for (i = 0; i < bmap->n_ineq; ++i) {
8980 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8981 if (sgn > 0)
8982 lower = 1;
8983 if (sgn < 0)
8984 upper = 1;
8987 return lower && upper;
8990 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8991 enum isl_dim_type type, unsigned pos)
8993 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8996 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8997 enum isl_dim_type type, unsigned pos)
8999 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9002 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9003 enum isl_dim_type type, unsigned pos)
9005 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9008 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9009 enum isl_dim_type type, unsigned pos)
9011 int i;
9013 if (!map)
9014 return -1;
9016 for (i = 0; i < map->n; ++i) {
9017 int bounded;
9018 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9019 if (bounded < 0 || !bounded)
9020 return bounded;
9023 return 1;
9026 /* Return 1 if the specified dim is involved in both an upper bound
9027 * and a lower bound.
9029 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9030 enum isl_dim_type type, unsigned pos)
9032 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9035 static int has_bound(__isl_keep isl_map *map,
9036 enum isl_dim_type type, unsigned pos,
9037 int (*fn)(__isl_keep isl_basic_map *bmap,
9038 enum isl_dim_type type, unsigned pos))
9040 int i;
9042 if (!map)
9043 return -1;
9045 for (i = 0; i < map->n; ++i) {
9046 int bounded;
9047 bounded = fn(map->p[i], type, pos);
9048 if (bounded < 0 || bounded)
9049 return bounded;
9052 return 0;
9055 /* Return 1 if the specified dim is involved in any lower bound.
9057 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9058 enum isl_dim_type type, unsigned pos)
9060 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9063 /* Return 1 if the specified dim is involved in any upper bound.
9065 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9066 enum isl_dim_type type, unsigned pos)
9068 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9071 /* For each of the "n" variables starting at "first", determine
9072 * the sign of the variable and put the results in the first "n"
9073 * elements of the array "signs".
9074 * Sign
9075 * 1 means that the variable is non-negative
9076 * -1 means that the variable is non-positive
9077 * 0 means the variable attains both positive and negative values.
9079 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9080 unsigned first, unsigned n, int *signs)
9082 isl_vec *bound = NULL;
9083 struct isl_tab *tab = NULL;
9084 struct isl_tab_undo *snap;
9085 int i;
9087 if (!bset || !signs)
9088 return -1;
9090 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9091 tab = isl_tab_from_basic_set(bset, 0);
9092 if (!bound || !tab)
9093 goto error;
9095 isl_seq_clr(bound->el, bound->size);
9096 isl_int_set_si(bound->el[0], -1);
9098 snap = isl_tab_snap(tab);
9099 for (i = 0; i < n; ++i) {
9100 int empty;
9102 isl_int_set_si(bound->el[1 + first + i], -1);
9103 if (isl_tab_add_ineq(tab, bound->el) < 0)
9104 goto error;
9105 empty = tab->empty;
9106 isl_int_set_si(bound->el[1 + first + i], 0);
9107 if (isl_tab_rollback(tab, snap) < 0)
9108 goto error;
9110 if (empty) {
9111 signs[i] = 1;
9112 continue;
9115 isl_int_set_si(bound->el[1 + first + i], 1);
9116 if (isl_tab_add_ineq(tab, bound->el) < 0)
9117 goto error;
9118 empty = tab->empty;
9119 isl_int_set_si(bound->el[1 + first + i], 0);
9120 if (isl_tab_rollback(tab, snap) < 0)
9121 goto error;
9123 signs[i] = empty ? -1 : 0;
9126 isl_tab_free(tab);
9127 isl_vec_free(bound);
9128 return 0;
9129 error:
9130 isl_tab_free(tab);
9131 isl_vec_free(bound);
9132 return -1;
9135 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9136 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9138 if (!bset || !signs)
9139 return -1;
9140 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9141 return -1);
9143 first += pos(bset->dim, type) - 1;
9144 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9147 /* Check if the given basic map is obviously single-valued.
9148 * In particular, for each output dimension, check that there is
9149 * an equality that defines the output dimension in terms of
9150 * earlier dimensions.
9152 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9154 int i, j;
9155 unsigned total;
9156 unsigned n_out;
9157 unsigned o_out;
9159 if (!bmap)
9160 return -1;
9162 total = 1 + isl_basic_map_total_dim(bmap);
9163 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9164 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9166 for (i = 0; i < n_out; ++i) {
9167 for (j = 0; j < bmap->n_eq; ++j) {
9168 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9169 continue;
9170 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9171 total - (o_out + i + 1)) == -1)
9172 break;
9174 if (j >= bmap->n_eq)
9175 return 0;
9178 return 1;
9181 /* Check if the given basic map is single-valued.
9182 * We simply compute
9184 * M \circ M^-1
9186 * and check if the result is a subset of the identity mapping.
9188 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9190 isl_space *space;
9191 isl_basic_map *test;
9192 isl_basic_map *id;
9193 int sv;
9195 sv = isl_basic_map_plain_is_single_valued(bmap);
9196 if (sv < 0 || sv)
9197 return sv;
9199 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9200 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9202 space = isl_basic_map_get_space(bmap);
9203 space = isl_space_map_from_set(isl_space_range(space));
9204 id = isl_basic_map_identity(space);
9206 sv = isl_basic_map_is_subset(test, id);
9208 isl_basic_map_free(test);
9209 isl_basic_map_free(id);
9211 return sv;
9214 /* Check if the given map is obviously single-valued.
9216 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9218 if (!map)
9219 return -1;
9220 if (map->n == 0)
9221 return 1;
9222 if (map->n >= 2)
9223 return 0;
9225 return isl_basic_map_plain_is_single_valued(map->p[0]);
9228 /* Check if the given map is single-valued.
9229 * We simply compute
9231 * M \circ M^-1
9233 * and check if the result is a subset of the identity mapping.
9235 int isl_map_is_single_valued(__isl_keep isl_map *map)
9237 isl_space *dim;
9238 isl_map *test;
9239 isl_map *id;
9240 int sv;
9242 sv = isl_map_plain_is_single_valued(map);
9243 if (sv < 0 || sv)
9244 return sv;
9246 test = isl_map_reverse(isl_map_copy(map));
9247 test = isl_map_apply_range(test, isl_map_copy(map));
9249 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9250 id = isl_map_identity(dim);
9252 sv = isl_map_is_subset(test, id);
9254 isl_map_free(test);
9255 isl_map_free(id);
9257 return sv;
9260 int isl_map_is_injective(__isl_keep isl_map *map)
9262 int in;
9264 map = isl_map_copy(map);
9265 map = isl_map_reverse(map);
9266 in = isl_map_is_single_valued(map);
9267 isl_map_free(map);
9269 return in;
9272 /* Check if the given map is obviously injective.
9274 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9276 int in;
9278 map = isl_map_copy(map);
9279 map = isl_map_reverse(map);
9280 in = isl_map_plain_is_single_valued(map);
9281 isl_map_free(map);
9283 return in;
9286 int isl_map_is_bijective(__isl_keep isl_map *map)
9288 int sv;
9290 sv = isl_map_is_single_valued(map);
9291 if (sv < 0 || !sv)
9292 return sv;
9294 return isl_map_is_injective(map);
9297 int isl_set_is_singleton(__isl_keep isl_set *set)
9299 return isl_map_is_single_valued((isl_map *)set);
9302 int isl_map_is_translation(__isl_keep isl_map *map)
9304 int ok;
9305 isl_set *delta;
9307 delta = isl_map_deltas(isl_map_copy(map));
9308 ok = isl_set_is_singleton(delta);
9309 isl_set_free(delta);
9311 return ok;
9314 static int unique(isl_int *p, unsigned pos, unsigned len)
9316 if (isl_seq_first_non_zero(p, pos) != -1)
9317 return 0;
9318 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
9319 return 0;
9320 return 1;
9323 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
9325 int i, j;
9326 unsigned nvar;
9327 unsigned ovar;
9329 if (!bset)
9330 return -1;
9332 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
9333 return 0;
9335 nvar = isl_basic_set_dim(bset, isl_dim_set);
9336 ovar = isl_space_offset(bset->dim, isl_dim_set);
9337 for (j = 0; j < nvar; ++j) {
9338 int lower = 0, upper = 0;
9339 for (i = 0; i < bset->n_eq; ++i) {
9340 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
9341 continue;
9342 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
9343 return 0;
9344 break;
9346 if (i < bset->n_eq)
9347 continue;
9348 for (i = 0; i < bset->n_ineq; ++i) {
9349 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
9350 continue;
9351 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
9352 return 0;
9353 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
9354 lower = 1;
9355 else
9356 upper = 1;
9358 if (!lower || !upper)
9359 return 0;
9362 return 1;
9365 int isl_set_is_box(__isl_keep isl_set *set)
9367 if (!set)
9368 return -1;
9369 if (set->n != 1)
9370 return 0;
9372 return isl_basic_set_is_box(set->p[0]);
9375 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
9377 if (!bset)
9378 return -1;
9380 return isl_space_is_wrapping(bset->dim);
9383 int isl_set_is_wrapping(__isl_keep isl_set *set)
9385 if (!set)
9386 return -1;
9388 return isl_space_is_wrapping(set->dim);
9391 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
9393 bmap = isl_basic_map_cow(bmap);
9394 if (!bmap)
9395 return NULL;
9397 bmap->dim = isl_space_wrap(bmap->dim);
9398 if (!bmap->dim)
9399 goto error;
9401 bmap = isl_basic_map_finalize(bmap);
9403 return (isl_basic_set *)bmap;
9404 error:
9405 isl_basic_map_free(bmap);
9406 return NULL;
9409 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9411 int i;
9413 map = isl_map_cow(map);
9414 if (!map)
9415 return NULL;
9417 for (i = 0; i < map->n; ++i) {
9418 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
9419 if (!map->p[i])
9420 goto error;
9422 map->dim = isl_space_wrap(map->dim);
9423 if (!map->dim)
9424 goto error;
9426 return (isl_set *)map;
9427 error:
9428 isl_map_free(map);
9429 return NULL;
9432 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
9434 bset = isl_basic_set_cow(bset);
9435 if (!bset)
9436 return NULL;
9438 bset->dim = isl_space_unwrap(bset->dim);
9439 if (!bset->dim)
9440 goto error;
9442 bset = isl_basic_set_finalize(bset);
9444 return (isl_basic_map *)bset;
9445 error:
9446 isl_basic_set_free(bset);
9447 return NULL;
9450 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9452 int i;
9454 if (!set)
9455 return NULL;
9457 if (!isl_set_is_wrapping(set))
9458 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9459 goto error);
9461 set = isl_set_cow(set);
9462 if (!set)
9463 return NULL;
9465 for (i = 0; i < set->n; ++i) {
9466 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9467 if (!set->p[i])
9468 goto error;
9471 set->dim = isl_space_unwrap(set->dim);
9472 if (!set->dim)
9473 goto error;
9475 return (isl_map *)set;
9476 error:
9477 isl_set_free(set);
9478 return NULL;
9481 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9482 enum isl_dim_type type)
9484 if (!bmap)
9485 return NULL;
9487 if (!isl_space_is_named_or_nested(bmap->dim, type))
9488 return bmap;
9490 bmap = isl_basic_map_cow(bmap);
9491 if (!bmap)
9492 return NULL;
9494 bmap->dim = isl_space_reset(bmap->dim, type);
9495 if (!bmap->dim)
9496 goto error;
9498 bmap = isl_basic_map_finalize(bmap);
9500 return bmap;
9501 error:
9502 isl_basic_map_free(bmap);
9503 return NULL;
9506 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9507 enum isl_dim_type type)
9509 int i;
9511 if (!map)
9512 return NULL;
9514 if (!isl_space_is_named_or_nested(map->dim, type))
9515 return map;
9517 map = isl_map_cow(map);
9518 if (!map)
9519 return NULL;
9521 for (i = 0; i < map->n; ++i) {
9522 map->p[i] = isl_basic_map_reset(map->p[i], type);
9523 if (!map->p[i])
9524 goto error;
9526 map->dim = isl_space_reset(map->dim, type);
9527 if (!map->dim)
9528 goto error;
9530 return map;
9531 error:
9532 isl_map_free(map);
9533 return NULL;
9536 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9538 if (!bmap)
9539 return NULL;
9541 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9542 return bmap;
9544 bmap = isl_basic_map_cow(bmap);
9545 if (!bmap)
9546 return NULL;
9548 bmap->dim = isl_space_flatten(bmap->dim);
9549 if (!bmap->dim)
9550 goto error;
9552 bmap = isl_basic_map_finalize(bmap);
9554 return bmap;
9555 error:
9556 isl_basic_map_free(bmap);
9557 return NULL;
9560 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9562 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9565 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9566 __isl_take isl_basic_map *bmap)
9568 if (!bmap)
9569 return NULL;
9571 if (!bmap->dim->nested[0])
9572 return bmap;
9574 bmap = isl_basic_map_cow(bmap);
9575 if (!bmap)
9576 return NULL;
9578 bmap->dim = isl_space_flatten_domain(bmap->dim);
9579 if (!bmap->dim)
9580 goto error;
9582 bmap = isl_basic_map_finalize(bmap);
9584 return bmap;
9585 error:
9586 isl_basic_map_free(bmap);
9587 return NULL;
9590 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9591 __isl_take isl_basic_map *bmap)
9593 if (!bmap)
9594 return NULL;
9596 if (!bmap->dim->nested[1])
9597 return bmap;
9599 bmap = isl_basic_map_cow(bmap);
9600 if (!bmap)
9601 return NULL;
9603 bmap->dim = isl_space_flatten_range(bmap->dim);
9604 if (!bmap->dim)
9605 goto error;
9607 bmap = isl_basic_map_finalize(bmap);
9609 return bmap;
9610 error:
9611 isl_basic_map_free(bmap);
9612 return NULL;
9615 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9617 int i;
9619 if (!map)
9620 return NULL;
9622 if (!map->dim->nested[0] && !map->dim->nested[1])
9623 return map;
9625 map = isl_map_cow(map);
9626 if (!map)
9627 return NULL;
9629 for (i = 0; i < map->n; ++i) {
9630 map->p[i] = isl_basic_map_flatten(map->p[i]);
9631 if (!map->p[i])
9632 goto error;
9634 map->dim = isl_space_flatten(map->dim);
9635 if (!map->dim)
9636 goto error;
9638 return map;
9639 error:
9640 isl_map_free(map);
9641 return NULL;
9644 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9646 return (isl_set *)isl_map_flatten((isl_map *)set);
9649 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9651 isl_space *dim, *flat_dim;
9652 isl_map *map;
9654 dim = isl_set_get_space(set);
9655 flat_dim = isl_space_flatten(isl_space_copy(dim));
9656 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9657 map = isl_map_intersect_domain(map, set);
9659 return map;
9662 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9664 int i;
9666 if (!map)
9667 return NULL;
9669 if (!map->dim->nested[0])
9670 return map;
9672 map = isl_map_cow(map);
9673 if (!map)
9674 return NULL;
9676 for (i = 0; i < map->n; ++i) {
9677 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9678 if (!map->p[i])
9679 goto error;
9681 map->dim = isl_space_flatten_domain(map->dim);
9682 if (!map->dim)
9683 goto error;
9685 return map;
9686 error:
9687 isl_map_free(map);
9688 return NULL;
9691 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9693 int i;
9695 if (!map)
9696 return NULL;
9698 if (!map->dim->nested[1])
9699 return map;
9701 map = isl_map_cow(map);
9702 if (!map)
9703 return NULL;
9705 for (i = 0; i < map->n; ++i) {
9706 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9707 if (!map->p[i])
9708 goto error;
9710 map->dim = isl_space_flatten_range(map->dim);
9711 if (!map->dim)
9712 goto error;
9714 return map;
9715 error:
9716 isl_map_free(map);
9717 return NULL;
9720 /* Reorder the dimensions of "bmap" according to the given dim_map
9721 * and set the dimension specification to "dim".
9723 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9724 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9726 isl_basic_map *res;
9727 unsigned flags;
9729 bmap = isl_basic_map_cow(bmap);
9730 if (!bmap || !dim || !dim_map)
9731 goto error;
9733 flags = bmap->flags;
9734 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
9735 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
9736 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
9737 res = isl_basic_map_alloc_space(dim,
9738 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9739 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9740 if (res)
9741 res->flags = flags;
9742 res = isl_basic_map_finalize(res);
9743 return res;
9744 error:
9745 free(dim_map);
9746 isl_basic_map_free(bmap);
9747 isl_space_free(dim);
9748 return NULL;
9751 /* Reorder the dimensions of "map" according to given reordering.
9753 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9754 __isl_take isl_reordering *r)
9756 int i;
9757 struct isl_dim_map *dim_map;
9759 map = isl_map_cow(map);
9760 dim_map = isl_dim_map_from_reordering(r);
9761 if (!map || !r || !dim_map)
9762 goto error;
9764 for (i = 0; i < map->n; ++i) {
9765 struct isl_dim_map *dim_map_i;
9767 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9769 map->p[i] = isl_basic_map_realign(map->p[i],
9770 isl_space_copy(r->dim), dim_map_i);
9772 if (!map->p[i])
9773 goto error;
9776 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9778 isl_reordering_free(r);
9779 free(dim_map);
9780 return map;
9781 error:
9782 free(dim_map);
9783 isl_map_free(map);
9784 isl_reordering_free(r);
9785 return NULL;
9788 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9789 __isl_take isl_reordering *r)
9791 return (isl_set *)isl_map_realign((isl_map *)set, r);
9794 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9795 __isl_take isl_space *model)
9797 isl_ctx *ctx;
9799 if (!map || !model)
9800 goto error;
9802 ctx = isl_space_get_ctx(model);
9803 if (!isl_space_has_named_params(model))
9804 isl_die(ctx, isl_error_invalid,
9805 "model has unnamed parameters", goto error);
9806 if (!isl_space_has_named_params(map->dim))
9807 isl_die(ctx, isl_error_invalid,
9808 "relation has unnamed parameters", goto error);
9809 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9810 isl_reordering *exp;
9812 model = isl_space_drop_dims(model, isl_dim_in,
9813 0, isl_space_dim(model, isl_dim_in));
9814 model = isl_space_drop_dims(model, isl_dim_out,
9815 0, isl_space_dim(model, isl_dim_out));
9816 exp = isl_parameter_alignment_reordering(map->dim, model);
9817 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9818 map = isl_map_realign(map, exp);
9821 isl_space_free(model);
9822 return map;
9823 error:
9824 isl_space_free(model);
9825 isl_map_free(map);
9826 return NULL;
9829 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9830 __isl_take isl_space *model)
9832 return isl_map_align_params(set, model);
9835 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9836 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9837 enum isl_dim_type c2, enum isl_dim_type c3,
9838 enum isl_dim_type c4, enum isl_dim_type c5)
9840 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9841 struct isl_mat *mat;
9842 int i, j, k;
9843 int pos;
9845 if (!bmap)
9846 return NULL;
9847 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9848 isl_basic_map_total_dim(bmap) + 1);
9849 if (!mat)
9850 return NULL;
9851 for (i = 0; i < bmap->n_eq; ++i)
9852 for (j = 0, pos = 0; j < 5; ++j) {
9853 int off = isl_basic_map_offset(bmap, c[j]);
9854 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9855 isl_int_set(mat->row[i][pos],
9856 bmap->eq[i][off + k]);
9857 ++pos;
9861 return mat;
9864 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9865 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9866 enum isl_dim_type c2, enum isl_dim_type c3,
9867 enum isl_dim_type c4, enum isl_dim_type c5)
9869 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9870 struct isl_mat *mat;
9871 int i, j, k;
9872 int pos;
9874 if (!bmap)
9875 return NULL;
9876 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9877 isl_basic_map_total_dim(bmap) + 1);
9878 if (!mat)
9879 return NULL;
9880 for (i = 0; i < bmap->n_ineq; ++i)
9881 for (j = 0, pos = 0; j < 5; ++j) {
9882 int off = isl_basic_map_offset(bmap, c[j]);
9883 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9884 isl_int_set(mat->row[i][pos],
9885 bmap->ineq[i][off + k]);
9886 ++pos;
9890 return mat;
9893 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9894 __isl_take isl_space *dim,
9895 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9896 enum isl_dim_type c2, enum isl_dim_type c3,
9897 enum isl_dim_type c4, enum isl_dim_type c5)
9899 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9900 isl_basic_map *bmap;
9901 unsigned total;
9902 unsigned extra;
9903 int i, j, k, l;
9904 int pos;
9906 if (!dim || !eq || !ineq)
9907 goto error;
9909 if (eq->n_col != ineq->n_col)
9910 isl_die(dim->ctx, isl_error_invalid,
9911 "equalities and inequalities matrices should have "
9912 "same number of columns", goto error);
9914 total = 1 + isl_space_dim(dim, isl_dim_all);
9916 if (eq->n_col < total)
9917 isl_die(dim->ctx, isl_error_invalid,
9918 "number of columns too small", goto error);
9920 extra = eq->n_col - total;
9922 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9923 eq->n_row, ineq->n_row);
9924 if (!bmap)
9925 goto error;
9926 for (i = 0; i < extra; ++i) {
9927 k = isl_basic_map_alloc_div(bmap);
9928 if (k < 0)
9929 goto error;
9930 isl_int_set_si(bmap->div[k][0], 0);
9932 for (i = 0; i < eq->n_row; ++i) {
9933 l = isl_basic_map_alloc_equality(bmap);
9934 if (l < 0)
9935 goto error;
9936 for (j = 0, pos = 0; j < 5; ++j) {
9937 int off = isl_basic_map_offset(bmap, c[j]);
9938 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9939 isl_int_set(bmap->eq[l][off + k],
9940 eq->row[i][pos]);
9941 ++pos;
9945 for (i = 0; i < ineq->n_row; ++i) {
9946 l = isl_basic_map_alloc_inequality(bmap);
9947 if (l < 0)
9948 goto error;
9949 for (j = 0, pos = 0; j < 5; ++j) {
9950 int off = isl_basic_map_offset(bmap, c[j]);
9951 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9952 isl_int_set(bmap->ineq[l][off + k],
9953 ineq->row[i][pos]);
9954 ++pos;
9959 isl_space_free(dim);
9960 isl_mat_free(eq);
9961 isl_mat_free(ineq);
9963 return bmap;
9964 error:
9965 isl_space_free(dim);
9966 isl_mat_free(eq);
9967 isl_mat_free(ineq);
9968 return NULL;
9971 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9972 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9973 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9975 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9976 c1, c2, c3, c4, isl_dim_in);
9979 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9980 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9981 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9983 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9984 c1, c2, c3, c4, isl_dim_in);
9987 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9988 __isl_take isl_space *dim,
9989 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9990 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9992 return (isl_basic_set*)
9993 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9994 c1, c2, c3, c4, isl_dim_in);
9997 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9999 if (!bmap)
10000 return -1;
10002 return isl_space_can_zip(bmap->dim);
10005 int isl_map_can_zip(__isl_keep isl_map *map)
10007 if (!map)
10008 return -1;
10010 return isl_space_can_zip(map->dim);
10013 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10014 * (A -> C) -> (B -> D).
10016 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10018 unsigned pos;
10019 unsigned n1;
10020 unsigned n2;
10022 if (!bmap)
10023 return NULL;
10025 if (!isl_basic_map_can_zip(bmap))
10026 isl_die(bmap->ctx, isl_error_invalid,
10027 "basic map cannot be zipped", goto error);
10028 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10029 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10030 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10031 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10032 bmap = isl_basic_map_cow(bmap);
10033 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10034 if (!bmap)
10035 return NULL;
10036 bmap->dim = isl_space_zip(bmap->dim);
10037 if (!bmap->dim)
10038 goto error;
10039 return bmap;
10040 error:
10041 isl_basic_map_free(bmap);
10042 return NULL;
10045 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10046 * (A -> C) -> (B -> D).
10048 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10050 int i;
10052 if (!map)
10053 return NULL;
10055 if (!isl_map_can_zip(map))
10056 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10057 goto error);
10059 map = isl_map_cow(map);
10060 if (!map)
10061 return NULL;
10063 for (i = 0; i < map->n; ++i) {
10064 map->p[i] = isl_basic_map_zip(map->p[i]);
10065 if (!map->p[i])
10066 goto error;
10069 map->dim = isl_space_zip(map->dim);
10070 if (!map->dim)
10071 goto error;
10073 return map;
10074 error:
10075 isl_map_free(map);
10076 return NULL;
10079 /* Can we apply isl_basic_map_curry to "bmap"?
10080 * That is, does it have a nested relation in its domain?
10082 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10084 if (!bmap)
10085 return -1;
10087 return isl_space_can_curry(bmap->dim);
10090 /* Can we apply isl_map_curry to "map"?
10091 * That is, does it have a nested relation in its domain?
10093 int isl_map_can_curry(__isl_keep isl_map *map)
10095 if (!map)
10096 return -1;
10098 return isl_space_can_curry(map->dim);
10101 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10102 * A -> (B -> C).
10104 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10107 if (!bmap)
10108 return NULL;
10110 if (!isl_basic_map_can_curry(bmap))
10111 isl_die(bmap->ctx, isl_error_invalid,
10112 "basic map cannot be curried", goto error);
10113 bmap->dim = isl_space_curry(bmap->dim);
10114 if (!bmap->dim)
10115 goto error;
10116 return bmap;
10117 error:
10118 isl_basic_map_free(bmap);
10119 return NULL;
10122 /* Given a map (A -> B) -> C, return the corresponding map
10123 * A -> (B -> C).
10125 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10127 int i;
10129 if (!map)
10130 return NULL;
10132 if (!isl_map_can_curry(map))
10133 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10134 goto error);
10136 map = isl_map_cow(map);
10137 if (!map)
10138 return NULL;
10140 for (i = 0; i < map->n; ++i) {
10141 map->p[i] = isl_basic_map_curry(map->p[i]);
10142 if (!map->p[i])
10143 goto error;
10146 map->dim = isl_space_curry(map->dim);
10147 if (!map->dim)
10148 goto error;
10150 return map;
10151 error:
10152 isl_map_free(map);
10153 return NULL;
10156 /* Construct a basic map mapping the domain of the affine expression
10157 * to a one-dimensional range prescribed by the affine expression.
10159 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
10161 int k;
10162 int pos;
10163 isl_local_space *ls;
10164 isl_basic_map *bmap;
10166 if (!aff)
10167 return NULL;
10169 ls = isl_aff_get_local_space(aff);
10170 bmap = isl_basic_map_from_local_space(ls);
10171 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
10172 k = isl_basic_map_alloc_equality(bmap);
10173 if (k < 0)
10174 goto error;
10176 pos = isl_basic_map_offset(bmap, isl_dim_out);
10177 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
10178 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
10179 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
10180 aff->v->size - (pos + 1));
10182 isl_aff_free(aff);
10183 bmap = isl_basic_map_finalize(bmap);
10184 return bmap;
10185 error:
10186 isl_aff_free(aff);
10187 isl_basic_map_free(bmap);
10188 return NULL;
10191 /* Construct a map mapping the domain of the affine expression
10192 * to a one-dimensional range prescribed by the affine expression.
10194 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
10196 isl_basic_map *bmap;
10198 bmap = isl_basic_map_from_aff(aff);
10199 return isl_map_from_basic_map(bmap);
10202 /* Construct a basic map mapping the domain the multi-affine expression
10203 * to its range, with each dimension in the range equated to the
10204 * corresponding affine expression.
10206 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
10207 __isl_take isl_multi_aff *maff)
10209 int i;
10210 isl_space *space;
10211 isl_basic_map *bmap;
10213 if (!maff)
10214 return NULL;
10216 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
10217 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
10218 "invalid space", return isl_multi_aff_free(maff));
10220 space = isl_space_domain(isl_multi_aff_get_space(maff));
10221 bmap = isl_basic_map_universe(isl_space_from_domain(space));
10223 for (i = 0; i < maff->n; ++i) {
10224 isl_aff *aff;
10225 isl_basic_map *bmap_i;
10227 aff = isl_aff_copy(maff->p[i]);
10228 bmap_i = isl_basic_map_from_aff(aff);
10230 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10233 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
10235 isl_multi_aff_free(maff);
10236 return bmap;
10239 /* Construct a map mapping the domain the multi-affine expression
10240 * to its range, with each dimension in the range equated to the
10241 * corresponding affine expression.
10243 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
10245 isl_basic_map *bmap;
10247 bmap = isl_basic_map_from_multi_aff(maff);
10248 return isl_map_from_basic_map(bmap);
10251 /* Construct a basic map mapping a domain in the given space to
10252 * to an n-dimensional range, with n the number of elements in the list,
10253 * where each coordinate in the range is prescribed by the
10254 * corresponding affine expression.
10255 * The domains of all affine expressions in the list are assumed to match
10256 * domain_dim.
10258 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
10259 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
10261 int i;
10262 isl_space *dim;
10263 isl_basic_map *bmap;
10265 if (!list)
10266 return NULL;
10268 dim = isl_space_from_domain(domain_dim);
10269 bmap = isl_basic_map_universe(dim);
10271 for (i = 0; i < list->n; ++i) {
10272 isl_aff *aff;
10273 isl_basic_map *bmap_i;
10275 aff = isl_aff_copy(list->p[i]);
10276 bmap_i = isl_basic_map_from_aff(aff);
10278 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10281 isl_aff_list_free(list);
10282 return bmap;
10285 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
10286 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10288 return isl_map_equate(set, type1, pos1, type2, pos2);
10291 /* Construct a basic map where the given dimensions are equal to each other.
10293 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
10294 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10296 isl_basic_map *bmap = NULL;
10297 int i;
10299 if (!space)
10300 return NULL;
10302 if (pos1 >= isl_space_dim(space, type1))
10303 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10304 "index out of bounds", goto error);
10305 if (pos2 >= isl_space_dim(space, type2))
10306 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10307 "index out of bounds", goto error);
10309 if (type1 == type2 && pos1 == pos2)
10310 return isl_basic_map_universe(space);
10312 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
10313 i = isl_basic_map_alloc_equality(bmap);
10314 if (i < 0)
10315 goto error;
10316 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10317 pos1 += isl_basic_map_offset(bmap, type1);
10318 pos2 += isl_basic_map_offset(bmap, type2);
10319 isl_int_set_si(bmap->eq[i][pos1], -1);
10320 isl_int_set_si(bmap->eq[i][pos2], 1);
10321 bmap = isl_basic_map_finalize(bmap);
10322 isl_space_free(space);
10323 return bmap;
10324 error:
10325 isl_space_free(space);
10326 isl_basic_map_free(bmap);
10327 return NULL;
10330 /* Add a constraint imposing that the given two dimensions are equal.
10332 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
10333 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10335 isl_basic_map *eq;
10337 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
10339 bmap = isl_basic_map_intersect(bmap, eq);
10341 return bmap;
10344 /* Add a constraint imposing that the given two dimensions are equal.
10346 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
10347 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10349 isl_basic_map *bmap;
10351 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
10353 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10355 return map;
10358 /* Add a constraint imposing that the given two dimensions have opposite values.
10360 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
10361 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10363 isl_basic_map *bmap = NULL;
10364 int i;
10366 if (!map)
10367 return NULL;
10369 if (pos1 >= isl_map_dim(map, type1))
10370 isl_die(map->ctx, isl_error_invalid,
10371 "index out of bounds", goto error);
10372 if (pos2 >= isl_map_dim(map, type2))
10373 isl_die(map->ctx, isl_error_invalid,
10374 "index out of bounds", goto error);
10376 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
10377 i = isl_basic_map_alloc_equality(bmap);
10378 if (i < 0)
10379 goto error;
10380 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10381 pos1 += isl_basic_map_offset(bmap, type1);
10382 pos2 += isl_basic_map_offset(bmap, type2);
10383 isl_int_set_si(bmap->eq[i][pos1], 1);
10384 isl_int_set_si(bmap->eq[i][pos2], 1);
10385 bmap = isl_basic_map_finalize(bmap);
10387 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10389 return map;
10390 error:
10391 isl_basic_map_free(bmap);
10392 isl_map_free(map);
10393 return NULL;
10396 /* Add a constraint imposing that the value of the first dimension is
10397 * greater than that of the second.
10399 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
10400 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10402 isl_basic_map *bmap = NULL;
10403 int i;
10405 if (!map)
10406 return NULL;
10408 if (pos1 >= isl_map_dim(map, type1))
10409 isl_die(map->ctx, isl_error_invalid,
10410 "index out of bounds", goto error);
10411 if (pos2 >= isl_map_dim(map, type2))
10412 isl_die(map->ctx, isl_error_invalid,
10413 "index out of bounds", goto error);
10415 if (type1 == type2 && pos1 == pos2) {
10416 isl_space *space = isl_map_get_space(map);
10417 isl_map_free(map);
10418 return isl_map_empty(space);
10421 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 0, 1);
10422 i = isl_basic_map_alloc_inequality(bmap);
10423 if (i < 0)
10424 goto error;
10425 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
10426 pos1 += isl_basic_map_offset(bmap, type1);
10427 pos2 += isl_basic_map_offset(bmap, type2);
10428 isl_int_set_si(bmap->ineq[i][pos1], 1);
10429 isl_int_set_si(bmap->ineq[i][pos2], -1);
10430 isl_int_set_si(bmap->ineq[i][0], -1);
10431 bmap = isl_basic_map_finalize(bmap);
10433 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10435 return map;
10436 error:
10437 isl_basic_map_free(bmap);
10438 isl_map_free(map);
10439 return NULL;
10442 /* Add a constraint imposing that the value of the first dimension is
10443 * smaller than that of the second.
10445 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
10446 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10448 return isl_map_order_gt(map, type2, pos2, type1, pos1);
10451 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
10452 int pos)
10454 isl_aff *div;
10455 isl_local_space *ls;
10457 if (!bmap)
10458 return NULL;
10460 if (!isl_basic_map_divs_known(bmap))
10461 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
10462 "some divs are unknown", return NULL);
10464 ls = isl_basic_map_get_local_space(bmap);
10465 div = isl_local_space_get_div(ls, pos);
10466 isl_local_space_free(ls);
10468 return div;
10471 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
10472 int pos)
10474 return isl_basic_map_get_div(bset, pos);
10477 /* Plug in "subs" for dimension "type", "pos" of "bset".
10479 * Let i be the dimension to replace and let "subs" be of the form
10481 * f/d
10483 * Any integer division with a non-zero coefficient for i,
10485 * floor((a i + g)/m)
10487 * is replaced by
10489 * floor((a f + d g)/(m d))
10491 * Constraints of the form
10493 * a i + g
10495 * are replaced by
10497 * a f + d g
10499 __isl_give isl_basic_set *isl_basic_set_substitute(
10500 __isl_take isl_basic_set *bset,
10501 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10503 int i;
10504 isl_int v;
10505 isl_ctx *ctx;
10507 if (bset && isl_basic_set_plain_is_empty(bset))
10508 return bset;
10510 bset = isl_basic_set_cow(bset);
10511 if (!bset || !subs)
10512 goto error;
10514 ctx = isl_basic_set_get_ctx(bset);
10515 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
10516 isl_die(ctx, isl_error_invalid,
10517 "spaces don't match", goto error);
10518 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
10519 isl_die(ctx, isl_error_unsupported,
10520 "cannot handle divs yet", goto error);
10522 pos += isl_basic_set_offset(bset, type);
10524 isl_int_init(v);
10526 for (i = 0; i < bset->n_eq; ++i) {
10527 if (isl_int_is_zero(bset->eq[i][pos]))
10528 continue;
10529 isl_int_set(v, bset->eq[i][pos]);
10530 isl_int_set_si(bset->eq[i][pos], 0);
10531 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
10532 v, subs->v->el + 1, subs->v->size - 1);
10535 for (i = 0; i < bset->n_ineq; ++i) {
10536 if (isl_int_is_zero(bset->ineq[i][pos]))
10537 continue;
10538 isl_int_set(v, bset->ineq[i][pos]);
10539 isl_int_set_si(bset->ineq[i][pos], 0);
10540 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
10541 v, subs->v->el + 1, subs->v->size - 1);
10544 for (i = 0; i < bset->n_div; ++i) {
10545 if (isl_int_is_zero(bset->div[i][1 + pos]))
10546 continue;
10547 isl_int_set(v, bset->div[i][1 + pos]);
10548 isl_int_set_si(bset->div[i][1 + pos], 0);
10549 isl_seq_combine(bset->div[i] + 1,
10550 subs->v->el[0], bset->div[i] + 1,
10551 v, subs->v->el + 1, subs->v->size - 1);
10552 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
10555 isl_int_clear(v);
10557 bset = isl_basic_set_simplify(bset);
10558 return isl_basic_set_finalize(bset);
10559 error:
10560 isl_basic_set_free(bset);
10561 return NULL;
10564 /* Plug in "subs" for dimension "type", "pos" of "set".
10566 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
10567 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10569 int i;
10571 if (set && isl_set_plain_is_empty(set))
10572 return set;
10574 set = isl_set_cow(set);
10575 if (!set || !subs)
10576 goto error;
10578 for (i = set->n - 1; i >= 0; --i) {
10579 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
10580 if (remove_if_empty(set, i) < 0)
10581 goto error;
10584 return set;
10585 error:
10586 isl_set_free(set);
10587 return NULL;