isl_morph.c: remove unused variable
[isl.git] / isl_map.c
blob06680379f736a6c4b0e9db3214d595a069370b1f
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <string.h>
14 #include <strings.h>
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl/blk.h>
18 #include "isl_dim_private.h"
19 #include "isl_equalities.h"
20 #include <isl/list.h>
21 #include <isl/lp.h>
22 #include <isl/seq.h>
23 #include <isl/set.h>
24 #include <isl/map.h>
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
28 #include "isl_tab.h"
29 #include <isl/vec.h>
30 #include <isl_mat_private.h>
31 #include <isl_dim_map.h>
32 #include <isl_local_space_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 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
353 enum isl_dim_type type)
355 return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
358 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
359 enum isl_dim_type type, const char *s)
361 int i;
363 map = isl_map_cow(map);
364 if (!map)
365 return NULL;
367 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
368 if (!map->dim)
369 goto error;
371 for (i = 0; i < map->n; ++i) {
372 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
373 if (!map->p[i])
374 goto error;
377 return map;
378 error:
379 isl_map_free(map);
380 return NULL;
383 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
384 enum isl_dim_type type)
386 return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
389 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
390 const char *s)
392 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
395 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
397 return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
400 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
402 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
405 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
406 enum isl_dim_type type, unsigned pos)
408 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
411 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
412 enum isl_dim_type type, unsigned pos)
414 return bset ? isl_dim_get_name(bset->dim, type, pos) : NULL;
417 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
418 enum isl_dim_type type, unsigned pos)
420 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
423 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
424 enum isl_dim_type type, unsigned pos)
426 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
429 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
430 __isl_take isl_basic_map *bmap,
431 enum isl_dim_type type, unsigned pos, const char *s)
433 if (!bmap)
434 return NULL;
435 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
436 if (!bmap->dim)
437 goto error;
438 return bmap;
439 error:
440 isl_basic_map_free(bmap);
441 return NULL;
444 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
445 enum isl_dim_type type, unsigned pos, const char *s)
447 int i;
449 if (!map)
450 return NULL;
452 map->dim = isl_dim_set_name(map->dim, type, pos, s);
453 if (!map->dim)
454 goto error;
456 for (i = 0; i < map->n; ++i) {
457 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
458 if (!map->p[i])
459 goto error;
462 return map;
463 error:
464 isl_map_free(map);
465 return NULL;
468 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
469 __isl_take isl_basic_set *bset,
470 enum isl_dim_type type, unsigned pos, const char *s)
472 return (isl_basic_set *)isl_basic_map_set_dim_name(
473 (isl_basic_map *)bset, type, pos, s);
476 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
477 enum isl_dim_type type, unsigned pos, const char *s)
479 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
482 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
484 if (!bmap)
485 return -1;
486 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
489 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
491 return isl_basic_map_is_rational(bset);
494 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
495 struct isl_basic_map *bmap, unsigned extra,
496 unsigned n_eq, unsigned n_ineq)
498 int i;
499 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
501 bmap->ctx = ctx;
502 isl_ctx_ref(ctx);
504 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
505 if (isl_blk_is_error(bmap->block))
506 goto error;
508 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
509 if (!bmap->ineq)
510 goto error;
512 if (extra == 0) {
513 bmap->block2 = isl_blk_empty();
514 bmap->div = NULL;
515 } else {
516 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
517 if (isl_blk_is_error(bmap->block2))
518 goto error;
520 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
521 if (!bmap->div)
522 goto error;
525 for (i = 0; i < n_ineq + n_eq; ++i)
526 bmap->ineq[i] = bmap->block.data + i * row_size;
528 for (i = 0; i < extra; ++i)
529 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
531 bmap->ref = 1;
532 bmap->flags = 0;
533 bmap->c_size = n_eq + n_ineq;
534 bmap->eq = bmap->ineq + n_ineq;
535 bmap->extra = extra;
536 bmap->n_eq = 0;
537 bmap->n_ineq = 0;
538 bmap->n_div = 0;
539 bmap->sample = NULL;
541 return bmap;
542 error:
543 isl_basic_map_free(bmap);
544 return NULL;
547 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
548 unsigned nparam, unsigned dim, unsigned extra,
549 unsigned n_eq, unsigned n_ineq)
551 struct isl_basic_map *bmap;
552 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
553 return (struct isl_basic_set *)bmap;
556 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
557 unsigned extra, unsigned n_eq, unsigned n_ineq)
559 struct isl_basic_map *bmap;
560 if (!dim)
561 return NULL;
562 isl_assert(dim->ctx, dim->n_in == 0, goto error);
563 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
564 return (struct isl_basic_set *)bmap;
565 error:
566 isl_dim_free(dim);
567 return NULL;
570 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
571 unsigned extra, unsigned n_eq, unsigned n_ineq)
573 struct isl_basic_map *bmap;
575 if (!dim)
576 return NULL;
577 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
578 if (!bmap)
579 goto error;
580 bmap->dim = dim;
582 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
583 error:
584 isl_dim_free(dim);
585 return NULL;
588 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
589 unsigned nparam, unsigned in, unsigned out, unsigned extra,
590 unsigned n_eq, unsigned n_ineq)
592 struct isl_basic_map *bmap;
593 struct isl_dim *dim;
595 dim = isl_dim_alloc(ctx, nparam, in, out);
596 if (!dim)
597 return NULL;
599 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
600 return bmap;
603 static void dup_constraints(
604 struct isl_basic_map *dst, struct isl_basic_map *src)
606 int i;
607 unsigned total = isl_basic_map_total_dim(src);
609 for (i = 0; i < src->n_eq; ++i) {
610 int j = isl_basic_map_alloc_equality(dst);
611 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
614 for (i = 0; i < src->n_ineq; ++i) {
615 int j = isl_basic_map_alloc_inequality(dst);
616 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
619 for (i = 0; i < src->n_div; ++i) {
620 int j = isl_basic_map_alloc_div(dst);
621 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
623 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
626 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
628 struct isl_basic_map *dup;
630 if (!bmap)
631 return NULL;
632 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
633 bmap->n_div, bmap->n_eq, bmap->n_ineq);
634 if (!dup)
635 return NULL;
636 dup_constraints(dup, bmap);
637 dup->flags = bmap->flags;
638 dup->sample = isl_vec_copy(bmap->sample);
639 return dup;
642 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
644 struct isl_basic_map *dup;
646 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
647 return (struct isl_basic_set *)dup;
650 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
652 if (!bset)
653 return NULL;
655 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
656 bset->ref++;
657 return bset;
659 return isl_basic_set_dup(bset);
662 struct isl_set *isl_set_copy(struct isl_set *set)
664 if (!set)
665 return NULL;
667 set->ref++;
668 return set;
671 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
673 if (!bmap)
674 return NULL;
676 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
677 bmap->ref++;
678 return bmap;
680 bmap = isl_basic_map_dup(bmap);
681 if (bmap)
682 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
683 return bmap;
686 struct isl_map *isl_map_copy(struct isl_map *map)
688 if (!map)
689 return NULL;
691 map->ref++;
692 return map;
695 void isl_basic_map_free(struct isl_basic_map *bmap)
697 if (!bmap)
698 return;
700 if (--bmap->ref > 0)
701 return;
703 isl_ctx_deref(bmap->ctx);
704 free(bmap->div);
705 isl_blk_free(bmap->ctx, bmap->block2);
706 free(bmap->ineq);
707 isl_blk_free(bmap->ctx, bmap->block);
708 isl_vec_free(bmap->sample);
709 isl_dim_free(bmap->dim);
710 free(bmap);
713 void isl_basic_set_free(struct isl_basic_set *bset)
715 isl_basic_map_free((struct isl_basic_map *)bset);
718 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
720 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
723 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
725 struct isl_ctx *ctx;
726 if (!bmap)
727 return -1;
728 ctx = bmap->ctx;
729 isl_assert(ctx, room_for_con(bmap, 1), return -1);
730 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
731 return -1);
732 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
733 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
734 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
735 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
736 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
737 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
738 isl_int *t;
739 int j = isl_basic_map_alloc_inequality(bmap);
740 if (j < 0)
741 return -1;
742 t = bmap->ineq[j];
743 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
744 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
745 bmap->eq[-1] = t;
746 bmap->n_eq++;
747 bmap->n_ineq--;
748 bmap->eq--;
749 return 0;
751 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
752 bmap->extra - bmap->n_div);
753 return bmap->n_eq++;
756 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
758 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
761 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
763 if (!bmap)
764 return -1;
765 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
766 bmap->n_eq -= n;
767 return 0;
770 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
772 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
775 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
777 isl_int *t;
778 if (!bmap)
779 return -1;
780 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
782 if (pos != bmap->n_eq - 1) {
783 t = bmap->eq[pos];
784 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
785 bmap->eq[bmap->n_eq - 1] = t;
787 bmap->n_eq--;
788 return 0;
791 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
793 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
796 void isl_basic_map_inequality_to_equality(
797 struct isl_basic_map *bmap, unsigned pos)
799 isl_int *t;
801 t = bmap->ineq[pos];
802 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
803 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
804 bmap->eq[-1] = t;
805 bmap->n_eq++;
806 bmap->n_ineq--;
807 bmap->eq--;
808 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
809 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
810 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
811 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
814 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
816 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
819 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
821 struct isl_ctx *ctx;
822 if (!bmap)
823 return -1;
824 ctx = bmap->ctx;
825 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
826 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
827 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
828 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
829 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
830 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
831 1 + isl_basic_map_total_dim(bmap),
832 bmap->extra - bmap->n_div);
833 return bmap->n_ineq++;
836 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
838 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
841 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
843 if (!bmap)
844 return -1;
845 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
846 bmap->n_ineq -= n;
847 return 0;
850 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
852 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
855 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
857 isl_int *t;
858 if (!bmap)
859 return -1;
860 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
862 if (pos != bmap->n_ineq - 1) {
863 t = bmap->ineq[pos];
864 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
865 bmap->ineq[bmap->n_ineq - 1] = t;
866 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
868 bmap->n_ineq--;
869 return 0;
872 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
874 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
877 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
878 isl_int *eq)
880 int k;
882 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
883 if (!bmap)
884 return NULL;
885 k = isl_basic_map_alloc_equality(bmap);
886 if (k < 0)
887 goto error;
888 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
889 return bmap;
890 error:
891 isl_basic_map_free(bmap);
892 return NULL;
895 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
896 isl_int *eq)
898 return (isl_basic_set *)
899 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
902 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
903 isl_int *ineq)
905 int k;
907 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
908 if (!bmap)
909 return NULL;
910 k = isl_basic_map_alloc_inequality(bmap);
911 if (k < 0)
912 goto error;
913 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
914 return bmap;
915 error:
916 isl_basic_map_free(bmap);
917 return NULL;
920 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
921 isl_int *ineq)
923 return (isl_basic_set *)
924 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
927 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
929 if (!bmap)
930 return -1;
931 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
932 isl_seq_clr(bmap->div[bmap->n_div] +
933 1 + 1 + isl_basic_map_total_dim(bmap),
934 bmap->extra - bmap->n_div);
935 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
936 return bmap->n_div++;
939 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
941 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
944 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
946 if (!bmap)
947 return -1;
948 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
949 bmap->n_div -= n;
950 return 0;
953 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
955 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
958 /* Copy constraint from src to dst, putting the vars of src at offset
959 * dim_off in dst and the divs of src at offset div_off in dst.
960 * If both sets are actually map, then dim_off applies to the input
961 * variables.
963 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
964 struct isl_basic_map *src_map, isl_int *src,
965 unsigned in_off, unsigned out_off, unsigned div_off)
967 unsigned src_nparam = isl_basic_map_n_param(src_map);
968 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
969 unsigned src_in = isl_basic_map_n_in(src_map);
970 unsigned dst_in = isl_basic_map_n_in(dst_map);
971 unsigned src_out = isl_basic_map_n_out(src_map);
972 unsigned dst_out = isl_basic_map_n_out(dst_map);
973 isl_int_set(dst[0], src[0]);
974 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
975 if (dst_nparam > src_nparam)
976 isl_seq_clr(dst+1+src_nparam,
977 dst_nparam - src_nparam);
978 isl_seq_clr(dst+1+dst_nparam, in_off);
979 isl_seq_cpy(dst+1+dst_nparam+in_off,
980 src+1+src_nparam,
981 isl_min(dst_in-in_off, src_in));
982 if (dst_in-in_off > src_in)
983 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
984 dst_in - in_off - src_in);
985 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
986 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
987 src+1+src_nparam+src_in,
988 isl_min(dst_out-out_off, src_out));
989 if (dst_out-out_off > src_out)
990 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
991 dst_out - out_off - src_out);
992 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
993 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
994 src+1+src_nparam+src_in+src_out,
995 isl_min(dst_map->extra-div_off, src_map->n_div));
996 if (dst_map->n_div-div_off > src_map->n_div)
997 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
998 div_off+src_map->n_div,
999 dst_map->n_div - div_off - src_map->n_div);
1002 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1003 struct isl_basic_map *src_map, isl_int *src,
1004 unsigned in_off, unsigned out_off, unsigned div_off)
1006 isl_int_set(dst[0], src[0]);
1007 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1010 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1011 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1013 int i;
1014 unsigned div_off;
1016 if (!bmap1 || !bmap2)
1017 goto error;
1019 div_off = bmap1->n_div;
1021 for (i = 0; i < bmap2->n_eq; ++i) {
1022 int i1 = isl_basic_map_alloc_equality(bmap1);
1023 if (i1 < 0)
1024 goto error;
1025 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1026 i_pos, o_pos, div_off);
1029 for (i = 0; i < bmap2->n_ineq; ++i) {
1030 int i1 = isl_basic_map_alloc_inequality(bmap1);
1031 if (i1 < 0)
1032 goto error;
1033 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1034 i_pos, o_pos, div_off);
1037 for (i = 0; i < bmap2->n_div; ++i) {
1038 int i1 = isl_basic_map_alloc_div(bmap1);
1039 if (i1 < 0)
1040 goto error;
1041 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1042 i_pos, o_pos, div_off);
1045 isl_basic_map_free(bmap2);
1047 return bmap1;
1049 error:
1050 isl_basic_map_free(bmap1);
1051 isl_basic_map_free(bmap2);
1052 return NULL;
1055 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1056 struct isl_basic_set *bset2, unsigned pos)
1058 return (struct isl_basic_set *)
1059 add_constraints((struct isl_basic_map *)bset1,
1060 (struct isl_basic_map *)bset2, 0, pos);
1063 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1064 struct isl_dim *dim, unsigned extra,
1065 unsigned n_eq, unsigned n_ineq)
1067 struct isl_basic_map *ext;
1068 unsigned flags;
1069 int dims_ok;
1071 if (!dim)
1072 goto error;
1074 if (!base)
1075 goto error;
1077 dims_ok = isl_dim_equal(base->dim, dim) &&
1078 base->extra >= base->n_div + extra;
1080 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1081 room_for_ineq(base, n_ineq)) {
1082 isl_dim_free(dim);
1083 return base;
1086 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1087 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1088 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1089 extra += base->extra;
1090 n_eq += base->n_eq;
1091 n_ineq += base->n_ineq;
1093 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1094 dim = NULL;
1095 if (!ext)
1096 goto error;
1098 if (dims_ok)
1099 ext->sample = isl_vec_copy(base->sample);
1100 flags = base->flags;
1101 ext = add_constraints(ext, base, 0, 0);
1102 if (ext) {
1103 ext->flags = flags;
1104 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1107 return ext;
1109 error:
1110 isl_dim_free(dim);
1111 isl_basic_map_free(base);
1112 return NULL;
1115 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1116 struct isl_dim *dim, unsigned extra,
1117 unsigned n_eq, unsigned n_ineq)
1119 return (struct isl_basic_set *)
1120 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1121 extra, n_eq, n_ineq);
1124 struct isl_basic_map *isl_basic_map_extend_constraints(
1125 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1127 if (!base)
1128 return NULL;
1129 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1130 0, n_eq, n_ineq);
1133 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1134 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1135 unsigned n_eq, unsigned n_ineq)
1137 struct isl_basic_map *bmap;
1138 struct isl_dim *dim;
1140 if (!base)
1141 return NULL;
1142 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1143 if (!dim)
1144 goto error;
1146 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1147 return bmap;
1148 error:
1149 isl_basic_map_free(base);
1150 return NULL;
1153 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1154 unsigned nparam, unsigned dim, unsigned extra,
1155 unsigned n_eq, unsigned n_ineq)
1157 return (struct isl_basic_set *)
1158 isl_basic_map_extend((struct isl_basic_map *)base,
1159 nparam, 0, dim, extra, n_eq, n_ineq);
1162 struct isl_basic_set *isl_basic_set_extend_constraints(
1163 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1165 return (struct isl_basic_set *)
1166 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1167 n_eq, n_ineq);
1170 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1172 return (struct isl_basic_set *)
1173 isl_basic_map_cow((struct isl_basic_map *)bset);
1176 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1178 if (!bmap)
1179 return NULL;
1181 if (bmap->ref > 1) {
1182 bmap->ref--;
1183 bmap = isl_basic_map_dup(bmap);
1185 if (bmap)
1186 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1187 return bmap;
1190 struct isl_set *isl_set_cow(struct isl_set *set)
1192 if (!set)
1193 return NULL;
1195 if (set->ref == 1)
1196 return set;
1197 set->ref--;
1198 return isl_set_dup(set);
1201 struct isl_map *isl_map_cow(struct isl_map *map)
1203 if (!map)
1204 return NULL;
1206 if (map->ref == 1)
1207 return map;
1208 map->ref--;
1209 return isl_map_dup(map);
1212 static void swap_vars(struct isl_blk blk, isl_int *a,
1213 unsigned a_len, unsigned b_len)
1215 isl_seq_cpy(blk.data, a+a_len, b_len);
1216 isl_seq_cpy(blk.data+b_len, a, a_len);
1217 isl_seq_cpy(a, blk.data, b_len+a_len);
1220 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1221 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1223 int i;
1224 struct isl_blk blk;
1226 if (!bmap)
1227 goto error;
1229 isl_assert(bmap->ctx,
1230 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1232 if (n1 == 0 || n2 == 0)
1233 return bmap;
1235 bmap = isl_basic_map_cow(bmap);
1236 if (!bmap)
1237 return NULL;
1239 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1240 if (isl_blk_is_error(blk))
1241 goto error;
1243 for (i = 0; i < bmap->n_eq; ++i)
1244 swap_vars(blk,
1245 bmap->eq[i] + pos, n1, n2);
1247 for (i = 0; i < bmap->n_ineq; ++i)
1248 swap_vars(blk,
1249 bmap->ineq[i] + pos, n1, n2);
1251 for (i = 0; i < bmap->n_div; ++i)
1252 swap_vars(blk,
1253 bmap->div[i]+1 + pos, n1, n2);
1255 isl_blk_free(bmap->ctx, blk);
1257 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1258 bmap = isl_basic_map_gauss(bmap, NULL);
1259 return isl_basic_map_finalize(bmap);
1260 error:
1261 isl_basic_map_free(bmap);
1262 return NULL;
1265 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1266 __isl_take isl_basic_set *bset, unsigned n)
1268 unsigned dim;
1269 unsigned nparam;
1271 nparam = isl_basic_set_n_param(bset);
1272 dim = isl_basic_set_n_dim(bset);
1273 isl_assert(bset->ctx, n <= dim, goto error);
1275 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1276 error:
1277 isl_basic_set_free(bset);
1278 return NULL;
1281 static __isl_give isl_set *isl_set_swap_vars(__isl_take isl_set *set,
1282 unsigned n)
1284 int i;
1285 set = isl_set_cow(set);
1286 if (!set)
1287 return NULL;
1289 for (i = 0; i < set->n; ++i) {
1290 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1291 if (!set->p[i]) {
1292 isl_set_free(set);
1293 return NULL;
1296 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1297 return set;
1300 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1302 int i = 0;
1303 unsigned total;
1304 if (!bmap)
1305 goto error;
1306 total = isl_basic_map_total_dim(bmap);
1307 isl_basic_map_free_div(bmap, bmap->n_div);
1308 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1309 if (bmap->n_eq > 0)
1310 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1311 else {
1312 i = isl_basic_map_alloc_equality(bmap);
1313 if (i < 0)
1314 goto error;
1316 isl_int_set_si(bmap->eq[i][0], 1);
1317 isl_seq_clr(bmap->eq[i]+1, total);
1318 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1319 isl_vec_free(bmap->sample);
1320 bmap->sample = NULL;
1321 return isl_basic_map_finalize(bmap);
1322 error:
1323 isl_basic_map_free(bmap);
1324 return NULL;
1327 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1329 return (struct isl_basic_set *)
1330 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1333 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1335 int i;
1336 unsigned off = isl_dim_total(bmap->dim);
1337 isl_int *t = bmap->div[a];
1338 bmap->div[a] = bmap->div[b];
1339 bmap->div[b] = t;
1341 for (i = 0; i < bmap->n_eq; ++i)
1342 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1344 for (i = 0; i < bmap->n_ineq; ++i)
1345 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1347 for (i = 0; i < bmap->n_div; ++i)
1348 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1349 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1352 /* Eliminate the specified n dimensions starting at first from the
1353 * constraints using Fourier-Motzkin. The dimensions themselves
1354 * are not removed.
1356 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1357 enum isl_dim_type type, unsigned first, unsigned n)
1359 int i;
1361 if (!map)
1362 return NULL;
1363 if (n == 0)
1364 return map;
1366 map = isl_map_cow(map);
1367 if (!map)
1368 return NULL;
1369 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1370 first += pos(map->dim, type) - 1;
1372 for (i = 0; i < map->n; ++i) {
1373 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1374 if (!map->p[i])
1375 goto error;
1377 return map;
1378 error:
1379 isl_map_free(map);
1380 return NULL;
1383 /* Eliminate the specified n dimensions starting at first from the
1384 * constraints using Fourier-Motzkin. The dimensions themselves
1385 * are not removed.
1387 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1388 enum isl_dim_type type, unsigned first, unsigned n)
1390 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1393 /* Eliminate the specified n dimensions starting at first from the
1394 * constraints using Fourier-Motzkin. The dimensions themselves
1395 * are not removed.
1397 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1398 unsigned first, unsigned n)
1400 return isl_set_eliminate(set, isl_dim_set, first, n);
1403 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1404 __isl_take isl_basic_map *bmap)
1406 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1407 bmap->n_div);
1408 if (!bmap)
1409 return NULL;
1410 bmap->n_div = 0;
1411 return isl_basic_map_finalize(bmap);
1414 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1415 __isl_take isl_basic_set *bset)
1417 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1418 (struct isl_basic_map *)bset);
1421 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1423 int i;
1425 if (!map)
1426 return NULL;
1427 if (map->n == 0)
1428 return map;
1430 map = isl_map_cow(map);
1431 if (!map)
1432 return NULL;
1434 for (i = 0; i < map->n; ++i) {
1435 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1436 if (!map->p[i])
1437 goto error;
1439 return map;
1440 error:
1441 isl_map_free(map);
1442 return NULL;
1445 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1447 return isl_map_remove_divs(set);
1450 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1451 enum isl_dim_type type, unsigned first, unsigned n)
1453 if (!bmap)
1454 return NULL;
1455 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1456 goto error);
1457 if (n == 0)
1458 return bmap;
1459 bmap = isl_basic_map_eliminate_vars(bmap,
1460 isl_basic_map_offset(bmap, type) - 1 + first, n);
1461 if (!bmap)
1462 return bmap;
1463 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1464 return bmap;
1465 bmap = isl_basic_map_drop(bmap, type, first, n);
1466 return bmap;
1467 error:
1468 isl_basic_map_free(bmap);
1469 return NULL;
1472 /* Return true if the definition of the given div (recursively) involves
1473 * any of the given variables.
1475 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1476 unsigned first, unsigned n)
1478 int i;
1479 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1481 if (isl_int_is_zero(bmap->div[div][0]))
1482 return 0;
1483 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1484 return 1;
1486 for (i = bmap->n_div - 1; i >= 0; --i) {
1487 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1488 continue;
1489 if (div_involves_vars(bmap, i, first, n))
1490 return 1;
1493 return 0;
1496 /* Remove all divs (recursively) involving any of the given dimensions
1497 * in their definitions.
1499 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1500 __isl_take isl_basic_map *bmap,
1501 enum isl_dim_type type, unsigned first, unsigned n)
1503 int i;
1505 if (!bmap)
1506 return NULL;
1507 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1508 goto error);
1509 first += isl_basic_map_offset(bmap, type);
1511 for (i = bmap->n_div - 1; i >= 0; --i) {
1512 if (!div_involves_vars(bmap, i, first, n))
1513 continue;
1514 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1517 return bmap;
1518 error:
1519 isl_basic_map_free(bmap);
1520 return NULL;
1523 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1524 enum isl_dim_type type, unsigned first, unsigned n)
1526 int i;
1528 if (!map)
1529 return NULL;
1530 if (map->n == 0)
1531 return map;
1533 map = isl_map_cow(map);
1534 if (!map)
1535 return NULL;
1537 for (i = 0; i < map->n; ++i) {
1538 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1539 type, first, n);
1540 if (!map->p[i])
1541 goto error;
1543 return map;
1544 error:
1545 isl_map_free(map);
1546 return NULL;
1549 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1550 enum isl_dim_type type, unsigned first, unsigned n)
1552 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1553 type, first, n);
1556 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1557 enum isl_dim_type type, unsigned first, unsigned n)
1559 int i;
1561 if (!bset)
1562 return -1;
1564 if (first + n > isl_basic_set_dim(bset, type))
1565 isl_die(bset->ctx, isl_error_invalid,
1566 "index out of bounds", return -1);
1568 first += isl_basic_set_offset(bset, type);
1569 for (i = 0; i < bset->n_eq; ++i)
1570 if (isl_seq_first_non_zero(bset->eq[i] + first, n) >= 0)
1571 return 1;
1572 for (i = 0; i < bset->n_ineq; ++i)
1573 if (isl_seq_first_non_zero(bset->ineq[i] + first, n) >= 0)
1574 return 1;
1576 return 0;
1579 int isl_set_involves_dims(__isl_keep isl_set *set,
1580 enum isl_dim_type type, unsigned first, unsigned n)
1582 int i;
1584 if (!set)
1585 return -1;
1587 if (first + n > isl_set_dim(set, type))
1588 isl_die(set->ctx, isl_error_invalid,
1589 "index out of bounds", return -1);
1591 for (i = 0; i < set->n; ++i) {
1592 int involves = isl_basic_set_involves_dims(set->p[i],
1593 type, first, n);
1594 if (involves < 0 || !involves)
1595 return involves;
1598 return 1;
1601 /* Return true if the definition of the given div is unknown or depends
1602 * on unknown divs.
1604 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1606 int i;
1607 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1609 if (isl_int_is_zero(bmap->div[div][0]))
1610 return 1;
1612 for (i = bmap->n_div - 1; i >= 0; --i) {
1613 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1614 continue;
1615 if (div_is_unknown(bmap, i))
1616 return 1;
1619 return 0;
1622 /* Remove all divs that are unknown or defined in terms of unknown divs.
1624 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1625 __isl_take isl_basic_map *bmap)
1627 int i;
1629 if (!bmap)
1630 return NULL;
1632 for (i = bmap->n_div - 1; i >= 0; --i) {
1633 if (!div_is_unknown(bmap, i))
1634 continue;
1635 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1638 return bmap;
1641 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1643 int i;
1645 if (!map)
1646 return NULL;
1647 if (map->n == 0)
1648 return map;
1650 map = isl_map_cow(map);
1651 if (!map)
1652 return NULL;
1654 for (i = 0; i < map->n; ++i) {
1655 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1656 if (!map->p[i])
1657 goto error;
1659 return map;
1660 error:
1661 isl_map_free(map);
1662 return NULL;
1665 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1667 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1670 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1671 __isl_take isl_basic_set *bset,
1672 enum isl_dim_type type, unsigned first, unsigned n)
1674 return (isl_basic_set *)
1675 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1678 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1679 enum isl_dim_type type, unsigned first, unsigned n)
1681 int i;
1683 if (n == 0)
1684 return map;
1686 map = isl_map_cow(map);
1687 if (!map)
1688 return NULL;
1689 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1691 for (i = 0; i < map->n; ++i) {
1692 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1693 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1694 if (!map->p[i])
1695 goto error;
1697 map = isl_map_drop(map, type, first, n);
1698 return map;
1699 error:
1700 isl_map_free(map);
1701 return NULL;
1704 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1705 enum isl_dim_type type, unsigned first, unsigned n)
1707 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1710 /* Project out n inputs starting at first using Fourier-Motzkin */
1711 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1712 unsigned first, unsigned n)
1714 return isl_map_remove_dims(map, isl_dim_in, first, n);
1717 static void dump_term(struct isl_basic_map *bmap,
1718 isl_int c, int pos, FILE *out)
1720 const char *name;
1721 unsigned in = isl_basic_map_n_in(bmap);
1722 unsigned dim = in + isl_basic_map_n_out(bmap);
1723 unsigned nparam = isl_basic_map_n_param(bmap);
1724 if (!pos)
1725 isl_int_print(out, c, 0);
1726 else {
1727 if (!isl_int_is_one(c))
1728 isl_int_print(out, c, 0);
1729 if (pos < 1 + nparam) {
1730 name = isl_dim_get_name(bmap->dim,
1731 isl_dim_param, pos - 1);
1732 if (name)
1733 fprintf(out, "%s", name);
1734 else
1735 fprintf(out, "p%d", pos - 1);
1736 } else if (pos < 1 + nparam + in)
1737 fprintf(out, "i%d", pos - 1 - nparam);
1738 else if (pos < 1 + nparam + dim)
1739 fprintf(out, "o%d", pos - 1 - nparam - in);
1740 else
1741 fprintf(out, "e%d", pos - 1 - nparam - dim);
1745 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1746 int sign, FILE *out)
1748 int i;
1749 int first;
1750 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1751 isl_int v;
1753 isl_int_init(v);
1754 for (i = 0, first = 1; i < len; ++i) {
1755 if (isl_int_sgn(c[i]) * sign <= 0)
1756 continue;
1757 if (!first)
1758 fprintf(out, " + ");
1759 first = 0;
1760 isl_int_abs(v, c[i]);
1761 dump_term(bmap, v, i, out);
1763 isl_int_clear(v);
1764 if (first)
1765 fprintf(out, "0");
1768 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1769 const char *op, FILE *out, int indent)
1771 int i;
1773 fprintf(out, "%*s", indent, "");
1775 dump_constraint_sign(bmap, c, 1, out);
1776 fprintf(out, " %s ", op);
1777 dump_constraint_sign(bmap, c, -1, out);
1779 fprintf(out, "\n");
1781 for (i = bmap->n_div; i < bmap->extra; ++i) {
1782 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1783 continue;
1784 fprintf(out, "%*s", indent, "");
1785 fprintf(out, "ERROR: unused div coefficient not zero\n");
1786 abort();
1790 static void dump_constraints(struct isl_basic_map *bmap,
1791 isl_int **c, unsigned n,
1792 const char *op, FILE *out, int indent)
1794 int i;
1796 for (i = 0; i < n; ++i)
1797 dump_constraint(bmap, c[i], op, out, indent);
1800 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1802 int j;
1803 int first = 1;
1804 unsigned total = isl_basic_map_total_dim(bmap);
1806 for (j = 0; j < 1 + total; ++j) {
1807 if (isl_int_is_zero(exp[j]))
1808 continue;
1809 if (!first && isl_int_is_pos(exp[j]))
1810 fprintf(out, "+");
1811 dump_term(bmap, exp[j], j, out);
1812 first = 0;
1816 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1818 int i;
1820 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1821 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1823 for (i = 0; i < bmap->n_div; ++i) {
1824 fprintf(out, "%*s", indent, "");
1825 fprintf(out, "e%d = [(", i);
1826 dump_affine(bmap, bmap->div[i]+1, out);
1827 fprintf(out, ")/");
1828 isl_int_print(out, bmap->div[i][0], 0);
1829 fprintf(out, "]\n");
1833 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1834 FILE *out, int indent)
1836 if (!bset) {
1837 fprintf(out, "null basic set\n");
1838 return;
1841 fprintf(out, "%*s", indent, "");
1842 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1843 bset->ref, bset->dim->nparam, bset->dim->n_out,
1844 bset->extra, bset->flags);
1845 dump((struct isl_basic_map *)bset, out, indent);
1848 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1849 FILE *out, int indent)
1851 if (!bmap) {
1852 fprintf(out, "null basic map\n");
1853 return;
1856 fprintf(out, "%*s", indent, "");
1857 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1858 "flags: %x, n_name: %d\n",
1859 bmap->ref,
1860 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1861 bmap->extra, bmap->flags, bmap->dim->n_name);
1862 dump(bmap, out, indent);
1865 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1867 unsigned total;
1868 if (!bmap)
1869 return -1;
1870 total = isl_basic_map_total_dim(bmap);
1871 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1872 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1873 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1874 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1875 return 0;
1878 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1880 struct isl_set *set;
1882 if (!dim)
1883 return NULL;
1884 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1885 isl_assert(dim->ctx, n >= 0, goto error);
1886 set = isl_alloc(dim->ctx, struct isl_set,
1887 sizeof(struct isl_set) +
1888 (n - 1) * sizeof(struct isl_basic_set *));
1889 if (!set)
1890 goto error;
1892 set->ctx = dim->ctx;
1893 isl_ctx_ref(set->ctx);
1894 set->ref = 1;
1895 set->size = n;
1896 set->n = 0;
1897 set->dim = dim;
1898 set->flags = flags;
1899 return set;
1900 error:
1901 isl_dim_free(dim);
1902 return NULL;
1905 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1906 unsigned nparam, unsigned dim, int n, unsigned flags)
1908 struct isl_set *set;
1909 struct isl_dim *dims;
1911 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1912 if (!dims)
1913 return NULL;
1915 set = isl_set_alloc_dim(dims, n, flags);
1916 return set;
1919 /* Make sure "map" has room for at least "n" more basic maps.
1921 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1923 int i;
1924 struct isl_map *grown = NULL;
1926 if (!map)
1927 return NULL;
1928 isl_assert(map->ctx, n >= 0, goto error);
1929 if (map->n + n <= map->size)
1930 return map;
1931 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1932 if (!grown)
1933 goto error;
1934 for (i = 0; i < map->n; ++i) {
1935 grown->p[i] = isl_basic_map_copy(map->p[i]);
1936 if (!grown->p[i])
1937 goto error;
1938 grown->n++;
1940 isl_map_free(map);
1941 return grown;
1942 error:
1943 isl_map_free(grown);
1944 isl_map_free(map);
1945 return NULL;
1948 /* Make sure "set" has room for at least "n" more basic sets.
1950 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1952 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1955 struct isl_set *isl_set_dup(struct isl_set *set)
1957 int i;
1958 struct isl_set *dup;
1960 if (!set)
1961 return NULL;
1963 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1964 if (!dup)
1965 return NULL;
1966 for (i = 0; i < set->n; ++i)
1967 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1968 return dup;
1971 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1973 return isl_map_from_basic_map(bset);
1976 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1978 struct isl_map *map;
1980 if (!bmap)
1981 return NULL;
1983 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1984 return isl_map_add_basic_map(map, bmap);
1987 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1988 __isl_take isl_basic_set *bset)
1990 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1991 (struct isl_basic_map *)bset);
1994 void isl_set_free(struct isl_set *set)
1996 int i;
1998 if (!set)
1999 return;
2001 if (--set->ref > 0)
2002 return;
2004 isl_ctx_deref(set->ctx);
2005 for (i = 0; i < set->n; ++i)
2006 isl_basic_set_free(set->p[i]);
2007 isl_dim_free(set->dim);
2008 free(set);
2011 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2013 int i;
2015 if (!set) {
2016 fprintf(out, "null set\n");
2017 return;
2020 fprintf(out, "%*s", indent, "");
2021 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2022 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2023 set->flags);
2024 for (i = 0; i < set->n; ++i) {
2025 fprintf(out, "%*s", indent, "");
2026 fprintf(out, "basic set %d:\n", i);
2027 isl_basic_set_print_internal(set->p[i], out, indent+4);
2031 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2033 int i;
2035 if (!map) {
2036 fprintf(out, "null map\n");
2037 return;
2040 fprintf(out, "%*s", indent, "");
2041 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2042 "flags: %x, n_name: %d\n",
2043 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2044 map->dim->n_out, map->flags, map->dim->n_name);
2045 for (i = 0; i < map->n; ++i) {
2046 fprintf(out, "%*s", indent, "");
2047 fprintf(out, "basic map %d:\n", i);
2048 isl_basic_map_print_internal(map->p[i], out, indent+4);
2052 struct isl_basic_map *isl_basic_map_intersect_domain(
2053 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2055 struct isl_basic_map *bmap_domain;
2057 if (!bmap || !bset)
2058 goto error;
2060 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2061 bset->dim, isl_dim_param), goto error);
2063 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2064 isl_assert(bset->ctx,
2065 isl_basic_map_compatible_domain(bmap, bset), goto error);
2067 bmap = isl_basic_map_cow(bmap);
2068 if (!bmap)
2069 goto error;
2070 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2071 bset->n_div, bset->n_eq, bset->n_ineq);
2072 bmap_domain = isl_basic_map_from_domain(bset);
2073 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2075 bmap = isl_basic_map_simplify(bmap);
2076 return isl_basic_map_finalize(bmap);
2077 error:
2078 isl_basic_map_free(bmap);
2079 isl_basic_set_free(bset);
2080 return NULL;
2083 struct isl_basic_map *isl_basic_map_intersect_range(
2084 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2086 struct isl_basic_map *bmap_range;
2088 if (!bmap || !bset)
2089 goto error;
2091 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2092 bset->dim, isl_dim_param), goto error);
2094 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2095 isl_assert(bset->ctx,
2096 isl_basic_map_compatible_range(bmap, bset), goto error);
2098 if (isl_basic_set_is_universe(bset)) {
2099 isl_basic_set_free(bset);
2100 return bmap;
2103 bmap = isl_basic_map_cow(bmap);
2104 if (!bmap)
2105 goto error;
2106 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2107 bset->n_div, bset->n_eq, bset->n_ineq);
2108 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2109 bmap = add_constraints(bmap, bmap_range, 0, 0);
2111 bmap = isl_basic_map_simplify(bmap);
2112 return isl_basic_map_finalize(bmap);
2113 error:
2114 isl_basic_map_free(bmap);
2115 isl_basic_set_free(bset);
2116 return NULL;
2119 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2121 int i;
2122 unsigned total;
2123 isl_int s;
2125 total = 1 + isl_basic_map_total_dim(bmap);
2126 if (total != vec->size)
2127 return -1;
2129 isl_int_init(s);
2131 for (i = 0; i < bmap->n_eq; ++i) {
2132 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2133 if (!isl_int_is_zero(s)) {
2134 isl_int_clear(s);
2135 return 0;
2139 for (i = 0; i < bmap->n_ineq; ++i) {
2140 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2141 if (isl_int_is_neg(s)) {
2142 isl_int_clear(s);
2143 return 0;
2147 isl_int_clear(s);
2149 return 1;
2152 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2154 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2157 struct isl_basic_map *isl_basic_map_intersect(
2158 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2160 struct isl_vec *sample = NULL;
2162 if (!bmap1 || !bmap2)
2163 goto error;
2165 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2166 bmap2->dim, isl_dim_param), goto error);
2167 if (isl_dim_total(bmap1->dim) ==
2168 isl_dim_size(bmap1->dim, isl_dim_param) &&
2169 isl_dim_total(bmap2->dim) !=
2170 isl_dim_size(bmap2->dim, isl_dim_param))
2171 return isl_basic_map_intersect(bmap2, bmap1);
2173 if (isl_dim_total(bmap2->dim) !=
2174 isl_dim_size(bmap2->dim, isl_dim_param))
2175 isl_assert(bmap1->ctx,
2176 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2178 if (bmap1->sample &&
2179 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2180 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2181 sample = isl_vec_copy(bmap1->sample);
2182 else if (bmap2->sample &&
2183 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2184 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2185 sample = isl_vec_copy(bmap2->sample);
2187 bmap1 = isl_basic_map_cow(bmap1);
2188 if (!bmap1)
2189 goto error;
2190 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2191 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2192 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2194 if (!bmap1)
2195 isl_vec_free(sample);
2196 else if (sample) {
2197 isl_vec_free(bmap1->sample);
2198 bmap1->sample = sample;
2201 bmap1 = isl_basic_map_simplify(bmap1);
2202 return isl_basic_map_finalize(bmap1);
2203 error:
2204 if (sample)
2205 isl_vec_free(sample);
2206 isl_basic_map_free(bmap1);
2207 isl_basic_map_free(bmap2);
2208 return NULL;
2211 struct isl_basic_set *isl_basic_set_intersect(
2212 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2214 return (struct isl_basic_set *)
2215 isl_basic_map_intersect(
2216 (struct isl_basic_map *)bset1,
2217 (struct isl_basic_map *)bset2);
2220 /* Special case of isl_map_intersect, where both map1 and map2
2221 * are convex, without any divs and such that either map1 or map2
2222 * contains a single constraint. This constraint is then simply
2223 * added to the other map.
2225 static __isl_give isl_map *map_intersect_add_constraint(
2226 __isl_take isl_map *map1, __isl_take isl_map *map2)
2228 struct isl_basic_map *bmap1;
2229 struct isl_basic_map *bmap2;
2231 isl_assert(map1->ctx, map1->n == 1, goto error);
2232 isl_assert(map2->ctx, map1->n == 1, goto error);
2233 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2234 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2236 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2237 return isl_map_intersect(map2, map1);
2239 isl_assert(map2->ctx,
2240 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2242 map1 = isl_map_cow(map1);
2243 if (!map1)
2244 goto error;
2245 if (isl_map_plain_is_empty(map1)) {
2246 isl_map_free(map2);
2247 return map1;
2249 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2250 if (map2->p[0]->n_eq == 1)
2251 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2252 else
2253 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2254 map2->p[0]->ineq[0]);
2256 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2257 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2258 if (!map1->p[0])
2259 goto error;
2261 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2262 isl_basic_map_free(map1->p[0]);
2263 map1->n = 0;
2266 isl_map_free(map2);
2268 return map1;
2269 error:
2270 isl_map_free(map1);
2271 isl_map_free(map2);
2272 return NULL;
2275 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2277 unsigned flags = 0;
2278 struct isl_map *result;
2279 int i, j;
2281 if (!map1 || !map2)
2282 goto error;
2284 if (isl_map_plain_is_empty(map1)) {
2285 isl_map_free(map2);
2286 return map1;
2288 if (isl_map_plain_is_empty(map2)) {
2289 isl_map_free(map1);
2290 return map2;
2293 if (map1->n == 1 && map2->n == 1 &&
2294 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2295 isl_dim_equal(map1->dim, map2->dim) &&
2296 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2297 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2298 return map_intersect_add_constraint(map1, map2);
2299 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2300 map2->dim, isl_dim_param), goto error);
2301 if (isl_dim_total(map1->dim) ==
2302 isl_dim_size(map1->dim, isl_dim_param) &&
2303 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2304 return isl_map_intersect(map2, map1);
2306 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2307 isl_assert(map1->ctx,
2308 isl_dim_equal(map1->dim, map2->dim), goto error);
2310 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2311 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2312 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2314 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2315 map1->n * map2->n, flags);
2316 if (!result)
2317 goto error;
2318 for (i = 0; i < map1->n; ++i)
2319 for (j = 0; j < map2->n; ++j) {
2320 struct isl_basic_map *part;
2321 part = isl_basic_map_intersect(
2322 isl_basic_map_copy(map1->p[i]),
2323 isl_basic_map_copy(map2->p[j]));
2324 if (isl_basic_map_is_empty(part))
2325 isl_basic_map_free(part);
2326 else
2327 result = isl_map_add_basic_map(result, part);
2328 if (!result)
2329 goto error;
2331 isl_map_free(map1);
2332 isl_map_free(map2);
2333 return result;
2334 error:
2335 isl_map_free(map1);
2336 isl_map_free(map2);
2337 return NULL;
2340 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2342 return (struct isl_set *)
2343 isl_map_intersect((struct isl_map *)set1,
2344 (struct isl_map *)set2);
2347 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2349 struct isl_dim *dim;
2350 struct isl_basic_set *bset;
2351 unsigned in;
2353 if (!bmap)
2354 return NULL;
2355 bmap = isl_basic_map_cow(bmap);
2356 if (!bmap)
2357 return NULL;
2358 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2359 in = isl_basic_map_n_in(bmap);
2360 bset = isl_basic_set_from_basic_map(bmap);
2361 bset = isl_basic_set_swap_vars(bset, in);
2362 return isl_basic_map_from_basic_set(bset, dim);
2365 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2366 enum isl_dim_type type, unsigned pos, unsigned n)
2368 struct isl_dim *res_dim;
2369 struct isl_basic_map *res;
2370 struct isl_dim_map *dim_map;
2371 unsigned total, off;
2372 enum isl_dim_type t;
2374 if (n == 0)
2375 return bmap;
2377 if (!bmap)
2378 return NULL;
2380 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2382 total = isl_basic_map_total_dim(bmap) + n;
2383 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2384 off = 0;
2385 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2386 if (t != type) {
2387 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2388 } else {
2389 unsigned size = isl_basic_map_dim(bmap, t);
2390 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2391 0, pos, off);
2392 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2393 pos, size - pos, off + pos + n);
2395 off += isl_dim_size(res_dim, t);
2397 isl_dim_map_div(dim_map, bmap, off);
2399 res = isl_basic_map_alloc_dim(res_dim,
2400 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2401 if (isl_basic_map_is_rational(bmap))
2402 res = isl_basic_map_set_rational(res);
2403 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2404 return isl_basic_map_finalize(res);
2407 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2408 enum isl_dim_type type, unsigned n)
2410 if (!bmap)
2411 return NULL;
2412 return isl_basic_map_insert(bmap, type,
2413 isl_basic_map_dim(bmap, type), n);
2416 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2417 enum isl_dim_type type, unsigned n)
2419 if (!bset)
2420 return NULL;
2421 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2422 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2423 error:
2424 isl_basic_set_free(bset);
2425 return NULL;
2428 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2429 enum isl_dim_type type, unsigned pos, unsigned n)
2431 int i;
2433 if (n == 0)
2434 return map;
2436 map = isl_map_cow(map);
2437 if (!map)
2438 return NULL;
2440 map->dim = isl_dim_insert(map->dim, type, pos, n);
2441 if (!map->dim)
2442 goto error;
2444 for (i = 0; i < map->n; ++i) {
2445 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2446 if (!map->p[i])
2447 goto error;
2450 return map;
2451 error:
2452 isl_map_free(map);
2453 return NULL;
2456 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2457 enum isl_dim_type type, unsigned pos, unsigned n)
2459 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2462 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2463 enum isl_dim_type type, unsigned n)
2465 if (!map)
2466 return NULL;
2467 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2470 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2471 enum isl_dim_type type, unsigned n)
2473 if (!set)
2474 return NULL;
2475 isl_assert(set->ctx, type != isl_dim_in, goto error);
2476 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2477 error:
2478 isl_set_free(set);
2479 return NULL;
2482 __isl_give isl_basic_map *isl_basic_map_move_dims(
2483 __isl_take isl_basic_map *bmap,
2484 enum isl_dim_type dst_type, unsigned dst_pos,
2485 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2487 int i;
2488 struct isl_dim_map *dim_map;
2489 struct isl_basic_map *res;
2490 enum isl_dim_type t;
2491 unsigned total, off;
2493 if (!bmap)
2494 return NULL;
2495 if (n == 0)
2496 return bmap;
2498 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2499 goto error);
2501 if (dst_type == src_type && dst_pos == src_pos)
2502 return bmap;
2504 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2506 if (pos(bmap->dim, dst_type) + dst_pos ==
2507 pos(bmap->dim, src_type) + src_pos +
2508 ((src_type < dst_type) ? n : 0)) {
2509 bmap = isl_basic_map_cow(bmap);
2510 if (!bmap)
2511 return NULL;
2513 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2514 src_type, src_pos, n);
2515 if (!bmap->dim)
2516 goto error;
2518 bmap = isl_basic_map_finalize(bmap);
2520 return bmap;
2523 total = isl_basic_map_total_dim(bmap);
2524 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2526 off = 0;
2527 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2528 unsigned size = isl_dim_size(bmap->dim, t);
2529 if (t == dst_type) {
2530 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2531 0, dst_pos, off);
2532 off += dst_pos;
2533 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2534 src_pos, n, off);
2535 off += n;
2536 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2537 dst_pos, size - dst_pos, off);
2538 off += size - dst_pos;
2539 } else if (t == src_type) {
2540 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2541 0, src_pos, off);
2542 off += src_pos;
2543 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2544 src_pos + n, size - src_pos - n, off);
2545 off += size - src_pos - n;
2546 } else {
2547 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2548 off += size;
2551 isl_dim_map_div(dim_map, bmap, off);
2553 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2554 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2555 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2557 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2558 src_type, src_pos, n);
2559 if (!bmap->dim)
2560 goto error;
2562 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2563 bmap = isl_basic_map_gauss(bmap, NULL);
2564 bmap = isl_basic_map_finalize(bmap);
2566 return bmap;
2567 error:
2568 isl_basic_map_free(bmap);
2569 return NULL;
2572 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2573 enum isl_dim_type dst_type, unsigned dst_pos,
2574 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2576 return (isl_basic_set *)isl_basic_map_move_dims(
2577 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2580 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2581 enum isl_dim_type dst_type, unsigned dst_pos,
2582 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2584 if (!set)
2585 return NULL;
2586 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2587 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2588 src_type, src_pos, n);
2589 error:
2590 isl_set_free(set);
2591 return NULL;
2594 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2595 enum isl_dim_type dst_type, unsigned dst_pos,
2596 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2598 int i;
2600 if (!map)
2601 return NULL;
2602 if (n == 0)
2603 return map;
2605 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2606 goto error);
2608 if (dst_type == src_type && dst_pos == src_pos)
2609 return map;
2611 isl_assert(map->ctx, dst_type != src_type, goto error);
2613 map = isl_map_cow(map);
2614 if (!map)
2615 return NULL;
2617 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2618 if (!map->dim)
2619 goto error;
2621 for (i = 0; i < map->n; ++i) {
2622 map->p[i] = isl_basic_map_move_dims(map->p[i],
2623 dst_type, dst_pos,
2624 src_type, src_pos, n);
2625 if (!map->p[i])
2626 goto error;
2629 return map;
2630 error:
2631 isl_map_free(map);
2632 return NULL;
2635 /* Move the specified dimensions to the last columns right before
2636 * the divs. Don't change the dimension specification of bmap.
2637 * That's the responsibility of the caller.
2639 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2640 enum isl_dim_type type, unsigned first, unsigned n)
2642 struct isl_dim_map *dim_map;
2643 struct isl_basic_map *res;
2644 enum isl_dim_type t;
2645 unsigned total, off;
2647 if (!bmap)
2648 return NULL;
2649 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2650 return bmap;
2652 total = isl_basic_map_total_dim(bmap);
2653 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2655 off = 0;
2656 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2657 unsigned size = isl_dim_size(bmap->dim, t);
2658 if (t == type) {
2659 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2660 0, first, off);
2661 off += first;
2662 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2663 first, n, total - bmap->n_div - n);
2664 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2665 first + n, size - (first + n), off);
2666 off += size - (first + n);
2667 } else {
2668 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2669 off += size;
2672 isl_dim_map_div(dim_map, bmap, off + n);
2674 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2675 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2676 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2677 return res;
2680 /* Turn the n dimensions of type type, starting at first
2681 * into existentially quantified variables.
2683 __isl_give isl_basic_map *isl_basic_map_project_out(
2684 __isl_take isl_basic_map *bmap,
2685 enum isl_dim_type type, unsigned first, unsigned n)
2687 int i;
2688 size_t row_size;
2689 isl_int **new_div;
2690 isl_int *old;
2692 if (n == 0)
2693 return bmap;
2695 if (!bmap)
2696 return NULL;
2698 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2699 return isl_basic_map_remove_dims(bmap, type, first, n);
2701 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2702 goto error);
2704 bmap = move_last(bmap, type, first, n);
2705 bmap = isl_basic_map_cow(bmap);
2706 if (!bmap)
2707 return NULL;
2709 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2710 old = bmap->block2.data;
2711 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2712 (bmap->extra + n) * (1 + row_size));
2713 if (!bmap->block2.data)
2714 goto error;
2715 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2716 if (!new_div)
2717 goto error;
2718 for (i = 0; i < n; ++i) {
2719 new_div[i] = bmap->block2.data +
2720 (bmap->extra + i) * (1 + row_size);
2721 isl_seq_clr(new_div[i], 1 + row_size);
2723 for (i = 0; i < bmap->extra; ++i)
2724 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2725 free(bmap->div);
2726 bmap->div = new_div;
2727 bmap->n_div += n;
2728 bmap->extra += n;
2730 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2731 if (!bmap->dim)
2732 goto error;
2733 bmap = isl_basic_map_simplify(bmap);
2734 bmap = isl_basic_map_drop_redundant_divs(bmap);
2735 return isl_basic_map_finalize(bmap);
2736 error:
2737 isl_basic_map_free(bmap);
2738 return NULL;
2741 /* Turn the n dimensions of type type, starting at first
2742 * into existentially quantified variables.
2744 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2745 enum isl_dim_type type, unsigned first, unsigned n)
2747 return (isl_basic_set *)isl_basic_map_project_out(
2748 (isl_basic_map *)bset, type, first, n);
2751 /* Turn the n dimensions of type type, starting at first
2752 * into existentially quantified variables.
2754 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2755 enum isl_dim_type type, unsigned first, unsigned n)
2757 int i;
2759 if (!map)
2760 return NULL;
2762 if (n == 0)
2763 return map;
2765 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2767 map = isl_map_cow(map);
2768 if (!map)
2769 return NULL;
2771 map->dim = isl_dim_drop(map->dim, type, first, n);
2772 if (!map->dim)
2773 goto error;
2775 for (i = 0; i < map->n; ++i) {
2776 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2777 if (!map->p[i])
2778 goto error;
2781 return map;
2782 error:
2783 isl_map_free(map);
2784 return NULL;
2787 /* Turn the n dimensions of type type, starting at first
2788 * into existentially quantified variables.
2790 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2791 enum isl_dim_type type, unsigned first, unsigned n)
2793 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2796 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2798 int i, j;
2800 for (i = 0; i < n; ++i) {
2801 j = isl_basic_map_alloc_div(bmap);
2802 if (j < 0)
2803 goto error;
2804 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2806 return bmap;
2807 error:
2808 isl_basic_map_free(bmap);
2809 return NULL;
2812 struct isl_basic_map *isl_basic_map_apply_range(
2813 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2815 struct isl_dim *dim_result = NULL;
2816 struct isl_basic_map *bmap;
2817 unsigned n_in, n_out, n, nparam, total, pos;
2818 struct isl_dim_map *dim_map1, *dim_map2;
2820 if (!bmap1 || !bmap2)
2821 goto error;
2823 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2824 isl_dim_copy(bmap2->dim));
2826 n_in = isl_basic_map_n_in(bmap1);
2827 n_out = isl_basic_map_n_out(bmap2);
2828 n = isl_basic_map_n_out(bmap1);
2829 nparam = isl_basic_map_n_param(bmap1);
2831 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2832 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2833 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2834 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2835 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2836 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2837 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2838 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2839 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2840 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2841 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2843 bmap = isl_basic_map_alloc_dim(dim_result,
2844 bmap1->n_div + bmap2->n_div + n,
2845 bmap1->n_eq + bmap2->n_eq,
2846 bmap1->n_ineq + bmap2->n_ineq);
2847 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2848 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2849 bmap = add_divs(bmap, n);
2850 bmap = isl_basic_map_simplify(bmap);
2851 bmap = isl_basic_map_drop_redundant_divs(bmap);
2852 return isl_basic_map_finalize(bmap);
2853 error:
2854 isl_basic_map_free(bmap1);
2855 isl_basic_map_free(bmap2);
2856 return NULL;
2859 struct isl_basic_set *isl_basic_set_apply(
2860 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2862 if (!bset || !bmap)
2863 goto error;
2865 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2866 goto error);
2868 return (struct isl_basic_set *)
2869 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2870 error:
2871 isl_basic_set_free(bset);
2872 isl_basic_map_free(bmap);
2873 return NULL;
2876 struct isl_basic_map *isl_basic_map_apply_domain(
2877 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2879 if (!bmap1 || !bmap2)
2880 goto error;
2882 isl_assert(bmap1->ctx,
2883 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2884 isl_assert(bmap1->ctx,
2885 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2886 goto error);
2888 bmap1 = isl_basic_map_reverse(bmap1);
2889 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2890 return isl_basic_map_reverse(bmap1);
2891 error:
2892 isl_basic_map_free(bmap1);
2893 isl_basic_map_free(bmap2);
2894 return NULL;
2897 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2898 * A \cap B -> f(A) + f(B)
2900 struct isl_basic_map *isl_basic_map_sum(
2901 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2903 unsigned n_in, n_out, nparam, total, pos;
2904 struct isl_basic_map *bmap = NULL;
2905 struct isl_dim_map *dim_map1, *dim_map2;
2906 int i;
2908 if (!bmap1 || !bmap2)
2909 goto error;
2911 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2912 goto error);
2914 nparam = isl_basic_map_n_param(bmap1);
2915 n_in = isl_basic_map_n_in(bmap1);
2916 n_out = isl_basic_map_n_out(bmap1);
2918 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2919 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2920 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2921 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2922 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2923 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2924 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2925 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2926 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2927 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2928 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2930 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2931 bmap1->n_div + bmap2->n_div + 2 * n_out,
2932 bmap1->n_eq + bmap2->n_eq + n_out,
2933 bmap1->n_ineq + bmap2->n_ineq);
2934 for (i = 0; i < n_out; ++i) {
2935 int j = isl_basic_map_alloc_equality(bmap);
2936 if (j < 0)
2937 goto error;
2938 isl_seq_clr(bmap->eq[j], 1+total);
2939 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2940 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2941 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2943 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2944 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2945 bmap = add_divs(bmap, 2 * n_out);
2947 bmap = isl_basic_map_simplify(bmap);
2948 return isl_basic_map_finalize(bmap);
2949 error:
2950 isl_basic_map_free(bmap);
2951 isl_basic_map_free(bmap1);
2952 isl_basic_map_free(bmap2);
2953 return NULL;
2956 /* Given two maps A -> f(A) and B -> g(B), construct a map
2957 * A \cap B -> f(A) + f(B)
2959 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2961 struct isl_map *result;
2962 int i, j;
2964 if (!map1 || !map2)
2965 goto error;
2967 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2969 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2970 map1->n * map2->n, 0);
2971 if (!result)
2972 goto error;
2973 for (i = 0; i < map1->n; ++i)
2974 for (j = 0; j < map2->n; ++j) {
2975 struct isl_basic_map *part;
2976 part = isl_basic_map_sum(
2977 isl_basic_map_copy(map1->p[i]),
2978 isl_basic_map_copy(map2->p[j]));
2979 if (isl_basic_map_is_empty(part))
2980 isl_basic_map_free(part);
2981 else
2982 result = isl_map_add_basic_map(result, part);
2983 if (!result)
2984 goto error;
2986 isl_map_free(map1);
2987 isl_map_free(map2);
2988 return result;
2989 error:
2990 isl_map_free(map1);
2991 isl_map_free(map2);
2992 return NULL;
2995 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2996 __isl_take isl_set *set2)
2998 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3001 /* Given a basic map A -> f(A), construct A -> -f(A).
3003 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3005 int i, j;
3006 unsigned off, n;
3008 bmap = isl_basic_map_cow(bmap);
3009 if (!bmap)
3010 return NULL;
3012 n = isl_basic_map_dim(bmap, isl_dim_out);
3013 off = isl_basic_map_offset(bmap, isl_dim_out);
3014 for (i = 0; i < bmap->n_eq; ++i)
3015 for (j = 0; j < n; ++j)
3016 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3017 for (i = 0; i < bmap->n_ineq; ++i)
3018 for (j = 0; j < n; ++j)
3019 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3020 for (i = 0; i < bmap->n_div; ++i)
3021 for (j = 0; j < n; ++j)
3022 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3023 return isl_basic_map_finalize(bmap);
3026 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3028 return isl_basic_map_neg(bset);
3031 /* Given a map A -> f(A), construct A -> -f(A).
3033 struct isl_map *isl_map_neg(struct isl_map *map)
3035 int i;
3037 map = isl_map_cow(map);
3038 if (!map)
3039 return NULL;
3041 for (i = 0; i < map->n; ++i) {
3042 map->p[i] = isl_basic_map_neg(map->p[i]);
3043 if (!map->p[i])
3044 goto error;
3047 return map;
3048 error:
3049 isl_map_free(map);
3050 return NULL;
3053 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3055 return (isl_set *)isl_map_neg((isl_map *)set);
3058 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3059 * A -> floor(f(A)/d).
3061 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3062 isl_int d)
3064 unsigned n_in, n_out, nparam, total, pos;
3065 struct isl_basic_map *result = NULL;
3066 struct isl_dim_map *dim_map;
3067 int i;
3069 if (!bmap)
3070 return NULL;
3072 nparam = isl_basic_map_n_param(bmap);
3073 n_in = isl_basic_map_n_in(bmap);
3074 n_out = isl_basic_map_n_out(bmap);
3076 total = nparam + n_in + n_out + bmap->n_div + n_out;
3077 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3078 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3079 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3080 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3081 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3083 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3084 bmap->n_div + n_out,
3085 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3086 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3087 result = add_divs(result, n_out);
3088 for (i = 0; i < n_out; ++i) {
3089 int j;
3090 j = isl_basic_map_alloc_inequality(result);
3091 if (j < 0)
3092 goto error;
3093 isl_seq_clr(result->ineq[j], 1+total);
3094 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3095 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3096 j = isl_basic_map_alloc_inequality(result);
3097 if (j < 0)
3098 goto error;
3099 isl_seq_clr(result->ineq[j], 1+total);
3100 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3101 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3102 isl_int_sub_ui(result->ineq[j][0], d, 1);
3105 result = isl_basic_map_simplify(result);
3106 return isl_basic_map_finalize(result);
3107 error:
3108 isl_basic_map_free(result);
3109 return NULL;
3112 /* Given a map A -> f(A) and an integer d, construct a map
3113 * A -> floor(f(A)/d).
3115 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3117 int i;
3119 map = isl_map_cow(map);
3120 if (!map)
3121 return NULL;
3123 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3124 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3125 for (i = 0; i < map->n; ++i) {
3126 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3127 if (!map->p[i])
3128 goto error;
3131 return map;
3132 error:
3133 isl_map_free(map);
3134 return NULL;
3137 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3139 int i;
3140 unsigned nparam;
3141 unsigned n_in;
3143 i = isl_basic_map_alloc_equality(bmap);
3144 if (i < 0)
3145 goto error;
3146 nparam = isl_basic_map_n_param(bmap);
3147 n_in = isl_basic_map_n_in(bmap);
3148 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3149 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3150 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3151 return isl_basic_map_finalize(bmap);
3152 error:
3153 isl_basic_map_free(bmap);
3154 return NULL;
3157 /* Add a constraints to "bmap" expressing i_pos < o_pos
3159 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3161 int i;
3162 unsigned nparam;
3163 unsigned n_in;
3165 i = isl_basic_map_alloc_inequality(bmap);
3166 if (i < 0)
3167 goto error;
3168 nparam = isl_basic_map_n_param(bmap);
3169 n_in = isl_basic_map_n_in(bmap);
3170 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3171 isl_int_set_si(bmap->ineq[i][0], -1);
3172 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3173 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3174 return isl_basic_map_finalize(bmap);
3175 error:
3176 isl_basic_map_free(bmap);
3177 return NULL;
3180 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3182 static __isl_give isl_basic_map *var_less_or_equal(
3183 __isl_take isl_basic_map *bmap, unsigned pos)
3185 int i;
3186 unsigned nparam;
3187 unsigned n_in;
3189 i = isl_basic_map_alloc_inequality(bmap);
3190 if (i < 0)
3191 goto error;
3192 nparam = isl_basic_map_n_param(bmap);
3193 n_in = isl_basic_map_n_in(bmap);
3194 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3195 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3196 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3197 return isl_basic_map_finalize(bmap);
3198 error:
3199 isl_basic_map_free(bmap);
3200 return NULL;
3203 /* Add a constraints to "bmap" expressing i_pos > o_pos
3205 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3207 int i;
3208 unsigned nparam;
3209 unsigned n_in;
3211 i = isl_basic_map_alloc_inequality(bmap);
3212 if (i < 0)
3213 goto error;
3214 nparam = isl_basic_map_n_param(bmap);
3215 n_in = isl_basic_map_n_in(bmap);
3216 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3217 isl_int_set_si(bmap->ineq[i][0], -1);
3218 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3219 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3220 return isl_basic_map_finalize(bmap);
3221 error:
3222 isl_basic_map_free(bmap);
3223 return NULL;
3226 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3228 static __isl_give isl_basic_map *var_more_or_equal(
3229 __isl_take isl_basic_map *bmap, unsigned pos)
3231 int i;
3232 unsigned nparam;
3233 unsigned n_in;
3235 i = isl_basic_map_alloc_inequality(bmap);
3236 if (i < 0)
3237 goto error;
3238 nparam = isl_basic_map_n_param(bmap);
3239 n_in = isl_basic_map_n_in(bmap);
3240 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3241 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3242 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3243 return isl_basic_map_finalize(bmap);
3244 error:
3245 isl_basic_map_free(bmap);
3246 return NULL;
3249 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3251 int i;
3252 struct isl_basic_map *bmap;
3253 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3254 if (!bmap)
3255 return NULL;
3256 for (i = 0; i < n_equal && bmap; ++i)
3257 bmap = var_equal(bmap, i);
3258 return isl_basic_map_finalize(bmap);
3261 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3263 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3265 int i;
3266 struct isl_basic_map *bmap;
3267 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3268 if (!bmap)
3269 return NULL;
3270 for (i = 0; i < pos && bmap; ++i)
3271 bmap = var_equal(bmap, i);
3272 if (bmap)
3273 bmap = var_less(bmap, pos);
3274 return isl_basic_map_finalize(bmap);
3277 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3279 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3280 __isl_take isl_dim *dim, unsigned pos)
3282 int i;
3283 isl_basic_map *bmap;
3285 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3286 for (i = 0; i < pos; ++i)
3287 bmap = var_equal(bmap, i);
3288 bmap = var_less_or_equal(bmap, pos);
3289 return isl_basic_map_finalize(bmap);
3292 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3294 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3296 int i;
3297 struct isl_basic_map *bmap;
3298 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3299 if (!bmap)
3300 return NULL;
3301 for (i = 0; i < pos && bmap; ++i)
3302 bmap = var_equal(bmap, i);
3303 if (bmap)
3304 bmap = var_more(bmap, pos);
3305 return isl_basic_map_finalize(bmap);
3308 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3310 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3311 __isl_take isl_dim *dim, unsigned pos)
3313 int i;
3314 isl_basic_map *bmap;
3316 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3317 for (i = 0; i < pos; ++i)
3318 bmap = var_equal(bmap, i);
3319 bmap = var_more_or_equal(bmap, pos);
3320 return isl_basic_map_finalize(bmap);
3323 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3324 unsigned n, int equal)
3326 struct isl_map *map;
3327 int i;
3329 if (n == 0 && equal)
3330 return isl_map_universe(dims);
3332 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3334 for (i = 0; i + 1 < n; ++i)
3335 map = isl_map_add_basic_map(map,
3336 isl_basic_map_less_at(isl_dim_copy(dims), i));
3337 if (n > 0) {
3338 if (equal)
3339 map = isl_map_add_basic_map(map,
3340 isl_basic_map_less_or_equal_at(dims, n - 1));
3341 else
3342 map = isl_map_add_basic_map(map,
3343 isl_basic_map_less_at(dims, n - 1));
3344 } else
3345 isl_dim_free(dims);
3347 return map;
3350 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3352 if (!dims)
3353 return NULL;
3354 return map_lex_lte_first(dims, dims->n_out, equal);
3357 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3359 return map_lex_lte_first(dim, n, 0);
3362 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3364 return map_lex_lte_first(dim, n, 1);
3367 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3369 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3372 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3374 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3377 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3378 unsigned n, int equal)
3380 struct isl_map *map;
3381 int i;
3383 if (n == 0 && equal)
3384 return isl_map_universe(dims);
3386 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3388 for (i = 0; i + 1 < n; ++i)
3389 map = isl_map_add_basic_map(map,
3390 isl_basic_map_more_at(isl_dim_copy(dims), i));
3391 if (n > 0) {
3392 if (equal)
3393 map = isl_map_add_basic_map(map,
3394 isl_basic_map_more_or_equal_at(dims, n - 1));
3395 else
3396 map = isl_map_add_basic_map(map,
3397 isl_basic_map_more_at(dims, n - 1));
3398 } else
3399 isl_dim_free(dims);
3401 return map;
3404 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3406 if (!dims)
3407 return NULL;
3408 return map_lex_gte_first(dims, dims->n_out, equal);
3411 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3413 return map_lex_gte_first(dim, n, 0);
3416 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3418 return map_lex_gte_first(dim, n, 1);
3421 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3423 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3426 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3428 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3431 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3432 __isl_take isl_set *set2)
3434 isl_map *map;
3435 map = isl_map_lex_le(isl_set_get_dim(set1));
3436 map = isl_map_intersect_domain(map, set1);
3437 map = isl_map_intersect_range(map, set2);
3438 return map;
3441 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3442 __isl_take isl_set *set2)
3444 isl_map *map;
3445 map = isl_map_lex_lt(isl_set_get_dim(set1));
3446 map = isl_map_intersect_domain(map, set1);
3447 map = isl_map_intersect_range(map, set2);
3448 return map;
3451 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3452 __isl_take isl_set *set2)
3454 isl_map *map;
3455 map = isl_map_lex_ge(isl_set_get_dim(set1));
3456 map = isl_map_intersect_domain(map, set1);
3457 map = isl_map_intersect_range(map, set2);
3458 return map;
3461 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3462 __isl_take isl_set *set2)
3464 isl_map *map;
3465 map = isl_map_lex_gt(isl_set_get_dim(set1));
3466 map = isl_map_intersect_domain(map, set1);
3467 map = isl_map_intersect_range(map, set2);
3468 return map;
3471 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3472 __isl_take isl_map *map2)
3474 isl_map *map;
3475 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3476 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3477 map = isl_map_apply_range(map, isl_map_reverse(map2));
3478 return map;
3481 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3482 __isl_take isl_map *map2)
3484 isl_map *map;
3485 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3486 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3487 map = isl_map_apply_range(map, isl_map_reverse(map2));
3488 return map;
3491 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3492 __isl_take isl_map *map2)
3494 isl_map *map;
3495 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3496 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3497 map = isl_map_apply_range(map, isl_map_reverse(map2));
3498 return map;
3501 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3502 __isl_take isl_map *map2)
3504 isl_map *map;
3505 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3506 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3507 map = isl_map_apply_range(map, isl_map_reverse(map2));
3508 return map;
3511 struct isl_basic_map *isl_basic_map_from_basic_set(
3512 struct isl_basic_set *bset, struct isl_dim *dim)
3514 struct isl_basic_map *bmap;
3516 bset = isl_basic_set_cow(bset);
3517 if (!bset || !dim)
3518 goto error;
3520 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3521 isl_dim_free(bset->dim);
3522 bmap = (struct isl_basic_map *) bset;
3523 bmap->dim = dim;
3524 return isl_basic_map_finalize(bmap);
3525 error:
3526 isl_basic_set_free(bset);
3527 isl_dim_free(dim);
3528 return NULL;
3531 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3533 if (!bmap)
3534 goto error;
3535 if (bmap->dim->n_in == 0)
3536 return (struct isl_basic_set *)bmap;
3537 bmap = isl_basic_map_cow(bmap);
3538 if (!bmap)
3539 goto error;
3540 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3541 if (!bmap->dim)
3542 goto error;
3543 bmap = isl_basic_map_finalize(bmap);
3544 return (struct isl_basic_set *)bmap;
3545 error:
3546 isl_basic_map_free(bmap);
3547 return NULL;
3550 /* For a div d = floor(f/m), add the constraints
3552 * f - m d >= 0
3553 * -(f-(n-1)) + m d >= 0
3555 * Note that the second constraint is the negation of
3557 * f - m d >= n
3559 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3560 unsigned pos, isl_int *div)
3562 int i, j;
3563 unsigned total = isl_basic_map_total_dim(bmap);
3565 i = isl_basic_map_alloc_inequality(bmap);
3566 if (i < 0)
3567 return -1;
3568 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3569 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3571 j = isl_basic_map_alloc_inequality(bmap);
3572 if (j < 0)
3573 return -1;
3574 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3575 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3576 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3577 return j;
3580 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3581 unsigned pos, isl_int *div)
3583 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3584 pos, div);
3587 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3589 unsigned total = isl_basic_map_total_dim(bmap);
3590 unsigned div_pos = total - bmap->n_div + div;
3592 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3593 bmap->div[div]);
3596 struct isl_basic_set *isl_basic_map_underlying_set(
3597 struct isl_basic_map *bmap)
3599 if (!bmap)
3600 goto error;
3601 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3602 bmap->n_div == 0 &&
3603 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3604 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3605 return (struct isl_basic_set *)bmap;
3606 bmap = isl_basic_map_cow(bmap);
3607 if (!bmap)
3608 goto error;
3609 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3610 if (!bmap->dim)
3611 goto error;
3612 bmap->extra -= bmap->n_div;
3613 bmap->n_div = 0;
3614 bmap = isl_basic_map_finalize(bmap);
3615 return (struct isl_basic_set *)bmap;
3616 error:
3617 return NULL;
3620 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3621 __isl_take isl_basic_set *bset)
3623 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3626 struct isl_basic_map *isl_basic_map_overlying_set(
3627 struct isl_basic_set *bset, struct isl_basic_map *like)
3629 struct isl_basic_map *bmap;
3630 struct isl_ctx *ctx;
3631 unsigned total;
3632 int i;
3634 if (!bset || !like)
3635 goto error;
3636 ctx = bset->ctx;
3637 isl_assert(ctx, bset->n_div == 0, goto error);
3638 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3639 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3640 goto error);
3641 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3642 isl_basic_map_free(like);
3643 return (struct isl_basic_map *)bset;
3645 bset = isl_basic_set_cow(bset);
3646 if (!bset)
3647 goto error;
3648 total = bset->dim->n_out + bset->extra;
3649 bmap = (struct isl_basic_map *)bset;
3650 isl_dim_free(bmap->dim);
3651 bmap->dim = isl_dim_copy(like->dim);
3652 if (!bmap->dim)
3653 goto error;
3654 bmap->n_div = like->n_div;
3655 bmap->extra += like->n_div;
3656 if (bmap->extra) {
3657 unsigned ltotal;
3658 isl_int **div;
3659 ltotal = total - bmap->extra + like->extra;
3660 if (ltotal > total)
3661 ltotal = total;
3662 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3663 bmap->extra * (1 + 1 + total));
3664 if (isl_blk_is_error(bmap->block2))
3665 goto error;
3666 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3667 if (!div)
3668 goto error;
3669 bmap->div = div;
3670 for (i = 0; i < bmap->extra; ++i)
3671 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3672 for (i = 0; i < like->n_div; ++i) {
3673 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3674 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3676 bmap = isl_basic_map_extend_constraints(bmap,
3677 0, 2 * like->n_div);
3678 for (i = 0; i < like->n_div; ++i) {
3679 if (isl_int_is_zero(bmap->div[i][0]))
3680 continue;
3681 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3682 goto error;
3685 isl_basic_map_free(like);
3686 bmap = isl_basic_map_simplify(bmap);
3687 bmap = isl_basic_map_finalize(bmap);
3688 return bmap;
3689 error:
3690 isl_basic_map_free(like);
3691 isl_basic_set_free(bset);
3692 return NULL;
3695 struct isl_basic_set *isl_basic_set_from_underlying_set(
3696 struct isl_basic_set *bset, struct isl_basic_set *like)
3698 return (struct isl_basic_set *)
3699 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3702 struct isl_set *isl_set_from_underlying_set(
3703 struct isl_set *set, struct isl_basic_set *like)
3705 int i;
3707 if (!set || !like)
3708 goto error;
3709 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3710 goto error);
3711 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3712 isl_basic_set_free(like);
3713 return set;
3715 set = isl_set_cow(set);
3716 if (!set)
3717 goto error;
3718 for (i = 0; i < set->n; ++i) {
3719 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3720 isl_basic_set_copy(like));
3721 if (!set->p[i])
3722 goto error;
3724 isl_dim_free(set->dim);
3725 set->dim = isl_dim_copy(like->dim);
3726 if (!set->dim)
3727 goto error;
3728 isl_basic_set_free(like);
3729 return set;
3730 error:
3731 isl_basic_set_free(like);
3732 isl_set_free(set);
3733 return NULL;
3736 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3738 int i;
3740 map = isl_map_cow(map);
3741 if (!map)
3742 return NULL;
3743 map->dim = isl_dim_cow(map->dim);
3744 if (!map->dim)
3745 goto error;
3747 for (i = 1; i < map->n; ++i)
3748 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3749 goto error);
3750 for (i = 0; i < map->n; ++i) {
3751 map->p[i] = (struct isl_basic_map *)
3752 isl_basic_map_underlying_set(map->p[i]);
3753 if (!map->p[i])
3754 goto error;
3756 if (map->n == 0)
3757 map->dim = isl_dim_underlying(map->dim, 0);
3758 else {
3759 isl_dim_free(map->dim);
3760 map->dim = isl_dim_copy(map->p[0]->dim);
3762 if (!map->dim)
3763 goto error;
3764 return (struct isl_set *)map;
3765 error:
3766 isl_map_free(map);
3767 return NULL;
3770 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3772 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3775 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3776 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3778 bmap = isl_basic_map_cow(bmap);
3779 if (!bmap || !dim)
3780 goto error;
3782 isl_dim_free(bmap->dim);
3783 bmap->dim = dim;
3785 bmap = isl_basic_map_finalize(bmap);
3787 return bmap;
3788 error:
3789 isl_basic_map_free(bmap);
3790 isl_dim_free(dim);
3791 return NULL;
3794 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3795 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3797 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3798 dim);
3801 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3802 __isl_take isl_dim *dim)
3804 int i;
3806 map = isl_map_cow(map);
3807 if (!map || !dim)
3808 goto error;
3810 for (i = 0; i < map->n; ++i) {
3811 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3812 isl_dim_copy(dim));
3813 if (!map->p[i])
3814 goto error;
3816 isl_dim_free(map->dim);
3817 map->dim = dim;
3819 return map;
3820 error:
3821 isl_map_free(map);
3822 isl_dim_free(dim);
3823 return NULL;
3826 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3827 __isl_take isl_dim *dim)
3829 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3832 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3834 isl_dim *dim;
3835 struct isl_basic_set *domain;
3836 unsigned n_in;
3837 unsigned n_out;
3839 if (!bmap)
3840 return NULL;
3841 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3843 n_in = isl_basic_map_n_in(bmap);
3844 n_out = isl_basic_map_n_out(bmap);
3845 domain = isl_basic_set_from_basic_map(bmap);
3846 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3848 domain = isl_basic_set_reset_dim(domain, dim);
3850 return domain;
3853 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
3855 if (!bmap)
3856 return -1;
3857 return isl_dim_may_be_set(bmap->dim);
3860 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3862 if (!bmap)
3863 return NULL;
3864 if (isl_basic_map_may_be_set(bmap))
3865 return bmap;
3866 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3869 __isl_give isl_basic_map *isl_basic_map_domain_map(
3870 __isl_take isl_basic_map *bmap)
3872 int i, k;
3873 isl_dim *dim;
3874 isl_basic_map *domain;
3875 isl_basic_set *bset;
3876 int nparam, n_in, n_out;
3877 unsigned total;
3879 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3880 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3881 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3883 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3884 domain = isl_basic_map_universe(dim);
3886 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3887 bmap = isl_basic_map_apply_range(bmap, domain);
3888 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3890 total = isl_basic_map_total_dim(bmap);
3892 for (i = 0; i < n_in; ++i) {
3893 k = isl_basic_map_alloc_equality(bmap);
3894 if (k < 0)
3895 goto error;
3896 isl_seq_clr(bmap->eq[k], 1 + total);
3897 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3898 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3901 bmap = isl_basic_map_gauss(bmap, NULL);
3902 return isl_basic_map_finalize(bmap);
3903 error:
3904 isl_basic_map_free(bmap);
3905 return NULL;
3908 __isl_give isl_basic_map *isl_basic_map_range_map(
3909 __isl_take isl_basic_map *bmap)
3911 int i, k;
3912 isl_dim *dim;
3913 isl_basic_map *range;
3914 isl_basic_set *bset;
3915 int nparam, n_in, n_out;
3916 unsigned total;
3918 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3919 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3920 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3922 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3923 range = isl_basic_map_universe(dim);
3925 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3926 bmap = isl_basic_map_apply_range(bmap, range);
3927 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3929 total = isl_basic_map_total_dim(bmap);
3931 for (i = 0; i < n_out; ++i) {
3932 k = isl_basic_map_alloc_equality(bmap);
3933 if (k < 0)
3934 goto error;
3935 isl_seq_clr(bmap->eq[k], 1 + total);
3936 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3937 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3940 bmap = isl_basic_map_gauss(bmap, NULL);
3941 return isl_basic_map_finalize(bmap);
3942 error:
3943 isl_basic_map_free(bmap);
3944 return NULL;
3947 int isl_map_may_be_set(__isl_keep isl_map *map)
3949 if (!map)
3950 return -1;
3951 return isl_dim_may_be_set(map->dim);
3954 struct isl_set *isl_map_range(struct isl_map *map)
3956 int i;
3957 struct isl_set *set;
3959 if (!map)
3960 goto error;
3961 if (isl_map_may_be_set(map))
3962 return (isl_set *)map;
3964 map = isl_map_cow(map);
3965 if (!map)
3966 goto error;
3968 set = (struct isl_set *) map;
3969 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3970 if (!set->dim)
3971 goto error;
3972 for (i = 0; i < map->n; ++i) {
3973 set->p[i] = isl_basic_map_range(map->p[i]);
3974 if (!set->p[i])
3975 goto error;
3977 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3978 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3979 return set;
3980 error:
3981 isl_map_free(map);
3982 return NULL;
3985 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3987 int i;
3988 isl_dim *domain_dim;
3990 map = isl_map_cow(map);
3991 if (!map)
3992 return NULL;
3994 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3995 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3996 map->dim = isl_dim_join(map->dim, domain_dim);
3997 if (!map->dim)
3998 goto error;
3999 for (i = 0; i < map->n; ++i) {
4000 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4001 if (!map->p[i])
4002 goto error;
4004 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4005 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4006 return map;
4007 error:
4008 isl_map_free(map);
4009 return NULL;
4012 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4014 int i;
4015 isl_dim *range_dim;
4017 map = isl_map_cow(map);
4018 if (!map)
4019 return NULL;
4021 range_dim = isl_dim_range(isl_map_get_dim(map));
4022 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
4023 map->dim = isl_dim_join(map->dim, range_dim);
4024 if (!map->dim)
4025 goto error;
4026 for (i = 0; i < map->n; ++i) {
4027 map->p[i] = isl_basic_map_range_map(map->p[i]);
4028 if (!map->p[i])
4029 goto error;
4031 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4032 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4033 return map;
4034 error:
4035 isl_map_free(map);
4036 return NULL;
4039 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4041 int i;
4042 struct isl_map *map = NULL;
4044 set = isl_set_cow(set);
4045 if (!set || !dim)
4046 goto error;
4047 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4048 map = (struct isl_map *)set;
4049 for (i = 0; i < set->n; ++i) {
4050 map->p[i] = isl_basic_map_from_basic_set(
4051 set->p[i], isl_dim_copy(dim));
4052 if (!map->p[i])
4053 goto error;
4055 isl_dim_free(map->dim);
4056 map->dim = dim;
4057 return map;
4058 error:
4059 isl_dim_free(dim);
4060 isl_set_free(set);
4061 return NULL;
4064 __isl_give isl_basic_map *isl_basic_map_from_domain(
4065 __isl_take isl_basic_set *bset)
4067 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4070 __isl_give isl_basic_map *isl_basic_map_from_range(
4071 __isl_take isl_basic_set *bset)
4073 return (isl_basic_map *)bset;
4076 struct isl_map *isl_map_from_range(struct isl_set *set)
4078 return (struct isl_map *)set;
4081 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4083 return isl_map_reverse(isl_map_from_range(set));
4086 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4087 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4089 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4090 isl_basic_map_from_range(range));
4093 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4094 __isl_take isl_set *range)
4096 return isl_map_apply_range(isl_map_from_domain(domain),
4097 isl_map_from_range(range));
4100 struct isl_set *isl_set_from_map(struct isl_map *map)
4102 int i;
4103 struct isl_set *set = NULL;
4105 if (!map)
4106 return NULL;
4107 map = isl_map_cow(map);
4108 if (!map)
4109 return NULL;
4110 map->dim = isl_dim_as_set_dim(map->dim);
4111 if (!map->dim)
4112 goto error;
4113 set = (struct isl_set *)map;
4114 for (i = 0; i < map->n; ++i) {
4115 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4116 if (!set->p[i])
4117 goto error;
4119 return set;
4120 error:
4121 isl_map_free(map);
4122 return NULL;
4125 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4127 struct isl_map *map;
4129 if (!dim)
4130 return NULL;
4131 isl_assert(dim->ctx, n >= 0, return NULL);
4132 map = isl_alloc(dim->ctx, struct isl_map,
4133 sizeof(struct isl_map) +
4134 (n - 1) * sizeof(struct isl_basic_map *));
4135 if (!map)
4136 goto error;
4138 map->ctx = dim->ctx;
4139 isl_ctx_ref(map->ctx);
4140 map->ref = 1;
4141 map->size = n;
4142 map->n = 0;
4143 map->dim = dim;
4144 map->flags = flags;
4145 return map;
4146 error:
4147 isl_dim_free(dim);
4148 return NULL;
4151 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4152 unsigned nparam, unsigned in, unsigned out, int n,
4153 unsigned flags)
4155 struct isl_map *map;
4156 struct isl_dim *dims;
4158 dims = isl_dim_alloc(ctx, nparam, in, out);
4159 if (!dims)
4160 return NULL;
4162 map = isl_map_alloc_dim(dims, n, flags);
4163 return map;
4166 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4168 struct isl_basic_map *bmap;
4169 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4170 bmap = isl_basic_map_set_to_empty(bmap);
4171 return bmap;
4174 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4176 struct isl_basic_set *bset;
4177 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4178 bset = isl_basic_set_set_to_empty(bset);
4179 return bset;
4182 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4184 struct isl_basic_map *bmap;
4185 if (!model)
4186 return NULL;
4187 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4188 bmap = isl_basic_map_set_to_empty(bmap);
4189 return bmap;
4192 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4194 struct isl_basic_map *bmap;
4195 if (!model)
4196 return NULL;
4197 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4198 bmap = isl_basic_map_set_to_empty(bmap);
4199 return bmap;
4202 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4204 struct isl_basic_set *bset;
4205 if (!model)
4206 return NULL;
4207 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4208 bset = isl_basic_set_set_to_empty(bset);
4209 return bset;
4212 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4214 struct isl_basic_map *bmap;
4215 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4216 bmap = isl_basic_map_finalize(bmap);
4217 return bmap;
4220 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4222 struct isl_basic_set *bset;
4223 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4224 bset = isl_basic_set_finalize(bset);
4225 return bset;
4228 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4230 int i;
4231 unsigned total = isl_dim_total(dim);
4232 isl_basic_map *bmap;
4234 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4235 for (i = 0; i < total; ++i) {
4236 int k = isl_basic_map_alloc_inequality(bmap);
4237 if (k < 0)
4238 goto error;
4239 isl_seq_clr(bmap->ineq[k], 1 + total);
4240 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4242 return bmap;
4243 error:
4244 isl_basic_map_free(bmap);
4245 return NULL;
4248 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4250 return isl_basic_map_nat_universe(dim);
4253 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4255 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4258 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4260 return isl_map_nat_universe(dim);
4263 __isl_give isl_basic_map *isl_basic_map_universe_like(
4264 __isl_keep isl_basic_map *model)
4266 if (!model)
4267 return NULL;
4268 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4271 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4273 if (!model)
4274 return NULL;
4275 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4278 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4279 __isl_keep isl_set *model)
4281 if (!model)
4282 return NULL;
4283 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4286 struct isl_map *isl_map_empty(struct isl_dim *dim)
4288 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4291 struct isl_map *isl_map_empty_like(struct isl_map *model)
4293 if (!model)
4294 return NULL;
4295 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4298 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4300 if (!model)
4301 return NULL;
4302 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4305 struct isl_set *isl_set_empty(struct isl_dim *dim)
4307 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4310 struct isl_set *isl_set_empty_like(struct isl_set *model)
4312 if (!model)
4313 return NULL;
4314 return isl_set_empty(isl_dim_copy(model->dim));
4317 struct isl_map *isl_map_universe(struct isl_dim *dim)
4319 struct isl_map *map;
4320 if (!dim)
4321 return NULL;
4322 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4323 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4324 return map;
4327 struct isl_set *isl_set_universe(struct isl_dim *dim)
4329 struct isl_set *set;
4330 if (!dim)
4331 return NULL;
4332 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4333 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4334 return set;
4337 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4339 if (!model)
4340 return NULL;
4341 return isl_set_universe(isl_dim_copy(model->dim));
4344 struct isl_map *isl_map_dup(struct isl_map *map)
4346 int i;
4347 struct isl_map *dup;
4349 if (!map)
4350 return NULL;
4351 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4352 for (i = 0; i < map->n; ++i)
4353 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4354 return dup;
4357 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4358 __isl_take isl_basic_map *bmap)
4360 if (!bmap || !map)
4361 goto error;
4362 if (isl_basic_map_plain_is_empty(bmap)) {
4363 isl_basic_map_free(bmap);
4364 return map;
4366 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4367 isl_assert(map->ctx, map->n < map->size, goto error);
4368 map->p[map->n] = bmap;
4369 map->n++;
4370 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4371 return map;
4372 error:
4373 if (map)
4374 isl_map_free(map);
4375 if (bmap)
4376 isl_basic_map_free(bmap);
4377 return NULL;
4380 void isl_map_free(struct isl_map *map)
4382 int i;
4384 if (!map)
4385 return;
4387 if (--map->ref > 0)
4388 return;
4390 isl_ctx_deref(map->ctx);
4391 for (i = 0; i < map->n; ++i)
4392 isl_basic_map_free(map->p[i]);
4393 isl_dim_free(map->dim);
4394 free(map);
4397 struct isl_map *isl_map_extend(struct isl_map *base,
4398 unsigned nparam, unsigned n_in, unsigned n_out)
4400 int i;
4402 base = isl_map_cow(base);
4403 if (!base)
4404 return NULL;
4406 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4407 if (!base->dim)
4408 goto error;
4409 for (i = 0; i < base->n; ++i) {
4410 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4411 isl_dim_copy(base->dim), 0, 0, 0);
4412 if (!base->p[i])
4413 goto error;
4415 return base;
4416 error:
4417 isl_map_free(base);
4418 return NULL;
4421 struct isl_set *isl_set_extend(struct isl_set *base,
4422 unsigned nparam, unsigned dim)
4424 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4425 nparam, 0, dim);
4428 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4429 struct isl_basic_map *bmap, unsigned pos, int value)
4431 int j;
4433 bmap = isl_basic_map_cow(bmap);
4434 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4435 j = isl_basic_map_alloc_equality(bmap);
4436 if (j < 0)
4437 goto error;
4438 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4439 isl_int_set_si(bmap->eq[j][pos], -1);
4440 isl_int_set_si(bmap->eq[j][0], value);
4441 bmap = isl_basic_map_simplify(bmap);
4442 return isl_basic_map_finalize(bmap);
4443 error:
4444 isl_basic_map_free(bmap);
4445 return NULL;
4448 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4449 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4451 int j;
4453 bmap = isl_basic_map_cow(bmap);
4454 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4455 j = isl_basic_map_alloc_equality(bmap);
4456 if (j < 0)
4457 goto error;
4458 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4459 isl_int_set_si(bmap->eq[j][pos], -1);
4460 isl_int_set(bmap->eq[j][0], value);
4461 bmap = isl_basic_map_simplify(bmap);
4462 return isl_basic_map_finalize(bmap);
4463 error:
4464 isl_basic_map_free(bmap);
4465 return NULL;
4468 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4469 enum isl_dim_type type, unsigned pos, int value)
4471 if (!bmap)
4472 return NULL;
4473 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4474 return isl_basic_map_fix_pos_si(bmap,
4475 isl_basic_map_offset(bmap, type) + pos, value);
4476 error:
4477 isl_basic_map_free(bmap);
4478 return NULL;
4481 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4482 enum isl_dim_type type, unsigned pos, isl_int value)
4484 if (!bmap)
4485 return NULL;
4486 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4487 return isl_basic_map_fix_pos(bmap,
4488 isl_basic_map_offset(bmap, type) + pos, value);
4489 error:
4490 isl_basic_map_free(bmap);
4491 return NULL;
4494 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4495 enum isl_dim_type type, unsigned pos, int value)
4497 return (struct isl_basic_set *)
4498 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4499 type, pos, value);
4502 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4503 enum isl_dim_type type, unsigned pos, isl_int value)
4505 return (struct isl_basic_set *)
4506 isl_basic_map_fix((struct isl_basic_map *)bset,
4507 type, pos, value);
4510 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4511 unsigned input, int value)
4513 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4516 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4517 unsigned dim, int value)
4519 return (struct isl_basic_set *)
4520 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4521 isl_dim_set, dim, value);
4524 struct isl_map *isl_map_fix_si(struct isl_map *map,
4525 enum isl_dim_type type, unsigned pos, int value)
4527 int i;
4529 map = isl_map_cow(map);
4530 if (!map)
4531 return NULL;
4533 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4534 for (i = 0; i < map->n; ++i) {
4535 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4536 if (!map->p[i])
4537 goto error;
4539 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4540 return map;
4541 error:
4542 isl_map_free(map);
4543 return NULL;
4546 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4547 enum isl_dim_type type, unsigned pos, int value)
4549 return (struct isl_set *)
4550 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4553 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4554 enum isl_dim_type type, unsigned pos, isl_int value)
4556 int i;
4558 map = isl_map_cow(map);
4559 if (!map)
4560 return NULL;
4562 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4563 for (i = 0; i < map->n; ++i) {
4564 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4565 if (!map->p[i])
4566 goto error;
4568 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4569 return map;
4570 error:
4571 isl_map_free(map);
4572 return NULL;
4575 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4576 enum isl_dim_type type, unsigned pos, isl_int value)
4578 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4581 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4582 unsigned input, int value)
4584 return isl_map_fix_si(map, isl_dim_in, input, value);
4587 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4589 return (struct isl_set *)
4590 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4593 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4594 __isl_take isl_basic_map *bmap,
4595 enum isl_dim_type type, unsigned pos, int value)
4597 int j;
4599 if (!bmap)
4600 return NULL;
4601 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4602 pos += isl_basic_map_offset(bmap, type);
4603 bmap = isl_basic_map_cow(bmap);
4604 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4605 j = isl_basic_map_alloc_inequality(bmap);
4606 if (j < 0)
4607 goto error;
4608 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4609 isl_int_set_si(bmap->ineq[j][pos], 1);
4610 isl_int_set_si(bmap->ineq[j][0], -value);
4611 bmap = isl_basic_map_simplify(bmap);
4612 return isl_basic_map_finalize(bmap);
4613 error:
4614 isl_basic_map_free(bmap);
4615 return NULL;
4618 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4619 unsigned dim, isl_int value)
4621 int j;
4623 bset = isl_basic_set_cow(bset);
4624 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4625 j = isl_basic_set_alloc_inequality(bset);
4626 if (j < 0)
4627 goto error;
4628 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4629 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4630 isl_int_neg(bset->ineq[j][0], value);
4631 bset = isl_basic_set_simplify(bset);
4632 return isl_basic_set_finalize(bset);
4633 error:
4634 isl_basic_set_free(bset);
4635 return NULL;
4638 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4639 enum isl_dim_type type, unsigned pos, int value)
4641 int i;
4643 map = isl_map_cow(map);
4644 if (!map)
4645 return NULL;
4647 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4648 for (i = 0; i < map->n; ++i) {
4649 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4650 type, pos, value);
4651 if (!map->p[i])
4652 goto error;
4654 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4655 return map;
4656 error:
4657 isl_map_free(map);
4658 return NULL;
4661 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4662 enum isl_dim_type type, unsigned pos, int value)
4664 return (struct isl_set *)
4665 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4668 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4669 isl_int value)
4671 int i;
4673 set = isl_set_cow(set);
4674 if (!set)
4675 return NULL;
4677 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4678 for (i = 0; i < set->n; ++i) {
4679 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4680 if (!set->p[i])
4681 goto error;
4683 return set;
4684 error:
4685 isl_set_free(set);
4686 return NULL;
4689 struct isl_map *isl_map_reverse(struct isl_map *map)
4691 int i;
4693 map = isl_map_cow(map);
4694 if (!map)
4695 return NULL;
4697 map->dim = isl_dim_reverse(map->dim);
4698 if (!map->dim)
4699 goto error;
4700 for (i = 0; i < map->n; ++i) {
4701 map->p[i] = isl_basic_map_reverse(map->p[i]);
4702 if (!map->p[i])
4703 goto error;
4705 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4706 return map;
4707 error:
4708 isl_map_free(map);
4709 return NULL;
4712 static struct isl_map *isl_basic_map_partial_lexopt(
4713 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4714 struct isl_set **empty, int max)
4716 if (!bmap)
4717 goto error;
4718 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4719 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4720 else
4721 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4722 error:
4723 isl_basic_map_free(bmap);
4724 isl_basic_set_free(dom);
4725 if (empty)
4726 *empty = NULL;
4727 return NULL;
4730 struct isl_map *isl_basic_map_partial_lexmax(
4731 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4732 struct isl_set **empty)
4734 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4737 struct isl_map *isl_basic_map_partial_lexmin(
4738 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4739 struct isl_set **empty)
4741 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4744 struct isl_set *isl_basic_set_partial_lexmin(
4745 struct isl_basic_set *bset, struct isl_basic_set *dom,
4746 struct isl_set **empty)
4748 return (struct isl_set *)
4749 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4750 dom, empty);
4753 struct isl_set *isl_basic_set_partial_lexmax(
4754 struct isl_basic_set *bset, struct isl_basic_set *dom,
4755 struct isl_set **empty)
4757 return (struct isl_set *)
4758 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4759 dom, empty);
4762 /* Given a basic map "bmap", compute the lexicographically minimal
4763 * (or maximal) image element for each domain element in dom.
4764 * Set *empty to those elements in dom that do not have an image element.
4766 * We first make sure the basic sets in dom are disjoint and then
4767 * simply collect the results over each of the basic sets separately.
4768 * We could probably improve the efficiency a bit by moving the union
4769 * domain down into the parametric integer programming.
4771 static __isl_give isl_map *basic_map_partial_lexopt(
4772 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4773 __isl_give isl_set **empty, int max)
4775 int i;
4776 struct isl_map *res;
4778 dom = isl_set_make_disjoint(dom);
4779 if (!dom)
4780 goto error;
4782 if (isl_set_plain_is_empty(dom)) {
4783 res = isl_map_empty_like_basic_map(bmap);
4784 *empty = isl_set_empty_like(dom);
4785 isl_set_free(dom);
4786 isl_basic_map_free(bmap);
4787 return res;
4790 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4791 isl_basic_set_copy(dom->p[0]), empty, max);
4793 for (i = 1; i < dom->n; ++i) {
4794 struct isl_map *res_i;
4795 struct isl_set *empty_i;
4797 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4798 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4800 res = isl_map_union_disjoint(res, res_i);
4801 *empty = isl_set_union_disjoint(*empty, empty_i);
4804 isl_set_free(dom);
4805 isl_basic_map_free(bmap);
4806 return res;
4807 error:
4808 *empty = NULL;
4809 isl_set_free(dom);
4810 isl_basic_map_free(bmap);
4811 return NULL;
4814 /* Given a map "map", compute the lexicographically minimal
4815 * (or maximal) image element for each domain element in dom.
4816 * Set *empty to those elements in dom that do not have an image element.
4818 * We first compute the lexicographically minimal or maximal element
4819 * in the first basic map. This results in a partial solution "res"
4820 * and a subset "todo" of dom that still need to be handled.
4821 * We then consider each of the remaining maps in "map" and successively
4822 * improve both "res" and "todo".
4824 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4825 * Assume we are computing the lexicographical maximum.
4826 * We first compute the lexicographically maximal element in basic map i.
4827 * This results in a partial solution res_i and a subset todo_i.
4828 * Then we combine these results with those obtain for the first k basic maps
4829 * to obtain a result that is valid for the first k+1 basic maps.
4830 * In particular, the set where there is no solution is the set where
4831 * there is no solution for the first k basic maps and also no solution
4832 * for the ith basic map, i.e.,
4834 * todo^i = todo^k * todo_i
4836 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4837 * solutions, arbitrarily breaking ties in favor of res^k.
4838 * That is, when res^k(a) >= res_i(a), we pick res^k and
4839 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4840 * the lexicographic order.)
4841 * In practice, we compute
4843 * res^k * (res_i . "<=")
4845 * and
4847 * res_i * (res^k . "<")
4849 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4850 * where only one of res^k and res_i provides a solution and we simply pick
4851 * that one, i.e.,
4853 * res^k * todo_i
4854 * and
4855 * res_i * todo^k
4857 * Note that we only compute these intersections when dom(res^k) intersects
4858 * dom(res_i). Otherwise, the only effect of these intersections is to
4859 * potentially break up res^k and res_i into smaller pieces.
4860 * We want to avoid such splintering as much as possible.
4861 * In fact, an earlier implementation of this function would look for
4862 * better results in the domain of res^k and for extra results in todo^k,
4863 * but this would always result in a splintering according to todo^k,
4864 * even when the domain of basic map i is disjoint from the domains of
4865 * the previous basic maps.
4867 static __isl_give isl_map *isl_map_partial_lexopt(
4868 __isl_take isl_map *map, __isl_take isl_set *dom,
4869 __isl_give isl_set **empty, int max)
4871 int i;
4872 struct isl_map *res;
4873 struct isl_set *todo;
4875 if (!map || !dom)
4876 goto error;
4878 if (isl_map_plain_is_empty(map)) {
4879 if (empty)
4880 *empty = dom;
4881 else
4882 isl_set_free(dom);
4883 return map;
4886 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4887 isl_set_copy(dom), &todo, max);
4889 for (i = 1; i < map->n; ++i) {
4890 isl_map *lt, *le;
4891 isl_map *res_i;
4892 isl_set *todo_i;
4893 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4895 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4896 isl_set_copy(dom), &todo_i, max);
4898 if (max) {
4899 lt = isl_map_lex_lt(isl_dim_copy(dim));
4900 le = isl_map_lex_le(dim);
4901 } else {
4902 lt = isl_map_lex_gt(isl_dim_copy(dim));
4903 le = isl_map_lex_ge(dim);
4905 lt = isl_map_apply_range(isl_map_copy(res), lt);
4906 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4907 le = isl_map_apply_range(isl_map_copy(res_i), le);
4908 le = isl_map_intersect(le, isl_map_copy(res));
4910 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4911 res = isl_map_intersect_domain(res,
4912 isl_set_copy(todo_i));
4913 res_i = isl_map_intersect_domain(res_i,
4914 isl_set_copy(todo));
4917 res = isl_map_union_disjoint(res, res_i);
4918 res = isl_map_union_disjoint(res, lt);
4919 res = isl_map_union_disjoint(res, le);
4921 todo = isl_set_intersect(todo, todo_i);
4924 isl_set_free(dom);
4925 isl_map_free(map);
4927 if (empty)
4928 *empty = todo;
4929 else
4930 isl_set_free(todo);
4932 return res;
4933 error:
4934 if (empty)
4935 *empty = NULL;
4936 isl_set_free(dom);
4937 isl_map_free(map);
4938 return NULL;
4941 __isl_give isl_map *isl_map_partial_lexmax(
4942 __isl_take isl_map *map, __isl_take isl_set *dom,
4943 __isl_give isl_set **empty)
4945 return isl_map_partial_lexopt(map, dom, empty, 1);
4948 __isl_give isl_map *isl_map_partial_lexmin(
4949 __isl_take isl_map *map, __isl_take isl_set *dom,
4950 __isl_give isl_set **empty)
4952 return isl_map_partial_lexopt(map, dom, empty, 0);
4955 __isl_give isl_set *isl_set_partial_lexmin(
4956 __isl_take isl_set *set, __isl_take isl_set *dom,
4957 __isl_give isl_set **empty)
4959 return (struct isl_set *)
4960 isl_map_partial_lexmin((struct isl_map *)set,
4961 dom, empty);
4964 __isl_give isl_set *isl_set_partial_lexmax(
4965 __isl_take isl_set *set, __isl_take isl_set *dom,
4966 __isl_give isl_set **empty)
4968 return (struct isl_set *)
4969 isl_map_partial_lexmax((struct isl_map *)set,
4970 dom, empty);
4973 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4975 struct isl_basic_set *dom = NULL;
4976 struct isl_dim *dom_dim;
4978 if (!bmap)
4979 goto error;
4980 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4981 dom = isl_basic_set_universe(dom_dim);
4982 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4983 error:
4984 isl_basic_map_free(bmap);
4985 return NULL;
4988 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4990 return isl_basic_map_lexopt(bmap, 0);
4993 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4995 return isl_basic_map_lexopt(bmap, 1);
4998 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5000 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5003 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5005 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5008 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5010 struct isl_set *dom = NULL;
5011 struct isl_dim *dom_dim;
5013 if (!map)
5014 goto error;
5015 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
5016 dom = isl_set_universe(dom_dim);
5017 return isl_map_partial_lexopt(map, dom, NULL, max);
5018 error:
5019 isl_map_free(map);
5020 return NULL;
5023 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5025 return isl_map_lexopt(map, 0);
5028 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5030 return isl_map_lexopt(map, 1);
5033 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5035 return (isl_set *)isl_map_lexmin((isl_map *)set);
5038 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5040 return (isl_set *)isl_map_lexmax((isl_map *)set);
5043 /* Apply a preimage specified by "mat" on the parameters of "bset".
5044 * bset is assumed to have only parameters and divs.
5046 static struct isl_basic_set *basic_set_parameter_preimage(
5047 struct isl_basic_set *bset, struct isl_mat *mat)
5049 unsigned nparam;
5051 if (!bset || !mat)
5052 goto error;
5054 bset->dim = isl_dim_cow(bset->dim);
5055 if (!bset->dim)
5056 goto error;
5058 nparam = isl_basic_set_dim(bset, isl_dim_param);
5060 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5062 bset->dim->nparam = 0;
5063 bset->dim->n_out = nparam;
5064 bset = isl_basic_set_preimage(bset, mat);
5065 if (bset) {
5066 bset->dim->nparam = bset->dim->n_out;
5067 bset->dim->n_out = 0;
5069 return bset;
5070 error:
5071 isl_mat_free(mat);
5072 isl_basic_set_free(bset);
5073 return NULL;
5076 /* Apply a preimage specified by "mat" on the parameters of "set".
5077 * set is assumed to have only parameters and divs.
5079 static struct isl_set *set_parameter_preimage(
5080 struct isl_set *set, struct isl_mat *mat)
5082 struct isl_dim *dim = NULL;
5083 unsigned nparam;
5085 if (!set || !mat)
5086 goto error;
5088 dim = isl_dim_copy(set->dim);
5089 dim = isl_dim_cow(dim);
5090 if (!dim)
5091 goto error;
5093 nparam = isl_set_dim(set, isl_dim_param);
5095 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5097 dim->nparam = 0;
5098 dim->n_out = nparam;
5099 isl_set_reset_dim(set, dim);
5100 set = isl_set_preimage(set, mat);
5101 if (!set)
5102 goto error2;
5103 dim = isl_dim_copy(set->dim);
5104 dim = isl_dim_cow(dim);
5105 if (!dim)
5106 goto error2;
5107 dim->nparam = dim->n_out;
5108 dim->n_out = 0;
5109 isl_set_reset_dim(set, dim);
5110 return set;
5111 error:
5112 isl_dim_free(dim);
5113 isl_mat_free(mat);
5114 error2:
5115 isl_set_free(set);
5116 return NULL;
5119 /* Intersect the basic set "bset" with the affine space specified by the
5120 * equalities in "eq".
5122 static struct isl_basic_set *basic_set_append_equalities(
5123 struct isl_basic_set *bset, struct isl_mat *eq)
5125 int i, k;
5126 unsigned len;
5128 if (!bset || !eq)
5129 goto error;
5131 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5132 eq->n_row, 0);
5133 if (!bset)
5134 goto error;
5136 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5137 for (i = 0; i < eq->n_row; ++i) {
5138 k = isl_basic_set_alloc_equality(bset);
5139 if (k < 0)
5140 goto error;
5141 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5142 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5144 isl_mat_free(eq);
5146 bset = isl_basic_set_gauss(bset, NULL);
5147 bset = isl_basic_set_finalize(bset);
5149 return bset;
5150 error:
5151 isl_mat_free(eq);
5152 isl_basic_set_free(bset);
5153 return NULL;
5156 /* Intersect the set "set" with the affine space specified by the
5157 * equalities in "eq".
5159 static struct isl_set *set_append_equalities(struct isl_set *set,
5160 struct isl_mat *eq)
5162 int i;
5164 if (!set || !eq)
5165 goto error;
5167 for (i = 0; i < set->n; ++i) {
5168 set->p[i] = basic_set_append_equalities(set->p[i],
5169 isl_mat_copy(eq));
5170 if (!set->p[i])
5171 goto error;
5173 isl_mat_free(eq);
5174 return set;
5175 error:
5176 isl_mat_free(eq);
5177 isl_set_free(set);
5178 return NULL;
5181 /* Project the given basic set onto its parameter domain, possibly introducing
5182 * new, explicit, existential variables in the constraints.
5183 * The input has parameters and (possibly implicit) existential variables.
5184 * The output has the same parameters, but only
5185 * explicit existentially quantified variables.
5187 * The actual projection is performed by pip, but pip doesn't seem
5188 * to like equalities very much, so we first remove the equalities
5189 * among the parameters by performing a variable compression on
5190 * the parameters. Afterward, an inverse transformation is performed
5191 * and the equalities among the parameters are inserted back in.
5193 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5195 int i, j;
5196 struct isl_mat *eq;
5197 struct isl_mat *T, *T2;
5198 struct isl_set *set;
5199 unsigned nparam, n_div;
5201 bset = isl_basic_set_cow(bset);
5202 if (!bset)
5203 return NULL;
5205 if (bset->n_eq == 0)
5206 return isl_basic_set_lexmin(bset);
5208 isl_basic_set_gauss(bset, NULL);
5210 nparam = isl_basic_set_dim(bset, isl_dim_param);
5211 n_div = isl_basic_set_dim(bset, isl_dim_div);
5213 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5214 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5215 ++i;
5217 if (i == bset->n_eq)
5218 return isl_basic_set_lexmin(bset);
5220 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5221 0, 1 + nparam);
5222 eq = isl_mat_cow(eq);
5223 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5224 if (T && T->n_col == 0) {
5225 isl_mat_free(T);
5226 isl_mat_free(T2);
5227 isl_mat_free(eq);
5228 bset = isl_basic_set_set_to_empty(bset);
5229 return isl_set_from_basic_set(bset);
5231 bset = basic_set_parameter_preimage(bset, T);
5233 set = isl_basic_set_lexmin(bset);
5234 set = set_parameter_preimage(set, T2);
5235 set = set_append_equalities(set, eq);
5236 return set;
5239 /* Compute an explicit representation for all the existentially
5240 * quantified variables.
5241 * The input and output dimensions are first turned into parameters.
5242 * compute_divs then returns a map with the same parameters and
5243 * no input or output dimensions and the dimension specification
5244 * is reset to that of the input.
5246 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5248 struct isl_basic_set *bset;
5249 struct isl_set *set;
5250 struct isl_map *map;
5251 struct isl_dim *dim, *orig_dim = NULL;
5252 unsigned nparam;
5253 unsigned n_in;
5254 unsigned n_out;
5256 bmap = isl_basic_map_cow(bmap);
5257 if (!bmap)
5258 return NULL;
5260 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5261 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5262 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5263 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5264 if (!dim)
5265 goto error;
5267 orig_dim = bmap->dim;
5268 bmap->dim = dim;
5269 bset = (struct isl_basic_set *)bmap;
5271 set = parameter_compute_divs(bset);
5272 map = (struct isl_map *)set;
5273 map = isl_map_reset_dim(map, orig_dim);
5275 return map;
5276 error:
5277 isl_basic_map_free(bmap);
5278 return NULL;
5281 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5283 int i;
5284 unsigned off;
5286 if (!bmap)
5287 return -1;
5289 off = isl_dim_total(bmap->dim);
5290 for (i = 0; i < bmap->n_div; ++i) {
5291 if (isl_int_is_zero(bmap->div[i][0]))
5292 return 0;
5293 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5294 return -1);
5296 return 1;
5299 static int map_divs_known(__isl_keep isl_map *map)
5301 int i;
5303 if (!map)
5304 return -1;
5306 for (i = 0; i < map->n; ++i) {
5307 int known = basic_map_divs_known(map->p[i]);
5308 if (known <= 0)
5309 return known;
5312 return 1;
5315 /* If bmap contains any unknown divs, then compute explicit
5316 * expressions for them. However, this computation may be
5317 * quite expensive, so first try to remove divs that aren't
5318 * strictly needed.
5320 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5322 int i;
5323 int known;
5324 struct isl_map *map;
5326 known = basic_map_divs_known(bmap);
5327 if (known < 0)
5328 goto error;
5329 if (known)
5330 return isl_map_from_basic_map(bmap);
5332 bmap = isl_basic_map_drop_redundant_divs(bmap);
5334 known = basic_map_divs_known(bmap);
5335 if (known < 0)
5336 goto error;
5337 if (known)
5338 return isl_map_from_basic_map(bmap);
5340 map = compute_divs(bmap);
5341 return map;
5342 error:
5343 isl_basic_map_free(bmap);
5344 return NULL;
5347 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5349 int i;
5350 int known;
5351 struct isl_map *res;
5353 if (!map)
5354 return NULL;
5355 if (map->n == 0)
5356 return map;
5358 known = map_divs_known(map);
5359 if (known < 0) {
5360 isl_map_free(map);
5361 return NULL;
5363 if (known)
5364 return map;
5366 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5367 for (i = 1 ; i < map->n; ++i) {
5368 struct isl_map *r2;
5369 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5370 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5371 res = isl_map_union_disjoint(res, r2);
5372 else
5373 res = isl_map_union(res, r2);
5375 isl_map_free(map);
5377 return res;
5380 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5382 return (struct isl_set *)
5383 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5386 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5388 return (struct isl_set *)
5389 isl_map_compute_divs((struct isl_map *)set);
5392 struct isl_set *isl_map_domain(struct isl_map *map)
5394 int i;
5395 struct isl_set *set;
5397 if (!map)
5398 goto error;
5400 map = isl_map_cow(map);
5401 if (!map)
5402 return NULL;
5404 set = (struct isl_set *)map;
5405 set->dim = isl_dim_domain(set->dim);
5406 if (!set->dim)
5407 goto error;
5408 for (i = 0; i < map->n; ++i) {
5409 set->p[i] = isl_basic_map_domain(map->p[i]);
5410 if (!set->p[i])
5411 goto error;
5413 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5414 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5415 return set;
5416 error:
5417 isl_map_free(map);
5418 return NULL;
5421 struct isl_map *isl_map_union_disjoint(
5422 struct isl_map *map1, struct isl_map *map2)
5424 int i;
5425 unsigned flags = 0;
5426 struct isl_map *map = NULL;
5428 if (!map1 || !map2)
5429 goto error;
5431 if (map1->n == 0) {
5432 isl_map_free(map1);
5433 return map2;
5435 if (map2->n == 0) {
5436 isl_map_free(map2);
5437 return map1;
5440 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5442 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5443 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5444 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5446 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5447 map1->n + map2->n, flags);
5448 if (!map)
5449 goto error;
5450 for (i = 0; i < map1->n; ++i) {
5451 map = isl_map_add_basic_map(map,
5452 isl_basic_map_copy(map1->p[i]));
5453 if (!map)
5454 goto error;
5456 for (i = 0; i < map2->n; ++i) {
5457 map = isl_map_add_basic_map(map,
5458 isl_basic_map_copy(map2->p[i]));
5459 if (!map)
5460 goto error;
5462 isl_map_free(map1);
5463 isl_map_free(map2);
5464 return map;
5465 error:
5466 isl_map_free(map);
5467 isl_map_free(map1);
5468 isl_map_free(map2);
5469 return NULL;
5472 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5474 map1 = isl_map_union_disjoint(map1, map2);
5475 if (!map1)
5476 return NULL;
5477 if (map1->n > 1)
5478 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5479 return map1;
5482 struct isl_set *isl_set_union_disjoint(
5483 struct isl_set *set1, struct isl_set *set2)
5485 return (struct isl_set *)
5486 isl_map_union_disjoint(
5487 (struct isl_map *)set1, (struct isl_map *)set2);
5490 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5492 return (struct isl_set *)
5493 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5496 struct isl_map *isl_map_intersect_range(
5497 struct isl_map *map, struct isl_set *set)
5499 unsigned flags = 0;
5500 struct isl_map *result;
5501 int i, j;
5503 if (!map || !set)
5504 goto error;
5506 if (!isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5507 isl_die(set->ctx, isl_error_invalid,
5508 "parameters don't match", goto error);
5510 if (isl_dim_size(set->dim, isl_dim_set) != 0 &&
5511 !isl_map_compatible_range(map, set))
5512 isl_die(set->ctx, isl_error_invalid,
5513 "incompatible spaces", goto error);
5515 if (isl_set_plain_is_universe(set)) {
5516 isl_set_free(set);
5517 return map;
5520 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5521 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5522 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5524 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5525 map->n * set->n, flags);
5526 if (!result)
5527 goto error;
5528 for (i = 0; i < map->n; ++i)
5529 for (j = 0; j < set->n; ++j) {
5530 result = isl_map_add_basic_map(result,
5531 isl_basic_map_intersect_range(
5532 isl_basic_map_copy(map->p[i]),
5533 isl_basic_set_copy(set->p[j])));
5534 if (!result)
5535 goto error;
5537 isl_map_free(map);
5538 isl_set_free(set);
5539 return result;
5540 error:
5541 isl_map_free(map);
5542 isl_set_free(set);
5543 return NULL;
5546 struct isl_map *isl_map_intersect_domain(
5547 struct isl_map *map, struct isl_set *set)
5549 return isl_map_reverse(
5550 isl_map_intersect_range(isl_map_reverse(map), set));
5553 struct isl_map *isl_map_apply_domain(
5554 struct isl_map *map1, struct isl_map *map2)
5556 if (!map1 || !map2)
5557 goto error;
5558 map1 = isl_map_reverse(map1);
5559 map1 = isl_map_apply_range(map1, map2);
5560 return isl_map_reverse(map1);
5561 error:
5562 isl_map_free(map1);
5563 isl_map_free(map2);
5564 return NULL;
5567 struct isl_map *isl_map_apply_range(
5568 struct isl_map *map1, struct isl_map *map2)
5570 struct isl_dim *dim_result;
5571 struct isl_map *result;
5572 int i, j;
5574 if (!map1 || !map2)
5575 goto error;
5577 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5578 isl_dim_copy(map2->dim));
5580 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5581 if (!result)
5582 goto error;
5583 for (i = 0; i < map1->n; ++i)
5584 for (j = 0; j < map2->n; ++j) {
5585 result = isl_map_add_basic_map(result,
5586 isl_basic_map_apply_range(
5587 isl_basic_map_copy(map1->p[i]),
5588 isl_basic_map_copy(map2->p[j])));
5589 if (!result)
5590 goto error;
5592 isl_map_free(map1);
5593 isl_map_free(map2);
5594 if (result && result->n <= 1)
5595 ISL_F_SET(result, ISL_MAP_DISJOINT);
5596 return result;
5597 error:
5598 isl_map_free(map1);
5599 isl_map_free(map2);
5600 return NULL;
5604 * returns range - domain
5606 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5608 isl_dim *dims, *target_dim;
5609 struct isl_basic_set *bset;
5610 unsigned dim;
5611 unsigned nparam;
5612 int i;
5614 if (!bmap)
5615 goto error;
5616 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5617 bmap->dim, isl_dim_out),
5618 goto error);
5619 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5620 dim = isl_basic_map_n_in(bmap);
5621 nparam = isl_basic_map_n_param(bmap);
5622 bset = isl_basic_set_from_basic_map(bmap);
5623 bset = isl_basic_set_cow(bset);
5624 dims = isl_basic_set_get_dim(bset);
5625 dims = isl_dim_add(dims, isl_dim_set, dim);
5626 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5627 bset = isl_basic_set_swap_vars(bset, 2*dim);
5628 for (i = 0; i < dim; ++i) {
5629 int j = isl_basic_map_alloc_equality(
5630 (struct isl_basic_map *)bset);
5631 if (j < 0)
5632 goto error;
5633 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5634 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5635 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5636 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5638 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5639 bset = isl_basic_set_reset_dim(bset, target_dim);
5640 return bset;
5641 error:
5642 isl_basic_map_free(bmap);
5643 return NULL;
5647 * returns range - domain
5649 struct isl_set *isl_map_deltas(struct isl_map *map)
5651 int i;
5652 isl_dim *dim;
5653 struct isl_set *result;
5655 if (!map)
5656 return NULL;
5658 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5659 map->dim, isl_dim_out),
5660 goto error);
5661 dim = isl_map_get_dim(map);
5662 dim = isl_dim_domain(dim);
5663 result = isl_set_alloc_dim(dim, map->n, 0);
5664 if (!result)
5665 goto error;
5666 for (i = 0; i < map->n; ++i)
5667 result = isl_set_add_basic_set(result,
5668 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5669 isl_map_free(map);
5670 return result;
5671 error:
5672 isl_map_free(map);
5673 return NULL;
5677 * returns [domain -> range] -> range - domain
5679 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5680 __isl_take isl_basic_map *bmap)
5682 int i, k;
5683 isl_dim *dim;
5684 isl_basic_map *domain;
5685 isl_basic_set *bset;
5686 int nparam, n;
5687 unsigned total;
5689 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5690 isl_die(bmap->ctx, isl_error_invalid,
5691 "domain and range don't match", goto error);
5693 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5694 n = isl_basic_map_dim(bmap, isl_dim_in);
5696 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5697 domain = isl_basic_map_universe(dim);
5699 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5700 bmap = isl_basic_map_apply_range(bmap, domain);
5701 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5703 total = isl_basic_map_total_dim(bmap);
5705 for (i = 0; i < n; ++i) {
5706 k = isl_basic_map_alloc_equality(bmap);
5707 if (k < 0)
5708 goto error;
5709 isl_seq_clr(bmap->eq[k], 1 + total);
5710 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5711 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5712 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5715 bmap = isl_basic_map_gauss(bmap, NULL);
5716 return isl_basic_map_finalize(bmap);
5717 error:
5718 isl_basic_map_free(bmap);
5719 return NULL;
5723 * returns [domain -> range] -> range - domain
5725 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5727 int i;
5728 isl_dim *domain_dim;
5730 if (!map)
5731 return NULL;
5733 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5734 isl_die(map->ctx, isl_error_invalid,
5735 "domain and range don't match", goto error);
5737 map = isl_map_cow(map);
5738 if (!map)
5739 return NULL;
5741 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5742 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5743 map->dim = isl_dim_join(map->dim, domain_dim);
5744 if (!map->dim)
5745 goto error;
5746 for (i = 0; i < map->n; ++i) {
5747 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5748 if (!map->p[i])
5749 goto error;
5751 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5752 return map;
5753 error:
5754 isl_map_free(map);
5755 return NULL;
5758 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5760 struct isl_basic_map *bmap;
5761 unsigned nparam;
5762 unsigned dim;
5763 int i;
5765 if (!dims)
5766 return NULL;
5768 nparam = dims->nparam;
5769 dim = dims->n_out;
5770 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5771 if (!bmap)
5772 goto error;
5774 for (i = 0; i < dim; ++i) {
5775 int j = isl_basic_map_alloc_equality(bmap);
5776 if (j < 0)
5777 goto error;
5778 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5779 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5780 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5782 return isl_basic_map_finalize(bmap);
5783 error:
5784 isl_basic_map_free(bmap);
5785 return NULL;
5788 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5790 if (!dim)
5791 return NULL;
5792 if (dim->n_in != dim->n_out)
5793 isl_die(dim->ctx, isl_error_invalid,
5794 "number of input and output dimensions needs to be "
5795 "the same", goto error);
5796 return basic_map_identity(dim);
5797 error:
5798 isl_dim_free(dim);
5799 return NULL;
5802 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5804 if (!model || !model->dim)
5805 return NULL;
5806 return isl_basic_map_identity(isl_dim_copy(model->dim));
5809 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5811 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5814 struct isl_map *isl_map_identity_like(struct isl_map *model)
5816 if (!model || !model->dim)
5817 return NULL;
5818 return isl_map_identity(isl_dim_copy(model->dim));
5821 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5823 if (!model || !model->dim)
5824 return NULL;
5825 return isl_map_identity(isl_dim_copy(model->dim));
5828 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5830 isl_dim *dim = isl_set_get_dim(set);
5831 isl_map *id;
5832 id = isl_map_identity(isl_dim_map_from_set(dim));
5833 return isl_map_intersect_range(id, set);
5836 /* Construct a basic set with all set dimensions having only non-negative
5837 * values.
5839 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5841 int i;
5842 unsigned nparam;
5843 unsigned dim;
5844 struct isl_basic_set *bset;
5846 if (!dims)
5847 return NULL;
5848 nparam = dims->nparam;
5849 dim = dims->n_out;
5850 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5851 if (!bset)
5852 return NULL;
5853 for (i = 0; i < dim; ++i) {
5854 int k = isl_basic_set_alloc_inequality(bset);
5855 if (k < 0)
5856 goto error;
5857 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5858 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5860 return bset;
5861 error:
5862 isl_basic_set_free(bset);
5863 return NULL;
5866 /* Construct the half-space x_pos >= 0.
5868 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5869 int pos)
5871 int k;
5872 isl_basic_set *nonneg;
5874 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5875 k = isl_basic_set_alloc_inequality(nonneg);
5876 if (k < 0)
5877 goto error;
5878 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5879 isl_int_set_si(nonneg->ineq[k][pos], 1);
5881 return isl_basic_set_finalize(nonneg);
5882 error:
5883 isl_basic_set_free(nonneg);
5884 return NULL;
5887 /* Construct the half-space x_pos <= -1.
5889 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5891 int k;
5892 isl_basic_set *neg;
5894 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5895 k = isl_basic_set_alloc_inequality(neg);
5896 if (k < 0)
5897 goto error;
5898 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5899 isl_int_set_si(neg->ineq[k][0], -1);
5900 isl_int_set_si(neg->ineq[k][pos], -1);
5902 return isl_basic_set_finalize(neg);
5903 error:
5904 isl_basic_set_free(neg);
5905 return NULL;
5908 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5909 enum isl_dim_type type, unsigned first, unsigned n)
5911 int i;
5912 isl_basic_set *nonneg;
5913 isl_basic_set *neg;
5915 if (!set)
5916 return NULL;
5917 if (n == 0)
5918 return set;
5920 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5922 for (i = 0; i < n; ++i) {
5923 nonneg = nonneg_halfspace(isl_set_get_dim(set),
5924 pos(set->dim, type) + first + i);
5925 neg = neg_halfspace(isl_set_get_dim(set),
5926 pos(set->dim, type) + first + i);
5928 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5931 return set;
5932 error:
5933 isl_set_free(set);
5934 return NULL;
5937 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
5938 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5939 void *user)
5941 isl_set *half;
5943 if (!set)
5944 return -1;
5945 if (isl_set_plain_is_empty(set)) {
5946 isl_set_free(set);
5947 return 0;
5949 if (first == len)
5950 return fn(set, signs, user);
5952 signs[first] = 1;
5953 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
5954 1 + first));
5955 half = isl_set_intersect(half, isl_set_copy(set));
5956 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
5957 goto error;
5959 signs[first] = -1;
5960 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
5961 1 + first));
5962 half = isl_set_intersect(half, set);
5963 return foreach_orthant(half, signs, first + 1, len, fn, user);
5964 error:
5965 isl_set_free(set);
5966 return -1;
5969 /* Call "fn" on the intersections of "set" with each of the orthants
5970 * (except for obviously empty intersections). The orthant is identified
5971 * by the signs array, with each entry having value 1 or -1 according
5972 * to the sign of the corresponding variable.
5974 int isl_set_foreach_orthant(__isl_keep isl_set *set,
5975 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5976 void *user)
5978 unsigned nparam;
5979 unsigned nvar;
5980 int *signs;
5981 int r;
5983 if (!set)
5984 return -1;
5985 if (isl_set_plain_is_empty(set))
5986 return 0;
5988 nparam = isl_set_dim(set, isl_dim_param);
5989 nvar = isl_set_dim(set, isl_dim_set);
5991 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
5993 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
5994 fn, user);
5996 free(signs);
5998 return r;
6001 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6003 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6006 int isl_basic_map_is_subset(
6007 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6009 int is_subset;
6010 struct isl_map *map1;
6011 struct isl_map *map2;
6013 if (!bmap1 || !bmap2)
6014 return -1;
6016 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6017 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6019 is_subset = isl_map_is_subset(map1, map2);
6021 isl_map_free(map1);
6022 isl_map_free(map2);
6024 return is_subset;
6027 int isl_basic_map_is_equal(
6028 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6030 int is_subset;
6032 if (!bmap1 || !bmap2)
6033 return -1;
6034 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6035 if (is_subset != 1)
6036 return is_subset;
6037 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6038 return is_subset;
6041 int isl_basic_set_is_equal(
6042 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6044 return isl_basic_map_is_equal(
6045 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6048 int isl_map_is_empty(struct isl_map *map)
6050 int i;
6051 int is_empty;
6053 if (!map)
6054 return -1;
6055 for (i = 0; i < map->n; ++i) {
6056 is_empty = isl_basic_map_is_empty(map->p[i]);
6057 if (is_empty < 0)
6058 return -1;
6059 if (!is_empty)
6060 return 0;
6062 return 1;
6065 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6067 return map ? map->n == 0 : -1;
6070 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6072 return isl_map_plain_is_empty(map);
6075 int isl_set_plain_is_empty(struct isl_set *set)
6077 return set ? set->n == 0 : -1;
6080 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6082 return isl_set_plain_is_empty(set);
6085 int isl_set_is_empty(struct isl_set *set)
6087 return isl_map_is_empty((struct isl_map *)set);
6090 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6092 if (!map1 || !map2)
6093 return -1;
6095 return isl_dim_equal(map1->dim, map2->dim);
6098 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6100 if (!set1 || !set2)
6101 return -1;
6103 return isl_dim_equal(set1->dim, set2->dim);
6106 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
6108 int is_subset;
6110 if (!map1 || !map2)
6111 return -1;
6112 is_subset = isl_map_is_subset(map1, map2);
6113 if (is_subset != 1)
6114 return is_subset;
6115 is_subset = isl_map_is_subset(map2, map1);
6116 return is_subset;
6119 int isl_basic_map_is_strict_subset(
6120 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6122 int is_subset;
6124 if (!bmap1 || !bmap2)
6125 return -1;
6126 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6127 if (is_subset != 1)
6128 return is_subset;
6129 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6130 if (is_subset == -1)
6131 return is_subset;
6132 return !is_subset;
6135 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6137 int is_subset;
6139 if (!map1 || !map2)
6140 return -1;
6141 is_subset = isl_map_is_subset(map1, map2);
6142 if (is_subset != 1)
6143 return is_subset;
6144 is_subset = isl_map_is_subset(map2, map1);
6145 if (is_subset == -1)
6146 return is_subset;
6147 return !is_subset;
6150 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6152 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6155 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6157 if (!bmap)
6158 return -1;
6159 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6162 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6164 if (!bset)
6165 return -1;
6166 return bset->n_eq == 0 && bset->n_ineq == 0;
6169 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6171 int i;
6173 if (!map)
6174 return -1;
6176 for (i = 0; i < map->n; ++i) {
6177 int r = isl_basic_map_is_universe(map->p[i]);
6178 if (r < 0 || r)
6179 return r;
6182 return 0;
6185 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6187 return isl_map_plain_is_universe((isl_map *) set);
6190 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6192 return isl_set_plain_is_universe(set);
6195 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6197 struct isl_basic_set *bset = NULL;
6198 struct isl_vec *sample = NULL;
6199 int empty;
6200 unsigned total;
6202 if (!bmap)
6203 return -1;
6205 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6206 return 1;
6208 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6209 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6210 copy = isl_basic_map_remove_redundancies(copy);
6211 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6212 isl_basic_map_free(copy);
6213 return empty;
6216 total = 1 + isl_basic_map_total_dim(bmap);
6217 if (bmap->sample && bmap->sample->size == total) {
6218 int contains = isl_basic_map_contains(bmap, bmap->sample);
6219 if (contains < 0)
6220 return -1;
6221 if (contains)
6222 return 0;
6224 isl_vec_free(bmap->sample);
6225 bmap->sample = NULL;
6226 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6227 if (!bset)
6228 return -1;
6229 sample = isl_basic_set_sample_vec(bset);
6230 if (!sample)
6231 return -1;
6232 empty = sample->size == 0;
6233 isl_vec_free(bmap->sample);
6234 bmap->sample = sample;
6235 if (empty)
6236 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6238 return empty;
6241 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6243 if (!bmap)
6244 return -1;
6245 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6248 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6250 return isl_basic_map_plain_is_empty(bmap);
6253 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6255 if (!bset)
6256 return -1;
6257 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6260 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6262 return isl_basic_set_plain_is_empty(bset);
6265 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6267 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6270 struct isl_map *isl_basic_map_union(
6271 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6273 struct isl_map *map;
6274 if (!bmap1 || !bmap2)
6275 return NULL;
6277 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6279 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6280 if (!map)
6281 goto error;
6282 map = isl_map_add_basic_map(map, bmap1);
6283 map = isl_map_add_basic_map(map, bmap2);
6284 return map;
6285 error:
6286 isl_basic_map_free(bmap1);
6287 isl_basic_map_free(bmap2);
6288 return NULL;
6291 struct isl_set *isl_basic_set_union(
6292 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6294 return (struct isl_set *)isl_basic_map_union(
6295 (struct isl_basic_map *)bset1,
6296 (struct isl_basic_map *)bset2);
6299 /* Order divs such that any div only depends on previous divs */
6300 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6302 int i;
6303 unsigned off;
6305 if (!bmap)
6306 return NULL;
6308 off = isl_dim_total(bmap->dim);
6310 for (i = 0; i < bmap->n_div; ++i) {
6311 int pos;
6312 if (isl_int_is_zero(bmap->div[i][0]))
6313 continue;
6314 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6315 bmap->n_div-i);
6316 if (pos == -1)
6317 continue;
6318 isl_basic_map_swap_div(bmap, i, i + pos);
6319 --i;
6321 return bmap;
6324 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6326 return (struct isl_basic_set *)
6327 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6330 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6332 int i;
6334 if (!map)
6335 return 0;
6337 for (i = 0; i < map->n; ++i) {
6338 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6339 if (!map->p[i])
6340 goto error;
6343 return map;
6344 error:
6345 isl_map_free(map);
6346 return NULL;
6349 /* Apply the expansion computed by isl_merge_divs.
6350 * The expansion itself is given by "exp" while the resulting
6351 * list of divs is given by "div".
6353 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6354 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6356 int i, j;
6357 int n_div;
6359 bset = isl_basic_set_cow(bset);
6360 if (!bset || !div)
6361 goto error;
6363 if (div->n_row < bset->n_div)
6364 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6365 "not an expansion", goto error);
6367 bset = isl_basic_map_extend_dim(bset, isl_dim_copy(bset->dim),
6368 div->n_row - bset->n_div, 0,
6369 2 * (div->n_row - bset->n_div));
6371 n_div = bset->n_div;
6372 for (i = n_div; i < div->n_row; ++i)
6373 if (isl_basic_set_alloc_div(bset) < 0)
6374 goto error;
6376 j = n_div - 1;
6377 for (i = div->n_row - 1; i >= 0; --i) {
6378 if (j >= 0 && exp[j] == i) {
6379 if (i != j)
6380 isl_basic_map_swap_div(bset, i, j);
6381 j--;
6382 } else {
6383 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6384 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6385 goto error;
6389 isl_mat_free(div);
6390 return bset;
6391 error:
6392 isl_basic_set_free(bset);
6393 isl_mat_free(div);
6394 return NULL;
6397 /* Look for a div in dst that corresponds to the div "div" in src.
6398 * The divs before "div" in src and dst are assumed to be the same.
6400 * Returns -1 if no corresponding div was found and the position
6401 * of the corresponding div in dst otherwise.
6403 static int find_div(struct isl_basic_map *dst,
6404 struct isl_basic_map *src, unsigned div)
6406 int i;
6408 unsigned total = isl_dim_total(src->dim);
6410 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6411 for (i = div; i < dst->n_div; ++i)
6412 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6413 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6414 dst->n_div - div) == -1)
6415 return i;
6416 return -1;
6419 struct isl_basic_map *isl_basic_map_align_divs(
6420 struct isl_basic_map *dst, struct isl_basic_map *src)
6422 int i;
6423 unsigned total = isl_dim_total(src->dim);
6425 if (!dst || !src)
6426 goto error;
6428 if (src->n_div == 0)
6429 return dst;
6431 for (i = 0; i < src->n_div; ++i)
6432 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6434 src = isl_basic_map_order_divs(src);
6435 dst = isl_basic_map_cow(dst);
6436 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6437 src->n_div, 0, 2 * src->n_div);
6438 if (!dst)
6439 return NULL;
6440 for (i = 0; i < src->n_div; ++i) {
6441 int j = find_div(dst, src, i);
6442 if (j < 0) {
6443 j = isl_basic_map_alloc_div(dst);
6444 if (j < 0)
6445 goto error;
6446 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6447 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6448 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6449 goto error;
6451 if (j != i)
6452 isl_basic_map_swap_div(dst, i, j);
6454 return dst;
6455 error:
6456 isl_basic_map_free(dst);
6457 return NULL;
6460 struct isl_basic_set *isl_basic_set_align_divs(
6461 struct isl_basic_set *dst, struct isl_basic_set *src)
6463 return (struct isl_basic_set *)isl_basic_map_align_divs(
6464 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6467 struct isl_map *isl_map_align_divs(struct isl_map *map)
6469 int i;
6471 if (!map)
6472 return NULL;
6473 if (map->n == 0)
6474 return map;
6475 map = isl_map_compute_divs(map);
6476 map = isl_map_cow(map);
6477 if (!map)
6478 return NULL;
6480 for (i = 1; i < map->n; ++i)
6481 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6482 for (i = 1; i < map->n; ++i)
6483 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6485 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6486 return map;
6489 struct isl_set *isl_set_align_divs(struct isl_set *set)
6491 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6494 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6496 if (!set || !map)
6497 goto error;
6498 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6499 map = isl_map_intersect_domain(map, set);
6500 set = isl_map_range(map);
6501 return set;
6502 error:
6503 isl_set_free(set);
6504 isl_map_free(map);
6505 return NULL;
6508 /* There is no need to cow as removing empty parts doesn't change
6509 * the meaning of the set.
6511 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6513 int i;
6515 if (!map)
6516 return NULL;
6518 for (i = map->n-1; i >= 0; --i) {
6519 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6520 continue;
6521 isl_basic_map_free(map->p[i]);
6522 if (i != map->n-1) {
6523 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6524 map->p[i] = map->p[map->n-1];
6526 map->n--;
6529 return map;
6532 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6534 return (struct isl_set *)
6535 isl_map_remove_empty_parts((struct isl_map *)set);
6538 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6540 struct isl_basic_map *bmap;
6541 if (!map || map->n == 0)
6542 return NULL;
6543 bmap = map->p[map->n-1];
6544 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6545 return isl_basic_map_copy(bmap);
6548 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6550 return (struct isl_basic_set *)
6551 isl_map_copy_basic_map((struct isl_map *)set);
6554 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6555 __isl_keep isl_basic_map *bmap)
6557 int i;
6559 if (!map || !bmap)
6560 goto error;
6561 for (i = map->n-1; i >= 0; --i) {
6562 if (map->p[i] != bmap)
6563 continue;
6564 map = isl_map_cow(map);
6565 if (!map)
6566 goto error;
6567 isl_basic_map_free(map->p[i]);
6568 if (i != map->n-1) {
6569 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6570 map->p[i] = map->p[map->n-1];
6572 map->n--;
6573 return map;
6575 return map;
6576 error:
6577 isl_map_free(map);
6578 return NULL;
6581 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6582 struct isl_basic_set *bset)
6584 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6585 (struct isl_basic_map *)bset);
6588 /* Given two basic sets bset1 and bset2, compute the maximal difference
6589 * between the values of dimension pos in bset1 and those in bset2
6590 * for any common value of the parameters and dimensions preceding pos.
6592 static enum isl_lp_result basic_set_maximal_difference_at(
6593 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6594 int pos, isl_int *opt)
6596 struct isl_dim *dims;
6597 struct isl_basic_map *bmap1 = NULL;
6598 struct isl_basic_map *bmap2 = NULL;
6599 struct isl_ctx *ctx;
6600 struct isl_vec *obj;
6601 unsigned total;
6602 unsigned nparam;
6603 unsigned dim1, dim2;
6604 enum isl_lp_result res;
6606 if (!bset1 || !bset2)
6607 return isl_lp_error;
6609 nparam = isl_basic_set_n_param(bset1);
6610 dim1 = isl_basic_set_n_dim(bset1);
6611 dim2 = isl_basic_set_n_dim(bset2);
6612 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6613 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6614 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6615 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6616 if (!bmap1 || !bmap2)
6617 goto error;
6618 bmap1 = isl_basic_map_cow(bmap1);
6619 bmap1 = isl_basic_map_extend(bmap1, nparam,
6620 pos, (dim1 - pos) + (dim2 - pos),
6621 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6622 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6623 if (!bmap1)
6624 goto error;
6625 total = isl_basic_map_total_dim(bmap1);
6626 ctx = bmap1->ctx;
6627 obj = isl_vec_alloc(ctx, 1 + total);
6628 isl_seq_clr(obj->block.data, 1 + total);
6629 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6630 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6631 if (!obj)
6632 goto error;
6633 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6634 opt, NULL, NULL);
6635 isl_basic_map_free(bmap1);
6636 isl_vec_free(obj);
6637 return res;
6638 error:
6639 isl_basic_map_free(bmap1);
6640 isl_basic_map_free(bmap2);
6641 return isl_lp_error;
6644 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6645 * for any common value of the parameters and dimensions preceding pos
6646 * in both basic sets, the values of dimension pos in bset1 are
6647 * smaller or larger than those in bset2.
6649 * Returns
6650 * 1 if bset1 follows bset2
6651 * -1 if bset1 precedes bset2
6652 * 0 if bset1 and bset2 are incomparable
6653 * -2 if some error occurred.
6655 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6656 struct isl_basic_set *bset2, int pos)
6658 isl_int opt;
6659 enum isl_lp_result res;
6660 int cmp;
6662 isl_int_init(opt);
6664 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6666 if (res == isl_lp_empty)
6667 cmp = 0;
6668 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6669 res == isl_lp_unbounded)
6670 cmp = 1;
6671 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6672 cmp = -1;
6673 else
6674 cmp = -2;
6676 isl_int_clear(opt);
6677 return cmp;
6680 /* Given two basic sets bset1 and bset2, check whether
6681 * for any common value of the parameters and dimensions preceding pos
6682 * there is a value of dimension pos in bset1 that is larger
6683 * than a value of the same dimension in bset2.
6685 * Return
6686 * 1 if there exists such a pair
6687 * 0 if there is no such pair, but there is a pair of equal values
6688 * -1 otherwise
6689 * -2 if some error occurred.
6691 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6692 __isl_keep isl_basic_set *bset2, int pos)
6694 isl_int opt;
6695 enum isl_lp_result res;
6696 int cmp;
6698 isl_int_init(opt);
6700 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6702 if (res == isl_lp_empty)
6703 cmp = -1;
6704 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6705 res == isl_lp_unbounded)
6706 cmp = 1;
6707 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6708 cmp = -1;
6709 else if (res == isl_lp_ok)
6710 cmp = 0;
6711 else
6712 cmp = -2;
6714 isl_int_clear(opt);
6715 return cmp;
6718 /* Given two sets set1 and set2, check whether
6719 * for any common value of the parameters and dimensions preceding pos
6720 * there is a value of dimension pos in set1 that is larger
6721 * than a value of the same dimension in set2.
6723 * Return
6724 * 1 if there exists such a pair
6725 * 0 if there is no such pair, but there is a pair of equal values
6726 * -1 otherwise
6727 * -2 if some error occurred.
6729 int isl_set_follows_at(__isl_keep isl_set *set1,
6730 __isl_keep isl_set *set2, int pos)
6732 int i, j;
6733 int follows = -1;
6735 if (!set1 || !set2)
6736 return -2;
6738 for (i = 0; i < set1->n; ++i)
6739 for (j = 0; j < set2->n; ++j) {
6740 int f;
6741 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6742 if (f == 1 || f == -2)
6743 return f;
6744 if (f > follows)
6745 follows = f;
6748 return follows;
6751 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
6752 unsigned pos, isl_int *val)
6754 int i;
6755 int d;
6756 unsigned total;
6758 if (!bmap)
6759 return -1;
6760 total = isl_basic_map_total_dim(bmap);
6761 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6762 for (; d+1 > pos; --d)
6763 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6764 break;
6765 if (d != pos)
6766 continue;
6767 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6768 return 0;
6769 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6770 return 0;
6771 if (!isl_int_is_one(bmap->eq[i][1+d]))
6772 return 0;
6773 if (val)
6774 isl_int_neg(*val, bmap->eq[i][0]);
6775 return 1;
6777 return 0;
6780 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
6781 unsigned pos, isl_int *val)
6783 int i;
6784 isl_int v;
6785 isl_int tmp;
6786 int fixed;
6788 if (!map)
6789 return -1;
6790 if (map->n == 0)
6791 return 0;
6792 if (map->n == 1)
6793 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
6794 isl_int_init(v);
6795 isl_int_init(tmp);
6796 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
6797 for (i = 1; fixed == 1 && i < map->n; ++i) {
6798 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
6799 if (fixed == 1 && isl_int_ne(tmp, v))
6800 fixed = 0;
6802 if (val)
6803 isl_int_set(*val, v);
6804 isl_int_clear(tmp);
6805 isl_int_clear(v);
6806 return fixed;
6809 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
6810 unsigned pos, isl_int *val)
6812 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
6813 pos, val);
6816 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
6817 isl_int *val)
6819 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
6822 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
6823 enum isl_dim_type type, unsigned pos, isl_int *val)
6825 if (pos >= isl_basic_map_dim(bmap, type))
6826 return -1;
6827 return isl_basic_map_plain_has_fixed_var(bmap,
6828 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6831 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
6832 enum isl_dim_type type, unsigned pos, isl_int *val)
6834 if (pos >= isl_map_dim(map, type))
6835 return -1;
6836 return isl_map_plain_has_fixed_var(map,
6837 map_offset(map, type) - 1 + pos, val);
6840 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
6841 enum isl_dim_type type, unsigned pos, isl_int *val)
6843 return isl_map_plain_is_fixed(map, type, pos, val);
6846 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6847 * then return this fixed value in *val.
6849 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
6850 unsigned dim, isl_int *val)
6852 return isl_basic_set_plain_has_fixed_var(bset,
6853 isl_basic_set_n_param(bset) + dim, val);
6856 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6857 * then return this fixed value in *val.
6859 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
6860 unsigned dim, isl_int *val)
6862 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6865 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
6866 unsigned dim, isl_int *val)
6868 return isl_set_plain_dim_is_fixed(set, dim, val);
6871 /* Check if input variable in has fixed value and if so and if val is not NULL,
6872 * then return this fixed value in *val.
6874 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
6875 unsigned in, isl_int *val)
6877 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
6880 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6881 * and if val is not NULL, then return this lower bound in *val.
6883 int isl_basic_set_plain_dim_has_fixed_lower_bound(
6884 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
6886 int i, i_eq = -1, i_ineq = -1;
6887 isl_int *c;
6888 unsigned total;
6889 unsigned nparam;
6891 if (!bset)
6892 return -1;
6893 total = isl_basic_set_total_dim(bset);
6894 nparam = isl_basic_set_n_param(bset);
6895 for (i = 0; i < bset->n_eq; ++i) {
6896 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6897 continue;
6898 if (i_eq != -1)
6899 return 0;
6900 i_eq = i;
6902 for (i = 0; i < bset->n_ineq; ++i) {
6903 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6904 continue;
6905 if (i_eq != -1 || i_ineq != -1)
6906 return 0;
6907 i_ineq = i;
6909 if (i_eq == -1 && i_ineq == -1)
6910 return 0;
6911 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6912 /* The coefficient should always be one due to normalization. */
6913 if (!isl_int_is_one(c[1+nparam+dim]))
6914 return 0;
6915 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6916 return 0;
6917 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6918 total - nparam - dim - 1) != -1)
6919 return 0;
6920 if (val)
6921 isl_int_neg(*val, c[0]);
6922 return 1;
6925 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
6926 unsigned dim, isl_int *val)
6928 int i;
6929 isl_int v;
6930 isl_int tmp;
6931 int fixed;
6933 if (!set)
6934 return -1;
6935 if (set->n == 0)
6936 return 0;
6937 if (set->n == 1)
6938 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
6939 dim, val);
6940 isl_int_init(v);
6941 isl_int_init(tmp);
6942 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
6943 dim, &v);
6944 for (i = 1; fixed == 1 && i < set->n; ++i) {
6945 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
6946 dim, &tmp);
6947 if (fixed == 1 && isl_int_ne(tmp, v))
6948 fixed = 0;
6950 if (val)
6951 isl_int_set(*val, v);
6952 isl_int_clear(tmp);
6953 isl_int_clear(v);
6954 return fixed;
6957 struct constraint {
6958 unsigned size;
6959 isl_int *c;
6962 /* uset_gist depends on constraints without existentially quantified
6963 * variables sorting first.
6965 static int qsort_constraint_cmp(const void *p1, const void *p2)
6967 const struct constraint *c1 = (const struct constraint *)p1;
6968 const struct constraint *c2 = (const struct constraint *)p2;
6969 int l1, l2;
6970 unsigned size = isl_min(c1->size, c2->size);
6972 l1 = isl_seq_last_non_zero(c1->c, size);
6973 l2 = isl_seq_last_non_zero(c2->c, size);
6975 if (l1 != l2)
6976 return l1 - l2;
6978 return isl_seq_cmp(c1->c, c2->c, size);
6981 static struct isl_basic_map *isl_basic_map_sort_constraints(
6982 struct isl_basic_map *bmap)
6984 int i;
6985 struct constraint *c;
6986 unsigned total;
6988 if (!bmap)
6989 return NULL;
6990 total = isl_basic_map_total_dim(bmap);
6991 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6992 if (!c)
6993 goto error;
6994 for (i = 0; i < bmap->n_ineq; ++i) {
6995 c[i].size = total;
6996 c[i].c = bmap->ineq[i];
6998 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6999 for (i = 0; i < bmap->n_ineq; ++i)
7000 bmap->ineq[i] = c[i].c;
7001 free(c);
7002 return bmap;
7003 error:
7004 isl_basic_map_free(bmap);
7005 return NULL;
7008 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7009 __isl_take isl_basic_set *bset)
7011 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7012 (struct isl_basic_map *)bset);
7015 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7017 if (!bmap)
7018 return NULL;
7019 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7020 return bmap;
7021 bmap = isl_basic_map_remove_redundancies(bmap);
7022 bmap = isl_basic_map_sort_constraints(bmap);
7023 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7024 return bmap;
7027 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7029 return (struct isl_basic_set *)isl_basic_map_normalize(
7030 (struct isl_basic_map *)bset);
7033 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7034 const __isl_keep isl_basic_map *bmap2)
7036 int i, cmp;
7037 unsigned total;
7039 if (bmap1 == bmap2)
7040 return 0;
7041 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7042 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7043 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7044 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7045 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7046 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7047 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7048 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7049 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7050 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7051 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7052 return 0;
7053 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7054 return 1;
7055 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7056 return -1;
7057 if (bmap1->n_eq != bmap2->n_eq)
7058 return bmap1->n_eq - bmap2->n_eq;
7059 if (bmap1->n_ineq != bmap2->n_ineq)
7060 return bmap1->n_ineq - bmap2->n_ineq;
7061 if (bmap1->n_div != bmap2->n_div)
7062 return bmap1->n_div - bmap2->n_div;
7063 total = isl_basic_map_total_dim(bmap1);
7064 for (i = 0; i < bmap1->n_eq; ++i) {
7065 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7066 if (cmp)
7067 return cmp;
7069 for (i = 0; i < bmap1->n_ineq; ++i) {
7070 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7071 if (cmp)
7072 return cmp;
7074 for (i = 0; i < bmap1->n_div; ++i) {
7075 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7076 if (cmp)
7077 return cmp;
7079 return 0;
7082 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7083 __isl_keep isl_basic_map *bmap2)
7085 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7088 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7089 __isl_keep isl_basic_set *bset2)
7091 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7092 (isl_basic_map *)bset2);
7095 static int qsort_bmap_cmp(const void *p1, const void *p2)
7097 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7098 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7100 return isl_basic_map_plain_cmp(bmap1, bmap2);
7103 /* We normalize in place, but if anything goes wrong we need
7104 * to return NULL, so we need to make sure we don't change the
7105 * meaning of any possible other copies of map.
7107 struct isl_map *isl_map_normalize(struct isl_map *map)
7109 int i, j;
7110 struct isl_basic_map *bmap;
7112 if (!map)
7113 return NULL;
7114 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7115 return map;
7116 for (i = 0; i < map->n; ++i) {
7117 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7118 if (!bmap)
7119 goto error;
7120 isl_basic_map_free(map->p[i]);
7121 map->p[i] = bmap;
7123 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7124 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7125 map = isl_map_remove_empty_parts(map);
7126 if (!map)
7127 return NULL;
7128 for (i = map->n - 1; i >= 1; --i) {
7129 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7130 continue;
7131 isl_basic_map_free(map->p[i-1]);
7132 for (j = i; j < map->n; ++j)
7133 map->p[j-1] = map->p[j];
7134 map->n--;
7136 return map;
7137 error:
7138 isl_map_free(map);
7139 return NULL;
7143 struct isl_set *isl_set_normalize(struct isl_set *set)
7145 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7148 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7150 int i;
7151 int equal;
7153 if (!map1 || !map2)
7154 return -1;
7156 if (map1 == map2)
7157 return 1;
7158 if (!isl_dim_equal(map1->dim, map2->dim))
7159 return 0;
7161 map1 = isl_map_copy(map1);
7162 map2 = isl_map_copy(map2);
7163 map1 = isl_map_normalize(map1);
7164 map2 = isl_map_normalize(map2);
7165 if (!map1 || !map2)
7166 goto error;
7167 equal = map1->n == map2->n;
7168 for (i = 0; equal && i < map1->n; ++i) {
7169 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7170 if (equal < 0)
7171 goto error;
7173 isl_map_free(map1);
7174 isl_map_free(map2);
7175 return equal;
7176 error:
7177 isl_map_free(map1);
7178 isl_map_free(map2);
7179 return -1;
7182 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7184 return isl_map_plain_is_equal(map1, map2);
7187 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7189 return isl_map_plain_is_equal((struct isl_map *)set1,
7190 (struct isl_map *)set2);
7193 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7195 return isl_set_plain_is_equal(set1, set2);
7198 /* Return an interval that ranges from min to max (inclusive)
7200 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7201 isl_int min, isl_int max)
7203 int k;
7204 struct isl_basic_set *bset = NULL;
7206 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7207 if (!bset)
7208 goto error;
7210 k = isl_basic_set_alloc_inequality(bset);
7211 if (k < 0)
7212 goto error;
7213 isl_int_set_si(bset->ineq[k][1], 1);
7214 isl_int_neg(bset->ineq[k][0], min);
7216 k = isl_basic_set_alloc_inequality(bset);
7217 if (k < 0)
7218 goto error;
7219 isl_int_set_si(bset->ineq[k][1], -1);
7220 isl_int_set(bset->ineq[k][0], max);
7222 return bset;
7223 error:
7224 isl_basic_set_free(bset);
7225 return NULL;
7228 /* Return the Cartesian product of the basic sets in list (in the given order).
7230 __isl_give isl_basic_set *isl_basic_set_list_product(
7231 __isl_take struct isl_basic_set_list *list)
7233 int i;
7234 unsigned dim;
7235 unsigned nparam;
7236 unsigned extra;
7237 unsigned n_eq;
7238 unsigned n_ineq;
7239 struct isl_basic_set *product = NULL;
7241 if (!list)
7242 goto error;
7243 isl_assert(list->ctx, list->n > 0, goto error);
7244 isl_assert(list->ctx, list->p[0], goto error);
7245 nparam = isl_basic_set_n_param(list->p[0]);
7246 dim = isl_basic_set_n_dim(list->p[0]);
7247 extra = list->p[0]->n_div;
7248 n_eq = list->p[0]->n_eq;
7249 n_ineq = list->p[0]->n_ineq;
7250 for (i = 1; i < list->n; ++i) {
7251 isl_assert(list->ctx, list->p[i], goto error);
7252 isl_assert(list->ctx,
7253 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7254 dim += isl_basic_set_n_dim(list->p[i]);
7255 extra += list->p[i]->n_div;
7256 n_eq += list->p[i]->n_eq;
7257 n_ineq += list->p[i]->n_ineq;
7259 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7260 n_eq, n_ineq);
7261 if (!product)
7262 goto error;
7263 dim = 0;
7264 for (i = 0; i < list->n; ++i) {
7265 isl_basic_set_add_constraints(product,
7266 isl_basic_set_copy(list->p[i]), dim);
7267 dim += isl_basic_set_n_dim(list->p[i]);
7269 isl_basic_set_list_free(list);
7270 return product;
7271 error:
7272 isl_basic_set_free(product);
7273 isl_basic_set_list_free(list);
7274 return NULL;
7277 struct isl_basic_map *isl_basic_map_product(
7278 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7280 struct isl_dim *dim_result = NULL;
7281 struct isl_basic_map *bmap;
7282 unsigned in1, in2, out1, out2, nparam, total, pos;
7283 struct isl_dim_map *dim_map1, *dim_map2;
7285 if (!bmap1 || !bmap2)
7286 goto error;
7288 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7289 bmap2->dim, isl_dim_param), goto error);
7290 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7291 isl_dim_copy(bmap2->dim));
7293 in1 = isl_basic_map_n_in(bmap1);
7294 in2 = isl_basic_map_n_in(bmap2);
7295 out1 = isl_basic_map_n_out(bmap1);
7296 out2 = isl_basic_map_n_out(bmap2);
7297 nparam = isl_basic_map_n_param(bmap1);
7299 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7300 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7301 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7302 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7303 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7304 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7305 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7306 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7307 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7308 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7309 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7311 bmap = isl_basic_map_alloc_dim(dim_result,
7312 bmap1->n_div + bmap2->n_div,
7313 bmap1->n_eq + bmap2->n_eq,
7314 bmap1->n_ineq + bmap2->n_ineq);
7315 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7316 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7317 bmap = isl_basic_map_simplify(bmap);
7318 return isl_basic_map_finalize(bmap);
7319 error:
7320 isl_basic_map_free(bmap1);
7321 isl_basic_map_free(bmap2);
7322 return NULL;
7325 __isl_give isl_basic_map *isl_basic_map_flat_product(
7326 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7328 isl_basic_map *prod;
7330 prod = isl_basic_map_product(bmap1, bmap2);
7331 prod = isl_basic_map_flatten(prod);
7332 return prod;
7335 __isl_give isl_basic_set *isl_basic_set_flat_product(
7336 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7338 return isl_basic_map_flat_product(bset1, bset2);
7341 __isl_give isl_basic_map *isl_basic_map_range_product(
7342 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7344 isl_dim *dim_result = NULL;
7345 isl_basic_map *bmap;
7346 unsigned in, out1, out2, nparam, total, pos;
7347 struct isl_dim_map *dim_map1, *dim_map2;
7349 if (!bmap1 || !bmap2)
7350 goto error;
7352 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7353 isl_dim_copy(bmap2->dim));
7355 in = isl_basic_map_dim(bmap1, isl_dim_in);
7356 out1 = isl_basic_map_n_out(bmap1);
7357 out2 = isl_basic_map_n_out(bmap2);
7358 nparam = isl_basic_map_n_param(bmap1);
7360 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7361 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7362 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7363 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7364 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7365 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7366 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7367 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7368 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7369 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7370 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7372 bmap = isl_basic_map_alloc_dim(dim_result,
7373 bmap1->n_div + bmap2->n_div,
7374 bmap1->n_eq + bmap2->n_eq,
7375 bmap1->n_ineq + bmap2->n_ineq);
7376 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7377 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7378 bmap = isl_basic_map_simplify(bmap);
7379 return isl_basic_map_finalize(bmap);
7380 error:
7381 isl_basic_map_free(bmap1);
7382 isl_basic_map_free(bmap2);
7383 return NULL;
7386 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7387 __isl_take isl_map *map2,
7388 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7389 __isl_take isl_dim *right),
7390 __isl_give isl_basic_map *(*basic_map_product)(
7391 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7393 unsigned flags = 0;
7394 struct isl_map *result;
7395 int i, j;
7397 if (!map1 || !map2)
7398 goto error;
7400 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7401 map2->dim, isl_dim_param), goto error);
7403 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7404 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7405 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7407 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7408 isl_dim_copy(map2->dim)),
7409 map1->n * map2->n, flags);
7410 if (!result)
7411 goto error;
7412 for (i = 0; i < map1->n; ++i)
7413 for (j = 0; j < map2->n; ++j) {
7414 struct isl_basic_map *part;
7415 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7416 isl_basic_map_copy(map2->p[j]));
7417 if (isl_basic_map_is_empty(part))
7418 isl_basic_map_free(part);
7419 else
7420 result = isl_map_add_basic_map(result, part);
7421 if (!result)
7422 goto error;
7424 isl_map_free(map1);
7425 isl_map_free(map2);
7426 return result;
7427 error:
7428 isl_map_free(map1);
7429 isl_map_free(map2);
7430 return NULL;
7433 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7435 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7437 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7440 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7442 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7443 __isl_take isl_map *map2)
7445 isl_map *prod;
7447 prod = isl_map_product(map1, map2);
7448 prod = isl_map_flatten(prod);
7449 return prod;
7452 /* Given two set A and B, construct its Cartesian product A x B.
7454 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7456 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7457 (struct isl_map *)set2);
7460 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7461 __isl_take isl_set *set2)
7463 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7466 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7468 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7469 __isl_take isl_map *map2)
7471 return map_product(map1, map2, &isl_dim_range_product,
7472 &isl_basic_map_range_product);
7475 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7477 int i;
7478 uint32_t hash = isl_hash_init();
7479 unsigned total;
7481 if (!bmap)
7482 return 0;
7483 bmap = isl_basic_map_copy(bmap);
7484 bmap = isl_basic_map_normalize(bmap);
7485 if (!bmap)
7486 return 0;
7487 total = isl_basic_map_total_dim(bmap);
7488 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7489 for (i = 0; i < bmap->n_eq; ++i) {
7490 uint32_t c_hash;
7491 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7492 isl_hash_hash(hash, c_hash);
7494 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7495 for (i = 0; i < bmap->n_ineq; ++i) {
7496 uint32_t c_hash;
7497 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7498 isl_hash_hash(hash, c_hash);
7500 isl_hash_byte(hash, bmap->n_div & 0xFF);
7501 for (i = 0; i < bmap->n_div; ++i) {
7502 uint32_t c_hash;
7503 if (isl_int_is_zero(bmap->div[i][0]))
7504 continue;
7505 isl_hash_byte(hash, i & 0xFF);
7506 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7507 isl_hash_hash(hash, c_hash);
7509 isl_basic_map_free(bmap);
7510 return hash;
7513 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7515 return isl_basic_map_get_hash((isl_basic_map *)bset);
7518 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7520 int i;
7521 uint32_t hash;
7523 if (!map)
7524 return 0;
7525 map = isl_map_copy(map);
7526 map = isl_map_normalize(map);
7527 if (!map)
7528 return 0;
7530 hash = isl_hash_init();
7531 for (i = 0; i < map->n; ++i) {
7532 uint32_t bmap_hash;
7533 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7534 isl_hash_hash(hash, bmap_hash);
7537 isl_map_free(map);
7539 return hash;
7542 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7544 return isl_map_get_hash((isl_map *)set);
7547 /* Check if the value for dimension dim is completely determined
7548 * by the values of the other parameters and variables.
7549 * That is, check if dimension dim is involved in an equality.
7551 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7553 int i;
7554 unsigned nparam;
7556 if (!bset)
7557 return -1;
7558 nparam = isl_basic_set_n_param(bset);
7559 for (i = 0; i < bset->n_eq; ++i)
7560 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7561 return 1;
7562 return 0;
7565 /* Check if the value for dimension dim is completely determined
7566 * by the values of the other parameters and variables.
7567 * That is, check if dimension dim is involved in an equality
7568 * for each of the subsets.
7570 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7572 int i;
7574 if (!set)
7575 return -1;
7576 for (i = 0; i < set->n; ++i) {
7577 int unique;
7578 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7579 if (unique != 1)
7580 return unique;
7582 return 1;
7585 int isl_set_n_basic_set(__isl_keep isl_set *set)
7587 return set ? set->n : 0;
7590 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7591 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7593 int i;
7595 if (!map)
7596 return -1;
7598 for (i = 0; i < map->n; ++i)
7599 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7600 return -1;
7602 return 0;
7605 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7606 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7608 int i;
7610 if (!set)
7611 return -1;
7613 for (i = 0; i < set->n; ++i)
7614 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7615 return -1;
7617 return 0;
7620 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7622 struct isl_dim *dim;
7624 if (!bset)
7625 return NULL;
7627 bset = isl_basic_set_cow(bset);
7628 if (!bset)
7629 return NULL;
7631 dim = isl_basic_set_get_dim(bset);
7632 dim = isl_dim_lift(dim, bset->n_div);
7633 if (!dim)
7634 goto error;
7635 isl_dim_free(bset->dim);
7636 bset->dim = dim;
7637 bset->extra -= bset->n_div;
7638 bset->n_div = 0;
7640 bset = isl_basic_set_finalize(bset);
7642 return bset;
7643 error:
7644 isl_basic_set_free(bset);
7645 return NULL;
7648 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7650 int i;
7651 struct isl_dim *dim;
7652 unsigned n_div;
7654 set = isl_set_align_divs(set);
7656 if (!set)
7657 return NULL;
7659 set = isl_set_cow(set);
7660 if (!set)
7661 return NULL;
7663 n_div = set->p[0]->n_div;
7664 dim = isl_set_get_dim(set);
7665 dim = isl_dim_lift(dim, n_div);
7666 if (!dim)
7667 goto error;
7668 isl_dim_free(set->dim);
7669 set->dim = dim;
7671 for (i = 0; i < set->n; ++i) {
7672 set->p[i] = isl_basic_set_lift(set->p[i]);
7673 if (!set->p[i])
7674 goto error;
7677 return set;
7678 error:
7679 isl_set_free(set);
7680 return NULL;
7683 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7685 struct isl_dim *dim;
7686 struct isl_basic_map *bmap;
7687 unsigned n_set;
7688 unsigned n_div;
7689 unsigned n_param;
7690 unsigned total;
7691 int i, k, l;
7693 set = isl_set_align_divs(set);
7695 if (!set)
7696 return NULL;
7698 dim = isl_set_get_dim(set);
7699 if (set->n == 0 || set->p[0]->n_div == 0) {
7700 isl_set_free(set);
7701 return isl_map_identity(isl_dim_map_from_set(dim));
7704 n_div = set->p[0]->n_div;
7705 dim = isl_dim_map_from_set(dim);
7706 n_param = isl_dim_size(dim, isl_dim_param);
7707 n_set = isl_dim_size(dim, isl_dim_in);
7708 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7709 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7710 for (i = 0; i < n_set; ++i)
7711 bmap = var_equal(bmap, i);
7713 total = n_param + n_set + n_set + n_div;
7714 for (i = 0; i < n_div; ++i) {
7715 k = isl_basic_map_alloc_inequality(bmap);
7716 if (k < 0)
7717 goto error;
7718 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7719 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7720 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7721 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7722 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7723 set->p[0]->div[i][0]);
7725 l = isl_basic_map_alloc_inequality(bmap);
7726 if (l < 0)
7727 goto error;
7728 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7729 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7730 set->p[0]->div[i][0]);
7731 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7734 isl_set_free(set);
7735 bmap = isl_basic_map_simplify(bmap);
7736 bmap = isl_basic_map_finalize(bmap);
7737 return isl_map_from_basic_map(bmap);
7738 error:
7739 isl_set_free(set);
7740 isl_basic_map_free(bmap);
7741 return NULL;
7744 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7746 unsigned dim;
7747 int size = 0;
7749 if (!bset)
7750 return -1;
7752 dim = isl_basic_set_total_dim(bset);
7753 size += bset->n_eq * (1 + dim);
7754 size += bset->n_ineq * (1 + dim);
7755 size += bset->n_div * (2 + dim);
7757 return size;
7760 int isl_set_size(__isl_keep isl_set *set)
7762 int i;
7763 int size = 0;
7765 if (!set)
7766 return -1;
7768 for (i = 0; i < set->n; ++i)
7769 size += isl_basic_set_size(set->p[i]);
7771 return size;
7774 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7775 enum isl_dim_type type, unsigned pos)
7777 int i;
7778 int lower, upper;
7780 if (!bmap)
7781 return -1;
7783 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7785 pos += isl_basic_map_offset(bmap, type);
7787 for (i = 0; i < bmap->n_eq; ++i)
7788 if (!isl_int_is_zero(bmap->eq[i][pos]))
7789 return 1;
7791 lower = upper = 0;
7792 for (i = 0; i < bmap->n_ineq; ++i) {
7793 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7794 if (sgn > 0)
7795 lower = 1;
7796 if (sgn < 0)
7797 upper = 1;
7800 return lower && upper;
7803 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7804 enum isl_dim_type type, unsigned pos)
7806 int i;
7808 if (!map)
7809 return -1;
7811 for (i = 0; i < map->n; ++i) {
7812 int bounded;
7813 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7814 if (bounded < 0 || !bounded)
7815 return bounded;
7818 return 1;
7821 /* Return 1 if the specified dim is involved in both an upper bound
7822 * and a lower bound.
7824 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7825 enum isl_dim_type type, unsigned pos)
7827 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7830 /* For each of the "n" variables starting at "first", determine
7831 * the sign of the variable and put the results in the first "n"
7832 * elements of the array "signs".
7833 * Sign
7834 * 1 means that the variable is non-negative
7835 * -1 means that the variable is non-positive
7836 * 0 means the variable attains both positive and negative values.
7838 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7839 unsigned first, unsigned n, int *signs)
7841 isl_vec *bound = NULL;
7842 struct isl_tab *tab = NULL;
7843 struct isl_tab_undo *snap;
7844 int i;
7846 if (!bset || !signs)
7847 return -1;
7849 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7850 tab = isl_tab_from_basic_set(bset);
7851 if (!bound || !tab)
7852 goto error;
7854 isl_seq_clr(bound->el, bound->size);
7855 isl_int_set_si(bound->el[0], -1);
7857 snap = isl_tab_snap(tab);
7858 for (i = 0; i < n; ++i) {
7859 int empty;
7861 isl_int_set_si(bound->el[1 + first + i], -1);
7862 if (isl_tab_add_ineq(tab, bound->el) < 0)
7863 goto error;
7864 empty = tab->empty;
7865 isl_int_set_si(bound->el[1 + first + i], 0);
7866 if (isl_tab_rollback(tab, snap) < 0)
7867 goto error;
7869 if (empty) {
7870 signs[i] = 1;
7871 continue;
7874 isl_int_set_si(bound->el[1 + first + i], 1);
7875 if (isl_tab_add_ineq(tab, bound->el) < 0)
7876 goto error;
7877 empty = tab->empty;
7878 isl_int_set_si(bound->el[1 + first + i], 0);
7879 if (isl_tab_rollback(tab, snap) < 0)
7880 goto error;
7882 signs[i] = empty ? -1 : 0;
7885 isl_tab_free(tab);
7886 isl_vec_free(bound);
7887 return 0;
7888 error:
7889 isl_tab_free(tab);
7890 isl_vec_free(bound);
7891 return -1;
7894 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7895 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7897 if (!bset || !signs)
7898 return -1;
7899 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7900 return -1);
7902 first += pos(bset->dim, type) - 1;
7903 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7906 /* Check if the given basic map is obviously single-valued.
7907 * In particular, for each output dimension, check that there is
7908 * an equality that defines the output dimension in terms of
7909 * earlier dimensions.
7911 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
7913 int i, j;
7914 unsigned total;
7915 unsigned n_out;
7916 unsigned o_out;
7918 if (!bmap)
7919 return -1;
7921 total = 1 + isl_basic_map_total_dim(bmap);
7922 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7923 o_out = isl_basic_map_offset(bmap, isl_dim_out);
7925 for (i = 0; i < n_out; ++i) {
7926 for (j = 0; j < bmap->n_eq; ++j) {
7927 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
7928 continue;
7929 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
7930 total - (o_out + i + 1)) == -1)
7931 break;
7933 if (j >= bmap->n_eq)
7934 return 0;
7937 return 1;
7940 /* Check if the given map is obviously single-valued.
7942 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
7944 int sv;
7946 if (!map)
7947 return -1;
7948 if (map->n == 0)
7949 return 1;
7950 if (map->n >= 2)
7951 return 0;
7953 return isl_basic_map_plain_is_single_valued(map->p[0]);
7956 /* Check if the given map is single-valued.
7957 * We simply compute
7959 * M \circ M^-1
7961 * and check if the result is a subset of the identity mapping.
7963 int isl_map_is_single_valued(__isl_keep isl_map *map)
7965 isl_dim *dim;
7966 isl_map *test;
7967 isl_map *id;
7968 int sv;
7970 sv = isl_map_plain_is_single_valued(map);
7971 if (sv < 0 || sv)
7972 return sv;
7974 test = isl_map_reverse(isl_map_copy(map));
7975 test = isl_map_apply_range(test, isl_map_copy(map));
7977 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
7978 id = isl_map_identity(dim);
7980 sv = isl_map_is_subset(test, id);
7982 isl_map_free(test);
7983 isl_map_free(id);
7985 return sv;
7988 int isl_map_is_injective(__isl_keep isl_map *map)
7990 int in;
7992 map = isl_map_copy(map);
7993 map = isl_map_reverse(map);
7994 in = isl_map_is_single_valued(map);
7995 isl_map_free(map);
7997 return in;
8000 /* Check if the given map is obviously injective.
8002 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8004 int in;
8006 map = isl_map_copy(map);
8007 map = isl_map_reverse(map);
8008 in = isl_map_plain_is_single_valued(map);
8009 isl_map_free(map);
8011 return in;
8014 int isl_map_is_bijective(__isl_keep isl_map *map)
8016 int sv;
8018 sv = isl_map_is_single_valued(map);
8019 if (sv < 0 || !sv)
8020 return sv;
8022 return isl_map_is_injective(map);
8025 int isl_set_is_singleton(__isl_keep isl_set *set)
8027 return isl_map_is_single_valued((isl_map *)set);
8030 int isl_map_is_translation(__isl_keep isl_map *map)
8032 int ok;
8033 isl_set *delta;
8035 delta = isl_map_deltas(isl_map_copy(map));
8036 ok = isl_set_is_singleton(delta);
8037 isl_set_free(delta);
8039 return ok;
8042 static int unique(isl_int *p, unsigned pos, unsigned len)
8044 if (isl_seq_first_non_zero(p, pos) != -1)
8045 return 0;
8046 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8047 return 0;
8048 return 1;
8051 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8053 int i, j;
8054 unsigned nvar;
8055 unsigned ovar;
8057 if (!bset)
8058 return -1;
8060 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8061 return 0;
8063 nvar = isl_basic_set_dim(bset, isl_dim_set);
8064 ovar = isl_dim_offset(bset->dim, isl_dim_set);
8065 for (j = 0; j < nvar; ++j) {
8066 int lower = 0, upper = 0;
8067 for (i = 0; i < bset->n_eq; ++i) {
8068 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8069 continue;
8070 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8071 return 0;
8072 break;
8074 if (i < bset->n_eq)
8075 continue;
8076 for (i = 0; i < bset->n_ineq; ++i) {
8077 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8078 continue;
8079 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8080 return 0;
8081 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8082 lower = 1;
8083 else
8084 upper = 1;
8086 if (!lower || !upper)
8087 return 0;
8090 return 1;
8093 int isl_set_is_box(__isl_keep isl_set *set)
8095 if (!set)
8096 return -1;
8097 if (set->n != 1)
8098 return 0;
8100 return isl_basic_set_is_box(set->p[0]);
8103 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8105 if (!bset)
8106 return -1;
8108 return isl_dim_is_wrapping(bset->dim);
8111 int isl_set_is_wrapping(__isl_keep isl_set *set)
8113 if (!set)
8114 return -1;
8116 return isl_dim_is_wrapping(set->dim);
8119 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8121 bmap = isl_basic_map_cow(bmap);
8122 if (!bmap)
8123 return NULL;
8125 bmap->dim = isl_dim_wrap(bmap->dim);
8126 if (!bmap->dim)
8127 goto error;
8129 bmap = isl_basic_map_finalize(bmap);
8131 return (isl_basic_set *)bmap;
8132 error:
8133 isl_basic_map_free(bmap);
8134 return NULL;
8137 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8139 int i;
8141 map = isl_map_cow(map);
8142 if (!map)
8143 return NULL;
8145 for (i = 0; i < map->n; ++i) {
8146 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8147 if (!map->p[i])
8148 goto error;
8150 map->dim = isl_dim_wrap(map->dim);
8151 if (!map->dim)
8152 goto error;
8154 return (isl_set *)map;
8155 error:
8156 isl_map_free(map);
8157 return NULL;
8160 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8162 bset = isl_basic_set_cow(bset);
8163 if (!bset)
8164 return NULL;
8166 bset->dim = isl_dim_unwrap(bset->dim);
8167 if (!bset->dim)
8168 goto error;
8170 bset = isl_basic_set_finalize(bset);
8172 return (isl_basic_map *)bset;
8173 error:
8174 isl_basic_set_free(bset);
8175 return NULL;
8178 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8180 int i;
8182 if (!set)
8183 return NULL;
8185 if (!isl_set_is_wrapping(set))
8186 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8187 goto error);
8189 set = isl_set_cow(set);
8190 if (!set)
8191 return NULL;
8193 for (i = 0; i < set->n; ++i) {
8194 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8195 if (!set->p[i])
8196 goto error;
8199 set->dim = isl_dim_unwrap(set->dim);
8200 if (!set->dim)
8201 goto error;
8203 return (isl_map *)set;
8204 error:
8205 isl_set_free(set);
8206 return NULL;
8209 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8210 enum isl_dim_type type)
8212 if (!bmap)
8213 return NULL;
8215 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8216 return bmap;
8218 bmap = isl_basic_map_cow(bmap);
8219 if (!bmap)
8220 return NULL;
8222 bmap->dim = isl_dim_reset(bmap->dim, type);
8223 if (!bmap->dim)
8224 goto error;
8226 bmap = isl_basic_map_finalize(bmap);
8228 return bmap;
8229 error:
8230 isl_basic_map_free(bmap);
8231 return NULL;
8234 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8235 enum isl_dim_type type)
8237 int i;
8239 if (!map)
8240 return NULL;
8242 if (!isl_dim_is_named_or_nested(map->dim, type))
8243 return map;
8245 map = isl_map_cow(map);
8246 if (!map)
8247 return NULL;
8249 for (i = 0; i < map->n; ++i) {
8250 map->p[i] = isl_basic_map_reset(map->p[i], type);
8251 if (!map->p[i])
8252 goto error;
8254 map->dim = isl_dim_reset(map->dim, type);
8255 if (!map->dim)
8256 goto error;
8258 return map;
8259 error:
8260 isl_map_free(map);
8261 return NULL;
8264 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8266 if (!bmap)
8267 return NULL;
8269 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8270 return bmap;
8272 bmap = isl_basic_map_cow(bmap);
8273 if (!bmap)
8274 return NULL;
8276 bmap->dim = isl_dim_flatten(bmap->dim);
8277 if (!bmap->dim)
8278 goto error;
8280 bmap = isl_basic_map_finalize(bmap);
8282 return bmap;
8283 error:
8284 isl_basic_map_free(bmap);
8285 return NULL;
8288 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8290 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8293 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8295 int i;
8297 if (!map)
8298 return NULL;
8300 if (!map->dim->nested[0] && !map->dim->nested[1])
8301 return map;
8303 map = isl_map_cow(map);
8304 if (!map)
8305 return NULL;
8307 for (i = 0; i < map->n; ++i) {
8308 map->p[i] = isl_basic_map_flatten(map->p[i]);
8309 if (!map->p[i])
8310 goto error;
8312 map->dim = isl_dim_flatten(map->dim);
8313 if (!map->dim)
8314 goto error;
8316 return map;
8317 error:
8318 isl_map_free(map);
8319 return NULL;
8322 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8324 return (isl_set *)isl_map_flatten((isl_map *)set);
8327 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8329 isl_dim *dim, *flat_dim;
8330 isl_map *map;
8332 dim = isl_set_get_dim(set);
8333 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8334 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8335 map = isl_map_intersect_domain(map, set);
8337 return map;
8340 /* Reorder the dimensions of "bmap" according to the given dim_map
8341 * and set the dimension specification to "dim".
8343 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8344 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8346 isl_basic_map *res;
8348 bmap = isl_basic_map_cow(bmap);
8349 if (!bmap || !dim || !dim_map)
8350 goto error;
8352 res = isl_basic_map_alloc_dim(dim,
8353 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8354 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
8355 res = isl_basic_map_finalize(res);
8356 return res;
8357 error:
8358 free(dim_map);
8359 isl_basic_map_free(bmap);
8360 isl_dim_free(dim);
8361 return NULL;
8364 /* Reorder the dimensions of "map" according to given reordering.
8366 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8367 __isl_take isl_reordering *r)
8369 int i;
8370 struct isl_dim_map *dim_map;
8372 map = isl_map_cow(map);
8373 dim_map = isl_dim_map_from_reordering(r);
8374 if (!map || !r || !dim_map)
8375 goto error;
8377 for (i = 0; i < map->n; ++i) {
8378 struct isl_dim_map *dim_map_i;
8380 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
8382 map->p[i] = isl_basic_map_realign(map->p[i],
8383 isl_dim_copy(r->dim), dim_map_i);
8385 if (!map->p[i])
8386 goto error;
8389 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8391 isl_reordering_free(r);
8392 free(dim_map);
8393 return map;
8394 error:
8395 free(dim_map);
8396 isl_map_free(map);
8397 isl_reordering_free(r);
8398 return NULL;
8401 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8402 __isl_take isl_reordering *r)
8404 return (isl_set *)isl_map_realign((isl_map *)set, r);
8407 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
8408 __isl_take isl_dim *model)
8410 isl_ctx *ctx;
8412 if (!map || !model)
8413 goto error;
8415 ctx = isl_dim_get_ctx(model);
8416 if (!isl_dim_has_named_params(model))
8417 isl_die(ctx, isl_error_invalid,
8418 "model has unnamed parameters", goto error);
8419 if (!isl_dim_has_named_params(map->dim))
8420 isl_die(ctx, isl_error_invalid,
8421 "relation has unnamed parameters", goto error);
8422 if (!isl_dim_match(map->dim, isl_dim_param, model, isl_dim_param)) {
8423 isl_reordering *exp;
8425 model = isl_dim_drop(model, isl_dim_in,
8426 0, isl_dim_size(model, isl_dim_in));
8427 model = isl_dim_drop(model, isl_dim_out,
8428 0, isl_dim_size(model, isl_dim_out));
8429 exp = isl_parameter_alignment_reordering(map->dim, model);
8430 exp = isl_reordering_extend_dim(exp, isl_map_get_dim(map));
8431 map = isl_map_realign(map, exp);
8434 isl_dim_free(model);
8435 return map;
8436 error:
8437 isl_dim_free(model);
8438 isl_map_free(map);
8439 return NULL;
8442 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
8443 __isl_take isl_dim *model)
8445 return isl_map_align_params(set, model);
8448 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8449 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8450 enum isl_dim_type c2, enum isl_dim_type c3,
8451 enum isl_dim_type c4, enum isl_dim_type c5)
8453 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8454 struct isl_mat *mat;
8455 int i, j, k;
8456 int pos;
8458 if (!bmap)
8459 return NULL;
8460 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8461 isl_basic_map_total_dim(bmap) + 1);
8462 if (!mat)
8463 return NULL;
8464 for (i = 0; i < bmap->n_eq; ++i)
8465 for (j = 0, pos = 0; j < 5; ++j) {
8466 int off = isl_basic_map_offset(bmap, c[j]);
8467 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8468 isl_int_set(mat->row[i][pos],
8469 bmap->eq[i][off + k]);
8470 ++pos;
8474 return mat;
8477 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8478 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8479 enum isl_dim_type c2, enum isl_dim_type c3,
8480 enum isl_dim_type c4, enum isl_dim_type c5)
8482 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8483 struct isl_mat *mat;
8484 int i, j, k;
8485 int pos;
8487 if (!bmap)
8488 return NULL;
8489 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8490 isl_basic_map_total_dim(bmap) + 1);
8491 if (!mat)
8492 return NULL;
8493 for (i = 0; i < bmap->n_ineq; ++i)
8494 for (j = 0, pos = 0; j < 5; ++j) {
8495 int off = isl_basic_map_offset(bmap, c[j]);
8496 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8497 isl_int_set(mat->row[i][pos],
8498 bmap->ineq[i][off + k]);
8499 ++pos;
8503 return mat;
8506 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8507 __isl_take isl_dim *dim,
8508 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8509 enum isl_dim_type c2, enum isl_dim_type c3,
8510 enum isl_dim_type c4, enum isl_dim_type c5)
8512 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8513 isl_basic_map *bmap;
8514 unsigned total;
8515 unsigned extra;
8516 int i, j, k, l;
8517 int pos;
8519 if (!dim || !eq || !ineq)
8520 goto error;
8522 if (eq->n_col != ineq->n_col)
8523 isl_die(dim->ctx, isl_error_invalid,
8524 "equalities and inequalities matrices should have "
8525 "same number of columns", goto error);
8527 total = 1 + isl_dim_total(dim);
8529 if (eq->n_col < total)
8530 isl_die(dim->ctx, isl_error_invalid,
8531 "number of columns too small", goto error);
8533 extra = eq->n_col - total;
8535 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8536 eq->n_row, ineq->n_row);
8537 if (!bmap)
8538 goto error;
8539 for (i = 0; i < extra; ++i) {
8540 k = isl_basic_map_alloc_div(bmap);
8541 if (k < 0)
8542 goto error;
8543 isl_int_set_si(bmap->div[k][0], 0);
8545 for (i = 0; i < eq->n_row; ++i) {
8546 l = isl_basic_map_alloc_equality(bmap);
8547 if (l < 0)
8548 goto error;
8549 for (j = 0, pos = 0; j < 5; ++j) {
8550 int off = isl_basic_map_offset(bmap, c[j]);
8551 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8552 isl_int_set(bmap->eq[l][off + k],
8553 eq->row[i][pos]);
8554 ++pos;
8558 for (i = 0; i < ineq->n_row; ++i) {
8559 l = isl_basic_map_alloc_inequality(bmap);
8560 if (l < 0)
8561 goto error;
8562 for (j = 0, pos = 0; j < 5; ++j) {
8563 int off = isl_basic_map_offset(bmap, c[j]);
8564 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8565 isl_int_set(bmap->ineq[l][off + k],
8566 ineq->row[i][pos]);
8567 ++pos;
8572 isl_dim_free(dim);
8573 isl_mat_free(eq);
8574 isl_mat_free(ineq);
8576 return bmap;
8577 error:
8578 isl_dim_free(dim);
8579 isl_mat_free(eq);
8580 isl_mat_free(ineq);
8581 return NULL;
8584 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8585 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8586 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8588 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8589 c1, c2, c3, c4, isl_dim_in);
8592 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8593 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8594 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8596 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8597 c1, c2, c3, c4, isl_dim_in);
8600 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8601 __isl_take isl_dim *dim,
8602 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8603 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8605 return (isl_basic_set*)
8606 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8607 c1, c2, c3, c4, isl_dim_in);
8610 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8612 if (!bmap)
8613 return -1;
8615 return isl_dim_can_zip(bmap->dim);
8618 int isl_map_can_zip(__isl_keep isl_map *map)
8620 if (!map)
8621 return -1;
8623 return isl_dim_can_zip(map->dim);
8626 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8627 * (A -> C) -> (B -> D).
8629 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8631 unsigned pos;
8632 unsigned n1;
8633 unsigned n2;
8635 if (!bmap)
8636 return NULL;
8638 if (!isl_basic_map_can_zip(bmap))
8639 isl_die(bmap->ctx, isl_error_invalid,
8640 "basic map cannot be zipped", goto error);
8641 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8642 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8643 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8644 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8645 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8646 if (!bmap)
8647 return NULL;
8648 bmap->dim = isl_dim_zip(bmap->dim);
8649 if (!bmap->dim)
8650 goto error;
8651 return bmap;
8652 error:
8653 isl_basic_map_free(bmap);
8654 return NULL;
8657 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8658 * (A -> C) -> (B -> D).
8660 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8662 int i;
8664 if (!map)
8665 return NULL;
8667 if (!isl_map_can_zip(map))
8668 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8669 goto error);
8671 map = isl_map_cow(map);
8672 if (!map)
8673 return NULL;
8675 for (i = 0; i < map->n; ++i) {
8676 map->p[i] = isl_basic_map_zip(map->p[i]);
8677 if (!map->p[i])
8678 goto error;
8681 map->dim = isl_dim_zip(map->dim);
8682 if (!map->dim)
8683 goto error;
8685 return map;
8686 error:
8687 isl_map_free(map);
8688 return NULL;