add isl_union_map_is_bijective
[isl.git] / isl_map.c
blobb81190f8c202ee3a6a0ba43e65648273e6eec106
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 <strings.h>
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl/blk.h>
18 #include "isl_dim_private.h"
19 #include "isl_equalities.h"
20 #include <isl/list.h>
21 #include <isl/lp.h>
22 #include <isl/seq.h>
23 #include <isl/set.h>
24 #include <isl/map.h>
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
28 #include "isl_tab.h"
29 #include <isl/vec.h>
30 #include <isl_mat_private.h>
31 #include <isl_dim_map.h>
33 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
35 switch (type) {
36 case isl_dim_param: return dim->nparam;
37 case isl_dim_in: return dim->n_in;
38 case isl_dim_out: return dim->n_out;
39 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
40 default: return 0;
44 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
46 switch (type) {
47 case isl_dim_param: return 1;
48 case isl_dim_in: return 1 + dim->nparam;
49 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
50 default: return 0;
54 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
55 enum isl_dim_type type)
57 if (!bmap)
58 return 0;
59 switch (type) {
60 case isl_dim_cst: return 1;
61 case isl_dim_param:
62 case isl_dim_in:
63 case isl_dim_out: return isl_dim_size(bmap->dim, type);
64 case isl_dim_div: return bmap->n_div;
65 case isl_dim_all: return isl_basic_map_total_dim(bmap);
66 default: return 0;
70 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
72 return map ? n(map->dim, type) : 0;
75 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
77 return set ? n(set->dim, type) : 0;
80 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
81 enum isl_dim_type type)
83 struct isl_dim *dim = bmap->dim;
84 switch (type) {
85 case isl_dim_cst: return 0;
86 case isl_dim_param: return 1;
87 case isl_dim_in: return 1 + dim->nparam;
88 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
89 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
90 default: return 0;
94 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
95 enum isl_dim_type type)
97 return isl_basic_map_offset(bset, type);
100 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
102 return pos(map->dim, type);
105 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
106 enum isl_dim_type type)
108 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
111 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
113 return isl_basic_set_dim(bset, isl_dim_set);
116 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
118 return isl_basic_set_dim(bset, isl_dim_param);
121 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
123 return isl_dim_total(bset->dim) + bset->n_div;
126 unsigned isl_set_n_dim(const struct isl_set *set)
128 return isl_set_dim(set, isl_dim_set);
131 unsigned isl_set_n_param(const struct isl_set *set)
133 return isl_set_dim(set, isl_dim_param);
136 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
138 return bmap ? bmap->dim->n_in : 0;
141 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
143 return bmap ? bmap->dim->n_out : 0;
146 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
148 return bmap ? bmap->dim->nparam : 0;
151 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
153 return bmap ? bmap->n_div : 0;
156 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
158 return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
161 unsigned isl_map_n_in(const struct isl_map *map)
163 return map->dim->n_in;
166 unsigned isl_map_n_out(const struct isl_map *map)
168 return map->dim->n_out;
171 unsigned isl_map_n_param(const struct isl_map *map)
173 return map->dim->nparam;
176 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
178 int m;
179 if (!map || !set)
180 return -1;
181 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
182 if (m < 0 || !m)
183 return m;
184 return isl_dim_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
187 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
188 struct isl_basic_set *bset)
190 int m;
191 if (!bmap || !bset)
192 return -1;
193 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
194 if (m < 0 || !m)
195 return m;
196 return isl_dim_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
199 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
200 struct isl_basic_set *bset)
202 int m;
203 if (!bmap || !bset)
204 return -1;
205 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
206 if (m < 0 || !m)
207 return m;
208 return isl_dim_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
211 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
213 return bmap ? bmap->ctx : NULL;
216 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
218 return bset ? bset->ctx : NULL;
221 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
223 return map ? map->ctx : NULL;
226 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
228 return set ? set->ctx : NULL;
231 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
233 if (!bmap)
234 return NULL;
235 return isl_dim_copy(bmap->dim);
238 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
240 if (!bset)
241 return NULL;
242 return isl_dim_copy(bset->dim);
245 struct isl_dim *isl_map_get_dim(struct isl_map *map)
247 if (!map)
248 return NULL;
249 return isl_dim_copy(map->dim);
252 struct isl_dim *isl_set_get_dim(struct isl_set *set)
254 if (!set)
255 return NULL;
256 return isl_dim_copy(set->dim);
259 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
260 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
262 bmap = isl_basic_map_cow(bmap);
263 if (!bmap)
264 return NULL;
265 bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
266 if (!bmap->dim)
267 goto error;
268 bmap = isl_basic_map_finalize(bmap);
269 return bmap;
270 error:
271 isl_basic_map_free(bmap);
272 return NULL;
275 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
276 enum isl_dim_type type)
278 return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
281 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
282 enum isl_dim_type type, const char *s)
284 int i;
286 map = isl_map_cow(map);
287 if (!map)
288 return NULL;
290 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
291 if (!map->dim)
292 goto error;
294 for (i = 0; i < map->n; ++i) {
295 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
296 if (!map->p[i])
297 goto error;
300 return map;
301 error:
302 isl_map_free(map);
303 return NULL;
306 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
307 enum isl_dim_type type)
309 return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
312 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
313 const char *s)
315 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
318 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
320 return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
323 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
325 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
328 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
329 enum isl_dim_type type, unsigned pos)
331 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
334 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
335 enum isl_dim_type type, unsigned pos)
337 return bset ? isl_dim_get_name(bset->dim, type, pos) : NULL;
340 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
341 enum isl_dim_type type, unsigned pos)
343 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
346 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
347 enum isl_dim_type type, unsigned pos)
349 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
352 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
353 __isl_take isl_basic_map *bmap,
354 enum isl_dim_type type, unsigned pos, const char *s)
356 if (!bmap)
357 return NULL;
358 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
359 if (!bmap->dim)
360 goto error;
361 return bmap;
362 error:
363 isl_basic_map_free(bmap);
364 return NULL;
367 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
368 enum isl_dim_type type, unsigned pos, const char *s)
370 int i;
372 if (!map)
373 return NULL;
375 map->dim = isl_dim_set_name(map->dim, type, pos, s);
376 if (!map->dim)
377 goto error;
379 for (i = 0; i < map->n; ++i) {
380 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
381 if (!map->p[i])
382 goto error;
385 return map;
386 error:
387 isl_map_free(map);
388 return NULL;
391 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
392 __isl_take isl_basic_set *bset,
393 enum isl_dim_type type, unsigned pos, const char *s)
395 return (isl_basic_set *)isl_basic_map_set_dim_name(
396 (isl_basic_map *)bset, type, pos, s);
399 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
400 enum isl_dim_type type, unsigned pos, const char *s)
402 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
405 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
407 if (!bmap)
408 return -1;
409 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
412 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
414 return isl_basic_map_is_rational(bset);
417 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
418 struct isl_basic_map *bmap, unsigned extra,
419 unsigned n_eq, unsigned n_ineq)
421 int i;
422 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
424 bmap->ctx = ctx;
425 isl_ctx_ref(ctx);
427 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
428 if (isl_blk_is_error(bmap->block))
429 goto error;
431 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
432 if (!bmap->ineq)
433 goto error;
435 if (extra == 0) {
436 bmap->block2 = isl_blk_empty();
437 bmap->div = NULL;
438 } else {
439 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
440 if (isl_blk_is_error(bmap->block2))
441 goto error;
443 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
444 if (!bmap->div)
445 goto error;
448 for (i = 0; i < n_ineq + n_eq; ++i)
449 bmap->ineq[i] = bmap->block.data + i * row_size;
451 for (i = 0; i < extra; ++i)
452 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
454 bmap->ref = 1;
455 bmap->flags = 0;
456 bmap->c_size = n_eq + n_ineq;
457 bmap->eq = bmap->ineq + n_ineq;
458 bmap->extra = extra;
459 bmap->n_eq = 0;
460 bmap->n_ineq = 0;
461 bmap->n_div = 0;
462 bmap->sample = NULL;
464 return bmap;
465 error:
466 isl_basic_map_free(bmap);
467 return NULL;
470 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
471 unsigned nparam, unsigned dim, unsigned extra,
472 unsigned n_eq, unsigned n_ineq)
474 struct isl_basic_map *bmap;
475 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
476 return (struct isl_basic_set *)bmap;
479 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
480 unsigned extra, unsigned n_eq, unsigned n_ineq)
482 struct isl_basic_map *bmap;
483 if (!dim)
484 return NULL;
485 isl_assert(dim->ctx, dim->n_in == 0, goto error);
486 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
487 return (struct isl_basic_set *)bmap;
488 error:
489 isl_dim_free(dim);
490 return NULL;
493 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
494 unsigned extra, unsigned n_eq, unsigned n_ineq)
496 struct isl_basic_map *bmap;
498 if (!dim)
499 return NULL;
500 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
501 if (!bmap)
502 goto error;
503 bmap->dim = dim;
505 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
506 error:
507 isl_dim_free(dim);
508 return NULL;
511 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
512 unsigned nparam, unsigned in, unsigned out, unsigned extra,
513 unsigned n_eq, unsigned n_ineq)
515 struct isl_basic_map *bmap;
516 struct isl_dim *dim;
518 dim = isl_dim_alloc(ctx, nparam, in, out);
519 if (!dim)
520 return NULL;
522 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
523 return bmap;
526 static void dup_constraints(
527 struct isl_basic_map *dst, struct isl_basic_map *src)
529 int i;
530 unsigned total = isl_basic_map_total_dim(src);
532 for (i = 0; i < src->n_eq; ++i) {
533 int j = isl_basic_map_alloc_equality(dst);
534 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
537 for (i = 0; i < src->n_ineq; ++i) {
538 int j = isl_basic_map_alloc_inequality(dst);
539 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
542 for (i = 0; i < src->n_div; ++i) {
543 int j = isl_basic_map_alloc_div(dst);
544 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
546 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
549 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
551 struct isl_basic_map *dup;
553 if (!bmap)
554 return NULL;
555 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
556 bmap->n_div, bmap->n_eq, bmap->n_ineq);
557 if (!dup)
558 return NULL;
559 dup_constraints(dup, bmap);
560 dup->flags = bmap->flags;
561 dup->sample = isl_vec_copy(bmap->sample);
562 return dup;
565 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
567 struct isl_basic_map *dup;
569 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
570 return (struct isl_basic_set *)dup;
573 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
575 if (!bset)
576 return NULL;
578 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
579 bset->ref++;
580 return bset;
582 return isl_basic_set_dup(bset);
585 struct isl_set *isl_set_copy(struct isl_set *set)
587 if (!set)
588 return NULL;
590 set->ref++;
591 return set;
594 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
596 if (!bmap)
597 return NULL;
599 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
600 bmap->ref++;
601 return bmap;
603 bmap = isl_basic_map_dup(bmap);
604 if (bmap)
605 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
606 return bmap;
609 struct isl_map *isl_map_copy(struct isl_map *map)
611 if (!map)
612 return NULL;
614 map->ref++;
615 return map;
618 void isl_basic_map_free(struct isl_basic_map *bmap)
620 if (!bmap)
621 return;
623 if (--bmap->ref > 0)
624 return;
626 isl_ctx_deref(bmap->ctx);
627 free(bmap->div);
628 isl_blk_free(bmap->ctx, bmap->block2);
629 free(bmap->ineq);
630 isl_blk_free(bmap->ctx, bmap->block);
631 isl_vec_free(bmap->sample);
632 isl_dim_free(bmap->dim);
633 free(bmap);
636 void isl_basic_set_free(struct isl_basic_set *bset)
638 isl_basic_map_free((struct isl_basic_map *)bset);
641 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
643 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
646 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
648 struct isl_ctx *ctx;
649 if (!bmap)
650 return -1;
651 ctx = bmap->ctx;
652 isl_assert(ctx, room_for_con(bmap, 1), return -1);
653 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
654 return -1);
655 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
656 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
657 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
658 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
659 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
660 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
661 isl_int *t;
662 int j = isl_basic_map_alloc_inequality(bmap);
663 if (j < 0)
664 return -1;
665 t = bmap->ineq[j];
666 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
667 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
668 bmap->eq[-1] = t;
669 bmap->n_eq++;
670 bmap->n_ineq--;
671 bmap->eq--;
672 return 0;
674 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
675 bmap->extra - bmap->n_div);
676 return bmap->n_eq++;
679 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
681 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
684 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
686 if (!bmap)
687 return -1;
688 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
689 bmap->n_eq -= n;
690 return 0;
693 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
695 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
698 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
700 isl_int *t;
701 if (!bmap)
702 return -1;
703 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
705 if (pos != bmap->n_eq - 1) {
706 t = bmap->eq[pos];
707 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
708 bmap->eq[bmap->n_eq - 1] = t;
710 bmap->n_eq--;
711 return 0;
714 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
716 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
719 void isl_basic_map_inequality_to_equality(
720 struct isl_basic_map *bmap, unsigned pos)
722 isl_int *t;
724 t = bmap->ineq[pos];
725 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
726 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
727 bmap->eq[-1] = t;
728 bmap->n_eq++;
729 bmap->n_ineq--;
730 bmap->eq--;
731 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
732 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
733 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
734 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
737 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
739 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
742 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
744 struct isl_ctx *ctx;
745 if (!bmap)
746 return -1;
747 ctx = bmap->ctx;
748 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
749 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
750 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
751 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
752 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
753 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
754 1 + isl_basic_map_total_dim(bmap),
755 bmap->extra - bmap->n_div);
756 return bmap->n_ineq++;
759 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
761 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
764 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
766 if (!bmap)
767 return -1;
768 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
769 bmap->n_ineq -= n;
770 return 0;
773 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
775 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
778 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
780 isl_int *t;
781 if (!bmap)
782 return -1;
783 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
785 if (pos != bmap->n_ineq - 1) {
786 t = bmap->ineq[pos];
787 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
788 bmap->ineq[bmap->n_ineq - 1] = t;
789 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
791 bmap->n_ineq--;
792 return 0;
795 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
797 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
800 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
801 isl_int *eq)
803 int k;
805 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
806 if (!bmap)
807 return NULL;
808 k = isl_basic_map_alloc_equality(bmap);
809 if (k < 0)
810 goto error;
811 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
812 return bmap;
813 error:
814 isl_basic_map_free(bmap);
815 return NULL;
818 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
819 isl_int *eq)
821 return (isl_basic_set *)
822 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
825 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
826 isl_int *ineq)
828 int k;
830 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
831 if (!bmap)
832 return NULL;
833 k = isl_basic_map_alloc_inequality(bmap);
834 if (k < 0)
835 goto error;
836 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
837 return bmap;
838 error:
839 isl_basic_map_free(bmap);
840 return NULL;
843 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
844 isl_int *ineq)
846 return (isl_basic_set *)
847 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
850 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
852 if (!bmap)
853 return -1;
854 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
855 isl_seq_clr(bmap->div[bmap->n_div] +
856 1 + 1 + isl_basic_map_total_dim(bmap),
857 bmap->extra - bmap->n_div);
858 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
859 return bmap->n_div++;
862 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
864 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
867 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
869 if (!bmap)
870 return -1;
871 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
872 bmap->n_div -= n;
873 return 0;
876 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
878 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
881 /* Copy constraint from src to dst, putting the vars of src at offset
882 * dim_off in dst and the divs of src at offset div_off in dst.
883 * If both sets are actually map, then dim_off applies to the input
884 * variables.
886 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
887 struct isl_basic_map *src_map, isl_int *src,
888 unsigned in_off, unsigned out_off, unsigned div_off)
890 unsigned src_nparam = isl_basic_map_n_param(src_map);
891 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
892 unsigned src_in = isl_basic_map_n_in(src_map);
893 unsigned dst_in = isl_basic_map_n_in(dst_map);
894 unsigned src_out = isl_basic_map_n_out(src_map);
895 unsigned dst_out = isl_basic_map_n_out(dst_map);
896 isl_int_set(dst[0], src[0]);
897 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
898 if (dst_nparam > src_nparam)
899 isl_seq_clr(dst+1+src_nparam,
900 dst_nparam - src_nparam);
901 isl_seq_clr(dst+1+dst_nparam, in_off);
902 isl_seq_cpy(dst+1+dst_nparam+in_off,
903 src+1+src_nparam,
904 isl_min(dst_in-in_off, src_in));
905 if (dst_in-in_off > src_in)
906 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
907 dst_in - in_off - src_in);
908 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
909 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
910 src+1+src_nparam+src_in,
911 isl_min(dst_out-out_off, src_out));
912 if (dst_out-out_off > src_out)
913 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
914 dst_out - out_off - src_out);
915 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
916 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
917 src+1+src_nparam+src_in+src_out,
918 isl_min(dst_map->extra-div_off, src_map->n_div));
919 if (dst_map->n_div-div_off > src_map->n_div)
920 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
921 div_off+src_map->n_div,
922 dst_map->n_div - div_off - src_map->n_div);
925 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
926 struct isl_basic_map *src_map, isl_int *src,
927 unsigned in_off, unsigned out_off, unsigned div_off)
929 isl_int_set(dst[0], src[0]);
930 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
933 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
934 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
936 int i;
937 unsigned div_off;
939 if (!bmap1 || !bmap2)
940 goto error;
942 div_off = bmap1->n_div;
944 for (i = 0; i < bmap2->n_eq; ++i) {
945 int i1 = isl_basic_map_alloc_equality(bmap1);
946 if (i1 < 0)
947 goto error;
948 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
949 i_pos, o_pos, div_off);
952 for (i = 0; i < bmap2->n_ineq; ++i) {
953 int i1 = isl_basic_map_alloc_inequality(bmap1);
954 if (i1 < 0)
955 goto error;
956 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
957 i_pos, o_pos, div_off);
960 for (i = 0; i < bmap2->n_div; ++i) {
961 int i1 = isl_basic_map_alloc_div(bmap1);
962 if (i1 < 0)
963 goto error;
964 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
965 i_pos, o_pos, div_off);
968 isl_basic_map_free(bmap2);
970 return bmap1;
972 error:
973 isl_basic_map_free(bmap1);
974 isl_basic_map_free(bmap2);
975 return NULL;
978 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
979 struct isl_basic_set *bset2, unsigned pos)
981 return (struct isl_basic_set *)
982 add_constraints((struct isl_basic_map *)bset1,
983 (struct isl_basic_map *)bset2, 0, pos);
986 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
987 struct isl_dim *dim, unsigned extra,
988 unsigned n_eq, unsigned n_ineq)
990 struct isl_basic_map *ext;
991 unsigned flags;
992 int dims_ok;
994 if (!dim)
995 goto error;
997 if (!base)
998 goto error;
1000 dims_ok = isl_dim_equal(base->dim, dim) &&
1001 base->extra >= base->n_div + extra;
1003 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1004 room_for_ineq(base, n_ineq)) {
1005 isl_dim_free(dim);
1006 return base;
1009 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1010 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1011 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1012 extra += base->extra;
1013 n_eq += base->n_eq;
1014 n_ineq += base->n_ineq;
1016 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1017 dim = NULL;
1018 if (!ext)
1019 goto error;
1021 if (dims_ok)
1022 ext->sample = isl_vec_copy(base->sample);
1023 flags = base->flags;
1024 ext = add_constraints(ext, base, 0, 0);
1025 if (ext) {
1026 ext->flags = flags;
1027 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1030 return ext;
1032 error:
1033 isl_dim_free(dim);
1034 isl_basic_map_free(base);
1035 return NULL;
1038 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1039 struct isl_dim *dim, unsigned extra,
1040 unsigned n_eq, unsigned n_ineq)
1042 return (struct isl_basic_set *)
1043 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1044 extra, n_eq, n_ineq);
1047 struct isl_basic_map *isl_basic_map_extend_constraints(
1048 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1050 if (!base)
1051 return NULL;
1052 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1053 0, n_eq, n_ineq);
1056 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1057 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1058 unsigned n_eq, unsigned n_ineq)
1060 struct isl_basic_map *bmap;
1061 struct isl_dim *dim;
1063 if (!base)
1064 return NULL;
1065 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1066 if (!dim)
1067 goto error;
1069 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1070 return bmap;
1071 error:
1072 isl_basic_map_free(base);
1073 return NULL;
1076 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1077 unsigned nparam, unsigned dim, unsigned extra,
1078 unsigned n_eq, unsigned n_ineq)
1080 return (struct isl_basic_set *)
1081 isl_basic_map_extend((struct isl_basic_map *)base,
1082 nparam, 0, dim, extra, n_eq, n_ineq);
1085 struct isl_basic_set *isl_basic_set_extend_constraints(
1086 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1088 return (struct isl_basic_set *)
1089 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1090 n_eq, n_ineq);
1093 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1095 return (struct isl_basic_set *)
1096 isl_basic_map_cow((struct isl_basic_map *)bset);
1099 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1101 if (!bmap)
1102 return NULL;
1104 if (bmap->ref > 1) {
1105 bmap->ref--;
1106 bmap = isl_basic_map_dup(bmap);
1108 if (bmap)
1109 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1110 return bmap;
1113 struct isl_set *isl_set_cow(struct isl_set *set)
1115 if (!set)
1116 return NULL;
1118 if (set->ref == 1)
1119 return set;
1120 set->ref--;
1121 return isl_set_dup(set);
1124 struct isl_map *isl_map_cow(struct isl_map *map)
1126 if (!map)
1127 return NULL;
1129 if (map->ref == 1)
1130 return map;
1131 map->ref--;
1132 return isl_map_dup(map);
1135 static void swap_vars(struct isl_blk blk, isl_int *a,
1136 unsigned a_len, unsigned b_len)
1138 isl_seq_cpy(blk.data, a+a_len, b_len);
1139 isl_seq_cpy(blk.data+b_len, a, a_len);
1140 isl_seq_cpy(a, blk.data, b_len+a_len);
1143 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1144 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1146 int i;
1147 struct isl_blk blk;
1149 if (!bmap)
1150 goto error;
1152 isl_assert(bmap->ctx,
1153 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1155 if (n1 == 0 || n2 == 0)
1156 return bmap;
1158 bmap = isl_basic_map_cow(bmap);
1159 if (!bmap)
1160 return NULL;
1162 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1163 if (isl_blk_is_error(blk))
1164 goto error;
1166 for (i = 0; i < bmap->n_eq; ++i)
1167 swap_vars(blk,
1168 bmap->eq[i] + pos, n1, n2);
1170 for (i = 0; i < bmap->n_ineq; ++i)
1171 swap_vars(blk,
1172 bmap->ineq[i] + pos, n1, n2);
1174 for (i = 0; i < bmap->n_div; ++i)
1175 swap_vars(blk,
1176 bmap->div[i]+1 + pos, n1, n2);
1178 isl_blk_free(bmap->ctx, blk);
1180 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1181 bmap = isl_basic_map_gauss(bmap, NULL);
1182 return isl_basic_map_finalize(bmap);
1183 error:
1184 isl_basic_map_free(bmap);
1185 return NULL;
1188 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1189 __isl_take isl_basic_set *bset, unsigned n)
1191 unsigned dim;
1192 unsigned nparam;
1194 nparam = isl_basic_set_n_param(bset);
1195 dim = isl_basic_set_n_dim(bset);
1196 isl_assert(bset->ctx, n <= dim, goto error);
1198 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1199 error:
1200 isl_basic_set_free(bset);
1201 return NULL;
1204 static __isl_give isl_set *isl_set_swap_vars(__isl_take isl_set *set,
1205 unsigned n)
1207 int i;
1208 set = isl_set_cow(set);
1209 if (!set)
1210 return NULL;
1212 for (i = 0; i < set->n; ++i) {
1213 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1214 if (!set->p[i]) {
1215 isl_set_free(set);
1216 return NULL;
1219 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1220 return set;
1223 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1225 int i = 0;
1226 unsigned total;
1227 if (!bmap)
1228 goto error;
1229 total = isl_basic_map_total_dim(bmap);
1230 isl_basic_map_free_div(bmap, bmap->n_div);
1231 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1232 if (bmap->n_eq > 0)
1233 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1234 else {
1235 i = isl_basic_map_alloc_equality(bmap);
1236 if (i < 0)
1237 goto error;
1239 isl_int_set_si(bmap->eq[i][0], 1);
1240 isl_seq_clr(bmap->eq[i]+1, total);
1241 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1242 isl_vec_free(bmap->sample);
1243 bmap->sample = NULL;
1244 return isl_basic_map_finalize(bmap);
1245 error:
1246 isl_basic_map_free(bmap);
1247 return NULL;
1250 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1252 return (struct isl_basic_set *)
1253 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1256 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1258 int i;
1259 unsigned off = isl_dim_total(bmap->dim);
1260 isl_int *t = bmap->div[a];
1261 bmap->div[a] = bmap->div[b];
1262 bmap->div[b] = t;
1264 for (i = 0; i < bmap->n_eq; ++i)
1265 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1267 for (i = 0; i < bmap->n_ineq; ++i)
1268 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1270 for (i = 0; i < bmap->n_div; ++i)
1271 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1275 /* Eliminate the specified n dimensions starting at first from the
1276 * constraints using Fourier-Motzkin. The dimensions themselves
1277 * are not removed.
1279 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1280 enum isl_dim_type type, unsigned first, unsigned n)
1282 int i;
1284 if (!map)
1285 return NULL;
1286 if (n == 0)
1287 return map;
1289 map = isl_map_cow(map);
1290 if (!map)
1291 return NULL;
1292 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1293 first += pos(map->dim, type) - 1;
1295 for (i = 0; i < map->n; ++i) {
1296 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1297 if (!map->p[i])
1298 goto error;
1300 return map;
1301 error:
1302 isl_map_free(map);
1303 return NULL;
1306 /* Eliminate the specified n dimensions starting at first from the
1307 * constraints using Fourier-Motzkin. The dimensions themselves
1308 * are not removed.
1310 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1311 enum isl_dim_type type, unsigned first, unsigned n)
1313 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1316 /* Eliminate the specified n dimensions starting at first from the
1317 * constraints using Fourier-Motzkin. The dimensions themselves
1318 * are not removed.
1320 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1321 unsigned first, unsigned n)
1323 return isl_set_eliminate(set, isl_dim_set, first, n);
1326 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1327 __isl_take isl_basic_map *bmap)
1329 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1330 bmap->n_div);
1331 if (!bmap)
1332 return NULL;
1333 bmap->n_div = 0;
1334 return isl_basic_map_finalize(bmap);
1337 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1338 __isl_take isl_basic_set *bset)
1340 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1341 (struct isl_basic_map *)bset);
1344 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1346 int i;
1348 if (!map)
1349 return NULL;
1350 if (map->n == 0)
1351 return map;
1353 map = isl_map_cow(map);
1354 if (!map)
1355 return NULL;
1357 for (i = 0; i < map->n; ++i) {
1358 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1359 if (!map->p[i])
1360 goto error;
1362 return map;
1363 error:
1364 isl_map_free(map);
1365 return NULL;
1368 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1370 return isl_map_remove_divs(set);
1373 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1374 enum isl_dim_type type, unsigned first, unsigned n)
1376 if (!bmap)
1377 return NULL;
1378 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1379 goto error);
1380 if (n == 0)
1381 return bmap;
1382 bmap = isl_basic_map_eliminate_vars(bmap,
1383 isl_basic_map_offset(bmap, type) - 1 + first, n);
1384 if (!bmap)
1385 return bmap;
1386 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1387 return bmap;
1388 bmap = isl_basic_map_drop(bmap, type, first, n);
1389 return bmap;
1390 error:
1391 isl_basic_map_free(bmap);
1392 return NULL;
1395 /* Return true if the definition of the given div (recursively) involves
1396 * any of the given variables.
1398 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1399 unsigned first, unsigned n)
1401 int i;
1402 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1404 if (isl_int_is_zero(bmap->div[div][0]))
1405 return 0;
1406 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1407 return 1;
1409 for (i = bmap->n_div - 1; i >= 0; --i) {
1410 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1411 continue;
1412 if (div_involves_vars(bmap, i, first, n))
1413 return 1;
1416 return 0;
1419 /* Remove all divs (recursively) involving any of the given dimensions
1420 * in their definitions.
1422 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1423 __isl_take isl_basic_map *bmap,
1424 enum isl_dim_type type, unsigned first, unsigned n)
1426 int i;
1428 if (!bmap)
1429 return NULL;
1430 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1431 goto error);
1432 first += isl_basic_map_offset(bmap, type);
1434 for (i = bmap->n_div - 1; i >= 0; --i) {
1435 if (!div_involves_vars(bmap, i, first, n))
1436 continue;
1437 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1440 return bmap;
1441 error:
1442 isl_basic_map_free(bmap);
1443 return NULL;
1446 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1447 enum isl_dim_type type, unsigned first, unsigned n)
1449 int i;
1451 if (!map)
1452 return NULL;
1453 if (map->n == 0)
1454 return map;
1456 map = isl_map_cow(map);
1457 if (!map)
1458 return NULL;
1460 for (i = 0; i < map->n; ++i) {
1461 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1462 type, first, n);
1463 if (!map->p[i])
1464 goto error;
1466 return map;
1467 error:
1468 isl_map_free(map);
1469 return NULL;
1472 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1473 enum isl_dim_type type, unsigned first, unsigned n)
1475 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1476 type, first, n);
1479 /* Return true if the definition of the given div is unknown or depends
1480 * on unknown divs.
1482 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1484 int i;
1485 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1487 if (isl_int_is_zero(bmap->div[div][0]))
1488 return 1;
1490 for (i = bmap->n_div - 1; i >= 0; --i) {
1491 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1492 continue;
1493 if (div_is_unknown(bmap, i))
1494 return 1;
1497 return 0;
1500 /* Remove all divs that are unknown or defined in terms of unknown divs.
1502 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1503 __isl_take isl_basic_map *bmap)
1505 int i;
1507 if (!bmap)
1508 return NULL;
1510 for (i = bmap->n_div - 1; i >= 0; --i) {
1511 if (!div_is_unknown(bmap, i))
1512 continue;
1513 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1516 return bmap;
1519 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1521 int i;
1523 if (!map)
1524 return NULL;
1525 if (map->n == 0)
1526 return map;
1528 map = isl_map_cow(map);
1529 if (!map)
1530 return NULL;
1532 for (i = 0; i < map->n; ++i) {
1533 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1534 if (!map->p[i])
1535 goto error;
1537 return map;
1538 error:
1539 isl_map_free(map);
1540 return NULL;
1543 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1545 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1548 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1549 __isl_take isl_basic_set *bset,
1550 enum isl_dim_type type, unsigned first, unsigned n)
1552 return (isl_basic_set *)
1553 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1556 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1557 enum isl_dim_type type, unsigned first, unsigned n)
1559 int i;
1561 if (n == 0)
1562 return map;
1564 map = isl_map_cow(map);
1565 if (!map)
1566 return NULL;
1567 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1569 for (i = 0; i < map->n; ++i) {
1570 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1571 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1572 if (!map->p[i])
1573 goto error;
1575 map = isl_map_drop(map, type, first, n);
1576 return map;
1577 error:
1578 isl_map_free(map);
1579 return NULL;
1582 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1583 enum isl_dim_type type, unsigned first, unsigned n)
1585 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1588 /* Project out n inputs starting at first using Fourier-Motzkin */
1589 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1590 unsigned first, unsigned n)
1592 return isl_map_remove_dims(map, isl_dim_in, first, n);
1595 static void dump_term(struct isl_basic_map *bmap,
1596 isl_int c, int pos, FILE *out)
1598 const char *name;
1599 unsigned in = isl_basic_map_n_in(bmap);
1600 unsigned dim = in + isl_basic_map_n_out(bmap);
1601 unsigned nparam = isl_basic_map_n_param(bmap);
1602 if (!pos)
1603 isl_int_print(out, c, 0);
1604 else {
1605 if (!isl_int_is_one(c))
1606 isl_int_print(out, c, 0);
1607 if (pos < 1 + nparam) {
1608 name = isl_dim_get_name(bmap->dim,
1609 isl_dim_param, pos - 1);
1610 if (name)
1611 fprintf(out, "%s", name);
1612 else
1613 fprintf(out, "p%d", pos - 1);
1614 } else if (pos < 1 + nparam + in)
1615 fprintf(out, "i%d", pos - 1 - nparam);
1616 else if (pos < 1 + nparam + dim)
1617 fprintf(out, "o%d", pos - 1 - nparam - in);
1618 else
1619 fprintf(out, "e%d", pos - 1 - nparam - dim);
1623 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1624 int sign, FILE *out)
1626 int i;
1627 int first;
1628 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1629 isl_int v;
1631 isl_int_init(v);
1632 for (i = 0, first = 1; i < len; ++i) {
1633 if (isl_int_sgn(c[i]) * sign <= 0)
1634 continue;
1635 if (!first)
1636 fprintf(out, " + ");
1637 first = 0;
1638 isl_int_abs(v, c[i]);
1639 dump_term(bmap, v, i, out);
1641 isl_int_clear(v);
1642 if (first)
1643 fprintf(out, "0");
1646 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1647 const char *op, FILE *out, int indent)
1649 int i;
1651 fprintf(out, "%*s", indent, "");
1653 dump_constraint_sign(bmap, c, 1, out);
1654 fprintf(out, " %s ", op);
1655 dump_constraint_sign(bmap, c, -1, out);
1657 fprintf(out, "\n");
1659 for (i = bmap->n_div; i < bmap->extra; ++i) {
1660 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1661 continue;
1662 fprintf(out, "%*s", indent, "");
1663 fprintf(out, "ERROR: unused div coefficient not zero\n");
1664 abort();
1668 static void dump_constraints(struct isl_basic_map *bmap,
1669 isl_int **c, unsigned n,
1670 const char *op, FILE *out, int indent)
1672 int i;
1674 for (i = 0; i < n; ++i)
1675 dump_constraint(bmap, c[i], op, out, indent);
1678 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1680 int j;
1681 int first = 1;
1682 unsigned total = isl_basic_map_total_dim(bmap);
1684 for (j = 0; j < 1 + total; ++j) {
1685 if (isl_int_is_zero(exp[j]))
1686 continue;
1687 if (!first && isl_int_is_pos(exp[j]))
1688 fprintf(out, "+");
1689 dump_term(bmap, exp[j], j, out);
1690 first = 0;
1694 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1696 int i;
1698 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1699 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1701 for (i = 0; i < bmap->n_div; ++i) {
1702 fprintf(out, "%*s", indent, "");
1703 fprintf(out, "e%d = [(", i);
1704 dump_affine(bmap, bmap->div[i]+1, out);
1705 fprintf(out, ")/");
1706 isl_int_print(out, bmap->div[i][0], 0);
1707 fprintf(out, "]\n");
1711 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1712 FILE *out, int indent)
1714 if (!bset) {
1715 fprintf(out, "null basic set\n");
1716 return;
1719 fprintf(out, "%*s", indent, "");
1720 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1721 bset->ref, bset->dim->nparam, bset->dim->n_out,
1722 bset->extra, bset->flags);
1723 dump((struct isl_basic_map *)bset, out, indent);
1726 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1727 FILE *out, int indent)
1729 if (!bmap) {
1730 fprintf(out, "null basic map\n");
1731 return;
1734 fprintf(out, "%*s", indent, "");
1735 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1736 "flags: %x, n_name: %d\n",
1737 bmap->ref,
1738 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1739 bmap->extra, bmap->flags, bmap->dim->n_name);
1740 dump(bmap, out, indent);
1743 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1745 unsigned total;
1746 if (!bmap)
1747 return -1;
1748 total = isl_basic_map_total_dim(bmap);
1749 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1750 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1751 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1752 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1753 return 0;
1756 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1758 struct isl_set *set;
1760 if (!dim)
1761 return NULL;
1762 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1763 isl_assert(dim->ctx, n >= 0, goto error);
1764 set = isl_alloc(dim->ctx, struct isl_set,
1765 sizeof(struct isl_set) +
1766 (n - 1) * sizeof(struct isl_basic_set *));
1767 if (!set)
1768 goto error;
1770 set->ctx = dim->ctx;
1771 isl_ctx_ref(set->ctx);
1772 set->ref = 1;
1773 set->size = n;
1774 set->n = 0;
1775 set->dim = dim;
1776 set->flags = flags;
1777 return set;
1778 error:
1779 isl_dim_free(dim);
1780 return NULL;
1783 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1784 unsigned nparam, unsigned dim, int n, unsigned flags)
1786 struct isl_set *set;
1787 struct isl_dim *dims;
1789 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1790 if (!dims)
1791 return NULL;
1793 set = isl_set_alloc_dim(dims, n, flags);
1794 return set;
1797 /* Make sure "map" has room for at least "n" more basic maps.
1799 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1801 int i;
1802 struct isl_map *grown = NULL;
1804 if (!map)
1805 return NULL;
1806 isl_assert(map->ctx, n >= 0, goto error);
1807 if (map->n + n <= map->size)
1808 return map;
1809 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1810 if (!grown)
1811 goto error;
1812 for (i = 0; i < map->n; ++i) {
1813 grown->p[i] = isl_basic_map_copy(map->p[i]);
1814 if (!grown->p[i])
1815 goto error;
1816 grown->n++;
1818 isl_map_free(map);
1819 return grown;
1820 error:
1821 isl_map_free(grown);
1822 isl_map_free(map);
1823 return NULL;
1826 /* Make sure "set" has room for at least "n" more basic sets.
1828 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1830 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1833 struct isl_set *isl_set_dup(struct isl_set *set)
1835 int i;
1836 struct isl_set *dup;
1838 if (!set)
1839 return NULL;
1841 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1842 if (!dup)
1843 return NULL;
1844 for (i = 0; i < set->n; ++i)
1845 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1846 return dup;
1849 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1851 return isl_map_from_basic_map(bset);
1854 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1856 struct isl_map *map;
1858 if (!bmap)
1859 return NULL;
1861 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1862 return isl_map_add_basic_map(map, bmap);
1865 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1866 __isl_take isl_basic_set *bset)
1868 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1869 (struct isl_basic_map *)bset);
1872 void isl_set_free(struct isl_set *set)
1874 int i;
1876 if (!set)
1877 return;
1879 if (--set->ref > 0)
1880 return;
1882 isl_ctx_deref(set->ctx);
1883 for (i = 0; i < set->n; ++i)
1884 isl_basic_set_free(set->p[i]);
1885 isl_dim_free(set->dim);
1886 free(set);
1889 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
1891 int i;
1893 if (!set) {
1894 fprintf(out, "null set\n");
1895 return;
1898 fprintf(out, "%*s", indent, "");
1899 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1900 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1901 set->flags);
1902 for (i = 0; i < set->n; ++i) {
1903 fprintf(out, "%*s", indent, "");
1904 fprintf(out, "basic set %d:\n", i);
1905 isl_basic_set_print_internal(set->p[i], out, indent+4);
1909 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
1911 int i;
1913 if (!map) {
1914 fprintf(out, "null map\n");
1915 return;
1918 fprintf(out, "%*s", indent, "");
1919 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1920 "flags: %x, n_name: %d\n",
1921 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1922 map->dim->n_out, map->flags, map->dim->n_name);
1923 for (i = 0; i < map->n; ++i) {
1924 fprintf(out, "%*s", indent, "");
1925 fprintf(out, "basic map %d:\n", i);
1926 isl_basic_map_print_internal(map->p[i], out, indent+4);
1930 struct isl_basic_map *isl_basic_map_intersect_domain(
1931 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1933 struct isl_basic_map *bmap_domain;
1935 if (!bmap || !bset)
1936 goto error;
1938 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1939 bset->dim, isl_dim_param), goto error);
1941 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1942 isl_assert(bset->ctx,
1943 isl_basic_map_compatible_domain(bmap, bset), goto error);
1945 bmap = isl_basic_map_cow(bmap);
1946 if (!bmap)
1947 goto error;
1948 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1949 bset->n_div, bset->n_eq, bset->n_ineq);
1950 bmap_domain = isl_basic_map_from_domain(bset);
1951 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1953 bmap = isl_basic_map_simplify(bmap);
1954 return isl_basic_map_finalize(bmap);
1955 error:
1956 isl_basic_map_free(bmap);
1957 isl_basic_set_free(bset);
1958 return NULL;
1961 struct isl_basic_map *isl_basic_map_intersect_range(
1962 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1964 struct isl_basic_map *bmap_range;
1966 if (!bmap || !bset)
1967 goto error;
1969 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1970 bset->dim, isl_dim_param), goto error);
1972 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1973 isl_assert(bset->ctx,
1974 isl_basic_map_compatible_range(bmap, bset), goto error);
1976 bmap = isl_basic_map_cow(bmap);
1977 if (!bmap)
1978 goto error;
1979 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1980 bset->n_div, bset->n_eq, bset->n_ineq);
1981 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1982 bmap = add_constraints(bmap, bmap_range, 0, 0);
1984 bmap = isl_basic_map_simplify(bmap);
1985 return isl_basic_map_finalize(bmap);
1986 error:
1987 isl_basic_map_free(bmap);
1988 isl_basic_set_free(bset);
1989 return NULL;
1992 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1994 int i;
1995 unsigned total;
1996 isl_int s;
1998 total = 1 + isl_basic_map_total_dim(bmap);
1999 if (total != vec->size)
2000 return -1;
2002 isl_int_init(s);
2004 for (i = 0; i < bmap->n_eq; ++i) {
2005 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2006 if (!isl_int_is_zero(s)) {
2007 isl_int_clear(s);
2008 return 0;
2012 for (i = 0; i < bmap->n_ineq; ++i) {
2013 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2014 if (isl_int_is_neg(s)) {
2015 isl_int_clear(s);
2016 return 0;
2020 isl_int_clear(s);
2022 return 1;
2025 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2027 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2030 struct isl_basic_map *isl_basic_map_intersect(
2031 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2033 struct isl_vec *sample = NULL;
2035 if (!bmap1 || !bmap2)
2036 goto error;
2038 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2039 bmap2->dim, isl_dim_param), goto error);
2040 if (isl_dim_total(bmap1->dim) ==
2041 isl_dim_size(bmap1->dim, isl_dim_param) &&
2042 isl_dim_total(bmap2->dim) !=
2043 isl_dim_size(bmap2->dim, isl_dim_param))
2044 return isl_basic_map_intersect(bmap2, bmap1);
2046 if (isl_dim_total(bmap2->dim) !=
2047 isl_dim_size(bmap2->dim, isl_dim_param))
2048 isl_assert(bmap1->ctx,
2049 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2051 if (bmap1->sample &&
2052 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2053 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2054 sample = isl_vec_copy(bmap1->sample);
2055 else if (bmap2->sample &&
2056 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2057 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2058 sample = isl_vec_copy(bmap2->sample);
2060 bmap1 = isl_basic_map_cow(bmap1);
2061 if (!bmap1)
2062 goto error;
2063 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2064 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2065 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2067 if (!bmap1)
2068 isl_vec_free(sample);
2069 else if (sample) {
2070 isl_vec_free(bmap1->sample);
2071 bmap1->sample = sample;
2074 bmap1 = isl_basic_map_simplify(bmap1);
2075 return isl_basic_map_finalize(bmap1);
2076 error:
2077 if (sample)
2078 isl_vec_free(sample);
2079 isl_basic_map_free(bmap1);
2080 isl_basic_map_free(bmap2);
2081 return NULL;
2084 struct isl_basic_set *isl_basic_set_intersect(
2085 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2087 return (struct isl_basic_set *)
2088 isl_basic_map_intersect(
2089 (struct isl_basic_map *)bset1,
2090 (struct isl_basic_map *)bset2);
2093 /* Special case of isl_map_intersect, where both map1 and map2
2094 * are convex, without any divs and such that either map1 or map2
2095 * contains a single constraint. This constraint is then simply
2096 * added to the other map.
2098 static __isl_give isl_map *map_intersect_add_constraint(
2099 __isl_take isl_map *map1, __isl_take isl_map *map2)
2101 struct isl_basic_map *bmap1;
2102 struct isl_basic_map *bmap2;
2104 isl_assert(map1->ctx, map1->n == 1, goto error);
2105 isl_assert(map2->ctx, map1->n == 1, goto error);
2106 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2107 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2109 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2110 return isl_map_intersect(map2, map1);
2112 isl_assert(map2->ctx,
2113 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2115 map1 = isl_map_cow(map1);
2116 if (!map1)
2117 goto error;
2118 if (isl_map_fast_is_empty(map1)) {
2119 isl_map_free(map2);
2120 return map1;
2122 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2123 if (map2->p[0]->n_eq == 1)
2124 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2125 else
2126 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2127 map2->p[0]->ineq[0]);
2129 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2130 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2131 if (!map1->p[0])
2132 goto error;
2134 if (isl_basic_map_fast_is_empty(map1->p[0])) {
2135 isl_basic_map_free(map1->p[0]);
2136 map1->n = 0;
2139 isl_map_free(map2);
2141 return map1;
2142 error:
2143 isl_map_free(map1);
2144 isl_map_free(map2);
2145 return NULL;
2148 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2150 unsigned flags = 0;
2151 struct isl_map *result;
2152 int i, j;
2154 if (!map1 || !map2)
2155 goto error;
2157 if (isl_map_fast_is_empty(map1)) {
2158 isl_map_free(map2);
2159 return map1;
2161 if (isl_map_fast_is_empty(map2)) {
2162 isl_map_free(map1);
2163 return map2;
2166 if (map1->n == 1 && map2->n == 1 &&
2167 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2168 isl_dim_equal(map1->dim, map2->dim) &&
2169 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2170 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2171 return map_intersect_add_constraint(map1, map2);
2172 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2173 map2->dim, isl_dim_param), goto error);
2174 if (isl_dim_total(map1->dim) ==
2175 isl_dim_size(map1->dim, isl_dim_param) &&
2176 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2177 return isl_map_intersect(map2, map1);
2179 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2180 isl_assert(map1->ctx,
2181 isl_dim_equal(map1->dim, map2->dim), goto error);
2183 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2184 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2185 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2187 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2188 map1->n * map2->n, flags);
2189 if (!result)
2190 goto error;
2191 for (i = 0; i < map1->n; ++i)
2192 for (j = 0; j < map2->n; ++j) {
2193 struct isl_basic_map *part;
2194 part = isl_basic_map_intersect(
2195 isl_basic_map_copy(map1->p[i]),
2196 isl_basic_map_copy(map2->p[j]));
2197 if (isl_basic_map_is_empty(part))
2198 isl_basic_map_free(part);
2199 else
2200 result = isl_map_add_basic_map(result, part);
2201 if (!result)
2202 goto error;
2204 isl_map_free(map1);
2205 isl_map_free(map2);
2206 return result;
2207 error:
2208 isl_map_free(map1);
2209 isl_map_free(map2);
2210 return NULL;
2213 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2215 return (struct isl_set *)
2216 isl_map_intersect((struct isl_map *)set1,
2217 (struct isl_map *)set2);
2220 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2222 struct isl_dim *dim;
2223 struct isl_basic_set *bset;
2224 unsigned in;
2226 if (!bmap)
2227 return NULL;
2228 bmap = isl_basic_map_cow(bmap);
2229 if (!bmap)
2230 return NULL;
2231 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2232 in = isl_basic_map_n_in(bmap);
2233 bset = isl_basic_set_from_basic_map(bmap);
2234 bset = isl_basic_set_swap_vars(bset, in);
2235 return isl_basic_map_from_basic_set(bset, dim);
2238 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2239 enum isl_dim_type type, unsigned pos, unsigned n)
2241 struct isl_dim *res_dim;
2242 struct isl_basic_map *res;
2243 struct isl_dim_map *dim_map;
2244 unsigned total, off;
2245 enum isl_dim_type t;
2247 if (n == 0)
2248 return bmap;
2250 if (!bmap)
2251 return NULL;
2253 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2255 total = isl_basic_map_total_dim(bmap) + n;
2256 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2257 off = 0;
2258 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2259 if (t != type) {
2260 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2261 } else {
2262 unsigned size = isl_basic_map_dim(bmap, t);
2263 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2264 0, pos, off);
2265 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2266 pos, size - pos, off + pos + n);
2268 off += isl_dim_size(res_dim, t);
2270 isl_dim_map_div(dim_map, bmap, off);
2272 res = isl_basic_map_alloc_dim(res_dim,
2273 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2274 if (isl_basic_map_is_rational(bmap))
2275 res = isl_basic_map_set_rational(res);
2276 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2277 return isl_basic_map_finalize(res);
2280 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2281 enum isl_dim_type type, unsigned n)
2283 if (!bmap)
2284 return NULL;
2285 return isl_basic_map_insert(bmap, type,
2286 isl_basic_map_dim(bmap, type), n);
2289 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2290 enum isl_dim_type type, unsigned n)
2292 if (!bset)
2293 return NULL;
2294 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2295 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2296 error:
2297 isl_basic_set_free(bset);
2298 return NULL;
2301 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2302 enum isl_dim_type type, unsigned pos, unsigned n)
2304 int i;
2306 if (n == 0)
2307 return map;
2309 map = isl_map_cow(map);
2310 if (!map)
2311 return NULL;
2313 map->dim = isl_dim_insert(map->dim, type, pos, n);
2314 if (!map->dim)
2315 goto error;
2317 for (i = 0; i < map->n; ++i) {
2318 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2319 if (!map->p[i])
2320 goto error;
2323 return map;
2324 error:
2325 isl_map_free(map);
2326 return NULL;
2329 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2330 enum isl_dim_type type, unsigned pos, unsigned n)
2332 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2335 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2336 enum isl_dim_type type, unsigned n)
2338 if (!map)
2339 return NULL;
2340 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2343 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2344 enum isl_dim_type type, unsigned n)
2346 if (!set)
2347 return NULL;
2348 isl_assert(set->ctx, type != isl_dim_in, goto error);
2349 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2350 error:
2351 isl_set_free(set);
2352 return NULL;
2355 __isl_give isl_basic_map *isl_basic_map_move_dims(
2356 __isl_take isl_basic_map *bmap,
2357 enum isl_dim_type dst_type, unsigned dst_pos,
2358 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2360 int i;
2361 struct isl_dim_map *dim_map;
2362 struct isl_basic_map *res;
2363 enum isl_dim_type t;
2364 unsigned total, off;
2366 if (!bmap)
2367 return NULL;
2368 if (n == 0)
2369 return bmap;
2371 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2372 goto error);
2374 if (dst_type == src_type && dst_pos == src_pos)
2375 return bmap;
2377 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2379 if (pos(bmap->dim, dst_type) + dst_pos ==
2380 pos(bmap->dim, src_type) + src_pos +
2381 ((src_type < dst_type) ? n : 0)) {
2382 bmap = isl_basic_map_cow(bmap);
2383 if (!bmap)
2384 return NULL;
2386 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2387 src_type, src_pos, n);
2388 if (!bmap->dim)
2389 goto error;
2391 bmap = isl_basic_map_finalize(bmap);
2393 return bmap;
2396 total = isl_basic_map_total_dim(bmap);
2397 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2399 off = 0;
2400 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2401 unsigned size = isl_dim_size(bmap->dim, t);
2402 if (t == dst_type) {
2403 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2404 0, dst_pos, off);
2405 off += dst_pos;
2406 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2407 src_pos, n, off);
2408 off += n;
2409 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2410 dst_pos, size - dst_pos, off);
2411 off += size - dst_pos;
2412 } else if (t == src_type) {
2413 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2414 0, src_pos, off);
2415 off += src_pos;
2416 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2417 src_pos + n, size - src_pos - n, off);
2418 off += size - src_pos - n;
2419 } else {
2420 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2421 off += size;
2424 isl_dim_map_div(dim_map, bmap, off);
2426 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2427 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2428 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2430 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2431 src_type, src_pos, n);
2432 if (!bmap->dim)
2433 goto error;
2435 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2436 bmap = isl_basic_map_gauss(bmap, NULL);
2437 bmap = isl_basic_map_finalize(bmap);
2439 return bmap;
2440 error:
2441 isl_basic_map_free(bmap);
2442 return NULL;
2445 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2446 enum isl_dim_type dst_type, unsigned dst_pos,
2447 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2449 return (isl_basic_set *)isl_basic_map_move_dims(
2450 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2453 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2454 enum isl_dim_type dst_type, unsigned dst_pos,
2455 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2457 if (!set)
2458 return NULL;
2459 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2460 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2461 src_type, src_pos, n);
2462 error:
2463 isl_set_free(set);
2464 return NULL;
2467 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2468 enum isl_dim_type dst_type, unsigned dst_pos,
2469 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2471 int i;
2473 if (!map)
2474 return NULL;
2475 if (n == 0)
2476 return map;
2478 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2479 goto error);
2481 if (dst_type == src_type && dst_pos == src_pos)
2482 return map;
2484 isl_assert(map->ctx, dst_type != src_type, goto error);
2486 map = isl_map_cow(map);
2487 if (!map)
2488 return NULL;
2490 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2491 if (!map->dim)
2492 goto error;
2494 for (i = 0; i < map->n; ++i) {
2495 map->p[i] = isl_basic_map_move_dims(map->p[i],
2496 dst_type, dst_pos,
2497 src_type, src_pos, n);
2498 if (!map->p[i])
2499 goto error;
2502 return map;
2503 error:
2504 isl_map_free(map);
2505 return NULL;
2508 /* Move the specified dimensions to the last columns right before
2509 * the divs. Don't change the dimension specification of bmap.
2510 * That's the responsibility of the caller.
2512 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2513 enum isl_dim_type type, unsigned first, unsigned n)
2515 struct isl_dim_map *dim_map;
2516 struct isl_basic_map *res;
2517 enum isl_dim_type t;
2518 unsigned total, off;
2520 if (!bmap)
2521 return NULL;
2522 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2523 return bmap;
2525 total = isl_basic_map_total_dim(bmap);
2526 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2528 off = 0;
2529 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2530 unsigned size = isl_dim_size(bmap->dim, t);
2531 if (t == type) {
2532 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2533 0, first, off);
2534 off += first;
2535 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2536 first, n, total - bmap->n_div - n);
2537 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2538 first + n, size - (first + n), off);
2539 off += size - (first + n);
2540 } else {
2541 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2542 off += size;
2545 isl_dim_map_div(dim_map, bmap, off + n);
2547 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2548 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2549 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2550 return res;
2553 /* Turn the n dimensions of type type, starting at first
2554 * into existentially quantified variables.
2556 __isl_give isl_basic_map *isl_basic_map_project_out(
2557 __isl_take isl_basic_map *bmap,
2558 enum isl_dim_type type, unsigned first, unsigned n)
2560 int i;
2561 size_t row_size;
2562 isl_int **new_div;
2563 isl_int *old;
2565 if (n == 0)
2566 return bmap;
2568 if (!bmap)
2569 return NULL;
2571 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2572 return isl_basic_map_remove_dims(bmap, type, first, n);
2574 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2575 goto error);
2577 bmap = move_last(bmap, type, first, n);
2578 bmap = isl_basic_map_cow(bmap);
2579 if (!bmap)
2580 return NULL;
2582 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2583 old = bmap->block2.data;
2584 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2585 (bmap->extra + n) * (1 + row_size));
2586 if (!bmap->block2.data)
2587 goto error;
2588 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2589 if (!new_div)
2590 goto error;
2591 for (i = 0; i < n; ++i) {
2592 new_div[i] = bmap->block2.data +
2593 (bmap->extra + i) * (1 + row_size);
2594 isl_seq_clr(new_div[i], 1 + row_size);
2596 for (i = 0; i < bmap->extra; ++i)
2597 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2598 free(bmap->div);
2599 bmap->div = new_div;
2600 bmap->n_div += n;
2601 bmap->extra += n;
2603 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2604 if (!bmap->dim)
2605 goto error;
2606 bmap = isl_basic_map_simplify(bmap);
2607 bmap = isl_basic_map_drop_redundant_divs(bmap);
2608 return isl_basic_map_finalize(bmap);
2609 error:
2610 isl_basic_map_free(bmap);
2611 return NULL;
2614 /* Turn the n dimensions of type type, starting at first
2615 * into existentially quantified variables.
2617 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2618 enum isl_dim_type type, unsigned first, unsigned n)
2620 return (isl_basic_set *)isl_basic_map_project_out(
2621 (isl_basic_map *)bset, type, first, n);
2624 /* Turn the n dimensions of type type, starting at first
2625 * into existentially quantified variables.
2627 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2628 enum isl_dim_type type, unsigned first, unsigned n)
2630 int i;
2632 if (!map)
2633 return NULL;
2635 if (n == 0)
2636 return map;
2638 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2640 map = isl_map_cow(map);
2641 if (!map)
2642 return NULL;
2644 map->dim = isl_dim_drop(map->dim, type, first, n);
2645 if (!map->dim)
2646 goto error;
2648 for (i = 0; i < map->n; ++i) {
2649 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2650 if (!map->p[i])
2651 goto error;
2654 return map;
2655 error:
2656 isl_map_free(map);
2657 return NULL;
2660 /* Turn the n dimensions of type type, starting at first
2661 * into existentially quantified variables.
2663 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2664 enum isl_dim_type type, unsigned first, unsigned n)
2666 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2669 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2671 int i, j;
2673 for (i = 0; i < n; ++i) {
2674 j = isl_basic_map_alloc_div(bmap);
2675 if (j < 0)
2676 goto error;
2677 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2679 return bmap;
2680 error:
2681 isl_basic_map_free(bmap);
2682 return NULL;
2685 struct isl_basic_map *isl_basic_map_apply_range(
2686 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2688 struct isl_dim *dim_result = NULL;
2689 struct isl_basic_map *bmap;
2690 unsigned n_in, n_out, n, nparam, total, pos;
2691 struct isl_dim_map *dim_map1, *dim_map2;
2693 if (!bmap1 || !bmap2)
2694 goto error;
2696 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2697 isl_dim_copy(bmap2->dim));
2699 n_in = isl_basic_map_n_in(bmap1);
2700 n_out = isl_basic_map_n_out(bmap2);
2701 n = isl_basic_map_n_out(bmap1);
2702 nparam = isl_basic_map_n_param(bmap1);
2704 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2705 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2706 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2707 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2708 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2709 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2710 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2711 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2712 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2713 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2714 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2716 bmap = isl_basic_map_alloc_dim(dim_result,
2717 bmap1->n_div + bmap2->n_div + n,
2718 bmap1->n_eq + bmap2->n_eq,
2719 bmap1->n_ineq + bmap2->n_ineq);
2720 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2721 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2722 bmap = add_divs(bmap, n);
2723 bmap = isl_basic_map_simplify(bmap);
2724 bmap = isl_basic_map_drop_redundant_divs(bmap);
2725 return isl_basic_map_finalize(bmap);
2726 error:
2727 isl_basic_map_free(bmap1);
2728 isl_basic_map_free(bmap2);
2729 return NULL;
2732 struct isl_basic_set *isl_basic_set_apply(
2733 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2735 if (!bset || !bmap)
2736 goto error;
2738 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2739 goto error);
2741 return (struct isl_basic_set *)
2742 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2743 error:
2744 isl_basic_set_free(bset);
2745 isl_basic_map_free(bmap);
2746 return NULL;
2749 struct isl_basic_map *isl_basic_map_apply_domain(
2750 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2752 if (!bmap1 || !bmap2)
2753 goto error;
2755 isl_assert(bmap1->ctx,
2756 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2757 isl_assert(bmap1->ctx,
2758 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2759 goto error);
2761 bmap1 = isl_basic_map_reverse(bmap1);
2762 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2763 return isl_basic_map_reverse(bmap1);
2764 error:
2765 isl_basic_map_free(bmap1);
2766 isl_basic_map_free(bmap2);
2767 return NULL;
2770 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2771 * A \cap B -> f(A) + f(B)
2773 struct isl_basic_map *isl_basic_map_sum(
2774 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2776 unsigned n_in, n_out, nparam, total, pos;
2777 struct isl_basic_map *bmap = NULL;
2778 struct isl_dim_map *dim_map1, *dim_map2;
2779 int i;
2781 if (!bmap1 || !bmap2)
2782 goto error;
2784 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2785 goto error);
2787 nparam = isl_basic_map_n_param(bmap1);
2788 n_in = isl_basic_map_n_in(bmap1);
2789 n_out = isl_basic_map_n_out(bmap1);
2791 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2792 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2793 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2794 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2795 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2796 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2797 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2798 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2799 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2800 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2801 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2803 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2804 bmap1->n_div + bmap2->n_div + 2 * n_out,
2805 bmap1->n_eq + bmap2->n_eq + n_out,
2806 bmap1->n_ineq + bmap2->n_ineq);
2807 for (i = 0; i < n_out; ++i) {
2808 int j = isl_basic_map_alloc_equality(bmap);
2809 if (j < 0)
2810 goto error;
2811 isl_seq_clr(bmap->eq[j], 1+total);
2812 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2813 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2814 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2816 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2817 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2818 bmap = add_divs(bmap, 2 * n_out);
2820 bmap = isl_basic_map_simplify(bmap);
2821 return isl_basic_map_finalize(bmap);
2822 error:
2823 isl_basic_map_free(bmap);
2824 isl_basic_map_free(bmap1);
2825 isl_basic_map_free(bmap2);
2826 return NULL;
2829 /* Given two maps A -> f(A) and B -> g(B), construct a map
2830 * A \cap B -> f(A) + f(B)
2832 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2834 struct isl_map *result;
2835 int i, j;
2837 if (!map1 || !map2)
2838 goto error;
2840 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2842 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2843 map1->n * map2->n, 0);
2844 if (!result)
2845 goto error;
2846 for (i = 0; i < map1->n; ++i)
2847 for (j = 0; j < map2->n; ++j) {
2848 struct isl_basic_map *part;
2849 part = isl_basic_map_sum(
2850 isl_basic_map_copy(map1->p[i]),
2851 isl_basic_map_copy(map2->p[j]));
2852 if (isl_basic_map_is_empty(part))
2853 isl_basic_map_free(part);
2854 else
2855 result = isl_map_add_basic_map(result, part);
2856 if (!result)
2857 goto error;
2859 isl_map_free(map1);
2860 isl_map_free(map2);
2861 return result;
2862 error:
2863 isl_map_free(map1);
2864 isl_map_free(map2);
2865 return NULL;
2868 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2869 __isl_take isl_set *set2)
2871 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2874 /* Given a basic map A -> f(A), construct A -> -f(A).
2876 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2878 int i, j;
2879 unsigned off, n;
2881 bmap = isl_basic_map_cow(bmap);
2882 if (!bmap)
2883 return NULL;
2885 n = isl_basic_map_dim(bmap, isl_dim_out);
2886 off = isl_basic_map_offset(bmap, isl_dim_out);
2887 for (i = 0; i < bmap->n_eq; ++i)
2888 for (j = 0; j < n; ++j)
2889 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2890 for (i = 0; i < bmap->n_ineq; ++i)
2891 for (j = 0; j < n; ++j)
2892 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2893 for (i = 0; i < bmap->n_div; ++i)
2894 for (j = 0; j < n; ++j)
2895 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2896 return isl_basic_map_finalize(bmap);
2899 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
2901 return isl_basic_map_neg(bset);
2904 /* Given a map A -> f(A), construct A -> -f(A).
2906 struct isl_map *isl_map_neg(struct isl_map *map)
2908 int i;
2910 map = isl_map_cow(map);
2911 if (!map)
2912 return NULL;
2914 for (i = 0; i < map->n; ++i) {
2915 map->p[i] = isl_basic_map_neg(map->p[i]);
2916 if (!map->p[i])
2917 goto error;
2920 return map;
2921 error:
2922 isl_map_free(map);
2923 return NULL;
2926 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
2928 return (isl_set *)isl_map_neg((isl_map *)set);
2931 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2932 * A -> floor(f(A)/d).
2934 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2935 isl_int d)
2937 unsigned n_in, n_out, nparam, total, pos;
2938 struct isl_basic_map *result = NULL;
2939 struct isl_dim_map *dim_map;
2940 int i;
2942 if (!bmap)
2943 return NULL;
2945 nparam = isl_basic_map_n_param(bmap);
2946 n_in = isl_basic_map_n_in(bmap);
2947 n_out = isl_basic_map_n_out(bmap);
2949 total = nparam + n_in + n_out + bmap->n_div + n_out;
2950 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2951 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2952 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2953 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2954 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2956 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2957 bmap->n_div + n_out,
2958 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2959 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
2960 result = add_divs(result, n_out);
2961 for (i = 0; i < n_out; ++i) {
2962 int j;
2963 j = isl_basic_map_alloc_inequality(result);
2964 if (j < 0)
2965 goto error;
2966 isl_seq_clr(result->ineq[j], 1+total);
2967 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2968 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2969 j = isl_basic_map_alloc_inequality(result);
2970 if (j < 0)
2971 goto error;
2972 isl_seq_clr(result->ineq[j], 1+total);
2973 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2974 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2975 isl_int_sub_ui(result->ineq[j][0], d, 1);
2978 result = isl_basic_map_simplify(result);
2979 return isl_basic_map_finalize(result);
2980 error:
2981 isl_basic_map_free(result);
2982 return NULL;
2985 /* Given a map A -> f(A) and an integer d, construct a map
2986 * A -> floor(f(A)/d).
2988 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2990 int i;
2992 map = isl_map_cow(map);
2993 if (!map)
2994 return NULL;
2996 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2997 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2998 for (i = 0; i < map->n; ++i) {
2999 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3000 if (!map->p[i])
3001 goto error;
3004 return map;
3005 error:
3006 isl_map_free(map);
3007 return NULL;
3010 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3012 int i;
3013 unsigned nparam;
3014 unsigned n_in;
3016 i = isl_basic_map_alloc_equality(bmap);
3017 if (i < 0)
3018 goto error;
3019 nparam = isl_basic_map_n_param(bmap);
3020 n_in = isl_basic_map_n_in(bmap);
3021 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3022 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3023 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3024 return isl_basic_map_finalize(bmap);
3025 error:
3026 isl_basic_map_free(bmap);
3027 return NULL;
3030 /* Add a constraints to "bmap" expressing i_pos < o_pos
3032 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3034 int i;
3035 unsigned nparam;
3036 unsigned n_in;
3038 i = isl_basic_map_alloc_inequality(bmap);
3039 if (i < 0)
3040 goto error;
3041 nparam = isl_basic_map_n_param(bmap);
3042 n_in = isl_basic_map_n_in(bmap);
3043 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3044 isl_int_set_si(bmap->ineq[i][0], -1);
3045 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3046 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3047 return isl_basic_map_finalize(bmap);
3048 error:
3049 isl_basic_map_free(bmap);
3050 return NULL;
3053 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3055 static __isl_give isl_basic_map *var_less_or_equal(
3056 __isl_take isl_basic_map *bmap, unsigned pos)
3058 int i;
3059 unsigned nparam;
3060 unsigned n_in;
3062 i = isl_basic_map_alloc_inequality(bmap);
3063 if (i < 0)
3064 goto error;
3065 nparam = isl_basic_map_n_param(bmap);
3066 n_in = isl_basic_map_n_in(bmap);
3067 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3068 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3069 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3070 return isl_basic_map_finalize(bmap);
3071 error:
3072 isl_basic_map_free(bmap);
3073 return NULL;
3076 /* Add a constraints to "bmap" expressing i_pos > o_pos
3078 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3080 int i;
3081 unsigned nparam;
3082 unsigned n_in;
3084 i = isl_basic_map_alloc_inequality(bmap);
3085 if (i < 0)
3086 goto error;
3087 nparam = isl_basic_map_n_param(bmap);
3088 n_in = isl_basic_map_n_in(bmap);
3089 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3090 isl_int_set_si(bmap->ineq[i][0], -1);
3091 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3092 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3093 return isl_basic_map_finalize(bmap);
3094 error:
3095 isl_basic_map_free(bmap);
3096 return NULL;
3099 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3101 static __isl_give isl_basic_map *var_more_or_equal(
3102 __isl_take isl_basic_map *bmap, unsigned pos)
3104 int i;
3105 unsigned nparam;
3106 unsigned n_in;
3108 i = isl_basic_map_alloc_inequality(bmap);
3109 if (i < 0)
3110 goto error;
3111 nparam = isl_basic_map_n_param(bmap);
3112 n_in = isl_basic_map_n_in(bmap);
3113 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3114 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3115 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3116 return isl_basic_map_finalize(bmap);
3117 error:
3118 isl_basic_map_free(bmap);
3119 return NULL;
3122 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3124 int i;
3125 struct isl_basic_map *bmap;
3126 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3127 if (!bmap)
3128 return NULL;
3129 for (i = 0; i < n_equal && bmap; ++i)
3130 bmap = var_equal(bmap, i);
3131 return isl_basic_map_finalize(bmap);
3134 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3136 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3138 int i;
3139 struct isl_basic_map *bmap;
3140 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3141 if (!bmap)
3142 return NULL;
3143 for (i = 0; i < pos && bmap; ++i)
3144 bmap = var_equal(bmap, i);
3145 if (bmap)
3146 bmap = var_less(bmap, pos);
3147 return isl_basic_map_finalize(bmap);
3150 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3152 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3153 __isl_take isl_dim *dim, unsigned pos)
3155 int i;
3156 isl_basic_map *bmap;
3158 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3159 for (i = 0; i < pos; ++i)
3160 bmap = var_equal(bmap, i);
3161 bmap = var_less_or_equal(bmap, pos);
3162 return isl_basic_map_finalize(bmap);
3165 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3167 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3169 int i;
3170 struct isl_basic_map *bmap;
3171 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3172 if (!bmap)
3173 return NULL;
3174 for (i = 0; i < pos && bmap; ++i)
3175 bmap = var_equal(bmap, i);
3176 if (bmap)
3177 bmap = var_more(bmap, pos);
3178 return isl_basic_map_finalize(bmap);
3181 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3183 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3184 __isl_take isl_dim *dim, unsigned pos)
3186 int i;
3187 isl_basic_map *bmap;
3189 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3190 for (i = 0; i < pos; ++i)
3191 bmap = var_equal(bmap, i);
3192 bmap = var_more_or_equal(bmap, pos);
3193 return isl_basic_map_finalize(bmap);
3196 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3197 unsigned n, int equal)
3199 struct isl_map *map;
3200 int i;
3202 if (n == 0 && equal)
3203 return isl_map_universe(dims);
3205 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3207 for (i = 0; i + 1 < n; ++i)
3208 map = isl_map_add_basic_map(map,
3209 isl_basic_map_less_at(isl_dim_copy(dims), i));
3210 if (n > 0) {
3211 if (equal)
3212 map = isl_map_add_basic_map(map,
3213 isl_basic_map_less_or_equal_at(dims, n - 1));
3214 else
3215 map = isl_map_add_basic_map(map,
3216 isl_basic_map_less_at(dims, n - 1));
3217 } else
3218 isl_dim_free(dims);
3220 return map;
3223 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3225 if (!dims)
3226 return NULL;
3227 return map_lex_lte_first(dims, dims->n_out, equal);
3230 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3232 return map_lex_lte_first(dim, n, 0);
3235 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3237 return map_lex_lte_first(dim, n, 1);
3240 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3242 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3245 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3247 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3250 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3251 unsigned n, int equal)
3253 struct isl_map *map;
3254 int i;
3256 if (n == 0 && equal)
3257 return isl_map_universe(dims);
3259 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3261 for (i = 0; i + 1 < n; ++i)
3262 map = isl_map_add_basic_map(map,
3263 isl_basic_map_more_at(isl_dim_copy(dims), i));
3264 if (n > 0) {
3265 if (equal)
3266 map = isl_map_add_basic_map(map,
3267 isl_basic_map_more_or_equal_at(dims, n - 1));
3268 else
3269 map = isl_map_add_basic_map(map,
3270 isl_basic_map_more_at(dims, n - 1));
3271 } else
3272 isl_dim_free(dims);
3274 return map;
3277 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3279 if (!dims)
3280 return NULL;
3281 return map_lex_gte_first(dims, dims->n_out, equal);
3284 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3286 return map_lex_gte_first(dim, n, 0);
3289 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3291 return map_lex_gte_first(dim, n, 1);
3294 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3296 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3299 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3301 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3304 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3305 __isl_take isl_set *set2)
3307 isl_map *map;
3308 map = isl_map_lex_le(isl_set_get_dim(set1));
3309 map = isl_map_intersect_domain(map, set1);
3310 map = isl_map_intersect_range(map, set2);
3311 return map;
3314 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3315 __isl_take isl_set *set2)
3317 isl_map *map;
3318 map = isl_map_lex_lt(isl_set_get_dim(set1));
3319 map = isl_map_intersect_domain(map, set1);
3320 map = isl_map_intersect_range(map, set2);
3321 return map;
3324 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3325 __isl_take isl_set *set2)
3327 isl_map *map;
3328 map = isl_map_lex_ge(isl_set_get_dim(set1));
3329 map = isl_map_intersect_domain(map, set1);
3330 map = isl_map_intersect_range(map, set2);
3331 return map;
3334 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3335 __isl_take isl_set *set2)
3337 isl_map *map;
3338 map = isl_map_lex_gt(isl_set_get_dim(set1));
3339 map = isl_map_intersect_domain(map, set1);
3340 map = isl_map_intersect_range(map, set2);
3341 return map;
3344 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3345 __isl_take isl_map *map2)
3347 isl_map *map;
3348 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3349 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3350 map = isl_map_apply_range(map, isl_map_reverse(map2));
3351 return map;
3354 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3355 __isl_take isl_map *map2)
3357 isl_map *map;
3358 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3359 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3360 map = isl_map_apply_range(map, isl_map_reverse(map2));
3361 return map;
3364 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3365 __isl_take isl_map *map2)
3367 isl_map *map;
3368 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3369 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3370 map = isl_map_apply_range(map, isl_map_reverse(map2));
3371 return map;
3374 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3375 __isl_take isl_map *map2)
3377 isl_map *map;
3378 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3379 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3380 map = isl_map_apply_range(map, isl_map_reverse(map2));
3381 return map;
3384 struct isl_basic_map *isl_basic_map_from_basic_set(
3385 struct isl_basic_set *bset, struct isl_dim *dim)
3387 struct isl_basic_map *bmap;
3389 bset = isl_basic_set_cow(bset);
3390 if (!bset || !dim)
3391 goto error;
3393 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3394 isl_dim_free(bset->dim);
3395 bmap = (struct isl_basic_map *) bset;
3396 bmap->dim = dim;
3397 return isl_basic_map_finalize(bmap);
3398 error:
3399 isl_basic_set_free(bset);
3400 isl_dim_free(dim);
3401 return NULL;
3404 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3406 if (!bmap)
3407 goto error;
3408 if (bmap->dim->n_in == 0)
3409 return (struct isl_basic_set *)bmap;
3410 bmap = isl_basic_map_cow(bmap);
3411 if (!bmap)
3412 goto error;
3413 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3414 if (!bmap->dim)
3415 goto error;
3416 bmap = isl_basic_map_finalize(bmap);
3417 return (struct isl_basic_set *)bmap;
3418 error:
3419 isl_basic_map_free(bmap);
3420 return NULL;
3423 /* For a div d = floor(f/m), add the constraints
3425 * f - m d >= 0
3426 * -(f-(n-1)) + m d >= 0
3428 * Note that the second constraint is the negation of
3430 * f - m d >= n
3432 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3433 unsigned pos, isl_int *div)
3435 int i, j;
3436 unsigned total = isl_basic_map_total_dim(bmap);
3438 i = isl_basic_map_alloc_inequality(bmap);
3439 if (i < 0)
3440 return -1;
3441 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3442 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3444 j = isl_basic_map_alloc_inequality(bmap);
3445 if (j < 0)
3446 return -1;
3447 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3448 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3449 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3450 return j;
3453 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3454 unsigned pos, isl_int *div)
3456 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3457 pos, div);
3460 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3462 unsigned total = isl_basic_map_total_dim(bmap);
3463 unsigned div_pos = total - bmap->n_div + div;
3465 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3466 bmap->div[div]);
3469 struct isl_basic_set *isl_basic_map_underlying_set(
3470 struct isl_basic_map *bmap)
3472 if (!bmap)
3473 goto error;
3474 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3475 bmap->n_div == 0 &&
3476 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3477 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3478 return (struct isl_basic_set *)bmap;
3479 bmap = isl_basic_map_cow(bmap);
3480 if (!bmap)
3481 goto error;
3482 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3483 if (!bmap->dim)
3484 goto error;
3485 bmap->extra -= bmap->n_div;
3486 bmap->n_div = 0;
3487 bmap = isl_basic_map_finalize(bmap);
3488 return (struct isl_basic_set *)bmap;
3489 error:
3490 return NULL;
3493 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3494 __isl_take isl_basic_set *bset)
3496 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3499 struct isl_basic_map *isl_basic_map_overlying_set(
3500 struct isl_basic_set *bset, struct isl_basic_map *like)
3502 struct isl_basic_map *bmap;
3503 struct isl_ctx *ctx;
3504 unsigned total;
3505 int i;
3507 if (!bset || !like)
3508 goto error;
3509 ctx = bset->ctx;
3510 isl_assert(ctx, bset->n_div == 0, goto error);
3511 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3512 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3513 goto error);
3514 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3515 isl_basic_map_free(like);
3516 return (struct isl_basic_map *)bset;
3518 bset = isl_basic_set_cow(bset);
3519 if (!bset)
3520 goto error;
3521 total = bset->dim->n_out + bset->extra;
3522 bmap = (struct isl_basic_map *)bset;
3523 isl_dim_free(bmap->dim);
3524 bmap->dim = isl_dim_copy(like->dim);
3525 if (!bmap->dim)
3526 goto error;
3527 bmap->n_div = like->n_div;
3528 bmap->extra += like->n_div;
3529 if (bmap->extra) {
3530 unsigned ltotal;
3531 isl_int **div;
3532 ltotal = total - bmap->extra + like->extra;
3533 if (ltotal > total)
3534 ltotal = total;
3535 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3536 bmap->extra * (1 + 1 + total));
3537 if (isl_blk_is_error(bmap->block2))
3538 goto error;
3539 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3540 if (!div)
3541 goto error;
3542 bmap->div = div;
3543 for (i = 0; i < bmap->extra; ++i)
3544 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3545 for (i = 0; i < like->n_div; ++i) {
3546 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3547 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3549 bmap = isl_basic_map_extend_constraints(bmap,
3550 0, 2 * like->n_div);
3551 for (i = 0; i < like->n_div; ++i) {
3552 if (isl_int_is_zero(bmap->div[i][0]))
3553 continue;
3554 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3555 goto error;
3558 isl_basic_map_free(like);
3559 bmap = isl_basic_map_simplify(bmap);
3560 bmap = isl_basic_map_finalize(bmap);
3561 return bmap;
3562 error:
3563 isl_basic_map_free(like);
3564 isl_basic_set_free(bset);
3565 return NULL;
3568 struct isl_basic_set *isl_basic_set_from_underlying_set(
3569 struct isl_basic_set *bset, struct isl_basic_set *like)
3571 return (struct isl_basic_set *)
3572 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3575 struct isl_set *isl_set_from_underlying_set(
3576 struct isl_set *set, struct isl_basic_set *like)
3578 int i;
3580 if (!set || !like)
3581 goto error;
3582 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3583 goto error);
3584 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3585 isl_basic_set_free(like);
3586 return set;
3588 set = isl_set_cow(set);
3589 if (!set)
3590 goto error;
3591 for (i = 0; i < set->n; ++i) {
3592 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3593 isl_basic_set_copy(like));
3594 if (!set->p[i])
3595 goto error;
3597 isl_dim_free(set->dim);
3598 set->dim = isl_dim_copy(like->dim);
3599 if (!set->dim)
3600 goto error;
3601 isl_basic_set_free(like);
3602 return set;
3603 error:
3604 isl_basic_set_free(like);
3605 isl_set_free(set);
3606 return NULL;
3609 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3611 int i;
3613 map = isl_map_cow(map);
3614 if (!map)
3615 return NULL;
3616 map->dim = isl_dim_cow(map->dim);
3617 if (!map->dim)
3618 goto error;
3620 for (i = 1; i < map->n; ++i)
3621 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3622 goto error);
3623 for (i = 0; i < map->n; ++i) {
3624 map->p[i] = (struct isl_basic_map *)
3625 isl_basic_map_underlying_set(map->p[i]);
3626 if (!map->p[i])
3627 goto error;
3629 if (map->n == 0)
3630 map->dim = isl_dim_underlying(map->dim, 0);
3631 else {
3632 isl_dim_free(map->dim);
3633 map->dim = isl_dim_copy(map->p[0]->dim);
3635 if (!map->dim)
3636 goto error;
3637 return (struct isl_set *)map;
3638 error:
3639 isl_map_free(map);
3640 return NULL;
3643 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3645 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3648 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3649 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3651 bmap = isl_basic_map_cow(bmap);
3652 if (!bmap || !dim)
3653 goto error;
3655 isl_dim_free(bmap->dim);
3656 bmap->dim = dim;
3658 bmap = isl_basic_map_finalize(bmap);
3660 return bmap;
3661 error:
3662 isl_basic_map_free(bmap);
3663 isl_dim_free(dim);
3664 return NULL;
3667 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3668 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3670 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3671 dim);
3674 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3675 __isl_take isl_dim *dim)
3677 int i;
3679 map = isl_map_cow(map);
3680 if (!map || !dim)
3681 goto error;
3683 for (i = 0; i < map->n; ++i) {
3684 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3685 isl_dim_copy(dim));
3686 if (!map->p[i])
3687 goto error;
3689 isl_dim_free(map->dim);
3690 map->dim = dim;
3692 return map;
3693 error:
3694 isl_map_free(map);
3695 isl_dim_free(dim);
3696 return NULL;
3699 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3700 __isl_take isl_dim *dim)
3702 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3705 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3707 isl_dim *dim;
3708 struct isl_basic_set *domain;
3709 unsigned n_in;
3710 unsigned n_out;
3712 if (!bmap)
3713 return NULL;
3714 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3716 n_in = isl_basic_map_n_in(bmap);
3717 n_out = isl_basic_map_n_out(bmap);
3718 domain = isl_basic_set_from_basic_map(bmap);
3719 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3721 domain = isl_basic_set_reset_dim(domain, dim);
3723 return domain;
3726 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3728 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3731 __isl_give isl_basic_map *isl_basic_map_domain_map(
3732 __isl_take isl_basic_map *bmap)
3734 int i, k;
3735 isl_dim *dim;
3736 isl_basic_map *domain;
3737 isl_basic_set *bset;
3738 int nparam, n_in, n_out;
3739 unsigned total;
3741 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3742 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3743 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3745 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3746 domain = isl_basic_map_universe(dim);
3748 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3749 bmap = isl_basic_map_apply_range(bmap, domain);
3750 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3752 total = isl_basic_map_total_dim(bmap);
3754 for (i = 0; i < n_in; ++i) {
3755 k = isl_basic_map_alloc_equality(bmap);
3756 if (k < 0)
3757 goto error;
3758 isl_seq_clr(bmap->eq[k], 1 + total);
3759 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3760 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3763 bmap = isl_basic_map_gauss(bmap, NULL);
3764 return isl_basic_map_finalize(bmap);
3765 error:
3766 isl_basic_map_free(bmap);
3767 return NULL;
3770 __isl_give isl_basic_map *isl_basic_map_range_map(
3771 __isl_take isl_basic_map *bmap)
3773 int i, k;
3774 isl_dim *dim;
3775 isl_basic_map *range;
3776 isl_basic_set *bset;
3777 int nparam, n_in, n_out;
3778 unsigned total;
3780 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3781 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3782 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3784 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3785 range = isl_basic_map_universe(dim);
3787 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3788 bmap = isl_basic_map_apply_range(bmap, range);
3789 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3791 total = isl_basic_map_total_dim(bmap);
3793 for (i = 0; i < n_out; ++i) {
3794 k = isl_basic_map_alloc_equality(bmap);
3795 if (k < 0)
3796 goto error;
3797 isl_seq_clr(bmap->eq[k], 1 + total);
3798 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3799 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3802 bmap = isl_basic_map_gauss(bmap, NULL);
3803 return isl_basic_map_finalize(bmap);
3804 error:
3805 isl_basic_map_free(bmap);
3806 return NULL;
3809 struct isl_set *isl_map_range(struct isl_map *map)
3811 int i;
3812 struct isl_set *set;
3814 if (!map)
3815 goto error;
3816 if (isl_map_dim(map, isl_dim_in) == 0)
3817 return (isl_set *)map;
3819 map = isl_map_cow(map);
3820 if (!map)
3821 goto error;
3823 set = (struct isl_set *) map;
3824 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3825 if (!set->dim)
3826 goto error;
3827 for (i = 0; i < map->n; ++i) {
3828 set->p[i] = isl_basic_map_range(map->p[i]);
3829 if (!set->p[i])
3830 goto error;
3832 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3833 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3834 return set;
3835 error:
3836 isl_map_free(map);
3837 return NULL;
3840 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3842 int i;
3843 isl_dim *domain_dim;
3845 map = isl_map_cow(map);
3846 if (!map)
3847 return NULL;
3849 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3850 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3851 map->dim = isl_dim_join(map->dim, domain_dim);
3852 if (!map->dim)
3853 goto error;
3854 for (i = 0; i < map->n; ++i) {
3855 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3856 if (!map->p[i])
3857 goto error;
3859 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3860 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3861 return map;
3862 error:
3863 isl_map_free(map);
3864 return NULL;
3867 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3869 int i;
3870 isl_dim *range_dim;
3872 map = isl_map_cow(map);
3873 if (!map)
3874 return NULL;
3876 range_dim = isl_dim_range(isl_map_get_dim(map));
3877 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3878 map->dim = isl_dim_join(map->dim, range_dim);
3879 if (!map->dim)
3880 goto error;
3881 for (i = 0; i < map->n; ++i) {
3882 map->p[i] = isl_basic_map_range_map(map->p[i]);
3883 if (!map->p[i])
3884 goto error;
3886 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3887 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3888 return map;
3889 error:
3890 isl_map_free(map);
3891 return NULL;
3894 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3896 int i;
3897 struct isl_map *map = NULL;
3899 set = isl_set_cow(set);
3900 if (!set || !dim)
3901 goto error;
3902 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3903 map = (struct isl_map *)set;
3904 for (i = 0; i < set->n; ++i) {
3905 map->p[i] = isl_basic_map_from_basic_set(
3906 set->p[i], isl_dim_copy(dim));
3907 if (!map->p[i])
3908 goto error;
3910 isl_dim_free(map->dim);
3911 map->dim = dim;
3912 return map;
3913 error:
3914 isl_dim_free(dim);
3915 isl_set_free(set);
3916 return NULL;
3919 __isl_give isl_basic_map *isl_basic_map_from_domain(
3920 __isl_take isl_basic_set *bset)
3922 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
3925 __isl_give isl_basic_map *isl_basic_map_from_range(
3926 __isl_take isl_basic_set *bset)
3928 return (isl_basic_map *)bset;
3931 struct isl_map *isl_map_from_range(struct isl_set *set)
3933 return (struct isl_map *)set;
3936 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3938 return isl_map_reverse(isl_map_from_range(set));
3941 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
3942 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
3944 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
3945 isl_basic_map_from_range(range));
3948 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3949 __isl_take isl_set *range)
3951 return isl_map_apply_range(isl_map_from_domain(domain),
3952 isl_map_from_range(range));
3955 struct isl_set *isl_set_from_map(struct isl_map *map)
3957 int i;
3958 struct isl_set *set = NULL;
3960 if (!map)
3961 return NULL;
3962 map = isl_map_cow(map);
3963 if (!map)
3964 return NULL;
3965 map->dim = isl_dim_as_set_dim(map->dim);
3966 if (!map->dim)
3967 goto error;
3968 set = (struct isl_set *)map;
3969 for (i = 0; i < map->n; ++i) {
3970 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3971 if (!set->p[i])
3972 goto error;
3974 return set;
3975 error:
3976 isl_map_free(map);
3977 return NULL;
3980 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3982 struct isl_map *map;
3984 if (!dim)
3985 return NULL;
3986 isl_assert(dim->ctx, n >= 0, return NULL);
3987 map = isl_alloc(dim->ctx, struct isl_map,
3988 sizeof(struct isl_map) +
3989 (n - 1) * sizeof(struct isl_basic_map *));
3990 if (!map)
3991 goto error;
3993 map->ctx = dim->ctx;
3994 isl_ctx_ref(map->ctx);
3995 map->ref = 1;
3996 map->size = n;
3997 map->n = 0;
3998 map->dim = dim;
3999 map->flags = flags;
4000 return map;
4001 error:
4002 isl_dim_free(dim);
4003 return NULL;
4006 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4007 unsigned nparam, unsigned in, unsigned out, int n,
4008 unsigned flags)
4010 struct isl_map *map;
4011 struct isl_dim *dims;
4013 dims = isl_dim_alloc(ctx, nparam, in, out);
4014 if (!dims)
4015 return NULL;
4017 map = isl_map_alloc_dim(dims, n, flags);
4018 return map;
4021 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4023 struct isl_basic_map *bmap;
4024 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4025 bmap = isl_basic_map_set_to_empty(bmap);
4026 return bmap;
4029 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4031 struct isl_basic_set *bset;
4032 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4033 bset = isl_basic_set_set_to_empty(bset);
4034 return bset;
4037 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4039 struct isl_basic_map *bmap;
4040 if (!model)
4041 return NULL;
4042 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4043 bmap = isl_basic_map_set_to_empty(bmap);
4044 return bmap;
4047 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4049 struct isl_basic_map *bmap;
4050 if (!model)
4051 return NULL;
4052 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4053 bmap = isl_basic_map_set_to_empty(bmap);
4054 return bmap;
4057 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4059 struct isl_basic_set *bset;
4060 if (!model)
4061 return NULL;
4062 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4063 bset = isl_basic_set_set_to_empty(bset);
4064 return bset;
4067 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4069 struct isl_basic_map *bmap;
4070 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4071 bmap = isl_basic_map_finalize(bmap);
4072 return bmap;
4075 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4077 struct isl_basic_set *bset;
4078 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4079 bset = isl_basic_set_finalize(bset);
4080 return bset;
4083 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4085 int i;
4086 unsigned total = isl_dim_total(dim);
4087 isl_basic_map *bmap;
4089 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4090 for (i = 0; i < total; ++i) {
4091 int k = isl_basic_map_alloc_inequality(bmap);
4092 if (k < 0)
4093 goto error;
4094 isl_seq_clr(bmap->ineq[k], 1 + total);
4095 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4097 return bmap;
4098 error:
4099 isl_basic_map_free(bmap);
4100 return NULL;
4103 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4105 return isl_basic_map_nat_universe(dim);
4108 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4110 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4113 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4115 return isl_map_nat_universe(dim);
4118 __isl_give isl_basic_map *isl_basic_map_universe_like(
4119 __isl_keep isl_basic_map *model)
4121 if (!model)
4122 return NULL;
4123 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4126 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4128 if (!model)
4129 return NULL;
4130 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4133 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4134 __isl_keep isl_set *model)
4136 if (!model)
4137 return NULL;
4138 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4141 struct isl_map *isl_map_empty(struct isl_dim *dim)
4143 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4146 struct isl_map *isl_map_empty_like(struct isl_map *model)
4148 if (!model)
4149 return NULL;
4150 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4153 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4155 if (!model)
4156 return NULL;
4157 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4160 struct isl_set *isl_set_empty(struct isl_dim *dim)
4162 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4165 struct isl_set *isl_set_empty_like(struct isl_set *model)
4167 if (!model)
4168 return NULL;
4169 return isl_set_empty(isl_dim_copy(model->dim));
4172 struct isl_map *isl_map_universe(struct isl_dim *dim)
4174 struct isl_map *map;
4175 if (!dim)
4176 return NULL;
4177 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4178 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4179 return map;
4182 struct isl_set *isl_set_universe(struct isl_dim *dim)
4184 struct isl_set *set;
4185 if (!dim)
4186 return NULL;
4187 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4188 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4189 return set;
4192 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4194 if (!model)
4195 return NULL;
4196 return isl_set_universe(isl_dim_copy(model->dim));
4199 struct isl_map *isl_map_dup(struct isl_map *map)
4201 int i;
4202 struct isl_map *dup;
4204 if (!map)
4205 return NULL;
4206 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4207 for (i = 0; i < map->n; ++i)
4208 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4209 return dup;
4212 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4213 __isl_take isl_basic_map *bmap)
4215 if (!bmap || !map)
4216 goto error;
4217 if (isl_basic_map_fast_is_empty(bmap)) {
4218 isl_basic_map_free(bmap);
4219 return map;
4221 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4222 isl_assert(map->ctx, map->n < map->size, goto error);
4223 map->p[map->n] = bmap;
4224 map->n++;
4225 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4226 return map;
4227 error:
4228 if (map)
4229 isl_map_free(map);
4230 if (bmap)
4231 isl_basic_map_free(bmap);
4232 return NULL;
4235 void isl_map_free(struct isl_map *map)
4237 int i;
4239 if (!map)
4240 return;
4242 if (--map->ref > 0)
4243 return;
4245 isl_ctx_deref(map->ctx);
4246 for (i = 0; i < map->n; ++i)
4247 isl_basic_map_free(map->p[i]);
4248 isl_dim_free(map->dim);
4249 free(map);
4252 struct isl_map *isl_map_extend(struct isl_map *base,
4253 unsigned nparam, unsigned n_in, unsigned n_out)
4255 int i;
4257 base = isl_map_cow(base);
4258 if (!base)
4259 return NULL;
4261 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4262 if (!base->dim)
4263 goto error;
4264 for (i = 0; i < base->n; ++i) {
4265 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4266 isl_dim_copy(base->dim), 0, 0, 0);
4267 if (!base->p[i])
4268 goto error;
4270 return base;
4271 error:
4272 isl_map_free(base);
4273 return NULL;
4276 struct isl_set *isl_set_extend(struct isl_set *base,
4277 unsigned nparam, unsigned dim)
4279 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4280 nparam, 0, dim);
4283 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4284 struct isl_basic_map *bmap, unsigned pos, int value)
4286 int j;
4288 bmap = isl_basic_map_cow(bmap);
4289 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4290 j = isl_basic_map_alloc_equality(bmap);
4291 if (j < 0)
4292 goto error;
4293 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4294 isl_int_set_si(bmap->eq[j][pos], -1);
4295 isl_int_set_si(bmap->eq[j][0], value);
4296 bmap = isl_basic_map_simplify(bmap);
4297 return isl_basic_map_finalize(bmap);
4298 error:
4299 isl_basic_map_free(bmap);
4300 return NULL;
4303 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4304 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4306 int j;
4308 bmap = isl_basic_map_cow(bmap);
4309 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4310 j = isl_basic_map_alloc_equality(bmap);
4311 if (j < 0)
4312 goto error;
4313 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4314 isl_int_set_si(bmap->eq[j][pos], -1);
4315 isl_int_set(bmap->eq[j][0], value);
4316 bmap = isl_basic_map_simplify(bmap);
4317 return isl_basic_map_finalize(bmap);
4318 error:
4319 isl_basic_map_free(bmap);
4320 return NULL;
4323 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4324 enum isl_dim_type type, unsigned pos, int value)
4326 if (!bmap)
4327 return NULL;
4328 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4329 return isl_basic_map_fix_pos_si(bmap,
4330 isl_basic_map_offset(bmap, type) + pos, value);
4331 error:
4332 isl_basic_map_free(bmap);
4333 return NULL;
4336 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4337 enum isl_dim_type type, unsigned pos, isl_int value)
4339 if (!bmap)
4340 return NULL;
4341 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4342 return isl_basic_map_fix_pos(bmap,
4343 isl_basic_map_offset(bmap, type) + pos, value);
4344 error:
4345 isl_basic_map_free(bmap);
4346 return NULL;
4349 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4350 enum isl_dim_type type, unsigned pos, int value)
4352 return (struct isl_basic_set *)
4353 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4354 type, pos, value);
4357 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4358 enum isl_dim_type type, unsigned pos, isl_int value)
4360 return (struct isl_basic_set *)
4361 isl_basic_map_fix((struct isl_basic_map *)bset,
4362 type, pos, value);
4365 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4366 unsigned input, int value)
4368 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4371 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4372 unsigned dim, int value)
4374 return (struct isl_basic_set *)
4375 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4376 isl_dim_set, dim, value);
4379 struct isl_map *isl_map_fix_si(struct isl_map *map,
4380 enum isl_dim_type type, unsigned pos, int value)
4382 int i;
4384 map = isl_map_cow(map);
4385 if (!map)
4386 return NULL;
4388 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4389 for (i = 0; i < map->n; ++i) {
4390 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4391 if (!map->p[i])
4392 goto error;
4394 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4395 return map;
4396 error:
4397 isl_map_free(map);
4398 return NULL;
4401 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4402 enum isl_dim_type type, unsigned pos, int value)
4404 return (struct isl_set *)
4405 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4408 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4409 enum isl_dim_type type, unsigned pos, isl_int value)
4411 int i;
4413 map = isl_map_cow(map);
4414 if (!map)
4415 return NULL;
4417 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4418 for (i = 0; i < map->n; ++i) {
4419 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4420 if (!map->p[i])
4421 goto error;
4423 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4424 return map;
4425 error:
4426 isl_map_free(map);
4427 return NULL;
4430 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4431 enum isl_dim_type type, unsigned pos, isl_int value)
4433 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4436 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4437 unsigned input, int value)
4439 return isl_map_fix_si(map, isl_dim_in, input, value);
4442 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4444 return (struct isl_set *)
4445 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4448 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4449 __isl_take isl_basic_map *bmap,
4450 enum isl_dim_type type, unsigned pos, int value)
4452 int j;
4454 if (!bmap)
4455 return NULL;
4456 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4457 pos += isl_basic_map_offset(bmap, type);
4458 bmap = isl_basic_map_cow(bmap);
4459 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4460 j = isl_basic_map_alloc_inequality(bmap);
4461 if (j < 0)
4462 goto error;
4463 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4464 isl_int_set_si(bmap->ineq[j][pos], 1);
4465 isl_int_set_si(bmap->ineq[j][0], -value);
4466 bmap = isl_basic_map_simplify(bmap);
4467 return isl_basic_map_finalize(bmap);
4468 error:
4469 isl_basic_map_free(bmap);
4470 return NULL;
4473 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4474 unsigned dim, isl_int value)
4476 int j;
4478 bset = isl_basic_set_cow(bset);
4479 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4480 j = isl_basic_set_alloc_inequality(bset);
4481 if (j < 0)
4482 goto error;
4483 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4484 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4485 isl_int_neg(bset->ineq[j][0], value);
4486 bset = isl_basic_set_simplify(bset);
4487 return isl_basic_set_finalize(bset);
4488 error:
4489 isl_basic_set_free(bset);
4490 return NULL;
4493 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4494 enum isl_dim_type type, unsigned pos, int value)
4496 int i;
4498 map = isl_map_cow(map);
4499 if (!map)
4500 return NULL;
4502 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4503 for (i = 0; i < map->n; ++i) {
4504 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4505 type, pos, value);
4506 if (!map->p[i])
4507 goto error;
4509 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4510 return map;
4511 error:
4512 isl_map_free(map);
4513 return NULL;
4516 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4517 enum isl_dim_type type, unsigned pos, int value)
4519 return (struct isl_set *)
4520 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4523 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4524 isl_int value)
4526 int i;
4528 set = isl_set_cow(set);
4529 if (!set)
4530 return NULL;
4532 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4533 for (i = 0; i < set->n; ++i) {
4534 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4535 if (!set->p[i])
4536 goto error;
4538 return set;
4539 error:
4540 isl_set_free(set);
4541 return NULL;
4544 struct isl_map *isl_map_reverse(struct isl_map *map)
4546 int i;
4548 map = isl_map_cow(map);
4549 if (!map)
4550 return NULL;
4552 map->dim = isl_dim_reverse(map->dim);
4553 if (!map->dim)
4554 goto error;
4555 for (i = 0; i < map->n; ++i) {
4556 map->p[i] = isl_basic_map_reverse(map->p[i]);
4557 if (!map->p[i])
4558 goto error;
4560 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4561 return map;
4562 error:
4563 isl_map_free(map);
4564 return NULL;
4567 static struct isl_map *isl_basic_map_partial_lexopt(
4568 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4569 struct isl_set **empty, int max)
4571 if (!bmap)
4572 goto error;
4573 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4574 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4575 else
4576 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4577 error:
4578 isl_basic_map_free(bmap);
4579 isl_basic_set_free(dom);
4580 if (empty)
4581 *empty = NULL;
4582 return NULL;
4585 struct isl_map *isl_basic_map_partial_lexmax(
4586 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4587 struct isl_set **empty)
4589 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4592 struct isl_map *isl_basic_map_partial_lexmin(
4593 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4594 struct isl_set **empty)
4596 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4599 struct isl_set *isl_basic_set_partial_lexmin(
4600 struct isl_basic_set *bset, struct isl_basic_set *dom,
4601 struct isl_set **empty)
4603 return (struct isl_set *)
4604 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4605 dom, empty);
4608 struct isl_set *isl_basic_set_partial_lexmax(
4609 struct isl_basic_set *bset, struct isl_basic_set *dom,
4610 struct isl_set **empty)
4612 return (struct isl_set *)
4613 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4614 dom, empty);
4617 /* Given a basic map "bmap", compute the lexicographically minimal
4618 * (or maximal) image element for each domain element in dom.
4619 * Set *empty to those elements in dom that do not have an image element.
4621 * We first make sure the basic sets in dom are disjoint and then
4622 * simply collect the results over each of the basic sets separately.
4623 * We could probably improve the efficiency a bit by moving the union
4624 * domain down into the parametric integer programming.
4626 static __isl_give isl_map *basic_map_partial_lexopt(
4627 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4628 __isl_give isl_set **empty, int max)
4630 int i;
4631 struct isl_map *res;
4633 dom = isl_set_make_disjoint(dom);
4634 if (!dom)
4635 goto error;
4637 if (isl_set_fast_is_empty(dom)) {
4638 res = isl_map_empty_like_basic_map(bmap);
4639 *empty = isl_set_empty_like(dom);
4640 isl_set_free(dom);
4641 isl_basic_map_free(bmap);
4642 return res;
4645 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4646 isl_basic_set_copy(dom->p[0]), empty, max);
4648 for (i = 1; i < dom->n; ++i) {
4649 struct isl_map *res_i;
4650 struct isl_set *empty_i;
4652 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4653 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4655 res = isl_map_union_disjoint(res, res_i);
4656 *empty = isl_set_union_disjoint(*empty, empty_i);
4659 isl_set_free(dom);
4660 isl_basic_map_free(bmap);
4661 return res;
4662 error:
4663 *empty = NULL;
4664 isl_set_free(dom);
4665 isl_basic_map_free(bmap);
4666 return NULL;
4669 /* Given a map "map", compute the lexicographically minimal
4670 * (or maximal) image element for each domain element in dom.
4671 * Set *empty to those elements in dom that do not have an image element.
4673 * We first compute the lexicographically minimal or maximal element
4674 * in the first basic map. This results in a partial solution "res"
4675 * and a subset "todo" of dom that still need to be handled.
4676 * We then consider each of the remaining maps in "map" and successively
4677 * improve both "res" and "todo".
4679 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4680 * Assume we are computing the lexicographical maximum.
4681 * We first compute the lexicographically maximal element in basic map i.
4682 * This results in a partial solution res_i and a subset todo_i.
4683 * Then we combine these results with those obtain for the first k basic maps
4684 * to obtain a result that is valid for the first k+1 basic maps.
4685 * In particular, the set where there is no solution is the set where
4686 * there is no solution for the first k basic maps and also no solution
4687 * for the ith basic map, i.e.,
4689 * todo^i = todo^k * todo_i
4691 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4692 * solutions, arbitrarily breaking ties in favor of res^k.
4693 * That is, when res^k(a) >= res_i(a), we pick res^k and
4694 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4695 * the lexicographic order.)
4696 * In practice, we compute
4698 * res^k * (res_i . "<=")
4700 * and
4702 * res_i * (res^k . "<")
4704 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4705 * where only one of res^k and res_i provides a solution and we simply pick
4706 * that one, i.e.,
4708 * res^k * todo_i
4709 * and
4710 * res_i * todo^k
4712 * Note that we only compute these intersections when dom(res^k) intersects
4713 * dom(res_i). Otherwise, the only effect of these intersections is to
4714 * potentially break up res^k and res_i into smaller pieces.
4715 * We want to avoid such splintering as much as possible.
4716 * In fact, an earlier implementation of this function would look for
4717 * better results in the domain of res^k and for extra results in todo^k,
4718 * but this would always result in a splintering according to todo^k,
4719 * even when the domain of basic map i is disjoint from the domains of
4720 * the previous basic maps.
4722 static __isl_give isl_map *isl_map_partial_lexopt(
4723 __isl_take isl_map *map, __isl_take isl_set *dom,
4724 __isl_give isl_set **empty, int max)
4726 int i;
4727 struct isl_map *res;
4728 struct isl_set *todo;
4730 if (!map || !dom)
4731 goto error;
4733 if (isl_map_fast_is_empty(map)) {
4734 if (empty)
4735 *empty = dom;
4736 else
4737 isl_set_free(dom);
4738 return map;
4741 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4742 isl_set_copy(dom), &todo, max);
4744 for (i = 1; i < map->n; ++i) {
4745 isl_map *lt, *le;
4746 isl_map *res_i;
4747 isl_set *todo_i;
4748 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4750 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4751 isl_set_copy(dom), &todo_i, max);
4753 if (max) {
4754 lt = isl_map_lex_lt(isl_dim_copy(dim));
4755 le = isl_map_lex_le(dim);
4756 } else {
4757 lt = isl_map_lex_gt(isl_dim_copy(dim));
4758 le = isl_map_lex_ge(dim);
4760 lt = isl_map_apply_range(isl_map_copy(res), lt);
4761 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4762 le = isl_map_apply_range(isl_map_copy(res_i), le);
4763 le = isl_map_intersect(le, isl_map_copy(res));
4765 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4766 res = isl_map_intersect_domain(res,
4767 isl_set_copy(todo_i));
4768 res_i = isl_map_intersect_domain(res_i,
4769 isl_set_copy(todo));
4772 res = isl_map_union_disjoint(res, res_i);
4773 res = isl_map_union_disjoint(res, lt);
4774 res = isl_map_union_disjoint(res, le);
4776 todo = isl_set_intersect(todo, todo_i);
4779 isl_set_free(dom);
4780 isl_map_free(map);
4782 if (empty)
4783 *empty = todo;
4784 else
4785 isl_set_free(todo);
4787 return res;
4788 error:
4789 if (empty)
4790 *empty = NULL;
4791 isl_set_free(dom);
4792 isl_map_free(map);
4793 return NULL;
4796 __isl_give isl_map *isl_map_partial_lexmax(
4797 __isl_take isl_map *map, __isl_take isl_set *dom,
4798 __isl_give isl_set **empty)
4800 return isl_map_partial_lexopt(map, dom, empty, 1);
4803 __isl_give isl_map *isl_map_partial_lexmin(
4804 __isl_take isl_map *map, __isl_take isl_set *dom,
4805 __isl_give isl_set **empty)
4807 return isl_map_partial_lexopt(map, dom, empty, 0);
4810 __isl_give isl_set *isl_set_partial_lexmin(
4811 __isl_take isl_set *set, __isl_take isl_set *dom,
4812 __isl_give isl_set **empty)
4814 return (struct isl_set *)
4815 isl_map_partial_lexmin((struct isl_map *)set,
4816 dom, empty);
4819 __isl_give isl_set *isl_set_partial_lexmax(
4820 __isl_take isl_set *set, __isl_take isl_set *dom,
4821 __isl_give isl_set **empty)
4823 return (struct isl_set *)
4824 isl_map_partial_lexmax((struct isl_map *)set,
4825 dom, empty);
4828 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4830 struct isl_basic_set *dom = NULL;
4831 struct isl_dim *dom_dim;
4833 if (!bmap)
4834 goto error;
4835 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4836 dom = isl_basic_set_universe(dom_dim);
4837 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4838 error:
4839 isl_basic_map_free(bmap);
4840 return NULL;
4843 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4845 return isl_basic_map_lexopt(bmap, 0);
4848 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4850 return isl_basic_map_lexopt(bmap, 1);
4853 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4855 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4858 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4860 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4863 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4865 struct isl_set *dom = NULL;
4866 struct isl_dim *dom_dim;
4868 if (!map)
4869 goto error;
4870 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4871 dom = isl_set_universe(dom_dim);
4872 return isl_map_partial_lexopt(map, dom, NULL, max);
4873 error:
4874 isl_map_free(map);
4875 return NULL;
4878 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4880 return isl_map_lexopt(map, 0);
4883 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4885 return isl_map_lexopt(map, 1);
4888 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4890 return (isl_set *)isl_map_lexmin((isl_map *)set);
4893 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4895 return (isl_set *)isl_map_lexmax((isl_map *)set);
4898 /* Apply a preimage specified by "mat" on the parameters of "bset".
4899 * bset is assumed to have only parameters and divs.
4901 static struct isl_basic_set *basic_set_parameter_preimage(
4902 struct isl_basic_set *bset, struct isl_mat *mat)
4904 unsigned nparam;
4906 if (!bset || !mat)
4907 goto error;
4909 bset->dim = isl_dim_cow(bset->dim);
4910 if (!bset->dim)
4911 goto error;
4913 nparam = isl_basic_set_dim(bset, isl_dim_param);
4915 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4917 bset->dim->nparam = 0;
4918 bset->dim->n_out = nparam;
4919 bset = isl_basic_set_preimage(bset, mat);
4920 if (bset) {
4921 bset->dim->nparam = bset->dim->n_out;
4922 bset->dim->n_out = 0;
4924 return bset;
4925 error:
4926 isl_mat_free(mat);
4927 isl_basic_set_free(bset);
4928 return NULL;
4931 /* Apply a preimage specified by "mat" on the parameters of "set".
4932 * set is assumed to have only parameters and divs.
4934 static struct isl_set *set_parameter_preimage(
4935 struct isl_set *set, struct isl_mat *mat)
4937 struct isl_dim *dim = NULL;
4938 unsigned nparam;
4940 if (!set || !mat)
4941 goto error;
4943 dim = isl_dim_copy(set->dim);
4944 dim = isl_dim_cow(dim);
4945 if (!dim)
4946 goto error;
4948 nparam = isl_set_dim(set, isl_dim_param);
4950 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4952 dim->nparam = 0;
4953 dim->n_out = nparam;
4954 isl_set_reset_dim(set, dim);
4955 set = isl_set_preimage(set, mat);
4956 if (!set)
4957 goto error2;
4958 dim = isl_dim_copy(set->dim);
4959 dim = isl_dim_cow(dim);
4960 if (!dim)
4961 goto error2;
4962 dim->nparam = dim->n_out;
4963 dim->n_out = 0;
4964 isl_set_reset_dim(set, dim);
4965 return set;
4966 error:
4967 isl_dim_free(dim);
4968 isl_mat_free(mat);
4969 error2:
4970 isl_set_free(set);
4971 return NULL;
4974 /* Intersect the basic set "bset" with the affine space specified by the
4975 * equalities in "eq".
4977 static struct isl_basic_set *basic_set_append_equalities(
4978 struct isl_basic_set *bset, struct isl_mat *eq)
4980 int i, k;
4981 unsigned len;
4983 if (!bset || !eq)
4984 goto error;
4986 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4987 eq->n_row, 0);
4988 if (!bset)
4989 goto error;
4991 len = 1 + isl_dim_total(bset->dim) + bset->extra;
4992 for (i = 0; i < eq->n_row; ++i) {
4993 k = isl_basic_set_alloc_equality(bset);
4994 if (k < 0)
4995 goto error;
4996 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4997 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4999 isl_mat_free(eq);
5001 bset = isl_basic_set_gauss(bset, NULL);
5002 bset = isl_basic_set_finalize(bset);
5004 return bset;
5005 error:
5006 isl_mat_free(eq);
5007 isl_basic_set_free(bset);
5008 return NULL;
5011 /* Intersect the set "set" with the affine space specified by the
5012 * equalities in "eq".
5014 static struct isl_set *set_append_equalities(struct isl_set *set,
5015 struct isl_mat *eq)
5017 int i;
5019 if (!set || !eq)
5020 goto error;
5022 for (i = 0; i < set->n; ++i) {
5023 set->p[i] = basic_set_append_equalities(set->p[i],
5024 isl_mat_copy(eq));
5025 if (!set->p[i])
5026 goto error;
5028 isl_mat_free(eq);
5029 return set;
5030 error:
5031 isl_mat_free(eq);
5032 isl_set_free(set);
5033 return NULL;
5036 /* Project the given basic set onto its parameter domain, possibly introducing
5037 * new, explicit, existential variables in the constraints.
5038 * The input has parameters and (possibly implicit) existential variables.
5039 * The output has the same parameters, but only
5040 * explicit existentially quantified variables.
5042 * The actual projection is performed by pip, but pip doesn't seem
5043 * to like equalities very much, so we first remove the equalities
5044 * among the parameters by performing a variable compression on
5045 * the parameters. Afterward, an inverse transformation is performed
5046 * and the equalities among the parameters are inserted back in.
5048 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5050 int i, j;
5051 struct isl_mat *eq;
5052 struct isl_mat *T, *T2;
5053 struct isl_set *set;
5054 unsigned nparam, n_div;
5056 bset = isl_basic_set_cow(bset);
5057 if (!bset)
5058 return NULL;
5060 if (bset->n_eq == 0)
5061 return isl_basic_set_lexmin(bset);
5063 isl_basic_set_gauss(bset, NULL);
5065 nparam = isl_basic_set_dim(bset, isl_dim_param);
5066 n_div = isl_basic_set_dim(bset, isl_dim_div);
5068 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5069 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5070 ++i;
5072 if (i == bset->n_eq)
5073 return isl_basic_set_lexmin(bset);
5075 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5076 0, 1 + nparam);
5077 eq = isl_mat_cow(eq);
5078 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5079 if (T && T->n_col == 0) {
5080 isl_mat_free(T);
5081 isl_mat_free(T2);
5082 isl_mat_free(eq);
5083 bset = isl_basic_set_set_to_empty(bset);
5084 return isl_set_from_basic_set(bset);
5086 bset = basic_set_parameter_preimage(bset, T);
5088 set = isl_basic_set_lexmin(bset);
5089 set = set_parameter_preimage(set, T2);
5090 set = set_append_equalities(set, eq);
5091 return set;
5094 /* Compute an explicit representation for all the existentially
5095 * quantified variables.
5096 * The input and output dimensions are first turned into parameters.
5097 * compute_divs then returns a map with the same parameters and
5098 * no input or output dimensions and the dimension specification
5099 * is reset to that of the input.
5101 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5103 struct isl_basic_set *bset;
5104 struct isl_set *set;
5105 struct isl_map *map;
5106 struct isl_dim *dim, *orig_dim = NULL;
5107 unsigned nparam;
5108 unsigned n_in;
5109 unsigned n_out;
5111 bmap = isl_basic_map_cow(bmap);
5112 if (!bmap)
5113 return NULL;
5115 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5116 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5117 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5118 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5119 if (!dim)
5120 goto error;
5122 orig_dim = bmap->dim;
5123 bmap->dim = dim;
5124 bset = (struct isl_basic_set *)bmap;
5126 set = parameter_compute_divs(bset);
5127 map = (struct isl_map *)set;
5128 map = isl_map_reset_dim(map, orig_dim);
5130 return map;
5131 error:
5132 isl_basic_map_free(bmap);
5133 return NULL;
5136 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5138 int i;
5139 unsigned off;
5141 if (!bmap)
5142 return -1;
5144 off = isl_dim_total(bmap->dim);
5145 for (i = 0; i < bmap->n_div; ++i) {
5146 if (isl_int_is_zero(bmap->div[i][0]))
5147 return 0;
5148 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5149 return -1);
5151 return 1;
5154 static int map_divs_known(__isl_keep isl_map *map)
5156 int i;
5158 if (!map)
5159 return -1;
5161 for (i = 0; i < map->n; ++i) {
5162 int known = basic_map_divs_known(map->p[i]);
5163 if (known <= 0)
5164 return known;
5167 return 1;
5170 /* If bmap contains any unknown divs, then compute explicit
5171 * expressions for them. However, this computation may be
5172 * quite expensive, so first try to remove divs that aren't
5173 * strictly needed.
5175 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5177 int i;
5178 int known;
5179 struct isl_map *map;
5181 known = basic_map_divs_known(bmap);
5182 if (known < 0)
5183 goto error;
5184 if (known)
5185 return isl_map_from_basic_map(bmap);
5187 bmap = isl_basic_map_drop_redundant_divs(bmap);
5189 known = basic_map_divs_known(bmap);
5190 if (known < 0)
5191 goto error;
5192 if (known)
5193 return isl_map_from_basic_map(bmap);
5195 map = compute_divs(bmap);
5196 return map;
5197 error:
5198 isl_basic_map_free(bmap);
5199 return NULL;
5202 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5204 int i;
5205 int known;
5206 struct isl_map *res;
5208 if (!map)
5209 return NULL;
5210 if (map->n == 0)
5211 return map;
5213 known = map_divs_known(map);
5214 if (known < 0) {
5215 isl_map_free(map);
5216 return NULL;
5218 if (known)
5219 return map;
5221 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5222 for (i = 1 ; i < map->n; ++i) {
5223 struct isl_map *r2;
5224 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5225 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5226 res = isl_map_union_disjoint(res, r2);
5227 else
5228 res = isl_map_union(res, r2);
5230 isl_map_free(map);
5232 return res;
5235 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5237 return (struct isl_set *)
5238 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5241 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5243 return (struct isl_set *)
5244 isl_map_compute_divs((struct isl_map *)set);
5247 struct isl_set *isl_map_domain(struct isl_map *map)
5249 int i;
5250 struct isl_set *set;
5252 if (!map)
5253 goto error;
5255 map = isl_map_cow(map);
5256 if (!map)
5257 return NULL;
5259 set = (struct isl_set *)map;
5260 set->dim = isl_dim_domain(set->dim);
5261 if (!set->dim)
5262 goto error;
5263 for (i = 0; i < map->n; ++i) {
5264 set->p[i] = isl_basic_map_domain(map->p[i]);
5265 if (!set->p[i])
5266 goto error;
5268 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5269 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5270 return set;
5271 error:
5272 isl_map_free(map);
5273 return NULL;
5276 struct isl_map *isl_map_union_disjoint(
5277 struct isl_map *map1, struct isl_map *map2)
5279 int i;
5280 unsigned flags = 0;
5281 struct isl_map *map = NULL;
5283 if (!map1 || !map2)
5284 goto error;
5286 if (map1->n == 0) {
5287 isl_map_free(map1);
5288 return map2;
5290 if (map2->n == 0) {
5291 isl_map_free(map2);
5292 return map1;
5295 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5297 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5298 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5299 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5301 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5302 map1->n + map2->n, flags);
5303 if (!map)
5304 goto error;
5305 for (i = 0; i < map1->n; ++i) {
5306 map = isl_map_add_basic_map(map,
5307 isl_basic_map_copy(map1->p[i]));
5308 if (!map)
5309 goto error;
5311 for (i = 0; i < map2->n; ++i) {
5312 map = isl_map_add_basic_map(map,
5313 isl_basic_map_copy(map2->p[i]));
5314 if (!map)
5315 goto error;
5317 isl_map_free(map1);
5318 isl_map_free(map2);
5319 return map;
5320 error:
5321 isl_map_free(map);
5322 isl_map_free(map1);
5323 isl_map_free(map2);
5324 return NULL;
5327 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5329 map1 = isl_map_union_disjoint(map1, map2);
5330 if (!map1)
5331 return NULL;
5332 if (map1->n > 1)
5333 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5334 return map1;
5337 struct isl_set *isl_set_union_disjoint(
5338 struct isl_set *set1, struct isl_set *set2)
5340 return (struct isl_set *)
5341 isl_map_union_disjoint(
5342 (struct isl_map *)set1, (struct isl_map *)set2);
5345 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5347 return (struct isl_set *)
5348 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5351 struct isl_map *isl_map_intersect_range(
5352 struct isl_map *map, struct isl_set *set)
5354 unsigned flags = 0;
5355 struct isl_map *result;
5356 int i, j;
5358 if (!map || !set)
5359 goto error;
5361 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5362 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5363 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5365 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5366 map->n * set->n, flags);
5367 if (!result)
5368 goto error;
5369 for (i = 0; i < map->n; ++i)
5370 for (j = 0; j < set->n; ++j) {
5371 result = isl_map_add_basic_map(result,
5372 isl_basic_map_intersect_range(
5373 isl_basic_map_copy(map->p[i]),
5374 isl_basic_set_copy(set->p[j])));
5375 if (!result)
5376 goto error;
5378 isl_map_free(map);
5379 isl_set_free(set);
5380 return result;
5381 error:
5382 isl_map_free(map);
5383 isl_set_free(set);
5384 return NULL;
5387 struct isl_map *isl_map_intersect_domain(
5388 struct isl_map *map, struct isl_set *set)
5390 return isl_map_reverse(
5391 isl_map_intersect_range(isl_map_reverse(map), set));
5394 struct isl_map *isl_map_apply_domain(
5395 struct isl_map *map1, struct isl_map *map2)
5397 if (!map1 || !map2)
5398 goto error;
5399 map1 = isl_map_reverse(map1);
5400 map1 = isl_map_apply_range(map1, map2);
5401 return isl_map_reverse(map1);
5402 error:
5403 isl_map_free(map1);
5404 isl_map_free(map2);
5405 return NULL;
5408 struct isl_map *isl_map_apply_range(
5409 struct isl_map *map1, struct isl_map *map2)
5411 struct isl_dim *dim_result;
5412 struct isl_map *result;
5413 int i, j;
5415 if (!map1 || !map2)
5416 goto error;
5418 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5419 isl_dim_copy(map2->dim));
5421 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5422 if (!result)
5423 goto error;
5424 for (i = 0; i < map1->n; ++i)
5425 for (j = 0; j < map2->n; ++j) {
5426 result = isl_map_add_basic_map(result,
5427 isl_basic_map_apply_range(
5428 isl_basic_map_copy(map1->p[i]),
5429 isl_basic_map_copy(map2->p[j])));
5430 if (!result)
5431 goto error;
5433 isl_map_free(map1);
5434 isl_map_free(map2);
5435 if (result && result->n <= 1)
5436 ISL_F_SET(result, ISL_MAP_DISJOINT);
5437 return result;
5438 error:
5439 isl_map_free(map1);
5440 isl_map_free(map2);
5441 return NULL;
5445 * returns range - domain
5447 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5449 isl_dim *dims, *target_dim;
5450 struct isl_basic_set *bset;
5451 unsigned dim;
5452 unsigned nparam;
5453 int i;
5455 if (!bmap)
5456 goto error;
5457 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5458 bmap->dim, isl_dim_out),
5459 goto error);
5460 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5461 dim = isl_basic_map_n_in(bmap);
5462 nparam = isl_basic_map_n_param(bmap);
5463 bset = isl_basic_set_from_basic_map(bmap);
5464 bset = isl_basic_set_cow(bset);
5465 dims = isl_basic_set_get_dim(bset);
5466 dims = isl_dim_add(dims, isl_dim_set, dim);
5467 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5468 bset = isl_basic_set_swap_vars(bset, 2*dim);
5469 for (i = 0; i < dim; ++i) {
5470 int j = isl_basic_map_alloc_equality(
5471 (struct isl_basic_map *)bset);
5472 if (j < 0)
5473 goto error;
5474 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5475 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5476 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5477 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5479 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5480 bset = isl_basic_set_reset_dim(bset, target_dim);
5481 return bset;
5482 error:
5483 isl_basic_map_free(bmap);
5484 return NULL;
5488 * returns range - domain
5490 struct isl_set *isl_map_deltas(struct isl_map *map)
5492 int i;
5493 isl_dim *dim;
5494 struct isl_set *result;
5496 if (!map)
5497 return NULL;
5499 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5500 map->dim, isl_dim_out),
5501 goto error);
5502 dim = isl_map_get_dim(map);
5503 dim = isl_dim_domain(dim);
5504 result = isl_set_alloc_dim(dim, map->n, 0);
5505 if (!result)
5506 goto error;
5507 for (i = 0; i < map->n; ++i)
5508 result = isl_set_add_basic_set(result,
5509 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5510 isl_map_free(map);
5511 return result;
5512 error:
5513 isl_map_free(map);
5514 return NULL;
5518 * returns [domain -> range] -> range - domain
5520 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5521 __isl_take isl_basic_map *bmap)
5523 int i, k;
5524 isl_dim *dim;
5525 isl_basic_map *domain;
5526 isl_basic_set *bset;
5527 int nparam, n;
5528 unsigned total;
5530 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5531 isl_die(bmap->ctx, isl_error_invalid,
5532 "domain and range don't match", goto error);
5534 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5535 n = isl_basic_map_dim(bmap, isl_dim_in);
5537 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5538 domain = isl_basic_map_universe(dim);
5540 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5541 bmap = isl_basic_map_apply_range(bmap, domain);
5542 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5544 total = isl_basic_map_total_dim(bmap);
5546 for (i = 0; i < n; ++i) {
5547 k = isl_basic_map_alloc_equality(bmap);
5548 if (k < 0)
5549 goto error;
5550 isl_seq_clr(bmap->eq[k], 1 + total);
5551 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5552 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5553 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5556 bmap = isl_basic_map_gauss(bmap, NULL);
5557 return isl_basic_map_finalize(bmap);
5558 error:
5559 isl_basic_map_free(bmap);
5560 return NULL;
5564 * returns [domain -> range] -> range - domain
5566 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5568 int i;
5569 isl_dim *domain_dim;
5571 if (!map)
5572 return NULL;
5574 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5575 isl_die(map->ctx, isl_error_invalid,
5576 "domain and range don't match", goto error);
5578 map = isl_map_cow(map);
5579 if (!map)
5580 return NULL;
5582 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5583 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5584 map->dim = isl_dim_join(map->dim, domain_dim);
5585 if (!map->dim)
5586 goto error;
5587 for (i = 0; i < map->n; ++i) {
5588 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5589 if (!map->p[i])
5590 goto error;
5592 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5593 return map;
5594 error:
5595 isl_map_free(map);
5596 return NULL;
5599 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5601 struct isl_basic_map *bmap;
5602 unsigned nparam;
5603 unsigned dim;
5604 int i;
5606 if (!dims)
5607 return NULL;
5609 nparam = dims->nparam;
5610 dim = dims->n_out;
5611 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5612 if (!bmap)
5613 goto error;
5615 for (i = 0; i < dim; ++i) {
5616 int j = isl_basic_map_alloc_equality(bmap);
5617 if (j < 0)
5618 goto error;
5619 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5620 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5621 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5623 return isl_basic_map_finalize(bmap);
5624 error:
5625 isl_basic_map_free(bmap);
5626 return NULL;
5629 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5631 if (!dim)
5632 return NULL;
5633 if (dim->n_in != dim->n_out)
5634 isl_die(dim->ctx, isl_error_invalid,
5635 "number of input and output dimensions needs to be "
5636 "the same", goto error);
5637 return basic_map_identity(dim);
5638 error:
5639 isl_dim_free(dim);
5640 return NULL;
5643 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5645 if (!model || !model->dim)
5646 return NULL;
5647 return isl_basic_map_identity(isl_dim_copy(model->dim));
5650 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5652 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5655 struct isl_map *isl_map_identity_like(struct isl_map *model)
5657 if (!model || !model->dim)
5658 return NULL;
5659 return isl_map_identity(isl_dim_copy(model->dim));
5662 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5664 if (!model || !model->dim)
5665 return NULL;
5666 return isl_map_identity(isl_dim_copy(model->dim));
5669 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5671 isl_dim *dim = isl_set_get_dim(set);
5672 isl_map *id;
5673 id = isl_map_identity(isl_dim_map_from_set(dim));
5674 return isl_map_intersect_range(id, set);
5677 /* Construct a basic set with all set dimensions having only non-negative
5678 * values.
5680 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5682 int i;
5683 unsigned nparam;
5684 unsigned dim;
5685 struct isl_basic_set *bset;
5687 if (!dims)
5688 return NULL;
5689 nparam = dims->nparam;
5690 dim = dims->n_out;
5691 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5692 if (!bset)
5693 return NULL;
5694 for (i = 0; i < dim; ++i) {
5695 int k = isl_basic_set_alloc_inequality(bset);
5696 if (k < 0)
5697 goto error;
5698 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5699 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5701 return bset;
5702 error:
5703 isl_basic_set_free(bset);
5704 return NULL;
5707 /* Construct the half-space x_pos >= 0.
5709 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5710 int pos)
5712 int k;
5713 isl_basic_set *nonneg;
5715 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5716 k = isl_basic_set_alloc_inequality(nonneg);
5717 if (k < 0)
5718 goto error;
5719 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5720 isl_int_set_si(nonneg->ineq[k][pos], 1);
5722 return isl_basic_set_finalize(nonneg);
5723 error:
5724 isl_basic_set_free(nonneg);
5725 return NULL;
5728 /* Construct the half-space x_pos <= -1.
5730 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5732 int k;
5733 isl_basic_set *neg;
5735 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5736 k = isl_basic_set_alloc_inequality(neg);
5737 if (k < 0)
5738 goto error;
5739 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5740 isl_int_set_si(neg->ineq[k][0], -1);
5741 isl_int_set_si(neg->ineq[k][pos], -1);
5743 return isl_basic_set_finalize(neg);
5744 error:
5745 isl_basic_set_free(neg);
5746 return NULL;
5749 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5750 enum isl_dim_type type, unsigned first, unsigned n)
5752 int i;
5753 isl_basic_set *nonneg;
5754 isl_basic_set *neg;
5756 if (!set)
5757 return NULL;
5758 if (n == 0)
5759 return set;
5761 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5763 for (i = 0; i < n; ++i) {
5764 nonneg = nonneg_halfspace(isl_set_get_dim(set),
5765 pos(set->dim, type) + first + i);
5766 neg = neg_halfspace(isl_set_get_dim(set),
5767 pos(set->dim, type) + first + i);
5769 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5772 return set;
5773 error:
5774 isl_set_free(set);
5775 return NULL;
5778 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
5779 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5780 void *user)
5782 isl_set *half;
5784 if (!set)
5785 return -1;
5786 if (isl_set_fast_is_empty(set)) {
5787 isl_set_free(set);
5788 return 0;
5790 if (first == len)
5791 return fn(set, signs, user);
5793 signs[first] = 1;
5794 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
5795 1 + first));
5796 half = isl_set_intersect(half, isl_set_copy(set));
5797 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
5798 goto error;
5800 signs[first] = -1;
5801 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
5802 1 + first));
5803 half = isl_set_intersect(half, set);
5804 return foreach_orthant(half, signs, first + 1, len, fn, user);
5805 error:
5806 isl_set_free(set);
5807 return -1;
5810 /* Call "fn" on the intersections of "set" with each of the orthants
5811 * (except for obviously empty intersections). The orthant is identified
5812 * by the signs array, with each entry having value 1 or -1 according
5813 * to the sign of the corresponding variable.
5815 int isl_set_foreach_orthant(__isl_keep isl_set *set,
5816 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5817 void *user)
5819 unsigned nparam;
5820 unsigned nvar;
5821 int *signs;
5822 int r;
5824 if (!set)
5825 return -1;
5826 if (isl_set_fast_is_empty(set))
5827 return 0;
5829 nparam = isl_set_dim(set, isl_dim_param);
5830 nvar = isl_set_dim(set, isl_dim_set);
5832 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
5834 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
5835 fn, user);
5837 free(signs);
5839 return r;
5842 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5844 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5847 int isl_basic_map_is_subset(
5848 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5850 int is_subset;
5851 struct isl_map *map1;
5852 struct isl_map *map2;
5854 if (!bmap1 || !bmap2)
5855 return -1;
5857 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5858 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5860 is_subset = isl_map_is_subset(map1, map2);
5862 isl_map_free(map1);
5863 isl_map_free(map2);
5865 return is_subset;
5868 int isl_basic_map_is_equal(
5869 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5871 int is_subset;
5873 if (!bmap1 || !bmap2)
5874 return -1;
5875 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5876 if (is_subset != 1)
5877 return is_subset;
5878 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5879 return is_subset;
5882 int isl_basic_set_is_equal(
5883 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5885 return isl_basic_map_is_equal(
5886 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
5889 int isl_map_is_empty(struct isl_map *map)
5891 int i;
5892 int is_empty;
5894 if (!map)
5895 return -1;
5896 for (i = 0; i < map->n; ++i) {
5897 is_empty = isl_basic_map_is_empty(map->p[i]);
5898 if (is_empty < 0)
5899 return -1;
5900 if (!is_empty)
5901 return 0;
5903 return 1;
5906 int isl_map_fast_is_empty(struct isl_map *map)
5908 return map ? map->n == 0 : -1;
5911 int isl_set_fast_is_empty(struct isl_set *set)
5913 return set ? set->n == 0 : -1;
5916 int isl_set_is_empty(struct isl_set *set)
5918 return isl_map_is_empty((struct isl_map *)set);
5921 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
5923 if (!map1 || !map2)
5924 return -1;
5926 return isl_dim_equal(map1->dim, map2->dim);
5929 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5931 if (!set1 || !set2)
5932 return -1;
5934 return isl_dim_equal(set1->dim, set2->dim);
5937 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
5939 int is_subset;
5941 if (!map1 || !map2)
5942 return -1;
5943 is_subset = isl_map_is_subset(map1, map2);
5944 if (is_subset != 1)
5945 return is_subset;
5946 is_subset = isl_map_is_subset(map2, map1);
5947 return is_subset;
5950 int isl_basic_map_is_strict_subset(
5951 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5953 int is_subset;
5955 if (!bmap1 || !bmap2)
5956 return -1;
5957 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5958 if (is_subset != 1)
5959 return is_subset;
5960 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5961 if (is_subset == -1)
5962 return is_subset;
5963 return !is_subset;
5966 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
5968 int is_subset;
5970 if (!map1 || !map2)
5971 return -1;
5972 is_subset = isl_map_is_subset(map1, map2);
5973 if (is_subset != 1)
5974 return is_subset;
5975 is_subset = isl_map_is_subset(map2, map1);
5976 if (is_subset == -1)
5977 return is_subset;
5978 return !is_subset;
5981 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5983 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5986 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5988 if (!bmap)
5989 return -1;
5990 return bmap->n_eq == 0 && bmap->n_ineq == 0;
5993 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5995 if (!bset)
5996 return -1;
5997 return bset->n_eq == 0 && bset->n_ineq == 0;
6000 int isl_map_fast_is_universe(__isl_keep isl_map *map)
6002 int i;
6004 if (!map)
6005 return -1;
6007 for (i = 0; i < map->n; ++i) {
6008 int r = isl_basic_map_is_universe(map->p[i]);
6009 if (r < 0 || r)
6010 return r;
6013 return 0;
6016 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6018 return isl_map_fast_is_universe((isl_map *) set);
6021 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6023 struct isl_basic_set *bset = NULL;
6024 struct isl_vec *sample = NULL;
6025 int empty;
6026 unsigned total;
6028 if (!bmap)
6029 return -1;
6031 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6032 return 1;
6034 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6035 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6036 copy = isl_basic_map_remove_redundancies(copy);
6037 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6038 isl_basic_map_free(copy);
6039 return empty;
6042 total = 1 + isl_basic_map_total_dim(bmap);
6043 if (bmap->sample && bmap->sample->size == total) {
6044 int contains = isl_basic_map_contains(bmap, bmap->sample);
6045 if (contains < 0)
6046 return -1;
6047 if (contains)
6048 return 0;
6050 isl_vec_free(bmap->sample);
6051 bmap->sample = NULL;
6052 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6053 if (!bset)
6054 return -1;
6055 sample = isl_basic_set_sample_vec(bset);
6056 if (!sample)
6057 return -1;
6058 empty = sample->size == 0;
6059 isl_vec_free(bmap->sample);
6060 bmap->sample = sample;
6061 if (empty)
6062 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6064 return empty;
6067 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
6069 if (!bmap)
6070 return -1;
6071 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6074 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
6076 if (!bset)
6077 return -1;
6078 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6081 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6083 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6086 struct isl_map *isl_basic_map_union(
6087 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6089 struct isl_map *map;
6090 if (!bmap1 || !bmap2)
6091 return NULL;
6093 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6095 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6096 if (!map)
6097 goto error;
6098 map = isl_map_add_basic_map(map, bmap1);
6099 map = isl_map_add_basic_map(map, bmap2);
6100 return map;
6101 error:
6102 isl_basic_map_free(bmap1);
6103 isl_basic_map_free(bmap2);
6104 return NULL;
6107 struct isl_set *isl_basic_set_union(
6108 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6110 return (struct isl_set *)isl_basic_map_union(
6111 (struct isl_basic_map *)bset1,
6112 (struct isl_basic_map *)bset2);
6115 /* Order divs such that any div only depends on previous divs */
6116 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6118 int i;
6119 unsigned off;
6121 if (!bmap)
6122 return NULL;
6124 off = isl_dim_total(bmap->dim);
6126 for (i = 0; i < bmap->n_div; ++i) {
6127 int pos;
6128 if (isl_int_is_zero(bmap->div[i][0]))
6129 continue;
6130 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6131 bmap->n_div-i);
6132 if (pos == -1)
6133 continue;
6134 isl_basic_map_swap_div(bmap, i, i + pos);
6135 --i;
6137 return bmap;
6140 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6142 return (struct isl_basic_set *)
6143 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6146 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6148 int i;
6150 if (!map)
6151 return 0;
6153 for (i = 0; i < map->n; ++i) {
6154 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6155 if (!map->p[i])
6156 goto error;
6159 return map;
6160 error:
6161 isl_map_free(map);
6162 return NULL;
6165 /* Look for a div in dst that corresponds to the div "div" in src.
6166 * The divs before "div" in src and dst are assumed to be the same.
6168 * Returns -1 if no corresponding div was found and the position
6169 * of the corresponding div in dst otherwise.
6171 static int find_div(struct isl_basic_map *dst,
6172 struct isl_basic_map *src, unsigned div)
6174 int i;
6176 unsigned total = isl_dim_total(src->dim);
6178 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6179 for (i = div; i < dst->n_div; ++i)
6180 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6181 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6182 dst->n_div - div) == -1)
6183 return i;
6184 return -1;
6187 struct isl_basic_map *isl_basic_map_align_divs(
6188 struct isl_basic_map *dst, struct isl_basic_map *src)
6190 int i;
6191 unsigned total = isl_dim_total(src->dim);
6193 if (!dst || !src)
6194 goto error;
6196 if (src->n_div == 0)
6197 return dst;
6199 for (i = 0; i < src->n_div; ++i)
6200 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6202 src = isl_basic_map_order_divs(src);
6203 dst = isl_basic_map_cow(dst);
6204 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6205 src->n_div, 0, 2 * src->n_div);
6206 if (!dst)
6207 return NULL;
6208 for (i = 0; i < src->n_div; ++i) {
6209 int j = find_div(dst, src, i);
6210 if (j < 0) {
6211 j = isl_basic_map_alloc_div(dst);
6212 if (j < 0)
6213 goto error;
6214 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6215 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6216 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6217 goto error;
6219 if (j != i)
6220 isl_basic_map_swap_div(dst, i, j);
6222 return dst;
6223 error:
6224 isl_basic_map_free(dst);
6225 return NULL;
6228 struct isl_basic_set *isl_basic_set_align_divs(
6229 struct isl_basic_set *dst, struct isl_basic_set *src)
6231 return (struct isl_basic_set *)isl_basic_map_align_divs(
6232 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6235 struct isl_map *isl_map_align_divs(struct isl_map *map)
6237 int i;
6239 if (!map)
6240 return NULL;
6241 if (map->n == 0)
6242 return map;
6243 map = isl_map_compute_divs(map);
6244 map = isl_map_cow(map);
6245 if (!map)
6246 return NULL;
6248 for (i = 1; i < map->n; ++i)
6249 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6250 for (i = 1; i < map->n; ++i)
6251 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6253 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6254 return map;
6257 struct isl_set *isl_set_align_divs(struct isl_set *set)
6259 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6262 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6264 if (!set || !map)
6265 goto error;
6266 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6267 map = isl_map_intersect_domain(map, set);
6268 set = isl_map_range(map);
6269 return set;
6270 error:
6271 isl_set_free(set);
6272 isl_map_free(map);
6273 return NULL;
6276 /* There is no need to cow as removing empty parts doesn't change
6277 * the meaning of the set.
6279 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6281 int i;
6283 if (!map)
6284 return NULL;
6286 for (i = map->n-1; i >= 0; --i) {
6287 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6288 continue;
6289 isl_basic_map_free(map->p[i]);
6290 if (i != map->n-1) {
6291 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6292 map->p[i] = map->p[map->n-1];
6294 map->n--;
6297 return map;
6300 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6302 return (struct isl_set *)
6303 isl_map_remove_empty_parts((struct isl_map *)set);
6306 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6308 struct isl_basic_map *bmap;
6309 if (!map || map->n == 0)
6310 return NULL;
6311 bmap = map->p[map->n-1];
6312 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6313 return isl_basic_map_copy(bmap);
6316 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6318 return (struct isl_basic_set *)
6319 isl_map_copy_basic_map((struct isl_map *)set);
6322 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6323 __isl_keep isl_basic_map *bmap)
6325 int i;
6327 if (!map || !bmap)
6328 goto error;
6329 for (i = map->n-1; i >= 0; --i) {
6330 if (map->p[i] != bmap)
6331 continue;
6332 map = isl_map_cow(map);
6333 if (!map)
6334 goto error;
6335 isl_basic_map_free(map->p[i]);
6336 if (i != map->n-1) {
6337 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6338 map->p[i] = map->p[map->n-1];
6340 map->n--;
6341 return map;
6343 return map;
6344 error:
6345 isl_map_free(map);
6346 return NULL;
6349 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6350 struct isl_basic_set *bset)
6352 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6353 (struct isl_basic_map *)bset);
6356 /* Given two basic sets bset1 and bset2, compute the maximal difference
6357 * between the values of dimension pos in bset1 and those in bset2
6358 * for any common value of the parameters and dimensions preceding pos.
6360 static enum isl_lp_result basic_set_maximal_difference_at(
6361 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6362 int pos, isl_int *opt)
6364 struct isl_dim *dims;
6365 struct isl_basic_map *bmap1 = NULL;
6366 struct isl_basic_map *bmap2 = NULL;
6367 struct isl_ctx *ctx;
6368 struct isl_vec *obj;
6369 unsigned total;
6370 unsigned nparam;
6371 unsigned dim1, dim2;
6372 enum isl_lp_result res;
6374 if (!bset1 || !bset2)
6375 return isl_lp_error;
6377 nparam = isl_basic_set_n_param(bset1);
6378 dim1 = isl_basic_set_n_dim(bset1);
6379 dim2 = isl_basic_set_n_dim(bset2);
6380 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6381 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6382 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6383 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6384 if (!bmap1 || !bmap2)
6385 goto error;
6386 bmap1 = isl_basic_map_cow(bmap1);
6387 bmap1 = isl_basic_map_extend(bmap1, nparam,
6388 pos, (dim1 - pos) + (dim2 - pos),
6389 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6390 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6391 if (!bmap1)
6392 goto error;
6393 total = isl_basic_map_total_dim(bmap1);
6394 ctx = bmap1->ctx;
6395 obj = isl_vec_alloc(ctx, 1 + total);
6396 isl_seq_clr(obj->block.data, 1 + total);
6397 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6398 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6399 if (!obj)
6400 goto error;
6401 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6402 opt, NULL, NULL);
6403 isl_basic_map_free(bmap1);
6404 isl_vec_free(obj);
6405 return res;
6406 error:
6407 isl_basic_map_free(bmap1);
6408 isl_basic_map_free(bmap2);
6409 return isl_lp_error;
6412 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6413 * for any common value of the parameters and dimensions preceding pos
6414 * in both basic sets, the values of dimension pos in bset1 are
6415 * smaller or larger than those in bset2.
6417 * Returns
6418 * 1 if bset1 follows bset2
6419 * -1 if bset1 precedes bset2
6420 * 0 if bset1 and bset2 are incomparable
6421 * -2 if some error occurred.
6423 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6424 struct isl_basic_set *bset2, int pos)
6426 isl_int opt;
6427 enum isl_lp_result res;
6428 int cmp;
6430 isl_int_init(opt);
6432 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6434 if (res == isl_lp_empty)
6435 cmp = 0;
6436 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6437 res == isl_lp_unbounded)
6438 cmp = 1;
6439 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6440 cmp = -1;
6441 else
6442 cmp = -2;
6444 isl_int_clear(opt);
6445 return cmp;
6448 /* Given two basic sets bset1 and bset2, check whether
6449 * for any common value of the parameters and dimensions preceding pos
6450 * there is a value of dimension pos in bset1 that is larger
6451 * than a value of the same dimension in bset2.
6453 * Return
6454 * 1 if there exists such a pair
6455 * 0 if there is no such pair, but there is a pair of equal values
6456 * -1 otherwise
6457 * -2 if some error occurred.
6459 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6460 __isl_keep isl_basic_set *bset2, int pos)
6462 isl_int opt;
6463 enum isl_lp_result res;
6464 int cmp;
6466 isl_int_init(opt);
6468 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6470 if (res == isl_lp_empty)
6471 cmp = -1;
6472 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6473 res == isl_lp_unbounded)
6474 cmp = 1;
6475 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6476 cmp = -1;
6477 else if (res == isl_lp_ok)
6478 cmp = 0;
6479 else
6480 cmp = -2;
6482 isl_int_clear(opt);
6483 return cmp;
6486 /* Given two sets set1 and set2, check whether
6487 * for any common value of the parameters and dimensions preceding pos
6488 * there is a value of dimension pos in set1 that is larger
6489 * than a value of the same dimension in set2.
6491 * Return
6492 * 1 if there exists such a pair
6493 * 0 if there is no such pair, but there is a pair of equal values
6494 * -1 otherwise
6495 * -2 if some error occurred.
6497 int isl_set_follows_at(__isl_keep isl_set *set1,
6498 __isl_keep isl_set *set2, int pos)
6500 int i, j;
6501 int follows = -1;
6503 if (!set1 || !set2)
6504 return -2;
6506 for (i = 0; i < set1->n; ++i)
6507 for (j = 0; j < set2->n; ++j) {
6508 int f;
6509 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6510 if (f == 1 || f == -2)
6511 return f;
6512 if (f > follows)
6513 follows = f;
6516 return follows;
6519 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
6520 unsigned pos, isl_int *val)
6522 int i;
6523 int d;
6524 unsigned total;
6526 if (!bmap)
6527 return -1;
6528 total = isl_basic_map_total_dim(bmap);
6529 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6530 for (; d+1 > pos; --d)
6531 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6532 break;
6533 if (d != pos)
6534 continue;
6535 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6536 return 0;
6537 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6538 return 0;
6539 if (!isl_int_is_one(bmap->eq[i][1+d]))
6540 return 0;
6541 if (val)
6542 isl_int_neg(*val, bmap->eq[i][0]);
6543 return 1;
6545 return 0;
6548 static int isl_map_fast_has_fixed_var(struct isl_map *map,
6549 unsigned pos, isl_int *val)
6551 int i;
6552 isl_int v;
6553 isl_int tmp;
6554 int fixed;
6556 if (!map)
6557 return -1;
6558 if (map->n == 0)
6559 return 0;
6560 if (map->n == 1)
6561 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
6562 isl_int_init(v);
6563 isl_int_init(tmp);
6564 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
6565 for (i = 1; fixed == 1 && i < map->n; ++i) {
6566 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
6567 if (fixed == 1 && isl_int_ne(tmp, v))
6568 fixed = 0;
6570 if (val)
6571 isl_int_set(*val, v);
6572 isl_int_clear(tmp);
6573 isl_int_clear(v);
6574 return fixed;
6577 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
6578 unsigned pos, isl_int *val)
6580 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
6581 pos, val);
6584 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
6585 isl_int *val)
6587 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
6590 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
6591 enum isl_dim_type type, unsigned pos, isl_int *val)
6593 if (pos >= isl_basic_map_dim(bmap, type))
6594 return -1;
6595 return isl_basic_map_fast_has_fixed_var(bmap,
6596 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6599 int isl_map_fast_is_fixed(struct isl_map *map,
6600 enum isl_dim_type type, unsigned pos, isl_int *val)
6602 if (pos >= isl_map_dim(map, type))
6603 return -1;
6604 return isl_map_fast_has_fixed_var(map,
6605 map_offset(map, type) - 1 + pos, val);
6608 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6609 * then return this fixed value in *val.
6611 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
6612 isl_int *val)
6614 return isl_basic_set_fast_has_fixed_var(bset,
6615 isl_basic_set_n_param(bset) + dim, val);
6618 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6619 * then return this fixed value in *val.
6621 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
6623 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6626 /* Check if input variable in has fixed value and if so and if val is not NULL,
6627 * then return this fixed value in *val.
6629 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
6631 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
6634 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6635 * and if val is not NULL, then return this lower bound in *val.
6637 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
6638 unsigned dim, isl_int *val)
6640 int i, i_eq = -1, i_ineq = -1;
6641 isl_int *c;
6642 unsigned total;
6643 unsigned nparam;
6645 if (!bset)
6646 return -1;
6647 total = isl_basic_set_total_dim(bset);
6648 nparam = isl_basic_set_n_param(bset);
6649 for (i = 0; i < bset->n_eq; ++i) {
6650 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6651 continue;
6652 if (i_eq != -1)
6653 return 0;
6654 i_eq = i;
6656 for (i = 0; i < bset->n_ineq; ++i) {
6657 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6658 continue;
6659 if (i_eq != -1 || i_ineq != -1)
6660 return 0;
6661 i_ineq = i;
6663 if (i_eq == -1 && i_ineq == -1)
6664 return 0;
6665 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6666 /* The coefficient should always be one due to normalization. */
6667 if (!isl_int_is_one(c[1+nparam+dim]))
6668 return 0;
6669 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6670 return 0;
6671 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6672 total - nparam - dim - 1) != -1)
6673 return 0;
6674 if (val)
6675 isl_int_neg(*val, c[0]);
6676 return 1;
6679 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
6680 unsigned dim, isl_int *val)
6682 int i;
6683 isl_int v;
6684 isl_int tmp;
6685 int fixed;
6687 if (!set)
6688 return -1;
6689 if (set->n == 0)
6690 return 0;
6691 if (set->n == 1)
6692 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6693 dim, val);
6694 isl_int_init(v);
6695 isl_int_init(tmp);
6696 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6697 dim, &v);
6698 for (i = 1; fixed == 1 && i < set->n; ++i) {
6699 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
6700 dim, &tmp);
6701 if (fixed == 1 && isl_int_ne(tmp, v))
6702 fixed = 0;
6704 if (val)
6705 isl_int_set(*val, v);
6706 isl_int_clear(tmp);
6707 isl_int_clear(v);
6708 return fixed;
6711 struct constraint {
6712 unsigned size;
6713 isl_int *c;
6716 /* uset_gist depends on constraints without existentially quantified
6717 * variables sorting first.
6719 static int qsort_constraint_cmp(const void *p1, const void *p2)
6721 const struct constraint *c1 = (const struct constraint *)p1;
6722 const struct constraint *c2 = (const struct constraint *)p2;
6723 int l1, l2;
6724 unsigned size = isl_min(c1->size, c2->size);
6726 l1 = isl_seq_last_non_zero(c1->c, size);
6727 l2 = isl_seq_last_non_zero(c2->c, size);
6729 if (l1 != l2)
6730 return l1 - l2;
6732 return isl_seq_cmp(c1->c, c2->c, size);
6735 static struct isl_basic_map *isl_basic_map_sort_constraints(
6736 struct isl_basic_map *bmap)
6738 int i;
6739 struct constraint *c;
6740 unsigned total;
6742 if (!bmap)
6743 return NULL;
6744 total = isl_basic_map_total_dim(bmap);
6745 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6746 if (!c)
6747 goto error;
6748 for (i = 0; i < bmap->n_ineq; ++i) {
6749 c[i].size = total;
6750 c[i].c = bmap->ineq[i];
6752 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6753 for (i = 0; i < bmap->n_ineq; ++i)
6754 bmap->ineq[i] = c[i].c;
6755 free(c);
6756 return bmap;
6757 error:
6758 isl_basic_map_free(bmap);
6759 return NULL;
6762 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6763 __isl_take isl_basic_set *bset)
6765 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6766 (struct isl_basic_map *)bset);
6769 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6771 if (!bmap)
6772 return NULL;
6773 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6774 return bmap;
6775 bmap = isl_basic_map_remove_redundancies(bmap);
6776 bmap = isl_basic_map_sort_constraints(bmap);
6777 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6778 return bmap;
6781 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6783 return (struct isl_basic_set *)isl_basic_map_normalize(
6784 (struct isl_basic_map *)bset);
6787 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6788 const __isl_keep isl_basic_map *bmap2)
6790 int i, cmp;
6791 unsigned total;
6793 if (bmap1 == bmap2)
6794 return 0;
6795 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
6796 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
6797 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
6798 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6799 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6800 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6801 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6802 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6803 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6804 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6805 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6806 return 0;
6807 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6808 return 1;
6809 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6810 return -1;
6811 if (bmap1->n_eq != bmap2->n_eq)
6812 return bmap1->n_eq - bmap2->n_eq;
6813 if (bmap1->n_ineq != bmap2->n_ineq)
6814 return bmap1->n_ineq - bmap2->n_ineq;
6815 if (bmap1->n_div != bmap2->n_div)
6816 return bmap1->n_div - bmap2->n_div;
6817 total = isl_basic_map_total_dim(bmap1);
6818 for (i = 0; i < bmap1->n_eq; ++i) {
6819 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6820 if (cmp)
6821 return cmp;
6823 for (i = 0; i < bmap1->n_ineq; ++i) {
6824 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6825 if (cmp)
6826 return cmp;
6828 for (i = 0; i < bmap1->n_div; ++i) {
6829 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6830 if (cmp)
6831 return cmp;
6833 return 0;
6836 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6837 __isl_keep isl_basic_map *bmap2)
6839 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6842 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6843 __isl_keep isl_basic_set *bset2)
6845 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6846 (isl_basic_map *)bset2);
6849 static int qsort_bmap_cmp(const void *p1, const void *p2)
6851 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6852 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6854 return isl_basic_map_fast_cmp(bmap1, bmap2);
6857 /* We normalize in place, but if anything goes wrong we need
6858 * to return NULL, so we need to make sure we don't change the
6859 * meaning of any possible other copies of map.
6861 struct isl_map *isl_map_normalize(struct isl_map *map)
6863 int i, j;
6864 struct isl_basic_map *bmap;
6866 if (!map)
6867 return NULL;
6868 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6869 return map;
6870 for (i = 0; i < map->n; ++i) {
6871 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6872 if (!bmap)
6873 goto error;
6874 isl_basic_map_free(map->p[i]);
6875 map->p[i] = bmap;
6877 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6878 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6879 map = isl_map_remove_empty_parts(map);
6880 if (!map)
6881 return NULL;
6882 for (i = map->n - 1; i >= 1; --i) {
6883 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6884 continue;
6885 isl_basic_map_free(map->p[i-1]);
6886 for (j = i; j < map->n; ++j)
6887 map->p[j-1] = map->p[j];
6888 map->n--;
6890 return map;
6891 error:
6892 isl_map_free(map);
6893 return NULL;
6897 struct isl_set *isl_set_normalize(struct isl_set *set)
6899 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
6902 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
6904 int i;
6905 int equal;
6907 if (!map1 || !map2)
6908 return -1;
6910 if (map1 == map2)
6911 return 1;
6912 if (!isl_dim_equal(map1->dim, map2->dim))
6913 return 0;
6915 map1 = isl_map_copy(map1);
6916 map2 = isl_map_copy(map2);
6917 map1 = isl_map_normalize(map1);
6918 map2 = isl_map_normalize(map2);
6919 if (!map1 || !map2)
6920 goto error;
6921 equal = map1->n == map2->n;
6922 for (i = 0; equal && i < map1->n; ++i) {
6923 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
6924 if (equal < 0)
6925 goto error;
6927 isl_map_free(map1);
6928 isl_map_free(map2);
6929 return equal;
6930 error:
6931 isl_map_free(map1);
6932 isl_map_free(map2);
6933 return -1;
6936 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
6938 return isl_map_fast_is_equal((struct isl_map *)set1,
6939 (struct isl_map *)set2);
6942 /* Return an interval that ranges from min to max (inclusive)
6944 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
6945 isl_int min, isl_int max)
6947 int k;
6948 struct isl_basic_set *bset = NULL;
6950 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
6951 if (!bset)
6952 goto error;
6954 k = isl_basic_set_alloc_inequality(bset);
6955 if (k < 0)
6956 goto error;
6957 isl_int_set_si(bset->ineq[k][1], 1);
6958 isl_int_neg(bset->ineq[k][0], min);
6960 k = isl_basic_set_alloc_inequality(bset);
6961 if (k < 0)
6962 goto error;
6963 isl_int_set_si(bset->ineq[k][1], -1);
6964 isl_int_set(bset->ineq[k][0], max);
6966 return bset;
6967 error:
6968 isl_basic_set_free(bset);
6969 return NULL;
6972 /* Return the Cartesian product of the basic sets in list (in the given order).
6974 __isl_give isl_basic_set *isl_basic_set_list_product(
6975 __isl_take struct isl_basic_set_list *list)
6977 int i;
6978 unsigned dim;
6979 unsigned nparam;
6980 unsigned extra;
6981 unsigned n_eq;
6982 unsigned n_ineq;
6983 struct isl_basic_set *product = NULL;
6985 if (!list)
6986 goto error;
6987 isl_assert(list->ctx, list->n > 0, goto error);
6988 isl_assert(list->ctx, list->p[0], goto error);
6989 nparam = isl_basic_set_n_param(list->p[0]);
6990 dim = isl_basic_set_n_dim(list->p[0]);
6991 extra = list->p[0]->n_div;
6992 n_eq = list->p[0]->n_eq;
6993 n_ineq = list->p[0]->n_ineq;
6994 for (i = 1; i < list->n; ++i) {
6995 isl_assert(list->ctx, list->p[i], goto error);
6996 isl_assert(list->ctx,
6997 nparam == isl_basic_set_n_param(list->p[i]), goto error);
6998 dim += isl_basic_set_n_dim(list->p[i]);
6999 extra += list->p[i]->n_div;
7000 n_eq += list->p[i]->n_eq;
7001 n_ineq += list->p[i]->n_ineq;
7003 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7004 n_eq, n_ineq);
7005 if (!product)
7006 goto error;
7007 dim = 0;
7008 for (i = 0; i < list->n; ++i) {
7009 isl_basic_set_add_constraints(product,
7010 isl_basic_set_copy(list->p[i]), dim);
7011 dim += isl_basic_set_n_dim(list->p[i]);
7013 isl_basic_set_list_free(list);
7014 return product;
7015 error:
7016 isl_basic_set_free(product);
7017 isl_basic_set_list_free(list);
7018 return NULL;
7021 struct isl_basic_map *isl_basic_map_product(
7022 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7024 struct isl_dim *dim_result = NULL;
7025 struct isl_basic_map *bmap;
7026 unsigned in1, in2, out1, out2, nparam, total, pos;
7027 struct isl_dim_map *dim_map1, *dim_map2;
7029 if (!bmap1 || !bmap2)
7030 goto error;
7032 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7033 bmap2->dim, isl_dim_param), goto error);
7034 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7035 isl_dim_copy(bmap2->dim));
7037 in1 = isl_basic_map_n_in(bmap1);
7038 in2 = isl_basic_map_n_in(bmap2);
7039 out1 = isl_basic_map_n_out(bmap1);
7040 out2 = isl_basic_map_n_out(bmap2);
7041 nparam = isl_basic_map_n_param(bmap1);
7043 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7044 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7045 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7046 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7047 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7048 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7049 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7050 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7051 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7052 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7053 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7055 bmap = isl_basic_map_alloc_dim(dim_result,
7056 bmap1->n_div + bmap2->n_div,
7057 bmap1->n_eq + bmap2->n_eq,
7058 bmap1->n_ineq + bmap2->n_ineq);
7059 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7060 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7061 bmap = isl_basic_map_simplify(bmap);
7062 return isl_basic_map_finalize(bmap);
7063 error:
7064 isl_basic_map_free(bmap1);
7065 isl_basic_map_free(bmap2);
7066 return NULL;
7069 __isl_give isl_basic_map *isl_basic_map_flat_product(
7070 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7072 isl_basic_map *prod;
7074 prod = isl_basic_map_product(bmap1, bmap2);
7075 prod = isl_basic_map_flatten(prod);
7076 return prod;
7079 __isl_give isl_basic_set *isl_basic_set_flat_product(
7080 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7082 return isl_basic_map_flat_product(bset1, bset2);
7085 __isl_give isl_basic_map *isl_basic_map_range_product(
7086 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7088 isl_dim *dim_result = NULL;
7089 isl_basic_map *bmap;
7090 unsigned in, out1, out2, nparam, total, pos;
7091 struct isl_dim_map *dim_map1, *dim_map2;
7093 if (!bmap1 || !bmap2)
7094 goto error;
7096 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7097 isl_dim_copy(bmap2->dim));
7099 in = isl_basic_map_dim(bmap1, isl_dim_in);
7100 out1 = isl_basic_map_n_out(bmap1);
7101 out2 = isl_basic_map_n_out(bmap2);
7102 nparam = isl_basic_map_n_param(bmap1);
7104 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7105 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7106 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7107 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7108 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7109 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7110 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7111 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7112 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7113 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7114 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7116 bmap = isl_basic_map_alloc_dim(dim_result,
7117 bmap1->n_div + bmap2->n_div,
7118 bmap1->n_eq + bmap2->n_eq,
7119 bmap1->n_ineq + bmap2->n_ineq);
7120 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7121 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7122 bmap = isl_basic_map_simplify(bmap);
7123 return isl_basic_map_finalize(bmap);
7124 error:
7125 isl_basic_map_free(bmap1);
7126 isl_basic_map_free(bmap2);
7127 return NULL;
7130 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7131 __isl_take isl_map *map2,
7132 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7133 __isl_take isl_dim *right),
7134 __isl_give isl_basic_map *(*basic_map_product)(
7135 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7137 unsigned flags = 0;
7138 struct isl_map *result;
7139 int i, j;
7141 if (!map1 || !map2)
7142 goto error;
7144 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7145 map2->dim, isl_dim_param), goto error);
7147 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7148 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7149 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7151 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7152 isl_dim_copy(map2->dim)),
7153 map1->n * map2->n, flags);
7154 if (!result)
7155 goto error;
7156 for (i = 0; i < map1->n; ++i)
7157 for (j = 0; j < map2->n; ++j) {
7158 struct isl_basic_map *part;
7159 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7160 isl_basic_map_copy(map2->p[j]));
7161 if (isl_basic_map_is_empty(part))
7162 isl_basic_map_free(part);
7163 else
7164 result = isl_map_add_basic_map(result, part);
7165 if (!result)
7166 goto error;
7168 isl_map_free(map1);
7169 isl_map_free(map2);
7170 return result;
7171 error:
7172 isl_map_free(map1);
7173 isl_map_free(map2);
7174 return NULL;
7177 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7179 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7181 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7184 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7186 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7187 __isl_take isl_map *map2)
7189 isl_map *prod;
7191 prod = isl_map_product(map1, map2);
7192 prod = isl_map_flatten(prod);
7193 return prod;
7196 /* Given two set A and B, construct its Cartesian product A x B.
7198 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7200 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7201 (struct isl_map *)set2);
7204 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7205 __isl_take isl_set *set2)
7207 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7210 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7212 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7213 __isl_take isl_map *map2)
7215 return map_product(map1, map2, &isl_dim_range_product,
7216 &isl_basic_map_range_product);
7219 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7221 int i;
7222 uint32_t hash = isl_hash_init();
7223 unsigned total;
7225 if (!bmap)
7226 return 0;
7227 bmap = isl_basic_map_copy(bmap);
7228 bmap = isl_basic_map_normalize(bmap);
7229 if (!bmap)
7230 return 0;
7231 total = isl_basic_map_total_dim(bmap);
7232 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7233 for (i = 0; i < bmap->n_eq; ++i) {
7234 uint32_t c_hash;
7235 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7236 isl_hash_hash(hash, c_hash);
7238 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7239 for (i = 0; i < bmap->n_ineq; ++i) {
7240 uint32_t c_hash;
7241 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7242 isl_hash_hash(hash, c_hash);
7244 isl_hash_byte(hash, bmap->n_div & 0xFF);
7245 for (i = 0; i < bmap->n_div; ++i) {
7246 uint32_t c_hash;
7247 if (isl_int_is_zero(bmap->div[i][0]))
7248 continue;
7249 isl_hash_byte(hash, i & 0xFF);
7250 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7251 isl_hash_hash(hash, c_hash);
7253 isl_basic_map_free(bmap);
7254 return hash;
7257 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7259 return isl_basic_map_get_hash((isl_basic_map *)bset);
7262 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7264 int i;
7265 uint32_t hash;
7267 if (!map)
7268 return 0;
7269 map = isl_map_copy(map);
7270 map = isl_map_normalize(map);
7271 if (!map)
7272 return 0;
7274 hash = isl_hash_init();
7275 for (i = 0; i < map->n; ++i) {
7276 uint32_t bmap_hash;
7277 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7278 isl_hash_hash(hash, bmap_hash);
7281 isl_map_free(map);
7283 return hash;
7286 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7288 return isl_map_get_hash((isl_map *)set);
7291 /* Check if the value for dimension dim is completely determined
7292 * by the values of the other parameters and variables.
7293 * That is, check if dimension dim is involved in an equality.
7295 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7297 int i;
7298 unsigned nparam;
7300 if (!bset)
7301 return -1;
7302 nparam = isl_basic_set_n_param(bset);
7303 for (i = 0; i < bset->n_eq; ++i)
7304 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7305 return 1;
7306 return 0;
7309 /* Check if the value for dimension dim is completely determined
7310 * by the values of the other parameters and variables.
7311 * That is, check if dimension dim is involved in an equality
7312 * for each of the subsets.
7314 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7316 int i;
7318 if (!set)
7319 return -1;
7320 for (i = 0; i < set->n; ++i) {
7321 int unique;
7322 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7323 if (unique != 1)
7324 return unique;
7326 return 1;
7329 int isl_set_n_basic_set(__isl_keep isl_set *set)
7331 return set ? set->n : 0;
7334 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7335 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7337 int i;
7339 if (!map)
7340 return -1;
7342 for (i = 0; i < map->n; ++i)
7343 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7344 return -1;
7346 return 0;
7349 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7350 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7352 int i;
7354 if (!set)
7355 return -1;
7357 for (i = 0; i < set->n; ++i)
7358 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7359 return -1;
7361 return 0;
7364 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7366 struct isl_dim *dim;
7368 if (!bset)
7369 return NULL;
7371 bset = isl_basic_set_cow(bset);
7372 if (!bset)
7373 return NULL;
7375 dim = isl_basic_set_get_dim(bset);
7376 dim = isl_dim_lift(dim, bset->n_div);
7377 if (!dim)
7378 goto error;
7379 isl_dim_free(bset->dim);
7380 bset->dim = dim;
7381 bset->extra -= bset->n_div;
7382 bset->n_div = 0;
7384 bset = isl_basic_set_finalize(bset);
7386 return bset;
7387 error:
7388 isl_basic_set_free(bset);
7389 return NULL;
7392 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7394 int i;
7395 struct isl_dim *dim;
7396 unsigned n_div;
7398 set = isl_set_align_divs(set);
7400 if (!set)
7401 return NULL;
7403 set = isl_set_cow(set);
7404 if (!set)
7405 return NULL;
7407 n_div = set->p[0]->n_div;
7408 dim = isl_set_get_dim(set);
7409 dim = isl_dim_lift(dim, n_div);
7410 if (!dim)
7411 goto error;
7412 isl_dim_free(set->dim);
7413 set->dim = dim;
7415 for (i = 0; i < set->n; ++i) {
7416 set->p[i] = isl_basic_set_lift(set->p[i]);
7417 if (!set->p[i])
7418 goto error;
7421 return set;
7422 error:
7423 isl_set_free(set);
7424 return NULL;
7427 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7429 struct isl_dim *dim;
7430 struct isl_basic_map *bmap;
7431 unsigned n_set;
7432 unsigned n_div;
7433 unsigned n_param;
7434 unsigned total;
7435 int i, k, l;
7437 set = isl_set_align_divs(set);
7439 if (!set)
7440 return NULL;
7442 dim = isl_set_get_dim(set);
7443 if (set->n == 0 || set->p[0]->n_div == 0) {
7444 isl_set_free(set);
7445 return isl_map_identity(isl_dim_map_from_set(dim));
7448 n_div = set->p[0]->n_div;
7449 dim = isl_dim_map_from_set(dim);
7450 n_param = isl_dim_size(dim, isl_dim_param);
7451 n_set = isl_dim_size(dim, isl_dim_in);
7452 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7453 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7454 for (i = 0; i < n_set; ++i)
7455 bmap = var_equal(bmap, i);
7457 total = n_param + n_set + n_set + n_div;
7458 for (i = 0; i < n_div; ++i) {
7459 k = isl_basic_map_alloc_inequality(bmap);
7460 if (k < 0)
7461 goto error;
7462 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7463 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7464 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7465 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7466 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7467 set->p[0]->div[i][0]);
7469 l = isl_basic_map_alloc_inequality(bmap);
7470 if (l < 0)
7471 goto error;
7472 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7473 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7474 set->p[0]->div[i][0]);
7475 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7478 isl_set_free(set);
7479 bmap = isl_basic_map_simplify(bmap);
7480 bmap = isl_basic_map_finalize(bmap);
7481 return isl_map_from_basic_map(bmap);
7482 error:
7483 isl_set_free(set);
7484 isl_basic_map_free(bmap);
7485 return NULL;
7488 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7490 unsigned dim;
7491 int size = 0;
7493 if (!bset)
7494 return -1;
7496 dim = isl_basic_set_total_dim(bset);
7497 size += bset->n_eq * (1 + dim);
7498 size += bset->n_ineq * (1 + dim);
7499 size += bset->n_div * (2 + dim);
7501 return size;
7504 int isl_set_size(__isl_keep isl_set *set)
7506 int i;
7507 int size = 0;
7509 if (!set)
7510 return -1;
7512 for (i = 0; i < set->n; ++i)
7513 size += isl_basic_set_size(set->p[i]);
7515 return size;
7518 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7519 enum isl_dim_type type, unsigned pos)
7521 int i;
7522 int lower, upper;
7524 if (!bmap)
7525 return -1;
7527 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7529 pos += isl_basic_map_offset(bmap, type);
7531 for (i = 0; i < bmap->n_eq; ++i)
7532 if (!isl_int_is_zero(bmap->eq[i][pos]))
7533 return 1;
7535 lower = upper = 0;
7536 for (i = 0; i < bmap->n_ineq; ++i) {
7537 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7538 if (sgn > 0)
7539 lower = 1;
7540 if (sgn < 0)
7541 upper = 1;
7544 return lower && upper;
7547 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7548 enum isl_dim_type type, unsigned pos)
7550 int i;
7552 if (!map)
7553 return -1;
7555 for (i = 0; i < map->n; ++i) {
7556 int bounded;
7557 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7558 if (bounded < 0 || !bounded)
7559 return bounded;
7562 return 1;
7565 /* Return 1 if the specified dim is involved in both an upper bound
7566 * and a lower bound.
7568 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7569 enum isl_dim_type type, unsigned pos)
7571 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7574 /* For each of the "n" variables starting at "first", determine
7575 * the sign of the variable and put the results in the first "n"
7576 * elements of the array "signs".
7577 * Sign
7578 * 1 means that the variable is non-negative
7579 * -1 means that the variable is non-positive
7580 * 0 means the variable attains both positive and negative values.
7582 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7583 unsigned first, unsigned n, int *signs)
7585 isl_vec *bound = NULL;
7586 struct isl_tab *tab = NULL;
7587 struct isl_tab_undo *snap;
7588 int i;
7590 if (!bset || !signs)
7591 return -1;
7593 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7594 tab = isl_tab_from_basic_set(bset);
7595 if (!bound || !tab)
7596 goto error;
7598 isl_seq_clr(bound->el, bound->size);
7599 isl_int_set_si(bound->el[0], -1);
7601 snap = isl_tab_snap(tab);
7602 for (i = 0; i < n; ++i) {
7603 int empty;
7605 isl_int_set_si(bound->el[1 + first + i], -1);
7606 if (isl_tab_add_ineq(tab, bound->el) < 0)
7607 goto error;
7608 empty = tab->empty;
7609 isl_int_set_si(bound->el[1 + first + i], 0);
7610 if (isl_tab_rollback(tab, snap) < 0)
7611 goto error;
7613 if (empty) {
7614 signs[i] = 1;
7615 continue;
7618 isl_int_set_si(bound->el[1 + first + i], 1);
7619 if (isl_tab_add_ineq(tab, bound->el) < 0)
7620 goto error;
7621 empty = tab->empty;
7622 isl_int_set_si(bound->el[1 + first + i], 0);
7623 if (isl_tab_rollback(tab, snap) < 0)
7624 goto error;
7626 signs[i] = empty ? -1 : 0;
7629 isl_tab_free(tab);
7630 isl_vec_free(bound);
7631 return 0;
7632 error:
7633 isl_tab_free(tab);
7634 isl_vec_free(bound);
7635 return -1;
7638 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7639 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7641 if (!bset || !signs)
7642 return -1;
7643 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7644 return -1);
7646 first += pos(bset->dim, type) - 1;
7647 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7650 /* Check if the given basic map is obviously single-valued.
7651 * In particular, for each output dimension, check that there is
7652 * an equality that defines the output dimension in terms of
7653 * earlier dimensions.
7655 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map *bmap)
7657 int i, j;
7658 unsigned total;
7659 unsigned n_out;
7660 unsigned o_out;
7662 if (!bmap)
7663 return -1;
7665 total = 1 + isl_basic_map_total_dim(bmap);
7666 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7667 o_out = isl_basic_map_offset(bmap, isl_dim_out);
7669 for (i = 0; i < n_out; ++i) {
7670 for (j = 0; j < bmap->n_eq; ++j) {
7671 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
7672 continue;
7673 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
7674 total - (o_out + i + 1)) == -1)
7675 break;
7677 if (j >= bmap->n_eq)
7678 return 0;
7681 return 1;
7684 /* Check if the given map is obviously single-valued.
7686 int isl_map_fast_is_single_valued(__isl_keep isl_map *map)
7688 int sv;
7690 if (!map)
7691 return -1;
7692 if (map->n == 0)
7693 return 1;
7694 if (map->n >= 2)
7695 return 0;
7697 return isl_basic_map_fast_is_single_valued(map->p[0]);
7700 /* Check if the given map is single-valued.
7701 * We simply compute
7703 * M \circ M^-1
7705 * and check if the result is a subset of the identity mapping.
7707 int isl_map_is_single_valued(__isl_keep isl_map *map)
7709 isl_dim *dim;
7710 isl_map *test;
7711 isl_map *id;
7712 int sv;
7714 sv = isl_map_fast_is_single_valued(map);
7715 if (sv < 0 || sv)
7716 return sv;
7718 test = isl_map_reverse(isl_map_copy(map));
7719 test = isl_map_apply_range(test, isl_map_copy(map));
7721 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
7722 id = isl_map_identity(dim);
7724 sv = isl_map_is_subset(test, id);
7726 isl_map_free(test);
7727 isl_map_free(id);
7729 return sv;
7732 int isl_map_is_bijective(__isl_keep isl_map *map)
7734 int sv;
7736 sv = isl_map_is_single_valued(map);
7737 if (sv < 0 || !sv)
7738 return sv;
7740 map = isl_map_copy(map);
7741 map = isl_map_reverse(map);
7742 sv = isl_map_is_single_valued(map);
7743 isl_map_free(map);
7745 return sv;
7748 int isl_set_is_singleton(__isl_keep isl_set *set)
7750 return isl_map_is_single_valued((isl_map *)set);
7753 int isl_map_is_translation(__isl_keep isl_map *map)
7755 int ok;
7756 isl_set *delta;
7758 delta = isl_map_deltas(isl_map_copy(map));
7759 ok = isl_set_is_singleton(delta);
7760 isl_set_free(delta);
7762 return ok;
7765 static int unique(isl_int *p, unsigned pos, unsigned len)
7767 if (isl_seq_first_non_zero(p, pos) != -1)
7768 return 0;
7769 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
7770 return 0;
7771 return 1;
7774 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7776 int i, j;
7777 unsigned nvar;
7778 unsigned ovar;
7780 if (!bset)
7781 return -1;
7783 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7784 return 0;
7786 nvar = isl_basic_set_dim(bset, isl_dim_set);
7787 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7788 for (j = 0; j < nvar; ++j) {
7789 int lower = 0, upper = 0;
7790 for (i = 0; i < bset->n_eq; ++i) {
7791 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7792 continue;
7793 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7794 return 0;
7795 break;
7797 if (i < bset->n_eq)
7798 continue;
7799 for (i = 0; i < bset->n_ineq; ++i) {
7800 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7801 continue;
7802 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7803 return 0;
7804 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7805 lower = 1;
7806 else
7807 upper = 1;
7809 if (!lower || !upper)
7810 return 0;
7813 return 1;
7816 int isl_set_is_box(__isl_keep isl_set *set)
7818 if (!set)
7819 return -1;
7820 if (set->n != 1)
7821 return 0;
7823 return isl_basic_set_is_box(set->p[0]);
7826 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7828 if (!bset)
7829 return -1;
7831 return isl_dim_is_wrapping(bset->dim);
7834 int isl_set_is_wrapping(__isl_keep isl_set *set)
7836 if (!set)
7837 return -1;
7839 return isl_dim_is_wrapping(set->dim);
7842 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7844 bmap = isl_basic_map_cow(bmap);
7845 if (!bmap)
7846 return NULL;
7848 bmap->dim = isl_dim_wrap(bmap->dim);
7849 if (!bmap->dim)
7850 goto error;
7852 bmap = isl_basic_map_finalize(bmap);
7854 return (isl_basic_set *)bmap;
7855 error:
7856 isl_basic_map_free(bmap);
7857 return NULL;
7860 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
7862 int i;
7864 map = isl_map_cow(map);
7865 if (!map)
7866 return NULL;
7868 for (i = 0; i < map->n; ++i) {
7869 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
7870 if (!map->p[i])
7871 goto error;
7873 map->dim = isl_dim_wrap(map->dim);
7874 if (!map->dim)
7875 goto error;
7877 return (isl_set *)map;
7878 error:
7879 isl_map_free(map);
7880 return NULL;
7883 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
7885 bset = isl_basic_set_cow(bset);
7886 if (!bset)
7887 return NULL;
7889 bset->dim = isl_dim_unwrap(bset->dim);
7890 if (!bset->dim)
7891 goto error;
7893 bset = isl_basic_set_finalize(bset);
7895 return (isl_basic_map *)bset;
7896 error:
7897 isl_basic_set_free(bset);
7898 return NULL;
7901 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
7903 int i;
7905 if (!set)
7906 return NULL;
7908 if (!isl_set_is_wrapping(set))
7909 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
7910 goto error);
7912 set = isl_set_cow(set);
7913 if (!set)
7914 return NULL;
7916 for (i = 0; i < set->n; ++i) {
7917 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
7918 if (!set->p[i])
7919 goto error;
7922 set->dim = isl_dim_unwrap(set->dim);
7923 if (!set->dim)
7924 goto error;
7926 return (isl_map *)set;
7927 error:
7928 isl_set_free(set);
7929 return NULL;
7932 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
7933 enum isl_dim_type type)
7935 if (!bmap)
7936 return NULL;
7938 if (!isl_dim_is_named_or_nested(bmap->dim, type))
7939 return bmap;
7941 bmap = isl_basic_map_cow(bmap);
7942 if (!bmap)
7943 return NULL;
7945 bmap->dim = isl_dim_reset(bmap->dim, type);
7946 if (!bmap->dim)
7947 goto error;
7949 bmap = isl_basic_map_finalize(bmap);
7951 return bmap;
7952 error:
7953 isl_basic_map_free(bmap);
7954 return NULL;
7957 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
7958 enum isl_dim_type type)
7960 int i;
7962 if (!map)
7963 return NULL;
7965 if (!isl_dim_is_named_or_nested(map->dim, type))
7966 return map;
7968 map = isl_map_cow(map);
7969 if (!map)
7970 return NULL;
7972 for (i = 0; i < map->n; ++i) {
7973 map->p[i] = isl_basic_map_reset(map->p[i], type);
7974 if (!map->p[i])
7975 goto error;
7977 map->dim = isl_dim_reset(map->dim, type);
7978 if (!map->dim)
7979 goto error;
7981 return map;
7982 error:
7983 isl_map_free(map);
7984 return NULL;
7987 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
7989 if (!bmap)
7990 return NULL;
7992 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
7993 return bmap;
7995 bmap = isl_basic_map_cow(bmap);
7996 if (!bmap)
7997 return NULL;
7999 bmap->dim = isl_dim_flatten(bmap->dim);
8000 if (!bmap->dim)
8001 goto error;
8003 bmap = isl_basic_map_finalize(bmap);
8005 return bmap;
8006 error:
8007 isl_basic_map_free(bmap);
8008 return NULL;
8011 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8013 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8016 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8018 int i;
8020 if (!map)
8021 return NULL;
8023 if (!map->dim->nested[0] && !map->dim->nested[1])
8024 return map;
8026 map = isl_map_cow(map);
8027 if (!map)
8028 return NULL;
8030 for (i = 0; i < map->n; ++i) {
8031 map->p[i] = isl_basic_map_flatten(map->p[i]);
8032 if (!map->p[i])
8033 goto error;
8035 map->dim = isl_dim_flatten(map->dim);
8036 if (!map->dim)
8037 goto error;
8039 return map;
8040 error:
8041 isl_map_free(map);
8042 return NULL;
8045 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8047 return (isl_set *)isl_map_flatten((isl_map *)set);
8050 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8052 isl_dim *dim, *flat_dim;
8053 isl_map *map;
8055 dim = isl_set_get_dim(set);
8056 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8057 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8058 map = isl_map_intersect_domain(map, set);
8060 return map;
8063 /* Reorder the dimensions of "bmap" according to the given dim_map
8064 * and set the dimension specification to "dim".
8066 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8067 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8069 isl_basic_map *res;
8071 bmap = isl_basic_map_cow(bmap);
8072 if (!bmap || !dim || !dim_map)
8073 goto error;
8075 res = isl_basic_map_alloc_dim(dim,
8076 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8077 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
8078 res = isl_basic_map_finalize(res);
8079 return res;
8080 error:
8081 free(dim_map);
8082 isl_basic_map_free(bmap);
8083 isl_dim_free(dim);
8084 return NULL;
8087 /* Reorder the dimensions of "map" according to given reordering.
8089 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8090 __isl_take isl_reordering *r)
8092 int i;
8093 struct isl_dim_map *dim_map;
8095 map = isl_map_cow(map);
8096 dim_map = isl_dim_map_from_reordering(r);
8097 if (!map || !r || !dim_map)
8098 goto error;
8100 for (i = 0; i < map->n; ++i) {
8101 struct isl_dim_map *dim_map_i;
8103 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
8105 map->p[i] = isl_basic_map_realign(map->p[i],
8106 isl_dim_copy(r->dim), dim_map_i);
8108 if (!map->p[i])
8109 goto error;
8112 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8114 isl_reordering_free(r);
8115 free(dim_map);
8116 return map;
8117 error:
8118 free(dim_map);
8119 isl_map_free(map);
8120 isl_reordering_free(r);
8121 return NULL;
8124 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8125 __isl_take isl_reordering *r)
8127 return (isl_set *)isl_map_realign((isl_map *)set, r);
8130 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8131 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8132 enum isl_dim_type c2, enum isl_dim_type c3,
8133 enum isl_dim_type c4, enum isl_dim_type c5)
8135 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8136 struct isl_mat *mat;
8137 int i, j, k;
8138 int pos;
8140 if (!bmap)
8141 return NULL;
8142 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8143 isl_basic_map_total_dim(bmap) + 1);
8144 if (!mat)
8145 return NULL;
8146 for (i = 0; i < bmap->n_eq; ++i)
8147 for (j = 0, pos = 0; j < 5; ++j) {
8148 int off = isl_basic_map_offset(bmap, c[j]);
8149 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8150 isl_int_set(mat->row[i][pos],
8151 bmap->eq[i][off + k]);
8152 ++pos;
8156 return mat;
8159 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8160 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8161 enum isl_dim_type c2, enum isl_dim_type c3,
8162 enum isl_dim_type c4, enum isl_dim_type c5)
8164 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8165 struct isl_mat *mat;
8166 int i, j, k;
8167 int pos;
8169 if (!bmap)
8170 return NULL;
8171 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8172 isl_basic_map_total_dim(bmap) + 1);
8173 if (!mat)
8174 return NULL;
8175 for (i = 0; i < bmap->n_ineq; ++i)
8176 for (j = 0, pos = 0; j < 5; ++j) {
8177 int off = isl_basic_map_offset(bmap, c[j]);
8178 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8179 isl_int_set(mat->row[i][pos],
8180 bmap->ineq[i][off + k]);
8181 ++pos;
8185 return mat;
8188 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8189 __isl_take isl_dim *dim,
8190 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8191 enum isl_dim_type c2, enum isl_dim_type c3,
8192 enum isl_dim_type c4, enum isl_dim_type c5)
8194 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8195 isl_basic_map *bmap;
8196 unsigned total;
8197 unsigned extra;
8198 int i, j, k, l;
8199 int pos;
8201 if (!dim || !eq || !ineq)
8202 goto error;
8204 if (eq->n_col != ineq->n_col)
8205 isl_die(dim->ctx, isl_error_invalid,
8206 "equalities and inequalities matrices should have "
8207 "same number of columns", goto error);
8209 total = 1 + isl_dim_total(dim);
8211 if (eq->n_col < total)
8212 isl_die(dim->ctx, isl_error_invalid,
8213 "number of columns too small", goto error);
8215 extra = eq->n_col - total;
8217 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8218 eq->n_row, ineq->n_row);
8219 if (!bmap)
8220 goto error;
8221 for (i = 0; i < extra; ++i) {
8222 k = isl_basic_map_alloc_div(bmap);
8223 if (k < 0)
8224 goto error;
8225 isl_int_set_si(bmap->div[k][0], 0);
8227 for (i = 0; i < eq->n_row; ++i) {
8228 l = isl_basic_map_alloc_equality(bmap);
8229 if (l < 0)
8230 goto error;
8231 for (j = 0, pos = 0; j < 5; ++j) {
8232 int off = isl_basic_map_offset(bmap, c[j]);
8233 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8234 isl_int_set(bmap->eq[l][off + k],
8235 eq->row[i][pos]);
8236 ++pos;
8240 for (i = 0; i < ineq->n_row; ++i) {
8241 l = isl_basic_map_alloc_inequality(bmap);
8242 if (l < 0)
8243 goto error;
8244 for (j = 0, pos = 0; j < 5; ++j) {
8245 int off = isl_basic_map_offset(bmap, c[j]);
8246 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8247 isl_int_set(bmap->ineq[l][off + k],
8248 ineq->row[i][pos]);
8249 ++pos;
8254 isl_dim_free(dim);
8255 isl_mat_free(eq);
8256 isl_mat_free(ineq);
8258 return bmap;
8259 error:
8260 isl_dim_free(dim);
8261 isl_mat_free(eq);
8262 isl_mat_free(ineq);
8263 return NULL;
8266 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8267 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8268 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8270 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8271 c1, c2, c3, c4, isl_dim_in);
8274 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8275 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8276 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8278 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8279 c1, c2, c3, c4, isl_dim_in);
8282 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8283 __isl_take isl_dim *dim,
8284 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8285 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8287 return (isl_basic_set*)
8288 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8289 c1, c2, c3, c4, isl_dim_in);
8292 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8294 if (!bmap)
8295 return -1;
8297 return isl_dim_can_zip(bmap->dim);
8300 int isl_map_can_zip(__isl_keep isl_map *map)
8302 if (!map)
8303 return -1;
8305 return isl_dim_can_zip(map->dim);
8308 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8309 * (A -> C) -> (B -> D).
8311 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8313 unsigned pos;
8314 unsigned n1;
8315 unsigned n2;
8317 if (!bmap)
8318 return NULL;
8320 if (!isl_basic_map_can_zip(bmap))
8321 isl_die(bmap->ctx, isl_error_invalid,
8322 "basic map cannot be zipped", goto error);
8323 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8324 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8325 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8326 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8327 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8328 if (!bmap)
8329 return NULL;
8330 bmap->dim = isl_dim_zip(bmap->dim);
8331 if (!bmap->dim)
8332 goto error;
8333 return bmap;
8334 error:
8335 isl_basic_map_free(bmap);
8336 return NULL;
8339 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8340 * (A -> C) -> (B -> D).
8342 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8344 int i;
8346 if (!map)
8347 return NULL;
8349 if (!isl_map_can_zip(map))
8350 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8351 goto error);
8353 map = isl_map_cow(map);
8354 if (!map)
8355 return NULL;
8357 for (i = 0; i < map->n; ++i) {
8358 map->p[i] = isl_basic_map_zip(map->p[i]);
8359 if (!map->p[i])
8360 goto error;
8363 map->dim = isl_dim_zip(map->dim);
8364 if (!map->dim)
8365 goto error;
8367 return map;
8368 error:
8369 isl_map_free(map);
8370 return NULL;