isl_schedule: don't let users get access to internal isl_band_lists
[isl.git] / isl_map.c
blob7d5151cab345d0da4ee20834d0802dcaba56853d
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <string.h>
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl/blk.h>
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
20 #include <isl/lp.h>
21 #include <isl/seq.h>
22 #include <isl/set.h>
23 #include <isl/map.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
27 #include "isl_tab.h"
28 #include <isl/vec.h>
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_private.h>
34 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
36 switch (type) {
37 case isl_dim_param: return dim->nparam;
38 case isl_dim_in: return dim->n_in;
39 case isl_dim_out: return dim->n_out;
40 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
41 default: return 0;
45 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
47 switch (type) {
48 case isl_dim_param: return 1;
49 case isl_dim_in: return 1 + dim->nparam;
50 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
51 default: return 0;
55 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
56 enum isl_dim_type type)
58 if (!bmap)
59 return 0;
60 switch (type) {
61 case isl_dim_cst: return 1;
62 case isl_dim_param:
63 case isl_dim_in:
64 case isl_dim_out: return isl_dim_size(bmap->dim, type);
65 case isl_dim_div: return bmap->n_div;
66 case isl_dim_all: return isl_basic_map_total_dim(bmap);
67 default: return 0;
71 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
73 return map ? n(map->dim, type) : 0;
76 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
78 return set ? n(set->dim, type) : 0;
81 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
82 enum isl_dim_type type)
84 struct isl_dim *dim = bmap->dim;
85 switch (type) {
86 case isl_dim_cst: return 0;
87 case isl_dim_param: return 1;
88 case isl_dim_in: return 1 + dim->nparam;
89 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
90 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
91 default: return 0;
95 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
96 enum isl_dim_type type)
98 return isl_basic_map_offset(bset, type);
101 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
103 return pos(map->dim, type);
106 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
107 enum isl_dim_type type)
109 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
112 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
114 return isl_basic_set_dim(bset, isl_dim_set);
117 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
119 return isl_basic_set_dim(bset, isl_dim_param);
122 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
124 return isl_dim_total(bset->dim) + bset->n_div;
127 unsigned isl_set_n_dim(const struct isl_set *set)
129 return isl_set_dim(set, isl_dim_set);
132 unsigned isl_set_n_param(const struct isl_set *set)
134 return isl_set_dim(set, isl_dim_param);
137 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
139 return bmap ? bmap->dim->n_in : 0;
142 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
144 return bmap ? bmap->dim->n_out : 0;
147 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
149 return bmap ? bmap->dim->nparam : 0;
152 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
154 return bmap ? bmap->n_div : 0;
157 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
159 return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
162 unsigned isl_map_n_in(const struct isl_map *map)
164 return map->dim->n_in;
167 unsigned isl_map_n_out(const struct isl_map *map)
169 return map->dim->n_out;
172 unsigned isl_map_n_param(const struct isl_map *map)
174 return map->dim->nparam;
177 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
179 int m;
180 if (!map || !set)
181 return -1;
182 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
183 if (m < 0 || !m)
184 return m;
185 return isl_dim_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
188 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
189 struct isl_basic_set *bset)
191 int m;
192 if (!bmap || !bset)
193 return -1;
194 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
195 if (m < 0 || !m)
196 return m;
197 return isl_dim_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
200 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
202 int m;
203 if (!map || !set)
204 return -1;
205 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
206 if (m < 0 || !m)
207 return m;
208 return isl_dim_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
211 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
212 struct isl_basic_set *bset)
214 int m;
215 if (!bmap || !bset)
216 return -1;
217 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
218 if (m < 0 || !m)
219 return m;
220 return isl_dim_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
223 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
225 return bmap ? bmap->ctx : NULL;
228 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
230 return bset ? bset->ctx : NULL;
233 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
235 return map ? map->ctx : NULL;
238 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
240 return set ? set->ctx : NULL;
243 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
245 if (!bmap)
246 return NULL;
247 return isl_dim_copy(bmap->dim);
250 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
252 if (!bset)
253 return NULL;
254 return isl_dim_copy(bset->dim);
257 __isl_give isl_local_space *isl_basic_map_get_local_space(
258 __isl_keep isl_basic_map *bmap)
260 int i;
261 isl_local_space *ls;
262 unsigned total;
264 if (!bmap)
265 return NULL;
267 total = isl_basic_map_total_dim(bmap);
268 ls = isl_local_space_alloc(isl_dim_copy(bmap->dim), bmap->n_div);
269 if (!ls)
270 return NULL;
272 for (i = 0; i < bmap->n_div; ++i)
273 isl_seq_cpy(ls->div->row[i], bmap->div[i], 2 + total);
275 return ls;
278 __isl_give isl_local_space *isl_basic_set_get_local_space(
279 __isl_keep isl_basic_set *bset)
281 return isl_basic_map_get_local_space(bset);
284 __isl_give isl_basic_map *isl_basic_map_from_local_space(
285 __isl_take isl_local_space *ls)
287 int i;
288 int n_div;
289 isl_basic_map *bmap;
291 if (!ls)
292 return NULL;
294 n_div = isl_local_space_dim(ls, isl_dim_div);
295 bmap = isl_basic_map_alloc_dim(isl_local_space_get_dim(ls),
296 n_div, 0, 2 * n_div);
298 for (i = 0; i < n_div; ++i)
299 if (isl_basic_map_alloc_div(bmap) < 0)
300 goto error;
302 for (i = 0; i < n_div; ++i) {
303 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
304 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
305 goto error;
308 isl_local_space_free(ls);
309 return bmap;
310 error:
311 isl_local_space_free(ls);
312 isl_basic_map_free(bmap);
313 return NULL;
316 __isl_give isl_basic_set *isl_basic_set_from_local_space(
317 __isl_take isl_local_space *ls)
319 return isl_basic_map_from_local_space(ls);
322 struct isl_dim *isl_map_get_dim(struct isl_map *map)
324 if (!map)
325 return NULL;
326 return isl_dim_copy(map->dim);
329 struct isl_dim *isl_set_get_dim(struct isl_set *set)
331 if (!set)
332 return NULL;
333 return isl_dim_copy(set->dim);
336 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
337 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
339 bmap = isl_basic_map_cow(bmap);
340 if (!bmap)
341 return NULL;
342 bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
343 if (!bmap->dim)
344 goto error;
345 bmap = isl_basic_map_finalize(bmap);
346 return bmap;
347 error:
348 isl_basic_map_free(bmap);
349 return NULL;
352 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
353 __isl_take isl_basic_set *bset, const char *s)
355 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
358 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
359 enum isl_dim_type type)
361 return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
364 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
365 enum isl_dim_type type, const char *s)
367 int i;
369 map = isl_map_cow(map);
370 if (!map)
371 return NULL;
373 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
374 if (!map->dim)
375 goto error;
377 for (i = 0; i < map->n; ++i) {
378 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
379 if (!map->p[i])
380 goto error;
383 return map;
384 error:
385 isl_map_free(map);
386 return NULL;
389 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
390 enum isl_dim_type type)
392 return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
395 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
396 const char *s)
398 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
401 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
403 return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
406 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
408 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
411 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
412 enum isl_dim_type type, unsigned pos)
414 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
417 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
418 enum isl_dim_type type, unsigned pos)
420 return bset ? isl_dim_get_name(bset->dim, type, pos) : NULL;
423 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
424 enum isl_dim_type type, unsigned pos)
426 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
429 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
430 enum isl_dim_type type, unsigned pos)
432 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
435 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
436 __isl_take isl_basic_map *bmap,
437 enum isl_dim_type type, unsigned pos, const char *s)
439 if (!bmap)
440 return NULL;
441 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
442 if (!bmap->dim)
443 goto error;
444 return bmap;
445 error:
446 isl_basic_map_free(bmap);
447 return NULL;
450 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
451 enum isl_dim_type type, unsigned pos, const char *s)
453 int i;
455 if (!map)
456 return NULL;
458 map->dim = isl_dim_set_name(map->dim, type, pos, s);
459 if (!map->dim)
460 goto error;
462 for (i = 0; i < map->n; ++i) {
463 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
464 if (!map->p[i])
465 goto error;
468 return map;
469 error:
470 isl_map_free(map);
471 return NULL;
474 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
475 __isl_take isl_basic_set *bset,
476 enum isl_dim_type type, unsigned pos, const char *s)
478 return (isl_basic_set *)isl_basic_map_set_dim_name(
479 (isl_basic_map *)bset, type, pos, s);
482 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
483 enum isl_dim_type type, unsigned pos, const char *s)
485 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
488 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
490 if (!bmap)
491 return -1;
492 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
495 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
497 return isl_basic_map_is_rational(bset);
500 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
501 struct isl_basic_map *bmap, unsigned extra,
502 unsigned n_eq, unsigned n_ineq)
504 int i;
505 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
507 bmap->ctx = ctx;
508 isl_ctx_ref(ctx);
510 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
511 if (isl_blk_is_error(bmap->block))
512 goto error;
514 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
515 if (!bmap->ineq)
516 goto error;
518 if (extra == 0) {
519 bmap->block2 = isl_blk_empty();
520 bmap->div = NULL;
521 } else {
522 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
523 if (isl_blk_is_error(bmap->block2))
524 goto error;
526 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
527 if (!bmap->div)
528 goto error;
531 for (i = 0; i < n_ineq + n_eq; ++i)
532 bmap->ineq[i] = bmap->block.data + i * row_size;
534 for (i = 0; i < extra; ++i)
535 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
537 bmap->ref = 1;
538 bmap->flags = 0;
539 bmap->c_size = n_eq + n_ineq;
540 bmap->eq = bmap->ineq + n_ineq;
541 bmap->extra = extra;
542 bmap->n_eq = 0;
543 bmap->n_ineq = 0;
544 bmap->n_div = 0;
545 bmap->sample = NULL;
547 return bmap;
548 error:
549 isl_basic_map_free(bmap);
550 return NULL;
553 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
554 unsigned nparam, unsigned dim, unsigned extra,
555 unsigned n_eq, unsigned n_ineq)
557 struct isl_basic_map *bmap;
558 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
559 return (struct isl_basic_set *)bmap;
562 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
563 unsigned extra, unsigned n_eq, unsigned n_ineq)
565 struct isl_basic_map *bmap;
566 if (!dim)
567 return NULL;
568 isl_assert(dim->ctx, dim->n_in == 0, goto error);
569 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
570 return (struct isl_basic_set *)bmap;
571 error:
572 isl_dim_free(dim);
573 return NULL;
576 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
577 unsigned extra, unsigned n_eq, unsigned n_ineq)
579 struct isl_basic_map *bmap;
581 if (!dim)
582 return NULL;
583 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
584 if (!bmap)
585 goto error;
586 bmap->dim = dim;
588 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
589 error:
590 isl_dim_free(dim);
591 return NULL;
594 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
595 unsigned nparam, unsigned in, unsigned out, unsigned extra,
596 unsigned n_eq, unsigned n_ineq)
598 struct isl_basic_map *bmap;
599 struct isl_dim *dim;
601 dim = isl_dim_alloc(ctx, nparam, in, out);
602 if (!dim)
603 return NULL;
605 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
606 return bmap;
609 static void dup_constraints(
610 struct isl_basic_map *dst, struct isl_basic_map *src)
612 int i;
613 unsigned total = isl_basic_map_total_dim(src);
615 for (i = 0; i < src->n_eq; ++i) {
616 int j = isl_basic_map_alloc_equality(dst);
617 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
620 for (i = 0; i < src->n_ineq; ++i) {
621 int j = isl_basic_map_alloc_inequality(dst);
622 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
625 for (i = 0; i < src->n_div; ++i) {
626 int j = isl_basic_map_alloc_div(dst);
627 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
629 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
632 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
634 struct isl_basic_map *dup;
636 if (!bmap)
637 return NULL;
638 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
639 bmap->n_div, bmap->n_eq, bmap->n_ineq);
640 if (!dup)
641 return NULL;
642 dup_constraints(dup, bmap);
643 dup->flags = bmap->flags;
644 dup->sample = isl_vec_copy(bmap->sample);
645 return dup;
648 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
650 struct isl_basic_map *dup;
652 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
653 return (struct isl_basic_set *)dup;
656 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
658 if (!bset)
659 return NULL;
661 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
662 bset->ref++;
663 return bset;
665 return isl_basic_set_dup(bset);
668 struct isl_set *isl_set_copy(struct isl_set *set)
670 if (!set)
671 return NULL;
673 set->ref++;
674 return set;
677 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
679 if (!bmap)
680 return NULL;
682 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
683 bmap->ref++;
684 return bmap;
686 bmap = isl_basic_map_dup(bmap);
687 if (bmap)
688 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
689 return bmap;
692 struct isl_map *isl_map_copy(struct isl_map *map)
694 if (!map)
695 return NULL;
697 map->ref++;
698 return map;
701 void isl_basic_map_free(struct isl_basic_map *bmap)
703 if (!bmap)
704 return;
706 if (--bmap->ref > 0)
707 return;
709 isl_ctx_deref(bmap->ctx);
710 free(bmap->div);
711 isl_blk_free(bmap->ctx, bmap->block2);
712 free(bmap->ineq);
713 isl_blk_free(bmap->ctx, bmap->block);
714 isl_vec_free(bmap->sample);
715 isl_dim_free(bmap->dim);
716 free(bmap);
719 void isl_basic_set_free(struct isl_basic_set *bset)
721 isl_basic_map_free((struct isl_basic_map *)bset);
724 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
726 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
729 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
731 struct isl_ctx *ctx;
732 if (!bmap)
733 return -1;
734 ctx = bmap->ctx;
735 isl_assert(ctx, room_for_con(bmap, 1), return -1);
736 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
737 return -1);
738 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
739 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
740 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
741 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
742 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
743 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
744 isl_int *t;
745 int j = isl_basic_map_alloc_inequality(bmap);
746 if (j < 0)
747 return -1;
748 t = bmap->ineq[j];
749 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
750 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
751 bmap->eq[-1] = t;
752 bmap->n_eq++;
753 bmap->n_ineq--;
754 bmap->eq--;
755 return 0;
757 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
758 bmap->extra - bmap->n_div);
759 return bmap->n_eq++;
762 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
764 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
767 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
769 if (!bmap)
770 return -1;
771 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
772 bmap->n_eq -= n;
773 return 0;
776 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
778 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
781 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
783 isl_int *t;
784 if (!bmap)
785 return -1;
786 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
788 if (pos != bmap->n_eq - 1) {
789 t = bmap->eq[pos];
790 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
791 bmap->eq[bmap->n_eq - 1] = t;
793 bmap->n_eq--;
794 return 0;
797 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
799 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
802 void isl_basic_map_inequality_to_equality(
803 struct isl_basic_map *bmap, unsigned pos)
805 isl_int *t;
807 t = bmap->ineq[pos];
808 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
809 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
810 bmap->eq[-1] = t;
811 bmap->n_eq++;
812 bmap->n_ineq--;
813 bmap->eq--;
814 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
815 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
816 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
817 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
820 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
822 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
825 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
827 struct isl_ctx *ctx;
828 if (!bmap)
829 return -1;
830 ctx = bmap->ctx;
831 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
832 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
833 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
834 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
835 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
836 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
837 1 + isl_basic_map_total_dim(bmap),
838 bmap->extra - bmap->n_div);
839 return bmap->n_ineq++;
842 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
844 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
847 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
849 if (!bmap)
850 return -1;
851 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
852 bmap->n_ineq -= n;
853 return 0;
856 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
858 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
861 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
863 isl_int *t;
864 if (!bmap)
865 return -1;
866 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
868 if (pos != bmap->n_ineq - 1) {
869 t = bmap->ineq[pos];
870 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
871 bmap->ineq[bmap->n_ineq - 1] = t;
872 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
874 bmap->n_ineq--;
875 return 0;
878 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
880 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
883 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
884 isl_int *eq)
886 int k;
888 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
889 if (!bmap)
890 return NULL;
891 k = isl_basic_map_alloc_equality(bmap);
892 if (k < 0)
893 goto error;
894 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
895 return bmap;
896 error:
897 isl_basic_map_free(bmap);
898 return NULL;
901 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
902 isl_int *eq)
904 return (isl_basic_set *)
905 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
908 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
909 isl_int *ineq)
911 int k;
913 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
914 if (!bmap)
915 return NULL;
916 k = isl_basic_map_alloc_inequality(bmap);
917 if (k < 0)
918 goto error;
919 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
920 return bmap;
921 error:
922 isl_basic_map_free(bmap);
923 return NULL;
926 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
927 isl_int *ineq)
929 return (isl_basic_set *)
930 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
933 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
935 if (!bmap)
936 return -1;
937 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
938 isl_seq_clr(bmap->div[bmap->n_div] +
939 1 + 1 + isl_basic_map_total_dim(bmap),
940 bmap->extra - bmap->n_div);
941 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
942 return bmap->n_div++;
945 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
947 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
950 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
952 if (!bmap)
953 return -1;
954 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
955 bmap->n_div -= n;
956 return 0;
959 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
961 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
964 /* Copy constraint from src to dst, putting the vars of src at offset
965 * dim_off in dst and the divs of src at offset div_off in dst.
966 * If both sets are actually map, then dim_off applies to the input
967 * variables.
969 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
970 struct isl_basic_map *src_map, isl_int *src,
971 unsigned in_off, unsigned out_off, unsigned div_off)
973 unsigned src_nparam = isl_basic_map_n_param(src_map);
974 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
975 unsigned src_in = isl_basic_map_n_in(src_map);
976 unsigned dst_in = isl_basic_map_n_in(dst_map);
977 unsigned src_out = isl_basic_map_n_out(src_map);
978 unsigned dst_out = isl_basic_map_n_out(dst_map);
979 isl_int_set(dst[0], src[0]);
980 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
981 if (dst_nparam > src_nparam)
982 isl_seq_clr(dst+1+src_nparam,
983 dst_nparam - src_nparam);
984 isl_seq_clr(dst+1+dst_nparam, in_off);
985 isl_seq_cpy(dst+1+dst_nparam+in_off,
986 src+1+src_nparam,
987 isl_min(dst_in-in_off, src_in));
988 if (dst_in-in_off > src_in)
989 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
990 dst_in - in_off - src_in);
991 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
992 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
993 src+1+src_nparam+src_in,
994 isl_min(dst_out-out_off, src_out));
995 if (dst_out-out_off > src_out)
996 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
997 dst_out - out_off - src_out);
998 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
999 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1000 src+1+src_nparam+src_in+src_out,
1001 isl_min(dst_map->extra-div_off, src_map->n_div));
1002 if (dst_map->n_div-div_off > src_map->n_div)
1003 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1004 div_off+src_map->n_div,
1005 dst_map->n_div - div_off - src_map->n_div);
1008 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1009 struct isl_basic_map *src_map, isl_int *src,
1010 unsigned in_off, unsigned out_off, unsigned div_off)
1012 isl_int_set(dst[0], src[0]);
1013 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1016 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1017 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1019 int i;
1020 unsigned div_off;
1022 if (!bmap1 || !bmap2)
1023 goto error;
1025 div_off = bmap1->n_div;
1027 for (i = 0; i < bmap2->n_eq; ++i) {
1028 int i1 = isl_basic_map_alloc_equality(bmap1);
1029 if (i1 < 0)
1030 goto error;
1031 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1032 i_pos, o_pos, div_off);
1035 for (i = 0; i < bmap2->n_ineq; ++i) {
1036 int i1 = isl_basic_map_alloc_inequality(bmap1);
1037 if (i1 < 0)
1038 goto error;
1039 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1040 i_pos, o_pos, div_off);
1043 for (i = 0; i < bmap2->n_div; ++i) {
1044 int i1 = isl_basic_map_alloc_div(bmap1);
1045 if (i1 < 0)
1046 goto error;
1047 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1048 i_pos, o_pos, div_off);
1051 isl_basic_map_free(bmap2);
1053 return bmap1;
1055 error:
1056 isl_basic_map_free(bmap1);
1057 isl_basic_map_free(bmap2);
1058 return NULL;
1061 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1062 struct isl_basic_set *bset2, unsigned pos)
1064 return (struct isl_basic_set *)
1065 add_constraints((struct isl_basic_map *)bset1,
1066 (struct isl_basic_map *)bset2, 0, pos);
1069 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1070 struct isl_dim *dim, unsigned extra,
1071 unsigned n_eq, unsigned n_ineq)
1073 struct isl_basic_map *ext;
1074 unsigned flags;
1075 int dims_ok;
1077 if (!dim)
1078 goto error;
1080 if (!base)
1081 goto error;
1083 dims_ok = isl_dim_equal(base->dim, dim) &&
1084 base->extra >= base->n_div + extra;
1086 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1087 room_for_ineq(base, n_ineq)) {
1088 isl_dim_free(dim);
1089 return base;
1092 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1093 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1094 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1095 extra += base->extra;
1096 n_eq += base->n_eq;
1097 n_ineq += base->n_ineq;
1099 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1100 dim = NULL;
1101 if (!ext)
1102 goto error;
1104 if (dims_ok)
1105 ext->sample = isl_vec_copy(base->sample);
1106 flags = base->flags;
1107 ext = add_constraints(ext, base, 0, 0);
1108 if (ext) {
1109 ext->flags = flags;
1110 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1113 return ext;
1115 error:
1116 isl_dim_free(dim);
1117 isl_basic_map_free(base);
1118 return NULL;
1121 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1122 struct isl_dim *dim, unsigned extra,
1123 unsigned n_eq, unsigned n_ineq)
1125 return (struct isl_basic_set *)
1126 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1127 extra, n_eq, n_ineq);
1130 struct isl_basic_map *isl_basic_map_extend_constraints(
1131 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1133 if (!base)
1134 return NULL;
1135 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1136 0, n_eq, n_ineq);
1139 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1140 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1141 unsigned n_eq, unsigned n_ineq)
1143 struct isl_basic_map *bmap;
1144 struct isl_dim *dim;
1146 if (!base)
1147 return NULL;
1148 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1149 if (!dim)
1150 goto error;
1152 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1153 return bmap;
1154 error:
1155 isl_basic_map_free(base);
1156 return NULL;
1159 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1160 unsigned nparam, unsigned dim, unsigned extra,
1161 unsigned n_eq, unsigned n_ineq)
1163 return (struct isl_basic_set *)
1164 isl_basic_map_extend((struct isl_basic_map *)base,
1165 nparam, 0, dim, extra, n_eq, n_ineq);
1168 struct isl_basic_set *isl_basic_set_extend_constraints(
1169 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1171 return (struct isl_basic_set *)
1172 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1173 n_eq, n_ineq);
1176 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1178 return (struct isl_basic_set *)
1179 isl_basic_map_cow((struct isl_basic_map *)bset);
1182 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1184 if (!bmap)
1185 return NULL;
1187 if (bmap->ref > 1) {
1188 bmap->ref--;
1189 bmap = isl_basic_map_dup(bmap);
1191 if (bmap)
1192 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1193 return bmap;
1196 struct isl_set *isl_set_cow(struct isl_set *set)
1198 if (!set)
1199 return NULL;
1201 if (set->ref == 1)
1202 return set;
1203 set->ref--;
1204 return isl_set_dup(set);
1207 struct isl_map *isl_map_cow(struct isl_map *map)
1209 if (!map)
1210 return NULL;
1212 if (map->ref == 1)
1213 return map;
1214 map->ref--;
1215 return isl_map_dup(map);
1218 static void swap_vars(struct isl_blk blk, isl_int *a,
1219 unsigned a_len, unsigned b_len)
1221 isl_seq_cpy(blk.data, a+a_len, b_len);
1222 isl_seq_cpy(blk.data+b_len, a, a_len);
1223 isl_seq_cpy(a, blk.data, b_len+a_len);
1226 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1227 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1229 int i;
1230 struct isl_blk blk;
1232 if (!bmap)
1233 goto error;
1235 isl_assert(bmap->ctx,
1236 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1238 if (n1 == 0 || n2 == 0)
1239 return bmap;
1241 bmap = isl_basic_map_cow(bmap);
1242 if (!bmap)
1243 return NULL;
1245 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1246 if (isl_blk_is_error(blk))
1247 goto error;
1249 for (i = 0; i < bmap->n_eq; ++i)
1250 swap_vars(blk,
1251 bmap->eq[i] + pos, n1, n2);
1253 for (i = 0; i < bmap->n_ineq; ++i)
1254 swap_vars(blk,
1255 bmap->ineq[i] + pos, n1, n2);
1257 for (i = 0; i < bmap->n_div; ++i)
1258 swap_vars(blk,
1259 bmap->div[i]+1 + pos, n1, n2);
1261 isl_blk_free(bmap->ctx, blk);
1263 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1264 bmap = isl_basic_map_gauss(bmap, NULL);
1265 return isl_basic_map_finalize(bmap);
1266 error:
1267 isl_basic_map_free(bmap);
1268 return NULL;
1271 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1272 __isl_take isl_basic_set *bset, unsigned n)
1274 unsigned dim;
1275 unsigned nparam;
1277 nparam = isl_basic_set_n_param(bset);
1278 dim = isl_basic_set_n_dim(bset);
1279 isl_assert(bset->ctx, n <= dim, goto error);
1281 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1282 error:
1283 isl_basic_set_free(bset);
1284 return NULL;
1287 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1289 int i = 0;
1290 unsigned total;
1291 if (!bmap)
1292 goto error;
1293 total = isl_basic_map_total_dim(bmap);
1294 isl_basic_map_free_div(bmap, bmap->n_div);
1295 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1296 if (bmap->n_eq > 0)
1297 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1298 else {
1299 i = isl_basic_map_alloc_equality(bmap);
1300 if (i < 0)
1301 goto error;
1303 isl_int_set_si(bmap->eq[i][0], 1);
1304 isl_seq_clr(bmap->eq[i]+1, total);
1305 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1306 isl_vec_free(bmap->sample);
1307 bmap->sample = NULL;
1308 return isl_basic_map_finalize(bmap);
1309 error:
1310 isl_basic_map_free(bmap);
1311 return NULL;
1314 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1316 return (struct isl_basic_set *)
1317 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1320 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1322 int i;
1323 unsigned off = isl_dim_total(bmap->dim);
1324 isl_int *t = bmap->div[a];
1325 bmap->div[a] = bmap->div[b];
1326 bmap->div[b] = t;
1328 for (i = 0; i < bmap->n_eq; ++i)
1329 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1331 for (i = 0; i < bmap->n_ineq; ++i)
1332 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1334 for (i = 0; i < bmap->n_div; ++i)
1335 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1336 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1339 /* Eliminate the specified n dimensions starting at first from the
1340 * constraints using Fourier-Motzkin. The dimensions themselves
1341 * are not removed.
1343 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1344 enum isl_dim_type type, unsigned first, unsigned n)
1346 int i;
1348 if (!map)
1349 return NULL;
1350 if (n == 0)
1351 return map;
1353 map = isl_map_cow(map);
1354 if (!map)
1355 return NULL;
1356 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1357 first += pos(map->dim, type) - 1;
1359 for (i = 0; i < map->n; ++i) {
1360 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1361 if (!map->p[i])
1362 goto error;
1364 return map;
1365 error:
1366 isl_map_free(map);
1367 return NULL;
1370 /* Eliminate the specified n dimensions starting at first from the
1371 * constraints using Fourier-Motzkin. The dimensions themselves
1372 * are not removed.
1374 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1375 enum isl_dim_type type, unsigned first, unsigned n)
1377 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1380 /* Eliminate the specified n dimensions starting at first from the
1381 * constraints using Fourier-Motzkin. The dimensions themselves
1382 * are not removed.
1384 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1385 unsigned first, unsigned n)
1387 return isl_set_eliminate(set, isl_dim_set, first, n);
1390 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1391 __isl_take isl_basic_map *bmap)
1393 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1394 bmap->n_div);
1395 if (!bmap)
1396 return NULL;
1397 bmap->n_div = 0;
1398 return isl_basic_map_finalize(bmap);
1401 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1402 __isl_take isl_basic_set *bset)
1404 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1405 (struct isl_basic_map *)bset);
1408 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1410 int i;
1412 if (!map)
1413 return NULL;
1414 if (map->n == 0)
1415 return map;
1417 map = isl_map_cow(map);
1418 if (!map)
1419 return NULL;
1421 for (i = 0; i < map->n; ++i) {
1422 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1423 if (!map->p[i])
1424 goto error;
1426 return map;
1427 error:
1428 isl_map_free(map);
1429 return NULL;
1432 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1434 return isl_map_remove_divs(set);
1437 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1438 enum isl_dim_type type, unsigned first, unsigned n)
1440 if (!bmap)
1441 return NULL;
1442 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1443 goto error);
1444 if (n == 0)
1445 return bmap;
1446 bmap = isl_basic_map_eliminate_vars(bmap,
1447 isl_basic_map_offset(bmap, type) - 1 + first, n);
1448 if (!bmap)
1449 return bmap;
1450 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1451 return bmap;
1452 bmap = isl_basic_map_drop(bmap, type, first, n);
1453 return bmap;
1454 error:
1455 isl_basic_map_free(bmap);
1456 return NULL;
1459 /* Return true if the definition of the given div (recursively) involves
1460 * any of the given variables.
1462 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1463 unsigned first, unsigned n)
1465 int i;
1466 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1468 if (isl_int_is_zero(bmap->div[div][0]))
1469 return 0;
1470 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1471 return 1;
1473 for (i = bmap->n_div - 1; i >= 0; --i) {
1474 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1475 continue;
1476 if (div_involves_vars(bmap, i, first, n))
1477 return 1;
1480 return 0;
1483 /* Remove all divs (recursively) involving any of the given dimensions
1484 * in their definitions.
1486 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1487 __isl_take isl_basic_map *bmap,
1488 enum isl_dim_type type, unsigned first, unsigned n)
1490 int i;
1492 if (!bmap)
1493 return NULL;
1494 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1495 goto error);
1496 first += isl_basic_map_offset(bmap, type);
1498 for (i = bmap->n_div - 1; i >= 0; --i) {
1499 if (!div_involves_vars(bmap, i, first, n))
1500 continue;
1501 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1502 if (!bmap)
1503 return NULL;
1504 i = bmap->n_div;
1507 return bmap;
1508 error:
1509 isl_basic_map_free(bmap);
1510 return NULL;
1513 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1514 enum isl_dim_type type, unsigned first, unsigned n)
1516 int i;
1518 if (!map)
1519 return NULL;
1520 if (map->n == 0)
1521 return map;
1523 map = isl_map_cow(map);
1524 if (!map)
1525 return NULL;
1527 for (i = 0; i < map->n; ++i) {
1528 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1529 type, first, n);
1530 if (!map->p[i])
1531 goto error;
1533 return map;
1534 error:
1535 isl_map_free(map);
1536 return NULL;
1539 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1540 enum isl_dim_type type, unsigned first, unsigned n)
1542 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1543 type, first, n);
1546 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1547 enum isl_dim_type type, unsigned first, unsigned n)
1549 int i;
1551 if (!bset)
1552 return -1;
1554 if (first + n > isl_basic_set_dim(bset, type))
1555 isl_die(bset->ctx, isl_error_invalid,
1556 "index out of bounds", return -1);
1558 first += isl_basic_set_offset(bset, type);
1559 for (i = 0; i < bset->n_eq; ++i)
1560 if (isl_seq_first_non_zero(bset->eq[i] + first, n) >= 0)
1561 return 1;
1562 for (i = 0; i < bset->n_ineq; ++i)
1563 if (isl_seq_first_non_zero(bset->ineq[i] + first, n) >= 0)
1564 return 1;
1566 return 0;
1569 int isl_set_involves_dims(__isl_keep isl_set *set,
1570 enum isl_dim_type type, unsigned first, unsigned n)
1572 int i;
1574 if (!set)
1575 return -1;
1577 if (first + n > isl_set_dim(set, type))
1578 isl_die(set->ctx, isl_error_invalid,
1579 "index out of bounds", return -1);
1581 for (i = 0; i < set->n; ++i) {
1582 int involves = isl_basic_set_involves_dims(set->p[i],
1583 type, first, n);
1584 if (involves < 0 || !involves)
1585 return involves;
1588 return 1;
1591 /* Return true if the definition of the given div is unknown or depends
1592 * on unknown divs.
1594 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1596 int i;
1597 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1599 if (isl_int_is_zero(bmap->div[div][0]))
1600 return 1;
1602 for (i = bmap->n_div - 1; i >= 0; --i) {
1603 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1604 continue;
1605 if (div_is_unknown(bmap, i))
1606 return 1;
1609 return 0;
1612 /* Remove all divs that are unknown or defined in terms of unknown divs.
1614 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1615 __isl_take isl_basic_map *bmap)
1617 int i;
1619 if (!bmap)
1620 return NULL;
1622 for (i = bmap->n_div - 1; i >= 0; --i) {
1623 if (!div_is_unknown(bmap, i))
1624 continue;
1625 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1628 return bmap;
1631 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1633 int i;
1635 if (!map)
1636 return NULL;
1637 if (map->n == 0)
1638 return map;
1640 map = isl_map_cow(map);
1641 if (!map)
1642 return NULL;
1644 for (i = 0; i < map->n; ++i) {
1645 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1646 if (!map->p[i])
1647 goto error;
1649 return map;
1650 error:
1651 isl_map_free(map);
1652 return NULL;
1655 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1657 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1660 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1661 __isl_take isl_basic_set *bset,
1662 enum isl_dim_type type, unsigned first, unsigned n)
1664 return (isl_basic_set *)
1665 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1668 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1669 enum isl_dim_type type, unsigned first, unsigned n)
1671 int i;
1673 if (n == 0)
1674 return map;
1676 map = isl_map_cow(map);
1677 if (!map)
1678 return NULL;
1679 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1681 for (i = 0; i < map->n; ++i) {
1682 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1683 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1684 if (!map->p[i])
1685 goto error;
1687 map = isl_map_drop(map, type, first, n);
1688 return map;
1689 error:
1690 isl_map_free(map);
1691 return NULL;
1694 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1695 enum isl_dim_type type, unsigned first, unsigned n)
1697 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1700 /* Project out n inputs starting at first using Fourier-Motzkin */
1701 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1702 unsigned first, unsigned n)
1704 return isl_map_remove_dims(map, isl_dim_in, first, n);
1707 static void dump_term(struct isl_basic_map *bmap,
1708 isl_int c, int pos, FILE *out)
1710 const char *name;
1711 unsigned in = isl_basic_map_n_in(bmap);
1712 unsigned dim = in + isl_basic_map_n_out(bmap);
1713 unsigned nparam = isl_basic_map_n_param(bmap);
1714 if (!pos)
1715 isl_int_print(out, c, 0);
1716 else {
1717 if (!isl_int_is_one(c))
1718 isl_int_print(out, c, 0);
1719 if (pos < 1 + nparam) {
1720 name = isl_dim_get_name(bmap->dim,
1721 isl_dim_param, pos - 1);
1722 if (name)
1723 fprintf(out, "%s", name);
1724 else
1725 fprintf(out, "p%d", pos - 1);
1726 } else if (pos < 1 + nparam + in)
1727 fprintf(out, "i%d", pos - 1 - nparam);
1728 else if (pos < 1 + nparam + dim)
1729 fprintf(out, "o%d", pos - 1 - nparam - in);
1730 else
1731 fprintf(out, "e%d", pos - 1 - nparam - dim);
1735 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1736 int sign, FILE *out)
1738 int i;
1739 int first;
1740 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1741 isl_int v;
1743 isl_int_init(v);
1744 for (i = 0, first = 1; i < len; ++i) {
1745 if (isl_int_sgn(c[i]) * sign <= 0)
1746 continue;
1747 if (!first)
1748 fprintf(out, " + ");
1749 first = 0;
1750 isl_int_abs(v, c[i]);
1751 dump_term(bmap, v, i, out);
1753 isl_int_clear(v);
1754 if (first)
1755 fprintf(out, "0");
1758 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1759 const char *op, FILE *out, int indent)
1761 int i;
1763 fprintf(out, "%*s", indent, "");
1765 dump_constraint_sign(bmap, c, 1, out);
1766 fprintf(out, " %s ", op);
1767 dump_constraint_sign(bmap, c, -1, out);
1769 fprintf(out, "\n");
1771 for (i = bmap->n_div; i < bmap->extra; ++i) {
1772 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1773 continue;
1774 fprintf(out, "%*s", indent, "");
1775 fprintf(out, "ERROR: unused div coefficient not zero\n");
1776 abort();
1780 static void dump_constraints(struct isl_basic_map *bmap,
1781 isl_int **c, unsigned n,
1782 const char *op, FILE *out, int indent)
1784 int i;
1786 for (i = 0; i < n; ++i)
1787 dump_constraint(bmap, c[i], op, out, indent);
1790 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1792 int j;
1793 int first = 1;
1794 unsigned total = isl_basic_map_total_dim(bmap);
1796 for (j = 0; j < 1 + total; ++j) {
1797 if (isl_int_is_zero(exp[j]))
1798 continue;
1799 if (!first && isl_int_is_pos(exp[j]))
1800 fprintf(out, "+");
1801 dump_term(bmap, exp[j], j, out);
1802 first = 0;
1806 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1808 int i;
1810 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1811 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1813 for (i = 0; i < bmap->n_div; ++i) {
1814 fprintf(out, "%*s", indent, "");
1815 fprintf(out, "e%d = [(", i);
1816 dump_affine(bmap, bmap->div[i]+1, out);
1817 fprintf(out, ")/");
1818 isl_int_print(out, bmap->div[i][0], 0);
1819 fprintf(out, "]\n");
1823 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1824 FILE *out, int indent)
1826 if (!bset) {
1827 fprintf(out, "null basic set\n");
1828 return;
1831 fprintf(out, "%*s", indent, "");
1832 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1833 bset->ref, bset->dim->nparam, bset->dim->n_out,
1834 bset->extra, bset->flags);
1835 dump((struct isl_basic_map *)bset, out, indent);
1838 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1839 FILE *out, int indent)
1841 if (!bmap) {
1842 fprintf(out, "null basic map\n");
1843 return;
1846 fprintf(out, "%*s", indent, "");
1847 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1848 "flags: %x, n_name: %d\n",
1849 bmap->ref,
1850 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1851 bmap->extra, bmap->flags, bmap->dim->n_name);
1852 dump(bmap, out, indent);
1855 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1857 unsigned total;
1858 if (!bmap)
1859 return -1;
1860 total = isl_basic_map_total_dim(bmap);
1861 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1862 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1863 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1864 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1865 return 0;
1868 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1870 struct isl_set *set;
1872 if (!dim)
1873 return NULL;
1874 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1875 isl_assert(dim->ctx, n >= 0, goto error);
1876 set = isl_alloc(dim->ctx, struct isl_set,
1877 sizeof(struct isl_set) +
1878 (n - 1) * sizeof(struct isl_basic_set *));
1879 if (!set)
1880 goto error;
1882 set->ctx = dim->ctx;
1883 isl_ctx_ref(set->ctx);
1884 set->ref = 1;
1885 set->size = n;
1886 set->n = 0;
1887 set->dim = dim;
1888 set->flags = flags;
1889 return set;
1890 error:
1891 isl_dim_free(dim);
1892 return NULL;
1895 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1896 unsigned nparam, unsigned dim, int n, unsigned flags)
1898 struct isl_set *set;
1899 struct isl_dim *dims;
1901 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1902 if (!dims)
1903 return NULL;
1905 set = isl_set_alloc_dim(dims, n, flags);
1906 return set;
1909 /* Make sure "map" has room for at least "n" more basic maps.
1911 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1913 int i;
1914 struct isl_map *grown = NULL;
1916 if (!map)
1917 return NULL;
1918 isl_assert(map->ctx, n >= 0, goto error);
1919 if (map->n + n <= map->size)
1920 return map;
1921 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1922 if (!grown)
1923 goto error;
1924 for (i = 0; i < map->n; ++i) {
1925 grown->p[i] = isl_basic_map_copy(map->p[i]);
1926 if (!grown->p[i])
1927 goto error;
1928 grown->n++;
1930 isl_map_free(map);
1931 return grown;
1932 error:
1933 isl_map_free(grown);
1934 isl_map_free(map);
1935 return NULL;
1938 /* Make sure "set" has room for at least "n" more basic sets.
1940 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1942 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1945 struct isl_set *isl_set_dup(struct isl_set *set)
1947 int i;
1948 struct isl_set *dup;
1950 if (!set)
1951 return NULL;
1953 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1954 if (!dup)
1955 return NULL;
1956 for (i = 0; i < set->n; ++i)
1957 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1958 return dup;
1961 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1963 return isl_map_from_basic_map(bset);
1966 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1968 struct isl_map *map;
1970 if (!bmap)
1971 return NULL;
1973 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1974 return isl_map_add_basic_map(map, bmap);
1977 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1978 __isl_take isl_basic_set *bset)
1980 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1981 (struct isl_basic_map *)bset);
1984 void isl_set_free(struct isl_set *set)
1986 int i;
1988 if (!set)
1989 return;
1991 if (--set->ref > 0)
1992 return;
1994 isl_ctx_deref(set->ctx);
1995 for (i = 0; i < set->n; ++i)
1996 isl_basic_set_free(set->p[i]);
1997 isl_dim_free(set->dim);
1998 free(set);
2001 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2003 int i;
2005 if (!set) {
2006 fprintf(out, "null set\n");
2007 return;
2010 fprintf(out, "%*s", indent, "");
2011 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2012 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2013 set->flags);
2014 for (i = 0; i < set->n; ++i) {
2015 fprintf(out, "%*s", indent, "");
2016 fprintf(out, "basic set %d:\n", i);
2017 isl_basic_set_print_internal(set->p[i], out, indent+4);
2021 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2023 int i;
2025 if (!map) {
2026 fprintf(out, "null map\n");
2027 return;
2030 fprintf(out, "%*s", indent, "");
2031 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2032 "flags: %x, n_name: %d\n",
2033 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2034 map->dim->n_out, map->flags, map->dim->n_name);
2035 for (i = 0; i < map->n; ++i) {
2036 fprintf(out, "%*s", indent, "");
2037 fprintf(out, "basic map %d:\n", i);
2038 isl_basic_map_print_internal(map->p[i], out, indent+4);
2042 struct isl_basic_map *isl_basic_map_intersect_domain(
2043 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2045 struct isl_basic_map *bmap_domain;
2047 if (!bmap || !bset)
2048 goto error;
2050 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2051 bset->dim, isl_dim_param), goto error);
2053 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2054 isl_assert(bset->ctx,
2055 isl_basic_map_compatible_domain(bmap, bset), goto error);
2057 bmap = isl_basic_map_cow(bmap);
2058 if (!bmap)
2059 goto error;
2060 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2061 bset->n_div, bset->n_eq, bset->n_ineq);
2062 bmap_domain = isl_basic_map_from_domain(bset);
2063 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2065 bmap = isl_basic_map_simplify(bmap);
2066 return isl_basic_map_finalize(bmap);
2067 error:
2068 isl_basic_map_free(bmap);
2069 isl_basic_set_free(bset);
2070 return NULL;
2073 struct isl_basic_map *isl_basic_map_intersect_range(
2074 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2076 struct isl_basic_map *bmap_range;
2078 if (!bmap || !bset)
2079 goto error;
2081 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2082 bset->dim, isl_dim_param), goto error);
2084 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2085 isl_assert(bset->ctx,
2086 isl_basic_map_compatible_range(bmap, bset), goto error);
2088 if (isl_basic_set_is_universe(bset)) {
2089 isl_basic_set_free(bset);
2090 return bmap;
2093 bmap = isl_basic_map_cow(bmap);
2094 if (!bmap)
2095 goto error;
2096 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2097 bset->n_div, bset->n_eq, bset->n_ineq);
2098 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2099 bmap = add_constraints(bmap, bmap_range, 0, 0);
2101 bmap = isl_basic_map_simplify(bmap);
2102 return isl_basic_map_finalize(bmap);
2103 error:
2104 isl_basic_map_free(bmap);
2105 isl_basic_set_free(bset);
2106 return NULL;
2109 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2111 int i;
2112 unsigned total;
2113 isl_int s;
2115 total = 1 + isl_basic_map_total_dim(bmap);
2116 if (total != vec->size)
2117 return -1;
2119 isl_int_init(s);
2121 for (i = 0; i < bmap->n_eq; ++i) {
2122 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2123 if (!isl_int_is_zero(s)) {
2124 isl_int_clear(s);
2125 return 0;
2129 for (i = 0; i < bmap->n_ineq; ++i) {
2130 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2131 if (isl_int_is_neg(s)) {
2132 isl_int_clear(s);
2133 return 0;
2137 isl_int_clear(s);
2139 return 1;
2142 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2144 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2147 struct isl_basic_map *isl_basic_map_intersect(
2148 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2150 struct isl_vec *sample = NULL;
2152 if (!bmap1 || !bmap2)
2153 goto error;
2155 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2156 bmap2->dim, isl_dim_param), goto error);
2157 if (isl_dim_total(bmap1->dim) ==
2158 isl_dim_size(bmap1->dim, isl_dim_param) &&
2159 isl_dim_total(bmap2->dim) !=
2160 isl_dim_size(bmap2->dim, isl_dim_param))
2161 return isl_basic_map_intersect(bmap2, bmap1);
2163 if (isl_dim_total(bmap2->dim) !=
2164 isl_dim_size(bmap2->dim, isl_dim_param))
2165 isl_assert(bmap1->ctx,
2166 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2168 if (bmap1->sample &&
2169 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2170 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2171 sample = isl_vec_copy(bmap1->sample);
2172 else if (bmap2->sample &&
2173 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2174 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2175 sample = isl_vec_copy(bmap2->sample);
2177 bmap1 = isl_basic_map_cow(bmap1);
2178 if (!bmap1)
2179 goto error;
2180 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2181 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2182 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2184 if (!bmap1)
2185 isl_vec_free(sample);
2186 else if (sample) {
2187 isl_vec_free(bmap1->sample);
2188 bmap1->sample = sample;
2191 bmap1 = isl_basic_map_simplify(bmap1);
2192 return isl_basic_map_finalize(bmap1);
2193 error:
2194 if (sample)
2195 isl_vec_free(sample);
2196 isl_basic_map_free(bmap1);
2197 isl_basic_map_free(bmap2);
2198 return NULL;
2201 struct isl_basic_set *isl_basic_set_intersect(
2202 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2204 return (struct isl_basic_set *)
2205 isl_basic_map_intersect(
2206 (struct isl_basic_map *)bset1,
2207 (struct isl_basic_map *)bset2);
2210 /* Special case of isl_map_intersect, where both map1 and map2
2211 * are convex, without any divs and such that either map1 or map2
2212 * contains a single constraint. This constraint is then simply
2213 * added to the other map.
2215 static __isl_give isl_map *map_intersect_add_constraint(
2216 __isl_take isl_map *map1, __isl_take isl_map *map2)
2218 isl_assert(map1->ctx, map1->n == 1, goto error);
2219 isl_assert(map2->ctx, map1->n == 1, goto error);
2220 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2221 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2223 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2224 return isl_map_intersect(map2, map1);
2226 isl_assert(map2->ctx,
2227 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2229 map1 = isl_map_cow(map1);
2230 if (!map1)
2231 goto error;
2232 if (isl_map_plain_is_empty(map1)) {
2233 isl_map_free(map2);
2234 return map1;
2236 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2237 if (map2->p[0]->n_eq == 1)
2238 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2239 else
2240 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2241 map2->p[0]->ineq[0]);
2243 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2244 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2245 if (!map1->p[0])
2246 goto error;
2248 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2249 isl_basic_map_free(map1->p[0]);
2250 map1->n = 0;
2253 isl_map_free(map2);
2255 return map1;
2256 error:
2257 isl_map_free(map1);
2258 isl_map_free(map2);
2259 return NULL;
2262 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2264 unsigned flags = 0;
2265 struct isl_map *result;
2266 int i, j;
2268 if (!map1 || !map2)
2269 goto error;
2271 if (isl_map_plain_is_empty(map1)) {
2272 isl_map_free(map2);
2273 return map1;
2275 if (isl_map_plain_is_empty(map2)) {
2276 isl_map_free(map1);
2277 return map2;
2280 if (map1->n == 1 && map2->n == 1 &&
2281 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2282 isl_dim_equal(map1->dim, map2->dim) &&
2283 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2284 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2285 return map_intersect_add_constraint(map1, map2);
2286 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2287 map2->dim, isl_dim_param), goto error);
2288 if (isl_dim_total(map1->dim) ==
2289 isl_dim_size(map1->dim, isl_dim_param) &&
2290 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2291 return isl_map_intersect(map2, map1);
2293 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2294 isl_assert(map1->ctx,
2295 isl_dim_equal(map1->dim, map2->dim), goto error);
2297 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2298 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2299 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2301 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2302 map1->n * map2->n, flags);
2303 if (!result)
2304 goto error;
2305 for (i = 0; i < map1->n; ++i)
2306 for (j = 0; j < map2->n; ++j) {
2307 struct isl_basic_map *part;
2308 part = isl_basic_map_intersect(
2309 isl_basic_map_copy(map1->p[i]),
2310 isl_basic_map_copy(map2->p[j]));
2311 if (isl_basic_map_is_empty(part))
2312 isl_basic_map_free(part);
2313 else
2314 result = isl_map_add_basic_map(result, part);
2315 if (!result)
2316 goto error;
2318 isl_map_free(map1);
2319 isl_map_free(map2);
2320 return result;
2321 error:
2322 isl_map_free(map1);
2323 isl_map_free(map2);
2324 return NULL;
2327 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2329 return (struct isl_set *)
2330 isl_map_intersect((struct isl_map *)set1,
2331 (struct isl_map *)set2);
2334 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2336 struct isl_dim *dim;
2337 struct isl_basic_set *bset;
2338 unsigned in;
2340 if (!bmap)
2341 return NULL;
2342 bmap = isl_basic_map_cow(bmap);
2343 if (!bmap)
2344 return NULL;
2345 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2346 in = isl_basic_map_n_in(bmap);
2347 bset = isl_basic_set_from_basic_map(bmap);
2348 bset = isl_basic_set_swap_vars(bset, in);
2349 return isl_basic_map_from_basic_set(bset, dim);
2352 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2353 enum isl_dim_type type, unsigned pos, unsigned n)
2355 struct isl_dim *res_dim;
2356 struct isl_basic_map *res;
2357 struct isl_dim_map *dim_map;
2358 unsigned total, off;
2359 enum isl_dim_type t;
2361 if (n == 0)
2362 return bmap;
2364 if (!bmap)
2365 return NULL;
2367 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2369 total = isl_basic_map_total_dim(bmap) + n;
2370 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2371 off = 0;
2372 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2373 if (t != type) {
2374 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2375 } else {
2376 unsigned size = isl_basic_map_dim(bmap, t);
2377 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2378 0, pos, off);
2379 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2380 pos, size - pos, off + pos + n);
2382 off += isl_dim_size(res_dim, t);
2384 isl_dim_map_div(dim_map, bmap, off);
2386 res = isl_basic_map_alloc_dim(res_dim,
2387 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2388 if (isl_basic_map_is_rational(bmap))
2389 res = isl_basic_map_set_rational(res);
2390 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2391 return isl_basic_map_finalize(res);
2394 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2395 enum isl_dim_type type, unsigned n)
2397 if (!bmap)
2398 return NULL;
2399 return isl_basic_map_insert(bmap, type,
2400 isl_basic_map_dim(bmap, type), n);
2403 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2404 enum isl_dim_type type, unsigned n)
2406 if (!bset)
2407 return NULL;
2408 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2409 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2410 error:
2411 isl_basic_set_free(bset);
2412 return NULL;
2415 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2416 enum isl_dim_type type, unsigned pos, unsigned n)
2418 int i;
2420 if (n == 0)
2421 return map;
2423 map = isl_map_cow(map);
2424 if (!map)
2425 return NULL;
2427 map->dim = isl_dim_insert(map->dim, type, pos, n);
2428 if (!map->dim)
2429 goto error;
2431 for (i = 0; i < map->n; ++i) {
2432 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2433 if (!map->p[i])
2434 goto error;
2437 return map;
2438 error:
2439 isl_map_free(map);
2440 return NULL;
2443 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2444 enum isl_dim_type type, unsigned pos, unsigned n)
2446 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2449 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2450 enum isl_dim_type type, unsigned n)
2452 if (!map)
2453 return NULL;
2454 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2457 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2458 enum isl_dim_type type, unsigned n)
2460 if (!set)
2461 return NULL;
2462 isl_assert(set->ctx, type != isl_dim_in, goto error);
2463 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2464 error:
2465 isl_set_free(set);
2466 return NULL;
2469 __isl_give isl_basic_map *isl_basic_map_move_dims(
2470 __isl_take isl_basic_map *bmap,
2471 enum isl_dim_type dst_type, unsigned dst_pos,
2472 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2474 struct isl_dim_map *dim_map;
2475 struct isl_basic_map *res;
2476 enum isl_dim_type t;
2477 unsigned total, off;
2479 if (!bmap)
2480 return NULL;
2481 if (n == 0)
2482 return bmap;
2484 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2485 goto error);
2487 if (dst_type == src_type && dst_pos == src_pos)
2488 return bmap;
2490 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2492 if (pos(bmap->dim, dst_type) + dst_pos ==
2493 pos(bmap->dim, src_type) + src_pos +
2494 ((src_type < dst_type) ? n : 0)) {
2495 bmap = isl_basic_map_cow(bmap);
2496 if (!bmap)
2497 return NULL;
2499 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2500 src_type, src_pos, n);
2501 if (!bmap->dim)
2502 goto error;
2504 bmap = isl_basic_map_finalize(bmap);
2506 return bmap;
2509 total = isl_basic_map_total_dim(bmap);
2510 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2512 off = 0;
2513 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2514 unsigned size = isl_dim_size(bmap->dim, t);
2515 if (t == dst_type) {
2516 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2517 0, dst_pos, off);
2518 off += dst_pos;
2519 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2520 src_pos, n, off);
2521 off += n;
2522 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2523 dst_pos, size - dst_pos, off);
2524 off += size - dst_pos;
2525 } else if (t == src_type) {
2526 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2527 0, src_pos, off);
2528 off += src_pos;
2529 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2530 src_pos + n, size - src_pos - n, off);
2531 off += size - src_pos - n;
2532 } else {
2533 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2534 off += size;
2537 isl_dim_map_div(dim_map, bmap, off);
2539 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2540 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2541 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2543 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2544 src_type, src_pos, n);
2545 if (!bmap->dim)
2546 goto error;
2548 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2549 bmap = isl_basic_map_gauss(bmap, NULL);
2550 bmap = isl_basic_map_finalize(bmap);
2552 return bmap;
2553 error:
2554 isl_basic_map_free(bmap);
2555 return NULL;
2558 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2559 enum isl_dim_type dst_type, unsigned dst_pos,
2560 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2562 return (isl_basic_set *)isl_basic_map_move_dims(
2563 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2566 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2567 enum isl_dim_type dst_type, unsigned dst_pos,
2568 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2570 if (!set)
2571 return NULL;
2572 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2573 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2574 src_type, src_pos, n);
2575 error:
2576 isl_set_free(set);
2577 return NULL;
2580 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2581 enum isl_dim_type dst_type, unsigned dst_pos,
2582 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2584 int i;
2586 if (!map)
2587 return NULL;
2588 if (n == 0)
2589 return map;
2591 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2592 goto error);
2594 if (dst_type == src_type && dst_pos == src_pos)
2595 return map;
2597 isl_assert(map->ctx, dst_type != src_type, goto error);
2599 map = isl_map_cow(map);
2600 if (!map)
2601 return NULL;
2603 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2604 if (!map->dim)
2605 goto error;
2607 for (i = 0; i < map->n; ++i) {
2608 map->p[i] = isl_basic_map_move_dims(map->p[i],
2609 dst_type, dst_pos,
2610 src_type, src_pos, n);
2611 if (!map->p[i])
2612 goto error;
2615 return map;
2616 error:
2617 isl_map_free(map);
2618 return NULL;
2621 /* Move the specified dimensions to the last columns right before
2622 * the divs. Don't change the dimension specification of bmap.
2623 * That's the responsibility of the caller.
2625 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2626 enum isl_dim_type type, unsigned first, unsigned n)
2628 struct isl_dim_map *dim_map;
2629 struct isl_basic_map *res;
2630 enum isl_dim_type t;
2631 unsigned total, off;
2633 if (!bmap)
2634 return NULL;
2635 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2636 return bmap;
2638 total = isl_basic_map_total_dim(bmap);
2639 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2641 off = 0;
2642 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2643 unsigned size = isl_dim_size(bmap->dim, t);
2644 if (t == type) {
2645 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2646 0, first, off);
2647 off += first;
2648 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2649 first, n, total - bmap->n_div - n);
2650 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2651 first + n, size - (first + n), off);
2652 off += size - (first + n);
2653 } else {
2654 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2655 off += size;
2658 isl_dim_map_div(dim_map, bmap, off + n);
2660 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2661 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2662 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2663 return res;
2666 /* Turn the n dimensions of type type, starting at first
2667 * into existentially quantified variables.
2669 __isl_give isl_basic_map *isl_basic_map_project_out(
2670 __isl_take isl_basic_map *bmap,
2671 enum isl_dim_type type, unsigned first, unsigned n)
2673 int i;
2674 size_t row_size;
2675 isl_int **new_div;
2676 isl_int *old;
2678 if (n == 0)
2679 return bmap;
2681 if (!bmap)
2682 return NULL;
2684 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2685 return isl_basic_map_remove_dims(bmap, type, first, n);
2687 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2688 goto error);
2690 bmap = move_last(bmap, type, first, n);
2691 bmap = isl_basic_map_cow(bmap);
2692 if (!bmap)
2693 return NULL;
2695 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2696 old = bmap->block2.data;
2697 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2698 (bmap->extra + n) * (1 + row_size));
2699 if (!bmap->block2.data)
2700 goto error;
2701 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2702 if (!new_div)
2703 goto error;
2704 for (i = 0; i < n; ++i) {
2705 new_div[i] = bmap->block2.data +
2706 (bmap->extra + i) * (1 + row_size);
2707 isl_seq_clr(new_div[i], 1 + row_size);
2709 for (i = 0; i < bmap->extra; ++i)
2710 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2711 free(bmap->div);
2712 bmap->div = new_div;
2713 bmap->n_div += n;
2714 bmap->extra += n;
2716 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2717 if (!bmap->dim)
2718 goto error;
2719 bmap = isl_basic_map_simplify(bmap);
2720 bmap = isl_basic_map_drop_redundant_divs(bmap);
2721 return isl_basic_map_finalize(bmap);
2722 error:
2723 isl_basic_map_free(bmap);
2724 return NULL;
2727 /* Turn the n dimensions of type type, starting at first
2728 * into existentially quantified variables.
2730 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2731 enum isl_dim_type type, unsigned first, unsigned n)
2733 return (isl_basic_set *)isl_basic_map_project_out(
2734 (isl_basic_map *)bset, type, first, n);
2737 /* Turn the n dimensions of type type, starting at first
2738 * into existentially quantified variables.
2740 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2741 enum isl_dim_type type, unsigned first, unsigned n)
2743 int i;
2745 if (!map)
2746 return NULL;
2748 if (n == 0)
2749 return map;
2751 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2753 map = isl_map_cow(map);
2754 if (!map)
2755 return NULL;
2757 map->dim = isl_dim_drop(map->dim, type, first, n);
2758 if (!map->dim)
2759 goto error;
2761 for (i = 0; i < map->n; ++i) {
2762 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2763 if (!map->p[i])
2764 goto error;
2767 return map;
2768 error:
2769 isl_map_free(map);
2770 return NULL;
2773 /* Turn the n dimensions of type type, starting at first
2774 * into existentially quantified variables.
2776 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2777 enum isl_dim_type type, unsigned first, unsigned n)
2779 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2782 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2784 int i, j;
2786 for (i = 0; i < n; ++i) {
2787 j = isl_basic_map_alloc_div(bmap);
2788 if (j < 0)
2789 goto error;
2790 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2792 return bmap;
2793 error:
2794 isl_basic_map_free(bmap);
2795 return NULL;
2798 struct isl_basic_map *isl_basic_map_apply_range(
2799 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2801 struct isl_dim *dim_result = NULL;
2802 struct isl_basic_map *bmap;
2803 unsigned n_in, n_out, n, nparam, total, pos;
2804 struct isl_dim_map *dim_map1, *dim_map2;
2806 if (!bmap1 || !bmap2)
2807 goto error;
2809 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2810 isl_dim_copy(bmap2->dim));
2812 n_in = isl_basic_map_n_in(bmap1);
2813 n_out = isl_basic_map_n_out(bmap2);
2814 n = isl_basic_map_n_out(bmap1);
2815 nparam = isl_basic_map_n_param(bmap1);
2817 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2818 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2819 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2820 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2821 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2822 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2823 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2824 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2825 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2826 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2827 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2829 bmap = isl_basic_map_alloc_dim(dim_result,
2830 bmap1->n_div + bmap2->n_div + n,
2831 bmap1->n_eq + bmap2->n_eq,
2832 bmap1->n_ineq + bmap2->n_ineq);
2833 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2834 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2835 bmap = add_divs(bmap, n);
2836 bmap = isl_basic_map_simplify(bmap);
2837 bmap = isl_basic_map_drop_redundant_divs(bmap);
2838 return isl_basic_map_finalize(bmap);
2839 error:
2840 isl_basic_map_free(bmap1);
2841 isl_basic_map_free(bmap2);
2842 return NULL;
2845 struct isl_basic_set *isl_basic_set_apply(
2846 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2848 if (!bset || !bmap)
2849 goto error;
2851 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2852 goto error);
2854 return (struct isl_basic_set *)
2855 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2856 error:
2857 isl_basic_set_free(bset);
2858 isl_basic_map_free(bmap);
2859 return NULL;
2862 struct isl_basic_map *isl_basic_map_apply_domain(
2863 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2865 if (!bmap1 || !bmap2)
2866 goto error;
2868 isl_assert(bmap1->ctx,
2869 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2870 isl_assert(bmap1->ctx,
2871 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2872 goto error);
2874 bmap1 = isl_basic_map_reverse(bmap1);
2875 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2876 return isl_basic_map_reverse(bmap1);
2877 error:
2878 isl_basic_map_free(bmap1);
2879 isl_basic_map_free(bmap2);
2880 return NULL;
2883 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2884 * A \cap B -> f(A) + f(B)
2886 struct isl_basic_map *isl_basic_map_sum(
2887 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2889 unsigned n_in, n_out, nparam, total, pos;
2890 struct isl_basic_map *bmap = NULL;
2891 struct isl_dim_map *dim_map1, *dim_map2;
2892 int i;
2894 if (!bmap1 || !bmap2)
2895 goto error;
2897 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2898 goto error);
2900 nparam = isl_basic_map_n_param(bmap1);
2901 n_in = isl_basic_map_n_in(bmap1);
2902 n_out = isl_basic_map_n_out(bmap1);
2904 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2905 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2906 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2907 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2908 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2909 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2910 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2911 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2912 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2913 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2914 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2916 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2917 bmap1->n_div + bmap2->n_div + 2 * n_out,
2918 bmap1->n_eq + bmap2->n_eq + n_out,
2919 bmap1->n_ineq + bmap2->n_ineq);
2920 for (i = 0; i < n_out; ++i) {
2921 int j = isl_basic_map_alloc_equality(bmap);
2922 if (j < 0)
2923 goto error;
2924 isl_seq_clr(bmap->eq[j], 1+total);
2925 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2926 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2927 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2929 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2930 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2931 bmap = add_divs(bmap, 2 * n_out);
2933 bmap = isl_basic_map_simplify(bmap);
2934 return isl_basic_map_finalize(bmap);
2935 error:
2936 isl_basic_map_free(bmap);
2937 isl_basic_map_free(bmap1);
2938 isl_basic_map_free(bmap2);
2939 return NULL;
2942 /* Given two maps A -> f(A) and B -> g(B), construct a map
2943 * A \cap B -> f(A) + f(B)
2945 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2947 struct isl_map *result;
2948 int i, j;
2950 if (!map1 || !map2)
2951 goto error;
2953 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2955 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2956 map1->n * map2->n, 0);
2957 if (!result)
2958 goto error;
2959 for (i = 0; i < map1->n; ++i)
2960 for (j = 0; j < map2->n; ++j) {
2961 struct isl_basic_map *part;
2962 part = isl_basic_map_sum(
2963 isl_basic_map_copy(map1->p[i]),
2964 isl_basic_map_copy(map2->p[j]));
2965 if (isl_basic_map_is_empty(part))
2966 isl_basic_map_free(part);
2967 else
2968 result = isl_map_add_basic_map(result, part);
2969 if (!result)
2970 goto error;
2972 isl_map_free(map1);
2973 isl_map_free(map2);
2974 return result;
2975 error:
2976 isl_map_free(map1);
2977 isl_map_free(map2);
2978 return NULL;
2981 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2982 __isl_take isl_set *set2)
2984 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2987 /* Given a basic map A -> f(A), construct A -> -f(A).
2989 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2991 int i, j;
2992 unsigned off, n;
2994 bmap = isl_basic_map_cow(bmap);
2995 if (!bmap)
2996 return NULL;
2998 n = isl_basic_map_dim(bmap, isl_dim_out);
2999 off = isl_basic_map_offset(bmap, isl_dim_out);
3000 for (i = 0; i < bmap->n_eq; ++i)
3001 for (j = 0; j < n; ++j)
3002 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3003 for (i = 0; i < bmap->n_ineq; ++i)
3004 for (j = 0; j < n; ++j)
3005 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3006 for (i = 0; i < bmap->n_div; ++i)
3007 for (j = 0; j < n; ++j)
3008 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3009 return isl_basic_map_finalize(bmap);
3012 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3014 return isl_basic_map_neg(bset);
3017 /* Given a map A -> f(A), construct A -> -f(A).
3019 struct isl_map *isl_map_neg(struct isl_map *map)
3021 int i;
3023 map = isl_map_cow(map);
3024 if (!map)
3025 return NULL;
3027 for (i = 0; i < map->n; ++i) {
3028 map->p[i] = isl_basic_map_neg(map->p[i]);
3029 if (!map->p[i])
3030 goto error;
3033 return map;
3034 error:
3035 isl_map_free(map);
3036 return NULL;
3039 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3041 return (isl_set *)isl_map_neg((isl_map *)set);
3044 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3045 * A -> floor(f(A)/d).
3047 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3048 isl_int d)
3050 unsigned n_in, n_out, nparam, total, pos;
3051 struct isl_basic_map *result = NULL;
3052 struct isl_dim_map *dim_map;
3053 int i;
3055 if (!bmap)
3056 return NULL;
3058 nparam = isl_basic_map_n_param(bmap);
3059 n_in = isl_basic_map_n_in(bmap);
3060 n_out = isl_basic_map_n_out(bmap);
3062 total = nparam + n_in + n_out + bmap->n_div + n_out;
3063 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3064 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3065 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3066 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3067 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3069 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3070 bmap->n_div + n_out,
3071 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3072 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3073 result = add_divs(result, n_out);
3074 for (i = 0; i < n_out; ++i) {
3075 int j;
3076 j = isl_basic_map_alloc_inequality(result);
3077 if (j < 0)
3078 goto error;
3079 isl_seq_clr(result->ineq[j], 1+total);
3080 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3081 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3082 j = isl_basic_map_alloc_inequality(result);
3083 if (j < 0)
3084 goto error;
3085 isl_seq_clr(result->ineq[j], 1+total);
3086 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3087 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3088 isl_int_sub_ui(result->ineq[j][0], d, 1);
3091 result = isl_basic_map_simplify(result);
3092 return isl_basic_map_finalize(result);
3093 error:
3094 isl_basic_map_free(result);
3095 return NULL;
3098 /* Given a map A -> f(A) and an integer d, construct a map
3099 * A -> floor(f(A)/d).
3101 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3103 int i;
3105 map = isl_map_cow(map);
3106 if (!map)
3107 return NULL;
3109 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3110 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3111 for (i = 0; i < map->n; ++i) {
3112 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3113 if (!map->p[i])
3114 goto error;
3117 return map;
3118 error:
3119 isl_map_free(map);
3120 return NULL;
3123 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3125 int i;
3126 unsigned nparam;
3127 unsigned n_in;
3129 i = isl_basic_map_alloc_equality(bmap);
3130 if (i < 0)
3131 goto error;
3132 nparam = isl_basic_map_n_param(bmap);
3133 n_in = isl_basic_map_n_in(bmap);
3134 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3135 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3136 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3137 return isl_basic_map_finalize(bmap);
3138 error:
3139 isl_basic_map_free(bmap);
3140 return NULL;
3143 /* Add a constraints to "bmap" expressing i_pos < o_pos
3145 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3147 int i;
3148 unsigned nparam;
3149 unsigned n_in;
3151 i = isl_basic_map_alloc_inequality(bmap);
3152 if (i < 0)
3153 goto error;
3154 nparam = isl_basic_map_n_param(bmap);
3155 n_in = isl_basic_map_n_in(bmap);
3156 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3157 isl_int_set_si(bmap->ineq[i][0], -1);
3158 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3159 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3160 return isl_basic_map_finalize(bmap);
3161 error:
3162 isl_basic_map_free(bmap);
3163 return NULL;
3166 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3168 static __isl_give isl_basic_map *var_less_or_equal(
3169 __isl_take isl_basic_map *bmap, unsigned pos)
3171 int i;
3172 unsigned nparam;
3173 unsigned n_in;
3175 i = isl_basic_map_alloc_inequality(bmap);
3176 if (i < 0)
3177 goto error;
3178 nparam = isl_basic_map_n_param(bmap);
3179 n_in = isl_basic_map_n_in(bmap);
3180 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3181 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3182 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3183 return isl_basic_map_finalize(bmap);
3184 error:
3185 isl_basic_map_free(bmap);
3186 return NULL;
3189 /* Add a constraints to "bmap" expressing i_pos > o_pos
3191 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3193 int i;
3194 unsigned nparam;
3195 unsigned n_in;
3197 i = isl_basic_map_alloc_inequality(bmap);
3198 if (i < 0)
3199 goto error;
3200 nparam = isl_basic_map_n_param(bmap);
3201 n_in = isl_basic_map_n_in(bmap);
3202 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3203 isl_int_set_si(bmap->ineq[i][0], -1);
3204 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3205 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3206 return isl_basic_map_finalize(bmap);
3207 error:
3208 isl_basic_map_free(bmap);
3209 return NULL;
3212 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3214 static __isl_give isl_basic_map *var_more_or_equal(
3215 __isl_take isl_basic_map *bmap, unsigned pos)
3217 int i;
3218 unsigned nparam;
3219 unsigned n_in;
3221 i = isl_basic_map_alloc_inequality(bmap);
3222 if (i < 0)
3223 goto error;
3224 nparam = isl_basic_map_n_param(bmap);
3225 n_in = isl_basic_map_n_in(bmap);
3226 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3227 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3228 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3229 return isl_basic_map_finalize(bmap);
3230 error:
3231 isl_basic_map_free(bmap);
3232 return NULL;
3235 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3237 int i;
3238 struct isl_basic_map *bmap;
3239 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3240 if (!bmap)
3241 return NULL;
3242 for (i = 0; i < n_equal && bmap; ++i)
3243 bmap = var_equal(bmap, i);
3244 return isl_basic_map_finalize(bmap);
3247 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3249 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3251 int i;
3252 struct isl_basic_map *bmap;
3253 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3254 if (!bmap)
3255 return NULL;
3256 for (i = 0; i < pos && bmap; ++i)
3257 bmap = var_equal(bmap, i);
3258 if (bmap)
3259 bmap = var_less(bmap, pos);
3260 return isl_basic_map_finalize(bmap);
3263 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3265 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3266 __isl_take isl_dim *dim, unsigned pos)
3268 int i;
3269 isl_basic_map *bmap;
3271 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3272 for (i = 0; i < pos; ++i)
3273 bmap = var_equal(bmap, i);
3274 bmap = var_less_or_equal(bmap, pos);
3275 return isl_basic_map_finalize(bmap);
3278 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3280 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3282 int i;
3283 struct isl_basic_map *bmap;
3284 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3285 if (!bmap)
3286 return NULL;
3287 for (i = 0; i < pos && bmap; ++i)
3288 bmap = var_equal(bmap, i);
3289 if (bmap)
3290 bmap = var_more(bmap, pos);
3291 return isl_basic_map_finalize(bmap);
3294 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3296 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3297 __isl_take isl_dim *dim, unsigned pos)
3299 int i;
3300 isl_basic_map *bmap;
3302 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3303 for (i = 0; i < pos; ++i)
3304 bmap = var_equal(bmap, i);
3305 bmap = var_more_or_equal(bmap, pos);
3306 return isl_basic_map_finalize(bmap);
3309 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3310 unsigned n, int equal)
3312 struct isl_map *map;
3313 int i;
3315 if (n == 0 && equal)
3316 return isl_map_universe(dims);
3318 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3320 for (i = 0; i + 1 < n; ++i)
3321 map = isl_map_add_basic_map(map,
3322 isl_basic_map_less_at(isl_dim_copy(dims), i));
3323 if (n > 0) {
3324 if (equal)
3325 map = isl_map_add_basic_map(map,
3326 isl_basic_map_less_or_equal_at(dims, n - 1));
3327 else
3328 map = isl_map_add_basic_map(map,
3329 isl_basic_map_less_at(dims, n - 1));
3330 } else
3331 isl_dim_free(dims);
3333 return map;
3336 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3338 if (!dims)
3339 return NULL;
3340 return map_lex_lte_first(dims, dims->n_out, equal);
3343 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3345 return map_lex_lte_first(dim, n, 0);
3348 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3350 return map_lex_lte_first(dim, n, 1);
3353 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3355 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3358 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3360 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3363 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3364 unsigned n, int equal)
3366 struct isl_map *map;
3367 int i;
3369 if (n == 0 && equal)
3370 return isl_map_universe(dims);
3372 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3374 for (i = 0; i + 1 < n; ++i)
3375 map = isl_map_add_basic_map(map,
3376 isl_basic_map_more_at(isl_dim_copy(dims), i));
3377 if (n > 0) {
3378 if (equal)
3379 map = isl_map_add_basic_map(map,
3380 isl_basic_map_more_or_equal_at(dims, n - 1));
3381 else
3382 map = isl_map_add_basic_map(map,
3383 isl_basic_map_more_at(dims, n - 1));
3384 } else
3385 isl_dim_free(dims);
3387 return map;
3390 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3392 if (!dims)
3393 return NULL;
3394 return map_lex_gte_first(dims, dims->n_out, equal);
3397 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3399 return map_lex_gte_first(dim, n, 0);
3402 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3404 return map_lex_gte_first(dim, n, 1);
3407 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3409 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3412 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3414 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3417 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3418 __isl_take isl_set *set2)
3420 isl_map *map;
3421 map = isl_map_lex_le(isl_set_get_dim(set1));
3422 map = isl_map_intersect_domain(map, set1);
3423 map = isl_map_intersect_range(map, set2);
3424 return map;
3427 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3428 __isl_take isl_set *set2)
3430 isl_map *map;
3431 map = isl_map_lex_lt(isl_set_get_dim(set1));
3432 map = isl_map_intersect_domain(map, set1);
3433 map = isl_map_intersect_range(map, set2);
3434 return map;
3437 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3438 __isl_take isl_set *set2)
3440 isl_map *map;
3441 map = isl_map_lex_ge(isl_set_get_dim(set1));
3442 map = isl_map_intersect_domain(map, set1);
3443 map = isl_map_intersect_range(map, set2);
3444 return map;
3447 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3448 __isl_take isl_set *set2)
3450 isl_map *map;
3451 map = isl_map_lex_gt(isl_set_get_dim(set1));
3452 map = isl_map_intersect_domain(map, set1);
3453 map = isl_map_intersect_range(map, set2);
3454 return map;
3457 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3458 __isl_take isl_map *map2)
3460 isl_map *map;
3461 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3462 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3463 map = isl_map_apply_range(map, isl_map_reverse(map2));
3464 return map;
3467 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3468 __isl_take isl_map *map2)
3470 isl_map *map;
3471 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3472 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3473 map = isl_map_apply_range(map, isl_map_reverse(map2));
3474 return map;
3477 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3478 __isl_take isl_map *map2)
3480 isl_map *map;
3481 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3482 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3483 map = isl_map_apply_range(map, isl_map_reverse(map2));
3484 return map;
3487 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3488 __isl_take isl_map *map2)
3490 isl_map *map;
3491 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3492 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3493 map = isl_map_apply_range(map, isl_map_reverse(map2));
3494 return map;
3497 struct isl_basic_map *isl_basic_map_from_basic_set(
3498 struct isl_basic_set *bset, struct isl_dim *dim)
3500 struct isl_basic_map *bmap;
3502 bset = isl_basic_set_cow(bset);
3503 if (!bset || !dim)
3504 goto error;
3506 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3507 isl_dim_free(bset->dim);
3508 bmap = (struct isl_basic_map *) bset;
3509 bmap->dim = dim;
3510 return isl_basic_map_finalize(bmap);
3511 error:
3512 isl_basic_set_free(bset);
3513 isl_dim_free(dim);
3514 return NULL;
3517 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3519 if (!bmap)
3520 goto error;
3521 if (bmap->dim->n_in == 0)
3522 return (struct isl_basic_set *)bmap;
3523 bmap = isl_basic_map_cow(bmap);
3524 if (!bmap)
3525 goto error;
3526 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3527 if (!bmap->dim)
3528 goto error;
3529 bmap = isl_basic_map_finalize(bmap);
3530 return (struct isl_basic_set *)bmap;
3531 error:
3532 isl_basic_map_free(bmap);
3533 return NULL;
3536 /* For a div d = floor(f/m), add the constraints
3538 * f - m d >= 0
3539 * -(f-(n-1)) + m d >= 0
3541 * Note that the second constraint is the negation of
3543 * f - m d >= n
3545 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3546 unsigned pos, isl_int *div)
3548 int i, j;
3549 unsigned total = isl_basic_map_total_dim(bmap);
3551 i = isl_basic_map_alloc_inequality(bmap);
3552 if (i < 0)
3553 return -1;
3554 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3555 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3557 j = isl_basic_map_alloc_inequality(bmap);
3558 if (j < 0)
3559 return -1;
3560 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3561 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3562 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3563 return j;
3566 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3567 unsigned pos, isl_int *div)
3569 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3570 pos, div);
3573 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3575 unsigned total = isl_basic_map_total_dim(bmap);
3576 unsigned div_pos = total - bmap->n_div + div;
3578 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3579 bmap->div[div]);
3582 struct isl_basic_set *isl_basic_map_underlying_set(
3583 struct isl_basic_map *bmap)
3585 if (!bmap)
3586 goto error;
3587 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3588 bmap->n_div == 0 &&
3589 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3590 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3591 return (struct isl_basic_set *)bmap;
3592 bmap = isl_basic_map_cow(bmap);
3593 if (!bmap)
3594 goto error;
3595 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3596 if (!bmap->dim)
3597 goto error;
3598 bmap->extra -= bmap->n_div;
3599 bmap->n_div = 0;
3600 bmap = isl_basic_map_finalize(bmap);
3601 return (struct isl_basic_set *)bmap;
3602 error:
3603 return NULL;
3606 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3607 __isl_take isl_basic_set *bset)
3609 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3612 struct isl_basic_map *isl_basic_map_overlying_set(
3613 struct isl_basic_set *bset, struct isl_basic_map *like)
3615 struct isl_basic_map *bmap;
3616 struct isl_ctx *ctx;
3617 unsigned total;
3618 int i;
3620 if (!bset || !like)
3621 goto error;
3622 ctx = bset->ctx;
3623 isl_assert(ctx, bset->n_div == 0, goto error);
3624 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3625 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3626 goto error);
3627 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3628 isl_basic_map_free(like);
3629 return (struct isl_basic_map *)bset;
3631 bset = isl_basic_set_cow(bset);
3632 if (!bset)
3633 goto error;
3634 total = bset->dim->n_out + bset->extra;
3635 bmap = (struct isl_basic_map *)bset;
3636 isl_dim_free(bmap->dim);
3637 bmap->dim = isl_dim_copy(like->dim);
3638 if (!bmap->dim)
3639 goto error;
3640 bmap->n_div = like->n_div;
3641 bmap->extra += like->n_div;
3642 if (bmap->extra) {
3643 unsigned ltotal;
3644 isl_int **div;
3645 ltotal = total - bmap->extra + like->extra;
3646 if (ltotal > total)
3647 ltotal = total;
3648 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3649 bmap->extra * (1 + 1 + total));
3650 if (isl_blk_is_error(bmap->block2))
3651 goto error;
3652 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3653 if (!div)
3654 goto error;
3655 bmap->div = div;
3656 for (i = 0; i < bmap->extra; ++i)
3657 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3658 for (i = 0; i < like->n_div; ++i) {
3659 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3660 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3662 bmap = isl_basic_map_extend_constraints(bmap,
3663 0, 2 * like->n_div);
3664 for (i = 0; i < like->n_div; ++i) {
3665 if (isl_int_is_zero(bmap->div[i][0]))
3666 continue;
3667 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3668 goto error;
3671 isl_basic_map_free(like);
3672 bmap = isl_basic_map_simplify(bmap);
3673 bmap = isl_basic_map_finalize(bmap);
3674 return bmap;
3675 error:
3676 isl_basic_map_free(like);
3677 isl_basic_set_free(bset);
3678 return NULL;
3681 struct isl_basic_set *isl_basic_set_from_underlying_set(
3682 struct isl_basic_set *bset, struct isl_basic_set *like)
3684 return (struct isl_basic_set *)
3685 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3688 struct isl_set *isl_set_from_underlying_set(
3689 struct isl_set *set, struct isl_basic_set *like)
3691 int i;
3693 if (!set || !like)
3694 goto error;
3695 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3696 goto error);
3697 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3698 isl_basic_set_free(like);
3699 return set;
3701 set = isl_set_cow(set);
3702 if (!set)
3703 goto error;
3704 for (i = 0; i < set->n; ++i) {
3705 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3706 isl_basic_set_copy(like));
3707 if (!set->p[i])
3708 goto error;
3710 isl_dim_free(set->dim);
3711 set->dim = isl_dim_copy(like->dim);
3712 if (!set->dim)
3713 goto error;
3714 isl_basic_set_free(like);
3715 return set;
3716 error:
3717 isl_basic_set_free(like);
3718 isl_set_free(set);
3719 return NULL;
3722 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3724 int i;
3726 map = isl_map_cow(map);
3727 if (!map)
3728 return NULL;
3729 map->dim = isl_dim_cow(map->dim);
3730 if (!map->dim)
3731 goto error;
3733 for (i = 1; i < map->n; ++i)
3734 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3735 goto error);
3736 for (i = 0; i < map->n; ++i) {
3737 map->p[i] = (struct isl_basic_map *)
3738 isl_basic_map_underlying_set(map->p[i]);
3739 if (!map->p[i])
3740 goto error;
3742 if (map->n == 0)
3743 map->dim = isl_dim_underlying(map->dim, 0);
3744 else {
3745 isl_dim_free(map->dim);
3746 map->dim = isl_dim_copy(map->p[0]->dim);
3748 if (!map->dim)
3749 goto error;
3750 return (struct isl_set *)map;
3751 error:
3752 isl_map_free(map);
3753 return NULL;
3756 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3758 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3761 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3762 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3764 bmap = isl_basic_map_cow(bmap);
3765 if (!bmap || !dim)
3766 goto error;
3768 isl_dim_free(bmap->dim);
3769 bmap->dim = dim;
3771 bmap = isl_basic_map_finalize(bmap);
3773 return bmap;
3774 error:
3775 isl_basic_map_free(bmap);
3776 isl_dim_free(dim);
3777 return NULL;
3780 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3781 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3783 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3784 dim);
3787 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3788 __isl_take isl_dim *dim)
3790 int i;
3792 map = isl_map_cow(map);
3793 if (!map || !dim)
3794 goto error;
3796 for (i = 0; i < map->n; ++i) {
3797 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3798 isl_dim_copy(dim));
3799 if (!map->p[i])
3800 goto error;
3802 isl_dim_free(map->dim);
3803 map->dim = dim;
3805 return map;
3806 error:
3807 isl_map_free(map);
3808 isl_dim_free(dim);
3809 return NULL;
3812 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3813 __isl_take isl_dim *dim)
3815 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3818 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3820 isl_dim *dim;
3821 struct isl_basic_set *domain;
3822 unsigned n_in;
3823 unsigned n_out;
3825 if (!bmap)
3826 return NULL;
3827 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3829 n_in = isl_basic_map_n_in(bmap);
3830 n_out = isl_basic_map_n_out(bmap);
3831 domain = isl_basic_set_from_basic_map(bmap);
3832 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3834 domain = isl_basic_set_reset_dim(domain, dim);
3836 return domain;
3839 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
3841 if (!bmap)
3842 return -1;
3843 return isl_dim_may_be_set(bmap->dim);
3846 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3848 if (!bmap)
3849 return NULL;
3850 if (isl_basic_map_may_be_set(bmap))
3851 return bmap;
3852 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3855 __isl_give isl_basic_map *isl_basic_map_domain_map(
3856 __isl_take isl_basic_map *bmap)
3858 int i, k;
3859 isl_dim *dim;
3860 isl_basic_map *domain;
3861 int nparam, n_in, n_out;
3862 unsigned total;
3864 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3865 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3866 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3868 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3869 domain = isl_basic_map_universe(dim);
3871 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3872 bmap = isl_basic_map_apply_range(bmap, domain);
3873 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3875 total = isl_basic_map_total_dim(bmap);
3877 for (i = 0; i < n_in; ++i) {
3878 k = isl_basic_map_alloc_equality(bmap);
3879 if (k < 0)
3880 goto error;
3881 isl_seq_clr(bmap->eq[k], 1 + total);
3882 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3883 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3886 bmap = isl_basic_map_gauss(bmap, NULL);
3887 return isl_basic_map_finalize(bmap);
3888 error:
3889 isl_basic_map_free(bmap);
3890 return NULL;
3893 __isl_give isl_basic_map *isl_basic_map_range_map(
3894 __isl_take isl_basic_map *bmap)
3896 int i, k;
3897 isl_dim *dim;
3898 isl_basic_map *range;
3899 int nparam, n_in, n_out;
3900 unsigned total;
3902 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3903 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3904 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3906 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3907 range = isl_basic_map_universe(dim);
3909 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3910 bmap = isl_basic_map_apply_range(bmap, range);
3911 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3913 total = isl_basic_map_total_dim(bmap);
3915 for (i = 0; i < n_out; ++i) {
3916 k = isl_basic_map_alloc_equality(bmap);
3917 if (k < 0)
3918 goto error;
3919 isl_seq_clr(bmap->eq[k], 1 + total);
3920 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3921 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3924 bmap = isl_basic_map_gauss(bmap, NULL);
3925 return isl_basic_map_finalize(bmap);
3926 error:
3927 isl_basic_map_free(bmap);
3928 return NULL;
3931 int isl_map_may_be_set(__isl_keep isl_map *map)
3933 if (!map)
3934 return -1;
3935 return isl_dim_may_be_set(map->dim);
3938 struct isl_set *isl_map_range(struct isl_map *map)
3940 int i;
3941 struct isl_set *set;
3943 if (!map)
3944 goto error;
3945 if (isl_map_may_be_set(map))
3946 return (isl_set *)map;
3948 map = isl_map_cow(map);
3949 if (!map)
3950 goto error;
3952 set = (struct isl_set *) map;
3953 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3954 if (!set->dim)
3955 goto error;
3956 for (i = 0; i < map->n; ++i) {
3957 set->p[i] = isl_basic_map_range(map->p[i]);
3958 if (!set->p[i])
3959 goto error;
3961 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3962 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3963 return set;
3964 error:
3965 isl_map_free(map);
3966 return NULL;
3969 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3971 int i;
3972 isl_dim *domain_dim;
3974 map = isl_map_cow(map);
3975 if (!map)
3976 return NULL;
3978 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3979 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3980 map->dim = isl_dim_join(map->dim, domain_dim);
3981 if (!map->dim)
3982 goto error;
3983 for (i = 0; i < map->n; ++i) {
3984 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3985 if (!map->p[i])
3986 goto error;
3988 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3989 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3990 return map;
3991 error:
3992 isl_map_free(map);
3993 return NULL;
3996 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3998 int i;
3999 isl_dim *range_dim;
4001 map = isl_map_cow(map);
4002 if (!map)
4003 return NULL;
4005 range_dim = isl_dim_range(isl_map_get_dim(map));
4006 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
4007 map->dim = isl_dim_join(map->dim, range_dim);
4008 if (!map->dim)
4009 goto error;
4010 for (i = 0; i < map->n; ++i) {
4011 map->p[i] = isl_basic_map_range_map(map->p[i]);
4012 if (!map->p[i])
4013 goto error;
4015 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4016 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4017 return map;
4018 error:
4019 isl_map_free(map);
4020 return NULL;
4023 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4025 int i;
4026 struct isl_map *map = NULL;
4028 set = isl_set_cow(set);
4029 if (!set || !dim)
4030 goto error;
4031 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4032 map = (struct isl_map *)set;
4033 for (i = 0; i < set->n; ++i) {
4034 map->p[i] = isl_basic_map_from_basic_set(
4035 set->p[i], isl_dim_copy(dim));
4036 if (!map->p[i])
4037 goto error;
4039 isl_dim_free(map->dim);
4040 map->dim = dim;
4041 return map;
4042 error:
4043 isl_dim_free(dim);
4044 isl_set_free(set);
4045 return NULL;
4048 __isl_give isl_basic_map *isl_basic_map_from_domain(
4049 __isl_take isl_basic_set *bset)
4051 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4054 __isl_give isl_basic_map *isl_basic_map_from_range(
4055 __isl_take isl_basic_set *bset)
4057 return (isl_basic_map *)bset;
4060 struct isl_map *isl_map_from_range(struct isl_set *set)
4062 return (struct isl_map *)set;
4065 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4067 return isl_map_reverse(isl_map_from_range(set));
4070 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4071 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4073 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4074 isl_basic_map_from_range(range));
4077 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4078 __isl_take isl_set *range)
4080 return isl_map_apply_range(isl_map_from_domain(domain),
4081 isl_map_from_range(range));
4084 struct isl_set *isl_set_from_map(struct isl_map *map)
4086 int i;
4087 struct isl_set *set = NULL;
4089 if (!map)
4090 return NULL;
4091 map = isl_map_cow(map);
4092 if (!map)
4093 return NULL;
4094 map->dim = isl_dim_as_set_dim(map->dim);
4095 if (!map->dim)
4096 goto error;
4097 set = (struct isl_set *)map;
4098 for (i = 0; i < map->n; ++i) {
4099 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4100 if (!set->p[i])
4101 goto error;
4103 return set;
4104 error:
4105 isl_map_free(map);
4106 return NULL;
4109 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4111 struct isl_map *map;
4113 if (!dim)
4114 return NULL;
4115 isl_assert(dim->ctx, n >= 0, return NULL);
4116 map = isl_alloc(dim->ctx, struct isl_map,
4117 sizeof(struct isl_map) +
4118 (n - 1) * sizeof(struct isl_basic_map *));
4119 if (!map)
4120 goto error;
4122 map->ctx = dim->ctx;
4123 isl_ctx_ref(map->ctx);
4124 map->ref = 1;
4125 map->size = n;
4126 map->n = 0;
4127 map->dim = dim;
4128 map->flags = flags;
4129 return map;
4130 error:
4131 isl_dim_free(dim);
4132 return NULL;
4135 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4136 unsigned nparam, unsigned in, unsigned out, int n,
4137 unsigned flags)
4139 struct isl_map *map;
4140 struct isl_dim *dims;
4142 dims = isl_dim_alloc(ctx, nparam, in, out);
4143 if (!dims)
4144 return NULL;
4146 map = isl_map_alloc_dim(dims, n, flags);
4147 return map;
4150 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4152 struct isl_basic_map *bmap;
4153 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4154 bmap = isl_basic_map_set_to_empty(bmap);
4155 return bmap;
4158 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4160 struct isl_basic_set *bset;
4161 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4162 bset = isl_basic_set_set_to_empty(bset);
4163 return bset;
4166 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4168 struct isl_basic_map *bmap;
4169 if (!model)
4170 return NULL;
4171 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4172 bmap = isl_basic_map_set_to_empty(bmap);
4173 return bmap;
4176 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4178 struct isl_basic_map *bmap;
4179 if (!model)
4180 return NULL;
4181 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4182 bmap = isl_basic_map_set_to_empty(bmap);
4183 return bmap;
4186 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4188 struct isl_basic_set *bset;
4189 if (!model)
4190 return NULL;
4191 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4192 bset = isl_basic_set_set_to_empty(bset);
4193 return bset;
4196 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4198 struct isl_basic_map *bmap;
4199 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4200 bmap = isl_basic_map_finalize(bmap);
4201 return bmap;
4204 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4206 struct isl_basic_set *bset;
4207 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4208 bset = isl_basic_set_finalize(bset);
4209 return bset;
4212 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4214 int i;
4215 unsigned total = isl_dim_total(dim);
4216 isl_basic_map *bmap;
4218 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4219 for (i = 0; i < total; ++i) {
4220 int k = isl_basic_map_alloc_inequality(bmap);
4221 if (k < 0)
4222 goto error;
4223 isl_seq_clr(bmap->ineq[k], 1 + total);
4224 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4226 return bmap;
4227 error:
4228 isl_basic_map_free(bmap);
4229 return NULL;
4232 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4234 return isl_basic_map_nat_universe(dim);
4237 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4239 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4242 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4244 return isl_map_nat_universe(dim);
4247 __isl_give isl_basic_map *isl_basic_map_universe_like(
4248 __isl_keep isl_basic_map *model)
4250 if (!model)
4251 return NULL;
4252 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4255 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4257 if (!model)
4258 return NULL;
4259 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4262 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4263 __isl_keep isl_set *model)
4265 if (!model)
4266 return NULL;
4267 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4270 struct isl_map *isl_map_empty(struct isl_dim *dim)
4272 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4275 struct isl_map *isl_map_empty_like(struct isl_map *model)
4277 if (!model)
4278 return NULL;
4279 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4282 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4284 if (!model)
4285 return NULL;
4286 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4289 struct isl_set *isl_set_empty(struct isl_dim *dim)
4291 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4294 struct isl_set *isl_set_empty_like(struct isl_set *model)
4296 if (!model)
4297 return NULL;
4298 return isl_set_empty(isl_dim_copy(model->dim));
4301 struct isl_map *isl_map_universe(struct isl_dim *dim)
4303 struct isl_map *map;
4304 if (!dim)
4305 return NULL;
4306 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4307 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4308 return map;
4311 struct isl_set *isl_set_universe(struct isl_dim *dim)
4313 struct isl_set *set;
4314 if (!dim)
4315 return NULL;
4316 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4317 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4318 return set;
4321 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4323 if (!model)
4324 return NULL;
4325 return isl_set_universe(isl_dim_copy(model->dim));
4328 struct isl_map *isl_map_dup(struct isl_map *map)
4330 int i;
4331 struct isl_map *dup;
4333 if (!map)
4334 return NULL;
4335 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4336 for (i = 0; i < map->n; ++i)
4337 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4338 return dup;
4341 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4342 __isl_take isl_basic_map *bmap)
4344 if (!bmap || !map)
4345 goto error;
4346 if (isl_basic_map_plain_is_empty(bmap)) {
4347 isl_basic_map_free(bmap);
4348 return map;
4350 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4351 isl_assert(map->ctx, map->n < map->size, goto error);
4352 map->p[map->n] = bmap;
4353 map->n++;
4354 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4355 return map;
4356 error:
4357 if (map)
4358 isl_map_free(map);
4359 if (bmap)
4360 isl_basic_map_free(bmap);
4361 return NULL;
4364 void isl_map_free(struct isl_map *map)
4366 int i;
4368 if (!map)
4369 return;
4371 if (--map->ref > 0)
4372 return;
4374 isl_ctx_deref(map->ctx);
4375 for (i = 0; i < map->n; ++i)
4376 isl_basic_map_free(map->p[i]);
4377 isl_dim_free(map->dim);
4378 free(map);
4381 struct isl_map *isl_map_extend(struct isl_map *base,
4382 unsigned nparam, unsigned n_in, unsigned n_out)
4384 int i;
4386 base = isl_map_cow(base);
4387 if (!base)
4388 return NULL;
4390 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4391 if (!base->dim)
4392 goto error;
4393 for (i = 0; i < base->n; ++i) {
4394 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4395 isl_dim_copy(base->dim), 0, 0, 0);
4396 if (!base->p[i])
4397 goto error;
4399 return base;
4400 error:
4401 isl_map_free(base);
4402 return NULL;
4405 struct isl_set *isl_set_extend(struct isl_set *base,
4406 unsigned nparam, unsigned dim)
4408 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4409 nparam, 0, dim);
4412 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4413 struct isl_basic_map *bmap, unsigned pos, int value)
4415 int j;
4417 bmap = isl_basic_map_cow(bmap);
4418 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4419 j = isl_basic_map_alloc_equality(bmap);
4420 if (j < 0)
4421 goto error;
4422 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4423 isl_int_set_si(bmap->eq[j][pos], -1);
4424 isl_int_set_si(bmap->eq[j][0], value);
4425 bmap = isl_basic_map_simplify(bmap);
4426 return isl_basic_map_finalize(bmap);
4427 error:
4428 isl_basic_map_free(bmap);
4429 return NULL;
4432 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4433 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4435 int j;
4437 bmap = isl_basic_map_cow(bmap);
4438 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4439 j = isl_basic_map_alloc_equality(bmap);
4440 if (j < 0)
4441 goto error;
4442 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4443 isl_int_set_si(bmap->eq[j][pos], -1);
4444 isl_int_set(bmap->eq[j][0], value);
4445 bmap = isl_basic_map_simplify(bmap);
4446 return isl_basic_map_finalize(bmap);
4447 error:
4448 isl_basic_map_free(bmap);
4449 return NULL;
4452 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4453 enum isl_dim_type type, unsigned pos, int value)
4455 if (!bmap)
4456 return NULL;
4457 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4458 return isl_basic_map_fix_pos_si(bmap,
4459 isl_basic_map_offset(bmap, type) + pos, value);
4460 error:
4461 isl_basic_map_free(bmap);
4462 return NULL;
4465 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4466 enum isl_dim_type type, unsigned pos, isl_int value)
4468 if (!bmap)
4469 return NULL;
4470 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4471 return isl_basic_map_fix_pos(bmap,
4472 isl_basic_map_offset(bmap, type) + pos, value);
4473 error:
4474 isl_basic_map_free(bmap);
4475 return NULL;
4478 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4479 enum isl_dim_type type, unsigned pos, int value)
4481 return (struct isl_basic_set *)
4482 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4483 type, pos, value);
4486 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4487 enum isl_dim_type type, unsigned pos, isl_int value)
4489 return (struct isl_basic_set *)
4490 isl_basic_map_fix((struct isl_basic_map *)bset,
4491 type, pos, value);
4494 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4495 unsigned input, int value)
4497 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4500 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4501 unsigned dim, int value)
4503 return (struct isl_basic_set *)
4504 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4505 isl_dim_set, dim, value);
4508 struct isl_map *isl_map_fix_si(struct isl_map *map,
4509 enum isl_dim_type type, unsigned pos, int value)
4511 int i;
4513 map = isl_map_cow(map);
4514 if (!map)
4515 return NULL;
4517 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4518 for (i = 0; i < map->n; ++i) {
4519 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4520 if (!map->p[i])
4521 goto error;
4523 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4524 return map;
4525 error:
4526 isl_map_free(map);
4527 return NULL;
4530 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4531 enum isl_dim_type type, unsigned pos, int value)
4533 return (struct isl_set *)
4534 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4537 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4538 enum isl_dim_type type, unsigned pos, isl_int value)
4540 int i;
4542 map = isl_map_cow(map);
4543 if (!map)
4544 return NULL;
4546 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4547 for (i = 0; i < map->n; ++i) {
4548 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4549 if (!map->p[i])
4550 goto error;
4552 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4553 return map;
4554 error:
4555 isl_map_free(map);
4556 return NULL;
4559 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4560 enum isl_dim_type type, unsigned pos, isl_int value)
4562 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4565 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4566 unsigned input, int value)
4568 return isl_map_fix_si(map, isl_dim_in, input, value);
4571 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4573 return (struct isl_set *)
4574 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4577 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4578 __isl_take isl_basic_map *bmap,
4579 enum isl_dim_type type, unsigned pos, int value)
4581 int j;
4583 if (!bmap)
4584 return NULL;
4585 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4586 pos += isl_basic_map_offset(bmap, type);
4587 bmap = isl_basic_map_cow(bmap);
4588 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4589 j = isl_basic_map_alloc_inequality(bmap);
4590 if (j < 0)
4591 goto error;
4592 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4593 isl_int_set_si(bmap->ineq[j][pos], 1);
4594 isl_int_set_si(bmap->ineq[j][0], -value);
4595 bmap = isl_basic_map_simplify(bmap);
4596 return isl_basic_map_finalize(bmap);
4597 error:
4598 isl_basic_map_free(bmap);
4599 return NULL;
4602 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4603 unsigned dim, isl_int value)
4605 int j;
4607 bset = isl_basic_set_cow(bset);
4608 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4609 j = isl_basic_set_alloc_inequality(bset);
4610 if (j < 0)
4611 goto error;
4612 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4613 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4614 isl_int_neg(bset->ineq[j][0], value);
4615 bset = isl_basic_set_simplify(bset);
4616 return isl_basic_set_finalize(bset);
4617 error:
4618 isl_basic_set_free(bset);
4619 return NULL;
4622 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4623 enum isl_dim_type type, unsigned pos, int value)
4625 int i;
4627 map = isl_map_cow(map);
4628 if (!map)
4629 return NULL;
4631 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4632 for (i = 0; i < map->n; ++i) {
4633 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4634 type, pos, value);
4635 if (!map->p[i])
4636 goto error;
4638 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4639 return map;
4640 error:
4641 isl_map_free(map);
4642 return NULL;
4645 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4646 enum isl_dim_type type, unsigned pos, int value)
4648 return (struct isl_set *)
4649 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4652 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4653 isl_int value)
4655 int i;
4657 set = isl_set_cow(set);
4658 if (!set)
4659 return NULL;
4661 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4662 for (i = 0; i < set->n; ++i) {
4663 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4664 if (!set->p[i])
4665 goto error;
4667 return set;
4668 error:
4669 isl_set_free(set);
4670 return NULL;
4673 struct isl_map *isl_map_reverse(struct isl_map *map)
4675 int i;
4677 map = isl_map_cow(map);
4678 if (!map)
4679 return NULL;
4681 map->dim = isl_dim_reverse(map->dim);
4682 if (!map->dim)
4683 goto error;
4684 for (i = 0; i < map->n; ++i) {
4685 map->p[i] = isl_basic_map_reverse(map->p[i]);
4686 if (!map->p[i])
4687 goto error;
4689 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4690 return map;
4691 error:
4692 isl_map_free(map);
4693 return NULL;
4696 static struct isl_map *isl_basic_map_partial_lexopt(
4697 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4698 struct isl_set **empty, int max)
4700 if (!bmap)
4701 goto error;
4702 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4703 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4704 else
4705 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4706 error:
4707 isl_basic_map_free(bmap);
4708 isl_basic_set_free(dom);
4709 if (empty)
4710 *empty = NULL;
4711 return NULL;
4714 struct isl_map *isl_basic_map_partial_lexmax(
4715 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4716 struct isl_set **empty)
4718 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4721 struct isl_map *isl_basic_map_partial_lexmin(
4722 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4723 struct isl_set **empty)
4725 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4728 struct isl_set *isl_basic_set_partial_lexmin(
4729 struct isl_basic_set *bset, struct isl_basic_set *dom,
4730 struct isl_set **empty)
4732 return (struct isl_set *)
4733 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4734 dom, empty);
4737 struct isl_set *isl_basic_set_partial_lexmax(
4738 struct isl_basic_set *bset, struct isl_basic_set *dom,
4739 struct isl_set **empty)
4741 return (struct isl_set *)
4742 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4743 dom, empty);
4746 /* Given a basic map "bmap", compute the lexicographically minimal
4747 * (or maximal) image element for each domain element in dom.
4748 * Set *empty to those elements in dom that do not have an image element.
4750 * We first make sure the basic sets in dom are disjoint and then
4751 * simply collect the results over each of the basic sets separately.
4752 * We could probably improve the efficiency a bit by moving the union
4753 * domain down into the parametric integer programming.
4755 static __isl_give isl_map *basic_map_partial_lexopt(
4756 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4757 __isl_give isl_set **empty, int max)
4759 int i;
4760 struct isl_map *res;
4762 dom = isl_set_make_disjoint(dom);
4763 if (!dom)
4764 goto error;
4766 if (isl_set_plain_is_empty(dom)) {
4767 res = isl_map_empty_like_basic_map(bmap);
4768 *empty = isl_set_empty_like(dom);
4769 isl_set_free(dom);
4770 isl_basic_map_free(bmap);
4771 return res;
4774 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4775 isl_basic_set_copy(dom->p[0]), empty, max);
4777 for (i = 1; i < dom->n; ++i) {
4778 struct isl_map *res_i;
4779 struct isl_set *empty_i;
4781 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4782 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4784 res = isl_map_union_disjoint(res, res_i);
4785 *empty = isl_set_union_disjoint(*empty, empty_i);
4788 isl_set_free(dom);
4789 isl_basic_map_free(bmap);
4790 return res;
4791 error:
4792 *empty = NULL;
4793 isl_set_free(dom);
4794 isl_basic_map_free(bmap);
4795 return NULL;
4798 /* Given a map "map", compute the lexicographically minimal
4799 * (or maximal) image element for each domain element in dom.
4800 * Set *empty to those elements in dom that do not have an image element.
4802 * We first compute the lexicographically minimal or maximal element
4803 * in the first basic map. This results in a partial solution "res"
4804 * and a subset "todo" of dom that still need to be handled.
4805 * We then consider each of the remaining maps in "map" and successively
4806 * improve both "res" and "todo".
4808 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4809 * Assume we are computing the lexicographical maximum.
4810 * We first compute the lexicographically maximal element in basic map i.
4811 * This results in a partial solution res_i and a subset todo_i.
4812 * Then we combine these results with those obtain for the first k basic maps
4813 * to obtain a result that is valid for the first k+1 basic maps.
4814 * In particular, the set where there is no solution is the set where
4815 * there is no solution for the first k basic maps and also no solution
4816 * for the ith basic map, i.e.,
4818 * todo^i = todo^k * todo_i
4820 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4821 * solutions, arbitrarily breaking ties in favor of res^k.
4822 * That is, when res^k(a) >= res_i(a), we pick res^k and
4823 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4824 * the lexicographic order.)
4825 * In practice, we compute
4827 * res^k * (res_i . "<=")
4829 * and
4831 * res_i * (res^k . "<")
4833 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4834 * where only one of res^k and res_i provides a solution and we simply pick
4835 * that one, i.e.,
4837 * res^k * todo_i
4838 * and
4839 * res_i * todo^k
4841 * Note that we only compute these intersections when dom(res^k) intersects
4842 * dom(res_i). Otherwise, the only effect of these intersections is to
4843 * potentially break up res^k and res_i into smaller pieces.
4844 * We want to avoid such splintering as much as possible.
4845 * In fact, an earlier implementation of this function would look for
4846 * better results in the domain of res^k and for extra results in todo^k,
4847 * but this would always result in a splintering according to todo^k,
4848 * even when the domain of basic map i is disjoint from the domains of
4849 * the previous basic maps.
4851 static __isl_give isl_map *isl_map_partial_lexopt(
4852 __isl_take isl_map *map, __isl_take isl_set *dom,
4853 __isl_give isl_set **empty, int max)
4855 int i;
4856 struct isl_map *res;
4857 struct isl_set *todo;
4859 if (!map || !dom)
4860 goto error;
4862 if (isl_map_plain_is_empty(map)) {
4863 if (empty)
4864 *empty = dom;
4865 else
4866 isl_set_free(dom);
4867 return map;
4870 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4871 isl_set_copy(dom), &todo, max);
4873 for (i = 1; i < map->n; ++i) {
4874 isl_map *lt, *le;
4875 isl_map *res_i;
4876 isl_set *todo_i;
4877 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4879 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4880 isl_set_copy(dom), &todo_i, max);
4882 if (max) {
4883 lt = isl_map_lex_lt(isl_dim_copy(dim));
4884 le = isl_map_lex_le(dim);
4885 } else {
4886 lt = isl_map_lex_gt(isl_dim_copy(dim));
4887 le = isl_map_lex_ge(dim);
4889 lt = isl_map_apply_range(isl_map_copy(res), lt);
4890 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4891 le = isl_map_apply_range(isl_map_copy(res_i), le);
4892 le = isl_map_intersect(le, isl_map_copy(res));
4894 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4895 res = isl_map_intersect_domain(res,
4896 isl_set_copy(todo_i));
4897 res_i = isl_map_intersect_domain(res_i,
4898 isl_set_copy(todo));
4901 res = isl_map_union_disjoint(res, res_i);
4902 res = isl_map_union_disjoint(res, lt);
4903 res = isl_map_union_disjoint(res, le);
4905 todo = isl_set_intersect(todo, todo_i);
4908 isl_set_free(dom);
4909 isl_map_free(map);
4911 if (empty)
4912 *empty = todo;
4913 else
4914 isl_set_free(todo);
4916 return res;
4917 error:
4918 if (empty)
4919 *empty = NULL;
4920 isl_set_free(dom);
4921 isl_map_free(map);
4922 return NULL;
4925 __isl_give isl_map *isl_map_partial_lexmax(
4926 __isl_take isl_map *map, __isl_take isl_set *dom,
4927 __isl_give isl_set **empty)
4929 return isl_map_partial_lexopt(map, dom, empty, 1);
4932 __isl_give isl_map *isl_map_partial_lexmin(
4933 __isl_take isl_map *map, __isl_take isl_set *dom,
4934 __isl_give isl_set **empty)
4936 return isl_map_partial_lexopt(map, dom, empty, 0);
4939 __isl_give isl_set *isl_set_partial_lexmin(
4940 __isl_take isl_set *set, __isl_take isl_set *dom,
4941 __isl_give isl_set **empty)
4943 return (struct isl_set *)
4944 isl_map_partial_lexmin((struct isl_map *)set,
4945 dom, empty);
4948 __isl_give isl_set *isl_set_partial_lexmax(
4949 __isl_take isl_set *set, __isl_take isl_set *dom,
4950 __isl_give isl_set **empty)
4952 return (struct isl_set *)
4953 isl_map_partial_lexmax((struct isl_map *)set,
4954 dom, empty);
4957 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4959 struct isl_basic_set *dom = NULL;
4960 struct isl_dim *dom_dim;
4962 if (!bmap)
4963 goto error;
4964 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4965 dom = isl_basic_set_universe(dom_dim);
4966 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4967 error:
4968 isl_basic_map_free(bmap);
4969 return NULL;
4972 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4974 return isl_basic_map_lexopt(bmap, 0);
4977 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4979 return isl_basic_map_lexopt(bmap, 1);
4982 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4984 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4987 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4989 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4992 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4994 struct isl_set *dom = NULL;
4995 struct isl_dim *dom_dim;
4997 if (!map)
4998 goto error;
4999 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
5000 dom = isl_set_universe(dom_dim);
5001 return isl_map_partial_lexopt(map, dom, NULL, max);
5002 error:
5003 isl_map_free(map);
5004 return NULL;
5007 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5009 return isl_map_lexopt(map, 0);
5012 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5014 return isl_map_lexopt(map, 1);
5017 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5019 return (isl_set *)isl_map_lexmin((isl_map *)set);
5022 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5024 return (isl_set *)isl_map_lexmax((isl_map *)set);
5027 /* Construct a map that equates the two given dimensions in the given space.
5029 static __isl_give isl_map *equate(__isl_take isl_dim *dim,
5030 enum isl_dim_type src_type, int src_pos,
5031 enum isl_dim_type dst_type, int dst_pos)
5033 isl_basic_map *bmap;
5034 int k;
5036 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
5037 k = isl_basic_map_alloc_equality(bmap);
5038 if (k < 0)
5039 goto error;
5040 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
5041 src_pos += isl_basic_map_offset(bmap, src_type);
5042 dst_pos += isl_basic_map_offset(bmap, dst_type);
5043 isl_int_set_si(bmap->eq[k][src_pos], 1);
5044 isl_int_set_si(bmap->eq[k][dst_pos], -1);
5046 return isl_map_from_basic_map(bmap);
5047 error:
5048 isl_basic_map_free(bmap);
5049 return NULL;
5052 /* Extract the first and only affine expression from list
5053 * and then add it to *pwaff with the given dom.
5054 * This domain is known to be disjoint from other domains
5055 * because of the way isl_basic_set_foreach_lexmax works.
5057 static int update_dim_max(__isl_take isl_basic_set *dom,
5058 __isl_take isl_aff_list *list, void *user)
5060 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5061 isl_aff *aff;
5062 isl_pw_aff **pwaff = user;
5063 isl_pw_aff *pwaff_i;
5065 if (isl_aff_list_n_aff(list) != 1)
5066 isl_die(ctx, isl_error_internal,
5067 "expecting single element list", goto error);
5069 aff = isl_aff_list_get_aff(list, 0);
5070 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5072 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5074 isl_aff_list_free(list);
5076 return 0;
5077 error:
5078 isl_basic_set_free(dom);
5079 isl_aff_list_free(list);
5080 return -1;
5083 /* Given a one-dimensional basic set, compute the maximum of that
5084 * dimension as an isl_pw_aff.
5086 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexmax
5087 * call update_dim_max on each leaf of the result.
5089 static __isl_give isl_pw_aff *basic_set_dim_max(__isl_keep isl_basic_set *bset)
5091 isl_dim *dim = isl_basic_set_get_dim(bset);
5092 isl_pw_aff *pwaff;
5093 int r;
5095 dim = isl_dim_domain(isl_dim_from_range(dim));
5096 pwaff = isl_pw_aff_empty(dim);
5098 r = isl_basic_set_foreach_lexmax(bset, &update_dim_max, &pwaff);
5099 if (r < 0)
5100 return isl_pw_aff_free(pwaff);
5102 return pwaff;
5105 /* Compute the maximum of the given set dimension as a function of the
5106 * parameters, but independently of the other set dimensions.
5108 * We first project the set onto the given dimension and then compute
5109 * the "lexicographic" maximum in each basic set, combining the results
5110 * using isl_pw_aff_max.
5112 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5114 int i;
5115 isl_map *map;
5116 isl_pw_aff *pwaff;
5118 map = isl_map_from_domain(set);
5119 map = isl_map_add_dims(map, isl_dim_out, 1);
5120 map = isl_map_intersect(map,
5121 equate(isl_map_get_dim(map), isl_dim_in, pos,
5122 isl_dim_out, 0));
5123 set = isl_map_range(map);
5124 if (!set)
5125 return NULL;
5127 if (set->n == 0) {
5128 isl_dim *dim = isl_set_get_dim(set);
5129 dim = isl_dim_domain(isl_dim_from_range(dim));
5130 isl_set_free(set);
5131 return isl_pw_aff_empty(dim);
5134 pwaff = basic_set_dim_max(set->p[0]);
5135 for (i = 1; i < set->n; ++i) {
5136 isl_pw_aff *pwaff_i;
5138 pwaff_i = basic_set_dim_max(set->p[i]);
5139 pwaff = isl_pw_aff_max(pwaff, pwaff_i);
5142 isl_set_free(set);
5144 return pwaff;
5147 /* Apply a preimage specified by "mat" on the parameters of "bset".
5148 * bset is assumed to have only parameters and divs.
5150 static struct isl_basic_set *basic_set_parameter_preimage(
5151 struct isl_basic_set *bset, struct isl_mat *mat)
5153 unsigned nparam;
5155 if (!bset || !mat)
5156 goto error;
5158 bset->dim = isl_dim_cow(bset->dim);
5159 if (!bset->dim)
5160 goto error;
5162 nparam = isl_basic_set_dim(bset, isl_dim_param);
5164 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5166 bset->dim->nparam = 0;
5167 bset->dim->n_out = nparam;
5168 bset = isl_basic_set_preimage(bset, mat);
5169 if (bset) {
5170 bset->dim->nparam = bset->dim->n_out;
5171 bset->dim->n_out = 0;
5173 return bset;
5174 error:
5175 isl_mat_free(mat);
5176 isl_basic_set_free(bset);
5177 return NULL;
5180 /* Apply a preimage specified by "mat" on the parameters of "set".
5181 * set is assumed to have only parameters and divs.
5183 static struct isl_set *set_parameter_preimage(
5184 struct isl_set *set, struct isl_mat *mat)
5186 struct isl_dim *dim = NULL;
5187 unsigned nparam;
5189 if (!set || !mat)
5190 goto error;
5192 dim = isl_dim_copy(set->dim);
5193 dim = isl_dim_cow(dim);
5194 if (!dim)
5195 goto error;
5197 nparam = isl_set_dim(set, isl_dim_param);
5199 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5201 dim->nparam = 0;
5202 dim->n_out = nparam;
5203 isl_set_reset_dim(set, dim);
5204 set = isl_set_preimage(set, mat);
5205 if (!set)
5206 goto error2;
5207 dim = isl_dim_copy(set->dim);
5208 dim = isl_dim_cow(dim);
5209 if (!dim)
5210 goto error2;
5211 dim->nparam = dim->n_out;
5212 dim->n_out = 0;
5213 isl_set_reset_dim(set, dim);
5214 return set;
5215 error:
5216 isl_dim_free(dim);
5217 isl_mat_free(mat);
5218 error2:
5219 isl_set_free(set);
5220 return NULL;
5223 /* Intersect the basic set "bset" with the affine space specified by the
5224 * equalities in "eq".
5226 static struct isl_basic_set *basic_set_append_equalities(
5227 struct isl_basic_set *bset, struct isl_mat *eq)
5229 int i, k;
5230 unsigned len;
5232 if (!bset || !eq)
5233 goto error;
5235 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5236 eq->n_row, 0);
5237 if (!bset)
5238 goto error;
5240 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5241 for (i = 0; i < eq->n_row; ++i) {
5242 k = isl_basic_set_alloc_equality(bset);
5243 if (k < 0)
5244 goto error;
5245 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5246 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5248 isl_mat_free(eq);
5250 bset = isl_basic_set_gauss(bset, NULL);
5251 bset = isl_basic_set_finalize(bset);
5253 return bset;
5254 error:
5255 isl_mat_free(eq);
5256 isl_basic_set_free(bset);
5257 return NULL;
5260 /* Intersect the set "set" with the affine space specified by the
5261 * equalities in "eq".
5263 static struct isl_set *set_append_equalities(struct isl_set *set,
5264 struct isl_mat *eq)
5266 int i;
5268 if (!set || !eq)
5269 goto error;
5271 for (i = 0; i < set->n; ++i) {
5272 set->p[i] = basic_set_append_equalities(set->p[i],
5273 isl_mat_copy(eq));
5274 if (!set->p[i])
5275 goto error;
5277 isl_mat_free(eq);
5278 return set;
5279 error:
5280 isl_mat_free(eq);
5281 isl_set_free(set);
5282 return NULL;
5285 /* Project the given basic set onto its parameter domain, possibly introducing
5286 * new, explicit, existential variables in the constraints.
5287 * The input has parameters and (possibly implicit) existential variables.
5288 * The output has the same parameters, but only
5289 * explicit existentially quantified variables.
5291 * The actual projection is performed by pip, but pip doesn't seem
5292 * to like equalities very much, so we first remove the equalities
5293 * among the parameters by performing a variable compression on
5294 * the parameters. Afterward, an inverse transformation is performed
5295 * and the equalities among the parameters are inserted back in.
5297 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5299 int i, j;
5300 struct isl_mat *eq;
5301 struct isl_mat *T, *T2;
5302 struct isl_set *set;
5303 unsigned nparam, n_div;
5305 bset = isl_basic_set_cow(bset);
5306 if (!bset)
5307 return NULL;
5309 if (bset->n_eq == 0)
5310 return isl_basic_set_lexmin(bset);
5312 isl_basic_set_gauss(bset, NULL);
5314 nparam = isl_basic_set_dim(bset, isl_dim_param);
5315 n_div = isl_basic_set_dim(bset, isl_dim_div);
5317 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5318 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5319 ++i;
5321 if (i == bset->n_eq)
5322 return isl_basic_set_lexmin(bset);
5324 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5325 0, 1 + nparam);
5326 eq = isl_mat_cow(eq);
5327 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5328 if (T && T->n_col == 0) {
5329 isl_mat_free(T);
5330 isl_mat_free(T2);
5331 isl_mat_free(eq);
5332 bset = isl_basic_set_set_to_empty(bset);
5333 return isl_set_from_basic_set(bset);
5335 bset = basic_set_parameter_preimage(bset, T);
5337 set = isl_basic_set_lexmin(bset);
5338 set = set_parameter_preimage(set, T2);
5339 set = set_append_equalities(set, eq);
5340 return set;
5343 /* Compute an explicit representation for all the existentially
5344 * quantified variables.
5345 * The input and output dimensions are first turned into parameters.
5346 * compute_divs then returns a map with the same parameters and
5347 * no input or output dimensions and the dimension specification
5348 * is reset to that of the input.
5350 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5352 struct isl_basic_set *bset;
5353 struct isl_set *set;
5354 struct isl_map *map;
5355 struct isl_dim *dim, *orig_dim = NULL;
5356 unsigned nparam;
5357 unsigned n_in;
5358 unsigned n_out;
5360 bmap = isl_basic_map_cow(bmap);
5361 if (!bmap)
5362 return NULL;
5364 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5365 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5366 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5367 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5368 if (!dim)
5369 goto error;
5371 orig_dim = bmap->dim;
5372 bmap->dim = dim;
5373 bset = (struct isl_basic_set *)bmap;
5375 set = parameter_compute_divs(bset);
5376 map = (struct isl_map *)set;
5377 map = isl_map_reset_dim(map, orig_dim);
5379 return map;
5380 error:
5381 isl_basic_map_free(bmap);
5382 return NULL;
5385 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5387 int i;
5388 unsigned off;
5390 if (!bmap)
5391 return -1;
5393 off = isl_dim_total(bmap->dim);
5394 for (i = 0; i < bmap->n_div; ++i) {
5395 if (isl_int_is_zero(bmap->div[i][0]))
5396 return 0;
5397 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5398 return -1);
5400 return 1;
5403 static int map_divs_known(__isl_keep isl_map *map)
5405 int i;
5407 if (!map)
5408 return -1;
5410 for (i = 0; i < map->n; ++i) {
5411 int known = isl_basic_map_divs_known(map->p[i]);
5412 if (known <= 0)
5413 return known;
5416 return 1;
5419 /* If bmap contains any unknown divs, then compute explicit
5420 * expressions for them. However, this computation may be
5421 * quite expensive, so first try to remove divs that aren't
5422 * strictly needed.
5424 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5426 int known;
5427 struct isl_map *map;
5429 known = isl_basic_map_divs_known(bmap);
5430 if (known < 0)
5431 goto error;
5432 if (known)
5433 return isl_map_from_basic_map(bmap);
5435 bmap = isl_basic_map_drop_redundant_divs(bmap);
5437 known = isl_basic_map_divs_known(bmap);
5438 if (known < 0)
5439 goto error;
5440 if (known)
5441 return isl_map_from_basic_map(bmap);
5443 map = compute_divs(bmap);
5444 return map;
5445 error:
5446 isl_basic_map_free(bmap);
5447 return NULL;
5450 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5452 int i;
5453 int known;
5454 struct isl_map *res;
5456 if (!map)
5457 return NULL;
5458 if (map->n == 0)
5459 return map;
5461 known = map_divs_known(map);
5462 if (known < 0) {
5463 isl_map_free(map);
5464 return NULL;
5466 if (known)
5467 return map;
5469 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5470 for (i = 1 ; i < map->n; ++i) {
5471 struct isl_map *r2;
5472 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5473 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5474 res = isl_map_union_disjoint(res, r2);
5475 else
5476 res = isl_map_union(res, r2);
5478 isl_map_free(map);
5480 return res;
5483 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5485 return (struct isl_set *)
5486 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5489 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5491 return (struct isl_set *)
5492 isl_map_compute_divs((struct isl_map *)set);
5495 struct isl_set *isl_map_domain(struct isl_map *map)
5497 int i;
5498 struct isl_set *set;
5500 if (!map)
5501 goto error;
5503 map = isl_map_cow(map);
5504 if (!map)
5505 return NULL;
5507 set = (struct isl_set *)map;
5508 set->dim = isl_dim_domain(set->dim);
5509 if (!set->dim)
5510 goto error;
5511 for (i = 0; i < map->n; ++i) {
5512 set->p[i] = isl_basic_map_domain(map->p[i]);
5513 if (!set->p[i])
5514 goto error;
5516 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5517 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5518 return set;
5519 error:
5520 isl_map_free(map);
5521 return NULL;
5524 struct isl_map *isl_map_union_disjoint(
5525 struct isl_map *map1, struct isl_map *map2)
5527 int i;
5528 unsigned flags = 0;
5529 struct isl_map *map = NULL;
5531 if (!map1 || !map2)
5532 goto error;
5534 if (map1->n == 0) {
5535 isl_map_free(map1);
5536 return map2;
5538 if (map2->n == 0) {
5539 isl_map_free(map2);
5540 return map1;
5543 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5545 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5546 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5547 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5549 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5550 map1->n + map2->n, flags);
5551 if (!map)
5552 goto error;
5553 for (i = 0; i < map1->n; ++i) {
5554 map = isl_map_add_basic_map(map,
5555 isl_basic_map_copy(map1->p[i]));
5556 if (!map)
5557 goto error;
5559 for (i = 0; i < map2->n; ++i) {
5560 map = isl_map_add_basic_map(map,
5561 isl_basic_map_copy(map2->p[i]));
5562 if (!map)
5563 goto error;
5565 isl_map_free(map1);
5566 isl_map_free(map2);
5567 return map;
5568 error:
5569 isl_map_free(map);
5570 isl_map_free(map1);
5571 isl_map_free(map2);
5572 return NULL;
5575 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5577 map1 = isl_map_union_disjoint(map1, map2);
5578 if (!map1)
5579 return NULL;
5580 if (map1->n > 1)
5581 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5582 return map1;
5585 struct isl_set *isl_set_union_disjoint(
5586 struct isl_set *set1, struct isl_set *set2)
5588 return (struct isl_set *)
5589 isl_map_union_disjoint(
5590 (struct isl_map *)set1, (struct isl_map *)set2);
5593 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5595 return (struct isl_set *)
5596 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5599 struct isl_map *isl_map_intersect_range(
5600 struct isl_map *map, struct isl_set *set)
5602 unsigned flags = 0;
5603 struct isl_map *result;
5604 int i, j;
5606 if (!map || !set)
5607 goto error;
5609 if (!isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5610 isl_die(set->ctx, isl_error_invalid,
5611 "parameters don't match", goto error);
5613 if (isl_dim_size(set->dim, isl_dim_set) != 0 &&
5614 !isl_map_compatible_range(map, set))
5615 isl_die(set->ctx, isl_error_invalid,
5616 "incompatible spaces", goto error);
5618 if (isl_set_plain_is_universe(set)) {
5619 isl_set_free(set);
5620 return map;
5623 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5624 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5625 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5627 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5628 map->n * set->n, flags);
5629 if (!result)
5630 goto error;
5631 for (i = 0; i < map->n; ++i)
5632 for (j = 0; j < set->n; ++j) {
5633 result = isl_map_add_basic_map(result,
5634 isl_basic_map_intersect_range(
5635 isl_basic_map_copy(map->p[i]),
5636 isl_basic_set_copy(set->p[j])));
5637 if (!result)
5638 goto error;
5640 isl_map_free(map);
5641 isl_set_free(set);
5642 return result;
5643 error:
5644 isl_map_free(map);
5645 isl_set_free(set);
5646 return NULL;
5649 struct isl_map *isl_map_intersect_domain(
5650 struct isl_map *map, struct isl_set *set)
5652 return isl_map_reverse(
5653 isl_map_intersect_range(isl_map_reverse(map), set));
5656 struct isl_map *isl_map_apply_domain(
5657 struct isl_map *map1, struct isl_map *map2)
5659 if (!map1 || !map2)
5660 goto error;
5661 map1 = isl_map_reverse(map1);
5662 map1 = isl_map_apply_range(map1, map2);
5663 return isl_map_reverse(map1);
5664 error:
5665 isl_map_free(map1);
5666 isl_map_free(map2);
5667 return NULL;
5670 struct isl_map *isl_map_apply_range(
5671 struct isl_map *map1, struct isl_map *map2)
5673 struct isl_dim *dim_result;
5674 struct isl_map *result;
5675 int i, j;
5677 if (!map1 || !map2)
5678 goto error;
5680 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5681 isl_dim_copy(map2->dim));
5683 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5684 if (!result)
5685 goto error;
5686 for (i = 0; i < map1->n; ++i)
5687 for (j = 0; j < map2->n; ++j) {
5688 result = isl_map_add_basic_map(result,
5689 isl_basic_map_apply_range(
5690 isl_basic_map_copy(map1->p[i]),
5691 isl_basic_map_copy(map2->p[j])));
5692 if (!result)
5693 goto error;
5695 isl_map_free(map1);
5696 isl_map_free(map2);
5697 if (result && result->n <= 1)
5698 ISL_F_SET(result, ISL_MAP_DISJOINT);
5699 return result;
5700 error:
5701 isl_map_free(map1);
5702 isl_map_free(map2);
5703 return NULL;
5707 * returns range - domain
5709 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5711 isl_dim *dims, *target_dim;
5712 struct isl_basic_set *bset;
5713 unsigned dim;
5714 unsigned nparam;
5715 int i;
5717 if (!bmap)
5718 goto error;
5719 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5720 bmap->dim, isl_dim_out),
5721 goto error);
5722 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5723 dim = isl_basic_map_n_in(bmap);
5724 nparam = isl_basic_map_n_param(bmap);
5725 bset = isl_basic_set_from_basic_map(bmap);
5726 bset = isl_basic_set_cow(bset);
5727 dims = isl_basic_set_get_dim(bset);
5728 dims = isl_dim_add(dims, isl_dim_set, dim);
5729 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5730 bset = isl_basic_set_swap_vars(bset, 2*dim);
5731 for (i = 0; i < dim; ++i) {
5732 int j = isl_basic_map_alloc_equality(
5733 (struct isl_basic_map *)bset);
5734 if (j < 0)
5735 goto error;
5736 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5737 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5738 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5739 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5741 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5742 bset = isl_basic_set_reset_dim(bset, target_dim);
5743 return bset;
5744 error:
5745 isl_basic_map_free(bmap);
5746 return NULL;
5750 * returns range - domain
5752 struct isl_set *isl_map_deltas(struct isl_map *map)
5754 int i;
5755 isl_dim *dim;
5756 struct isl_set *result;
5758 if (!map)
5759 return NULL;
5761 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5762 map->dim, isl_dim_out),
5763 goto error);
5764 dim = isl_map_get_dim(map);
5765 dim = isl_dim_domain(dim);
5766 result = isl_set_alloc_dim(dim, map->n, 0);
5767 if (!result)
5768 goto error;
5769 for (i = 0; i < map->n; ++i)
5770 result = isl_set_add_basic_set(result,
5771 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5772 isl_map_free(map);
5773 return result;
5774 error:
5775 isl_map_free(map);
5776 return NULL;
5780 * returns [domain -> range] -> range - domain
5782 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5783 __isl_take isl_basic_map *bmap)
5785 int i, k;
5786 isl_dim *dim;
5787 isl_basic_map *domain;
5788 int nparam, n;
5789 unsigned total;
5791 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5792 isl_die(bmap->ctx, isl_error_invalid,
5793 "domain and range don't match", goto error);
5795 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5796 n = isl_basic_map_dim(bmap, isl_dim_in);
5798 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5799 domain = isl_basic_map_universe(dim);
5801 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5802 bmap = isl_basic_map_apply_range(bmap, domain);
5803 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5805 total = isl_basic_map_total_dim(bmap);
5807 for (i = 0; i < n; ++i) {
5808 k = isl_basic_map_alloc_equality(bmap);
5809 if (k < 0)
5810 goto error;
5811 isl_seq_clr(bmap->eq[k], 1 + total);
5812 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5813 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5814 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5817 bmap = isl_basic_map_gauss(bmap, NULL);
5818 return isl_basic_map_finalize(bmap);
5819 error:
5820 isl_basic_map_free(bmap);
5821 return NULL;
5825 * returns [domain -> range] -> range - domain
5827 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5829 int i;
5830 isl_dim *domain_dim;
5832 if (!map)
5833 return NULL;
5835 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5836 isl_die(map->ctx, isl_error_invalid,
5837 "domain and range don't match", goto error);
5839 map = isl_map_cow(map);
5840 if (!map)
5841 return NULL;
5843 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5844 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5845 map->dim = isl_dim_join(map->dim, domain_dim);
5846 if (!map->dim)
5847 goto error;
5848 for (i = 0; i < map->n; ++i) {
5849 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5850 if (!map->p[i])
5851 goto error;
5853 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5854 return map;
5855 error:
5856 isl_map_free(map);
5857 return NULL;
5860 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5862 struct isl_basic_map *bmap;
5863 unsigned nparam;
5864 unsigned dim;
5865 int i;
5867 if (!dims)
5868 return NULL;
5870 nparam = dims->nparam;
5871 dim = dims->n_out;
5872 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5873 if (!bmap)
5874 goto error;
5876 for (i = 0; i < dim; ++i) {
5877 int j = isl_basic_map_alloc_equality(bmap);
5878 if (j < 0)
5879 goto error;
5880 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5881 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5882 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5884 return isl_basic_map_finalize(bmap);
5885 error:
5886 isl_basic_map_free(bmap);
5887 return NULL;
5890 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5892 if (!dim)
5893 return NULL;
5894 if (dim->n_in != dim->n_out)
5895 isl_die(dim->ctx, isl_error_invalid,
5896 "number of input and output dimensions needs to be "
5897 "the same", goto error);
5898 return basic_map_identity(dim);
5899 error:
5900 isl_dim_free(dim);
5901 return NULL;
5904 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5906 if (!model || !model->dim)
5907 return NULL;
5908 return isl_basic_map_identity(isl_dim_copy(model->dim));
5911 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5913 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5916 struct isl_map *isl_map_identity_like(struct isl_map *model)
5918 if (!model || !model->dim)
5919 return NULL;
5920 return isl_map_identity(isl_dim_copy(model->dim));
5923 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5925 if (!model || !model->dim)
5926 return NULL;
5927 return isl_map_identity(isl_dim_copy(model->dim));
5930 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5932 isl_dim *dim = isl_set_get_dim(set);
5933 isl_map *id;
5934 id = isl_map_identity(isl_dim_map_from_set(dim));
5935 return isl_map_intersect_range(id, set);
5938 /* Construct a basic set with all set dimensions having only non-negative
5939 * values.
5941 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5943 int i;
5944 unsigned nparam;
5945 unsigned dim;
5946 struct isl_basic_set *bset;
5948 if (!dims)
5949 return NULL;
5950 nparam = dims->nparam;
5951 dim = dims->n_out;
5952 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5953 if (!bset)
5954 return NULL;
5955 for (i = 0; i < dim; ++i) {
5956 int k = isl_basic_set_alloc_inequality(bset);
5957 if (k < 0)
5958 goto error;
5959 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5960 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5962 return bset;
5963 error:
5964 isl_basic_set_free(bset);
5965 return NULL;
5968 /* Construct the half-space x_pos >= 0.
5970 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5971 int pos)
5973 int k;
5974 isl_basic_set *nonneg;
5976 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5977 k = isl_basic_set_alloc_inequality(nonneg);
5978 if (k < 0)
5979 goto error;
5980 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5981 isl_int_set_si(nonneg->ineq[k][pos], 1);
5983 return isl_basic_set_finalize(nonneg);
5984 error:
5985 isl_basic_set_free(nonneg);
5986 return NULL;
5989 /* Construct the half-space x_pos <= -1.
5991 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5993 int k;
5994 isl_basic_set *neg;
5996 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5997 k = isl_basic_set_alloc_inequality(neg);
5998 if (k < 0)
5999 goto error;
6000 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6001 isl_int_set_si(neg->ineq[k][0], -1);
6002 isl_int_set_si(neg->ineq[k][pos], -1);
6004 return isl_basic_set_finalize(neg);
6005 error:
6006 isl_basic_set_free(neg);
6007 return NULL;
6010 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6011 enum isl_dim_type type, unsigned first, unsigned n)
6013 int i;
6014 isl_basic_set *nonneg;
6015 isl_basic_set *neg;
6017 if (!set)
6018 return NULL;
6019 if (n == 0)
6020 return set;
6022 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6024 for (i = 0; i < n; ++i) {
6025 nonneg = nonneg_halfspace(isl_set_get_dim(set),
6026 pos(set->dim, type) + first + i);
6027 neg = neg_halfspace(isl_set_get_dim(set),
6028 pos(set->dim, type) + first + i);
6030 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6033 return set;
6034 error:
6035 isl_set_free(set);
6036 return NULL;
6039 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6040 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6041 void *user)
6043 isl_set *half;
6045 if (!set)
6046 return -1;
6047 if (isl_set_plain_is_empty(set)) {
6048 isl_set_free(set);
6049 return 0;
6051 if (first == len)
6052 return fn(set, signs, user);
6054 signs[first] = 1;
6055 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
6056 1 + first));
6057 half = isl_set_intersect(half, isl_set_copy(set));
6058 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6059 goto error;
6061 signs[first] = -1;
6062 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
6063 1 + first));
6064 half = isl_set_intersect(half, set);
6065 return foreach_orthant(half, signs, first + 1, len, fn, user);
6066 error:
6067 isl_set_free(set);
6068 return -1;
6071 /* Call "fn" on the intersections of "set" with each of the orthants
6072 * (except for obviously empty intersections). The orthant is identified
6073 * by the signs array, with each entry having value 1 or -1 according
6074 * to the sign of the corresponding variable.
6076 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6077 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6078 void *user)
6080 unsigned nparam;
6081 unsigned nvar;
6082 int *signs;
6083 int r;
6085 if (!set)
6086 return -1;
6087 if (isl_set_plain_is_empty(set))
6088 return 0;
6090 nparam = isl_set_dim(set, isl_dim_param);
6091 nvar = isl_set_dim(set, isl_dim_set);
6093 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6095 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6096 fn, user);
6098 free(signs);
6100 return r;
6103 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6105 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6108 int isl_basic_map_is_subset(
6109 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6111 int is_subset;
6112 struct isl_map *map1;
6113 struct isl_map *map2;
6115 if (!bmap1 || !bmap2)
6116 return -1;
6118 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6119 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6121 is_subset = isl_map_is_subset(map1, map2);
6123 isl_map_free(map1);
6124 isl_map_free(map2);
6126 return is_subset;
6129 int isl_basic_map_is_equal(
6130 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6132 int is_subset;
6134 if (!bmap1 || !bmap2)
6135 return -1;
6136 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6137 if (is_subset != 1)
6138 return is_subset;
6139 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6140 return is_subset;
6143 int isl_basic_set_is_equal(
6144 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6146 return isl_basic_map_is_equal(
6147 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6150 int isl_map_is_empty(struct isl_map *map)
6152 int i;
6153 int is_empty;
6155 if (!map)
6156 return -1;
6157 for (i = 0; i < map->n; ++i) {
6158 is_empty = isl_basic_map_is_empty(map->p[i]);
6159 if (is_empty < 0)
6160 return -1;
6161 if (!is_empty)
6162 return 0;
6164 return 1;
6167 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6169 return map ? map->n == 0 : -1;
6172 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6174 return isl_map_plain_is_empty(map);
6177 int isl_set_plain_is_empty(struct isl_set *set)
6179 return set ? set->n == 0 : -1;
6182 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6184 return isl_set_plain_is_empty(set);
6187 int isl_set_is_empty(struct isl_set *set)
6189 return isl_map_is_empty((struct isl_map *)set);
6192 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6194 if (!map1 || !map2)
6195 return -1;
6197 return isl_dim_equal(map1->dim, map2->dim);
6200 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6202 if (!set1 || !set2)
6203 return -1;
6205 return isl_dim_equal(set1->dim, set2->dim);
6208 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
6210 int is_subset;
6212 if (!map1 || !map2)
6213 return -1;
6214 is_subset = isl_map_is_subset(map1, map2);
6215 if (is_subset != 1)
6216 return is_subset;
6217 is_subset = isl_map_is_subset(map2, map1);
6218 return is_subset;
6221 int isl_basic_map_is_strict_subset(
6222 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6224 int is_subset;
6226 if (!bmap1 || !bmap2)
6227 return -1;
6228 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6229 if (is_subset != 1)
6230 return is_subset;
6231 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6232 if (is_subset == -1)
6233 return is_subset;
6234 return !is_subset;
6237 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6239 int is_subset;
6241 if (!map1 || !map2)
6242 return -1;
6243 is_subset = isl_map_is_subset(map1, map2);
6244 if (is_subset != 1)
6245 return is_subset;
6246 is_subset = isl_map_is_subset(map2, map1);
6247 if (is_subset == -1)
6248 return is_subset;
6249 return !is_subset;
6252 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6254 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6257 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6259 if (!bmap)
6260 return -1;
6261 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6264 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6266 if (!bset)
6267 return -1;
6268 return bset->n_eq == 0 && bset->n_ineq == 0;
6271 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6273 int i;
6275 if (!map)
6276 return -1;
6278 for (i = 0; i < map->n; ++i) {
6279 int r = isl_basic_map_is_universe(map->p[i]);
6280 if (r < 0 || r)
6281 return r;
6284 return 0;
6287 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6289 return isl_map_plain_is_universe((isl_map *) set);
6292 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6294 return isl_set_plain_is_universe(set);
6297 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6299 struct isl_basic_set *bset = NULL;
6300 struct isl_vec *sample = NULL;
6301 int empty;
6302 unsigned total;
6304 if (!bmap)
6305 return -1;
6307 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6308 return 1;
6310 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6311 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6312 copy = isl_basic_map_remove_redundancies(copy);
6313 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6314 isl_basic_map_free(copy);
6315 return empty;
6318 total = 1 + isl_basic_map_total_dim(bmap);
6319 if (bmap->sample && bmap->sample->size == total) {
6320 int contains = isl_basic_map_contains(bmap, bmap->sample);
6321 if (contains < 0)
6322 return -1;
6323 if (contains)
6324 return 0;
6326 isl_vec_free(bmap->sample);
6327 bmap->sample = NULL;
6328 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6329 if (!bset)
6330 return -1;
6331 sample = isl_basic_set_sample_vec(bset);
6332 if (!sample)
6333 return -1;
6334 empty = sample->size == 0;
6335 isl_vec_free(bmap->sample);
6336 bmap->sample = sample;
6337 if (empty)
6338 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6340 return empty;
6343 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6345 if (!bmap)
6346 return -1;
6347 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6350 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6352 return isl_basic_map_plain_is_empty(bmap);
6355 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6357 if (!bset)
6358 return -1;
6359 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6362 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6364 return isl_basic_set_plain_is_empty(bset);
6367 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6369 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6372 struct isl_map *isl_basic_map_union(
6373 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6375 struct isl_map *map;
6376 if (!bmap1 || !bmap2)
6377 return NULL;
6379 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6381 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6382 if (!map)
6383 goto error;
6384 map = isl_map_add_basic_map(map, bmap1);
6385 map = isl_map_add_basic_map(map, bmap2);
6386 return map;
6387 error:
6388 isl_basic_map_free(bmap1);
6389 isl_basic_map_free(bmap2);
6390 return NULL;
6393 struct isl_set *isl_basic_set_union(
6394 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6396 return (struct isl_set *)isl_basic_map_union(
6397 (struct isl_basic_map *)bset1,
6398 (struct isl_basic_map *)bset2);
6401 /* Order divs such that any div only depends on previous divs */
6402 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6404 int i;
6405 unsigned off;
6407 if (!bmap)
6408 return NULL;
6410 off = isl_dim_total(bmap->dim);
6412 for (i = 0; i < bmap->n_div; ++i) {
6413 int pos;
6414 if (isl_int_is_zero(bmap->div[i][0]))
6415 continue;
6416 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6417 bmap->n_div-i);
6418 if (pos == -1)
6419 continue;
6420 isl_basic_map_swap_div(bmap, i, i + pos);
6421 --i;
6423 return bmap;
6426 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6428 return (struct isl_basic_set *)
6429 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6432 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6434 int i;
6436 if (!map)
6437 return 0;
6439 for (i = 0; i < map->n; ++i) {
6440 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6441 if (!map->p[i])
6442 goto error;
6445 return map;
6446 error:
6447 isl_map_free(map);
6448 return NULL;
6451 /* Apply the expansion computed by isl_merge_divs.
6452 * The expansion itself is given by "exp" while the resulting
6453 * list of divs is given by "div".
6455 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6456 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6458 int i, j;
6459 int n_div;
6461 bset = isl_basic_set_cow(bset);
6462 if (!bset || !div)
6463 goto error;
6465 if (div->n_row < bset->n_div)
6466 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6467 "not an expansion", goto error);
6469 bset = isl_basic_map_extend_dim(bset, isl_dim_copy(bset->dim),
6470 div->n_row - bset->n_div, 0,
6471 2 * (div->n_row - bset->n_div));
6473 n_div = bset->n_div;
6474 for (i = n_div; i < div->n_row; ++i)
6475 if (isl_basic_set_alloc_div(bset) < 0)
6476 goto error;
6478 j = n_div - 1;
6479 for (i = div->n_row - 1; i >= 0; --i) {
6480 if (j >= 0 && exp[j] == i) {
6481 if (i != j)
6482 isl_basic_map_swap_div(bset, i, j);
6483 j--;
6484 } else {
6485 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6486 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6487 goto error;
6491 isl_mat_free(div);
6492 return bset;
6493 error:
6494 isl_basic_set_free(bset);
6495 isl_mat_free(div);
6496 return NULL;
6499 /* Look for a div in dst that corresponds to the div "div" in src.
6500 * The divs before "div" in src and dst are assumed to be the same.
6502 * Returns -1 if no corresponding div was found and the position
6503 * of the corresponding div in dst otherwise.
6505 static int find_div(struct isl_basic_map *dst,
6506 struct isl_basic_map *src, unsigned div)
6508 int i;
6510 unsigned total = isl_dim_total(src->dim);
6512 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6513 for (i = div; i < dst->n_div; ++i)
6514 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6515 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6516 dst->n_div - div) == -1)
6517 return i;
6518 return -1;
6521 struct isl_basic_map *isl_basic_map_align_divs(
6522 struct isl_basic_map *dst, struct isl_basic_map *src)
6524 int i;
6525 unsigned total = isl_dim_total(src->dim);
6527 if (!dst || !src)
6528 goto error;
6530 if (src->n_div == 0)
6531 return dst;
6533 for (i = 0; i < src->n_div; ++i)
6534 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6536 src = isl_basic_map_order_divs(src);
6537 dst = isl_basic_map_cow(dst);
6538 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6539 src->n_div, 0, 2 * src->n_div);
6540 if (!dst)
6541 return NULL;
6542 for (i = 0; i < src->n_div; ++i) {
6543 int j = find_div(dst, src, i);
6544 if (j < 0) {
6545 j = isl_basic_map_alloc_div(dst);
6546 if (j < 0)
6547 goto error;
6548 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6549 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6550 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6551 goto error;
6553 if (j != i)
6554 isl_basic_map_swap_div(dst, i, j);
6556 return dst;
6557 error:
6558 isl_basic_map_free(dst);
6559 return NULL;
6562 struct isl_basic_set *isl_basic_set_align_divs(
6563 struct isl_basic_set *dst, struct isl_basic_set *src)
6565 return (struct isl_basic_set *)isl_basic_map_align_divs(
6566 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6569 struct isl_map *isl_map_align_divs(struct isl_map *map)
6571 int i;
6573 if (!map)
6574 return NULL;
6575 if (map->n == 0)
6576 return map;
6577 map = isl_map_compute_divs(map);
6578 map = isl_map_cow(map);
6579 if (!map)
6580 return NULL;
6582 for (i = 1; i < map->n; ++i)
6583 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6584 for (i = 1; i < map->n; ++i)
6585 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6587 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6588 return map;
6591 struct isl_set *isl_set_align_divs(struct isl_set *set)
6593 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6596 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6598 if (!set || !map)
6599 goto error;
6600 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6601 map = isl_map_intersect_domain(map, set);
6602 set = isl_map_range(map);
6603 return set;
6604 error:
6605 isl_set_free(set);
6606 isl_map_free(map);
6607 return NULL;
6610 /* There is no need to cow as removing empty parts doesn't change
6611 * the meaning of the set.
6613 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6615 int i;
6617 if (!map)
6618 return NULL;
6620 for (i = map->n-1; i >= 0; --i) {
6621 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6622 continue;
6623 isl_basic_map_free(map->p[i]);
6624 if (i != map->n-1) {
6625 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6626 map->p[i] = map->p[map->n-1];
6628 map->n--;
6631 return map;
6634 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6636 return (struct isl_set *)
6637 isl_map_remove_empty_parts((struct isl_map *)set);
6640 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6642 struct isl_basic_map *bmap;
6643 if (!map || map->n == 0)
6644 return NULL;
6645 bmap = map->p[map->n-1];
6646 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6647 return isl_basic_map_copy(bmap);
6650 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6652 return (struct isl_basic_set *)
6653 isl_map_copy_basic_map((struct isl_map *)set);
6656 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6657 __isl_keep isl_basic_map *bmap)
6659 int i;
6661 if (!map || !bmap)
6662 goto error;
6663 for (i = map->n-1; i >= 0; --i) {
6664 if (map->p[i] != bmap)
6665 continue;
6666 map = isl_map_cow(map);
6667 if (!map)
6668 goto error;
6669 isl_basic_map_free(map->p[i]);
6670 if (i != map->n-1) {
6671 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6672 map->p[i] = map->p[map->n-1];
6674 map->n--;
6675 return map;
6677 return map;
6678 error:
6679 isl_map_free(map);
6680 return NULL;
6683 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6684 struct isl_basic_set *bset)
6686 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6687 (struct isl_basic_map *)bset);
6690 /* Given two basic sets bset1 and bset2, compute the maximal difference
6691 * between the values of dimension pos in bset1 and those in bset2
6692 * for any common value of the parameters and dimensions preceding pos.
6694 static enum isl_lp_result basic_set_maximal_difference_at(
6695 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6696 int pos, isl_int *opt)
6698 struct isl_dim *dims;
6699 struct isl_basic_map *bmap1 = NULL;
6700 struct isl_basic_map *bmap2 = NULL;
6701 struct isl_ctx *ctx;
6702 struct isl_vec *obj;
6703 unsigned total;
6704 unsigned nparam;
6705 unsigned dim1, dim2;
6706 enum isl_lp_result res;
6708 if (!bset1 || !bset2)
6709 return isl_lp_error;
6711 nparam = isl_basic_set_n_param(bset1);
6712 dim1 = isl_basic_set_n_dim(bset1);
6713 dim2 = isl_basic_set_n_dim(bset2);
6714 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6715 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6716 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6717 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6718 if (!bmap1 || !bmap2)
6719 goto error;
6720 bmap1 = isl_basic_map_cow(bmap1);
6721 bmap1 = isl_basic_map_extend(bmap1, nparam,
6722 pos, (dim1 - pos) + (dim2 - pos),
6723 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6724 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6725 if (!bmap1)
6726 goto error;
6727 total = isl_basic_map_total_dim(bmap1);
6728 ctx = bmap1->ctx;
6729 obj = isl_vec_alloc(ctx, 1 + total);
6730 isl_seq_clr(obj->block.data, 1 + total);
6731 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6732 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6733 if (!obj)
6734 goto error;
6735 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6736 opt, NULL, NULL);
6737 isl_basic_map_free(bmap1);
6738 isl_vec_free(obj);
6739 return res;
6740 error:
6741 isl_basic_map_free(bmap1);
6742 isl_basic_map_free(bmap2);
6743 return isl_lp_error;
6746 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6747 * for any common value of the parameters and dimensions preceding pos
6748 * in both basic sets, the values of dimension pos in bset1 are
6749 * smaller or larger than those in bset2.
6751 * Returns
6752 * 1 if bset1 follows bset2
6753 * -1 if bset1 precedes bset2
6754 * 0 if bset1 and bset2 are incomparable
6755 * -2 if some error occurred.
6757 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6758 struct isl_basic_set *bset2, int pos)
6760 isl_int opt;
6761 enum isl_lp_result res;
6762 int cmp;
6764 isl_int_init(opt);
6766 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6768 if (res == isl_lp_empty)
6769 cmp = 0;
6770 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6771 res == isl_lp_unbounded)
6772 cmp = 1;
6773 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6774 cmp = -1;
6775 else
6776 cmp = -2;
6778 isl_int_clear(opt);
6779 return cmp;
6782 /* Given two basic sets bset1 and bset2, check whether
6783 * for any common value of the parameters and dimensions preceding pos
6784 * there is a value of dimension pos in bset1 that is larger
6785 * than a value of the same dimension in bset2.
6787 * Return
6788 * 1 if there exists such a pair
6789 * 0 if there is no such pair, but there is a pair of equal values
6790 * -1 otherwise
6791 * -2 if some error occurred.
6793 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6794 __isl_keep isl_basic_set *bset2, int pos)
6796 isl_int opt;
6797 enum isl_lp_result res;
6798 int cmp;
6800 isl_int_init(opt);
6802 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6804 if (res == isl_lp_empty)
6805 cmp = -1;
6806 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6807 res == isl_lp_unbounded)
6808 cmp = 1;
6809 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6810 cmp = -1;
6811 else if (res == isl_lp_ok)
6812 cmp = 0;
6813 else
6814 cmp = -2;
6816 isl_int_clear(opt);
6817 return cmp;
6820 /* Given two sets set1 and set2, check whether
6821 * for any common value of the parameters and dimensions preceding pos
6822 * there is a value of dimension pos in set1 that is larger
6823 * than a value of the same dimension in set2.
6825 * Return
6826 * 1 if there exists such a pair
6827 * 0 if there is no such pair, but there is a pair of equal values
6828 * -1 otherwise
6829 * -2 if some error occurred.
6831 int isl_set_follows_at(__isl_keep isl_set *set1,
6832 __isl_keep isl_set *set2, int pos)
6834 int i, j;
6835 int follows = -1;
6837 if (!set1 || !set2)
6838 return -2;
6840 for (i = 0; i < set1->n; ++i)
6841 for (j = 0; j < set2->n; ++j) {
6842 int f;
6843 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6844 if (f == 1 || f == -2)
6845 return f;
6846 if (f > follows)
6847 follows = f;
6850 return follows;
6853 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
6854 unsigned pos, isl_int *val)
6856 int i;
6857 int d;
6858 unsigned total;
6860 if (!bmap)
6861 return -1;
6862 total = isl_basic_map_total_dim(bmap);
6863 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6864 for (; d+1 > pos; --d)
6865 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6866 break;
6867 if (d != pos)
6868 continue;
6869 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6870 return 0;
6871 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6872 return 0;
6873 if (!isl_int_is_one(bmap->eq[i][1+d]))
6874 return 0;
6875 if (val)
6876 isl_int_neg(*val, bmap->eq[i][0]);
6877 return 1;
6879 return 0;
6882 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
6883 unsigned pos, isl_int *val)
6885 int i;
6886 isl_int v;
6887 isl_int tmp;
6888 int fixed;
6890 if (!map)
6891 return -1;
6892 if (map->n == 0)
6893 return 0;
6894 if (map->n == 1)
6895 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
6896 isl_int_init(v);
6897 isl_int_init(tmp);
6898 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
6899 for (i = 1; fixed == 1 && i < map->n; ++i) {
6900 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
6901 if (fixed == 1 && isl_int_ne(tmp, v))
6902 fixed = 0;
6904 if (val)
6905 isl_int_set(*val, v);
6906 isl_int_clear(tmp);
6907 isl_int_clear(v);
6908 return fixed;
6911 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
6912 unsigned pos, isl_int *val)
6914 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
6915 pos, val);
6918 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
6919 isl_int *val)
6921 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
6924 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
6925 enum isl_dim_type type, unsigned pos, isl_int *val)
6927 if (pos >= isl_basic_map_dim(bmap, type))
6928 return -1;
6929 return isl_basic_map_plain_has_fixed_var(bmap,
6930 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6933 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
6934 enum isl_dim_type type, unsigned pos, isl_int *val)
6936 if (pos >= isl_map_dim(map, type))
6937 return -1;
6938 return isl_map_plain_has_fixed_var(map,
6939 map_offset(map, type) - 1 + pos, val);
6942 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
6943 enum isl_dim_type type, unsigned pos, isl_int *val)
6945 return isl_map_plain_is_fixed(map, type, pos, val);
6948 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6949 * then return this fixed value in *val.
6951 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
6952 unsigned dim, isl_int *val)
6954 return isl_basic_set_plain_has_fixed_var(bset,
6955 isl_basic_set_n_param(bset) + dim, val);
6958 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6959 * then return this fixed value in *val.
6961 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
6962 unsigned dim, isl_int *val)
6964 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6967 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
6968 unsigned dim, isl_int *val)
6970 return isl_set_plain_dim_is_fixed(set, dim, val);
6973 /* Check if input variable in has fixed value and if so and if val is not NULL,
6974 * then return this fixed value in *val.
6976 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
6977 unsigned in, isl_int *val)
6979 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
6982 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6983 * and if val is not NULL, then return this lower bound in *val.
6985 int isl_basic_set_plain_dim_has_fixed_lower_bound(
6986 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
6988 int i, i_eq = -1, i_ineq = -1;
6989 isl_int *c;
6990 unsigned total;
6991 unsigned nparam;
6993 if (!bset)
6994 return -1;
6995 total = isl_basic_set_total_dim(bset);
6996 nparam = isl_basic_set_n_param(bset);
6997 for (i = 0; i < bset->n_eq; ++i) {
6998 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6999 continue;
7000 if (i_eq != -1)
7001 return 0;
7002 i_eq = i;
7004 for (i = 0; i < bset->n_ineq; ++i) {
7005 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7006 continue;
7007 if (i_eq != -1 || i_ineq != -1)
7008 return 0;
7009 i_ineq = i;
7011 if (i_eq == -1 && i_ineq == -1)
7012 return 0;
7013 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7014 /* The coefficient should always be one due to normalization. */
7015 if (!isl_int_is_one(c[1+nparam+dim]))
7016 return 0;
7017 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7018 return 0;
7019 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7020 total - nparam - dim - 1) != -1)
7021 return 0;
7022 if (val)
7023 isl_int_neg(*val, c[0]);
7024 return 1;
7027 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7028 unsigned dim, isl_int *val)
7030 int i;
7031 isl_int v;
7032 isl_int tmp;
7033 int fixed;
7035 if (!set)
7036 return -1;
7037 if (set->n == 0)
7038 return 0;
7039 if (set->n == 1)
7040 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7041 dim, val);
7042 isl_int_init(v);
7043 isl_int_init(tmp);
7044 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7045 dim, &v);
7046 for (i = 1; fixed == 1 && i < set->n; ++i) {
7047 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7048 dim, &tmp);
7049 if (fixed == 1 && isl_int_ne(tmp, v))
7050 fixed = 0;
7052 if (val)
7053 isl_int_set(*val, v);
7054 isl_int_clear(tmp);
7055 isl_int_clear(v);
7056 return fixed;
7059 struct constraint {
7060 unsigned size;
7061 isl_int *c;
7064 /* uset_gist depends on constraints without existentially quantified
7065 * variables sorting first.
7067 static int qsort_constraint_cmp(const void *p1, const void *p2)
7069 const struct constraint *c1 = (const struct constraint *)p1;
7070 const struct constraint *c2 = (const struct constraint *)p2;
7071 int l1, l2;
7072 unsigned size = isl_min(c1->size, c2->size);
7074 l1 = isl_seq_last_non_zero(c1->c, size);
7075 l2 = isl_seq_last_non_zero(c2->c, size);
7077 if (l1 != l2)
7078 return l1 - l2;
7080 return isl_seq_cmp(c1->c, c2->c, size);
7083 static struct isl_basic_map *isl_basic_map_sort_constraints(
7084 struct isl_basic_map *bmap)
7086 int i;
7087 struct constraint *c;
7088 unsigned total;
7090 if (!bmap)
7091 return NULL;
7092 total = isl_basic_map_total_dim(bmap);
7093 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7094 if (!c)
7095 goto error;
7096 for (i = 0; i < bmap->n_ineq; ++i) {
7097 c[i].size = total;
7098 c[i].c = bmap->ineq[i];
7100 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7101 for (i = 0; i < bmap->n_ineq; ++i)
7102 bmap->ineq[i] = c[i].c;
7103 free(c);
7104 return bmap;
7105 error:
7106 isl_basic_map_free(bmap);
7107 return NULL;
7110 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7111 __isl_take isl_basic_set *bset)
7113 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7114 (struct isl_basic_map *)bset);
7117 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7119 if (!bmap)
7120 return NULL;
7121 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7122 return bmap;
7123 bmap = isl_basic_map_remove_redundancies(bmap);
7124 bmap = isl_basic_map_sort_constraints(bmap);
7125 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7126 return bmap;
7129 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7131 return (struct isl_basic_set *)isl_basic_map_normalize(
7132 (struct isl_basic_map *)bset);
7135 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7136 const __isl_keep isl_basic_map *bmap2)
7138 int i, cmp;
7139 unsigned total;
7141 if (bmap1 == bmap2)
7142 return 0;
7143 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7144 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7145 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7146 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7147 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7148 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7149 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7150 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7151 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7152 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7153 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7154 return 0;
7155 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7156 return 1;
7157 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7158 return -1;
7159 if (bmap1->n_eq != bmap2->n_eq)
7160 return bmap1->n_eq - bmap2->n_eq;
7161 if (bmap1->n_ineq != bmap2->n_ineq)
7162 return bmap1->n_ineq - bmap2->n_ineq;
7163 if (bmap1->n_div != bmap2->n_div)
7164 return bmap1->n_div - bmap2->n_div;
7165 total = isl_basic_map_total_dim(bmap1);
7166 for (i = 0; i < bmap1->n_eq; ++i) {
7167 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7168 if (cmp)
7169 return cmp;
7171 for (i = 0; i < bmap1->n_ineq; ++i) {
7172 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7173 if (cmp)
7174 return cmp;
7176 for (i = 0; i < bmap1->n_div; ++i) {
7177 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7178 if (cmp)
7179 return cmp;
7181 return 0;
7184 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7185 __isl_keep isl_basic_map *bmap2)
7187 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7190 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7191 __isl_keep isl_basic_set *bset2)
7193 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7194 (isl_basic_map *)bset2);
7197 static int qsort_bmap_cmp(const void *p1, const void *p2)
7199 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7200 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7202 return isl_basic_map_plain_cmp(bmap1, bmap2);
7205 /* We normalize in place, but if anything goes wrong we need
7206 * to return NULL, so we need to make sure we don't change the
7207 * meaning of any possible other copies of map.
7209 struct isl_map *isl_map_normalize(struct isl_map *map)
7211 int i, j;
7212 struct isl_basic_map *bmap;
7214 if (!map)
7215 return NULL;
7216 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7217 return map;
7218 for (i = 0; i < map->n; ++i) {
7219 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7220 if (!bmap)
7221 goto error;
7222 isl_basic_map_free(map->p[i]);
7223 map->p[i] = bmap;
7225 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7226 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7227 map = isl_map_remove_empty_parts(map);
7228 if (!map)
7229 return NULL;
7230 for (i = map->n - 1; i >= 1; --i) {
7231 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7232 continue;
7233 isl_basic_map_free(map->p[i-1]);
7234 for (j = i; j < map->n; ++j)
7235 map->p[j-1] = map->p[j];
7236 map->n--;
7238 return map;
7239 error:
7240 isl_map_free(map);
7241 return NULL;
7245 struct isl_set *isl_set_normalize(struct isl_set *set)
7247 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7250 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7252 int i;
7253 int equal;
7255 if (!map1 || !map2)
7256 return -1;
7258 if (map1 == map2)
7259 return 1;
7260 if (!isl_dim_equal(map1->dim, map2->dim))
7261 return 0;
7263 map1 = isl_map_copy(map1);
7264 map2 = isl_map_copy(map2);
7265 map1 = isl_map_normalize(map1);
7266 map2 = isl_map_normalize(map2);
7267 if (!map1 || !map2)
7268 goto error;
7269 equal = map1->n == map2->n;
7270 for (i = 0; equal && i < map1->n; ++i) {
7271 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7272 if (equal < 0)
7273 goto error;
7275 isl_map_free(map1);
7276 isl_map_free(map2);
7277 return equal;
7278 error:
7279 isl_map_free(map1);
7280 isl_map_free(map2);
7281 return -1;
7284 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7286 return isl_map_plain_is_equal(map1, map2);
7289 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7291 return isl_map_plain_is_equal((struct isl_map *)set1,
7292 (struct isl_map *)set2);
7295 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7297 return isl_set_plain_is_equal(set1, set2);
7300 /* Return an interval that ranges from min to max (inclusive)
7302 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7303 isl_int min, isl_int max)
7305 int k;
7306 struct isl_basic_set *bset = NULL;
7308 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7309 if (!bset)
7310 goto error;
7312 k = isl_basic_set_alloc_inequality(bset);
7313 if (k < 0)
7314 goto error;
7315 isl_int_set_si(bset->ineq[k][1], 1);
7316 isl_int_neg(bset->ineq[k][0], min);
7318 k = isl_basic_set_alloc_inequality(bset);
7319 if (k < 0)
7320 goto error;
7321 isl_int_set_si(bset->ineq[k][1], -1);
7322 isl_int_set(bset->ineq[k][0], max);
7324 return bset;
7325 error:
7326 isl_basic_set_free(bset);
7327 return NULL;
7330 /* Return the Cartesian product of the basic sets in list (in the given order).
7332 __isl_give isl_basic_set *isl_basic_set_list_product(
7333 __isl_take struct isl_basic_set_list *list)
7335 int i;
7336 unsigned dim;
7337 unsigned nparam;
7338 unsigned extra;
7339 unsigned n_eq;
7340 unsigned n_ineq;
7341 struct isl_basic_set *product = NULL;
7343 if (!list)
7344 goto error;
7345 isl_assert(list->ctx, list->n > 0, goto error);
7346 isl_assert(list->ctx, list->p[0], goto error);
7347 nparam = isl_basic_set_n_param(list->p[0]);
7348 dim = isl_basic_set_n_dim(list->p[0]);
7349 extra = list->p[0]->n_div;
7350 n_eq = list->p[0]->n_eq;
7351 n_ineq = list->p[0]->n_ineq;
7352 for (i = 1; i < list->n; ++i) {
7353 isl_assert(list->ctx, list->p[i], goto error);
7354 isl_assert(list->ctx,
7355 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7356 dim += isl_basic_set_n_dim(list->p[i]);
7357 extra += list->p[i]->n_div;
7358 n_eq += list->p[i]->n_eq;
7359 n_ineq += list->p[i]->n_ineq;
7361 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7362 n_eq, n_ineq);
7363 if (!product)
7364 goto error;
7365 dim = 0;
7366 for (i = 0; i < list->n; ++i) {
7367 isl_basic_set_add_constraints(product,
7368 isl_basic_set_copy(list->p[i]), dim);
7369 dim += isl_basic_set_n_dim(list->p[i]);
7371 isl_basic_set_list_free(list);
7372 return product;
7373 error:
7374 isl_basic_set_free(product);
7375 isl_basic_set_list_free(list);
7376 return NULL;
7379 struct isl_basic_map *isl_basic_map_product(
7380 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7382 struct isl_dim *dim_result = NULL;
7383 struct isl_basic_map *bmap;
7384 unsigned in1, in2, out1, out2, nparam, total, pos;
7385 struct isl_dim_map *dim_map1, *dim_map2;
7387 if (!bmap1 || !bmap2)
7388 goto error;
7390 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7391 bmap2->dim, isl_dim_param), goto error);
7392 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7393 isl_dim_copy(bmap2->dim));
7395 in1 = isl_basic_map_n_in(bmap1);
7396 in2 = isl_basic_map_n_in(bmap2);
7397 out1 = isl_basic_map_n_out(bmap1);
7398 out2 = isl_basic_map_n_out(bmap2);
7399 nparam = isl_basic_map_n_param(bmap1);
7401 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7402 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7403 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7404 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7405 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7406 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7407 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7408 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7409 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7410 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7411 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7413 bmap = isl_basic_map_alloc_dim(dim_result,
7414 bmap1->n_div + bmap2->n_div,
7415 bmap1->n_eq + bmap2->n_eq,
7416 bmap1->n_ineq + bmap2->n_ineq);
7417 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7418 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7419 bmap = isl_basic_map_simplify(bmap);
7420 return isl_basic_map_finalize(bmap);
7421 error:
7422 isl_basic_map_free(bmap1);
7423 isl_basic_map_free(bmap2);
7424 return NULL;
7427 __isl_give isl_basic_map *isl_basic_map_flat_product(
7428 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7430 isl_basic_map *prod;
7432 prod = isl_basic_map_product(bmap1, bmap2);
7433 prod = isl_basic_map_flatten(prod);
7434 return prod;
7437 __isl_give isl_basic_set *isl_basic_set_flat_product(
7438 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7440 return isl_basic_map_flat_product(bset1, bset2);
7443 __isl_give isl_basic_map *isl_basic_map_range_product(
7444 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7446 isl_dim *dim_result = NULL;
7447 isl_basic_map *bmap;
7448 unsigned in, out1, out2, nparam, total, pos;
7449 struct isl_dim_map *dim_map1, *dim_map2;
7451 if (!bmap1 || !bmap2)
7452 goto error;
7454 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7455 isl_dim_copy(bmap2->dim));
7457 in = isl_basic_map_dim(bmap1, isl_dim_in);
7458 out1 = isl_basic_map_n_out(bmap1);
7459 out2 = isl_basic_map_n_out(bmap2);
7460 nparam = isl_basic_map_n_param(bmap1);
7462 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7463 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7464 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7465 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7466 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7467 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7468 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7469 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7470 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7471 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7472 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7474 bmap = isl_basic_map_alloc_dim(dim_result,
7475 bmap1->n_div + bmap2->n_div,
7476 bmap1->n_eq + bmap2->n_eq,
7477 bmap1->n_ineq + bmap2->n_ineq);
7478 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7479 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7480 bmap = isl_basic_map_simplify(bmap);
7481 return isl_basic_map_finalize(bmap);
7482 error:
7483 isl_basic_map_free(bmap1);
7484 isl_basic_map_free(bmap2);
7485 return NULL;
7488 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
7489 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7491 isl_basic_map *prod;
7493 prod = isl_basic_map_range_product(bmap1, bmap2);
7494 prod = isl_basic_map_flatten_range(prod);
7495 return prod;
7498 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7499 __isl_take isl_map *map2,
7500 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7501 __isl_take isl_dim *right),
7502 __isl_give isl_basic_map *(*basic_map_product)(
7503 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7505 unsigned flags = 0;
7506 struct isl_map *result;
7507 int i, j;
7509 if (!map1 || !map2)
7510 goto error;
7512 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7513 map2->dim, isl_dim_param), goto error);
7515 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7516 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7517 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7519 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7520 isl_dim_copy(map2->dim)),
7521 map1->n * map2->n, flags);
7522 if (!result)
7523 goto error;
7524 for (i = 0; i < map1->n; ++i)
7525 for (j = 0; j < map2->n; ++j) {
7526 struct isl_basic_map *part;
7527 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7528 isl_basic_map_copy(map2->p[j]));
7529 if (isl_basic_map_is_empty(part))
7530 isl_basic_map_free(part);
7531 else
7532 result = isl_map_add_basic_map(result, part);
7533 if (!result)
7534 goto error;
7536 isl_map_free(map1);
7537 isl_map_free(map2);
7538 return result;
7539 error:
7540 isl_map_free(map1);
7541 isl_map_free(map2);
7542 return NULL;
7545 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7547 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7549 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7552 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7554 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7555 __isl_take isl_map *map2)
7557 isl_map *prod;
7559 prod = isl_map_product(map1, map2);
7560 prod = isl_map_flatten(prod);
7561 return prod;
7564 /* Given two set A and B, construct its Cartesian product A x B.
7566 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7568 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7569 (struct isl_map *)set2);
7572 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7573 __isl_take isl_set *set2)
7575 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7578 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7580 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7581 __isl_take isl_map *map2)
7583 return map_product(map1, map2, &isl_dim_range_product,
7584 &isl_basic_map_range_product);
7587 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7589 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
7590 __isl_take isl_map *map2)
7592 isl_map *prod;
7594 prod = isl_map_range_product(map1, map2);
7595 prod = isl_map_flatten_range(prod);
7596 return prod;
7599 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7601 int i;
7602 uint32_t hash = isl_hash_init();
7603 unsigned total;
7605 if (!bmap)
7606 return 0;
7607 bmap = isl_basic_map_copy(bmap);
7608 bmap = isl_basic_map_normalize(bmap);
7609 if (!bmap)
7610 return 0;
7611 total = isl_basic_map_total_dim(bmap);
7612 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7613 for (i = 0; i < bmap->n_eq; ++i) {
7614 uint32_t c_hash;
7615 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7616 isl_hash_hash(hash, c_hash);
7618 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7619 for (i = 0; i < bmap->n_ineq; ++i) {
7620 uint32_t c_hash;
7621 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7622 isl_hash_hash(hash, c_hash);
7624 isl_hash_byte(hash, bmap->n_div & 0xFF);
7625 for (i = 0; i < bmap->n_div; ++i) {
7626 uint32_t c_hash;
7627 if (isl_int_is_zero(bmap->div[i][0]))
7628 continue;
7629 isl_hash_byte(hash, i & 0xFF);
7630 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7631 isl_hash_hash(hash, c_hash);
7633 isl_basic_map_free(bmap);
7634 return hash;
7637 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7639 return isl_basic_map_get_hash((isl_basic_map *)bset);
7642 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7644 int i;
7645 uint32_t hash;
7647 if (!map)
7648 return 0;
7649 map = isl_map_copy(map);
7650 map = isl_map_normalize(map);
7651 if (!map)
7652 return 0;
7654 hash = isl_hash_init();
7655 for (i = 0; i < map->n; ++i) {
7656 uint32_t bmap_hash;
7657 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7658 isl_hash_hash(hash, bmap_hash);
7661 isl_map_free(map);
7663 return hash;
7666 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7668 return isl_map_get_hash((isl_map *)set);
7671 /* Check if the value for dimension dim is completely determined
7672 * by the values of the other parameters and variables.
7673 * That is, check if dimension dim is involved in an equality.
7675 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7677 int i;
7678 unsigned nparam;
7680 if (!bset)
7681 return -1;
7682 nparam = isl_basic_set_n_param(bset);
7683 for (i = 0; i < bset->n_eq; ++i)
7684 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7685 return 1;
7686 return 0;
7689 /* Check if the value for dimension dim is completely determined
7690 * by the values of the other parameters and variables.
7691 * That is, check if dimension dim is involved in an equality
7692 * for each of the subsets.
7694 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7696 int i;
7698 if (!set)
7699 return -1;
7700 for (i = 0; i < set->n; ++i) {
7701 int unique;
7702 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7703 if (unique != 1)
7704 return unique;
7706 return 1;
7709 int isl_set_n_basic_set(__isl_keep isl_set *set)
7711 return set ? set->n : 0;
7714 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7715 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7717 int i;
7719 if (!map)
7720 return -1;
7722 for (i = 0; i < map->n; ++i)
7723 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7724 return -1;
7726 return 0;
7729 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7730 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7732 int i;
7734 if (!set)
7735 return -1;
7737 for (i = 0; i < set->n; ++i)
7738 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7739 return -1;
7741 return 0;
7744 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7746 struct isl_dim *dim;
7748 if (!bset)
7749 return NULL;
7751 bset = isl_basic_set_cow(bset);
7752 if (!bset)
7753 return NULL;
7755 dim = isl_basic_set_get_dim(bset);
7756 dim = isl_dim_lift(dim, bset->n_div);
7757 if (!dim)
7758 goto error;
7759 isl_dim_free(bset->dim);
7760 bset->dim = dim;
7761 bset->extra -= bset->n_div;
7762 bset->n_div = 0;
7764 bset = isl_basic_set_finalize(bset);
7766 return bset;
7767 error:
7768 isl_basic_set_free(bset);
7769 return NULL;
7772 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7774 int i;
7775 struct isl_dim *dim;
7776 unsigned n_div;
7778 set = isl_set_align_divs(set);
7780 if (!set)
7781 return NULL;
7783 set = isl_set_cow(set);
7784 if (!set)
7785 return NULL;
7787 n_div = set->p[0]->n_div;
7788 dim = isl_set_get_dim(set);
7789 dim = isl_dim_lift(dim, n_div);
7790 if (!dim)
7791 goto error;
7792 isl_dim_free(set->dim);
7793 set->dim = dim;
7795 for (i = 0; i < set->n; ++i) {
7796 set->p[i] = isl_basic_set_lift(set->p[i]);
7797 if (!set->p[i])
7798 goto error;
7801 return set;
7802 error:
7803 isl_set_free(set);
7804 return NULL;
7807 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7809 struct isl_dim *dim;
7810 struct isl_basic_map *bmap;
7811 unsigned n_set;
7812 unsigned n_div;
7813 unsigned n_param;
7814 unsigned total;
7815 int i, k, l;
7817 set = isl_set_align_divs(set);
7819 if (!set)
7820 return NULL;
7822 dim = isl_set_get_dim(set);
7823 if (set->n == 0 || set->p[0]->n_div == 0) {
7824 isl_set_free(set);
7825 return isl_map_identity(isl_dim_map_from_set(dim));
7828 n_div = set->p[0]->n_div;
7829 dim = isl_dim_map_from_set(dim);
7830 n_param = isl_dim_size(dim, isl_dim_param);
7831 n_set = isl_dim_size(dim, isl_dim_in);
7832 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7833 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7834 for (i = 0; i < n_set; ++i)
7835 bmap = var_equal(bmap, i);
7837 total = n_param + n_set + n_set + n_div;
7838 for (i = 0; i < n_div; ++i) {
7839 k = isl_basic_map_alloc_inequality(bmap);
7840 if (k < 0)
7841 goto error;
7842 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7843 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7844 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7845 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7846 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7847 set->p[0]->div[i][0]);
7849 l = isl_basic_map_alloc_inequality(bmap);
7850 if (l < 0)
7851 goto error;
7852 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7853 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7854 set->p[0]->div[i][0]);
7855 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7858 isl_set_free(set);
7859 bmap = isl_basic_map_simplify(bmap);
7860 bmap = isl_basic_map_finalize(bmap);
7861 return isl_map_from_basic_map(bmap);
7862 error:
7863 isl_set_free(set);
7864 isl_basic_map_free(bmap);
7865 return NULL;
7868 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7870 unsigned dim;
7871 int size = 0;
7873 if (!bset)
7874 return -1;
7876 dim = isl_basic_set_total_dim(bset);
7877 size += bset->n_eq * (1 + dim);
7878 size += bset->n_ineq * (1 + dim);
7879 size += bset->n_div * (2 + dim);
7881 return size;
7884 int isl_set_size(__isl_keep isl_set *set)
7886 int i;
7887 int size = 0;
7889 if (!set)
7890 return -1;
7892 for (i = 0; i < set->n; ++i)
7893 size += isl_basic_set_size(set->p[i]);
7895 return size;
7898 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7899 enum isl_dim_type type, unsigned pos)
7901 int i;
7902 int lower, upper;
7904 if (!bmap)
7905 return -1;
7907 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7909 pos += isl_basic_map_offset(bmap, type);
7911 for (i = 0; i < bmap->n_eq; ++i)
7912 if (!isl_int_is_zero(bmap->eq[i][pos]))
7913 return 1;
7915 lower = upper = 0;
7916 for (i = 0; i < bmap->n_ineq; ++i) {
7917 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7918 if (sgn > 0)
7919 lower = 1;
7920 if (sgn < 0)
7921 upper = 1;
7924 return lower && upper;
7927 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7928 enum isl_dim_type type, unsigned pos)
7930 int i;
7932 if (!map)
7933 return -1;
7935 for (i = 0; i < map->n; ++i) {
7936 int bounded;
7937 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7938 if (bounded < 0 || !bounded)
7939 return bounded;
7942 return 1;
7945 /* Return 1 if the specified dim is involved in both an upper bound
7946 * and a lower bound.
7948 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7949 enum isl_dim_type type, unsigned pos)
7951 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7954 /* For each of the "n" variables starting at "first", determine
7955 * the sign of the variable and put the results in the first "n"
7956 * elements of the array "signs".
7957 * Sign
7958 * 1 means that the variable is non-negative
7959 * -1 means that the variable is non-positive
7960 * 0 means the variable attains both positive and negative values.
7962 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7963 unsigned first, unsigned n, int *signs)
7965 isl_vec *bound = NULL;
7966 struct isl_tab *tab = NULL;
7967 struct isl_tab_undo *snap;
7968 int i;
7970 if (!bset || !signs)
7971 return -1;
7973 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7974 tab = isl_tab_from_basic_set(bset);
7975 if (!bound || !tab)
7976 goto error;
7978 isl_seq_clr(bound->el, bound->size);
7979 isl_int_set_si(bound->el[0], -1);
7981 snap = isl_tab_snap(tab);
7982 for (i = 0; i < n; ++i) {
7983 int empty;
7985 isl_int_set_si(bound->el[1 + first + i], -1);
7986 if (isl_tab_add_ineq(tab, bound->el) < 0)
7987 goto error;
7988 empty = tab->empty;
7989 isl_int_set_si(bound->el[1 + first + i], 0);
7990 if (isl_tab_rollback(tab, snap) < 0)
7991 goto error;
7993 if (empty) {
7994 signs[i] = 1;
7995 continue;
7998 isl_int_set_si(bound->el[1 + first + i], 1);
7999 if (isl_tab_add_ineq(tab, bound->el) < 0)
8000 goto error;
8001 empty = tab->empty;
8002 isl_int_set_si(bound->el[1 + first + i], 0);
8003 if (isl_tab_rollback(tab, snap) < 0)
8004 goto error;
8006 signs[i] = empty ? -1 : 0;
8009 isl_tab_free(tab);
8010 isl_vec_free(bound);
8011 return 0;
8012 error:
8013 isl_tab_free(tab);
8014 isl_vec_free(bound);
8015 return -1;
8018 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8019 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8021 if (!bset || !signs)
8022 return -1;
8023 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8024 return -1);
8026 first += pos(bset->dim, type) - 1;
8027 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8030 /* Check if the given basic map is obviously single-valued.
8031 * In particular, for each output dimension, check that there is
8032 * an equality that defines the output dimension in terms of
8033 * earlier dimensions.
8035 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8037 int i, j;
8038 unsigned total;
8039 unsigned n_out;
8040 unsigned o_out;
8042 if (!bmap)
8043 return -1;
8045 total = 1 + isl_basic_map_total_dim(bmap);
8046 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8047 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8049 for (i = 0; i < n_out; ++i) {
8050 for (j = 0; j < bmap->n_eq; ++j) {
8051 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8052 continue;
8053 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8054 total - (o_out + i + 1)) == -1)
8055 break;
8057 if (j >= bmap->n_eq)
8058 return 0;
8061 return 1;
8064 /* Check if the given map is obviously single-valued.
8066 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8068 if (!map)
8069 return -1;
8070 if (map->n == 0)
8071 return 1;
8072 if (map->n >= 2)
8073 return 0;
8075 return isl_basic_map_plain_is_single_valued(map->p[0]);
8078 /* Check if the given map is single-valued.
8079 * We simply compute
8081 * M \circ M^-1
8083 * and check if the result is a subset of the identity mapping.
8085 int isl_map_is_single_valued(__isl_keep isl_map *map)
8087 isl_dim *dim;
8088 isl_map *test;
8089 isl_map *id;
8090 int sv;
8092 sv = isl_map_plain_is_single_valued(map);
8093 if (sv < 0 || sv)
8094 return sv;
8096 test = isl_map_reverse(isl_map_copy(map));
8097 test = isl_map_apply_range(test, isl_map_copy(map));
8099 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
8100 id = isl_map_identity(dim);
8102 sv = isl_map_is_subset(test, id);
8104 isl_map_free(test);
8105 isl_map_free(id);
8107 return sv;
8110 int isl_map_is_injective(__isl_keep isl_map *map)
8112 int in;
8114 map = isl_map_copy(map);
8115 map = isl_map_reverse(map);
8116 in = isl_map_is_single_valued(map);
8117 isl_map_free(map);
8119 return in;
8122 /* Check if the given map is obviously injective.
8124 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8126 int in;
8128 map = isl_map_copy(map);
8129 map = isl_map_reverse(map);
8130 in = isl_map_plain_is_single_valued(map);
8131 isl_map_free(map);
8133 return in;
8136 int isl_map_is_bijective(__isl_keep isl_map *map)
8138 int sv;
8140 sv = isl_map_is_single_valued(map);
8141 if (sv < 0 || !sv)
8142 return sv;
8144 return isl_map_is_injective(map);
8147 int isl_set_is_singleton(__isl_keep isl_set *set)
8149 return isl_map_is_single_valued((isl_map *)set);
8152 int isl_map_is_translation(__isl_keep isl_map *map)
8154 int ok;
8155 isl_set *delta;
8157 delta = isl_map_deltas(isl_map_copy(map));
8158 ok = isl_set_is_singleton(delta);
8159 isl_set_free(delta);
8161 return ok;
8164 static int unique(isl_int *p, unsigned pos, unsigned len)
8166 if (isl_seq_first_non_zero(p, pos) != -1)
8167 return 0;
8168 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8169 return 0;
8170 return 1;
8173 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8175 int i, j;
8176 unsigned nvar;
8177 unsigned ovar;
8179 if (!bset)
8180 return -1;
8182 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8183 return 0;
8185 nvar = isl_basic_set_dim(bset, isl_dim_set);
8186 ovar = isl_dim_offset(bset->dim, isl_dim_set);
8187 for (j = 0; j < nvar; ++j) {
8188 int lower = 0, upper = 0;
8189 for (i = 0; i < bset->n_eq; ++i) {
8190 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8191 continue;
8192 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8193 return 0;
8194 break;
8196 if (i < bset->n_eq)
8197 continue;
8198 for (i = 0; i < bset->n_ineq; ++i) {
8199 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8200 continue;
8201 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8202 return 0;
8203 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8204 lower = 1;
8205 else
8206 upper = 1;
8208 if (!lower || !upper)
8209 return 0;
8212 return 1;
8215 int isl_set_is_box(__isl_keep isl_set *set)
8217 if (!set)
8218 return -1;
8219 if (set->n != 1)
8220 return 0;
8222 return isl_basic_set_is_box(set->p[0]);
8225 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8227 if (!bset)
8228 return -1;
8230 return isl_dim_is_wrapping(bset->dim);
8233 int isl_set_is_wrapping(__isl_keep isl_set *set)
8235 if (!set)
8236 return -1;
8238 return isl_dim_is_wrapping(set->dim);
8241 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8243 bmap = isl_basic_map_cow(bmap);
8244 if (!bmap)
8245 return NULL;
8247 bmap->dim = isl_dim_wrap(bmap->dim);
8248 if (!bmap->dim)
8249 goto error;
8251 bmap = isl_basic_map_finalize(bmap);
8253 return (isl_basic_set *)bmap;
8254 error:
8255 isl_basic_map_free(bmap);
8256 return NULL;
8259 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8261 int i;
8263 map = isl_map_cow(map);
8264 if (!map)
8265 return NULL;
8267 for (i = 0; i < map->n; ++i) {
8268 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8269 if (!map->p[i])
8270 goto error;
8272 map->dim = isl_dim_wrap(map->dim);
8273 if (!map->dim)
8274 goto error;
8276 return (isl_set *)map;
8277 error:
8278 isl_map_free(map);
8279 return NULL;
8282 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8284 bset = isl_basic_set_cow(bset);
8285 if (!bset)
8286 return NULL;
8288 bset->dim = isl_dim_unwrap(bset->dim);
8289 if (!bset->dim)
8290 goto error;
8292 bset = isl_basic_set_finalize(bset);
8294 return (isl_basic_map *)bset;
8295 error:
8296 isl_basic_set_free(bset);
8297 return NULL;
8300 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8302 int i;
8304 if (!set)
8305 return NULL;
8307 if (!isl_set_is_wrapping(set))
8308 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8309 goto error);
8311 set = isl_set_cow(set);
8312 if (!set)
8313 return NULL;
8315 for (i = 0; i < set->n; ++i) {
8316 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8317 if (!set->p[i])
8318 goto error;
8321 set->dim = isl_dim_unwrap(set->dim);
8322 if (!set->dim)
8323 goto error;
8325 return (isl_map *)set;
8326 error:
8327 isl_set_free(set);
8328 return NULL;
8331 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8332 enum isl_dim_type type)
8334 if (!bmap)
8335 return NULL;
8337 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8338 return bmap;
8340 bmap = isl_basic_map_cow(bmap);
8341 if (!bmap)
8342 return NULL;
8344 bmap->dim = isl_dim_reset(bmap->dim, type);
8345 if (!bmap->dim)
8346 goto error;
8348 bmap = isl_basic_map_finalize(bmap);
8350 return bmap;
8351 error:
8352 isl_basic_map_free(bmap);
8353 return NULL;
8356 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8357 enum isl_dim_type type)
8359 int i;
8361 if (!map)
8362 return NULL;
8364 if (!isl_dim_is_named_or_nested(map->dim, type))
8365 return map;
8367 map = isl_map_cow(map);
8368 if (!map)
8369 return NULL;
8371 for (i = 0; i < map->n; ++i) {
8372 map->p[i] = isl_basic_map_reset(map->p[i], type);
8373 if (!map->p[i])
8374 goto error;
8376 map->dim = isl_dim_reset(map->dim, type);
8377 if (!map->dim)
8378 goto error;
8380 return map;
8381 error:
8382 isl_map_free(map);
8383 return NULL;
8386 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8388 if (!bmap)
8389 return NULL;
8391 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8392 return bmap;
8394 bmap = isl_basic_map_cow(bmap);
8395 if (!bmap)
8396 return NULL;
8398 bmap->dim = isl_dim_flatten(bmap->dim);
8399 if (!bmap->dim)
8400 goto error;
8402 bmap = isl_basic_map_finalize(bmap);
8404 return bmap;
8405 error:
8406 isl_basic_map_free(bmap);
8407 return NULL;
8410 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8412 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8415 __isl_give isl_basic_map *isl_basic_map_flatten_range(
8416 __isl_take isl_basic_map *bmap)
8418 if (!bmap)
8419 return NULL;
8421 if (!bmap->dim->nested[1])
8422 return bmap;
8424 bmap = isl_basic_map_cow(bmap);
8425 if (!bmap)
8426 return NULL;
8428 bmap->dim = isl_dim_flatten_range(bmap->dim);
8429 if (!bmap->dim)
8430 goto error;
8432 bmap = isl_basic_map_finalize(bmap);
8434 return bmap;
8435 error:
8436 isl_basic_map_free(bmap);
8437 return NULL;
8440 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8442 int i;
8444 if (!map)
8445 return NULL;
8447 if (!map->dim->nested[0] && !map->dim->nested[1])
8448 return map;
8450 map = isl_map_cow(map);
8451 if (!map)
8452 return NULL;
8454 for (i = 0; i < map->n; ++i) {
8455 map->p[i] = isl_basic_map_flatten(map->p[i]);
8456 if (!map->p[i])
8457 goto error;
8459 map->dim = isl_dim_flatten(map->dim);
8460 if (!map->dim)
8461 goto error;
8463 return map;
8464 error:
8465 isl_map_free(map);
8466 return NULL;
8469 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8471 return (isl_set *)isl_map_flatten((isl_map *)set);
8474 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8476 isl_dim *dim, *flat_dim;
8477 isl_map *map;
8479 dim = isl_set_get_dim(set);
8480 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8481 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8482 map = isl_map_intersect_domain(map, set);
8484 return map;
8487 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
8489 int i;
8491 if (!map)
8492 return NULL;
8494 if (!map->dim->nested[1])
8495 return map;
8497 map = isl_map_cow(map);
8498 if (!map)
8499 return NULL;
8501 for (i = 0; i < map->n; ++i) {
8502 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
8503 if (!map->p[i])
8504 goto error;
8506 map->dim = isl_dim_flatten_range(map->dim);
8507 if (!map->dim)
8508 goto error;
8510 return map;
8511 error:
8512 isl_map_free(map);
8513 return NULL;
8516 /* Reorder the dimensions of "bmap" according to the given dim_map
8517 * and set the dimension specification to "dim".
8519 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8520 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8522 isl_basic_map *res;
8524 bmap = isl_basic_map_cow(bmap);
8525 if (!bmap || !dim || !dim_map)
8526 goto error;
8528 res = isl_basic_map_alloc_dim(dim,
8529 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8530 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
8531 res = isl_basic_map_finalize(res);
8532 return res;
8533 error:
8534 free(dim_map);
8535 isl_basic_map_free(bmap);
8536 isl_dim_free(dim);
8537 return NULL;
8540 /* Reorder the dimensions of "map" according to given reordering.
8542 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8543 __isl_take isl_reordering *r)
8545 int i;
8546 struct isl_dim_map *dim_map;
8548 map = isl_map_cow(map);
8549 dim_map = isl_dim_map_from_reordering(r);
8550 if (!map || !r || !dim_map)
8551 goto error;
8553 for (i = 0; i < map->n; ++i) {
8554 struct isl_dim_map *dim_map_i;
8556 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
8558 map->p[i] = isl_basic_map_realign(map->p[i],
8559 isl_dim_copy(r->dim), dim_map_i);
8561 if (!map->p[i])
8562 goto error;
8565 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8567 isl_reordering_free(r);
8568 free(dim_map);
8569 return map;
8570 error:
8571 free(dim_map);
8572 isl_map_free(map);
8573 isl_reordering_free(r);
8574 return NULL;
8577 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8578 __isl_take isl_reordering *r)
8580 return (isl_set *)isl_map_realign((isl_map *)set, r);
8583 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
8584 __isl_take isl_dim *model)
8586 isl_ctx *ctx;
8588 if (!map || !model)
8589 goto error;
8591 ctx = isl_dim_get_ctx(model);
8592 if (!isl_dim_has_named_params(model))
8593 isl_die(ctx, isl_error_invalid,
8594 "model has unnamed parameters", goto error);
8595 if (!isl_dim_has_named_params(map->dim))
8596 isl_die(ctx, isl_error_invalid,
8597 "relation has unnamed parameters", goto error);
8598 if (!isl_dim_match(map->dim, isl_dim_param, model, isl_dim_param)) {
8599 isl_reordering *exp;
8601 model = isl_dim_drop(model, isl_dim_in,
8602 0, isl_dim_size(model, isl_dim_in));
8603 model = isl_dim_drop(model, isl_dim_out,
8604 0, isl_dim_size(model, isl_dim_out));
8605 exp = isl_parameter_alignment_reordering(map->dim, model);
8606 exp = isl_reordering_extend_dim(exp, isl_map_get_dim(map));
8607 map = isl_map_realign(map, exp);
8610 isl_dim_free(model);
8611 return map;
8612 error:
8613 isl_dim_free(model);
8614 isl_map_free(map);
8615 return NULL;
8618 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
8619 __isl_take isl_dim *model)
8621 return isl_map_align_params(set, model);
8624 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8625 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8626 enum isl_dim_type c2, enum isl_dim_type c3,
8627 enum isl_dim_type c4, enum isl_dim_type c5)
8629 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8630 struct isl_mat *mat;
8631 int i, j, k;
8632 int pos;
8634 if (!bmap)
8635 return NULL;
8636 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8637 isl_basic_map_total_dim(bmap) + 1);
8638 if (!mat)
8639 return NULL;
8640 for (i = 0; i < bmap->n_eq; ++i)
8641 for (j = 0, pos = 0; j < 5; ++j) {
8642 int off = isl_basic_map_offset(bmap, c[j]);
8643 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8644 isl_int_set(mat->row[i][pos],
8645 bmap->eq[i][off + k]);
8646 ++pos;
8650 return mat;
8653 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8654 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8655 enum isl_dim_type c2, enum isl_dim_type c3,
8656 enum isl_dim_type c4, enum isl_dim_type c5)
8658 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8659 struct isl_mat *mat;
8660 int i, j, k;
8661 int pos;
8663 if (!bmap)
8664 return NULL;
8665 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8666 isl_basic_map_total_dim(bmap) + 1);
8667 if (!mat)
8668 return NULL;
8669 for (i = 0; i < bmap->n_ineq; ++i)
8670 for (j = 0, pos = 0; j < 5; ++j) {
8671 int off = isl_basic_map_offset(bmap, c[j]);
8672 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8673 isl_int_set(mat->row[i][pos],
8674 bmap->ineq[i][off + k]);
8675 ++pos;
8679 return mat;
8682 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8683 __isl_take isl_dim *dim,
8684 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8685 enum isl_dim_type c2, enum isl_dim_type c3,
8686 enum isl_dim_type c4, enum isl_dim_type c5)
8688 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8689 isl_basic_map *bmap;
8690 unsigned total;
8691 unsigned extra;
8692 int i, j, k, l;
8693 int pos;
8695 if (!dim || !eq || !ineq)
8696 goto error;
8698 if (eq->n_col != ineq->n_col)
8699 isl_die(dim->ctx, isl_error_invalid,
8700 "equalities and inequalities matrices should have "
8701 "same number of columns", goto error);
8703 total = 1 + isl_dim_total(dim);
8705 if (eq->n_col < total)
8706 isl_die(dim->ctx, isl_error_invalid,
8707 "number of columns too small", goto error);
8709 extra = eq->n_col - total;
8711 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8712 eq->n_row, ineq->n_row);
8713 if (!bmap)
8714 goto error;
8715 for (i = 0; i < extra; ++i) {
8716 k = isl_basic_map_alloc_div(bmap);
8717 if (k < 0)
8718 goto error;
8719 isl_int_set_si(bmap->div[k][0], 0);
8721 for (i = 0; i < eq->n_row; ++i) {
8722 l = isl_basic_map_alloc_equality(bmap);
8723 if (l < 0)
8724 goto error;
8725 for (j = 0, pos = 0; j < 5; ++j) {
8726 int off = isl_basic_map_offset(bmap, c[j]);
8727 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8728 isl_int_set(bmap->eq[l][off + k],
8729 eq->row[i][pos]);
8730 ++pos;
8734 for (i = 0; i < ineq->n_row; ++i) {
8735 l = isl_basic_map_alloc_inequality(bmap);
8736 if (l < 0)
8737 goto error;
8738 for (j = 0, pos = 0; j < 5; ++j) {
8739 int off = isl_basic_map_offset(bmap, c[j]);
8740 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8741 isl_int_set(bmap->ineq[l][off + k],
8742 ineq->row[i][pos]);
8743 ++pos;
8748 isl_dim_free(dim);
8749 isl_mat_free(eq);
8750 isl_mat_free(ineq);
8752 return bmap;
8753 error:
8754 isl_dim_free(dim);
8755 isl_mat_free(eq);
8756 isl_mat_free(ineq);
8757 return NULL;
8760 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8761 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8762 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8764 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8765 c1, c2, c3, c4, isl_dim_in);
8768 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8769 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8770 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8772 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8773 c1, c2, c3, c4, isl_dim_in);
8776 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8777 __isl_take isl_dim *dim,
8778 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8779 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8781 return (isl_basic_set*)
8782 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8783 c1, c2, c3, c4, isl_dim_in);
8786 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8788 if (!bmap)
8789 return -1;
8791 return isl_dim_can_zip(bmap->dim);
8794 int isl_map_can_zip(__isl_keep isl_map *map)
8796 if (!map)
8797 return -1;
8799 return isl_dim_can_zip(map->dim);
8802 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8803 * (A -> C) -> (B -> D).
8805 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8807 unsigned pos;
8808 unsigned n1;
8809 unsigned n2;
8811 if (!bmap)
8812 return NULL;
8814 if (!isl_basic_map_can_zip(bmap))
8815 isl_die(bmap->ctx, isl_error_invalid,
8816 "basic map cannot be zipped", goto error);
8817 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8818 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8819 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8820 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8821 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8822 if (!bmap)
8823 return NULL;
8824 bmap->dim = isl_dim_zip(bmap->dim);
8825 if (!bmap->dim)
8826 goto error;
8827 return bmap;
8828 error:
8829 isl_basic_map_free(bmap);
8830 return NULL;
8833 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8834 * (A -> C) -> (B -> D).
8836 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8838 int i;
8840 if (!map)
8841 return NULL;
8843 if (!isl_map_can_zip(map))
8844 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8845 goto error);
8847 map = isl_map_cow(map);
8848 if (!map)
8849 return NULL;
8851 for (i = 0; i < map->n; ++i) {
8852 map->p[i] = isl_basic_map_zip(map->p[i]);
8853 if (!map->p[i])
8854 goto error;
8857 map->dim = isl_dim_zip(map->dim);
8858 if (!map->dim)
8859 goto error;
8861 return map;
8862 error:
8863 isl_map_free(map);
8864 return NULL;
8867 /* Construct a basic map mapping the domain of the affine expression
8868 * to a one-dimensional range prescribed by the affine expression.
8870 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
8872 int k;
8873 int pos;
8874 isl_local_space *ls;
8875 isl_basic_map *bmap;
8877 if (!aff)
8878 return NULL;
8880 ls = isl_aff_get_local_space(aff);
8881 ls = isl_local_space_from_domain(ls);
8882 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
8883 bmap = isl_basic_map_from_local_space(ls);
8884 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
8885 k = isl_basic_map_alloc_equality(bmap);
8886 if (k < 0)
8887 goto error;
8889 pos = isl_basic_map_offset(bmap, isl_dim_out);
8890 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
8891 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
8892 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
8893 aff->v->size - (pos + 1));
8895 isl_aff_free(aff);
8896 bmap = isl_basic_map_finalize(bmap);
8897 return bmap;
8898 error:
8899 isl_aff_free(aff);
8900 isl_basic_map_free(bmap);
8901 return NULL;
8904 /* Construct a basic map mapping a domain in the given space to
8905 * to an n-dimensional range, with n the number of elements in the list,
8906 * where each coordinate in the range is prescribed by the
8907 * corresponding affine expression.
8908 * The domains of all affine expressions in the list are assumed to match
8909 * domain_dim.
8911 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
8912 __isl_take isl_dim *domain_dim, __isl_take isl_aff_list *list)
8914 int i;
8915 isl_dim *dim;
8916 isl_basic_map *bmap;
8918 if (!list)
8919 return NULL;
8921 dim = isl_dim_from_domain(domain_dim);
8922 bmap = isl_basic_map_universe(dim);
8924 for (i = 0; i < list->n; ++i) {
8925 isl_aff *aff;
8926 isl_basic_map *bmap_i;
8928 aff = isl_aff_copy(list->p[i]);
8929 bmap_i = isl_basic_map_from_aff(aff);
8931 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
8934 isl_aff_list_free(list);
8935 return bmap;