isl_pw_*_add_disjoint: try and extend one of the pws instead of copying pieces
[isl.git] / isl_map.c
blobbb242e71d10ee394ed14538486dab22821ce3577
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_map_has_dim_id(__isl_keep isl_map *map,
545 enum isl_dim_type type, unsigned pos)
547 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
550 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
551 enum isl_dim_type type, unsigned pos)
553 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
556 int isl_set_has_dim_id(__isl_keep isl_set *set,
557 enum isl_dim_type type, unsigned pos)
559 return isl_map_has_dim_id(set, type, pos);
562 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
563 enum isl_dim_type type, unsigned pos)
565 return isl_map_get_dim_id(set, type, pos);
568 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
569 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
571 map = isl_map_cow(map);
572 if (!map)
573 return isl_id_free(id);
575 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
577 return isl_map_reset_space(map, isl_space_copy(map->dim));
580 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
581 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
583 return isl_map_set_dim_id(set, type, pos, id);
586 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
587 __isl_keep isl_id *id)
589 if (!map)
590 return -1;
591 return isl_space_find_dim_by_id(map->dim, type, id);
594 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
595 __isl_keep isl_id *id)
597 return isl_map_find_dim_by_id(set, type, id);
600 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
602 if (!bmap)
603 return -1;
604 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
607 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
609 return isl_basic_map_is_rational(bset);
612 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
613 struct isl_basic_map *bmap, unsigned extra,
614 unsigned n_eq, unsigned n_ineq)
616 int i;
617 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
619 bmap->ctx = ctx;
620 isl_ctx_ref(ctx);
622 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
623 if (isl_blk_is_error(bmap->block))
624 goto error;
626 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
627 if (!bmap->ineq)
628 goto error;
630 if (extra == 0) {
631 bmap->block2 = isl_blk_empty();
632 bmap->div = NULL;
633 } else {
634 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
635 if (isl_blk_is_error(bmap->block2))
636 goto error;
638 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
639 if (!bmap->div)
640 goto error;
643 for (i = 0; i < n_ineq + n_eq; ++i)
644 bmap->ineq[i] = bmap->block.data + i * row_size;
646 for (i = 0; i < extra; ++i)
647 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
649 bmap->ref = 1;
650 bmap->flags = 0;
651 bmap->c_size = n_eq + n_ineq;
652 bmap->eq = bmap->ineq + n_ineq;
653 bmap->extra = extra;
654 bmap->n_eq = 0;
655 bmap->n_ineq = 0;
656 bmap->n_div = 0;
657 bmap->sample = NULL;
659 return bmap;
660 error:
661 isl_basic_map_free(bmap);
662 return NULL;
665 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
666 unsigned nparam, unsigned dim, unsigned extra,
667 unsigned n_eq, unsigned n_ineq)
669 struct isl_basic_map *bmap;
670 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
671 return (struct isl_basic_set *)bmap;
674 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
675 unsigned extra, unsigned n_eq, unsigned n_ineq)
677 struct isl_basic_map *bmap;
678 if (!dim)
679 return NULL;
680 isl_assert(dim->ctx, dim->n_in == 0, goto error);
681 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
682 return (struct isl_basic_set *)bmap;
683 error:
684 isl_space_free(dim);
685 return NULL;
688 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
689 unsigned extra, unsigned n_eq, unsigned n_ineq)
691 struct isl_basic_map *bmap;
693 if (!dim)
694 return NULL;
695 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
696 if (!bmap)
697 goto error;
698 bmap->dim = dim;
700 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
701 error:
702 isl_space_free(dim);
703 return NULL;
706 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
707 unsigned nparam, unsigned in, unsigned out, unsigned extra,
708 unsigned n_eq, unsigned n_ineq)
710 struct isl_basic_map *bmap;
711 isl_space *dim;
713 dim = isl_space_alloc(ctx, nparam, in, out);
714 if (!dim)
715 return NULL;
717 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
718 return bmap;
721 static void dup_constraints(
722 struct isl_basic_map *dst, struct isl_basic_map *src)
724 int i;
725 unsigned total = isl_basic_map_total_dim(src);
727 for (i = 0; i < src->n_eq; ++i) {
728 int j = isl_basic_map_alloc_equality(dst);
729 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
732 for (i = 0; i < src->n_ineq; ++i) {
733 int j = isl_basic_map_alloc_inequality(dst);
734 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
737 for (i = 0; i < src->n_div; ++i) {
738 int j = isl_basic_map_alloc_div(dst);
739 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
741 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
744 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
746 struct isl_basic_map *dup;
748 if (!bmap)
749 return NULL;
750 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
751 bmap->n_div, bmap->n_eq, bmap->n_ineq);
752 if (!dup)
753 return NULL;
754 dup_constraints(dup, bmap);
755 dup->flags = bmap->flags;
756 dup->sample = isl_vec_copy(bmap->sample);
757 return dup;
760 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
762 struct isl_basic_map *dup;
764 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
765 return (struct isl_basic_set *)dup;
768 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
770 if (!bset)
771 return NULL;
773 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
774 bset->ref++;
775 return bset;
777 return isl_basic_set_dup(bset);
780 struct isl_set *isl_set_copy(struct isl_set *set)
782 if (!set)
783 return NULL;
785 set->ref++;
786 return set;
789 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
791 if (!bmap)
792 return NULL;
794 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
795 bmap->ref++;
796 return bmap;
798 bmap = isl_basic_map_dup(bmap);
799 if (bmap)
800 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
801 return bmap;
804 struct isl_map *isl_map_copy(struct isl_map *map)
806 if (!map)
807 return NULL;
809 map->ref++;
810 return map;
813 void isl_basic_map_free(struct isl_basic_map *bmap)
815 if (!bmap)
816 return;
818 if (--bmap->ref > 0)
819 return;
821 isl_ctx_deref(bmap->ctx);
822 free(bmap->div);
823 isl_blk_free(bmap->ctx, bmap->block2);
824 free(bmap->ineq);
825 isl_blk_free(bmap->ctx, bmap->block);
826 isl_vec_free(bmap->sample);
827 isl_space_free(bmap->dim);
828 free(bmap);
831 void isl_basic_set_free(struct isl_basic_set *bset)
833 isl_basic_map_free((struct isl_basic_map *)bset);
836 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
838 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
841 __isl_give isl_map *isl_map_align_params_map_map_and(
842 __isl_take isl_map *map1, __isl_take isl_map *map2,
843 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
844 __isl_take isl_map *map2))
846 if (!map1 || !map2)
847 goto error;
848 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
849 return fn(map1, map2);
850 if (!isl_space_has_named_params(map1->dim) ||
851 !isl_space_has_named_params(map2->dim))
852 isl_die(map1->ctx, isl_error_invalid,
853 "unaligned unnamed parameters", goto error);
854 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
855 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
856 return fn(map1, map2);
857 error:
858 isl_map_free(map1);
859 isl_map_free(map2);
860 return NULL;
863 static int align_params_map_map_and_test(__isl_keep isl_map *map1,
864 __isl_keep isl_map *map2,
865 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
867 int r;
869 if (!map1 || !map2)
870 return -1;
871 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
872 return fn(map1, map2);
873 if (!isl_space_has_named_params(map1->dim) ||
874 !isl_space_has_named_params(map2->dim))
875 isl_die(map1->ctx, isl_error_invalid,
876 "unaligned unnamed parameters", return -1);
877 map1 = isl_map_copy(map1);
878 map2 = isl_map_copy(map2);
879 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
880 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
881 r = fn(map1, map2);
882 isl_map_free(map1);
883 isl_map_free(map2);
884 return r;
887 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
889 struct isl_ctx *ctx;
890 if (!bmap)
891 return -1;
892 ctx = bmap->ctx;
893 isl_assert(ctx, room_for_con(bmap, 1), return -1);
894 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
895 return -1);
896 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
897 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
898 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
899 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
900 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
901 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
902 isl_int *t;
903 int j = isl_basic_map_alloc_inequality(bmap);
904 if (j < 0)
905 return -1;
906 t = bmap->ineq[j];
907 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
908 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
909 bmap->eq[-1] = t;
910 bmap->n_eq++;
911 bmap->n_ineq--;
912 bmap->eq--;
913 return 0;
915 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
916 bmap->extra - bmap->n_div);
917 return bmap->n_eq++;
920 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
922 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
925 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
927 if (!bmap)
928 return -1;
929 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
930 bmap->n_eq -= n;
931 return 0;
934 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
936 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
939 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
941 isl_int *t;
942 if (!bmap)
943 return -1;
944 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
946 if (pos != bmap->n_eq - 1) {
947 t = bmap->eq[pos];
948 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
949 bmap->eq[bmap->n_eq - 1] = t;
951 bmap->n_eq--;
952 return 0;
955 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
957 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
960 void isl_basic_map_inequality_to_equality(
961 struct isl_basic_map *bmap, unsigned pos)
963 isl_int *t;
965 t = bmap->ineq[pos];
966 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
967 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
968 bmap->eq[-1] = t;
969 bmap->n_eq++;
970 bmap->n_ineq--;
971 bmap->eq--;
972 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
973 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
974 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
975 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
978 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
980 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
983 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
985 struct isl_ctx *ctx;
986 if (!bmap)
987 return -1;
988 ctx = bmap->ctx;
989 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
990 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
991 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
992 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
993 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
994 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
995 1 + isl_basic_map_total_dim(bmap),
996 bmap->extra - bmap->n_div);
997 return bmap->n_ineq++;
1000 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1002 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1005 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1007 if (!bmap)
1008 return -1;
1009 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1010 bmap->n_ineq -= n;
1011 return 0;
1014 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1016 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1019 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1021 isl_int *t;
1022 if (!bmap)
1023 return -1;
1024 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1026 if (pos != bmap->n_ineq - 1) {
1027 t = bmap->ineq[pos];
1028 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1029 bmap->ineq[bmap->n_ineq - 1] = t;
1030 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1032 bmap->n_ineq--;
1033 return 0;
1036 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1038 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1041 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1042 isl_int *eq)
1044 int k;
1046 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1047 if (!bmap)
1048 return NULL;
1049 k = isl_basic_map_alloc_equality(bmap);
1050 if (k < 0)
1051 goto error;
1052 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1053 return bmap;
1054 error:
1055 isl_basic_map_free(bmap);
1056 return NULL;
1059 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1060 isl_int *eq)
1062 return (isl_basic_set *)
1063 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1066 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1067 isl_int *ineq)
1069 int k;
1071 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1072 if (!bmap)
1073 return NULL;
1074 k = isl_basic_map_alloc_inequality(bmap);
1075 if (k < 0)
1076 goto error;
1077 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1078 return bmap;
1079 error:
1080 isl_basic_map_free(bmap);
1081 return NULL;
1084 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1085 isl_int *ineq)
1087 return (isl_basic_set *)
1088 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1091 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1093 if (!bmap)
1094 return -1;
1095 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1096 isl_seq_clr(bmap->div[bmap->n_div] +
1097 1 + 1 + isl_basic_map_total_dim(bmap),
1098 bmap->extra - bmap->n_div);
1099 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1100 return bmap->n_div++;
1103 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1105 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1108 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1110 if (!bmap)
1111 return -1;
1112 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1113 bmap->n_div -= n;
1114 return 0;
1117 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1119 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1122 /* Copy constraint from src to dst, putting the vars of src at offset
1123 * dim_off in dst and the divs of src at offset div_off in dst.
1124 * If both sets are actually map, then dim_off applies to the input
1125 * variables.
1127 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1128 struct isl_basic_map *src_map, isl_int *src,
1129 unsigned in_off, unsigned out_off, unsigned div_off)
1131 unsigned src_nparam = isl_basic_map_n_param(src_map);
1132 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1133 unsigned src_in = isl_basic_map_n_in(src_map);
1134 unsigned dst_in = isl_basic_map_n_in(dst_map);
1135 unsigned src_out = isl_basic_map_n_out(src_map);
1136 unsigned dst_out = isl_basic_map_n_out(dst_map);
1137 isl_int_set(dst[0], src[0]);
1138 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1139 if (dst_nparam > src_nparam)
1140 isl_seq_clr(dst+1+src_nparam,
1141 dst_nparam - src_nparam);
1142 isl_seq_clr(dst+1+dst_nparam, in_off);
1143 isl_seq_cpy(dst+1+dst_nparam+in_off,
1144 src+1+src_nparam,
1145 isl_min(dst_in-in_off, src_in));
1146 if (dst_in-in_off > src_in)
1147 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1148 dst_in - in_off - src_in);
1149 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1150 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1151 src+1+src_nparam+src_in,
1152 isl_min(dst_out-out_off, src_out));
1153 if (dst_out-out_off > src_out)
1154 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1155 dst_out - out_off - src_out);
1156 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1157 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1158 src+1+src_nparam+src_in+src_out,
1159 isl_min(dst_map->extra-div_off, src_map->n_div));
1160 if (dst_map->n_div-div_off > src_map->n_div)
1161 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1162 div_off+src_map->n_div,
1163 dst_map->n_div - div_off - src_map->n_div);
1166 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1167 struct isl_basic_map *src_map, isl_int *src,
1168 unsigned in_off, unsigned out_off, unsigned div_off)
1170 isl_int_set(dst[0], src[0]);
1171 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1174 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1175 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1177 int i;
1178 unsigned div_off;
1180 if (!bmap1 || !bmap2)
1181 goto error;
1183 div_off = bmap1->n_div;
1185 for (i = 0; i < bmap2->n_eq; ++i) {
1186 int i1 = isl_basic_map_alloc_equality(bmap1);
1187 if (i1 < 0)
1188 goto error;
1189 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1190 i_pos, o_pos, div_off);
1193 for (i = 0; i < bmap2->n_ineq; ++i) {
1194 int i1 = isl_basic_map_alloc_inequality(bmap1);
1195 if (i1 < 0)
1196 goto error;
1197 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1198 i_pos, o_pos, div_off);
1201 for (i = 0; i < bmap2->n_div; ++i) {
1202 int i1 = isl_basic_map_alloc_div(bmap1);
1203 if (i1 < 0)
1204 goto error;
1205 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1206 i_pos, o_pos, div_off);
1209 isl_basic_map_free(bmap2);
1211 return bmap1;
1213 error:
1214 isl_basic_map_free(bmap1);
1215 isl_basic_map_free(bmap2);
1216 return NULL;
1219 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1220 struct isl_basic_set *bset2, unsigned pos)
1222 return (struct isl_basic_set *)
1223 add_constraints((struct isl_basic_map *)bset1,
1224 (struct isl_basic_map *)bset2, 0, pos);
1227 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1228 __isl_take isl_space *dim, unsigned extra,
1229 unsigned n_eq, unsigned n_ineq)
1231 struct isl_basic_map *ext;
1232 unsigned flags;
1233 int dims_ok;
1235 if (!dim)
1236 goto error;
1238 if (!base)
1239 goto error;
1241 dims_ok = isl_space_is_equal(base->dim, dim) &&
1242 base->extra >= base->n_div + extra;
1244 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1245 room_for_ineq(base, n_ineq)) {
1246 isl_space_free(dim);
1247 return base;
1250 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1251 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1252 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1253 extra += base->extra;
1254 n_eq += base->n_eq;
1255 n_ineq += base->n_ineq;
1257 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1258 dim = NULL;
1259 if (!ext)
1260 goto error;
1262 if (dims_ok)
1263 ext->sample = isl_vec_copy(base->sample);
1264 flags = base->flags;
1265 ext = add_constraints(ext, base, 0, 0);
1266 if (ext) {
1267 ext->flags = flags;
1268 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1271 return ext;
1273 error:
1274 isl_space_free(dim);
1275 isl_basic_map_free(base);
1276 return NULL;
1279 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1280 __isl_take isl_space *dim, unsigned extra,
1281 unsigned n_eq, unsigned n_ineq)
1283 return (struct isl_basic_set *)
1284 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1285 extra, n_eq, n_ineq);
1288 struct isl_basic_map *isl_basic_map_extend_constraints(
1289 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1291 if (!base)
1292 return NULL;
1293 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1294 0, n_eq, n_ineq);
1297 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1298 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1299 unsigned n_eq, unsigned n_ineq)
1301 struct isl_basic_map *bmap;
1302 isl_space *dim;
1304 if (!base)
1305 return NULL;
1306 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1307 if (!dim)
1308 goto error;
1310 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1311 return bmap;
1312 error:
1313 isl_basic_map_free(base);
1314 return NULL;
1317 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1318 unsigned nparam, unsigned dim, unsigned extra,
1319 unsigned n_eq, unsigned n_ineq)
1321 return (struct isl_basic_set *)
1322 isl_basic_map_extend((struct isl_basic_map *)base,
1323 nparam, 0, dim, extra, n_eq, n_ineq);
1326 struct isl_basic_set *isl_basic_set_extend_constraints(
1327 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1329 return (struct isl_basic_set *)
1330 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1331 n_eq, n_ineq);
1334 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1336 return (struct isl_basic_set *)
1337 isl_basic_map_cow((struct isl_basic_map *)bset);
1340 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1342 if (!bmap)
1343 return NULL;
1345 if (bmap->ref > 1) {
1346 bmap->ref--;
1347 bmap = isl_basic_map_dup(bmap);
1349 if (bmap)
1350 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1351 return bmap;
1354 struct isl_set *isl_set_cow(struct isl_set *set)
1356 if (!set)
1357 return NULL;
1359 if (set->ref == 1)
1360 return set;
1361 set->ref--;
1362 return isl_set_dup(set);
1365 struct isl_map *isl_map_cow(struct isl_map *map)
1367 if (!map)
1368 return NULL;
1370 if (map->ref == 1)
1371 return map;
1372 map->ref--;
1373 return isl_map_dup(map);
1376 static void swap_vars(struct isl_blk blk, isl_int *a,
1377 unsigned a_len, unsigned b_len)
1379 isl_seq_cpy(blk.data, a+a_len, b_len);
1380 isl_seq_cpy(blk.data+b_len, a, a_len);
1381 isl_seq_cpy(a, blk.data, b_len+a_len);
1384 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1385 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1387 int i;
1388 struct isl_blk blk;
1390 if (!bmap)
1391 goto error;
1393 isl_assert(bmap->ctx,
1394 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1396 if (n1 == 0 || n2 == 0)
1397 return bmap;
1399 bmap = isl_basic_map_cow(bmap);
1400 if (!bmap)
1401 return NULL;
1403 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1404 if (isl_blk_is_error(blk))
1405 goto error;
1407 for (i = 0; i < bmap->n_eq; ++i)
1408 swap_vars(blk,
1409 bmap->eq[i] + pos, n1, n2);
1411 for (i = 0; i < bmap->n_ineq; ++i)
1412 swap_vars(blk,
1413 bmap->ineq[i] + pos, n1, n2);
1415 for (i = 0; i < bmap->n_div; ++i)
1416 swap_vars(blk,
1417 bmap->div[i]+1 + pos, n1, n2);
1419 isl_blk_free(bmap->ctx, blk);
1421 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1422 bmap = isl_basic_map_gauss(bmap, NULL);
1423 return isl_basic_map_finalize(bmap);
1424 error:
1425 isl_basic_map_free(bmap);
1426 return NULL;
1429 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1430 __isl_take isl_basic_set *bset, unsigned n)
1432 unsigned dim;
1433 unsigned nparam;
1435 nparam = isl_basic_set_n_param(bset);
1436 dim = isl_basic_set_n_dim(bset);
1437 isl_assert(bset->ctx, n <= dim, goto error);
1439 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1440 error:
1441 isl_basic_set_free(bset);
1442 return NULL;
1445 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1447 int i = 0;
1448 unsigned total;
1449 if (!bmap)
1450 goto error;
1451 total = isl_basic_map_total_dim(bmap);
1452 isl_basic_map_free_div(bmap, bmap->n_div);
1453 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1454 if (bmap->n_eq > 0)
1455 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1456 else {
1457 i = isl_basic_map_alloc_equality(bmap);
1458 if (i < 0)
1459 goto error;
1461 isl_int_set_si(bmap->eq[i][0], 1);
1462 isl_seq_clr(bmap->eq[i]+1, total);
1463 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1464 isl_vec_free(bmap->sample);
1465 bmap->sample = NULL;
1466 return isl_basic_map_finalize(bmap);
1467 error:
1468 isl_basic_map_free(bmap);
1469 return NULL;
1472 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1474 return (struct isl_basic_set *)
1475 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1478 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1480 int i;
1481 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1482 isl_int *t = bmap->div[a];
1483 bmap->div[a] = bmap->div[b];
1484 bmap->div[b] = t;
1486 for (i = 0; i < bmap->n_eq; ++i)
1487 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1489 for (i = 0; i < bmap->n_ineq; ++i)
1490 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1492 for (i = 0; i < bmap->n_div; ++i)
1493 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1494 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1497 /* Eliminate the specified n dimensions starting at first from the
1498 * constraints using Fourier-Motzkin. The dimensions themselves
1499 * are not removed.
1501 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1502 enum isl_dim_type type, unsigned first, unsigned n)
1504 int i;
1506 if (!map)
1507 return NULL;
1508 if (n == 0)
1509 return map;
1511 map = isl_map_cow(map);
1512 if (!map)
1513 return NULL;
1514 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1515 first += pos(map->dim, type) - 1;
1517 for (i = 0; i < map->n; ++i) {
1518 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1519 if (!map->p[i])
1520 goto error;
1522 return map;
1523 error:
1524 isl_map_free(map);
1525 return NULL;
1528 /* Eliminate the specified n dimensions starting at first from the
1529 * constraints using Fourier-Motzkin. The dimensions themselves
1530 * are not removed.
1532 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1533 enum isl_dim_type type, unsigned first, unsigned n)
1535 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1538 /* Eliminate the specified n dimensions starting at first from the
1539 * constraints using Fourier-Motzkin. The dimensions themselves
1540 * are not removed.
1542 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1543 unsigned first, unsigned n)
1545 return isl_set_eliminate(set, isl_dim_set, first, n);
1548 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1549 __isl_take isl_basic_map *bmap)
1551 if (!bmap)
1552 return NULL;
1553 bmap = isl_basic_map_eliminate_vars(bmap,
1554 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1555 if (!bmap)
1556 return NULL;
1557 bmap->n_div = 0;
1558 return isl_basic_map_finalize(bmap);
1561 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1562 __isl_take isl_basic_set *bset)
1564 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1565 (struct isl_basic_map *)bset);
1568 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1570 int i;
1572 if (!map)
1573 return NULL;
1574 if (map->n == 0)
1575 return map;
1577 map = isl_map_cow(map);
1578 if (!map)
1579 return NULL;
1581 for (i = 0; i < map->n; ++i) {
1582 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1583 if (!map->p[i])
1584 goto error;
1586 return map;
1587 error:
1588 isl_map_free(map);
1589 return NULL;
1592 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1594 return isl_map_remove_divs(set);
1597 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1598 enum isl_dim_type type, unsigned first, unsigned n)
1600 if (!bmap)
1601 return NULL;
1602 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1603 goto error);
1604 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1605 return bmap;
1606 bmap = isl_basic_map_eliminate_vars(bmap,
1607 isl_basic_map_offset(bmap, type) - 1 + first, n);
1608 if (!bmap)
1609 return bmap;
1610 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1611 return bmap;
1612 bmap = isl_basic_map_drop(bmap, type, first, n);
1613 return bmap;
1614 error:
1615 isl_basic_map_free(bmap);
1616 return NULL;
1619 /* Return true if the definition of the given div (recursively) involves
1620 * any of the given variables.
1622 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1623 unsigned first, unsigned n)
1625 int i;
1626 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1628 if (isl_int_is_zero(bmap->div[div][0]))
1629 return 0;
1630 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1631 return 1;
1633 for (i = bmap->n_div - 1; i >= 0; --i) {
1634 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1635 continue;
1636 if (div_involves_vars(bmap, i, first, n))
1637 return 1;
1640 return 0;
1643 /* Remove all divs (recursively) involving any of the given dimensions
1644 * in their definitions.
1646 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1647 __isl_take isl_basic_map *bmap,
1648 enum isl_dim_type type, unsigned first, unsigned n)
1650 int i;
1652 if (!bmap)
1653 return NULL;
1654 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1655 goto error);
1656 first += isl_basic_map_offset(bmap, type);
1658 for (i = bmap->n_div - 1; i >= 0; --i) {
1659 if (!div_involves_vars(bmap, i, first, n))
1660 continue;
1661 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1662 if (!bmap)
1663 return NULL;
1664 i = bmap->n_div;
1667 return bmap;
1668 error:
1669 isl_basic_map_free(bmap);
1670 return NULL;
1673 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1674 enum isl_dim_type type, unsigned first, unsigned n)
1676 int i;
1678 if (!map)
1679 return NULL;
1680 if (map->n == 0)
1681 return map;
1683 map = isl_map_cow(map);
1684 if (!map)
1685 return NULL;
1687 for (i = 0; i < map->n; ++i) {
1688 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1689 type, first, n);
1690 if (!map->p[i])
1691 goto error;
1693 return map;
1694 error:
1695 isl_map_free(map);
1696 return NULL;
1699 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1700 enum isl_dim_type type, unsigned first, unsigned n)
1702 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1703 type, first, n);
1706 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1707 enum isl_dim_type type, unsigned first, unsigned n)
1709 int i;
1711 if (!bmap)
1712 return -1;
1714 if (first + n > isl_basic_map_dim(bmap, type))
1715 isl_die(bmap->ctx, isl_error_invalid,
1716 "index out of bounds", return -1);
1718 first += isl_basic_map_offset(bmap, type);
1719 for (i = 0; i < bmap->n_eq; ++i)
1720 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1721 return 1;
1722 for (i = 0; i < bmap->n_ineq; ++i)
1723 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1724 return 1;
1726 return 0;
1729 int isl_map_involves_dims(__isl_keep isl_map *map,
1730 enum isl_dim_type type, unsigned first, unsigned n)
1732 int i;
1734 if (!map)
1735 return -1;
1737 if (first + n > isl_map_dim(map, type))
1738 isl_die(map->ctx, isl_error_invalid,
1739 "index out of bounds", return -1);
1741 for (i = 0; i < map->n; ++i) {
1742 int involves = isl_basic_map_involves_dims(map->p[i],
1743 type, first, n);
1744 if (involves < 0 || !involves)
1745 return involves;
1748 return 1;
1751 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1752 enum isl_dim_type type, unsigned first, unsigned n)
1754 return isl_basic_map_involves_dims(bset, type, first, n);
1757 int isl_set_involves_dims(__isl_keep isl_set *set,
1758 enum isl_dim_type type, unsigned first, unsigned n)
1760 return isl_map_involves_dims(set, type, first, n);
1763 /* Return true if the definition of the given div is unknown or depends
1764 * on unknown divs.
1766 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1768 int i;
1769 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1771 if (isl_int_is_zero(bmap->div[div][0]))
1772 return 1;
1774 for (i = bmap->n_div - 1; i >= 0; --i) {
1775 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1776 continue;
1777 if (div_is_unknown(bmap, i))
1778 return 1;
1781 return 0;
1784 /* Remove all divs that are unknown or defined in terms of unknown divs.
1786 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1787 __isl_take isl_basic_map *bmap)
1789 int i;
1791 if (!bmap)
1792 return NULL;
1794 for (i = bmap->n_div - 1; i >= 0; --i) {
1795 if (!div_is_unknown(bmap, i))
1796 continue;
1797 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1800 return bmap;
1803 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1805 int i;
1807 if (!map)
1808 return NULL;
1809 if (map->n == 0)
1810 return map;
1812 map = isl_map_cow(map);
1813 if (!map)
1814 return NULL;
1816 for (i = 0; i < map->n; ++i) {
1817 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1818 if (!map->p[i])
1819 goto error;
1821 return map;
1822 error:
1823 isl_map_free(map);
1824 return NULL;
1827 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1829 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1832 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1833 __isl_take isl_basic_set *bset,
1834 enum isl_dim_type type, unsigned first, unsigned n)
1836 return (isl_basic_set *)
1837 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1840 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1841 enum isl_dim_type type, unsigned first, unsigned n)
1843 int i;
1845 if (n == 0)
1846 return map;
1848 map = isl_map_cow(map);
1849 if (!map)
1850 return NULL;
1851 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1853 for (i = 0; i < map->n; ++i) {
1854 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1855 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1856 if (!map->p[i])
1857 goto error;
1859 map = isl_map_drop(map, type, first, n);
1860 return map;
1861 error:
1862 isl_map_free(map);
1863 return NULL;
1866 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1867 enum isl_dim_type type, unsigned first, unsigned n)
1869 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1872 /* Project out n inputs starting at first using Fourier-Motzkin */
1873 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1874 unsigned first, unsigned n)
1876 return isl_map_remove_dims(map, isl_dim_in, first, n);
1879 static void dump_term(struct isl_basic_map *bmap,
1880 isl_int c, int pos, FILE *out)
1882 const char *name;
1883 unsigned in = isl_basic_map_n_in(bmap);
1884 unsigned dim = in + isl_basic_map_n_out(bmap);
1885 unsigned nparam = isl_basic_map_n_param(bmap);
1886 if (!pos)
1887 isl_int_print(out, c, 0);
1888 else {
1889 if (!isl_int_is_one(c))
1890 isl_int_print(out, c, 0);
1891 if (pos < 1 + nparam) {
1892 name = isl_space_get_dim_name(bmap->dim,
1893 isl_dim_param, pos - 1);
1894 if (name)
1895 fprintf(out, "%s", name);
1896 else
1897 fprintf(out, "p%d", pos - 1);
1898 } else if (pos < 1 + nparam + in)
1899 fprintf(out, "i%d", pos - 1 - nparam);
1900 else if (pos < 1 + nparam + dim)
1901 fprintf(out, "o%d", pos - 1 - nparam - in);
1902 else
1903 fprintf(out, "e%d", pos - 1 - nparam - dim);
1907 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1908 int sign, FILE *out)
1910 int i;
1911 int first;
1912 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1913 isl_int v;
1915 isl_int_init(v);
1916 for (i = 0, first = 1; i < len; ++i) {
1917 if (isl_int_sgn(c[i]) * sign <= 0)
1918 continue;
1919 if (!first)
1920 fprintf(out, " + ");
1921 first = 0;
1922 isl_int_abs(v, c[i]);
1923 dump_term(bmap, v, i, out);
1925 isl_int_clear(v);
1926 if (first)
1927 fprintf(out, "0");
1930 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1931 const char *op, FILE *out, int indent)
1933 int i;
1935 fprintf(out, "%*s", indent, "");
1937 dump_constraint_sign(bmap, c, 1, out);
1938 fprintf(out, " %s ", op);
1939 dump_constraint_sign(bmap, c, -1, out);
1941 fprintf(out, "\n");
1943 for (i = bmap->n_div; i < bmap->extra; ++i) {
1944 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
1945 continue;
1946 fprintf(out, "%*s", indent, "");
1947 fprintf(out, "ERROR: unused div coefficient not zero\n");
1948 abort();
1952 static void dump_constraints(struct isl_basic_map *bmap,
1953 isl_int **c, unsigned n,
1954 const char *op, FILE *out, int indent)
1956 int i;
1958 for (i = 0; i < n; ++i)
1959 dump_constraint(bmap, c[i], op, out, indent);
1962 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1964 int j;
1965 int first = 1;
1966 unsigned total = isl_basic_map_total_dim(bmap);
1968 for (j = 0; j < 1 + total; ++j) {
1969 if (isl_int_is_zero(exp[j]))
1970 continue;
1971 if (!first && isl_int_is_pos(exp[j]))
1972 fprintf(out, "+");
1973 dump_term(bmap, exp[j], j, out);
1974 first = 0;
1978 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1980 int i;
1982 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1983 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1985 for (i = 0; i < bmap->n_div; ++i) {
1986 fprintf(out, "%*s", indent, "");
1987 fprintf(out, "e%d = [(", i);
1988 dump_affine(bmap, bmap->div[i]+1, out);
1989 fprintf(out, ")/");
1990 isl_int_print(out, bmap->div[i][0], 0);
1991 fprintf(out, "]\n");
1995 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1996 FILE *out, int indent)
1998 if (!bset) {
1999 fprintf(out, "null basic set\n");
2000 return;
2003 fprintf(out, "%*s", indent, "");
2004 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2005 bset->ref, bset->dim->nparam, bset->dim->n_out,
2006 bset->extra, bset->flags);
2007 dump((struct isl_basic_map *)bset, out, indent);
2010 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2011 FILE *out, int indent)
2013 if (!bmap) {
2014 fprintf(out, "null basic map\n");
2015 return;
2018 fprintf(out, "%*s", indent, "");
2019 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2020 "flags: %x, n_name: %d\n",
2021 bmap->ref,
2022 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2023 bmap->extra, bmap->flags, bmap->dim->n_id);
2024 dump(bmap, out, indent);
2027 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2029 unsigned total;
2030 if (!bmap)
2031 return -1;
2032 total = isl_basic_map_total_dim(bmap);
2033 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2034 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2035 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2036 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2037 return 0;
2040 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2041 unsigned flags)
2043 struct isl_set *set;
2045 if (!dim)
2046 return NULL;
2047 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2048 isl_assert(dim->ctx, n >= 0, goto error);
2049 set = isl_alloc(dim->ctx, struct isl_set,
2050 sizeof(struct isl_set) +
2051 (n - 1) * sizeof(struct isl_basic_set *));
2052 if (!set)
2053 goto error;
2055 set->ctx = dim->ctx;
2056 isl_ctx_ref(set->ctx);
2057 set->ref = 1;
2058 set->size = n;
2059 set->n = 0;
2060 set->dim = dim;
2061 set->flags = flags;
2062 return set;
2063 error:
2064 isl_space_free(dim);
2065 return NULL;
2068 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2069 unsigned nparam, unsigned dim, int n, unsigned flags)
2071 struct isl_set *set;
2072 isl_space *dims;
2074 dims = isl_space_alloc(ctx, nparam, 0, dim);
2075 if (!dims)
2076 return NULL;
2078 set = isl_set_alloc_space(dims, n, flags);
2079 return set;
2082 /* Make sure "map" has room for at least "n" more basic maps.
2084 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2086 int i;
2087 struct isl_map *grown = NULL;
2089 if (!map)
2090 return NULL;
2091 isl_assert(map->ctx, n >= 0, goto error);
2092 if (map->n + n <= map->size)
2093 return map;
2094 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2095 if (!grown)
2096 goto error;
2097 for (i = 0; i < map->n; ++i) {
2098 grown->p[i] = isl_basic_map_copy(map->p[i]);
2099 if (!grown->p[i])
2100 goto error;
2101 grown->n++;
2103 isl_map_free(map);
2104 return grown;
2105 error:
2106 isl_map_free(grown);
2107 isl_map_free(map);
2108 return NULL;
2111 /* Make sure "set" has room for at least "n" more basic sets.
2113 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2115 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2118 struct isl_set *isl_set_dup(struct isl_set *set)
2120 int i;
2121 struct isl_set *dup;
2123 if (!set)
2124 return NULL;
2126 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2127 if (!dup)
2128 return NULL;
2129 for (i = 0; i < set->n; ++i)
2130 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2131 return dup;
2134 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2136 return isl_map_from_basic_map(bset);
2139 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2141 struct isl_map *map;
2143 if (!bmap)
2144 return NULL;
2146 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2147 return isl_map_add_basic_map(map, bmap);
2150 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2151 __isl_take isl_basic_set *bset)
2153 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2154 (struct isl_basic_map *)bset);
2157 void isl_set_free(struct isl_set *set)
2159 int i;
2161 if (!set)
2162 return;
2164 if (--set->ref > 0)
2165 return;
2167 isl_ctx_deref(set->ctx);
2168 for (i = 0; i < set->n; ++i)
2169 isl_basic_set_free(set->p[i]);
2170 isl_space_free(set->dim);
2171 free(set);
2174 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2176 int i;
2178 if (!set) {
2179 fprintf(out, "null set\n");
2180 return;
2183 fprintf(out, "%*s", indent, "");
2184 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2185 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2186 set->flags);
2187 for (i = 0; i < set->n; ++i) {
2188 fprintf(out, "%*s", indent, "");
2189 fprintf(out, "basic set %d:\n", i);
2190 isl_basic_set_print_internal(set->p[i], out, indent+4);
2194 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2196 int i;
2198 if (!map) {
2199 fprintf(out, "null map\n");
2200 return;
2203 fprintf(out, "%*s", indent, "");
2204 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2205 "flags: %x, n_name: %d\n",
2206 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2207 map->dim->n_out, map->flags, map->dim->n_id);
2208 for (i = 0; i < map->n; ++i) {
2209 fprintf(out, "%*s", indent, "");
2210 fprintf(out, "basic map %d:\n", i);
2211 isl_basic_map_print_internal(map->p[i], out, indent+4);
2215 struct isl_basic_map *isl_basic_map_intersect_domain(
2216 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2218 struct isl_basic_map *bmap_domain;
2220 if (!bmap || !bset)
2221 goto error;
2223 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2224 bset->dim, isl_dim_param), goto error);
2226 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2227 isl_assert(bset->ctx,
2228 isl_basic_map_compatible_domain(bmap, bset), goto error);
2230 bmap = isl_basic_map_cow(bmap);
2231 if (!bmap)
2232 goto error;
2233 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2234 bset->n_div, bset->n_eq, bset->n_ineq);
2235 bmap_domain = isl_basic_map_from_domain(bset);
2236 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2238 bmap = isl_basic_map_simplify(bmap);
2239 return isl_basic_map_finalize(bmap);
2240 error:
2241 isl_basic_map_free(bmap);
2242 isl_basic_set_free(bset);
2243 return NULL;
2246 struct isl_basic_map *isl_basic_map_intersect_range(
2247 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2249 struct isl_basic_map *bmap_range;
2251 if (!bmap || !bset)
2252 goto error;
2254 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2255 bset->dim, isl_dim_param), goto error);
2257 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2258 isl_assert(bset->ctx,
2259 isl_basic_map_compatible_range(bmap, bset), goto error);
2261 if (isl_basic_set_is_universe(bset)) {
2262 isl_basic_set_free(bset);
2263 return bmap;
2266 bmap = isl_basic_map_cow(bmap);
2267 if (!bmap)
2268 goto error;
2269 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2270 bset->n_div, bset->n_eq, bset->n_ineq);
2271 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2272 bmap = add_constraints(bmap, bmap_range, 0, 0);
2274 bmap = isl_basic_map_simplify(bmap);
2275 return isl_basic_map_finalize(bmap);
2276 error:
2277 isl_basic_map_free(bmap);
2278 isl_basic_set_free(bset);
2279 return NULL;
2282 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2284 int i;
2285 unsigned total;
2286 isl_int s;
2288 total = 1 + isl_basic_map_total_dim(bmap);
2289 if (total != vec->size)
2290 return -1;
2292 isl_int_init(s);
2294 for (i = 0; i < bmap->n_eq; ++i) {
2295 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2296 if (!isl_int_is_zero(s)) {
2297 isl_int_clear(s);
2298 return 0;
2302 for (i = 0; i < bmap->n_ineq; ++i) {
2303 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2304 if (isl_int_is_neg(s)) {
2305 isl_int_clear(s);
2306 return 0;
2310 isl_int_clear(s);
2312 return 1;
2315 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2317 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2320 struct isl_basic_map *isl_basic_map_intersect(
2321 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2323 struct isl_vec *sample = NULL;
2325 if (!bmap1 || !bmap2)
2326 goto error;
2328 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2329 bmap2->dim, isl_dim_param), goto error);
2330 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2331 isl_space_dim(bmap1->dim, isl_dim_param) &&
2332 isl_space_dim(bmap2->dim, isl_dim_all) !=
2333 isl_space_dim(bmap2->dim, isl_dim_param))
2334 return isl_basic_map_intersect(bmap2, bmap1);
2336 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2337 isl_space_dim(bmap2->dim, isl_dim_param))
2338 isl_assert(bmap1->ctx,
2339 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2341 if (bmap1->sample &&
2342 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2343 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2344 sample = isl_vec_copy(bmap1->sample);
2345 else if (bmap2->sample &&
2346 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2347 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2348 sample = isl_vec_copy(bmap2->sample);
2350 bmap1 = isl_basic_map_cow(bmap1);
2351 if (!bmap1)
2352 goto error;
2353 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2354 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2355 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2357 if (!bmap1)
2358 isl_vec_free(sample);
2359 else if (sample) {
2360 isl_vec_free(bmap1->sample);
2361 bmap1->sample = sample;
2364 bmap1 = isl_basic_map_simplify(bmap1);
2365 return isl_basic_map_finalize(bmap1);
2366 error:
2367 if (sample)
2368 isl_vec_free(sample);
2369 isl_basic_map_free(bmap1);
2370 isl_basic_map_free(bmap2);
2371 return NULL;
2374 struct isl_basic_set *isl_basic_set_intersect(
2375 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2377 return (struct isl_basic_set *)
2378 isl_basic_map_intersect(
2379 (struct isl_basic_map *)bset1,
2380 (struct isl_basic_map *)bset2);
2383 /* Special case of isl_map_intersect, where both map1 and map2
2384 * are convex, without any divs and such that either map1 or map2
2385 * contains a single constraint. This constraint is then simply
2386 * added to the other map.
2388 static __isl_give isl_map *map_intersect_add_constraint(
2389 __isl_take isl_map *map1, __isl_take isl_map *map2)
2391 isl_assert(map1->ctx, map1->n == 1, goto error);
2392 isl_assert(map2->ctx, map1->n == 1, goto error);
2393 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2394 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2396 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2397 return isl_map_intersect(map2, map1);
2399 isl_assert(map2->ctx,
2400 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2402 map1 = isl_map_cow(map1);
2403 if (!map1)
2404 goto error;
2405 if (isl_map_plain_is_empty(map1)) {
2406 isl_map_free(map2);
2407 return map1;
2409 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2410 if (map2->p[0]->n_eq == 1)
2411 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2412 else
2413 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2414 map2->p[0]->ineq[0]);
2416 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2417 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2418 if (!map1->p[0])
2419 goto error;
2421 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2422 isl_basic_map_free(map1->p[0]);
2423 map1->n = 0;
2426 isl_map_free(map2);
2428 return map1;
2429 error:
2430 isl_map_free(map1);
2431 isl_map_free(map2);
2432 return NULL;
2435 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2436 __isl_take isl_map *map2)
2438 unsigned flags = 0;
2439 struct isl_map *result;
2440 int i, j;
2442 if (!map1 || !map2)
2443 goto error;
2445 if (isl_map_plain_is_empty(map1) &&
2446 isl_space_is_equal(map1->dim, map2->dim)) {
2447 isl_map_free(map2);
2448 return map1;
2450 if (isl_map_plain_is_empty(map2) &&
2451 isl_space_is_equal(map1->dim, map2->dim)) {
2452 isl_map_free(map1);
2453 return map2;
2456 if (map1->n == 1 && map2->n == 1 &&
2457 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2458 isl_space_is_equal(map1->dim, map2->dim) &&
2459 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2460 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2461 return map_intersect_add_constraint(map1, map2);
2462 if (isl_space_dim(map1->dim, isl_dim_all) ==
2463 isl_space_dim(map1->dim, isl_dim_param) &&
2464 isl_space_dim(map2->dim, isl_dim_all) !=
2465 isl_space_dim(map2->dim, isl_dim_param))
2466 return isl_map_intersect(map2, map1);
2468 if (isl_space_dim(map2->dim, isl_dim_all) !=
2469 isl_space_dim(map2->dim, isl_dim_param))
2470 isl_assert(map1->ctx,
2471 isl_space_is_equal(map1->dim, map2->dim), goto error);
2473 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2474 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2475 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2477 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2478 map1->n * map2->n, flags);
2479 if (!result)
2480 goto error;
2481 for (i = 0; i < map1->n; ++i)
2482 for (j = 0; j < map2->n; ++j) {
2483 struct isl_basic_map *part;
2484 part = isl_basic_map_intersect(
2485 isl_basic_map_copy(map1->p[i]),
2486 isl_basic_map_copy(map2->p[j]));
2487 if (isl_basic_map_is_empty(part))
2488 isl_basic_map_free(part);
2489 else
2490 result = isl_map_add_basic_map(result, part);
2491 if (!result)
2492 goto error;
2494 isl_map_free(map1);
2495 isl_map_free(map2);
2496 return result;
2497 error:
2498 isl_map_free(map1);
2499 isl_map_free(map2);
2500 return NULL;
2503 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2504 __isl_take isl_map *map2)
2506 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2509 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2511 return (struct isl_set *)
2512 isl_map_intersect((struct isl_map *)set1,
2513 (struct isl_map *)set2);
2516 /* The current implementation of isl_map_intersect accepts intersections
2517 * with parameter domains, so we can just call that for now.
2519 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2520 __isl_take isl_set *params)
2522 return isl_map_intersect(map, params);
2525 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2526 __isl_take isl_map *map2)
2528 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2531 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2532 __isl_take isl_set *params)
2534 return isl_map_intersect_params(set, params);
2537 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2539 isl_space *dim;
2540 struct isl_basic_set *bset;
2541 unsigned in;
2543 if (!bmap)
2544 return NULL;
2545 bmap = isl_basic_map_cow(bmap);
2546 if (!bmap)
2547 return NULL;
2548 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2549 in = isl_basic_map_n_in(bmap);
2550 bset = isl_basic_set_from_basic_map(bmap);
2551 bset = isl_basic_set_swap_vars(bset, in);
2552 return isl_basic_map_from_basic_set(bset, dim);
2555 static __isl_give isl_basic_map *basic_map_space_reset(
2556 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2558 isl_space *space;
2560 if (!isl_space_is_named_or_nested(bmap->dim, type))
2561 return bmap;
2563 space = isl_basic_map_get_space(bmap);
2564 space = isl_space_reset(space, type);
2565 bmap = isl_basic_map_reset_space(bmap, space);
2566 return bmap;
2569 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2570 enum isl_dim_type type, unsigned pos, unsigned n)
2572 isl_space *res_dim;
2573 struct isl_basic_map *res;
2574 struct isl_dim_map *dim_map;
2575 unsigned total, off;
2576 enum isl_dim_type t;
2578 if (n == 0)
2579 return basic_map_space_reset(bmap, type);
2581 if (!bmap)
2582 return NULL;
2584 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2586 total = isl_basic_map_total_dim(bmap) + n;
2587 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2588 off = 0;
2589 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2590 if (t != type) {
2591 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2592 } else {
2593 unsigned size = isl_basic_map_dim(bmap, t);
2594 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2595 0, pos, off);
2596 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2597 pos, size - pos, off + pos + n);
2599 off += isl_space_dim(res_dim, t);
2601 isl_dim_map_div(dim_map, bmap, off);
2603 res = isl_basic_map_alloc_space(res_dim,
2604 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2605 if (isl_basic_map_is_rational(bmap))
2606 res = isl_basic_map_set_rational(res);
2607 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2608 return isl_basic_map_finalize(res);
2611 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2612 enum isl_dim_type type, unsigned n)
2614 if (!bmap)
2615 return NULL;
2616 return isl_basic_map_insert(bmap, type,
2617 isl_basic_map_dim(bmap, type), n);
2620 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2621 enum isl_dim_type type, unsigned n)
2623 if (!bset)
2624 return NULL;
2625 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2626 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2627 error:
2628 isl_basic_set_free(bset);
2629 return NULL;
2632 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2633 enum isl_dim_type type)
2635 isl_space *space;
2637 if (!isl_space_is_named_or_nested(map->dim, type))
2638 return map;
2640 space = isl_map_get_space(map);
2641 space = isl_space_reset(space, type);
2642 map = isl_map_reset_space(map, space);
2643 return map;
2646 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2647 enum isl_dim_type type, unsigned pos, unsigned n)
2649 int i;
2651 if (n == 0)
2652 return map_space_reset(map, type);
2654 map = isl_map_cow(map);
2655 if (!map)
2656 return NULL;
2658 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2659 if (!map->dim)
2660 goto error;
2662 for (i = 0; i < map->n; ++i) {
2663 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2664 if (!map->p[i])
2665 goto error;
2668 return map;
2669 error:
2670 isl_map_free(map);
2671 return NULL;
2674 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2675 enum isl_dim_type type, unsigned pos, unsigned n)
2677 return isl_map_insert_dims(set, type, pos, n);
2680 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2681 enum isl_dim_type type, unsigned n)
2683 if (!map)
2684 return NULL;
2685 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2688 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2689 enum isl_dim_type type, unsigned n)
2691 if (!set)
2692 return NULL;
2693 isl_assert(set->ctx, type != isl_dim_in, goto error);
2694 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2695 error:
2696 isl_set_free(set);
2697 return NULL;
2700 __isl_give isl_basic_map *isl_basic_map_move_dims(
2701 __isl_take isl_basic_map *bmap,
2702 enum isl_dim_type dst_type, unsigned dst_pos,
2703 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2705 struct isl_dim_map *dim_map;
2706 struct isl_basic_map *res;
2707 enum isl_dim_type t;
2708 unsigned total, off;
2710 if (!bmap)
2711 return NULL;
2712 if (n == 0)
2713 return bmap;
2715 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2716 goto error);
2718 if (dst_type == src_type && dst_pos == src_pos)
2719 return bmap;
2721 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2723 if (pos(bmap->dim, dst_type) + dst_pos ==
2724 pos(bmap->dim, src_type) + src_pos +
2725 ((src_type < dst_type) ? n : 0)) {
2726 bmap = isl_basic_map_cow(bmap);
2727 if (!bmap)
2728 return NULL;
2730 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2731 src_type, src_pos, n);
2732 if (!bmap->dim)
2733 goto error;
2735 bmap = isl_basic_map_finalize(bmap);
2737 return bmap;
2740 total = isl_basic_map_total_dim(bmap);
2741 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2743 off = 0;
2744 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2745 unsigned size = isl_space_dim(bmap->dim, t);
2746 if (t == dst_type) {
2747 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2748 0, dst_pos, off);
2749 off += dst_pos;
2750 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2751 src_pos, n, off);
2752 off += n;
2753 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2754 dst_pos, size - dst_pos, off);
2755 off += size - dst_pos;
2756 } else if (t == src_type) {
2757 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2758 0, src_pos, off);
2759 off += src_pos;
2760 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2761 src_pos + n, size - src_pos - n, off);
2762 off += size - src_pos - n;
2763 } else {
2764 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2765 off += size;
2768 isl_dim_map_div(dim_map, bmap, off);
2770 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2771 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2772 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2774 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2775 src_type, src_pos, n);
2776 if (!bmap->dim)
2777 goto error;
2779 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2780 bmap = isl_basic_map_gauss(bmap, NULL);
2781 bmap = isl_basic_map_finalize(bmap);
2783 return bmap;
2784 error:
2785 isl_basic_map_free(bmap);
2786 return NULL;
2789 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2790 enum isl_dim_type dst_type, unsigned dst_pos,
2791 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2793 return (isl_basic_set *)isl_basic_map_move_dims(
2794 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2797 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2798 enum isl_dim_type dst_type, unsigned dst_pos,
2799 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2801 if (!set)
2802 return NULL;
2803 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2804 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2805 src_type, src_pos, n);
2806 error:
2807 isl_set_free(set);
2808 return NULL;
2811 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2812 enum isl_dim_type dst_type, unsigned dst_pos,
2813 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2815 int i;
2817 if (!map)
2818 return NULL;
2819 if (n == 0)
2820 return map;
2822 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2823 goto error);
2825 if (dst_type == src_type && dst_pos == src_pos)
2826 return map;
2828 isl_assert(map->ctx, dst_type != src_type, goto error);
2830 map = isl_map_cow(map);
2831 if (!map)
2832 return NULL;
2834 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2835 if (!map->dim)
2836 goto error;
2838 for (i = 0; i < map->n; ++i) {
2839 map->p[i] = isl_basic_map_move_dims(map->p[i],
2840 dst_type, dst_pos,
2841 src_type, src_pos, n);
2842 if (!map->p[i])
2843 goto error;
2846 return map;
2847 error:
2848 isl_map_free(map);
2849 return NULL;
2852 /* Move the specified dimensions to the last columns right before
2853 * the divs. Don't change the dimension specification of bmap.
2854 * That's the responsibility of the caller.
2856 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2857 enum isl_dim_type type, unsigned first, unsigned n)
2859 struct isl_dim_map *dim_map;
2860 struct isl_basic_map *res;
2861 enum isl_dim_type t;
2862 unsigned total, off;
2864 if (!bmap)
2865 return NULL;
2866 if (pos(bmap->dim, type) + first + n ==
2867 1 + isl_space_dim(bmap->dim, isl_dim_all))
2868 return bmap;
2870 total = isl_basic_map_total_dim(bmap);
2871 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2873 off = 0;
2874 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2875 unsigned size = isl_space_dim(bmap->dim, t);
2876 if (t == type) {
2877 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2878 0, first, off);
2879 off += first;
2880 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2881 first, n, total - bmap->n_div - n);
2882 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2883 first + n, size - (first + n), off);
2884 off += size - (first + n);
2885 } else {
2886 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2887 off += size;
2890 isl_dim_map_div(dim_map, bmap, off + n);
2892 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2893 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2894 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2895 return res;
2898 /* Turn the n dimensions of type type, starting at first
2899 * into existentially quantified variables.
2901 __isl_give isl_basic_map *isl_basic_map_project_out(
2902 __isl_take isl_basic_map *bmap,
2903 enum isl_dim_type type, unsigned first, unsigned n)
2905 int i;
2906 size_t row_size;
2907 isl_int **new_div;
2908 isl_int *old;
2910 if (n == 0)
2911 return bmap;
2913 if (!bmap)
2914 return NULL;
2916 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2917 return isl_basic_map_remove_dims(bmap, type, first, n);
2919 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2920 goto error);
2922 bmap = move_last(bmap, type, first, n);
2923 bmap = isl_basic_map_cow(bmap);
2924 if (!bmap)
2925 return NULL;
2927 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
2928 old = bmap->block2.data;
2929 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2930 (bmap->extra + n) * (1 + row_size));
2931 if (!bmap->block2.data)
2932 goto error;
2933 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2934 if (!new_div)
2935 goto error;
2936 for (i = 0; i < n; ++i) {
2937 new_div[i] = bmap->block2.data +
2938 (bmap->extra + i) * (1 + row_size);
2939 isl_seq_clr(new_div[i], 1 + row_size);
2941 for (i = 0; i < bmap->extra; ++i)
2942 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2943 free(bmap->div);
2944 bmap->div = new_div;
2945 bmap->n_div += n;
2946 bmap->extra += n;
2948 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2949 if (!bmap->dim)
2950 goto error;
2951 bmap = isl_basic_map_simplify(bmap);
2952 bmap = isl_basic_map_drop_redundant_divs(bmap);
2953 return isl_basic_map_finalize(bmap);
2954 error:
2955 isl_basic_map_free(bmap);
2956 return NULL;
2959 /* Turn the n dimensions of type type, starting at first
2960 * into existentially quantified variables.
2962 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2963 enum isl_dim_type type, unsigned first, unsigned n)
2965 return (isl_basic_set *)isl_basic_map_project_out(
2966 (isl_basic_map *)bset, type, first, n);
2969 /* Turn the n dimensions of type type, starting at first
2970 * into existentially quantified variables.
2972 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2973 enum isl_dim_type type, unsigned first, unsigned n)
2975 int i;
2977 if (!map)
2978 return NULL;
2980 if (n == 0)
2981 return map;
2983 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2985 map = isl_map_cow(map);
2986 if (!map)
2987 return NULL;
2989 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2990 if (!map->dim)
2991 goto error;
2993 for (i = 0; i < map->n; ++i) {
2994 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2995 if (!map->p[i])
2996 goto error;
2999 return map;
3000 error:
3001 isl_map_free(map);
3002 return NULL;
3005 /* Turn the n dimensions of type type, starting at first
3006 * into existentially quantified variables.
3008 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3009 enum isl_dim_type type, unsigned first, unsigned n)
3011 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3014 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3016 int i, j;
3018 for (i = 0; i < n; ++i) {
3019 j = isl_basic_map_alloc_div(bmap);
3020 if (j < 0)
3021 goto error;
3022 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3024 return bmap;
3025 error:
3026 isl_basic_map_free(bmap);
3027 return NULL;
3030 struct isl_basic_map *isl_basic_map_apply_range(
3031 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3033 isl_space *dim_result = NULL;
3034 struct isl_basic_map *bmap;
3035 unsigned n_in, n_out, n, nparam, total, pos;
3036 struct isl_dim_map *dim_map1, *dim_map2;
3038 if (!bmap1 || !bmap2)
3039 goto error;
3041 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3042 isl_space_copy(bmap2->dim));
3044 n_in = isl_basic_map_n_in(bmap1);
3045 n_out = isl_basic_map_n_out(bmap2);
3046 n = isl_basic_map_n_out(bmap1);
3047 nparam = isl_basic_map_n_param(bmap1);
3049 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3050 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3051 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3052 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3053 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3054 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3055 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3056 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3057 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3058 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3059 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3061 bmap = isl_basic_map_alloc_space(dim_result,
3062 bmap1->n_div + bmap2->n_div + n,
3063 bmap1->n_eq + bmap2->n_eq,
3064 bmap1->n_ineq + bmap2->n_ineq);
3065 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3066 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3067 bmap = add_divs(bmap, n);
3068 bmap = isl_basic_map_simplify(bmap);
3069 bmap = isl_basic_map_drop_redundant_divs(bmap);
3070 return isl_basic_map_finalize(bmap);
3071 error:
3072 isl_basic_map_free(bmap1);
3073 isl_basic_map_free(bmap2);
3074 return NULL;
3077 struct isl_basic_set *isl_basic_set_apply(
3078 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3080 if (!bset || !bmap)
3081 goto error;
3083 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3084 goto error);
3086 return (struct isl_basic_set *)
3087 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3088 error:
3089 isl_basic_set_free(bset);
3090 isl_basic_map_free(bmap);
3091 return NULL;
3094 struct isl_basic_map *isl_basic_map_apply_domain(
3095 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3097 if (!bmap1 || !bmap2)
3098 goto error;
3100 isl_assert(bmap1->ctx,
3101 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3102 isl_assert(bmap1->ctx,
3103 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3104 goto error);
3106 bmap1 = isl_basic_map_reverse(bmap1);
3107 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3108 return isl_basic_map_reverse(bmap1);
3109 error:
3110 isl_basic_map_free(bmap1);
3111 isl_basic_map_free(bmap2);
3112 return NULL;
3115 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3116 * A \cap B -> f(A) + f(B)
3118 struct isl_basic_map *isl_basic_map_sum(
3119 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3121 unsigned n_in, n_out, nparam, total, pos;
3122 struct isl_basic_map *bmap = NULL;
3123 struct isl_dim_map *dim_map1, *dim_map2;
3124 int i;
3126 if (!bmap1 || !bmap2)
3127 goto error;
3129 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3130 goto error);
3132 nparam = isl_basic_map_n_param(bmap1);
3133 n_in = isl_basic_map_n_in(bmap1);
3134 n_out = isl_basic_map_n_out(bmap1);
3136 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3137 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3138 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3139 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3140 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3141 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3142 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3143 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3144 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3145 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3146 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3148 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3149 bmap1->n_div + bmap2->n_div + 2 * n_out,
3150 bmap1->n_eq + bmap2->n_eq + n_out,
3151 bmap1->n_ineq + bmap2->n_ineq);
3152 for (i = 0; i < n_out; ++i) {
3153 int j = isl_basic_map_alloc_equality(bmap);
3154 if (j < 0)
3155 goto error;
3156 isl_seq_clr(bmap->eq[j], 1+total);
3157 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3158 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3159 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3161 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3162 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3163 bmap = add_divs(bmap, 2 * n_out);
3165 bmap = isl_basic_map_simplify(bmap);
3166 return isl_basic_map_finalize(bmap);
3167 error:
3168 isl_basic_map_free(bmap);
3169 isl_basic_map_free(bmap1);
3170 isl_basic_map_free(bmap2);
3171 return NULL;
3174 /* Given two maps A -> f(A) and B -> g(B), construct a map
3175 * A \cap B -> f(A) + f(B)
3177 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3179 struct isl_map *result;
3180 int i, j;
3182 if (!map1 || !map2)
3183 goto error;
3185 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3187 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3188 map1->n * map2->n, 0);
3189 if (!result)
3190 goto error;
3191 for (i = 0; i < map1->n; ++i)
3192 for (j = 0; j < map2->n; ++j) {
3193 struct isl_basic_map *part;
3194 part = isl_basic_map_sum(
3195 isl_basic_map_copy(map1->p[i]),
3196 isl_basic_map_copy(map2->p[j]));
3197 if (isl_basic_map_is_empty(part))
3198 isl_basic_map_free(part);
3199 else
3200 result = isl_map_add_basic_map(result, part);
3201 if (!result)
3202 goto error;
3204 isl_map_free(map1);
3205 isl_map_free(map2);
3206 return result;
3207 error:
3208 isl_map_free(map1);
3209 isl_map_free(map2);
3210 return NULL;
3213 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3214 __isl_take isl_set *set2)
3216 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3219 /* Given a basic map A -> f(A), construct A -> -f(A).
3221 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3223 int i, j;
3224 unsigned off, n;
3226 bmap = isl_basic_map_cow(bmap);
3227 if (!bmap)
3228 return NULL;
3230 n = isl_basic_map_dim(bmap, isl_dim_out);
3231 off = isl_basic_map_offset(bmap, isl_dim_out);
3232 for (i = 0; i < bmap->n_eq; ++i)
3233 for (j = 0; j < n; ++j)
3234 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3235 for (i = 0; i < bmap->n_ineq; ++i)
3236 for (j = 0; j < n; ++j)
3237 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3238 for (i = 0; i < bmap->n_div; ++i)
3239 for (j = 0; j < n; ++j)
3240 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3241 bmap = isl_basic_map_gauss(bmap, NULL);
3242 return isl_basic_map_finalize(bmap);
3245 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3247 return isl_basic_map_neg(bset);
3250 /* Given a map A -> f(A), construct A -> -f(A).
3252 struct isl_map *isl_map_neg(struct isl_map *map)
3254 int i;
3256 map = isl_map_cow(map);
3257 if (!map)
3258 return NULL;
3260 for (i = 0; i < map->n; ++i) {
3261 map->p[i] = isl_basic_map_neg(map->p[i]);
3262 if (!map->p[i])
3263 goto error;
3266 return map;
3267 error:
3268 isl_map_free(map);
3269 return NULL;
3272 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3274 return (isl_set *)isl_map_neg((isl_map *)set);
3277 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3278 * A -> floor(f(A)/d).
3280 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3281 isl_int d)
3283 unsigned n_in, n_out, nparam, total, pos;
3284 struct isl_basic_map *result = NULL;
3285 struct isl_dim_map *dim_map;
3286 int i;
3288 if (!bmap)
3289 return NULL;
3291 nparam = isl_basic_map_n_param(bmap);
3292 n_in = isl_basic_map_n_in(bmap);
3293 n_out = isl_basic_map_n_out(bmap);
3295 total = nparam + n_in + n_out + bmap->n_div + n_out;
3296 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3297 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3298 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3299 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3300 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3302 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3303 bmap->n_div + n_out,
3304 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3305 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3306 result = add_divs(result, n_out);
3307 for (i = 0; i < n_out; ++i) {
3308 int j;
3309 j = isl_basic_map_alloc_inequality(result);
3310 if (j < 0)
3311 goto error;
3312 isl_seq_clr(result->ineq[j], 1+total);
3313 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3314 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3315 j = isl_basic_map_alloc_inequality(result);
3316 if (j < 0)
3317 goto error;
3318 isl_seq_clr(result->ineq[j], 1+total);
3319 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3320 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3321 isl_int_sub_ui(result->ineq[j][0], d, 1);
3324 result = isl_basic_map_simplify(result);
3325 return isl_basic_map_finalize(result);
3326 error:
3327 isl_basic_map_free(result);
3328 return NULL;
3331 /* Given a map A -> f(A) and an integer d, construct a map
3332 * A -> floor(f(A)/d).
3334 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3336 int i;
3338 map = isl_map_cow(map);
3339 if (!map)
3340 return NULL;
3342 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3343 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3344 for (i = 0; i < map->n; ++i) {
3345 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3346 if (!map->p[i])
3347 goto error;
3350 return map;
3351 error:
3352 isl_map_free(map);
3353 return NULL;
3356 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3358 int i;
3359 unsigned nparam;
3360 unsigned n_in;
3362 i = isl_basic_map_alloc_equality(bmap);
3363 if (i < 0)
3364 goto error;
3365 nparam = isl_basic_map_n_param(bmap);
3366 n_in = isl_basic_map_n_in(bmap);
3367 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3368 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3369 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3370 return isl_basic_map_finalize(bmap);
3371 error:
3372 isl_basic_map_free(bmap);
3373 return NULL;
3376 /* Add a constraints to "bmap" expressing i_pos < o_pos
3378 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3380 int i;
3381 unsigned nparam;
3382 unsigned n_in;
3384 i = isl_basic_map_alloc_inequality(bmap);
3385 if (i < 0)
3386 goto error;
3387 nparam = isl_basic_map_n_param(bmap);
3388 n_in = isl_basic_map_n_in(bmap);
3389 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3390 isl_int_set_si(bmap->ineq[i][0], -1);
3391 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3392 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3393 return isl_basic_map_finalize(bmap);
3394 error:
3395 isl_basic_map_free(bmap);
3396 return NULL;
3399 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3401 static __isl_give isl_basic_map *var_less_or_equal(
3402 __isl_take isl_basic_map *bmap, unsigned pos)
3404 int i;
3405 unsigned nparam;
3406 unsigned n_in;
3408 i = isl_basic_map_alloc_inequality(bmap);
3409 if (i < 0)
3410 goto error;
3411 nparam = isl_basic_map_n_param(bmap);
3412 n_in = isl_basic_map_n_in(bmap);
3413 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3414 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3415 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3416 return isl_basic_map_finalize(bmap);
3417 error:
3418 isl_basic_map_free(bmap);
3419 return NULL;
3422 /* Add a constraints to "bmap" expressing i_pos > o_pos
3424 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3426 int i;
3427 unsigned nparam;
3428 unsigned n_in;
3430 i = isl_basic_map_alloc_inequality(bmap);
3431 if (i < 0)
3432 goto error;
3433 nparam = isl_basic_map_n_param(bmap);
3434 n_in = isl_basic_map_n_in(bmap);
3435 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3436 isl_int_set_si(bmap->ineq[i][0], -1);
3437 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3438 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3439 return isl_basic_map_finalize(bmap);
3440 error:
3441 isl_basic_map_free(bmap);
3442 return NULL;
3445 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3447 static __isl_give isl_basic_map *var_more_or_equal(
3448 __isl_take isl_basic_map *bmap, unsigned pos)
3450 int i;
3451 unsigned nparam;
3452 unsigned n_in;
3454 i = isl_basic_map_alloc_inequality(bmap);
3455 if (i < 0)
3456 goto error;
3457 nparam = isl_basic_map_n_param(bmap);
3458 n_in = isl_basic_map_n_in(bmap);
3459 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3460 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3461 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3462 return isl_basic_map_finalize(bmap);
3463 error:
3464 isl_basic_map_free(bmap);
3465 return NULL;
3468 __isl_give isl_basic_map *isl_basic_map_equal(
3469 __isl_take isl_space *dim, unsigned n_equal)
3471 int i;
3472 struct isl_basic_map *bmap;
3473 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3474 if (!bmap)
3475 return NULL;
3476 for (i = 0; i < n_equal && bmap; ++i)
3477 bmap = var_equal(bmap, i);
3478 return isl_basic_map_finalize(bmap);
3481 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3483 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3484 unsigned pos)
3486 int i;
3487 struct isl_basic_map *bmap;
3488 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3489 if (!bmap)
3490 return NULL;
3491 for (i = 0; i < pos && bmap; ++i)
3492 bmap = var_equal(bmap, i);
3493 if (bmap)
3494 bmap = var_less(bmap, pos);
3495 return isl_basic_map_finalize(bmap);
3498 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3500 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3501 __isl_take isl_space *dim, unsigned pos)
3503 int i;
3504 isl_basic_map *bmap;
3506 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3507 for (i = 0; i < pos; ++i)
3508 bmap = var_equal(bmap, i);
3509 bmap = var_less_or_equal(bmap, pos);
3510 return isl_basic_map_finalize(bmap);
3513 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3515 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3516 unsigned pos)
3518 int i;
3519 struct isl_basic_map *bmap;
3520 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3521 if (!bmap)
3522 return NULL;
3523 for (i = 0; i < pos && bmap; ++i)
3524 bmap = var_equal(bmap, i);
3525 if (bmap)
3526 bmap = var_more(bmap, pos);
3527 return isl_basic_map_finalize(bmap);
3530 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3532 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3533 __isl_take isl_space *dim, unsigned pos)
3535 int i;
3536 isl_basic_map *bmap;
3538 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3539 for (i = 0; i < pos; ++i)
3540 bmap = var_equal(bmap, i);
3541 bmap = var_more_or_equal(bmap, pos);
3542 return isl_basic_map_finalize(bmap);
3545 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3546 unsigned n, int equal)
3548 struct isl_map *map;
3549 int i;
3551 if (n == 0 && equal)
3552 return isl_map_universe(dims);
3554 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3556 for (i = 0; i + 1 < n; ++i)
3557 map = isl_map_add_basic_map(map,
3558 isl_basic_map_less_at(isl_space_copy(dims), i));
3559 if (n > 0) {
3560 if (equal)
3561 map = isl_map_add_basic_map(map,
3562 isl_basic_map_less_or_equal_at(dims, n - 1));
3563 else
3564 map = isl_map_add_basic_map(map,
3565 isl_basic_map_less_at(dims, n - 1));
3566 } else
3567 isl_space_free(dims);
3569 return map;
3572 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3574 if (!dims)
3575 return NULL;
3576 return map_lex_lte_first(dims, dims->n_out, equal);
3579 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3581 return map_lex_lte_first(dim, n, 0);
3584 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3586 return map_lex_lte_first(dim, n, 1);
3589 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3591 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3594 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3596 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3599 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3600 unsigned n, int equal)
3602 struct isl_map *map;
3603 int i;
3605 if (n == 0 && equal)
3606 return isl_map_universe(dims);
3608 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3610 for (i = 0; i + 1 < n; ++i)
3611 map = isl_map_add_basic_map(map,
3612 isl_basic_map_more_at(isl_space_copy(dims), i));
3613 if (n > 0) {
3614 if (equal)
3615 map = isl_map_add_basic_map(map,
3616 isl_basic_map_more_or_equal_at(dims, n - 1));
3617 else
3618 map = isl_map_add_basic_map(map,
3619 isl_basic_map_more_at(dims, n - 1));
3620 } else
3621 isl_space_free(dims);
3623 return map;
3626 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3628 if (!dims)
3629 return NULL;
3630 return map_lex_gte_first(dims, dims->n_out, equal);
3633 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3635 return map_lex_gte_first(dim, n, 0);
3638 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3640 return map_lex_gte_first(dim, n, 1);
3643 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3645 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3648 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3650 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3653 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3654 __isl_take isl_set *set2)
3656 isl_map *map;
3657 map = isl_map_lex_le(isl_set_get_space(set1));
3658 map = isl_map_intersect_domain(map, set1);
3659 map = isl_map_intersect_range(map, set2);
3660 return map;
3663 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3664 __isl_take isl_set *set2)
3666 isl_map *map;
3667 map = isl_map_lex_lt(isl_set_get_space(set1));
3668 map = isl_map_intersect_domain(map, set1);
3669 map = isl_map_intersect_range(map, set2);
3670 return map;
3673 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3674 __isl_take isl_set *set2)
3676 isl_map *map;
3677 map = isl_map_lex_ge(isl_set_get_space(set1));
3678 map = isl_map_intersect_domain(map, set1);
3679 map = isl_map_intersect_range(map, set2);
3680 return map;
3683 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3684 __isl_take isl_set *set2)
3686 isl_map *map;
3687 map = isl_map_lex_gt(isl_set_get_space(set1));
3688 map = isl_map_intersect_domain(map, set1);
3689 map = isl_map_intersect_range(map, set2);
3690 return map;
3693 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3694 __isl_take isl_map *map2)
3696 isl_map *map;
3697 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3698 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3699 map = isl_map_apply_range(map, isl_map_reverse(map2));
3700 return map;
3703 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3704 __isl_take isl_map *map2)
3706 isl_map *map;
3707 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3708 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3709 map = isl_map_apply_range(map, isl_map_reverse(map2));
3710 return map;
3713 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3714 __isl_take isl_map *map2)
3716 isl_map *map;
3717 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3718 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3719 map = isl_map_apply_range(map, isl_map_reverse(map2));
3720 return map;
3723 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3724 __isl_take isl_map *map2)
3726 isl_map *map;
3727 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
3728 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3729 map = isl_map_apply_range(map, isl_map_reverse(map2));
3730 return map;
3733 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
3734 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3736 struct isl_basic_map *bmap;
3738 bset = isl_basic_set_cow(bset);
3739 if (!bset || !dim)
3740 goto error;
3742 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3743 isl_space_free(bset->dim);
3744 bmap = (struct isl_basic_map *) bset;
3745 bmap->dim = dim;
3746 return isl_basic_map_finalize(bmap);
3747 error:
3748 isl_basic_set_free(bset);
3749 isl_space_free(dim);
3750 return NULL;
3753 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3755 if (!bmap)
3756 goto error;
3757 if (bmap->dim->n_in == 0)
3758 return (struct isl_basic_set *)bmap;
3759 bmap = isl_basic_map_cow(bmap);
3760 if (!bmap)
3761 goto error;
3762 bmap->dim = isl_space_as_set_space(bmap->dim);
3763 if (!bmap->dim)
3764 goto error;
3765 bmap = isl_basic_map_finalize(bmap);
3766 return (struct isl_basic_set *)bmap;
3767 error:
3768 isl_basic_map_free(bmap);
3769 return NULL;
3772 /* For a div d = floor(f/m), add the constraints
3774 * f - m d >= 0
3775 * -(f-(n-1)) + m d >= 0
3777 * Note that the second constraint is the negation of
3779 * f - m d >= n
3781 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3782 unsigned pos, isl_int *div)
3784 int i, j;
3785 unsigned total = isl_basic_map_total_dim(bmap);
3787 i = isl_basic_map_alloc_inequality(bmap);
3788 if (i < 0)
3789 return -1;
3790 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3791 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3793 j = isl_basic_map_alloc_inequality(bmap);
3794 if (j < 0)
3795 return -1;
3796 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3797 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3798 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3799 return j;
3802 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3803 unsigned pos, isl_int *div)
3805 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3806 pos, div);
3809 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3811 unsigned total = isl_basic_map_total_dim(bmap);
3812 unsigned div_pos = total - bmap->n_div + div;
3814 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3815 bmap->div[div]);
3818 struct isl_basic_set *isl_basic_map_underlying_set(
3819 struct isl_basic_map *bmap)
3821 if (!bmap)
3822 goto error;
3823 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3824 bmap->n_div == 0 &&
3825 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3826 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3827 return (struct isl_basic_set *)bmap;
3828 bmap = isl_basic_map_cow(bmap);
3829 if (!bmap)
3830 goto error;
3831 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3832 if (!bmap->dim)
3833 goto error;
3834 bmap->extra -= bmap->n_div;
3835 bmap->n_div = 0;
3836 bmap = isl_basic_map_finalize(bmap);
3837 return (struct isl_basic_set *)bmap;
3838 error:
3839 return NULL;
3842 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3843 __isl_take isl_basic_set *bset)
3845 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3848 struct isl_basic_map *isl_basic_map_overlying_set(
3849 struct isl_basic_set *bset, struct isl_basic_map *like)
3851 struct isl_basic_map *bmap;
3852 struct isl_ctx *ctx;
3853 unsigned total;
3854 int i;
3856 if (!bset || !like)
3857 goto error;
3858 ctx = bset->ctx;
3859 isl_assert(ctx, bset->n_div == 0, goto error);
3860 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3861 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3862 goto error);
3863 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
3864 isl_basic_map_free(like);
3865 return (struct isl_basic_map *)bset;
3867 bset = isl_basic_set_cow(bset);
3868 if (!bset)
3869 goto error;
3870 total = bset->dim->n_out + bset->extra;
3871 bmap = (struct isl_basic_map *)bset;
3872 isl_space_free(bmap->dim);
3873 bmap->dim = isl_space_copy(like->dim);
3874 if (!bmap->dim)
3875 goto error;
3876 bmap->n_div = like->n_div;
3877 bmap->extra += like->n_div;
3878 if (bmap->extra) {
3879 unsigned ltotal;
3880 isl_int **div;
3881 ltotal = total - bmap->extra + like->extra;
3882 if (ltotal > total)
3883 ltotal = total;
3884 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3885 bmap->extra * (1 + 1 + total));
3886 if (isl_blk_is_error(bmap->block2))
3887 goto error;
3888 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3889 if (!div)
3890 goto error;
3891 bmap->div = div;
3892 for (i = 0; i < bmap->extra; ++i)
3893 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3894 for (i = 0; i < like->n_div; ++i) {
3895 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3896 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3898 bmap = isl_basic_map_extend_constraints(bmap,
3899 0, 2 * like->n_div);
3900 for (i = 0; i < like->n_div; ++i) {
3901 if (isl_int_is_zero(bmap->div[i][0]))
3902 continue;
3903 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3904 goto error;
3907 isl_basic_map_free(like);
3908 bmap = isl_basic_map_simplify(bmap);
3909 bmap = isl_basic_map_finalize(bmap);
3910 return bmap;
3911 error:
3912 isl_basic_map_free(like);
3913 isl_basic_set_free(bset);
3914 return NULL;
3917 struct isl_basic_set *isl_basic_set_from_underlying_set(
3918 struct isl_basic_set *bset, struct isl_basic_set *like)
3920 return (struct isl_basic_set *)
3921 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3924 struct isl_set *isl_set_from_underlying_set(
3925 struct isl_set *set, struct isl_basic_set *like)
3927 int i;
3929 if (!set || !like)
3930 goto error;
3931 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3932 goto error);
3933 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
3934 isl_basic_set_free(like);
3935 return set;
3937 set = isl_set_cow(set);
3938 if (!set)
3939 goto error;
3940 for (i = 0; i < set->n; ++i) {
3941 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3942 isl_basic_set_copy(like));
3943 if (!set->p[i])
3944 goto error;
3946 isl_space_free(set->dim);
3947 set->dim = isl_space_copy(like->dim);
3948 if (!set->dim)
3949 goto error;
3950 isl_basic_set_free(like);
3951 return set;
3952 error:
3953 isl_basic_set_free(like);
3954 isl_set_free(set);
3955 return NULL;
3958 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3960 int i;
3962 map = isl_map_cow(map);
3963 if (!map)
3964 return NULL;
3965 map->dim = isl_space_cow(map->dim);
3966 if (!map->dim)
3967 goto error;
3969 for (i = 1; i < map->n; ++i)
3970 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3971 goto error);
3972 for (i = 0; i < map->n; ++i) {
3973 map->p[i] = (struct isl_basic_map *)
3974 isl_basic_map_underlying_set(map->p[i]);
3975 if (!map->p[i])
3976 goto error;
3978 if (map->n == 0)
3979 map->dim = isl_space_underlying(map->dim, 0);
3980 else {
3981 isl_space_free(map->dim);
3982 map->dim = isl_space_copy(map->p[0]->dim);
3984 if (!map->dim)
3985 goto error;
3986 return (struct isl_set *)map;
3987 error:
3988 isl_map_free(map);
3989 return NULL;
3992 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3994 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3997 __isl_give isl_basic_map *isl_basic_map_reset_space(
3998 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4000 bmap = isl_basic_map_cow(bmap);
4001 if (!bmap || !dim)
4002 goto error;
4004 isl_space_free(bmap->dim);
4005 bmap->dim = dim;
4007 bmap = isl_basic_map_finalize(bmap);
4009 return bmap;
4010 error:
4011 isl_basic_map_free(bmap);
4012 isl_space_free(dim);
4013 return NULL;
4016 __isl_give isl_basic_set *isl_basic_set_reset_space(
4017 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4019 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4020 dim);
4023 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4024 __isl_take isl_space *dim)
4026 int i;
4028 map = isl_map_cow(map);
4029 if (!map || !dim)
4030 goto error;
4032 for (i = 0; i < map->n; ++i) {
4033 map->p[i] = isl_basic_map_reset_space(map->p[i],
4034 isl_space_copy(dim));
4035 if (!map->p[i])
4036 goto error;
4038 isl_space_free(map->dim);
4039 map->dim = dim;
4041 return map;
4042 error:
4043 isl_map_free(map);
4044 isl_space_free(dim);
4045 return NULL;
4048 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4049 __isl_take isl_space *dim)
4051 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4054 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4056 isl_space *dim;
4057 struct isl_basic_set *domain;
4058 unsigned n_in;
4059 unsigned n_out;
4061 if (!bmap)
4062 return NULL;
4063 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4065 n_in = isl_basic_map_n_in(bmap);
4066 n_out = isl_basic_map_n_out(bmap);
4067 domain = isl_basic_set_from_basic_map(bmap);
4068 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4070 domain = isl_basic_set_reset_space(domain, dim);
4072 return domain;
4075 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4077 if (!bmap)
4078 return -1;
4079 return isl_space_may_be_set(bmap->dim);
4082 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4084 if (!bmap)
4085 return NULL;
4086 if (isl_basic_map_may_be_set(bmap))
4087 return bmap;
4088 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4091 __isl_give isl_basic_map *isl_basic_map_domain_map(
4092 __isl_take isl_basic_map *bmap)
4094 int i, k;
4095 isl_space *dim;
4096 isl_basic_map *domain;
4097 int nparam, n_in, n_out;
4098 unsigned total;
4100 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4101 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4102 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4104 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4105 domain = isl_basic_map_universe(dim);
4107 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4108 bmap = isl_basic_map_apply_range(bmap, domain);
4109 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4111 total = isl_basic_map_total_dim(bmap);
4113 for (i = 0; i < n_in; ++i) {
4114 k = isl_basic_map_alloc_equality(bmap);
4115 if (k < 0)
4116 goto error;
4117 isl_seq_clr(bmap->eq[k], 1 + total);
4118 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4119 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4122 bmap = isl_basic_map_gauss(bmap, NULL);
4123 return isl_basic_map_finalize(bmap);
4124 error:
4125 isl_basic_map_free(bmap);
4126 return NULL;
4129 __isl_give isl_basic_map *isl_basic_map_range_map(
4130 __isl_take isl_basic_map *bmap)
4132 int i, k;
4133 isl_space *dim;
4134 isl_basic_map *range;
4135 int nparam, n_in, n_out;
4136 unsigned total;
4138 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4139 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4140 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4142 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4143 range = isl_basic_map_universe(dim);
4145 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4146 bmap = isl_basic_map_apply_range(bmap, range);
4147 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4149 total = isl_basic_map_total_dim(bmap);
4151 for (i = 0; i < n_out; ++i) {
4152 k = isl_basic_map_alloc_equality(bmap);
4153 if (k < 0)
4154 goto error;
4155 isl_seq_clr(bmap->eq[k], 1 + total);
4156 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4157 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4160 bmap = isl_basic_map_gauss(bmap, NULL);
4161 return isl_basic_map_finalize(bmap);
4162 error:
4163 isl_basic_map_free(bmap);
4164 return NULL;
4167 int isl_map_may_be_set(__isl_keep isl_map *map)
4169 if (!map)
4170 return -1;
4171 return isl_space_may_be_set(map->dim);
4174 struct isl_set *isl_map_range(struct isl_map *map)
4176 int i;
4177 struct isl_set *set;
4179 if (!map)
4180 goto error;
4181 if (isl_map_may_be_set(map))
4182 return (isl_set *)map;
4184 map = isl_map_cow(map);
4185 if (!map)
4186 goto error;
4188 set = (struct isl_set *) map;
4189 set->dim = isl_space_drop_inputs(set->dim, 0, set->dim->n_in);
4190 if (!set->dim)
4191 goto error;
4192 for (i = 0; i < map->n; ++i) {
4193 set->p[i] = isl_basic_map_range(map->p[i]);
4194 if (!set->p[i])
4195 goto error;
4197 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4198 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4199 return set;
4200 error:
4201 isl_map_free(map);
4202 return NULL;
4205 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4207 int i;
4208 isl_space *domain_dim;
4210 map = isl_map_cow(map);
4211 if (!map)
4212 return NULL;
4214 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4215 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4216 map->dim = isl_space_join(map->dim, domain_dim);
4217 if (!map->dim)
4218 goto error;
4219 for (i = 0; i < map->n; ++i) {
4220 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4221 if (!map->p[i])
4222 goto error;
4224 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4225 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4226 return map;
4227 error:
4228 isl_map_free(map);
4229 return NULL;
4232 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4234 int i;
4235 isl_space *range_dim;
4237 map = isl_map_cow(map);
4238 if (!map)
4239 return NULL;
4241 range_dim = isl_space_range(isl_map_get_space(map));
4242 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4243 map->dim = isl_space_join(map->dim, range_dim);
4244 if (!map->dim)
4245 goto error;
4246 for (i = 0; i < map->n; ++i) {
4247 map->p[i] = isl_basic_map_range_map(map->p[i]);
4248 if (!map->p[i])
4249 goto error;
4251 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4252 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4253 return map;
4254 error:
4255 isl_map_free(map);
4256 return NULL;
4259 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4260 __isl_take isl_space *dim)
4262 int i;
4263 struct isl_map *map = NULL;
4265 set = isl_set_cow(set);
4266 if (!set || !dim)
4267 goto error;
4268 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4269 map = (struct isl_map *)set;
4270 for (i = 0; i < set->n; ++i) {
4271 map->p[i] = isl_basic_map_from_basic_set(
4272 set->p[i], isl_space_copy(dim));
4273 if (!map->p[i])
4274 goto error;
4276 isl_space_free(map->dim);
4277 map->dim = dim;
4278 return map;
4279 error:
4280 isl_space_free(dim);
4281 isl_set_free(set);
4282 return NULL;
4285 __isl_give isl_basic_map *isl_basic_map_from_domain(
4286 __isl_take isl_basic_set *bset)
4288 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4291 __isl_give isl_basic_map *isl_basic_map_from_range(
4292 __isl_take isl_basic_set *bset)
4294 return (isl_basic_map *)bset;
4297 struct isl_map *isl_map_from_range(struct isl_set *set)
4299 return (struct isl_map *)set;
4302 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4304 return isl_map_reverse(isl_map_from_range(set));
4307 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4308 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4310 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4311 isl_basic_map_from_range(range));
4314 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4315 __isl_take isl_set *range)
4317 return isl_map_apply_range(isl_map_from_domain(domain),
4318 isl_map_from_range(range));
4321 struct isl_set *isl_set_from_map(struct isl_map *map)
4323 int i;
4324 struct isl_set *set = NULL;
4326 if (!map)
4327 return NULL;
4328 map = isl_map_cow(map);
4329 if (!map)
4330 return NULL;
4331 map->dim = isl_space_as_set_space(map->dim);
4332 if (!map->dim)
4333 goto error;
4334 set = (struct isl_set *)map;
4335 for (i = 0; i < map->n; ++i) {
4336 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4337 if (!set->p[i])
4338 goto error;
4340 return set;
4341 error:
4342 isl_map_free(map);
4343 return NULL;
4346 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4347 unsigned flags)
4349 struct isl_map *map;
4351 if (!dim)
4352 return NULL;
4353 isl_assert(dim->ctx, n >= 0, return NULL);
4354 map = isl_alloc(dim->ctx, struct isl_map,
4355 sizeof(struct isl_map) +
4356 (n - 1) * sizeof(struct isl_basic_map *));
4357 if (!map)
4358 goto error;
4360 map->ctx = dim->ctx;
4361 isl_ctx_ref(map->ctx);
4362 map->ref = 1;
4363 map->size = n;
4364 map->n = 0;
4365 map->dim = dim;
4366 map->flags = flags;
4367 return map;
4368 error:
4369 isl_space_free(dim);
4370 return NULL;
4373 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4374 unsigned nparam, unsigned in, unsigned out, int n,
4375 unsigned flags)
4377 struct isl_map *map;
4378 isl_space *dims;
4380 dims = isl_space_alloc(ctx, nparam, in, out);
4381 if (!dims)
4382 return NULL;
4384 map = isl_map_alloc_space(dims, n, flags);
4385 return map;
4388 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4390 struct isl_basic_map *bmap;
4391 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4392 bmap = isl_basic_map_set_to_empty(bmap);
4393 return bmap;
4396 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4398 struct isl_basic_set *bset;
4399 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4400 bset = isl_basic_set_set_to_empty(bset);
4401 return bset;
4404 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4406 struct isl_basic_map *bmap;
4407 if (!model)
4408 return NULL;
4409 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4410 bmap = isl_basic_map_set_to_empty(bmap);
4411 return bmap;
4414 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4416 struct isl_basic_map *bmap;
4417 if (!model)
4418 return NULL;
4419 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4420 bmap = isl_basic_map_set_to_empty(bmap);
4421 return bmap;
4424 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4426 struct isl_basic_set *bset;
4427 if (!model)
4428 return NULL;
4429 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4430 bset = isl_basic_set_set_to_empty(bset);
4431 return bset;
4434 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4436 struct isl_basic_map *bmap;
4437 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4438 bmap = isl_basic_map_finalize(bmap);
4439 return bmap;
4442 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4444 struct isl_basic_set *bset;
4445 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4446 bset = isl_basic_set_finalize(bset);
4447 return bset;
4450 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4452 int i;
4453 unsigned total = isl_space_dim(dim, isl_dim_all);
4454 isl_basic_map *bmap;
4456 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4457 for (i = 0; i < total; ++i) {
4458 int k = isl_basic_map_alloc_inequality(bmap);
4459 if (k < 0)
4460 goto error;
4461 isl_seq_clr(bmap->ineq[k], 1 + total);
4462 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4464 return bmap;
4465 error:
4466 isl_basic_map_free(bmap);
4467 return NULL;
4470 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4472 return isl_basic_map_nat_universe(dim);
4475 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4477 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4480 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4482 return isl_map_nat_universe(dim);
4485 __isl_give isl_basic_map *isl_basic_map_universe_like(
4486 __isl_keep isl_basic_map *model)
4488 if (!model)
4489 return NULL;
4490 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4493 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4495 if (!model)
4496 return NULL;
4497 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4500 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4501 __isl_keep isl_set *model)
4503 if (!model)
4504 return NULL;
4505 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4508 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4510 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4513 struct isl_map *isl_map_empty_like(struct isl_map *model)
4515 if (!model)
4516 return NULL;
4517 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4520 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4522 if (!model)
4523 return NULL;
4524 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4527 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4529 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4532 struct isl_set *isl_set_empty_like(struct isl_set *model)
4534 if (!model)
4535 return NULL;
4536 return isl_set_empty(isl_space_copy(model->dim));
4539 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4541 struct isl_map *map;
4542 if (!dim)
4543 return NULL;
4544 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4545 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4546 return map;
4549 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4551 struct isl_set *set;
4552 if (!dim)
4553 return NULL;
4554 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4555 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4556 return set;
4559 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4561 if (!model)
4562 return NULL;
4563 return isl_set_universe(isl_space_copy(model->dim));
4566 struct isl_map *isl_map_dup(struct isl_map *map)
4568 int i;
4569 struct isl_map *dup;
4571 if (!map)
4572 return NULL;
4573 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4574 for (i = 0; i < map->n; ++i)
4575 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4576 return dup;
4579 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4580 __isl_take isl_basic_map *bmap)
4582 if (!bmap || !map)
4583 goto error;
4584 if (isl_basic_map_plain_is_empty(bmap)) {
4585 isl_basic_map_free(bmap);
4586 return map;
4588 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4589 isl_assert(map->ctx, map->n < map->size, goto error);
4590 map->p[map->n] = bmap;
4591 map->n++;
4592 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4593 return map;
4594 error:
4595 if (map)
4596 isl_map_free(map);
4597 if (bmap)
4598 isl_basic_map_free(bmap);
4599 return NULL;
4602 void isl_map_free(struct isl_map *map)
4604 int i;
4606 if (!map)
4607 return;
4609 if (--map->ref > 0)
4610 return;
4612 isl_ctx_deref(map->ctx);
4613 for (i = 0; i < map->n; ++i)
4614 isl_basic_map_free(map->p[i]);
4615 isl_space_free(map->dim);
4616 free(map);
4619 struct isl_map *isl_map_extend(struct isl_map *base,
4620 unsigned nparam, unsigned n_in, unsigned n_out)
4622 int i;
4624 base = isl_map_cow(base);
4625 if (!base)
4626 return NULL;
4628 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4629 if (!base->dim)
4630 goto error;
4631 for (i = 0; i < base->n; ++i) {
4632 base->p[i] = isl_basic_map_extend_space(base->p[i],
4633 isl_space_copy(base->dim), 0, 0, 0);
4634 if (!base->p[i])
4635 goto error;
4637 return base;
4638 error:
4639 isl_map_free(base);
4640 return NULL;
4643 struct isl_set *isl_set_extend(struct isl_set *base,
4644 unsigned nparam, unsigned dim)
4646 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4647 nparam, 0, dim);
4650 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4651 struct isl_basic_map *bmap, unsigned pos, int value)
4653 int j;
4655 bmap = isl_basic_map_cow(bmap);
4656 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4657 j = isl_basic_map_alloc_equality(bmap);
4658 if (j < 0)
4659 goto error;
4660 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4661 isl_int_set_si(bmap->eq[j][pos], -1);
4662 isl_int_set_si(bmap->eq[j][0], value);
4663 bmap = isl_basic_map_simplify(bmap);
4664 return isl_basic_map_finalize(bmap);
4665 error:
4666 isl_basic_map_free(bmap);
4667 return NULL;
4670 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4671 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4673 int j;
4675 bmap = isl_basic_map_cow(bmap);
4676 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4677 j = isl_basic_map_alloc_equality(bmap);
4678 if (j < 0)
4679 goto error;
4680 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4681 isl_int_set_si(bmap->eq[j][pos], -1);
4682 isl_int_set(bmap->eq[j][0], value);
4683 bmap = isl_basic_map_simplify(bmap);
4684 return isl_basic_map_finalize(bmap);
4685 error:
4686 isl_basic_map_free(bmap);
4687 return NULL;
4690 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4691 enum isl_dim_type type, unsigned pos, int value)
4693 if (!bmap)
4694 return NULL;
4695 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4696 return isl_basic_map_fix_pos_si(bmap,
4697 isl_basic_map_offset(bmap, type) + pos, value);
4698 error:
4699 isl_basic_map_free(bmap);
4700 return NULL;
4703 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4704 enum isl_dim_type type, unsigned pos, isl_int value)
4706 if (!bmap)
4707 return NULL;
4708 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4709 return isl_basic_map_fix_pos(bmap,
4710 isl_basic_map_offset(bmap, type) + pos, value);
4711 error:
4712 isl_basic_map_free(bmap);
4713 return NULL;
4716 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4717 enum isl_dim_type type, unsigned pos, int value)
4719 return (struct isl_basic_set *)
4720 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4721 type, pos, value);
4724 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4725 enum isl_dim_type type, unsigned pos, isl_int value)
4727 return (struct isl_basic_set *)
4728 isl_basic_map_fix((struct isl_basic_map *)bset,
4729 type, pos, value);
4732 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4733 unsigned input, int value)
4735 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4738 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4739 unsigned dim, int value)
4741 return (struct isl_basic_set *)
4742 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4743 isl_dim_set, dim, value);
4746 struct isl_map *isl_map_fix_si(struct isl_map *map,
4747 enum isl_dim_type type, unsigned pos, int value)
4749 int i;
4751 map = isl_map_cow(map);
4752 if (!map)
4753 return NULL;
4755 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4756 for (i = 0; i < map->n; ++i) {
4757 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4758 if (!map->p[i])
4759 goto error;
4761 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4762 return map;
4763 error:
4764 isl_map_free(map);
4765 return NULL;
4768 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4769 enum isl_dim_type type, unsigned pos, int value)
4771 return (struct isl_set *)
4772 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4775 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4776 enum isl_dim_type type, unsigned pos, isl_int value)
4778 int i;
4780 map = isl_map_cow(map);
4781 if (!map)
4782 return NULL;
4784 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4785 for (i = 0; i < map->n; ++i) {
4786 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4787 if (!map->p[i])
4788 goto error;
4790 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4791 return map;
4792 error:
4793 isl_map_free(map);
4794 return NULL;
4797 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4798 enum isl_dim_type type, unsigned pos, isl_int value)
4800 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4803 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4804 unsigned input, int value)
4806 return isl_map_fix_si(map, isl_dim_in, input, value);
4809 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4811 return (struct isl_set *)
4812 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4815 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4816 __isl_take isl_basic_map *bmap,
4817 enum isl_dim_type type, unsigned pos, int value)
4819 int j;
4821 if (!bmap)
4822 return NULL;
4823 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4824 pos += isl_basic_map_offset(bmap, type);
4825 bmap = isl_basic_map_cow(bmap);
4826 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4827 j = isl_basic_map_alloc_inequality(bmap);
4828 if (j < 0)
4829 goto error;
4830 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4831 isl_int_set_si(bmap->ineq[j][pos], 1);
4832 isl_int_set_si(bmap->ineq[j][0], -value);
4833 bmap = isl_basic_map_simplify(bmap);
4834 return isl_basic_map_finalize(bmap);
4835 error:
4836 isl_basic_map_free(bmap);
4837 return NULL;
4840 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4841 unsigned dim, isl_int value)
4843 int j;
4845 bset = isl_basic_set_cow(bset);
4846 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4847 j = isl_basic_set_alloc_inequality(bset);
4848 if (j < 0)
4849 goto error;
4850 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4851 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4852 isl_int_neg(bset->ineq[j][0], value);
4853 bset = isl_basic_set_simplify(bset);
4854 return isl_basic_set_finalize(bset);
4855 error:
4856 isl_basic_set_free(bset);
4857 return NULL;
4860 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4861 enum isl_dim_type type, unsigned pos, int value)
4863 int i;
4865 map = isl_map_cow(map);
4866 if (!map)
4867 return NULL;
4869 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4870 for (i = 0; i < map->n; ++i) {
4871 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4872 type, pos, value);
4873 if (!map->p[i])
4874 goto error;
4876 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4877 return map;
4878 error:
4879 isl_map_free(map);
4880 return NULL;
4883 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4884 enum isl_dim_type type, unsigned pos, int value)
4886 return (struct isl_set *)
4887 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4890 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4891 isl_int value)
4893 int i;
4895 set = isl_set_cow(set);
4896 if (!set)
4897 return NULL;
4899 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4900 for (i = 0; i < set->n; ++i) {
4901 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4902 if (!set->p[i])
4903 goto error;
4905 return set;
4906 error:
4907 isl_set_free(set);
4908 return NULL;
4911 struct isl_map *isl_map_reverse(struct isl_map *map)
4913 int i;
4915 map = isl_map_cow(map);
4916 if (!map)
4917 return NULL;
4919 map->dim = isl_space_reverse(map->dim);
4920 if (!map->dim)
4921 goto error;
4922 for (i = 0; i < map->n; ++i) {
4923 map->p[i] = isl_basic_map_reverse(map->p[i]);
4924 if (!map->p[i])
4925 goto error;
4927 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4928 return map;
4929 error:
4930 isl_map_free(map);
4931 return NULL;
4934 static struct isl_map *isl_basic_map_partial_lexopt(
4935 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4936 struct isl_set **empty, int max)
4938 if (!bmap)
4939 goto error;
4940 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4941 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4942 else
4943 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4944 error:
4945 isl_basic_map_free(bmap);
4946 isl_basic_set_free(dom);
4947 if (empty)
4948 *empty = NULL;
4949 return NULL;
4952 struct isl_map *isl_basic_map_partial_lexmax(
4953 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4954 struct isl_set **empty)
4956 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4959 struct isl_map *isl_basic_map_partial_lexmin(
4960 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4961 struct isl_set **empty)
4963 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4966 struct isl_set *isl_basic_set_partial_lexmin(
4967 struct isl_basic_set *bset, struct isl_basic_set *dom,
4968 struct isl_set **empty)
4970 return (struct isl_set *)
4971 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4972 dom, empty);
4975 struct isl_set *isl_basic_set_partial_lexmax(
4976 struct isl_basic_set *bset, struct isl_basic_set *dom,
4977 struct isl_set **empty)
4979 return (struct isl_set *)
4980 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4981 dom, empty);
4984 /* Given a basic map "bmap", compute the lexicographically minimal
4985 * (or maximal) image element for each domain element in dom.
4986 * Set *empty to those elements in dom that do not have an image element.
4988 * We first make sure the basic sets in dom are disjoint and then
4989 * simply collect the results over each of the basic sets separately.
4990 * We could probably improve the efficiency a bit by moving the union
4991 * domain down into the parametric integer programming.
4993 static __isl_give isl_map *basic_map_partial_lexopt(
4994 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4995 __isl_give isl_set **empty, int max)
4997 int i;
4998 struct isl_map *res;
5000 dom = isl_set_make_disjoint(dom);
5001 if (!dom)
5002 goto error;
5004 if (isl_set_plain_is_empty(dom)) {
5005 res = isl_map_empty_like_basic_map(bmap);
5006 *empty = isl_set_empty_like(dom);
5007 isl_set_free(dom);
5008 isl_basic_map_free(bmap);
5009 return res;
5012 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5013 isl_basic_set_copy(dom->p[0]), empty, max);
5015 for (i = 1; i < dom->n; ++i) {
5016 struct isl_map *res_i;
5017 struct isl_set *empty_i;
5019 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5020 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5022 res = isl_map_union_disjoint(res, res_i);
5023 *empty = isl_set_union_disjoint(*empty, empty_i);
5026 isl_set_free(dom);
5027 isl_basic_map_free(bmap);
5028 return res;
5029 error:
5030 *empty = NULL;
5031 isl_set_free(dom);
5032 isl_basic_map_free(bmap);
5033 return NULL;
5036 /* Given a map "map", compute the lexicographically minimal
5037 * (or maximal) image element for each domain element in dom.
5038 * Set *empty to those elements in dom that do not have an image element.
5040 * We first compute the lexicographically minimal or maximal element
5041 * in the first basic map. This results in a partial solution "res"
5042 * and a subset "todo" of dom that still need to be handled.
5043 * We then consider each of the remaining maps in "map" and successively
5044 * improve both "res" and "todo".
5046 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5047 * Assume we are computing the lexicographical maximum.
5048 * We first compute the lexicographically maximal element in basic map i.
5049 * This results in a partial solution res_i and a subset todo_i.
5050 * Then we combine these results with those obtain for the first k basic maps
5051 * to obtain a result that is valid for the first k+1 basic maps.
5052 * In particular, the set where there is no solution is the set where
5053 * there is no solution for the first k basic maps and also no solution
5054 * for the ith basic map, i.e.,
5056 * todo^i = todo^k * todo_i
5058 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5059 * solutions, arbitrarily breaking ties in favor of res^k.
5060 * That is, when res^k(a) >= res_i(a), we pick res^k and
5061 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5062 * the lexicographic order.)
5063 * In practice, we compute
5065 * res^k * (res_i . "<=")
5067 * and
5069 * res_i * (res^k . "<")
5071 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5072 * where only one of res^k and res_i provides a solution and we simply pick
5073 * that one, i.e.,
5075 * res^k * todo_i
5076 * and
5077 * res_i * todo^k
5079 * Note that we only compute these intersections when dom(res^k) intersects
5080 * dom(res_i). Otherwise, the only effect of these intersections is to
5081 * potentially break up res^k and res_i into smaller pieces.
5082 * We want to avoid such splintering as much as possible.
5083 * In fact, an earlier implementation of this function would look for
5084 * better results in the domain of res^k and for extra results in todo^k,
5085 * but this would always result in a splintering according to todo^k,
5086 * even when the domain of basic map i is disjoint from the domains of
5087 * the previous basic maps.
5089 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5090 __isl_take isl_map *map, __isl_take isl_set *dom,
5091 __isl_give isl_set **empty, int max)
5093 int i;
5094 struct isl_map *res;
5095 struct isl_set *todo;
5097 if (!map || !dom)
5098 goto error;
5100 if (isl_map_plain_is_empty(map)) {
5101 if (empty)
5102 *empty = dom;
5103 else
5104 isl_set_free(dom);
5105 return map;
5108 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5109 isl_set_copy(dom), &todo, max);
5111 for (i = 1; i < map->n; ++i) {
5112 isl_map *lt, *le;
5113 isl_map *res_i;
5114 isl_set *todo_i;
5115 isl_space *dim = isl_space_range(isl_map_get_space(res));
5117 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5118 isl_set_copy(dom), &todo_i, max);
5120 if (max) {
5121 lt = isl_map_lex_lt(isl_space_copy(dim));
5122 le = isl_map_lex_le(dim);
5123 } else {
5124 lt = isl_map_lex_gt(isl_space_copy(dim));
5125 le = isl_map_lex_ge(dim);
5127 lt = isl_map_apply_range(isl_map_copy(res), lt);
5128 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5129 le = isl_map_apply_range(isl_map_copy(res_i), le);
5130 le = isl_map_intersect(le, isl_map_copy(res));
5132 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5133 res = isl_map_intersect_domain(res,
5134 isl_set_copy(todo_i));
5135 res_i = isl_map_intersect_domain(res_i,
5136 isl_set_copy(todo));
5139 res = isl_map_union_disjoint(res, res_i);
5140 res = isl_map_union_disjoint(res, lt);
5141 res = isl_map_union_disjoint(res, le);
5143 todo = isl_set_intersect(todo, todo_i);
5146 isl_set_free(dom);
5147 isl_map_free(map);
5149 if (empty)
5150 *empty = todo;
5151 else
5152 isl_set_free(todo);
5154 return res;
5155 error:
5156 if (empty)
5157 *empty = NULL;
5158 isl_set_free(dom);
5159 isl_map_free(map);
5160 return NULL;
5163 /* Given a map "map", compute the lexicographically minimal
5164 * (or maximal) image element for each domain element in dom.
5165 * Set *empty to those elements in dom that do not have an image element.
5167 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5169 static __isl_give isl_map *isl_map_partial_lexopt(
5170 __isl_take isl_map *map, __isl_take isl_set *dom,
5171 __isl_give isl_set **empty, int max)
5173 if (!map || !dom)
5174 goto error;
5175 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5176 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5177 if (!isl_space_has_named_params(map->dim) ||
5178 !isl_space_has_named_params(dom->dim))
5179 isl_die(map->ctx, isl_error_invalid,
5180 "unaligned unnamed parameters", goto error);
5181 map = isl_map_align_params(map, isl_map_get_space(dom));
5182 dom = isl_map_align_params(dom, isl_map_get_space(map));
5183 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5184 error:
5185 if (empty)
5186 *empty = NULL;
5187 isl_set_free(dom);
5188 isl_map_free(map);
5189 return NULL;
5192 __isl_give isl_map *isl_map_partial_lexmax(
5193 __isl_take isl_map *map, __isl_take isl_set *dom,
5194 __isl_give isl_set **empty)
5196 return isl_map_partial_lexopt(map, dom, empty, 1);
5199 __isl_give isl_map *isl_map_partial_lexmin(
5200 __isl_take isl_map *map, __isl_take isl_set *dom,
5201 __isl_give isl_set **empty)
5203 return isl_map_partial_lexopt(map, dom, empty, 0);
5206 __isl_give isl_set *isl_set_partial_lexmin(
5207 __isl_take isl_set *set, __isl_take isl_set *dom,
5208 __isl_give isl_set **empty)
5210 return (struct isl_set *)
5211 isl_map_partial_lexmin((struct isl_map *)set,
5212 dom, empty);
5215 __isl_give isl_set *isl_set_partial_lexmax(
5216 __isl_take isl_set *set, __isl_take isl_set *dom,
5217 __isl_give isl_set **empty)
5219 return (struct isl_set *)
5220 isl_map_partial_lexmax((struct isl_map *)set,
5221 dom, empty);
5224 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5226 struct isl_basic_set *dom = NULL;
5227 isl_space *dom_dim;
5229 if (!bmap)
5230 goto error;
5231 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5232 dom = isl_basic_set_universe(dom_dim);
5233 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5234 error:
5235 isl_basic_map_free(bmap);
5236 return NULL;
5239 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5241 return isl_basic_map_lexopt(bmap, 0);
5244 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5246 return isl_basic_map_lexopt(bmap, 1);
5249 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5251 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5254 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5256 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5259 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5261 struct isl_set *dom = NULL;
5262 isl_space *dom_dim;
5264 if (!map)
5265 goto error;
5266 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5267 dom = isl_set_universe(dom_dim);
5268 return isl_map_partial_lexopt(map, dom, NULL, max);
5269 error:
5270 isl_map_free(map);
5271 return NULL;
5274 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5276 return isl_map_lexopt(map, 0);
5279 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5281 return isl_map_lexopt(map, 1);
5284 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5286 return (isl_set *)isl_map_lexmin((isl_map *)set);
5289 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5291 return (isl_set *)isl_map_lexmax((isl_map *)set);
5294 /* Construct a map that equates the two given dimensions in the given space.
5296 static __isl_give isl_map *equate(__isl_take isl_space *dim,
5297 enum isl_dim_type src_type, int src_pos,
5298 enum isl_dim_type dst_type, int dst_pos)
5300 isl_basic_map *bmap;
5301 int k;
5303 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5304 k = isl_basic_map_alloc_equality(bmap);
5305 if (k < 0)
5306 goto error;
5307 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
5308 src_pos += isl_basic_map_offset(bmap, src_type);
5309 dst_pos += isl_basic_map_offset(bmap, dst_type);
5310 isl_int_set_si(bmap->eq[k][src_pos], 1);
5311 isl_int_set_si(bmap->eq[k][dst_pos], -1);
5313 return isl_map_from_basic_map(bmap);
5314 error:
5315 isl_basic_map_free(bmap);
5316 return NULL;
5319 /* Extract the first and only affine expression from list
5320 * and then add it to *pwaff with the given dom.
5321 * This domain is known to be disjoint from other domains
5322 * because of the way isl_basic_set_foreach_lexmax works.
5324 static int update_dim_opt(__isl_take isl_basic_set *dom,
5325 __isl_take isl_aff_list *list, void *user)
5327 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5328 isl_aff *aff;
5329 isl_pw_aff **pwaff = user;
5330 isl_pw_aff *pwaff_i;
5332 if (isl_aff_list_n_aff(list) != 1)
5333 isl_die(ctx, isl_error_internal,
5334 "expecting single element list", goto error);
5336 aff = isl_aff_list_get_aff(list, 0);
5337 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5339 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5341 isl_aff_list_free(list);
5343 return 0;
5344 error:
5345 isl_basic_set_free(dom);
5346 isl_aff_list_free(list);
5347 return -1;
5350 /* Given a one-dimensional basic set, compute the minimum or maximum of that
5351 * dimension as an isl_pw_aff.
5353 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexopt
5354 * call update_dim_opt on each leaf of the result.
5356 static __isl_give isl_pw_aff *basic_set_dim_opt(__isl_keep isl_basic_set *bset,
5357 int max)
5359 isl_space *dim = isl_basic_set_get_space(bset);
5360 isl_pw_aff *pwaff;
5361 int r;
5363 dim = isl_space_domain(isl_space_from_range(dim));
5364 pwaff = isl_pw_aff_empty(dim);
5366 r = isl_basic_set_foreach_lexopt(bset, max, &update_dim_opt, &pwaff);
5367 if (r < 0)
5368 return isl_pw_aff_free(pwaff);
5370 return pwaff;
5373 /* Compute the minimum or maximum of the given set dimension
5374 * as a function of the parameters, but independently of
5375 * the other set dimensions.
5377 * We first project the set onto the given dimension and then compute
5378 * the "lexicographic" maximum in each basic set, combining the results
5379 * using isl_pw_aff_union_max.
5381 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5382 int max)
5384 int i;
5385 isl_map *map;
5386 isl_pw_aff *pwaff;
5388 map = isl_map_from_domain(set);
5389 map = isl_map_add_dims(map, isl_dim_out, 1);
5390 map = isl_map_intersect(map,
5391 equate(isl_map_get_space(map), isl_dim_in, pos,
5392 isl_dim_out, 0));
5393 set = isl_map_range(map);
5394 if (!set)
5395 return NULL;
5397 if (set->n == 0) {
5398 isl_space *dim = isl_set_get_space(set);
5399 dim = isl_space_domain(isl_space_from_range(dim));
5400 isl_set_free(set);
5401 return isl_pw_aff_empty(dim);
5404 pwaff = basic_set_dim_opt(set->p[0], max);
5405 for (i = 1; i < set->n; ++i) {
5406 isl_pw_aff *pwaff_i;
5408 pwaff_i = basic_set_dim_opt(set->p[i], max);
5409 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5412 isl_set_free(set);
5414 return pwaff;
5417 /* Compute the maximum of the given set dimension as a function of the
5418 * parameters, but independently of the other set dimensions.
5420 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5422 return set_dim_opt(set, pos, 1);
5425 /* Compute the minimum of the given set dimension as a function of the
5426 * parameters, but independently of the other set dimensions.
5428 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5430 return set_dim_opt(set, pos, 0);
5433 /* Apply a preimage specified by "mat" on the parameters of "bset".
5434 * bset is assumed to have only parameters and divs.
5436 static struct isl_basic_set *basic_set_parameter_preimage(
5437 struct isl_basic_set *bset, struct isl_mat *mat)
5439 unsigned nparam;
5441 if (!bset || !mat)
5442 goto error;
5444 bset->dim = isl_space_cow(bset->dim);
5445 if (!bset->dim)
5446 goto error;
5448 nparam = isl_basic_set_dim(bset, isl_dim_param);
5450 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5452 bset->dim->nparam = 0;
5453 bset->dim->n_out = nparam;
5454 bset = isl_basic_set_preimage(bset, mat);
5455 if (bset) {
5456 bset->dim->nparam = bset->dim->n_out;
5457 bset->dim->n_out = 0;
5459 return bset;
5460 error:
5461 isl_mat_free(mat);
5462 isl_basic_set_free(bset);
5463 return NULL;
5466 /* Apply a preimage specified by "mat" on the parameters of "set".
5467 * set is assumed to have only parameters and divs.
5469 static struct isl_set *set_parameter_preimage(
5470 struct isl_set *set, struct isl_mat *mat)
5472 isl_space *dim = NULL;
5473 unsigned nparam;
5475 if (!set || !mat)
5476 goto error;
5478 dim = isl_space_copy(set->dim);
5479 dim = isl_space_cow(dim);
5480 if (!dim)
5481 goto error;
5483 nparam = isl_set_dim(set, isl_dim_param);
5485 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5487 dim->nparam = 0;
5488 dim->n_out = nparam;
5489 isl_set_reset_space(set, dim);
5490 set = isl_set_preimage(set, mat);
5491 if (!set)
5492 goto error2;
5493 dim = isl_space_copy(set->dim);
5494 dim = isl_space_cow(dim);
5495 if (!dim)
5496 goto error2;
5497 dim->nparam = dim->n_out;
5498 dim->n_out = 0;
5499 isl_set_reset_space(set, dim);
5500 return set;
5501 error:
5502 isl_space_free(dim);
5503 isl_mat_free(mat);
5504 error2:
5505 isl_set_free(set);
5506 return NULL;
5509 /* Intersect the basic set "bset" with the affine space specified by the
5510 * equalities in "eq".
5512 static struct isl_basic_set *basic_set_append_equalities(
5513 struct isl_basic_set *bset, struct isl_mat *eq)
5515 int i, k;
5516 unsigned len;
5518 if (!bset || !eq)
5519 goto error;
5521 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5522 eq->n_row, 0);
5523 if (!bset)
5524 goto error;
5526 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5527 for (i = 0; i < eq->n_row; ++i) {
5528 k = isl_basic_set_alloc_equality(bset);
5529 if (k < 0)
5530 goto error;
5531 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5532 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5534 isl_mat_free(eq);
5536 bset = isl_basic_set_gauss(bset, NULL);
5537 bset = isl_basic_set_finalize(bset);
5539 return bset;
5540 error:
5541 isl_mat_free(eq);
5542 isl_basic_set_free(bset);
5543 return NULL;
5546 /* Intersect the set "set" with the affine space specified by the
5547 * equalities in "eq".
5549 static struct isl_set *set_append_equalities(struct isl_set *set,
5550 struct isl_mat *eq)
5552 int i;
5554 if (!set || !eq)
5555 goto error;
5557 for (i = 0; i < set->n; ++i) {
5558 set->p[i] = basic_set_append_equalities(set->p[i],
5559 isl_mat_copy(eq));
5560 if (!set->p[i])
5561 goto error;
5563 isl_mat_free(eq);
5564 return set;
5565 error:
5566 isl_mat_free(eq);
5567 isl_set_free(set);
5568 return NULL;
5571 /* Project the given basic set onto its parameter domain, possibly introducing
5572 * new, explicit, existential variables in the constraints.
5573 * The input has parameters and (possibly implicit) existential variables.
5574 * The output has the same parameters, but only
5575 * explicit existentially quantified variables.
5577 * The actual projection is performed by pip, but pip doesn't seem
5578 * to like equalities very much, so we first remove the equalities
5579 * among the parameters by performing a variable compression on
5580 * the parameters. Afterward, an inverse transformation is performed
5581 * and the equalities among the parameters are inserted back in.
5583 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5585 int i, j;
5586 struct isl_mat *eq;
5587 struct isl_mat *T, *T2;
5588 struct isl_set *set;
5589 unsigned nparam, n_div;
5591 bset = isl_basic_set_cow(bset);
5592 if (!bset)
5593 return NULL;
5595 if (bset->n_eq == 0)
5596 return isl_basic_set_lexmin(bset);
5598 isl_basic_set_gauss(bset, NULL);
5600 nparam = isl_basic_set_dim(bset, isl_dim_param);
5601 n_div = isl_basic_set_dim(bset, isl_dim_div);
5603 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5604 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5605 ++i;
5607 if (i == bset->n_eq)
5608 return isl_basic_set_lexmin(bset);
5610 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5611 0, 1 + nparam);
5612 eq = isl_mat_cow(eq);
5613 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5614 if (T && T->n_col == 0) {
5615 isl_mat_free(T);
5616 isl_mat_free(T2);
5617 isl_mat_free(eq);
5618 bset = isl_basic_set_set_to_empty(bset);
5619 return isl_set_from_basic_set(bset);
5621 bset = basic_set_parameter_preimage(bset, T);
5623 set = isl_basic_set_lexmin(bset);
5624 set = set_parameter_preimage(set, T2);
5625 set = set_append_equalities(set, eq);
5626 return set;
5629 /* Compute an explicit representation for all the existentially
5630 * quantified variables.
5631 * The input and output dimensions are first turned into parameters.
5632 * compute_divs then returns a map with the same parameters and
5633 * no input or output dimensions and the dimension specification
5634 * is reset to that of the input.
5636 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5638 struct isl_basic_set *bset;
5639 struct isl_set *set;
5640 struct isl_map *map;
5641 isl_space *dim, *orig_dim = NULL;
5642 unsigned nparam;
5643 unsigned n_in;
5644 unsigned n_out;
5646 bmap = isl_basic_map_cow(bmap);
5647 if (!bmap)
5648 return NULL;
5650 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5651 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5652 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5653 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5654 if (!dim)
5655 goto error;
5657 orig_dim = bmap->dim;
5658 bmap->dim = dim;
5659 bset = (struct isl_basic_set *)bmap;
5661 set = parameter_compute_divs(bset);
5662 map = (struct isl_map *)set;
5663 map = isl_map_reset_space(map, orig_dim);
5665 return map;
5666 error:
5667 isl_basic_map_free(bmap);
5668 return NULL;
5671 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5673 int i;
5674 unsigned off;
5676 if (!bmap)
5677 return -1;
5679 off = isl_space_dim(bmap->dim, isl_dim_all);
5680 for (i = 0; i < bmap->n_div; ++i) {
5681 if (isl_int_is_zero(bmap->div[i][0]))
5682 return 0;
5683 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5684 return -1);
5686 return 1;
5689 static int map_divs_known(__isl_keep isl_map *map)
5691 int i;
5693 if (!map)
5694 return -1;
5696 for (i = 0; i < map->n; ++i) {
5697 int known = isl_basic_map_divs_known(map->p[i]);
5698 if (known <= 0)
5699 return known;
5702 return 1;
5705 /* If bmap contains any unknown divs, then compute explicit
5706 * expressions for them. However, this computation may be
5707 * quite expensive, so first try to remove divs that aren't
5708 * strictly needed.
5710 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5712 int known;
5713 struct isl_map *map;
5715 known = isl_basic_map_divs_known(bmap);
5716 if (known < 0)
5717 goto error;
5718 if (known)
5719 return isl_map_from_basic_map(bmap);
5721 bmap = isl_basic_map_drop_redundant_divs(bmap);
5723 known = isl_basic_map_divs_known(bmap);
5724 if (known < 0)
5725 goto error;
5726 if (known)
5727 return isl_map_from_basic_map(bmap);
5729 map = compute_divs(bmap);
5730 return map;
5731 error:
5732 isl_basic_map_free(bmap);
5733 return NULL;
5736 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5738 int i;
5739 int known;
5740 struct isl_map *res;
5742 if (!map)
5743 return NULL;
5744 if (map->n == 0)
5745 return map;
5747 known = map_divs_known(map);
5748 if (known < 0) {
5749 isl_map_free(map);
5750 return NULL;
5752 if (known)
5753 return map;
5755 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5756 for (i = 1 ; i < map->n; ++i) {
5757 struct isl_map *r2;
5758 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5759 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5760 res = isl_map_union_disjoint(res, r2);
5761 else
5762 res = isl_map_union(res, r2);
5764 isl_map_free(map);
5766 return res;
5769 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5771 return (struct isl_set *)
5772 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5775 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5777 return (struct isl_set *)
5778 isl_map_compute_divs((struct isl_map *)set);
5781 struct isl_set *isl_map_domain(struct isl_map *map)
5783 int i;
5784 struct isl_set *set;
5786 if (!map)
5787 goto error;
5789 map = isl_map_cow(map);
5790 if (!map)
5791 return NULL;
5793 set = (struct isl_set *)map;
5794 set->dim = isl_space_domain(set->dim);
5795 if (!set->dim)
5796 goto error;
5797 for (i = 0; i < map->n; ++i) {
5798 set->p[i] = isl_basic_map_domain(map->p[i]);
5799 if (!set->p[i])
5800 goto error;
5802 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5803 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5804 return set;
5805 error:
5806 isl_map_free(map);
5807 return NULL;
5810 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
5811 __isl_take isl_map *map2)
5813 int i;
5814 unsigned flags = 0;
5815 struct isl_map *map = NULL;
5817 if (!map1 || !map2)
5818 goto error;
5820 if (map1->n == 0) {
5821 isl_map_free(map1);
5822 return map2;
5824 if (map2->n == 0) {
5825 isl_map_free(map2);
5826 return map1;
5829 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
5831 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5832 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5833 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5835 map = isl_map_alloc_space(isl_space_copy(map1->dim),
5836 map1->n + map2->n, flags);
5837 if (!map)
5838 goto error;
5839 for (i = 0; i < map1->n; ++i) {
5840 map = isl_map_add_basic_map(map,
5841 isl_basic_map_copy(map1->p[i]));
5842 if (!map)
5843 goto error;
5845 for (i = 0; i < map2->n; ++i) {
5846 map = isl_map_add_basic_map(map,
5847 isl_basic_map_copy(map2->p[i]));
5848 if (!map)
5849 goto error;
5851 isl_map_free(map1);
5852 isl_map_free(map2);
5853 return map;
5854 error:
5855 isl_map_free(map);
5856 isl_map_free(map1);
5857 isl_map_free(map2);
5858 return NULL;
5861 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
5862 __isl_take isl_map *map2)
5864 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
5867 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5869 map1 = isl_map_union_disjoint(map1, map2);
5870 if (!map1)
5871 return NULL;
5872 if (map1->n > 1)
5873 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5874 return map1;
5877 struct isl_set *isl_set_union_disjoint(
5878 struct isl_set *set1, struct isl_set *set2)
5880 return (struct isl_set *)
5881 isl_map_union_disjoint(
5882 (struct isl_map *)set1, (struct isl_map *)set2);
5885 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5887 return (struct isl_set *)
5888 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5891 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
5892 __isl_take isl_set *set)
5894 unsigned flags = 0;
5895 struct isl_map *result;
5896 int i, j;
5898 if (!map || !set)
5899 goto error;
5901 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5902 isl_die(set->ctx, isl_error_invalid,
5903 "parameters don't match", goto error);
5905 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
5906 !isl_map_compatible_range(map, set))
5907 isl_die(set->ctx, isl_error_invalid,
5908 "incompatible spaces", goto error);
5910 if (isl_set_plain_is_universe(set)) {
5911 isl_set_free(set);
5912 return map;
5915 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5916 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5917 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5919 result = isl_map_alloc_space(isl_space_copy(map->dim),
5920 map->n * set->n, flags);
5921 if (!result)
5922 goto error;
5923 for (i = 0; i < map->n; ++i)
5924 for (j = 0; j < set->n; ++j) {
5925 result = isl_map_add_basic_map(result,
5926 isl_basic_map_intersect_range(
5927 isl_basic_map_copy(map->p[i]),
5928 isl_basic_set_copy(set->p[j])));
5929 if (!result)
5930 goto error;
5932 isl_map_free(map);
5933 isl_set_free(set);
5934 return result;
5935 error:
5936 isl_map_free(map);
5937 isl_set_free(set);
5938 return NULL;
5941 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
5942 __isl_take isl_set *set)
5944 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
5947 struct isl_map *isl_map_intersect_domain(
5948 struct isl_map *map, struct isl_set *set)
5950 return isl_map_reverse(
5951 isl_map_intersect_range(isl_map_reverse(map), set));
5954 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
5955 __isl_take isl_map *map2)
5957 if (!map1 || !map2)
5958 goto error;
5959 map1 = isl_map_reverse(map1);
5960 map1 = isl_map_apply_range(map1, map2);
5961 return isl_map_reverse(map1);
5962 error:
5963 isl_map_free(map1);
5964 isl_map_free(map2);
5965 return NULL;
5968 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
5969 __isl_take isl_map *map2)
5971 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
5974 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
5975 __isl_take isl_map *map2)
5977 isl_space *dim_result;
5978 struct isl_map *result;
5979 int i, j;
5981 if (!map1 || !map2)
5982 goto error;
5984 dim_result = isl_space_join(isl_space_copy(map1->dim),
5985 isl_space_copy(map2->dim));
5987 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
5988 if (!result)
5989 goto error;
5990 for (i = 0; i < map1->n; ++i)
5991 for (j = 0; j < map2->n; ++j) {
5992 result = isl_map_add_basic_map(result,
5993 isl_basic_map_apply_range(
5994 isl_basic_map_copy(map1->p[i]),
5995 isl_basic_map_copy(map2->p[j])));
5996 if (!result)
5997 goto error;
5999 isl_map_free(map1);
6000 isl_map_free(map2);
6001 if (result && result->n <= 1)
6002 ISL_F_SET(result, ISL_MAP_DISJOINT);
6003 return result;
6004 error:
6005 isl_map_free(map1);
6006 isl_map_free(map2);
6007 return NULL;
6010 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6011 __isl_take isl_map *map2)
6013 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6017 * returns range - domain
6019 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6021 isl_space *dims, *target_dim;
6022 struct isl_basic_set *bset;
6023 unsigned dim;
6024 unsigned nparam;
6025 int i;
6027 if (!bmap)
6028 goto error;
6029 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6030 bmap->dim, isl_dim_out),
6031 goto error);
6032 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6033 dim = isl_basic_map_n_in(bmap);
6034 nparam = isl_basic_map_n_param(bmap);
6035 bset = isl_basic_set_from_basic_map(bmap);
6036 bset = isl_basic_set_cow(bset);
6037 dims = isl_basic_set_get_space(bset);
6038 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6039 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6040 bset = isl_basic_set_swap_vars(bset, 2*dim);
6041 for (i = 0; i < dim; ++i) {
6042 int j = isl_basic_map_alloc_equality(
6043 (struct isl_basic_map *)bset);
6044 if (j < 0)
6045 goto error;
6046 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6047 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6048 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6049 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6051 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6052 bset = isl_basic_set_reset_space(bset, target_dim);
6053 return bset;
6054 error:
6055 isl_basic_map_free(bmap);
6056 return NULL;
6060 * returns range - domain
6062 struct isl_set *isl_map_deltas(struct isl_map *map)
6064 int i;
6065 isl_space *dim;
6066 struct isl_set *result;
6068 if (!map)
6069 return NULL;
6071 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6072 map->dim, isl_dim_out),
6073 goto error);
6074 dim = isl_map_get_space(map);
6075 dim = isl_space_domain(dim);
6076 result = isl_set_alloc_space(dim, map->n, 0);
6077 if (!result)
6078 goto error;
6079 for (i = 0; i < map->n; ++i)
6080 result = isl_set_add_basic_set(result,
6081 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6082 isl_map_free(map);
6083 return result;
6084 error:
6085 isl_map_free(map);
6086 return NULL;
6090 * returns [domain -> range] -> range - domain
6092 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6093 __isl_take isl_basic_map *bmap)
6095 int i, k;
6096 isl_space *dim;
6097 isl_basic_map *domain;
6098 int nparam, n;
6099 unsigned total;
6101 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6102 isl_die(bmap->ctx, isl_error_invalid,
6103 "domain and range don't match", goto error);
6105 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6106 n = isl_basic_map_dim(bmap, isl_dim_in);
6108 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6109 domain = isl_basic_map_universe(dim);
6111 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6112 bmap = isl_basic_map_apply_range(bmap, domain);
6113 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6115 total = isl_basic_map_total_dim(bmap);
6117 for (i = 0; i < n; ++i) {
6118 k = isl_basic_map_alloc_equality(bmap);
6119 if (k < 0)
6120 goto error;
6121 isl_seq_clr(bmap->eq[k], 1 + total);
6122 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6123 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6124 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6127 bmap = isl_basic_map_gauss(bmap, NULL);
6128 return isl_basic_map_finalize(bmap);
6129 error:
6130 isl_basic_map_free(bmap);
6131 return NULL;
6135 * returns [domain -> range] -> range - domain
6137 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6139 int i;
6140 isl_space *domain_dim;
6142 if (!map)
6143 return NULL;
6145 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6146 isl_die(map->ctx, isl_error_invalid,
6147 "domain and range don't match", goto error);
6149 map = isl_map_cow(map);
6150 if (!map)
6151 return NULL;
6153 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6154 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6155 map->dim = isl_space_join(map->dim, domain_dim);
6156 if (!map->dim)
6157 goto error;
6158 for (i = 0; i < map->n; ++i) {
6159 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6160 if (!map->p[i])
6161 goto error;
6163 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6164 return map;
6165 error:
6166 isl_map_free(map);
6167 return NULL;
6170 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6172 struct isl_basic_map *bmap;
6173 unsigned nparam;
6174 unsigned dim;
6175 int i;
6177 if (!dims)
6178 return NULL;
6180 nparam = dims->nparam;
6181 dim = dims->n_out;
6182 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6183 if (!bmap)
6184 goto error;
6186 for (i = 0; i < dim; ++i) {
6187 int j = isl_basic_map_alloc_equality(bmap);
6188 if (j < 0)
6189 goto error;
6190 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6191 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6192 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6194 return isl_basic_map_finalize(bmap);
6195 error:
6196 isl_basic_map_free(bmap);
6197 return NULL;
6200 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6202 if (!dim)
6203 return NULL;
6204 if (dim->n_in != dim->n_out)
6205 isl_die(dim->ctx, isl_error_invalid,
6206 "number of input and output dimensions needs to be "
6207 "the same", goto error);
6208 return basic_map_identity(dim);
6209 error:
6210 isl_space_free(dim);
6211 return NULL;
6214 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6216 if (!model || !model->dim)
6217 return NULL;
6218 return isl_basic_map_identity(isl_space_copy(model->dim));
6221 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6223 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6226 struct isl_map *isl_map_identity_like(struct isl_map *model)
6228 if (!model || !model->dim)
6229 return NULL;
6230 return isl_map_identity(isl_space_copy(model->dim));
6233 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6235 if (!model || !model->dim)
6236 return NULL;
6237 return isl_map_identity(isl_space_copy(model->dim));
6240 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6242 isl_space *dim = isl_set_get_space(set);
6243 isl_map *id;
6244 id = isl_map_identity(isl_space_map_from_set(dim));
6245 return isl_map_intersect_range(id, set);
6248 /* Construct a basic set with all set dimensions having only non-negative
6249 * values.
6251 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6252 __isl_take isl_space *space)
6254 int i;
6255 unsigned nparam;
6256 unsigned dim;
6257 struct isl_basic_set *bset;
6259 if (!space)
6260 return NULL;
6261 nparam = space->nparam;
6262 dim = space->n_out;
6263 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6264 if (!bset)
6265 return NULL;
6266 for (i = 0; i < dim; ++i) {
6267 int k = isl_basic_set_alloc_inequality(bset);
6268 if (k < 0)
6269 goto error;
6270 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6271 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6273 return bset;
6274 error:
6275 isl_basic_set_free(bset);
6276 return NULL;
6279 /* Construct the half-space x_pos >= 0.
6281 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6282 int pos)
6284 int k;
6285 isl_basic_set *nonneg;
6287 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6288 k = isl_basic_set_alloc_inequality(nonneg);
6289 if (k < 0)
6290 goto error;
6291 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6292 isl_int_set_si(nonneg->ineq[k][pos], 1);
6294 return isl_basic_set_finalize(nonneg);
6295 error:
6296 isl_basic_set_free(nonneg);
6297 return NULL;
6300 /* Construct the half-space x_pos <= -1.
6302 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6304 int k;
6305 isl_basic_set *neg;
6307 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6308 k = isl_basic_set_alloc_inequality(neg);
6309 if (k < 0)
6310 goto error;
6311 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6312 isl_int_set_si(neg->ineq[k][0], -1);
6313 isl_int_set_si(neg->ineq[k][pos], -1);
6315 return isl_basic_set_finalize(neg);
6316 error:
6317 isl_basic_set_free(neg);
6318 return NULL;
6321 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6322 enum isl_dim_type type, unsigned first, unsigned n)
6324 int i;
6325 isl_basic_set *nonneg;
6326 isl_basic_set *neg;
6328 if (!set)
6329 return NULL;
6330 if (n == 0)
6331 return set;
6333 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6335 for (i = 0; i < n; ++i) {
6336 nonneg = nonneg_halfspace(isl_set_get_space(set),
6337 pos(set->dim, type) + first + i);
6338 neg = neg_halfspace(isl_set_get_space(set),
6339 pos(set->dim, type) + first + i);
6341 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6344 return set;
6345 error:
6346 isl_set_free(set);
6347 return NULL;
6350 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6351 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6352 void *user)
6354 isl_set *half;
6356 if (!set)
6357 return -1;
6358 if (isl_set_plain_is_empty(set)) {
6359 isl_set_free(set);
6360 return 0;
6362 if (first == len)
6363 return fn(set, signs, user);
6365 signs[first] = 1;
6366 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6367 1 + first));
6368 half = isl_set_intersect(half, isl_set_copy(set));
6369 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6370 goto error;
6372 signs[first] = -1;
6373 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6374 1 + first));
6375 half = isl_set_intersect(half, set);
6376 return foreach_orthant(half, signs, first + 1, len, fn, user);
6377 error:
6378 isl_set_free(set);
6379 return -1;
6382 /* Call "fn" on the intersections of "set" with each of the orthants
6383 * (except for obviously empty intersections). The orthant is identified
6384 * by the signs array, with each entry having value 1 or -1 according
6385 * to the sign of the corresponding variable.
6387 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6388 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6389 void *user)
6391 unsigned nparam;
6392 unsigned nvar;
6393 int *signs;
6394 int r;
6396 if (!set)
6397 return -1;
6398 if (isl_set_plain_is_empty(set))
6399 return 0;
6401 nparam = isl_set_dim(set, isl_dim_param);
6402 nvar = isl_set_dim(set, isl_dim_set);
6404 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6406 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6407 fn, user);
6409 free(signs);
6411 return r;
6414 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6416 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6419 int isl_basic_map_is_subset(
6420 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6422 int is_subset;
6423 struct isl_map *map1;
6424 struct isl_map *map2;
6426 if (!bmap1 || !bmap2)
6427 return -1;
6429 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6430 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6432 is_subset = isl_map_is_subset(map1, map2);
6434 isl_map_free(map1);
6435 isl_map_free(map2);
6437 return is_subset;
6440 int isl_basic_map_is_equal(
6441 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6443 int is_subset;
6445 if (!bmap1 || !bmap2)
6446 return -1;
6447 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6448 if (is_subset != 1)
6449 return is_subset;
6450 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6451 return is_subset;
6454 int isl_basic_set_is_equal(
6455 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6457 return isl_basic_map_is_equal(
6458 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6461 int isl_map_is_empty(struct isl_map *map)
6463 int i;
6464 int is_empty;
6466 if (!map)
6467 return -1;
6468 for (i = 0; i < map->n; ++i) {
6469 is_empty = isl_basic_map_is_empty(map->p[i]);
6470 if (is_empty < 0)
6471 return -1;
6472 if (!is_empty)
6473 return 0;
6475 return 1;
6478 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6480 return map ? map->n == 0 : -1;
6483 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6485 return isl_map_plain_is_empty(map);
6488 int isl_set_plain_is_empty(struct isl_set *set)
6490 return set ? set->n == 0 : -1;
6493 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6495 return isl_set_plain_is_empty(set);
6498 int isl_set_is_empty(struct isl_set *set)
6500 return isl_map_is_empty((struct isl_map *)set);
6503 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6505 if (!map1 || !map2)
6506 return -1;
6508 return isl_space_is_equal(map1->dim, map2->dim);
6511 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6513 if (!set1 || !set2)
6514 return -1;
6516 return isl_space_is_equal(set1->dim, set2->dim);
6519 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6521 int is_subset;
6523 if (!map1 || !map2)
6524 return -1;
6525 is_subset = isl_map_is_subset(map1, map2);
6526 if (is_subset != 1)
6527 return is_subset;
6528 is_subset = isl_map_is_subset(map2, map1);
6529 return is_subset;
6532 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6534 return align_params_map_map_and_test(map1, map2, &map_is_equal);
6537 int isl_basic_map_is_strict_subset(
6538 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6540 int is_subset;
6542 if (!bmap1 || !bmap2)
6543 return -1;
6544 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6545 if (is_subset != 1)
6546 return is_subset;
6547 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6548 if (is_subset == -1)
6549 return is_subset;
6550 return !is_subset;
6553 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6555 int is_subset;
6557 if (!map1 || !map2)
6558 return -1;
6559 is_subset = isl_map_is_subset(map1, map2);
6560 if (is_subset != 1)
6561 return is_subset;
6562 is_subset = isl_map_is_subset(map2, map1);
6563 if (is_subset == -1)
6564 return is_subset;
6565 return !is_subset;
6568 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6570 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6573 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6575 if (!bmap)
6576 return -1;
6577 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6580 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6582 if (!bset)
6583 return -1;
6584 return bset->n_eq == 0 && bset->n_ineq == 0;
6587 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6589 int i;
6591 if (!map)
6592 return -1;
6594 for (i = 0; i < map->n; ++i) {
6595 int r = isl_basic_map_is_universe(map->p[i]);
6596 if (r < 0 || r)
6597 return r;
6600 return 0;
6603 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6605 return isl_map_plain_is_universe((isl_map *) set);
6608 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6610 return isl_set_plain_is_universe(set);
6613 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6615 struct isl_basic_set *bset = NULL;
6616 struct isl_vec *sample = NULL;
6617 int empty;
6618 unsigned total;
6620 if (!bmap)
6621 return -1;
6623 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6624 return 1;
6626 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6627 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6628 copy = isl_basic_map_remove_redundancies(copy);
6629 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6630 isl_basic_map_free(copy);
6631 return empty;
6634 total = 1 + isl_basic_map_total_dim(bmap);
6635 if (bmap->sample && bmap->sample->size == total) {
6636 int contains = isl_basic_map_contains(bmap, bmap->sample);
6637 if (contains < 0)
6638 return -1;
6639 if (contains)
6640 return 0;
6642 isl_vec_free(bmap->sample);
6643 bmap->sample = NULL;
6644 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6645 if (!bset)
6646 return -1;
6647 sample = isl_basic_set_sample_vec(bset);
6648 if (!sample)
6649 return -1;
6650 empty = sample->size == 0;
6651 isl_vec_free(bmap->sample);
6652 bmap->sample = sample;
6653 if (empty)
6654 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6656 return empty;
6659 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6661 if (!bmap)
6662 return -1;
6663 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6666 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6668 return isl_basic_map_plain_is_empty(bmap);
6671 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6673 if (!bset)
6674 return -1;
6675 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6678 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6680 return isl_basic_set_plain_is_empty(bset);
6683 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6685 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6688 struct isl_map *isl_basic_map_union(
6689 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6691 struct isl_map *map;
6692 if (!bmap1 || !bmap2)
6693 return NULL;
6695 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
6697 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
6698 if (!map)
6699 goto error;
6700 map = isl_map_add_basic_map(map, bmap1);
6701 map = isl_map_add_basic_map(map, bmap2);
6702 return map;
6703 error:
6704 isl_basic_map_free(bmap1);
6705 isl_basic_map_free(bmap2);
6706 return NULL;
6709 struct isl_set *isl_basic_set_union(
6710 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6712 return (struct isl_set *)isl_basic_map_union(
6713 (struct isl_basic_map *)bset1,
6714 (struct isl_basic_map *)bset2);
6717 /* Order divs such that any div only depends on previous divs */
6718 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6720 int i;
6721 unsigned off;
6723 if (!bmap)
6724 return NULL;
6726 off = isl_space_dim(bmap->dim, isl_dim_all);
6728 for (i = 0; i < bmap->n_div; ++i) {
6729 int pos;
6730 if (isl_int_is_zero(bmap->div[i][0]))
6731 continue;
6732 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6733 bmap->n_div-i);
6734 if (pos == -1)
6735 continue;
6736 isl_basic_map_swap_div(bmap, i, i + pos);
6737 --i;
6739 return bmap;
6742 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6744 return (struct isl_basic_set *)
6745 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6748 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6750 int i;
6752 if (!map)
6753 return 0;
6755 for (i = 0; i < map->n; ++i) {
6756 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6757 if (!map->p[i])
6758 goto error;
6761 return map;
6762 error:
6763 isl_map_free(map);
6764 return NULL;
6767 /* Apply the expansion computed by isl_merge_divs.
6768 * The expansion itself is given by "exp" while the resulting
6769 * list of divs is given by "div".
6771 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6772 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6774 int i, j;
6775 int n_div;
6777 bset = isl_basic_set_cow(bset);
6778 if (!bset || !div)
6779 goto error;
6781 if (div->n_row < bset->n_div)
6782 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6783 "not an expansion", goto error);
6785 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
6786 div->n_row - bset->n_div, 0,
6787 2 * (div->n_row - bset->n_div));
6789 n_div = bset->n_div;
6790 for (i = n_div; i < div->n_row; ++i)
6791 if (isl_basic_set_alloc_div(bset) < 0)
6792 goto error;
6794 j = n_div - 1;
6795 for (i = div->n_row - 1; i >= 0; --i) {
6796 if (j >= 0 && exp[j] == i) {
6797 if (i != j)
6798 isl_basic_map_swap_div(bset, i, j);
6799 j--;
6800 } else {
6801 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6802 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6803 goto error;
6807 isl_mat_free(div);
6808 return bset;
6809 error:
6810 isl_basic_set_free(bset);
6811 isl_mat_free(div);
6812 return NULL;
6815 /* Look for a div in dst that corresponds to the div "div" in src.
6816 * The divs before "div" in src and dst are assumed to be the same.
6818 * Returns -1 if no corresponding div was found and the position
6819 * of the corresponding div in dst otherwise.
6821 static int find_div(struct isl_basic_map *dst,
6822 struct isl_basic_map *src, unsigned div)
6824 int i;
6826 unsigned total = isl_space_dim(src->dim, isl_dim_all);
6828 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6829 for (i = div; i < dst->n_div; ++i)
6830 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6831 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6832 dst->n_div - div) == -1)
6833 return i;
6834 return -1;
6837 struct isl_basic_map *isl_basic_map_align_divs(
6838 struct isl_basic_map *dst, struct isl_basic_map *src)
6840 int i;
6841 unsigned total = isl_space_dim(src->dim, isl_dim_all);
6843 if (!dst || !src)
6844 goto error;
6846 if (src->n_div == 0)
6847 return dst;
6849 for (i = 0; i < src->n_div; ++i)
6850 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6852 src = isl_basic_map_order_divs(src);
6853 dst = isl_basic_map_cow(dst);
6854 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
6855 src->n_div, 0, 2 * src->n_div);
6856 if (!dst)
6857 return NULL;
6858 for (i = 0; i < src->n_div; ++i) {
6859 int j = find_div(dst, src, i);
6860 if (j < 0) {
6861 j = isl_basic_map_alloc_div(dst);
6862 if (j < 0)
6863 goto error;
6864 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6865 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6866 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6867 goto error;
6869 if (j != i)
6870 isl_basic_map_swap_div(dst, i, j);
6872 return dst;
6873 error:
6874 isl_basic_map_free(dst);
6875 return NULL;
6878 struct isl_basic_set *isl_basic_set_align_divs(
6879 struct isl_basic_set *dst, struct isl_basic_set *src)
6881 return (struct isl_basic_set *)isl_basic_map_align_divs(
6882 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6885 struct isl_map *isl_map_align_divs(struct isl_map *map)
6887 int i;
6889 if (!map)
6890 return NULL;
6891 if (map->n == 0)
6892 return map;
6893 map = isl_map_compute_divs(map);
6894 map = isl_map_cow(map);
6895 if (!map)
6896 return NULL;
6898 for (i = 1; i < map->n; ++i)
6899 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6900 for (i = 1; i < map->n; ++i)
6901 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6903 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6904 return map;
6907 struct isl_set *isl_set_align_divs(struct isl_set *set)
6909 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6912 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
6913 __isl_take isl_map *map)
6915 if (!set || !map)
6916 goto error;
6917 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6918 map = isl_map_intersect_domain(map, set);
6919 set = isl_map_range(map);
6920 return set;
6921 error:
6922 isl_set_free(set);
6923 isl_map_free(map);
6924 return NULL;
6927 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
6928 __isl_take isl_map *map)
6930 return isl_map_align_params_map_map_and(set, map, &set_apply);
6933 /* There is no need to cow as removing empty parts doesn't change
6934 * the meaning of the set.
6936 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6938 int i;
6940 if (!map)
6941 return NULL;
6943 for (i = map->n-1; i >= 0; --i) {
6944 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6945 continue;
6946 isl_basic_map_free(map->p[i]);
6947 if (i != map->n-1) {
6948 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6949 map->p[i] = map->p[map->n-1];
6951 map->n--;
6954 return map;
6957 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6959 return (struct isl_set *)
6960 isl_map_remove_empty_parts((struct isl_map *)set);
6963 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6965 struct isl_basic_map *bmap;
6966 if (!map || map->n == 0)
6967 return NULL;
6968 bmap = map->p[map->n-1];
6969 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6970 return isl_basic_map_copy(bmap);
6973 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6975 return (struct isl_basic_set *)
6976 isl_map_copy_basic_map((struct isl_map *)set);
6979 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6980 __isl_keep isl_basic_map *bmap)
6982 int i;
6984 if (!map || !bmap)
6985 goto error;
6986 for (i = map->n-1; i >= 0; --i) {
6987 if (map->p[i] != bmap)
6988 continue;
6989 map = isl_map_cow(map);
6990 if (!map)
6991 goto error;
6992 isl_basic_map_free(map->p[i]);
6993 if (i != map->n-1) {
6994 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6995 map->p[i] = map->p[map->n-1];
6997 map->n--;
6998 return map;
7000 return map;
7001 error:
7002 isl_map_free(map);
7003 return NULL;
7006 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7007 struct isl_basic_set *bset)
7009 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7010 (struct isl_basic_map *)bset);
7013 /* Given two basic sets bset1 and bset2, compute the maximal difference
7014 * between the values of dimension pos in bset1 and those in bset2
7015 * for any common value of the parameters and dimensions preceding pos.
7017 static enum isl_lp_result basic_set_maximal_difference_at(
7018 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7019 int pos, isl_int *opt)
7021 isl_space *dims;
7022 struct isl_basic_map *bmap1 = NULL;
7023 struct isl_basic_map *bmap2 = NULL;
7024 struct isl_ctx *ctx;
7025 struct isl_vec *obj;
7026 unsigned total;
7027 unsigned nparam;
7028 unsigned dim1, dim2;
7029 enum isl_lp_result res;
7031 if (!bset1 || !bset2)
7032 return isl_lp_error;
7034 nparam = isl_basic_set_n_param(bset1);
7035 dim1 = isl_basic_set_n_dim(bset1);
7036 dim2 = isl_basic_set_n_dim(bset2);
7037 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7038 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7039 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7040 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7041 if (!bmap1 || !bmap2)
7042 goto error;
7043 bmap1 = isl_basic_map_cow(bmap1);
7044 bmap1 = isl_basic_map_extend(bmap1, nparam,
7045 pos, (dim1 - pos) + (dim2 - pos),
7046 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7047 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7048 if (!bmap1)
7049 goto error;
7050 total = isl_basic_map_total_dim(bmap1);
7051 ctx = bmap1->ctx;
7052 obj = isl_vec_alloc(ctx, 1 + total);
7053 isl_seq_clr(obj->block.data, 1 + total);
7054 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7055 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7056 if (!obj)
7057 goto error;
7058 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7059 opt, NULL, NULL);
7060 isl_basic_map_free(bmap1);
7061 isl_vec_free(obj);
7062 return res;
7063 error:
7064 isl_basic_map_free(bmap1);
7065 isl_basic_map_free(bmap2);
7066 return isl_lp_error;
7069 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7070 * for any common value of the parameters and dimensions preceding pos
7071 * in both basic sets, the values of dimension pos in bset1 are
7072 * smaller or larger than those in bset2.
7074 * Returns
7075 * 1 if bset1 follows bset2
7076 * -1 if bset1 precedes bset2
7077 * 0 if bset1 and bset2 are incomparable
7078 * -2 if some error occurred.
7080 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7081 struct isl_basic_set *bset2, int pos)
7083 isl_int opt;
7084 enum isl_lp_result res;
7085 int cmp;
7087 isl_int_init(opt);
7089 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7091 if (res == isl_lp_empty)
7092 cmp = 0;
7093 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7094 res == isl_lp_unbounded)
7095 cmp = 1;
7096 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7097 cmp = -1;
7098 else
7099 cmp = -2;
7101 isl_int_clear(opt);
7102 return cmp;
7105 /* Given two basic sets bset1 and bset2, check whether
7106 * for any common value of the parameters and dimensions preceding pos
7107 * there is a value of dimension pos in bset1 that is larger
7108 * than a value of the same dimension in bset2.
7110 * Return
7111 * 1 if there exists such a pair
7112 * 0 if there is no such pair, but there is a pair of equal values
7113 * -1 otherwise
7114 * -2 if some error occurred.
7116 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7117 __isl_keep isl_basic_set *bset2, int pos)
7119 isl_int opt;
7120 enum isl_lp_result res;
7121 int cmp;
7123 isl_int_init(opt);
7125 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7127 if (res == isl_lp_empty)
7128 cmp = -1;
7129 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7130 res == isl_lp_unbounded)
7131 cmp = 1;
7132 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7133 cmp = -1;
7134 else if (res == isl_lp_ok)
7135 cmp = 0;
7136 else
7137 cmp = -2;
7139 isl_int_clear(opt);
7140 return cmp;
7143 /* Given two sets set1 and set2, check whether
7144 * for any common value of the parameters and dimensions preceding pos
7145 * there is a value of dimension pos in set1 that is larger
7146 * than a value of the same dimension in set2.
7148 * Return
7149 * 1 if there exists such a pair
7150 * 0 if there is no such pair, but there is a pair of equal values
7151 * -1 otherwise
7152 * -2 if some error occurred.
7154 int isl_set_follows_at(__isl_keep isl_set *set1,
7155 __isl_keep isl_set *set2, int pos)
7157 int i, j;
7158 int follows = -1;
7160 if (!set1 || !set2)
7161 return -2;
7163 for (i = 0; i < set1->n; ++i)
7164 for (j = 0; j < set2->n; ++j) {
7165 int f;
7166 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7167 if (f == 1 || f == -2)
7168 return f;
7169 if (f > follows)
7170 follows = f;
7173 return follows;
7176 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7177 unsigned pos, isl_int *val)
7179 int i;
7180 int d;
7181 unsigned total;
7183 if (!bmap)
7184 return -1;
7185 total = isl_basic_map_total_dim(bmap);
7186 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7187 for (; d+1 > pos; --d)
7188 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7189 break;
7190 if (d != pos)
7191 continue;
7192 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7193 return 0;
7194 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7195 return 0;
7196 if (!isl_int_is_one(bmap->eq[i][1+d]))
7197 return 0;
7198 if (val)
7199 isl_int_neg(*val, bmap->eq[i][0]);
7200 return 1;
7202 return 0;
7205 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7206 unsigned pos, isl_int *val)
7208 int i;
7209 isl_int v;
7210 isl_int tmp;
7211 int fixed;
7213 if (!map)
7214 return -1;
7215 if (map->n == 0)
7216 return 0;
7217 if (map->n == 1)
7218 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7219 isl_int_init(v);
7220 isl_int_init(tmp);
7221 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7222 for (i = 1; fixed == 1 && i < map->n; ++i) {
7223 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7224 if (fixed == 1 && isl_int_ne(tmp, v))
7225 fixed = 0;
7227 if (val)
7228 isl_int_set(*val, v);
7229 isl_int_clear(tmp);
7230 isl_int_clear(v);
7231 return fixed;
7234 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7235 unsigned pos, isl_int *val)
7237 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7238 pos, val);
7241 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7242 isl_int *val)
7244 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7247 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7248 enum isl_dim_type type, unsigned pos, isl_int *val)
7250 if (pos >= isl_basic_map_dim(bmap, type))
7251 return -1;
7252 return isl_basic_map_plain_has_fixed_var(bmap,
7253 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7256 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7257 enum isl_dim_type type, unsigned pos, isl_int *val)
7259 if (pos >= isl_map_dim(map, type))
7260 return -1;
7261 return isl_map_plain_has_fixed_var(map,
7262 map_offset(map, type) - 1 + pos, val);
7265 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7266 enum isl_dim_type type, unsigned pos, isl_int *val)
7268 return isl_map_plain_is_fixed(map, type, pos, val);
7271 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7272 * then return this fixed value in *val.
7274 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7275 unsigned dim, isl_int *val)
7277 return isl_basic_set_plain_has_fixed_var(bset,
7278 isl_basic_set_n_param(bset) + dim, val);
7281 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7282 * then return this fixed value in *val.
7284 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7285 unsigned dim, isl_int *val)
7287 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7290 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7291 unsigned dim, isl_int *val)
7293 return isl_set_plain_dim_is_fixed(set, dim, val);
7296 /* Check if input variable in has fixed value and if so and if val is not NULL,
7297 * then return this fixed value in *val.
7299 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7300 unsigned in, isl_int *val)
7302 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7305 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7306 * and if val is not NULL, then return this lower bound in *val.
7308 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7309 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7311 int i, i_eq = -1, i_ineq = -1;
7312 isl_int *c;
7313 unsigned total;
7314 unsigned nparam;
7316 if (!bset)
7317 return -1;
7318 total = isl_basic_set_total_dim(bset);
7319 nparam = isl_basic_set_n_param(bset);
7320 for (i = 0; i < bset->n_eq; ++i) {
7321 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7322 continue;
7323 if (i_eq != -1)
7324 return 0;
7325 i_eq = i;
7327 for (i = 0; i < bset->n_ineq; ++i) {
7328 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7329 continue;
7330 if (i_eq != -1 || i_ineq != -1)
7331 return 0;
7332 i_ineq = i;
7334 if (i_eq == -1 && i_ineq == -1)
7335 return 0;
7336 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7337 /* The coefficient should always be one due to normalization. */
7338 if (!isl_int_is_one(c[1+nparam+dim]))
7339 return 0;
7340 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7341 return 0;
7342 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7343 total - nparam - dim - 1) != -1)
7344 return 0;
7345 if (val)
7346 isl_int_neg(*val, c[0]);
7347 return 1;
7350 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7351 unsigned dim, isl_int *val)
7353 int i;
7354 isl_int v;
7355 isl_int tmp;
7356 int fixed;
7358 if (!set)
7359 return -1;
7360 if (set->n == 0)
7361 return 0;
7362 if (set->n == 1)
7363 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7364 dim, val);
7365 isl_int_init(v);
7366 isl_int_init(tmp);
7367 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7368 dim, &v);
7369 for (i = 1; fixed == 1 && i < set->n; ++i) {
7370 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7371 dim, &tmp);
7372 if (fixed == 1 && isl_int_ne(tmp, v))
7373 fixed = 0;
7375 if (val)
7376 isl_int_set(*val, v);
7377 isl_int_clear(tmp);
7378 isl_int_clear(v);
7379 return fixed;
7382 struct constraint {
7383 unsigned size;
7384 isl_int *c;
7387 /* uset_gist depends on constraints without existentially quantified
7388 * variables sorting first.
7390 static int qsort_constraint_cmp(const void *p1, const void *p2)
7392 const struct constraint *c1 = (const struct constraint *)p1;
7393 const struct constraint *c2 = (const struct constraint *)p2;
7394 int l1, l2;
7395 unsigned size = isl_min(c1->size, c2->size);
7397 l1 = isl_seq_last_non_zero(c1->c, size);
7398 l2 = isl_seq_last_non_zero(c2->c, size);
7400 if (l1 != l2)
7401 return l1 - l2;
7403 return isl_seq_cmp(c1->c, c2->c, size);
7406 static struct isl_basic_map *isl_basic_map_sort_constraints(
7407 struct isl_basic_map *bmap)
7409 int i;
7410 struct constraint *c;
7411 unsigned total;
7413 if (!bmap)
7414 return NULL;
7415 total = isl_basic_map_total_dim(bmap);
7416 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7417 if (!c)
7418 goto error;
7419 for (i = 0; i < bmap->n_ineq; ++i) {
7420 c[i].size = total;
7421 c[i].c = bmap->ineq[i];
7423 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7424 for (i = 0; i < bmap->n_ineq; ++i)
7425 bmap->ineq[i] = c[i].c;
7426 free(c);
7427 return bmap;
7428 error:
7429 isl_basic_map_free(bmap);
7430 return NULL;
7433 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7434 __isl_take isl_basic_set *bset)
7436 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7437 (struct isl_basic_map *)bset);
7440 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7442 if (!bmap)
7443 return NULL;
7444 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7445 return bmap;
7446 bmap = isl_basic_map_remove_redundancies(bmap);
7447 bmap = isl_basic_map_sort_constraints(bmap);
7448 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7449 return bmap;
7452 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7454 return (struct isl_basic_set *)isl_basic_map_normalize(
7455 (struct isl_basic_map *)bset);
7458 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7459 const __isl_keep isl_basic_map *bmap2)
7461 int i, cmp;
7462 unsigned total;
7464 if (bmap1 == bmap2)
7465 return 0;
7466 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7467 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7468 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7469 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7470 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7471 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7472 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7473 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7474 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7475 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7476 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7477 return 0;
7478 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7479 return 1;
7480 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7481 return -1;
7482 if (bmap1->n_eq != bmap2->n_eq)
7483 return bmap1->n_eq - bmap2->n_eq;
7484 if (bmap1->n_ineq != bmap2->n_ineq)
7485 return bmap1->n_ineq - bmap2->n_ineq;
7486 if (bmap1->n_div != bmap2->n_div)
7487 return bmap1->n_div - bmap2->n_div;
7488 total = isl_basic_map_total_dim(bmap1);
7489 for (i = 0; i < bmap1->n_eq; ++i) {
7490 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7491 if (cmp)
7492 return cmp;
7494 for (i = 0; i < bmap1->n_ineq; ++i) {
7495 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7496 if (cmp)
7497 return cmp;
7499 for (i = 0; i < bmap1->n_div; ++i) {
7500 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7501 if (cmp)
7502 return cmp;
7504 return 0;
7507 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7508 __isl_keep isl_basic_map *bmap2)
7510 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7513 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7514 __isl_keep isl_basic_set *bset2)
7516 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7517 (isl_basic_map *)bset2);
7520 static int qsort_bmap_cmp(const void *p1, const void *p2)
7522 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7523 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7525 return isl_basic_map_plain_cmp(bmap1, bmap2);
7528 /* We normalize in place, but if anything goes wrong we need
7529 * to return NULL, so we need to make sure we don't change the
7530 * meaning of any possible other copies of map.
7532 struct isl_map *isl_map_normalize(struct isl_map *map)
7534 int i, j;
7535 struct isl_basic_map *bmap;
7537 if (!map)
7538 return NULL;
7539 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7540 return map;
7541 for (i = 0; i < map->n; ++i) {
7542 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7543 if (!bmap)
7544 goto error;
7545 isl_basic_map_free(map->p[i]);
7546 map->p[i] = bmap;
7548 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7549 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7550 map = isl_map_remove_empty_parts(map);
7551 if (!map)
7552 return NULL;
7553 for (i = map->n - 1; i >= 1; --i) {
7554 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7555 continue;
7556 isl_basic_map_free(map->p[i-1]);
7557 for (j = i; j < map->n; ++j)
7558 map->p[j-1] = map->p[j];
7559 map->n--;
7561 return map;
7562 error:
7563 isl_map_free(map);
7564 return NULL;
7568 struct isl_set *isl_set_normalize(struct isl_set *set)
7570 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7573 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7575 int i;
7576 int equal;
7578 if (!map1 || !map2)
7579 return -1;
7581 if (map1 == map2)
7582 return 1;
7583 if (!isl_space_is_equal(map1->dim, map2->dim))
7584 return 0;
7586 map1 = isl_map_copy(map1);
7587 map2 = isl_map_copy(map2);
7588 map1 = isl_map_normalize(map1);
7589 map2 = isl_map_normalize(map2);
7590 if (!map1 || !map2)
7591 goto error;
7592 equal = map1->n == map2->n;
7593 for (i = 0; equal && i < map1->n; ++i) {
7594 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7595 if (equal < 0)
7596 goto error;
7598 isl_map_free(map1);
7599 isl_map_free(map2);
7600 return equal;
7601 error:
7602 isl_map_free(map1);
7603 isl_map_free(map2);
7604 return -1;
7607 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7609 return isl_map_plain_is_equal(map1, map2);
7612 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7614 return isl_map_plain_is_equal((struct isl_map *)set1,
7615 (struct isl_map *)set2);
7618 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7620 return isl_set_plain_is_equal(set1, set2);
7623 /* Return an interval that ranges from min to max (inclusive)
7625 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7626 isl_int min, isl_int max)
7628 int k;
7629 struct isl_basic_set *bset = NULL;
7631 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7632 if (!bset)
7633 goto error;
7635 k = isl_basic_set_alloc_inequality(bset);
7636 if (k < 0)
7637 goto error;
7638 isl_int_set_si(bset->ineq[k][1], 1);
7639 isl_int_neg(bset->ineq[k][0], min);
7641 k = isl_basic_set_alloc_inequality(bset);
7642 if (k < 0)
7643 goto error;
7644 isl_int_set_si(bset->ineq[k][1], -1);
7645 isl_int_set(bset->ineq[k][0], max);
7647 return bset;
7648 error:
7649 isl_basic_set_free(bset);
7650 return NULL;
7653 /* Return the Cartesian product of the basic sets in list (in the given order).
7655 __isl_give isl_basic_set *isl_basic_set_list_product(
7656 __isl_take struct isl_basic_set_list *list)
7658 int i;
7659 unsigned dim;
7660 unsigned nparam;
7661 unsigned extra;
7662 unsigned n_eq;
7663 unsigned n_ineq;
7664 struct isl_basic_set *product = NULL;
7666 if (!list)
7667 goto error;
7668 isl_assert(list->ctx, list->n > 0, goto error);
7669 isl_assert(list->ctx, list->p[0], goto error);
7670 nparam = isl_basic_set_n_param(list->p[0]);
7671 dim = isl_basic_set_n_dim(list->p[0]);
7672 extra = list->p[0]->n_div;
7673 n_eq = list->p[0]->n_eq;
7674 n_ineq = list->p[0]->n_ineq;
7675 for (i = 1; i < list->n; ++i) {
7676 isl_assert(list->ctx, list->p[i], goto error);
7677 isl_assert(list->ctx,
7678 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7679 dim += isl_basic_set_n_dim(list->p[i]);
7680 extra += list->p[i]->n_div;
7681 n_eq += list->p[i]->n_eq;
7682 n_ineq += list->p[i]->n_ineq;
7684 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7685 n_eq, n_ineq);
7686 if (!product)
7687 goto error;
7688 dim = 0;
7689 for (i = 0; i < list->n; ++i) {
7690 isl_basic_set_add_constraints(product,
7691 isl_basic_set_copy(list->p[i]), dim);
7692 dim += isl_basic_set_n_dim(list->p[i]);
7694 isl_basic_set_list_free(list);
7695 return product;
7696 error:
7697 isl_basic_set_free(product);
7698 isl_basic_set_list_free(list);
7699 return NULL;
7702 struct isl_basic_map *isl_basic_map_product(
7703 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7705 isl_space *dim_result = NULL;
7706 struct isl_basic_map *bmap;
7707 unsigned in1, in2, out1, out2, nparam, total, pos;
7708 struct isl_dim_map *dim_map1, *dim_map2;
7710 if (!bmap1 || !bmap2)
7711 goto error;
7713 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
7714 bmap2->dim, isl_dim_param), goto error);
7715 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
7716 isl_space_copy(bmap2->dim));
7718 in1 = isl_basic_map_n_in(bmap1);
7719 in2 = isl_basic_map_n_in(bmap2);
7720 out1 = isl_basic_map_n_out(bmap1);
7721 out2 = isl_basic_map_n_out(bmap2);
7722 nparam = isl_basic_map_n_param(bmap1);
7724 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7725 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7726 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7727 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7728 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7729 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7730 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7731 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7732 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7733 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7734 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7736 bmap = isl_basic_map_alloc_space(dim_result,
7737 bmap1->n_div + bmap2->n_div,
7738 bmap1->n_eq + bmap2->n_eq,
7739 bmap1->n_ineq + bmap2->n_ineq);
7740 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7741 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7742 bmap = isl_basic_map_simplify(bmap);
7743 return isl_basic_map_finalize(bmap);
7744 error:
7745 isl_basic_map_free(bmap1);
7746 isl_basic_map_free(bmap2);
7747 return NULL;
7750 __isl_give isl_basic_map *isl_basic_map_flat_product(
7751 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7753 isl_basic_map *prod;
7755 prod = isl_basic_map_product(bmap1, bmap2);
7756 prod = isl_basic_map_flatten(prod);
7757 return prod;
7760 __isl_give isl_basic_set *isl_basic_set_flat_product(
7761 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7763 return isl_basic_map_flat_range_product(bset1, bset2);
7766 __isl_give isl_basic_map *isl_basic_map_domain_product(
7767 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7769 isl_space *space_result = NULL;
7770 isl_basic_map *bmap;
7771 unsigned in1, in2, out, nparam, total, pos;
7772 struct isl_dim_map *dim_map1, *dim_map2;
7774 if (!bmap1 || !bmap2)
7775 goto error;
7777 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
7778 isl_space_copy(bmap2->dim));
7780 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
7781 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
7782 out = isl_basic_map_dim(bmap1, isl_dim_out);
7783 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
7785 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
7786 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7787 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7788 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7789 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7790 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7791 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7792 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7793 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
7794 isl_dim_map_div(dim_map1, bmap1, pos += out);
7795 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7797 bmap = isl_basic_map_alloc_space(space_result,
7798 bmap1->n_div + bmap2->n_div,
7799 bmap1->n_eq + bmap2->n_eq,
7800 bmap1->n_ineq + bmap2->n_ineq);
7801 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7802 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7803 bmap = isl_basic_map_simplify(bmap);
7804 return isl_basic_map_finalize(bmap);
7805 error:
7806 isl_basic_map_free(bmap1);
7807 isl_basic_map_free(bmap2);
7808 return NULL;
7811 __isl_give isl_basic_map *isl_basic_map_range_product(
7812 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7814 isl_space *dim_result = NULL;
7815 isl_basic_map *bmap;
7816 unsigned in, out1, out2, nparam, total, pos;
7817 struct isl_dim_map *dim_map1, *dim_map2;
7819 if (!bmap1 || !bmap2)
7820 goto error;
7822 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
7823 isl_space_copy(bmap2->dim));
7825 in = isl_basic_map_dim(bmap1, isl_dim_in);
7826 out1 = isl_basic_map_n_out(bmap1);
7827 out2 = isl_basic_map_n_out(bmap2);
7828 nparam = isl_basic_map_n_param(bmap1);
7830 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7831 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7832 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7833 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7834 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7835 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7836 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7837 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7838 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7839 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7840 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7842 bmap = isl_basic_map_alloc_space(dim_result,
7843 bmap1->n_div + bmap2->n_div,
7844 bmap1->n_eq + bmap2->n_eq,
7845 bmap1->n_ineq + bmap2->n_ineq);
7846 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7847 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7848 bmap = isl_basic_map_simplify(bmap);
7849 return isl_basic_map_finalize(bmap);
7850 error:
7851 isl_basic_map_free(bmap1);
7852 isl_basic_map_free(bmap2);
7853 return NULL;
7856 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
7857 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7859 isl_basic_map *prod;
7861 prod = isl_basic_map_range_product(bmap1, bmap2);
7862 prod = isl_basic_map_flatten_range(prod);
7863 return prod;
7866 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7867 __isl_take isl_map *map2,
7868 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
7869 __isl_take isl_space *right),
7870 __isl_give isl_basic_map *(*basic_map_product)(
7871 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7873 unsigned flags = 0;
7874 struct isl_map *result;
7875 int i, j;
7877 if (!map1 || !map2)
7878 goto error;
7880 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
7881 map2->dim, isl_dim_param), goto error);
7883 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7884 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7885 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7887 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
7888 isl_space_copy(map2->dim)),
7889 map1->n * map2->n, flags);
7890 if (!result)
7891 goto error;
7892 for (i = 0; i < map1->n; ++i)
7893 for (j = 0; j < map2->n; ++j) {
7894 struct isl_basic_map *part;
7895 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7896 isl_basic_map_copy(map2->p[j]));
7897 if (isl_basic_map_is_empty(part))
7898 isl_basic_map_free(part);
7899 else
7900 result = isl_map_add_basic_map(result, part);
7901 if (!result)
7902 goto error;
7904 isl_map_free(map1);
7905 isl_map_free(map2);
7906 return result;
7907 error:
7908 isl_map_free(map1);
7909 isl_map_free(map2);
7910 return NULL;
7913 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7915 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
7916 __isl_take isl_map *map2)
7918 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
7921 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
7922 __isl_take isl_map *map2)
7924 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
7927 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7929 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7930 __isl_take isl_map *map2)
7932 isl_map *prod;
7934 prod = isl_map_product(map1, map2);
7935 prod = isl_map_flatten(prod);
7936 return prod;
7939 /* Given two set A and B, construct its Cartesian product A x B.
7941 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7943 return isl_map_range_product(set1, set2);
7946 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7947 __isl_take isl_set *set2)
7949 return isl_map_flat_range_product(set1, set2);
7952 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
7954 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
7955 __isl_take isl_map *map2)
7957 return map_product(map1, map2, &isl_space_domain_product,
7958 &isl_basic_map_domain_product);
7961 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7963 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
7964 __isl_take isl_map *map2)
7966 return map_product(map1, map2, &isl_space_range_product,
7967 &isl_basic_map_range_product);
7970 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
7971 __isl_take isl_map *map2)
7973 return isl_map_align_params_map_map_and(map1, map2,
7974 &map_domain_product_aligned);
7977 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7978 __isl_take isl_map *map2)
7980 return isl_map_align_params_map_map_and(map1, map2,
7981 &map_range_product_aligned);
7984 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
7986 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
7987 __isl_take isl_map *map2)
7989 isl_map *prod;
7991 prod = isl_map_domain_product(map1, map2);
7992 prod = isl_map_flatten_domain(prod);
7993 return prod;
7996 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7998 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
7999 __isl_take isl_map *map2)
8001 isl_map *prod;
8003 prod = isl_map_range_product(map1, map2);
8004 prod = isl_map_flatten_range(prod);
8005 return prod;
8008 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8010 int i;
8011 uint32_t hash = isl_hash_init();
8012 unsigned total;
8014 if (!bmap)
8015 return 0;
8016 bmap = isl_basic_map_copy(bmap);
8017 bmap = isl_basic_map_normalize(bmap);
8018 if (!bmap)
8019 return 0;
8020 total = isl_basic_map_total_dim(bmap);
8021 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8022 for (i = 0; i < bmap->n_eq; ++i) {
8023 uint32_t c_hash;
8024 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8025 isl_hash_hash(hash, c_hash);
8027 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8028 for (i = 0; i < bmap->n_ineq; ++i) {
8029 uint32_t c_hash;
8030 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8031 isl_hash_hash(hash, c_hash);
8033 isl_hash_byte(hash, bmap->n_div & 0xFF);
8034 for (i = 0; i < bmap->n_div; ++i) {
8035 uint32_t c_hash;
8036 if (isl_int_is_zero(bmap->div[i][0]))
8037 continue;
8038 isl_hash_byte(hash, i & 0xFF);
8039 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8040 isl_hash_hash(hash, c_hash);
8042 isl_basic_map_free(bmap);
8043 return hash;
8046 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8048 return isl_basic_map_get_hash((isl_basic_map *)bset);
8051 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8053 int i;
8054 uint32_t hash;
8056 if (!map)
8057 return 0;
8058 map = isl_map_copy(map);
8059 map = isl_map_normalize(map);
8060 if (!map)
8061 return 0;
8063 hash = isl_hash_init();
8064 for (i = 0; i < map->n; ++i) {
8065 uint32_t bmap_hash;
8066 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8067 isl_hash_hash(hash, bmap_hash);
8070 isl_map_free(map);
8072 return hash;
8075 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8077 return isl_map_get_hash((isl_map *)set);
8080 /* Check if the value for dimension dim is completely determined
8081 * by the values of the other parameters and variables.
8082 * That is, check if dimension dim is involved in an equality.
8084 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8086 int i;
8087 unsigned nparam;
8089 if (!bset)
8090 return -1;
8091 nparam = isl_basic_set_n_param(bset);
8092 for (i = 0; i < bset->n_eq; ++i)
8093 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8094 return 1;
8095 return 0;
8098 /* Check if the value for dimension dim is completely determined
8099 * by the values of the other parameters and variables.
8100 * That is, check if dimension dim is involved in an equality
8101 * for each of the subsets.
8103 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8105 int i;
8107 if (!set)
8108 return -1;
8109 for (i = 0; i < set->n; ++i) {
8110 int unique;
8111 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8112 if (unique != 1)
8113 return unique;
8115 return 1;
8118 int isl_set_n_basic_set(__isl_keep isl_set *set)
8120 return set ? set->n : 0;
8123 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8124 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8126 int i;
8128 if (!map)
8129 return -1;
8131 for (i = 0; i < map->n; ++i)
8132 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8133 return -1;
8135 return 0;
8138 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8139 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8141 int i;
8143 if (!set)
8144 return -1;
8146 for (i = 0; i < set->n; ++i)
8147 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8148 return -1;
8150 return 0;
8153 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8155 isl_space *dim;
8157 if (!bset)
8158 return NULL;
8160 bset = isl_basic_set_cow(bset);
8161 if (!bset)
8162 return NULL;
8164 dim = isl_basic_set_get_space(bset);
8165 dim = isl_space_lift(dim, bset->n_div);
8166 if (!dim)
8167 goto error;
8168 isl_space_free(bset->dim);
8169 bset->dim = dim;
8170 bset->extra -= bset->n_div;
8171 bset->n_div = 0;
8173 bset = isl_basic_set_finalize(bset);
8175 return bset;
8176 error:
8177 isl_basic_set_free(bset);
8178 return NULL;
8181 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8183 int i;
8184 isl_space *dim;
8185 unsigned n_div;
8187 set = isl_set_align_divs(set);
8189 if (!set)
8190 return NULL;
8192 set = isl_set_cow(set);
8193 if (!set)
8194 return NULL;
8196 n_div = set->p[0]->n_div;
8197 dim = isl_set_get_space(set);
8198 dim = isl_space_lift(dim, n_div);
8199 if (!dim)
8200 goto error;
8201 isl_space_free(set->dim);
8202 set->dim = dim;
8204 for (i = 0; i < set->n; ++i) {
8205 set->p[i] = isl_basic_set_lift(set->p[i]);
8206 if (!set->p[i])
8207 goto error;
8210 return set;
8211 error:
8212 isl_set_free(set);
8213 return NULL;
8216 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8218 isl_space *dim;
8219 struct isl_basic_map *bmap;
8220 unsigned n_set;
8221 unsigned n_div;
8222 unsigned n_param;
8223 unsigned total;
8224 int i, k, l;
8226 set = isl_set_align_divs(set);
8228 if (!set)
8229 return NULL;
8231 dim = isl_set_get_space(set);
8232 if (set->n == 0 || set->p[0]->n_div == 0) {
8233 isl_set_free(set);
8234 return isl_map_identity(isl_space_map_from_set(dim));
8237 n_div = set->p[0]->n_div;
8238 dim = isl_space_map_from_set(dim);
8239 n_param = isl_space_dim(dim, isl_dim_param);
8240 n_set = isl_space_dim(dim, isl_dim_in);
8241 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8242 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8243 for (i = 0; i < n_set; ++i)
8244 bmap = var_equal(bmap, i);
8246 total = n_param + n_set + n_set + n_div;
8247 for (i = 0; i < n_div; ++i) {
8248 k = isl_basic_map_alloc_inequality(bmap);
8249 if (k < 0)
8250 goto error;
8251 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8252 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8253 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8254 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8255 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8256 set->p[0]->div[i][0]);
8258 l = isl_basic_map_alloc_inequality(bmap);
8259 if (l < 0)
8260 goto error;
8261 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8262 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8263 set->p[0]->div[i][0]);
8264 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8267 isl_set_free(set);
8268 bmap = isl_basic_map_simplify(bmap);
8269 bmap = isl_basic_map_finalize(bmap);
8270 return isl_map_from_basic_map(bmap);
8271 error:
8272 isl_set_free(set);
8273 isl_basic_map_free(bmap);
8274 return NULL;
8277 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8279 unsigned dim;
8280 int size = 0;
8282 if (!bset)
8283 return -1;
8285 dim = isl_basic_set_total_dim(bset);
8286 size += bset->n_eq * (1 + dim);
8287 size += bset->n_ineq * (1 + dim);
8288 size += bset->n_div * (2 + dim);
8290 return size;
8293 int isl_set_size(__isl_keep isl_set *set)
8295 int i;
8296 int size = 0;
8298 if (!set)
8299 return -1;
8301 for (i = 0; i < set->n; ++i)
8302 size += isl_basic_set_size(set->p[i]);
8304 return size;
8307 /* Check if there is any lower bound (if lower == 0) and/or upper
8308 * bound (if upper == 0) on the specified dim.
8310 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8311 enum isl_dim_type type, unsigned pos, int lower, int upper)
8313 int i;
8315 if (!bmap)
8316 return -1;
8318 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8320 pos += isl_basic_map_offset(bmap, type);
8322 for (i = 0; i < bmap->n_div; ++i) {
8323 if (isl_int_is_zero(bmap->div[i][0]))
8324 continue;
8325 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8326 return 1;
8329 for (i = 0; i < bmap->n_eq; ++i)
8330 if (!isl_int_is_zero(bmap->eq[i][pos]))
8331 return 1;
8333 for (i = 0; i < bmap->n_ineq; ++i) {
8334 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8335 if (sgn > 0)
8336 lower = 1;
8337 if (sgn < 0)
8338 upper = 1;
8341 return lower && upper;
8344 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8345 enum isl_dim_type type, unsigned pos)
8347 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8350 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8351 enum isl_dim_type type, unsigned pos)
8353 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8356 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8357 enum isl_dim_type type, unsigned pos)
8359 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8362 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8363 enum isl_dim_type type, unsigned pos)
8365 int i;
8367 if (!map)
8368 return -1;
8370 for (i = 0; i < map->n; ++i) {
8371 int bounded;
8372 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8373 if (bounded < 0 || !bounded)
8374 return bounded;
8377 return 1;
8380 /* Return 1 if the specified dim is involved in both an upper bound
8381 * and a lower bound.
8383 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8384 enum isl_dim_type type, unsigned pos)
8386 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8389 static int has_bound(__isl_keep isl_map *map,
8390 enum isl_dim_type type, unsigned pos,
8391 int (*fn)(__isl_keep isl_basic_map *bmap,
8392 enum isl_dim_type type, unsigned pos))
8394 int i;
8396 if (!map)
8397 return -1;
8399 for (i = 0; i < map->n; ++i) {
8400 int bounded;
8401 bounded = fn(map->p[i], type, pos);
8402 if (bounded < 0 || bounded)
8403 return bounded;
8406 return 0;
8409 /* Return 1 if the specified dim is involved in any lower bound.
8411 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8412 enum isl_dim_type type, unsigned pos)
8414 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8417 /* Return 1 if the specified dim is involved in any upper bound.
8419 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8420 enum isl_dim_type type, unsigned pos)
8422 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8425 /* For each of the "n" variables starting at "first", determine
8426 * the sign of the variable and put the results in the first "n"
8427 * elements of the array "signs".
8428 * Sign
8429 * 1 means that the variable is non-negative
8430 * -1 means that the variable is non-positive
8431 * 0 means the variable attains both positive and negative values.
8433 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8434 unsigned first, unsigned n, int *signs)
8436 isl_vec *bound = NULL;
8437 struct isl_tab *tab = NULL;
8438 struct isl_tab_undo *snap;
8439 int i;
8441 if (!bset || !signs)
8442 return -1;
8444 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8445 tab = isl_tab_from_basic_set(bset);
8446 if (!bound || !tab)
8447 goto error;
8449 isl_seq_clr(bound->el, bound->size);
8450 isl_int_set_si(bound->el[0], -1);
8452 snap = isl_tab_snap(tab);
8453 for (i = 0; i < n; ++i) {
8454 int empty;
8456 isl_int_set_si(bound->el[1 + first + i], -1);
8457 if (isl_tab_add_ineq(tab, bound->el) < 0)
8458 goto error;
8459 empty = tab->empty;
8460 isl_int_set_si(bound->el[1 + first + i], 0);
8461 if (isl_tab_rollback(tab, snap) < 0)
8462 goto error;
8464 if (empty) {
8465 signs[i] = 1;
8466 continue;
8469 isl_int_set_si(bound->el[1 + first + i], 1);
8470 if (isl_tab_add_ineq(tab, bound->el) < 0)
8471 goto error;
8472 empty = tab->empty;
8473 isl_int_set_si(bound->el[1 + first + i], 0);
8474 if (isl_tab_rollback(tab, snap) < 0)
8475 goto error;
8477 signs[i] = empty ? -1 : 0;
8480 isl_tab_free(tab);
8481 isl_vec_free(bound);
8482 return 0;
8483 error:
8484 isl_tab_free(tab);
8485 isl_vec_free(bound);
8486 return -1;
8489 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8490 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8492 if (!bset || !signs)
8493 return -1;
8494 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8495 return -1);
8497 first += pos(bset->dim, type) - 1;
8498 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8501 /* Check if the given basic map is obviously single-valued.
8502 * In particular, for each output dimension, check that there is
8503 * an equality that defines the output dimension in terms of
8504 * earlier dimensions.
8506 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8508 int i, j;
8509 unsigned total;
8510 unsigned n_out;
8511 unsigned o_out;
8513 if (!bmap)
8514 return -1;
8516 total = 1 + isl_basic_map_total_dim(bmap);
8517 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8518 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8520 for (i = 0; i < n_out; ++i) {
8521 for (j = 0; j < bmap->n_eq; ++j) {
8522 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8523 continue;
8524 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8525 total - (o_out + i + 1)) == -1)
8526 break;
8528 if (j >= bmap->n_eq)
8529 return 0;
8532 return 1;
8535 /* Check if the given map is obviously single-valued.
8537 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8539 if (!map)
8540 return -1;
8541 if (map->n == 0)
8542 return 1;
8543 if (map->n >= 2)
8544 return 0;
8546 return isl_basic_map_plain_is_single_valued(map->p[0]);
8549 /* Check if the given map is single-valued.
8550 * We simply compute
8552 * M \circ M^-1
8554 * and check if the result is a subset of the identity mapping.
8556 int isl_map_is_single_valued(__isl_keep isl_map *map)
8558 isl_space *dim;
8559 isl_map *test;
8560 isl_map *id;
8561 int sv;
8563 sv = isl_map_plain_is_single_valued(map);
8564 if (sv < 0 || sv)
8565 return sv;
8567 test = isl_map_reverse(isl_map_copy(map));
8568 test = isl_map_apply_range(test, isl_map_copy(map));
8570 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8571 id = isl_map_identity(dim);
8573 sv = isl_map_is_subset(test, id);
8575 isl_map_free(test);
8576 isl_map_free(id);
8578 return sv;
8581 int isl_map_is_injective(__isl_keep isl_map *map)
8583 int in;
8585 map = isl_map_copy(map);
8586 map = isl_map_reverse(map);
8587 in = isl_map_is_single_valued(map);
8588 isl_map_free(map);
8590 return in;
8593 /* Check if the given map is obviously injective.
8595 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8597 int in;
8599 map = isl_map_copy(map);
8600 map = isl_map_reverse(map);
8601 in = isl_map_plain_is_single_valued(map);
8602 isl_map_free(map);
8604 return in;
8607 int isl_map_is_bijective(__isl_keep isl_map *map)
8609 int sv;
8611 sv = isl_map_is_single_valued(map);
8612 if (sv < 0 || !sv)
8613 return sv;
8615 return isl_map_is_injective(map);
8618 int isl_set_is_singleton(__isl_keep isl_set *set)
8620 return isl_map_is_single_valued((isl_map *)set);
8623 int isl_map_is_translation(__isl_keep isl_map *map)
8625 int ok;
8626 isl_set *delta;
8628 delta = isl_map_deltas(isl_map_copy(map));
8629 ok = isl_set_is_singleton(delta);
8630 isl_set_free(delta);
8632 return ok;
8635 static int unique(isl_int *p, unsigned pos, unsigned len)
8637 if (isl_seq_first_non_zero(p, pos) != -1)
8638 return 0;
8639 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8640 return 0;
8641 return 1;
8644 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8646 int i, j;
8647 unsigned nvar;
8648 unsigned ovar;
8650 if (!bset)
8651 return -1;
8653 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8654 return 0;
8656 nvar = isl_basic_set_dim(bset, isl_dim_set);
8657 ovar = isl_space_offset(bset->dim, isl_dim_set);
8658 for (j = 0; j < nvar; ++j) {
8659 int lower = 0, upper = 0;
8660 for (i = 0; i < bset->n_eq; ++i) {
8661 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8662 continue;
8663 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8664 return 0;
8665 break;
8667 if (i < bset->n_eq)
8668 continue;
8669 for (i = 0; i < bset->n_ineq; ++i) {
8670 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8671 continue;
8672 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8673 return 0;
8674 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8675 lower = 1;
8676 else
8677 upper = 1;
8679 if (!lower || !upper)
8680 return 0;
8683 return 1;
8686 int isl_set_is_box(__isl_keep isl_set *set)
8688 if (!set)
8689 return -1;
8690 if (set->n != 1)
8691 return 0;
8693 return isl_basic_set_is_box(set->p[0]);
8696 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8698 if (!bset)
8699 return -1;
8701 return isl_space_is_wrapping(bset->dim);
8704 int isl_set_is_wrapping(__isl_keep isl_set *set)
8706 if (!set)
8707 return -1;
8709 return isl_space_is_wrapping(set->dim);
8712 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8714 bmap = isl_basic_map_cow(bmap);
8715 if (!bmap)
8716 return NULL;
8718 bmap->dim = isl_space_wrap(bmap->dim);
8719 if (!bmap->dim)
8720 goto error;
8722 bmap = isl_basic_map_finalize(bmap);
8724 return (isl_basic_set *)bmap;
8725 error:
8726 isl_basic_map_free(bmap);
8727 return NULL;
8730 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8732 int i;
8734 map = isl_map_cow(map);
8735 if (!map)
8736 return NULL;
8738 for (i = 0; i < map->n; ++i) {
8739 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8740 if (!map->p[i])
8741 goto error;
8743 map->dim = isl_space_wrap(map->dim);
8744 if (!map->dim)
8745 goto error;
8747 return (isl_set *)map;
8748 error:
8749 isl_map_free(map);
8750 return NULL;
8753 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8755 bset = isl_basic_set_cow(bset);
8756 if (!bset)
8757 return NULL;
8759 bset->dim = isl_space_unwrap(bset->dim);
8760 if (!bset->dim)
8761 goto error;
8763 bset = isl_basic_set_finalize(bset);
8765 return (isl_basic_map *)bset;
8766 error:
8767 isl_basic_set_free(bset);
8768 return NULL;
8771 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8773 int i;
8775 if (!set)
8776 return NULL;
8778 if (!isl_set_is_wrapping(set))
8779 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8780 goto error);
8782 set = isl_set_cow(set);
8783 if (!set)
8784 return NULL;
8786 for (i = 0; i < set->n; ++i) {
8787 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8788 if (!set->p[i])
8789 goto error;
8792 set->dim = isl_space_unwrap(set->dim);
8793 if (!set->dim)
8794 goto error;
8796 return (isl_map *)set;
8797 error:
8798 isl_set_free(set);
8799 return NULL;
8802 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8803 enum isl_dim_type type)
8805 if (!bmap)
8806 return NULL;
8808 if (!isl_space_is_named_or_nested(bmap->dim, type))
8809 return bmap;
8811 bmap = isl_basic_map_cow(bmap);
8812 if (!bmap)
8813 return NULL;
8815 bmap->dim = isl_space_reset(bmap->dim, type);
8816 if (!bmap->dim)
8817 goto error;
8819 bmap = isl_basic_map_finalize(bmap);
8821 return bmap;
8822 error:
8823 isl_basic_map_free(bmap);
8824 return NULL;
8827 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8828 enum isl_dim_type type)
8830 int i;
8832 if (!map)
8833 return NULL;
8835 if (!isl_space_is_named_or_nested(map->dim, type))
8836 return map;
8838 map = isl_map_cow(map);
8839 if (!map)
8840 return NULL;
8842 for (i = 0; i < map->n; ++i) {
8843 map->p[i] = isl_basic_map_reset(map->p[i], type);
8844 if (!map->p[i])
8845 goto error;
8847 map->dim = isl_space_reset(map->dim, type);
8848 if (!map->dim)
8849 goto error;
8851 return map;
8852 error:
8853 isl_map_free(map);
8854 return NULL;
8857 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8859 if (!bmap)
8860 return NULL;
8862 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8863 return bmap;
8865 bmap = isl_basic_map_cow(bmap);
8866 if (!bmap)
8867 return NULL;
8869 bmap->dim = isl_space_flatten(bmap->dim);
8870 if (!bmap->dim)
8871 goto error;
8873 bmap = isl_basic_map_finalize(bmap);
8875 return bmap;
8876 error:
8877 isl_basic_map_free(bmap);
8878 return NULL;
8881 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8883 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8886 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
8887 __isl_take isl_basic_map *bmap)
8889 if (!bmap)
8890 return NULL;
8892 if (!bmap->dim->nested[0])
8893 return bmap;
8895 bmap = isl_basic_map_cow(bmap);
8896 if (!bmap)
8897 return NULL;
8899 bmap->dim = isl_space_flatten_domain(bmap->dim);
8900 if (!bmap->dim)
8901 goto error;
8903 bmap = isl_basic_map_finalize(bmap);
8905 return bmap;
8906 error:
8907 isl_basic_map_free(bmap);
8908 return NULL;
8911 __isl_give isl_basic_map *isl_basic_map_flatten_range(
8912 __isl_take isl_basic_map *bmap)
8914 if (!bmap)
8915 return NULL;
8917 if (!bmap->dim->nested[1])
8918 return bmap;
8920 bmap = isl_basic_map_cow(bmap);
8921 if (!bmap)
8922 return NULL;
8924 bmap->dim = isl_space_flatten_range(bmap->dim);
8925 if (!bmap->dim)
8926 goto error;
8928 bmap = isl_basic_map_finalize(bmap);
8930 return bmap;
8931 error:
8932 isl_basic_map_free(bmap);
8933 return NULL;
8936 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8938 int i;
8940 if (!map)
8941 return NULL;
8943 if (!map->dim->nested[0] && !map->dim->nested[1])
8944 return map;
8946 map = isl_map_cow(map);
8947 if (!map)
8948 return NULL;
8950 for (i = 0; i < map->n; ++i) {
8951 map->p[i] = isl_basic_map_flatten(map->p[i]);
8952 if (!map->p[i])
8953 goto error;
8955 map->dim = isl_space_flatten(map->dim);
8956 if (!map->dim)
8957 goto error;
8959 return map;
8960 error:
8961 isl_map_free(map);
8962 return NULL;
8965 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8967 return (isl_set *)isl_map_flatten((isl_map *)set);
8970 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8972 isl_space *dim, *flat_dim;
8973 isl_map *map;
8975 dim = isl_set_get_space(set);
8976 flat_dim = isl_space_flatten(isl_space_copy(dim));
8977 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
8978 map = isl_map_intersect_domain(map, set);
8980 return map;
8983 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
8985 int i;
8987 if (!map)
8988 return NULL;
8990 if (!map->dim->nested[0])
8991 return map;
8993 map = isl_map_cow(map);
8994 if (!map)
8995 return NULL;
8997 for (i = 0; i < map->n; ++i) {
8998 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
8999 if (!map->p[i])
9000 goto error;
9002 map->dim = isl_space_flatten_domain(map->dim);
9003 if (!map->dim)
9004 goto error;
9006 return map;
9007 error:
9008 isl_map_free(map);
9009 return NULL;
9012 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9014 int i;
9016 if (!map)
9017 return NULL;
9019 if (!map->dim->nested[1])
9020 return map;
9022 map = isl_map_cow(map);
9023 if (!map)
9024 return NULL;
9026 for (i = 0; i < map->n; ++i) {
9027 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9028 if (!map->p[i])
9029 goto error;
9031 map->dim = isl_space_flatten_range(map->dim);
9032 if (!map->dim)
9033 goto error;
9035 return map;
9036 error:
9037 isl_map_free(map);
9038 return NULL;
9041 /* Reorder the dimensions of "bmap" according to the given dim_map
9042 * and set the dimension specification to "dim".
9044 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9045 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9047 isl_basic_map *res;
9049 bmap = isl_basic_map_cow(bmap);
9050 if (!bmap || !dim || !dim_map)
9051 goto error;
9053 res = isl_basic_map_alloc_space(dim,
9054 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9055 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9056 res = isl_basic_map_finalize(res);
9057 return res;
9058 error:
9059 free(dim_map);
9060 isl_basic_map_free(bmap);
9061 isl_space_free(dim);
9062 return NULL;
9065 /* Reorder the dimensions of "map" according to given reordering.
9067 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9068 __isl_take isl_reordering *r)
9070 int i;
9071 struct isl_dim_map *dim_map;
9073 map = isl_map_cow(map);
9074 dim_map = isl_dim_map_from_reordering(r);
9075 if (!map || !r || !dim_map)
9076 goto error;
9078 for (i = 0; i < map->n; ++i) {
9079 struct isl_dim_map *dim_map_i;
9081 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9083 map->p[i] = isl_basic_map_realign(map->p[i],
9084 isl_space_copy(r->dim), dim_map_i);
9086 if (!map->p[i])
9087 goto error;
9090 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9092 isl_reordering_free(r);
9093 free(dim_map);
9094 return map;
9095 error:
9096 free(dim_map);
9097 isl_map_free(map);
9098 isl_reordering_free(r);
9099 return NULL;
9102 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9103 __isl_take isl_reordering *r)
9105 return (isl_set *)isl_map_realign((isl_map *)set, r);
9108 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9109 __isl_take isl_space *model)
9111 isl_ctx *ctx;
9113 if (!map || !model)
9114 goto error;
9116 ctx = isl_space_get_ctx(model);
9117 if (!isl_space_has_named_params(model))
9118 isl_die(ctx, isl_error_invalid,
9119 "model has unnamed parameters", goto error);
9120 if (!isl_space_has_named_params(map->dim))
9121 isl_die(ctx, isl_error_invalid,
9122 "relation has unnamed parameters", goto error);
9123 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9124 isl_reordering *exp;
9126 model = isl_space_drop_dims(model, isl_dim_in,
9127 0, isl_space_dim(model, isl_dim_in));
9128 model = isl_space_drop_dims(model, isl_dim_out,
9129 0, isl_space_dim(model, isl_dim_out));
9130 exp = isl_parameter_alignment_reordering(map->dim, model);
9131 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9132 map = isl_map_realign(map, exp);
9135 isl_space_free(model);
9136 return map;
9137 error:
9138 isl_space_free(model);
9139 isl_map_free(map);
9140 return NULL;
9143 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9144 __isl_take isl_space *model)
9146 return isl_map_align_params(set, model);
9149 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9150 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9151 enum isl_dim_type c2, enum isl_dim_type c3,
9152 enum isl_dim_type c4, enum isl_dim_type c5)
9154 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9155 struct isl_mat *mat;
9156 int i, j, k;
9157 int pos;
9159 if (!bmap)
9160 return NULL;
9161 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9162 isl_basic_map_total_dim(bmap) + 1);
9163 if (!mat)
9164 return NULL;
9165 for (i = 0; i < bmap->n_eq; ++i)
9166 for (j = 0, pos = 0; j < 5; ++j) {
9167 int off = isl_basic_map_offset(bmap, c[j]);
9168 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9169 isl_int_set(mat->row[i][pos],
9170 bmap->eq[i][off + k]);
9171 ++pos;
9175 return mat;
9178 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9179 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9180 enum isl_dim_type c2, enum isl_dim_type c3,
9181 enum isl_dim_type c4, enum isl_dim_type c5)
9183 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9184 struct isl_mat *mat;
9185 int i, j, k;
9186 int pos;
9188 if (!bmap)
9189 return NULL;
9190 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9191 isl_basic_map_total_dim(bmap) + 1);
9192 if (!mat)
9193 return NULL;
9194 for (i = 0; i < bmap->n_ineq; ++i)
9195 for (j = 0, pos = 0; j < 5; ++j) {
9196 int off = isl_basic_map_offset(bmap, c[j]);
9197 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9198 isl_int_set(mat->row[i][pos],
9199 bmap->ineq[i][off + k]);
9200 ++pos;
9204 return mat;
9207 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9208 __isl_take isl_space *dim,
9209 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9210 enum isl_dim_type c2, enum isl_dim_type c3,
9211 enum isl_dim_type c4, enum isl_dim_type c5)
9213 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9214 isl_basic_map *bmap;
9215 unsigned total;
9216 unsigned extra;
9217 int i, j, k, l;
9218 int pos;
9220 if (!dim || !eq || !ineq)
9221 goto error;
9223 if (eq->n_col != ineq->n_col)
9224 isl_die(dim->ctx, isl_error_invalid,
9225 "equalities and inequalities matrices should have "
9226 "same number of columns", goto error);
9228 total = 1 + isl_space_dim(dim, isl_dim_all);
9230 if (eq->n_col < total)
9231 isl_die(dim->ctx, isl_error_invalid,
9232 "number of columns too small", goto error);
9234 extra = eq->n_col - total;
9236 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9237 eq->n_row, ineq->n_row);
9238 if (!bmap)
9239 goto error;
9240 for (i = 0; i < extra; ++i) {
9241 k = isl_basic_map_alloc_div(bmap);
9242 if (k < 0)
9243 goto error;
9244 isl_int_set_si(bmap->div[k][0], 0);
9246 for (i = 0; i < eq->n_row; ++i) {
9247 l = isl_basic_map_alloc_equality(bmap);
9248 if (l < 0)
9249 goto error;
9250 for (j = 0, pos = 0; j < 5; ++j) {
9251 int off = isl_basic_map_offset(bmap, c[j]);
9252 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9253 isl_int_set(bmap->eq[l][off + k],
9254 eq->row[i][pos]);
9255 ++pos;
9259 for (i = 0; i < ineq->n_row; ++i) {
9260 l = isl_basic_map_alloc_inequality(bmap);
9261 if (l < 0)
9262 goto error;
9263 for (j = 0, pos = 0; j < 5; ++j) {
9264 int off = isl_basic_map_offset(bmap, c[j]);
9265 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9266 isl_int_set(bmap->ineq[l][off + k],
9267 ineq->row[i][pos]);
9268 ++pos;
9273 isl_space_free(dim);
9274 isl_mat_free(eq);
9275 isl_mat_free(ineq);
9277 return bmap;
9278 error:
9279 isl_space_free(dim);
9280 isl_mat_free(eq);
9281 isl_mat_free(ineq);
9282 return NULL;
9285 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9286 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9287 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9289 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9290 c1, c2, c3, c4, isl_dim_in);
9293 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9294 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9295 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9297 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9298 c1, c2, c3, c4, isl_dim_in);
9301 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9302 __isl_take isl_space *dim,
9303 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9304 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9306 return (isl_basic_set*)
9307 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9308 c1, c2, c3, c4, isl_dim_in);
9311 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9313 if (!bmap)
9314 return -1;
9316 return isl_space_can_zip(bmap->dim);
9319 int isl_map_can_zip(__isl_keep isl_map *map)
9321 if (!map)
9322 return -1;
9324 return isl_space_can_zip(map->dim);
9327 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9328 * (A -> C) -> (B -> D).
9330 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9332 unsigned pos;
9333 unsigned n1;
9334 unsigned n2;
9336 if (!bmap)
9337 return NULL;
9339 if (!isl_basic_map_can_zip(bmap))
9340 isl_die(bmap->ctx, isl_error_invalid,
9341 "basic map cannot be zipped", goto error);
9342 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9343 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9344 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9345 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9346 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9347 if (!bmap)
9348 return NULL;
9349 bmap->dim = isl_space_zip(bmap->dim);
9350 if (!bmap->dim)
9351 goto error;
9352 return bmap;
9353 error:
9354 isl_basic_map_free(bmap);
9355 return NULL;
9358 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9359 * (A -> C) -> (B -> D).
9361 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9363 int i;
9365 if (!map)
9366 return NULL;
9368 if (!isl_map_can_zip(map))
9369 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9370 goto error);
9372 map = isl_map_cow(map);
9373 if (!map)
9374 return NULL;
9376 for (i = 0; i < map->n; ++i) {
9377 map->p[i] = isl_basic_map_zip(map->p[i]);
9378 if (!map->p[i])
9379 goto error;
9382 map->dim = isl_space_zip(map->dim);
9383 if (!map->dim)
9384 goto error;
9386 return map;
9387 error:
9388 isl_map_free(map);
9389 return NULL;
9392 /* Construct a basic map mapping the domain of the affine expression
9393 * to a one-dimensional range prescribed by the affine expression.
9395 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9397 int k;
9398 int pos;
9399 isl_local_space *ls;
9400 isl_basic_map *bmap;
9402 if (!aff)
9403 return NULL;
9405 ls = isl_aff_get_local_space(aff);
9406 ls = isl_local_space_from_domain(ls);
9407 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
9408 bmap = isl_basic_map_from_local_space(ls);
9409 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9410 k = isl_basic_map_alloc_equality(bmap);
9411 if (k < 0)
9412 goto error;
9414 pos = isl_basic_map_offset(bmap, isl_dim_out);
9415 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9416 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9417 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9418 aff->v->size - (pos + 1));
9420 isl_aff_free(aff);
9421 bmap = isl_basic_map_finalize(bmap);
9422 return bmap;
9423 error:
9424 isl_aff_free(aff);
9425 isl_basic_map_free(bmap);
9426 return NULL;
9429 /* Construct a basic map mapping a domain in the given space to
9430 * to an n-dimensional range, with n the number of elements in the list,
9431 * where each coordinate in the range is prescribed by the
9432 * corresponding affine expression.
9433 * The domains of all affine expressions in the list are assumed to match
9434 * domain_dim.
9436 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9437 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9439 int i;
9440 isl_space *dim;
9441 isl_basic_map *bmap;
9443 if (!list)
9444 return NULL;
9446 dim = isl_space_from_domain(domain_dim);
9447 bmap = isl_basic_map_universe(dim);
9449 for (i = 0; i < list->n; ++i) {
9450 isl_aff *aff;
9451 isl_basic_map *bmap_i;
9453 aff = isl_aff_copy(list->p[i]);
9454 bmap_i = isl_basic_map_from_aff(aff);
9456 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9459 isl_aff_list_free(list);
9460 return bmap;
9463 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9464 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9466 return isl_map_equate(set, type1, pos1, type2, pos2);
9469 /* Add a constraint imposing that the given two dimensions are equal.
9471 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9472 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9474 isl_basic_map *bmap = NULL;
9475 int i;
9477 if (!map)
9478 return NULL;
9480 if (pos1 >= isl_map_dim(map, type1))
9481 isl_die(map->ctx, isl_error_invalid,
9482 "index out of bounds", goto error);
9483 if (pos2 >= isl_map_dim(map, type2))
9484 isl_die(map->ctx, isl_error_invalid,
9485 "index out of bounds", goto error);
9487 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9488 i = isl_basic_map_alloc_equality(bmap);
9489 if (i < 0)
9490 goto error;
9491 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9492 pos1 += isl_basic_map_offset(bmap, type1);
9493 pos2 += isl_basic_map_offset(bmap, type2);
9494 isl_int_set_si(bmap->eq[i][pos1], -1);
9495 isl_int_set_si(bmap->eq[i][pos2], 1);
9496 bmap = isl_basic_map_finalize(bmap);
9498 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9500 return map;
9501 error:
9502 isl_basic_map_free(bmap);
9503 isl_map_free(map);
9504 return NULL;
9507 /* Add a constraint imposing that the given two dimensions have opposite values.
9509 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9510 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9512 isl_basic_map *bmap = NULL;
9513 int i;
9515 if (!map)
9516 return NULL;
9518 if (pos1 >= isl_map_dim(map, type1))
9519 isl_die(map->ctx, isl_error_invalid,
9520 "index out of bounds", goto error);
9521 if (pos2 >= isl_map_dim(map, type2))
9522 isl_die(map->ctx, isl_error_invalid,
9523 "index out of bounds", goto error);
9525 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9526 i = isl_basic_map_alloc_equality(bmap);
9527 if (i < 0)
9528 goto error;
9529 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9530 pos1 += isl_basic_map_offset(bmap, type1);
9531 pos2 += isl_basic_map_offset(bmap, type2);
9532 isl_int_set_si(bmap->eq[i][pos1], 1);
9533 isl_int_set_si(bmap->eq[i][pos2], 1);
9534 bmap = isl_basic_map_finalize(bmap);
9536 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9538 return map;
9539 error:
9540 isl_basic_map_free(bmap);
9541 isl_map_free(map);
9542 return NULL;