add isl_local_space_intersect
[isl.git] / isl_map.c
blobd2a518c2c07ee1088c38d32a618a9a3e617f0508
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <string.h>
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl/blk.h>
17 #include "isl_space_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
20 #include <isl/lp.h>
21 #include <isl/seq.h>
22 #include <isl/set.h>
23 #include <isl/map.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
27 #include "isl_tab.h"
28 #include <isl/vec.h>
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_private.h>
34 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
36 switch (type) {
37 case isl_dim_param: return dim->nparam;
38 case isl_dim_in: return dim->n_in;
39 case isl_dim_out: return dim->n_out;
40 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
41 default: return 0;
45 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
47 switch (type) {
48 case isl_dim_param: return 1;
49 case isl_dim_in: return 1 + dim->nparam;
50 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
51 default: return 0;
55 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
56 enum isl_dim_type type)
58 if (!bmap)
59 return 0;
60 switch (type) {
61 case isl_dim_cst: return 1;
62 case isl_dim_param:
63 case isl_dim_in:
64 case isl_dim_out: return isl_space_dim(bmap->dim, type);
65 case isl_dim_div: return bmap->n_div;
66 case isl_dim_all: return isl_basic_map_total_dim(bmap);
67 default: return 0;
71 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
73 return map ? n(map->dim, type) : 0;
76 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
78 return set ? n(set->dim, type) : 0;
81 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
82 enum isl_dim_type type)
84 isl_space *dim = bmap->dim;
85 switch (type) {
86 case isl_dim_cst: return 0;
87 case isl_dim_param: return 1;
88 case isl_dim_in: return 1 + dim->nparam;
89 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
90 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
91 default: return 0;
95 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
96 enum isl_dim_type type)
98 return isl_basic_map_offset(bset, type);
101 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
103 return pos(map->dim, type);
106 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
107 enum isl_dim_type type)
109 return isl_basic_map_dim(bset, type);
112 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
114 return isl_basic_set_dim(bset, isl_dim_set);
117 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
119 return isl_basic_set_dim(bset, isl_dim_param);
122 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
124 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
127 unsigned isl_set_n_dim(__isl_keep isl_set *set)
129 return isl_set_dim(set, isl_dim_set);
132 unsigned isl_set_n_param(__isl_keep isl_set *set)
134 return isl_set_dim(set, isl_dim_param);
137 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
139 return bmap ? bmap->dim->n_in : 0;
142 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
144 return bmap ? bmap->dim->n_out : 0;
147 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
149 return bmap ? bmap->dim->nparam : 0;
152 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
154 return bmap ? bmap->n_div : 0;
157 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
159 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
162 unsigned isl_map_n_in(const struct isl_map *map)
164 return map ? map->dim->n_in : 0;
167 unsigned isl_map_n_out(const struct isl_map *map)
169 return map ? map->dim->n_out : 0;
172 unsigned isl_map_n_param(const struct isl_map *map)
174 return map ? map->dim->nparam : 0;
177 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
179 int m;
180 if (!map || !set)
181 return -1;
182 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
183 if (m < 0 || !m)
184 return m;
185 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
188 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
189 struct isl_basic_set *bset)
191 int m;
192 if (!bmap || !bset)
193 return -1;
194 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
195 if (m < 0 || !m)
196 return m;
197 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
200 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
202 int m;
203 if (!map || !set)
204 return -1;
205 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
206 if (m < 0 || !m)
207 return m;
208 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
211 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
212 struct isl_basic_set *bset)
214 int m;
215 if (!bmap || !bset)
216 return -1;
217 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
218 if (m < 0 || !m)
219 return m;
220 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
223 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
225 return bmap ? bmap->ctx : NULL;
228 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
230 return bset ? bset->ctx : NULL;
233 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
235 return map ? map->ctx : NULL;
238 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
240 return set ? set->ctx : NULL;
243 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
245 if (!bmap)
246 return NULL;
247 return isl_space_copy(bmap->dim);
250 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
252 if (!bset)
253 return NULL;
254 return isl_space_copy(bset->dim);
257 __isl_give isl_local_space *isl_basic_map_get_local_space(
258 __isl_keep isl_basic_map *bmap)
260 int i;
261 isl_local_space *ls;
262 unsigned total;
264 if (!bmap)
265 return NULL;
267 total = isl_basic_map_total_dim(bmap);
268 ls = isl_local_space_alloc(isl_space_copy(bmap->dim), bmap->n_div);
269 if (!ls)
270 return NULL;
272 for (i = 0; i < bmap->n_div; ++i)
273 isl_seq_cpy(ls->div->row[i], bmap->div[i], 2 + total);
275 return ls;
278 __isl_give isl_local_space *isl_basic_set_get_local_space(
279 __isl_keep isl_basic_set *bset)
281 return isl_basic_map_get_local_space(bset);
284 __isl_give isl_basic_map *isl_basic_map_from_local_space(
285 __isl_take isl_local_space *ls)
287 int i;
288 int n_div;
289 isl_basic_map *bmap;
291 if (!ls)
292 return NULL;
294 n_div = isl_local_space_dim(ls, isl_dim_div);
295 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
296 n_div, 0, 2 * n_div);
298 for (i = 0; i < n_div; ++i)
299 if (isl_basic_map_alloc_div(bmap) < 0)
300 goto error;
302 for (i = 0; i < n_div; ++i) {
303 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
304 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
305 goto error;
308 isl_local_space_free(ls);
309 return bmap;
310 error:
311 isl_local_space_free(ls);
312 isl_basic_map_free(bmap);
313 return NULL;
316 __isl_give isl_basic_set *isl_basic_set_from_local_space(
317 __isl_take isl_local_space *ls)
319 return isl_basic_map_from_local_space(ls);
322 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
324 if (!map)
325 return NULL;
326 return isl_space_copy(map->dim);
329 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
331 if (!set)
332 return NULL;
333 return isl_space_copy(set->dim);
336 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
337 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
339 bmap = isl_basic_map_cow(bmap);
340 if (!bmap)
341 return NULL;
342 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
343 if (!bmap->dim)
344 goto error;
345 bmap = isl_basic_map_finalize(bmap);
346 return bmap;
347 error:
348 isl_basic_map_free(bmap);
349 return NULL;
352 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
353 __isl_take isl_basic_set *bset, const char *s)
355 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
358 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
359 enum isl_dim_type type)
361 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
364 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
365 enum isl_dim_type type, const char *s)
367 int i;
369 map = isl_map_cow(map);
370 if (!map)
371 return NULL;
373 map->dim = isl_space_set_tuple_name(map->dim, type, s);
374 if (!map->dim)
375 goto error;
377 for (i = 0; i < map->n; ++i) {
378 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
379 if (!map->p[i])
380 goto error;
383 return map;
384 error:
385 isl_map_free(map);
386 return NULL;
389 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
390 enum isl_dim_type type)
392 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
395 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
396 const char *s)
398 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
401 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
402 enum isl_dim_type type, __isl_take isl_id *id)
404 map = isl_map_cow(map);
405 if (!map)
406 return isl_id_free(id);
408 map->dim = isl_space_set_tuple_id(map->dim, type, id);
410 return isl_map_reset_space(map, isl_space_copy(map->dim));
413 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
414 __isl_take isl_id *id)
416 return isl_map_set_tuple_id(set, isl_dim_set, id);
419 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
420 enum isl_dim_type type)
422 map = isl_map_cow(map);
423 if (!map)
424 return NULL;
426 map->dim = isl_space_reset_tuple_id(map->dim, type);
428 return isl_map_reset_space(map, isl_space_copy(map->dim));
431 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
433 return isl_map_reset_tuple_id(set, isl_dim_set);
436 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
438 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
441 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
442 enum isl_dim_type type)
444 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
447 int isl_set_has_tuple_id(__isl_keep isl_set *set)
449 return isl_map_has_tuple_id(set, isl_dim_set);
452 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
454 return isl_map_get_tuple_id(set, isl_dim_set);
457 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
459 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
462 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
464 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
467 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
468 enum isl_dim_type type, unsigned pos)
470 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
473 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
474 enum isl_dim_type type, unsigned pos)
476 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
479 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
480 enum isl_dim_type type, unsigned pos)
482 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
485 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
486 enum isl_dim_type type, unsigned pos)
488 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
491 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
492 __isl_take isl_basic_map *bmap,
493 enum isl_dim_type type, unsigned pos, const char *s)
495 if (!bmap)
496 return NULL;
497 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
498 if (!bmap->dim)
499 goto error;
500 return bmap;
501 error:
502 isl_basic_map_free(bmap);
503 return NULL;
506 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
507 enum isl_dim_type type, unsigned pos, const char *s)
509 int i;
511 if (!map)
512 return NULL;
514 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
515 if (!map->dim)
516 goto error;
518 for (i = 0; i < map->n; ++i) {
519 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
520 if (!map->p[i])
521 goto error;
524 return map;
525 error:
526 isl_map_free(map);
527 return NULL;
530 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
531 __isl_take isl_basic_set *bset,
532 enum isl_dim_type type, unsigned pos, const char *s)
534 return (isl_basic_set *)isl_basic_map_set_dim_name(
535 (isl_basic_map *)bset, type, pos, s);
538 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
539 enum isl_dim_type type, unsigned pos, const char *s)
541 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
544 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
545 enum isl_dim_type type, unsigned pos)
547 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
550 int isl_map_has_dim_id(__isl_keep isl_map *map,
551 enum isl_dim_type type, unsigned pos)
553 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
556 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
557 enum isl_dim_type type, unsigned pos)
559 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
562 int isl_set_has_dim_id(__isl_keep isl_set *set,
563 enum isl_dim_type type, unsigned pos)
565 return isl_map_has_dim_id(set, type, pos);
568 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
569 enum isl_dim_type type, unsigned pos)
571 return isl_map_get_dim_id(set, type, pos);
574 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
575 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
577 map = isl_map_cow(map);
578 if (!map)
579 return isl_id_free(id);
581 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
583 return isl_map_reset_space(map, isl_space_copy(map->dim));
586 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
587 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
589 return isl_map_set_dim_id(set, type, pos, id);
592 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
593 __isl_keep isl_id *id)
595 if (!map)
596 return -1;
597 return isl_space_find_dim_by_id(map->dim, type, id);
600 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
601 __isl_keep isl_id *id)
603 return isl_map_find_dim_by_id(set, type, id);
606 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
607 const char *name)
609 if (!map)
610 return -1;
611 return isl_space_find_dim_by_name(map->dim, type, name);
614 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
616 if (!bmap)
617 return -1;
618 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
621 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
623 return isl_basic_map_is_rational(bset);
626 /* Is this basic set a parameter domain?
628 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
630 if (!bset)
631 return -1;
632 return isl_space_is_params(bset->dim);
635 /* Is this set a parameter domain?
637 int isl_set_is_params(__isl_keep isl_set *set)
639 if (!set)
640 return -1;
641 return isl_space_is_params(set->dim);
644 /* Is this map actually a parameter domain?
645 * Users should never call this function. Outside of isl,
646 * a map can never be a parameter domain.
648 int isl_map_is_params(__isl_keep isl_map *map)
650 if (!map)
651 return -1;
652 return isl_space_is_params(map->dim);
655 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
656 struct isl_basic_map *bmap, unsigned extra,
657 unsigned n_eq, unsigned n_ineq)
659 int i;
660 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
662 bmap->ctx = ctx;
663 isl_ctx_ref(ctx);
665 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
666 if (isl_blk_is_error(bmap->block))
667 goto error;
669 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
670 if (!bmap->ineq)
671 goto error;
673 if (extra == 0) {
674 bmap->block2 = isl_blk_empty();
675 bmap->div = NULL;
676 } else {
677 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
678 if (isl_blk_is_error(bmap->block2))
679 goto error;
681 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
682 if (!bmap->div)
683 goto error;
686 for (i = 0; i < n_ineq + n_eq; ++i)
687 bmap->ineq[i] = bmap->block.data + i * row_size;
689 for (i = 0; i < extra; ++i)
690 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
692 bmap->ref = 1;
693 bmap->flags = 0;
694 bmap->c_size = n_eq + n_ineq;
695 bmap->eq = bmap->ineq + n_ineq;
696 bmap->extra = extra;
697 bmap->n_eq = 0;
698 bmap->n_ineq = 0;
699 bmap->n_div = 0;
700 bmap->sample = NULL;
702 return bmap;
703 error:
704 isl_basic_map_free(bmap);
705 return NULL;
708 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
709 unsigned nparam, unsigned dim, unsigned extra,
710 unsigned n_eq, unsigned n_ineq)
712 struct isl_basic_map *bmap;
713 isl_space *space;
715 space = isl_space_set_alloc(ctx, nparam, dim);
716 if (!space)
717 return NULL;
719 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
720 return (struct isl_basic_set *)bmap;
723 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
724 unsigned extra, unsigned n_eq, unsigned n_ineq)
726 struct isl_basic_map *bmap;
727 if (!dim)
728 return NULL;
729 isl_assert(dim->ctx, dim->n_in == 0, goto error);
730 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
731 return (struct isl_basic_set *)bmap;
732 error:
733 isl_space_free(dim);
734 return NULL;
737 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
738 unsigned extra, unsigned n_eq, unsigned n_ineq)
740 struct isl_basic_map *bmap;
742 if (!dim)
743 return NULL;
744 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
745 if (!bmap)
746 goto error;
747 bmap->dim = dim;
749 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
750 error:
751 isl_space_free(dim);
752 return NULL;
755 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
756 unsigned nparam, unsigned in, unsigned out, unsigned extra,
757 unsigned n_eq, unsigned n_ineq)
759 struct isl_basic_map *bmap;
760 isl_space *dim;
762 dim = isl_space_alloc(ctx, nparam, in, out);
763 if (!dim)
764 return NULL;
766 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
767 return bmap;
770 static void dup_constraints(
771 struct isl_basic_map *dst, struct isl_basic_map *src)
773 int i;
774 unsigned total = isl_basic_map_total_dim(src);
776 for (i = 0; i < src->n_eq; ++i) {
777 int j = isl_basic_map_alloc_equality(dst);
778 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
781 for (i = 0; i < src->n_ineq; ++i) {
782 int j = isl_basic_map_alloc_inequality(dst);
783 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
786 for (i = 0; i < src->n_div; ++i) {
787 int j = isl_basic_map_alloc_div(dst);
788 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
790 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
793 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
795 struct isl_basic_map *dup;
797 if (!bmap)
798 return NULL;
799 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
800 bmap->n_div, bmap->n_eq, bmap->n_ineq);
801 if (!dup)
802 return NULL;
803 dup_constraints(dup, bmap);
804 dup->flags = bmap->flags;
805 dup->sample = isl_vec_copy(bmap->sample);
806 return dup;
809 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
811 struct isl_basic_map *dup;
813 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
814 return (struct isl_basic_set *)dup;
817 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
819 if (!bset)
820 return NULL;
822 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
823 bset->ref++;
824 return bset;
826 return isl_basic_set_dup(bset);
829 struct isl_set *isl_set_copy(struct isl_set *set)
831 if (!set)
832 return NULL;
834 set->ref++;
835 return set;
838 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
840 if (!bmap)
841 return NULL;
843 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
844 bmap->ref++;
845 return bmap;
847 bmap = isl_basic_map_dup(bmap);
848 if (bmap)
849 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
850 return bmap;
853 struct isl_map *isl_map_copy(struct isl_map *map)
855 if (!map)
856 return NULL;
858 map->ref++;
859 return map;
862 void isl_basic_map_free(struct isl_basic_map *bmap)
864 if (!bmap)
865 return;
867 if (--bmap->ref > 0)
868 return;
870 isl_ctx_deref(bmap->ctx);
871 free(bmap->div);
872 isl_blk_free(bmap->ctx, bmap->block2);
873 free(bmap->ineq);
874 isl_blk_free(bmap->ctx, bmap->block);
875 isl_vec_free(bmap->sample);
876 isl_space_free(bmap->dim);
877 free(bmap);
880 void isl_basic_set_free(struct isl_basic_set *bset)
882 isl_basic_map_free((struct isl_basic_map *)bset);
885 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
887 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
890 __isl_give isl_map *isl_map_align_params_map_map_and(
891 __isl_take isl_map *map1, __isl_take isl_map *map2,
892 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
893 __isl_take isl_map *map2))
895 if (!map1 || !map2)
896 goto error;
897 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
898 return fn(map1, map2);
899 if (!isl_space_has_named_params(map1->dim) ||
900 !isl_space_has_named_params(map2->dim))
901 isl_die(map1->ctx, isl_error_invalid,
902 "unaligned unnamed parameters", goto error);
903 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
904 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
905 return fn(map1, map2);
906 error:
907 isl_map_free(map1);
908 isl_map_free(map2);
909 return NULL;
912 static int align_params_map_map_and_test(__isl_keep isl_map *map1,
913 __isl_keep isl_map *map2,
914 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
916 int r;
918 if (!map1 || !map2)
919 return -1;
920 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
921 return fn(map1, map2);
922 if (!isl_space_has_named_params(map1->dim) ||
923 !isl_space_has_named_params(map2->dim))
924 isl_die(map1->ctx, isl_error_invalid,
925 "unaligned unnamed parameters", return -1);
926 map1 = isl_map_copy(map1);
927 map2 = isl_map_copy(map2);
928 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
929 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
930 r = fn(map1, map2);
931 isl_map_free(map1);
932 isl_map_free(map2);
933 return r;
936 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
938 struct isl_ctx *ctx;
939 if (!bmap)
940 return -1;
941 ctx = bmap->ctx;
942 isl_assert(ctx, room_for_con(bmap, 1), return -1);
943 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
944 return -1);
945 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
946 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
947 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
948 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
949 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
950 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
951 isl_int *t;
952 int j = isl_basic_map_alloc_inequality(bmap);
953 if (j < 0)
954 return -1;
955 t = bmap->ineq[j];
956 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
957 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
958 bmap->eq[-1] = t;
959 bmap->n_eq++;
960 bmap->n_ineq--;
961 bmap->eq--;
962 return 0;
964 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
965 bmap->extra - bmap->n_div);
966 return bmap->n_eq++;
969 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
971 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
974 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
976 if (!bmap)
977 return -1;
978 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
979 bmap->n_eq -= n;
980 return 0;
983 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
985 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
988 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
990 isl_int *t;
991 if (!bmap)
992 return -1;
993 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
995 if (pos != bmap->n_eq - 1) {
996 t = bmap->eq[pos];
997 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
998 bmap->eq[bmap->n_eq - 1] = t;
1000 bmap->n_eq--;
1001 return 0;
1004 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1006 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1009 void isl_basic_map_inequality_to_equality(
1010 struct isl_basic_map *bmap, unsigned pos)
1012 isl_int *t;
1014 t = bmap->ineq[pos];
1015 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1016 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1017 bmap->eq[-1] = t;
1018 bmap->n_eq++;
1019 bmap->n_ineq--;
1020 bmap->eq--;
1021 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1022 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1023 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1024 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1027 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1029 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1032 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1034 struct isl_ctx *ctx;
1035 if (!bmap)
1036 return -1;
1037 ctx = bmap->ctx;
1038 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1039 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1040 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1041 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1042 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1043 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1044 1 + isl_basic_map_total_dim(bmap),
1045 bmap->extra - bmap->n_div);
1046 return bmap->n_ineq++;
1049 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1051 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1054 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1056 if (!bmap)
1057 return -1;
1058 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1059 bmap->n_ineq -= n;
1060 return 0;
1063 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1065 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1068 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1070 isl_int *t;
1071 if (!bmap)
1072 return -1;
1073 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1075 if (pos != bmap->n_ineq - 1) {
1076 t = bmap->ineq[pos];
1077 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1078 bmap->ineq[bmap->n_ineq - 1] = t;
1079 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1081 bmap->n_ineq--;
1082 return 0;
1085 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1087 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1090 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1091 isl_int *eq)
1093 int k;
1095 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1096 if (!bmap)
1097 return NULL;
1098 k = isl_basic_map_alloc_equality(bmap);
1099 if (k < 0)
1100 goto error;
1101 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1102 return bmap;
1103 error:
1104 isl_basic_map_free(bmap);
1105 return NULL;
1108 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1109 isl_int *eq)
1111 return (isl_basic_set *)
1112 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1115 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1116 isl_int *ineq)
1118 int k;
1120 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1121 if (!bmap)
1122 return NULL;
1123 k = isl_basic_map_alloc_inequality(bmap);
1124 if (k < 0)
1125 goto error;
1126 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1127 return bmap;
1128 error:
1129 isl_basic_map_free(bmap);
1130 return NULL;
1133 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1134 isl_int *ineq)
1136 return (isl_basic_set *)
1137 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1140 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1142 if (!bmap)
1143 return -1;
1144 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1145 isl_seq_clr(bmap->div[bmap->n_div] +
1146 1 + 1 + isl_basic_map_total_dim(bmap),
1147 bmap->extra - bmap->n_div);
1148 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1149 return bmap->n_div++;
1152 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1154 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1157 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1159 if (!bmap)
1160 return -1;
1161 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1162 bmap->n_div -= n;
1163 return 0;
1166 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1168 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1171 /* Copy constraint from src to dst, putting the vars of src at offset
1172 * dim_off in dst and the divs of src at offset div_off in dst.
1173 * If both sets are actually map, then dim_off applies to the input
1174 * variables.
1176 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1177 struct isl_basic_map *src_map, isl_int *src,
1178 unsigned in_off, unsigned out_off, unsigned div_off)
1180 unsigned src_nparam = isl_basic_map_n_param(src_map);
1181 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1182 unsigned src_in = isl_basic_map_n_in(src_map);
1183 unsigned dst_in = isl_basic_map_n_in(dst_map);
1184 unsigned src_out = isl_basic_map_n_out(src_map);
1185 unsigned dst_out = isl_basic_map_n_out(dst_map);
1186 isl_int_set(dst[0], src[0]);
1187 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1188 if (dst_nparam > src_nparam)
1189 isl_seq_clr(dst+1+src_nparam,
1190 dst_nparam - src_nparam);
1191 isl_seq_clr(dst+1+dst_nparam, in_off);
1192 isl_seq_cpy(dst+1+dst_nparam+in_off,
1193 src+1+src_nparam,
1194 isl_min(dst_in-in_off, src_in));
1195 if (dst_in-in_off > src_in)
1196 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1197 dst_in - in_off - src_in);
1198 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1199 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1200 src+1+src_nparam+src_in,
1201 isl_min(dst_out-out_off, src_out));
1202 if (dst_out-out_off > src_out)
1203 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1204 dst_out - out_off - src_out);
1205 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1206 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1207 src+1+src_nparam+src_in+src_out,
1208 isl_min(dst_map->extra-div_off, src_map->n_div));
1209 if (dst_map->n_div-div_off > src_map->n_div)
1210 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1211 div_off+src_map->n_div,
1212 dst_map->n_div - div_off - src_map->n_div);
1215 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1216 struct isl_basic_map *src_map, isl_int *src,
1217 unsigned in_off, unsigned out_off, unsigned div_off)
1219 isl_int_set(dst[0], src[0]);
1220 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1223 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1224 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1226 int i;
1227 unsigned div_off;
1229 if (!bmap1 || !bmap2)
1230 goto error;
1232 div_off = bmap1->n_div;
1234 for (i = 0; i < bmap2->n_eq; ++i) {
1235 int i1 = isl_basic_map_alloc_equality(bmap1);
1236 if (i1 < 0)
1237 goto error;
1238 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1239 i_pos, o_pos, div_off);
1242 for (i = 0; i < bmap2->n_ineq; ++i) {
1243 int i1 = isl_basic_map_alloc_inequality(bmap1);
1244 if (i1 < 0)
1245 goto error;
1246 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1247 i_pos, o_pos, div_off);
1250 for (i = 0; i < bmap2->n_div; ++i) {
1251 int i1 = isl_basic_map_alloc_div(bmap1);
1252 if (i1 < 0)
1253 goto error;
1254 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1255 i_pos, o_pos, div_off);
1258 isl_basic_map_free(bmap2);
1260 return bmap1;
1262 error:
1263 isl_basic_map_free(bmap1);
1264 isl_basic_map_free(bmap2);
1265 return NULL;
1268 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1269 struct isl_basic_set *bset2, unsigned pos)
1271 return (struct isl_basic_set *)
1272 add_constraints((struct isl_basic_map *)bset1,
1273 (struct isl_basic_map *)bset2, 0, pos);
1276 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1277 __isl_take isl_space *dim, unsigned extra,
1278 unsigned n_eq, unsigned n_ineq)
1280 struct isl_basic_map *ext;
1281 unsigned flags;
1282 int dims_ok;
1284 if (!dim)
1285 goto error;
1287 if (!base)
1288 goto error;
1290 dims_ok = isl_space_is_equal(base->dim, dim) &&
1291 base->extra >= base->n_div + extra;
1293 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1294 room_for_ineq(base, n_ineq)) {
1295 isl_space_free(dim);
1296 return base;
1299 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1300 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1301 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1302 extra += base->extra;
1303 n_eq += base->n_eq;
1304 n_ineq += base->n_ineq;
1306 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1307 dim = NULL;
1308 if (!ext)
1309 goto error;
1311 if (dims_ok)
1312 ext->sample = isl_vec_copy(base->sample);
1313 flags = base->flags;
1314 ext = add_constraints(ext, base, 0, 0);
1315 if (ext) {
1316 ext->flags = flags;
1317 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1320 return ext;
1322 error:
1323 isl_space_free(dim);
1324 isl_basic_map_free(base);
1325 return NULL;
1328 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1329 __isl_take isl_space *dim, unsigned extra,
1330 unsigned n_eq, unsigned n_ineq)
1332 return (struct isl_basic_set *)
1333 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1334 extra, n_eq, n_ineq);
1337 struct isl_basic_map *isl_basic_map_extend_constraints(
1338 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1340 if (!base)
1341 return NULL;
1342 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1343 0, n_eq, n_ineq);
1346 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1347 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1348 unsigned n_eq, unsigned n_ineq)
1350 struct isl_basic_map *bmap;
1351 isl_space *dim;
1353 if (!base)
1354 return NULL;
1355 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1356 if (!dim)
1357 goto error;
1359 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1360 return bmap;
1361 error:
1362 isl_basic_map_free(base);
1363 return NULL;
1366 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1367 unsigned nparam, unsigned dim, unsigned extra,
1368 unsigned n_eq, unsigned n_ineq)
1370 return (struct isl_basic_set *)
1371 isl_basic_map_extend((struct isl_basic_map *)base,
1372 nparam, 0, dim, extra, n_eq, n_ineq);
1375 struct isl_basic_set *isl_basic_set_extend_constraints(
1376 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1378 return (struct isl_basic_set *)
1379 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1380 n_eq, n_ineq);
1383 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1385 return (struct isl_basic_set *)
1386 isl_basic_map_cow((struct isl_basic_map *)bset);
1389 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1391 if (!bmap)
1392 return NULL;
1394 if (bmap->ref > 1) {
1395 bmap->ref--;
1396 bmap = isl_basic_map_dup(bmap);
1398 if (bmap)
1399 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1400 return bmap;
1403 struct isl_set *isl_set_cow(struct isl_set *set)
1405 if (!set)
1406 return NULL;
1408 if (set->ref == 1)
1409 return set;
1410 set->ref--;
1411 return isl_set_dup(set);
1414 struct isl_map *isl_map_cow(struct isl_map *map)
1416 if (!map)
1417 return NULL;
1419 if (map->ref == 1)
1420 return map;
1421 map->ref--;
1422 return isl_map_dup(map);
1425 static void swap_vars(struct isl_blk blk, isl_int *a,
1426 unsigned a_len, unsigned b_len)
1428 isl_seq_cpy(blk.data, a+a_len, b_len);
1429 isl_seq_cpy(blk.data+b_len, a, a_len);
1430 isl_seq_cpy(a, blk.data, b_len+a_len);
1433 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1434 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1436 int i;
1437 struct isl_blk blk;
1439 if (!bmap)
1440 goto error;
1442 isl_assert(bmap->ctx,
1443 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1445 if (n1 == 0 || n2 == 0)
1446 return bmap;
1448 bmap = isl_basic_map_cow(bmap);
1449 if (!bmap)
1450 return NULL;
1452 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1453 if (isl_blk_is_error(blk))
1454 goto error;
1456 for (i = 0; i < bmap->n_eq; ++i)
1457 swap_vars(blk,
1458 bmap->eq[i] + pos, n1, n2);
1460 for (i = 0; i < bmap->n_ineq; ++i)
1461 swap_vars(blk,
1462 bmap->ineq[i] + pos, n1, n2);
1464 for (i = 0; i < bmap->n_div; ++i)
1465 swap_vars(blk,
1466 bmap->div[i]+1 + pos, n1, n2);
1468 isl_blk_free(bmap->ctx, blk);
1470 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1471 bmap = isl_basic_map_gauss(bmap, NULL);
1472 return isl_basic_map_finalize(bmap);
1473 error:
1474 isl_basic_map_free(bmap);
1475 return NULL;
1478 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1479 __isl_take isl_basic_set *bset, unsigned n)
1481 unsigned dim;
1482 unsigned nparam;
1484 nparam = isl_basic_set_n_param(bset);
1485 dim = isl_basic_set_n_dim(bset);
1486 isl_assert(bset->ctx, n <= dim, goto error);
1488 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1489 error:
1490 isl_basic_set_free(bset);
1491 return NULL;
1494 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1496 int i = 0;
1497 unsigned total;
1498 if (!bmap)
1499 goto error;
1500 total = isl_basic_map_total_dim(bmap);
1501 isl_basic_map_free_div(bmap, bmap->n_div);
1502 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1503 if (bmap->n_eq > 0)
1504 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1505 else {
1506 i = isl_basic_map_alloc_equality(bmap);
1507 if (i < 0)
1508 goto error;
1510 isl_int_set_si(bmap->eq[i][0], 1);
1511 isl_seq_clr(bmap->eq[i]+1, total);
1512 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1513 isl_vec_free(bmap->sample);
1514 bmap->sample = NULL;
1515 return isl_basic_map_finalize(bmap);
1516 error:
1517 isl_basic_map_free(bmap);
1518 return NULL;
1521 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1523 return (struct isl_basic_set *)
1524 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1527 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1529 int i;
1530 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1531 isl_int *t = bmap->div[a];
1532 bmap->div[a] = bmap->div[b];
1533 bmap->div[b] = t;
1535 for (i = 0; i < bmap->n_eq; ++i)
1536 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1538 for (i = 0; i < bmap->n_ineq; ++i)
1539 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1541 for (i = 0; i < bmap->n_div; ++i)
1542 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1543 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1546 /* Eliminate the specified n dimensions starting at first from the
1547 * constraints using Fourier-Motzkin. The dimensions themselves
1548 * are not removed.
1550 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1551 enum isl_dim_type type, unsigned first, unsigned n)
1553 int i;
1555 if (!map)
1556 return NULL;
1557 if (n == 0)
1558 return map;
1560 map = isl_map_cow(map);
1561 if (!map)
1562 return NULL;
1563 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1564 first += pos(map->dim, type) - 1;
1566 for (i = 0; i < map->n; ++i) {
1567 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1568 if (!map->p[i])
1569 goto error;
1571 return map;
1572 error:
1573 isl_map_free(map);
1574 return NULL;
1577 /* Eliminate the specified n dimensions starting at first from the
1578 * constraints using Fourier-Motzkin. The dimensions themselves
1579 * are not removed.
1581 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1582 enum isl_dim_type type, unsigned first, unsigned n)
1584 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1587 /* Eliminate the specified n dimensions starting at first from the
1588 * constraints using Fourier-Motzkin. The dimensions themselves
1589 * are not removed.
1591 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1592 unsigned first, unsigned n)
1594 return isl_set_eliminate(set, isl_dim_set, first, n);
1597 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1598 __isl_take isl_basic_map *bmap)
1600 if (!bmap)
1601 return NULL;
1602 bmap = isl_basic_map_eliminate_vars(bmap,
1603 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1604 if (!bmap)
1605 return NULL;
1606 bmap->n_div = 0;
1607 return isl_basic_map_finalize(bmap);
1610 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1611 __isl_take isl_basic_set *bset)
1613 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1614 (struct isl_basic_map *)bset);
1617 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1619 int i;
1621 if (!map)
1622 return NULL;
1623 if (map->n == 0)
1624 return map;
1626 map = isl_map_cow(map);
1627 if (!map)
1628 return NULL;
1630 for (i = 0; i < map->n; ++i) {
1631 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1632 if (!map->p[i])
1633 goto error;
1635 return map;
1636 error:
1637 isl_map_free(map);
1638 return NULL;
1641 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1643 return isl_map_remove_divs(set);
1646 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1647 enum isl_dim_type type, unsigned first, unsigned n)
1649 if (!bmap)
1650 return NULL;
1651 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1652 goto error);
1653 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1654 return bmap;
1655 bmap = isl_basic_map_eliminate_vars(bmap,
1656 isl_basic_map_offset(bmap, type) - 1 + first, n);
1657 if (!bmap)
1658 return bmap;
1659 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1660 return bmap;
1661 bmap = isl_basic_map_drop(bmap, type, first, n);
1662 return bmap;
1663 error:
1664 isl_basic_map_free(bmap);
1665 return NULL;
1668 /* Return true if the definition of the given div (recursively) involves
1669 * any of the given variables.
1671 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1672 unsigned first, unsigned n)
1674 int i;
1675 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1677 if (isl_int_is_zero(bmap->div[div][0]))
1678 return 0;
1679 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1680 return 1;
1682 for (i = bmap->n_div - 1; i >= 0; --i) {
1683 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1684 continue;
1685 if (div_involves_vars(bmap, i, first, n))
1686 return 1;
1689 return 0;
1692 /* Remove all divs (recursively) involving any of the given dimensions
1693 * in their definitions.
1695 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1696 __isl_take isl_basic_map *bmap,
1697 enum isl_dim_type type, unsigned first, unsigned n)
1699 int i;
1701 if (!bmap)
1702 return NULL;
1703 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1704 goto error);
1705 first += isl_basic_map_offset(bmap, type);
1707 for (i = bmap->n_div - 1; i >= 0; --i) {
1708 if (!div_involves_vars(bmap, i, first, n))
1709 continue;
1710 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1711 if (!bmap)
1712 return NULL;
1713 i = bmap->n_div;
1716 return bmap;
1717 error:
1718 isl_basic_map_free(bmap);
1719 return NULL;
1722 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1723 enum isl_dim_type type, unsigned first, unsigned n)
1725 int i;
1727 if (!map)
1728 return NULL;
1729 if (map->n == 0)
1730 return map;
1732 map = isl_map_cow(map);
1733 if (!map)
1734 return NULL;
1736 for (i = 0; i < map->n; ++i) {
1737 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1738 type, first, n);
1739 if (!map->p[i])
1740 goto error;
1742 return map;
1743 error:
1744 isl_map_free(map);
1745 return NULL;
1748 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1749 enum isl_dim_type type, unsigned first, unsigned n)
1751 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1752 type, first, n);
1755 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1756 enum isl_dim_type type, unsigned first, unsigned n)
1758 int i;
1760 if (!bmap)
1761 return -1;
1763 if (first + n > isl_basic_map_dim(bmap, type))
1764 isl_die(bmap->ctx, isl_error_invalid,
1765 "index out of bounds", return -1);
1767 first += isl_basic_map_offset(bmap, type);
1768 for (i = 0; i < bmap->n_eq; ++i)
1769 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1770 return 1;
1771 for (i = 0; i < bmap->n_ineq; ++i)
1772 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1773 return 1;
1775 return 0;
1778 int isl_map_involves_dims(__isl_keep isl_map *map,
1779 enum isl_dim_type type, unsigned first, unsigned n)
1781 int i;
1783 if (!map)
1784 return -1;
1786 if (first + n > isl_map_dim(map, type))
1787 isl_die(map->ctx, isl_error_invalid,
1788 "index out of bounds", return -1);
1790 for (i = 0; i < map->n; ++i) {
1791 int involves = isl_basic_map_involves_dims(map->p[i],
1792 type, first, n);
1793 if (involves < 0 || involves)
1794 return involves;
1797 return 0;
1800 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1801 enum isl_dim_type type, unsigned first, unsigned n)
1803 return isl_basic_map_involves_dims(bset, type, first, n);
1806 int isl_set_involves_dims(__isl_keep isl_set *set,
1807 enum isl_dim_type type, unsigned first, unsigned n)
1809 return isl_map_involves_dims(set, type, first, n);
1812 /* Return true if the definition of the given div is unknown or depends
1813 * on unknown divs.
1815 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1817 int i;
1818 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1820 if (isl_int_is_zero(bmap->div[div][0]))
1821 return 1;
1823 for (i = bmap->n_div - 1; i >= 0; --i) {
1824 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1825 continue;
1826 if (div_is_unknown(bmap, i))
1827 return 1;
1830 return 0;
1833 /* Remove all divs that are unknown or defined in terms of unknown divs.
1835 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1836 __isl_take isl_basic_map *bmap)
1838 int i;
1840 if (!bmap)
1841 return NULL;
1843 for (i = bmap->n_div - 1; i >= 0; --i) {
1844 if (!div_is_unknown(bmap, i))
1845 continue;
1846 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1849 return bmap;
1852 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1854 int i;
1856 if (!map)
1857 return NULL;
1858 if (map->n == 0)
1859 return map;
1861 map = isl_map_cow(map);
1862 if (!map)
1863 return NULL;
1865 for (i = 0; i < map->n; ++i) {
1866 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1867 if (!map->p[i])
1868 goto error;
1870 return map;
1871 error:
1872 isl_map_free(map);
1873 return NULL;
1876 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1878 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1881 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1882 __isl_take isl_basic_set *bset,
1883 enum isl_dim_type type, unsigned first, unsigned n)
1885 return (isl_basic_set *)
1886 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1889 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1890 enum isl_dim_type type, unsigned first, unsigned n)
1892 int i;
1894 if (n == 0)
1895 return map;
1897 map = isl_map_cow(map);
1898 if (!map)
1899 return NULL;
1900 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1902 for (i = 0; i < map->n; ++i) {
1903 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1904 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1905 if (!map->p[i])
1906 goto error;
1908 map = isl_map_drop(map, type, first, n);
1909 return map;
1910 error:
1911 isl_map_free(map);
1912 return NULL;
1915 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1916 enum isl_dim_type type, unsigned first, unsigned n)
1918 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1921 /* Project out n inputs starting at first using Fourier-Motzkin */
1922 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1923 unsigned first, unsigned n)
1925 return isl_map_remove_dims(map, isl_dim_in, first, n);
1928 static void dump_term(struct isl_basic_map *bmap,
1929 isl_int c, int pos, FILE *out)
1931 const char *name;
1932 unsigned in = isl_basic_map_n_in(bmap);
1933 unsigned dim = in + isl_basic_map_n_out(bmap);
1934 unsigned nparam = isl_basic_map_n_param(bmap);
1935 if (!pos)
1936 isl_int_print(out, c, 0);
1937 else {
1938 if (!isl_int_is_one(c))
1939 isl_int_print(out, c, 0);
1940 if (pos < 1 + nparam) {
1941 name = isl_space_get_dim_name(bmap->dim,
1942 isl_dim_param, pos - 1);
1943 if (name)
1944 fprintf(out, "%s", name);
1945 else
1946 fprintf(out, "p%d", pos - 1);
1947 } else if (pos < 1 + nparam + in)
1948 fprintf(out, "i%d", pos - 1 - nparam);
1949 else if (pos < 1 + nparam + dim)
1950 fprintf(out, "o%d", pos - 1 - nparam - in);
1951 else
1952 fprintf(out, "e%d", pos - 1 - nparam - dim);
1956 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1957 int sign, FILE *out)
1959 int i;
1960 int first;
1961 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1962 isl_int v;
1964 isl_int_init(v);
1965 for (i = 0, first = 1; i < len; ++i) {
1966 if (isl_int_sgn(c[i]) * sign <= 0)
1967 continue;
1968 if (!first)
1969 fprintf(out, " + ");
1970 first = 0;
1971 isl_int_abs(v, c[i]);
1972 dump_term(bmap, v, i, out);
1974 isl_int_clear(v);
1975 if (first)
1976 fprintf(out, "0");
1979 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1980 const char *op, FILE *out, int indent)
1982 int i;
1984 fprintf(out, "%*s", indent, "");
1986 dump_constraint_sign(bmap, c, 1, out);
1987 fprintf(out, " %s ", op);
1988 dump_constraint_sign(bmap, c, -1, out);
1990 fprintf(out, "\n");
1992 for (i = bmap->n_div; i < bmap->extra; ++i) {
1993 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
1994 continue;
1995 fprintf(out, "%*s", indent, "");
1996 fprintf(out, "ERROR: unused div coefficient not zero\n");
1997 abort();
2001 static void dump_constraints(struct isl_basic_map *bmap,
2002 isl_int **c, unsigned n,
2003 const char *op, FILE *out, int indent)
2005 int i;
2007 for (i = 0; i < n; ++i)
2008 dump_constraint(bmap, c[i], op, out, indent);
2011 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2013 int j;
2014 int first = 1;
2015 unsigned total = isl_basic_map_total_dim(bmap);
2017 for (j = 0; j < 1 + total; ++j) {
2018 if (isl_int_is_zero(exp[j]))
2019 continue;
2020 if (!first && isl_int_is_pos(exp[j]))
2021 fprintf(out, "+");
2022 dump_term(bmap, exp[j], j, out);
2023 first = 0;
2027 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2029 int i;
2031 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2032 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2034 for (i = 0; i < bmap->n_div; ++i) {
2035 fprintf(out, "%*s", indent, "");
2036 fprintf(out, "e%d = [(", i);
2037 dump_affine(bmap, bmap->div[i]+1, out);
2038 fprintf(out, ")/");
2039 isl_int_print(out, bmap->div[i][0], 0);
2040 fprintf(out, "]\n");
2044 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2045 FILE *out, int indent)
2047 if (!bset) {
2048 fprintf(out, "null basic set\n");
2049 return;
2052 fprintf(out, "%*s", indent, "");
2053 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2054 bset->ref, bset->dim->nparam, bset->dim->n_out,
2055 bset->extra, bset->flags);
2056 dump((struct isl_basic_map *)bset, out, indent);
2059 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2060 FILE *out, int indent)
2062 if (!bmap) {
2063 fprintf(out, "null basic map\n");
2064 return;
2067 fprintf(out, "%*s", indent, "");
2068 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2069 "flags: %x, n_name: %d\n",
2070 bmap->ref,
2071 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2072 bmap->extra, bmap->flags, bmap->dim->n_id);
2073 dump(bmap, out, indent);
2076 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2078 unsigned total;
2079 if (!bmap)
2080 return -1;
2081 total = isl_basic_map_total_dim(bmap);
2082 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2083 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2084 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2085 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2086 return 0;
2089 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2090 unsigned flags)
2092 struct isl_set *set;
2094 if (!dim)
2095 return NULL;
2096 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2097 isl_assert(dim->ctx, n >= 0, goto error);
2098 set = isl_alloc(dim->ctx, struct isl_set,
2099 sizeof(struct isl_set) +
2100 (n - 1) * sizeof(struct isl_basic_set *));
2101 if (!set)
2102 goto error;
2104 set->ctx = dim->ctx;
2105 isl_ctx_ref(set->ctx);
2106 set->ref = 1;
2107 set->size = n;
2108 set->n = 0;
2109 set->dim = dim;
2110 set->flags = flags;
2111 return set;
2112 error:
2113 isl_space_free(dim);
2114 return NULL;
2117 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2118 unsigned nparam, unsigned dim, int n, unsigned flags)
2120 struct isl_set *set;
2121 isl_space *dims;
2123 dims = isl_space_alloc(ctx, nparam, 0, dim);
2124 if (!dims)
2125 return NULL;
2127 set = isl_set_alloc_space(dims, n, flags);
2128 return set;
2131 /* Make sure "map" has room for at least "n" more basic maps.
2133 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2135 int i;
2136 struct isl_map *grown = NULL;
2138 if (!map)
2139 return NULL;
2140 isl_assert(map->ctx, n >= 0, goto error);
2141 if (map->n + n <= map->size)
2142 return map;
2143 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2144 if (!grown)
2145 goto error;
2146 for (i = 0; i < map->n; ++i) {
2147 grown->p[i] = isl_basic_map_copy(map->p[i]);
2148 if (!grown->p[i])
2149 goto error;
2150 grown->n++;
2152 isl_map_free(map);
2153 return grown;
2154 error:
2155 isl_map_free(grown);
2156 isl_map_free(map);
2157 return NULL;
2160 /* Make sure "set" has room for at least "n" more basic sets.
2162 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2164 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2167 struct isl_set *isl_set_dup(struct isl_set *set)
2169 int i;
2170 struct isl_set *dup;
2172 if (!set)
2173 return NULL;
2175 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2176 if (!dup)
2177 return NULL;
2178 for (i = 0; i < set->n; ++i)
2179 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2180 return dup;
2183 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2185 return isl_map_from_basic_map(bset);
2188 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2190 struct isl_map *map;
2192 if (!bmap)
2193 return NULL;
2195 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2196 return isl_map_add_basic_map(map, bmap);
2199 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2200 __isl_take isl_basic_set *bset)
2202 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2203 (struct isl_basic_map *)bset);
2206 void isl_set_free(struct isl_set *set)
2208 int i;
2210 if (!set)
2211 return;
2213 if (--set->ref > 0)
2214 return;
2216 isl_ctx_deref(set->ctx);
2217 for (i = 0; i < set->n; ++i)
2218 isl_basic_set_free(set->p[i]);
2219 isl_space_free(set->dim);
2220 free(set);
2223 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2225 int i;
2227 if (!set) {
2228 fprintf(out, "null set\n");
2229 return;
2232 fprintf(out, "%*s", indent, "");
2233 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2234 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2235 set->flags);
2236 for (i = 0; i < set->n; ++i) {
2237 fprintf(out, "%*s", indent, "");
2238 fprintf(out, "basic set %d:\n", i);
2239 isl_basic_set_print_internal(set->p[i], out, indent+4);
2243 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2245 int i;
2247 if (!map) {
2248 fprintf(out, "null map\n");
2249 return;
2252 fprintf(out, "%*s", indent, "");
2253 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2254 "flags: %x, n_name: %d\n",
2255 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2256 map->dim->n_out, map->flags, map->dim->n_id);
2257 for (i = 0; i < map->n; ++i) {
2258 fprintf(out, "%*s", indent, "");
2259 fprintf(out, "basic map %d:\n", i);
2260 isl_basic_map_print_internal(map->p[i], out, indent+4);
2264 struct isl_basic_map *isl_basic_map_intersect_domain(
2265 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2267 struct isl_basic_map *bmap_domain;
2269 if (!bmap || !bset)
2270 goto error;
2272 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2273 bset->dim, isl_dim_param), goto error);
2275 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2276 isl_assert(bset->ctx,
2277 isl_basic_map_compatible_domain(bmap, bset), goto error);
2279 bmap = isl_basic_map_cow(bmap);
2280 if (!bmap)
2281 goto error;
2282 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2283 bset->n_div, bset->n_eq, bset->n_ineq);
2284 bmap_domain = isl_basic_map_from_domain(bset);
2285 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2287 bmap = isl_basic_map_simplify(bmap);
2288 return isl_basic_map_finalize(bmap);
2289 error:
2290 isl_basic_map_free(bmap);
2291 isl_basic_set_free(bset);
2292 return NULL;
2295 struct isl_basic_map *isl_basic_map_intersect_range(
2296 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2298 struct isl_basic_map *bmap_range;
2300 if (!bmap || !bset)
2301 goto error;
2303 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2304 bset->dim, isl_dim_param), goto error);
2306 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2307 isl_assert(bset->ctx,
2308 isl_basic_map_compatible_range(bmap, bset), goto error);
2310 if (isl_basic_set_is_universe(bset)) {
2311 isl_basic_set_free(bset);
2312 return bmap;
2315 bmap = isl_basic_map_cow(bmap);
2316 if (!bmap)
2317 goto error;
2318 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2319 bset->n_div, bset->n_eq, bset->n_ineq);
2320 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2321 bmap = add_constraints(bmap, bmap_range, 0, 0);
2323 bmap = isl_basic_map_simplify(bmap);
2324 return isl_basic_map_finalize(bmap);
2325 error:
2326 isl_basic_map_free(bmap);
2327 isl_basic_set_free(bset);
2328 return NULL;
2331 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2333 int i;
2334 unsigned total;
2335 isl_int s;
2337 total = 1 + isl_basic_map_total_dim(bmap);
2338 if (total != vec->size)
2339 return -1;
2341 isl_int_init(s);
2343 for (i = 0; i < bmap->n_eq; ++i) {
2344 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2345 if (!isl_int_is_zero(s)) {
2346 isl_int_clear(s);
2347 return 0;
2351 for (i = 0; i < bmap->n_ineq; ++i) {
2352 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2353 if (isl_int_is_neg(s)) {
2354 isl_int_clear(s);
2355 return 0;
2359 isl_int_clear(s);
2361 return 1;
2364 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2366 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2369 struct isl_basic_map *isl_basic_map_intersect(
2370 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2372 struct isl_vec *sample = NULL;
2374 if (!bmap1 || !bmap2)
2375 goto error;
2377 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2378 bmap2->dim, isl_dim_param), goto error);
2379 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2380 isl_space_dim(bmap1->dim, isl_dim_param) &&
2381 isl_space_dim(bmap2->dim, isl_dim_all) !=
2382 isl_space_dim(bmap2->dim, isl_dim_param))
2383 return isl_basic_map_intersect(bmap2, bmap1);
2385 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2386 isl_space_dim(bmap2->dim, isl_dim_param))
2387 isl_assert(bmap1->ctx,
2388 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2390 if (bmap1->sample &&
2391 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2392 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2393 sample = isl_vec_copy(bmap1->sample);
2394 else if (bmap2->sample &&
2395 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2396 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2397 sample = isl_vec_copy(bmap2->sample);
2399 bmap1 = isl_basic_map_cow(bmap1);
2400 if (!bmap1)
2401 goto error;
2402 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2403 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2404 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2406 if (!bmap1)
2407 isl_vec_free(sample);
2408 else if (sample) {
2409 isl_vec_free(bmap1->sample);
2410 bmap1->sample = sample;
2413 bmap1 = isl_basic_map_simplify(bmap1);
2414 return isl_basic_map_finalize(bmap1);
2415 error:
2416 if (sample)
2417 isl_vec_free(sample);
2418 isl_basic_map_free(bmap1);
2419 isl_basic_map_free(bmap2);
2420 return NULL;
2423 struct isl_basic_set *isl_basic_set_intersect(
2424 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2426 return (struct isl_basic_set *)
2427 isl_basic_map_intersect(
2428 (struct isl_basic_map *)bset1,
2429 (struct isl_basic_map *)bset2);
2432 /* Special case of isl_map_intersect, where both map1 and map2
2433 * are convex, without any divs and such that either map1 or map2
2434 * contains a single constraint. This constraint is then simply
2435 * added to the other map.
2437 static __isl_give isl_map *map_intersect_add_constraint(
2438 __isl_take isl_map *map1, __isl_take isl_map *map2)
2440 isl_assert(map1->ctx, map1->n == 1, goto error);
2441 isl_assert(map2->ctx, map1->n == 1, goto error);
2442 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2443 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2445 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2446 return isl_map_intersect(map2, map1);
2448 isl_assert(map2->ctx,
2449 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2451 map1 = isl_map_cow(map1);
2452 if (!map1)
2453 goto error;
2454 if (isl_map_plain_is_empty(map1)) {
2455 isl_map_free(map2);
2456 return map1;
2458 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2459 if (map2->p[0]->n_eq == 1)
2460 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2461 else
2462 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2463 map2->p[0]->ineq[0]);
2465 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2466 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2467 if (!map1->p[0])
2468 goto error;
2470 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2471 isl_basic_map_free(map1->p[0]);
2472 map1->n = 0;
2475 isl_map_free(map2);
2477 return map1;
2478 error:
2479 isl_map_free(map1);
2480 isl_map_free(map2);
2481 return NULL;
2484 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2485 __isl_take isl_map *map2)
2487 unsigned flags = 0;
2488 struct isl_map *result;
2489 int i, j;
2491 if (!map1 || !map2)
2492 goto error;
2494 if (isl_map_plain_is_empty(map1) &&
2495 isl_space_is_equal(map1->dim, map2->dim)) {
2496 isl_map_free(map2);
2497 return map1;
2499 if (isl_map_plain_is_empty(map2) &&
2500 isl_space_is_equal(map1->dim, map2->dim)) {
2501 isl_map_free(map1);
2502 return map2;
2505 if (map1->n == 1 && map2->n == 1 &&
2506 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2507 isl_space_is_equal(map1->dim, map2->dim) &&
2508 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2509 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2510 return map_intersect_add_constraint(map1, map2);
2511 if (isl_map_is_params(map1) && !isl_map_is_params(map2))
2512 return isl_map_intersect(map2, map1);
2513 if (isl_space_dim(map1->dim, isl_dim_all) ==
2514 isl_space_dim(map1->dim, isl_dim_param) &&
2515 isl_space_dim(map2->dim, isl_dim_all) !=
2516 isl_space_dim(map2->dim, isl_dim_param))
2517 return isl_map_intersect(map2, map1);
2519 if (isl_space_dim(map2->dim, isl_dim_all) !=
2520 isl_space_dim(map2->dim, isl_dim_param))
2521 isl_assert(map1->ctx,
2522 isl_space_is_equal(map1->dim, map2->dim), goto error);
2524 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2525 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2526 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2528 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2529 map1->n * map2->n, flags);
2530 if (!result)
2531 goto error;
2532 for (i = 0; i < map1->n; ++i)
2533 for (j = 0; j < map2->n; ++j) {
2534 struct isl_basic_map *part;
2535 part = isl_basic_map_intersect(
2536 isl_basic_map_copy(map1->p[i]),
2537 isl_basic_map_copy(map2->p[j]));
2538 if (isl_basic_map_is_empty(part))
2539 isl_basic_map_free(part);
2540 else
2541 result = isl_map_add_basic_map(result, part);
2542 if (!result)
2543 goto error;
2545 isl_map_free(map1);
2546 isl_map_free(map2);
2547 return result;
2548 error:
2549 isl_map_free(map1);
2550 isl_map_free(map2);
2551 return NULL;
2554 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2555 __isl_take isl_map *map2)
2557 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2560 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2562 return (struct isl_set *)
2563 isl_map_intersect((struct isl_map *)set1,
2564 (struct isl_map *)set2);
2567 /* The current implementation of isl_map_intersect accepts intersections
2568 * with parameter domains, so we can just call that for now.
2570 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2571 __isl_take isl_set *params)
2573 return isl_map_intersect(map, params);
2576 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2577 __isl_take isl_map *map2)
2579 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2582 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2583 __isl_take isl_set *params)
2585 return isl_map_intersect_params(set, params);
2588 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2590 isl_space *dim;
2591 struct isl_basic_set *bset;
2592 unsigned in;
2594 if (!bmap)
2595 return NULL;
2596 bmap = isl_basic_map_cow(bmap);
2597 if (!bmap)
2598 return NULL;
2599 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2600 in = isl_basic_map_n_in(bmap);
2601 bset = isl_basic_set_from_basic_map(bmap);
2602 bset = isl_basic_set_swap_vars(bset, in);
2603 return isl_basic_map_from_basic_set(bset, dim);
2606 static __isl_give isl_basic_map *basic_map_space_reset(
2607 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2609 isl_space *space;
2611 if (!isl_space_is_named_or_nested(bmap->dim, type))
2612 return bmap;
2614 space = isl_basic_map_get_space(bmap);
2615 space = isl_space_reset(space, type);
2616 bmap = isl_basic_map_reset_space(bmap, space);
2617 return bmap;
2620 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2621 enum isl_dim_type type, unsigned pos, unsigned n)
2623 isl_space *res_dim;
2624 struct isl_basic_map *res;
2625 struct isl_dim_map *dim_map;
2626 unsigned total, off;
2627 enum isl_dim_type t;
2629 if (n == 0)
2630 return basic_map_space_reset(bmap, type);
2632 if (!bmap)
2633 return NULL;
2635 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2637 total = isl_basic_map_total_dim(bmap) + n;
2638 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2639 off = 0;
2640 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2641 if (t != type) {
2642 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2643 } else {
2644 unsigned size = isl_basic_map_dim(bmap, t);
2645 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2646 0, pos, off);
2647 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2648 pos, size - pos, off + pos + n);
2650 off += isl_space_dim(res_dim, t);
2652 isl_dim_map_div(dim_map, bmap, off);
2654 res = isl_basic_map_alloc_space(res_dim,
2655 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2656 if (isl_basic_map_is_rational(bmap))
2657 res = isl_basic_map_set_rational(res);
2658 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2659 return isl_basic_map_finalize(res);
2662 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2663 enum isl_dim_type type, unsigned n)
2665 if (!bmap)
2666 return NULL;
2667 return isl_basic_map_insert(bmap, type,
2668 isl_basic_map_dim(bmap, type), n);
2671 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2672 enum isl_dim_type type, unsigned n)
2674 if (!bset)
2675 return NULL;
2676 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2677 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2678 error:
2679 isl_basic_set_free(bset);
2680 return NULL;
2683 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2684 enum isl_dim_type type)
2686 isl_space *space;
2688 if (!isl_space_is_named_or_nested(map->dim, type))
2689 return map;
2691 space = isl_map_get_space(map);
2692 space = isl_space_reset(space, type);
2693 map = isl_map_reset_space(map, space);
2694 return map;
2697 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2698 enum isl_dim_type type, unsigned pos, unsigned n)
2700 int i;
2702 if (n == 0)
2703 return map_space_reset(map, type);
2705 map = isl_map_cow(map);
2706 if (!map)
2707 return NULL;
2709 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2710 if (!map->dim)
2711 goto error;
2713 for (i = 0; i < map->n; ++i) {
2714 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2715 if (!map->p[i])
2716 goto error;
2719 return map;
2720 error:
2721 isl_map_free(map);
2722 return NULL;
2725 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2726 enum isl_dim_type type, unsigned pos, unsigned n)
2728 return isl_map_insert_dims(set, type, pos, n);
2731 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2732 enum isl_dim_type type, unsigned n)
2734 if (!map)
2735 return NULL;
2736 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2739 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2740 enum isl_dim_type type, unsigned n)
2742 if (!set)
2743 return NULL;
2744 isl_assert(set->ctx, type != isl_dim_in, goto error);
2745 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2746 error:
2747 isl_set_free(set);
2748 return NULL;
2751 __isl_give isl_basic_map *isl_basic_map_move_dims(
2752 __isl_take isl_basic_map *bmap,
2753 enum isl_dim_type dst_type, unsigned dst_pos,
2754 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2756 struct isl_dim_map *dim_map;
2757 struct isl_basic_map *res;
2758 enum isl_dim_type t;
2759 unsigned total, off;
2761 if (!bmap)
2762 return NULL;
2763 if (n == 0)
2764 return bmap;
2766 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2767 goto error);
2769 if (dst_type == src_type && dst_pos == src_pos)
2770 return bmap;
2772 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2774 if (pos(bmap->dim, dst_type) + dst_pos ==
2775 pos(bmap->dim, src_type) + src_pos +
2776 ((src_type < dst_type) ? n : 0)) {
2777 bmap = isl_basic_map_cow(bmap);
2778 if (!bmap)
2779 return NULL;
2781 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2782 src_type, src_pos, n);
2783 if (!bmap->dim)
2784 goto error;
2786 bmap = isl_basic_map_finalize(bmap);
2788 return bmap;
2791 total = isl_basic_map_total_dim(bmap);
2792 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2794 off = 0;
2795 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2796 unsigned size = isl_space_dim(bmap->dim, t);
2797 if (t == dst_type) {
2798 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2799 0, dst_pos, off);
2800 off += dst_pos;
2801 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2802 src_pos, n, off);
2803 off += n;
2804 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2805 dst_pos, size - dst_pos, off);
2806 off += size - dst_pos;
2807 } else if (t == src_type) {
2808 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2809 0, src_pos, off);
2810 off += src_pos;
2811 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2812 src_pos + n, size - src_pos - n, off);
2813 off += size - src_pos - n;
2814 } else {
2815 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2816 off += size;
2819 isl_dim_map_div(dim_map, bmap, off);
2821 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2822 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2823 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2825 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2826 src_type, src_pos, n);
2827 if (!bmap->dim)
2828 goto error;
2830 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2831 bmap = isl_basic_map_gauss(bmap, NULL);
2832 bmap = isl_basic_map_finalize(bmap);
2834 return bmap;
2835 error:
2836 isl_basic_map_free(bmap);
2837 return NULL;
2840 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2841 enum isl_dim_type dst_type, unsigned dst_pos,
2842 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2844 return (isl_basic_set *)isl_basic_map_move_dims(
2845 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2848 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2849 enum isl_dim_type dst_type, unsigned dst_pos,
2850 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2852 if (!set)
2853 return NULL;
2854 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2855 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2856 src_type, src_pos, n);
2857 error:
2858 isl_set_free(set);
2859 return NULL;
2862 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2863 enum isl_dim_type dst_type, unsigned dst_pos,
2864 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2866 int i;
2868 if (!map)
2869 return NULL;
2870 if (n == 0)
2871 return map;
2873 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2874 goto error);
2876 if (dst_type == src_type && dst_pos == src_pos)
2877 return map;
2879 isl_assert(map->ctx, dst_type != src_type, goto error);
2881 map = isl_map_cow(map);
2882 if (!map)
2883 return NULL;
2885 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2886 if (!map->dim)
2887 goto error;
2889 for (i = 0; i < map->n; ++i) {
2890 map->p[i] = isl_basic_map_move_dims(map->p[i],
2891 dst_type, dst_pos,
2892 src_type, src_pos, n);
2893 if (!map->p[i])
2894 goto error;
2897 return map;
2898 error:
2899 isl_map_free(map);
2900 return NULL;
2903 /* Move the specified dimensions to the last columns right before
2904 * the divs. Don't change the dimension specification of bmap.
2905 * That's the responsibility of the caller.
2907 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2908 enum isl_dim_type type, unsigned first, unsigned n)
2910 struct isl_dim_map *dim_map;
2911 struct isl_basic_map *res;
2912 enum isl_dim_type t;
2913 unsigned total, off;
2915 if (!bmap)
2916 return NULL;
2917 if (pos(bmap->dim, type) + first + n ==
2918 1 + isl_space_dim(bmap->dim, isl_dim_all))
2919 return bmap;
2921 total = isl_basic_map_total_dim(bmap);
2922 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2924 off = 0;
2925 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2926 unsigned size = isl_space_dim(bmap->dim, t);
2927 if (t == type) {
2928 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2929 0, first, off);
2930 off += first;
2931 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2932 first, n, total - bmap->n_div - n);
2933 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2934 first + n, size - (first + n), off);
2935 off += size - (first + n);
2936 } else {
2937 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2938 off += size;
2941 isl_dim_map_div(dim_map, bmap, off + n);
2943 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2944 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2945 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2946 return res;
2949 /* Turn the n dimensions of type type, starting at first
2950 * into existentially quantified variables.
2952 __isl_give isl_basic_map *isl_basic_map_project_out(
2953 __isl_take isl_basic_map *bmap,
2954 enum isl_dim_type type, unsigned first, unsigned n)
2956 int i;
2957 size_t row_size;
2958 isl_int **new_div;
2959 isl_int *old;
2961 if (n == 0)
2962 return bmap;
2964 if (!bmap)
2965 return NULL;
2967 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2968 return isl_basic_map_remove_dims(bmap, type, first, n);
2970 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2971 goto error);
2973 bmap = move_last(bmap, type, first, n);
2974 bmap = isl_basic_map_cow(bmap);
2975 if (!bmap)
2976 return NULL;
2978 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
2979 old = bmap->block2.data;
2980 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2981 (bmap->extra + n) * (1 + row_size));
2982 if (!bmap->block2.data)
2983 goto error;
2984 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2985 if (!new_div)
2986 goto error;
2987 for (i = 0; i < n; ++i) {
2988 new_div[i] = bmap->block2.data +
2989 (bmap->extra + i) * (1 + row_size);
2990 isl_seq_clr(new_div[i], 1 + row_size);
2992 for (i = 0; i < bmap->extra; ++i)
2993 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2994 free(bmap->div);
2995 bmap->div = new_div;
2996 bmap->n_div += n;
2997 bmap->extra += n;
2999 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3000 if (!bmap->dim)
3001 goto error;
3002 bmap = isl_basic_map_simplify(bmap);
3003 bmap = isl_basic_map_drop_redundant_divs(bmap);
3004 return isl_basic_map_finalize(bmap);
3005 error:
3006 isl_basic_map_free(bmap);
3007 return NULL;
3010 /* Turn the n dimensions of type type, starting at first
3011 * into existentially quantified variables.
3013 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3014 enum isl_dim_type type, unsigned first, unsigned n)
3016 return (isl_basic_set *)isl_basic_map_project_out(
3017 (isl_basic_map *)bset, type, first, n);
3020 /* Turn the n dimensions of type type, starting at first
3021 * into existentially quantified variables.
3023 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3024 enum isl_dim_type type, unsigned first, unsigned n)
3026 int i;
3028 if (!map)
3029 return NULL;
3031 if (n == 0)
3032 return map;
3034 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3036 map = isl_map_cow(map);
3037 if (!map)
3038 return NULL;
3040 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3041 if (!map->dim)
3042 goto error;
3044 for (i = 0; i < map->n; ++i) {
3045 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3046 if (!map->p[i])
3047 goto error;
3050 return map;
3051 error:
3052 isl_map_free(map);
3053 return NULL;
3056 /* Turn the n dimensions of type type, starting at first
3057 * into existentially quantified variables.
3059 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3060 enum isl_dim_type type, unsigned first, unsigned n)
3062 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3065 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3067 int i, j;
3069 for (i = 0; i < n; ++i) {
3070 j = isl_basic_map_alloc_div(bmap);
3071 if (j < 0)
3072 goto error;
3073 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3075 return bmap;
3076 error:
3077 isl_basic_map_free(bmap);
3078 return NULL;
3081 struct isl_basic_map *isl_basic_map_apply_range(
3082 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3084 isl_space *dim_result = NULL;
3085 struct isl_basic_map *bmap;
3086 unsigned n_in, n_out, n, nparam, total, pos;
3087 struct isl_dim_map *dim_map1, *dim_map2;
3089 if (!bmap1 || !bmap2)
3090 goto error;
3092 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3093 isl_space_copy(bmap2->dim));
3095 n_in = isl_basic_map_n_in(bmap1);
3096 n_out = isl_basic_map_n_out(bmap2);
3097 n = isl_basic_map_n_out(bmap1);
3098 nparam = isl_basic_map_n_param(bmap1);
3100 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3101 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3102 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3103 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3104 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3105 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3106 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3107 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3108 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3109 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3110 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3112 bmap = isl_basic_map_alloc_space(dim_result,
3113 bmap1->n_div + bmap2->n_div + n,
3114 bmap1->n_eq + bmap2->n_eq,
3115 bmap1->n_ineq + bmap2->n_ineq);
3116 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3117 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3118 bmap = add_divs(bmap, n);
3119 bmap = isl_basic_map_simplify(bmap);
3120 bmap = isl_basic_map_drop_redundant_divs(bmap);
3121 return isl_basic_map_finalize(bmap);
3122 error:
3123 isl_basic_map_free(bmap1);
3124 isl_basic_map_free(bmap2);
3125 return NULL;
3128 struct isl_basic_set *isl_basic_set_apply(
3129 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3131 if (!bset || !bmap)
3132 goto error;
3134 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3135 goto error);
3137 return (struct isl_basic_set *)
3138 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3139 error:
3140 isl_basic_set_free(bset);
3141 isl_basic_map_free(bmap);
3142 return NULL;
3145 struct isl_basic_map *isl_basic_map_apply_domain(
3146 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3148 if (!bmap1 || !bmap2)
3149 goto error;
3151 isl_assert(bmap1->ctx,
3152 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3153 isl_assert(bmap1->ctx,
3154 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3155 goto error);
3157 bmap1 = isl_basic_map_reverse(bmap1);
3158 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3159 return isl_basic_map_reverse(bmap1);
3160 error:
3161 isl_basic_map_free(bmap1);
3162 isl_basic_map_free(bmap2);
3163 return NULL;
3166 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3167 * A \cap B -> f(A) + f(B)
3169 struct isl_basic_map *isl_basic_map_sum(
3170 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3172 unsigned n_in, n_out, nparam, total, pos;
3173 struct isl_basic_map *bmap = NULL;
3174 struct isl_dim_map *dim_map1, *dim_map2;
3175 int i;
3177 if (!bmap1 || !bmap2)
3178 goto error;
3180 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3181 goto error);
3183 nparam = isl_basic_map_n_param(bmap1);
3184 n_in = isl_basic_map_n_in(bmap1);
3185 n_out = isl_basic_map_n_out(bmap1);
3187 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3188 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3189 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3190 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3191 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3192 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3193 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3194 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3195 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3196 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3197 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3199 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3200 bmap1->n_div + bmap2->n_div + 2 * n_out,
3201 bmap1->n_eq + bmap2->n_eq + n_out,
3202 bmap1->n_ineq + bmap2->n_ineq);
3203 for (i = 0; i < n_out; ++i) {
3204 int j = isl_basic_map_alloc_equality(bmap);
3205 if (j < 0)
3206 goto error;
3207 isl_seq_clr(bmap->eq[j], 1+total);
3208 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3209 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3210 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3212 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3213 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3214 bmap = add_divs(bmap, 2 * n_out);
3216 bmap = isl_basic_map_simplify(bmap);
3217 return isl_basic_map_finalize(bmap);
3218 error:
3219 isl_basic_map_free(bmap);
3220 isl_basic_map_free(bmap1);
3221 isl_basic_map_free(bmap2);
3222 return NULL;
3225 /* Given two maps A -> f(A) and B -> g(B), construct a map
3226 * A \cap B -> f(A) + f(B)
3228 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3230 struct isl_map *result;
3231 int i, j;
3233 if (!map1 || !map2)
3234 goto error;
3236 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3238 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3239 map1->n * map2->n, 0);
3240 if (!result)
3241 goto error;
3242 for (i = 0; i < map1->n; ++i)
3243 for (j = 0; j < map2->n; ++j) {
3244 struct isl_basic_map *part;
3245 part = isl_basic_map_sum(
3246 isl_basic_map_copy(map1->p[i]),
3247 isl_basic_map_copy(map2->p[j]));
3248 if (isl_basic_map_is_empty(part))
3249 isl_basic_map_free(part);
3250 else
3251 result = isl_map_add_basic_map(result, part);
3252 if (!result)
3253 goto error;
3255 isl_map_free(map1);
3256 isl_map_free(map2);
3257 return result;
3258 error:
3259 isl_map_free(map1);
3260 isl_map_free(map2);
3261 return NULL;
3264 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3265 __isl_take isl_set *set2)
3267 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3270 /* Given a basic map A -> f(A), construct A -> -f(A).
3272 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3274 int i, j;
3275 unsigned off, n;
3277 bmap = isl_basic_map_cow(bmap);
3278 if (!bmap)
3279 return NULL;
3281 n = isl_basic_map_dim(bmap, isl_dim_out);
3282 off = isl_basic_map_offset(bmap, isl_dim_out);
3283 for (i = 0; i < bmap->n_eq; ++i)
3284 for (j = 0; j < n; ++j)
3285 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3286 for (i = 0; i < bmap->n_ineq; ++i)
3287 for (j = 0; j < n; ++j)
3288 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3289 for (i = 0; i < bmap->n_div; ++i)
3290 for (j = 0; j < n; ++j)
3291 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3292 bmap = isl_basic_map_gauss(bmap, NULL);
3293 return isl_basic_map_finalize(bmap);
3296 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3298 return isl_basic_map_neg(bset);
3301 /* Given a map A -> f(A), construct A -> -f(A).
3303 struct isl_map *isl_map_neg(struct isl_map *map)
3305 int i;
3307 map = isl_map_cow(map);
3308 if (!map)
3309 return NULL;
3311 for (i = 0; i < map->n; ++i) {
3312 map->p[i] = isl_basic_map_neg(map->p[i]);
3313 if (!map->p[i])
3314 goto error;
3317 return map;
3318 error:
3319 isl_map_free(map);
3320 return NULL;
3323 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3325 return (isl_set *)isl_map_neg((isl_map *)set);
3328 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3329 * A -> floor(f(A)/d).
3331 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3332 isl_int d)
3334 unsigned n_in, n_out, nparam, total, pos;
3335 struct isl_basic_map *result = NULL;
3336 struct isl_dim_map *dim_map;
3337 int i;
3339 if (!bmap)
3340 return NULL;
3342 nparam = isl_basic_map_n_param(bmap);
3343 n_in = isl_basic_map_n_in(bmap);
3344 n_out = isl_basic_map_n_out(bmap);
3346 total = nparam + n_in + n_out + bmap->n_div + n_out;
3347 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3348 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3349 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3350 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3351 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3353 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3354 bmap->n_div + n_out,
3355 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3356 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3357 result = add_divs(result, n_out);
3358 for (i = 0; i < n_out; ++i) {
3359 int j;
3360 j = isl_basic_map_alloc_inequality(result);
3361 if (j < 0)
3362 goto error;
3363 isl_seq_clr(result->ineq[j], 1+total);
3364 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3365 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3366 j = isl_basic_map_alloc_inequality(result);
3367 if (j < 0)
3368 goto error;
3369 isl_seq_clr(result->ineq[j], 1+total);
3370 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3371 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3372 isl_int_sub_ui(result->ineq[j][0], d, 1);
3375 result = isl_basic_map_simplify(result);
3376 return isl_basic_map_finalize(result);
3377 error:
3378 isl_basic_map_free(result);
3379 return NULL;
3382 /* Given a map A -> f(A) and an integer d, construct a map
3383 * A -> floor(f(A)/d).
3385 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3387 int i;
3389 map = isl_map_cow(map);
3390 if (!map)
3391 return NULL;
3393 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3394 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3395 for (i = 0; i < map->n; ++i) {
3396 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3397 if (!map->p[i])
3398 goto error;
3401 return map;
3402 error:
3403 isl_map_free(map);
3404 return NULL;
3407 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3409 int i;
3410 unsigned nparam;
3411 unsigned n_in;
3413 i = isl_basic_map_alloc_equality(bmap);
3414 if (i < 0)
3415 goto error;
3416 nparam = isl_basic_map_n_param(bmap);
3417 n_in = isl_basic_map_n_in(bmap);
3418 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3419 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3420 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3421 return isl_basic_map_finalize(bmap);
3422 error:
3423 isl_basic_map_free(bmap);
3424 return NULL;
3427 /* Add a constraints to "bmap" expressing i_pos < o_pos
3429 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3431 int i;
3432 unsigned nparam;
3433 unsigned n_in;
3435 i = isl_basic_map_alloc_inequality(bmap);
3436 if (i < 0)
3437 goto error;
3438 nparam = isl_basic_map_n_param(bmap);
3439 n_in = isl_basic_map_n_in(bmap);
3440 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3441 isl_int_set_si(bmap->ineq[i][0], -1);
3442 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3443 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3444 return isl_basic_map_finalize(bmap);
3445 error:
3446 isl_basic_map_free(bmap);
3447 return NULL;
3450 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3452 static __isl_give isl_basic_map *var_less_or_equal(
3453 __isl_take isl_basic_map *bmap, unsigned pos)
3455 int i;
3456 unsigned nparam;
3457 unsigned n_in;
3459 i = isl_basic_map_alloc_inequality(bmap);
3460 if (i < 0)
3461 goto error;
3462 nparam = isl_basic_map_n_param(bmap);
3463 n_in = isl_basic_map_n_in(bmap);
3464 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3465 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3466 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3467 return isl_basic_map_finalize(bmap);
3468 error:
3469 isl_basic_map_free(bmap);
3470 return NULL;
3473 /* Add a constraints to "bmap" expressing i_pos > o_pos
3475 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3477 int i;
3478 unsigned nparam;
3479 unsigned n_in;
3481 i = isl_basic_map_alloc_inequality(bmap);
3482 if (i < 0)
3483 goto error;
3484 nparam = isl_basic_map_n_param(bmap);
3485 n_in = isl_basic_map_n_in(bmap);
3486 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3487 isl_int_set_si(bmap->ineq[i][0], -1);
3488 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3489 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3490 return isl_basic_map_finalize(bmap);
3491 error:
3492 isl_basic_map_free(bmap);
3493 return NULL;
3496 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3498 static __isl_give isl_basic_map *var_more_or_equal(
3499 __isl_take isl_basic_map *bmap, unsigned pos)
3501 int i;
3502 unsigned nparam;
3503 unsigned n_in;
3505 i = isl_basic_map_alloc_inequality(bmap);
3506 if (i < 0)
3507 goto error;
3508 nparam = isl_basic_map_n_param(bmap);
3509 n_in = isl_basic_map_n_in(bmap);
3510 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3511 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3512 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3513 return isl_basic_map_finalize(bmap);
3514 error:
3515 isl_basic_map_free(bmap);
3516 return NULL;
3519 __isl_give isl_basic_map *isl_basic_map_equal(
3520 __isl_take isl_space *dim, unsigned n_equal)
3522 int i;
3523 struct isl_basic_map *bmap;
3524 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3525 if (!bmap)
3526 return NULL;
3527 for (i = 0; i < n_equal && bmap; ++i)
3528 bmap = var_equal(bmap, i);
3529 return isl_basic_map_finalize(bmap);
3532 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3534 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3535 unsigned pos)
3537 int i;
3538 struct isl_basic_map *bmap;
3539 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3540 if (!bmap)
3541 return NULL;
3542 for (i = 0; i < pos && bmap; ++i)
3543 bmap = var_equal(bmap, i);
3544 if (bmap)
3545 bmap = var_less(bmap, pos);
3546 return isl_basic_map_finalize(bmap);
3549 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3551 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3552 __isl_take isl_space *dim, unsigned pos)
3554 int i;
3555 isl_basic_map *bmap;
3557 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3558 for (i = 0; i < pos; ++i)
3559 bmap = var_equal(bmap, i);
3560 bmap = var_less_or_equal(bmap, pos);
3561 return isl_basic_map_finalize(bmap);
3564 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3566 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3567 unsigned pos)
3569 int i;
3570 struct isl_basic_map *bmap;
3571 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3572 if (!bmap)
3573 return NULL;
3574 for (i = 0; i < pos && bmap; ++i)
3575 bmap = var_equal(bmap, i);
3576 if (bmap)
3577 bmap = var_more(bmap, pos);
3578 return isl_basic_map_finalize(bmap);
3581 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3583 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3584 __isl_take isl_space *dim, unsigned pos)
3586 int i;
3587 isl_basic_map *bmap;
3589 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3590 for (i = 0; i < pos; ++i)
3591 bmap = var_equal(bmap, i);
3592 bmap = var_more_or_equal(bmap, pos);
3593 return isl_basic_map_finalize(bmap);
3596 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3597 unsigned n, int equal)
3599 struct isl_map *map;
3600 int i;
3602 if (n == 0 && equal)
3603 return isl_map_universe(dims);
3605 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3607 for (i = 0; i + 1 < n; ++i)
3608 map = isl_map_add_basic_map(map,
3609 isl_basic_map_less_at(isl_space_copy(dims), i));
3610 if (n > 0) {
3611 if (equal)
3612 map = isl_map_add_basic_map(map,
3613 isl_basic_map_less_or_equal_at(dims, n - 1));
3614 else
3615 map = isl_map_add_basic_map(map,
3616 isl_basic_map_less_at(dims, n - 1));
3617 } else
3618 isl_space_free(dims);
3620 return map;
3623 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3625 if (!dims)
3626 return NULL;
3627 return map_lex_lte_first(dims, dims->n_out, equal);
3630 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3632 return map_lex_lte_first(dim, n, 0);
3635 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3637 return map_lex_lte_first(dim, n, 1);
3640 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3642 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3645 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3647 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3650 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3651 unsigned n, int equal)
3653 struct isl_map *map;
3654 int i;
3656 if (n == 0 && equal)
3657 return isl_map_universe(dims);
3659 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3661 for (i = 0; i + 1 < n; ++i)
3662 map = isl_map_add_basic_map(map,
3663 isl_basic_map_more_at(isl_space_copy(dims), i));
3664 if (n > 0) {
3665 if (equal)
3666 map = isl_map_add_basic_map(map,
3667 isl_basic_map_more_or_equal_at(dims, n - 1));
3668 else
3669 map = isl_map_add_basic_map(map,
3670 isl_basic_map_more_at(dims, n - 1));
3671 } else
3672 isl_space_free(dims);
3674 return map;
3677 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3679 if (!dims)
3680 return NULL;
3681 return map_lex_gte_first(dims, dims->n_out, equal);
3684 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3686 return map_lex_gte_first(dim, n, 0);
3689 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3691 return map_lex_gte_first(dim, n, 1);
3694 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3696 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3699 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3701 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3704 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3705 __isl_take isl_set *set2)
3707 isl_map *map;
3708 map = isl_map_lex_le(isl_set_get_space(set1));
3709 map = isl_map_intersect_domain(map, set1);
3710 map = isl_map_intersect_range(map, set2);
3711 return map;
3714 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3715 __isl_take isl_set *set2)
3717 isl_map *map;
3718 map = isl_map_lex_lt(isl_set_get_space(set1));
3719 map = isl_map_intersect_domain(map, set1);
3720 map = isl_map_intersect_range(map, set2);
3721 return map;
3724 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3725 __isl_take isl_set *set2)
3727 isl_map *map;
3728 map = isl_map_lex_ge(isl_set_get_space(set1));
3729 map = isl_map_intersect_domain(map, set1);
3730 map = isl_map_intersect_range(map, set2);
3731 return map;
3734 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3735 __isl_take isl_set *set2)
3737 isl_map *map;
3738 map = isl_map_lex_gt(isl_set_get_space(set1));
3739 map = isl_map_intersect_domain(map, set1);
3740 map = isl_map_intersect_range(map, set2);
3741 return map;
3744 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3745 __isl_take isl_map *map2)
3747 isl_map *map;
3748 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3749 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3750 map = isl_map_apply_range(map, isl_map_reverse(map2));
3751 return map;
3754 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3755 __isl_take isl_map *map2)
3757 isl_map *map;
3758 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3759 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3760 map = isl_map_apply_range(map, isl_map_reverse(map2));
3761 return map;
3764 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3765 __isl_take isl_map *map2)
3767 isl_map *map;
3768 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3769 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3770 map = isl_map_apply_range(map, isl_map_reverse(map2));
3771 return map;
3774 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3775 __isl_take isl_map *map2)
3777 isl_map *map;
3778 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
3779 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3780 map = isl_map_apply_range(map, isl_map_reverse(map2));
3781 return map;
3784 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
3785 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3787 struct isl_basic_map *bmap;
3789 bset = isl_basic_set_cow(bset);
3790 if (!bset || !dim)
3791 goto error;
3793 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3794 isl_space_free(bset->dim);
3795 bmap = (struct isl_basic_map *) bset;
3796 bmap->dim = dim;
3797 return isl_basic_map_finalize(bmap);
3798 error:
3799 isl_basic_set_free(bset);
3800 isl_space_free(dim);
3801 return NULL;
3804 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3806 if (!bmap)
3807 goto error;
3808 if (bmap->dim->n_in == 0)
3809 return (struct isl_basic_set *)bmap;
3810 bmap = isl_basic_map_cow(bmap);
3811 if (!bmap)
3812 goto error;
3813 bmap->dim = isl_space_as_set_space(bmap->dim);
3814 if (!bmap->dim)
3815 goto error;
3816 bmap = isl_basic_map_finalize(bmap);
3817 return (struct isl_basic_set *)bmap;
3818 error:
3819 isl_basic_map_free(bmap);
3820 return NULL;
3823 /* For a div d = floor(f/m), add the constraints
3825 * f - m d >= 0
3826 * -(f-(n-1)) + m d >= 0
3828 * Note that the second constraint is the negation of
3830 * f - m d >= n
3832 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3833 unsigned pos, isl_int *div)
3835 int i, j;
3836 unsigned total = isl_basic_map_total_dim(bmap);
3838 i = isl_basic_map_alloc_inequality(bmap);
3839 if (i < 0)
3840 return -1;
3841 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3842 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3844 j = isl_basic_map_alloc_inequality(bmap);
3845 if (j < 0)
3846 return -1;
3847 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3848 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3849 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3850 return j;
3853 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3854 unsigned pos, isl_int *div)
3856 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3857 pos, div);
3860 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3862 unsigned total = isl_basic_map_total_dim(bmap);
3863 unsigned div_pos = total - bmap->n_div + div;
3865 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3866 bmap->div[div]);
3869 struct isl_basic_set *isl_basic_map_underlying_set(
3870 struct isl_basic_map *bmap)
3872 if (!bmap)
3873 goto error;
3874 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3875 bmap->n_div == 0 &&
3876 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3877 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3878 return (struct isl_basic_set *)bmap;
3879 bmap = isl_basic_map_cow(bmap);
3880 if (!bmap)
3881 goto error;
3882 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3883 if (!bmap->dim)
3884 goto error;
3885 bmap->extra -= bmap->n_div;
3886 bmap->n_div = 0;
3887 bmap = isl_basic_map_finalize(bmap);
3888 return (struct isl_basic_set *)bmap;
3889 error:
3890 return NULL;
3893 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3894 __isl_take isl_basic_set *bset)
3896 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3899 struct isl_basic_map *isl_basic_map_overlying_set(
3900 struct isl_basic_set *bset, struct isl_basic_map *like)
3902 struct isl_basic_map *bmap;
3903 struct isl_ctx *ctx;
3904 unsigned total;
3905 int i;
3907 if (!bset || !like)
3908 goto error;
3909 ctx = bset->ctx;
3910 isl_assert(ctx, bset->n_div == 0, goto error);
3911 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3912 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3913 goto error);
3914 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
3915 isl_basic_map_free(like);
3916 return (struct isl_basic_map *)bset;
3918 bset = isl_basic_set_cow(bset);
3919 if (!bset)
3920 goto error;
3921 total = bset->dim->n_out + bset->extra;
3922 bmap = (struct isl_basic_map *)bset;
3923 isl_space_free(bmap->dim);
3924 bmap->dim = isl_space_copy(like->dim);
3925 if (!bmap->dim)
3926 goto error;
3927 bmap->n_div = like->n_div;
3928 bmap->extra += like->n_div;
3929 if (bmap->extra) {
3930 unsigned ltotal;
3931 isl_int **div;
3932 ltotal = total - bmap->extra + like->extra;
3933 if (ltotal > total)
3934 ltotal = total;
3935 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3936 bmap->extra * (1 + 1 + total));
3937 if (isl_blk_is_error(bmap->block2))
3938 goto error;
3939 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3940 if (!div)
3941 goto error;
3942 bmap->div = div;
3943 for (i = 0; i < bmap->extra; ++i)
3944 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3945 for (i = 0; i < like->n_div; ++i) {
3946 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3947 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3949 bmap = isl_basic_map_extend_constraints(bmap,
3950 0, 2 * like->n_div);
3951 for (i = 0; i < like->n_div; ++i) {
3952 if (isl_int_is_zero(bmap->div[i][0]))
3953 continue;
3954 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3955 goto error;
3958 isl_basic_map_free(like);
3959 bmap = isl_basic_map_simplify(bmap);
3960 bmap = isl_basic_map_finalize(bmap);
3961 return bmap;
3962 error:
3963 isl_basic_map_free(like);
3964 isl_basic_set_free(bset);
3965 return NULL;
3968 struct isl_basic_set *isl_basic_set_from_underlying_set(
3969 struct isl_basic_set *bset, struct isl_basic_set *like)
3971 return (struct isl_basic_set *)
3972 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3975 struct isl_set *isl_set_from_underlying_set(
3976 struct isl_set *set, struct isl_basic_set *like)
3978 int i;
3980 if (!set || !like)
3981 goto error;
3982 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3983 goto error);
3984 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
3985 isl_basic_set_free(like);
3986 return set;
3988 set = isl_set_cow(set);
3989 if (!set)
3990 goto error;
3991 for (i = 0; i < set->n; ++i) {
3992 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3993 isl_basic_set_copy(like));
3994 if (!set->p[i])
3995 goto error;
3997 isl_space_free(set->dim);
3998 set->dim = isl_space_copy(like->dim);
3999 if (!set->dim)
4000 goto error;
4001 isl_basic_set_free(like);
4002 return set;
4003 error:
4004 isl_basic_set_free(like);
4005 isl_set_free(set);
4006 return NULL;
4009 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4011 int i;
4013 map = isl_map_cow(map);
4014 if (!map)
4015 return NULL;
4016 map->dim = isl_space_cow(map->dim);
4017 if (!map->dim)
4018 goto error;
4020 for (i = 1; i < map->n; ++i)
4021 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4022 goto error);
4023 for (i = 0; i < map->n; ++i) {
4024 map->p[i] = (struct isl_basic_map *)
4025 isl_basic_map_underlying_set(map->p[i]);
4026 if (!map->p[i])
4027 goto error;
4029 if (map->n == 0)
4030 map->dim = isl_space_underlying(map->dim, 0);
4031 else {
4032 isl_space_free(map->dim);
4033 map->dim = isl_space_copy(map->p[0]->dim);
4035 if (!map->dim)
4036 goto error;
4037 return (struct isl_set *)map;
4038 error:
4039 isl_map_free(map);
4040 return NULL;
4043 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4045 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4048 __isl_give isl_basic_map *isl_basic_map_reset_space(
4049 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4051 bmap = isl_basic_map_cow(bmap);
4052 if (!bmap || !dim)
4053 goto error;
4055 isl_space_free(bmap->dim);
4056 bmap->dim = dim;
4058 bmap = isl_basic_map_finalize(bmap);
4060 return bmap;
4061 error:
4062 isl_basic_map_free(bmap);
4063 isl_space_free(dim);
4064 return NULL;
4067 __isl_give isl_basic_set *isl_basic_set_reset_space(
4068 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4070 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4071 dim);
4074 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4075 __isl_take isl_space *dim)
4077 int i;
4079 map = isl_map_cow(map);
4080 if (!map || !dim)
4081 goto error;
4083 for (i = 0; i < map->n; ++i) {
4084 map->p[i] = isl_basic_map_reset_space(map->p[i],
4085 isl_space_copy(dim));
4086 if (!map->p[i])
4087 goto error;
4089 isl_space_free(map->dim);
4090 map->dim = dim;
4092 return map;
4093 error:
4094 isl_map_free(map);
4095 isl_space_free(dim);
4096 return NULL;
4099 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4100 __isl_take isl_space *dim)
4102 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4105 /* Compute the parameter domain of the given basic set.
4107 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4109 isl_space *space;
4110 unsigned n;
4112 if (isl_basic_set_is_params(bset))
4113 return bset;
4115 n = isl_basic_set_dim(bset, isl_dim_set);
4116 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4117 space = isl_basic_set_get_space(bset);
4118 space = isl_space_params(space);
4119 bset = isl_basic_set_reset_space(bset, space);
4120 return bset;
4123 /* Compute the parameter domain of the given set.
4125 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4127 isl_space *space;
4128 unsigned n;
4130 if (isl_set_is_params(set))
4131 return set;
4133 n = isl_set_dim(set, isl_dim_set);
4134 set = isl_set_project_out(set, isl_dim_set, 0, n);
4135 space = isl_set_get_space(set);
4136 space = isl_space_params(space);
4137 set = isl_set_reset_space(set, space);
4138 return set;
4141 /* Construct a zero-dimensional set with the given parameter domain.
4143 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4145 isl_space *space;
4146 space = isl_set_get_space(set);
4147 space = isl_space_set_from_params(space);
4148 set = isl_set_reset_space(set, space);
4149 return set;
4152 /* Compute the parameter domain of the given map.
4154 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4156 isl_space *space;
4157 unsigned n;
4159 n = isl_map_dim(map, isl_dim_in);
4160 map = isl_map_project_out(map, isl_dim_in, 0, n);
4161 n = isl_map_dim(map, isl_dim_out);
4162 map = isl_map_project_out(map, isl_dim_out, 0, n);
4163 space = isl_map_get_space(map);
4164 space = isl_space_params(space);
4165 map = isl_map_reset_space(map, space);
4166 return map;
4169 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4171 isl_space *dim;
4172 struct isl_basic_set *domain;
4173 unsigned n_in;
4174 unsigned n_out;
4176 if (!bmap)
4177 return NULL;
4178 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4180 n_in = isl_basic_map_n_in(bmap);
4181 n_out = isl_basic_map_n_out(bmap);
4182 domain = isl_basic_set_from_basic_map(bmap);
4183 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4185 domain = isl_basic_set_reset_space(domain, dim);
4187 return domain;
4190 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4192 if (!bmap)
4193 return -1;
4194 return isl_space_may_be_set(bmap->dim);
4197 /* Is this basic map actually a set?
4198 * Users should never call this function. Outside of isl,
4199 * the type should indicate whether something is a set or a map.
4201 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4203 if (!bmap)
4204 return -1;
4205 return isl_space_is_set(bmap->dim);
4208 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4210 if (!bmap)
4211 return NULL;
4212 if (isl_basic_map_is_set(bmap))
4213 return bmap;
4214 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4217 __isl_give isl_basic_map *isl_basic_map_domain_map(
4218 __isl_take isl_basic_map *bmap)
4220 int i, k;
4221 isl_space *dim;
4222 isl_basic_map *domain;
4223 int nparam, n_in, n_out;
4224 unsigned total;
4226 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4227 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4228 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4230 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4231 domain = isl_basic_map_universe(dim);
4233 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4234 bmap = isl_basic_map_apply_range(bmap, domain);
4235 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4237 total = isl_basic_map_total_dim(bmap);
4239 for (i = 0; i < n_in; ++i) {
4240 k = isl_basic_map_alloc_equality(bmap);
4241 if (k < 0)
4242 goto error;
4243 isl_seq_clr(bmap->eq[k], 1 + total);
4244 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4245 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4248 bmap = isl_basic_map_gauss(bmap, NULL);
4249 return isl_basic_map_finalize(bmap);
4250 error:
4251 isl_basic_map_free(bmap);
4252 return NULL;
4255 __isl_give isl_basic_map *isl_basic_map_range_map(
4256 __isl_take isl_basic_map *bmap)
4258 int i, k;
4259 isl_space *dim;
4260 isl_basic_map *range;
4261 int nparam, n_in, n_out;
4262 unsigned total;
4264 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4265 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4266 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4268 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4269 range = isl_basic_map_universe(dim);
4271 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4272 bmap = isl_basic_map_apply_range(bmap, range);
4273 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4275 total = isl_basic_map_total_dim(bmap);
4277 for (i = 0; i < n_out; ++i) {
4278 k = isl_basic_map_alloc_equality(bmap);
4279 if (k < 0)
4280 goto error;
4281 isl_seq_clr(bmap->eq[k], 1 + total);
4282 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4283 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4286 bmap = isl_basic_map_gauss(bmap, NULL);
4287 return isl_basic_map_finalize(bmap);
4288 error:
4289 isl_basic_map_free(bmap);
4290 return NULL;
4293 int isl_map_may_be_set(__isl_keep isl_map *map)
4295 if (!map)
4296 return -1;
4297 return isl_space_may_be_set(map->dim);
4300 /* Is this map actually a set?
4301 * Users should never call this function. Outside of isl,
4302 * the type should indicate whether something is a set or a map.
4304 int isl_map_is_set(__isl_keep isl_map *map)
4306 if (!map)
4307 return -1;
4308 return isl_space_is_set(map->dim);
4311 struct isl_set *isl_map_range(struct isl_map *map)
4313 int i;
4314 struct isl_set *set;
4316 if (!map)
4317 goto error;
4318 if (isl_map_is_set(map))
4319 return (isl_set *)map;
4321 map = isl_map_cow(map);
4322 if (!map)
4323 goto error;
4325 set = (struct isl_set *) map;
4326 set->dim = isl_space_range(set->dim);
4327 if (!set->dim)
4328 goto error;
4329 for (i = 0; i < map->n; ++i) {
4330 set->p[i] = isl_basic_map_range(map->p[i]);
4331 if (!set->p[i])
4332 goto error;
4334 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4335 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4336 return set;
4337 error:
4338 isl_map_free(map);
4339 return NULL;
4342 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4344 int i;
4345 isl_space *domain_dim;
4347 map = isl_map_cow(map);
4348 if (!map)
4349 return NULL;
4351 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4352 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4353 map->dim = isl_space_join(map->dim, domain_dim);
4354 if (!map->dim)
4355 goto error;
4356 for (i = 0; i < map->n; ++i) {
4357 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4358 if (!map->p[i])
4359 goto error;
4361 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4362 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4363 return map;
4364 error:
4365 isl_map_free(map);
4366 return NULL;
4369 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4371 int i;
4372 isl_space *range_dim;
4374 map = isl_map_cow(map);
4375 if (!map)
4376 return NULL;
4378 range_dim = isl_space_range(isl_map_get_space(map));
4379 range_dim = isl_space_from_range(range_dim);
4380 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4381 map->dim = isl_space_join(map->dim, range_dim);
4382 if (!map->dim)
4383 goto error;
4384 for (i = 0; i < map->n; ++i) {
4385 map->p[i] = isl_basic_map_range_map(map->p[i]);
4386 if (!map->p[i])
4387 goto error;
4389 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4390 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4391 return map;
4392 error:
4393 isl_map_free(map);
4394 return NULL;
4397 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4398 __isl_take isl_space *dim)
4400 int i;
4401 struct isl_map *map = NULL;
4403 set = isl_set_cow(set);
4404 if (!set || !dim)
4405 goto error;
4406 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4407 map = (struct isl_map *)set;
4408 for (i = 0; i < set->n; ++i) {
4409 map->p[i] = isl_basic_map_from_basic_set(
4410 set->p[i], isl_space_copy(dim));
4411 if (!map->p[i])
4412 goto error;
4414 isl_space_free(map->dim);
4415 map->dim = dim;
4416 return map;
4417 error:
4418 isl_space_free(dim);
4419 isl_set_free(set);
4420 return NULL;
4423 __isl_give isl_basic_map *isl_basic_map_from_domain(
4424 __isl_take isl_basic_set *bset)
4426 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4429 __isl_give isl_basic_map *isl_basic_map_from_range(
4430 __isl_take isl_basic_set *bset)
4432 isl_space *space;
4433 space = isl_basic_set_get_space(bset);
4434 space = isl_space_from_range(space);
4435 bset = isl_basic_set_reset_space(bset, space);
4436 return (isl_basic_map *)bset;
4439 struct isl_map *isl_map_from_range(struct isl_set *set)
4441 isl_space *space;
4442 space = isl_set_get_space(set);
4443 space = isl_space_from_range(space);
4444 set = isl_set_reset_space(set, space);
4445 return (struct isl_map *)set;
4448 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4450 return isl_map_reverse(isl_map_from_range(set));
4453 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4454 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4456 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4459 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4460 __isl_take isl_set *range)
4462 return isl_map_apply_range(isl_map_reverse(domain), range);
4465 struct isl_set *isl_set_from_map(struct isl_map *map)
4467 int i;
4468 struct isl_set *set = NULL;
4470 if (!map)
4471 return NULL;
4472 map = isl_map_cow(map);
4473 if (!map)
4474 return NULL;
4475 map->dim = isl_space_as_set_space(map->dim);
4476 if (!map->dim)
4477 goto error;
4478 set = (struct isl_set *)map;
4479 for (i = 0; i < map->n; ++i) {
4480 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4481 if (!set->p[i])
4482 goto error;
4484 return set;
4485 error:
4486 isl_map_free(map);
4487 return NULL;
4490 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4491 unsigned flags)
4493 struct isl_map *map;
4495 if (!dim)
4496 return NULL;
4497 isl_assert(dim->ctx, n >= 0, return NULL);
4498 map = isl_alloc(dim->ctx, struct isl_map,
4499 sizeof(struct isl_map) +
4500 (n - 1) * sizeof(struct isl_basic_map *));
4501 if (!map)
4502 goto error;
4504 map->ctx = dim->ctx;
4505 isl_ctx_ref(map->ctx);
4506 map->ref = 1;
4507 map->size = n;
4508 map->n = 0;
4509 map->dim = dim;
4510 map->flags = flags;
4511 return map;
4512 error:
4513 isl_space_free(dim);
4514 return NULL;
4517 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4518 unsigned nparam, unsigned in, unsigned out, int n,
4519 unsigned flags)
4521 struct isl_map *map;
4522 isl_space *dims;
4524 dims = isl_space_alloc(ctx, nparam, in, out);
4525 if (!dims)
4526 return NULL;
4528 map = isl_map_alloc_space(dims, n, flags);
4529 return map;
4532 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4534 struct isl_basic_map *bmap;
4535 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4536 bmap = isl_basic_map_set_to_empty(bmap);
4537 return bmap;
4540 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4542 struct isl_basic_set *bset;
4543 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4544 bset = isl_basic_set_set_to_empty(bset);
4545 return bset;
4548 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4550 struct isl_basic_map *bmap;
4551 if (!model)
4552 return NULL;
4553 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4554 bmap = isl_basic_map_set_to_empty(bmap);
4555 return bmap;
4558 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4560 struct isl_basic_map *bmap;
4561 if (!model)
4562 return NULL;
4563 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4564 bmap = isl_basic_map_set_to_empty(bmap);
4565 return bmap;
4568 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4570 struct isl_basic_set *bset;
4571 if (!model)
4572 return NULL;
4573 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4574 bset = isl_basic_set_set_to_empty(bset);
4575 return bset;
4578 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4580 struct isl_basic_map *bmap;
4581 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4582 bmap = isl_basic_map_finalize(bmap);
4583 return bmap;
4586 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4588 struct isl_basic_set *bset;
4589 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4590 bset = isl_basic_set_finalize(bset);
4591 return bset;
4594 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4596 int i;
4597 unsigned total = isl_space_dim(dim, isl_dim_all);
4598 isl_basic_map *bmap;
4600 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4601 for (i = 0; i < total; ++i) {
4602 int k = isl_basic_map_alloc_inequality(bmap);
4603 if (k < 0)
4604 goto error;
4605 isl_seq_clr(bmap->ineq[k], 1 + total);
4606 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4608 return bmap;
4609 error:
4610 isl_basic_map_free(bmap);
4611 return NULL;
4614 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4616 return isl_basic_map_nat_universe(dim);
4619 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4621 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4624 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4626 return isl_map_nat_universe(dim);
4629 __isl_give isl_basic_map *isl_basic_map_universe_like(
4630 __isl_keep isl_basic_map *model)
4632 if (!model)
4633 return NULL;
4634 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4637 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4639 if (!model)
4640 return NULL;
4641 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4644 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4645 __isl_keep isl_set *model)
4647 if (!model)
4648 return NULL;
4649 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4652 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4654 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4657 struct isl_map *isl_map_empty_like(struct isl_map *model)
4659 if (!model)
4660 return NULL;
4661 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4664 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4666 if (!model)
4667 return NULL;
4668 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4671 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4673 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4676 struct isl_set *isl_set_empty_like(struct isl_set *model)
4678 if (!model)
4679 return NULL;
4680 return isl_set_empty(isl_space_copy(model->dim));
4683 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4685 struct isl_map *map;
4686 if (!dim)
4687 return NULL;
4688 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4689 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4690 return map;
4693 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4695 struct isl_set *set;
4696 if (!dim)
4697 return NULL;
4698 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4699 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4700 return set;
4703 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4705 if (!model)
4706 return NULL;
4707 return isl_set_universe(isl_space_copy(model->dim));
4710 struct isl_map *isl_map_dup(struct isl_map *map)
4712 int i;
4713 struct isl_map *dup;
4715 if (!map)
4716 return NULL;
4717 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4718 for (i = 0; i < map->n; ++i)
4719 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4720 return dup;
4723 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4724 __isl_take isl_basic_map *bmap)
4726 if (!bmap || !map)
4727 goto error;
4728 if (isl_basic_map_plain_is_empty(bmap)) {
4729 isl_basic_map_free(bmap);
4730 return map;
4732 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4733 isl_assert(map->ctx, map->n < map->size, goto error);
4734 map->p[map->n] = bmap;
4735 map->n++;
4736 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4737 return map;
4738 error:
4739 if (map)
4740 isl_map_free(map);
4741 if (bmap)
4742 isl_basic_map_free(bmap);
4743 return NULL;
4746 void isl_map_free(struct isl_map *map)
4748 int i;
4750 if (!map)
4751 return;
4753 if (--map->ref > 0)
4754 return;
4756 isl_ctx_deref(map->ctx);
4757 for (i = 0; i < map->n; ++i)
4758 isl_basic_map_free(map->p[i]);
4759 isl_space_free(map->dim);
4760 free(map);
4763 struct isl_map *isl_map_extend(struct isl_map *base,
4764 unsigned nparam, unsigned n_in, unsigned n_out)
4766 int i;
4768 base = isl_map_cow(base);
4769 if (!base)
4770 return NULL;
4772 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4773 if (!base->dim)
4774 goto error;
4775 for (i = 0; i < base->n; ++i) {
4776 base->p[i] = isl_basic_map_extend_space(base->p[i],
4777 isl_space_copy(base->dim), 0, 0, 0);
4778 if (!base->p[i])
4779 goto error;
4781 return base;
4782 error:
4783 isl_map_free(base);
4784 return NULL;
4787 struct isl_set *isl_set_extend(struct isl_set *base,
4788 unsigned nparam, unsigned dim)
4790 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4791 nparam, 0, dim);
4794 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4795 struct isl_basic_map *bmap, unsigned pos, int value)
4797 int j;
4799 bmap = isl_basic_map_cow(bmap);
4800 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4801 j = isl_basic_map_alloc_equality(bmap);
4802 if (j < 0)
4803 goto error;
4804 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4805 isl_int_set_si(bmap->eq[j][pos], -1);
4806 isl_int_set_si(bmap->eq[j][0], value);
4807 bmap = isl_basic_map_simplify(bmap);
4808 return isl_basic_map_finalize(bmap);
4809 error:
4810 isl_basic_map_free(bmap);
4811 return NULL;
4814 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4815 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4817 int j;
4819 bmap = isl_basic_map_cow(bmap);
4820 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4821 j = isl_basic_map_alloc_equality(bmap);
4822 if (j < 0)
4823 goto error;
4824 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4825 isl_int_set_si(bmap->eq[j][pos], -1);
4826 isl_int_set(bmap->eq[j][0], value);
4827 bmap = isl_basic_map_simplify(bmap);
4828 return isl_basic_map_finalize(bmap);
4829 error:
4830 isl_basic_map_free(bmap);
4831 return NULL;
4834 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4835 enum isl_dim_type type, unsigned pos, int value)
4837 if (!bmap)
4838 return NULL;
4839 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4840 return isl_basic_map_fix_pos_si(bmap,
4841 isl_basic_map_offset(bmap, type) + pos, value);
4842 error:
4843 isl_basic_map_free(bmap);
4844 return NULL;
4847 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4848 enum isl_dim_type type, unsigned pos, isl_int value)
4850 if (!bmap)
4851 return NULL;
4852 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4853 return isl_basic_map_fix_pos(bmap,
4854 isl_basic_map_offset(bmap, type) + pos, value);
4855 error:
4856 isl_basic_map_free(bmap);
4857 return NULL;
4860 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4861 enum isl_dim_type type, unsigned pos, int value)
4863 return (struct isl_basic_set *)
4864 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4865 type, pos, value);
4868 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4869 enum isl_dim_type type, unsigned pos, isl_int value)
4871 return (struct isl_basic_set *)
4872 isl_basic_map_fix((struct isl_basic_map *)bset,
4873 type, pos, value);
4876 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4877 unsigned input, int value)
4879 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4882 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4883 unsigned dim, int value)
4885 return (struct isl_basic_set *)
4886 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4887 isl_dim_set, dim, value);
4890 struct isl_map *isl_map_fix_si(struct isl_map *map,
4891 enum isl_dim_type type, unsigned pos, int value)
4893 int i;
4895 map = isl_map_cow(map);
4896 if (!map)
4897 return NULL;
4899 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4900 for (i = 0; i < map->n; ++i) {
4901 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4902 if (!map->p[i])
4903 goto error;
4905 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4906 return map;
4907 error:
4908 isl_map_free(map);
4909 return NULL;
4912 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4913 enum isl_dim_type type, unsigned pos, int value)
4915 return (struct isl_set *)
4916 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4919 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4920 enum isl_dim_type type, unsigned pos, isl_int value)
4922 int i;
4924 map = isl_map_cow(map);
4925 if (!map)
4926 return NULL;
4928 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4929 for (i = 0; i < map->n; ++i) {
4930 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4931 if (!map->p[i])
4932 goto error;
4934 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4935 return map;
4936 error:
4937 isl_map_free(map);
4938 return NULL;
4941 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4942 enum isl_dim_type type, unsigned pos, isl_int value)
4944 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4947 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4948 unsigned input, int value)
4950 return isl_map_fix_si(map, isl_dim_in, input, value);
4953 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4955 return (struct isl_set *)
4956 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4959 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4960 __isl_take isl_basic_map *bmap,
4961 enum isl_dim_type type, unsigned pos, int value)
4963 int j;
4965 if (!bmap)
4966 return NULL;
4967 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4968 pos += isl_basic_map_offset(bmap, type);
4969 bmap = isl_basic_map_cow(bmap);
4970 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4971 j = isl_basic_map_alloc_inequality(bmap);
4972 if (j < 0)
4973 goto error;
4974 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4975 isl_int_set_si(bmap->ineq[j][pos], 1);
4976 isl_int_set_si(bmap->ineq[j][0], -value);
4977 bmap = isl_basic_map_simplify(bmap);
4978 return isl_basic_map_finalize(bmap);
4979 error:
4980 isl_basic_map_free(bmap);
4981 return NULL;
4984 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4985 unsigned dim, isl_int value)
4987 int j;
4989 bset = isl_basic_set_cow(bset);
4990 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4991 j = isl_basic_set_alloc_inequality(bset);
4992 if (j < 0)
4993 goto error;
4994 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4995 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4996 isl_int_neg(bset->ineq[j][0], value);
4997 bset = isl_basic_set_simplify(bset);
4998 return isl_basic_set_finalize(bset);
4999 error:
5000 isl_basic_set_free(bset);
5001 return NULL;
5004 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5005 enum isl_dim_type type, unsigned pos, int value)
5007 int i;
5009 map = isl_map_cow(map);
5010 if (!map)
5011 return NULL;
5013 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5014 for (i = 0; i < map->n; ++i) {
5015 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
5016 type, pos, value);
5017 if (!map->p[i])
5018 goto error;
5020 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5021 return map;
5022 error:
5023 isl_map_free(map);
5024 return NULL;
5027 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5028 enum isl_dim_type type, unsigned pos, int value)
5030 return (struct isl_set *)
5031 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5034 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5035 isl_int value)
5037 int i;
5039 set = isl_set_cow(set);
5040 if (!set)
5041 return NULL;
5043 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5044 for (i = 0; i < set->n; ++i) {
5045 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5046 if (!set->p[i])
5047 goto error;
5049 return set;
5050 error:
5051 isl_set_free(set);
5052 return NULL;
5055 struct isl_map *isl_map_reverse(struct isl_map *map)
5057 int i;
5059 map = isl_map_cow(map);
5060 if (!map)
5061 return NULL;
5063 map->dim = isl_space_reverse(map->dim);
5064 if (!map->dim)
5065 goto error;
5066 for (i = 0; i < map->n; ++i) {
5067 map->p[i] = isl_basic_map_reverse(map->p[i]);
5068 if (!map->p[i])
5069 goto error;
5071 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5072 return map;
5073 error:
5074 isl_map_free(map);
5075 return NULL;
5078 static struct isl_map *isl_basic_map_partial_lexopt(
5079 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5080 struct isl_set **empty, int max)
5082 if (!bmap)
5083 goto error;
5084 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5085 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5086 else
5087 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5088 error:
5089 isl_basic_map_free(bmap);
5090 isl_basic_set_free(dom);
5091 if (empty)
5092 *empty = NULL;
5093 return NULL;
5096 struct isl_map *isl_basic_map_partial_lexmax(
5097 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5098 struct isl_set **empty)
5100 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5103 struct isl_map *isl_basic_map_partial_lexmin(
5104 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5105 struct isl_set **empty)
5107 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5110 struct isl_set *isl_basic_set_partial_lexmin(
5111 struct isl_basic_set *bset, struct isl_basic_set *dom,
5112 struct isl_set **empty)
5114 return (struct isl_set *)
5115 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5116 dom, empty);
5119 struct isl_set *isl_basic_set_partial_lexmax(
5120 struct isl_basic_set *bset, struct isl_basic_set *dom,
5121 struct isl_set **empty)
5123 return (struct isl_set *)
5124 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5125 dom, empty);
5128 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5129 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5130 __isl_give isl_set **empty)
5132 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5135 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5136 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5137 __isl_give isl_set **empty)
5139 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5142 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5143 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5144 __isl_give isl_set **empty)
5146 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5149 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5150 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5151 __isl_give isl_set **empty)
5153 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5156 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5157 __isl_take isl_basic_map *bmap, int max)
5159 isl_basic_set *dom = NULL;
5160 isl_space *dom_space;
5162 if (!bmap)
5163 goto error;
5164 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5165 dom = isl_basic_set_universe(dom_space);
5166 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5167 error:
5168 isl_basic_map_free(bmap);
5169 return NULL;
5172 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5173 __isl_take isl_basic_map *bmap)
5175 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5178 /* Given a basic map "bmap", compute the lexicographically minimal
5179 * (or maximal) image element for each domain element in dom.
5180 * Set *empty to those elements in dom that do not have an image element.
5182 * We first make sure the basic sets in dom are disjoint and then
5183 * simply collect the results over each of the basic sets separately.
5184 * We could probably improve the efficiency a bit by moving the union
5185 * domain down into the parametric integer programming.
5187 static __isl_give isl_map *basic_map_partial_lexopt(
5188 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5189 __isl_give isl_set **empty, int max)
5191 int i;
5192 struct isl_map *res;
5194 dom = isl_set_make_disjoint(dom);
5195 if (!dom)
5196 goto error;
5198 if (isl_set_plain_is_empty(dom)) {
5199 res = isl_map_empty_like_basic_map(bmap);
5200 *empty = isl_set_empty_like(dom);
5201 isl_set_free(dom);
5202 isl_basic_map_free(bmap);
5203 return res;
5206 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5207 isl_basic_set_copy(dom->p[0]), empty, max);
5209 for (i = 1; i < dom->n; ++i) {
5210 struct isl_map *res_i;
5211 struct isl_set *empty_i;
5213 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5214 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5216 res = isl_map_union_disjoint(res, res_i);
5217 *empty = isl_set_union_disjoint(*empty, empty_i);
5220 isl_set_free(dom);
5221 isl_basic_map_free(bmap);
5222 return res;
5223 error:
5224 *empty = NULL;
5225 isl_set_free(dom);
5226 isl_basic_map_free(bmap);
5227 return NULL;
5230 /* Given a map "map", compute the lexicographically minimal
5231 * (or maximal) image element for each domain element in dom.
5232 * Set *empty to those elements in dom that do not have an image element.
5234 * We first compute the lexicographically minimal or maximal element
5235 * in the first basic map. This results in a partial solution "res"
5236 * and a subset "todo" of dom that still need to be handled.
5237 * We then consider each of the remaining maps in "map" and successively
5238 * improve both "res" and "todo".
5240 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5241 * Assume we are computing the lexicographical maximum.
5242 * We first compute the lexicographically maximal element in basic map i.
5243 * This results in a partial solution res_i and a subset todo_i.
5244 * Then we combine these results with those obtain for the first k basic maps
5245 * to obtain a result that is valid for the first k+1 basic maps.
5246 * In particular, the set where there is no solution is the set where
5247 * there is no solution for the first k basic maps and also no solution
5248 * for the ith basic map, i.e.,
5250 * todo^i = todo^k * todo_i
5252 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5253 * solutions, arbitrarily breaking ties in favor of res^k.
5254 * That is, when res^k(a) >= res_i(a), we pick res^k and
5255 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5256 * the lexicographic order.)
5257 * In practice, we compute
5259 * res^k * (res_i . "<=")
5261 * and
5263 * res_i * (res^k . "<")
5265 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5266 * where only one of res^k and res_i provides a solution and we simply pick
5267 * that one, i.e.,
5269 * res^k * todo_i
5270 * and
5271 * res_i * todo^k
5273 * Note that we only compute these intersections when dom(res^k) intersects
5274 * dom(res_i). Otherwise, the only effect of these intersections is to
5275 * potentially break up res^k and res_i into smaller pieces.
5276 * We want to avoid such splintering as much as possible.
5277 * In fact, an earlier implementation of this function would look for
5278 * better results in the domain of res^k and for extra results in todo^k,
5279 * but this would always result in a splintering according to todo^k,
5280 * even when the domain of basic map i is disjoint from the domains of
5281 * the previous basic maps.
5283 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5284 __isl_take isl_map *map, __isl_take isl_set *dom,
5285 __isl_give isl_set **empty, int max)
5287 int i;
5288 struct isl_map *res;
5289 struct isl_set *todo;
5291 if (!map || !dom)
5292 goto error;
5294 if (isl_map_plain_is_empty(map)) {
5295 if (empty)
5296 *empty = dom;
5297 else
5298 isl_set_free(dom);
5299 return map;
5302 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5303 isl_set_copy(dom), &todo, max);
5305 for (i = 1; i < map->n; ++i) {
5306 isl_map *lt, *le;
5307 isl_map *res_i;
5308 isl_set *todo_i;
5309 isl_space *dim = isl_space_range(isl_map_get_space(res));
5311 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5312 isl_set_copy(dom), &todo_i, max);
5314 if (max) {
5315 lt = isl_map_lex_lt(isl_space_copy(dim));
5316 le = isl_map_lex_le(dim);
5317 } else {
5318 lt = isl_map_lex_gt(isl_space_copy(dim));
5319 le = isl_map_lex_ge(dim);
5321 lt = isl_map_apply_range(isl_map_copy(res), lt);
5322 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5323 le = isl_map_apply_range(isl_map_copy(res_i), le);
5324 le = isl_map_intersect(le, isl_map_copy(res));
5326 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5327 res = isl_map_intersect_domain(res,
5328 isl_set_copy(todo_i));
5329 res_i = isl_map_intersect_domain(res_i,
5330 isl_set_copy(todo));
5333 res = isl_map_union_disjoint(res, res_i);
5334 res = isl_map_union_disjoint(res, lt);
5335 res = isl_map_union_disjoint(res, le);
5337 todo = isl_set_intersect(todo, todo_i);
5340 isl_set_free(dom);
5341 isl_map_free(map);
5343 if (empty)
5344 *empty = todo;
5345 else
5346 isl_set_free(todo);
5348 return res;
5349 error:
5350 if (empty)
5351 *empty = NULL;
5352 isl_set_free(dom);
5353 isl_map_free(map);
5354 return NULL;
5357 /* Given a map "map", compute the lexicographically minimal
5358 * (or maximal) image element for each domain element in dom.
5359 * Set *empty to those elements in dom that do not have an image element.
5361 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5363 static __isl_give isl_map *isl_map_partial_lexopt(
5364 __isl_take isl_map *map, __isl_take isl_set *dom,
5365 __isl_give isl_set **empty, int max)
5367 if (!map || !dom)
5368 goto error;
5369 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5370 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5371 if (!isl_space_has_named_params(map->dim) ||
5372 !isl_space_has_named_params(dom->dim))
5373 isl_die(map->ctx, isl_error_invalid,
5374 "unaligned unnamed parameters", goto error);
5375 map = isl_map_align_params(map, isl_map_get_space(dom));
5376 dom = isl_map_align_params(dom, isl_map_get_space(map));
5377 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5378 error:
5379 if (empty)
5380 *empty = NULL;
5381 isl_set_free(dom);
5382 isl_map_free(map);
5383 return NULL;
5386 __isl_give isl_map *isl_map_partial_lexmax(
5387 __isl_take isl_map *map, __isl_take isl_set *dom,
5388 __isl_give isl_set **empty)
5390 return isl_map_partial_lexopt(map, dom, empty, 1);
5393 __isl_give isl_map *isl_map_partial_lexmin(
5394 __isl_take isl_map *map, __isl_take isl_set *dom,
5395 __isl_give isl_set **empty)
5397 return isl_map_partial_lexopt(map, dom, empty, 0);
5400 __isl_give isl_set *isl_set_partial_lexmin(
5401 __isl_take isl_set *set, __isl_take isl_set *dom,
5402 __isl_give isl_set **empty)
5404 return (struct isl_set *)
5405 isl_map_partial_lexmin((struct isl_map *)set,
5406 dom, empty);
5409 __isl_give isl_set *isl_set_partial_lexmax(
5410 __isl_take isl_set *set, __isl_take isl_set *dom,
5411 __isl_give isl_set **empty)
5413 return (struct isl_set *)
5414 isl_map_partial_lexmax((struct isl_map *)set,
5415 dom, empty);
5418 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5420 struct isl_basic_set *dom = NULL;
5421 isl_space *dom_dim;
5423 if (!bmap)
5424 goto error;
5425 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5426 dom = isl_basic_set_universe(dom_dim);
5427 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5428 error:
5429 isl_basic_map_free(bmap);
5430 return NULL;
5433 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5435 return isl_basic_map_lexopt(bmap, 0);
5438 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5440 return isl_basic_map_lexopt(bmap, 1);
5443 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5445 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5448 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5450 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5453 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5455 struct isl_set *dom = NULL;
5456 isl_space *dom_dim;
5458 if (!map)
5459 goto error;
5460 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5461 dom = isl_set_universe(dom_dim);
5462 return isl_map_partial_lexopt(map, dom, NULL, max);
5463 error:
5464 isl_map_free(map);
5465 return NULL;
5468 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5470 return isl_map_lexopt(map, 0);
5473 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5475 return isl_map_lexopt(map, 1);
5478 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5480 return (isl_set *)isl_map_lexmin((isl_map *)set);
5483 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5485 return (isl_set *)isl_map_lexmax((isl_map *)set);
5488 /* Extract the first and only affine expression from list
5489 * and then add it to *pwaff with the given dom.
5490 * This domain is known to be disjoint from other domains
5491 * because of the way isl_basic_map_foreach_lexmax works.
5493 static int update_dim_opt(__isl_take isl_basic_set *dom,
5494 __isl_take isl_aff_list *list, void *user)
5496 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5497 isl_aff *aff;
5498 isl_pw_aff **pwaff = user;
5499 isl_pw_aff *pwaff_i;
5501 if (isl_aff_list_n_aff(list) != 1)
5502 isl_die(ctx, isl_error_internal,
5503 "expecting single element list", goto error);
5505 aff = isl_aff_list_get_aff(list, 0);
5506 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5508 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5510 isl_aff_list_free(list);
5512 return 0;
5513 error:
5514 isl_basic_set_free(dom);
5515 isl_aff_list_free(list);
5516 return -1;
5519 /* Given a basic map with one output dimension, compute the minimum or
5520 * maximum of that dimension as an isl_pw_aff.
5522 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5523 * call update_dim_opt on each leaf of the result.
5525 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
5526 int max)
5528 isl_space *dim = isl_basic_map_get_space(bmap);
5529 isl_pw_aff *pwaff;
5530 int r;
5532 dim = isl_space_from_domain(isl_space_domain(dim));
5533 dim = isl_space_add_dims(dim, isl_dim_out, 1);
5534 pwaff = isl_pw_aff_empty(dim);
5536 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
5537 if (r < 0)
5538 return isl_pw_aff_free(pwaff);
5540 return pwaff;
5543 /* Compute the minimum or maximum of the given output dimension
5544 * as a function of the parameters and the input dimensions,
5545 * but independently of the other output dimensions.
5547 * We first project out the other output dimension and then compute
5548 * the "lexicographic" maximum in each basic map, combining the results
5549 * using isl_pw_aff_union_max.
5551 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
5552 int max)
5554 int i;
5555 isl_pw_aff *pwaff;
5556 unsigned n_out;
5558 n_out = isl_map_dim(map, isl_dim_out);
5559 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
5560 map = isl_map_project_out(map, isl_dim_out, 0, pos);
5561 if (!map)
5562 return NULL;
5564 if (map->n == 0) {
5565 isl_space *dim = isl_map_get_space(map);
5566 dim = isl_space_domain(isl_space_from_range(dim));
5567 isl_map_free(map);
5568 return isl_pw_aff_empty(dim);
5571 pwaff = basic_map_dim_opt(map->p[0], max);
5572 for (i = 1; i < map->n; ++i) {
5573 isl_pw_aff *pwaff_i;
5575 pwaff_i = basic_map_dim_opt(map->p[i], max);
5576 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5579 isl_map_free(map);
5581 return pwaff;
5584 /* Compute the maximum of the given output dimension as a function of the
5585 * parameters and input dimensions, but independently of
5586 * the other output dimensions.
5588 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
5590 return map_dim_opt(map, pos, 1);
5593 /* Compute the minimum or maximum of the given set dimension
5594 * as a function of the parameters,
5595 * but independently of the other set dimensions.
5597 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5598 int max)
5600 return map_dim_opt(set, pos, max);
5603 /* Compute the maximum of the given set dimension as a function of the
5604 * parameters, but independently of the other set dimensions.
5606 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5608 return set_dim_opt(set, pos, 1);
5611 /* Compute the minimum of the given set dimension as a function of the
5612 * parameters, but independently of the other set dimensions.
5614 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5616 return set_dim_opt(set, pos, 0);
5619 /* Apply a preimage specified by "mat" on the parameters of "bset".
5620 * bset is assumed to have only parameters and divs.
5622 static struct isl_basic_set *basic_set_parameter_preimage(
5623 struct isl_basic_set *bset, struct isl_mat *mat)
5625 unsigned nparam;
5627 if (!bset || !mat)
5628 goto error;
5630 bset->dim = isl_space_cow(bset->dim);
5631 if (!bset->dim)
5632 goto error;
5634 nparam = isl_basic_set_dim(bset, isl_dim_param);
5636 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5638 bset->dim->nparam = 0;
5639 bset->dim->n_out = nparam;
5640 bset = isl_basic_set_preimage(bset, mat);
5641 if (bset) {
5642 bset->dim->nparam = bset->dim->n_out;
5643 bset->dim->n_out = 0;
5645 return bset;
5646 error:
5647 isl_mat_free(mat);
5648 isl_basic_set_free(bset);
5649 return NULL;
5652 /* Apply a preimage specified by "mat" on the parameters of "set".
5653 * set is assumed to have only parameters and divs.
5655 static struct isl_set *set_parameter_preimage(
5656 struct isl_set *set, struct isl_mat *mat)
5658 isl_space *dim = NULL;
5659 unsigned nparam;
5661 if (!set || !mat)
5662 goto error;
5664 dim = isl_space_copy(set->dim);
5665 dim = isl_space_cow(dim);
5666 if (!dim)
5667 goto error;
5669 nparam = isl_set_dim(set, isl_dim_param);
5671 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5673 dim->nparam = 0;
5674 dim->n_out = nparam;
5675 isl_set_reset_space(set, dim);
5676 set = isl_set_preimage(set, mat);
5677 if (!set)
5678 goto error2;
5679 dim = isl_space_copy(set->dim);
5680 dim = isl_space_cow(dim);
5681 if (!dim)
5682 goto error2;
5683 dim->nparam = dim->n_out;
5684 dim->n_out = 0;
5685 isl_set_reset_space(set, dim);
5686 return set;
5687 error:
5688 isl_space_free(dim);
5689 isl_mat_free(mat);
5690 error2:
5691 isl_set_free(set);
5692 return NULL;
5695 /* Intersect the basic set "bset" with the affine space specified by the
5696 * equalities in "eq".
5698 static struct isl_basic_set *basic_set_append_equalities(
5699 struct isl_basic_set *bset, struct isl_mat *eq)
5701 int i, k;
5702 unsigned len;
5704 if (!bset || !eq)
5705 goto error;
5707 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5708 eq->n_row, 0);
5709 if (!bset)
5710 goto error;
5712 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5713 for (i = 0; i < eq->n_row; ++i) {
5714 k = isl_basic_set_alloc_equality(bset);
5715 if (k < 0)
5716 goto error;
5717 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5718 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5720 isl_mat_free(eq);
5722 bset = isl_basic_set_gauss(bset, NULL);
5723 bset = isl_basic_set_finalize(bset);
5725 return bset;
5726 error:
5727 isl_mat_free(eq);
5728 isl_basic_set_free(bset);
5729 return NULL;
5732 /* Intersect the set "set" with the affine space specified by the
5733 * equalities in "eq".
5735 static struct isl_set *set_append_equalities(struct isl_set *set,
5736 struct isl_mat *eq)
5738 int i;
5740 if (!set || !eq)
5741 goto error;
5743 for (i = 0; i < set->n; ++i) {
5744 set->p[i] = basic_set_append_equalities(set->p[i],
5745 isl_mat_copy(eq));
5746 if (!set->p[i])
5747 goto error;
5749 isl_mat_free(eq);
5750 return set;
5751 error:
5752 isl_mat_free(eq);
5753 isl_set_free(set);
5754 return NULL;
5757 /* Project the given basic set onto its parameter domain, possibly introducing
5758 * new, explicit, existential variables in the constraints.
5759 * The input has parameters and (possibly implicit) existential variables.
5760 * The output has the same parameters, but only
5761 * explicit existentially quantified variables.
5763 * The actual projection is performed by pip, but pip doesn't seem
5764 * to like equalities very much, so we first remove the equalities
5765 * among the parameters by performing a variable compression on
5766 * the parameters. Afterward, an inverse transformation is performed
5767 * and the equalities among the parameters are inserted back in.
5769 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5771 int i, j;
5772 struct isl_mat *eq;
5773 struct isl_mat *T, *T2;
5774 struct isl_set *set;
5775 unsigned nparam, n_div;
5777 bset = isl_basic_set_cow(bset);
5778 if (!bset)
5779 return NULL;
5781 if (bset->n_eq == 0)
5782 return isl_basic_set_lexmin(bset);
5784 isl_basic_set_gauss(bset, NULL);
5786 nparam = isl_basic_set_dim(bset, isl_dim_param);
5787 n_div = isl_basic_set_dim(bset, isl_dim_div);
5789 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5790 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5791 ++i;
5793 if (i == bset->n_eq)
5794 return isl_basic_set_lexmin(bset);
5796 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5797 0, 1 + nparam);
5798 eq = isl_mat_cow(eq);
5799 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5800 if (T && T->n_col == 0) {
5801 isl_mat_free(T);
5802 isl_mat_free(T2);
5803 isl_mat_free(eq);
5804 bset = isl_basic_set_set_to_empty(bset);
5805 return isl_set_from_basic_set(bset);
5807 bset = basic_set_parameter_preimage(bset, T);
5809 set = isl_basic_set_lexmin(bset);
5810 set = set_parameter_preimage(set, T2);
5811 set = set_append_equalities(set, eq);
5812 return set;
5815 /* Compute an explicit representation for all the existentially
5816 * quantified variables.
5817 * The input and output dimensions are first turned into parameters.
5818 * compute_divs then returns a map with the same parameters and
5819 * no input or output dimensions and the dimension specification
5820 * is reset to that of the input.
5822 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5824 struct isl_basic_set *bset;
5825 struct isl_set *set;
5826 struct isl_map *map;
5827 isl_space *dim, *orig_dim = NULL;
5828 unsigned nparam;
5829 unsigned n_in;
5830 unsigned n_out;
5832 bmap = isl_basic_map_cow(bmap);
5833 if (!bmap)
5834 return NULL;
5836 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5837 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5838 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5839 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5840 if (!dim)
5841 goto error;
5843 orig_dim = bmap->dim;
5844 bmap->dim = dim;
5845 bset = (struct isl_basic_set *)bmap;
5847 set = parameter_compute_divs(bset);
5848 map = (struct isl_map *)set;
5849 map = isl_map_reset_space(map, orig_dim);
5851 return map;
5852 error:
5853 isl_basic_map_free(bmap);
5854 return NULL;
5857 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5859 int i;
5860 unsigned off;
5862 if (!bmap)
5863 return -1;
5865 off = isl_space_dim(bmap->dim, isl_dim_all);
5866 for (i = 0; i < bmap->n_div; ++i) {
5867 if (isl_int_is_zero(bmap->div[i][0]))
5868 return 0;
5869 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5870 return -1);
5872 return 1;
5875 static int map_divs_known(__isl_keep isl_map *map)
5877 int i;
5879 if (!map)
5880 return -1;
5882 for (i = 0; i < map->n; ++i) {
5883 int known = isl_basic_map_divs_known(map->p[i]);
5884 if (known <= 0)
5885 return known;
5888 return 1;
5891 /* If bmap contains any unknown divs, then compute explicit
5892 * expressions for them. However, this computation may be
5893 * quite expensive, so first try to remove divs that aren't
5894 * strictly needed.
5896 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5898 int known;
5899 struct isl_map *map;
5901 known = isl_basic_map_divs_known(bmap);
5902 if (known < 0)
5903 goto error;
5904 if (known)
5905 return isl_map_from_basic_map(bmap);
5907 bmap = isl_basic_map_drop_redundant_divs(bmap);
5909 known = isl_basic_map_divs_known(bmap);
5910 if (known < 0)
5911 goto error;
5912 if (known)
5913 return isl_map_from_basic_map(bmap);
5915 map = compute_divs(bmap);
5916 return map;
5917 error:
5918 isl_basic_map_free(bmap);
5919 return NULL;
5922 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5924 int i;
5925 int known;
5926 struct isl_map *res;
5928 if (!map)
5929 return NULL;
5930 if (map->n == 0)
5931 return map;
5933 known = map_divs_known(map);
5934 if (known < 0) {
5935 isl_map_free(map);
5936 return NULL;
5938 if (known)
5939 return map;
5941 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5942 for (i = 1 ; i < map->n; ++i) {
5943 struct isl_map *r2;
5944 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5945 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5946 res = isl_map_union_disjoint(res, r2);
5947 else
5948 res = isl_map_union(res, r2);
5950 isl_map_free(map);
5952 return res;
5955 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5957 return (struct isl_set *)
5958 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5961 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5963 return (struct isl_set *)
5964 isl_map_compute_divs((struct isl_map *)set);
5967 struct isl_set *isl_map_domain(struct isl_map *map)
5969 int i;
5970 struct isl_set *set;
5972 if (!map)
5973 goto error;
5975 map = isl_map_cow(map);
5976 if (!map)
5977 return NULL;
5979 set = (struct isl_set *)map;
5980 set->dim = isl_space_domain(set->dim);
5981 if (!set->dim)
5982 goto error;
5983 for (i = 0; i < map->n; ++i) {
5984 set->p[i] = isl_basic_map_domain(map->p[i]);
5985 if (!set->p[i])
5986 goto error;
5988 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5989 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5990 return set;
5991 error:
5992 isl_map_free(map);
5993 return NULL;
5996 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
5997 __isl_take isl_map *map2)
5999 int i;
6000 unsigned flags = 0;
6001 struct isl_map *map = NULL;
6003 if (!map1 || !map2)
6004 goto error;
6006 if (map1->n == 0) {
6007 isl_map_free(map1);
6008 return map2;
6010 if (map2->n == 0) {
6011 isl_map_free(map2);
6012 return map1;
6015 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6017 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6018 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6019 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6021 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6022 map1->n + map2->n, flags);
6023 if (!map)
6024 goto error;
6025 for (i = 0; i < map1->n; ++i) {
6026 map = isl_map_add_basic_map(map,
6027 isl_basic_map_copy(map1->p[i]));
6028 if (!map)
6029 goto error;
6031 for (i = 0; i < map2->n; ++i) {
6032 map = isl_map_add_basic_map(map,
6033 isl_basic_map_copy(map2->p[i]));
6034 if (!map)
6035 goto error;
6037 isl_map_free(map1);
6038 isl_map_free(map2);
6039 return map;
6040 error:
6041 isl_map_free(map);
6042 isl_map_free(map1);
6043 isl_map_free(map2);
6044 return NULL;
6047 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6048 __isl_take isl_map *map2)
6050 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6053 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6055 map1 = isl_map_union_disjoint(map1, map2);
6056 if (!map1)
6057 return NULL;
6058 if (map1->n > 1)
6059 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6060 return map1;
6063 struct isl_set *isl_set_union_disjoint(
6064 struct isl_set *set1, struct isl_set *set2)
6066 return (struct isl_set *)
6067 isl_map_union_disjoint(
6068 (struct isl_map *)set1, (struct isl_map *)set2);
6071 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6073 return (struct isl_set *)
6074 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6077 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6078 __isl_take isl_set *set)
6080 unsigned flags = 0;
6081 struct isl_map *result;
6082 int i, j;
6084 if (!map || !set)
6085 goto error;
6087 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6088 isl_die(set->ctx, isl_error_invalid,
6089 "parameters don't match", goto error);
6091 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6092 !isl_map_compatible_range(map, set))
6093 isl_die(set->ctx, isl_error_invalid,
6094 "incompatible spaces", goto error);
6096 if (isl_set_plain_is_universe(set)) {
6097 isl_set_free(set);
6098 return map;
6101 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6102 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6103 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6105 result = isl_map_alloc_space(isl_space_copy(map->dim),
6106 map->n * set->n, flags);
6107 if (!result)
6108 goto error;
6109 for (i = 0; i < map->n; ++i)
6110 for (j = 0; j < set->n; ++j) {
6111 result = isl_map_add_basic_map(result,
6112 isl_basic_map_intersect_range(
6113 isl_basic_map_copy(map->p[i]),
6114 isl_basic_set_copy(set->p[j])));
6115 if (!result)
6116 goto error;
6118 isl_map_free(map);
6119 isl_set_free(set);
6120 return result;
6121 error:
6122 isl_map_free(map);
6123 isl_set_free(set);
6124 return NULL;
6127 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6128 __isl_take isl_set *set)
6130 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6133 struct isl_map *isl_map_intersect_domain(
6134 struct isl_map *map, struct isl_set *set)
6136 return isl_map_reverse(
6137 isl_map_intersect_range(isl_map_reverse(map), set));
6140 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6141 __isl_take isl_map *map2)
6143 if (!map1 || !map2)
6144 goto error;
6145 map1 = isl_map_reverse(map1);
6146 map1 = isl_map_apply_range(map1, map2);
6147 return isl_map_reverse(map1);
6148 error:
6149 isl_map_free(map1);
6150 isl_map_free(map2);
6151 return NULL;
6154 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6155 __isl_take isl_map *map2)
6157 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6160 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6161 __isl_take isl_map *map2)
6163 isl_space *dim_result;
6164 struct isl_map *result;
6165 int i, j;
6167 if (!map1 || !map2)
6168 goto error;
6170 dim_result = isl_space_join(isl_space_copy(map1->dim),
6171 isl_space_copy(map2->dim));
6173 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6174 if (!result)
6175 goto error;
6176 for (i = 0; i < map1->n; ++i)
6177 for (j = 0; j < map2->n; ++j) {
6178 result = isl_map_add_basic_map(result,
6179 isl_basic_map_apply_range(
6180 isl_basic_map_copy(map1->p[i]),
6181 isl_basic_map_copy(map2->p[j])));
6182 if (!result)
6183 goto error;
6185 isl_map_free(map1);
6186 isl_map_free(map2);
6187 if (result && result->n <= 1)
6188 ISL_F_SET(result, ISL_MAP_DISJOINT);
6189 return result;
6190 error:
6191 isl_map_free(map1);
6192 isl_map_free(map2);
6193 return NULL;
6196 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6197 __isl_take isl_map *map2)
6199 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6203 * returns range - domain
6205 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6207 isl_space *dims, *target_dim;
6208 struct isl_basic_set *bset;
6209 unsigned dim;
6210 unsigned nparam;
6211 int i;
6213 if (!bmap)
6214 goto error;
6215 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6216 bmap->dim, isl_dim_out),
6217 goto error);
6218 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6219 dim = isl_basic_map_n_in(bmap);
6220 nparam = isl_basic_map_n_param(bmap);
6221 bset = isl_basic_set_from_basic_map(bmap);
6222 bset = isl_basic_set_cow(bset);
6223 dims = isl_basic_set_get_space(bset);
6224 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6225 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6226 bset = isl_basic_set_swap_vars(bset, 2*dim);
6227 for (i = 0; i < dim; ++i) {
6228 int j = isl_basic_map_alloc_equality(
6229 (struct isl_basic_map *)bset);
6230 if (j < 0)
6231 goto error;
6232 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6233 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6234 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6235 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6237 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6238 bset = isl_basic_set_reset_space(bset, target_dim);
6239 return bset;
6240 error:
6241 isl_basic_map_free(bmap);
6242 return NULL;
6246 * returns range - domain
6248 struct isl_set *isl_map_deltas(struct isl_map *map)
6250 int i;
6251 isl_space *dim;
6252 struct isl_set *result;
6254 if (!map)
6255 return NULL;
6257 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6258 map->dim, isl_dim_out),
6259 goto error);
6260 dim = isl_map_get_space(map);
6261 dim = isl_space_domain(dim);
6262 result = isl_set_alloc_space(dim, map->n, 0);
6263 if (!result)
6264 goto error;
6265 for (i = 0; i < map->n; ++i)
6266 result = isl_set_add_basic_set(result,
6267 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6268 isl_map_free(map);
6269 return result;
6270 error:
6271 isl_map_free(map);
6272 return NULL;
6276 * returns [domain -> range] -> range - domain
6278 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6279 __isl_take isl_basic_map *bmap)
6281 int i, k;
6282 isl_space *dim;
6283 isl_basic_map *domain;
6284 int nparam, n;
6285 unsigned total;
6287 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6288 isl_die(bmap->ctx, isl_error_invalid,
6289 "domain and range don't match", goto error);
6291 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6292 n = isl_basic_map_dim(bmap, isl_dim_in);
6294 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6295 domain = isl_basic_map_universe(dim);
6297 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6298 bmap = isl_basic_map_apply_range(bmap, domain);
6299 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6301 total = isl_basic_map_total_dim(bmap);
6303 for (i = 0; i < n; ++i) {
6304 k = isl_basic_map_alloc_equality(bmap);
6305 if (k < 0)
6306 goto error;
6307 isl_seq_clr(bmap->eq[k], 1 + total);
6308 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6309 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6310 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6313 bmap = isl_basic_map_gauss(bmap, NULL);
6314 return isl_basic_map_finalize(bmap);
6315 error:
6316 isl_basic_map_free(bmap);
6317 return NULL;
6321 * returns [domain -> range] -> range - domain
6323 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6325 int i;
6326 isl_space *domain_dim;
6328 if (!map)
6329 return NULL;
6331 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6332 isl_die(map->ctx, isl_error_invalid,
6333 "domain and range don't match", goto error);
6335 map = isl_map_cow(map);
6336 if (!map)
6337 return NULL;
6339 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6340 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6341 map->dim = isl_space_join(map->dim, domain_dim);
6342 if (!map->dim)
6343 goto error;
6344 for (i = 0; i < map->n; ++i) {
6345 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6346 if (!map->p[i])
6347 goto error;
6349 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6350 return map;
6351 error:
6352 isl_map_free(map);
6353 return NULL;
6356 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6358 struct isl_basic_map *bmap;
6359 unsigned nparam;
6360 unsigned dim;
6361 int i;
6363 if (!dims)
6364 return NULL;
6366 nparam = dims->nparam;
6367 dim = dims->n_out;
6368 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6369 if (!bmap)
6370 goto error;
6372 for (i = 0; i < dim; ++i) {
6373 int j = isl_basic_map_alloc_equality(bmap);
6374 if (j < 0)
6375 goto error;
6376 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6377 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6378 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6380 return isl_basic_map_finalize(bmap);
6381 error:
6382 isl_basic_map_free(bmap);
6383 return NULL;
6386 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6388 if (!dim)
6389 return NULL;
6390 if (dim->n_in != dim->n_out)
6391 isl_die(dim->ctx, isl_error_invalid,
6392 "number of input and output dimensions needs to be "
6393 "the same", goto error);
6394 return basic_map_identity(dim);
6395 error:
6396 isl_space_free(dim);
6397 return NULL;
6400 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6402 if (!model || !model->dim)
6403 return NULL;
6404 return isl_basic_map_identity(isl_space_copy(model->dim));
6407 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6409 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6412 struct isl_map *isl_map_identity_like(struct isl_map *model)
6414 if (!model || !model->dim)
6415 return NULL;
6416 return isl_map_identity(isl_space_copy(model->dim));
6419 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6421 if (!model || !model->dim)
6422 return NULL;
6423 return isl_map_identity(isl_space_copy(model->dim));
6426 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6428 isl_space *dim = isl_set_get_space(set);
6429 isl_map *id;
6430 id = isl_map_identity(isl_space_map_from_set(dim));
6431 return isl_map_intersect_range(id, set);
6434 /* Construct a basic set with all set dimensions having only non-negative
6435 * values.
6437 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6438 __isl_take isl_space *space)
6440 int i;
6441 unsigned nparam;
6442 unsigned dim;
6443 struct isl_basic_set *bset;
6445 if (!space)
6446 return NULL;
6447 nparam = space->nparam;
6448 dim = space->n_out;
6449 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6450 if (!bset)
6451 return NULL;
6452 for (i = 0; i < dim; ++i) {
6453 int k = isl_basic_set_alloc_inequality(bset);
6454 if (k < 0)
6455 goto error;
6456 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6457 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6459 return bset;
6460 error:
6461 isl_basic_set_free(bset);
6462 return NULL;
6465 /* Construct the half-space x_pos >= 0.
6467 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6468 int pos)
6470 int k;
6471 isl_basic_set *nonneg;
6473 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6474 k = isl_basic_set_alloc_inequality(nonneg);
6475 if (k < 0)
6476 goto error;
6477 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6478 isl_int_set_si(nonneg->ineq[k][pos], 1);
6480 return isl_basic_set_finalize(nonneg);
6481 error:
6482 isl_basic_set_free(nonneg);
6483 return NULL;
6486 /* Construct the half-space x_pos <= -1.
6488 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6490 int k;
6491 isl_basic_set *neg;
6493 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6494 k = isl_basic_set_alloc_inequality(neg);
6495 if (k < 0)
6496 goto error;
6497 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6498 isl_int_set_si(neg->ineq[k][0], -1);
6499 isl_int_set_si(neg->ineq[k][pos], -1);
6501 return isl_basic_set_finalize(neg);
6502 error:
6503 isl_basic_set_free(neg);
6504 return NULL;
6507 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6508 enum isl_dim_type type, unsigned first, unsigned n)
6510 int i;
6511 isl_basic_set *nonneg;
6512 isl_basic_set *neg;
6514 if (!set)
6515 return NULL;
6516 if (n == 0)
6517 return set;
6519 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6521 for (i = 0; i < n; ++i) {
6522 nonneg = nonneg_halfspace(isl_set_get_space(set),
6523 pos(set->dim, type) + first + i);
6524 neg = neg_halfspace(isl_set_get_space(set),
6525 pos(set->dim, type) + first + i);
6527 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6530 return set;
6531 error:
6532 isl_set_free(set);
6533 return NULL;
6536 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6537 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6538 void *user)
6540 isl_set *half;
6542 if (!set)
6543 return -1;
6544 if (isl_set_plain_is_empty(set)) {
6545 isl_set_free(set);
6546 return 0;
6548 if (first == len)
6549 return fn(set, signs, user);
6551 signs[first] = 1;
6552 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6553 1 + first));
6554 half = isl_set_intersect(half, isl_set_copy(set));
6555 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6556 goto error;
6558 signs[first] = -1;
6559 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6560 1 + first));
6561 half = isl_set_intersect(half, set);
6562 return foreach_orthant(half, signs, first + 1, len, fn, user);
6563 error:
6564 isl_set_free(set);
6565 return -1;
6568 /* Call "fn" on the intersections of "set" with each of the orthants
6569 * (except for obviously empty intersections). The orthant is identified
6570 * by the signs array, with each entry having value 1 or -1 according
6571 * to the sign of the corresponding variable.
6573 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6574 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6575 void *user)
6577 unsigned nparam;
6578 unsigned nvar;
6579 int *signs;
6580 int r;
6582 if (!set)
6583 return -1;
6584 if (isl_set_plain_is_empty(set))
6585 return 0;
6587 nparam = isl_set_dim(set, isl_dim_param);
6588 nvar = isl_set_dim(set, isl_dim_set);
6590 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6592 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6593 fn, user);
6595 free(signs);
6597 return r;
6600 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6602 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6605 int isl_basic_map_is_subset(
6606 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6608 int is_subset;
6609 struct isl_map *map1;
6610 struct isl_map *map2;
6612 if (!bmap1 || !bmap2)
6613 return -1;
6615 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6616 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6618 is_subset = isl_map_is_subset(map1, map2);
6620 isl_map_free(map1);
6621 isl_map_free(map2);
6623 return is_subset;
6626 int isl_basic_map_is_equal(
6627 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6629 int is_subset;
6631 if (!bmap1 || !bmap2)
6632 return -1;
6633 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6634 if (is_subset != 1)
6635 return is_subset;
6636 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6637 return is_subset;
6640 int isl_basic_set_is_equal(
6641 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6643 return isl_basic_map_is_equal(
6644 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6647 int isl_map_is_empty(struct isl_map *map)
6649 int i;
6650 int is_empty;
6652 if (!map)
6653 return -1;
6654 for (i = 0; i < map->n; ++i) {
6655 is_empty = isl_basic_map_is_empty(map->p[i]);
6656 if (is_empty < 0)
6657 return -1;
6658 if (!is_empty)
6659 return 0;
6661 return 1;
6664 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6666 return map ? map->n == 0 : -1;
6669 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6671 return isl_map_plain_is_empty(map);
6674 int isl_set_plain_is_empty(struct isl_set *set)
6676 return set ? set->n == 0 : -1;
6679 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6681 return isl_set_plain_is_empty(set);
6684 int isl_set_is_empty(struct isl_set *set)
6686 return isl_map_is_empty((struct isl_map *)set);
6689 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6691 if (!map1 || !map2)
6692 return -1;
6694 return isl_space_is_equal(map1->dim, map2->dim);
6697 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6699 if (!set1 || !set2)
6700 return -1;
6702 return isl_space_is_equal(set1->dim, set2->dim);
6705 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6707 int is_subset;
6709 if (!map1 || !map2)
6710 return -1;
6711 is_subset = isl_map_is_subset(map1, map2);
6712 if (is_subset != 1)
6713 return is_subset;
6714 is_subset = isl_map_is_subset(map2, map1);
6715 return is_subset;
6718 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6720 return align_params_map_map_and_test(map1, map2, &map_is_equal);
6723 int isl_basic_map_is_strict_subset(
6724 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6726 int is_subset;
6728 if (!bmap1 || !bmap2)
6729 return -1;
6730 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6731 if (is_subset != 1)
6732 return is_subset;
6733 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6734 if (is_subset == -1)
6735 return is_subset;
6736 return !is_subset;
6739 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6741 int is_subset;
6743 if (!map1 || !map2)
6744 return -1;
6745 is_subset = isl_map_is_subset(map1, map2);
6746 if (is_subset != 1)
6747 return is_subset;
6748 is_subset = isl_map_is_subset(map2, map1);
6749 if (is_subset == -1)
6750 return is_subset;
6751 return !is_subset;
6754 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6756 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6759 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6761 if (!bmap)
6762 return -1;
6763 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6766 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6768 if (!bset)
6769 return -1;
6770 return bset->n_eq == 0 && bset->n_ineq == 0;
6773 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6775 int i;
6777 if (!map)
6778 return -1;
6780 for (i = 0; i < map->n; ++i) {
6781 int r = isl_basic_map_is_universe(map->p[i]);
6782 if (r < 0 || r)
6783 return r;
6786 return 0;
6789 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6791 return isl_map_plain_is_universe((isl_map *) set);
6794 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6796 return isl_set_plain_is_universe(set);
6799 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6801 struct isl_basic_set *bset = NULL;
6802 struct isl_vec *sample = NULL;
6803 int empty;
6804 unsigned total;
6806 if (!bmap)
6807 return -1;
6809 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6810 return 1;
6812 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6813 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6814 copy = isl_basic_map_remove_redundancies(copy);
6815 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6816 isl_basic_map_free(copy);
6817 return empty;
6820 total = 1 + isl_basic_map_total_dim(bmap);
6821 if (bmap->sample && bmap->sample->size == total) {
6822 int contains = isl_basic_map_contains(bmap, bmap->sample);
6823 if (contains < 0)
6824 return -1;
6825 if (contains)
6826 return 0;
6828 isl_vec_free(bmap->sample);
6829 bmap->sample = NULL;
6830 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6831 if (!bset)
6832 return -1;
6833 sample = isl_basic_set_sample_vec(bset);
6834 if (!sample)
6835 return -1;
6836 empty = sample->size == 0;
6837 isl_vec_free(bmap->sample);
6838 bmap->sample = sample;
6839 if (empty)
6840 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6842 return empty;
6845 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6847 if (!bmap)
6848 return -1;
6849 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6852 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6854 return isl_basic_map_plain_is_empty(bmap);
6857 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6859 if (!bset)
6860 return -1;
6861 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6864 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6866 return isl_basic_set_plain_is_empty(bset);
6869 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6871 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6874 struct isl_map *isl_basic_map_union(
6875 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6877 struct isl_map *map;
6878 if (!bmap1 || !bmap2)
6879 return NULL;
6881 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
6883 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
6884 if (!map)
6885 goto error;
6886 map = isl_map_add_basic_map(map, bmap1);
6887 map = isl_map_add_basic_map(map, bmap2);
6888 return map;
6889 error:
6890 isl_basic_map_free(bmap1);
6891 isl_basic_map_free(bmap2);
6892 return NULL;
6895 struct isl_set *isl_basic_set_union(
6896 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6898 return (struct isl_set *)isl_basic_map_union(
6899 (struct isl_basic_map *)bset1,
6900 (struct isl_basic_map *)bset2);
6903 /* Order divs such that any div only depends on previous divs */
6904 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6906 int i;
6907 unsigned off;
6909 if (!bmap)
6910 return NULL;
6912 off = isl_space_dim(bmap->dim, isl_dim_all);
6914 for (i = 0; i < bmap->n_div; ++i) {
6915 int pos;
6916 if (isl_int_is_zero(bmap->div[i][0]))
6917 continue;
6918 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6919 bmap->n_div-i);
6920 if (pos == -1)
6921 continue;
6922 isl_basic_map_swap_div(bmap, i, i + pos);
6923 --i;
6925 return bmap;
6928 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6930 return (struct isl_basic_set *)
6931 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6934 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6936 int i;
6938 if (!map)
6939 return 0;
6941 for (i = 0; i < map->n; ++i) {
6942 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6943 if (!map->p[i])
6944 goto error;
6947 return map;
6948 error:
6949 isl_map_free(map);
6950 return NULL;
6953 /* Apply the expansion computed by isl_merge_divs.
6954 * The expansion itself is given by "exp" while the resulting
6955 * list of divs is given by "div".
6957 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6958 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6960 int i, j;
6961 int n_div;
6963 bset = isl_basic_set_cow(bset);
6964 if (!bset || !div)
6965 goto error;
6967 if (div->n_row < bset->n_div)
6968 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6969 "not an expansion", goto error);
6971 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
6972 div->n_row - bset->n_div, 0,
6973 2 * (div->n_row - bset->n_div));
6975 n_div = bset->n_div;
6976 for (i = n_div; i < div->n_row; ++i)
6977 if (isl_basic_set_alloc_div(bset) < 0)
6978 goto error;
6980 j = n_div - 1;
6981 for (i = div->n_row - 1; i >= 0; --i) {
6982 if (j >= 0 && exp[j] == i) {
6983 if (i != j)
6984 isl_basic_map_swap_div(bset, i, j);
6985 j--;
6986 } else {
6987 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6988 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6989 goto error;
6993 isl_mat_free(div);
6994 return bset;
6995 error:
6996 isl_basic_set_free(bset);
6997 isl_mat_free(div);
6998 return NULL;
7001 /* Look for a div in dst that corresponds to the div "div" in src.
7002 * The divs before "div" in src and dst are assumed to be the same.
7004 * Returns -1 if no corresponding div was found and the position
7005 * of the corresponding div in dst otherwise.
7007 static int find_div(struct isl_basic_map *dst,
7008 struct isl_basic_map *src, unsigned div)
7010 int i;
7012 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7014 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7015 for (i = div; i < dst->n_div; ++i)
7016 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7017 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7018 dst->n_div - div) == -1)
7019 return i;
7020 return -1;
7023 struct isl_basic_map *isl_basic_map_align_divs(
7024 struct isl_basic_map *dst, struct isl_basic_map *src)
7026 int i;
7027 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7029 if (!dst || !src)
7030 goto error;
7032 if (src->n_div == 0)
7033 return dst;
7035 for (i = 0; i < src->n_div; ++i)
7036 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7038 src = isl_basic_map_order_divs(src);
7039 dst = isl_basic_map_cow(dst);
7040 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7041 src->n_div, 0, 2 * src->n_div);
7042 if (!dst)
7043 return NULL;
7044 for (i = 0; i < src->n_div; ++i) {
7045 int j = find_div(dst, src, i);
7046 if (j < 0) {
7047 j = isl_basic_map_alloc_div(dst);
7048 if (j < 0)
7049 goto error;
7050 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7051 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7052 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7053 goto error;
7055 if (j != i)
7056 isl_basic_map_swap_div(dst, i, j);
7058 return dst;
7059 error:
7060 isl_basic_map_free(dst);
7061 return NULL;
7064 struct isl_basic_set *isl_basic_set_align_divs(
7065 struct isl_basic_set *dst, struct isl_basic_set *src)
7067 return (struct isl_basic_set *)isl_basic_map_align_divs(
7068 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7071 struct isl_map *isl_map_align_divs(struct isl_map *map)
7073 int i;
7075 if (!map)
7076 return NULL;
7077 if (map->n == 0)
7078 return map;
7079 map = isl_map_compute_divs(map);
7080 map = isl_map_cow(map);
7081 if (!map)
7082 return NULL;
7084 for (i = 1; i < map->n; ++i)
7085 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7086 for (i = 1; i < map->n; ++i)
7087 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7089 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7090 return map;
7093 struct isl_set *isl_set_align_divs(struct isl_set *set)
7095 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7098 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7099 __isl_take isl_map *map)
7101 if (!set || !map)
7102 goto error;
7103 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7104 map = isl_map_intersect_domain(map, set);
7105 set = isl_map_range(map);
7106 return set;
7107 error:
7108 isl_set_free(set);
7109 isl_map_free(map);
7110 return NULL;
7113 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7114 __isl_take isl_map *map)
7116 return isl_map_align_params_map_map_and(set, map, &set_apply);
7119 /* There is no need to cow as removing empty parts doesn't change
7120 * the meaning of the set.
7122 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7124 int i;
7126 if (!map)
7127 return NULL;
7129 for (i = map->n-1; i >= 0; --i) {
7130 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
7131 continue;
7132 isl_basic_map_free(map->p[i]);
7133 if (i != map->n-1) {
7134 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7135 map->p[i] = map->p[map->n-1];
7137 map->n--;
7140 return map;
7143 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7145 return (struct isl_set *)
7146 isl_map_remove_empty_parts((struct isl_map *)set);
7149 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7151 struct isl_basic_map *bmap;
7152 if (!map || map->n == 0)
7153 return NULL;
7154 bmap = map->p[map->n-1];
7155 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7156 return isl_basic_map_copy(bmap);
7159 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7161 return (struct isl_basic_set *)
7162 isl_map_copy_basic_map((struct isl_map *)set);
7165 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7166 __isl_keep isl_basic_map *bmap)
7168 int i;
7170 if (!map || !bmap)
7171 goto error;
7172 for (i = map->n-1; i >= 0; --i) {
7173 if (map->p[i] != bmap)
7174 continue;
7175 map = isl_map_cow(map);
7176 if (!map)
7177 goto error;
7178 isl_basic_map_free(map->p[i]);
7179 if (i != map->n-1) {
7180 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7181 map->p[i] = map->p[map->n-1];
7183 map->n--;
7184 return map;
7186 return map;
7187 error:
7188 isl_map_free(map);
7189 return NULL;
7192 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7193 struct isl_basic_set *bset)
7195 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7196 (struct isl_basic_map *)bset);
7199 /* Given two basic sets bset1 and bset2, compute the maximal difference
7200 * between the values of dimension pos in bset1 and those in bset2
7201 * for any common value of the parameters and dimensions preceding pos.
7203 static enum isl_lp_result basic_set_maximal_difference_at(
7204 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7205 int pos, isl_int *opt)
7207 isl_space *dims;
7208 struct isl_basic_map *bmap1 = NULL;
7209 struct isl_basic_map *bmap2 = NULL;
7210 struct isl_ctx *ctx;
7211 struct isl_vec *obj;
7212 unsigned total;
7213 unsigned nparam;
7214 unsigned dim1, dim2;
7215 enum isl_lp_result res;
7217 if (!bset1 || !bset2)
7218 return isl_lp_error;
7220 nparam = isl_basic_set_n_param(bset1);
7221 dim1 = isl_basic_set_n_dim(bset1);
7222 dim2 = isl_basic_set_n_dim(bset2);
7223 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7224 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7225 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7226 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7227 if (!bmap1 || !bmap2)
7228 goto error;
7229 bmap1 = isl_basic_map_cow(bmap1);
7230 bmap1 = isl_basic_map_extend(bmap1, nparam,
7231 pos, (dim1 - pos) + (dim2 - pos),
7232 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7233 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7234 if (!bmap1)
7235 goto error;
7236 total = isl_basic_map_total_dim(bmap1);
7237 ctx = bmap1->ctx;
7238 obj = isl_vec_alloc(ctx, 1 + total);
7239 isl_seq_clr(obj->block.data, 1 + total);
7240 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7241 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7242 if (!obj)
7243 goto error;
7244 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7245 opt, NULL, NULL);
7246 isl_basic_map_free(bmap1);
7247 isl_vec_free(obj);
7248 return res;
7249 error:
7250 isl_basic_map_free(bmap1);
7251 isl_basic_map_free(bmap2);
7252 return isl_lp_error;
7255 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7256 * for any common value of the parameters and dimensions preceding pos
7257 * in both basic sets, the values of dimension pos in bset1 are
7258 * smaller or larger than those in bset2.
7260 * Returns
7261 * 1 if bset1 follows bset2
7262 * -1 if bset1 precedes bset2
7263 * 0 if bset1 and bset2 are incomparable
7264 * -2 if some error occurred.
7266 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7267 struct isl_basic_set *bset2, int pos)
7269 isl_int opt;
7270 enum isl_lp_result res;
7271 int cmp;
7273 isl_int_init(opt);
7275 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7277 if (res == isl_lp_empty)
7278 cmp = 0;
7279 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7280 res == isl_lp_unbounded)
7281 cmp = 1;
7282 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7283 cmp = -1;
7284 else
7285 cmp = -2;
7287 isl_int_clear(opt);
7288 return cmp;
7291 /* Given two basic sets bset1 and bset2, check whether
7292 * for any common value of the parameters and dimensions preceding pos
7293 * there is a value of dimension pos in bset1 that is larger
7294 * than a value of the same dimension in bset2.
7296 * Return
7297 * 1 if there exists such a pair
7298 * 0 if there is no such pair, but there is a pair of equal values
7299 * -1 otherwise
7300 * -2 if some error occurred.
7302 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7303 __isl_keep isl_basic_set *bset2, int pos)
7305 isl_int opt;
7306 enum isl_lp_result res;
7307 int cmp;
7309 isl_int_init(opt);
7311 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7313 if (res == isl_lp_empty)
7314 cmp = -1;
7315 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7316 res == isl_lp_unbounded)
7317 cmp = 1;
7318 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7319 cmp = -1;
7320 else if (res == isl_lp_ok)
7321 cmp = 0;
7322 else
7323 cmp = -2;
7325 isl_int_clear(opt);
7326 return cmp;
7329 /* Given two sets set1 and set2, check whether
7330 * for any common value of the parameters and dimensions preceding pos
7331 * there is a value of dimension pos in set1 that is larger
7332 * than a value of the same dimension in set2.
7334 * Return
7335 * 1 if there exists such a pair
7336 * 0 if there is no such pair, but there is a pair of equal values
7337 * -1 otherwise
7338 * -2 if some error occurred.
7340 int isl_set_follows_at(__isl_keep isl_set *set1,
7341 __isl_keep isl_set *set2, int pos)
7343 int i, j;
7344 int follows = -1;
7346 if (!set1 || !set2)
7347 return -2;
7349 for (i = 0; i < set1->n; ++i)
7350 for (j = 0; j < set2->n; ++j) {
7351 int f;
7352 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7353 if (f == 1 || f == -2)
7354 return f;
7355 if (f > follows)
7356 follows = f;
7359 return follows;
7362 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7363 unsigned pos, isl_int *val)
7365 int i;
7366 int d;
7367 unsigned total;
7369 if (!bmap)
7370 return -1;
7371 total = isl_basic_map_total_dim(bmap);
7372 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7373 for (; d+1 > pos; --d)
7374 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7375 break;
7376 if (d != pos)
7377 continue;
7378 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7379 return 0;
7380 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7381 return 0;
7382 if (!isl_int_is_one(bmap->eq[i][1+d]))
7383 return 0;
7384 if (val)
7385 isl_int_neg(*val, bmap->eq[i][0]);
7386 return 1;
7388 return 0;
7391 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7392 unsigned pos, isl_int *val)
7394 int i;
7395 isl_int v;
7396 isl_int tmp;
7397 int fixed;
7399 if (!map)
7400 return -1;
7401 if (map->n == 0)
7402 return 0;
7403 if (map->n == 1)
7404 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7405 isl_int_init(v);
7406 isl_int_init(tmp);
7407 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7408 for (i = 1; fixed == 1 && i < map->n; ++i) {
7409 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7410 if (fixed == 1 && isl_int_ne(tmp, v))
7411 fixed = 0;
7413 if (val)
7414 isl_int_set(*val, v);
7415 isl_int_clear(tmp);
7416 isl_int_clear(v);
7417 return fixed;
7420 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7421 unsigned pos, isl_int *val)
7423 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7424 pos, val);
7427 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7428 isl_int *val)
7430 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7433 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7434 enum isl_dim_type type, unsigned pos, isl_int *val)
7436 if (pos >= isl_basic_map_dim(bmap, type))
7437 return -1;
7438 return isl_basic_map_plain_has_fixed_var(bmap,
7439 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7442 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7443 enum isl_dim_type type, unsigned pos, isl_int *val)
7445 if (pos >= isl_map_dim(map, type))
7446 return -1;
7447 return isl_map_plain_has_fixed_var(map,
7448 map_offset(map, type) - 1 + pos, val);
7451 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
7452 enum isl_dim_type type, unsigned pos, isl_int *val)
7454 return isl_map_plain_is_fixed(set, type, pos, val);
7457 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7458 enum isl_dim_type type, unsigned pos, isl_int *val)
7460 return isl_map_plain_is_fixed(map, type, pos, val);
7463 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7464 * then return this fixed value in *val.
7466 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7467 unsigned dim, isl_int *val)
7469 return isl_basic_set_plain_has_fixed_var(bset,
7470 isl_basic_set_n_param(bset) + dim, val);
7473 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7474 * then return this fixed value in *val.
7476 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7477 unsigned dim, isl_int *val)
7479 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7482 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7483 unsigned dim, isl_int *val)
7485 return isl_set_plain_dim_is_fixed(set, dim, val);
7488 /* Check if input variable in has fixed value and if so and if val is not NULL,
7489 * then return this fixed value in *val.
7491 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7492 unsigned in, isl_int *val)
7494 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7497 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7498 * and if val is not NULL, then return this lower bound in *val.
7500 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7501 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7503 int i, i_eq = -1, i_ineq = -1;
7504 isl_int *c;
7505 unsigned total;
7506 unsigned nparam;
7508 if (!bset)
7509 return -1;
7510 total = isl_basic_set_total_dim(bset);
7511 nparam = isl_basic_set_n_param(bset);
7512 for (i = 0; i < bset->n_eq; ++i) {
7513 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7514 continue;
7515 if (i_eq != -1)
7516 return 0;
7517 i_eq = i;
7519 for (i = 0; i < bset->n_ineq; ++i) {
7520 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7521 continue;
7522 if (i_eq != -1 || i_ineq != -1)
7523 return 0;
7524 i_ineq = i;
7526 if (i_eq == -1 && i_ineq == -1)
7527 return 0;
7528 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7529 /* The coefficient should always be one due to normalization. */
7530 if (!isl_int_is_one(c[1+nparam+dim]))
7531 return 0;
7532 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7533 return 0;
7534 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7535 total - nparam - dim - 1) != -1)
7536 return 0;
7537 if (val)
7538 isl_int_neg(*val, c[0]);
7539 return 1;
7542 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7543 unsigned dim, isl_int *val)
7545 int i;
7546 isl_int v;
7547 isl_int tmp;
7548 int fixed;
7550 if (!set)
7551 return -1;
7552 if (set->n == 0)
7553 return 0;
7554 if (set->n == 1)
7555 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7556 dim, val);
7557 isl_int_init(v);
7558 isl_int_init(tmp);
7559 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7560 dim, &v);
7561 for (i = 1; fixed == 1 && i < set->n; ++i) {
7562 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7563 dim, &tmp);
7564 if (fixed == 1 && isl_int_ne(tmp, v))
7565 fixed = 0;
7567 if (val)
7568 isl_int_set(*val, v);
7569 isl_int_clear(tmp);
7570 isl_int_clear(v);
7571 return fixed;
7574 struct constraint {
7575 unsigned size;
7576 isl_int *c;
7579 /* uset_gist depends on constraints without existentially quantified
7580 * variables sorting first.
7582 static int qsort_constraint_cmp(const void *p1, const void *p2)
7584 const struct constraint *c1 = (const struct constraint *)p1;
7585 const struct constraint *c2 = (const struct constraint *)p2;
7586 int l1, l2;
7587 unsigned size = isl_min(c1->size, c2->size);
7589 l1 = isl_seq_last_non_zero(c1->c, size);
7590 l2 = isl_seq_last_non_zero(c2->c, size);
7592 if (l1 != l2)
7593 return l1 - l2;
7595 return isl_seq_cmp(c1->c, c2->c, size);
7598 static struct isl_basic_map *isl_basic_map_sort_constraints(
7599 struct isl_basic_map *bmap)
7601 int i;
7602 struct constraint *c;
7603 unsigned total;
7605 if (!bmap)
7606 return NULL;
7607 total = isl_basic_map_total_dim(bmap);
7608 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7609 if (!c)
7610 goto error;
7611 for (i = 0; i < bmap->n_ineq; ++i) {
7612 c[i].size = total;
7613 c[i].c = bmap->ineq[i];
7615 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7616 for (i = 0; i < bmap->n_ineq; ++i)
7617 bmap->ineq[i] = c[i].c;
7618 free(c);
7619 return bmap;
7620 error:
7621 isl_basic_map_free(bmap);
7622 return NULL;
7625 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7626 __isl_take isl_basic_set *bset)
7628 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7629 (struct isl_basic_map *)bset);
7632 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7634 if (!bmap)
7635 return NULL;
7636 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7637 return bmap;
7638 bmap = isl_basic_map_remove_redundancies(bmap);
7639 bmap = isl_basic_map_sort_constraints(bmap);
7640 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7641 return bmap;
7644 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7646 return (struct isl_basic_set *)isl_basic_map_normalize(
7647 (struct isl_basic_map *)bset);
7650 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7651 const __isl_keep isl_basic_map *bmap2)
7653 int i, cmp;
7654 unsigned total;
7656 if (bmap1 == bmap2)
7657 return 0;
7658 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7659 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7660 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7661 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7662 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7663 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7664 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7665 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7666 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7667 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7668 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7669 return 0;
7670 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7671 return 1;
7672 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7673 return -1;
7674 if (bmap1->n_eq != bmap2->n_eq)
7675 return bmap1->n_eq - bmap2->n_eq;
7676 if (bmap1->n_ineq != bmap2->n_ineq)
7677 return bmap1->n_ineq - bmap2->n_ineq;
7678 if (bmap1->n_div != bmap2->n_div)
7679 return bmap1->n_div - bmap2->n_div;
7680 total = isl_basic_map_total_dim(bmap1);
7681 for (i = 0; i < bmap1->n_eq; ++i) {
7682 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7683 if (cmp)
7684 return cmp;
7686 for (i = 0; i < bmap1->n_ineq; ++i) {
7687 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7688 if (cmp)
7689 return cmp;
7691 for (i = 0; i < bmap1->n_div; ++i) {
7692 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7693 if (cmp)
7694 return cmp;
7696 return 0;
7699 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
7700 const __isl_keep isl_basic_set *bset2)
7702 return isl_basic_map_plain_cmp(bset1, bset2);
7705 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
7706 const __isl_keep isl_set *set2)
7708 int i, cmp;
7710 if (set1 == set2)
7711 return 0;
7712 if (set1->n != set2->n)
7713 return set1->n - set2->n;
7715 for (i = 0; i < set1->n; ++i) {
7716 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
7717 if (cmp)
7718 return cmp;
7721 return 0;
7724 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7725 __isl_keep isl_basic_map *bmap2)
7727 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7730 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7731 __isl_keep isl_basic_set *bset2)
7733 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7734 (isl_basic_map *)bset2);
7737 static int qsort_bmap_cmp(const void *p1, const void *p2)
7739 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7740 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7742 return isl_basic_map_plain_cmp(bmap1, bmap2);
7745 /* We normalize in place, but if anything goes wrong we need
7746 * to return NULL, so we need to make sure we don't change the
7747 * meaning of any possible other copies of map.
7749 struct isl_map *isl_map_normalize(struct isl_map *map)
7751 int i, j;
7752 struct isl_basic_map *bmap;
7754 if (!map)
7755 return NULL;
7756 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7757 return map;
7758 for (i = 0; i < map->n; ++i) {
7759 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7760 if (!bmap)
7761 goto error;
7762 isl_basic_map_free(map->p[i]);
7763 map->p[i] = bmap;
7765 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7766 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7767 map = isl_map_remove_empty_parts(map);
7768 if (!map)
7769 return NULL;
7770 for (i = map->n - 1; i >= 1; --i) {
7771 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7772 continue;
7773 isl_basic_map_free(map->p[i-1]);
7774 for (j = i; j < map->n; ++j)
7775 map->p[j-1] = map->p[j];
7776 map->n--;
7778 return map;
7779 error:
7780 isl_map_free(map);
7781 return NULL;
7785 struct isl_set *isl_set_normalize(struct isl_set *set)
7787 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7790 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7792 int i;
7793 int equal;
7795 if (!map1 || !map2)
7796 return -1;
7798 if (map1 == map2)
7799 return 1;
7800 if (!isl_space_is_equal(map1->dim, map2->dim))
7801 return 0;
7803 map1 = isl_map_copy(map1);
7804 map2 = isl_map_copy(map2);
7805 map1 = isl_map_normalize(map1);
7806 map2 = isl_map_normalize(map2);
7807 if (!map1 || !map2)
7808 goto error;
7809 equal = map1->n == map2->n;
7810 for (i = 0; equal && i < map1->n; ++i) {
7811 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7812 if (equal < 0)
7813 goto error;
7815 isl_map_free(map1);
7816 isl_map_free(map2);
7817 return equal;
7818 error:
7819 isl_map_free(map1);
7820 isl_map_free(map2);
7821 return -1;
7824 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7826 return isl_map_plain_is_equal(map1, map2);
7829 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7831 return isl_map_plain_is_equal((struct isl_map *)set1,
7832 (struct isl_map *)set2);
7835 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7837 return isl_set_plain_is_equal(set1, set2);
7840 /* Return an interval that ranges from min to max (inclusive)
7842 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7843 isl_int min, isl_int max)
7845 int k;
7846 struct isl_basic_set *bset = NULL;
7848 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7849 if (!bset)
7850 goto error;
7852 k = isl_basic_set_alloc_inequality(bset);
7853 if (k < 0)
7854 goto error;
7855 isl_int_set_si(bset->ineq[k][1], 1);
7856 isl_int_neg(bset->ineq[k][0], min);
7858 k = isl_basic_set_alloc_inequality(bset);
7859 if (k < 0)
7860 goto error;
7861 isl_int_set_si(bset->ineq[k][1], -1);
7862 isl_int_set(bset->ineq[k][0], max);
7864 return bset;
7865 error:
7866 isl_basic_set_free(bset);
7867 return NULL;
7870 /* Return the Cartesian product of the basic sets in list (in the given order).
7872 __isl_give isl_basic_set *isl_basic_set_list_product(
7873 __isl_take struct isl_basic_set_list *list)
7875 int i;
7876 unsigned dim;
7877 unsigned nparam;
7878 unsigned extra;
7879 unsigned n_eq;
7880 unsigned n_ineq;
7881 struct isl_basic_set *product = NULL;
7883 if (!list)
7884 goto error;
7885 isl_assert(list->ctx, list->n > 0, goto error);
7886 isl_assert(list->ctx, list->p[0], goto error);
7887 nparam = isl_basic_set_n_param(list->p[0]);
7888 dim = isl_basic_set_n_dim(list->p[0]);
7889 extra = list->p[0]->n_div;
7890 n_eq = list->p[0]->n_eq;
7891 n_ineq = list->p[0]->n_ineq;
7892 for (i = 1; i < list->n; ++i) {
7893 isl_assert(list->ctx, list->p[i], goto error);
7894 isl_assert(list->ctx,
7895 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7896 dim += isl_basic_set_n_dim(list->p[i]);
7897 extra += list->p[i]->n_div;
7898 n_eq += list->p[i]->n_eq;
7899 n_ineq += list->p[i]->n_ineq;
7901 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7902 n_eq, n_ineq);
7903 if (!product)
7904 goto error;
7905 dim = 0;
7906 for (i = 0; i < list->n; ++i) {
7907 isl_basic_set_add_constraints(product,
7908 isl_basic_set_copy(list->p[i]), dim);
7909 dim += isl_basic_set_n_dim(list->p[i]);
7911 isl_basic_set_list_free(list);
7912 return product;
7913 error:
7914 isl_basic_set_free(product);
7915 isl_basic_set_list_free(list);
7916 return NULL;
7919 struct isl_basic_map *isl_basic_map_product(
7920 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7922 isl_space *dim_result = NULL;
7923 struct isl_basic_map *bmap;
7924 unsigned in1, in2, out1, out2, nparam, total, pos;
7925 struct isl_dim_map *dim_map1, *dim_map2;
7927 if (!bmap1 || !bmap2)
7928 goto error;
7930 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
7931 bmap2->dim, isl_dim_param), goto error);
7932 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
7933 isl_space_copy(bmap2->dim));
7935 in1 = isl_basic_map_n_in(bmap1);
7936 in2 = isl_basic_map_n_in(bmap2);
7937 out1 = isl_basic_map_n_out(bmap1);
7938 out2 = isl_basic_map_n_out(bmap2);
7939 nparam = isl_basic_map_n_param(bmap1);
7941 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7942 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7943 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7944 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7945 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7946 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7947 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7948 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7949 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7950 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7951 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7953 bmap = isl_basic_map_alloc_space(dim_result,
7954 bmap1->n_div + bmap2->n_div,
7955 bmap1->n_eq + bmap2->n_eq,
7956 bmap1->n_ineq + bmap2->n_ineq);
7957 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7958 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7959 bmap = isl_basic_map_simplify(bmap);
7960 return isl_basic_map_finalize(bmap);
7961 error:
7962 isl_basic_map_free(bmap1);
7963 isl_basic_map_free(bmap2);
7964 return NULL;
7967 __isl_give isl_basic_map *isl_basic_map_flat_product(
7968 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7970 isl_basic_map *prod;
7972 prod = isl_basic_map_product(bmap1, bmap2);
7973 prod = isl_basic_map_flatten(prod);
7974 return prod;
7977 __isl_give isl_basic_set *isl_basic_set_flat_product(
7978 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7980 return isl_basic_map_flat_range_product(bset1, bset2);
7983 __isl_give isl_basic_map *isl_basic_map_domain_product(
7984 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7986 isl_space *space_result = NULL;
7987 isl_basic_map *bmap;
7988 unsigned in1, in2, out, nparam, total, pos;
7989 struct isl_dim_map *dim_map1, *dim_map2;
7991 if (!bmap1 || !bmap2)
7992 goto error;
7994 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
7995 isl_space_copy(bmap2->dim));
7997 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
7998 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
7999 out = isl_basic_map_dim(bmap1, isl_dim_out);
8000 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8002 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8003 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8004 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8005 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8006 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8007 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8008 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8009 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8010 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8011 isl_dim_map_div(dim_map1, bmap1, pos += out);
8012 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8014 bmap = isl_basic_map_alloc_space(space_result,
8015 bmap1->n_div + bmap2->n_div,
8016 bmap1->n_eq + bmap2->n_eq,
8017 bmap1->n_ineq + bmap2->n_ineq);
8018 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8019 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8020 bmap = isl_basic_map_simplify(bmap);
8021 return isl_basic_map_finalize(bmap);
8022 error:
8023 isl_basic_map_free(bmap1);
8024 isl_basic_map_free(bmap2);
8025 return NULL;
8028 __isl_give isl_basic_map *isl_basic_map_range_product(
8029 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8031 isl_space *dim_result = NULL;
8032 isl_basic_map *bmap;
8033 unsigned in, out1, out2, nparam, total, pos;
8034 struct isl_dim_map *dim_map1, *dim_map2;
8036 if (!bmap1 || !bmap2)
8037 goto error;
8039 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8040 isl_space_copy(bmap2->dim));
8042 in = isl_basic_map_dim(bmap1, isl_dim_in);
8043 out1 = isl_basic_map_n_out(bmap1);
8044 out2 = isl_basic_map_n_out(bmap2);
8045 nparam = isl_basic_map_n_param(bmap1);
8047 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8048 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8049 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8050 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8051 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8052 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8053 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8054 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8055 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8056 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8057 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8059 bmap = isl_basic_map_alloc_space(dim_result,
8060 bmap1->n_div + bmap2->n_div,
8061 bmap1->n_eq + bmap2->n_eq,
8062 bmap1->n_ineq + bmap2->n_ineq);
8063 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8064 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8065 bmap = isl_basic_map_simplify(bmap);
8066 return isl_basic_map_finalize(bmap);
8067 error:
8068 isl_basic_map_free(bmap1);
8069 isl_basic_map_free(bmap2);
8070 return NULL;
8073 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8074 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8076 isl_basic_map *prod;
8078 prod = isl_basic_map_range_product(bmap1, bmap2);
8079 prod = isl_basic_map_flatten_range(prod);
8080 return prod;
8083 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8084 __isl_take isl_map *map2,
8085 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8086 __isl_take isl_space *right),
8087 __isl_give isl_basic_map *(*basic_map_product)(
8088 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8090 unsigned flags = 0;
8091 struct isl_map *result;
8092 int i, j;
8094 if (!map1 || !map2)
8095 goto error;
8097 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8098 map2->dim, isl_dim_param), goto error);
8100 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8101 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8102 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8104 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8105 isl_space_copy(map2->dim)),
8106 map1->n * map2->n, flags);
8107 if (!result)
8108 goto error;
8109 for (i = 0; i < map1->n; ++i)
8110 for (j = 0; j < map2->n; ++j) {
8111 struct isl_basic_map *part;
8112 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8113 isl_basic_map_copy(map2->p[j]));
8114 if (isl_basic_map_is_empty(part))
8115 isl_basic_map_free(part);
8116 else
8117 result = isl_map_add_basic_map(result, part);
8118 if (!result)
8119 goto error;
8121 isl_map_free(map1);
8122 isl_map_free(map2);
8123 return result;
8124 error:
8125 isl_map_free(map1);
8126 isl_map_free(map2);
8127 return NULL;
8130 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8132 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8133 __isl_take isl_map *map2)
8135 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8138 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8139 __isl_take isl_map *map2)
8141 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8144 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8146 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8147 __isl_take isl_map *map2)
8149 isl_map *prod;
8151 prod = isl_map_product(map1, map2);
8152 prod = isl_map_flatten(prod);
8153 return prod;
8156 /* Given two set A and B, construct its Cartesian product A x B.
8158 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8160 return isl_map_range_product(set1, set2);
8163 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8164 __isl_take isl_set *set2)
8166 return isl_map_flat_range_product(set1, set2);
8169 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8171 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8172 __isl_take isl_map *map2)
8174 return map_product(map1, map2, &isl_space_domain_product,
8175 &isl_basic_map_domain_product);
8178 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8180 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8181 __isl_take isl_map *map2)
8183 return map_product(map1, map2, &isl_space_range_product,
8184 &isl_basic_map_range_product);
8187 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8188 __isl_take isl_map *map2)
8190 return isl_map_align_params_map_map_and(map1, map2,
8191 &map_domain_product_aligned);
8194 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8195 __isl_take isl_map *map2)
8197 return isl_map_align_params_map_map_and(map1, map2,
8198 &map_range_product_aligned);
8201 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8203 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8204 __isl_take isl_map *map2)
8206 isl_map *prod;
8208 prod = isl_map_domain_product(map1, map2);
8209 prod = isl_map_flatten_domain(prod);
8210 return prod;
8213 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8215 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8216 __isl_take isl_map *map2)
8218 isl_map *prod;
8220 prod = isl_map_range_product(map1, map2);
8221 prod = isl_map_flatten_range(prod);
8222 return prod;
8225 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8227 int i;
8228 uint32_t hash = isl_hash_init();
8229 unsigned total;
8231 if (!bmap)
8232 return 0;
8233 bmap = isl_basic_map_copy(bmap);
8234 bmap = isl_basic_map_normalize(bmap);
8235 if (!bmap)
8236 return 0;
8237 total = isl_basic_map_total_dim(bmap);
8238 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8239 for (i = 0; i < bmap->n_eq; ++i) {
8240 uint32_t c_hash;
8241 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8242 isl_hash_hash(hash, c_hash);
8244 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8245 for (i = 0; i < bmap->n_ineq; ++i) {
8246 uint32_t c_hash;
8247 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8248 isl_hash_hash(hash, c_hash);
8250 isl_hash_byte(hash, bmap->n_div & 0xFF);
8251 for (i = 0; i < bmap->n_div; ++i) {
8252 uint32_t c_hash;
8253 if (isl_int_is_zero(bmap->div[i][0]))
8254 continue;
8255 isl_hash_byte(hash, i & 0xFF);
8256 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8257 isl_hash_hash(hash, c_hash);
8259 isl_basic_map_free(bmap);
8260 return hash;
8263 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8265 return isl_basic_map_get_hash((isl_basic_map *)bset);
8268 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8270 int i;
8271 uint32_t hash;
8273 if (!map)
8274 return 0;
8275 map = isl_map_copy(map);
8276 map = isl_map_normalize(map);
8277 if (!map)
8278 return 0;
8280 hash = isl_hash_init();
8281 for (i = 0; i < map->n; ++i) {
8282 uint32_t bmap_hash;
8283 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8284 isl_hash_hash(hash, bmap_hash);
8287 isl_map_free(map);
8289 return hash;
8292 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8294 return isl_map_get_hash((isl_map *)set);
8297 /* Check if the value for dimension dim is completely determined
8298 * by the values of the other parameters and variables.
8299 * That is, check if dimension dim is involved in an equality.
8301 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8303 int i;
8304 unsigned nparam;
8306 if (!bset)
8307 return -1;
8308 nparam = isl_basic_set_n_param(bset);
8309 for (i = 0; i < bset->n_eq; ++i)
8310 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8311 return 1;
8312 return 0;
8315 /* Check if the value for dimension dim is completely determined
8316 * by the values of the other parameters and variables.
8317 * That is, check if dimension dim is involved in an equality
8318 * for each of the subsets.
8320 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8322 int i;
8324 if (!set)
8325 return -1;
8326 for (i = 0; i < set->n; ++i) {
8327 int unique;
8328 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8329 if (unique != 1)
8330 return unique;
8332 return 1;
8335 int isl_set_n_basic_set(__isl_keep isl_set *set)
8337 return set ? set->n : 0;
8340 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8341 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8343 int i;
8345 if (!map)
8346 return -1;
8348 for (i = 0; i < map->n; ++i)
8349 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8350 return -1;
8352 return 0;
8355 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8356 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8358 int i;
8360 if (!set)
8361 return -1;
8363 for (i = 0; i < set->n; ++i)
8364 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8365 return -1;
8367 return 0;
8370 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8372 isl_space *dim;
8374 if (!bset)
8375 return NULL;
8377 bset = isl_basic_set_cow(bset);
8378 if (!bset)
8379 return NULL;
8381 dim = isl_basic_set_get_space(bset);
8382 dim = isl_space_lift(dim, bset->n_div);
8383 if (!dim)
8384 goto error;
8385 isl_space_free(bset->dim);
8386 bset->dim = dim;
8387 bset->extra -= bset->n_div;
8388 bset->n_div = 0;
8390 bset = isl_basic_set_finalize(bset);
8392 return bset;
8393 error:
8394 isl_basic_set_free(bset);
8395 return NULL;
8398 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8400 int i;
8401 isl_space *dim;
8402 unsigned n_div;
8404 set = isl_set_align_divs(set);
8406 if (!set)
8407 return NULL;
8409 set = isl_set_cow(set);
8410 if (!set)
8411 return NULL;
8413 n_div = set->p[0]->n_div;
8414 dim = isl_set_get_space(set);
8415 dim = isl_space_lift(dim, n_div);
8416 if (!dim)
8417 goto error;
8418 isl_space_free(set->dim);
8419 set->dim = dim;
8421 for (i = 0; i < set->n; ++i) {
8422 set->p[i] = isl_basic_set_lift(set->p[i]);
8423 if (!set->p[i])
8424 goto error;
8427 return set;
8428 error:
8429 isl_set_free(set);
8430 return NULL;
8433 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8435 isl_space *dim;
8436 struct isl_basic_map *bmap;
8437 unsigned n_set;
8438 unsigned n_div;
8439 unsigned n_param;
8440 unsigned total;
8441 int i, k, l;
8443 set = isl_set_align_divs(set);
8445 if (!set)
8446 return NULL;
8448 dim = isl_set_get_space(set);
8449 if (set->n == 0 || set->p[0]->n_div == 0) {
8450 isl_set_free(set);
8451 return isl_map_identity(isl_space_map_from_set(dim));
8454 n_div = set->p[0]->n_div;
8455 dim = isl_space_map_from_set(dim);
8456 n_param = isl_space_dim(dim, isl_dim_param);
8457 n_set = isl_space_dim(dim, isl_dim_in);
8458 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8459 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8460 for (i = 0; i < n_set; ++i)
8461 bmap = var_equal(bmap, i);
8463 total = n_param + n_set + n_set + n_div;
8464 for (i = 0; i < n_div; ++i) {
8465 k = isl_basic_map_alloc_inequality(bmap);
8466 if (k < 0)
8467 goto error;
8468 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8469 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8470 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8471 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8472 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8473 set->p[0]->div[i][0]);
8475 l = isl_basic_map_alloc_inequality(bmap);
8476 if (l < 0)
8477 goto error;
8478 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8479 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8480 set->p[0]->div[i][0]);
8481 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8484 isl_set_free(set);
8485 bmap = isl_basic_map_simplify(bmap);
8486 bmap = isl_basic_map_finalize(bmap);
8487 return isl_map_from_basic_map(bmap);
8488 error:
8489 isl_set_free(set);
8490 isl_basic_map_free(bmap);
8491 return NULL;
8494 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8496 unsigned dim;
8497 int size = 0;
8499 if (!bset)
8500 return -1;
8502 dim = isl_basic_set_total_dim(bset);
8503 size += bset->n_eq * (1 + dim);
8504 size += bset->n_ineq * (1 + dim);
8505 size += bset->n_div * (2 + dim);
8507 return size;
8510 int isl_set_size(__isl_keep isl_set *set)
8512 int i;
8513 int size = 0;
8515 if (!set)
8516 return -1;
8518 for (i = 0; i < set->n; ++i)
8519 size += isl_basic_set_size(set->p[i]);
8521 return size;
8524 /* Check if there is any lower bound (if lower == 0) and/or upper
8525 * bound (if upper == 0) on the specified dim.
8527 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8528 enum isl_dim_type type, unsigned pos, int lower, int upper)
8530 int i;
8532 if (!bmap)
8533 return -1;
8535 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8537 pos += isl_basic_map_offset(bmap, type);
8539 for (i = 0; i < bmap->n_div; ++i) {
8540 if (isl_int_is_zero(bmap->div[i][0]))
8541 continue;
8542 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8543 return 1;
8546 for (i = 0; i < bmap->n_eq; ++i)
8547 if (!isl_int_is_zero(bmap->eq[i][pos]))
8548 return 1;
8550 for (i = 0; i < bmap->n_ineq; ++i) {
8551 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8552 if (sgn > 0)
8553 lower = 1;
8554 if (sgn < 0)
8555 upper = 1;
8558 return lower && upper;
8561 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8562 enum isl_dim_type type, unsigned pos)
8564 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8567 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8568 enum isl_dim_type type, unsigned pos)
8570 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8573 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8574 enum isl_dim_type type, unsigned pos)
8576 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8579 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8580 enum isl_dim_type type, unsigned pos)
8582 int i;
8584 if (!map)
8585 return -1;
8587 for (i = 0; i < map->n; ++i) {
8588 int bounded;
8589 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8590 if (bounded < 0 || !bounded)
8591 return bounded;
8594 return 1;
8597 /* Return 1 if the specified dim is involved in both an upper bound
8598 * and a lower bound.
8600 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8601 enum isl_dim_type type, unsigned pos)
8603 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8606 static int has_bound(__isl_keep isl_map *map,
8607 enum isl_dim_type type, unsigned pos,
8608 int (*fn)(__isl_keep isl_basic_map *bmap,
8609 enum isl_dim_type type, unsigned pos))
8611 int i;
8613 if (!map)
8614 return -1;
8616 for (i = 0; i < map->n; ++i) {
8617 int bounded;
8618 bounded = fn(map->p[i], type, pos);
8619 if (bounded < 0 || bounded)
8620 return bounded;
8623 return 0;
8626 /* Return 1 if the specified dim is involved in any lower bound.
8628 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8629 enum isl_dim_type type, unsigned pos)
8631 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8634 /* Return 1 if the specified dim is involved in any upper bound.
8636 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8637 enum isl_dim_type type, unsigned pos)
8639 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8642 /* For each of the "n" variables starting at "first", determine
8643 * the sign of the variable and put the results in the first "n"
8644 * elements of the array "signs".
8645 * Sign
8646 * 1 means that the variable is non-negative
8647 * -1 means that the variable is non-positive
8648 * 0 means the variable attains both positive and negative values.
8650 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8651 unsigned first, unsigned n, int *signs)
8653 isl_vec *bound = NULL;
8654 struct isl_tab *tab = NULL;
8655 struct isl_tab_undo *snap;
8656 int i;
8658 if (!bset || !signs)
8659 return -1;
8661 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8662 tab = isl_tab_from_basic_set(bset);
8663 if (!bound || !tab)
8664 goto error;
8666 isl_seq_clr(bound->el, bound->size);
8667 isl_int_set_si(bound->el[0], -1);
8669 snap = isl_tab_snap(tab);
8670 for (i = 0; i < n; ++i) {
8671 int empty;
8673 isl_int_set_si(bound->el[1 + first + i], -1);
8674 if (isl_tab_add_ineq(tab, bound->el) < 0)
8675 goto error;
8676 empty = tab->empty;
8677 isl_int_set_si(bound->el[1 + first + i], 0);
8678 if (isl_tab_rollback(tab, snap) < 0)
8679 goto error;
8681 if (empty) {
8682 signs[i] = 1;
8683 continue;
8686 isl_int_set_si(bound->el[1 + first + i], 1);
8687 if (isl_tab_add_ineq(tab, bound->el) < 0)
8688 goto error;
8689 empty = tab->empty;
8690 isl_int_set_si(bound->el[1 + first + i], 0);
8691 if (isl_tab_rollback(tab, snap) < 0)
8692 goto error;
8694 signs[i] = empty ? -1 : 0;
8697 isl_tab_free(tab);
8698 isl_vec_free(bound);
8699 return 0;
8700 error:
8701 isl_tab_free(tab);
8702 isl_vec_free(bound);
8703 return -1;
8706 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8707 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8709 if (!bset || !signs)
8710 return -1;
8711 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8712 return -1);
8714 first += pos(bset->dim, type) - 1;
8715 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8718 /* Check if the given basic map is obviously single-valued.
8719 * In particular, for each output dimension, check that there is
8720 * an equality that defines the output dimension in terms of
8721 * earlier dimensions.
8723 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8725 int i, j;
8726 unsigned total;
8727 unsigned n_out;
8728 unsigned o_out;
8730 if (!bmap)
8731 return -1;
8733 total = 1 + isl_basic_map_total_dim(bmap);
8734 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8735 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8737 for (i = 0; i < n_out; ++i) {
8738 for (j = 0; j < bmap->n_eq; ++j) {
8739 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8740 continue;
8741 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8742 total - (o_out + i + 1)) == -1)
8743 break;
8745 if (j >= bmap->n_eq)
8746 return 0;
8749 return 1;
8752 /* Check if the given map is obviously single-valued.
8754 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8756 if (!map)
8757 return -1;
8758 if (map->n == 0)
8759 return 1;
8760 if (map->n >= 2)
8761 return 0;
8763 return isl_basic_map_plain_is_single_valued(map->p[0]);
8766 /* Check if the given map is single-valued.
8767 * We simply compute
8769 * M \circ M^-1
8771 * and check if the result is a subset of the identity mapping.
8773 int isl_map_is_single_valued(__isl_keep isl_map *map)
8775 isl_space *dim;
8776 isl_map *test;
8777 isl_map *id;
8778 int sv;
8780 sv = isl_map_plain_is_single_valued(map);
8781 if (sv < 0 || sv)
8782 return sv;
8784 test = isl_map_reverse(isl_map_copy(map));
8785 test = isl_map_apply_range(test, isl_map_copy(map));
8787 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8788 id = isl_map_identity(dim);
8790 sv = isl_map_is_subset(test, id);
8792 isl_map_free(test);
8793 isl_map_free(id);
8795 return sv;
8798 int isl_map_is_injective(__isl_keep isl_map *map)
8800 int in;
8802 map = isl_map_copy(map);
8803 map = isl_map_reverse(map);
8804 in = isl_map_is_single_valued(map);
8805 isl_map_free(map);
8807 return in;
8810 /* Check if the given map is obviously injective.
8812 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8814 int in;
8816 map = isl_map_copy(map);
8817 map = isl_map_reverse(map);
8818 in = isl_map_plain_is_single_valued(map);
8819 isl_map_free(map);
8821 return in;
8824 int isl_map_is_bijective(__isl_keep isl_map *map)
8826 int sv;
8828 sv = isl_map_is_single_valued(map);
8829 if (sv < 0 || !sv)
8830 return sv;
8832 return isl_map_is_injective(map);
8835 int isl_set_is_singleton(__isl_keep isl_set *set)
8837 return isl_map_is_single_valued((isl_map *)set);
8840 int isl_map_is_translation(__isl_keep isl_map *map)
8842 int ok;
8843 isl_set *delta;
8845 delta = isl_map_deltas(isl_map_copy(map));
8846 ok = isl_set_is_singleton(delta);
8847 isl_set_free(delta);
8849 return ok;
8852 static int unique(isl_int *p, unsigned pos, unsigned len)
8854 if (isl_seq_first_non_zero(p, pos) != -1)
8855 return 0;
8856 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8857 return 0;
8858 return 1;
8861 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8863 int i, j;
8864 unsigned nvar;
8865 unsigned ovar;
8867 if (!bset)
8868 return -1;
8870 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8871 return 0;
8873 nvar = isl_basic_set_dim(bset, isl_dim_set);
8874 ovar = isl_space_offset(bset->dim, isl_dim_set);
8875 for (j = 0; j < nvar; ++j) {
8876 int lower = 0, upper = 0;
8877 for (i = 0; i < bset->n_eq; ++i) {
8878 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8879 continue;
8880 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8881 return 0;
8882 break;
8884 if (i < bset->n_eq)
8885 continue;
8886 for (i = 0; i < bset->n_ineq; ++i) {
8887 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8888 continue;
8889 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8890 return 0;
8891 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8892 lower = 1;
8893 else
8894 upper = 1;
8896 if (!lower || !upper)
8897 return 0;
8900 return 1;
8903 int isl_set_is_box(__isl_keep isl_set *set)
8905 if (!set)
8906 return -1;
8907 if (set->n != 1)
8908 return 0;
8910 return isl_basic_set_is_box(set->p[0]);
8913 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8915 if (!bset)
8916 return -1;
8918 return isl_space_is_wrapping(bset->dim);
8921 int isl_set_is_wrapping(__isl_keep isl_set *set)
8923 if (!set)
8924 return -1;
8926 return isl_space_is_wrapping(set->dim);
8929 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8931 bmap = isl_basic_map_cow(bmap);
8932 if (!bmap)
8933 return NULL;
8935 bmap->dim = isl_space_wrap(bmap->dim);
8936 if (!bmap->dim)
8937 goto error;
8939 bmap = isl_basic_map_finalize(bmap);
8941 return (isl_basic_set *)bmap;
8942 error:
8943 isl_basic_map_free(bmap);
8944 return NULL;
8947 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8949 int i;
8951 map = isl_map_cow(map);
8952 if (!map)
8953 return NULL;
8955 for (i = 0; i < map->n; ++i) {
8956 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8957 if (!map->p[i])
8958 goto error;
8960 map->dim = isl_space_wrap(map->dim);
8961 if (!map->dim)
8962 goto error;
8964 return (isl_set *)map;
8965 error:
8966 isl_map_free(map);
8967 return NULL;
8970 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8972 bset = isl_basic_set_cow(bset);
8973 if (!bset)
8974 return NULL;
8976 bset->dim = isl_space_unwrap(bset->dim);
8977 if (!bset->dim)
8978 goto error;
8980 bset = isl_basic_set_finalize(bset);
8982 return (isl_basic_map *)bset;
8983 error:
8984 isl_basic_set_free(bset);
8985 return NULL;
8988 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8990 int i;
8992 if (!set)
8993 return NULL;
8995 if (!isl_set_is_wrapping(set))
8996 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8997 goto error);
8999 set = isl_set_cow(set);
9000 if (!set)
9001 return NULL;
9003 for (i = 0; i < set->n; ++i) {
9004 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9005 if (!set->p[i])
9006 goto error;
9009 set->dim = isl_space_unwrap(set->dim);
9010 if (!set->dim)
9011 goto error;
9013 return (isl_map *)set;
9014 error:
9015 isl_set_free(set);
9016 return NULL;
9019 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9020 enum isl_dim_type type)
9022 if (!bmap)
9023 return NULL;
9025 if (!isl_space_is_named_or_nested(bmap->dim, type))
9026 return bmap;
9028 bmap = isl_basic_map_cow(bmap);
9029 if (!bmap)
9030 return NULL;
9032 bmap->dim = isl_space_reset(bmap->dim, type);
9033 if (!bmap->dim)
9034 goto error;
9036 bmap = isl_basic_map_finalize(bmap);
9038 return bmap;
9039 error:
9040 isl_basic_map_free(bmap);
9041 return NULL;
9044 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9045 enum isl_dim_type type)
9047 int i;
9049 if (!map)
9050 return NULL;
9052 if (!isl_space_is_named_or_nested(map->dim, type))
9053 return map;
9055 map = isl_map_cow(map);
9056 if (!map)
9057 return NULL;
9059 for (i = 0; i < map->n; ++i) {
9060 map->p[i] = isl_basic_map_reset(map->p[i], type);
9061 if (!map->p[i])
9062 goto error;
9064 map->dim = isl_space_reset(map->dim, type);
9065 if (!map->dim)
9066 goto error;
9068 return map;
9069 error:
9070 isl_map_free(map);
9071 return NULL;
9074 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9076 if (!bmap)
9077 return NULL;
9079 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9080 return bmap;
9082 bmap = isl_basic_map_cow(bmap);
9083 if (!bmap)
9084 return NULL;
9086 bmap->dim = isl_space_flatten(bmap->dim);
9087 if (!bmap->dim)
9088 goto error;
9090 bmap = isl_basic_map_finalize(bmap);
9092 return bmap;
9093 error:
9094 isl_basic_map_free(bmap);
9095 return NULL;
9098 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9100 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9103 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9104 __isl_take isl_basic_map *bmap)
9106 if (!bmap)
9107 return NULL;
9109 if (!bmap->dim->nested[0])
9110 return bmap;
9112 bmap = isl_basic_map_cow(bmap);
9113 if (!bmap)
9114 return NULL;
9116 bmap->dim = isl_space_flatten_domain(bmap->dim);
9117 if (!bmap->dim)
9118 goto error;
9120 bmap = isl_basic_map_finalize(bmap);
9122 return bmap;
9123 error:
9124 isl_basic_map_free(bmap);
9125 return NULL;
9128 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9129 __isl_take isl_basic_map *bmap)
9131 if (!bmap)
9132 return NULL;
9134 if (!bmap->dim->nested[1])
9135 return bmap;
9137 bmap = isl_basic_map_cow(bmap);
9138 if (!bmap)
9139 return NULL;
9141 bmap->dim = isl_space_flatten_range(bmap->dim);
9142 if (!bmap->dim)
9143 goto error;
9145 bmap = isl_basic_map_finalize(bmap);
9147 return bmap;
9148 error:
9149 isl_basic_map_free(bmap);
9150 return NULL;
9153 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9155 int i;
9157 if (!map)
9158 return NULL;
9160 if (!map->dim->nested[0] && !map->dim->nested[1])
9161 return map;
9163 map = isl_map_cow(map);
9164 if (!map)
9165 return NULL;
9167 for (i = 0; i < map->n; ++i) {
9168 map->p[i] = isl_basic_map_flatten(map->p[i]);
9169 if (!map->p[i])
9170 goto error;
9172 map->dim = isl_space_flatten(map->dim);
9173 if (!map->dim)
9174 goto error;
9176 return map;
9177 error:
9178 isl_map_free(map);
9179 return NULL;
9182 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9184 return (isl_set *)isl_map_flatten((isl_map *)set);
9187 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9189 isl_space *dim, *flat_dim;
9190 isl_map *map;
9192 dim = isl_set_get_space(set);
9193 flat_dim = isl_space_flatten(isl_space_copy(dim));
9194 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9195 map = isl_map_intersect_domain(map, set);
9197 return map;
9200 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9202 int i;
9204 if (!map)
9205 return NULL;
9207 if (!map->dim->nested[0])
9208 return map;
9210 map = isl_map_cow(map);
9211 if (!map)
9212 return NULL;
9214 for (i = 0; i < map->n; ++i) {
9215 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9216 if (!map->p[i])
9217 goto error;
9219 map->dim = isl_space_flatten_domain(map->dim);
9220 if (!map->dim)
9221 goto error;
9223 return map;
9224 error:
9225 isl_map_free(map);
9226 return NULL;
9229 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9231 int i;
9233 if (!map)
9234 return NULL;
9236 if (!map->dim->nested[1])
9237 return map;
9239 map = isl_map_cow(map);
9240 if (!map)
9241 return NULL;
9243 for (i = 0; i < map->n; ++i) {
9244 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9245 if (!map->p[i])
9246 goto error;
9248 map->dim = isl_space_flatten_range(map->dim);
9249 if (!map->dim)
9250 goto error;
9252 return map;
9253 error:
9254 isl_map_free(map);
9255 return NULL;
9258 /* Reorder the dimensions of "bmap" according to the given dim_map
9259 * and set the dimension specification to "dim".
9261 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9262 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9264 isl_basic_map *res;
9266 bmap = isl_basic_map_cow(bmap);
9267 if (!bmap || !dim || !dim_map)
9268 goto error;
9270 res = isl_basic_map_alloc_space(dim,
9271 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9272 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9273 res = isl_basic_map_finalize(res);
9274 return res;
9275 error:
9276 free(dim_map);
9277 isl_basic_map_free(bmap);
9278 isl_space_free(dim);
9279 return NULL;
9282 /* Reorder the dimensions of "map" according to given reordering.
9284 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9285 __isl_take isl_reordering *r)
9287 int i;
9288 struct isl_dim_map *dim_map;
9290 map = isl_map_cow(map);
9291 dim_map = isl_dim_map_from_reordering(r);
9292 if (!map || !r || !dim_map)
9293 goto error;
9295 for (i = 0; i < map->n; ++i) {
9296 struct isl_dim_map *dim_map_i;
9298 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9300 map->p[i] = isl_basic_map_realign(map->p[i],
9301 isl_space_copy(r->dim), dim_map_i);
9303 if (!map->p[i])
9304 goto error;
9307 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9309 isl_reordering_free(r);
9310 free(dim_map);
9311 return map;
9312 error:
9313 free(dim_map);
9314 isl_map_free(map);
9315 isl_reordering_free(r);
9316 return NULL;
9319 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9320 __isl_take isl_reordering *r)
9322 return (isl_set *)isl_map_realign((isl_map *)set, r);
9325 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9326 __isl_take isl_space *model)
9328 isl_ctx *ctx;
9330 if (!map || !model)
9331 goto error;
9333 ctx = isl_space_get_ctx(model);
9334 if (!isl_space_has_named_params(model))
9335 isl_die(ctx, isl_error_invalid,
9336 "model has unnamed parameters", goto error);
9337 if (!isl_space_has_named_params(map->dim))
9338 isl_die(ctx, isl_error_invalid,
9339 "relation has unnamed parameters", goto error);
9340 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9341 isl_reordering *exp;
9343 model = isl_space_drop_dims(model, isl_dim_in,
9344 0, isl_space_dim(model, isl_dim_in));
9345 model = isl_space_drop_dims(model, isl_dim_out,
9346 0, isl_space_dim(model, isl_dim_out));
9347 exp = isl_parameter_alignment_reordering(map->dim, model);
9348 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9349 map = isl_map_realign(map, exp);
9352 isl_space_free(model);
9353 return map;
9354 error:
9355 isl_space_free(model);
9356 isl_map_free(map);
9357 return NULL;
9360 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9361 __isl_take isl_space *model)
9363 return isl_map_align_params(set, model);
9366 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9367 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9368 enum isl_dim_type c2, enum isl_dim_type c3,
9369 enum isl_dim_type c4, enum isl_dim_type c5)
9371 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9372 struct isl_mat *mat;
9373 int i, j, k;
9374 int pos;
9376 if (!bmap)
9377 return NULL;
9378 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9379 isl_basic_map_total_dim(bmap) + 1);
9380 if (!mat)
9381 return NULL;
9382 for (i = 0; i < bmap->n_eq; ++i)
9383 for (j = 0, pos = 0; j < 5; ++j) {
9384 int off = isl_basic_map_offset(bmap, c[j]);
9385 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9386 isl_int_set(mat->row[i][pos],
9387 bmap->eq[i][off + k]);
9388 ++pos;
9392 return mat;
9395 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9396 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9397 enum isl_dim_type c2, enum isl_dim_type c3,
9398 enum isl_dim_type c4, enum isl_dim_type c5)
9400 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9401 struct isl_mat *mat;
9402 int i, j, k;
9403 int pos;
9405 if (!bmap)
9406 return NULL;
9407 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9408 isl_basic_map_total_dim(bmap) + 1);
9409 if (!mat)
9410 return NULL;
9411 for (i = 0; i < bmap->n_ineq; ++i)
9412 for (j = 0, pos = 0; j < 5; ++j) {
9413 int off = isl_basic_map_offset(bmap, c[j]);
9414 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9415 isl_int_set(mat->row[i][pos],
9416 bmap->ineq[i][off + k]);
9417 ++pos;
9421 return mat;
9424 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9425 __isl_take isl_space *dim,
9426 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9427 enum isl_dim_type c2, enum isl_dim_type c3,
9428 enum isl_dim_type c4, enum isl_dim_type c5)
9430 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9431 isl_basic_map *bmap;
9432 unsigned total;
9433 unsigned extra;
9434 int i, j, k, l;
9435 int pos;
9437 if (!dim || !eq || !ineq)
9438 goto error;
9440 if (eq->n_col != ineq->n_col)
9441 isl_die(dim->ctx, isl_error_invalid,
9442 "equalities and inequalities matrices should have "
9443 "same number of columns", goto error);
9445 total = 1 + isl_space_dim(dim, isl_dim_all);
9447 if (eq->n_col < total)
9448 isl_die(dim->ctx, isl_error_invalid,
9449 "number of columns too small", goto error);
9451 extra = eq->n_col - total;
9453 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9454 eq->n_row, ineq->n_row);
9455 if (!bmap)
9456 goto error;
9457 for (i = 0; i < extra; ++i) {
9458 k = isl_basic_map_alloc_div(bmap);
9459 if (k < 0)
9460 goto error;
9461 isl_int_set_si(bmap->div[k][0], 0);
9463 for (i = 0; i < eq->n_row; ++i) {
9464 l = isl_basic_map_alloc_equality(bmap);
9465 if (l < 0)
9466 goto error;
9467 for (j = 0, pos = 0; j < 5; ++j) {
9468 int off = isl_basic_map_offset(bmap, c[j]);
9469 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9470 isl_int_set(bmap->eq[l][off + k],
9471 eq->row[i][pos]);
9472 ++pos;
9476 for (i = 0; i < ineq->n_row; ++i) {
9477 l = isl_basic_map_alloc_inequality(bmap);
9478 if (l < 0)
9479 goto error;
9480 for (j = 0, pos = 0; j < 5; ++j) {
9481 int off = isl_basic_map_offset(bmap, c[j]);
9482 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9483 isl_int_set(bmap->ineq[l][off + k],
9484 ineq->row[i][pos]);
9485 ++pos;
9490 isl_space_free(dim);
9491 isl_mat_free(eq);
9492 isl_mat_free(ineq);
9494 return bmap;
9495 error:
9496 isl_space_free(dim);
9497 isl_mat_free(eq);
9498 isl_mat_free(ineq);
9499 return NULL;
9502 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9503 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9504 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9506 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9507 c1, c2, c3, c4, isl_dim_in);
9510 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9511 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9512 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9514 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9515 c1, c2, c3, c4, isl_dim_in);
9518 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9519 __isl_take isl_space *dim,
9520 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9521 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9523 return (isl_basic_set*)
9524 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9525 c1, c2, c3, c4, isl_dim_in);
9528 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9530 if (!bmap)
9531 return -1;
9533 return isl_space_can_zip(bmap->dim);
9536 int isl_map_can_zip(__isl_keep isl_map *map)
9538 if (!map)
9539 return -1;
9541 return isl_space_can_zip(map->dim);
9544 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9545 * (A -> C) -> (B -> D).
9547 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9549 unsigned pos;
9550 unsigned n1;
9551 unsigned n2;
9553 if (!bmap)
9554 return NULL;
9556 if (!isl_basic_map_can_zip(bmap))
9557 isl_die(bmap->ctx, isl_error_invalid,
9558 "basic map cannot be zipped", goto error);
9559 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9560 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9561 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9562 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9563 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9564 if (!bmap)
9565 return NULL;
9566 bmap->dim = isl_space_zip(bmap->dim);
9567 if (!bmap->dim)
9568 goto error;
9569 return bmap;
9570 error:
9571 isl_basic_map_free(bmap);
9572 return NULL;
9575 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9576 * (A -> C) -> (B -> D).
9578 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9580 int i;
9582 if (!map)
9583 return NULL;
9585 if (!isl_map_can_zip(map))
9586 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9587 goto error);
9589 map = isl_map_cow(map);
9590 if (!map)
9591 return NULL;
9593 for (i = 0; i < map->n; ++i) {
9594 map->p[i] = isl_basic_map_zip(map->p[i]);
9595 if (!map->p[i])
9596 goto error;
9599 map->dim = isl_space_zip(map->dim);
9600 if (!map->dim)
9601 goto error;
9603 return map;
9604 error:
9605 isl_map_free(map);
9606 return NULL;
9609 /* Construct a basic map mapping the domain of the affine expression
9610 * to a one-dimensional range prescribed by the affine expression.
9612 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9614 int k;
9615 int pos;
9616 isl_local_space *ls;
9617 isl_basic_map *bmap;
9619 if (!aff)
9620 return NULL;
9622 ls = isl_aff_get_local_space(aff);
9623 bmap = isl_basic_map_from_local_space(ls);
9624 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9625 k = isl_basic_map_alloc_equality(bmap);
9626 if (k < 0)
9627 goto error;
9629 pos = isl_basic_map_offset(bmap, isl_dim_out);
9630 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9631 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9632 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9633 aff->v->size - (pos + 1));
9635 isl_aff_free(aff);
9636 bmap = isl_basic_map_finalize(bmap);
9637 return bmap;
9638 error:
9639 isl_aff_free(aff);
9640 isl_basic_map_free(bmap);
9641 return NULL;
9644 /* Construct a basic map mapping the domain the multi-affine expression
9645 * to its range, with each dimension in the range equated to the
9646 * corresponding affine expression.
9648 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
9649 __isl_take isl_multi_aff *maff)
9651 int i;
9652 isl_space *space;
9653 isl_basic_map *bmap;
9655 if (!maff)
9656 return NULL;
9658 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
9659 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
9660 "invalid space", return isl_multi_aff_free(maff));
9662 space = isl_space_domain(isl_multi_aff_get_space(maff));
9663 bmap = isl_basic_map_universe(isl_space_from_domain(space));
9665 for (i = 0; i < maff->n; ++i) {
9666 isl_aff *aff;
9667 isl_basic_map *bmap_i;
9669 aff = isl_aff_copy(maff->p[i]);
9670 bmap_i = isl_basic_map_from_aff(aff);
9672 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9675 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
9677 isl_multi_aff_free(maff);
9678 return bmap;
9681 /* Construct a basic map mapping a domain in the given space to
9682 * to an n-dimensional range, with n the number of elements in the list,
9683 * where each coordinate in the range is prescribed by the
9684 * corresponding affine expression.
9685 * The domains of all affine expressions in the list are assumed to match
9686 * domain_dim.
9688 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9689 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9691 int i;
9692 isl_space *dim;
9693 isl_basic_map *bmap;
9695 if (!list)
9696 return NULL;
9698 dim = isl_space_from_domain(domain_dim);
9699 bmap = isl_basic_map_universe(dim);
9701 for (i = 0; i < list->n; ++i) {
9702 isl_aff *aff;
9703 isl_basic_map *bmap_i;
9705 aff = isl_aff_copy(list->p[i]);
9706 bmap_i = isl_basic_map_from_aff(aff);
9708 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9711 isl_aff_list_free(list);
9712 return bmap;
9715 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9716 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9718 return isl_map_equate(set, type1, pos1, type2, pos2);
9721 /* Add a constraint imposing that the given two dimensions are equal.
9723 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9724 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9726 isl_basic_map *bmap = NULL;
9727 int i;
9729 if (!map)
9730 return NULL;
9732 if (pos1 >= isl_map_dim(map, type1))
9733 isl_die(map->ctx, isl_error_invalid,
9734 "index out of bounds", goto error);
9735 if (pos2 >= isl_map_dim(map, type2))
9736 isl_die(map->ctx, isl_error_invalid,
9737 "index out of bounds", goto error);
9739 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9740 i = isl_basic_map_alloc_equality(bmap);
9741 if (i < 0)
9742 goto error;
9743 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9744 pos1 += isl_basic_map_offset(bmap, type1);
9745 pos2 += isl_basic_map_offset(bmap, type2);
9746 isl_int_set_si(bmap->eq[i][pos1], -1);
9747 isl_int_set_si(bmap->eq[i][pos2], 1);
9748 bmap = isl_basic_map_finalize(bmap);
9750 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9752 return map;
9753 error:
9754 isl_basic_map_free(bmap);
9755 isl_map_free(map);
9756 return NULL;
9759 /* Add a constraint imposing that the given two dimensions have opposite values.
9761 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9762 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9764 isl_basic_map *bmap = NULL;
9765 int i;
9767 if (!map)
9768 return NULL;
9770 if (pos1 >= isl_map_dim(map, type1))
9771 isl_die(map->ctx, isl_error_invalid,
9772 "index out of bounds", goto error);
9773 if (pos2 >= isl_map_dim(map, type2))
9774 isl_die(map->ctx, isl_error_invalid,
9775 "index out of bounds", goto error);
9777 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9778 i = isl_basic_map_alloc_equality(bmap);
9779 if (i < 0)
9780 goto error;
9781 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9782 pos1 += isl_basic_map_offset(bmap, type1);
9783 pos2 += isl_basic_map_offset(bmap, type2);
9784 isl_int_set_si(bmap->eq[i][pos1], 1);
9785 isl_int_set_si(bmap->eq[i][pos2], 1);
9786 bmap = isl_basic_map_finalize(bmap);
9788 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9790 return map;
9791 error:
9792 isl_basic_map_free(bmap);
9793 isl_map_free(map);
9794 return NULL;
9797 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
9798 int pos)
9800 isl_aff *div;
9801 isl_local_space *ls;
9803 if (!bmap)
9804 return NULL;
9806 if (!isl_basic_map_divs_known(bmap))
9807 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
9808 "some divs are unknown", return NULL);
9810 ls = isl_basic_map_get_local_space(bmap);
9811 div = isl_local_space_get_div(ls, pos);
9812 isl_local_space_free(ls);
9814 return div;
9817 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
9818 int pos)
9820 return isl_basic_map_get_div(bset, pos);
9823 /* Plug in "subs" for dimension "type", "pos" of "bset".
9825 * Let i be the dimension to replace and let "subs" be of the form
9827 * f/d
9829 * Any integer division with a non-zero coefficient for i,
9831 * floor((a i + g)/m)
9833 * is replaced by
9835 * floor((a f + d g)/(m d))
9837 * Constraints of the form
9839 * a i + g
9841 * are replaced by
9843 * a f + d g
9845 __isl_give isl_basic_set *isl_basic_set_substitute(
9846 __isl_take isl_basic_set *bset,
9847 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
9849 int i;
9850 isl_int v;
9851 isl_ctx *ctx;
9853 if (bset && isl_basic_set_plain_is_empty(bset))
9854 return bset;
9856 bset = isl_basic_set_cow(bset);
9857 if (!bset || !subs)
9858 goto error;
9860 ctx = isl_basic_set_get_ctx(bset);
9861 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
9862 isl_die(ctx, isl_error_invalid,
9863 "spaces don't match", goto error);
9864 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
9865 isl_die(ctx, isl_error_unsupported,
9866 "cannot handle divs yet", goto error);
9868 pos += isl_basic_set_offset(bset, type);
9870 isl_int_init(v);
9872 for (i = 0; i < bset->n_eq; ++i) {
9873 if (isl_int_is_zero(bset->eq[i][pos]))
9874 continue;
9875 isl_int_set(v, bset->eq[i][pos]);
9876 isl_int_set_si(bset->eq[i][pos], 0);
9877 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
9878 v, subs->v->el + 1, subs->v->size - 1);
9881 for (i = 0; i < bset->n_ineq; ++i) {
9882 if (isl_int_is_zero(bset->ineq[i][pos]))
9883 continue;
9884 isl_int_set(v, bset->ineq[i][pos]);
9885 isl_int_set_si(bset->ineq[i][pos], 0);
9886 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
9887 v, subs->v->el + 1, subs->v->size - 1);
9890 for (i = 0; i < bset->n_div; ++i) {
9891 if (isl_int_is_zero(bset->div[i][1 + pos]))
9892 continue;
9893 isl_int_set(v, bset->div[i][1 + pos]);
9894 isl_int_set_si(bset->div[i][1 + pos], 0);
9895 isl_seq_combine(bset->div[i] + 1,
9896 subs->v->el[0], bset->div[i] + 1,
9897 v, subs->v->el + 1, subs->v->size - 1);
9898 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
9901 isl_int_clear(v);
9903 bset = isl_basic_set_simplify(bset);
9904 return isl_basic_set_finalize(bset);
9905 error:
9906 isl_basic_set_free(bset);
9907 return NULL;
9910 /* Plug in "subs" for dimension "type", "pos" of "set".
9912 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
9913 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
9915 int i;
9917 if (set && isl_set_plain_is_empty(set))
9918 return set;
9920 set = isl_set_cow(set);
9921 if (!set || !subs)
9922 goto error;
9924 for (i = set->n - 1; i >= 0; --i) {
9925 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
9926 if (!set->p[i])
9927 goto error;
9928 if (isl_basic_set_plain_is_empty(set->p[i])) {
9929 isl_basic_set_free(set->p[i]);
9930 if (i != set->n - 1)
9931 set->p[i] = set->p[set->n - 1];
9932 set->n--;
9936 return set;
9937 error:
9938 isl_set_free(set);
9939 return NULL;